a62224c0be2ae87d0e8295019640677eef8b4ee9
[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 struct 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
1224 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1225
1226 static void scan_partial_symbols (struct partial_die_info *,
1227 CORE_ADDR *, CORE_ADDR *,
1228 int, struct dwarf2_cu *);
1229
1230 static void add_partial_symbol (struct partial_die_info *,
1231 struct dwarf2_cu *);
1232
1233 static void add_partial_namespace (struct partial_die_info *pdi,
1234 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1235 int set_addrmap, struct dwarf2_cu *cu);
1236
1237 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1238 CORE_ADDR *highpc, int set_addrmap,
1239 struct dwarf2_cu *cu);
1240
1241 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1242 struct dwarf2_cu *cu);
1243
1244 static void add_partial_subprogram (struct partial_die_info *pdi,
1245 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1246 int need_pc, struct dwarf2_cu *cu);
1247
1248 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1249
1250 static struct partial_die_info *load_partial_dies
1251 (const struct die_reader_specs *, const gdb_byte *, int);
1252
1253 /* A pair of partial_die_info and compilation unit. */
1254 struct cu_partial_die_info
1255 {
1256 /* The compilation unit of the partial_die_info. */
1257 struct dwarf2_cu *cu;
1258 /* A partial_die_info. */
1259 struct partial_die_info *pdi;
1260
1261 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1262 : cu (cu),
1263 pdi (pdi)
1264 { /* Nothing. */ }
1265
1266 private:
1267 cu_partial_die_info () = delete;
1268 };
1269
1270 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1271 struct dwarf2_cu *);
1272
1273 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1274 struct attribute *, struct attr_abbrev *,
1275 const gdb_byte *, bool *need_reprocess);
1276
1277 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1278 struct attribute *attr);
1279
1280 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1281
1282 static sect_offset read_abbrev_offset
1283 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1284 struct dwarf2_section_info *, sect_offset);
1285
1286 static const char *read_indirect_string
1287 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1288 const struct comp_unit_head *, unsigned int *);
1289
1290 static const char *read_indirect_string_at_offset
1291 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1292
1293 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1294 const gdb_byte *,
1295 unsigned int *);
1296
1297 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1298 ULONGEST str_index);
1299
1300 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1301 ULONGEST str_index);
1302
1303 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1304
1305 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1306 struct dwarf2_cu *);
1307
1308 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1309 struct dwarf2_cu *cu);
1310
1311 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1312
1313 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1314 struct dwarf2_cu *cu);
1315
1316 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1317
1318 static struct die_info *die_specification (struct die_info *die,
1319 struct dwarf2_cu **);
1320
1321 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1322 struct dwarf2_cu *cu);
1323
1324 static void dwarf_decode_lines (struct line_header *, const char *,
1325 struct dwarf2_cu *, dwarf2_psymtab *,
1326 CORE_ADDR, int decode_mapping);
1327
1328 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1329 const char *);
1330
1331 static struct symbol *new_symbol (struct die_info *, struct type *,
1332 struct dwarf2_cu *, struct symbol * = NULL);
1333
1334 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1335 struct dwarf2_cu *);
1336
1337 static void dwarf2_const_value_attr (const struct attribute *attr,
1338 struct type *type,
1339 const char *name,
1340 struct obstack *obstack,
1341 struct dwarf2_cu *cu, LONGEST *value,
1342 const gdb_byte **bytes,
1343 struct dwarf2_locexpr_baton **baton);
1344
1345 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1346
1347 static int need_gnat_info (struct dwarf2_cu *);
1348
1349 static struct type *die_descriptive_type (struct die_info *,
1350 struct dwarf2_cu *);
1351
1352 static void set_descriptive_type (struct type *, struct die_info *,
1353 struct dwarf2_cu *);
1354
1355 static struct type *die_containing_type (struct die_info *,
1356 struct dwarf2_cu *);
1357
1358 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1359 struct dwarf2_cu *);
1360
1361 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1362
1363 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1364
1365 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1366
1367 static char *typename_concat (struct obstack *obs, const char *prefix,
1368 const char *suffix, int physname,
1369 struct dwarf2_cu *cu);
1370
1371 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1372
1373 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1374
1375 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1376
1377 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1378
1379 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1380
1381 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1382
1383 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1384 struct dwarf2_cu *, dwarf2_psymtab *);
1385
1386 /* Return the .debug_loclists section to use for cu. */
1387 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1388
1389 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1390 values. Keep the items ordered with increasing constraints compliance. */
1391 enum pc_bounds_kind
1392 {
1393 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1394 PC_BOUNDS_NOT_PRESENT,
1395
1396 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1397 were present but they do not form a valid range of PC addresses. */
1398 PC_BOUNDS_INVALID,
1399
1400 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1401 PC_BOUNDS_RANGES,
1402
1403 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1404 PC_BOUNDS_HIGH_LOW,
1405 };
1406
1407 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1408 CORE_ADDR *, CORE_ADDR *,
1409 struct dwarf2_cu *,
1410 dwarf2_psymtab *);
1411
1412 static void get_scope_pc_bounds (struct die_info *,
1413 CORE_ADDR *, CORE_ADDR *,
1414 struct dwarf2_cu *);
1415
1416 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1417 CORE_ADDR, struct dwarf2_cu *);
1418
1419 static void dwarf2_add_field (struct field_info *, struct die_info *,
1420 struct dwarf2_cu *);
1421
1422 static void dwarf2_attach_fields_to_type (struct field_info *,
1423 struct type *, struct dwarf2_cu *);
1424
1425 static void dwarf2_add_member_fn (struct field_info *,
1426 struct die_info *, struct type *,
1427 struct dwarf2_cu *);
1428
1429 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1430 struct type *,
1431 struct dwarf2_cu *);
1432
1433 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1434
1435 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1436
1437 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1438
1439 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1440
1441 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1442
1443 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1444
1445 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1446
1447 static struct type *read_module_type (struct die_info *die,
1448 struct dwarf2_cu *cu);
1449
1450 static const char *namespace_name (struct die_info *die,
1451 int *is_anonymous, struct dwarf2_cu *);
1452
1453 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1454
1455 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1456 bool * = nullptr);
1457
1458 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1459 struct dwarf2_cu *);
1460
1461 static struct die_info *read_die_and_siblings_1
1462 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1463 struct die_info *);
1464
1465 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1466 const gdb_byte *info_ptr,
1467 const gdb_byte **new_info_ptr,
1468 struct die_info *parent);
1469
1470 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1471 struct die_info **, const gdb_byte *,
1472 int);
1473
1474 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1475 struct die_info **, const gdb_byte *);
1476
1477 static void process_die (struct die_info *, struct dwarf2_cu *);
1478
1479 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1480 struct objfile *);
1481
1482 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1483
1484 static const char *dwarf2_full_name (const char *name,
1485 struct die_info *die,
1486 struct dwarf2_cu *cu);
1487
1488 static const char *dwarf2_physname (const char *name, struct die_info *die,
1489 struct dwarf2_cu *cu);
1490
1491 static struct die_info *dwarf2_extension (struct die_info *die,
1492 struct dwarf2_cu **);
1493
1494 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1495
1496 static void dump_die_for_error (struct die_info *);
1497
1498 static void dump_die_1 (struct ui_file *, int level, int max_level,
1499 struct die_info *);
1500
1501 /*static*/ void dump_die (struct die_info *, int max_level);
1502
1503 static void store_in_ref_table (struct die_info *,
1504 struct dwarf2_cu *);
1505
1506 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1507 const struct attribute *,
1508 struct dwarf2_cu **);
1509
1510 static struct die_info *follow_die_ref (struct die_info *,
1511 const struct attribute *,
1512 struct dwarf2_cu **);
1513
1514 static struct die_info *follow_die_sig (struct die_info *,
1515 const struct attribute *,
1516 struct dwarf2_cu **);
1517
1518 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1519 struct dwarf2_cu *);
1520
1521 static struct type *get_DW_AT_signature_type (struct die_info *,
1522 const struct attribute *,
1523 struct dwarf2_cu *);
1524
1525 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1526 dwarf2_per_objfile *per_objfile);
1527
1528 static void read_signatured_type (signatured_type *sig_type,
1529 dwarf2_per_objfile *per_objfile);
1530
1531 static int attr_to_dynamic_prop (const struct attribute *attr,
1532 struct die_info *die, struct dwarf2_cu *cu,
1533 struct dynamic_prop *prop, struct type *type);
1534
1535 /* memory allocation interface */
1536
1537 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1538
1539 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1540
1541 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1542
1543 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1544 struct dwarf2_loclist_baton *baton,
1545 const struct attribute *attr);
1546
1547 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1548 struct symbol *sym,
1549 struct dwarf2_cu *cu,
1550 int is_block);
1551
1552 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1553 const gdb_byte *info_ptr,
1554 struct abbrev_info *abbrev);
1555
1556 static hashval_t partial_die_hash (const void *item);
1557
1558 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1559
1560 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1561 (sect_offset sect_off, unsigned int offset_in_dwz,
1562 struct dwarf2_per_objfile *dwarf2_per_objfile);
1563
1564 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1565 struct die_info *comp_unit_die,
1566 enum language pretend_language);
1567
1568 static struct type *set_die_type (struct die_info *, struct type *,
1569 struct dwarf2_cu *);
1570
1571 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1572
1573 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1574
1575 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1576 dwarf2_per_objfile *per_objfile,
1577 bool skip_partial,
1578 enum language pretend_language);
1579
1580 static void process_full_comp_unit (dwarf2_cu *cu,
1581 enum language pretend_language);
1582
1583 static void process_full_type_unit (dwarf2_cu *cu,
1584 enum language pretend_language);
1585
1586 static void dwarf2_add_dependence (struct dwarf2_cu *,
1587 struct dwarf2_per_cu_data *);
1588
1589 static void dwarf2_mark (struct dwarf2_cu *);
1590
1591 static struct type *get_die_type_at_offset (sect_offset,
1592 dwarf2_per_cu_data *per_cu,
1593 dwarf2_per_objfile *per_objfile);
1594
1595 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1596
1597 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1598 dwarf2_per_objfile *per_objfile,
1599 enum language pretend_language);
1600
1601 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1602
1603 /* Class, the destructor of which frees all allocated queue entries. This
1604 will only have work to do if an error was thrown while processing the
1605 dwarf. If no error was thrown then the queue entries should have all
1606 been processed, and freed, as we went along. */
1607
1608 class dwarf2_queue_guard
1609 {
1610 public:
1611 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1612 : m_per_objfile (per_objfile)
1613 {
1614 }
1615
1616 /* Free any entries remaining on the queue. There should only be
1617 entries left if we hit an error while processing the dwarf. */
1618 ~dwarf2_queue_guard ()
1619 {
1620 /* Ensure that no memory is allocated by the queue. */
1621 std::queue<dwarf2_queue_item> empty;
1622 std::swap (m_per_objfile->per_bfd->queue, empty);
1623 }
1624
1625 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1626
1627 private:
1628 dwarf2_per_objfile *m_per_objfile;
1629 };
1630
1631 dwarf2_queue_item::~dwarf2_queue_item ()
1632 {
1633 /* Anything still marked queued is likely to be in an
1634 inconsistent state, so discard it. */
1635 if (per_cu->queued)
1636 {
1637 per_objfile->remove_cu (per_cu);
1638 per_cu->queued = 0;
1639 }
1640 }
1641
1642 /* The return type of find_file_and_directory. Note, the enclosed
1643 string pointers are only valid while this object is valid. */
1644
1645 struct file_and_directory
1646 {
1647 /* The filename. This is never NULL. */
1648 const char *name;
1649
1650 /* The compilation directory. NULL if not known. If we needed to
1651 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1652 points directly to the DW_AT_comp_dir string attribute owned by
1653 the obstack that owns the DIE. */
1654 const char *comp_dir;
1655
1656 /* If we needed to build a new string for comp_dir, this is what
1657 owns the storage. */
1658 std::string comp_dir_storage;
1659 };
1660
1661 static file_and_directory find_file_and_directory (struct die_info *die,
1662 struct dwarf2_cu *cu);
1663
1664 static htab_up allocate_signatured_type_table ();
1665
1666 static htab_up allocate_dwo_unit_table ();
1667
1668 static struct dwo_unit *lookup_dwo_unit_in_dwp
1669 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1670 struct dwp_file *dwp_file, const char *comp_dir,
1671 ULONGEST signature, int is_debug_types);
1672
1673 static struct dwp_file *get_dwp_file
1674 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1675
1676 static struct dwo_unit *lookup_dwo_comp_unit
1677 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1678 ULONGEST signature);
1679
1680 static struct dwo_unit *lookup_dwo_type_unit
1681 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1682
1683 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1684
1685 /* A unique pointer to a dwo_file. */
1686
1687 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1688
1689 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1690
1691 static void check_producer (struct dwarf2_cu *cu);
1692
1693 static void free_line_header_voidp (void *arg);
1694 \f
1695 /* Various complaints about symbol reading that don't abort the process. */
1696
1697 static void
1698 dwarf2_debug_line_missing_file_complaint (void)
1699 {
1700 complaint (_(".debug_line section has line data without a file"));
1701 }
1702
1703 static void
1704 dwarf2_debug_line_missing_end_sequence_complaint (void)
1705 {
1706 complaint (_(".debug_line section has line "
1707 "program sequence without an end"));
1708 }
1709
1710 static void
1711 dwarf2_complex_location_expr_complaint (void)
1712 {
1713 complaint (_("location expression too complex"));
1714 }
1715
1716 static void
1717 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1718 int arg3)
1719 {
1720 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1721 arg1, arg2, arg3);
1722 }
1723
1724 static void
1725 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1726 {
1727 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1728 arg1, arg2);
1729 }
1730
1731 /* Hash function for line_header_hash. */
1732
1733 static hashval_t
1734 line_header_hash (const struct line_header *ofs)
1735 {
1736 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1737 }
1738
1739 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1740
1741 static hashval_t
1742 line_header_hash_voidp (const void *item)
1743 {
1744 const struct line_header *ofs = (const struct line_header *) item;
1745
1746 return line_header_hash (ofs);
1747 }
1748
1749 /* Equality function for line_header_hash. */
1750
1751 static int
1752 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1753 {
1754 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1755 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1756
1757 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1758 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1759 }
1760
1761 \f
1762
1763 /* See declaration. */
1764
1765 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1766 bool can_copy_)
1767 : obfd (obfd),
1768 can_copy (can_copy_)
1769 {
1770 if (names == NULL)
1771 names = &dwarf2_elf_names;
1772
1773 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1774 locate_sections (obfd, sec, *names);
1775 }
1776
1777 dwarf2_per_bfd::~dwarf2_per_bfd ()
1778 {
1779 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1780 per_cu->imported_symtabs_free ();
1781
1782 for (signatured_type *sig_type : all_type_units)
1783 sig_type->per_cu.imported_symtabs_free ();
1784
1785 /* Everything else should be on this->obstack. */
1786 }
1787
1788 /* See read.h. */
1789
1790 void
1791 dwarf2_per_objfile::remove_all_cus ()
1792 {
1793 for (auto pair : m_dwarf2_cus)
1794 delete pair.second;
1795
1796 m_dwarf2_cus.clear ();
1797 }
1798
1799 /* A helper class that calls free_cached_comp_units on
1800 destruction. */
1801
1802 class free_cached_comp_units
1803 {
1804 public:
1805
1806 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1807 : m_per_objfile (per_objfile)
1808 {
1809 }
1810
1811 ~free_cached_comp_units ()
1812 {
1813 m_per_objfile->remove_all_cus ();
1814 }
1815
1816 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1817
1818 private:
1819
1820 dwarf2_per_objfile *m_per_objfile;
1821 };
1822
1823 /* See read.h. */
1824
1825 bool
1826 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1827 {
1828 gdb_assert (per_cu->index < this->m_symtabs.size ());
1829
1830 return this->m_symtabs[per_cu->index] != nullptr;
1831 }
1832
1833 /* See read.h. */
1834
1835 compunit_symtab *
1836 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1837 {
1838 gdb_assert (per_cu->index < this->m_symtabs.size ());
1839
1840 return this->m_symtabs[per_cu->index];
1841 }
1842
1843 /* See read.h. */
1844
1845 void
1846 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1847 compunit_symtab *symtab)
1848 {
1849 gdb_assert (per_cu->index < this->m_symtabs.size ());
1850 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1851
1852 this->m_symtabs[per_cu->index] = symtab;
1853 }
1854
1855 /* Try to locate the sections we need for DWARF 2 debugging
1856 information and return true if we have enough to do something.
1857 NAMES points to the dwarf2 section names, or is NULL if the standard
1858 ELF names are used. CAN_COPY is true for formats where symbol
1859 interposition is possible and so symbol values must follow copy
1860 relocation rules. */
1861
1862 int
1863 dwarf2_has_info (struct objfile *objfile,
1864 const struct dwarf2_debug_sections *names,
1865 bool can_copy)
1866 {
1867 if (objfile->flags & OBJF_READNEVER)
1868 return 0;
1869
1870 struct dwarf2_per_objfile *dwarf2_per_objfile
1871 = get_dwarf2_per_objfile (objfile);
1872
1873 if (dwarf2_per_objfile == NULL)
1874 {
1875 dwarf2_per_bfd *per_bfd;
1876
1877 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1878 doesn't require relocations and if there aren't partial symbols
1879 from some other reader. */
1880 if (!objfile_has_partial_symbols (objfile)
1881 && !gdb_bfd_requires_relocations (objfile->obfd))
1882 {
1883 /* See if one has been created for this BFD yet. */
1884 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1885
1886 if (per_bfd == nullptr)
1887 {
1888 /* No, create it now. */
1889 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1890 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1891 }
1892 }
1893 else
1894 {
1895 /* No sharing possible, create one specifically for this objfile. */
1896 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1897 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1898 }
1899
1900 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1901 }
1902
1903 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1904 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1905 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1906 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1907 }
1908
1909 /* When loading sections, we look either for uncompressed section or for
1910 compressed section names. */
1911
1912 static int
1913 section_is_p (const char *section_name,
1914 const struct dwarf2_section_names *names)
1915 {
1916 if (names->normal != NULL
1917 && strcmp (section_name, names->normal) == 0)
1918 return 1;
1919 if (names->compressed != NULL
1920 && strcmp (section_name, names->compressed) == 0)
1921 return 1;
1922 return 0;
1923 }
1924
1925 /* See declaration. */
1926
1927 void
1928 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1929 const dwarf2_debug_sections &names)
1930 {
1931 flagword aflag = bfd_section_flags (sectp);
1932
1933 if ((aflag & SEC_HAS_CONTENTS) == 0)
1934 {
1935 }
1936 else if (elf_section_data (sectp)->this_hdr.sh_size
1937 > bfd_get_file_size (abfd))
1938 {
1939 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1940 warning (_("Discarding section %s which has a section size (%s"
1941 ") larger than the file size [in module %s]"),
1942 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1943 bfd_get_filename (abfd));
1944 }
1945 else if (section_is_p (sectp->name, &names.info))
1946 {
1947 this->info.s.section = sectp;
1948 this->info.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.abbrev))
1951 {
1952 this->abbrev.s.section = sectp;
1953 this->abbrev.size = bfd_section_size (sectp);
1954 }
1955 else if (section_is_p (sectp->name, &names.line))
1956 {
1957 this->line.s.section = sectp;
1958 this->line.size = bfd_section_size (sectp);
1959 }
1960 else if (section_is_p (sectp->name, &names.loc))
1961 {
1962 this->loc.s.section = sectp;
1963 this->loc.size = bfd_section_size (sectp);
1964 }
1965 else if (section_is_p (sectp->name, &names.loclists))
1966 {
1967 this->loclists.s.section = sectp;
1968 this->loclists.size = bfd_section_size (sectp);
1969 }
1970 else if (section_is_p (sectp->name, &names.macinfo))
1971 {
1972 this->macinfo.s.section = sectp;
1973 this->macinfo.size = bfd_section_size (sectp);
1974 }
1975 else if (section_is_p (sectp->name, &names.macro))
1976 {
1977 this->macro.s.section = sectp;
1978 this->macro.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.str))
1981 {
1982 this->str.s.section = sectp;
1983 this->str.size = bfd_section_size (sectp);
1984 }
1985 else if (section_is_p (sectp->name, &names.str_offsets))
1986 {
1987 this->str_offsets.s.section = sectp;
1988 this->str_offsets.size = bfd_section_size (sectp);
1989 }
1990 else if (section_is_p (sectp->name, &names.line_str))
1991 {
1992 this->line_str.s.section = sectp;
1993 this->line_str.size = bfd_section_size (sectp);
1994 }
1995 else if (section_is_p (sectp->name, &names.addr))
1996 {
1997 this->addr.s.section = sectp;
1998 this->addr.size = bfd_section_size (sectp);
1999 }
2000 else if (section_is_p (sectp->name, &names.frame))
2001 {
2002 this->frame.s.section = sectp;
2003 this->frame.size = bfd_section_size (sectp);
2004 }
2005 else if (section_is_p (sectp->name, &names.eh_frame))
2006 {
2007 this->eh_frame.s.section = sectp;
2008 this->eh_frame.size = bfd_section_size (sectp);
2009 }
2010 else if (section_is_p (sectp->name, &names.ranges))
2011 {
2012 this->ranges.s.section = sectp;
2013 this->ranges.size = bfd_section_size (sectp);
2014 }
2015 else if (section_is_p (sectp->name, &names.rnglists))
2016 {
2017 this->rnglists.s.section = sectp;
2018 this->rnglists.size = bfd_section_size (sectp);
2019 }
2020 else if (section_is_p (sectp->name, &names.types))
2021 {
2022 struct dwarf2_section_info type_section;
2023
2024 memset (&type_section, 0, sizeof (type_section));
2025 type_section.s.section = sectp;
2026 type_section.size = bfd_section_size (sectp);
2027
2028 this->types.push_back (type_section);
2029 }
2030 else if (section_is_p (sectp->name, &names.gdb_index))
2031 {
2032 this->gdb_index.s.section = sectp;
2033 this->gdb_index.size = bfd_section_size (sectp);
2034 }
2035 else if (section_is_p (sectp->name, &names.debug_names))
2036 {
2037 this->debug_names.s.section = sectp;
2038 this->debug_names.size = bfd_section_size (sectp);
2039 }
2040 else if (section_is_p (sectp->name, &names.debug_aranges))
2041 {
2042 this->debug_aranges.s.section = sectp;
2043 this->debug_aranges.size = bfd_section_size (sectp);
2044 }
2045
2046 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2047 && bfd_section_vma (sectp) == 0)
2048 this->has_section_at_zero = true;
2049 }
2050
2051 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2052 SECTION_NAME. */
2053
2054 void
2055 dwarf2_get_section_info (struct objfile *objfile,
2056 enum dwarf2_section_enum sect,
2057 asection **sectp, const gdb_byte **bufp,
2058 bfd_size_type *sizep)
2059 {
2060 struct dwarf2_per_objfile *data = get_dwarf2_per_objfile (objfile);
2061 struct dwarf2_section_info *info;
2062
2063 /* We may see an objfile without any DWARF, in which case we just
2064 return nothing. */
2065 if (data == NULL)
2066 {
2067 *sectp = NULL;
2068 *bufp = NULL;
2069 *sizep = 0;
2070 return;
2071 }
2072 switch (sect)
2073 {
2074 case DWARF2_DEBUG_FRAME:
2075 info = &data->per_bfd->frame;
2076 break;
2077 case DWARF2_EH_FRAME:
2078 info = &data->per_bfd->eh_frame;
2079 break;
2080 default:
2081 gdb_assert_not_reached ("unexpected section");
2082 }
2083
2084 info->read (objfile);
2085
2086 *sectp = info->get_bfd_section ();
2087 *bufp = info->buffer;
2088 *sizep = info->size;
2089 }
2090
2091 /* A helper function to find the sections for a .dwz file. */
2092
2093 static void
2094 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2095 {
2096 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2097
2098 /* Note that we only support the standard ELF names, because .dwz
2099 is ELF-only (at the time of writing). */
2100 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2101 {
2102 dwz_file->abbrev.s.section = sectp;
2103 dwz_file->abbrev.size = bfd_section_size (sectp);
2104 }
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2106 {
2107 dwz_file->info.s.section = sectp;
2108 dwz_file->info.size = bfd_section_size (sectp);
2109 }
2110 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2111 {
2112 dwz_file->str.s.section = sectp;
2113 dwz_file->str.size = bfd_section_size (sectp);
2114 }
2115 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2116 {
2117 dwz_file->line.s.section = sectp;
2118 dwz_file->line.size = bfd_section_size (sectp);
2119 }
2120 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2121 {
2122 dwz_file->macro.s.section = sectp;
2123 dwz_file->macro.size = bfd_section_size (sectp);
2124 }
2125 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2126 {
2127 dwz_file->gdb_index.s.section = sectp;
2128 dwz_file->gdb_index.size = bfd_section_size (sectp);
2129 }
2130 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2131 {
2132 dwz_file->debug_names.s.section = sectp;
2133 dwz_file->debug_names.size = bfd_section_size (sectp);
2134 }
2135 }
2136
2137 /* See dwarf2read.h. */
2138
2139 struct dwz_file *
2140 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2141 {
2142 const char *filename;
2143 bfd_size_type buildid_len_arg;
2144 size_t buildid_len;
2145 bfd_byte *buildid;
2146
2147 if (per_bfd->dwz_file != NULL)
2148 return per_bfd->dwz_file.get ();
2149
2150 bfd_set_error (bfd_error_no_error);
2151 gdb::unique_xmalloc_ptr<char> data
2152 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2153 &buildid_len_arg, &buildid));
2154 if (data == NULL)
2155 {
2156 if (bfd_get_error () == bfd_error_no_error)
2157 return NULL;
2158 error (_("could not read '.gnu_debugaltlink' section: %s"),
2159 bfd_errmsg (bfd_get_error ()));
2160 }
2161
2162 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2163
2164 buildid_len = (size_t) buildid_len_arg;
2165
2166 filename = data.get ();
2167
2168 std::string abs_storage;
2169 if (!IS_ABSOLUTE_PATH (filename))
2170 {
2171 gdb::unique_xmalloc_ptr<char> abs
2172 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2173
2174 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2175 filename = abs_storage.c_str ();
2176 }
2177
2178 /* First try the file name given in the section. If that doesn't
2179 work, try to use the build-id instead. */
2180 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2181 if (dwz_bfd != NULL)
2182 {
2183 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2184 dwz_bfd.reset (nullptr);
2185 }
2186
2187 if (dwz_bfd == NULL)
2188 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2189
2190 if (dwz_bfd == nullptr)
2191 {
2192 gdb::unique_xmalloc_ptr<char> alt_filename;
2193 const char *origname = bfd_get_filename (per_bfd->obfd);
2194
2195 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2196 buildid_len,
2197 origname,
2198 &alt_filename));
2199
2200 if (fd.get () >= 0)
2201 {
2202 /* File successfully retrieved from server. */
2203 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2204
2205 if (dwz_bfd == nullptr)
2206 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2207 alt_filename.get ());
2208 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2209 dwz_bfd.reset (nullptr);
2210 }
2211 }
2212
2213 if (dwz_bfd == NULL)
2214 error (_("could not find '.gnu_debugaltlink' file for %s"),
2215 bfd_get_filename (per_bfd->obfd));
2216
2217 std::unique_ptr<struct dwz_file> result
2218 (new struct dwz_file (std::move (dwz_bfd)));
2219
2220 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2221 result.get ());
2222
2223 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2224 per_bfd->dwz_file = std::move (result);
2225 return per_bfd->dwz_file.get ();
2226 }
2227 \f
2228 /* DWARF quick_symbols_functions support. */
2229
2230 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2231 unique line tables, so we maintain a separate table of all .debug_line
2232 derived entries to support the sharing.
2233 All the quick functions need is the list of file names. We discard the
2234 line_header when we're done and don't need to record it here. */
2235 struct quick_file_names
2236 {
2237 /* The data used to construct the hash key. */
2238 struct stmt_list_hash hash;
2239
2240 /* The number of entries in file_names, real_names. */
2241 unsigned int num_file_names;
2242
2243 /* The file names from the line table, after being run through
2244 file_full_name. */
2245 const char **file_names;
2246
2247 /* The file names from the line table after being run through
2248 gdb_realpath. These are computed lazily. */
2249 const char **real_names;
2250 };
2251
2252 /* When using the index (and thus not using psymtabs), each CU has an
2253 object of this type. This is used to hold information needed by
2254 the various "quick" methods. */
2255 struct dwarf2_per_cu_quick_data
2256 {
2257 /* The file table. This can be NULL if there was no file table
2258 or it's currently not read in.
2259 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2260 struct quick_file_names *file_names;
2261
2262 /* A temporary mark bit used when iterating over all CUs in
2263 expand_symtabs_matching. */
2264 unsigned int mark : 1;
2265
2266 /* True if we've tried to read the file table and found there isn't one.
2267 There will be no point in trying to read it again next time. */
2268 unsigned int no_file_data : 1;
2269 };
2270
2271 /* Utility hash function for a stmt_list_hash. */
2272
2273 static hashval_t
2274 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2275 {
2276 hashval_t v = 0;
2277
2278 if (stmt_list_hash->dwo_unit != NULL)
2279 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2280 v += to_underlying (stmt_list_hash->line_sect_off);
2281 return v;
2282 }
2283
2284 /* Utility equality function for a stmt_list_hash. */
2285
2286 static int
2287 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2288 const struct stmt_list_hash *rhs)
2289 {
2290 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2291 return 0;
2292 if (lhs->dwo_unit != NULL
2293 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2294 return 0;
2295
2296 return lhs->line_sect_off == rhs->line_sect_off;
2297 }
2298
2299 /* Hash function for a quick_file_names. */
2300
2301 static hashval_t
2302 hash_file_name_entry (const void *e)
2303 {
2304 const struct quick_file_names *file_data
2305 = (const struct quick_file_names *) e;
2306
2307 return hash_stmt_list_entry (&file_data->hash);
2308 }
2309
2310 /* Equality function for a quick_file_names. */
2311
2312 static int
2313 eq_file_name_entry (const void *a, const void *b)
2314 {
2315 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2316 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2317
2318 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2319 }
2320
2321 /* Delete function for a quick_file_names. */
2322
2323 static void
2324 delete_file_name_entry (void *e)
2325 {
2326 struct quick_file_names *file_data = (struct quick_file_names *) e;
2327 int i;
2328
2329 for (i = 0; i < file_data->num_file_names; ++i)
2330 {
2331 xfree ((void*) file_data->file_names[i]);
2332 if (file_data->real_names)
2333 xfree ((void*) file_data->real_names[i]);
2334 }
2335
2336 /* The space for the struct itself lives on the obstack, so we don't
2337 free it here. */
2338 }
2339
2340 /* Create a quick_file_names hash table. */
2341
2342 static htab_up
2343 create_quick_file_names_table (unsigned int nr_initial_entries)
2344 {
2345 return htab_up (htab_create_alloc (nr_initial_entries,
2346 hash_file_name_entry, eq_file_name_entry,
2347 delete_file_name_entry, xcalloc, xfree));
2348 }
2349
2350 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2351 function is unrelated to symtabs, symtab would have to be created afterwards.
2352 You should call age_cached_comp_units after processing the CU. */
2353
2354 static dwarf2_cu *
2355 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2356 bool skip_partial)
2357 {
2358 if (per_cu->is_debug_types)
2359 load_full_type_unit (per_cu, per_objfile);
2360 else
2361 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2362
2363 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2364 if (cu == nullptr)
2365 return nullptr; /* Dummy CU. */
2366
2367 dwarf2_find_base_address (cu->dies, cu);
2368
2369 return cu;
2370 }
2371
2372 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2373
2374 static void
2375 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2376 dwarf2_per_objfile *dwarf2_per_objfile,
2377 bool skip_partial)
2378 {
2379 /* Skip type_unit_groups, reading the type units they contain
2380 is handled elsewhere. */
2381 if (per_cu->type_unit_group_p ())
2382 return;
2383
2384 /* The destructor of dwarf2_queue_guard frees any entries left on
2385 the queue. After this point we're guaranteed to leave this function
2386 with the dwarf queue empty. */
2387 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2388
2389 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2390 {
2391 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
2392 dwarf2_cu *cu = load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2393
2394 /* If we just loaded a CU from a DWO, and we're working with an index
2395 that may badly handle TUs, load all the TUs in that DWO as well.
2396 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2397 if (!per_cu->is_debug_types
2398 && cu != NULL
2399 && cu->dwo_unit != NULL
2400 && dwarf2_per_objfile->per_bfd->index_table != NULL
2401 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2402 /* DWP files aren't supported yet. */
2403 && get_dwp_file (dwarf2_per_objfile) == NULL)
2404 queue_and_load_all_dwo_tus (cu);
2405 }
2406
2407 process_queue (dwarf2_per_objfile);
2408
2409 /* Age the cache, releasing compilation units that have not
2410 been used recently. */
2411 dwarf2_per_objfile->age_comp_units ();
2412 }
2413
2414 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2415 the per-objfile for which this symtab is instantiated.
2416
2417 Returns the resulting symbol table. */
2418
2419 static struct compunit_symtab *
2420 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2421 dwarf2_per_objfile *dwarf2_per_objfile,
2422 bool skip_partial)
2423 {
2424 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2425
2426 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2427 {
2428 free_cached_comp_units freer (dwarf2_per_objfile);
2429 scoped_restore decrementer = increment_reading_symtab ();
2430 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2431 process_cu_includes (dwarf2_per_objfile);
2432 }
2433
2434 return dwarf2_per_objfile->get_symtab (per_cu);
2435 }
2436
2437 /* See declaration. */
2438
2439 dwarf2_per_cu_data *
2440 dwarf2_per_bfd::get_cutu (int index)
2441 {
2442 if (index >= this->all_comp_units.size ())
2443 {
2444 index -= this->all_comp_units.size ();
2445 gdb_assert (index < this->all_type_units.size ());
2446 return &this->all_type_units[index]->per_cu;
2447 }
2448
2449 return this->all_comp_units[index];
2450 }
2451
2452 /* See declaration. */
2453
2454 dwarf2_per_cu_data *
2455 dwarf2_per_bfd::get_cu (int index)
2456 {
2457 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2458
2459 return this->all_comp_units[index];
2460 }
2461
2462 /* See declaration. */
2463
2464 signatured_type *
2465 dwarf2_per_bfd::get_tu (int index)
2466 {
2467 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2468
2469 return this->all_type_units[index];
2470 }
2471
2472 /* See read.h. */
2473
2474 dwarf2_per_cu_data *
2475 dwarf2_per_bfd::allocate_per_cu ()
2476 {
2477 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2478 result->per_bfd = this;
2479 result->index = m_num_psymtabs++;
2480 return result;
2481 }
2482
2483 /* See read.h. */
2484
2485 signatured_type *
2486 dwarf2_per_bfd::allocate_signatured_type ()
2487 {
2488 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2489 result->per_cu.per_bfd = this;
2490 result->per_cu.index = m_num_psymtabs++;
2491 return result;
2492 }
2493
2494 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2495 obstack, and constructed with the specified field values. */
2496
2497 static dwarf2_per_cu_data *
2498 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2499 struct dwarf2_section_info *section,
2500 int is_dwz,
2501 sect_offset sect_off, ULONGEST length)
2502 {
2503 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2504 the_cu->sect_off = sect_off;
2505 the_cu->length = length;
2506 the_cu->section = section;
2507 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2508 struct dwarf2_per_cu_quick_data);
2509 the_cu->is_dwz = is_dwz;
2510 return the_cu;
2511 }
2512
2513 /* A helper for create_cus_from_index that handles a given list of
2514 CUs. */
2515
2516 static void
2517 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2518 const gdb_byte *cu_list, offset_type n_elements,
2519 struct dwarf2_section_info *section,
2520 int is_dwz)
2521 {
2522 for (offset_type i = 0; i < n_elements; i += 2)
2523 {
2524 gdb_static_assert (sizeof (ULONGEST) >= 8);
2525
2526 sect_offset sect_off
2527 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2528 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2529 cu_list += 2 * 8;
2530
2531 dwarf2_per_cu_data *per_cu
2532 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2533 length);
2534 per_bfd->all_comp_units.push_back (per_cu);
2535 }
2536 }
2537
2538 /* Read the CU list from the mapped index, and use it to create all
2539 the CU objects for PER_BFD. */
2540
2541 static void
2542 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2543 const gdb_byte *cu_list, offset_type cu_list_elements,
2544 const gdb_byte *dwz_list, offset_type dwz_elements)
2545 {
2546 gdb_assert (per_bfd->all_comp_units.empty ());
2547 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2548
2549 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2550 &per_bfd->info, 0);
2551
2552 if (dwz_elements == 0)
2553 return;
2554
2555 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2556 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2557 &dwz->info, 1);
2558 }
2559
2560 /* Create the signatured type hash table from the index. */
2561
2562 static void
2563 create_signatured_type_table_from_index
2564 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2565 const gdb_byte *bytes, offset_type elements)
2566 {
2567 gdb_assert (per_bfd->all_type_units.empty ());
2568 per_bfd->all_type_units.reserve (elements / 3);
2569
2570 htab_up sig_types_hash = allocate_signatured_type_table ();
2571
2572 for (offset_type i = 0; i < elements; i += 3)
2573 {
2574 struct signatured_type *sig_type;
2575 ULONGEST signature;
2576 void **slot;
2577 cu_offset type_offset_in_tu;
2578
2579 gdb_static_assert (sizeof (ULONGEST) >= 8);
2580 sect_offset sect_off
2581 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2582 type_offset_in_tu
2583 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2584 BFD_ENDIAN_LITTLE);
2585 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2586 bytes += 3 * 8;
2587
2588 sig_type = per_bfd->allocate_signatured_type ();
2589 sig_type->signature = signature;
2590 sig_type->type_offset_in_tu = type_offset_in_tu;
2591 sig_type->per_cu.is_debug_types = 1;
2592 sig_type->per_cu.section = section;
2593 sig_type->per_cu.sect_off = sect_off;
2594 sig_type->per_cu.v.quick
2595 = OBSTACK_ZALLOC (&per_bfd->obstack,
2596 struct dwarf2_per_cu_quick_data);
2597
2598 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2599 *slot = sig_type;
2600
2601 per_bfd->all_type_units.push_back (sig_type);
2602 }
2603
2604 per_bfd->signatured_types = std::move (sig_types_hash);
2605 }
2606
2607 /* Create the signatured type hash table from .debug_names. */
2608
2609 static void
2610 create_signatured_type_table_from_debug_names
2611 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2612 const mapped_debug_names &map,
2613 struct dwarf2_section_info *section,
2614 struct dwarf2_section_info *abbrev_section)
2615 {
2616 struct objfile *objfile = dwarf2_per_objfile->objfile;
2617
2618 section->read (objfile);
2619 abbrev_section->read (objfile);
2620
2621 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2622 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2623
2624 htab_up sig_types_hash = allocate_signatured_type_table ();
2625
2626 for (uint32_t i = 0; i < map.tu_count; ++i)
2627 {
2628 struct signatured_type *sig_type;
2629 void **slot;
2630
2631 sect_offset sect_off
2632 = (sect_offset) (extract_unsigned_integer
2633 (map.tu_table_reordered + i * map.offset_size,
2634 map.offset_size,
2635 map.dwarf5_byte_order));
2636
2637 comp_unit_head cu_header;
2638 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2639 abbrev_section,
2640 section->buffer + to_underlying (sect_off),
2641 rcuh_kind::TYPE);
2642
2643 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2644 sig_type->signature = cu_header.signature;
2645 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2646 sig_type->per_cu.is_debug_types = 1;
2647 sig_type->per_cu.section = section;
2648 sig_type->per_cu.sect_off = sect_off;
2649 sig_type->per_cu.v.quick
2650 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2651 struct dwarf2_per_cu_quick_data);
2652
2653 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2654 *slot = sig_type;
2655
2656 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2657 }
2658
2659 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2660 }
2661
2662 /* Read the address map data from the mapped index, and use it to
2663 populate the objfile's psymtabs_addrmap. */
2664
2665 static void
2666 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2667 struct mapped_index *index)
2668 {
2669 struct objfile *objfile = dwarf2_per_objfile->objfile;
2670 struct gdbarch *gdbarch = objfile->arch ();
2671 const gdb_byte *iter, *end;
2672 struct addrmap *mutable_map;
2673 CORE_ADDR baseaddr;
2674
2675 auto_obstack temp_obstack;
2676
2677 mutable_map = addrmap_create_mutable (&temp_obstack);
2678
2679 iter = index->address_table.data ();
2680 end = iter + index->address_table.size ();
2681
2682 baseaddr = objfile->text_section_offset ();
2683
2684 while (iter < end)
2685 {
2686 ULONGEST hi, lo, cu_index;
2687 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2688 iter += 8;
2689 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2690 iter += 8;
2691 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2692 iter += 4;
2693
2694 if (lo > hi)
2695 {
2696 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2697 hex_string (lo), hex_string (hi));
2698 continue;
2699 }
2700
2701 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2702 {
2703 complaint (_(".gdb_index address table has invalid CU number %u"),
2704 (unsigned) cu_index);
2705 continue;
2706 }
2707
2708 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2709 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2710 addrmap_set_empty (mutable_map, lo, hi - 1,
2711 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2712 }
2713
2714 objfile->partial_symtabs->psymtabs_addrmap
2715 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2716 }
2717
2718 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2719 populate the objfile's psymtabs_addrmap. */
2720
2721 static void
2722 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2723 struct dwarf2_section_info *section)
2724 {
2725 struct objfile *objfile = dwarf2_per_objfile->objfile;
2726 bfd *abfd = objfile->obfd;
2727 struct gdbarch *gdbarch = objfile->arch ();
2728 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2729
2730 auto_obstack temp_obstack;
2731 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2732
2733 std::unordered_map<sect_offset,
2734 dwarf2_per_cu_data *,
2735 gdb::hash_enum<sect_offset>>
2736 debug_info_offset_to_per_cu;
2737 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2738 {
2739 const auto insertpair
2740 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2741 if (!insertpair.second)
2742 {
2743 warning (_("Section .debug_aranges in %s has duplicate "
2744 "debug_info_offset %s, ignoring .debug_aranges."),
2745 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2746 return;
2747 }
2748 }
2749
2750 section->read (objfile);
2751
2752 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2753
2754 const gdb_byte *addr = section->buffer;
2755
2756 while (addr < section->buffer + section->size)
2757 {
2758 const gdb_byte *const entry_addr = addr;
2759 unsigned int bytes_read;
2760
2761 const LONGEST entry_length = read_initial_length (abfd, addr,
2762 &bytes_read);
2763 addr += bytes_read;
2764
2765 const gdb_byte *const entry_end = addr + entry_length;
2766 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2767 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2768 if (addr + entry_length > section->buffer + section->size)
2769 {
2770 warning (_("Section .debug_aranges in %s entry at offset %s "
2771 "length %s exceeds section length %s, "
2772 "ignoring .debug_aranges."),
2773 objfile_name (objfile),
2774 plongest (entry_addr - section->buffer),
2775 plongest (bytes_read + entry_length),
2776 pulongest (section->size));
2777 return;
2778 }
2779
2780 /* The version number. */
2781 const uint16_t version = read_2_bytes (abfd, addr);
2782 addr += 2;
2783 if (version != 2)
2784 {
2785 warning (_("Section .debug_aranges in %s entry at offset %s "
2786 "has unsupported version %d, ignoring .debug_aranges."),
2787 objfile_name (objfile),
2788 plongest (entry_addr - section->buffer), version);
2789 return;
2790 }
2791
2792 const uint64_t debug_info_offset
2793 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2794 addr += offset_size;
2795 const auto per_cu_it
2796 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2797 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2798 {
2799 warning (_("Section .debug_aranges in %s entry at offset %s "
2800 "debug_info_offset %s does not exists, "
2801 "ignoring .debug_aranges."),
2802 objfile_name (objfile),
2803 plongest (entry_addr - section->buffer),
2804 pulongest (debug_info_offset));
2805 return;
2806 }
2807 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2808
2809 const uint8_t address_size = *addr++;
2810 if (address_size < 1 || address_size > 8)
2811 {
2812 warning (_("Section .debug_aranges in %s entry at offset %s "
2813 "address_size %u is invalid, ignoring .debug_aranges."),
2814 objfile_name (objfile),
2815 plongest (entry_addr - section->buffer), address_size);
2816 return;
2817 }
2818
2819 const uint8_t segment_selector_size = *addr++;
2820 if (segment_selector_size != 0)
2821 {
2822 warning (_("Section .debug_aranges in %s entry at offset %s "
2823 "segment_selector_size %u is not supported, "
2824 "ignoring .debug_aranges."),
2825 objfile_name (objfile),
2826 plongest (entry_addr - section->buffer),
2827 segment_selector_size);
2828 return;
2829 }
2830
2831 /* Must pad to an alignment boundary that is twice the address
2832 size. It is undocumented by the DWARF standard but GCC does
2833 use it. */
2834 for (size_t padding = ((-(addr - section->buffer))
2835 & (2 * address_size - 1));
2836 padding > 0; padding--)
2837 if (*addr++ != 0)
2838 {
2839 warning (_("Section .debug_aranges in %s entry at offset %s "
2840 "padding is not zero, ignoring .debug_aranges."),
2841 objfile_name (objfile),
2842 plongest (entry_addr - section->buffer));
2843 return;
2844 }
2845
2846 for (;;)
2847 {
2848 if (addr + 2 * address_size > entry_end)
2849 {
2850 warning (_("Section .debug_aranges in %s entry at offset %s "
2851 "address list is not properly terminated, "
2852 "ignoring .debug_aranges."),
2853 objfile_name (objfile),
2854 plongest (entry_addr - section->buffer));
2855 return;
2856 }
2857 ULONGEST start = extract_unsigned_integer (addr, address_size,
2858 dwarf5_byte_order);
2859 addr += address_size;
2860 ULONGEST length = extract_unsigned_integer (addr, address_size,
2861 dwarf5_byte_order);
2862 addr += address_size;
2863 if (start == 0 && length == 0)
2864 break;
2865 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2866 {
2867 /* Symbol was eliminated due to a COMDAT group. */
2868 continue;
2869 }
2870 ULONGEST end = start + length;
2871 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2872 - baseaddr);
2873 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2874 - baseaddr);
2875 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2876 }
2877 }
2878
2879 objfile->partial_symtabs->psymtabs_addrmap
2880 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2881 }
2882
2883 /* Find a slot in the mapped index INDEX for the object named NAME.
2884 If NAME is found, set *VEC_OUT to point to the CU vector in the
2885 constant pool and return true. If NAME cannot be found, return
2886 false. */
2887
2888 static bool
2889 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2890 offset_type **vec_out)
2891 {
2892 offset_type hash;
2893 offset_type slot, step;
2894 int (*cmp) (const char *, const char *);
2895
2896 gdb::unique_xmalloc_ptr<char> without_params;
2897 if (current_language->la_language == language_cplus
2898 || current_language->la_language == language_fortran
2899 || current_language->la_language == language_d)
2900 {
2901 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2902 not contain any. */
2903
2904 if (strchr (name, '(') != NULL)
2905 {
2906 without_params = cp_remove_params (name);
2907
2908 if (without_params != NULL)
2909 name = without_params.get ();
2910 }
2911 }
2912
2913 /* Index version 4 did not support case insensitive searches. But the
2914 indices for case insensitive languages are built in lowercase, therefore
2915 simulate our NAME being searched is also lowercased. */
2916 hash = mapped_index_string_hash ((index->version == 4
2917 && case_sensitivity == case_sensitive_off
2918 ? 5 : index->version),
2919 name);
2920
2921 slot = hash & (index->symbol_table.size () - 1);
2922 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2923 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2924
2925 for (;;)
2926 {
2927 const char *str;
2928
2929 const auto &bucket = index->symbol_table[slot];
2930 if (bucket.name == 0 && bucket.vec == 0)
2931 return false;
2932
2933 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2934 if (!cmp (name, str))
2935 {
2936 *vec_out = (offset_type *) (index->constant_pool
2937 + MAYBE_SWAP (bucket.vec));
2938 return true;
2939 }
2940
2941 slot = (slot + step) & (index->symbol_table.size () - 1);
2942 }
2943 }
2944
2945 /* A helper function that reads the .gdb_index from BUFFER and fills
2946 in MAP. FILENAME is the name of the file containing the data;
2947 it is used for error reporting. DEPRECATED_OK is true if it is
2948 ok to use deprecated sections.
2949
2950 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2951 out parameters that are filled in with information about the CU and
2952 TU lists in the section.
2953
2954 Returns true if all went well, false otherwise. */
2955
2956 static bool
2957 read_gdb_index_from_buffer (const char *filename,
2958 bool deprecated_ok,
2959 gdb::array_view<const gdb_byte> buffer,
2960 struct mapped_index *map,
2961 const gdb_byte **cu_list,
2962 offset_type *cu_list_elements,
2963 const gdb_byte **types_list,
2964 offset_type *types_list_elements)
2965 {
2966 const gdb_byte *addr = &buffer[0];
2967
2968 /* Version check. */
2969 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2970 /* Versions earlier than 3 emitted every copy of a psymbol. This
2971 causes the index to behave very poorly for certain requests. Version 3
2972 contained incomplete addrmap. So, it seems better to just ignore such
2973 indices. */
2974 if (version < 4)
2975 {
2976 static int warning_printed = 0;
2977 if (!warning_printed)
2978 {
2979 warning (_("Skipping obsolete .gdb_index section in %s."),
2980 filename);
2981 warning_printed = 1;
2982 }
2983 return 0;
2984 }
2985 /* Index version 4 uses a different hash function than index version
2986 5 and later.
2987
2988 Versions earlier than 6 did not emit psymbols for inlined
2989 functions. Using these files will cause GDB not to be able to
2990 set breakpoints on inlined functions by name, so we ignore these
2991 indices unless the user has done
2992 "set use-deprecated-index-sections on". */
2993 if (version < 6 && !deprecated_ok)
2994 {
2995 static int warning_printed = 0;
2996 if (!warning_printed)
2997 {
2998 warning (_("\
2999 Skipping deprecated .gdb_index section in %s.\n\
3000 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3001 to use the section anyway."),
3002 filename);
3003 warning_printed = 1;
3004 }
3005 return 0;
3006 }
3007 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3008 of the TU (for symbols coming from TUs),
3009 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3010 Plus gold-generated indices can have duplicate entries for global symbols,
3011 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3012 These are just performance bugs, and we can't distinguish gdb-generated
3013 indices from gold-generated ones, so issue no warning here. */
3014
3015 /* Indexes with higher version than the one supported by GDB may be no
3016 longer backward compatible. */
3017 if (version > 8)
3018 return 0;
3019
3020 map->version = version;
3021
3022 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3023
3024 int i = 0;
3025 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3026 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3027 / 8);
3028 ++i;
3029
3030 *types_list = addr + MAYBE_SWAP (metadata[i]);
3031 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3032 - MAYBE_SWAP (metadata[i]))
3033 / 8);
3034 ++i;
3035
3036 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3037 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3038 map->address_table
3039 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3040 ++i;
3041
3042 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3043 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3044 map->symbol_table
3045 = gdb::array_view<mapped_index::symbol_table_slot>
3046 ((mapped_index::symbol_table_slot *) symbol_table,
3047 (mapped_index::symbol_table_slot *) symbol_table_end);
3048
3049 ++i;
3050 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3051
3052 return 1;
3053 }
3054
3055 /* Callback types for dwarf2_read_gdb_index. */
3056
3057 typedef gdb::function_view
3058 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3059 get_gdb_index_contents_ftype;
3060 typedef gdb::function_view
3061 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3062 get_gdb_index_contents_dwz_ftype;
3063
3064 /* Read .gdb_index. If everything went ok, initialize the "quick"
3065 elements of all the CUs and return 1. Otherwise, return 0. */
3066
3067 static int
3068 dwarf2_read_gdb_index
3069 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3070 get_gdb_index_contents_ftype get_gdb_index_contents,
3071 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3072 {
3073 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3074 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3075 struct dwz_file *dwz;
3076 struct objfile *objfile = dwarf2_per_objfile->objfile;
3077
3078 gdb::array_view<const gdb_byte> main_index_contents
3079 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3080
3081 if (main_index_contents.empty ())
3082 return 0;
3083
3084 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3085 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3086 use_deprecated_index_sections,
3087 main_index_contents, map.get (), &cu_list,
3088 &cu_list_elements, &types_list,
3089 &types_list_elements))
3090 return 0;
3091
3092 /* Don't use the index if it's empty. */
3093 if (map->symbol_table.empty ())
3094 return 0;
3095
3096 /* If there is a .dwz file, read it so we can get its CU list as
3097 well. */
3098 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3099 if (dwz != NULL)
3100 {
3101 struct mapped_index dwz_map;
3102 const gdb_byte *dwz_types_ignore;
3103 offset_type dwz_types_elements_ignore;
3104
3105 gdb::array_view<const gdb_byte> dwz_index_content
3106 = get_gdb_index_contents_dwz (objfile, dwz);
3107
3108 if (dwz_index_content.empty ())
3109 return 0;
3110
3111 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3112 1, dwz_index_content, &dwz_map,
3113 &dwz_list, &dwz_list_elements,
3114 &dwz_types_ignore,
3115 &dwz_types_elements_ignore))
3116 {
3117 warning (_("could not read '.gdb_index' section from %s; skipping"),
3118 bfd_get_filename (dwz->dwz_bfd.get ()));
3119 return 0;
3120 }
3121 }
3122
3123 create_cus_from_index (dwarf2_per_objfile->per_bfd, cu_list, cu_list_elements,
3124 dwz_list, dwz_list_elements);
3125
3126 if (types_list_elements)
3127 {
3128 /* We can only handle a single .debug_types when we have an
3129 index. */
3130 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3131 return 0;
3132
3133 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3134
3135 create_signatured_type_table_from_index (dwarf2_per_objfile->per_bfd,
3136 section, types_list,
3137 types_list_elements);
3138 }
3139
3140 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3141
3142 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3143 dwarf2_per_objfile->per_bfd->using_index = 1;
3144 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3145 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3146
3147 return 1;
3148 }
3149
3150 /* die_reader_func for dw2_get_file_names. */
3151
3152 static void
3153 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3154 const gdb_byte *info_ptr,
3155 struct die_info *comp_unit_die)
3156 {
3157 struct dwarf2_cu *cu = reader->cu;
3158 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3159 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3160 struct dwarf2_per_cu_data *lh_cu;
3161 struct attribute *attr;
3162 void **slot;
3163 struct quick_file_names *qfn;
3164
3165 gdb_assert (! this_cu->is_debug_types);
3166
3167 /* Our callers never want to match partial units -- instead they
3168 will match the enclosing full CU. */
3169 if (comp_unit_die->tag == DW_TAG_partial_unit)
3170 {
3171 this_cu->v.quick->no_file_data = 1;
3172 return;
3173 }
3174
3175 lh_cu = this_cu;
3176 slot = NULL;
3177
3178 line_header_up lh;
3179 sect_offset line_offset {};
3180
3181 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3182 if (attr != nullptr)
3183 {
3184 struct quick_file_names find_entry;
3185
3186 line_offset = (sect_offset) DW_UNSND (attr);
3187
3188 /* We may have already read in this line header (TU line header sharing).
3189 If we have we're done. */
3190 find_entry.hash.dwo_unit = cu->dwo_unit;
3191 find_entry.hash.line_sect_off = line_offset;
3192 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3193 &find_entry, INSERT);
3194 if (*slot != NULL)
3195 {
3196 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3197 return;
3198 }
3199
3200 lh = dwarf_decode_line_header (line_offset, cu);
3201 }
3202 if (lh == NULL)
3203 {
3204 lh_cu->v.quick->no_file_data = 1;
3205 return;
3206 }
3207
3208 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3209 qfn->hash.dwo_unit = cu->dwo_unit;
3210 qfn->hash.line_sect_off = line_offset;
3211 gdb_assert (slot != NULL);
3212 *slot = qfn;
3213
3214 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3215
3216 int offset = 0;
3217 if (strcmp (fnd.name, "<unknown>") != 0)
3218 ++offset;
3219
3220 qfn->num_file_names = offset + lh->file_names_size ();
3221 qfn->file_names =
3222 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3223 qfn->num_file_names);
3224 if (offset != 0)
3225 qfn->file_names[0] = xstrdup (fnd.name);
3226 for (int i = 0; i < lh->file_names_size (); ++i)
3227 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3228 fnd.comp_dir).release ();
3229 qfn->real_names = NULL;
3230
3231 lh_cu->v.quick->file_names = qfn;
3232 }
3233
3234 /* A helper for the "quick" functions which attempts to read the line
3235 table for THIS_CU. */
3236
3237 static struct quick_file_names *
3238 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3239 dwarf2_per_objfile *per_objfile)
3240 {
3241 /* This should never be called for TUs. */
3242 gdb_assert (! this_cu->is_debug_types);
3243 /* Nor type unit groups. */
3244 gdb_assert (! this_cu->type_unit_group_p ());
3245
3246 if (this_cu->v.quick->file_names != NULL)
3247 return this_cu->v.quick->file_names;
3248 /* If we know there is no line data, no point in looking again. */
3249 if (this_cu->v.quick->no_file_data)
3250 return NULL;
3251
3252 cutu_reader reader (this_cu, per_objfile);
3253 if (!reader.dummy_p)
3254 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3255
3256 if (this_cu->v.quick->no_file_data)
3257 return NULL;
3258 return this_cu->v.quick->file_names;
3259 }
3260
3261 /* A helper for the "quick" functions which computes and caches the
3262 real path for a given file name from the line table. */
3263
3264 static const char *
3265 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3266 struct quick_file_names *qfn, int index)
3267 {
3268 if (qfn->real_names == NULL)
3269 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3270 qfn->num_file_names, const char *);
3271
3272 if (qfn->real_names[index] == NULL)
3273 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3274
3275 return qfn->real_names[index];
3276 }
3277
3278 static struct symtab *
3279 dw2_find_last_source_symtab (struct objfile *objfile)
3280 {
3281 struct dwarf2_per_objfile *dwarf2_per_objfile
3282 = get_dwarf2_per_objfile (objfile);
3283 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3284 compunit_symtab *cust
3285 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3286
3287 if (cust == NULL)
3288 return NULL;
3289
3290 return compunit_primary_filetab (cust);
3291 }
3292
3293 /* Traversal function for dw2_forget_cached_source_info. */
3294
3295 static int
3296 dw2_free_cached_file_names (void **slot, void *info)
3297 {
3298 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3299
3300 if (file_data->real_names)
3301 {
3302 int i;
3303
3304 for (i = 0; i < file_data->num_file_names; ++i)
3305 {
3306 xfree ((void*) file_data->real_names[i]);
3307 file_data->real_names[i] = NULL;
3308 }
3309 }
3310
3311 return 1;
3312 }
3313
3314 static void
3315 dw2_forget_cached_source_info (struct objfile *objfile)
3316 {
3317 struct dwarf2_per_objfile *dwarf2_per_objfile
3318 = get_dwarf2_per_objfile (objfile);
3319
3320 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3321 dw2_free_cached_file_names, NULL);
3322 }
3323
3324 /* Helper function for dw2_map_symtabs_matching_filename that expands
3325 the symtabs and calls the iterator. */
3326
3327 static int
3328 dw2_map_expand_apply (struct objfile *objfile,
3329 struct dwarf2_per_cu_data *per_cu,
3330 const char *name, const char *real_path,
3331 gdb::function_view<bool (symtab *)> callback)
3332 {
3333 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3334
3335 /* Don't visit already-expanded CUs. */
3336 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3337 if (per_objfile->symtab_set_p (per_cu))
3338 return 0;
3339
3340 /* This may expand more than one symtab, and we want to iterate over
3341 all of them. */
3342 dw2_instantiate_symtab (per_cu, per_objfile, false);
3343
3344 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3345 last_made, callback);
3346 }
3347
3348 /* Implementation of the map_symtabs_matching_filename method. */
3349
3350 static bool
3351 dw2_map_symtabs_matching_filename
3352 (struct objfile *objfile, const char *name, const char *real_path,
3353 gdb::function_view<bool (symtab *)> callback)
3354 {
3355 const char *name_basename = lbasename (name);
3356 struct dwarf2_per_objfile *dwarf2_per_objfile
3357 = get_dwarf2_per_objfile (objfile);
3358
3359 /* The rule is CUs specify all the files, including those used by
3360 any TU, so there's no need to scan TUs here. */
3361
3362 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3363 {
3364 /* We only need to look at symtabs not already expanded. */
3365 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3366 continue;
3367
3368 quick_file_names *file_data
3369 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3370 if (file_data == NULL)
3371 continue;
3372
3373 for (int j = 0; j < file_data->num_file_names; ++j)
3374 {
3375 const char *this_name = file_data->file_names[j];
3376 const char *this_real_name;
3377
3378 if (compare_filenames_for_search (this_name, name))
3379 {
3380 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3381 callback))
3382 return true;
3383 continue;
3384 }
3385
3386 /* Before we invoke realpath, which can get expensive when many
3387 files are involved, do a quick comparison of the basenames. */
3388 if (! basenames_may_differ
3389 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3390 continue;
3391
3392 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3393 file_data, j);
3394 if (compare_filenames_for_search (this_real_name, name))
3395 {
3396 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3397 callback))
3398 return true;
3399 continue;
3400 }
3401
3402 if (real_path != NULL)
3403 {
3404 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3405 gdb_assert (IS_ABSOLUTE_PATH (name));
3406 if (this_real_name != NULL
3407 && FILENAME_CMP (real_path, this_real_name) == 0)
3408 {
3409 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3410 callback))
3411 return true;
3412 continue;
3413 }
3414 }
3415 }
3416 }
3417
3418 return false;
3419 }
3420
3421 /* Struct used to manage iterating over all CUs looking for a symbol. */
3422
3423 struct dw2_symtab_iterator
3424 {
3425 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3426 struct dwarf2_per_objfile *dwarf2_per_objfile;
3427 /* If set, only look for symbols that match that block. Valid values are
3428 GLOBAL_BLOCK and STATIC_BLOCK. */
3429 gdb::optional<block_enum> block_index;
3430 /* The kind of symbol we're looking for. */
3431 domain_enum domain;
3432 /* The list of CUs from the index entry of the symbol,
3433 or NULL if not found. */
3434 offset_type *vec;
3435 /* The next element in VEC to look at. */
3436 int next;
3437 /* The number of elements in VEC, or zero if there is no match. */
3438 int length;
3439 /* Have we seen a global version of the symbol?
3440 If so we can ignore all further global instances.
3441 This is to work around gold/15646, inefficient gold-generated
3442 indices. */
3443 int global_seen;
3444 };
3445
3446 /* Initialize the index symtab iterator ITER. */
3447
3448 static void
3449 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3450 struct dwarf2_per_objfile *dwarf2_per_objfile,
3451 gdb::optional<block_enum> block_index,
3452 domain_enum domain,
3453 const char *name)
3454 {
3455 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3456 iter->block_index = block_index;
3457 iter->domain = domain;
3458 iter->next = 0;
3459 iter->global_seen = 0;
3460
3461 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3462
3463 /* index is NULL if OBJF_READNOW. */
3464 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3465 iter->length = MAYBE_SWAP (*iter->vec);
3466 else
3467 {
3468 iter->vec = NULL;
3469 iter->length = 0;
3470 }
3471 }
3472
3473 /* Return the next matching CU or NULL if there are no more. */
3474
3475 static struct dwarf2_per_cu_data *
3476 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3477 {
3478 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3479
3480 for ( ; iter->next < iter->length; ++iter->next)
3481 {
3482 offset_type cu_index_and_attrs =
3483 MAYBE_SWAP (iter->vec[iter->next + 1]);
3484 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3485 gdb_index_symbol_kind symbol_kind =
3486 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3487 /* Only check the symbol attributes if they're present.
3488 Indices prior to version 7 don't record them,
3489 and indices >= 7 may elide them for certain symbols
3490 (gold does this). */
3491 int attrs_valid =
3492 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3493 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3494
3495 /* Don't crash on bad data. */
3496 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3497 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3498 {
3499 complaint (_(".gdb_index entry has bad CU index"
3500 " [in module %s]"),
3501 objfile_name (dwarf2_per_objfile->objfile));
3502 continue;
3503 }
3504
3505 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3506
3507 /* Skip if already read in. */
3508 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3509 continue;
3510
3511 /* Check static vs global. */
3512 if (attrs_valid)
3513 {
3514 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3515
3516 if (iter->block_index.has_value ())
3517 {
3518 bool want_static = *iter->block_index == STATIC_BLOCK;
3519
3520 if (is_static != want_static)
3521 continue;
3522 }
3523
3524 /* Work around gold/15646. */
3525 if (!is_static && iter->global_seen)
3526 continue;
3527 if (!is_static)
3528 iter->global_seen = 1;
3529 }
3530
3531 /* Only check the symbol's kind if it has one. */
3532 if (attrs_valid)
3533 {
3534 switch (iter->domain)
3535 {
3536 case VAR_DOMAIN:
3537 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3538 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3539 /* Some types are also in VAR_DOMAIN. */
3540 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3541 continue;
3542 break;
3543 case STRUCT_DOMAIN:
3544 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3545 continue;
3546 break;
3547 case LABEL_DOMAIN:
3548 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3549 continue;
3550 break;
3551 case MODULE_DOMAIN:
3552 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3553 continue;
3554 break;
3555 default:
3556 break;
3557 }
3558 }
3559
3560 ++iter->next;
3561 return per_cu;
3562 }
3563
3564 return NULL;
3565 }
3566
3567 static struct compunit_symtab *
3568 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3569 const char *name, domain_enum domain)
3570 {
3571 struct compunit_symtab *stab_best = NULL;
3572 struct dwarf2_per_objfile *dwarf2_per_objfile
3573 = get_dwarf2_per_objfile (objfile);
3574
3575 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3576
3577 struct dw2_symtab_iterator iter;
3578 struct dwarf2_per_cu_data *per_cu;
3579
3580 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3581
3582 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3583 {
3584 struct symbol *sym, *with_opaque = NULL;
3585 struct compunit_symtab *stab
3586 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3587 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3588 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3589
3590 sym = block_find_symbol (block, name, domain,
3591 block_find_non_opaque_type_preferred,
3592 &with_opaque);
3593
3594 /* Some caution must be observed with overloaded functions
3595 and methods, since the index will not contain any overload
3596 information (but NAME might contain it). */
3597
3598 if (sym != NULL
3599 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3600 return stab;
3601 if (with_opaque != NULL
3602 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3603 stab_best = stab;
3604
3605 /* Keep looking through other CUs. */
3606 }
3607
3608 return stab_best;
3609 }
3610
3611 static void
3612 dw2_print_stats (struct objfile *objfile)
3613 {
3614 struct dwarf2_per_objfile *dwarf2_per_objfile
3615 = get_dwarf2_per_objfile (objfile);
3616 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3617 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3618 int count = 0;
3619
3620 for (int i = 0; i < total; ++i)
3621 {
3622 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3623
3624 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3625 ++count;
3626 }
3627 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3628 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3629 }
3630
3631 /* This dumps minimal information about the index.
3632 It is called via "mt print objfiles".
3633 One use is to verify .gdb_index has been loaded by the
3634 gdb.dwarf2/gdb-index.exp testcase. */
3635
3636 static void
3637 dw2_dump (struct objfile *objfile)
3638 {
3639 struct dwarf2_per_objfile *dwarf2_per_objfile
3640 = get_dwarf2_per_objfile (objfile);
3641
3642 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3643 printf_filtered (".gdb_index:");
3644 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3645 {
3646 printf_filtered (" version %d\n",
3647 dwarf2_per_objfile->per_bfd->index_table->version);
3648 }
3649 else
3650 printf_filtered (" faked for \"readnow\"\n");
3651 printf_filtered ("\n");
3652 }
3653
3654 static void
3655 dw2_expand_symtabs_for_function (struct objfile *objfile,
3656 const char *func_name)
3657 {
3658 struct dwarf2_per_objfile *dwarf2_per_objfile
3659 = get_dwarf2_per_objfile (objfile);
3660
3661 struct dw2_symtab_iterator iter;
3662 struct dwarf2_per_cu_data *per_cu;
3663
3664 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3665
3666 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3667 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3668
3669 }
3670
3671 static void
3672 dw2_expand_all_symtabs (struct objfile *objfile)
3673 {
3674 struct dwarf2_per_objfile *dwarf2_per_objfile
3675 = get_dwarf2_per_objfile (objfile);
3676 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3677 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3678
3679 for (int i = 0; i < total_units; ++i)
3680 {
3681 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3682
3683 /* We don't want to directly expand a partial CU, because if we
3684 read it with the wrong language, then assertion failures can
3685 be triggered later on. See PR symtab/23010. So, tell
3686 dw2_instantiate_symtab to skip partial CUs -- any important
3687 partial CU will be read via DW_TAG_imported_unit anyway. */
3688 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3689 }
3690 }
3691
3692 static void
3693 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3694 const char *fullname)
3695 {
3696 struct dwarf2_per_objfile *dwarf2_per_objfile
3697 = get_dwarf2_per_objfile (objfile);
3698
3699 /* We don't need to consider type units here.
3700 This is only called for examining code, e.g. expand_line_sal.
3701 There can be an order of magnitude (or more) more type units
3702 than comp units, and we avoid them if we can. */
3703
3704 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3705 {
3706 /* We only need to look at symtabs not already expanded. */
3707 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3708 continue;
3709
3710 quick_file_names *file_data
3711 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3712 if (file_data == NULL)
3713 continue;
3714
3715 for (int j = 0; j < file_data->num_file_names; ++j)
3716 {
3717 const char *this_fullname = file_data->file_names[j];
3718
3719 if (filename_cmp (this_fullname, fullname) == 0)
3720 {
3721 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3722 break;
3723 }
3724 }
3725 }
3726 }
3727
3728 static void
3729 dw2_expand_symtabs_matching_symbol
3730 (mapped_index_base &index,
3731 const lookup_name_info &lookup_name_in,
3732 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3733 enum search_domain kind,
3734 gdb::function_view<bool (offset_type)> match_callback,
3735 dwarf2_per_objfile *per_objfile);
3736
3737 static void
3738 dw2_expand_symtabs_matching_one
3739 (dwarf2_per_cu_data *per_cu,
3740 dwarf2_per_objfile *per_objfile,
3741 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3742 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3743
3744 static void
3745 dw2_map_matching_symbols
3746 (struct objfile *objfile,
3747 const lookup_name_info &name, domain_enum domain,
3748 int global,
3749 gdb::function_view<symbol_found_callback_ftype> callback,
3750 symbol_compare_ftype *ordered_compare)
3751 {
3752 /* Used for Ada. */
3753 struct dwarf2_per_objfile *dwarf2_per_objfile
3754 = get_dwarf2_per_objfile (objfile);
3755
3756 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3757
3758 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3759 {
3760 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3761 here though if the current language is Ada for a non-Ada objfile
3762 using GNU index. */
3763 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3764
3765 const char *match_name = name.ada ().lookup_name ().c_str ();
3766 auto matcher = [&] (const char *symname)
3767 {
3768 if (ordered_compare == nullptr)
3769 return true;
3770 return ordered_compare (symname, match_name) == 0;
3771 };
3772
3773 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3774 [&] (offset_type namei)
3775 {
3776 struct dw2_symtab_iterator iter;
3777 struct dwarf2_per_cu_data *per_cu;
3778
3779 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3780 match_name);
3781 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3782 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3783 nullptr);
3784 return true;
3785 }, dwarf2_per_objfile);
3786 }
3787 else
3788 {
3789 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3790 proceed assuming all symtabs have been read in. */
3791 }
3792
3793 for (compunit_symtab *cust : objfile->compunits ())
3794 {
3795 const struct block *block;
3796
3797 if (cust == NULL)
3798 continue;
3799 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3800 if (!iterate_over_symbols_terminated (block, name,
3801 domain, callback))
3802 return;
3803 }
3804 }
3805
3806 /* Starting from a search name, return the string that finds the upper
3807 bound of all strings that start with SEARCH_NAME in a sorted name
3808 list. Returns the empty string to indicate that the upper bound is
3809 the end of the list. */
3810
3811 static std::string
3812 make_sort_after_prefix_name (const char *search_name)
3813 {
3814 /* When looking to complete "func", we find the upper bound of all
3815 symbols that start with "func" by looking for where we'd insert
3816 the closest string that would follow "func" in lexicographical
3817 order. Usually, that's "func"-with-last-character-incremented,
3818 i.e. "fund". Mind non-ASCII characters, though. Usually those
3819 will be UTF-8 multi-byte sequences, but we can't be certain.
3820 Especially mind the 0xff character, which is a valid character in
3821 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3822 rule out compilers allowing it in identifiers. Note that
3823 conveniently, strcmp/strcasecmp are specified to compare
3824 characters interpreted as unsigned char. So what we do is treat
3825 the whole string as a base 256 number composed of a sequence of
3826 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3827 to 0, and carries 1 to the following more-significant position.
3828 If the very first character in SEARCH_NAME ends up incremented
3829 and carries/overflows, then the upper bound is the end of the
3830 list. The string after the empty string is also the empty
3831 string.
3832
3833 Some examples of this operation:
3834
3835 SEARCH_NAME => "+1" RESULT
3836
3837 "abc" => "abd"
3838 "ab\xff" => "ac"
3839 "\xff" "a" "\xff" => "\xff" "b"
3840 "\xff" => ""
3841 "\xff\xff" => ""
3842 "" => ""
3843
3844 Then, with these symbols for example:
3845
3846 func
3847 func1
3848 fund
3849
3850 completing "func" looks for symbols between "func" and
3851 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3852 which finds "func" and "func1", but not "fund".
3853
3854 And with:
3855
3856 funcÿ (Latin1 'ÿ' [0xff])
3857 funcÿ1
3858 fund
3859
3860 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3861 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3862
3863 And with:
3864
3865 ÿÿ (Latin1 'ÿ' [0xff])
3866 ÿÿ1
3867
3868 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3869 the end of the list.
3870 */
3871 std::string after = search_name;
3872 while (!after.empty () && (unsigned char) after.back () == 0xff)
3873 after.pop_back ();
3874 if (!after.empty ())
3875 after.back () = (unsigned char) after.back () + 1;
3876 return after;
3877 }
3878
3879 /* See declaration. */
3880
3881 std::pair<std::vector<name_component>::const_iterator,
3882 std::vector<name_component>::const_iterator>
3883 mapped_index_base::find_name_components_bounds
3884 (const lookup_name_info &lookup_name_without_params, language lang,
3885 dwarf2_per_objfile *per_objfile) const
3886 {
3887 auto *name_cmp
3888 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3889
3890 const char *lang_name
3891 = lookup_name_without_params.language_lookup_name (lang);
3892
3893 /* Comparison function object for lower_bound that matches against a
3894 given symbol name. */
3895 auto lookup_compare_lower = [&] (const name_component &elem,
3896 const char *name)
3897 {
3898 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3899 const char *elem_name = elem_qualified + elem.name_offset;
3900 return name_cmp (elem_name, name) < 0;
3901 };
3902
3903 /* Comparison function object for upper_bound that matches against a
3904 given symbol name. */
3905 auto lookup_compare_upper = [&] (const char *name,
3906 const name_component &elem)
3907 {
3908 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3909 const char *elem_name = elem_qualified + elem.name_offset;
3910 return name_cmp (name, elem_name) < 0;
3911 };
3912
3913 auto begin = this->name_components.begin ();
3914 auto end = this->name_components.end ();
3915
3916 /* Find the lower bound. */
3917 auto lower = [&] ()
3918 {
3919 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3920 return begin;
3921 else
3922 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3923 } ();
3924
3925 /* Find the upper bound. */
3926 auto upper = [&] ()
3927 {
3928 if (lookup_name_without_params.completion_mode ())
3929 {
3930 /* In completion mode, we want UPPER to point past all
3931 symbols names that have the same prefix. I.e., with
3932 these symbols, and completing "func":
3933
3934 function << lower bound
3935 function1
3936 other_function << upper bound
3937
3938 We find the upper bound by looking for the insertion
3939 point of "func"-with-last-character-incremented,
3940 i.e. "fund". */
3941 std::string after = make_sort_after_prefix_name (lang_name);
3942 if (after.empty ())
3943 return end;
3944 return std::lower_bound (lower, end, after.c_str (),
3945 lookup_compare_lower);
3946 }
3947 else
3948 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3949 } ();
3950
3951 return {lower, upper};
3952 }
3953
3954 /* See declaration. */
3955
3956 void
3957 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3958 {
3959 if (!this->name_components.empty ())
3960 return;
3961
3962 this->name_components_casing = case_sensitivity;
3963 auto *name_cmp
3964 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3965
3966 /* The code below only knows how to break apart components of C++
3967 symbol names (and other languages that use '::' as
3968 namespace/module separator) and Ada symbol names. */
3969 auto count = this->symbol_name_count ();
3970 for (offset_type idx = 0; idx < count; idx++)
3971 {
3972 if (this->symbol_name_slot_invalid (idx))
3973 continue;
3974
3975 const char *name = this->symbol_name_at (idx, per_objfile);
3976
3977 /* Add each name component to the name component table. */
3978 unsigned int previous_len = 0;
3979
3980 if (strstr (name, "::") != nullptr)
3981 {
3982 for (unsigned int current_len = cp_find_first_component (name);
3983 name[current_len] != '\0';
3984 current_len += cp_find_first_component (name + current_len))
3985 {
3986 gdb_assert (name[current_len] == ':');
3987 this->name_components.push_back ({previous_len, idx});
3988 /* Skip the '::'. */
3989 current_len += 2;
3990 previous_len = current_len;
3991 }
3992 }
3993 else
3994 {
3995 /* Handle the Ada encoded (aka mangled) form here. */
3996 for (const char *iter = strstr (name, "__");
3997 iter != nullptr;
3998 iter = strstr (iter, "__"))
3999 {
4000 this->name_components.push_back ({previous_len, idx});
4001 iter += 2;
4002 previous_len = iter - name;
4003 }
4004 }
4005
4006 this->name_components.push_back ({previous_len, idx});
4007 }
4008
4009 /* Sort name_components elements by name. */
4010 auto name_comp_compare = [&] (const name_component &left,
4011 const name_component &right)
4012 {
4013 const char *left_qualified
4014 = this->symbol_name_at (left.idx, per_objfile);
4015 const char *right_qualified
4016 = this->symbol_name_at (right.idx, per_objfile);
4017
4018 const char *left_name = left_qualified + left.name_offset;
4019 const char *right_name = right_qualified + right.name_offset;
4020
4021 return name_cmp (left_name, right_name) < 0;
4022 };
4023
4024 std::sort (this->name_components.begin (),
4025 this->name_components.end (),
4026 name_comp_compare);
4027 }
4028
4029 /* Helper for dw2_expand_symtabs_matching that works with a
4030 mapped_index_base instead of the containing objfile. This is split
4031 to a separate function in order to be able to unit test the
4032 name_components matching using a mock mapped_index_base. For each
4033 symbol name that matches, calls MATCH_CALLBACK, passing it the
4034 symbol's index in the mapped_index_base symbol table. */
4035
4036 static void
4037 dw2_expand_symtabs_matching_symbol
4038 (mapped_index_base &index,
4039 const lookup_name_info &lookup_name_in,
4040 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4041 enum search_domain kind,
4042 gdb::function_view<bool (offset_type)> match_callback,
4043 dwarf2_per_objfile *per_objfile)
4044 {
4045 lookup_name_info lookup_name_without_params
4046 = lookup_name_in.make_ignore_params ();
4047
4048 /* Build the symbol name component sorted vector, if we haven't
4049 yet. */
4050 index.build_name_components (per_objfile);
4051
4052 /* The same symbol may appear more than once in the range though.
4053 E.g., if we're looking for symbols that complete "w", and we have
4054 a symbol named "w1::w2", we'll find the two name components for
4055 that same symbol in the range. To be sure we only call the
4056 callback once per symbol, we first collect the symbol name
4057 indexes that matched in a temporary vector and ignore
4058 duplicates. */
4059 std::vector<offset_type> matches;
4060
4061 struct name_and_matcher
4062 {
4063 symbol_name_matcher_ftype *matcher;
4064 const char *name;
4065
4066 bool operator== (const name_and_matcher &other) const
4067 {
4068 return matcher == other.matcher && strcmp (name, other.name) == 0;
4069 }
4070 };
4071
4072 /* A vector holding all the different symbol name matchers, for all
4073 languages. */
4074 std::vector<name_and_matcher> matchers;
4075
4076 for (int i = 0; i < nr_languages; i++)
4077 {
4078 enum language lang_e = (enum language) i;
4079
4080 const language_defn *lang = language_def (lang_e);
4081 symbol_name_matcher_ftype *name_matcher
4082 = get_symbol_name_matcher (lang, lookup_name_without_params);
4083
4084 name_and_matcher key {
4085 name_matcher,
4086 lookup_name_without_params.language_lookup_name (lang_e)
4087 };
4088
4089 /* Don't insert the same comparison routine more than once.
4090 Note that we do this linear walk. This is not a problem in
4091 practice because the number of supported languages is
4092 low. */
4093 if (std::find (matchers.begin (), matchers.end (), key)
4094 != matchers.end ())
4095 continue;
4096 matchers.push_back (std::move (key));
4097
4098 auto bounds
4099 = index.find_name_components_bounds (lookup_name_without_params,
4100 lang_e, per_objfile);
4101
4102 /* Now for each symbol name in range, check to see if we have a name
4103 match, and if so, call the MATCH_CALLBACK callback. */
4104
4105 for (; bounds.first != bounds.second; ++bounds.first)
4106 {
4107 const char *qualified
4108 = index.symbol_name_at (bounds.first->idx, per_objfile);
4109
4110 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4111 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4112 continue;
4113
4114 matches.push_back (bounds.first->idx);
4115 }
4116 }
4117
4118 std::sort (matches.begin (), matches.end ());
4119
4120 /* Finally call the callback, once per match. */
4121 ULONGEST prev = -1;
4122 for (offset_type idx : matches)
4123 {
4124 if (prev != idx)
4125 {
4126 if (!match_callback (idx))
4127 break;
4128 prev = idx;
4129 }
4130 }
4131
4132 /* Above we use a type wider than idx's for 'prev', since 0 and
4133 (offset_type)-1 are both possible values. */
4134 static_assert (sizeof (prev) > sizeof (offset_type), "");
4135 }
4136
4137 #if GDB_SELF_TEST
4138
4139 namespace selftests { namespace dw2_expand_symtabs_matching {
4140
4141 /* A mock .gdb_index/.debug_names-like name index table, enough to
4142 exercise dw2_expand_symtabs_matching_symbol, which works with the
4143 mapped_index_base interface. Builds an index from the symbol list
4144 passed as parameter to the constructor. */
4145 class mock_mapped_index : public mapped_index_base
4146 {
4147 public:
4148 mock_mapped_index (gdb::array_view<const char *> symbols)
4149 : m_symbol_table (symbols)
4150 {}
4151
4152 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4153
4154 /* Return the number of names in the symbol table. */
4155 size_t symbol_name_count () const override
4156 {
4157 return m_symbol_table.size ();
4158 }
4159
4160 /* Get the name of the symbol at IDX in the symbol table. */
4161 const char *symbol_name_at
4162 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4163 {
4164 return m_symbol_table[idx];
4165 }
4166
4167 private:
4168 gdb::array_view<const char *> m_symbol_table;
4169 };
4170
4171 /* Convenience function that converts a NULL pointer to a "<null>"
4172 string, to pass to print routines. */
4173
4174 static const char *
4175 string_or_null (const char *str)
4176 {
4177 return str != NULL ? str : "<null>";
4178 }
4179
4180 /* Check if a lookup_name_info built from
4181 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4182 index. EXPECTED_LIST is the list of expected matches, in expected
4183 matching order. If no match expected, then an empty list is
4184 specified. Returns true on success. On failure prints a warning
4185 indicating the file:line that failed, and returns false. */
4186
4187 static bool
4188 check_match (const char *file, int line,
4189 mock_mapped_index &mock_index,
4190 const char *name, symbol_name_match_type match_type,
4191 bool completion_mode,
4192 std::initializer_list<const char *> expected_list,
4193 dwarf2_per_objfile *per_objfile)
4194 {
4195 lookup_name_info lookup_name (name, match_type, completion_mode);
4196
4197 bool matched = true;
4198
4199 auto mismatch = [&] (const char *expected_str,
4200 const char *got)
4201 {
4202 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4203 "expected=\"%s\", got=\"%s\"\n"),
4204 file, line,
4205 (match_type == symbol_name_match_type::FULL
4206 ? "FULL" : "WILD"),
4207 name, string_or_null (expected_str), string_or_null (got));
4208 matched = false;
4209 };
4210
4211 auto expected_it = expected_list.begin ();
4212 auto expected_end = expected_list.end ();
4213
4214 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4215 NULL, ALL_DOMAIN,
4216 [&] (offset_type idx)
4217 {
4218 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4219 const char *expected_str
4220 = expected_it == expected_end ? NULL : *expected_it++;
4221
4222 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4223 mismatch (expected_str, matched_name);
4224 return true;
4225 }, per_objfile);
4226
4227 const char *expected_str
4228 = expected_it == expected_end ? NULL : *expected_it++;
4229 if (expected_str != NULL)
4230 mismatch (expected_str, NULL);
4231
4232 return matched;
4233 }
4234
4235 /* The symbols added to the mock mapped_index for testing (in
4236 canonical form). */
4237 static const char *test_symbols[] = {
4238 "function",
4239 "std::bar",
4240 "std::zfunction",
4241 "std::zfunction2",
4242 "w1::w2",
4243 "ns::foo<char*>",
4244 "ns::foo<int>",
4245 "ns::foo<long>",
4246 "ns2::tmpl<int>::foo2",
4247 "(anonymous namespace)::A::B::C",
4248
4249 /* These are used to check that the increment-last-char in the
4250 matching algorithm for completion doesn't match "t1_fund" when
4251 completing "t1_func". */
4252 "t1_func",
4253 "t1_func1",
4254 "t1_fund",
4255 "t1_fund1",
4256
4257 /* A UTF-8 name with multi-byte sequences to make sure that
4258 cp-name-parser understands this as a single identifier ("função"
4259 is "function" in PT). */
4260 u8"u8função",
4261
4262 /* \377 (0xff) is Latin1 'ÿ'. */
4263 "yfunc\377",
4264
4265 /* \377 (0xff) is Latin1 'ÿ'. */
4266 "\377",
4267 "\377\377123",
4268
4269 /* A name with all sorts of complications. Starts with "z" to make
4270 it easier for the completion tests below. */
4271 #define Z_SYM_NAME \
4272 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4273 "::tuple<(anonymous namespace)::ui*, " \
4274 "std::default_delete<(anonymous namespace)::ui>, void>"
4275
4276 Z_SYM_NAME
4277 };
4278
4279 /* Returns true if the mapped_index_base::find_name_component_bounds
4280 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4281 in completion mode. */
4282
4283 static bool
4284 check_find_bounds_finds (mapped_index_base &index,
4285 const char *search_name,
4286 gdb::array_view<const char *> expected_syms,
4287 dwarf2_per_objfile *per_objfile)
4288 {
4289 lookup_name_info lookup_name (search_name,
4290 symbol_name_match_type::FULL, true);
4291
4292 auto bounds = index.find_name_components_bounds (lookup_name,
4293 language_cplus,
4294 per_objfile);
4295
4296 size_t distance = std::distance (bounds.first, bounds.second);
4297 if (distance != expected_syms.size ())
4298 return false;
4299
4300 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4301 {
4302 auto nc_elem = bounds.first + exp_elem;
4303 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4304 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4305 return false;
4306 }
4307
4308 return true;
4309 }
4310
4311 /* Test the lower-level mapped_index::find_name_component_bounds
4312 method. */
4313
4314 static void
4315 test_mapped_index_find_name_component_bounds ()
4316 {
4317 mock_mapped_index mock_index (test_symbols);
4318
4319 mock_index.build_name_components (NULL /* per_objfile */);
4320
4321 /* Test the lower-level mapped_index::find_name_component_bounds
4322 method in completion mode. */
4323 {
4324 static const char *expected_syms[] = {
4325 "t1_func",
4326 "t1_func1",
4327 };
4328
4329 SELF_CHECK (check_find_bounds_finds
4330 (mock_index, "t1_func", expected_syms,
4331 NULL /* per_objfile */));
4332 }
4333
4334 /* Check that the increment-last-char in the name matching algorithm
4335 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4336 {
4337 static const char *expected_syms1[] = {
4338 "\377",
4339 "\377\377123",
4340 };
4341 SELF_CHECK (check_find_bounds_finds
4342 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4343
4344 static const char *expected_syms2[] = {
4345 "\377\377123",
4346 };
4347 SELF_CHECK (check_find_bounds_finds
4348 (mock_index, "\377\377", expected_syms2,
4349 NULL /* per_objfile */));
4350 }
4351 }
4352
4353 /* Test dw2_expand_symtabs_matching_symbol. */
4354
4355 static void
4356 test_dw2_expand_symtabs_matching_symbol ()
4357 {
4358 mock_mapped_index mock_index (test_symbols);
4359
4360 /* We let all tests run until the end even if some fails, for debug
4361 convenience. */
4362 bool any_mismatch = false;
4363
4364 /* Create the expected symbols list (an initializer_list). Needed
4365 because lists have commas, and we need to pass them to CHECK,
4366 which is a macro. */
4367 #define EXPECT(...) { __VA_ARGS__ }
4368
4369 /* Wrapper for check_match that passes down the current
4370 __FILE__/__LINE__. */
4371 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4372 any_mismatch |= !check_match (__FILE__, __LINE__, \
4373 mock_index, \
4374 NAME, MATCH_TYPE, COMPLETION_MODE, \
4375 EXPECTED_LIST, NULL)
4376
4377 /* Identity checks. */
4378 for (const char *sym : test_symbols)
4379 {
4380 /* Should be able to match all existing symbols. */
4381 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4382 EXPECT (sym));
4383
4384 /* Should be able to match all existing symbols with
4385 parameters. */
4386 std::string with_params = std::string (sym) + "(int)";
4387 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4388 EXPECT (sym));
4389
4390 /* Should be able to match all existing symbols with
4391 parameters and qualifiers. */
4392 with_params = std::string (sym) + " ( int ) const";
4393 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4394 EXPECT (sym));
4395
4396 /* This should really find sym, but cp-name-parser.y doesn't
4397 know about lvalue/rvalue qualifiers yet. */
4398 with_params = std::string (sym) + " ( int ) &&";
4399 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4400 {});
4401 }
4402
4403 /* Check that the name matching algorithm for completion doesn't get
4404 confused with Latin1 'ÿ' / 0xff. */
4405 {
4406 static const char str[] = "\377";
4407 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4408 EXPECT ("\377", "\377\377123"));
4409 }
4410
4411 /* Check that the increment-last-char in the matching algorithm for
4412 completion doesn't match "t1_fund" when completing "t1_func". */
4413 {
4414 static const char str[] = "t1_func";
4415 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4416 EXPECT ("t1_func", "t1_func1"));
4417 }
4418
4419 /* Check that completion mode works at each prefix of the expected
4420 symbol name. */
4421 {
4422 static const char str[] = "function(int)";
4423 size_t len = strlen (str);
4424 std::string lookup;
4425
4426 for (size_t i = 1; i < len; i++)
4427 {
4428 lookup.assign (str, i);
4429 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4430 EXPECT ("function"));
4431 }
4432 }
4433
4434 /* While "w" is a prefix of both components, the match function
4435 should still only be called once. */
4436 {
4437 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4438 EXPECT ("w1::w2"));
4439 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4440 EXPECT ("w1::w2"));
4441 }
4442
4443 /* Same, with a "complicated" symbol. */
4444 {
4445 static const char str[] = Z_SYM_NAME;
4446 size_t len = strlen (str);
4447 std::string lookup;
4448
4449 for (size_t i = 1; i < len; i++)
4450 {
4451 lookup.assign (str, i);
4452 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4453 EXPECT (Z_SYM_NAME));
4454 }
4455 }
4456
4457 /* In FULL mode, an incomplete symbol doesn't match. */
4458 {
4459 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4460 {});
4461 }
4462
4463 /* A complete symbol with parameters matches any overload, since the
4464 index has no overload info. */
4465 {
4466 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4467 EXPECT ("std::zfunction", "std::zfunction2"));
4468 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4469 EXPECT ("std::zfunction", "std::zfunction2"));
4470 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4471 EXPECT ("std::zfunction", "std::zfunction2"));
4472 }
4473
4474 /* Check that whitespace is ignored appropriately. A symbol with a
4475 template argument list. */
4476 {
4477 static const char expected[] = "ns::foo<int>";
4478 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4479 EXPECT (expected));
4480 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4481 EXPECT (expected));
4482 }
4483
4484 /* Check that whitespace is ignored appropriately. A symbol with a
4485 template argument list that includes a pointer. */
4486 {
4487 static const char expected[] = "ns::foo<char*>";
4488 /* Try both completion and non-completion modes. */
4489 static const bool completion_mode[2] = {false, true};
4490 for (size_t i = 0; i < 2; i++)
4491 {
4492 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4493 completion_mode[i], EXPECT (expected));
4494 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4495 completion_mode[i], EXPECT (expected));
4496
4497 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4498 completion_mode[i], EXPECT (expected));
4499 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4500 completion_mode[i], EXPECT (expected));
4501 }
4502 }
4503
4504 {
4505 /* Check method qualifiers are ignored. */
4506 static const char expected[] = "ns::foo<char*>";
4507 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4508 symbol_name_match_type::FULL, true, EXPECT (expected));
4509 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4510 symbol_name_match_type::FULL, true, EXPECT (expected));
4511 CHECK_MATCH ("foo < char * > ( int ) const",
4512 symbol_name_match_type::WILD, true, EXPECT (expected));
4513 CHECK_MATCH ("foo < char * > ( int ) &&",
4514 symbol_name_match_type::WILD, true, EXPECT (expected));
4515 }
4516
4517 /* Test lookup names that don't match anything. */
4518 {
4519 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4520 {});
4521
4522 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4523 {});
4524 }
4525
4526 /* Some wild matching tests, exercising "(anonymous namespace)",
4527 which should not be confused with a parameter list. */
4528 {
4529 static const char *syms[] = {
4530 "A::B::C",
4531 "B::C",
4532 "C",
4533 "A :: B :: C ( int )",
4534 "B :: C ( int )",
4535 "C ( int )",
4536 };
4537
4538 for (const char *s : syms)
4539 {
4540 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4541 EXPECT ("(anonymous namespace)::A::B::C"));
4542 }
4543 }
4544
4545 {
4546 static const char expected[] = "ns2::tmpl<int>::foo2";
4547 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4548 EXPECT (expected));
4549 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4550 EXPECT (expected));
4551 }
4552
4553 SELF_CHECK (!any_mismatch);
4554
4555 #undef EXPECT
4556 #undef CHECK_MATCH
4557 }
4558
4559 static void
4560 run_test ()
4561 {
4562 test_mapped_index_find_name_component_bounds ();
4563 test_dw2_expand_symtabs_matching_symbol ();
4564 }
4565
4566 }} // namespace selftests::dw2_expand_symtabs_matching
4567
4568 #endif /* GDB_SELF_TEST */
4569
4570 /* If FILE_MATCHER is NULL or if PER_CU has
4571 dwarf2_per_cu_quick_data::MARK set (see
4572 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4573 EXPANSION_NOTIFY on it. */
4574
4575 static void
4576 dw2_expand_symtabs_matching_one
4577 (dwarf2_per_cu_data *per_cu,
4578 dwarf2_per_objfile *per_objfile,
4579 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4580 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4581 {
4582 if (file_matcher == NULL || per_cu->v.quick->mark)
4583 {
4584 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4585
4586 compunit_symtab *symtab
4587 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4588 gdb_assert (symtab != nullptr);
4589
4590 if (expansion_notify != NULL && symtab_was_null)
4591 expansion_notify (symtab);
4592 }
4593 }
4594
4595 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4596 matched, to expand corresponding CUs that were marked. IDX is the
4597 index of the symbol name that matched. */
4598
4599 static void
4600 dw2_expand_marked_cus
4601 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4602 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4603 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4604 search_domain kind)
4605 {
4606 offset_type *vec, vec_len, vec_idx;
4607 bool global_seen = false;
4608 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4609
4610 vec = (offset_type *) (index.constant_pool
4611 + MAYBE_SWAP (index.symbol_table[idx].vec));
4612 vec_len = MAYBE_SWAP (vec[0]);
4613 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4614 {
4615 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4616 /* This value is only valid for index versions >= 7. */
4617 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4618 gdb_index_symbol_kind symbol_kind =
4619 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4620 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4621 /* Only check the symbol attributes if they're present.
4622 Indices prior to version 7 don't record them,
4623 and indices >= 7 may elide them for certain symbols
4624 (gold does this). */
4625 int attrs_valid =
4626 (index.version >= 7
4627 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4628
4629 /* Work around gold/15646. */
4630 if (attrs_valid)
4631 {
4632 if (!is_static && global_seen)
4633 continue;
4634 if (!is_static)
4635 global_seen = true;
4636 }
4637
4638 /* Only check the symbol's kind if it has one. */
4639 if (attrs_valid)
4640 {
4641 switch (kind)
4642 {
4643 case VARIABLES_DOMAIN:
4644 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4645 continue;
4646 break;
4647 case FUNCTIONS_DOMAIN:
4648 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4649 continue;
4650 break;
4651 case TYPES_DOMAIN:
4652 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4653 continue;
4654 break;
4655 case MODULES_DOMAIN:
4656 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4657 continue;
4658 break;
4659 default:
4660 break;
4661 }
4662 }
4663
4664 /* Don't crash on bad data. */
4665 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4666 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4667 {
4668 complaint (_(".gdb_index entry has bad CU index"
4669 " [in module %s]"),
4670 objfile_name (dwarf2_per_objfile->objfile));
4671 continue;
4672 }
4673
4674 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4675 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4676 expansion_notify);
4677 }
4678 }
4679
4680 /* If FILE_MATCHER is non-NULL, set all the
4681 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4682 that match FILE_MATCHER. */
4683
4684 static void
4685 dw_expand_symtabs_matching_file_matcher
4686 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4687 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4688 {
4689 if (file_matcher == NULL)
4690 return;
4691
4692 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4693 htab_eq_pointer,
4694 NULL, xcalloc, xfree));
4695 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4696 htab_eq_pointer,
4697 NULL, xcalloc, xfree));
4698
4699 /* The rule is CUs specify all the files, including those used by
4700 any TU, so there's no need to scan TUs here. */
4701
4702 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4703 {
4704 QUIT;
4705
4706 per_cu->v.quick->mark = 0;
4707
4708 /* We only need to look at symtabs not already expanded. */
4709 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4710 continue;
4711
4712 quick_file_names *file_data
4713 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4714 if (file_data == NULL)
4715 continue;
4716
4717 if (htab_find (visited_not_found.get (), file_data) != NULL)
4718 continue;
4719 else if (htab_find (visited_found.get (), file_data) != NULL)
4720 {
4721 per_cu->v.quick->mark = 1;
4722 continue;
4723 }
4724
4725 for (int j = 0; j < file_data->num_file_names; ++j)
4726 {
4727 const char *this_real_name;
4728
4729 if (file_matcher (file_data->file_names[j], false))
4730 {
4731 per_cu->v.quick->mark = 1;
4732 break;
4733 }
4734
4735 /* Before we invoke realpath, which can get expensive when many
4736 files are involved, do a quick comparison of the basenames. */
4737 if (!basenames_may_differ
4738 && !file_matcher (lbasename (file_data->file_names[j]),
4739 true))
4740 continue;
4741
4742 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4743 file_data, j);
4744 if (file_matcher (this_real_name, false))
4745 {
4746 per_cu->v.quick->mark = 1;
4747 break;
4748 }
4749 }
4750
4751 void **slot = htab_find_slot (per_cu->v.quick->mark
4752 ? visited_found.get ()
4753 : visited_not_found.get (),
4754 file_data, INSERT);
4755 *slot = file_data;
4756 }
4757 }
4758
4759 static void
4760 dw2_expand_symtabs_matching
4761 (struct objfile *objfile,
4762 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4763 const lookup_name_info *lookup_name,
4764 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4765 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4766 enum search_domain kind)
4767 {
4768 struct dwarf2_per_objfile *dwarf2_per_objfile
4769 = get_dwarf2_per_objfile (objfile);
4770
4771 /* index_table is NULL if OBJF_READNOW. */
4772 if (!dwarf2_per_objfile->per_bfd->index_table)
4773 return;
4774
4775 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4776
4777 if (symbol_matcher == NULL && lookup_name == NULL)
4778 {
4779 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4780 {
4781 QUIT;
4782
4783 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4784 file_matcher, expansion_notify);
4785 }
4786 return;
4787 }
4788
4789 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4790
4791 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4792 symbol_matcher,
4793 kind, [&] (offset_type idx)
4794 {
4795 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4796 expansion_notify, kind);
4797 return true;
4798 }, dwarf2_per_objfile);
4799 }
4800
4801 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4802 symtab. */
4803
4804 static struct compunit_symtab *
4805 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4806 CORE_ADDR pc)
4807 {
4808 int i;
4809
4810 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4811 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4812 return cust;
4813
4814 if (cust->includes == NULL)
4815 return NULL;
4816
4817 for (i = 0; cust->includes[i]; ++i)
4818 {
4819 struct compunit_symtab *s = cust->includes[i];
4820
4821 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4822 if (s != NULL)
4823 return s;
4824 }
4825
4826 return NULL;
4827 }
4828
4829 static struct compunit_symtab *
4830 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4831 struct bound_minimal_symbol msymbol,
4832 CORE_ADDR pc,
4833 struct obj_section *section,
4834 int warn_if_readin)
4835 {
4836 struct dwarf2_per_cu_data *data;
4837 struct compunit_symtab *result;
4838
4839 if (!objfile->partial_symtabs->psymtabs_addrmap)
4840 return NULL;
4841
4842 CORE_ADDR baseaddr = objfile->text_section_offset ();
4843 data = (struct dwarf2_per_cu_data *) addrmap_find
4844 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4845 if (!data)
4846 return NULL;
4847
4848 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4849 if (warn_if_readin && per_objfile->symtab_set_p (data))
4850 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4851 paddress (objfile->arch (), pc));
4852
4853 result = recursively_find_pc_sect_compunit_symtab
4854 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4855
4856 gdb_assert (result != NULL);
4857 return result;
4858 }
4859
4860 static void
4861 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4862 void *data, int need_fullname)
4863 {
4864 struct dwarf2_per_objfile *dwarf2_per_objfile
4865 = get_dwarf2_per_objfile (objfile);
4866
4867 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4868 {
4869 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4870
4871 htab_up visited (htab_create_alloc (10,
4872 htab_hash_pointer, htab_eq_pointer,
4873 NULL, xcalloc, xfree));
4874
4875 /* The rule is CUs specify all the files, including those used
4876 by any TU, so there's no need to scan TUs here. We can
4877 ignore file names coming from already-expanded CUs. */
4878
4879 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4880 {
4881 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4882 {
4883 void **slot = htab_find_slot (visited.get (),
4884 per_cu->v.quick->file_names,
4885 INSERT);
4886
4887 *slot = per_cu->v.quick->file_names;
4888 }
4889 }
4890
4891 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4892 {
4893 /* We only need to look at symtabs not already expanded. */
4894 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4895 continue;
4896
4897 quick_file_names *file_data
4898 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4899 if (file_data == NULL)
4900 continue;
4901
4902 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4903 if (*slot)
4904 {
4905 /* Already visited. */
4906 continue;
4907 }
4908 *slot = file_data;
4909
4910 for (int j = 0; j < file_data->num_file_names; ++j)
4911 {
4912 const char *filename = file_data->file_names[j];
4913 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4914 }
4915 }
4916 }
4917
4918 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4919 {
4920 gdb::unique_xmalloc_ptr<char> this_real_name;
4921
4922 if (need_fullname)
4923 this_real_name = gdb_realpath (filename);
4924 (*fun) (filename, this_real_name.get (), data);
4925 });
4926 }
4927
4928 static int
4929 dw2_has_symbols (struct objfile *objfile)
4930 {
4931 return 1;
4932 }
4933
4934 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4935 {
4936 dw2_has_symbols,
4937 dw2_find_last_source_symtab,
4938 dw2_forget_cached_source_info,
4939 dw2_map_symtabs_matching_filename,
4940 dw2_lookup_symbol,
4941 NULL,
4942 dw2_print_stats,
4943 dw2_dump,
4944 dw2_expand_symtabs_for_function,
4945 dw2_expand_all_symtabs,
4946 dw2_expand_symtabs_with_fullname,
4947 dw2_map_matching_symbols,
4948 dw2_expand_symtabs_matching,
4949 dw2_find_pc_sect_compunit_symtab,
4950 NULL,
4951 dw2_map_symbol_filenames
4952 };
4953
4954 /* DWARF-5 debug_names reader. */
4955
4956 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4957 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4958
4959 /* A helper function that reads the .debug_names section in SECTION
4960 and fills in MAP. FILENAME is the name of the file containing the
4961 section; it is used for error reporting.
4962
4963 Returns true if all went well, false otherwise. */
4964
4965 static bool
4966 read_debug_names_from_section (struct objfile *objfile,
4967 const char *filename,
4968 struct dwarf2_section_info *section,
4969 mapped_debug_names &map)
4970 {
4971 if (section->empty ())
4972 return false;
4973
4974 /* Older elfutils strip versions could keep the section in the main
4975 executable while splitting it for the separate debug info file. */
4976 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4977 return false;
4978
4979 section->read (objfile);
4980
4981 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4982
4983 const gdb_byte *addr = section->buffer;
4984
4985 bfd *const abfd = section->get_bfd_owner ();
4986
4987 unsigned int bytes_read;
4988 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4989 addr += bytes_read;
4990
4991 map.dwarf5_is_dwarf64 = bytes_read != 4;
4992 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4993 if (bytes_read + length != section->size)
4994 {
4995 /* There may be multiple per-CU indices. */
4996 warning (_("Section .debug_names in %s length %s does not match "
4997 "section length %s, ignoring .debug_names."),
4998 filename, plongest (bytes_read + length),
4999 pulongest (section->size));
5000 return false;
5001 }
5002
5003 /* The version number. */
5004 uint16_t version = read_2_bytes (abfd, addr);
5005 addr += 2;
5006 if (version != 5)
5007 {
5008 warning (_("Section .debug_names in %s has unsupported version %d, "
5009 "ignoring .debug_names."),
5010 filename, version);
5011 return false;
5012 }
5013
5014 /* Padding. */
5015 uint16_t padding = read_2_bytes (abfd, addr);
5016 addr += 2;
5017 if (padding != 0)
5018 {
5019 warning (_("Section .debug_names in %s has unsupported padding %d, "
5020 "ignoring .debug_names."),
5021 filename, padding);
5022 return false;
5023 }
5024
5025 /* comp_unit_count - The number of CUs in the CU list. */
5026 map.cu_count = read_4_bytes (abfd, addr);
5027 addr += 4;
5028
5029 /* local_type_unit_count - The number of TUs in the local TU
5030 list. */
5031 map.tu_count = read_4_bytes (abfd, addr);
5032 addr += 4;
5033
5034 /* foreign_type_unit_count - The number of TUs in the foreign TU
5035 list. */
5036 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5037 addr += 4;
5038 if (foreign_tu_count != 0)
5039 {
5040 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5041 "ignoring .debug_names."),
5042 filename, static_cast<unsigned long> (foreign_tu_count));
5043 return false;
5044 }
5045
5046 /* bucket_count - The number of hash buckets in the hash lookup
5047 table. */
5048 map.bucket_count = read_4_bytes (abfd, addr);
5049 addr += 4;
5050
5051 /* name_count - The number of unique names in the index. */
5052 map.name_count = read_4_bytes (abfd, addr);
5053 addr += 4;
5054
5055 /* abbrev_table_size - The size in bytes of the abbreviations
5056 table. */
5057 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5058 addr += 4;
5059
5060 /* augmentation_string_size - The size in bytes of the augmentation
5061 string. This value is rounded up to a multiple of 4. */
5062 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5063 addr += 4;
5064 map.augmentation_is_gdb = ((augmentation_string_size
5065 == sizeof (dwarf5_augmentation))
5066 && memcmp (addr, dwarf5_augmentation,
5067 sizeof (dwarf5_augmentation)) == 0);
5068 augmentation_string_size += (-augmentation_string_size) & 3;
5069 addr += augmentation_string_size;
5070
5071 /* List of CUs */
5072 map.cu_table_reordered = addr;
5073 addr += map.cu_count * map.offset_size;
5074
5075 /* List of Local TUs */
5076 map.tu_table_reordered = addr;
5077 addr += map.tu_count * map.offset_size;
5078
5079 /* Hash Lookup Table */
5080 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5081 addr += map.bucket_count * 4;
5082 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5083 addr += map.name_count * 4;
5084
5085 /* Name Table */
5086 map.name_table_string_offs_reordered = addr;
5087 addr += map.name_count * map.offset_size;
5088 map.name_table_entry_offs_reordered = addr;
5089 addr += map.name_count * map.offset_size;
5090
5091 const gdb_byte *abbrev_table_start = addr;
5092 for (;;)
5093 {
5094 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5095 addr += bytes_read;
5096 if (index_num == 0)
5097 break;
5098
5099 const auto insertpair
5100 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5101 if (!insertpair.second)
5102 {
5103 warning (_("Section .debug_names in %s has duplicate index %s, "
5104 "ignoring .debug_names."),
5105 filename, pulongest (index_num));
5106 return false;
5107 }
5108 mapped_debug_names::index_val &indexval = insertpair.first->second;
5109 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5110 addr += bytes_read;
5111
5112 for (;;)
5113 {
5114 mapped_debug_names::index_val::attr attr;
5115 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5116 addr += bytes_read;
5117 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5118 addr += bytes_read;
5119 if (attr.form == DW_FORM_implicit_const)
5120 {
5121 attr.implicit_const = read_signed_leb128 (abfd, addr,
5122 &bytes_read);
5123 addr += bytes_read;
5124 }
5125 if (attr.dw_idx == 0 && attr.form == 0)
5126 break;
5127 indexval.attr_vec.push_back (std::move (attr));
5128 }
5129 }
5130 if (addr != abbrev_table_start + abbrev_table_size)
5131 {
5132 warning (_("Section .debug_names in %s has abbreviation_table "
5133 "of size %s vs. written as %u, ignoring .debug_names."),
5134 filename, plongest (addr - abbrev_table_start),
5135 abbrev_table_size);
5136 return false;
5137 }
5138 map.entry_pool = addr;
5139
5140 return true;
5141 }
5142
5143 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5144 list. */
5145
5146 static void
5147 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5148 const mapped_debug_names &map,
5149 dwarf2_section_info &section,
5150 bool is_dwz)
5151 {
5152 if (!map.augmentation_is_gdb)
5153 {
5154 for (uint32_t i = 0; i < map.cu_count; ++i)
5155 {
5156 sect_offset sect_off
5157 = (sect_offset) (extract_unsigned_integer
5158 (map.cu_table_reordered + i * map.offset_size,
5159 map.offset_size,
5160 map.dwarf5_byte_order));
5161 /* We don't know the length of the CU, because the CU list in a
5162 .debug_names index can be incomplete, so we can't use the start of
5163 the next CU as end of this CU. We create the CUs here with length 0,
5164 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5165 dwarf2_per_cu_data *per_cu
5166 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5167 per_bfd->all_comp_units.push_back (per_cu);
5168 }
5169 }
5170
5171 sect_offset sect_off_prev;
5172 for (uint32_t i = 0; i <= map.cu_count; ++i)
5173 {
5174 sect_offset sect_off_next;
5175 if (i < map.cu_count)
5176 {
5177 sect_off_next
5178 = (sect_offset) (extract_unsigned_integer
5179 (map.cu_table_reordered + i * map.offset_size,
5180 map.offset_size,
5181 map.dwarf5_byte_order));
5182 }
5183 else
5184 sect_off_next = (sect_offset) section.size;
5185 if (i >= 1)
5186 {
5187 const ULONGEST length = sect_off_next - sect_off_prev;
5188 dwarf2_per_cu_data *per_cu
5189 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5190 sect_off_prev, length);
5191 per_bfd->all_comp_units.push_back (per_cu);
5192 }
5193 sect_off_prev = sect_off_next;
5194 }
5195 }
5196
5197 /* Read the CU list from the mapped index, and use it to create all
5198 the CU objects for this dwarf2_per_objfile. */
5199
5200 static void
5201 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5202 const mapped_debug_names &map,
5203 const mapped_debug_names &dwz_map)
5204 {
5205 gdb_assert (per_bfd->all_comp_units.empty ());
5206 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5207
5208 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5209 false /* is_dwz */);
5210
5211 if (dwz_map.cu_count == 0)
5212 return;
5213
5214 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5215 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5216 true /* is_dwz */);
5217 }
5218
5219 /* Read .debug_names. If everything went ok, initialize the "quick"
5220 elements of all the CUs and return true. Otherwise, return false. */
5221
5222 static bool
5223 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5224 {
5225 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5226 mapped_debug_names dwz_map;
5227 struct objfile *objfile = dwarf2_per_objfile->objfile;
5228
5229 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5230 &dwarf2_per_objfile->per_bfd->debug_names,
5231 *map))
5232 return false;
5233
5234 /* Don't use the index if it's empty. */
5235 if (map->name_count == 0)
5236 return false;
5237
5238 /* If there is a .dwz file, read it so we can get its CU list as
5239 well. */
5240 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5241 if (dwz != NULL)
5242 {
5243 if (!read_debug_names_from_section (objfile,
5244 bfd_get_filename (dwz->dwz_bfd.get ()),
5245 &dwz->debug_names, dwz_map))
5246 {
5247 warning (_("could not read '.debug_names' section from %s; skipping"),
5248 bfd_get_filename (dwz->dwz_bfd.get ()));
5249 return false;
5250 }
5251 }
5252
5253 create_cus_from_debug_names (dwarf2_per_objfile->per_bfd, *map, dwz_map);
5254
5255 if (map->tu_count != 0)
5256 {
5257 /* We can only handle a single .debug_types when we have an
5258 index. */
5259 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5260 return false;
5261
5262 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5263
5264 create_signatured_type_table_from_debug_names
5265 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5266 }
5267
5268 create_addrmap_from_aranges (dwarf2_per_objfile,
5269 &dwarf2_per_objfile->per_bfd->debug_aranges);
5270
5271 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5272 dwarf2_per_objfile->per_bfd->using_index = 1;
5273 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5274 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5275
5276 return true;
5277 }
5278
5279 /* Type used to manage iterating over all CUs looking for a symbol for
5280 .debug_names. */
5281
5282 class dw2_debug_names_iterator
5283 {
5284 public:
5285 dw2_debug_names_iterator (const mapped_debug_names &map,
5286 gdb::optional<block_enum> block_index,
5287 domain_enum domain,
5288 const char *name, dwarf2_per_objfile *per_objfile)
5289 : m_map (map), m_block_index (block_index), m_domain (domain),
5290 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5291 m_per_objfile (per_objfile)
5292 {}
5293
5294 dw2_debug_names_iterator (const mapped_debug_names &map,
5295 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5296 : m_map (map),
5297 m_search (search),
5298 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5299 m_per_objfile (per_objfile)
5300 {}
5301
5302 dw2_debug_names_iterator (const mapped_debug_names &map,
5303 block_enum block_index, domain_enum domain,
5304 uint32_t namei, dwarf2_per_objfile *per_objfile)
5305 : m_map (map), m_block_index (block_index), m_domain (domain),
5306 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5307 m_per_objfile (per_objfile)
5308 {}
5309
5310 /* Return the next matching CU or NULL if there are no more. */
5311 dwarf2_per_cu_data *next ();
5312
5313 private:
5314 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5315 const char *name, dwarf2_per_objfile *per_objfile);
5316 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5317 uint32_t namei, dwarf2_per_objfile *per_objfile);
5318
5319 /* The internalized form of .debug_names. */
5320 const mapped_debug_names &m_map;
5321
5322 /* If set, only look for symbols that match that block. Valid values are
5323 GLOBAL_BLOCK and STATIC_BLOCK. */
5324 const gdb::optional<block_enum> m_block_index;
5325
5326 /* The kind of symbol we're looking for. */
5327 const domain_enum m_domain = UNDEF_DOMAIN;
5328 const search_domain m_search = ALL_DOMAIN;
5329
5330 /* The list of CUs from the index entry of the symbol, or NULL if
5331 not found. */
5332 const gdb_byte *m_addr;
5333
5334 dwarf2_per_objfile *m_per_objfile;
5335 };
5336
5337 const char *
5338 mapped_debug_names::namei_to_name
5339 (uint32_t namei, dwarf2_per_objfile *dwarf2_per_objfile) const
5340 {
5341 const ULONGEST namei_string_offs
5342 = extract_unsigned_integer ((name_table_string_offs_reordered
5343 + namei * offset_size),
5344 offset_size,
5345 dwarf5_byte_order);
5346 return read_indirect_string_at_offset (dwarf2_per_objfile,
5347 namei_string_offs);
5348 }
5349
5350 /* Find a slot in .debug_names for the object named NAME. If NAME is
5351 found, return pointer to its pool data. If NAME cannot be found,
5352 return NULL. */
5353
5354 const gdb_byte *
5355 dw2_debug_names_iterator::find_vec_in_debug_names
5356 (const mapped_debug_names &map, const char *name, dwarf2_per_objfile *per_objfile)
5357 {
5358 int (*cmp) (const char *, const char *);
5359
5360 gdb::unique_xmalloc_ptr<char> without_params;
5361 if (current_language->la_language == language_cplus
5362 || current_language->la_language == language_fortran
5363 || current_language->la_language == language_d)
5364 {
5365 /* NAME is already canonical. Drop any qualifiers as
5366 .debug_names does not contain any. */
5367
5368 if (strchr (name, '(') != NULL)
5369 {
5370 without_params = cp_remove_params (name);
5371 if (without_params != NULL)
5372 name = without_params.get ();
5373 }
5374 }
5375
5376 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5377
5378 const uint32_t full_hash = dwarf5_djb_hash (name);
5379 uint32_t namei
5380 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5381 (map.bucket_table_reordered
5382 + (full_hash % map.bucket_count)), 4,
5383 map.dwarf5_byte_order);
5384 if (namei == 0)
5385 return NULL;
5386 --namei;
5387 if (namei >= map.name_count)
5388 {
5389 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5390 "[in module %s]"),
5391 namei, map.name_count,
5392 objfile_name (per_objfile->objfile));
5393 return NULL;
5394 }
5395
5396 for (;;)
5397 {
5398 const uint32_t namei_full_hash
5399 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5400 (map.hash_table_reordered + namei), 4,
5401 map.dwarf5_byte_order);
5402 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5403 return NULL;
5404
5405 if (full_hash == namei_full_hash)
5406 {
5407 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5408
5409 #if 0 /* An expensive sanity check. */
5410 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5411 {
5412 complaint (_("Wrong .debug_names hash for string at index %u "
5413 "[in module %s]"),
5414 namei, objfile_name (dwarf2_per_objfile->objfile));
5415 return NULL;
5416 }
5417 #endif
5418
5419 if (cmp (namei_string, name) == 0)
5420 {
5421 const ULONGEST namei_entry_offs
5422 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5423 + namei * map.offset_size),
5424 map.offset_size, map.dwarf5_byte_order);
5425 return map.entry_pool + namei_entry_offs;
5426 }
5427 }
5428
5429 ++namei;
5430 if (namei >= map.name_count)
5431 return NULL;
5432 }
5433 }
5434
5435 const gdb_byte *
5436 dw2_debug_names_iterator::find_vec_in_debug_names
5437 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5438 {
5439 if (namei >= map.name_count)
5440 {
5441 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5442 "[in module %s]"),
5443 namei, map.name_count,
5444 objfile_name (per_objfile->objfile));
5445 return NULL;
5446 }
5447
5448 const ULONGEST namei_entry_offs
5449 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5450 + namei * map.offset_size),
5451 map.offset_size, map.dwarf5_byte_order);
5452 return map.entry_pool + namei_entry_offs;
5453 }
5454
5455 /* See dw2_debug_names_iterator. */
5456
5457 dwarf2_per_cu_data *
5458 dw2_debug_names_iterator::next ()
5459 {
5460 if (m_addr == NULL)
5461 return NULL;
5462
5463 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5464 struct objfile *objfile = m_per_objfile->objfile;
5465 bfd *const abfd = objfile->obfd;
5466
5467 again:
5468
5469 unsigned int bytes_read;
5470 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5471 m_addr += bytes_read;
5472 if (abbrev == 0)
5473 return NULL;
5474
5475 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5476 if (indexval_it == m_map.abbrev_map.cend ())
5477 {
5478 complaint (_("Wrong .debug_names undefined abbrev code %s "
5479 "[in module %s]"),
5480 pulongest (abbrev), objfile_name (objfile));
5481 return NULL;
5482 }
5483 const mapped_debug_names::index_val &indexval = indexval_it->second;
5484 enum class symbol_linkage {
5485 unknown,
5486 static_,
5487 extern_,
5488 } symbol_linkage_ = symbol_linkage::unknown;
5489 dwarf2_per_cu_data *per_cu = NULL;
5490 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5491 {
5492 ULONGEST ull;
5493 switch (attr.form)
5494 {
5495 case DW_FORM_implicit_const:
5496 ull = attr.implicit_const;
5497 break;
5498 case DW_FORM_flag_present:
5499 ull = 1;
5500 break;
5501 case DW_FORM_udata:
5502 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5503 m_addr += bytes_read;
5504 break;
5505 case DW_FORM_ref4:
5506 ull = read_4_bytes (abfd, m_addr);
5507 m_addr += 4;
5508 break;
5509 case DW_FORM_ref8:
5510 ull = read_8_bytes (abfd, m_addr);
5511 m_addr += 8;
5512 break;
5513 case DW_FORM_ref_sig8:
5514 ull = read_8_bytes (abfd, m_addr);
5515 m_addr += 8;
5516 break;
5517 default:
5518 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5519 dwarf_form_name (attr.form),
5520 objfile_name (objfile));
5521 return NULL;
5522 }
5523 switch (attr.dw_idx)
5524 {
5525 case DW_IDX_compile_unit:
5526 /* Don't crash on bad data. */
5527 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5528 {
5529 complaint (_(".debug_names entry has bad CU index %s"
5530 " [in module %s]"),
5531 pulongest (ull),
5532 objfile_name (objfile));
5533 continue;
5534 }
5535 per_cu = per_bfd->get_cutu (ull);
5536 break;
5537 case DW_IDX_type_unit:
5538 /* Don't crash on bad data. */
5539 if (ull >= per_bfd->all_type_units.size ())
5540 {
5541 complaint (_(".debug_names entry has bad TU index %s"
5542 " [in module %s]"),
5543 pulongest (ull),
5544 objfile_name (objfile));
5545 continue;
5546 }
5547 per_cu = &per_bfd->get_tu (ull)->per_cu;
5548 break;
5549 case DW_IDX_die_offset:
5550 /* In a per-CU index (as opposed to a per-module index), index
5551 entries without CU attribute implicitly refer to the single CU. */
5552 if (per_cu == NULL)
5553 per_cu = per_bfd->get_cu (0);
5554 break;
5555 case DW_IDX_GNU_internal:
5556 if (!m_map.augmentation_is_gdb)
5557 break;
5558 symbol_linkage_ = symbol_linkage::static_;
5559 break;
5560 case DW_IDX_GNU_external:
5561 if (!m_map.augmentation_is_gdb)
5562 break;
5563 symbol_linkage_ = symbol_linkage::extern_;
5564 break;
5565 }
5566 }
5567
5568 /* Skip if already read in. */
5569 if (m_per_objfile->symtab_set_p (per_cu))
5570 goto again;
5571
5572 /* Check static vs global. */
5573 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5574 {
5575 const bool want_static = *m_block_index == STATIC_BLOCK;
5576 const bool symbol_is_static =
5577 symbol_linkage_ == symbol_linkage::static_;
5578 if (want_static != symbol_is_static)
5579 goto again;
5580 }
5581
5582 /* Match dw2_symtab_iter_next, symbol_kind
5583 and debug_names::psymbol_tag. */
5584 switch (m_domain)
5585 {
5586 case VAR_DOMAIN:
5587 switch (indexval.dwarf_tag)
5588 {
5589 case DW_TAG_variable:
5590 case DW_TAG_subprogram:
5591 /* Some types are also in VAR_DOMAIN. */
5592 case DW_TAG_typedef:
5593 case DW_TAG_structure_type:
5594 break;
5595 default:
5596 goto again;
5597 }
5598 break;
5599 case STRUCT_DOMAIN:
5600 switch (indexval.dwarf_tag)
5601 {
5602 case DW_TAG_typedef:
5603 case DW_TAG_structure_type:
5604 break;
5605 default:
5606 goto again;
5607 }
5608 break;
5609 case LABEL_DOMAIN:
5610 switch (indexval.dwarf_tag)
5611 {
5612 case 0:
5613 case DW_TAG_variable:
5614 break;
5615 default:
5616 goto again;
5617 }
5618 break;
5619 case MODULE_DOMAIN:
5620 switch (indexval.dwarf_tag)
5621 {
5622 case DW_TAG_module:
5623 break;
5624 default:
5625 goto again;
5626 }
5627 break;
5628 default:
5629 break;
5630 }
5631
5632 /* Match dw2_expand_symtabs_matching, symbol_kind and
5633 debug_names::psymbol_tag. */
5634 switch (m_search)
5635 {
5636 case VARIABLES_DOMAIN:
5637 switch (indexval.dwarf_tag)
5638 {
5639 case DW_TAG_variable:
5640 break;
5641 default:
5642 goto again;
5643 }
5644 break;
5645 case FUNCTIONS_DOMAIN:
5646 switch (indexval.dwarf_tag)
5647 {
5648 case DW_TAG_subprogram:
5649 break;
5650 default:
5651 goto again;
5652 }
5653 break;
5654 case TYPES_DOMAIN:
5655 switch (indexval.dwarf_tag)
5656 {
5657 case DW_TAG_typedef:
5658 case DW_TAG_structure_type:
5659 break;
5660 default:
5661 goto again;
5662 }
5663 break;
5664 case MODULES_DOMAIN:
5665 switch (indexval.dwarf_tag)
5666 {
5667 case DW_TAG_module:
5668 break;
5669 default:
5670 goto again;
5671 }
5672 default:
5673 break;
5674 }
5675
5676 return per_cu;
5677 }
5678
5679 static struct compunit_symtab *
5680 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5681 const char *name, domain_enum domain)
5682 {
5683 struct dwarf2_per_objfile *dwarf2_per_objfile
5684 = get_dwarf2_per_objfile (objfile);
5685
5686 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5687 if (!mapp)
5688 {
5689 /* index is NULL if OBJF_READNOW. */
5690 return NULL;
5691 }
5692 const auto &map = *mapp;
5693
5694 dw2_debug_names_iterator iter (map, block_index, domain, name,
5695 dwarf2_per_objfile);
5696
5697 struct compunit_symtab *stab_best = NULL;
5698 struct dwarf2_per_cu_data *per_cu;
5699 while ((per_cu = iter.next ()) != NULL)
5700 {
5701 struct symbol *sym, *with_opaque = NULL;
5702 compunit_symtab *stab
5703 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5704 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5705 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5706
5707 sym = block_find_symbol (block, name, domain,
5708 block_find_non_opaque_type_preferred,
5709 &with_opaque);
5710
5711 /* Some caution must be observed with overloaded functions and
5712 methods, since the index will not contain any overload
5713 information (but NAME might contain it). */
5714
5715 if (sym != NULL
5716 && strcmp_iw (sym->search_name (), name) == 0)
5717 return stab;
5718 if (with_opaque != NULL
5719 && strcmp_iw (with_opaque->search_name (), name) == 0)
5720 stab_best = stab;
5721
5722 /* Keep looking through other CUs. */
5723 }
5724
5725 return stab_best;
5726 }
5727
5728 /* This dumps minimal information about .debug_names. It is called
5729 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5730 uses this to verify that .debug_names has been loaded. */
5731
5732 static void
5733 dw2_debug_names_dump (struct objfile *objfile)
5734 {
5735 struct dwarf2_per_objfile *dwarf2_per_objfile
5736 = get_dwarf2_per_objfile (objfile);
5737
5738 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5739 printf_filtered (".debug_names:");
5740 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5741 printf_filtered (" exists\n");
5742 else
5743 printf_filtered (" faked for \"readnow\"\n");
5744 printf_filtered ("\n");
5745 }
5746
5747 static void
5748 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5749 const char *func_name)
5750 {
5751 struct dwarf2_per_objfile *dwarf2_per_objfile
5752 = get_dwarf2_per_objfile (objfile);
5753
5754 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5755 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5756 {
5757 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5758
5759 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5760 dwarf2_per_objfile);
5761
5762 struct dwarf2_per_cu_data *per_cu;
5763 while ((per_cu = iter.next ()) != NULL)
5764 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5765 }
5766 }
5767
5768 static void
5769 dw2_debug_names_map_matching_symbols
5770 (struct objfile *objfile,
5771 const lookup_name_info &name, domain_enum domain,
5772 int global,
5773 gdb::function_view<symbol_found_callback_ftype> callback,
5774 symbol_compare_ftype *ordered_compare)
5775 {
5776 struct dwarf2_per_objfile *dwarf2_per_objfile
5777 = get_dwarf2_per_objfile (objfile);
5778
5779 /* debug_names_table is NULL if OBJF_READNOW. */
5780 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5781 return;
5782
5783 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5784 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5785
5786 const char *match_name = name.ada ().lookup_name ().c_str ();
5787 auto matcher = [&] (const char *symname)
5788 {
5789 if (ordered_compare == nullptr)
5790 return true;
5791 return ordered_compare (symname, match_name) == 0;
5792 };
5793
5794 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5795 [&] (offset_type namei)
5796 {
5797 /* The name was matched, now expand corresponding CUs that were
5798 marked. */
5799 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5800 dwarf2_per_objfile);
5801
5802 struct dwarf2_per_cu_data *per_cu;
5803 while ((per_cu = iter.next ()) != NULL)
5804 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5805 nullptr);
5806 return true;
5807 }, dwarf2_per_objfile);
5808
5809 /* It's a shame we couldn't do this inside the
5810 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5811 that have already been expanded. Instead, this loop matches what
5812 the psymtab code does. */
5813 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5814 {
5815 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5816 if (symtab != nullptr)
5817 {
5818 const struct block *block
5819 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5820 if (!iterate_over_symbols_terminated (block, name,
5821 domain, callback))
5822 break;
5823 }
5824 }
5825 }
5826
5827 static void
5828 dw2_debug_names_expand_symtabs_matching
5829 (struct objfile *objfile,
5830 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5831 const lookup_name_info *lookup_name,
5832 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5833 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5834 enum search_domain kind)
5835 {
5836 struct dwarf2_per_objfile *dwarf2_per_objfile
5837 = get_dwarf2_per_objfile (objfile);
5838
5839 /* debug_names_table is NULL if OBJF_READNOW. */
5840 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5841 return;
5842
5843 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5844
5845 if (symbol_matcher == NULL && lookup_name == NULL)
5846 {
5847 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5848 {
5849 QUIT;
5850
5851 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5852 file_matcher, expansion_notify);
5853 }
5854 return;
5855 }
5856
5857 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5858
5859 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5860 symbol_matcher,
5861 kind, [&] (offset_type namei)
5862 {
5863 /* The name was matched, now expand corresponding CUs that were
5864 marked. */
5865 dw2_debug_names_iterator iter (map, kind, namei, dwarf2_per_objfile);
5866
5867 struct dwarf2_per_cu_data *per_cu;
5868 while ((per_cu = iter.next ()) != NULL)
5869 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5870 file_matcher, expansion_notify);
5871 return true;
5872 }, dwarf2_per_objfile);
5873 }
5874
5875 const struct quick_symbol_functions dwarf2_debug_names_functions =
5876 {
5877 dw2_has_symbols,
5878 dw2_find_last_source_symtab,
5879 dw2_forget_cached_source_info,
5880 dw2_map_symtabs_matching_filename,
5881 dw2_debug_names_lookup_symbol,
5882 NULL,
5883 dw2_print_stats,
5884 dw2_debug_names_dump,
5885 dw2_debug_names_expand_symtabs_for_function,
5886 dw2_expand_all_symtabs,
5887 dw2_expand_symtabs_with_fullname,
5888 dw2_debug_names_map_matching_symbols,
5889 dw2_debug_names_expand_symtabs_matching,
5890 dw2_find_pc_sect_compunit_symtab,
5891 NULL,
5892 dw2_map_symbol_filenames
5893 };
5894
5895 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5896 to either a dwarf2_per_bfd or dwz_file object. */
5897
5898 template <typename T>
5899 static gdb::array_view<const gdb_byte>
5900 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5901 {
5902 dwarf2_section_info *section = &section_owner->gdb_index;
5903
5904 if (section->empty ())
5905 return {};
5906
5907 /* Older elfutils strip versions could keep the section in the main
5908 executable while splitting it for the separate debug info file. */
5909 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5910 return {};
5911
5912 section->read (obj);
5913
5914 /* dwarf2_section_info::size is a bfd_size_type, while
5915 gdb::array_view works with size_t. On 32-bit hosts, with
5916 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5917 is 32-bit. So we need an explicit narrowing conversion here.
5918 This is fine, because it's impossible to allocate or mmap an
5919 array/buffer larger than what size_t can represent. */
5920 return gdb::make_array_view (section->buffer, section->size);
5921 }
5922
5923 /* Lookup the index cache for the contents of the index associated to
5924 DWARF2_OBJ. */
5925
5926 static gdb::array_view<const gdb_byte>
5927 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5928 {
5929 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5930 if (build_id == nullptr)
5931 return {};
5932
5933 return global_index_cache.lookup_gdb_index (build_id,
5934 &dwarf2_per_bfd->index_cache_res);
5935 }
5936
5937 /* Same as the above, but for DWZ. */
5938
5939 static gdb::array_view<const gdb_byte>
5940 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5941 {
5942 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5943 if (build_id == nullptr)
5944 return {};
5945
5946 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5947 }
5948
5949 /* See symfile.h. */
5950
5951 bool
5952 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5953 {
5954 struct dwarf2_per_objfile *dwarf2_per_objfile
5955 = get_dwarf2_per_objfile (objfile);
5956 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
5957
5958 /* If we're about to read full symbols, don't bother with the
5959 indices. In this case we also don't care if some other debug
5960 format is making psymtabs, because they are all about to be
5961 expanded anyway. */
5962 if ((objfile->flags & OBJF_READNOW))
5963 {
5964 /* When using READNOW, the using_index flag (set below) indicates that
5965 PER_BFD was already initialized, when we loaded some other objfile. */
5966 if (per_bfd->using_index)
5967 {
5968 *index_kind = dw_index_kind::GDB_INDEX;
5969 dwarf2_per_objfile->resize_symtabs ();
5970 return true;
5971 }
5972
5973 per_bfd->using_index = 1;
5974 create_all_comp_units (dwarf2_per_objfile);
5975 create_all_type_units (dwarf2_per_objfile);
5976 per_bfd->quick_file_names_table
5977 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5978 dwarf2_per_objfile->resize_symtabs ();
5979
5980 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5981 + per_bfd->all_type_units.size ()); ++i)
5982 {
5983 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
5984
5985 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5986 struct dwarf2_per_cu_quick_data);
5987 }
5988
5989 /* Return 1 so that gdb sees the "quick" functions. However,
5990 these functions will be no-ops because we will have expanded
5991 all symtabs. */
5992 *index_kind = dw_index_kind::GDB_INDEX;
5993 return true;
5994 }
5995
5996 /* Was a debug names index already read when we processed an objfile sharing
5997 PER_BFD? */
5998 if (per_bfd->debug_names_table != nullptr)
5999 {
6000 *index_kind = dw_index_kind::DEBUG_NAMES;
6001 dwarf2_per_objfile->resize_symtabs ();
6002 return true;
6003 }
6004
6005 /* Was a GDB index already read when we processed an objfile sharing
6006 PER_BFD? */
6007 if (per_bfd->index_table != nullptr)
6008 {
6009 *index_kind = dw_index_kind::GDB_INDEX;
6010 dwarf2_per_objfile->resize_symtabs ();
6011 return true;
6012 }
6013
6014 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6015 {
6016 *index_kind = dw_index_kind::DEBUG_NAMES;
6017 dwarf2_per_objfile->resize_symtabs ();
6018 return true;
6019 }
6020
6021 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6022 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6023 get_gdb_index_contents_from_section<dwz_file>))
6024 {
6025 *index_kind = dw_index_kind::GDB_INDEX;
6026 dwarf2_per_objfile->resize_symtabs ();
6027 return true;
6028 }
6029
6030 /* ... otherwise, try to find the index in the index cache. */
6031 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6032 get_gdb_index_contents_from_cache,
6033 get_gdb_index_contents_from_cache_dwz))
6034 {
6035 global_index_cache.hit ();
6036 *index_kind = dw_index_kind::GDB_INDEX;
6037 dwarf2_per_objfile->resize_symtabs ();
6038 return true;
6039 }
6040
6041 global_index_cache.miss ();
6042 return false;
6043 }
6044
6045 \f
6046
6047 /* Build a partial symbol table. */
6048
6049 void
6050 dwarf2_build_psymtabs (struct objfile *objfile)
6051 {
6052 struct dwarf2_per_objfile *dwarf2_per_objfile
6053 = get_dwarf2_per_objfile (objfile);
6054 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6055
6056 if (per_bfd->partial_symtabs != nullptr)
6057 {
6058 /* Partial symbols were already read, so now we can simply
6059 attach them. */
6060 objfile->partial_symtabs = per_bfd->partial_symtabs;
6061 dwarf2_per_objfile->resize_symtabs ();
6062 return;
6063 }
6064
6065 init_psymbol_list (objfile, 1024);
6066
6067 try
6068 {
6069 /* This isn't really ideal: all the data we allocate on the
6070 objfile's obstack is still uselessly kept around. However,
6071 freeing it seems unsafe. */
6072 psymtab_discarder psymtabs (objfile);
6073 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6074 psymtabs.keep ();
6075
6076 dwarf2_per_objfile->resize_symtabs ();
6077
6078 /* (maybe) store an index in the cache. */
6079 global_index_cache.store (dwarf2_per_objfile);
6080 }
6081 catch (const gdb_exception_error &except)
6082 {
6083 exception_print (gdb_stderr, except);
6084 }
6085
6086 /* Finish by setting the local reference to partial symtabs, so that
6087 we don't try to read them again if reading another objfile with the same
6088 BFD. If we can't in fact share, this won't make a difference anyway as
6089 the dwarf2_per_bfd object won't be shared. */
6090 per_bfd->partial_symtabs = objfile->partial_symtabs;
6091 }
6092
6093 /* Find the base address of the compilation unit for range lists and
6094 location lists. It will normally be specified by DW_AT_low_pc.
6095 In DWARF-3 draft 4, the base address could be overridden by
6096 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6097 compilation units with discontinuous ranges. */
6098
6099 static void
6100 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6101 {
6102 struct attribute *attr;
6103
6104 cu->base_address.reset ();
6105
6106 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6107 if (attr != nullptr)
6108 cu->base_address = attr->value_as_address ();
6109 else
6110 {
6111 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6112 if (attr != nullptr)
6113 cu->base_address = attr->value_as_address ();
6114 }
6115 }
6116
6117 /* Helper function that returns the proper abbrev section for
6118 THIS_CU. */
6119
6120 static struct dwarf2_section_info *
6121 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6122 {
6123 struct dwarf2_section_info *abbrev;
6124 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6125
6126 if (this_cu->is_dwz)
6127 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6128 else
6129 abbrev = &per_bfd->abbrev;
6130
6131 return abbrev;
6132 }
6133
6134 /* Fetch the abbreviation table offset from a comp or type unit header. */
6135
6136 static sect_offset
6137 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6138 struct dwarf2_section_info *section,
6139 sect_offset sect_off)
6140 {
6141 bfd *abfd = section->get_bfd_owner ();
6142 const gdb_byte *info_ptr;
6143 unsigned int initial_length_size, offset_size;
6144 uint16_t version;
6145
6146 section->read (dwarf2_per_objfile->objfile);
6147 info_ptr = section->buffer + to_underlying (sect_off);
6148 read_initial_length (abfd, info_ptr, &initial_length_size);
6149 offset_size = initial_length_size == 4 ? 4 : 8;
6150 info_ptr += initial_length_size;
6151
6152 version = read_2_bytes (abfd, info_ptr);
6153 info_ptr += 2;
6154 if (version >= 5)
6155 {
6156 /* Skip unit type and address size. */
6157 info_ptr += 2;
6158 }
6159
6160 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6161 }
6162
6163 /* A partial symtab that is used only for include files. */
6164 struct dwarf2_include_psymtab : public partial_symtab
6165 {
6166 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6167 : partial_symtab (filename, objfile)
6168 {
6169 }
6170
6171 void read_symtab (struct objfile *objfile) override
6172 {
6173 /* It's an include file, no symbols to read for it.
6174 Everything is in the includer symtab. */
6175
6176 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6177 expansion of the includer psymtab. We use the dependencies[0] field to
6178 model the includer. But if we go the regular route of calling
6179 expand_psymtab here, and having expand_psymtab call expand_dependencies
6180 to expand the includer, we'll only use expand_psymtab on the includer
6181 (making it a non-toplevel psymtab), while if we expand the includer via
6182 another path, we'll use read_symtab (making it a toplevel psymtab).
6183 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6184 psymtab, and trigger read_symtab on the includer here directly. */
6185 includer ()->read_symtab (objfile);
6186 }
6187
6188 void expand_psymtab (struct objfile *objfile) override
6189 {
6190 /* This is not called by read_symtab, and should not be called by any
6191 expand_dependencies. */
6192 gdb_assert (false);
6193 }
6194
6195 bool readin_p (struct objfile *objfile) const override
6196 {
6197 return includer ()->readin_p (objfile);
6198 }
6199
6200 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6201 {
6202 return nullptr;
6203 }
6204
6205 private:
6206 partial_symtab *includer () const
6207 {
6208 /* An include psymtab has exactly one dependency: the psymtab that
6209 includes it. */
6210 gdb_assert (this->number_of_dependencies == 1);
6211 return this->dependencies[0];
6212 }
6213 };
6214
6215 /* Allocate a new partial symtab for file named NAME and mark this new
6216 partial symtab as being an include of PST. */
6217
6218 static void
6219 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6220 struct objfile *objfile)
6221 {
6222 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6223
6224 if (!IS_ABSOLUTE_PATH (subpst->filename))
6225 subpst->dirname = pst->dirname;
6226
6227 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6228 subpst->dependencies[0] = pst;
6229 subpst->number_of_dependencies = 1;
6230 }
6231
6232 /* Read the Line Number Program data and extract the list of files
6233 included by the source file represented by PST. Build an include
6234 partial symtab for each of these included files. */
6235
6236 static void
6237 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6238 struct die_info *die,
6239 dwarf2_psymtab *pst)
6240 {
6241 line_header_up lh;
6242 struct attribute *attr;
6243
6244 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6245 if (attr != nullptr)
6246 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6247 if (lh == NULL)
6248 return; /* No linetable, so no includes. */
6249
6250 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6251 that we pass in the raw text_low here; that is ok because we're
6252 only decoding the line table to make include partial symtabs, and
6253 so the addresses aren't really used. */
6254 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6255 pst->raw_text_low (), 1);
6256 }
6257
6258 static hashval_t
6259 hash_signatured_type (const void *item)
6260 {
6261 const struct signatured_type *sig_type
6262 = (const struct signatured_type *) item;
6263
6264 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6265 return sig_type->signature;
6266 }
6267
6268 static int
6269 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6270 {
6271 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6272 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6273
6274 return lhs->signature == rhs->signature;
6275 }
6276
6277 /* Allocate a hash table for signatured types. */
6278
6279 static htab_up
6280 allocate_signatured_type_table ()
6281 {
6282 return htab_up (htab_create_alloc (41,
6283 hash_signatured_type,
6284 eq_signatured_type,
6285 NULL, xcalloc, xfree));
6286 }
6287
6288 /* A helper function to add a signatured type CU to a table. */
6289
6290 static int
6291 add_signatured_type_cu_to_table (void **slot, void *datum)
6292 {
6293 struct signatured_type *sigt = (struct signatured_type *) *slot;
6294 std::vector<signatured_type *> *all_type_units
6295 = (std::vector<signatured_type *> *) datum;
6296
6297 all_type_units->push_back (sigt);
6298
6299 return 1;
6300 }
6301
6302 /* A helper for create_debug_types_hash_table. Read types from SECTION
6303 and fill them into TYPES_HTAB. It will process only type units,
6304 therefore DW_UT_type. */
6305
6306 static void
6307 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6308 struct dwo_file *dwo_file,
6309 dwarf2_section_info *section, htab_up &types_htab,
6310 rcuh_kind section_kind)
6311 {
6312 struct objfile *objfile = dwarf2_per_objfile->objfile;
6313 struct dwarf2_section_info *abbrev_section;
6314 bfd *abfd;
6315 const gdb_byte *info_ptr, *end_ptr;
6316
6317 abbrev_section = (dwo_file != NULL
6318 ? &dwo_file->sections.abbrev
6319 : &dwarf2_per_objfile->per_bfd->abbrev);
6320
6321 if (dwarf_read_debug)
6322 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6323 section->get_name (),
6324 abbrev_section->get_file_name ());
6325
6326 section->read (objfile);
6327 info_ptr = section->buffer;
6328
6329 if (info_ptr == NULL)
6330 return;
6331
6332 /* We can't set abfd until now because the section may be empty or
6333 not present, in which case the bfd is unknown. */
6334 abfd = section->get_bfd_owner ();
6335
6336 /* We don't use cutu_reader here because we don't need to read
6337 any dies: the signature is in the header. */
6338
6339 end_ptr = info_ptr + section->size;
6340 while (info_ptr < end_ptr)
6341 {
6342 struct signatured_type *sig_type;
6343 struct dwo_unit *dwo_tu;
6344 void **slot;
6345 const gdb_byte *ptr = info_ptr;
6346 struct comp_unit_head header;
6347 unsigned int length;
6348
6349 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6350
6351 /* Initialize it due to a false compiler warning. */
6352 header.signature = -1;
6353 header.type_cu_offset_in_tu = (cu_offset) -1;
6354
6355 /* We need to read the type's signature in order to build the hash
6356 table, but we don't need anything else just yet. */
6357
6358 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6359 abbrev_section, ptr, section_kind);
6360
6361 length = header.get_length ();
6362
6363 /* Skip dummy type units. */
6364 if (ptr >= info_ptr + length
6365 || peek_abbrev_code (abfd, ptr) == 0
6366 || header.unit_type != DW_UT_type)
6367 {
6368 info_ptr += length;
6369 continue;
6370 }
6371
6372 if (types_htab == NULL)
6373 {
6374 if (dwo_file)
6375 types_htab = allocate_dwo_unit_table ();
6376 else
6377 types_htab = allocate_signatured_type_table ();
6378 }
6379
6380 if (dwo_file)
6381 {
6382 sig_type = NULL;
6383 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6384 struct dwo_unit);
6385 dwo_tu->dwo_file = dwo_file;
6386 dwo_tu->signature = header.signature;
6387 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6388 dwo_tu->section = section;
6389 dwo_tu->sect_off = sect_off;
6390 dwo_tu->length = length;
6391 }
6392 else
6393 {
6394 /* N.B.: type_offset is not usable if this type uses a DWO file.
6395 The real type_offset is in the DWO file. */
6396 dwo_tu = NULL;
6397 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6398 sig_type->signature = header.signature;
6399 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6400 sig_type->per_cu.is_debug_types = 1;
6401 sig_type->per_cu.section = section;
6402 sig_type->per_cu.sect_off = sect_off;
6403 sig_type->per_cu.length = length;
6404 }
6405
6406 slot = htab_find_slot (types_htab.get (),
6407 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6408 INSERT);
6409 gdb_assert (slot != NULL);
6410 if (*slot != NULL)
6411 {
6412 sect_offset dup_sect_off;
6413
6414 if (dwo_file)
6415 {
6416 const struct dwo_unit *dup_tu
6417 = (const struct dwo_unit *) *slot;
6418
6419 dup_sect_off = dup_tu->sect_off;
6420 }
6421 else
6422 {
6423 const struct signatured_type *dup_tu
6424 = (const struct signatured_type *) *slot;
6425
6426 dup_sect_off = dup_tu->per_cu.sect_off;
6427 }
6428
6429 complaint (_("debug type entry at offset %s is duplicate to"
6430 " the entry at offset %s, signature %s"),
6431 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6432 hex_string (header.signature));
6433 }
6434 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6435
6436 if (dwarf_read_debug > 1)
6437 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6438 sect_offset_str (sect_off),
6439 hex_string (header.signature));
6440
6441 info_ptr += length;
6442 }
6443 }
6444
6445 /* Create the hash table of all entries in the .debug_types
6446 (or .debug_types.dwo) section(s).
6447 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6448 otherwise it is NULL.
6449
6450 The result is a pointer to the hash table or NULL if there are no types.
6451
6452 Note: This function processes DWO files only, not DWP files. */
6453
6454 static void
6455 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6456 struct dwo_file *dwo_file,
6457 gdb::array_view<dwarf2_section_info> type_sections,
6458 htab_up &types_htab)
6459 {
6460 for (dwarf2_section_info &section : type_sections)
6461 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6462 types_htab, rcuh_kind::TYPE);
6463 }
6464
6465 /* Create the hash table of all entries in the .debug_types section,
6466 and initialize all_type_units.
6467 The result is zero if there is an error (e.g. missing .debug_types section),
6468 otherwise non-zero. */
6469
6470 static int
6471 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6472 {
6473 htab_up types_htab;
6474
6475 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6476 &dwarf2_per_objfile->per_bfd->info, types_htab,
6477 rcuh_kind::COMPILE);
6478 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6479 dwarf2_per_objfile->per_bfd->types, types_htab);
6480 if (types_htab == NULL)
6481 {
6482 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6483 return 0;
6484 }
6485
6486 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6487
6488 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6489 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6490 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6491
6492 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6493 add_signatured_type_cu_to_table,
6494 &dwarf2_per_objfile->per_bfd->all_type_units);
6495
6496 return 1;
6497 }
6498
6499 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6500 If SLOT is non-NULL, it is the entry to use in the hash table.
6501 Otherwise we find one. */
6502
6503 static struct signatured_type *
6504 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6505 void **slot)
6506 {
6507 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6508 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6509 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6510
6511 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6512
6513 dwarf2_per_objfile->resize_symtabs ();
6514
6515 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6516 sig_type->signature = sig;
6517 sig_type->per_cu.is_debug_types = 1;
6518 if (dwarf2_per_objfile->per_bfd->using_index)
6519 {
6520 sig_type->per_cu.v.quick =
6521 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6522 struct dwarf2_per_cu_quick_data);
6523 }
6524
6525 if (slot == NULL)
6526 {
6527 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6528 sig_type, INSERT);
6529 }
6530 gdb_assert (*slot == NULL);
6531 *slot = sig_type;
6532 /* The rest of sig_type must be filled in by the caller. */
6533 return sig_type;
6534 }
6535
6536 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6537 Fill in SIG_ENTRY with DWO_ENTRY. */
6538
6539 static void
6540 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6541 struct signatured_type *sig_entry,
6542 struct dwo_unit *dwo_entry)
6543 {
6544 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6545
6546 /* Make sure we're not clobbering something we don't expect to. */
6547 gdb_assert (! sig_entry->per_cu.queued);
6548 gdb_assert (dwarf2_per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6549 if (per_bfd->using_index)
6550 {
6551 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6552 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6553 }
6554 else
6555 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6556 gdb_assert (sig_entry->signature == dwo_entry->signature);
6557 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6558 gdb_assert (sig_entry->type_unit_group == NULL);
6559 gdb_assert (sig_entry->dwo_unit == NULL);
6560
6561 sig_entry->per_cu.section = dwo_entry->section;
6562 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6563 sig_entry->per_cu.length = dwo_entry->length;
6564 sig_entry->per_cu.reading_dwo_directly = 1;
6565 sig_entry->per_cu.per_bfd = per_bfd;
6566 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6567 sig_entry->dwo_unit = dwo_entry;
6568 }
6569
6570 /* Subroutine of lookup_signatured_type.
6571 If we haven't read the TU yet, create the signatured_type data structure
6572 for a TU to be read in directly from a DWO file, bypassing the stub.
6573 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6574 using .gdb_index, then when reading a CU we want to stay in the DWO file
6575 containing that CU. Otherwise we could end up reading several other DWO
6576 files (due to comdat folding) to process the transitive closure of all the
6577 mentioned TUs, and that can be slow. The current DWO file will have every
6578 type signature that it needs.
6579 We only do this for .gdb_index because in the psymtab case we already have
6580 to read all the DWOs to build the type unit groups. */
6581
6582 static struct signatured_type *
6583 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6584 {
6585 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6586 struct dwo_file *dwo_file;
6587 struct dwo_unit find_dwo_entry, *dwo_entry;
6588 struct signatured_type find_sig_entry, *sig_entry;
6589 void **slot;
6590
6591 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6592
6593 /* If TU skeletons have been removed then we may not have read in any
6594 TUs yet. */
6595 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6596 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6597
6598 /* We only ever need to read in one copy of a signatured type.
6599 Use the global signatured_types array to do our own comdat-folding
6600 of types. If this is the first time we're reading this TU, and
6601 the TU has an entry in .gdb_index, replace the recorded data from
6602 .gdb_index with this TU. */
6603
6604 find_sig_entry.signature = sig;
6605 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6606 &find_sig_entry, INSERT);
6607 sig_entry = (struct signatured_type *) *slot;
6608
6609 /* We can get here with the TU already read, *or* in the process of being
6610 read. Don't reassign the global entry to point to this DWO if that's
6611 the case. Also note that if the TU is already being read, it may not
6612 have come from a DWO, the program may be a mix of Fission-compiled
6613 code and non-Fission-compiled code. */
6614
6615 /* Have we already tried to read this TU?
6616 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6617 needn't exist in the global table yet). */
6618 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6619 return sig_entry;
6620
6621 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6622 dwo_unit of the TU itself. */
6623 dwo_file = cu->dwo_unit->dwo_file;
6624
6625 /* Ok, this is the first time we're reading this TU. */
6626 if (dwo_file->tus == NULL)
6627 return NULL;
6628 find_dwo_entry.signature = sig;
6629 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6630 &find_dwo_entry);
6631 if (dwo_entry == NULL)
6632 return NULL;
6633
6634 /* If the global table doesn't have an entry for this TU, add one. */
6635 if (sig_entry == NULL)
6636 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6637
6638 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6639 sig_entry->per_cu.tu_read = 1;
6640 return sig_entry;
6641 }
6642
6643 /* Subroutine of lookup_signatured_type.
6644 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6645 then try the DWP file. If the TU stub (skeleton) has been removed then
6646 it won't be in .gdb_index. */
6647
6648 static struct signatured_type *
6649 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6650 {
6651 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6652 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6653 struct dwo_unit *dwo_entry;
6654 struct signatured_type find_sig_entry, *sig_entry;
6655 void **slot;
6656
6657 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6658 gdb_assert (dwp_file != NULL);
6659
6660 /* If TU skeletons have been removed then we may not have read in any
6661 TUs yet. */
6662 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6663 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6664
6665 find_sig_entry.signature = sig;
6666 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6667 &find_sig_entry, INSERT);
6668 sig_entry = (struct signatured_type *) *slot;
6669
6670 /* Have we already tried to read this TU?
6671 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6672 needn't exist in the global table yet). */
6673 if (sig_entry != NULL)
6674 return sig_entry;
6675
6676 if (dwp_file->tus == NULL)
6677 return NULL;
6678 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6679 sig, 1 /* is_debug_types */);
6680 if (dwo_entry == NULL)
6681 return NULL;
6682
6683 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6684 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6685
6686 return sig_entry;
6687 }
6688
6689 /* Lookup a signature based type for DW_FORM_ref_sig8.
6690 Returns NULL if signature SIG is not present in the table.
6691 It is up to the caller to complain about this. */
6692
6693 static struct signatured_type *
6694 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6695 {
6696 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6697
6698 if (cu->dwo_unit
6699 && dwarf2_per_objfile->per_bfd->using_index)
6700 {
6701 /* We're in a DWO/DWP file, and we're using .gdb_index.
6702 These cases require special processing. */
6703 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6704 return lookup_dwo_signatured_type (cu, sig);
6705 else
6706 return lookup_dwp_signatured_type (cu, sig);
6707 }
6708 else
6709 {
6710 struct signatured_type find_entry, *entry;
6711
6712 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6713 return NULL;
6714 find_entry.signature = sig;
6715 entry = ((struct signatured_type *)
6716 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6717 &find_entry));
6718 return entry;
6719 }
6720 }
6721
6722 /* Low level DIE reading support. */
6723
6724 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6725
6726 static void
6727 init_cu_die_reader (struct die_reader_specs *reader,
6728 struct dwarf2_cu *cu,
6729 struct dwarf2_section_info *section,
6730 struct dwo_file *dwo_file,
6731 struct abbrev_table *abbrev_table)
6732 {
6733 gdb_assert (section->readin && section->buffer != NULL);
6734 reader->abfd = section->get_bfd_owner ();
6735 reader->cu = cu;
6736 reader->dwo_file = dwo_file;
6737 reader->die_section = section;
6738 reader->buffer = section->buffer;
6739 reader->buffer_end = section->buffer + section->size;
6740 reader->abbrev_table = abbrev_table;
6741 }
6742
6743 /* Subroutine of cutu_reader to simplify it.
6744 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6745 There's just a lot of work to do, and cutu_reader is big enough
6746 already.
6747
6748 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6749 from it to the DIE in the DWO. If NULL we are skipping the stub.
6750 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6751 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6752 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6753 STUB_COMP_DIR may be non-NULL.
6754 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6755 are filled in with the info of the DIE from the DWO file.
6756 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6757 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6758 kept around for at least as long as *RESULT_READER.
6759
6760 The result is non-zero if a valid (non-dummy) DIE was found. */
6761
6762 static int
6763 read_cutu_die_from_dwo (dwarf2_cu *cu,
6764 struct dwo_unit *dwo_unit,
6765 struct die_info *stub_comp_unit_die,
6766 const char *stub_comp_dir,
6767 struct die_reader_specs *result_reader,
6768 const gdb_byte **result_info_ptr,
6769 struct die_info **result_comp_unit_die,
6770 abbrev_table_up *result_dwo_abbrev_table)
6771 {
6772 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6773 dwarf2_per_cu_data *per_cu = cu->per_cu;
6774 struct objfile *objfile = dwarf2_per_objfile->objfile;
6775 bfd *abfd;
6776 const gdb_byte *begin_info_ptr, *info_ptr;
6777 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6778 int i,num_extra_attrs;
6779 struct dwarf2_section_info *dwo_abbrev_section;
6780 struct die_info *comp_unit_die;
6781
6782 /* At most one of these may be provided. */
6783 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6784
6785 /* These attributes aren't processed until later:
6786 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6787 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6788 referenced later. However, these attributes are found in the stub
6789 which we won't have later. In order to not impose this complication
6790 on the rest of the code, we read them here and copy them to the
6791 DWO CU/TU die. */
6792
6793 stmt_list = NULL;
6794 low_pc = NULL;
6795 high_pc = NULL;
6796 ranges = NULL;
6797 comp_dir = NULL;
6798
6799 if (stub_comp_unit_die != NULL)
6800 {
6801 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6802 DWO file. */
6803 if (!per_cu->is_debug_types)
6804 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6805 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6806 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6807 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6808 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6809
6810 cu->addr_base = stub_comp_unit_die->addr_base ();
6811
6812 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6813 here (if needed). We need the value before we can process
6814 DW_AT_ranges. */
6815 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6816 }
6817 else if (stub_comp_dir != NULL)
6818 {
6819 /* Reconstruct the comp_dir attribute to simplify the code below. */
6820 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6821 comp_dir->name = DW_AT_comp_dir;
6822 comp_dir->form = DW_FORM_string;
6823 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6824 DW_STRING (comp_dir) = stub_comp_dir;
6825 }
6826
6827 /* Set up for reading the DWO CU/TU. */
6828 cu->dwo_unit = dwo_unit;
6829 dwarf2_section_info *section = dwo_unit->section;
6830 section->read (objfile);
6831 abfd = section->get_bfd_owner ();
6832 begin_info_ptr = info_ptr = (section->buffer
6833 + to_underlying (dwo_unit->sect_off));
6834 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6835
6836 if (per_cu->is_debug_types)
6837 {
6838 signatured_type *sig_type = (struct signatured_type *) per_cu;
6839
6840 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6841 &cu->header, section,
6842 dwo_abbrev_section,
6843 info_ptr, rcuh_kind::TYPE);
6844 /* This is not an assert because it can be caused by bad debug info. */
6845 if (sig_type->signature != cu->header.signature)
6846 {
6847 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6848 " TU at offset %s [in module %s]"),
6849 hex_string (sig_type->signature),
6850 hex_string (cu->header.signature),
6851 sect_offset_str (dwo_unit->sect_off),
6852 bfd_get_filename (abfd));
6853 }
6854 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6855 /* For DWOs coming from DWP files, we don't know the CU length
6856 nor the type's offset in the TU until now. */
6857 dwo_unit->length = cu->header.get_length ();
6858 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6859
6860 /* Establish the type offset that can be used to lookup the type.
6861 For DWO files, we don't know it until now. */
6862 sig_type->type_offset_in_section
6863 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6864 }
6865 else
6866 {
6867 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6868 &cu->header, section,
6869 dwo_abbrev_section,
6870 info_ptr, rcuh_kind::COMPILE);
6871 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6872 /* For DWOs coming from DWP files, we don't know the CU length
6873 until now. */
6874 dwo_unit->length = cu->header.get_length ();
6875 }
6876
6877 *result_dwo_abbrev_table
6878 = abbrev_table::read (objfile, dwo_abbrev_section,
6879 cu->header.abbrev_sect_off);
6880 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6881 result_dwo_abbrev_table->get ());
6882
6883 /* Read in the die, but leave space to copy over the attributes
6884 from the stub. This has the benefit of simplifying the rest of
6885 the code - all the work to maintain the illusion of a single
6886 DW_TAG_{compile,type}_unit DIE is done here. */
6887 num_extra_attrs = ((stmt_list != NULL)
6888 + (low_pc != NULL)
6889 + (high_pc != NULL)
6890 + (ranges != NULL)
6891 + (comp_dir != NULL));
6892 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6893 num_extra_attrs);
6894
6895 /* Copy over the attributes from the stub to the DIE we just read in. */
6896 comp_unit_die = *result_comp_unit_die;
6897 i = comp_unit_die->num_attrs;
6898 if (stmt_list != NULL)
6899 comp_unit_die->attrs[i++] = *stmt_list;
6900 if (low_pc != NULL)
6901 comp_unit_die->attrs[i++] = *low_pc;
6902 if (high_pc != NULL)
6903 comp_unit_die->attrs[i++] = *high_pc;
6904 if (ranges != NULL)
6905 comp_unit_die->attrs[i++] = *ranges;
6906 if (comp_dir != NULL)
6907 comp_unit_die->attrs[i++] = *comp_dir;
6908 comp_unit_die->num_attrs += num_extra_attrs;
6909
6910 if (dwarf_die_debug)
6911 {
6912 fprintf_unfiltered (gdb_stdlog,
6913 "Read die from %s@0x%x of %s:\n",
6914 section->get_name (),
6915 (unsigned) (begin_info_ptr - section->buffer),
6916 bfd_get_filename (abfd));
6917 dump_die (comp_unit_die, dwarf_die_debug);
6918 }
6919
6920 /* Skip dummy compilation units. */
6921 if (info_ptr >= begin_info_ptr + dwo_unit->length
6922 || peek_abbrev_code (abfd, info_ptr) == 0)
6923 return 0;
6924
6925 *result_info_ptr = info_ptr;
6926 return 1;
6927 }
6928
6929 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6930 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6931 signature is part of the header. */
6932 static gdb::optional<ULONGEST>
6933 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6934 {
6935 if (cu->header.version >= 5)
6936 return cu->header.signature;
6937 struct attribute *attr;
6938 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6939 if (attr == nullptr)
6940 return gdb::optional<ULONGEST> ();
6941 return DW_UNSND (attr);
6942 }
6943
6944 /* Subroutine of cutu_reader to simplify it.
6945 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6946 Returns NULL if the specified DWO unit cannot be found. */
6947
6948 static struct dwo_unit *
6949 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6950 {
6951 dwarf2_per_cu_data *per_cu = cu->per_cu;
6952 struct dwo_unit *dwo_unit;
6953 const char *comp_dir;
6954
6955 gdb_assert (cu != NULL);
6956
6957 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6958 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6959 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6960
6961 if (per_cu->is_debug_types)
6962 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6963 else
6964 {
6965 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6966
6967 if (!signature.has_value ())
6968 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6969 " [in module %s]"),
6970 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6971
6972 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6973 }
6974
6975 return dwo_unit;
6976 }
6977
6978 /* Subroutine of cutu_reader to simplify it.
6979 See it for a description of the parameters.
6980 Read a TU directly from a DWO file, bypassing the stub. */
6981
6982 void
6983 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6984 dwarf2_per_objfile *per_objfile,
6985 dwarf2_cu *existing_cu)
6986 {
6987 struct signatured_type *sig_type;
6988
6989 /* Verify we can do the following downcast, and that we have the
6990 data we need. */
6991 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6992 sig_type = (struct signatured_type *) this_cu;
6993 gdb_assert (sig_type->dwo_unit != NULL);
6994
6995 dwarf2_cu *cu;
6996
6997 if (existing_cu != nullptr)
6998 {
6999 cu = existing_cu;
7000 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7001 /* There's no need to do the rereading_dwo_cu handling that
7002 cutu_reader does since we don't read the stub. */
7003 }
7004 else
7005 {
7006 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7007 in per_objfile yet. */
7008 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7009 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7010 cu = m_new_cu.get ();
7011 }
7012
7013 /* A future optimization, if needed, would be to use an existing
7014 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7015 could share abbrev tables. */
7016
7017 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7018 NULL /* stub_comp_unit_die */,
7019 sig_type->dwo_unit->dwo_file->comp_dir,
7020 this, &info_ptr,
7021 &comp_unit_die,
7022 &m_dwo_abbrev_table) == 0)
7023 {
7024 /* Dummy die. */
7025 dummy_p = true;
7026 }
7027 }
7028
7029 /* Initialize a CU (or TU) and read its DIEs.
7030 If the CU defers to a DWO file, read the DWO file as well.
7031
7032 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7033 Otherwise the table specified in the comp unit header is read in and used.
7034 This is an optimization for when we already have the abbrev table.
7035
7036 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7037 allocated. */
7038
7039 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7040 dwarf2_per_objfile *dwarf2_per_objfile,
7041 struct abbrev_table *abbrev_table,
7042 dwarf2_cu *existing_cu,
7043 bool skip_partial)
7044 : die_reader_specs {},
7045 m_this_cu (this_cu)
7046 {
7047 struct objfile *objfile = dwarf2_per_objfile->objfile;
7048 struct dwarf2_section_info *section = this_cu->section;
7049 bfd *abfd = section->get_bfd_owner ();
7050 const gdb_byte *begin_info_ptr;
7051 struct signatured_type *sig_type = NULL;
7052 struct dwarf2_section_info *abbrev_section;
7053 /* Non-zero if CU currently points to a DWO file and we need to
7054 reread it. When this happens we need to reread the skeleton die
7055 before we can reread the DWO file (this only applies to CUs, not TUs). */
7056 int rereading_dwo_cu = 0;
7057
7058 if (dwarf_die_debug)
7059 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7060 this_cu->is_debug_types ? "type" : "comp",
7061 sect_offset_str (this_cu->sect_off));
7062
7063 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7064 file (instead of going through the stub), short-circuit all of this. */
7065 if (this_cu->reading_dwo_directly)
7066 {
7067 /* Narrow down the scope of possibilities to have to understand. */
7068 gdb_assert (this_cu->is_debug_types);
7069 gdb_assert (abbrev_table == NULL);
7070 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, existing_cu);
7071 return;
7072 }
7073
7074 /* This is cheap if the section is already read in. */
7075 section->read (objfile);
7076
7077 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7078
7079 abbrev_section = get_abbrev_section_for_cu (this_cu);
7080
7081 dwarf2_cu *cu;
7082
7083 if (existing_cu != nullptr)
7084 {
7085 cu = existing_cu;
7086 /* If this CU is from a DWO file we need to start over, we need to
7087 refetch the attributes from the skeleton CU.
7088 This could be optimized by retrieving those attributes from when we
7089 were here the first time: the previous comp_unit_die was stored in
7090 comp_unit_obstack. But there's no data yet that we need this
7091 optimization. */
7092 if (cu->dwo_unit != NULL)
7093 rereading_dwo_cu = 1;
7094 }
7095 else
7096 {
7097 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7098 in per_objfile yet. */
7099 gdb_assert (dwarf2_per_objfile->get_cu (this_cu) == nullptr);
7100 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7101 cu = m_new_cu.get ();
7102 }
7103
7104 /* Get the header. */
7105 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7106 {
7107 /* We already have the header, there's no need to read it in again. */
7108 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7109 }
7110 else
7111 {
7112 if (this_cu->is_debug_types)
7113 {
7114 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7115 &cu->header, section,
7116 abbrev_section, info_ptr,
7117 rcuh_kind::TYPE);
7118
7119 /* Since per_cu is the first member of struct signatured_type,
7120 we can go from a pointer to one to a pointer to the other. */
7121 sig_type = (struct signatured_type *) this_cu;
7122 gdb_assert (sig_type->signature == cu->header.signature);
7123 gdb_assert (sig_type->type_offset_in_tu
7124 == cu->header.type_cu_offset_in_tu);
7125 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7126
7127 /* LENGTH has not been set yet for type units if we're
7128 using .gdb_index. */
7129 this_cu->length = cu->header.get_length ();
7130
7131 /* Establish the type offset that can be used to lookup the type. */
7132 sig_type->type_offset_in_section =
7133 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7134
7135 this_cu->dwarf_version = cu->header.version;
7136 }
7137 else
7138 {
7139 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7140 &cu->header, section,
7141 abbrev_section,
7142 info_ptr,
7143 rcuh_kind::COMPILE);
7144
7145 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7146 if (this_cu->length == 0)
7147 this_cu->length = cu->header.get_length ();
7148 else
7149 gdb_assert (this_cu->length == cu->header.get_length ());
7150 this_cu->dwarf_version = cu->header.version;
7151 }
7152 }
7153
7154 /* Skip dummy compilation units. */
7155 if (info_ptr >= begin_info_ptr + this_cu->length
7156 || peek_abbrev_code (abfd, info_ptr) == 0)
7157 {
7158 dummy_p = true;
7159 return;
7160 }
7161
7162 /* If we don't have them yet, read the abbrevs for this compilation unit.
7163 And if we need to read them now, make sure they're freed when we're
7164 done. */
7165 if (abbrev_table != NULL)
7166 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7167 else
7168 {
7169 m_abbrev_table_holder
7170 = abbrev_table::read (objfile, abbrev_section,
7171 cu->header.abbrev_sect_off);
7172 abbrev_table = m_abbrev_table_holder.get ();
7173 }
7174
7175 /* Read the top level CU/TU die. */
7176 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7177 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7178
7179 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7180 {
7181 dummy_p = true;
7182 return;
7183 }
7184
7185 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7186 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7187 table from the DWO file and pass the ownership over to us. It will be
7188 referenced from READER, so we must make sure to free it after we're done
7189 with READER.
7190
7191 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7192 DWO CU, that this test will fail (the attribute will not be present). */
7193 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7194 if (dwo_name != nullptr)
7195 {
7196 struct dwo_unit *dwo_unit;
7197 struct die_info *dwo_comp_unit_die;
7198
7199 if (comp_unit_die->has_children)
7200 {
7201 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7202 " has children (offset %s) [in module %s]"),
7203 sect_offset_str (this_cu->sect_off),
7204 bfd_get_filename (abfd));
7205 }
7206 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7207 if (dwo_unit != NULL)
7208 {
7209 if (read_cutu_die_from_dwo (cu, dwo_unit,
7210 comp_unit_die, NULL,
7211 this, &info_ptr,
7212 &dwo_comp_unit_die,
7213 &m_dwo_abbrev_table) == 0)
7214 {
7215 /* Dummy die. */
7216 dummy_p = true;
7217 return;
7218 }
7219 comp_unit_die = dwo_comp_unit_die;
7220 }
7221 else
7222 {
7223 /* Yikes, we couldn't find the rest of the DIE, we only have
7224 the stub. A complaint has already been logged. There's
7225 not much more we can do except pass on the stub DIE to
7226 die_reader_func. We don't want to throw an error on bad
7227 debug info. */
7228 }
7229 }
7230 }
7231
7232 void
7233 cutu_reader::keep ()
7234 {
7235 /* Done, clean up. */
7236 gdb_assert (!dummy_p);
7237 if (m_new_cu != NULL)
7238 {
7239 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7240 now. */
7241 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7242 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7243 }
7244 }
7245
7246 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7247 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7248 assumed to have already done the lookup to find the DWO file).
7249
7250 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7251 THIS_CU->is_debug_types, but nothing else.
7252
7253 We fill in THIS_CU->length.
7254
7255 THIS_CU->cu is always freed when done.
7256 This is done in order to not leave THIS_CU->cu in a state where we have
7257 to care whether it refers to the "main" CU or the DWO CU.
7258
7259 When parent_cu is passed, it is used to provide a default value for
7260 str_offsets_base and addr_base from the parent. */
7261
7262 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7263 dwarf2_per_objfile *dwarf2_per_objfile,
7264 struct dwarf2_cu *parent_cu,
7265 struct dwo_file *dwo_file)
7266 : die_reader_specs {},
7267 m_this_cu (this_cu)
7268 {
7269 struct objfile *objfile = dwarf2_per_objfile->objfile;
7270 struct dwarf2_section_info *section = this_cu->section;
7271 bfd *abfd = section->get_bfd_owner ();
7272 struct dwarf2_section_info *abbrev_section;
7273 const gdb_byte *begin_info_ptr, *info_ptr;
7274
7275 if (dwarf_die_debug)
7276 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7277 this_cu->is_debug_types ? "type" : "comp",
7278 sect_offset_str (this_cu->sect_off));
7279
7280 gdb_assert (dwarf2_per_objfile->get_cu (this_cu) == nullptr);
7281
7282 abbrev_section = (dwo_file != NULL
7283 ? &dwo_file->sections.abbrev
7284 : get_abbrev_section_for_cu (this_cu));
7285
7286 /* This is cheap if the section is already read in. */
7287 section->read (objfile);
7288
7289 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7290
7291 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7292 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7293 &m_new_cu->header, section,
7294 abbrev_section, info_ptr,
7295 (this_cu->is_debug_types
7296 ? rcuh_kind::TYPE
7297 : rcuh_kind::COMPILE));
7298
7299 if (parent_cu != nullptr)
7300 {
7301 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7302 m_new_cu->addr_base = parent_cu->addr_base;
7303 }
7304 this_cu->length = m_new_cu->header.get_length ();
7305
7306 /* Skip dummy compilation units. */
7307 if (info_ptr >= begin_info_ptr + this_cu->length
7308 || peek_abbrev_code (abfd, info_ptr) == 0)
7309 {
7310 dummy_p = true;
7311 return;
7312 }
7313
7314 m_abbrev_table_holder
7315 = abbrev_table::read (objfile, abbrev_section,
7316 m_new_cu->header.abbrev_sect_off);
7317
7318 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7319 m_abbrev_table_holder.get ());
7320 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7321 }
7322
7323 \f
7324 /* Type Unit Groups.
7325
7326 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7327 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7328 so that all types coming from the same compilation (.o file) are grouped
7329 together. A future step could be to put the types in the same symtab as
7330 the CU the types ultimately came from. */
7331
7332 static hashval_t
7333 hash_type_unit_group (const void *item)
7334 {
7335 const struct type_unit_group *tu_group
7336 = (const struct type_unit_group *) item;
7337
7338 return hash_stmt_list_entry (&tu_group->hash);
7339 }
7340
7341 static int
7342 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7343 {
7344 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7345 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7346
7347 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7348 }
7349
7350 /* Allocate a hash table for type unit groups. */
7351
7352 static htab_up
7353 allocate_type_unit_groups_table ()
7354 {
7355 return htab_up (htab_create_alloc (3,
7356 hash_type_unit_group,
7357 eq_type_unit_group,
7358 NULL, xcalloc, xfree));
7359 }
7360
7361 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7362 partial symtabs. We combine several TUs per psymtab to not let the size
7363 of any one psymtab grow too big. */
7364 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7365 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7366
7367 /* Helper routine for get_type_unit_group.
7368 Create the type_unit_group object used to hold one or more TUs. */
7369
7370 static struct type_unit_group *
7371 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7372 {
7373 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7374 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7375 struct dwarf2_per_cu_data *per_cu;
7376 struct type_unit_group *tu_group;
7377
7378 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7379 struct type_unit_group);
7380 per_cu = &tu_group->per_cu;
7381 per_cu->per_bfd = per_bfd;
7382
7383 if (per_bfd->using_index)
7384 {
7385 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7386 struct dwarf2_per_cu_quick_data);
7387 }
7388 else
7389 {
7390 unsigned int line_offset = to_underlying (line_offset_struct);
7391 dwarf2_psymtab *pst;
7392 std::string name;
7393
7394 /* Give the symtab a useful name for debug purposes. */
7395 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7396 name = string_printf ("<type_units_%d>",
7397 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7398 else
7399 name = string_printf ("<type_units_at_0x%x>", line_offset);
7400
7401 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7402 pst->anonymous = true;
7403 }
7404
7405 tu_group->hash.dwo_unit = cu->dwo_unit;
7406 tu_group->hash.line_sect_off = line_offset_struct;
7407
7408 return tu_group;
7409 }
7410
7411 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7412 STMT_LIST is a DW_AT_stmt_list attribute. */
7413
7414 static struct type_unit_group *
7415 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7416 {
7417 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7418 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7419 struct type_unit_group *tu_group;
7420 void **slot;
7421 unsigned int line_offset;
7422 struct type_unit_group type_unit_group_for_lookup;
7423
7424 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7425 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7426
7427 /* Do we need to create a new group, or can we use an existing one? */
7428
7429 if (stmt_list)
7430 {
7431 line_offset = DW_UNSND (stmt_list);
7432 ++tu_stats->nr_symtab_sharers;
7433 }
7434 else
7435 {
7436 /* Ugh, no stmt_list. Rare, but we have to handle it.
7437 We can do various things here like create one group per TU or
7438 spread them over multiple groups to split up the expansion work.
7439 To avoid worst case scenarios (too many groups or too large groups)
7440 we, umm, group them in bunches. */
7441 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7442 | (tu_stats->nr_stmt_less_type_units
7443 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7444 ++tu_stats->nr_stmt_less_type_units;
7445 }
7446
7447 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7448 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7449 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7450 &type_unit_group_for_lookup, INSERT);
7451 if (*slot != NULL)
7452 {
7453 tu_group = (struct type_unit_group *) *slot;
7454 gdb_assert (tu_group != NULL);
7455 }
7456 else
7457 {
7458 sect_offset line_offset_struct = (sect_offset) line_offset;
7459 tu_group = create_type_unit_group (cu, line_offset_struct);
7460 *slot = tu_group;
7461 ++tu_stats->nr_symtabs;
7462 }
7463
7464 return tu_group;
7465 }
7466 \f
7467 /* Partial symbol tables. */
7468
7469 /* Create a psymtab named NAME and assign it to PER_CU.
7470
7471 The caller must fill in the following details:
7472 dirname, textlow, texthigh. */
7473
7474 static dwarf2_psymtab *
7475 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7476 dwarf2_per_objfile *per_objfile,
7477 const char *name)
7478 {
7479 struct objfile *objfile = per_objfile->objfile;
7480 dwarf2_psymtab *pst;
7481
7482 pst = new dwarf2_psymtab (name, objfile, per_cu);
7483
7484 pst->psymtabs_addrmap_supported = true;
7485
7486 /* This is the glue that links PST into GDB's symbol API. */
7487 per_cu->v.psymtab = pst;
7488
7489 return pst;
7490 }
7491
7492 /* DIE reader function for process_psymtab_comp_unit. */
7493
7494 static void
7495 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7496 const gdb_byte *info_ptr,
7497 struct die_info *comp_unit_die,
7498 enum language pretend_language)
7499 {
7500 struct dwarf2_cu *cu = reader->cu;
7501 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7502 struct objfile *objfile = per_objfile->objfile;
7503 struct gdbarch *gdbarch = objfile->arch ();
7504 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7505 CORE_ADDR baseaddr;
7506 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7507 dwarf2_psymtab *pst;
7508 enum pc_bounds_kind cu_bounds_kind;
7509 const char *filename;
7510
7511 gdb_assert (! per_cu->is_debug_types);
7512
7513 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7514
7515 /* Allocate a new partial symbol table structure. */
7516 gdb::unique_xmalloc_ptr<char> debug_filename;
7517 static const char artificial[] = "<artificial>";
7518 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7519 if (filename == NULL)
7520 filename = "";
7521 else if (strcmp (filename, artificial) == 0)
7522 {
7523 debug_filename.reset (concat (artificial, "@",
7524 sect_offset_str (per_cu->sect_off),
7525 (char *) NULL));
7526 filename = debug_filename.get ();
7527 }
7528
7529 pst = create_partial_symtab (per_cu, per_objfile, filename);
7530
7531 /* This must be done before calling dwarf2_build_include_psymtabs. */
7532 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7533
7534 baseaddr = objfile->text_section_offset ();
7535
7536 dwarf2_find_base_address (comp_unit_die, cu);
7537
7538 /* Possibly set the default values of LOWPC and HIGHPC from
7539 `DW_AT_ranges'. */
7540 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7541 &best_highpc, cu, pst);
7542 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7543 {
7544 CORE_ADDR low
7545 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7546 - baseaddr);
7547 CORE_ADDR high
7548 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7549 - baseaddr - 1);
7550 /* Store the contiguous range if it is not empty; it can be
7551 empty for CUs with no code. */
7552 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7553 low, high, pst);
7554 }
7555
7556 /* Check if comp unit has_children.
7557 If so, read the rest of the partial symbols from this comp unit.
7558 If not, there's no more debug_info for this comp unit. */
7559 if (comp_unit_die->has_children)
7560 {
7561 struct partial_die_info *first_die;
7562 CORE_ADDR lowpc, highpc;
7563
7564 lowpc = ((CORE_ADDR) -1);
7565 highpc = ((CORE_ADDR) 0);
7566
7567 first_die = load_partial_dies (reader, info_ptr, 1);
7568
7569 scan_partial_symbols (first_die, &lowpc, &highpc,
7570 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7571
7572 /* If we didn't find a lowpc, set it to highpc to avoid
7573 complaints from `maint check'. */
7574 if (lowpc == ((CORE_ADDR) -1))
7575 lowpc = highpc;
7576
7577 /* If the compilation unit didn't have an explicit address range,
7578 then use the information extracted from its child dies. */
7579 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7580 {
7581 best_lowpc = lowpc;
7582 best_highpc = highpc;
7583 }
7584 }
7585 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7586 best_lowpc + baseaddr)
7587 - baseaddr);
7588 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7589 best_highpc + baseaddr)
7590 - baseaddr);
7591
7592 end_psymtab_common (objfile, pst);
7593
7594 if (!cu->per_cu->imported_symtabs_empty ())
7595 {
7596 int i;
7597 int len = cu->per_cu->imported_symtabs_size ();
7598
7599 /* Fill in 'dependencies' here; we fill in 'users' in a
7600 post-pass. */
7601 pst->number_of_dependencies = len;
7602 pst->dependencies
7603 = objfile->partial_symtabs->allocate_dependencies (len);
7604 for (i = 0; i < len; ++i)
7605 {
7606 pst->dependencies[i]
7607 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7608 }
7609
7610 cu->per_cu->imported_symtabs_free ();
7611 }
7612
7613 /* Get the list of files included in the current compilation unit,
7614 and build a psymtab for each of them. */
7615 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7616
7617 if (dwarf_read_debug)
7618 fprintf_unfiltered (gdb_stdlog,
7619 "Psymtab for %s unit @%s: %s - %s"
7620 ", %d global, %d static syms\n",
7621 per_cu->is_debug_types ? "type" : "comp",
7622 sect_offset_str (per_cu->sect_off),
7623 paddress (gdbarch, pst->text_low (objfile)),
7624 paddress (gdbarch, pst->text_high (objfile)),
7625 pst->n_global_syms, pst->n_static_syms);
7626 }
7627
7628 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7629 Process compilation unit THIS_CU for a psymtab. */
7630
7631 static void
7632 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7633 dwarf2_per_objfile *per_objfile,
7634 bool want_partial_unit,
7635 enum language pretend_language)
7636 {
7637 /* If this compilation unit was already read in, free the
7638 cached copy in order to read it in again. This is
7639 necessary because we skipped some symbols when we first
7640 read in the compilation unit (see load_partial_dies).
7641 This problem could be avoided, but the benefit is unclear. */
7642 per_objfile->remove_cu (this_cu);
7643
7644 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7645
7646 switch (reader.comp_unit_die->tag)
7647 {
7648 case DW_TAG_compile_unit:
7649 this_cu->unit_type = DW_UT_compile;
7650 break;
7651 case DW_TAG_partial_unit:
7652 this_cu->unit_type = DW_UT_partial;
7653 break;
7654 default:
7655 abort ();
7656 }
7657
7658 if (reader.dummy_p)
7659 {
7660 /* Nothing. */
7661 }
7662 else if (this_cu->is_debug_types)
7663 build_type_psymtabs_reader (&reader, reader.info_ptr,
7664 reader.comp_unit_die);
7665 else if (want_partial_unit
7666 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7667 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7668 reader.comp_unit_die,
7669 pretend_language);
7670
7671 this_cu->lang = reader.cu->language;
7672
7673 /* Age out any secondary CUs. */
7674 per_objfile->age_comp_units ();
7675 }
7676
7677 /* Reader function for build_type_psymtabs. */
7678
7679 static void
7680 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7681 const gdb_byte *info_ptr,
7682 struct die_info *type_unit_die)
7683 {
7684 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7685 struct objfile *objfile = dwarf2_per_objfile->objfile;
7686 struct dwarf2_cu *cu = reader->cu;
7687 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7688 struct signatured_type *sig_type;
7689 struct type_unit_group *tu_group;
7690 struct attribute *attr;
7691 struct partial_die_info *first_die;
7692 CORE_ADDR lowpc, highpc;
7693 dwarf2_psymtab *pst;
7694
7695 gdb_assert (per_cu->is_debug_types);
7696 sig_type = (struct signatured_type *) per_cu;
7697
7698 if (! type_unit_die->has_children)
7699 return;
7700
7701 attr = type_unit_die->attr (DW_AT_stmt_list);
7702 tu_group = get_type_unit_group (cu, attr);
7703
7704 if (tu_group->tus == nullptr)
7705 tu_group->tus = new std::vector<signatured_type *>;
7706 tu_group->tus->push_back (sig_type);
7707
7708 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7709 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7710 pst->anonymous = true;
7711
7712 first_die = load_partial_dies (reader, info_ptr, 1);
7713
7714 lowpc = (CORE_ADDR) -1;
7715 highpc = (CORE_ADDR) 0;
7716 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7717
7718 end_psymtab_common (objfile, pst);
7719 }
7720
7721 /* Struct used to sort TUs by their abbreviation table offset. */
7722
7723 struct tu_abbrev_offset
7724 {
7725 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7726 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7727 {}
7728
7729 signatured_type *sig_type;
7730 sect_offset abbrev_offset;
7731 };
7732
7733 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7734
7735 static bool
7736 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7737 const struct tu_abbrev_offset &b)
7738 {
7739 return a.abbrev_offset < b.abbrev_offset;
7740 }
7741
7742 /* Efficiently read all the type units.
7743 This does the bulk of the work for build_type_psymtabs.
7744
7745 The efficiency is because we sort TUs by the abbrev table they use and
7746 only read each abbrev table once. In one program there are 200K TUs
7747 sharing 8K abbrev tables.
7748
7749 The main purpose of this function is to support building the
7750 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7751 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7752 can collapse the search space by grouping them by stmt_list.
7753 The savings can be significant, in the same program from above the 200K TUs
7754 share 8K stmt_list tables.
7755
7756 FUNC is expected to call get_type_unit_group, which will create the
7757 struct type_unit_group if necessary and add it to
7758 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7759
7760 static void
7761 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7762 {
7763 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7764 abbrev_table_up abbrev_table;
7765 sect_offset abbrev_offset;
7766
7767 /* It's up to the caller to not call us multiple times. */
7768 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7769
7770 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7771 return;
7772
7773 /* TUs typically share abbrev tables, and there can be way more TUs than
7774 abbrev tables. Sort by abbrev table to reduce the number of times we
7775 read each abbrev table in.
7776 Alternatives are to punt or to maintain a cache of abbrev tables.
7777 This is simpler and efficient enough for now.
7778
7779 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7780 symtab to use). Typically TUs with the same abbrev offset have the same
7781 stmt_list value too so in practice this should work well.
7782
7783 The basic algorithm here is:
7784
7785 sort TUs by abbrev table
7786 for each TU with same abbrev table:
7787 read abbrev table if first user
7788 read TU top level DIE
7789 [IWBN if DWO skeletons had DW_AT_stmt_list]
7790 call FUNC */
7791
7792 if (dwarf_read_debug)
7793 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7794
7795 /* Sort in a separate table to maintain the order of all_type_units
7796 for .gdb_index: TU indices directly index all_type_units. */
7797 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7798 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7799
7800 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7801 sorted_by_abbrev.emplace_back
7802 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7803 sig_type->per_cu.section,
7804 sig_type->per_cu.sect_off));
7805
7806 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7807 sort_tu_by_abbrev_offset);
7808
7809 abbrev_offset = (sect_offset) ~(unsigned) 0;
7810
7811 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7812 {
7813 /* Switch to the next abbrev table if necessary. */
7814 if (abbrev_table == NULL
7815 || tu.abbrev_offset != abbrev_offset)
7816 {
7817 abbrev_offset = tu.abbrev_offset;
7818 abbrev_table =
7819 abbrev_table::read (dwarf2_per_objfile->objfile,
7820 &dwarf2_per_objfile->per_bfd->abbrev,
7821 abbrev_offset);
7822 ++tu_stats->nr_uniq_abbrev_tables;
7823 }
7824
7825 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7826 abbrev_table.get (), nullptr, false);
7827 if (!reader.dummy_p)
7828 build_type_psymtabs_reader (&reader, reader.info_ptr,
7829 reader.comp_unit_die);
7830 }
7831 }
7832
7833 /* Print collected type unit statistics. */
7834
7835 static void
7836 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7837 {
7838 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7839
7840 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7841 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7842 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7843 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7844 tu_stats->nr_uniq_abbrev_tables);
7845 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7846 tu_stats->nr_symtabs);
7847 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7848 tu_stats->nr_symtab_sharers);
7849 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7850 tu_stats->nr_stmt_less_type_units);
7851 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7852 tu_stats->nr_all_type_units_reallocs);
7853 }
7854
7855 /* Traversal function for build_type_psymtabs. */
7856
7857 static int
7858 build_type_psymtab_dependencies (void **slot, void *info)
7859 {
7860 struct dwarf2_per_objfile *dwarf2_per_objfile
7861 = (struct dwarf2_per_objfile *) info;
7862 struct objfile *objfile = dwarf2_per_objfile->objfile;
7863 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7864 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7865 dwarf2_psymtab *pst = per_cu->v.psymtab;
7866 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7867 int i;
7868
7869 gdb_assert (len > 0);
7870 gdb_assert (per_cu->type_unit_group_p ());
7871
7872 pst->number_of_dependencies = len;
7873 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7874 for (i = 0; i < len; ++i)
7875 {
7876 struct signatured_type *iter = tu_group->tus->at (i);
7877 gdb_assert (iter->per_cu.is_debug_types);
7878 pst->dependencies[i] = iter->per_cu.v.psymtab;
7879 iter->type_unit_group = tu_group;
7880 }
7881
7882 delete tu_group->tus;
7883 tu_group->tus = nullptr;
7884
7885 return 1;
7886 }
7887
7888 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7889 Build partial symbol tables for the .debug_types comp-units. */
7890
7891 static void
7892 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7893 {
7894 if (! create_all_type_units (dwarf2_per_objfile))
7895 return;
7896
7897 build_type_psymtabs_1 (dwarf2_per_objfile);
7898 }
7899
7900 /* Traversal function for process_skeletonless_type_unit.
7901 Read a TU in a DWO file and build partial symbols for it. */
7902
7903 static int
7904 process_skeletonless_type_unit (void **slot, void *info)
7905 {
7906 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7907 struct dwarf2_per_objfile *dwarf2_per_objfile
7908 = (struct dwarf2_per_objfile *) info;
7909 struct signatured_type find_entry, *entry;
7910
7911 /* If this TU doesn't exist in the global table, add it and read it in. */
7912
7913 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7914 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7915
7916 find_entry.signature = dwo_unit->signature;
7917 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7918 &find_entry, INSERT);
7919 /* If we've already seen this type there's nothing to do. What's happening
7920 is we're doing our own version of comdat-folding here. */
7921 if (*slot != NULL)
7922 return 1;
7923
7924 /* This does the job that create_all_type_units would have done for
7925 this TU. */
7926 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7927 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7928 *slot = entry;
7929
7930 /* This does the job that build_type_psymtabs_1 would have done. */
7931 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, nullptr, nullptr,
7932 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 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7960 {
7961 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7962 if (get_dwp_file (dwarf2_per_objfile) == NULL
7963 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7964 {
7965 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7966 process_dwo_file_for_skeletonless_type_units,
7967 dwarf2_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 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7975 {
7976 for (dwarf2_per_cu_data *per_cu : dwarf2_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 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7997 {
7998 struct objfile *objfile = dwarf2_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 (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
8008 true);
8009
8010 dwarf2_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 (dwarf2_per_objfile);
8015
8016 build_type_psymtabs (dwarf2_per_objfile);
8017
8018 create_all_comp_units (dwarf2_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 : dwarf2_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, dwarf2_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 (dwarf2_per_objfile);
8039
8040 /* Now that all TUs have been processed we can fill in the dependencies. */
8041 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
8042 {
8043 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
8044 build_type_psymtab_dependencies, dwarf2_per_objfile);
8045 }
8046
8047 if (dwarf_read_debug)
8048 print_tu_stats (dwarf2_per_objfile);
8049
8050 set_partial_user (dwarf2_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 (struct dwarf2_per_objfile *dwarf2_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 = dwarf2_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 (dwarf2_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 = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8120 else
8121 {
8122 auto sig_type = dwarf2_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 dwarf2_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 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8144 {
8145 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8146 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8147 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8148
8149 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8150 if (dwz != NULL)
8151 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8152 1);
8153 }
8154
8155 /* Process all loaded DIEs for compilation unit CU, starting at
8156 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8157 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8158 DW_AT_ranges). See the comments of add_partial_subprogram on how
8159 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8160
8161 static void
8162 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8163 CORE_ADDR *highpc, int set_addrmap,
8164 struct dwarf2_cu *cu)
8165 {
8166 struct partial_die_info *pdi;
8167
8168 /* Now, march along the PDI's, descending into ones which have
8169 interesting children but skipping the children of the other ones,
8170 until we reach the end of the compilation unit. */
8171
8172 pdi = first_die;
8173
8174 while (pdi != NULL)
8175 {
8176 pdi->fixup (cu);
8177
8178 /* Anonymous namespaces or modules have no name but have interesting
8179 children, so we need to look at them. Ditto for anonymous
8180 enums. */
8181
8182 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8183 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8184 || pdi->tag == DW_TAG_imported_unit
8185 || pdi->tag == DW_TAG_inlined_subroutine)
8186 {
8187 switch (pdi->tag)
8188 {
8189 case DW_TAG_subprogram:
8190 case DW_TAG_inlined_subroutine:
8191 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8192 break;
8193 case DW_TAG_constant:
8194 case DW_TAG_variable:
8195 case DW_TAG_typedef:
8196 case DW_TAG_union_type:
8197 if (!pdi->is_declaration
8198 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8199 {
8200 add_partial_symbol (pdi, cu);
8201 }
8202 break;
8203 case DW_TAG_class_type:
8204 case DW_TAG_interface_type:
8205 case DW_TAG_structure_type:
8206 if (!pdi->is_declaration)
8207 {
8208 add_partial_symbol (pdi, cu);
8209 }
8210 if ((cu->language == language_rust
8211 || cu->language == language_cplus) && pdi->has_children)
8212 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8213 set_addrmap, cu);
8214 break;
8215 case DW_TAG_enumeration_type:
8216 if (!pdi->is_declaration)
8217 add_partial_enumeration (pdi, cu);
8218 break;
8219 case DW_TAG_base_type:
8220 case DW_TAG_subrange_type:
8221 /* File scope base type definitions are added to the partial
8222 symbol table. */
8223 add_partial_symbol (pdi, cu);
8224 break;
8225 case DW_TAG_namespace:
8226 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8227 break;
8228 case DW_TAG_module:
8229 if (!pdi->is_declaration)
8230 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8231 break;
8232 case DW_TAG_imported_unit:
8233 {
8234 struct dwarf2_per_cu_data *per_cu;
8235
8236 /* For now we don't handle imported units in type units. */
8237 if (cu->per_cu->is_debug_types)
8238 {
8239 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8240 " supported in type units [in module %s]"),
8241 objfile_name (cu->per_objfile->objfile));
8242 }
8243
8244 per_cu = dwarf2_find_containing_comp_unit
8245 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8246
8247 /* Go read the partial unit, if needed. */
8248 if (per_cu->v.psymtab == NULL)
8249 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8250 cu->language);
8251
8252 cu->per_cu->imported_symtabs_push (per_cu);
8253 }
8254 break;
8255 case DW_TAG_imported_declaration:
8256 add_partial_symbol (pdi, cu);
8257 break;
8258 default:
8259 break;
8260 }
8261 }
8262
8263 /* If the die has a sibling, skip to the sibling. */
8264
8265 pdi = pdi->die_sibling;
8266 }
8267 }
8268
8269 /* Functions used to compute the fully scoped name of a partial DIE.
8270
8271 Normally, this is simple. For C++, the parent DIE's fully scoped
8272 name is concatenated with "::" and the partial DIE's name.
8273 Enumerators are an exception; they use the scope of their parent
8274 enumeration type, i.e. the name of the enumeration type is not
8275 prepended to the enumerator.
8276
8277 There are two complexities. One is DW_AT_specification; in this
8278 case "parent" means the parent of the target of the specification,
8279 instead of the direct parent of the DIE. The other is compilers
8280 which do not emit DW_TAG_namespace; in this case we try to guess
8281 the fully qualified name of structure types from their members'
8282 linkage names. This must be done using the DIE's children rather
8283 than the children of any DW_AT_specification target. We only need
8284 to do this for structures at the top level, i.e. if the target of
8285 any DW_AT_specification (if any; otherwise the DIE itself) does not
8286 have a parent. */
8287
8288 /* Compute the scope prefix associated with PDI's parent, in
8289 compilation unit CU. The result will be allocated on CU's
8290 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8291 field. NULL is returned if no prefix is necessary. */
8292 static const char *
8293 partial_die_parent_scope (struct partial_die_info *pdi,
8294 struct dwarf2_cu *cu)
8295 {
8296 const char *grandparent_scope;
8297 struct partial_die_info *parent, *real_pdi;
8298
8299 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8300 then this means the parent of the specification DIE. */
8301
8302 real_pdi = pdi;
8303 while (real_pdi->has_specification)
8304 {
8305 auto res = find_partial_die (real_pdi->spec_offset,
8306 real_pdi->spec_is_dwz, cu);
8307 real_pdi = res.pdi;
8308 cu = res.cu;
8309 }
8310
8311 parent = real_pdi->die_parent;
8312 if (parent == NULL)
8313 return NULL;
8314
8315 if (parent->scope_set)
8316 return parent->scope;
8317
8318 parent->fixup (cu);
8319
8320 grandparent_scope = partial_die_parent_scope (parent, cu);
8321
8322 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8323 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8324 Work around this problem here. */
8325 if (cu->language == language_cplus
8326 && parent->tag == DW_TAG_namespace
8327 && strcmp (parent->name (cu), "::") == 0
8328 && grandparent_scope == NULL)
8329 {
8330 parent->scope = NULL;
8331 parent->scope_set = 1;
8332 return NULL;
8333 }
8334
8335 /* Nested subroutines in Fortran get a prefix. */
8336 if (pdi->tag == DW_TAG_enumerator)
8337 /* Enumerators should not get the name of the enumeration as a prefix. */
8338 parent->scope = grandparent_scope;
8339 else if (parent->tag == DW_TAG_namespace
8340 || parent->tag == DW_TAG_module
8341 || parent->tag == DW_TAG_structure_type
8342 || parent->tag == DW_TAG_class_type
8343 || parent->tag == DW_TAG_interface_type
8344 || parent->tag == DW_TAG_union_type
8345 || parent->tag == DW_TAG_enumeration_type
8346 || (cu->language == language_fortran
8347 && parent->tag == DW_TAG_subprogram
8348 && pdi->tag == DW_TAG_subprogram))
8349 {
8350 if (grandparent_scope == NULL)
8351 parent->scope = parent->name (cu);
8352 else
8353 parent->scope = typename_concat (&cu->comp_unit_obstack,
8354 grandparent_scope,
8355 parent->name (cu), 0, cu);
8356 }
8357 else
8358 {
8359 /* FIXME drow/2004-04-01: What should we be doing with
8360 function-local names? For partial symbols, we should probably be
8361 ignoring them. */
8362 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8363 dwarf_tag_name (parent->tag),
8364 sect_offset_str (pdi->sect_off));
8365 parent->scope = grandparent_scope;
8366 }
8367
8368 parent->scope_set = 1;
8369 return parent->scope;
8370 }
8371
8372 /* Return the fully scoped name associated with PDI, from compilation unit
8373 CU. The result will be allocated with malloc. */
8374
8375 static gdb::unique_xmalloc_ptr<char>
8376 partial_die_full_name (struct partial_die_info *pdi,
8377 struct dwarf2_cu *cu)
8378 {
8379 const char *parent_scope;
8380
8381 /* If this is a template instantiation, we can not work out the
8382 template arguments from partial DIEs. So, unfortunately, we have
8383 to go through the full DIEs. At least any work we do building
8384 types here will be reused if full symbols are loaded later. */
8385 if (pdi->has_template_arguments)
8386 {
8387 pdi->fixup (cu);
8388
8389 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8390 {
8391 struct die_info *die;
8392 struct attribute attr;
8393 struct dwarf2_cu *ref_cu = cu;
8394
8395 /* DW_FORM_ref_addr is using section offset. */
8396 attr.name = (enum dwarf_attribute) 0;
8397 attr.form = DW_FORM_ref_addr;
8398 attr.u.unsnd = to_underlying (pdi->sect_off);
8399 die = follow_die_ref (NULL, &attr, &ref_cu);
8400
8401 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8402 }
8403 }
8404
8405 parent_scope = partial_die_parent_scope (pdi, cu);
8406 if (parent_scope == NULL)
8407 return NULL;
8408 else
8409 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8410 pdi->name (cu),
8411 0, cu));
8412 }
8413
8414 static void
8415 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8416 {
8417 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8418 struct objfile *objfile = dwarf2_per_objfile->objfile;
8419 struct gdbarch *gdbarch = objfile->arch ();
8420 CORE_ADDR addr = 0;
8421 const char *actual_name = NULL;
8422 CORE_ADDR baseaddr;
8423
8424 baseaddr = objfile->text_section_offset ();
8425
8426 gdb::unique_xmalloc_ptr<char> built_actual_name
8427 = partial_die_full_name (pdi, cu);
8428 if (built_actual_name != NULL)
8429 actual_name = built_actual_name.get ();
8430
8431 if (actual_name == NULL)
8432 actual_name = pdi->name (cu);
8433
8434 partial_symbol psymbol;
8435 memset (&psymbol, 0, sizeof (psymbol));
8436 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8437 psymbol.ginfo.section = -1;
8438
8439 /* The code below indicates that the psymbol should be installed by
8440 setting this. */
8441 gdb::optional<psymbol_placement> where;
8442
8443 switch (pdi->tag)
8444 {
8445 case DW_TAG_inlined_subroutine:
8446 case DW_TAG_subprogram:
8447 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8448 - baseaddr);
8449 if (pdi->is_external
8450 || cu->language == language_ada
8451 || (cu->language == language_fortran
8452 && pdi->die_parent != NULL
8453 && pdi->die_parent->tag == DW_TAG_subprogram))
8454 {
8455 /* Normally, only "external" DIEs are part of the global scope.
8456 But in Ada and Fortran, we want to be able to access nested
8457 procedures globally. So all Ada and Fortran subprograms are
8458 stored in the global scope. */
8459 where = psymbol_placement::GLOBAL;
8460 }
8461 else
8462 where = psymbol_placement::STATIC;
8463
8464 psymbol.domain = VAR_DOMAIN;
8465 psymbol.aclass = LOC_BLOCK;
8466 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8467 psymbol.ginfo.value.address = addr;
8468
8469 if (pdi->main_subprogram && actual_name != NULL)
8470 set_objfile_main_name (objfile, actual_name, cu->language);
8471 break;
8472 case DW_TAG_constant:
8473 psymbol.domain = VAR_DOMAIN;
8474 psymbol.aclass = LOC_STATIC;
8475 where = (pdi->is_external
8476 ? psymbol_placement::GLOBAL
8477 : psymbol_placement::STATIC);
8478 break;
8479 case DW_TAG_variable:
8480 if (pdi->d.locdesc)
8481 addr = decode_locdesc (pdi->d.locdesc, cu);
8482
8483 if (pdi->d.locdesc
8484 && addr == 0
8485 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8486 {
8487 /* A global or static variable may also have been stripped
8488 out by the linker if unused, in which case its address
8489 will be nullified; do not add such variables into partial
8490 symbol table then. */
8491 }
8492 else if (pdi->is_external)
8493 {
8494 /* Global Variable.
8495 Don't enter into the minimal symbol tables as there is
8496 a minimal symbol table entry from the ELF symbols already.
8497 Enter into partial symbol table if it has a location
8498 descriptor or a type.
8499 If the location descriptor is missing, new_symbol will create
8500 a LOC_UNRESOLVED symbol, the address of the variable will then
8501 be determined from the minimal symbol table whenever the variable
8502 is referenced.
8503 The address for the partial symbol table entry is not
8504 used by GDB, but it comes in handy for debugging partial symbol
8505 table building. */
8506
8507 if (pdi->d.locdesc || pdi->has_type)
8508 {
8509 psymbol.domain = VAR_DOMAIN;
8510 psymbol.aclass = LOC_STATIC;
8511 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8512 psymbol.ginfo.value.address = addr;
8513 where = psymbol_placement::GLOBAL;
8514 }
8515 }
8516 else
8517 {
8518 int has_loc = pdi->d.locdesc != NULL;
8519
8520 /* Static Variable. Skip symbols whose value we cannot know (those
8521 without location descriptors or constant values). */
8522 if (!has_loc && !pdi->has_const_value)
8523 return;
8524
8525 psymbol.domain = VAR_DOMAIN;
8526 psymbol.aclass = LOC_STATIC;
8527 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8528 if (has_loc)
8529 psymbol.ginfo.value.address = addr;
8530 where = psymbol_placement::STATIC;
8531 }
8532 break;
8533 case DW_TAG_typedef:
8534 case DW_TAG_base_type:
8535 case DW_TAG_subrange_type:
8536 psymbol.domain = VAR_DOMAIN;
8537 psymbol.aclass = LOC_TYPEDEF;
8538 where = psymbol_placement::STATIC;
8539 break;
8540 case DW_TAG_imported_declaration:
8541 case DW_TAG_namespace:
8542 psymbol.domain = VAR_DOMAIN;
8543 psymbol.aclass = LOC_TYPEDEF;
8544 where = psymbol_placement::GLOBAL;
8545 break;
8546 case DW_TAG_module:
8547 /* With Fortran 77 there might be a "BLOCK DATA" module
8548 available without any name. If so, we skip the module as it
8549 doesn't bring any value. */
8550 if (actual_name != nullptr)
8551 {
8552 psymbol.domain = MODULE_DOMAIN;
8553 psymbol.aclass = LOC_TYPEDEF;
8554 where = psymbol_placement::GLOBAL;
8555 }
8556 break;
8557 case DW_TAG_class_type:
8558 case DW_TAG_interface_type:
8559 case DW_TAG_structure_type:
8560 case DW_TAG_union_type:
8561 case DW_TAG_enumeration_type:
8562 /* Skip external references. The DWARF standard says in the section
8563 about "Structure, Union, and Class Type Entries": "An incomplete
8564 structure, union or class type is represented by a structure,
8565 union or class entry that does not have a byte size attribute
8566 and that has a DW_AT_declaration attribute." */
8567 if (!pdi->has_byte_size && pdi->is_declaration)
8568 return;
8569
8570 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8571 static vs. global. */
8572 psymbol.domain = STRUCT_DOMAIN;
8573 psymbol.aclass = LOC_TYPEDEF;
8574 where = (cu->language == language_cplus
8575 ? psymbol_placement::GLOBAL
8576 : psymbol_placement::STATIC);
8577 break;
8578 case DW_TAG_enumerator:
8579 psymbol.domain = VAR_DOMAIN;
8580 psymbol.aclass = LOC_CONST;
8581 where = (cu->language == language_cplus
8582 ? psymbol_placement::GLOBAL
8583 : psymbol_placement::STATIC);
8584 break;
8585 default:
8586 break;
8587 }
8588
8589 if (where.has_value ())
8590 {
8591 if (built_actual_name != nullptr)
8592 actual_name = objfile->intern (actual_name);
8593 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8594 psymbol.ginfo.set_linkage_name (actual_name);
8595 else
8596 {
8597 psymbol.ginfo.set_demangled_name (actual_name,
8598 &objfile->objfile_obstack);
8599 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8600 }
8601 add_psymbol_to_list (psymbol, *where, objfile);
8602 }
8603 }
8604
8605 /* Read a partial die corresponding to a namespace; also, add a symbol
8606 corresponding to that namespace to the symbol table. NAMESPACE is
8607 the name of the enclosing namespace. */
8608
8609 static void
8610 add_partial_namespace (struct partial_die_info *pdi,
8611 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8612 int set_addrmap, struct dwarf2_cu *cu)
8613 {
8614 /* Add a symbol for the namespace. */
8615
8616 add_partial_symbol (pdi, cu);
8617
8618 /* Now scan partial symbols in that namespace. */
8619
8620 if (pdi->has_children)
8621 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8622 }
8623
8624 /* Read a partial die corresponding to a Fortran module. */
8625
8626 static void
8627 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8628 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8629 {
8630 /* Add a symbol for the namespace. */
8631
8632 add_partial_symbol (pdi, cu);
8633
8634 /* Now scan partial symbols in that module. */
8635
8636 if (pdi->has_children)
8637 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8638 }
8639
8640 /* Read a partial die corresponding to a subprogram or an inlined
8641 subprogram and create a partial symbol for that subprogram.
8642 When the CU language allows it, this routine also defines a partial
8643 symbol for each nested subprogram that this subprogram contains.
8644 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8645 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8646
8647 PDI may also be a lexical block, in which case we simply search
8648 recursively for subprograms defined inside that lexical block.
8649 Again, this is only performed when the CU language allows this
8650 type of definitions. */
8651
8652 static void
8653 add_partial_subprogram (struct partial_die_info *pdi,
8654 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8655 int set_addrmap, struct dwarf2_cu *cu)
8656 {
8657 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8658 {
8659 if (pdi->has_pc_info)
8660 {
8661 if (pdi->lowpc < *lowpc)
8662 *lowpc = pdi->lowpc;
8663 if (pdi->highpc > *highpc)
8664 *highpc = pdi->highpc;
8665 if (set_addrmap)
8666 {
8667 struct objfile *objfile = cu->per_objfile->objfile;
8668 struct gdbarch *gdbarch = objfile->arch ();
8669 CORE_ADDR baseaddr;
8670 CORE_ADDR this_highpc;
8671 CORE_ADDR this_lowpc;
8672
8673 baseaddr = objfile->text_section_offset ();
8674 this_lowpc
8675 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8676 pdi->lowpc + baseaddr)
8677 - baseaddr);
8678 this_highpc
8679 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8680 pdi->highpc + baseaddr)
8681 - baseaddr);
8682 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8683 this_lowpc, this_highpc - 1,
8684 cu->per_cu->v.psymtab);
8685 }
8686 }
8687
8688 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8689 {
8690 if (!pdi->is_declaration)
8691 /* Ignore subprogram DIEs that do not have a name, they are
8692 illegal. Do not emit a complaint at this point, we will
8693 do so when we convert this psymtab into a symtab. */
8694 if (pdi->name (cu))
8695 add_partial_symbol (pdi, cu);
8696 }
8697 }
8698
8699 if (! pdi->has_children)
8700 return;
8701
8702 if (cu->language == language_ada || cu->language == language_fortran)
8703 {
8704 pdi = pdi->die_child;
8705 while (pdi != NULL)
8706 {
8707 pdi->fixup (cu);
8708 if (pdi->tag == DW_TAG_subprogram
8709 || pdi->tag == DW_TAG_inlined_subroutine
8710 || pdi->tag == DW_TAG_lexical_block)
8711 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8712 pdi = pdi->die_sibling;
8713 }
8714 }
8715 }
8716
8717 /* Read a partial die corresponding to an enumeration type. */
8718
8719 static void
8720 add_partial_enumeration (struct partial_die_info *enum_pdi,
8721 struct dwarf2_cu *cu)
8722 {
8723 struct partial_die_info *pdi;
8724
8725 if (enum_pdi->name (cu) != NULL)
8726 add_partial_symbol (enum_pdi, cu);
8727
8728 pdi = enum_pdi->die_child;
8729 while (pdi)
8730 {
8731 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8732 complaint (_("malformed enumerator DIE ignored"));
8733 else
8734 add_partial_symbol (pdi, cu);
8735 pdi = pdi->die_sibling;
8736 }
8737 }
8738
8739 /* Return the initial uleb128 in the die at INFO_PTR. */
8740
8741 static unsigned int
8742 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8743 {
8744 unsigned int bytes_read;
8745
8746 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8747 }
8748
8749 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8750 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8751
8752 Return the corresponding abbrev, or NULL if the number is zero (indicating
8753 an empty DIE). In either case *BYTES_READ will be set to the length of
8754 the initial number. */
8755
8756 static struct abbrev_info *
8757 peek_die_abbrev (const die_reader_specs &reader,
8758 const gdb_byte *info_ptr, unsigned int *bytes_read)
8759 {
8760 dwarf2_cu *cu = reader.cu;
8761 bfd *abfd = cu->per_objfile->objfile->obfd;
8762 unsigned int abbrev_number
8763 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8764
8765 if (abbrev_number == 0)
8766 return NULL;
8767
8768 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8769 if (!abbrev)
8770 {
8771 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8772 " at offset %s [in module %s]"),
8773 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8774 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8775 }
8776
8777 return abbrev;
8778 }
8779
8780 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8781 Returns a pointer to the end of a series of DIEs, terminated by an empty
8782 DIE. Any children of the skipped DIEs will also be skipped. */
8783
8784 static const gdb_byte *
8785 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8786 {
8787 while (1)
8788 {
8789 unsigned int bytes_read;
8790 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8791
8792 if (abbrev == NULL)
8793 return info_ptr + bytes_read;
8794 else
8795 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8796 }
8797 }
8798
8799 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8800 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8801 abbrev corresponding to that skipped uleb128 should be passed in
8802 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8803 children. */
8804
8805 static const gdb_byte *
8806 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8807 struct abbrev_info *abbrev)
8808 {
8809 unsigned int bytes_read;
8810 struct attribute attr;
8811 bfd *abfd = reader->abfd;
8812 struct dwarf2_cu *cu = reader->cu;
8813 const gdb_byte *buffer = reader->buffer;
8814 const gdb_byte *buffer_end = reader->buffer_end;
8815 unsigned int form, i;
8816
8817 for (i = 0; i < abbrev->num_attrs; i++)
8818 {
8819 /* The only abbrev we care about is DW_AT_sibling. */
8820 if (abbrev->attrs[i].name == DW_AT_sibling)
8821 {
8822 bool ignored;
8823 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8824 &ignored);
8825 if (attr.form == DW_FORM_ref_addr)
8826 complaint (_("ignoring absolute DW_AT_sibling"));
8827 else
8828 {
8829 sect_offset off = attr.get_ref_die_offset ();
8830 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8831
8832 if (sibling_ptr < info_ptr)
8833 complaint (_("DW_AT_sibling points backwards"));
8834 else if (sibling_ptr > reader->buffer_end)
8835 reader->die_section->overflow_complaint ();
8836 else
8837 return sibling_ptr;
8838 }
8839 }
8840
8841 /* If it isn't DW_AT_sibling, skip this attribute. */
8842 form = abbrev->attrs[i].form;
8843 skip_attribute:
8844 switch (form)
8845 {
8846 case DW_FORM_ref_addr:
8847 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8848 and later it is offset sized. */
8849 if (cu->header.version == 2)
8850 info_ptr += cu->header.addr_size;
8851 else
8852 info_ptr += cu->header.offset_size;
8853 break;
8854 case DW_FORM_GNU_ref_alt:
8855 info_ptr += cu->header.offset_size;
8856 break;
8857 case DW_FORM_addr:
8858 info_ptr += cu->header.addr_size;
8859 break;
8860 case DW_FORM_data1:
8861 case DW_FORM_ref1:
8862 case DW_FORM_flag:
8863 case DW_FORM_strx1:
8864 info_ptr += 1;
8865 break;
8866 case DW_FORM_flag_present:
8867 case DW_FORM_implicit_const:
8868 break;
8869 case DW_FORM_data2:
8870 case DW_FORM_ref2:
8871 case DW_FORM_strx2:
8872 info_ptr += 2;
8873 break;
8874 case DW_FORM_strx3:
8875 info_ptr += 3;
8876 break;
8877 case DW_FORM_data4:
8878 case DW_FORM_ref4:
8879 case DW_FORM_strx4:
8880 info_ptr += 4;
8881 break;
8882 case DW_FORM_data8:
8883 case DW_FORM_ref8:
8884 case DW_FORM_ref_sig8:
8885 info_ptr += 8;
8886 break;
8887 case DW_FORM_data16:
8888 info_ptr += 16;
8889 break;
8890 case DW_FORM_string:
8891 read_direct_string (abfd, info_ptr, &bytes_read);
8892 info_ptr += bytes_read;
8893 break;
8894 case DW_FORM_sec_offset:
8895 case DW_FORM_strp:
8896 case DW_FORM_GNU_strp_alt:
8897 info_ptr += cu->header.offset_size;
8898 break;
8899 case DW_FORM_exprloc:
8900 case DW_FORM_block:
8901 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8902 info_ptr += bytes_read;
8903 break;
8904 case DW_FORM_block1:
8905 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8906 break;
8907 case DW_FORM_block2:
8908 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8909 break;
8910 case DW_FORM_block4:
8911 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8912 break;
8913 case DW_FORM_addrx:
8914 case DW_FORM_strx:
8915 case DW_FORM_sdata:
8916 case DW_FORM_udata:
8917 case DW_FORM_ref_udata:
8918 case DW_FORM_GNU_addr_index:
8919 case DW_FORM_GNU_str_index:
8920 case DW_FORM_rnglistx:
8921 case DW_FORM_loclistx:
8922 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8923 break;
8924 case DW_FORM_indirect:
8925 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8926 info_ptr += bytes_read;
8927 /* We need to continue parsing from here, so just go back to
8928 the top. */
8929 goto skip_attribute;
8930
8931 default:
8932 error (_("Dwarf Error: Cannot handle %s "
8933 "in DWARF reader [in module %s]"),
8934 dwarf_form_name (form),
8935 bfd_get_filename (abfd));
8936 }
8937 }
8938
8939 if (abbrev->has_children)
8940 return skip_children (reader, info_ptr);
8941 else
8942 return info_ptr;
8943 }
8944
8945 /* Locate ORIG_PDI's sibling.
8946 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8947
8948 static const gdb_byte *
8949 locate_pdi_sibling (const struct die_reader_specs *reader,
8950 struct partial_die_info *orig_pdi,
8951 const gdb_byte *info_ptr)
8952 {
8953 /* Do we know the sibling already? */
8954
8955 if (orig_pdi->sibling)
8956 return orig_pdi->sibling;
8957
8958 /* Are there any children to deal with? */
8959
8960 if (!orig_pdi->has_children)
8961 return info_ptr;
8962
8963 /* Skip the children the long way. */
8964
8965 return skip_children (reader, info_ptr);
8966 }
8967
8968 /* Expand this partial symbol table into a full symbol table. SELF is
8969 not NULL. */
8970
8971 void
8972 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8973 {
8974 struct dwarf2_per_objfile *dwarf2_per_objfile
8975 = get_dwarf2_per_objfile (objfile);
8976
8977 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8978
8979 /* If this psymtab is constructed from a debug-only objfile, the
8980 has_section_at_zero flag will not necessarily be correct. We
8981 can get the correct value for this flag by looking at the data
8982 associated with the (presumably stripped) associated objfile. */
8983 if (objfile->separate_debug_objfile_backlink)
8984 {
8985 struct dwarf2_per_objfile *dpo_backlink
8986 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8987
8988 dwarf2_per_objfile->per_bfd->has_section_at_zero
8989 = dpo_backlink->per_bfd->has_section_at_zero;
8990 }
8991
8992 expand_psymtab (objfile);
8993
8994 process_cu_includes (dwarf2_per_objfile);
8995 }
8996 \f
8997 /* Reading in full CUs. */
8998
8999 /* Add PER_CU to the queue. */
9000
9001 static void
9002 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9003 dwarf2_per_objfile *per_objfile,
9004 enum language pretend_language)
9005 {
9006 per_cu->queued = 1;
9007 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
9008 }
9009
9010 /* If PER_CU is not yet queued, add it to the queue.
9011 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9012 dependency.
9013 The result is non-zero if PER_CU was queued, otherwise the result is zero
9014 meaning either PER_CU is already queued or it is already loaded.
9015
9016 N.B. There is an invariant here that if a CU is queued then it is loaded.
9017 The caller is required to load PER_CU if we return non-zero. */
9018
9019 static int
9020 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9021 dwarf2_per_cu_data *per_cu,
9022 dwarf2_per_objfile *per_objfile,
9023 enum language pretend_language)
9024 {
9025 /* We may arrive here during partial symbol reading, if we need full
9026 DIEs to process an unusual case (e.g. template arguments). Do
9027 not queue PER_CU, just tell our caller to load its DIEs. */
9028 if (per_cu->per_bfd->reading_partial_symbols)
9029 {
9030 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9031
9032 if (cu == NULL || cu->dies == NULL)
9033 return 1;
9034 return 0;
9035 }
9036
9037 /* Mark the dependence relation so that we don't flush PER_CU
9038 too early. */
9039 if (dependent_cu != NULL)
9040 dwarf2_add_dependence (dependent_cu, per_cu);
9041
9042 /* If it's already on the queue, we have nothing to do. */
9043 if (per_cu->queued)
9044 return 0;
9045
9046 /* If the compilation unit is already loaded, just mark it as
9047 used. */
9048 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9049 if (cu != nullptr)
9050 {
9051 cu->last_used = 0;
9052 return 0;
9053 }
9054
9055 /* Add it to the queue. */
9056 queue_comp_unit (per_cu, per_objfile, pretend_language);
9057
9058 return 1;
9059 }
9060
9061 /* Process the queue. */
9062
9063 static void
9064 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9065 {
9066 if (dwarf_read_debug)
9067 {
9068 fprintf_unfiltered (gdb_stdlog,
9069 "Expanding one or more symtabs of objfile %s ...\n",
9070 objfile_name (dwarf2_per_objfile->objfile));
9071 }
9072
9073 /* The queue starts out with one item, but following a DIE reference
9074 may load a new CU, adding it to the end of the queue. */
9075 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
9076 {
9077 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
9078 dwarf2_per_cu_data *per_cu = item.per_cu;
9079
9080 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9081 {
9082 dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
9083
9084 /* Skip dummy CUs. */
9085 if (cu != nullptr)
9086 {
9087 unsigned int debug_print_threshold;
9088 char buf[100];
9089
9090 if (per_cu->is_debug_types)
9091 {
9092 struct signatured_type *sig_type =
9093 (struct signatured_type *) per_cu;
9094
9095 sprintf (buf, "TU %s at offset %s",
9096 hex_string (sig_type->signature),
9097 sect_offset_str (per_cu->sect_off));
9098 /* There can be 100s of TUs.
9099 Only print them in verbose mode. */
9100 debug_print_threshold = 2;
9101 }
9102 else
9103 {
9104 sprintf (buf, "CU at offset %s",
9105 sect_offset_str (per_cu->sect_off));
9106 debug_print_threshold = 1;
9107 }
9108
9109 if (dwarf_read_debug >= debug_print_threshold)
9110 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9111
9112 if (per_cu->is_debug_types)
9113 process_full_type_unit (cu, item.pretend_language);
9114 else
9115 process_full_comp_unit (cu, item.pretend_language);
9116
9117 if (dwarf_read_debug >= debug_print_threshold)
9118 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9119 }
9120 }
9121
9122 per_cu->queued = 0;
9123 dwarf2_per_objfile->per_bfd->queue.pop ();
9124 }
9125
9126 if (dwarf_read_debug)
9127 {
9128 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9129 objfile_name (dwarf2_per_objfile->objfile));
9130 }
9131 }
9132
9133 /* Read in full symbols for PST, and anything it depends on. */
9134
9135 void
9136 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9137 {
9138 gdb_assert (!readin_p (objfile));
9139
9140 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9141 free_cached_comp_units freer (per_objfile);
9142 expand_dependencies (objfile);
9143
9144 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9145 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9146 }
9147
9148 /* See psympriv.h. */
9149
9150 bool
9151 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9152 {
9153 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9154 return per_objfile->symtab_set_p (per_cu_data);
9155 }
9156
9157 /* See psympriv.h. */
9158
9159 compunit_symtab *
9160 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9161 {
9162 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9163 return per_objfile->get_symtab (per_cu_data);
9164 }
9165
9166 /* Trivial hash function for die_info: the hash value of a DIE
9167 is its offset in .debug_info for this objfile. */
9168
9169 static hashval_t
9170 die_hash (const void *item)
9171 {
9172 const struct die_info *die = (const struct die_info *) item;
9173
9174 return to_underlying (die->sect_off);
9175 }
9176
9177 /* Trivial comparison function for die_info structures: two DIEs
9178 are equal if they have the same offset. */
9179
9180 static int
9181 die_eq (const void *item_lhs, const void *item_rhs)
9182 {
9183 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9184 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9185
9186 return die_lhs->sect_off == die_rhs->sect_off;
9187 }
9188
9189 /* Load the DIEs associated with PER_CU into memory. */
9190
9191 static void
9192 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9193 dwarf2_per_objfile *per_objfile,
9194 bool skip_partial,
9195 enum language pretend_language)
9196 {
9197 gdb_assert (! this_cu->is_debug_types);
9198
9199 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9200 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9201 if (reader.dummy_p)
9202 return;
9203
9204 struct dwarf2_cu *cu = reader.cu;
9205 const gdb_byte *info_ptr = reader.info_ptr;
9206
9207 gdb_assert (cu->die_hash == NULL);
9208 cu->die_hash =
9209 htab_create_alloc_ex (cu->header.length / 12,
9210 die_hash,
9211 die_eq,
9212 NULL,
9213 &cu->comp_unit_obstack,
9214 hashtab_obstack_allocate,
9215 dummy_obstack_deallocate);
9216
9217 if (reader.comp_unit_die->has_children)
9218 reader.comp_unit_die->child
9219 = read_die_and_siblings (&reader, reader.info_ptr,
9220 &info_ptr, reader.comp_unit_die);
9221 cu->dies = reader.comp_unit_die;
9222 /* comp_unit_die is not stored in die_hash, no need. */
9223
9224 /* We try not to read any attributes in this function, because not
9225 all CUs needed for references have been loaded yet, and symbol
9226 table processing isn't initialized. But we have to set the CU language,
9227 or we won't be able to build types correctly.
9228 Similarly, if we do not read the producer, we can not apply
9229 producer-specific interpretation. */
9230 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9231
9232 reader.keep ();
9233 }
9234
9235 /* Add a DIE to the delayed physname list. */
9236
9237 static void
9238 add_to_method_list (struct type *type, int fnfield_index, int index,
9239 const char *name, struct die_info *die,
9240 struct dwarf2_cu *cu)
9241 {
9242 struct delayed_method_info mi;
9243 mi.type = type;
9244 mi.fnfield_index = fnfield_index;
9245 mi.index = index;
9246 mi.name = name;
9247 mi.die = die;
9248 cu->method_list.push_back (mi);
9249 }
9250
9251 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9252 "const" / "volatile". If so, decrements LEN by the length of the
9253 modifier and return true. Otherwise return false. */
9254
9255 template<size_t N>
9256 static bool
9257 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9258 {
9259 size_t mod_len = sizeof (mod) - 1;
9260 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9261 {
9262 len -= mod_len;
9263 return true;
9264 }
9265 return false;
9266 }
9267
9268 /* Compute the physnames of any methods on the CU's method list.
9269
9270 The computation of method physnames is delayed in order to avoid the
9271 (bad) condition that one of the method's formal parameters is of an as yet
9272 incomplete type. */
9273
9274 static void
9275 compute_delayed_physnames (struct dwarf2_cu *cu)
9276 {
9277 /* Only C++ delays computing physnames. */
9278 if (cu->method_list.empty ())
9279 return;
9280 gdb_assert (cu->language == language_cplus);
9281
9282 for (const delayed_method_info &mi : cu->method_list)
9283 {
9284 const char *physname;
9285 struct fn_fieldlist *fn_flp
9286 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9287 physname = dwarf2_physname (mi.name, mi.die, cu);
9288 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9289 = physname ? physname : "";
9290
9291 /* Since there's no tag to indicate whether a method is a
9292 const/volatile overload, extract that information out of the
9293 demangled name. */
9294 if (physname != NULL)
9295 {
9296 size_t len = strlen (physname);
9297
9298 while (1)
9299 {
9300 if (physname[len] == ')') /* shortcut */
9301 break;
9302 else if (check_modifier (physname, len, " const"))
9303 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9304 else if (check_modifier (physname, len, " volatile"))
9305 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9306 else
9307 break;
9308 }
9309 }
9310 }
9311
9312 /* The list is no longer needed. */
9313 cu->method_list.clear ();
9314 }
9315
9316 /* Go objects should be embedded in a DW_TAG_module DIE,
9317 and it's not clear if/how imported objects will appear.
9318 To keep Go support simple until that's worked out,
9319 go back through what we've read and create something usable.
9320 We could do this while processing each DIE, and feels kinda cleaner,
9321 but that way is more invasive.
9322 This is to, for example, allow the user to type "p var" or "b main"
9323 without having to specify the package name, and allow lookups
9324 of module.object to work in contexts that use the expression
9325 parser. */
9326
9327 static void
9328 fixup_go_packaging (struct dwarf2_cu *cu)
9329 {
9330 gdb::unique_xmalloc_ptr<char> package_name;
9331 struct pending *list;
9332 int i;
9333
9334 for (list = *cu->get_builder ()->get_global_symbols ();
9335 list != NULL;
9336 list = list->next)
9337 {
9338 for (i = 0; i < list->nsyms; ++i)
9339 {
9340 struct symbol *sym = list->symbol[i];
9341
9342 if (sym->language () == language_go
9343 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9344 {
9345 gdb::unique_xmalloc_ptr<char> this_package_name
9346 (go_symbol_package_name (sym));
9347
9348 if (this_package_name == NULL)
9349 continue;
9350 if (package_name == NULL)
9351 package_name = std::move (this_package_name);
9352 else
9353 {
9354 struct objfile *objfile = cu->per_objfile->objfile;
9355 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9356 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9357 (symbol_symtab (sym) != NULL
9358 ? symtab_to_filename_for_display
9359 (symbol_symtab (sym))
9360 : objfile_name (objfile)),
9361 this_package_name.get (), package_name.get ());
9362 }
9363 }
9364 }
9365 }
9366
9367 if (package_name != NULL)
9368 {
9369 struct objfile *objfile = cu->per_objfile->objfile;
9370 const char *saved_package_name = objfile->intern (package_name.get ());
9371 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9372 saved_package_name);
9373 struct symbol *sym;
9374
9375 sym = new (&objfile->objfile_obstack) symbol;
9376 sym->set_language (language_go, &objfile->objfile_obstack);
9377 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9378 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9379 e.g., "main" finds the "main" module and not C's main(). */
9380 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9381 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9382 SYMBOL_TYPE (sym) = type;
9383
9384 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9385 }
9386 }
9387
9388 /* Allocate a fully-qualified name consisting of the two parts on the
9389 obstack. */
9390
9391 static const char *
9392 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9393 {
9394 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9395 }
9396
9397 /* A helper that allocates a variant part to attach to a Rust enum
9398 type. OBSTACK is where the results should be allocated. TYPE is
9399 the type we're processing. DISCRIMINANT_INDEX is the index of the
9400 discriminant. It must be the index of one of the fields of TYPE.
9401 DEFAULT_INDEX is the index of the default field; or -1 if there is
9402 no default. RANGES is indexed by "effective" field number (the
9403 field index, but omitting the discriminant and default fields) and
9404 must hold the discriminant values used by the variants. Note that
9405 RANGES must have a lifetime at least as long as OBSTACK -- either
9406 already allocated on it, or static. */
9407
9408 static void
9409 alloc_rust_variant (struct obstack *obstack, struct type *type,
9410 int discriminant_index, int default_index,
9411 gdb::array_view<discriminant_range> ranges)
9412 {
9413 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9414 must be handled by the caller. */
9415 gdb_assert (discriminant_index >= 0
9416 && discriminant_index < type->num_fields ());
9417 gdb_assert (default_index == -1
9418 || (default_index >= 0 && default_index < type->num_fields ()));
9419
9420 /* We have one variant for each non-discriminant field. */
9421 int n_variants = type->num_fields () - 1;
9422
9423 variant *variants = new (obstack) variant[n_variants];
9424 int var_idx = 0;
9425 int range_idx = 0;
9426 for (int i = 0; i < type->num_fields (); ++i)
9427 {
9428 if (i == discriminant_index)
9429 continue;
9430
9431 variants[var_idx].first_field = i;
9432 variants[var_idx].last_field = i + 1;
9433
9434 /* The default field does not need a range, but other fields do.
9435 We skipped the discriminant above. */
9436 if (i != default_index)
9437 {
9438 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9439 ++range_idx;
9440 }
9441
9442 ++var_idx;
9443 }
9444
9445 gdb_assert (range_idx == ranges.size ());
9446 gdb_assert (var_idx == n_variants);
9447
9448 variant_part *part = new (obstack) variant_part;
9449 part->discriminant_index = discriminant_index;
9450 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9451 discriminant_index));
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_TYPE (type, 0);
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 = TYPE_FIELD_TYPE (field_type, index);
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_TYPE (type, 0) = 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_TYPE (type, 1)->name ());
9546 TYPE_FIELD_TYPE (type, 1)->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_TYPE (type, 2) = 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_TYPE (type, 0);
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_TYPE (type, i);
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 = FIELD_TYPE (*disr_field);
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_TYPE (type, i)->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_TYPE (type, i);
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 (struct dwarf2_per_objfile *dwarf2_per_objfile)
9842 {
9843 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9844 {
9845 if (! iter->is_debug_types)
9846 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
9847 }
9848
9849 dwarf2_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 *dwarf2_per_objfile = cu->per_objfile;
9859 struct objfile *objfile = dwarf2_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 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
9942
9943 /* Push it for inclusion processing later. */
9944 dwarf2_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 *dwarf2_per_objfile = cu->per_objfile;
9958 struct objfile *objfile = dwarf2_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 dwarf2_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 dwarf2_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_TYPE (type, 0)->code () == TYPE_CODE_PTR
10515 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10516 0))))
10517 buf.puts (" const");
10518 }
10519 }
10520
10521 const std::string &intermediate_name = buf.string ();
10522
10523 if (cu->language == language_cplus)
10524 canonical_name
10525 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10526 objfile);
10527
10528 /* If we only computed INTERMEDIATE_NAME, or if
10529 INTERMEDIATE_NAME is already canonical, then we need to
10530 intern it. */
10531 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10532 name = objfile->intern (intermediate_name);
10533 else
10534 name = canonical_name;
10535 }
10536 }
10537
10538 return name;
10539 }
10540
10541 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10542 If scope qualifiers are appropriate they will be added. The result
10543 will be allocated on the storage_obstack, or NULL if the DIE does
10544 not have a name. NAME may either be from a previous call to
10545 dwarf2_name or NULL.
10546
10547 The output string will be canonicalized (if C++). */
10548
10549 static const char *
10550 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10551 {
10552 return dwarf2_compute_name (name, die, cu, 0);
10553 }
10554
10555 /* Construct a physname for the given DIE in CU. NAME may either be
10556 from a previous call to dwarf2_name or NULL. The result will be
10557 allocated on the objfile_objstack or NULL if the DIE does not have a
10558 name.
10559
10560 The output string will be canonicalized (if C++). */
10561
10562 static const char *
10563 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10564 {
10565 struct objfile *objfile = cu->per_objfile->objfile;
10566 const char *retval, *mangled = NULL, *canon = NULL;
10567 int need_copy = 1;
10568
10569 /* In this case dwarf2_compute_name is just a shortcut not building anything
10570 on its own. */
10571 if (!die_needs_namespace (die, cu))
10572 return dwarf2_compute_name (name, die, cu, 1);
10573
10574 if (cu->language != language_rust)
10575 mangled = dw2_linkage_name (die, cu);
10576
10577 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10578 has computed. */
10579 gdb::unique_xmalloc_ptr<char> demangled;
10580 if (mangled != NULL)
10581 {
10582
10583 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10584 {
10585 /* Do nothing (do not demangle the symbol name). */
10586 }
10587 else if (cu->language == language_go)
10588 {
10589 /* This is a lie, but we already lie to the caller new_symbol.
10590 new_symbol assumes we return the mangled name.
10591 This just undoes that lie until things are cleaned up. */
10592 }
10593 else
10594 {
10595 /* Use DMGL_RET_DROP for C++ template functions to suppress
10596 their return type. It is easier for GDB users to search
10597 for such functions as `name(params)' than `long name(params)'.
10598 In such case the minimal symbol names do not match the full
10599 symbol names but for template functions there is never a need
10600 to look up their definition from their declaration so
10601 the only disadvantage remains the minimal symbol variant
10602 `long name(params)' does not have the proper inferior type. */
10603 demangled.reset (gdb_demangle (mangled,
10604 (DMGL_PARAMS | DMGL_ANSI
10605 | DMGL_RET_DROP)));
10606 }
10607 if (demangled)
10608 canon = demangled.get ();
10609 else
10610 {
10611 canon = mangled;
10612 need_copy = 0;
10613 }
10614 }
10615
10616 if (canon == NULL || check_physname)
10617 {
10618 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10619
10620 if (canon != NULL && strcmp (physname, canon) != 0)
10621 {
10622 /* It may not mean a bug in GDB. The compiler could also
10623 compute DW_AT_linkage_name incorrectly. But in such case
10624 GDB would need to be bug-to-bug compatible. */
10625
10626 complaint (_("Computed physname <%s> does not match demangled <%s> "
10627 "(from linkage <%s>) - DIE at %s [in module %s]"),
10628 physname, canon, mangled, sect_offset_str (die->sect_off),
10629 objfile_name (objfile));
10630
10631 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10632 is available here - over computed PHYSNAME. It is safer
10633 against both buggy GDB and buggy compilers. */
10634
10635 retval = canon;
10636 }
10637 else
10638 {
10639 retval = physname;
10640 need_copy = 0;
10641 }
10642 }
10643 else
10644 retval = canon;
10645
10646 if (need_copy)
10647 retval = objfile->intern (retval);
10648
10649 return retval;
10650 }
10651
10652 /* Inspect DIE in CU for a namespace alias. If one exists, record
10653 a new symbol for it.
10654
10655 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10656
10657 static int
10658 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10659 {
10660 struct attribute *attr;
10661
10662 /* If the die does not have a name, this is not a namespace
10663 alias. */
10664 attr = dwarf2_attr (die, DW_AT_name, cu);
10665 if (attr != NULL)
10666 {
10667 int num;
10668 struct die_info *d = die;
10669 struct dwarf2_cu *imported_cu = cu;
10670
10671 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10672 keep inspecting DIEs until we hit the underlying import. */
10673 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10674 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10675 {
10676 attr = dwarf2_attr (d, DW_AT_import, cu);
10677 if (attr == NULL)
10678 break;
10679
10680 d = follow_die_ref (d, attr, &imported_cu);
10681 if (d->tag != DW_TAG_imported_declaration)
10682 break;
10683 }
10684
10685 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10686 {
10687 complaint (_("DIE at %s has too many recursively imported "
10688 "declarations"), sect_offset_str (d->sect_off));
10689 return 0;
10690 }
10691
10692 if (attr != NULL)
10693 {
10694 struct type *type;
10695 sect_offset sect_off = attr->get_ref_die_offset ();
10696
10697 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10698 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10699 {
10700 /* This declaration is a global namespace alias. Add
10701 a symbol for it whose type is the aliased namespace. */
10702 new_symbol (die, type, cu);
10703 return 1;
10704 }
10705 }
10706 }
10707
10708 return 0;
10709 }
10710
10711 /* Return the using directives repository (global or local?) to use in the
10712 current context for CU.
10713
10714 For Ada, imported declarations can materialize renamings, which *may* be
10715 global. However it is impossible (for now?) in DWARF to distinguish
10716 "external" imported declarations and "static" ones. As all imported
10717 declarations seem to be static in all other languages, make them all CU-wide
10718 global only in Ada. */
10719
10720 static struct using_direct **
10721 using_directives (struct dwarf2_cu *cu)
10722 {
10723 if (cu->language == language_ada
10724 && cu->get_builder ()->outermost_context_p ())
10725 return cu->get_builder ()->get_global_using_directives ();
10726 else
10727 return cu->get_builder ()->get_local_using_directives ();
10728 }
10729
10730 /* Read the import statement specified by the given die and record it. */
10731
10732 static void
10733 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10734 {
10735 struct objfile *objfile = cu->per_objfile->objfile;
10736 struct attribute *import_attr;
10737 struct die_info *imported_die, *child_die;
10738 struct dwarf2_cu *imported_cu;
10739 const char *imported_name;
10740 const char *imported_name_prefix;
10741 const char *canonical_name;
10742 const char *import_alias;
10743 const char *imported_declaration = NULL;
10744 const char *import_prefix;
10745 std::vector<const char *> excludes;
10746
10747 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10748 if (import_attr == NULL)
10749 {
10750 complaint (_("Tag '%s' has no DW_AT_import"),
10751 dwarf_tag_name (die->tag));
10752 return;
10753 }
10754
10755 imported_cu = cu;
10756 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10757 imported_name = dwarf2_name (imported_die, imported_cu);
10758 if (imported_name == NULL)
10759 {
10760 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10761
10762 The import in the following code:
10763 namespace A
10764 {
10765 typedef int B;
10766 }
10767
10768 int main ()
10769 {
10770 using A::B;
10771 B b;
10772 return b;
10773 }
10774
10775 ...
10776 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10777 <52> DW_AT_decl_file : 1
10778 <53> DW_AT_decl_line : 6
10779 <54> DW_AT_import : <0x75>
10780 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10781 <59> DW_AT_name : B
10782 <5b> DW_AT_decl_file : 1
10783 <5c> DW_AT_decl_line : 2
10784 <5d> DW_AT_type : <0x6e>
10785 ...
10786 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10787 <76> DW_AT_byte_size : 4
10788 <77> DW_AT_encoding : 5 (signed)
10789
10790 imports the wrong die ( 0x75 instead of 0x58 ).
10791 This case will be ignored until the gcc bug is fixed. */
10792 return;
10793 }
10794
10795 /* Figure out the local name after import. */
10796 import_alias = dwarf2_name (die, cu);
10797
10798 /* Figure out where the statement is being imported to. */
10799 import_prefix = determine_prefix (die, cu);
10800
10801 /* Figure out what the scope of the imported die is and prepend it
10802 to the name of the imported die. */
10803 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10804
10805 if (imported_die->tag != DW_TAG_namespace
10806 && imported_die->tag != DW_TAG_module)
10807 {
10808 imported_declaration = imported_name;
10809 canonical_name = imported_name_prefix;
10810 }
10811 else if (strlen (imported_name_prefix) > 0)
10812 canonical_name = obconcat (&objfile->objfile_obstack,
10813 imported_name_prefix,
10814 (cu->language == language_d ? "." : "::"),
10815 imported_name, (char *) NULL);
10816 else
10817 canonical_name = imported_name;
10818
10819 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10820 for (child_die = die->child; child_die && child_die->tag;
10821 child_die = child_die->sibling)
10822 {
10823 /* DWARF-4: A Fortran use statement with a “rename list” may be
10824 represented by an imported module entry with an import attribute
10825 referring to the module and owned entries corresponding to those
10826 entities that are renamed as part of being imported. */
10827
10828 if (child_die->tag != DW_TAG_imported_declaration)
10829 {
10830 complaint (_("child DW_TAG_imported_declaration expected "
10831 "- DIE at %s [in module %s]"),
10832 sect_offset_str (child_die->sect_off),
10833 objfile_name (objfile));
10834 continue;
10835 }
10836
10837 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10838 if (import_attr == NULL)
10839 {
10840 complaint (_("Tag '%s' has no DW_AT_import"),
10841 dwarf_tag_name (child_die->tag));
10842 continue;
10843 }
10844
10845 imported_cu = cu;
10846 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10847 &imported_cu);
10848 imported_name = dwarf2_name (imported_die, imported_cu);
10849 if (imported_name == NULL)
10850 {
10851 complaint (_("child DW_TAG_imported_declaration has unknown "
10852 "imported name - DIE at %s [in module %s]"),
10853 sect_offset_str (child_die->sect_off),
10854 objfile_name (objfile));
10855 continue;
10856 }
10857
10858 excludes.push_back (imported_name);
10859
10860 process_die (child_die, cu);
10861 }
10862
10863 add_using_directive (using_directives (cu),
10864 import_prefix,
10865 canonical_name,
10866 import_alias,
10867 imported_declaration,
10868 excludes,
10869 0,
10870 &objfile->objfile_obstack);
10871 }
10872
10873 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10874 types, but gives them a size of zero. Starting with version 14,
10875 ICC is compatible with GCC. */
10876
10877 static bool
10878 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10879 {
10880 if (!cu->checked_producer)
10881 check_producer (cu);
10882
10883 return cu->producer_is_icc_lt_14;
10884 }
10885
10886 /* ICC generates a DW_AT_type for C void functions. This was observed on
10887 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10888 which says that void functions should not have a DW_AT_type. */
10889
10890 static bool
10891 producer_is_icc (struct dwarf2_cu *cu)
10892 {
10893 if (!cu->checked_producer)
10894 check_producer (cu);
10895
10896 return cu->producer_is_icc;
10897 }
10898
10899 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10900 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10901 this, it was first present in GCC release 4.3.0. */
10902
10903 static bool
10904 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10905 {
10906 if (!cu->checked_producer)
10907 check_producer (cu);
10908
10909 return cu->producer_is_gcc_lt_4_3;
10910 }
10911
10912 static file_and_directory
10913 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10914 {
10915 file_and_directory res;
10916
10917 /* Find the filename. Do not use dwarf2_name here, since the filename
10918 is not a source language identifier. */
10919 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10920 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10921
10922 if (res.comp_dir == NULL
10923 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10924 && IS_ABSOLUTE_PATH (res.name))
10925 {
10926 res.comp_dir_storage = ldirname (res.name);
10927 if (!res.comp_dir_storage.empty ())
10928 res.comp_dir = res.comp_dir_storage.c_str ();
10929 }
10930 if (res.comp_dir != NULL)
10931 {
10932 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10933 directory, get rid of it. */
10934 const char *cp = strchr (res.comp_dir, ':');
10935
10936 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10937 res.comp_dir = cp + 1;
10938 }
10939
10940 if (res.name == NULL)
10941 res.name = "<unknown>";
10942
10943 return res;
10944 }
10945
10946 /* Handle DW_AT_stmt_list for a compilation unit.
10947 DIE is the DW_TAG_compile_unit die for CU.
10948 COMP_DIR is the compilation directory. LOWPC is passed to
10949 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10950
10951 static void
10952 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10953 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10954 {
10955 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10956 struct attribute *attr;
10957 struct line_header line_header_local;
10958 hashval_t line_header_local_hash;
10959 void **slot;
10960 int decode_mapping;
10961
10962 gdb_assert (! cu->per_cu->is_debug_types);
10963
10964 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10965 if (attr == NULL)
10966 return;
10967
10968 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10969
10970 /* The line header hash table is only created if needed (it exists to
10971 prevent redundant reading of the line table for partial_units).
10972 If we're given a partial_unit, we'll need it. If we're given a
10973 compile_unit, then use the line header hash table if it's already
10974 created, but don't create one just yet. */
10975
10976 if (dwarf2_per_objfile->line_header_hash == NULL
10977 && die->tag == DW_TAG_partial_unit)
10978 {
10979 dwarf2_per_objfile->line_header_hash
10980 .reset (htab_create_alloc (127, line_header_hash_voidp,
10981 line_header_eq_voidp,
10982 free_line_header_voidp,
10983 xcalloc, xfree));
10984 }
10985
10986 line_header_local.sect_off = line_offset;
10987 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10988 line_header_local_hash = line_header_hash (&line_header_local);
10989 if (dwarf2_per_objfile->line_header_hash != NULL)
10990 {
10991 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10992 &line_header_local,
10993 line_header_local_hash, NO_INSERT);
10994
10995 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10996 is not present in *SLOT (since if there is something in *SLOT then
10997 it will be for a partial_unit). */
10998 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10999 {
11000 gdb_assert (*slot != NULL);
11001 cu->line_header = (struct line_header *) *slot;
11002 return;
11003 }
11004 }
11005
11006 /* dwarf_decode_line_header does not yet provide sufficient information.
11007 We always have to call also dwarf_decode_lines for it. */
11008 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11009 if (lh == NULL)
11010 return;
11011
11012 cu->line_header = lh.release ();
11013 cu->line_header_die_owner = die;
11014
11015 if (dwarf2_per_objfile->line_header_hash == NULL)
11016 slot = NULL;
11017 else
11018 {
11019 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
11020 &line_header_local,
11021 line_header_local_hash, INSERT);
11022 gdb_assert (slot != NULL);
11023 }
11024 if (slot != NULL && *slot == NULL)
11025 {
11026 /* This newly decoded line number information unit will be owned
11027 by line_header_hash hash table. */
11028 *slot = cu->line_header;
11029 cu->line_header_die_owner = NULL;
11030 }
11031 else
11032 {
11033 /* We cannot free any current entry in (*slot) as that struct line_header
11034 may be already used by multiple CUs. Create only temporary decoded
11035 line_header for this CU - it may happen at most once for each line
11036 number information unit. And if we're not using line_header_hash
11037 then this is what we want as well. */
11038 gdb_assert (die->tag != DW_TAG_partial_unit);
11039 }
11040 decode_mapping = (die->tag != DW_TAG_partial_unit);
11041 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11042 decode_mapping);
11043
11044 }
11045
11046 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11047
11048 static void
11049 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11050 {
11051 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
11052 struct objfile *objfile = dwarf2_per_objfile->objfile;
11053 struct gdbarch *gdbarch = objfile->arch ();
11054 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11055 CORE_ADDR highpc = ((CORE_ADDR) 0);
11056 struct attribute *attr;
11057 struct die_info *child_die;
11058 CORE_ADDR baseaddr;
11059
11060 prepare_one_comp_unit (cu, die, cu->language);
11061 baseaddr = objfile->text_section_offset ();
11062
11063 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11064
11065 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11066 from finish_block. */
11067 if (lowpc == ((CORE_ADDR) -1))
11068 lowpc = highpc;
11069 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11070
11071 file_and_directory fnd = find_file_and_directory (die, cu);
11072
11073 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11074 standardised yet. As a workaround for the language detection we fall
11075 back to the DW_AT_producer string. */
11076 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11077 cu->language = language_opencl;
11078
11079 /* Similar hack for Go. */
11080 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11081 set_cu_language (DW_LANG_Go, cu);
11082
11083 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11084
11085 /* Decode line number information if present. We do this before
11086 processing child DIEs, so that the line header table is available
11087 for DW_AT_decl_file. */
11088 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11089
11090 /* Process all dies in compilation unit. */
11091 if (die->child != NULL)
11092 {
11093 child_die = die->child;
11094 while (child_die && child_die->tag)
11095 {
11096 process_die (child_die, cu);
11097 child_die = child_die->sibling;
11098 }
11099 }
11100
11101 /* Decode macro information, if present. Dwarf 2 macro information
11102 refers to information in the line number info statement program
11103 header, so we can only read it if we've read the header
11104 successfully. */
11105 attr = dwarf2_attr (die, DW_AT_macros, cu);
11106 if (attr == NULL)
11107 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11108 if (attr && cu->line_header)
11109 {
11110 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11111 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11112
11113 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11114 }
11115 else
11116 {
11117 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11118 if (attr && cu->line_header)
11119 {
11120 unsigned int macro_offset = DW_UNSND (attr);
11121
11122 dwarf_decode_macros (cu, macro_offset, 0);
11123 }
11124 }
11125 }
11126
11127 void
11128 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11129 {
11130 struct type_unit_group *tu_group;
11131 int first_time;
11132 struct attribute *attr;
11133 unsigned int i;
11134 struct signatured_type *sig_type;
11135
11136 gdb_assert (per_cu->is_debug_types);
11137 sig_type = (struct signatured_type *) per_cu;
11138
11139 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11140
11141 /* If we're using .gdb_index (includes -readnow) then
11142 per_cu->type_unit_group may not have been set up yet. */
11143 if (sig_type->type_unit_group == NULL)
11144 sig_type->type_unit_group = get_type_unit_group (this, attr);
11145 tu_group = sig_type->type_unit_group;
11146
11147 /* If we've already processed this stmt_list there's no real need to
11148 do it again, we could fake it and just recreate the part we need
11149 (file name,index -> symtab mapping). If data shows this optimization
11150 is useful we can do it then. */
11151 type_unit_group_unshareable *tug_unshare
11152 = per_objfile->get_type_unit_group_unshareable (tu_group);
11153 first_time = tug_unshare->compunit_symtab == NULL;
11154
11155 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11156 debug info. */
11157 line_header_up lh;
11158 if (attr != NULL)
11159 {
11160 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11161 lh = dwarf_decode_line_header (line_offset, this);
11162 }
11163 if (lh == NULL)
11164 {
11165 if (first_time)
11166 start_symtab ("", NULL, 0);
11167 else
11168 {
11169 gdb_assert (tug_unshare->symtabs == NULL);
11170 gdb_assert (m_builder == nullptr);
11171 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11172 m_builder.reset (new struct buildsym_compunit
11173 (COMPUNIT_OBJFILE (cust), "",
11174 COMPUNIT_DIRNAME (cust),
11175 compunit_language (cust),
11176 0, cust));
11177 list_in_scope = get_builder ()->get_file_symbols ();
11178 }
11179 return;
11180 }
11181
11182 line_header = lh.release ();
11183 line_header_die_owner = die;
11184
11185 if (first_time)
11186 {
11187 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11188
11189 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11190 still initializing it, and our caller (a few levels up)
11191 process_full_type_unit still needs to know if this is the first
11192 time. */
11193
11194 tug_unshare->symtabs
11195 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11196 struct symtab *, line_header->file_names_size ());
11197
11198 auto &file_names = line_header->file_names ();
11199 for (i = 0; i < file_names.size (); ++i)
11200 {
11201 file_entry &fe = file_names[i];
11202 dwarf2_start_subfile (this, fe.name,
11203 fe.include_dir (line_header));
11204 buildsym_compunit *b = get_builder ();
11205 if (b->get_current_subfile ()->symtab == NULL)
11206 {
11207 /* NOTE: start_subfile will recognize when it's been
11208 passed a file it has already seen. So we can't
11209 assume there's a simple mapping from
11210 cu->line_header->file_names to subfiles, plus
11211 cu->line_header->file_names may contain dups. */
11212 b->get_current_subfile ()->symtab
11213 = allocate_symtab (cust, b->get_current_subfile ()->name);
11214 }
11215
11216 fe.symtab = b->get_current_subfile ()->symtab;
11217 tug_unshare->symtabs[i] = fe.symtab;
11218 }
11219 }
11220 else
11221 {
11222 gdb_assert (m_builder == nullptr);
11223 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11224 m_builder.reset (new struct buildsym_compunit
11225 (COMPUNIT_OBJFILE (cust), "",
11226 COMPUNIT_DIRNAME (cust),
11227 compunit_language (cust),
11228 0, cust));
11229 list_in_scope = get_builder ()->get_file_symbols ();
11230
11231 auto &file_names = line_header->file_names ();
11232 for (i = 0; i < file_names.size (); ++i)
11233 {
11234 file_entry &fe = file_names[i];
11235 fe.symtab = tug_unshare->symtabs[i];
11236 }
11237 }
11238
11239 /* The main symtab is allocated last. Type units don't have DW_AT_name
11240 so they don't have a "real" (so to speak) symtab anyway.
11241 There is later code that will assign the main symtab to all symbols
11242 that don't have one. We need to handle the case of a symbol with a
11243 missing symtab (DW_AT_decl_file) anyway. */
11244 }
11245
11246 /* Process DW_TAG_type_unit.
11247 For TUs we want to skip the first top level sibling if it's not the
11248 actual type being defined by this TU. In this case the first top
11249 level sibling is there to provide context only. */
11250
11251 static void
11252 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11253 {
11254 struct die_info *child_die;
11255
11256 prepare_one_comp_unit (cu, die, language_minimal);
11257
11258 /* Initialize (or reinitialize) the machinery for building symtabs.
11259 We do this before processing child DIEs, so that the line header table
11260 is available for DW_AT_decl_file. */
11261 cu->setup_type_unit_groups (die);
11262
11263 if (die->child != NULL)
11264 {
11265 child_die = die->child;
11266 while (child_die && child_die->tag)
11267 {
11268 process_die (child_die, cu);
11269 child_die = child_die->sibling;
11270 }
11271 }
11272 }
11273 \f
11274 /* DWO/DWP files.
11275
11276 http://gcc.gnu.org/wiki/DebugFission
11277 http://gcc.gnu.org/wiki/DebugFissionDWP
11278
11279 To simplify handling of both DWO files ("object" files with the DWARF info)
11280 and DWP files (a file with the DWOs packaged up into one file), we treat
11281 DWP files as having a collection of virtual DWO files. */
11282
11283 static hashval_t
11284 hash_dwo_file (const void *item)
11285 {
11286 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11287 hashval_t hash;
11288
11289 hash = htab_hash_string (dwo_file->dwo_name);
11290 if (dwo_file->comp_dir != NULL)
11291 hash += htab_hash_string (dwo_file->comp_dir);
11292 return hash;
11293 }
11294
11295 static int
11296 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11297 {
11298 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11299 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11300
11301 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11302 return 0;
11303 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11304 return lhs->comp_dir == rhs->comp_dir;
11305 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11306 }
11307
11308 /* Allocate a hash table for DWO files. */
11309
11310 static htab_up
11311 allocate_dwo_file_hash_table ()
11312 {
11313 auto delete_dwo_file = [] (void *item)
11314 {
11315 struct dwo_file *dwo_file = (struct dwo_file *) item;
11316
11317 delete dwo_file;
11318 };
11319
11320 return htab_up (htab_create_alloc (41,
11321 hash_dwo_file,
11322 eq_dwo_file,
11323 delete_dwo_file,
11324 xcalloc, xfree));
11325 }
11326
11327 /* Lookup DWO file DWO_NAME. */
11328
11329 static void **
11330 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11331 const char *dwo_name,
11332 const char *comp_dir)
11333 {
11334 struct dwo_file find_entry;
11335 void **slot;
11336
11337 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11338 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11339
11340 find_entry.dwo_name = dwo_name;
11341 find_entry.comp_dir = comp_dir;
11342 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11343 INSERT);
11344
11345 return slot;
11346 }
11347
11348 static hashval_t
11349 hash_dwo_unit (const void *item)
11350 {
11351 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11352
11353 /* This drops the top 32 bits of the id, but is ok for a hash. */
11354 return dwo_unit->signature;
11355 }
11356
11357 static int
11358 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11359 {
11360 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11361 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11362
11363 /* The signature is assumed to be unique within the DWO file.
11364 So while object file CU dwo_id's always have the value zero,
11365 that's OK, assuming each object file DWO file has only one CU,
11366 and that's the rule for now. */
11367 return lhs->signature == rhs->signature;
11368 }
11369
11370 /* Allocate a hash table for DWO CUs,TUs.
11371 There is one of these tables for each of CUs,TUs for each DWO file. */
11372
11373 static htab_up
11374 allocate_dwo_unit_table ()
11375 {
11376 /* Start out with a pretty small number.
11377 Generally DWO files contain only one CU and maybe some TUs. */
11378 return htab_up (htab_create_alloc (3,
11379 hash_dwo_unit,
11380 eq_dwo_unit,
11381 NULL, xcalloc, xfree));
11382 }
11383
11384 /* die_reader_func for create_dwo_cu. */
11385
11386 static void
11387 create_dwo_cu_reader (const struct die_reader_specs *reader,
11388 const gdb_byte *info_ptr,
11389 struct die_info *comp_unit_die,
11390 struct dwo_file *dwo_file,
11391 struct dwo_unit *dwo_unit)
11392 {
11393 struct dwarf2_cu *cu = reader->cu;
11394 sect_offset sect_off = cu->per_cu->sect_off;
11395 struct dwarf2_section_info *section = cu->per_cu->section;
11396
11397 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11398 if (!signature.has_value ())
11399 {
11400 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11401 " its dwo_id [in module %s]"),
11402 sect_offset_str (sect_off), dwo_file->dwo_name);
11403 return;
11404 }
11405
11406 dwo_unit->dwo_file = dwo_file;
11407 dwo_unit->signature = *signature;
11408 dwo_unit->section = section;
11409 dwo_unit->sect_off = sect_off;
11410 dwo_unit->length = cu->per_cu->length;
11411
11412 if (dwarf_read_debug)
11413 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11414 sect_offset_str (sect_off),
11415 hex_string (dwo_unit->signature));
11416 }
11417
11418 /* Create the dwo_units for the CUs in a DWO_FILE.
11419 Note: This function processes DWO files only, not DWP files. */
11420
11421 static void
11422 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11423 dwarf2_cu *cu, struct dwo_file &dwo_file,
11424 dwarf2_section_info &section, htab_up &cus_htab)
11425 {
11426 struct objfile *objfile = dwarf2_per_objfile->objfile;
11427 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11428 const gdb_byte *info_ptr, *end_ptr;
11429
11430 section.read (objfile);
11431 info_ptr = section.buffer;
11432
11433 if (info_ptr == NULL)
11434 return;
11435
11436 if (dwarf_read_debug)
11437 {
11438 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11439 section.get_name (),
11440 section.get_file_name ());
11441 }
11442
11443 end_ptr = info_ptr + section.size;
11444 while (info_ptr < end_ptr)
11445 {
11446 struct dwarf2_per_cu_data per_cu;
11447 struct dwo_unit read_unit {};
11448 struct dwo_unit *dwo_unit;
11449 void **slot;
11450 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11451
11452 memset (&per_cu, 0, sizeof (per_cu));
11453 per_cu.per_bfd = per_bfd;
11454 per_cu.is_debug_types = 0;
11455 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11456 per_cu.section = &section;
11457
11458 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11459 if (!reader.dummy_p)
11460 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11461 &dwo_file, &read_unit);
11462 info_ptr += per_cu.length;
11463
11464 // If the unit could not be parsed, skip it.
11465 if (read_unit.dwo_file == NULL)
11466 continue;
11467
11468 if (cus_htab == NULL)
11469 cus_htab = allocate_dwo_unit_table ();
11470
11471 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11472 struct dwo_unit);
11473 *dwo_unit = read_unit;
11474 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11475 gdb_assert (slot != NULL);
11476 if (*slot != NULL)
11477 {
11478 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11479 sect_offset dup_sect_off = dup_cu->sect_off;
11480
11481 complaint (_("debug cu entry at offset %s is duplicate to"
11482 " the entry at offset %s, signature %s"),
11483 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11484 hex_string (dwo_unit->signature));
11485 }
11486 *slot = (void *)dwo_unit;
11487 }
11488 }
11489
11490 /* DWP file .debug_{cu,tu}_index section format:
11491 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11492
11493 DWP Version 1:
11494
11495 Both index sections have the same format, and serve to map a 64-bit
11496 signature to a set of section numbers. Each section begins with a header,
11497 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11498 indexes, and a pool of 32-bit section numbers. The index sections will be
11499 aligned at 8-byte boundaries in the file.
11500
11501 The index section header consists of:
11502
11503 V, 32 bit version number
11504 -, 32 bits unused
11505 N, 32 bit number of compilation units or type units in the index
11506 M, 32 bit number of slots in the hash table
11507
11508 Numbers are recorded using the byte order of the application binary.
11509
11510 The hash table begins at offset 16 in the section, and consists of an array
11511 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11512 order of the application binary). Unused slots in the hash table are 0.
11513 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11514
11515 The parallel table begins immediately after the hash table
11516 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11517 array of 32-bit indexes (using the byte order of the application binary),
11518 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11519 table contains a 32-bit index into the pool of section numbers. For unused
11520 hash table slots, the corresponding entry in the parallel table will be 0.
11521
11522 The pool of section numbers begins immediately following the hash table
11523 (at offset 16 + 12 * M from the beginning of the section). The pool of
11524 section numbers consists of an array of 32-bit words (using the byte order
11525 of the application binary). Each item in the array is indexed starting
11526 from 0. The hash table entry provides the index of the first section
11527 number in the set. Additional section numbers in the set follow, and the
11528 set is terminated by a 0 entry (section number 0 is not used in ELF).
11529
11530 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11531 section must be the first entry in the set, and the .debug_abbrev.dwo must
11532 be the second entry. Other members of the set may follow in any order.
11533
11534 ---
11535
11536 DWP Version 2:
11537
11538 DWP Version 2 combines all the .debug_info, etc. sections into one,
11539 and the entries in the index tables are now offsets into these sections.
11540 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11541 section.
11542
11543 Index Section Contents:
11544 Header
11545 Hash Table of Signatures dwp_hash_table.hash_table
11546 Parallel Table of Indices dwp_hash_table.unit_table
11547 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11548 Table of Section Sizes dwp_hash_table.v2.sizes
11549
11550 The index section header consists of:
11551
11552 V, 32 bit version number
11553 L, 32 bit number of columns in the table of section offsets
11554 N, 32 bit number of compilation units or type units in the index
11555 M, 32 bit number of slots in the hash table
11556
11557 Numbers are recorded using the byte order of the application binary.
11558
11559 The hash table has the same format as version 1.
11560 The parallel table of indices has the same format as version 1,
11561 except that the entries are origin-1 indices into the table of sections
11562 offsets and the table of section sizes.
11563
11564 The table of offsets begins immediately following the parallel table
11565 (at offset 16 + 12 * M from the beginning of the section). The table is
11566 a two-dimensional array of 32-bit words (using the byte order of the
11567 application binary), with L columns and N+1 rows, in row-major order.
11568 Each row in the array is indexed starting from 0. The first row provides
11569 a key to the remaining rows: each column in this row provides an identifier
11570 for a debug section, and the offsets in the same column of subsequent rows
11571 refer to that section. The section identifiers are:
11572
11573 DW_SECT_INFO 1 .debug_info.dwo
11574 DW_SECT_TYPES 2 .debug_types.dwo
11575 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11576 DW_SECT_LINE 4 .debug_line.dwo
11577 DW_SECT_LOC 5 .debug_loc.dwo
11578 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11579 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11580 DW_SECT_MACRO 8 .debug_macro.dwo
11581
11582 The offsets provided by the CU and TU index sections are the base offsets
11583 for the contributions made by each CU or TU to the corresponding section
11584 in the package file. Each CU and TU header contains an abbrev_offset
11585 field, used to find the abbreviations table for that CU or TU within the
11586 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11587 be interpreted as relative to the base offset given in the index section.
11588 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11589 should be interpreted as relative to the base offset for .debug_line.dwo,
11590 and offsets into other debug sections obtained from DWARF attributes should
11591 also be interpreted as relative to the corresponding base offset.
11592
11593 The table of sizes begins immediately following the table of offsets.
11594 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11595 with L columns and N rows, in row-major order. Each row in the array is
11596 indexed starting from 1 (row 0 is shared by the two tables).
11597
11598 ---
11599
11600 Hash table lookup is handled the same in version 1 and 2:
11601
11602 We assume that N and M will not exceed 2^32 - 1.
11603 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11604
11605 Given a 64-bit compilation unit signature or a type signature S, an entry
11606 in the hash table is located as follows:
11607
11608 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11609 the low-order k bits all set to 1.
11610
11611 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11612
11613 3) If the hash table entry at index H matches the signature, use that
11614 entry. If the hash table entry at index H is unused (all zeroes),
11615 terminate the search: the signature is not present in the table.
11616
11617 4) Let H = (H + H') modulo M. Repeat at Step 3.
11618
11619 Because M > N and H' and M are relatively prime, the search is guaranteed
11620 to stop at an unused slot or find the match. */
11621
11622 /* Create a hash table to map DWO IDs to their CU/TU entry in
11623 .debug_{info,types}.dwo in DWP_FILE.
11624 Returns NULL if there isn't one.
11625 Note: This function processes DWP files only, not DWO files. */
11626
11627 static struct dwp_hash_table *
11628 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11629 struct dwp_file *dwp_file, int is_debug_types)
11630 {
11631 struct objfile *objfile = dwarf2_per_objfile->objfile;
11632 bfd *dbfd = dwp_file->dbfd.get ();
11633 const gdb_byte *index_ptr, *index_end;
11634 struct dwarf2_section_info *index;
11635 uint32_t version, nr_columns, nr_units, nr_slots;
11636 struct dwp_hash_table *htab;
11637
11638 if (is_debug_types)
11639 index = &dwp_file->sections.tu_index;
11640 else
11641 index = &dwp_file->sections.cu_index;
11642
11643 if (index->empty ())
11644 return NULL;
11645 index->read (objfile);
11646
11647 index_ptr = index->buffer;
11648 index_end = index_ptr + index->size;
11649
11650 version = read_4_bytes (dbfd, index_ptr);
11651 index_ptr += 4;
11652 if (version == 2)
11653 nr_columns = read_4_bytes (dbfd, index_ptr);
11654 else
11655 nr_columns = 0;
11656 index_ptr += 4;
11657 nr_units = read_4_bytes (dbfd, index_ptr);
11658 index_ptr += 4;
11659 nr_slots = read_4_bytes (dbfd, index_ptr);
11660 index_ptr += 4;
11661
11662 if (version != 1 && version != 2)
11663 {
11664 error (_("Dwarf Error: unsupported DWP file version (%s)"
11665 " [in module %s]"),
11666 pulongest (version), dwp_file->name);
11667 }
11668 if (nr_slots != (nr_slots & -nr_slots))
11669 {
11670 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11671 " is not power of 2 [in module %s]"),
11672 pulongest (nr_slots), dwp_file->name);
11673 }
11674
11675 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11676 htab->version = version;
11677 htab->nr_columns = nr_columns;
11678 htab->nr_units = nr_units;
11679 htab->nr_slots = nr_slots;
11680 htab->hash_table = index_ptr;
11681 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11682
11683 /* Exit early if the table is empty. */
11684 if (nr_slots == 0 || nr_units == 0
11685 || (version == 2 && nr_columns == 0))
11686 {
11687 /* All must be zero. */
11688 if (nr_slots != 0 || nr_units != 0
11689 || (version == 2 && nr_columns != 0))
11690 {
11691 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11692 " all zero [in modules %s]"),
11693 dwp_file->name);
11694 }
11695 return htab;
11696 }
11697
11698 if (version == 1)
11699 {
11700 htab->section_pool.v1.indices =
11701 htab->unit_table + sizeof (uint32_t) * nr_slots;
11702 /* It's harder to decide whether the section is too small in v1.
11703 V1 is deprecated anyway so we punt. */
11704 }
11705 else
11706 {
11707 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11708 int *ids = htab->section_pool.v2.section_ids;
11709 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11710 /* Reverse map for error checking. */
11711 int ids_seen[DW_SECT_MAX + 1];
11712 int i;
11713
11714 if (nr_columns < 2)
11715 {
11716 error (_("Dwarf Error: bad DWP hash table, too few columns"
11717 " in section table [in module %s]"),
11718 dwp_file->name);
11719 }
11720 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11721 {
11722 error (_("Dwarf Error: bad DWP hash table, too many columns"
11723 " in section table [in module %s]"),
11724 dwp_file->name);
11725 }
11726 memset (ids, 255, sizeof_ids);
11727 memset (ids_seen, 255, sizeof (ids_seen));
11728 for (i = 0; i < nr_columns; ++i)
11729 {
11730 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11731
11732 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11733 {
11734 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11735 " in section table [in module %s]"),
11736 id, dwp_file->name);
11737 }
11738 if (ids_seen[id] != -1)
11739 {
11740 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11741 " id %d in section table [in module %s]"),
11742 id, dwp_file->name);
11743 }
11744 ids_seen[id] = i;
11745 ids[i] = id;
11746 }
11747 /* Must have exactly one info or types section. */
11748 if (((ids_seen[DW_SECT_INFO] != -1)
11749 + (ids_seen[DW_SECT_TYPES] != -1))
11750 != 1)
11751 {
11752 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11753 " DWO info/types section [in module %s]"),
11754 dwp_file->name);
11755 }
11756 /* Must have an abbrev section. */
11757 if (ids_seen[DW_SECT_ABBREV] == -1)
11758 {
11759 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11760 " section [in module %s]"),
11761 dwp_file->name);
11762 }
11763 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11764 htab->section_pool.v2.sizes =
11765 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11766 * nr_units * nr_columns);
11767 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11768 * nr_units * nr_columns))
11769 > index_end)
11770 {
11771 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11772 " [in module %s]"),
11773 dwp_file->name);
11774 }
11775 }
11776
11777 return htab;
11778 }
11779
11780 /* Update SECTIONS with the data from SECTP.
11781
11782 This function is like the other "locate" section routines that are
11783 passed to bfd_map_over_sections, but in this context the sections to
11784 read comes from the DWP V1 hash table, not the full ELF section table.
11785
11786 The result is non-zero for success, or zero if an error was found. */
11787
11788 static int
11789 locate_v1_virtual_dwo_sections (asection *sectp,
11790 struct virtual_v1_dwo_sections *sections)
11791 {
11792 const struct dwop_section_names *names = &dwop_section_names;
11793
11794 if (section_is_p (sectp->name, &names->abbrev_dwo))
11795 {
11796 /* There can be only one. */
11797 if (sections->abbrev.s.section != NULL)
11798 return 0;
11799 sections->abbrev.s.section = sectp;
11800 sections->abbrev.size = bfd_section_size (sectp);
11801 }
11802 else if (section_is_p (sectp->name, &names->info_dwo)
11803 || section_is_p (sectp->name, &names->types_dwo))
11804 {
11805 /* There can be only one. */
11806 if (sections->info_or_types.s.section != NULL)
11807 return 0;
11808 sections->info_or_types.s.section = sectp;
11809 sections->info_or_types.size = bfd_section_size (sectp);
11810 }
11811 else if (section_is_p (sectp->name, &names->line_dwo))
11812 {
11813 /* There can be only one. */
11814 if (sections->line.s.section != NULL)
11815 return 0;
11816 sections->line.s.section = sectp;
11817 sections->line.size = bfd_section_size (sectp);
11818 }
11819 else if (section_is_p (sectp->name, &names->loc_dwo))
11820 {
11821 /* There can be only one. */
11822 if (sections->loc.s.section != NULL)
11823 return 0;
11824 sections->loc.s.section = sectp;
11825 sections->loc.size = bfd_section_size (sectp);
11826 }
11827 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11828 {
11829 /* There can be only one. */
11830 if (sections->macinfo.s.section != NULL)
11831 return 0;
11832 sections->macinfo.s.section = sectp;
11833 sections->macinfo.size = bfd_section_size (sectp);
11834 }
11835 else if (section_is_p (sectp->name, &names->macro_dwo))
11836 {
11837 /* There can be only one. */
11838 if (sections->macro.s.section != NULL)
11839 return 0;
11840 sections->macro.s.section = sectp;
11841 sections->macro.size = bfd_section_size (sectp);
11842 }
11843 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11844 {
11845 /* There can be only one. */
11846 if (sections->str_offsets.s.section != NULL)
11847 return 0;
11848 sections->str_offsets.s.section = sectp;
11849 sections->str_offsets.size = bfd_section_size (sectp);
11850 }
11851 else
11852 {
11853 /* No other kind of section is valid. */
11854 return 0;
11855 }
11856
11857 return 1;
11858 }
11859
11860 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11861 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11862 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11863 This is for DWP version 1 files. */
11864
11865 static struct dwo_unit *
11866 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11867 struct dwp_file *dwp_file,
11868 uint32_t unit_index,
11869 const char *comp_dir,
11870 ULONGEST signature, int is_debug_types)
11871 {
11872 const struct dwp_hash_table *dwp_htab =
11873 is_debug_types ? dwp_file->tus : dwp_file->cus;
11874 bfd *dbfd = dwp_file->dbfd.get ();
11875 const char *kind = is_debug_types ? "TU" : "CU";
11876 struct dwo_file *dwo_file;
11877 struct dwo_unit *dwo_unit;
11878 struct virtual_v1_dwo_sections sections;
11879 void **dwo_file_slot;
11880 int i;
11881
11882 gdb_assert (dwp_file->version == 1);
11883
11884 if (dwarf_read_debug)
11885 {
11886 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11887 kind,
11888 pulongest (unit_index), hex_string (signature),
11889 dwp_file->name);
11890 }
11891
11892 /* Fetch the sections of this DWO unit.
11893 Put a limit on the number of sections we look for so that bad data
11894 doesn't cause us to loop forever. */
11895
11896 #define MAX_NR_V1_DWO_SECTIONS \
11897 (1 /* .debug_info or .debug_types */ \
11898 + 1 /* .debug_abbrev */ \
11899 + 1 /* .debug_line */ \
11900 + 1 /* .debug_loc */ \
11901 + 1 /* .debug_str_offsets */ \
11902 + 1 /* .debug_macro or .debug_macinfo */ \
11903 + 1 /* trailing zero */)
11904
11905 memset (&sections, 0, sizeof (sections));
11906
11907 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11908 {
11909 asection *sectp;
11910 uint32_t section_nr =
11911 read_4_bytes (dbfd,
11912 dwp_htab->section_pool.v1.indices
11913 + (unit_index + i) * sizeof (uint32_t));
11914
11915 if (section_nr == 0)
11916 break;
11917 if (section_nr >= dwp_file->num_sections)
11918 {
11919 error (_("Dwarf Error: bad DWP hash table, section number too large"
11920 " [in module %s]"),
11921 dwp_file->name);
11922 }
11923
11924 sectp = dwp_file->elf_sections[section_nr];
11925 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11926 {
11927 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11928 " [in module %s]"),
11929 dwp_file->name);
11930 }
11931 }
11932
11933 if (i < 2
11934 || sections.info_or_types.empty ()
11935 || sections.abbrev.empty ())
11936 {
11937 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11938 " [in module %s]"),
11939 dwp_file->name);
11940 }
11941 if (i == MAX_NR_V1_DWO_SECTIONS)
11942 {
11943 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11944 " [in module %s]"),
11945 dwp_file->name);
11946 }
11947
11948 /* It's easier for the rest of the code if we fake a struct dwo_file and
11949 have dwo_unit "live" in that. At least for now.
11950
11951 The DWP file can be made up of a random collection of CUs and TUs.
11952 However, for each CU + set of TUs that came from the same original DWO
11953 file, we can combine them back into a virtual DWO file to save space
11954 (fewer struct dwo_file objects to allocate). Remember that for really
11955 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11956
11957 std::string virtual_dwo_name =
11958 string_printf ("virtual-dwo/%d-%d-%d-%d",
11959 sections.abbrev.get_id (),
11960 sections.line.get_id (),
11961 sections.loc.get_id (),
11962 sections.str_offsets.get_id ());
11963 /* Can we use an existing virtual DWO file? */
11964 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11965 virtual_dwo_name.c_str (),
11966 comp_dir);
11967 /* Create one if necessary. */
11968 if (*dwo_file_slot == NULL)
11969 {
11970 if (dwarf_read_debug)
11971 {
11972 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11973 virtual_dwo_name.c_str ());
11974 }
11975 dwo_file = new struct dwo_file;
11976 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11977 dwo_file->comp_dir = comp_dir;
11978 dwo_file->sections.abbrev = sections.abbrev;
11979 dwo_file->sections.line = sections.line;
11980 dwo_file->sections.loc = sections.loc;
11981 dwo_file->sections.macinfo = sections.macinfo;
11982 dwo_file->sections.macro = sections.macro;
11983 dwo_file->sections.str_offsets = sections.str_offsets;
11984 /* The "str" section is global to the entire DWP file. */
11985 dwo_file->sections.str = dwp_file->sections.str;
11986 /* The info or types section is assigned below to dwo_unit,
11987 there's no need to record it in dwo_file.
11988 Also, we can't simply record type sections in dwo_file because
11989 we record a pointer into the vector in dwo_unit. As we collect more
11990 types we'll grow the vector and eventually have to reallocate space
11991 for it, invalidating all copies of pointers into the previous
11992 contents. */
11993 *dwo_file_slot = dwo_file;
11994 }
11995 else
11996 {
11997 if (dwarf_read_debug)
11998 {
11999 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12000 virtual_dwo_name.c_str ());
12001 }
12002 dwo_file = (struct dwo_file *) *dwo_file_slot;
12003 }
12004
12005 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12006 dwo_unit->dwo_file = dwo_file;
12007 dwo_unit->signature = signature;
12008 dwo_unit->section =
12009 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12010 *dwo_unit->section = sections.info_or_types;
12011 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12012
12013 return dwo_unit;
12014 }
12015
12016 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12017 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12018 piece within that section used by a TU/CU, return a virtual section
12019 of just that piece. */
12020
12021 static struct dwarf2_section_info
12022 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12023 struct dwarf2_section_info *section,
12024 bfd_size_type offset, bfd_size_type size)
12025 {
12026 struct dwarf2_section_info result;
12027 asection *sectp;
12028
12029 gdb_assert (section != NULL);
12030 gdb_assert (!section->is_virtual);
12031
12032 memset (&result, 0, sizeof (result));
12033 result.s.containing_section = section;
12034 result.is_virtual = true;
12035
12036 if (size == 0)
12037 return result;
12038
12039 sectp = section->get_bfd_section ();
12040
12041 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12042 bounds of the real section. This is a pretty-rare event, so just
12043 flag an error (easier) instead of a warning and trying to cope. */
12044 if (sectp == NULL
12045 || offset + size > bfd_section_size (sectp))
12046 {
12047 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12048 " in section %s [in module %s]"),
12049 sectp ? bfd_section_name (sectp) : "<unknown>",
12050 objfile_name (dwarf2_per_objfile->objfile));
12051 }
12052
12053 result.virtual_offset = offset;
12054 result.size = size;
12055 return result;
12056 }
12057
12058 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12059 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12060 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12061 This is for DWP version 2 files. */
12062
12063 static struct dwo_unit *
12064 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12065 struct dwp_file *dwp_file,
12066 uint32_t unit_index,
12067 const char *comp_dir,
12068 ULONGEST signature, int is_debug_types)
12069 {
12070 const struct dwp_hash_table *dwp_htab =
12071 is_debug_types ? dwp_file->tus : dwp_file->cus;
12072 bfd *dbfd = dwp_file->dbfd.get ();
12073 const char *kind = is_debug_types ? "TU" : "CU";
12074 struct dwo_file *dwo_file;
12075 struct dwo_unit *dwo_unit;
12076 struct virtual_v2_dwo_sections sections;
12077 void **dwo_file_slot;
12078 int i;
12079
12080 gdb_assert (dwp_file->version == 2);
12081
12082 if (dwarf_read_debug)
12083 {
12084 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12085 kind,
12086 pulongest (unit_index), hex_string (signature),
12087 dwp_file->name);
12088 }
12089
12090 /* Fetch the section offsets of this DWO unit. */
12091
12092 memset (&sections, 0, sizeof (sections));
12093
12094 for (i = 0; i < dwp_htab->nr_columns; ++i)
12095 {
12096 uint32_t offset = read_4_bytes (dbfd,
12097 dwp_htab->section_pool.v2.offsets
12098 + (((unit_index - 1) * dwp_htab->nr_columns
12099 + i)
12100 * sizeof (uint32_t)));
12101 uint32_t size = read_4_bytes (dbfd,
12102 dwp_htab->section_pool.v2.sizes
12103 + (((unit_index - 1) * dwp_htab->nr_columns
12104 + i)
12105 * sizeof (uint32_t)));
12106
12107 switch (dwp_htab->section_pool.v2.section_ids[i])
12108 {
12109 case DW_SECT_INFO:
12110 case DW_SECT_TYPES:
12111 sections.info_or_types_offset = offset;
12112 sections.info_or_types_size = size;
12113 break;
12114 case DW_SECT_ABBREV:
12115 sections.abbrev_offset = offset;
12116 sections.abbrev_size = size;
12117 break;
12118 case DW_SECT_LINE:
12119 sections.line_offset = offset;
12120 sections.line_size = size;
12121 break;
12122 case DW_SECT_LOC:
12123 sections.loc_offset = offset;
12124 sections.loc_size = size;
12125 break;
12126 case DW_SECT_STR_OFFSETS:
12127 sections.str_offsets_offset = offset;
12128 sections.str_offsets_size = size;
12129 break;
12130 case DW_SECT_MACINFO:
12131 sections.macinfo_offset = offset;
12132 sections.macinfo_size = size;
12133 break;
12134 case DW_SECT_MACRO:
12135 sections.macro_offset = offset;
12136 sections.macro_size = size;
12137 break;
12138 }
12139 }
12140
12141 /* It's easier for the rest of the code if we fake a struct dwo_file and
12142 have dwo_unit "live" in that. At least for now.
12143
12144 The DWP file can be made up of a random collection of CUs and TUs.
12145 However, for each CU + set of TUs that came from the same original DWO
12146 file, we can combine them back into a virtual DWO file to save space
12147 (fewer struct dwo_file objects to allocate). Remember that for really
12148 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12149
12150 std::string virtual_dwo_name =
12151 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12152 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12153 (long) (sections.line_size ? sections.line_offset : 0),
12154 (long) (sections.loc_size ? sections.loc_offset : 0),
12155 (long) (sections.str_offsets_size
12156 ? sections.str_offsets_offset : 0));
12157 /* Can we use an existing virtual DWO file? */
12158 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12159 virtual_dwo_name.c_str (),
12160 comp_dir);
12161 /* Create one if necessary. */
12162 if (*dwo_file_slot == NULL)
12163 {
12164 if (dwarf_read_debug)
12165 {
12166 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12167 virtual_dwo_name.c_str ());
12168 }
12169 dwo_file = new struct dwo_file;
12170 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12171 dwo_file->comp_dir = comp_dir;
12172 dwo_file->sections.abbrev =
12173 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12174 sections.abbrev_offset, sections.abbrev_size);
12175 dwo_file->sections.line =
12176 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12177 sections.line_offset, sections.line_size);
12178 dwo_file->sections.loc =
12179 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12180 sections.loc_offset, sections.loc_size);
12181 dwo_file->sections.macinfo =
12182 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12183 sections.macinfo_offset, sections.macinfo_size);
12184 dwo_file->sections.macro =
12185 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12186 sections.macro_offset, sections.macro_size);
12187 dwo_file->sections.str_offsets =
12188 create_dwp_v2_section (dwarf2_per_objfile,
12189 &dwp_file->sections.str_offsets,
12190 sections.str_offsets_offset,
12191 sections.str_offsets_size);
12192 /* The "str" section is global to the entire DWP file. */
12193 dwo_file->sections.str = dwp_file->sections.str;
12194 /* The info or types section is assigned below to dwo_unit,
12195 there's no need to record it in dwo_file.
12196 Also, we can't simply record type sections in dwo_file because
12197 we record a pointer into the vector in dwo_unit. As we collect more
12198 types we'll grow the vector and eventually have to reallocate space
12199 for it, invalidating all copies of pointers into the previous
12200 contents. */
12201 *dwo_file_slot = dwo_file;
12202 }
12203 else
12204 {
12205 if (dwarf_read_debug)
12206 {
12207 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12208 virtual_dwo_name.c_str ());
12209 }
12210 dwo_file = (struct dwo_file *) *dwo_file_slot;
12211 }
12212
12213 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12214 dwo_unit->dwo_file = dwo_file;
12215 dwo_unit->signature = signature;
12216 dwo_unit->section =
12217 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12218 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12219 is_debug_types
12220 ? &dwp_file->sections.types
12221 : &dwp_file->sections.info,
12222 sections.info_or_types_offset,
12223 sections.info_or_types_size);
12224 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12225
12226 return dwo_unit;
12227 }
12228
12229 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12230 Returns NULL if the signature isn't found. */
12231
12232 static struct dwo_unit *
12233 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12234 struct dwp_file *dwp_file, const char *comp_dir,
12235 ULONGEST signature, int is_debug_types)
12236 {
12237 const struct dwp_hash_table *dwp_htab =
12238 is_debug_types ? dwp_file->tus : dwp_file->cus;
12239 bfd *dbfd = dwp_file->dbfd.get ();
12240 uint32_t mask = dwp_htab->nr_slots - 1;
12241 uint32_t hash = signature & mask;
12242 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12243 unsigned int i;
12244 void **slot;
12245 struct dwo_unit find_dwo_cu;
12246
12247 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12248 find_dwo_cu.signature = signature;
12249 slot = htab_find_slot (is_debug_types
12250 ? dwp_file->loaded_tus.get ()
12251 : dwp_file->loaded_cus.get (),
12252 &find_dwo_cu, INSERT);
12253
12254 if (*slot != NULL)
12255 return (struct dwo_unit *) *slot;
12256
12257 /* Use a for loop so that we don't loop forever on bad debug info. */
12258 for (i = 0; i < dwp_htab->nr_slots; ++i)
12259 {
12260 ULONGEST signature_in_table;
12261
12262 signature_in_table =
12263 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12264 if (signature_in_table == signature)
12265 {
12266 uint32_t unit_index =
12267 read_4_bytes (dbfd,
12268 dwp_htab->unit_table + hash * sizeof (uint32_t));
12269
12270 if (dwp_file->version == 1)
12271 {
12272 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12273 dwp_file, unit_index,
12274 comp_dir, signature,
12275 is_debug_types);
12276 }
12277 else
12278 {
12279 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12280 dwp_file, unit_index,
12281 comp_dir, signature,
12282 is_debug_types);
12283 }
12284 return (struct dwo_unit *) *slot;
12285 }
12286 if (signature_in_table == 0)
12287 return NULL;
12288 hash = (hash + hash2) & mask;
12289 }
12290
12291 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12292 " [in module %s]"),
12293 dwp_file->name);
12294 }
12295
12296 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12297 Open the file specified by FILE_NAME and hand it off to BFD for
12298 preliminary analysis. Return a newly initialized bfd *, which
12299 includes a canonicalized copy of FILE_NAME.
12300 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12301 SEARCH_CWD is true if the current directory is to be searched.
12302 It will be searched before debug-file-directory.
12303 If successful, the file is added to the bfd include table of the
12304 objfile's bfd (see gdb_bfd_record_inclusion).
12305 If unable to find/open the file, return NULL.
12306 NOTE: This function is derived from symfile_bfd_open. */
12307
12308 static gdb_bfd_ref_ptr
12309 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12310 const char *file_name, int is_dwp, int search_cwd)
12311 {
12312 int desc;
12313 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12314 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12315 to debug_file_directory. */
12316 const char *search_path;
12317 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12318
12319 gdb::unique_xmalloc_ptr<char> search_path_holder;
12320 if (search_cwd)
12321 {
12322 if (*debug_file_directory != '\0')
12323 {
12324 search_path_holder.reset (concat (".", dirname_separator_string,
12325 debug_file_directory,
12326 (char *) NULL));
12327 search_path = search_path_holder.get ();
12328 }
12329 else
12330 search_path = ".";
12331 }
12332 else
12333 search_path = debug_file_directory;
12334
12335 openp_flags flags = OPF_RETURN_REALPATH;
12336 if (is_dwp)
12337 flags |= OPF_SEARCH_IN_PATH;
12338
12339 gdb::unique_xmalloc_ptr<char> absolute_name;
12340 desc = openp (search_path, flags, file_name,
12341 O_RDONLY | O_BINARY, &absolute_name);
12342 if (desc < 0)
12343 return NULL;
12344
12345 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12346 gnutarget, desc));
12347 if (sym_bfd == NULL)
12348 return NULL;
12349 bfd_set_cacheable (sym_bfd.get (), 1);
12350
12351 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12352 return NULL;
12353
12354 /* Success. Record the bfd as having been included by the objfile's bfd.
12355 This is important because things like demangled_names_hash lives in the
12356 objfile's per_bfd space and may have references to things like symbol
12357 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12358 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12359
12360 return sym_bfd;
12361 }
12362
12363 /* Try to open DWO file FILE_NAME.
12364 COMP_DIR is the DW_AT_comp_dir attribute.
12365 The result is the bfd handle of the file.
12366 If there is a problem finding or opening the file, return NULL.
12367 Upon success, the canonicalized path of the file is stored in the bfd,
12368 same as symfile_bfd_open. */
12369
12370 static gdb_bfd_ref_ptr
12371 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12372 const char *file_name, const char *comp_dir)
12373 {
12374 if (IS_ABSOLUTE_PATH (file_name))
12375 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12376 0 /*is_dwp*/, 0 /*search_cwd*/);
12377
12378 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12379
12380 if (comp_dir != NULL)
12381 {
12382 gdb::unique_xmalloc_ptr<char> path_to_try
12383 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12384
12385 /* NOTE: If comp_dir is a relative path, this will also try the
12386 search path, which seems useful. */
12387 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12388 path_to_try.get (),
12389 0 /*is_dwp*/,
12390 1 /*search_cwd*/));
12391 if (abfd != NULL)
12392 return abfd;
12393 }
12394
12395 /* That didn't work, try debug-file-directory, which, despite its name,
12396 is a list of paths. */
12397
12398 if (*debug_file_directory == '\0')
12399 return NULL;
12400
12401 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12402 0 /*is_dwp*/, 1 /*search_cwd*/);
12403 }
12404
12405 /* This function is mapped across the sections and remembers the offset and
12406 size of each of the DWO debugging sections we are interested in. */
12407
12408 static void
12409 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12410 {
12411 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12412 const struct dwop_section_names *names = &dwop_section_names;
12413
12414 if (section_is_p (sectp->name, &names->abbrev_dwo))
12415 {
12416 dwo_sections->abbrev.s.section = sectp;
12417 dwo_sections->abbrev.size = bfd_section_size (sectp);
12418 }
12419 else if (section_is_p (sectp->name, &names->info_dwo))
12420 {
12421 dwo_sections->info.s.section = sectp;
12422 dwo_sections->info.size = bfd_section_size (sectp);
12423 }
12424 else if (section_is_p (sectp->name, &names->line_dwo))
12425 {
12426 dwo_sections->line.s.section = sectp;
12427 dwo_sections->line.size = bfd_section_size (sectp);
12428 }
12429 else if (section_is_p (sectp->name, &names->loc_dwo))
12430 {
12431 dwo_sections->loc.s.section = sectp;
12432 dwo_sections->loc.size = bfd_section_size (sectp);
12433 }
12434 else if (section_is_p (sectp->name, &names->loclists_dwo))
12435 {
12436 dwo_sections->loclists.s.section = sectp;
12437 dwo_sections->loclists.size = bfd_section_size (sectp);
12438 }
12439 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12440 {
12441 dwo_sections->macinfo.s.section = sectp;
12442 dwo_sections->macinfo.size = bfd_section_size (sectp);
12443 }
12444 else if (section_is_p (sectp->name, &names->macro_dwo))
12445 {
12446 dwo_sections->macro.s.section = sectp;
12447 dwo_sections->macro.size = bfd_section_size (sectp);
12448 }
12449 else if (section_is_p (sectp->name, &names->str_dwo))
12450 {
12451 dwo_sections->str.s.section = sectp;
12452 dwo_sections->str.size = bfd_section_size (sectp);
12453 }
12454 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12455 {
12456 dwo_sections->str_offsets.s.section = sectp;
12457 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12458 }
12459 else if (section_is_p (sectp->name, &names->types_dwo))
12460 {
12461 struct dwarf2_section_info type_section;
12462
12463 memset (&type_section, 0, sizeof (type_section));
12464 type_section.s.section = sectp;
12465 type_section.size = bfd_section_size (sectp);
12466 dwo_sections->types.push_back (type_section);
12467 }
12468 }
12469
12470 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12471 by PER_CU. This is for the non-DWP case.
12472 The result is NULL if DWO_NAME can't be found. */
12473
12474 static struct dwo_file *
12475 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12476 const char *comp_dir)
12477 {
12478 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12479
12480 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12481 if (dbfd == NULL)
12482 {
12483 if (dwarf_read_debug)
12484 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12485 return NULL;
12486 }
12487
12488 dwo_file_up dwo_file (new struct dwo_file);
12489 dwo_file->dwo_name = dwo_name;
12490 dwo_file->comp_dir = comp_dir;
12491 dwo_file->dbfd = std::move (dbfd);
12492
12493 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12494 &dwo_file->sections);
12495
12496 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
12497 dwo_file->sections.info, dwo_file->cus);
12498
12499 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12500 dwo_file->sections.types, dwo_file->tus);
12501
12502 if (dwarf_read_debug)
12503 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12504
12505 return dwo_file.release ();
12506 }
12507
12508 /* This function is mapped across the sections and remembers the offset and
12509 size of each of the DWP debugging sections common to version 1 and 2 that
12510 we are interested in. */
12511
12512 static void
12513 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12514 void *dwp_file_ptr)
12515 {
12516 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12517 const struct dwop_section_names *names = &dwop_section_names;
12518 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12519
12520 /* Record the ELF section number for later lookup: this is what the
12521 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12522 gdb_assert (elf_section_nr < dwp_file->num_sections);
12523 dwp_file->elf_sections[elf_section_nr] = sectp;
12524
12525 /* Look for specific sections that we need. */
12526 if (section_is_p (sectp->name, &names->str_dwo))
12527 {
12528 dwp_file->sections.str.s.section = sectp;
12529 dwp_file->sections.str.size = bfd_section_size (sectp);
12530 }
12531 else if (section_is_p (sectp->name, &names->cu_index))
12532 {
12533 dwp_file->sections.cu_index.s.section = sectp;
12534 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12535 }
12536 else if (section_is_p (sectp->name, &names->tu_index))
12537 {
12538 dwp_file->sections.tu_index.s.section = sectp;
12539 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12540 }
12541 }
12542
12543 /* This function is mapped across the sections and remembers the offset and
12544 size of each of the DWP version 2 debugging sections that we are interested
12545 in. This is split into a separate function because we don't know if we
12546 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12547
12548 static void
12549 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12550 {
12551 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12552 const struct dwop_section_names *names = &dwop_section_names;
12553 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12554
12555 /* Record the ELF section number for later lookup: this is what the
12556 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12557 gdb_assert (elf_section_nr < dwp_file->num_sections);
12558 dwp_file->elf_sections[elf_section_nr] = sectp;
12559
12560 /* Look for specific sections that we need. */
12561 if (section_is_p (sectp->name, &names->abbrev_dwo))
12562 {
12563 dwp_file->sections.abbrev.s.section = sectp;
12564 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12565 }
12566 else if (section_is_p (sectp->name, &names->info_dwo))
12567 {
12568 dwp_file->sections.info.s.section = sectp;
12569 dwp_file->sections.info.size = bfd_section_size (sectp);
12570 }
12571 else if (section_is_p (sectp->name, &names->line_dwo))
12572 {
12573 dwp_file->sections.line.s.section = sectp;
12574 dwp_file->sections.line.size = bfd_section_size (sectp);
12575 }
12576 else if (section_is_p (sectp->name, &names->loc_dwo))
12577 {
12578 dwp_file->sections.loc.s.section = sectp;
12579 dwp_file->sections.loc.size = bfd_section_size (sectp);
12580 }
12581 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12582 {
12583 dwp_file->sections.macinfo.s.section = sectp;
12584 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12585 }
12586 else if (section_is_p (sectp->name, &names->macro_dwo))
12587 {
12588 dwp_file->sections.macro.s.section = sectp;
12589 dwp_file->sections.macro.size = bfd_section_size (sectp);
12590 }
12591 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12592 {
12593 dwp_file->sections.str_offsets.s.section = sectp;
12594 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12595 }
12596 else if (section_is_p (sectp->name, &names->types_dwo))
12597 {
12598 dwp_file->sections.types.s.section = sectp;
12599 dwp_file->sections.types.size = bfd_section_size (sectp);
12600 }
12601 }
12602
12603 /* Hash function for dwp_file loaded CUs/TUs. */
12604
12605 static hashval_t
12606 hash_dwp_loaded_cutus (const void *item)
12607 {
12608 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12609
12610 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12611 return dwo_unit->signature;
12612 }
12613
12614 /* Equality function for dwp_file loaded CUs/TUs. */
12615
12616 static int
12617 eq_dwp_loaded_cutus (const void *a, const void *b)
12618 {
12619 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12620 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12621
12622 return dua->signature == dub->signature;
12623 }
12624
12625 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12626
12627 static htab_up
12628 allocate_dwp_loaded_cutus_table ()
12629 {
12630 return htab_up (htab_create_alloc (3,
12631 hash_dwp_loaded_cutus,
12632 eq_dwp_loaded_cutus,
12633 NULL, xcalloc, xfree));
12634 }
12635
12636 /* Try to open DWP file FILE_NAME.
12637 The result is the bfd handle of the file.
12638 If there is a problem finding or opening the file, return NULL.
12639 Upon success, the canonicalized path of the file is stored in the bfd,
12640 same as symfile_bfd_open. */
12641
12642 static gdb_bfd_ref_ptr
12643 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12644 const char *file_name)
12645 {
12646 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12647 1 /*is_dwp*/,
12648 1 /*search_cwd*/));
12649 if (abfd != NULL)
12650 return abfd;
12651
12652 /* Work around upstream bug 15652.
12653 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12654 [Whether that's a "bug" is debatable, but it is getting in our way.]
12655 We have no real idea where the dwp file is, because gdb's realpath-ing
12656 of the executable's path may have discarded the needed info.
12657 [IWBN if the dwp file name was recorded in the executable, akin to
12658 .gnu_debuglink, but that doesn't exist yet.]
12659 Strip the directory from FILE_NAME and search again. */
12660 if (*debug_file_directory != '\0')
12661 {
12662 /* Don't implicitly search the current directory here.
12663 If the user wants to search "." to handle this case,
12664 it must be added to debug-file-directory. */
12665 return try_open_dwop_file (dwarf2_per_objfile,
12666 lbasename (file_name), 1 /*is_dwp*/,
12667 0 /*search_cwd*/);
12668 }
12669
12670 return NULL;
12671 }
12672
12673 /* Initialize the use of the DWP file for the current objfile.
12674 By convention the name of the DWP file is ${objfile}.dwp.
12675 The result is NULL if it can't be found. */
12676
12677 static std::unique_ptr<struct dwp_file>
12678 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12679 {
12680 struct objfile *objfile = dwarf2_per_objfile->objfile;
12681
12682 /* Try to find first .dwp for the binary file before any symbolic links
12683 resolving. */
12684
12685 /* If the objfile is a debug file, find the name of the real binary
12686 file and get the name of dwp file from there. */
12687 std::string dwp_name;
12688 if (objfile->separate_debug_objfile_backlink != NULL)
12689 {
12690 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12691 const char *backlink_basename = lbasename (backlink->original_name);
12692
12693 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12694 }
12695 else
12696 dwp_name = objfile->original_name;
12697
12698 dwp_name += ".dwp";
12699
12700 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12701 if (dbfd == NULL
12702 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12703 {
12704 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12705 dwp_name = objfile_name (objfile);
12706 dwp_name += ".dwp";
12707 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12708 }
12709
12710 if (dbfd == NULL)
12711 {
12712 if (dwarf_read_debug)
12713 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12714 return std::unique_ptr<dwp_file> ();
12715 }
12716
12717 const char *name = bfd_get_filename (dbfd.get ());
12718 std::unique_ptr<struct dwp_file> dwp_file
12719 (new struct dwp_file (name, std::move (dbfd)));
12720
12721 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12722 dwp_file->elf_sections =
12723 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12724 dwp_file->num_sections, asection *);
12725
12726 bfd_map_over_sections (dwp_file->dbfd.get (),
12727 dwarf2_locate_common_dwp_sections,
12728 dwp_file.get ());
12729
12730 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12731 0);
12732
12733 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12734 1);
12735
12736 /* The DWP file version is stored in the hash table. Oh well. */
12737 if (dwp_file->cus && dwp_file->tus
12738 && dwp_file->cus->version != dwp_file->tus->version)
12739 {
12740 /* Technically speaking, we should try to limp along, but this is
12741 pretty bizarre. We use pulongest here because that's the established
12742 portability solution (e.g, we cannot use %u for uint32_t). */
12743 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12744 " TU version %s [in DWP file %s]"),
12745 pulongest (dwp_file->cus->version),
12746 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12747 }
12748
12749 if (dwp_file->cus)
12750 dwp_file->version = dwp_file->cus->version;
12751 else if (dwp_file->tus)
12752 dwp_file->version = dwp_file->tus->version;
12753 else
12754 dwp_file->version = 2;
12755
12756 if (dwp_file->version == 2)
12757 bfd_map_over_sections (dwp_file->dbfd.get (),
12758 dwarf2_locate_v2_dwp_sections,
12759 dwp_file.get ());
12760
12761 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12762 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12763
12764 if (dwarf_read_debug)
12765 {
12766 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12767 fprintf_unfiltered (gdb_stdlog,
12768 " %s CUs, %s TUs\n",
12769 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12770 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12771 }
12772
12773 return dwp_file;
12774 }
12775
12776 /* Wrapper around open_and_init_dwp_file, only open it once. */
12777
12778 static struct dwp_file *
12779 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12780 {
12781 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12782 {
12783 dwarf2_per_objfile->per_bfd->dwp_file
12784 = open_and_init_dwp_file (dwarf2_per_objfile);
12785 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12786 }
12787 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12788 }
12789
12790 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12791 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12792 or in the DWP file for the objfile, referenced by THIS_UNIT.
12793 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12794 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12795
12796 This is called, for example, when wanting to read a variable with a
12797 complex location. Therefore we don't want to do file i/o for every call.
12798 Therefore we don't want to look for a DWO file on every call.
12799 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12800 then we check if we've already seen DWO_NAME, and only THEN do we check
12801 for a DWO file.
12802
12803 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12804 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12805
12806 static struct dwo_unit *
12807 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12808 ULONGEST signature, int is_debug_types)
12809 {
12810 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12811 struct objfile *objfile = dwarf2_per_objfile->objfile;
12812 const char *kind = is_debug_types ? "TU" : "CU";
12813 void **dwo_file_slot;
12814 struct dwo_file *dwo_file;
12815 struct dwp_file *dwp_file;
12816
12817 /* First see if there's a DWP file.
12818 If we have a DWP file but didn't find the DWO inside it, don't
12819 look for the original DWO file. It makes gdb behave differently
12820 depending on whether one is debugging in the build tree. */
12821
12822 dwp_file = get_dwp_file (dwarf2_per_objfile);
12823 if (dwp_file != NULL)
12824 {
12825 const struct dwp_hash_table *dwp_htab =
12826 is_debug_types ? dwp_file->tus : dwp_file->cus;
12827
12828 if (dwp_htab != NULL)
12829 {
12830 struct dwo_unit *dwo_cutu =
12831 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12832 signature, is_debug_types);
12833
12834 if (dwo_cutu != NULL)
12835 {
12836 if (dwarf_read_debug)
12837 {
12838 fprintf_unfiltered (gdb_stdlog,
12839 "Virtual DWO %s %s found: @%s\n",
12840 kind, hex_string (signature),
12841 host_address_to_string (dwo_cutu));
12842 }
12843 return dwo_cutu;
12844 }
12845 }
12846 }
12847 else
12848 {
12849 /* No DWP file, look for the DWO file. */
12850
12851 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12852 dwo_name, comp_dir);
12853 if (*dwo_file_slot == NULL)
12854 {
12855 /* Read in the file and build a table of the CUs/TUs it contains. */
12856 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12857 }
12858 /* NOTE: This will be NULL if unable to open the file. */
12859 dwo_file = (struct dwo_file *) *dwo_file_slot;
12860
12861 if (dwo_file != NULL)
12862 {
12863 struct dwo_unit *dwo_cutu = NULL;
12864
12865 if (is_debug_types && dwo_file->tus)
12866 {
12867 struct dwo_unit find_dwo_cutu;
12868
12869 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12870 find_dwo_cutu.signature = signature;
12871 dwo_cutu
12872 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12873 &find_dwo_cutu);
12874 }
12875 else if (!is_debug_types && dwo_file->cus)
12876 {
12877 struct dwo_unit find_dwo_cutu;
12878
12879 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12880 find_dwo_cutu.signature = signature;
12881 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12882 &find_dwo_cutu);
12883 }
12884
12885 if (dwo_cutu != NULL)
12886 {
12887 if (dwarf_read_debug)
12888 {
12889 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12890 kind, dwo_name, hex_string (signature),
12891 host_address_to_string (dwo_cutu));
12892 }
12893 return dwo_cutu;
12894 }
12895 }
12896 }
12897
12898 /* We didn't find it. This could mean a dwo_id mismatch, or
12899 someone deleted the DWO/DWP file, or the search path isn't set up
12900 correctly to find the file. */
12901
12902 if (dwarf_read_debug)
12903 {
12904 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12905 kind, dwo_name, hex_string (signature));
12906 }
12907
12908 /* This is a warning and not a complaint because it can be caused by
12909 pilot error (e.g., user accidentally deleting the DWO). */
12910 {
12911 /* Print the name of the DWP file if we looked there, helps the user
12912 better diagnose the problem. */
12913 std::string dwp_text;
12914
12915 if (dwp_file != NULL)
12916 dwp_text = string_printf (" [in DWP file %s]",
12917 lbasename (dwp_file->name));
12918
12919 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12920 " [in module %s]"),
12921 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12922 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12923 }
12924 return NULL;
12925 }
12926
12927 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12928 See lookup_dwo_cutu_unit for details. */
12929
12930 static struct dwo_unit *
12931 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12932 ULONGEST signature)
12933 {
12934 gdb_assert (!cu->per_cu->is_debug_types);
12935
12936 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12937 }
12938
12939 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12940 See lookup_dwo_cutu_unit for details. */
12941
12942 static struct dwo_unit *
12943 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12944 {
12945 gdb_assert (cu->per_cu->is_debug_types);
12946
12947 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12948
12949 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12950 }
12951
12952 /* Traversal function for queue_and_load_all_dwo_tus. */
12953
12954 static int
12955 queue_and_load_dwo_tu (void **slot, void *info)
12956 {
12957 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12958 dwarf2_cu *cu = (dwarf2_cu *) info;
12959 ULONGEST signature = dwo_unit->signature;
12960 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12961
12962 if (sig_type != NULL)
12963 {
12964 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12965
12966 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12967 a real dependency of PER_CU on SIG_TYPE. That is detected later
12968 while processing PER_CU. */
12969 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12970 load_full_type_unit (sig_cu, cu->per_objfile);
12971 cu->per_cu->imported_symtabs_push (sig_cu);
12972 }
12973
12974 return 1;
12975 }
12976
12977 /* Queue all TUs contained in the DWO of CU to be read in.
12978 The DWO may have the only definition of the type, though it may not be
12979 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12980 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12981
12982 static void
12983 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12984 {
12985 struct dwo_unit *dwo_unit;
12986 struct dwo_file *dwo_file;
12987
12988 gdb_assert (cu != nullptr);
12989 gdb_assert (!cu->per_cu->is_debug_types);
12990 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12991
12992 dwo_unit = cu->dwo_unit;
12993 gdb_assert (dwo_unit != NULL);
12994
12995 dwo_file = dwo_unit->dwo_file;
12996 if (dwo_file->tus != NULL)
12997 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12998 }
12999
13000 /* Read in various DIEs. */
13001
13002 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13003 Inherit only the children of the DW_AT_abstract_origin DIE not being
13004 already referenced by DW_AT_abstract_origin from the children of the
13005 current DIE. */
13006
13007 static void
13008 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13009 {
13010 struct die_info *child_die;
13011 sect_offset *offsetp;
13012 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13013 struct die_info *origin_die;
13014 /* Iterator of the ORIGIN_DIE children. */
13015 struct die_info *origin_child_die;
13016 struct attribute *attr;
13017 struct dwarf2_cu *origin_cu;
13018 struct pending **origin_previous_list_in_scope;
13019
13020 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13021 if (!attr)
13022 return;
13023
13024 /* Note that following die references may follow to a die in a
13025 different cu. */
13026
13027 origin_cu = cu;
13028 origin_die = follow_die_ref (die, attr, &origin_cu);
13029
13030 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13031 symbols in. */
13032 origin_previous_list_in_scope = origin_cu->list_in_scope;
13033 origin_cu->list_in_scope = cu->list_in_scope;
13034
13035 if (die->tag != origin_die->tag
13036 && !(die->tag == DW_TAG_inlined_subroutine
13037 && origin_die->tag == DW_TAG_subprogram))
13038 complaint (_("DIE %s and its abstract origin %s have different tags"),
13039 sect_offset_str (die->sect_off),
13040 sect_offset_str (origin_die->sect_off));
13041
13042 std::vector<sect_offset> offsets;
13043
13044 for (child_die = die->child;
13045 child_die && child_die->tag;
13046 child_die = child_die->sibling)
13047 {
13048 struct die_info *child_origin_die;
13049 struct dwarf2_cu *child_origin_cu;
13050
13051 /* We are trying to process concrete instance entries:
13052 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13053 it's not relevant to our analysis here. i.e. detecting DIEs that are
13054 present in the abstract instance but not referenced in the concrete
13055 one. */
13056 if (child_die->tag == DW_TAG_call_site
13057 || child_die->tag == DW_TAG_GNU_call_site)
13058 continue;
13059
13060 /* For each CHILD_DIE, find the corresponding child of
13061 ORIGIN_DIE. If there is more than one layer of
13062 DW_AT_abstract_origin, follow them all; there shouldn't be,
13063 but GCC versions at least through 4.4 generate this (GCC PR
13064 40573). */
13065 child_origin_die = child_die;
13066 child_origin_cu = cu;
13067 while (1)
13068 {
13069 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13070 child_origin_cu);
13071 if (attr == NULL)
13072 break;
13073 child_origin_die = follow_die_ref (child_origin_die, attr,
13074 &child_origin_cu);
13075 }
13076
13077 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13078 counterpart may exist. */
13079 if (child_origin_die != child_die)
13080 {
13081 if (child_die->tag != child_origin_die->tag
13082 && !(child_die->tag == DW_TAG_inlined_subroutine
13083 && child_origin_die->tag == DW_TAG_subprogram))
13084 complaint (_("Child DIE %s and its abstract origin %s have "
13085 "different tags"),
13086 sect_offset_str (child_die->sect_off),
13087 sect_offset_str (child_origin_die->sect_off));
13088 if (child_origin_die->parent != origin_die)
13089 complaint (_("Child DIE %s and its abstract origin %s have "
13090 "different parents"),
13091 sect_offset_str (child_die->sect_off),
13092 sect_offset_str (child_origin_die->sect_off));
13093 else
13094 offsets.push_back (child_origin_die->sect_off);
13095 }
13096 }
13097 std::sort (offsets.begin (), offsets.end ());
13098 sect_offset *offsets_end = offsets.data () + offsets.size ();
13099 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13100 if (offsetp[-1] == *offsetp)
13101 complaint (_("Multiple children of DIE %s refer "
13102 "to DIE %s as their abstract origin"),
13103 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13104
13105 offsetp = offsets.data ();
13106 origin_child_die = origin_die->child;
13107 while (origin_child_die && origin_child_die->tag)
13108 {
13109 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13110 while (offsetp < offsets_end
13111 && *offsetp < origin_child_die->sect_off)
13112 offsetp++;
13113 if (offsetp >= offsets_end
13114 || *offsetp > origin_child_die->sect_off)
13115 {
13116 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13117 Check whether we're already processing ORIGIN_CHILD_DIE.
13118 This can happen with mutually referenced abstract_origins.
13119 PR 16581. */
13120 if (!origin_child_die->in_process)
13121 process_die (origin_child_die, origin_cu);
13122 }
13123 origin_child_die = origin_child_die->sibling;
13124 }
13125 origin_cu->list_in_scope = origin_previous_list_in_scope;
13126
13127 if (cu != origin_cu)
13128 compute_delayed_physnames (origin_cu);
13129 }
13130
13131 static void
13132 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13133 {
13134 struct objfile *objfile = cu->per_objfile->objfile;
13135 struct gdbarch *gdbarch = objfile->arch ();
13136 struct context_stack *newobj;
13137 CORE_ADDR lowpc;
13138 CORE_ADDR highpc;
13139 struct die_info *child_die;
13140 struct attribute *attr, *call_line, *call_file;
13141 const char *name;
13142 CORE_ADDR baseaddr;
13143 struct block *block;
13144 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13145 std::vector<struct symbol *> template_args;
13146 struct template_symbol *templ_func = NULL;
13147
13148 if (inlined_func)
13149 {
13150 /* If we do not have call site information, we can't show the
13151 caller of this inlined function. That's too confusing, so
13152 only use the scope for local variables. */
13153 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13154 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13155 if (call_line == NULL || call_file == NULL)
13156 {
13157 read_lexical_block_scope (die, cu);
13158 return;
13159 }
13160 }
13161
13162 baseaddr = objfile->text_section_offset ();
13163
13164 name = dwarf2_name (die, cu);
13165
13166 /* Ignore functions with missing or empty names. These are actually
13167 illegal according to the DWARF standard. */
13168 if (name == NULL)
13169 {
13170 complaint (_("missing name for subprogram DIE at %s"),
13171 sect_offset_str (die->sect_off));
13172 return;
13173 }
13174
13175 /* Ignore functions with missing or invalid low and high pc attributes. */
13176 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13177 <= PC_BOUNDS_INVALID)
13178 {
13179 attr = dwarf2_attr (die, DW_AT_external, cu);
13180 if (!attr || !DW_UNSND (attr))
13181 complaint (_("cannot get low and high bounds "
13182 "for subprogram DIE at %s"),
13183 sect_offset_str (die->sect_off));
13184 return;
13185 }
13186
13187 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13188 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13189
13190 /* If we have any template arguments, then we must allocate a
13191 different sort of symbol. */
13192 for (child_die = die->child; child_die; child_die = child_die->sibling)
13193 {
13194 if (child_die->tag == DW_TAG_template_type_param
13195 || child_die->tag == DW_TAG_template_value_param)
13196 {
13197 templ_func = new (&objfile->objfile_obstack) template_symbol;
13198 templ_func->subclass = SYMBOL_TEMPLATE;
13199 break;
13200 }
13201 }
13202
13203 newobj = cu->get_builder ()->push_context (0, lowpc);
13204 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13205 (struct symbol *) templ_func);
13206
13207 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13208 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13209 cu->language);
13210
13211 /* If there is a location expression for DW_AT_frame_base, record
13212 it. */
13213 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13214 if (attr != nullptr)
13215 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13216
13217 /* If there is a location for the static link, record it. */
13218 newobj->static_link = NULL;
13219 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13220 if (attr != nullptr)
13221 {
13222 newobj->static_link
13223 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13224 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13225 cu->addr_type ());
13226 }
13227
13228 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13229
13230 if (die->child != NULL)
13231 {
13232 child_die = die->child;
13233 while (child_die && child_die->tag)
13234 {
13235 if (child_die->tag == DW_TAG_template_type_param
13236 || child_die->tag == DW_TAG_template_value_param)
13237 {
13238 struct symbol *arg = new_symbol (child_die, NULL, cu);
13239
13240 if (arg != NULL)
13241 template_args.push_back (arg);
13242 }
13243 else
13244 process_die (child_die, cu);
13245 child_die = child_die->sibling;
13246 }
13247 }
13248
13249 inherit_abstract_dies (die, cu);
13250
13251 /* If we have a DW_AT_specification, we might need to import using
13252 directives from the context of the specification DIE. See the
13253 comment in determine_prefix. */
13254 if (cu->language == language_cplus
13255 && dwarf2_attr (die, DW_AT_specification, cu))
13256 {
13257 struct dwarf2_cu *spec_cu = cu;
13258 struct die_info *spec_die = die_specification (die, &spec_cu);
13259
13260 while (spec_die)
13261 {
13262 child_die = spec_die->child;
13263 while (child_die && child_die->tag)
13264 {
13265 if (child_die->tag == DW_TAG_imported_module)
13266 process_die (child_die, spec_cu);
13267 child_die = child_die->sibling;
13268 }
13269
13270 /* In some cases, GCC generates specification DIEs that
13271 themselves contain DW_AT_specification attributes. */
13272 spec_die = die_specification (spec_die, &spec_cu);
13273 }
13274 }
13275
13276 struct context_stack cstk = cu->get_builder ()->pop_context ();
13277 /* Make a block for the local symbols within. */
13278 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13279 cstk.static_link, lowpc, highpc);
13280
13281 /* For C++, set the block's scope. */
13282 if ((cu->language == language_cplus
13283 || cu->language == language_fortran
13284 || cu->language == language_d
13285 || cu->language == language_rust)
13286 && cu->processing_has_namespace_info)
13287 block_set_scope (block, determine_prefix (die, cu),
13288 &objfile->objfile_obstack);
13289
13290 /* If we have address ranges, record them. */
13291 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13292
13293 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13294
13295 /* Attach template arguments to function. */
13296 if (!template_args.empty ())
13297 {
13298 gdb_assert (templ_func != NULL);
13299
13300 templ_func->n_template_arguments = template_args.size ();
13301 templ_func->template_arguments
13302 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13303 templ_func->n_template_arguments);
13304 memcpy (templ_func->template_arguments,
13305 template_args.data (),
13306 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13307
13308 /* Make sure that the symtab is set on the new symbols. Even
13309 though they don't appear in this symtab directly, other parts
13310 of gdb assume that symbols do, and this is reasonably
13311 true. */
13312 for (symbol *sym : template_args)
13313 symbol_set_symtab (sym, symbol_symtab (templ_func));
13314 }
13315
13316 /* In C++, we can have functions nested inside functions (e.g., when
13317 a function declares a class that has methods). This means that
13318 when we finish processing a function scope, we may need to go
13319 back to building a containing block's symbol lists. */
13320 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13321 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13322
13323 /* If we've finished processing a top-level function, subsequent
13324 symbols go in the file symbol list. */
13325 if (cu->get_builder ()->outermost_context_p ())
13326 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13327 }
13328
13329 /* Process all the DIES contained within a lexical block scope. Start
13330 a new scope, process the dies, and then close the scope. */
13331
13332 static void
13333 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13334 {
13335 struct objfile *objfile = cu->per_objfile->objfile;
13336 struct gdbarch *gdbarch = objfile->arch ();
13337 CORE_ADDR lowpc, highpc;
13338 struct die_info *child_die;
13339 CORE_ADDR baseaddr;
13340
13341 baseaddr = objfile->text_section_offset ();
13342
13343 /* Ignore blocks with missing or invalid low and high pc attributes. */
13344 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13345 as multiple lexical blocks? Handling children in a sane way would
13346 be nasty. Might be easier to properly extend generic blocks to
13347 describe ranges. */
13348 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13349 {
13350 case PC_BOUNDS_NOT_PRESENT:
13351 /* DW_TAG_lexical_block has no attributes, process its children as if
13352 there was no wrapping by that DW_TAG_lexical_block.
13353 GCC does no longer produces such DWARF since GCC r224161. */
13354 for (child_die = die->child;
13355 child_die != NULL && child_die->tag;
13356 child_die = child_die->sibling)
13357 {
13358 /* We might already be processing this DIE. This can happen
13359 in an unusual circumstance -- where a subroutine A
13360 appears lexically in another subroutine B, but A actually
13361 inlines B. The recursion is broken here, rather than in
13362 inherit_abstract_dies, because it seems better to simply
13363 drop concrete children here. */
13364 if (!child_die->in_process)
13365 process_die (child_die, cu);
13366 }
13367 return;
13368 case PC_BOUNDS_INVALID:
13369 return;
13370 }
13371 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13372 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13373
13374 cu->get_builder ()->push_context (0, lowpc);
13375 if (die->child != NULL)
13376 {
13377 child_die = die->child;
13378 while (child_die && child_die->tag)
13379 {
13380 process_die (child_die, cu);
13381 child_die = child_die->sibling;
13382 }
13383 }
13384 inherit_abstract_dies (die, cu);
13385 struct context_stack cstk = cu->get_builder ()->pop_context ();
13386
13387 if (*cu->get_builder ()->get_local_symbols () != NULL
13388 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13389 {
13390 struct block *block
13391 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13392 cstk.start_addr, highpc);
13393
13394 /* Note that recording ranges after traversing children, as we
13395 do here, means that recording a parent's ranges entails
13396 walking across all its children's ranges as they appear in
13397 the address map, which is quadratic behavior.
13398
13399 It would be nicer to record the parent's ranges before
13400 traversing its children, simply overriding whatever you find
13401 there. But since we don't even decide whether to create a
13402 block until after we've traversed its children, that's hard
13403 to do. */
13404 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13405 }
13406 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13407 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13408 }
13409
13410 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13411
13412 static void
13413 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13414 {
13415 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13416 struct objfile *objfile = per_objfile->objfile;
13417 struct gdbarch *gdbarch = objfile->arch ();
13418 CORE_ADDR pc, baseaddr;
13419 struct attribute *attr;
13420 struct call_site *call_site, call_site_local;
13421 void **slot;
13422 int nparams;
13423 struct die_info *child_die;
13424
13425 baseaddr = objfile->text_section_offset ();
13426
13427 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13428 if (attr == NULL)
13429 {
13430 /* This was a pre-DWARF-5 GNU extension alias
13431 for DW_AT_call_return_pc. */
13432 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13433 }
13434 if (!attr)
13435 {
13436 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13437 "DIE %s [in module %s]"),
13438 sect_offset_str (die->sect_off), objfile_name (objfile));
13439 return;
13440 }
13441 pc = attr->value_as_address () + baseaddr;
13442 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13443
13444 if (cu->call_site_htab == NULL)
13445 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13446 NULL, &objfile->objfile_obstack,
13447 hashtab_obstack_allocate, NULL);
13448 call_site_local.pc = pc;
13449 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13450 if (*slot != NULL)
13451 {
13452 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13453 "DIE %s [in module %s]"),
13454 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13455 objfile_name (objfile));
13456 return;
13457 }
13458
13459 /* Count parameters at the caller. */
13460
13461 nparams = 0;
13462 for (child_die = die->child; child_die && child_die->tag;
13463 child_die = child_die->sibling)
13464 {
13465 if (child_die->tag != DW_TAG_call_site_parameter
13466 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13467 {
13468 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13469 "DW_TAG_call_site child DIE %s [in module %s]"),
13470 child_die->tag, sect_offset_str (child_die->sect_off),
13471 objfile_name (objfile));
13472 continue;
13473 }
13474
13475 nparams++;
13476 }
13477
13478 call_site
13479 = ((struct call_site *)
13480 obstack_alloc (&objfile->objfile_obstack,
13481 sizeof (*call_site)
13482 + (sizeof (*call_site->parameter) * (nparams - 1))));
13483 *slot = call_site;
13484 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13485 call_site->pc = pc;
13486
13487 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13488 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13489 {
13490 struct die_info *func_die;
13491
13492 /* Skip also over DW_TAG_inlined_subroutine. */
13493 for (func_die = die->parent;
13494 func_die && func_die->tag != DW_TAG_subprogram
13495 && func_die->tag != DW_TAG_subroutine_type;
13496 func_die = func_die->parent);
13497
13498 /* DW_AT_call_all_calls is a superset
13499 of DW_AT_call_all_tail_calls. */
13500 if (func_die
13501 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13502 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13503 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13504 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13505 {
13506 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13507 not complete. But keep CALL_SITE for look ups via call_site_htab,
13508 both the initial caller containing the real return address PC and
13509 the final callee containing the current PC of a chain of tail
13510 calls do not need to have the tail call list complete. But any
13511 function candidate for a virtual tail call frame searched via
13512 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13513 determined unambiguously. */
13514 }
13515 else
13516 {
13517 struct type *func_type = NULL;
13518
13519 if (func_die)
13520 func_type = get_die_type (func_die, cu);
13521 if (func_type != NULL)
13522 {
13523 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13524
13525 /* Enlist this call site to the function. */
13526 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13527 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13528 }
13529 else
13530 complaint (_("Cannot find function owning DW_TAG_call_site "
13531 "DIE %s [in module %s]"),
13532 sect_offset_str (die->sect_off), objfile_name (objfile));
13533 }
13534 }
13535
13536 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13537 if (attr == NULL)
13538 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13539 if (attr == NULL)
13540 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13541 if (attr == NULL)
13542 {
13543 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13544 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13545 }
13546 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13547 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13548 /* Keep NULL DWARF_BLOCK. */;
13549 else if (attr->form_is_block ())
13550 {
13551 struct dwarf2_locexpr_baton *dlbaton;
13552
13553 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13554 dlbaton->data = DW_BLOCK (attr)->data;
13555 dlbaton->size = DW_BLOCK (attr)->size;
13556 dlbaton->per_objfile = per_objfile;
13557 dlbaton->per_cu = cu->per_cu;
13558
13559 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13560 }
13561 else if (attr->form_is_ref ())
13562 {
13563 struct dwarf2_cu *target_cu = cu;
13564 struct die_info *target_die;
13565
13566 target_die = follow_die_ref (die, attr, &target_cu);
13567 gdb_assert (target_cu->per_objfile->objfile == objfile);
13568 if (die_is_declaration (target_die, target_cu))
13569 {
13570 const char *target_physname;
13571
13572 /* Prefer the mangled name; otherwise compute the demangled one. */
13573 target_physname = dw2_linkage_name (target_die, target_cu);
13574 if (target_physname == NULL)
13575 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13576 if (target_physname == NULL)
13577 complaint (_("DW_AT_call_target target DIE has invalid "
13578 "physname, for referencing DIE %s [in module %s]"),
13579 sect_offset_str (die->sect_off), objfile_name (objfile));
13580 else
13581 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13582 }
13583 else
13584 {
13585 CORE_ADDR lowpc;
13586
13587 /* DW_AT_entry_pc should be preferred. */
13588 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13589 <= PC_BOUNDS_INVALID)
13590 complaint (_("DW_AT_call_target target DIE has invalid "
13591 "low pc, for referencing DIE %s [in module %s]"),
13592 sect_offset_str (die->sect_off), objfile_name (objfile));
13593 else
13594 {
13595 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13596 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13597 }
13598 }
13599 }
13600 else
13601 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13602 "block nor reference, for DIE %s [in module %s]"),
13603 sect_offset_str (die->sect_off), objfile_name (objfile));
13604
13605 call_site->per_cu = cu->per_cu;
13606 call_site->per_objfile = per_objfile;
13607
13608 for (child_die = die->child;
13609 child_die && child_die->tag;
13610 child_die = child_die->sibling)
13611 {
13612 struct call_site_parameter *parameter;
13613 struct attribute *loc, *origin;
13614
13615 if (child_die->tag != DW_TAG_call_site_parameter
13616 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13617 {
13618 /* Already printed the complaint above. */
13619 continue;
13620 }
13621
13622 gdb_assert (call_site->parameter_count < nparams);
13623 parameter = &call_site->parameter[call_site->parameter_count];
13624
13625 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13626 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13627 register is contained in DW_AT_call_value. */
13628
13629 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13630 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13631 if (origin == NULL)
13632 {
13633 /* This was a pre-DWARF-5 GNU extension alias
13634 for DW_AT_call_parameter. */
13635 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13636 }
13637 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13638 {
13639 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13640
13641 sect_offset sect_off = origin->get_ref_die_offset ();
13642 if (!cu->header.offset_in_cu_p (sect_off))
13643 {
13644 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13645 binding can be done only inside one CU. Such referenced DIE
13646 therefore cannot be even moved to DW_TAG_partial_unit. */
13647 complaint (_("DW_AT_call_parameter offset is not in CU for "
13648 "DW_TAG_call_site child DIE %s [in module %s]"),
13649 sect_offset_str (child_die->sect_off),
13650 objfile_name (objfile));
13651 continue;
13652 }
13653 parameter->u.param_cu_off
13654 = (cu_offset) (sect_off - cu->header.sect_off);
13655 }
13656 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13657 {
13658 complaint (_("No DW_FORM_block* DW_AT_location for "
13659 "DW_TAG_call_site child DIE %s [in module %s]"),
13660 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13661 continue;
13662 }
13663 else
13664 {
13665 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13666 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13667 if (parameter->u.dwarf_reg != -1)
13668 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13669 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13670 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13671 &parameter->u.fb_offset))
13672 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13673 else
13674 {
13675 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13676 "for DW_FORM_block* DW_AT_location is supported for "
13677 "DW_TAG_call_site child DIE %s "
13678 "[in module %s]"),
13679 sect_offset_str (child_die->sect_off),
13680 objfile_name (objfile));
13681 continue;
13682 }
13683 }
13684
13685 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13686 if (attr == NULL)
13687 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13688 if (attr == NULL || !attr->form_is_block ())
13689 {
13690 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13691 "DW_TAG_call_site child DIE %s [in module %s]"),
13692 sect_offset_str (child_die->sect_off),
13693 objfile_name (objfile));
13694 continue;
13695 }
13696 parameter->value = DW_BLOCK (attr)->data;
13697 parameter->value_size = DW_BLOCK (attr)->size;
13698
13699 /* Parameters are not pre-cleared by memset above. */
13700 parameter->data_value = NULL;
13701 parameter->data_value_size = 0;
13702 call_site->parameter_count++;
13703
13704 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13705 if (attr == NULL)
13706 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13707 if (attr != nullptr)
13708 {
13709 if (!attr->form_is_block ())
13710 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13711 "DW_TAG_call_site child DIE %s [in module %s]"),
13712 sect_offset_str (child_die->sect_off),
13713 objfile_name (objfile));
13714 else
13715 {
13716 parameter->data_value = DW_BLOCK (attr)->data;
13717 parameter->data_value_size = DW_BLOCK (attr)->size;
13718 }
13719 }
13720 }
13721 }
13722
13723 /* Helper function for read_variable. If DIE represents a virtual
13724 table, then return the type of the concrete object that is
13725 associated with the virtual table. Otherwise, return NULL. */
13726
13727 static struct type *
13728 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13729 {
13730 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13731 if (attr == NULL)
13732 return NULL;
13733
13734 /* Find the type DIE. */
13735 struct die_info *type_die = NULL;
13736 struct dwarf2_cu *type_cu = cu;
13737
13738 if (attr->form_is_ref ())
13739 type_die = follow_die_ref (die, attr, &type_cu);
13740 if (type_die == NULL)
13741 return NULL;
13742
13743 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13744 return NULL;
13745 return die_containing_type (type_die, type_cu);
13746 }
13747
13748 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13749
13750 static void
13751 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13752 {
13753 struct rust_vtable_symbol *storage = NULL;
13754
13755 if (cu->language == language_rust)
13756 {
13757 struct type *containing_type = rust_containing_type (die, cu);
13758
13759 if (containing_type != NULL)
13760 {
13761 struct objfile *objfile = cu->per_objfile->objfile;
13762
13763 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13764 storage->concrete_type = containing_type;
13765 storage->subclass = SYMBOL_RUST_VTABLE;
13766 }
13767 }
13768
13769 struct symbol *res = new_symbol (die, NULL, cu, storage);
13770 struct attribute *abstract_origin
13771 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13772 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13773 if (res == NULL && loc && abstract_origin)
13774 {
13775 /* We have a variable without a name, but with a location and an abstract
13776 origin. This may be a concrete instance of an abstract variable
13777 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13778 later. */
13779 struct dwarf2_cu *origin_cu = cu;
13780 struct die_info *origin_die
13781 = follow_die_ref (die, abstract_origin, &origin_cu);
13782 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13783 per_objfile->per_bfd->abstract_to_concrete
13784 [origin_die->sect_off].push_back (die->sect_off);
13785 }
13786 }
13787
13788 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13789 reading .debug_rnglists.
13790 Callback's type should be:
13791 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13792 Return true if the attributes are present and valid, otherwise,
13793 return false. */
13794
13795 template <typename Callback>
13796 static bool
13797 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13798 Callback &&callback)
13799 {
13800 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13801 struct objfile *objfile = dwarf2_per_objfile->objfile;
13802 bfd *obfd = objfile->obfd;
13803 /* Base address selection entry. */
13804 gdb::optional<CORE_ADDR> base;
13805 const gdb_byte *buffer;
13806 CORE_ADDR baseaddr;
13807 bool overflow = false;
13808
13809 base = cu->base_address;
13810
13811 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13812 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13813 {
13814 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13815 offset);
13816 return false;
13817 }
13818 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13819
13820 baseaddr = objfile->text_section_offset ();
13821
13822 while (1)
13823 {
13824 /* Initialize it due to a false compiler warning. */
13825 CORE_ADDR range_beginning = 0, range_end = 0;
13826 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13827 + dwarf2_per_objfile->per_bfd->rnglists.size);
13828 unsigned int bytes_read;
13829
13830 if (buffer == buf_end)
13831 {
13832 overflow = true;
13833 break;
13834 }
13835 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13836 switch (rlet)
13837 {
13838 case DW_RLE_end_of_list:
13839 break;
13840 case DW_RLE_base_address:
13841 if (buffer + cu->header.addr_size > buf_end)
13842 {
13843 overflow = true;
13844 break;
13845 }
13846 base = cu->header.read_address (obfd, buffer, &bytes_read);
13847 buffer += bytes_read;
13848 break;
13849 case DW_RLE_start_length:
13850 if (buffer + cu->header.addr_size > buf_end)
13851 {
13852 overflow = true;
13853 break;
13854 }
13855 range_beginning = cu->header.read_address (obfd, buffer,
13856 &bytes_read);
13857 buffer += bytes_read;
13858 range_end = (range_beginning
13859 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13860 buffer += bytes_read;
13861 if (buffer > buf_end)
13862 {
13863 overflow = true;
13864 break;
13865 }
13866 break;
13867 case DW_RLE_offset_pair:
13868 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13869 buffer += bytes_read;
13870 if (buffer > buf_end)
13871 {
13872 overflow = true;
13873 break;
13874 }
13875 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13876 buffer += bytes_read;
13877 if (buffer > buf_end)
13878 {
13879 overflow = true;
13880 break;
13881 }
13882 break;
13883 case DW_RLE_start_end:
13884 if (buffer + 2 * cu->header.addr_size > buf_end)
13885 {
13886 overflow = true;
13887 break;
13888 }
13889 range_beginning = cu->header.read_address (obfd, buffer,
13890 &bytes_read);
13891 buffer += bytes_read;
13892 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13893 buffer += bytes_read;
13894 break;
13895 default:
13896 complaint (_("Invalid .debug_rnglists data (no base address)"));
13897 return false;
13898 }
13899 if (rlet == DW_RLE_end_of_list || overflow)
13900 break;
13901 if (rlet == DW_RLE_base_address)
13902 continue;
13903
13904 if (!base.has_value ())
13905 {
13906 /* We have no valid base address for the ranges
13907 data. */
13908 complaint (_("Invalid .debug_rnglists data (no base address)"));
13909 return false;
13910 }
13911
13912 if (range_beginning > range_end)
13913 {
13914 /* Inverted range entries are invalid. */
13915 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13916 return false;
13917 }
13918
13919 /* Empty range entries have no effect. */
13920 if (range_beginning == range_end)
13921 continue;
13922
13923 range_beginning += *base;
13924 range_end += *base;
13925
13926 /* A not-uncommon case of bad debug info.
13927 Don't pollute the addrmap with bad data. */
13928 if (range_beginning + baseaddr == 0
13929 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13930 {
13931 complaint (_(".debug_rnglists entry has start address of zero"
13932 " [in module %s]"), objfile_name (objfile));
13933 continue;
13934 }
13935
13936 callback (range_beginning, range_end);
13937 }
13938
13939 if (overflow)
13940 {
13941 complaint (_("Offset %d is not terminated "
13942 "for DW_AT_ranges attribute"),
13943 offset);
13944 return false;
13945 }
13946
13947 return true;
13948 }
13949
13950 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13951 Callback's type should be:
13952 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13953 Return 1 if the attributes are present and valid, otherwise, return 0. */
13954
13955 template <typename Callback>
13956 static int
13957 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13958 Callback &&callback)
13959 {
13960 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13961 struct objfile *objfile = per_objfile->objfile;
13962 struct comp_unit_head *cu_header = &cu->header;
13963 bfd *obfd = objfile->obfd;
13964 unsigned int addr_size = cu_header->addr_size;
13965 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13966 /* Base address selection entry. */
13967 gdb::optional<CORE_ADDR> base;
13968 unsigned int dummy;
13969 const gdb_byte *buffer;
13970 CORE_ADDR baseaddr;
13971
13972 if (cu_header->version >= 5)
13973 return dwarf2_rnglists_process (offset, cu, callback);
13974
13975 base = cu->base_address;
13976
13977 per_objfile->per_bfd->ranges.read (objfile);
13978 if (offset >= per_objfile->per_bfd->ranges.size)
13979 {
13980 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13981 offset);
13982 return 0;
13983 }
13984 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13985
13986 baseaddr = objfile->text_section_offset ();
13987
13988 while (1)
13989 {
13990 CORE_ADDR range_beginning, range_end;
13991
13992 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13993 buffer += addr_size;
13994 range_end = cu->header.read_address (obfd, buffer, &dummy);
13995 buffer += addr_size;
13996 offset += 2 * addr_size;
13997
13998 /* An end of list marker is a pair of zero addresses. */
13999 if (range_beginning == 0 && range_end == 0)
14000 /* Found the end of list entry. */
14001 break;
14002
14003 /* Each base address selection entry is a pair of 2 values.
14004 The first is the largest possible address, the second is
14005 the base address. Check for a base address here. */
14006 if ((range_beginning & mask) == mask)
14007 {
14008 /* If we found the largest possible address, then we already
14009 have the base address in range_end. */
14010 base = range_end;
14011 continue;
14012 }
14013
14014 if (!base.has_value ())
14015 {
14016 /* We have no valid base address for the ranges
14017 data. */
14018 complaint (_("Invalid .debug_ranges data (no base address)"));
14019 return 0;
14020 }
14021
14022 if (range_beginning > range_end)
14023 {
14024 /* Inverted range entries are invalid. */
14025 complaint (_("Invalid .debug_ranges data (inverted range)"));
14026 return 0;
14027 }
14028
14029 /* Empty range entries have no effect. */
14030 if (range_beginning == range_end)
14031 continue;
14032
14033 range_beginning += *base;
14034 range_end += *base;
14035
14036 /* A not-uncommon case of bad debug info.
14037 Don't pollute the addrmap with bad data. */
14038 if (range_beginning + baseaddr == 0
14039 && !per_objfile->per_bfd->has_section_at_zero)
14040 {
14041 complaint (_(".debug_ranges entry has start address of zero"
14042 " [in module %s]"), objfile_name (objfile));
14043 continue;
14044 }
14045
14046 callback (range_beginning, range_end);
14047 }
14048
14049 return 1;
14050 }
14051
14052 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14053 Return 1 if the attributes are present and valid, otherwise, return 0.
14054 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14055
14056 static int
14057 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14058 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14059 dwarf2_psymtab *ranges_pst)
14060 {
14061 struct objfile *objfile = cu->per_objfile->objfile;
14062 struct gdbarch *gdbarch = objfile->arch ();
14063 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14064 int low_set = 0;
14065 CORE_ADDR low = 0;
14066 CORE_ADDR high = 0;
14067 int retval;
14068
14069 retval = dwarf2_ranges_process (offset, cu,
14070 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14071 {
14072 if (ranges_pst != NULL)
14073 {
14074 CORE_ADDR lowpc;
14075 CORE_ADDR highpc;
14076
14077 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14078 range_beginning + baseaddr)
14079 - baseaddr);
14080 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14081 range_end + baseaddr)
14082 - baseaddr);
14083 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14084 lowpc, highpc - 1, ranges_pst);
14085 }
14086
14087 /* FIXME: This is recording everything as a low-high
14088 segment of consecutive addresses. We should have a
14089 data structure for discontiguous block ranges
14090 instead. */
14091 if (! low_set)
14092 {
14093 low = range_beginning;
14094 high = range_end;
14095 low_set = 1;
14096 }
14097 else
14098 {
14099 if (range_beginning < low)
14100 low = range_beginning;
14101 if (range_end > high)
14102 high = range_end;
14103 }
14104 });
14105 if (!retval)
14106 return 0;
14107
14108 if (! low_set)
14109 /* If the first entry is an end-of-list marker, the range
14110 describes an empty scope, i.e. no instructions. */
14111 return 0;
14112
14113 if (low_return)
14114 *low_return = low;
14115 if (high_return)
14116 *high_return = high;
14117 return 1;
14118 }
14119
14120 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14121 definition for the return value. *LOWPC and *HIGHPC are set iff
14122 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14123
14124 static enum pc_bounds_kind
14125 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14126 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14127 dwarf2_psymtab *pst)
14128 {
14129 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14130 struct attribute *attr;
14131 struct attribute *attr_high;
14132 CORE_ADDR low = 0;
14133 CORE_ADDR high = 0;
14134 enum pc_bounds_kind ret;
14135
14136 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14137 if (attr_high)
14138 {
14139 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14140 if (attr != nullptr)
14141 {
14142 low = attr->value_as_address ();
14143 high = attr_high->value_as_address ();
14144 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14145 high += low;
14146 }
14147 else
14148 /* Found high w/o low attribute. */
14149 return PC_BOUNDS_INVALID;
14150
14151 /* Found consecutive range of addresses. */
14152 ret = PC_BOUNDS_HIGH_LOW;
14153 }
14154 else
14155 {
14156 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14157 if (attr != NULL)
14158 {
14159 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14160 We take advantage of the fact that DW_AT_ranges does not appear
14161 in DW_TAG_compile_unit of DWO files. */
14162 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14163 unsigned int ranges_offset = (DW_UNSND (attr)
14164 + (need_ranges_base
14165 ? cu->ranges_base
14166 : 0));
14167
14168 /* Value of the DW_AT_ranges attribute is the offset in the
14169 .debug_ranges section. */
14170 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14171 return PC_BOUNDS_INVALID;
14172 /* Found discontinuous range of addresses. */
14173 ret = PC_BOUNDS_RANGES;
14174 }
14175 else
14176 return PC_BOUNDS_NOT_PRESENT;
14177 }
14178
14179 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14180 if (high <= low)
14181 return PC_BOUNDS_INVALID;
14182
14183 /* When using the GNU linker, .gnu.linkonce. sections are used to
14184 eliminate duplicate copies of functions and vtables and such.
14185 The linker will arbitrarily choose one and discard the others.
14186 The AT_*_pc values for such functions refer to local labels in
14187 these sections. If the section from that file was discarded, the
14188 labels are not in the output, so the relocs get a value of 0.
14189 If this is a discarded function, mark the pc bounds as invalid,
14190 so that GDB will ignore it. */
14191 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14192 return PC_BOUNDS_INVALID;
14193
14194 *lowpc = low;
14195 if (highpc)
14196 *highpc = high;
14197 return ret;
14198 }
14199
14200 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14201 its low and high PC addresses. Do nothing if these addresses could not
14202 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14203 and HIGHPC to the high address if greater than HIGHPC. */
14204
14205 static void
14206 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14207 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14208 struct dwarf2_cu *cu)
14209 {
14210 CORE_ADDR low, high;
14211 struct die_info *child = die->child;
14212
14213 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14214 {
14215 *lowpc = std::min (*lowpc, low);
14216 *highpc = std::max (*highpc, high);
14217 }
14218
14219 /* If the language does not allow nested subprograms (either inside
14220 subprograms or lexical blocks), we're done. */
14221 if (cu->language != language_ada)
14222 return;
14223
14224 /* Check all the children of the given DIE. If it contains nested
14225 subprograms, then check their pc bounds. Likewise, we need to
14226 check lexical blocks as well, as they may also contain subprogram
14227 definitions. */
14228 while (child && child->tag)
14229 {
14230 if (child->tag == DW_TAG_subprogram
14231 || child->tag == DW_TAG_lexical_block)
14232 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14233 child = child->sibling;
14234 }
14235 }
14236
14237 /* Get the low and high pc's represented by the scope DIE, and store
14238 them in *LOWPC and *HIGHPC. If the correct values can't be
14239 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14240
14241 static void
14242 get_scope_pc_bounds (struct die_info *die,
14243 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14244 struct dwarf2_cu *cu)
14245 {
14246 CORE_ADDR best_low = (CORE_ADDR) -1;
14247 CORE_ADDR best_high = (CORE_ADDR) 0;
14248 CORE_ADDR current_low, current_high;
14249
14250 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14251 >= PC_BOUNDS_RANGES)
14252 {
14253 best_low = current_low;
14254 best_high = current_high;
14255 }
14256 else
14257 {
14258 struct die_info *child = die->child;
14259
14260 while (child && child->tag)
14261 {
14262 switch (child->tag) {
14263 case DW_TAG_subprogram:
14264 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14265 break;
14266 case DW_TAG_namespace:
14267 case DW_TAG_module:
14268 /* FIXME: carlton/2004-01-16: Should we do this for
14269 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14270 that current GCC's always emit the DIEs corresponding
14271 to definitions of methods of classes as children of a
14272 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14273 the DIEs giving the declarations, which could be
14274 anywhere). But I don't see any reason why the
14275 standards says that they have to be there. */
14276 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14277
14278 if (current_low != ((CORE_ADDR) -1))
14279 {
14280 best_low = std::min (best_low, current_low);
14281 best_high = std::max (best_high, current_high);
14282 }
14283 break;
14284 default:
14285 /* Ignore. */
14286 break;
14287 }
14288
14289 child = child->sibling;
14290 }
14291 }
14292
14293 *lowpc = best_low;
14294 *highpc = best_high;
14295 }
14296
14297 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14298 in DIE. */
14299
14300 static void
14301 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14302 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14303 {
14304 struct objfile *objfile = cu->per_objfile->objfile;
14305 struct gdbarch *gdbarch = objfile->arch ();
14306 struct attribute *attr;
14307 struct attribute *attr_high;
14308
14309 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14310 if (attr_high)
14311 {
14312 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14313 if (attr != nullptr)
14314 {
14315 CORE_ADDR low = attr->value_as_address ();
14316 CORE_ADDR high = attr_high->value_as_address ();
14317
14318 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14319 high += low;
14320
14321 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14322 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14323 cu->get_builder ()->record_block_range (block, low, high - 1);
14324 }
14325 }
14326
14327 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14328 if (attr != nullptr)
14329 {
14330 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14331 We take advantage of the fact that DW_AT_ranges does not appear
14332 in DW_TAG_compile_unit of DWO files. */
14333 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14334
14335 /* The value of the DW_AT_ranges attribute is the offset of the
14336 address range list in the .debug_ranges section. */
14337 unsigned long offset = (DW_UNSND (attr)
14338 + (need_ranges_base ? cu->ranges_base : 0));
14339
14340 std::vector<blockrange> blockvec;
14341 dwarf2_ranges_process (offset, cu,
14342 [&] (CORE_ADDR start, CORE_ADDR end)
14343 {
14344 start += baseaddr;
14345 end += baseaddr;
14346 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14347 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14348 cu->get_builder ()->record_block_range (block, start, end - 1);
14349 blockvec.emplace_back (start, end);
14350 });
14351
14352 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14353 }
14354 }
14355
14356 /* Check whether the producer field indicates either of GCC < 4.6, or the
14357 Intel C/C++ compiler, and cache the result in CU. */
14358
14359 static void
14360 check_producer (struct dwarf2_cu *cu)
14361 {
14362 int major, minor;
14363
14364 if (cu->producer == NULL)
14365 {
14366 /* For unknown compilers expect their behavior is DWARF version
14367 compliant.
14368
14369 GCC started to support .debug_types sections by -gdwarf-4 since
14370 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14371 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14372 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14373 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14374 }
14375 else if (producer_is_gcc (cu->producer, &major, &minor))
14376 {
14377 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14378 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14379 }
14380 else if (producer_is_icc (cu->producer, &major, &minor))
14381 {
14382 cu->producer_is_icc = true;
14383 cu->producer_is_icc_lt_14 = major < 14;
14384 }
14385 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14386 cu->producer_is_codewarrior = true;
14387 else
14388 {
14389 /* For other non-GCC compilers, expect their behavior is DWARF version
14390 compliant. */
14391 }
14392
14393 cu->checked_producer = true;
14394 }
14395
14396 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14397 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14398 during 4.6.0 experimental. */
14399
14400 static bool
14401 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14402 {
14403 if (!cu->checked_producer)
14404 check_producer (cu);
14405
14406 return cu->producer_is_gxx_lt_4_6;
14407 }
14408
14409
14410 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14411 with incorrect is_stmt attributes. */
14412
14413 static bool
14414 producer_is_codewarrior (struct dwarf2_cu *cu)
14415 {
14416 if (!cu->checked_producer)
14417 check_producer (cu);
14418
14419 return cu->producer_is_codewarrior;
14420 }
14421
14422 /* Return the default accessibility type if it is not overridden by
14423 DW_AT_accessibility. */
14424
14425 static enum dwarf_access_attribute
14426 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14427 {
14428 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14429 {
14430 /* The default DWARF 2 accessibility for members is public, the default
14431 accessibility for inheritance is private. */
14432
14433 if (die->tag != DW_TAG_inheritance)
14434 return DW_ACCESS_public;
14435 else
14436 return DW_ACCESS_private;
14437 }
14438 else
14439 {
14440 /* DWARF 3+ defines the default accessibility a different way. The same
14441 rules apply now for DW_TAG_inheritance as for the members and it only
14442 depends on the container kind. */
14443
14444 if (die->parent->tag == DW_TAG_class_type)
14445 return DW_ACCESS_private;
14446 else
14447 return DW_ACCESS_public;
14448 }
14449 }
14450
14451 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14452 offset. If the attribute was not found return 0, otherwise return
14453 1. If it was found but could not properly be handled, set *OFFSET
14454 to 0. */
14455
14456 static int
14457 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14458 LONGEST *offset)
14459 {
14460 struct attribute *attr;
14461
14462 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14463 if (attr != NULL)
14464 {
14465 *offset = 0;
14466
14467 /* Note that we do not check for a section offset first here.
14468 This is because DW_AT_data_member_location is new in DWARF 4,
14469 so if we see it, we can assume that a constant form is really
14470 a constant and not a section offset. */
14471 if (attr->form_is_constant ())
14472 *offset = attr->constant_value (0);
14473 else if (attr->form_is_section_offset ())
14474 dwarf2_complex_location_expr_complaint ();
14475 else if (attr->form_is_block ())
14476 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14477 else
14478 dwarf2_complex_location_expr_complaint ();
14479
14480 return 1;
14481 }
14482
14483 return 0;
14484 }
14485
14486 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14487
14488 static void
14489 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14490 struct field *field)
14491 {
14492 struct attribute *attr;
14493
14494 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14495 if (attr != NULL)
14496 {
14497 if (attr->form_is_constant ())
14498 {
14499 LONGEST offset = attr->constant_value (0);
14500 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14501 }
14502 else if (attr->form_is_section_offset ())
14503 dwarf2_complex_location_expr_complaint ();
14504 else if (attr->form_is_block ())
14505 {
14506 bool handled;
14507 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14508 if (handled)
14509 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14510 else
14511 {
14512 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14513 struct objfile *objfile = per_objfile->objfile;
14514 struct dwarf2_locexpr_baton *dlbaton
14515 = XOBNEW (&objfile->objfile_obstack,
14516 struct dwarf2_locexpr_baton);
14517 dlbaton->data = DW_BLOCK (attr)->data;
14518 dlbaton->size = DW_BLOCK (attr)->size;
14519 /* When using this baton, we want to compute the address
14520 of the field, not the value. This is why
14521 is_reference is set to false here. */
14522 dlbaton->is_reference = false;
14523 dlbaton->per_objfile = per_objfile;
14524 dlbaton->per_cu = cu->per_cu;
14525
14526 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14527 }
14528 }
14529 else
14530 dwarf2_complex_location_expr_complaint ();
14531 }
14532 }
14533
14534 /* Add an aggregate field to the field list. */
14535
14536 static void
14537 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14538 struct dwarf2_cu *cu)
14539 {
14540 struct objfile *objfile = cu->per_objfile->objfile;
14541 struct gdbarch *gdbarch = objfile->arch ();
14542 struct nextfield *new_field;
14543 struct attribute *attr;
14544 struct field *fp;
14545 const char *fieldname = "";
14546
14547 if (die->tag == DW_TAG_inheritance)
14548 {
14549 fip->baseclasses.emplace_back ();
14550 new_field = &fip->baseclasses.back ();
14551 }
14552 else
14553 {
14554 fip->fields.emplace_back ();
14555 new_field = &fip->fields.back ();
14556 }
14557
14558 new_field->offset = die->sect_off;
14559
14560 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14561 if (attr != nullptr)
14562 new_field->accessibility = DW_UNSND (attr);
14563 else
14564 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14565 if (new_field->accessibility != DW_ACCESS_public)
14566 fip->non_public_fields = 1;
14567
14568 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14569 if (attr != nullptr)
14570 new_field->virtuality = DW_UNSND (attr);
14571 else
14572 new_field->virtuality = DW_VIRTUALITY_none;
14573
14574 fp = &new_field->field;
14575
14576 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14577 {
14578 /* Data member other than a C++ static data member. */
14579
14580 /* Get type of field. */
14581 fp->type = die_type (die, cu);
14582
14583 SET_FIELD_BITPOS (*fp, 0);
14584
14585 /* Get bit size of field (zero if none). */
14586 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14587 if (attr != nullptr)
14588 {
14589 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14590 }
14591 else
14592 {
14593 FIELD_BITSIZE (*fp) = 0;
14594 }
14595
14596 /* Get bit offset of field. */
14597 handle_data_member_location (die, cu, fp);
14598 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14599 if (attr != nullptr)
14600 {
14601 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14602 {
14603 /* For big endian bits, the DW_AT_bit_offset gives the
14604 additional bit offset from the MSB of the containing
14605 anonymous object to the MSB of the field. We don't
14606 have to do anything special since we don't need to
14607 know the size of the anonymous object. */
14608 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14609 }
14610 else
14611 {
14612 /* For little endian bits, compute the bit offset to the
14613 MSB of the anonymous object, subtract off the number of
14614 bits from the MSB of the field to the MSB of the
14615 object, and then subtract off the number of bits of
14616 the field itself. The result is the bit offset of
14617 the LSB of the field. */
14618 int anonymous_size;
14619 int bit_offset = DW_UNSND (attr);
14620
14621 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14622 if (attr != nullptr)
14623 {
14624 /* The size of the anonymous object containing
14625 the bit field is explicit, so use the
14626 indicated size (in bytes). */
14627 anonymous_size = DW_UNSND (attr);
14628 }
14629 else
14630 {
14631 /* The size of the anonymous object containing
14632 the bit field must be inferred from the type
14633 attribute of the data member containing the
14634 bit field. */
14635 anonymous_size = TYPE_LENGTH (fp->type);
14636 }
14637 SET_FIELD_BITPOS (*fp,
14638 (FIELD_BITPOS (*fp)
14639 + anonymous_size * bits_per_byte
14640 - bit_offset - FIELD_BITSIZE (*fp)));
14641 }
14642 }
14643 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14644 if (attr != NULL)
14645 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14646 + attr->constant_value (0)));
14647
14648 /* Get name of field. */
14649 fieldname = dwarf2_name (die, cu);
14650 if (fieldname == NULL)
14651 fieldname = "";
14652
14653 /* The name is already allocated along with this objfile, so we don't
14654 need to duplicate it for the type. */
14655 fp->name = fieldname;
14656
14657 /* Change accessibility for artificial fields (e.g. virtual table
14658 pointer or virtual base class pointer) to private. */
14659 if (dwarf2_attr (die, DW_AT_artificial, cu))
14660 {
14661 FIELD_ARTIFICIAL (*fp) = 1;
14662 new_field->accessibility = DW_ACCESS_private;
14663 fip->non_public_fields = 1;
14664 }
14665 }
14666 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14667 {
14668 /* C++ static member. */
14669
14670 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14671 is a declaration, but all versions of G++ as of this writing
14672 (so through at least 3.2.1) incorrectly generate
14673 DW_TAG_variable tags. */
14674
14675 const char *physname;
14676
14677 /* Get name of field. */
14678 fieldname = dwarf2_name (die, cu);
14679 if (fieldname == NULL)
14680 return;
14681
14682 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14683 if (attr
14684 /* Only create a symbol if this is an external value.
14685 new_symbol checks this and puts the value in the global symbol
14686 table, which we want. If it is not external, new_symbol
14687 will try to put the value in cu->list_in_scope which is wrong. */
14688 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14689 {
14690 /* A static const member, not much different than an enum as far as
14691 we're concerned, except that we can support more types. */
14692 new_symbol (die, NULL, cu);
14693 }
14694
14695 /* Get physical name. */
14696 physname = dwarf2_physname (fieldname, die, cu);
14697
14698 /* The name is already allocated along with this objfile, so we don't
14699 need to duplicate it for the type. */
14700 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14701 FIELD_TYPE (*fp) = die_type (die, cu);
14702 FIELD_NAME (*fp) = fieldname;
14703 }
14704 else if (die->tag == DW_TAG_inheritance)
14705 {
14706 /* C++ base class field. */
14707 handle_data_member_location (die, cu, fp);
14708 FIELD_BITSIZE (*fp) = 0;
14709 FIELD_TYPE (*fp) = die_type (die, cu);
14710 FIELD_NAME (*fp) = fp->type->name ();
14711 }
14712 else
14713 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14714 }
14715
14716 /* Can the type given by DIE define another type? */
14717
14718 static bool
14719 type_can_define_types (const struct die_info *die)
14720 {
14721 switch (die->tag)
14722 {
14723 case DW_TAG_typedef:
14724 case DW_TAG_class_type:
14725 case DW_TAG_structure_type:
14726 case DW_TAG_union_type:
14727 case DW_TAG_enumeration_type:
14728 return true;
14729
14730 default:
14731 return false;
14732 }
14733 }
14734
14735 /* Add a type definition defined in the scope of the FIP's class. */
14736
14737 static void
14738 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14739 struct dwarf2_cu *cu)
14740 {
14741 struct decl_field fp;
14742 memset (&fp, 0, sizeof (fp));
14743
14744 gdb_assert (type_can_define_types (die));
14745
14746 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14747 fp.name = dwarf2_name (die, cu);
14748 fp.type = read_type_die (die, cu);
14749
14750 /* Save accessibility. */
14751 enum dwarf_access_attribute accessibility;
14752 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14753 if (attr != NULL)
14754 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14755 else
14756 accessibility = dwarf2_default_access_attribute (die, cu);
14757 switch (accessibility)
14758 {
14759 case DW_ACCESS_public:
14760 /* The assumed value if neither private nor protected. */
14761 break;
14762 case DW_ACCESS_private:
14763 fp.is_private = 1;
14764 break;
14765 case DW_ACCESS_protected:
14766 fp.is_protected = 1;
14767 break;
14768 default:
14769 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14770 }
14771
14772 if (die->tag == DW_TAG_typedef)
14773 fip->typedef_field_list.push_back (fp);
14774 else
14775 fip->nested_types_list.push_back (fp);
14776 }
14777
14778 /* A convenience typedef that's used when finding the discriminant
14779 field for a variant part. */
14780 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14781 offset_map_type;
14782
14783 /* Compute the discriminant range for a given variant. OBSTACK is
14784 where the results will be stored. VARIANT is the variant to
14785 process. IS_UNSIGNED indicates whether the discriminant is signed
14786 or unsigned. */
14787
14788 static const gdb::array_view<discriminant_range>
14789 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14790 bool is_unsigned)
14791 {
14792 std::vector<discriminant_range> ranges;
14793
14794 if (variant.default_branch)
14795 return {};
14796
14797 if (variant.discr_list_data == nullptr)
14798 {
14799 discriminant_range r
14800 = {variant.discriminant_value, variant.discriminant_value};
14801 ranges.push_back (r);
14802 }
14803 else
14804 {
14805 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14806 variant.discr_list_data->size);
14807 while (!data.empty ())
14808 {
14809 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14810 {
14811 complaint (_("invalid discriminant marker: %d"), data[0]);
14812 break;
14813 }
14814 bool is_range = data[0] == DW_DSC_range;
14815 data = data.slice (1);
14816
14817 ULONGEST low, high;
14818 unsigned int bytes_read;
14819
14820 if (data.empty ())
14821 {
14822 complaint (_("DW_AT_discr_list missing low value"));
14823 break;
14824 }
14825 if (is_unsigned)
14826 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14827 else
14828 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14829 &bytes_read);
14830 data = data.slice (bytes_read);
14831
14832 if (is_range)
14833 {
14834 if (data.empty ())
14835 {
14836 complaint (_("DW_AT_discr_list missing high value"));
14837 break;
14838 }
14839 if (is_unsigned)
14840 high = read_unsigned_leb128 (nullptr, data.data (),
14841 &bytes_read);
14842 else
14843 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14844 &bytes_read);
14845 data = data.slice (bytes_read);
14846 }
14847 else
14848 high = low;
14849
14850 ranges.push_back ({ low, high });
14851 }
14852 }
14853
14854 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14855 ranges.size ());
14856 std::copy (ranges.begin (), ranges.end (), result);
14857 return gdb::array_view<discriminant_range> (result, ranges.size ());
14858 }
14859
14860 static const gdb::array_view<variant_part> create_variant_parts
14861 (struct obstack *obstack,
14862 const offset_map_type &offset_map,
14863 struct field_info *fi,
14864 const std::vector<variant_part_builder> &variant_parts);
14865
14866 /* Fill in a "struct variant" for a given variant field. RESULT is
14867 the variant to fill in. OBSTACK is where any needed allocations
14868 will be done. OFFSET_MAP holds the mapping from section offsets to
14869 fields for the type. FI describes the fields of the type we're
14870 processing. FIELD is the variant field we're converting. */
14871
14872 static void
14873 create_one_variant (variant &result, struct obstack *obstack,
14874 const offset_map_type &offset_map,
14875 struct field_info *fi, const variant_field &field)
14876 {
14877 result.discriminants = convert_variant_range (obstack, field, false);
14878 result.first_field = field.first_field + fi->baseclasses.size ();
14879 result.last_field = field.last_field + fi->baseclasses.size ();
14880 result.parts = create_variant_parts (obstack, offset_map, fi,
14881 field.variant_parts);
14882 }
14883
14884 /* Fill in a "struct variant_part" for a given variant part. RESULT
14885 is the variant part to fill in. OBSTACK is where any needed
14886 allocations will be done. OFFSET_MAP holds the mapping from
14887 section offsets to fields for the type. FI describes the fields of
14888 the type we're processing. BUILDER is the variant part to be
14889 converted. */
14890
14891 static void
14892 create_one_variant_part (variant_part &result,
14893 struct obstack *obstack,
14894 const offset_map_type &offset_map,
14895 struct field_info *fi,
14896 const variant_part_builder &builder)
14897 {
14898 auto iter = offset_map.find (builder.discriminant_offset);
14899 if (iter == offset_map.end ())
14900 {
14901 result.discriminant_index = -1;
14902 /* Doesn't matter. */
14903 result.is_unsigned = false;
14904 }
14905 else
14906 {
14907 result.discriminant_index = iter->second;
14908 result.is_unsigned
14909 = TYPE_UNSIGNED (FIELD_TYPE
14910 (fi->fields[result.discriminant_index].field));
14911 }
14912
14913 size_t n = builder.variants.size ();
14914 variant *output = new (obstack) variant[n];
14915 for (size_t i = 0; i < n; ++i)
14916 create_one_variant (output[i], obstack, offset_map, fi,
14917 builder.variants[i]);
14918
14919 result.variants = gdb::array_view<variant> (output, n);
14920 }
14921
14922 /* Create a vector of variant parts that can be attached to a type.
14923 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14924 holds the mapping from section offsets to fields for the type. FI
14925 describes the fields of the type we're processing. VARIANT_PARTS
14926 is the vector to convert. */
14927
14928 static const gdb::array_view<variant_part>
14929 create_variant_parts (struct obstack *obstack,
14930 const offset_map_type &offset_map,
14931 struct field_info *fi,
14932 const std::vector<variant_part_builder> &variant_parts)
14933 {
14934 if (variant_parts.empty ())
14935 return {};
14936
14937 size_t n = variant_parts.size ();
14938 variant_part *result = new (obstack) variant_part[n];
14939 for (size_t i = 0; i < n; ++i)
14940 create_one_variant_part (result[i], obstack, offset_map, fi,
14941 variant_parts[i]);
14942
14943 return gdb::array_view<variant_part> (result, n);
14944 }
14945
14946 /* Compute the variant part vector for FIP, attaching it to TYPE when
14947 done. */
14948
14949 static void
14950 add_variant_property (struct field_info *fip, struct type *type,
14951 struct dwarf2_cu *cu)
14952 {
14953 /* Map section offsets of fields to their field index. Note the
14954 field index here does not take the number of baseclasses into
14955 account. */
14956 offset_map_type offset_map;
14957 for (int i = 0; i < fip->fields.size (); ++i)
14958 offset_map[fip->fields[i].offset] = i;
14959
14960 struct objfile *objfile = cu->per_objfile->objfile;
14961 gdb::array_view<variant_part> parts
14962 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14963 fip->variant_parts);
14964
14965 struct dynamic_prop prop;
14966 prop.kind = PROP_VARIANT_PARTS;
14967 prop.data.variant_parts
14968 = ((gdb::array_view<variant_part> *)
14969 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14970
14971 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14972 }
14973
14974 /* Create the vector of fields, and attach it to the type. */
14975
14976 static void
14977 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14978 struct dwarf2_cu *cu)
14979 {
14980 int nfields = fip->nfields ();
14981
14982 /* Record the field count, allocate space for the array of fields,
14983 and create blank accessibility bitfields if necessary. */
14984 type->set_num_fields (nfields);
14985 type->set_fields
14986 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14987
14988 if (fip->non_public_fields && cu->language != language_ada)
14989 {
14990 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14991
14992 TYPE_FIELD_PRIVATE_BITS (type) =
14993 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14994 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14995
14996 TYPE_FIELD_PROTECTED_BITS (type) =
14997 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14998 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14999
15000 TYPE_FIELD_IGNORE_BITS (type) =
15001 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15002 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15003 }
15004
15005 /* If the type has baseclasses, allocate and clear a bit vector for
15006 TYPE_FIELD_VIRTUAL_BITS. */
15007 if (!fip->baseclasses.empty () && cu->language != language_ada)
15008 {
15009 int num_bytes = B_BYTES (fip->baseclasses.size ());
15010 unsigned char *pointer;
15011
15012 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15013 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15014 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15015 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15016 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15017 }
15018
15019 if (!fip->variant_parts.empty ())
15020 add_variant_property (fip, type, cu);
15021
15022 /* Copy the saved-up fields into the field vector. */
15023 for (int i = 0; i < nfields; ++i)
15024 {
15025 struct nextfield &field
15026 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15027 : fip->fields[i - fip->baseclasses.size ()]);
15028
15029 type->field (i) = field.field;
15030 switch (field.accessibility)
15031 {
15032 case DW_ACCESS_private:
15033 if (cu->language != language_ada)
15034 SET_TYPE_FIELD_PRIVATE (type, i);
15035 break;
15036
15037 case DW_ACCESS_protected:
15038 if (cu->language != language_ada)
15039 SET_TYPE_FIELD_PROTECTED (type, i);
15040 break;
15041
15042 case DW_ACCESS_public:
15043 break;
15044
15045 default:
15046 /* Unknown accessibility. Complain and treat it as public. */
15047 {
15048 complaint (_("unsupported accessibility %d"),
15049 field.accessibility);
15050 }
15051 break;
15052 }
15053 if (i < fip->baseclasses.size ())
15054 {
15055 switch (field.virtuality)
15056 {
15057 case DW_VIRTUALITY_virtual:
15058 case DW_VIRTUALITY_pure_virtual:
15059 if (cu->language == language_ada)
15060 error (_("unexpected virtuality in component of Ada type"));
15061 SET_TYPE_FIELD_VIRTUAL (type, i);
15062 break;
15063 }
15064 }
15065 }
15066 }
15067
15068 /* Return true if this member function is a constructor, false
15069 otherwise. */
15070
15071 static int
15072 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15073 {
15074 const char *fieldname;
15075 const char *type_name;
15076 int len;
15077
15078 if (die->parent == NULL)
15079 return 0;
15080
15081 if (die->parent->tag != DW_TAG_structure_type
15082 && die->parent->tag != DW_TAG_union_type
15083 && die->parent->tag != DW_TAG_class_type)
15084 return 0;
15085
15086 fieldname = dwarf2_name (die, cu);
15087 type_name = dwarf2_name (die->parent, cu);
15088 if (fieldname == NULL || type_name == NULL)
15089 return 0;
15090
15091 len = strlen (fieldname);
15092 return (strncmp (fieldname, type_name, len) == 0
15093 && (type_name[len] == '\0' || type_name[len] == '<'));
15094 }
15095
15096 /* Check if the given VALUE is a recognized enum
15097 dwarf_defaulted_attribute constant according to DWARF5 spec,
15098 Table 7.24. */
15099
15100 static bool
15101 is_valid_DW_AT_defaulted (ULONGEST value)
15102 {
15103 switch (value)
15104 {
15105 case DW_DEFAULTED_no:
15106 case DW_DEFAULTED_in_class:
15107 case DW_DEFAULTED_out_of_class:
15108 return true;
15109 }
15110
15111 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15112 return false;
15113 }
15114
15115 /* Add a member function to the proper fieldlist. */
15116
15117 static void
15118 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15119 struct type *type, struct dwarf2_cu *cu)
15120 {
15121 struct objfile *objfile = cu->per_objfile->objfile;
15122 struct attribute *attr;
15123 int i;
15124 struct fnfieldlist *flp = nullptr;
15125 struct fn_field *fnp;
15126 const char *fieldname;
15127 struct type *this_type;
15128 enum dwarf_access_attribute accessibility;
15129
15130 if (cu->language == language_ada)
15131 error (_("unexpected member function in Ada type"));
15132
15133 /* Get name of member function. */
15134 fieldname = dwarf2_name (die, cu);
15135 if (fieldname == NULL)
15136 return;
15137
15138 /* Look up member function name in fieldlist. */
15139 for (i = 0; i < fip->fnfieldlists.size (); i++)
15140 {
15141 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15142 {
15143 flp = &fip->fnfieldlists[i];
15144 break;
15145 }
15146 }
15147
15148 /* Create a new fnfieldlist if necessary. */
15149 if (flp == nullptr)
15150 {
15151 fip->fnfieldlists.emplace_back ();
15152 flp = &fip->fnfieldlists.back ();
15153 flp->name = fieldname;
15154 i = fip->fnfieldlists.size () - 1;
15155 }
15156
15157 /* Create a new member function field and add it to the vector of
15158 fnfieldlists. */
15159 flp->fnfields.emplace_back ();
15160 fnp = &flp->fnfields.back ();
15161
15162 /* Delay processing of the physname until later. */
15163 if (cu->language == language_cplus)
15164 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15165 die, cu);
15166 else
15167 {
15168 const char *physname = dwarf2_physname (fieldname, die, cu);
15169 fnp->physname = physname ? physname : "";
15170 }
15171
15172 fnp->type = alloc_type (objfile);
15173 this_type = read_type_die (die, cu);
15174 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15175 {
15176 int nparams = this_type->num_fields ();
15177
15178 /* TYPE is the domain of this method, and THIS_TYPE is the type
15179 of the method itself (TYPE_CODE_METHOD). */
15180 smash_to_method_type (fnp->type, type,
15181 TYPE_TARGET_TYPE (this_type),
15182 this_type->fields (),
15183 this_type->num_fields (),
15184 TYPE_VARARGS (this_type));
15185
15186 /* Handle static member functions.
15187 Dwarf2 has no clean way to discern C++ static and non-static
15188 member functions. G++ helps GDB by marking the first
15189 parameter for non-static member functions (which is the this
15190 pointer) as artificial. We obtain this information from
15191 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15192 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15193 fnp->voffset = VOFFSET_STATIC;
15194 }
15195 else
15196 complaint (_("member function type missing for '%s'"),
15197 dwarf2_full_name (fieldname, die, cu));
15198
15199 /* Get fcontext from DW_AT_containing_type if present. */
15200 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15201 fnp->fcontext = die_containing_type (die, cu);
15202
15203 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15204 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15205
15206 /* Get accessibility. */
15207 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15208 if (attr != nullptr)
15209 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15210 else
15211 accessibility = dwarf2_default_access_attribute (die, cu);
15212 switch (accessibility)
15213 {
15214 case DW_ACCESS_private:
15215 fnp->is_private = 1;
15216 break;
15217 case DW_ACCESS_protected:
15218 fnp->is_protected = 1;
15219 break;
15220 }
15221
15222 /* Check for artificial methods. */
15223 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15224 if (attr && DW_UNSND (attr) != 0)
15225 fnp->is_artificial = 1;
15226
15227 /* Check for defaulted methods. */
15228 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15229 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15230 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15231
15232 /* Check for deleted methods. */
15233 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15234 if (attr != nullptr && DW_UNSND (attr) != 0)
15235 fnp->is_deleted = 1;
15236
15237 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15238
15239 /* Get index in virtual function table if it is a virtual member
15240 function. For older versions of GCC, this is an offset in the
15241 appropriate virtual table, as specified by DW_AT_containing_type.
15242 For everyone else, it is an expression to be evaluated relative
15243 to the object address. */
15244
15245 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15246 if (attr != nullptr)
15247 {
15248 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15249 {
15250 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15251 {
15252 /* Old-style GCC. */
15253 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15254 }
15255 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15256 || (DW_BLOCK (attr)->size > 1
15257 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15258 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15259 {
15260 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15261 if ((fnp->voffset % cu->header.addr_size) != 0)
15262 dwarf2_complex_location_expr_complaint ();
15263 else
15264 fnp->voffset /= cu->header.addr_size;
15265 fnp->voffset += 2;
15266 }
15267 else
15268 dwarf2_complex_location_expr_complaint ();
15269
15270 if (!fnp->fcontext)
15271 {
15272 /* If there is no `this' field and no DW_AT_containing_type,
15273 we cannot actually find a base class context for the
15274 vtable! */
15275 if (this_type->num_fields () == 0
15276 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15277 {
15278 complaint (_("cannot determine context for virtual member "
15279 "function \"%s\" (offset %s)"),
15280 fieldname, sect_offset_str (die->sect_off));
15281 }
15282 else
15283 {
15284 fnp->fcontext
15285 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15286 }
15287 }
15288 }
15289 else if (attr->form_is_section_offset ())
15290 {
15291 dwarf2_complex_location_expr_complaint ();
15292 }
15293 else
15294 {
15295 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15296 fieldname);
15297 }
15298 }
15299 else
15300 {
15301 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15302 if (attr && DW_UNSND (attr))
15303 {
15304 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15305 complaint (_("Member function \"%s\" (offset %s) is virtual "
15306 "but the vtable offset is not specified"),
15307 fieldname, sect_offset_str (die->sect_off));
15308 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15309 TYPE_CPLUS_DYNAMIC (type) = 1;
15310 }
15311 }
15312 }
15313
15314 /* Create the vector of member function fields, and attach it to the type. */
15315
15316 static void
15317 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15318 struct dwarf2_cu *cu)
15319 {
15320 if (cu->language == language_ada)
15321 error (_("unexpected member functions in Ada type"));
15322
15323 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15324 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15325 TYPE_ALLOC (type,
15326 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15327
15328 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15329 {
15330 struct fnfieldlist &nf = fip->fnfieldlists[i];
15331 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15332
15333 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15334 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15335 fn_flp->fn_fields = (struct fn_field *)
15336 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15337
15338 for (int k = 0; k < nf.fnfields.size (); ++k)
15339 fn_flp->fn_fields[k] = nf.fnfields[k];
15340 }
15341
15342 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15343 }
15344
15345 /* Returns non-zero if NAME is the name of a vtable member in CU's
15346 language, zero otherwise. */
15347 static int
15348 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15349 {
15350 static const char vptr[] = "_vptr";
15351
15352 /* Look for the C++ form of the vtable. */
15353 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15354 return 1;
15355
15356 return 0;
15357 }
15358
15359 /* GCC outputs unnamed structures that are really pointers to member
15360 functions, with the ABI-specified layout. If TYPE describes
15361 such a structure, smash it into a member function type.
15362
15363 GCC shouldn't do this; it should just output pointer to member DIEs.
15364 This is GCC PR debug/28767. */
15365
15366 static void
15367 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15368 {
15369 struct type *pfn_type, *self_type, *new_type;
15370
15371 /* Check for a structure with no name and two children. */
15372 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15373 return;
15374
15375 /* Check for __pfn and __delta members. */
15376 if (TYPE_FIELD_NAME (type, 0) == NULL
15377 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15378 || TYPE_FIELD_NAME (type, 1) == NULL
15379 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15380 return;
15381
15382 /* Find the type of the method. */
15383 pfn_type = TYPE_FIELD_TYPE (type, 0);
15384 if (pfn_type == NULL
15385 || pfn_type->code () != TYPE_CODE_PTR
15386 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15387 return;
15388
15389 /* Look for the "this" argument. */
15390 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15391 if (pfn_type->num_fields () == 0
15392 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15393 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15394 return;
15395
15396 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15397 new_type = alloc_type (objfile);
15398 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15399 pfn_type->fields (), pfn_type->num_fields (),
15400 TYPE_VARARGS (pfn_type));
15401 smash_to_methodptr_type (type, new_type);
15402 }
15403
15404 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15405 appropriate error checking and issuing complaints if there is a
15406 problem. */
15407
15408 static ULONGEST
15409 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15410 {
15411 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15412
15413 if (attr == nullptr)
15414 return 0;
15415
15416 if (!attr->form_is_constant ())
15417 {
15418 complaint (_("DW_AT_alignment must have constant form"
15419 " - DIE at %s [in module %s]"),
15420 sect_offset_str (die->sect_off),
15421 objfile_name (cu->per_objfile->objfile));
15422 return 0;
15423 }
15424
15425 ULONGEST align;
15426 if (attr->form == DW_FORM_sdata)
15427 {
15428 LONGEST val = DW_SND (attr);
15429 if (val < 0)
15430 {
15431 complaint (_("DW_AT_alignment value must not be negative"
15432 " - DIE at %s [in module %s]"),
15433 sect_offset_str (die->sect_off),
15434 objfile_name (cu->per_objfile->objfile));
15435 return 0;
15436 }
15437 align = val;
15438 }
15439 else
15440 align = DW_UNSND (attr);
15441
15442 if (align == 0)
15443 {
15444 complaint (_("DW_AT_alignment value must not be zero"
15445 " - DIE at %s [in module %s]"),
15446 sect_offset_str (die->sect_off),
15447 objfile_name (cu->per_objfile->objfile));
15448 return 0;
15449 }
15450 if ((align & (align - 1)) != 0)
15451 {
15452 complaint (_("DW_AT_alignment value must be a power of 2"
15453 " - DIE at %s [in module %s]"),
15454 sect_offset_str (die->sect_off),
15455 objfile_name (cu->per_objfile->objfile));
15456 return 0;
15457 }
15458
15459 return align;
15460 }
15461
15462 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15463 the alignment for TYPE. */
15464
15465 static void
15466 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15467 struct type *type)
15468 {
15469 if (!set_type_align (type, get_alignment (cu, die)))
15470 complaint (_("DW_AT_alignment value too large"
15471 " - DIE at %s [in module %s]"),
15472 sect_offset_str (die->sect_off),
15473 objfile_name (cu->per_objfile->objfile));
15474 }
15475
15476 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15477 constant for a type, according to DWARF5 spec, Table 5.5. */
15478
15479 static bool
15480 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15481 {
15482 switch (value)
15483 {
15484 case DW_CC_normal:
15485 case DW_CC_pass_by_reference:
15486 case DW_CC_pass_by_value:
15487 return true;
15488
15489 default:
15490 complaint (_("unrecognized DW_AT_calling_convention value "
15491 "(%s) for a type"), pulongest (value));
15492 return false;
15493 }
15494 }
15495
15496 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15497 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15498 also according to GNU-specific values (see include/dwarf2.h). */
15499
15500 static bool
15501 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15502 {
15503 switch (value)
15504 {
15505 case DW_CC_normal:
15506 case DW_CC_program:
15507 case DW_CC_nocall:
15508 return true;
15509
15510 case DW_CC_GNU_renesas_sh:
15511 case DW_CC_GNU_borland_fastcall_i386:
15512 case DW_CC_GDB_IBM_OpenCL:
15513 return true;
15514
15515 default:
15516 complaint (_("unrecognized DW_AT_calling_convention value "
15517 "(%s) for a subroutine"), pulongest (value));
15518 return false;
15519 }
15520 }
15521
15522 /* Called when we find the DIE that starts a structure or union scope
15523 (definition) to create a type for the structure or union. Fill in
15524 the type's name and general properties; the members will not be
15525 processed until process_structure_scope. A symbol table entry for
15526 the type will also not be done until process_structure_scope (assuming
15527 the type has a name).
15528
15529 NOTE: we need to call these functions regardless of whether or not the
15530 DIE has a DW_AT_name attribute, since it might be an anonymous
15531 structure or union. This gets the type entered into our set of
15532 user defined types. */
15533
15534 static struct type *
15535 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15536 {
15537 struct objfile *objfile = cu->per_objfile->objfile;
15538 struct type *type;
15539 struct attribute *attr;
15540 const char *name;
15541
15542 /* If the definition of this type lives in .debug_types, read that type.
15543 Don't follow DW_AT_specification though, that will take us back up
15544 the chain and we want to go down. */
15545 attr = die->attr (DW_AT_signature);
15546 if (attr != nullptr)
15547 {
15548 type = get_DW_AT_signature_type (die, attr, cu);
15549
15550 /* The type's CU may not be the same as CU.
15551 Ensure TYPE is recorded with CU in die_type_hash. */
15552 return set_die_type (die, type, cu);
15553 }
15554
15555 type = alloc_type (objfile);
15556 INIT_CPLUS_SPECIFIC (type);
15557
15558 name = dwarf2_name (die, cu);
15559 if (name != NULL)
15560 {
15561 if (cu->language == language_cplus
15562 || cu->language == language_d
15563 || cu->language == language_rust)
15564 {
15565 const char *full_name = dwarf2_full_name (name, die, cu);
15566
15567 /* dwarf2_full_name might have already finished building the DIE's
15568 type. If so, there is no need to continue. */
15569 if (get_die_type (die, cu) != NULL)
15570 return get_die_type (die, cu);
15571
15572 type->set_name (full_name);
15573 }
15574 else
15575 {
15576 /* The name is already allocated along with this objfile, so
15577 we don't need to duplicate it for the type. */
15578 type->set_name (name);
15579 }
15580 }
15581
15582 if (die->tag == DW_TAG_structure_type)
15583 {
15584 type->set_code (TYPE_CODE_STRUCT);
15585 }
15586 else if (die->tag == DW_TAG_union_type)
15587 {
15588 type->set_code (TYPE_CODE_UNION);
15589 }
15590 else
15591 {
15592 type->set_code (TYPE_CODE_STRUCT);
15593 }
15594
15595 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15596 TYPE_DECLARED_CLASS (type) = 1;
15597
15598 /* Store the calling convention in the type if it's available in
15599 the die. Otherwise the calling convention remains set to
15600 the default value DW_CC_normal. */
15601 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15602 if (attr != nullptr
15603 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15604 {
15605 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15606 TYPE_CPLUS_CALLING_CONVENTION (type)
15607 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15608 }
15609
15610 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15611 if (attr != nullptr)
15612 {
15613 if (attr->form_is_constant ())
15614 TYPE_LENGTH (type) = DW_UNSND (attr);
15615 else
15616 {
15617 struct dynamic_prop prop;
15618 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15619 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15620 TYPE_LENGTH (type) = 0;
15621 }
15622 }
15623 else
15624 {
15625 TYPE_LENGTH (type) = 0;
15626 }
15627
15628 maybe_set_alignment (cu, die, type);
15629
15630 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15631 {
15632 /* ICC<14 does not output the required DW_AT_declaration on
15633 incomplete types, but gives them a size of zero. */
15634 TYPE_STUB (type) = 1;
15635 }
15636 else
15637 TYPE_STUB_SUPPORTED (type) = 1;
15638
15639 if (die_is_declaration (die, cu))
15640 TYPE_STUB (type) = 1;
15641 else if (attr == NULL && die->child == NULL
15642 && producer_is_realview (cu->producer))
15643 /* RealView does not output the required DW_AT_declaration
15644 on incomplete types. */
15645 TYPE_STUB (type) = 1;
15646
15647 /* We need to add the type field to the die immediately so we don't
15648 infinitely recurse when dealing with pointers to the structure
15649 type within the structure itself. */
15650 set_die_type (die, type, cu);
15651
15652 /* set_die_type should be already done. */
15653 set_descriptive_type (type, die, cu);
15654
15655 return type;
15656 }
15657
15658 static void handle_struct_member_die
15659 (struct die_info *child_die,
15660 struct type *type,
15661 struct field_info *fi,
15662 std::vector<struct symbol *> *template_args,
15663 struct dwarf2_cu *cu);
15664
15665 /* A helper for handle_struct_member_die that handles
15666 DW_TAG_variant_part. */
15667
15668 static void
15669 handle_variant_part (struct die_info *die, struct type *type,
15670 struct field_info *fi,
15671 std::vector<struct symbol *> *template_args,
15672 struct dwarf2_cu *cu)
15673 {
15674 variant_part_builder *new_part;
15675 if (fi->current_variant_part == nullptr)
15676 {
15677 fi->variant_parts.emplace_back ();
15678 new_part = &fi->variant_parts.back ();
15679 }
15680 else if (!fi->current_variant_part->processing_variant)
15681 {
15682 complaint (_("nested DW_TAG_variant_part seen "
15683 "- DIE at %s [in module %s]"),
15684 sect_offset_str (die->sect_off),
15685 objfile_name (cu->per_objfile->objfile));
15686 return;
15687 }
15688 else
15689 {
15690 variant_field &current = fi->current_variant_part->variants.back ();
15691 current.variant_parts.emplace_back ();
15692 new_part = &current.variant_parts.back ();
15693 }
15694
15695 /* When we recurse, we want callees to add to this new variant
15696 part. */
15697 scoped_restore save_current_variant_part
15698 = make_scoped_restore (&fi->current_variant_part, new_part);
15699
15700 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15701 if (discr == NULL)
15702 {
15703 /* It's a univariant form, an extension we support. */
15704 }
15705 else if (discr->form_is_ref ())
15706 {
15707 struct dwarf2_cu *target_cu = cu;
15708 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15709
15710 new_part->discriminant_offset = target_die->sect_off;
15711 }
15712 else
15713 {
15714 complaint (_("DW_AT_discr does not have DIE reference form"
15715 " - DIE at %s [in module %s]"),
15716 sect_offset_str (die->sect_off),
15717 objfile_name (cu->per_objfile->objfile));
15718 }
15719
15720 for (die_info *child_die = die->child;
15721 child_die != NULL;
15722 child_die = child_die->sibling)
15723 handle_struct_member_die (child_die, type, fi, template_args, cu);
15724 }
15725
15726 /* A helper for handle_struct_member_die that handles
15727 DW_TAG_variant. */
15728
15729 static void
15730 handle_variant (struct die_info *die, struct type *type,
15731 struct field_info *fi,
15732 std::vector<struct symbol *> *template_args,
15733 struct dwarf2_cu *cu)
15734 {
15735 if (fi->current_variant_part == nullptr)
15736 {
15737 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15738 "- DIE at %s [in module %s]"),
15739 sect_offset_str (die->sect_off),
15740 objfile_name (cu->per_objfile->objfile));
15741 return;
15742 }
15743 if (fi->current_variant_part->processing_variant)
15744 {
15745 complaint (_("nested DW_TAG_variant seen "
15746 "- DIE at %s [in module %s]"),
15747 sect_offset_str (die->sect_off),
15748 objfile_name (cu->per_objfile->objfile));
15749 return;
15750 }
15751
15752 scoped_restore save_processing_variant
15753 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15754 true);
15755
15756 fi->current_variant_part->variants.emplace_back ();
15757 variant_field &variant = fi->current_variant_part->variants.back ();
15758 variant.first_field = fi->fields.size ();
15759
15760 /* In a variant we want to get the discriminant and also add a
15761 field for our sole member child. */
15762 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15763 if (discr == nullptr)
15764 {
15765 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15766 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15767 variant.default_branch = true;
15768 else
15769 variant.discr_list_data = DW_BLOCK (discr);
15770 }
15771 else
15772 variant.discriminant_value = DW_UNSND (discr);
15773
15774 for (die_info *variant_child = die->child;
15775 variant_child != NULL;
15776 variant_child = variant_child->sibling)
15777 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15778
15779 variant.last_field = fi->fields.size ();
15780 }
15781
15782 /* A helper for process_structure_scope that handles a single member
15783 DIE. */
15784
15785 static void
15786 handle_struct_member_die (struct die_info *child_die, struct type *type,
15787 struct field_info *fi,
15788 std::vector<struct symbol *> *template_args,
15789 struct dwarf2_cu *cu)
15790 {
15791 if (child_die->tag == DW_TAG_member
15792 || child_die->tag == DW_TAG_variable)
15793 {
15794 /* NOTE: carlton/2002-11-05: A C++ static data member
15795 should be a DW_TAG_member that is a declaration, but
15796 all versions of G++ as of this writing (so through at
15797 least 3.2.1) incorrectly generate DW_TAG_variable
15798 tags for them instead. */
15799 dwarf2_add_field (fi, child_die, cu);
15800 }
15801 else if (child_die->tag == DW_TAG_subprogram)
15802 {
15803 /* Rust doesn't have member functions in the C++ sense.
15804 However, it does emit ordinary functions as children
15805 of a struct DIE. */
15806 if (cu->language == language_rust)
15807 read_func_scope (child_die, cu);
15808 else
15809 {
15810 /* C++ member function. */
15811 dwarf2_add_member_fn (fi, child_die, type, cu);
15812 }
15813 }
15814 else if (child_die->tag == DW_TAG_inheritance)
15815 {
15816 /* C++ base class field. */
15817 dwarf2_add_field (fi, child_die, cu);
15818 }
15819 else if (type_can_define_types (child_die))
15820 dwarf2_add_type_defn (fi, child_die, cu);
15821 else if (child_die->tag == DW_TAG_template_type_param
15822 || child_die->tag == DW_TAG_template_value_param)
15823 {
15824 struct symbol *arg = new_symbol (child_die, NULL, cu);
15825
15826 if (arg != NULL)
15827 template_args->push_back (arg);
15828 }
15829 else if (child_die->tag == DW_TAG_variant_part)
15830 handle_variant_part (child_die, type, fi, template_args, cu);
15831 else if (child_die->tag == DW_TAG_variant)
15832 handle_variant (child_die, type, fi, template_args, cu);
15833 }
15834
15835 /* Finish creating a structure or union type, including filling in
15836 its members and creating a symbol for it. */
15837
15838 static void
15839 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15840 {
15841 struct objfile *objfile = cu->per_objfile->objfile;
15842 struct die_info *child_die;
15843 struct type *type;
15844
15845 type = get_die_type (die, cu);
15846 if (type == NULL)
15847 type = read_structure_type (die, cu);
15848
15849 bool has_template_parameters = false;
15850 if (die->child != NULL && ! die_is_declaration (die, cu))
15851 {
15852 struct field_info fi;
15853 std::vector<struct symbol *> template_args;
15854
15855 child_die = die->child;
15856
15857 while (child_die && child_die->tag)
15858 {
15859 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15860 child_die = child_die->sibling;
15861 }
15862
15863 /* Attach template arguments to type. */
15864 if (!template_args.empty ())
15865 {
15866 has_template_parameters = true;
15867 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15868 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15869 TYPE_TEMPLATE_ARGUMENTS (type)
15870 = XOBNEWVEC (&objfile->objfile_obstack,
15871 struct symbol *,
15872 TYPE_N_TEMPLATE_ARGUMENTS (type));
15873 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15874 template_args.data (),
15875 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15876 * sizeof (struct symbol *)));
15877 }
15878
15879 /* Attach fields and member functions to the type. */
15880 if (fi.nfields () > 0)
15881 dwarf2_attach_fields_to_type (&fi, type, cu);
15882 if (!fi.fnfieldlists.empty ())
15883 {
15884 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15885
15886 /* Get the type which refers to the base class (possibly this
15887 class itself) which contains the vtable pointer for the current
15888 class from the DW_AT_containing_type attribute. This use of
15889 DW_AT_containing_type is a GNU extension. */
15890
15891 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15892 {
15893 struct type *t = die_containing_type (die, cu);
15894
15895 set_type_vptr_basetype (type, t);
15896 if (type == t)
15897 {
15898 int i;
15899
15900 /* Our own class provides vtbl ptr. */
15901 for (i = t->num_fields () - 1;
15902 i >= TYPE_N_BASECLASSES (t);
15903 --i)
15904 {
15905 const char *fieldname = TYPE_FIELD_NAME (t, i);
15906
15907 if (is_vtable_name (fieldname, cu))
15908 {
15909 set_type_vptr_fieldno (type, i);
15910 break;
15911 }
15912 }
15913
15914 /* Complain if virtual function table field not found. */
15915 if (i < TYPE_N_BASECLASSES (t))
15916 complaint (_("virtual function table pointer "
15917 "not found when defining class '%s'"),
15918 type->name () ? type->name () : "");
15919 }
15920 else
15921 {
15922 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15923 }
15924 }
15925 else if (cu->producer
15926 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15927 {
15928 /* The IBM XLC compiler does not provide direct indication
15929 of the containing type, but the vtable pointer is
15930 always named __vfp. */
15931
15932 int i;
15933
15934 for (i = type->num_fields () - 1;
15935 i >= TYPE_N_BASECLASSES (type);
15936 --i)
15937 {
15938 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15939 {
15940 set_type_vptr_fieldno (type, i);
15941 set_type_vptr_basetype (type, type);
15942 break;
15943 }
15944 }
15945 }
15946 }
15947
15948 /* Copy fi.typedef_field_list linked list elements content into the
15949 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15950 if (!fi.typedef_field_list.empty ())
15951 {
15952 int count = fi.typedef_field_list.size ();
15953
15954 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15955 TYPE_TYPEDEF_FIELD_ARRAY (type)
15956 = ((struct decl_field *)
15957 TYPE_ALLOC (type,
15958 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15959 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15960
15961 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15962 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15963 }
15964
15965 /* Copy fi.nested_types_list linked list elements content into the
15966 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15967 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15968 {
15969 int count = fi.nested_types_list.size ();
15970
15971 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15972 TYPE_NESTED_TYPES_ARRAY (type)
15973 = ((struct decl_field *)
15974 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15975 TYPE_NESTED_TYPES_COUNT (type) = count;
15976
15977 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15978 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15979 }
15980 }
15981
15982 quirk_gcc_member_function_pointer (type, objfile);
15983 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15984 cu->rust_unions.push_back (type);
15985
15986 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15987 snapshots) has been known to create a die giving a declaration
15988 for a class that has, as a child, a die giving a definition for a
15989 nested class. So we have to process our children even if the
15990 current die is a declaration. Normally, of course, a declaration
15991 won't have any children at all. */
15992
15993 child_die = die->child;
15994
15995 while (child_die != NULL && child_die->tag)
15996 {
15997 if (child_die->tag == DW_TAG_member
15998 || child_die->tag == DW_TAG_variable
15999 || child_die->tag == DW_TAG_inheritance
16000 || child_die->tag == DW_TAG_template_value_param
16001 || child_die->tag == DW_TAG_template_type_param)
16002 {
16003 /* Do nothing. */
16004 }
16005 else
16006 process_die (child_die, cu);
16007
16008 child_die = child_die->sibling;
16009 }
16010
16011 /* Do not consider external references. According to the DWARF standard,
16012 these DIEs are identified by the fact that they have no byte_size
16013 attribute, and a declaration attribute. */
16014 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16015 || !die_is_declaration (die, cu)
16016 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16017 {
16018 struct symbol *sym = new_symbol (die, type, cu);
16019
16020 if (has_template_parameters)
16021 {
16022 struct symtab *symtab;
16023 if (sym != nullptr)
16024 symtab = symbol_symtab (sym);
16025 else if (cu->line_header != nullptr)
16026 {
16027 /* Any related symtab will do. */
16028 symtab
16029 = cu->line_header->file_names ()[0].symtab;
16030 }
16031 else
16032 {
16033 symtab = nullptr;
16034 complaint (_("could not find suitable "
16035 "symtab for template parameter"
16036 " - DIE at %s [in module %s]"),
16037 sect_offset_str (die->sect_off),
16038 objfile_name (objfile));
16039 }
16040
16041 if (symtab != nullptr)
16042 {
16043 /* Make sure that the symtab is set on the new symbols.
16044 Even though they don't appear in this symtab directly,
16045 other parts of gdb assume that symbols do, and this is
16046 reasonably true. */
16047 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16048 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16049 }
16050 }
16051 }
16052 }
16053
16054 /* Assuming DIE is an enumeration type, and TYPE is its associated
16055 type, update TYPE using some information only available in DIE's
16056 children. In particular, the fields are computed. */
16057
16058 static void
16059 update_enumeration_type_from_children (struct die_info *die,
16060 struct type *type,
16061 struct dwarf2_cu *cu)
16062 {
16063 struct die_info *child_die;
16064 int unsigned_enum = 1;
16065 int flag_enum = 1;
16066
16067 auto_obstack obstack;
16068 std::vector<struct field> fields;
16069
16070 for (child_die = die->child;
16071 child_die != NULL && child_die->tag;
16072 child_die = child_die->sibling)
16073 {
16074 struct attribute *attr;
16075 LONGEST value;
16076 const gdb_byte *bytes;
16077 struct dwarf2_locexpr_baton *baton;
16078 const char *name;
16079
16080 if (child_die->tag != DW_TAG_enumerator)
16081 continue;
16082
16083 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16084 if (attr == NULL)
16085 continue;
16086
16087 name = dwarf2_name (child_die, cu);
16088 if (name == NULL)
16089 name = "<anonymous enumerator>";
16090
16091 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16092 &value, &bytes, &baton);
16093 if (value < 0)
16094 {
16095 unsigned_enum = 0;
16096 flag_enum = 0;
16097 }
16098 else
16099 {
16100 if (count_one_bits_ll (value) >= 2)
16101 flag_enum = 0;
16102 }
16103
16104 fields.emplace_back ();
16105 struct field &field = fields.back ();
16106 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16107 SET_FIELD_ENUMVAL (field, value);
16108 }
16109
16110 if (!fields.empty ())
16111 {
16112 type->set_num_fields (fields.size ());
16113 type->set_fields
16114 ((struct field *)
16115 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16116 memcpy (type->fields (), fields.data (),
16117 sizeof (struct field) * fields.size ());
16118 }
16119
16120 if (unsigned_enum)
16121 TYPE_UNSIGNED (type) = 1;
16122 if (flag_enum)
16123 TYPE_FLAG_ENUM (type) = 1;
16124 }
16125
16126 /* Given a DW_AT_enumeration_type die, set its type. We do not
16127 complete the type's fields yet, or create any symbols. */
16128
16129 static struct type *
16130 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16131 {
16132 struct objfile *objfile = cu->per_objfile->objfile;
16133 struct type *type;
16134 struct attribute *attr;
16135 const char *name;
16136
16137 /* If the definition of this type lives in .debug_types, read that type.
16138 Don't follow DW_AT_specification though, that will take us back up
16139 the chain and we want to go down. */
16140 attr = die->attr (DW_AT_signature);
16141 if (attr != nullptr)
16142 {
16143 type = get_DW_AT_signature_type (die, attr, cu);
16144
16145 /* The type's CU may not be the same as CU.
16146 Ensure TYPE is recorded with CU in die_type_hash. */
16147 return set_die_type (die, type, cu);
16148 }
16149
16150 type = alloc_type (objfile);
16151
16152 type->set_code (TYPE_CODE_ENUM);
16153 name = dwarf2_full_name (NULL, die, cu);
16154 if (name != NULL)
16155 type->set_name (name);
16156
16157 attr = dwarf2_attr (die, DW_AT_type, cu);
16158 if (attr != NULL)
16159 {
16160 struct type *underlying_type = die_type (die, cu);
16161
16162 TYPE_TARGET_TYPE (type) = underlying_type;
16163 }
16164
16165 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16166 if (attr != nullptr)
16167 {
16168 TYPE_LENGTH (type) = DW_UNSND (attr);
16169 }
16170 else
16171 {
16172 TYPE_LENGTH (type) = 0;
16173 }
16174
16175 maybe_set_alignment (cu, die, type);
16176
16177 /* The enumeration DIE can be incomplete. In Ada, any type can be
16178 declared as private in the package spec, and then defined only
16179 inside the package body. Such types are known as Taft Amendment
16180 Types. When another package uses such a type, an incomplete DIE
16181 may be generated by the compiler. */
16182 if (die_is_declaration (die, cu))
16183 TYPE_STUB (type) = 1;
16184
16185 /* If this type has an underlying type that is not a stub, then we
16186 may use its attributes. We always use the "unsigned" attribute
16187 in this situation, because ordinarily we guess whether the type
16188 is unsigned -- but the guess can be wrong and the underlying type
16189 can tell us the reality. However, we defer to a local size
16190 attribute if one exists, because this lets the compiler override
16191 the underlying type if needed. */
16192 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16193 {
16194 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16195 underlying_type = check_typedef (underlying_type);
16196 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16197 if (TYPE_LENGTH (type) == 0)
16198 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16199 if (TYPE_RAW_ALIGN (type) == 0
16200 && TYPE_RAW_ALIGN (underlying_type) != 0)
16201 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16202 }
16203
16204 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16205
16206 set_die_type (die, type, cu);
16207
16208 /* Finish the creation of this type by using the enum's children.
16209 Note that, as usual, this must come after set_die_type to avoid
16210 infinite recursion when trying to compute the names of the
16211 enumerators. */
16212 update_enumeration_type_from_children (die, type, cu);
16213
16214 return type;
16215 }
16216
16217 /* Given a pointer to a die which begins an enumeration, process all
16218 the dies that define the members of the enumeration, and create the
16219 symbol for the enumeration type.
16220
16221 NOTE: We reverse the order of the element list. */
16222
16223 static void
16224 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16225 {
16226 struct type *this_type;
16227
16228 this_type = get_die_type (die, cu);
16229 if (this_type == NULL)
16230 this_type = read_enumeration_type (die, cu);
16231
16232 if (die->child != NULL)
16233 {
16234 struct die_info *child_die;
16235 const char *name;
16236
16237 child_die = die->child;
16238 while (child_die && child_die->tag)
16239 {
16240 if (child_die->tag != DW_TAG_enumerator)
16241 {
16242 process_die (child_die, cu);
16243 }
16244 else
16245 {
16246 name = dwarf2_name (child_die, cu);
16247 if (name)
16248 new_symbol (child_die, this_type, cu);
16249 }
16250
16251 child_die = child_die->sibling;
16252 }
16253 }
16254
16255 /* If we are reading an enum from a .debug_types unit, and the enum
16256 is a declaration, and the enum is not the signatured type in the
16257 unit, then we do not want to add a symbol for it. Adding a
16258 symbol would in some cases obscure the true definition of the
16259 enum, giving users an incomplete type when the definition is
16260 actually available. Note that we do not want to do this for all
16261 enums which are just declarations, because C++0x allows forward
16262 enum declarations. */
16263 if (cu->per_cu->is_debug_types
16264 && die_is_declaration (die, cu))
16265 {
16266 struct signatured_type *sig_type;
16267
16268 sig_type = (struct signatured_type *) cu->per_cu;
16269 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16270 if (sig_type->type_offset_in_section != die->sect_off)
16271 return;
16272 }
16273
16274 new_symbol (die, this_type, cu);
16275 }
16276
16277 /* Extract all information from a DW_TAG_array_type DIE and put it in
16278 the DIE's type field. For now, this only handles one dimensional
16279 arrays. */
16280
16281 static struct type *
16282 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16283 {
16284 struct objfile *objfile = cu->per_objfile->objfile;
16285 struct die_info *child_die;
16286 struct type *type;
16287 struct type *element_type, *range_type, *index_type;
16288 struct attribute *attr;
16289 const char *name;
16290 struct dynamic_prop *byte_stride_prop = NULL;
16291 unsigned int bit_stride = 0;
16292
16293 element_type = die_type (die, cu);
16294
16295 /* The die_type call above may have already set the type for this DIE. */
16296 type = get_die_type (die, cu);
16297 if (type)
16298 return type;
16299
16300 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16301 if (attr != NULL)
16302 {
16303 int stride_ok;
16304 struct type *prop_type = cu->addr_sized_int_type (false);
16305
16306 byte_stride_prop
16307 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16308 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16309 prop_type);
16310 if (!stride_ok)
16311 {
16312 complaint (_("unable to read array DW_AT_byte_stride "
16313 " - DIE at %s [in module %s]"),
16314 sect_offset_str (die->sect_off),
16315 objfile_name (cu->per_objfile->objfile));
16316 /* Ignore this attribute. We will likely not be able to print
16317 arrays of this type correctly, but there is little we can do
16318 to help if we cannot read the attribute's value. */
16319 byte_stride_prop = NULL;
16320 }
16321 }
16322
16323 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16324 if (attr != NULL)
16325 bit_stride = DW_UNSND (attr);
16326
16327 /* Irix 6.2 native cc creates array types without children for
16328 arrays with unspecified length. */
16329 if (die->child == NULL)
16330 {
16331 index_type = objfile_type (objfile)->builtin_int;
16332 range_type = create_static_range_type (NULL, index_type, 0, -1);
16333 type = create_array_type_with_stride (NULL, element_type, range_type,
16334 byte_stride_prop, bit_stride);
16335 return set_die_type (die, type, cu);
16336 }
16337
16338 std::vector<struct type *> range_types;
16339 child_die = die->child;
16340 while (child_die && child_die->tag)
16341 {
16342 if (child_die->tag == DW_TAG_subrange_type)
16343 {
16344 struct type *child_type = read_type_die (child_die, cu);
16345
16346 if (child_type != NULL)
16347 {
16348 /* The range type was succesfully read. Save it for the
16349 array type creation. */
16350 range_types.push_back (child_type);
16351 }
16352 }
16353 child_die = child_die->sibling;
16354 }
16355
16356 /* Dwarf2 dimensions are output from left to right, create the
16357 necessary array types in backwards order. */
16358
16359 type = element_type;
16360
16361 if (read_array_order (die, cu) == DW_ORD_col_major)
16362 {
16363 int i = 0;
16364
16365 while (i < range_types.size ())
16366 type = create_array_type_with_stride (NULL, type, range_types[i++],
16367 byte_stride_prop, bit_stride);
16368 }
16369 else
16370 {
16371 size_t ndim = range_types.size ();
16372 while (ndim-- > 0)
16373 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16374 byte_stride_prop, bit_stride);
16375 }
16376
16377 /* Understand Dwarf2 support for vector types (like they occur on
16378 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16379 array type. This is not part of the Dwarf2/3 standard yet, but a
16380 custom vendor extension. The main difference between a regular
16381 array and the vector variant is that vectors are passed by value
16382 to functions. */
16383 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16384 if (attr != nullptr)
16385 make_vector_type (type);
16386
16387 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16388 implementation may choose to implement triple vectors using this
16389 attribute. */
16390 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16391 if (attr != nullptr)
16392 {
16393 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16394 TYPE_LENGTH (type) = DW_UNSND (attr);
16395 else
16396 complaint (_("DW_AT_byte_size for array type smaller "
16397 "than the total size of elements"));
16398 }
16399
16400 name = dwarf2_name (die, cu);
16401 if (name)
16402 type->set_name (name);
16403
16404 maybe_set_alignment (cu, die, type);
16405
16406 /* Install the type in the die. */
16407 set_die_type (die, type, cu);
16408
16409 /* set_die_type should be already done. */
16410 set_descriptive_type (type, die, cu);
16411
16412 return type;
16413 }
16414
16415 static enum dwarf_array_dim_ordering
16416 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16417 {
16418 struct attribute *attr;
16419
16420 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16421
16422 if (attr != nullptr)
16423 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16424
16425 /* GNU F77 is a special case, as at 08/2004 array type info is the
16426 opposite order to the dwarf2 specification, but data is still
16427 laid out as per normal fortran.
16428
16429 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16430 version checking. */
16431
16432 if (cu->language == language_fortran
16433 && cu->producer && strstr (cu->producer, "GNU F77"))
16434 {
16435 return DW_ORD_row_major;
16436 }
16437
16438 switch (cu->language_defn->la_array_ordering)
16439 {
16440 case array_column_major:
16441 return DW_ORD_col_major;
16442 case array_row_major:
16443 default:
16444 return DW_ORD_row_major;
16445 };
16446 }
16447
16448 /* Extract all information from a DW_TAG_set_type DIE and put it in
16449 the DIE's type field. */
16450
16451 static struct type *
16452 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16453 {
16454 struct type *domain_type, *set_type;
16455 struct attribute *attr;
16456
16457 domain_type = die_type (die, cu);
16458
16459 /* The die_type call above may have already set the type for this DIE. */
16460 set_type = get_die_type (die, cu);
16461 if (set_type)
16462 return set_type;
16463
16464 set_type = create_set_type (NULL, domain_type);
16465
16466 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16467 if (attr != nullptr)
16468 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16469
16470 maybe_set_alignment (cu, die, set_type);
16471
16472 return set_die_type (die, set_type, cu);
16473 }
16474
16475 /* A helper for read_common_block that creates a locexpr baton.
16476 SYM is the symbol which we are marking as computed.
16477 COMMON_DIE is the DIE for the common block.
16478 COMMON_LOC is the location expression attribute for the common
16479 block itself.
16480 MEMBER_LOC is the location expression attribute for the particular
16481 member of the common block that we are processing.
16482 CU is the CU from which the above come. */
16483
16484 static void
16485 mark_common_block_symbol_computed (struct symbol *sym,
16486 struct die_info *common_die,
16487 struct attribute *common_loc,
16488 struct attribute *member_loc,
16489 struct dwarf2_cu *cu)
16490 {
16491 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16492 struct objfile *objfile = per_objfile->objfile;
16493 struct dwarf2_locexpr_baton *baton;
16494 gdb_byte *ptr;
16495 unsigned int cu_off;
16496 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16497 LONGEST offset = 0;
16498
16499 gdb_assert (common_loc && member_loc);
16500 gdb_assert (common_loc->form_is_block ());
16501 gdb_assert (member_loc->form_is_block ()
16502 || member_loc->form_is_constant ());
16503
16504 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16505 baton->per_objfile = per_objfile;
16506 baton->per_cu = cu->per_cu;
16507 gdb_assert (baton->per_cu);
16508
16509 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16510
16511 if (member_loc->form_is_constant ())
16512 {
16513 offset = member_loc->constant_value (0);
16514 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16515 }
16516 else
16517 baton->size += DW_BLOCK (member_loc)->size;
16518
16519 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16520 baton->data = ptr;
16521
16522 *ptr++ = DW_OP_call4;
16523 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16524 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16525 ptr += 4;
16526
16527 if (member_loc->form_is_constant ())
16528 {
16529 *ptr++ = DW_OP_addr;
16530 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16531 ptr += cu->header.addr_size;
16532 }
16533 else
16534 {
16535 /* We have to copy the data here, because DW_OP_call4 will only
16536 use a DW_AT_location attribute. */
16537 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16538 ptr += DW_BLOCK (member_loc)->size;
16539 }
16540
16541 *ptr++ = DW_OP_plus;
16542 gdb_assert (ptr - baton->data == baton->size);
16543
16544 SYMBOL_LOCATION_BATON (sym) = baton;
16545 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16546 }
16547
16548 /* Create appropriate locally-scoped variables for all the
16549 DW_TAG_common_block entries. Also create a struct common_block
16550 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16551 is used to separate the common blocks name namespace from regular
16552 variable names. */
16553
16554 static void
16555 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16556 {
16557 struct attribute *attr;
16558
16559 attr = dwarf2_attr (die, DW_AT_location, cu);
16560 if (attr != nullptr)
16561 {
16562 /* Support the .debug_loc offsets. */
16563 if (attr->form_is_block ())
16564 {
16565 /* Ok. */
16566 }
16567 else if (attr->form_is_section_offset ())
16568 {
16569 dwarf2_complex_location_expr_complaint ();
16570 attr = NULL;
16571 }
16572 else
16573 {
16574 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16575 "common block member");
16576 attr = NULL;
16577 }
16578 }
16579
16580 if (die->child != NULL)
16581 {
16582 struct objfile *objfile = cu->per_objfile->objfile;
16583 struct die_info *child_die;
16584 size_t n_entries = 0, size;
16585 struct common_block *common_block;
16586 struct symbol *sym;
16587
16588 for (child_die = die->child;
16589 child_die && child_die->tag;
16590 child_die = child_die->sibling)
16591 ++n_entries;
16592
16593 size = (sizeof (struct common_block)
16594 + (n_entries - 1) * sizeof (struct symbol *));
16595 common_block
16596 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16597 size);
16598 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16599 common_block->n_entries = 0;
16600
16601 for (child_die = die->child;
16602 child_die && child_die->tag;
16603 child_die = child_die->sibling)
16604 {
16605 /* Create the symbol in the DW_TAG_common_block block in the current
16606 symbol scope. */
16607 sym = new_symbol (child_die, NULL, cu);
16608 if (sym != NULL)
16609 {
16610 struct attribute *member_loc;
16611
16612 common_block->contents[common_block->n_entries++] = sym;
16613
16614 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16615 cu);
16616 if (member_loc)
16617 {
16618 /* GDB has handled this for a long time, but it is
16619 not specified by DWARF. It seems to have been
16620 emitted by gfortran at least as recently as:
16621 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16622 complaint (_("Variable in common block has "
16623 "DW_AT_data_member_location "
16624 "- DIE at %s [in module %s]"),
16625 sect_offset_str (child_die->sect_off),
16626 objfile_name (objfile));
16627
16628 if (member_loc->form_is_section_offset ())
16629 dwarf2_complex_location_expr_complaint ();
16630 else if (member_loc->form_is_constant ()
16631 || member_loc->form_is_block ())
16632 {
16633 if (attr != nullptr)
16634 mark_common_block_symbol_computed (sym, die, attr,
16635 member_loc, cu);
16636 }
16637 else
16638 dwarf2_complex_location_expr_complaint ();
16639 }
16640 }
16641 }
16642
16643 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16644 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16645 }
16646 }
16647
16648 /* Create a type for a C++ namespace. */
16649
16650 static struct type *
16651 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16652 {
16653 struct objfile *objfile = cu->per_objfile->objfile;
16654 const char *previous_prefix, *name;
16655 int is_anonymous;
16656 struct type *type;
16657
16658 /* For extensions, reuse the type of the original namespace. */
16659 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16660 {
16661 struct die_info *ext_die;
16662 struct dwarf2_cu *ext_cu = cu;
16663
16664 ext_die = dwarf2_extension (die, &ext_cu);
16665 type = read_type_die (ext_die, ext_cu);
16666
16667 /* EXT_CU may not be the same as CU.
16668 Ensure TYPE is recorded with CU in die_type_hash. */
16669 return set_die_type (die, type, cu);
16670 }
16671
16672 name = namespace_name (die, &is_anonymous, cu);
16673
16674 /* Now build the name of the current namespace. */
16675
16676 previous_prefix = determine_prefix (die, cu);
16677 if (previous_prefix[0] != '\0')
16678 name = typename_concat (&objfile->objfile_obstack,
16679 previous_prefix, name, 0, cu);
16680
16681 /* Create the type. */
16682 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16683
16684 return set_die_type (die, type, cu);
16685 }
16686
16687 /* Read a namespace scope. */
16688
16689 static void
16690 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16691 {
16692 struct objfile *objfile = cu->per_objfile->objfile;
16693 int is_anonymous;
16694
16695 /* Add a symbol associated to this if we haven't seen the namespace
16696 before. Also, add a using directive if it's an anonymous
16697 namespace. */
16698
16699 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16700 {
16701 struct type *type;
16702
16703 type = read_type_die (die, cu);
16704 new_symbol (die, type, cu);
16705
16706 namespace_name (die, &is_anonymous, cu);
16707 if (is_anonymous)
16708 {
16709 const char *previous_prefix = determine_prefix (die, cu);
16710
16711 std::vector<const char *> excludes;
16712 add_using_directive (using_directives (cu),
16713 previous_prefix, type->name (), NULL,
16714 NULL, excludes, 0, &objfile->objfile_obstack);
16715 }
16716 }
16717
16718 if (die->child != NULL)
16719 {
16720 struct die_info *child_die = die->child;
16721
16722 while (child_die && child_die->tag)
16723 {
16724 process_die (child_die, cu);
16725 child_die = child_die->sibling;
16726 }
16727 }
16728 }
16729
16730 /* Read a Fortran module as type. This DIE can be only a declaration used for
16731 imported module. Still we need that type as local Fortran "use ... only"
16732 declaration imports depend on the created type in determine_prefix. */
16733
16734 static struct type *
16735 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16736 {
16737 struct objfile *objfile = cu->per_objfile->objfile;
16738 const char *module_name;
16739 struct type *type;
16740
16741 module_name = dwarf2_name (die, cu);
16742 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16743
16744 return set_die_type (die, type, cu);
16745 }
16746
16747 /* Read a Fortran module. */
16748
16749 static void
16750 read_module (struct die_info *die, struct dwarf2_cu *cu)
16751 {
16752 struct die_info *child_die = die->child;
16753 struct type *type;
16754
16755 type = read_type_die (die, cu);
16756 new_symbol (die, type, cu);
16757
16758 while (child_die && child_die->tag)
16759 {
16760 process_die (child_die, cu);
16761 child_die = child_die->sibling;
16762 }
16763 }
16764
16765 /* Return the name of the namespace represented by DIE. Set
16766 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16767 namespace. */
16768
16769 static const char *
16770 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16771 {
16772 struct die_info *current_die;
16773 const char *name = NULL;
16774
16775 /* Loop through the extensions until we find a name. */
16776
16777 for (current_die = die;
16778 current_die != NULL;
16779 current_die = dwarf2_extension (die, &cu))
16780 {
16781 /* We don't use dwarf2_name here so that we can detect the absence
16782 of a name -> anonymous namespace. */
16783 name = dwarf2_string_attr (die, DW_AT_name, cu);
16784
16785 if (name != NULL)
16786 break;
16787 }
16788
16789 /* Is it an anonymous namespace? */
16790
16791 *is_anonymous = (name == NULL);
16792 if (*is_anonymous)
16793 name = CP_ANONYMOUS_NAMESPACE_STR;
16794
16795 return name;
16796 }
16797
16798 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16799 the user defined type vector. */
16800
16801 static struct type *
16802 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16803 {
16804 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16805 struct comp_unit_head *cu_header = &cu->header;
16806 struct type *type;
16807 struct attribute *attr_byte_size;
16808 struct attribute *attr_address_class;
16809 int byte_size, addr_class;
16810 struct type *target_type;
16811
16812 target_type = die_type (die, cu);
16813
16814 /* The die_type call above may have already set the type for this DIE. */
16815 type = get_die_type (die, cu);
16816 if (type)
16817 return type;
16818
16819 type = lookup_pointer_type (target_type);
16820
16821 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16822 if (attr_byte_size)
16823 byte_size = DW_UNSND (attr_byte_size);
16824 else
16825 byte_size = cu_header->addr_size;
16826
16827 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16828 if (attr_address_class)
16829 addr_class = DW_UNSND (attr_address_class);
16830 else
16831 addr_class = DW_ADDR_none;
16832
16833 ULONGEST alignment = get_alignment (cu, die);
16834
16835 /* If the pointer size, alignment, or address class is different
16836 than the default, create a type variant marked as such and set
16837 the length accordingly. */
16838 if (TYPE_LENGTH (type) != byte_size
16839 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16840 && alignment != TYPE_RAW_ALIGN (type))
16841 || addr_class != DW_ADDR_none)
16842 {
16843 if (gdbarch_address_class_type_flags_p (gdbarch))
16844 {
16845 int type_flags;
16846
16847 type_flags = gdbarch_address_class_type_flags
16848 (gdbarch, byte_size, addr_class);
16849 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16850 == 0);
16851 type = make_type_with_address_space (type, type_flags);
16852 }
16853 else if (TYPE_LENGTH (type) != byte_size)
16854 {
16855 complaint (_("invalid pointer size %d"), byte_size);
16856 }
16857 else if (TYPE_RAW_ALIGN (type) != alignment)
16858 {
16859 complaint (_("Invalid DW_AT_alignment"
16860 " - DIE at %s [in module %s]"),
16861 sect_offset_str (die->sect_off),
16862 objfile_name (cu->per_objfile->objfile));
16863 }
16864 else
16865 {
16866 /* Should we also complain about unhandled address classes? */
16867 }
16868 }
16869
16870 TYPE_LENGTH (type) = byte_size;
16871 set_type_align (type, alignment);
16872 return set_die_type (die, type, cu);
16873 }
16874
16875 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16876 the user defined type vector. */
16877
16878 static struct type *
16879 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16880 {
16881 struct type *type;
16882 struct type *to_type;
16883 struct type *domain;
16884
16885 to_type = die_type (die, cu);
16886 domain = die_containing_type (die, cu);
16887
16888 /* The calls above may have already set the type for this DIE. */
16889 type = get_die_type (die, cu);
16890 if (type)
16891 return type;
16892
16893 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16894 type = lookup_methodptr_type (to_type);
16895 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16896 {
16897 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16898
16899 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16900 to_type->fields (), to_type->num_fields (),
16901 TYPE_VARARGS (to_type));
16902 type = lookup_methodptr_type (new_type);
16903 }
16904 else
16905 type = lookup_memberptr_type (to_type, domain);
16906
16907 return set_die_type (die, type, cu);
16908 }
16909
16910 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16911 the user defined type vector. */
16912
16913 static struct type *
16914 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16915 enum type_code refcode)
16916 {
16917 struct comp_unit_head *cu_header = &cu->header;
16918 struct type *type, *target_type;
16919 struct attribute *attr;
16920
16921 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16922
16923 target_type = die_type (die, cu);
16924
16925 /* The die_type call above may have already set the type for this DIE. */
16926 type = get_die_type (die, cu);
16927 if (type)
16928 return type;
16929
16930 type = lookup_reference_type (target_type, refcode);
16931 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16932 if (attr != nullptr)
16933 {
16934 TYPE_LENGTH (type) = DW_UNSND (attr);
16935 }
16936 else
16937 {
16938 TYPE_LENGTH (type) = cu_header->addr_size;
16939 }
16940 maybe_set_alignment (cu, die, type);
16941 return set_die_type (die, type, cu);
16942 }
16943
16944 /* Add the given cv-qualifiers to the element type of the array. GCC
16945 outputs DWARF type qualifiers that apply to an array, not the
16946 element type. But GDB relies on the array element type to carry
16947 the cv-qualifiers. This mimics section 6.7.3 of the C99
16948 specification. */
16949
16950 static struct type *
16951 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16952 struct type *base_type, int cnst, int voltl)
16953 {
16954 struct type *el_type, *inner_array;
16955
16956 base_type = copy_type (base_type);
16957 inner_array = base_type;
16958
16959 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16960 {
16961 TYPE_TARGET_TYPE (inner_array) =
16962 copy_type (TYPE_TARGET_TYPE (inner_array));
16963 inner_array = TYPE_TARGET_TYPE (inner_array);
16964 }
16965
16966 el_type = TYPE_TARGET_TYPE (inner_array);
16967 cnst |= TYPE_CONST (el_type);
16968 voltl |= TYPE_VOLATILE (el_type);
16969 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16970
16971 return set_die_type (die, base_type, cu);
16972 }
16973
16974 static struct type *
16975 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16976 {
16977 struct type *base_type, *cv_type;
16978
16979 base_type = die_type (die, cu);
16980
16981 /* The die_type call above may have already set the type for this DIE. */
16982 cv_type = get_die_type (die, cu);
16983 if (cv_type)
16984 return cv_type;
16985
16986 /* In case the const qualifier is applied to an array type, the element type
16987 is so qualified, not the array type (section 6.7.3 of C99). */
16988 if (base_type->code () == TYPE_CODE_ARRAY)
16989 return add_array_cv_type (die, cu, base_type, 1, 0);
16990
16991 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16992 return set_die_type (die, cv_type, cu);
16993 }
16994
16995 static struct type *
16996 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16997 {
16998 struct type *base_type, *cv_type;
16999
17000 base_type = die_type (die, cu);
17001
17002 /* The die_type call above may have already set the type for this DIE. */
17003 cv_type = get_die_type (die, cu);
17004 if (cv_type)
17005 return cv_type;
17006
17007 /* In case the volatile qualifier is applied to an array type, the
17008 element type is so qualified, not the array type (section 6.7.3
17009 of C99). */
17010 if (base_type->code () == TYPE_CODE_ARRAY)
17011 return add_array_cv_type (die, cu, base_type, 0, 1);
17012
17013 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17014 return set_die_type (die, cv_type, cu);
17015 }
17016
17017 /* Handle DW_TAG_restrict_type. */
17018
17019 static struct type *
17020 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17021 {
17022 struct type *base_type, *cv_type;
17023
17024 base_type = die_type (die, cu);
17025
17026 /* The die_type call above may have already set the type for this DIE. */
17027 cv_type = get_die_type (die, cu);
17028 if (cv_type)
17029 return cv_type;
17030
17031 cv_type = make_restrict_type (base_type);
17032 return set_die_type (die, cv_type, cu);
17033 }
17034
17035 /* Handle DW_TAG_atomic_type. */
17036
17037 static struct type *
17038 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17039 {
17040 struct type *base_type, *cv_type;
17041
17042 base_type = die_type (die, cu);
17043
17044 /* The die_type call above may have already set the type for this DIE. */
17045 cv_type = get_die_type (die, cu);
17046 if (cv_type)
17047 return cv_type;
17048
17049 cv_type = make_atomic_type (base_type);
17050 return set_die_type (die, cv_type, cu);
17051 }
17052
17053 /* Extract all information from a DW_TAG_string_type DIE and add to
17054 the user defined type vector. It isn't really a user defined type,
17055 but it behaves like one, with other DIE's using an AT_user_def_type
17056 attribute to reference it. */
17057
17058 static struct type *
17059 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17060 {
17061 struct objfile *objfile = cu->per_objfile->objfile;
17062 struct gdbarch *gdbarch = objfile->arch ();
17063 struct type *type, *range_type, *index_type, *char_type;
17064 struct attribute *attr;
17065 struct dynamic_prop prop;
17066 bool length_is_constant = true;
17067 LONGEST length;
17068
17069 /* There are a couple of places where bit sizes might be made use of
17070 when parsing a DW_TAG_string_type, however, no producer that we know
17071 of make use of these. Handling bit sizes that are a multiple of the
17072 byte size is easy enough, but what about other bit sizes? Lets deal
17073 with that problem when we have to. Warn about these attributes being
17074 unsupported, then parse the type and ignore them like we always
17075 have. */
17076 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17077 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17078 {
17079 static bool warning_printed = false;
17080 if (!warning_printed)
17081 {
17082 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17083 "currently supported on DW_TAG_string_type."));
17084 warning_printed = true;
17085 }
17086 }
17087
17088 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17089 if (attr != nullptr && !attr->form_is_constant ())
17090 {
17091 /* The string length describes the location at which the length of
17092 the string can be found. The size of the length field can be
17093 specified with one of the attributes below. */
17094 struct type *prop_type;
17095 struct attribute *len
17096 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17097 if (len == nullptr)
17098 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17099 if (len != nullptr && len->form_is_constant ())
17100 {
17101 /* Pass 0 as the default as we know this attribute is constant
17102 and the default value will not be returned. */
17103 LONGEST sz = len->constant_value (0);
17104 prop_type = cu->per_objfile->int_type (sz, true);
17105 }
17106 else
17107 {
17108 /* If the size is not specified then we assume it is the size of
17109 an address on this target. */
17110 prop_type = cu->addr_sized_int_type (true);
17111 }
17112
17113 /* Convert the attribute into a dynamic property. */
17114 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17115 length = 1;
17116 else
17117 length_is_constant = false;
17118 }
17119 else if (attr != nullptr)
17120 {
17121 /* This DW_AT_string_length just contains the length with no
17122 indirection. There's no need to create a dynamic property in this
17123 case. Pass 0 for the default value as we know it will not be
17124 returned in this case. */
17125 length = attr->constant_value (0);
17126 }
17127 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17128 {
17129 /* We don't currently support non-constant byte sizes for strings. */
17130 length = attr->constant_value (1);
17131 }
17132 else
17133 {
17134 /* Use 1 as a fallback length if we have nothing else. */
17135 length = 1;
17136 }
17137
17138 index_type = objfile_type (objfile)->builtin_int;
17139 if (length_is_constant)
17140 range_type = create_static_range_type (NULL, index_type, 1, length);
17141 else
17142 {
17143 struct dynamic_prop low_bound;
17144
17145 low_bound.kind = PROP_CONST;
17146 low_bound.data.const_val = 1;
17147 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17148 }
17149 char_type = language_string_char_type (cu->language_defn, gdbarch);
17150 type = create_string_type (NULL, char_type, range_type);
17151
17152 return set_die_type (die, type, cu);
17153 }
17154
17155 /* Assuming that DIE corresponds to a function, returns nonzero
17156 if the function is prototyped. */
17157
17158 static int
17159 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17160 {
17161 struct attribute *attr;
17162
17163 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17164 if (attr && (DW_UNSND (attr) != 0))
17165 return 1;
17166
17167 /* The DWARF standard implies that the DW_AT_prototyped attribute
17168 is only meaningful for C, but the concept also extends to other
17169 languages that allow unprototyped functions (Eg: Objective C).
17170 For all other languages, assume that functions are always
17171 prototyped. */
17172 if (cu->language != language_c
17173 && cu->language != language_objc
17174 && cu->language != language_opencl)
17175 return 1;
17176
17177 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17178 prototyped and unprototyped functions; default to prototyped,
17179 since that is more common in modern code (and RealView warns
17180 about unprototyped functions). */
17181 if (producer_is_realview (cu->producer))
17182 return 1;
17183
17184 return 0;
17185 }
17186
17187 /* Handle DIES due to C code like:
17188
17189 struct foo
17190 {
17191 int (*funcp)(int a, long l);
17192 int b;
17193 };
17194
17195 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17196
17197 static struct type *
17198 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17199 {
17200 struct objfile *objfile = cu->per_objfile->objfile;
17201 struct type *type; /* Type that this function returns. */
17202 struct type *ftype; /* Function that returns above type. */
17203 struct attribute *attr;
17204
17205 type = die_type (die, cu);
17206
17207 /* The die_type call above may have already set the type for this DIE. */
17208 ftype = get_die_type (die, cu);
17209 if (ftype)
17210 return ftype;
17211
17212 ftype = lookup_function_type (type);
17213
17214 if (prototyped_function_p (die, cu))
17215 TYPE_PROTOTYPED (ftype) = 1;
17216
17217 /* Store the calling convention in the type if it's available in
17218 the subroutine die. Otherwise set the calling convention to
17219 the default value DW_CC_normal. */
17220 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17221 if (attr != nullptr
17222 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17223 TYPE_CALLING_CONVENTION (ftype)
17224 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17225 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17226 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17227 else
17228 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17229
17230 /* Record whether the function returns normally to its caller or not
17231 if the DWARF producer set that information. */
17232 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17233 if (attr && (DW_UNSND (attr) != 0))
17234 TYPE_NO_RETURN (ftype) = 1;
17235
17236 /* We need to add the subroutine type to the die immediately so
17237 we don't infinitely recurse when dealing with parameters
17238 declared as the same subroutine type. */
17239 set_die_type (die, ftype, cu);
17240
17241 if (die->child != NULL)
17242 {
17243 struct type *void_type = objfile_type (objfile)->builtin_void;
17244 struct die_info *child_die;
17245 int nparams, iparams;
17246
17247 /* Count the number of parameters.
17248 FIXME: GDB currently ignores vararg functions, but knows about
17249 vararg member functions. */
17250 nparams = 0;
17251 child_die = die->child;
17252 while (child_die && child_die->tag)
17253 {
17254 if (child_die->tag == DW_TAG_formal_parameter)
17255 nparams++;
17256 else if (child_die->tag == DW_TAG_unspecified_parameters)
17257 TYPE_VARARGS (ftype) = 1;
17258 child_die = child_die->sibling;
17259 }
17260
17261 /* Allocate storage for parameters and fill them in. */
17262 ftype->set_num_fields (nparams);
17263 ftype->set_fields
17264 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17265
17266 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17267 even if we error out during the parameters reading below. */
17268 for (iparams = 0; iparams < nparams; iparams++)
17269 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17270
17271 iparams = 0;
17272 child_die = die->child;
17273 while (child_die && child_die->tag)
17274 {
17275 if (child_die->tag == DW_TAG_formal_parameter)
17276 {
17277 struct type *arg_type;
17278
17279 /* DWARF version 2 has no clean way to discern C++
17280 static and non-static member functions. G++ helps
17281 GDB by marking the first parameter for non-static
17282 member functions (which is the this pointer) as
17283 artificial. We pass this information to
17284 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17285
17286 DWARF version 3 added DW_AT_object_pointer, which GCC
17287 4.5 does not yet generate. */
17288 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17289 if (attr != nullptr)
17290 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17291 else
17292 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17293 arg_type = die_type (child_die, cu);
17294
17295 /* RealView does not mark THIS as const, which the testsuite
17296 expects. GCC marks THIS as const in method definitions,
17297 but not in the class specifications (GCC PR 43053). */
17298 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17299 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17300 {
17301 int is_this = 0;
17302 struct dwarf2_cu *arg_cu = cu;
17303 const char *name = dwarf2_name (child_die, cu);
17304
17305 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17306 if (attr != nullptr)
17307 {
17308 /* If the compiler emits this, use it. */
17309 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17310 is_this = 1;
17311 }
17312 else if (name && strcmp (name, "this") == 0)
17313 /* Function definitions will have the argument names. */
17314 is_this = 1;
17315 else if (name == NULL && iparams == 0)
17316 /* Declarations may not have the names, so like
17317 elsewhere in GDB, assume an artificial first
17318 argument is "this". */
17319 is_this = 1;
17320
17321 if (is_this)
17322 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17323 arg_type, 0);
17324 }
17325
17326 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17327 iparams++;
17328 }
17329 child_die = child_die->sibling;
17330 }
17331 }
17332
17333 return ftype;
17334 }
17335
17336 static struct type *
17337 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17338 {
17339 struct objfile *objfile = cu->per_objfile->objfile;
17340 const char *name = NULL;
17341 struct type *this_type, *target_type;
17342
17343 name = dwarf2_full_name (NULL, die, cu);
17344 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17345 TYPE_TARGET_STUB (this_type) = 1;
17346 set_die_type (die, this_type, cu);
17347 target_type = die_type (die, cu);
17348 if (target_type != this_type)
17349 TYPE_TARGET_TYPE (this_type) = target_type;
17350 else
17351 {
17352 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17353 spec and cause infinite loops in GDB. */
17354 complaint (_("Self-referential DW_TAG_typedef "
17355 "- DIE at %s [in module %s]"),
17356 sect_offset_str (die->sect_off), objfile_name (objfile));
17357 TYPE_TARGET_TYPE (this_type) = NULL;
17358 }
17359 if (name == NULL)
17360 {
17361 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17362 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17363 Handle these by just returning the target type, rather than
17364 constructing an anonymous typedef type and trying to handle this
17365 elsewhere. */
17366 set_die_type (die, target_type, cu);
17367 return target_type;
17368 }
17369 return this_type;
17370 }
17371
17372 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17373 (which may be different from NAME) to the architecture back-end to allow
17374 it to guess the correct format if necessary. */
17375
17376 static struct type *
17377 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17378 const char *name_hint, enum bfd_endian byte_order)
17379 {
17380 struct gdbarch *gdbarch = objfile->arch ();
17381 const struct floatformat **format;
17382 struct type *type;
17383
17384 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17385 if (format)
17386 type = init_float_type (objfile, bits, name, format, byte_order);
17387 else
17388 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17389
17390 return type;
17391 }
17392
17393 /* Allocate an integer type of size BITS and name NAME. */
17394
17395 static struct type *
17396 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17397 int bits, int unsigned_p, const char *name)
17398 {
17399 struct type *type;
17400
17401 /* Versions of Intel's C Compiler generate an integer type called "void"
17402 instead of using DW_TAG_unspecified_type. This has been seen on
17403 at least versions 14, 17, and 18. */
17404 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17405 && strcmp (name, "void") == 0)
17406 type = objfile_type (objfile)->builtin_void;
17407 else
17408 type = init_integer_type (objfile, bits, unsigned_p, name);
17409
17410 return type;
17411 }
17412
17413 /* Initialise and return a floating point type of size BITS suitable for
17414 use as a component of a complex number. The NAME_HINT is passed through
17415 when initialising the floating point type and is the name of the complex
17416 type.
17417
17418 As DWARF doesn't currently provide an explicit name for the components
17419 of a complex number, but it can be helpful to have these components
17420 named, we try to select a suitable name based on the size of the
17421 component. */
17422 static struct type *
17423 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17424 struct objfile *objfile,
17425 int bits, const char *name_hint,
17426 enum bfd_endian byte_order)
17427 {
17428 gdbarch *gdbarch = objfile->arch ();
17429 struct type *tt = nullptr;
17430
17431 /* Try to find a suitable floating point builtin type of size BITS.
17432 We're going to use the name of this type as the name for the complex
17433 target type that we are about to create. */
17434 switch (cu->language)
17435 {
17436 case language_fortran:
17437 switch (bits)
17438 {
17439 case 32:
17440 tt = builtin_f_type (gdbarch)->builtin_real;
17441 break;
17442 case 64:
17443 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17444 break;
17445 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17446 case 128:
17447 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17448 break;
17449 }
17450 break;
17451 default:
17452 switch (bits)
17453 {
17454 case 32:
17455 tt = builtin_type (gdbarch)->builtin_float;
17456 break;
17457 case 64:
17458 tt = builtin_type (gdbarch)->builtin_double;
17459 break;
17460 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17461 case 128:
17462 tt = builtin_type (gdbarch)->builtin_long_double;
17463 break;
17464 }
17465 break;
17466 }
17467
17468 /* If the type we found doesn't match the size we were looking for, then
17469 pretend we didn't find a type at all, the complex target type we
17470 create will then be nameless. */
17471 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17472 tt = nullptr;
17473
17474 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17475 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17476 }
17477
17478 /* Find a representation of a given base type and install
17479 it in the TYPE field of the die. */
17480
17481 static struct type *
17482 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17483 {
17484 struct objfile *objfile = cu->per_objfile->objfile;
17485 struct type *type;
17486 struct attribute *attr;
17487 int encoding = 0, bits = 0;
17488 const char *name;
17489 gdbarch *arch;
17490
17491 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17492 if (attr != nullptr)
17493 encoding = DW_UNSND (attr);
17494 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17495 if (attr != nullptr)
17496 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17497 name = dwarf2_name (die, cu);
17498 if (!name)
17499 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17500
17501 arch = objfile->arch ();
17502 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17503
17504 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17505 if (attr)
17506 {
17507 int endianity = DW_UNSND (attr);
17508
17509 switch (endianity)
17510 {
17511 case DW_END_big:
17512 byte_order = BFD_ENDIAN_BIG;
17513 break;
17514 case DW_END_little:
17515 byte_order = BFD_ENDIAN_LITTLE;
17516 break;
17517 default:
17518 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17519 break;
17520 }
17521 }
17522
17523 switch (encoding)
17524 {
17525 case DW_ATE_address:
17526 /* Turn DW_ATE_address into a void * pointer. */
17527 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17528 type = init_pointer_type (objfile, bits, name, type);
17529 break;
17530 case DW_ATE_boolean:
17531 type = init_boolean_type (objfile, bits, 1, name);
17532 break;
17533 case DW_ATE_complex_float:
17534 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17535 byte_order);
17536 if (type->code () == TYPE_CODE_ERROR)
17537 {
17538 if (name == nullptr)
17539 {
17540 struct obstack *obstack
17541 = &cu->per_objfile->objfile->objfile_obstack;
17542 name = obconcat (obstack, "_Complex ", type->name (),
17543 nullptr);
17544 }
17545 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17546 }
17547 else
17548 type = init_complex_type (name, type);
17549 break;
17550 case DW_ATE_decimal_float:
17551 type = init_decfloat_type (objfile, bits, name);
17552 break;
17553 case DW_ATE_float:
17554 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17555 break;
17556 case DW_ATE_signed:
17557 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17558 break;
17559 case DW_ATE_unsigned:
17560 if (cu->language == language_fortran
17561 && name
17562 && startswith (name, "character("))
17563 type = init_character_type (objfile, bits, 1, name);
17564 else
17565 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17566 break;
17567 case DW_ATE_signed_char:
17568 if (cu->language == language_ada || cu->language == language_m2
17569 || cu->language == language_pascal
17570 || cu->language == language_fortran)
17571 type = init_character_type (objfile, bits, 0, name);
17572 else
17573 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17574 break;
17575 case DW_ATE_unsigned_char:
17576 if (cu->language == language_ada || cu->language == language_m2
17577 || cu->language == language_pascal
17578 || cu->language == language_fortran
17579 || cu->language == language_rust)
17580 type = init_character_type (objfile, bits, 1, name);
17581 else
17582 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17583 break;
17584 case DW_ATE_UTF:
17585 {
17586 if (bits == 16)
17587 type = builtin_type (arch)->builtin_char16;
17588 else if (bits == 32)
17589 type = builtin_type (arch)->builtin_char32;
17590 else
17591 {
17592 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17593 bits);
17594 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17595 }
17596 return set_die_type (die, type, cu);
17597 }
17598 break;
17599
17600 default:
17601 complaint (_("unsupported DW_AT_encoding: '%s'"),
17602 dwarf_type_encoding_name (encoding));
17603 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17604 break;
17605 }
17606
17607 if (name && strcmp (name, "char") == 0)
17608 TYPE_NOSIGN (type) = 1;
17609
17610 maybe_set_alignment (cu, die, type);
17611
17612 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17613
17614 return set_die_type (die, type, cu);
17615 }
17616
17617 /* Parse dwarf attribute if it's a block, reference or constant and put the
17618 resulting value of the attribute into struct bound_prop.
17619 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17620
17621 static int
17622 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17623 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17624 struct type *default_type)
17625 {
17626 struct dwarf2_property_baton *baton;
17627 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17628 struct objfile *objfile = per_objfile->objfile;
17629 struct obstack *obstack = &objfile->objfile_obstack;
17630
17631 gdb_assert (default_type != NULL);
17632
17633 if (attr == NULL || prop == NULL)
17634 return 0;
17635
17636 if (attr->form_is_block ())
17637 {
17638 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17639 baton->property_type = default_type;
17640 baton->locexpr.per_cu = cu->per_cu;
17641 baton->locexpr.per_objfile = per_objfile;
17642 baton->locexpr.size = DW_BLOCK (attr)->size;
17643 baton->locexpr.data = DW_BLOCK (attr)->data;
17644 switch (attr->name)
17645 {
17646 case DW_AT_string_length:
17647 baton->locexpr.is_reference = true;
17648 break;
17649 default:
17650 baton->locexpr.is_reference = false;
17651 break;
17652 }
17653 prop->data.baton = baton;
17654 prop->kind = PROP_LOCEXPR;
17655 gdb_assert (prop->data.baton != NULL);
17656 }
17657 else if (attr->form_is_ref ())
17658 {
17659 struct dwarf2_cu *target_cu = cu;
17660 struct die_info *target_die;
17661 struct attribute *target_attr;
17662
17663 target_die = follow_die_ref (die, attr, &target_cu);
17664 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17665 if (target_attr == NULL)
17666 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17667 target_cu);
17668 if (target_attr == NULL)
17669 return 0;
17670
17671 switch (target_attr->name)
17672 {
17673 case DW_AT_location:
17674 if (target_attr->form_is_section_offset ())
17675 {
17676 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17677 baton->property_type = die_type (target_die, target_cu);
17678 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17679 prop->data.baton = baton;
17680 prop->kind = PROP_LOCLIST;
17681 gdb_assert (prop->data.baton != NULL);
17682 }
17683 else if (target_attr->form_is_block ())
17684 {
17685 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17686 baton->property_type = die_type (target_die, target_cu);
17687 baton->locexpr.per_cu = cu->per_cu;
17688 baton->locexpr.per_objfile = per_objfile;
17689 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17690 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17691 baton->locexpr.is_reference = true;
17692 prop->data.baton = baton;
17693 prop->kind = PROP_LOCEXPR;
17694 gdb_assert (prop->data.baton != NULL);
17695 }
17696 else
17697 {
17698 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17699 "dynamic property");
17700 return 0;
17701 }
17702 break;
17703 case DW_AT_data_member_location:
17704 {
17705 LONGEST offset;
17706
17707 if (!handle_data_member_location (target_die, target_cu,
17708 &offset))
17709 return 0;
17710
17711 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17712 baton->property_type = read_type_die (target_die->parent,
17713 target_cu);
17714 baton->offset_info.offset = offset;
17715 baton->offset_info.type = die_type (target_die, target_cu);
17716 prop->data.baton = baton;
17717 prop->kind = PROP_ADDR_OFFSET;
17718 break;
17719 }
17720 }
17721 }
17722 else if (attr->form_is_constant ())
17723 {
17724 prop->data.const_val = attr->constant_value (0);
17725 prop->kind = PROP_CONST;
17726 }
17727 else
17728 {
17729 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17730 dwarf2_name (die, cu));
17731 return 0;
17732 }
17733
17734 return 1;
17735 }
17736
17737 /* See read.h. */
17738
17739 struct type *
17740 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17741 {
17742 struct type *int_type;
17743
17744 /* Helper macro to examine the various builtin types. */
17745 #define TRY_TYPE(F) \
17746 int_type = (unsigned_p \
17747 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17748 : objfile_type (objfile)->builtin_ ## F); \
17749 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17750 return int_type
17751
17752 TRY_TYPE (char);
17753 TRY_TYPE (short);
17754 TRY_TYPE (int);
17755 TRY_TYPE (long);
17756 TRY_TYPE (long_long);
17757
17758 #undef TRY_TYPE
17759
17760 gdb_assert_not_reached ("unable to find suitable integer type");
17761 }
17762
17763 /* See read.h. */
17764
17765 struct type *
17766 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17767 {
17768 int addr_size = this->per_cu->addr_size ();
17769 return this->per_objfile->int_type (addr_size, unsigned_p);
17770 }
17771
17772 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17773 present (which is valid) then compute the default type based on the
17774 compilation units address size. */
17775
17776 static struct type *
17777 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17778 {
17779 struct type *index_type = die_type (die, cu);
17780
17781 /* Dwarf-2 specifications explicitly allows to create subrange types
17782 without specifying a base type.
17783 In that case, the base type must be set to the type of
17784 the lower bound, upper bound or count, in that order, if any of these
17785 three attributes references an object that has a type.
17786 If no base type is found, the Dwarf-2 specifications say that
17787 a signed integer type of size equal to the size of an address should
17788 be used.
17789 For the following C code: `extern char gdb_int [];'
17790 GCC produces an empty range DIE.
17791 FIXME: muller/2010-05-28: Possible references to object for low bound,
17792 high bound or count are not yet handled by this code. */
17793 if (index_type->code () == TYPE_CODE_VOID)
17794 index_type = cu->addr_sized_int_type (false);
17795
17796 return index_type;
17797 }
17798
17799 /* Read the given DW_AT_subrange DIE. */
17800
17801 static struct type *
17802 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17803 {
17804 struct type *base_type, *orig_base_type;
17805 struct type *range_type;
17806 struct attribute *attr;
17807 struct dynamic_prop low, high;
17808 int low_default_is_valid;
17809 int high_bound_is_count = 0;
17810 const char *name;
17811 ULONGEST negative_mask;
17812
17813 orig_base_type = read_subrange_index_type (die, cu);
17814
17815 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17816 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17817 creating the range type, but we use the result of check_typedef
17818 when examining properties of the type. */
17819 base_type = check_typedef (orig_base_type);
17820
17821 /* The die_type call above may have already set the type for this DIE. */
17822 range_type = get_die_type (die, cu);
17823 if (range_type)
17824 return range_type;
17825
17826 low.kind = PROP_CONST;
17827 high.kind = PROP_CONST;
17828 high.data.const_val = 0;
17829
17830 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17831 omitting DW_AT_lower_bound. */
17832 switch (cu->language)
17833 {
17834 case language_c:
17835 case language_cplus:
17836 low.data.const_val = 0;
17837 low_default_is_valid = 1;
17838 break;
17839 case language_fortran:
17840 low.data.const_val = 1;
17841 low_default_is_valid = 1;
17842 break;
17843 case language_d:
17844 case language_objc:
17845 case language_rust:
17846 low.data.const_val = 0;
17847 low_default_is_valid = (cu->header.version >= 4);
17848 break;
17849 case language_ada:
17850 case language_m2:
17851 case language_pascal:
17852 low.data.const_val = 1;
17853 low_default_is_valid = (cu->header.version >= 4);
17854 break;
17855 default:
17856 low.data.const_val = 0;
17857 low_default_is_valid = 0;
17858 break;
17859 }
17860
17861 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17862 if (attr != nullptr)
17863 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17864 else if (!low_default_is_valid)
17865 complaint (_("Missing DW_AT_lower_bound "
17866 "- DIE at %s [in module %s]"),
17867 sect_offset_str (die->sect_off),
17868 objfile_name (cu->per_objfile->objfile));
17869
17870 struct attribute *attr_ub, *attr_count;
17871 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17872 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17873 {
17874 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17875 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17876 {
17877 /* If bounds are constant do the final calculation here. */
17878 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17879 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17880 else
17881 high_bound_is_count = 1;
17882 }
17883 else
17884 {
17885 if (attr_ub != NULL)
17886 complaint (_("Unresolved DW_AT_upper_bound "
17887 "- DIE at %s [in module %s]"),
17888 sect_offset_str (die->sect_off),
17889 objfile_name (cu->per_objfile->objfile));
17890 if (attr_count != NULL)
17891 complaint (_("Unresolved DW_AT_count "
17892 "- DIE at %s [in module %s]"),
17893 sect_offset_str (die->sect_off),
17894 objfile_name (cu->per_objfile->objfile));
17895 }
17896 }
17897
17898 LONGEST bias = 0;
17899 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17900 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17901 bias = bias_attr->constant_value (0);
17902
17903 /* Normally, the DWARF producers are expected to use a signed
17904 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17905 But this is unfortunately not always the case, as witnessed
17906 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17907 is used instead. To work around that ambiguity, we treat
17908 the bounds as signed, and thus sign-extend their values, when
17909 the base type is signed. */
17910 negative_mask =
17911 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17912 if (low.kind == PROP_CONST
17913 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17914 low.data.const_val |= negative_mask;
17915 if (high.kind == PROP_CONST
17916 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17917 high.data.const_val |= negative_mask;
17918
17919 /* Check for bit and byte strides. */
17920 struct dynamic_prop byte_stride_prop;
17921 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17922 if (attr_byte_stride != nullptr)
17923 {
17924 struct type *prop_type = cu->addr_sized_int_type (false);
17925 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17926 prop_type);
17927 }
17928
17929 struct dynamic_prop bit_stride_prop;
17930 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17931 if (attr_bit_stride != nullptr)
17932 {
17933 /* It only makes sense to have either a bit or byte stride. */
17934 if (attr_byte_stride != nullptr)
17935 {
17936 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17937 "- DIE at %s [in module %s]"),
17938 sect_offset_str (die->sect_off),
17939 objfile_name (cu->per_objfile->objfile));
17940 attr_bit_stride = nullptr;
17941 }
17942 else
17943 {
17944 struct type *prop_type = cu->addr_sized_int_type (false);
17945 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17946 prop_type);
17947 }
17948 }
17949
17950 if (attr_byte_stride != nullptr
17951 || attr_bit_stride != nullptr)
17952 {
17953 bool byte_stride_p = (attr_byte_stride != nullptr);
17954 struct dynamic_prop *stride
17955 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17956
17957 range_type
17958 = create_range_type_with_stride (NULL, orig_base_type, &low,
17959 &high, bias, stride, byte_stride_p);
17960 }
17961 else
17962 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17963
17964 if (high_bound_is_count)
17965 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17966
17967 /* Ada expects an empty array on no boundary attributes. */
17968 if (attr == NULL && cu->language != language_ada)
17969 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17970
17971 name = dwarf2_name (die, cu);
17972 if (name)
17973 range_type->set_name (name);
17974
17975 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17976 if (attr != nullptr)
17977 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17978
17979 maybe_set_alignment (cu, die, range_type);
17980
17981 set_die_type (die, range_type, cu);
17982
17983 /* set_die_type should be already done. */
17984 set_descriptive_type (range_type, die, cu);
17985
17986 return range_type;
17987 }
17988
17989 static struct type *
17990 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17991 {
17992 struct type *type;
17993
17994 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17995 type->set_name (dwarf2_name (die, cu));
17996
17997 /* In Ada, an unspecified type is typically used when the description
17998 of the type is deferred to a different unit. When encountering
17999 such a type, we treat it as a stub, and try to resolve it later on,
18000 when needed. */
18001 if (cu->language == language_ada)
18002 TYPE_STUB (type) = 1;
18003
18004 return set_die_type (die, type, cu);
18005 }
18006
18007 /* Read a single die and all its descendents. Set the die's sibling
18008 field to NULL; set other fields in the die correctly, and set all
18009 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18010 location of the info_ptr after reading all of those dies. PARENT
18011 is the parent of the die in question. */
18012
18013 static struct die_info *
18014 read_die_and_children (const struct die_reader_specs *reader,
18015 const gdb_byte *info_ptr,
18016 const gdb_byte **new_info_ptr,
18017 struct die_info *parent)
18018 {
18019 struct die_info *die;
18020 const gdb_byte *cur_ptr;
18021
18022 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18023 if (die == NULL)
18024 {
18025 *new_info_ptr = cur_ptr;
18026 return NULL;
18027 }
18028 store_in_ref_table (die, reader->cu);
18029
18030 if (die->has_children)
18031 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18032 else
18033 {
18034 die->child = NULL;
18035 *new_info_ptr = cur_ptr;
18036 }
18037
18038 die->sibling = NULL;
18039 die->parent = parent;
18040 return die;
18041 }
18042
18043 /* Read a die, all of its descendents, and all of its siblings; set
18044 all of the fields of all of the dies correctly. Arguments are as
18045 in read_die_and_children. */
18046
18047 static struct die_info *
18048 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18049 const gdb_byte *info_ptr,
18050 const gdb_byte **new_info_ptr,
18051 struct die_info *parent)
18052 {
18053 struct die_info *first_die, *last_sibling;
18054 const gdb_byte *cur_ptr;
18055
18056 cur_ptr = info_ptr;
18057 first_die = last_sibling = NULL;
18058
18059 while (1)
18060 {
18061 struct die_info *die
18062 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18063
18064 if (die == NULL)
18065 {
18066 *new_info_ptr = cur_ptr;
18067 return first_die;
18068 }
18069
18070 if (!first_die)
18071 first_die = die;
18072 else
18073 last_sibling->sibling = die;
18074
18075 last_sibling = die;
18076 }
18077 }
18078
18079 /* Read a die, all of its descendents, and all of its siblings; set
18080 all of the fields of all of the dies correctly. Arguments are as
18081 in read_die_and_children.
18082 This the main entry point for reading a DIE and all its children. */
18083
18084 static struct die_info *
18085 read_die_and_siblings (const struct die_reader_specs *reader,
18086 const gdb_byte *info_ptr,
18087 const gdb_byte **new_info_ptr,
18088 struct die_info *parent)
18089 {
18090 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18091 new_info_ptr, parent);
18092
18093 if (dwarf_die_debug)
18094 {
18095 fprintf_unfiltered (gdb_stdlog,
18096 "Read die from %s@0x%x of %s:\n",
18097 reader->die_section->get_name (),
18098 (unsigned) (info_ptr - reader->die_section->buffer),
18099 bfd_get_filename (reader->abfd));
18100 dump_die (die, dwarf_die_debug);
18101 }
18102
18103 return die;
18104 }
18105
18106 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18107 attributes.
18108 The caller is responsible for filling in the extra attributes
18109 and updating (*DIEP)->num_attrs.
18110 Set DIEP to point to a newly allocated die with its information,
18111 except for its child, sibling, and parent fields. */
18112
18113 static const gdb_byte *
18114 read_full_die_1 (const struct die_reader_specs *reader,
18115 struct die_info **diep, const gdb_byte *info_ptr,
18116 int num_extra_attrs)
18117 {
18118 unsigned int abbrev_number, bytes_read, i;
18119 struct abbrev_info *abbrev;
18120 struct die_info *die;
18121 struct dwarf2_cu *cu = reader->cu;
18122 bfd *abfd = reader->abfd;
18123
18124 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18125 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18126 info_ptr += bytes_read;
18127 if (!abbrev_number)
18128 {
18129 *diep = NULL;
18130 return info_ptr;
18131 }
18132
18133 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18134 if (!abbrev)
18135 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18136 abbrev_number,
18137 bfd_get_filename (abfd));
18138
18139 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18140 die->sect_off = sect_off;
18141 die->tag = abbrev->tag;
18142 die->abbrev = abbrev_number;
18143 die->has_children = abbrev->has_children;
18144
18145 /* Make the result usable.
18146 The caller needs to update num_attrs after adding the extra
18147 attributes. */
18148 die->num_attrs = abbrev->num_attrs;
18149
18150 std::vector<int> indexes_that_need_reprocess;
18151 for (i = 0; i < abbrev->num_attrs; ++i)
18152 {
18153 bool need_reprocess;
18154 info_ptr =
18155 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18156 info_ptr, &need_reprocess);
18157 if (need_reprocess)
18158 indexes_that_need_reprocess.push_back (i);
18159 }
18160
18161 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18162 if (attr != nullptr)
18163 cu->str_offsets_base = DW_UNSND (attr);
18164
18165 attr = die->attr (DW_AT_loclists_base);
18166 if (attr != nullptr)
18167 cu->loclist_base = DW_UNSND (attr);
18168
18169 auto maybe_addr_base = die->addr_base ();
18170 if (maybe_addr_base.has_value ())
18171 cu->addr_base = *maybe_addr_base;
18172 for (int index : indexes_that_need_reprocess)
18173 read_attribute_reprocess (reader, &die->attrs[index]);
18174 *diep = die;
18175 return info_ptr;
18176 }
18177
18178 /* Read a die and all its attributes.
18179 Set DIEP to point to a newly allocated die with its information,
18180 except for its child, sibling, and parent fields. */
18181
18182 static const gdb_byte *
18183 read_full_die (const struct die_reader_specs *reader,
18184 struct die_info **diep, const gdb_byte *info_ptr)
18185 {
18186 const gdb_byte *result;
18187
18188 result = read_full_die_1 (reader, diep, info_ptr, 0);
18189
18190 if (dwarf_die_debug)
18191 {
18192 fprintf_unfiltered (gdb_stdlog,
18193 "Read die from %s@0x%x of %s:\n",
18194 reader->die_section->get_name (),
18195 (unsigned) (info_ptr - reader->die_section->buffer),
18196 bfd_get_filename (reader->abfd));
18197 dump_die (*diep, dwarf_die_debug);
18198 }
18199
18200 return result;
18201 }
18202 \f
18203
18204 /* Returns nonzero if TAG represents a type that we might generate a partial
18205 symbol for. */
18206
18207 static int
18208 is_type_tag_for_partial (int tag)
18209 {
18210 switch (tag)
18211 {
18212 #if 0
18213 /* Some types that would be reasonable to generate partial symbols for,
18214 that we don't at present. */
18215 case DW_TAG_array_type:
18216 case DW_TAG_file_type:
18217 case DW_TAG_ptr_to_member_type:
18218 case DW_TAG_set_type:
18219 case DW_TAG_string_type:
18220 case DW_TAG_subroutine_type:
18221 #endif
18222 case DW_TAG_base_type:
18223 case DW_TAG_class_type:
18224 case DW_TAG_interface_type:
18225 case DW_TAG_enumeration_type:
18226 case DW_TAG_structure_type:
18227 case DW_TAG_subrange_type:
18228 case DW_TAG_typedef:
18229 case DW_TAG_union_type:
18230 return 1;
18231 default:
18232 return 0;
18233 }
18234 }
18235
18236 /* Load all DIEs that are interesting for partial symbols into memory. */
18237
18238 static struct partial_die_info *
18239 load_partial_dies (const struct die_reader_specs *reader,
18240 const gdb_byte *info_ptr, int building_psymtab)
18241 {
18242 struct dwarf2_cu *cu = reader->cu;
18243 struct objfile *objfile = cu->per_objfile->objfile;
18244 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18245 unsigned int bytes_read;
18246 unsigned int load_all = 0;
18247 int nesting_level = 1;
18248
18249 parent_die = NULL;
18250 last_die = NULL;
18251
18252 gdb_assert (cu->per_cu != NULL);
18253 if (cu->per_cu->load_all_dies)
18254 load_all = 1;
18255
18256 cu->partial_dies
18257 = htab_create_alloc_ex (cu->header.length / 12,
18258 partial_die_hash,
18259 partial_die_eq,
18260 NULL,
18261 &cu->comp_unit_obstack,
18262 hashtab_obstack_allocate,
18263 dummy_obstack_deallocate);
18264
18265 while (1)
18266 {
18267 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18268
18269 /* A NULL abbrev means the end of a series of children. */
18270 if (abbrev == NULL)
18271 {
18272 if (--nesting_level == 0)
18273 return first_die;
18274
18275 info_ptr += bytes_read;
18276 last_die = parent_die;
18277 parent_die = parent_die->die_parent;
18278 continue;
18279 }
18280
18281 /* Check for template arguments. We never save these; if
18282 they're seen, we just mark the parent, and go on our way. */
18283 if (parent_die != NULL
18284 && cu->language == language_cplus
18285 && (abbrev->tag == DW_TAG_template_type_param
18286 || abbrev->tag == DW_TAG_template_value_param))
18287 {
18288 parent_die->has_template_arguments = 1;
18289
18290 if (!load_all)
18291 {
18292 /* We don't need a partial DIE for the template argument. */
18293 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18294 continue;
18295 }
18296 }
18297
18298 /* We only recurse into c++ subprograms looking for template arguments.
18299 Skip their other children. */
18300 if (!load_all
18301 && cu->language == language_cplus
18302 && parent_die != NULL
18303 && parent_die->tag == DW_TAG_subprogram)
18304 {
18305 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18306 continue;
18307 }
18308
18309 /* Check whether this DIE is interesting enough to save. Normally
18310 we would not be interested in members here, but there may be
18311 later variables referencing them via DW_AT_specification (for
18312 static members). */
18313 if (!load_all
18314 && !is_type_tag_for_partial (abbrev->tag)
18315 && abbrev->tag != DW_TAG_constant
18316 && abbrev->tag != DW_TAG_enumerator
18317 && abbrev->tag != DW_TAG_subprogram
18318 && abbrev->tag != DW_TAG_inlined_subroutine
18319 && abbrev->tag != DW_TAG_lexical_block
18320 && abbrev->tag != DW_TAG_variable
18321 && abbrev->tag != DW_TAG_namespace
18322 && abbrev->tag != DW_TAG_module
18323 && abbrev->tag != DW_TAG_member
18324 && abbrev->tag != DW_TAG_imported_unit
18325 && abbrev->tag != DW_TAG_imported_declaration)
18326 {
18327 /* Otherwise we skip to the next sibling, if any. */
18328 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18329 continue;
18330 }
18331
18332 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18333 abbrev);
18334
18335 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18336
18337 /* This two-pass algorithm for processing partial symbols has a
18338 high cost in cache pressure. Thus, handle some simple cases
18339 here which cover the majority of C partial symbols. DIEs
18340 which neither have specification tags in them, nor could have
18341 specification tags elsewhere pointing at them, can simply be
18342 processed and discarded.
18343
18344 This segment is also optional; scan_partial_symbols and
18345 add_partial_symbol will handle these DIEs if we chain
18346 them in normally. When compilers which do not emit large
18347 quantities of duplicate debug information are more common,
18348 this code can probably be removed. */
18349
18350 /* Any complete simple types at the top level (pretty much all
18351 of them, for a language without namespaces), can be processed
18352 directly. */
18353 if (parent_die == NULL
18354 && pdi.has_specification == 0
18355 && pdi.is_declaration == 0
18356 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18357 || pdi.tag == DW_TAG_base_type
18358 || pdi.tag == DW_TAG_subrange_type))
18359 {
18360 if (building_psymtab && pdi.raw_name != NULL)
18361 add_partial_symbol (&pdi, cu);
18362
18363 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18364 continue;
18365 }
18366
18367 /* The exception for DW_TAG_typedef with has_children above is
18368 a workaround of GCC PR debug/47510. In the case of this complaint
18369 type_name_or_error will error on such types later.
18370
18371 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18372 it could not find the child DIEs referenced later, this is checked
18373 above. In correct DWARF DW_TAG_typedef should have no children. */
18374
18375 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18376 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18377 "- DIE at %s [in module %s]"),
18378 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18379
18380 /* If we're at the second level, and we're an enumerator, and
18381 our parent has no specification (meaning possibly lives in a
18382 namespace elsewhere), then we can add the partial symbol now
18383 instead of queueing it. */
18384 if (pdi.tag == DW_TAG_enumerator
18385 && parent_die != NULL
18386 && parent_die->die_parent == NULL
18387 && parent_die->tag == DW_TAG_enumeration_type
18388 && parent_die->has_specification == 0)
18389 {
18390 if (pdi.raw_name == NULL)
18391 complaint (_("malformed enumerator DIE ignored"));
18392 else if (building_psymtab)
18393 add_partial_symbol (&pdi, cu);
18394
18395 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18396 continue;
18397 }
18398
18399 struct partial_die_info *part_die
18400 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18401
18402 /* We'll save this DIE so link it in. */
18403 part_die->die_parent = parent_die;
18404 part_die->die_sibling = NULL;
18405 part_die->die_child = NULL;
18406
18407 if (last_die && last_die == parent_die)
18408 last_die->die_child = part_die;
18409 else if (last_die)
18410 last_die->die_sibling = part_die;
18411
18412 last_die = part_die;
18413
18414 if (first_die == NULL)
18415 first_die = part_die;
18416
18417 /* Maybe add the DIE to the hash table. Not all DIEs that we
18418 find interesting need to be in the hash table, because we
18419 also have the parent/sibling/child chains; only those that we
18420 might refer to by offset later during partial symbol reading.
18421
18422 For now this means things that might have be the target of a
18423 DW_AT_specification, DW_AT_abstract_origin, or
18424 DW_AT_extension. DW_AT_extension will refer only to
18425 namespaces; DW_AT_abstract_origin refers to functions (and
18426 many things under the function DIE, but we do not recurse
18427 into function DIEs during partial symbol reading) and
18428 possibly variables as well; DW_AT_specification refers to
18429 declarations. Declarations ought to have the DW_AT_declaration
18430 flag. It happens that GCC forgets to put it in sometimes, but
18431 only for functions, not for types.
18432
18433 Adding more things than necessary to the hash table is harmless
18434 except for the performance cost. Adding too few will result in
18435 wasted time in find_partial_die, when we reread the compilation
18436 unit with load_all_dies set. */
18437
18438 if (load_all
18439 || abbrev->tag == DW_TAG_constant
18440 || abbrev->tag == DW_TAG_subprogram
18441 || abbrev->tag == DW_TAG_variable
18442 || abbrev->tag == DW_TAG_namespace
18443 || part_die->is_declaration)
18444 {
18445 void **slot;
18446
18447 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18448 to_underlying (part_die->sect_off),
18449 INSERT);
18450 *slot = part_die;
18451 }
18452
18453 /* For some DIEs we want to follow their children (if any). For C
18454 we have no reason to follow the children of structures; for other
18455 languages we have to, so that we can get at method physnames
18456 to infer fully qualified class names, for DW_AT_specification,
18457 and for C++ template arguments. For C++, we also look one level
18458 inside functions to find template arguments (if the name of the
18459 function does not already contain the template arguments).
18460
18461 For Ada and Fortran, we need to scan the children of subprograms
18462 and lexical blocks as well because these languages allow the
18463 definition of nested entities that could be interesting for the
18464 debugger, such as nested subprograms for instance. */
18465 if (last_die->has_children
18466 && (load_all
18467 || last_die->tag == DW_TAG_namespace
18468 || last_die->tag == DW_TAG_module
18469 || last_die->tag == DW_TAG_enumeration_type
18470 || (cu->language == language_cplus
18471 && last_die->tag == DW_TAG_subprogram
18472 && (last_die->raw_name == NULL
18473 || strchr (last_die->raw_name, '<') == NULL))
18474 || (cu->language != language_c
18475 && (last_die->tag == DW_TAG_class_type
18476 || last_die->tag == DW_TAG_interface_type
18477 || last_die->tag == DW_TAG_structure_type
18478 || last_die->tag == DW_TAG_union_type))
18479 || ((cu->language == language_ada
18480 || cu->language == language_fortran)
18481 && (last_die->tag == DW_TAG_subprogram
18482 || last_die->tag == DW_TAG_lexical_block))))
18483 {
18484 nesting_level++;
18485 parent_die = last_die;
18486 continue;
18487 }
18488
18489 /* Otherwise we skip to the next sibling, if any. */
18490 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18491
18492 /* Back to the top, do it again. */
18493 }
18494 }
18495
18496 partial_die_info::partial_die_info (sect_offset sect_off_,
18497 struct abbrev_info *abbrev)
18498 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18499 {
18500 }
18501
18502 /* See class definition. */
18503
18504 const char *
18505 partial_die_info::name (dwarf2_cu *cu)
18506 {
18507 if (!canonical_name && raw_name != nullptr)
18508 {
18509 struct objfile *objfile = cu->per_objfile->objfile;
18510 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18511 canonical_name = 1;
18512 }
18513
18514 return raw_name;
18515 }
18516
18517 /* Read a minimal amount of information into the minimal die structure.
18518 INFO_PTR should point just after the initial uleb128 of a DIE. */
18519
18520 const gdb_byte *
18521 partial_die_info::read (const struct die_reader_specs *reader,
18522 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18523 {
18524 struct dwarf2_cu *cu = reader->cu;
18525 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18526 unsigned int i;
18527 int has_low_pc_attr = 0;
18528 int has_high_pc_attr = 0;
18529 int high_pc_relative = 0;
18530
18531 for (i = 0; i < abbrev.num_attrs; ++i)
18532 {
18533 attribute attr;
18534 bool need_reprocess;
18535 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18536 info_ptr, &need_reprocess);
18537 /* String and address offsets that need to do the reprocessing have
18538 already been read at this point, so there is no need to wait until
18539 the loop terminates to do the reprocessing. */
18540 if (need_reprocess)
18541 read_attribute_reprocess (reader, &attr);
18542 /* Store the data if it is of an attribute we want to keep in a
18543 partial symbol table. */
18544 switch (attr.name)
18545 {
18546 case DW_AT_name:
18547 switch (tag)
18548 {
18549 case DW_TAG_compile_unit:
18550 case DW_TAG_partial_unit:
18551 case DW_TAG_type_unit:
18552 /* Compilation units have a DW_AT_name that is a filename, not
18553 a source language identifier. */
18554 case DW_TAG_enumeration_type:
18555 case DW_TAG_enumerator:
18556 /* These tags always have simple identifiers already; no need
18557 to canonicalize them. */
18558 canonical_name = 1;
18559 raw_name = DW_STRING (&attr);
18560 break;
18561 default:
18562 canonical_name = 0;
18563 raw_name = DW_STRING (&attr);
18564 break;
18565 }
18566 break;
18567 case DW_AT_linkage_name:
18568 case DW_AT_MIPS_linkage_name:
18569 /* Note that both forms of linkage name might appear. We
18570 assume they will be the same, and we only store the last
18571 one we see. */
18572 linkage_name = attr.value_as_string ();
18573 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18574 See https://github.com/rust-lang/rust/issues/32925. */
18575 if (cu->language == language_rust && linkage_name != NULL
18576 && strchr (linkage_name, '{') != NULL)
18577 linkage_name = NULL;
18578 break;
18579 case DW_AT_low_pc:
18580 has_low_pc_attr = 1;
18581 lowpc = attr.value_as_address ();
18582 break;
18583 case DW_AT_high_pc:
18584 has_high_pc_attr = 1;
18585 highpc = attr.value_as_address ();
18586 if (cu->header.version >= 4 && attr.form_is_constant ())
18587 high_pc_relative = 1;
18588 break;
18589 case DW_AT_location:
18590 /* Support the .debug_loc offsets. */
18591 if (attr.form_is_block ())
18592 {
18593 d.locdesc = DW_BLOCK (&attr);
18594 }
18595 else if (attr.form_is_section_offset ())
18596 {
18597 dwarf2_complex_location_expr_complaint ();
18598 }
18599 else
18600 {
18601 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18602 "partial symbol information");
18603 }
18604 break;
18605 case DW_AT_external:
18606 is_external = DW_UNSND (&attr);
18607 break;
18608 case DW_AT_declaration:
18609 is_declaration = DW_UNSND (&attr);
18610 break;
18611 case DW_AT_type:
18612 has_type = 1;
18613 break;
18614 case DW_AT_abstract_origin:
18615 case DW_AT_specification:
18616 case DW_AT_extension:
18617 has_specification = 1;
18618 spec_offset = attr.get_ref_die_offset ();
18619 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18620 || cu->per_cu->is_dwz);
18621 break;
18622 case DW_AT_sibling:
18623 /* Ignore absolute siblings, they might point outside of
18624 the current compile unit. */
18625 if (attr.form == DW_FORM_ref_addr)
18626 complaint (_("ignoring absolute DW_AT_sibling"));
18627 else
18628 {
18629 const gdb_byte *buffer = reader->buffer;
18630 sect_offset off = attr.get_ref_die_offset ();
18631 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18632
18633 if (sibling_ptr < info_ptr)
18634 complaint (_("DW_AT_sibling points backwards"));
18635 else if (sibling_ptr > reader->buffer_end)
18636 reader->die_section->overflow_complaint ();
18637 else
18638 sibling = sibling_ptr;
18639 }
18640 break;
18641 case DW_AT_byte_size:
18642 has_byte_size = 1;
18643 break;
18644 case DW_AT_const_value:
18645 has_const_value = 1;
18646 break;
18647 case DW_AT_calling_convention:
18648 /* DWARF doesn't provide a way to identify a program's source-level
18649 entry point. DW_AT_calling_convention attributes are only meant
18650 to describe functions' calling conventions.
18651
18652 However, because it's a necessary piece of information in
18653 Fortran, and before DWARF 4 DW_CC_program was the only
18654 piece of debugging information whose definition refers to
18655 a 'main program' at all, several compilers marked Fortran
18656 main programs with DW_CC_program --- even when those
18657 functions use the standard calling conventions.
18658
18659 Although DWARF now specifies a way to provide this
18660 information, we support this practice for backward
18661 compatibility. */
18662 if (DW_UNSND (&attr) == DW_CC_program
18663 && cu->language == language_fortran)
18664 main_subprogram = 1;
18665 break;
18666 case DW_AT_inline:
18667 if (DW_UNSND (&attr) == DW_INL_inlined
18668 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18669 may_be_inlined = 1;
18670 break;
18671
18672 case DW_AT_import:
18673 if (tag == DW_TAG_imported_unit)
18674 {
18675 d.sect_off = attr.get_ref_die_offset ();
18676 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18677 || cu->per_cu->is_dwz);
18678 }
18679 break;
18680
18681 case DW_AT_main_subprogram:
18682 main_subprogram = DW_UNSND (&attr);
18683 break;
18684
18685 case DW_AT_ranges:
18686 {
18687 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18688 but that requires a full DIE, so instead we just
18689 reimplement it. */
18690 int need_ranges_base = tag != DW_TAG_compile_unit;
18691 unsigned int ranges_offset = (DW_UNSND (&attr)
18692 + (need_ranges_base
18693 ? cu->ranges_base
18694 : 0));
18695
18696 /* Value of the DW_AT_ranges attribute is the offset in the
18697 .debug_ranges section. */
18698 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18699 nullptr))
18700 has_pc_info = 1;
18701 }
18702 break;
18703
18704 default:
18705 break;
18706 }
18707 }
18708
18709 /* For Ada, if both the name and the linkage name appear, we prefer
18710 the latter. This lets "catch exception" work better, regardless
18711 of the order in which the name and linkage name were emitted.
18712 Really, though, this is just a workaround for the fact that gdb
18713 doesn't store both the name and the linkage name. */
18714 if (cu->language == language_ada && linkage_name != nullptr)
18715 raw_name = linkage_name;
18716
18717 if (high_pc_relative)
18718 highpc += lowpc;
18719
18720 if (has_low_pc_attr && has_high_pc_attr)
18721 {
18722 /* When using the GNU linker, .gnu.linkonce. sections are used to
18723 eliminate duplicate copies of functions and vtables and such.
18724 The linker will arbitrarily choose one and discard the others.
18725 The AT_*_pc values for such functions refer to local labels in
18726 these sections. If the section from that file was discarded, the
18727 labels are not in the output, so the relocs get a value of 0.
18728 If this is a discarded function, mark the pc bounds as invalid,
18729 so that GDB will ignore it. */
18730 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18731 {
18732 struct objfile *objfile = dwarf2_per_objfile->objfile;
18733 struct gdbarch *gdbarch = objfile->arch ();
18734
18735 complaint (_("DW_AT_low_pc %s is zero "
18736 "for DIE at %s [in module %s]"),
18737 paddress (gdbarch, lowpc),
18738 sect_offset_str (sect_off),
18739 objfile_name (objfile));
18740 }
18741 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18742 else if (lowpc >= highpc)
18743 {
18744 struct objfile *objfile = dwarf2_per_objfile->objfile;
18745 struct gdbarch *gdbarch = objfile->arch ();
18746
18747 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18748 "for DIE at %s [in module %s]"),
18749 paddress (gdbarch, lowpc),
18750 paddress (gdbarch, highpc),
18751 sect_offset_str (sect_off),
18752 objfile_name (objfile));
18753 }
18754 else
18755 has_pc_info = 1;
18756 }
18757
18758 return info_ptr;
18759 }
18760
18761 /* Find a cached partial DIE at OFFSET in CU. */
18762
18763 struct partial_die_info *
18764 dwarf2_cu::find_partial_die (sect_offset sect_off)
18765 {
18766 struct partial_die_info *lookup_die = NULL;
18767 struct partial_die_info part_die (sect_off);
18768
18769 lookup_die = ((struct partial_die_info *)
18770 htab_find_with_hash (partial_dies, &part_die,
18771 to_underlying (sect_off)));
18772
18773 return lookup_die;
18774 }
18775
18776 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18777 except in the case of .debug_types DIEs which do not reference
18778 outside their CU (they do however referencing other types via
18779 DW_FORM_ref_sig8). */
18780
18781 static const struct cu_partial_die_info
18782 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18783 {
18784 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18785 struct objfile *objfile = dwarf2_per_objfile->objfile;
18786 struct partial_die_info *pd = NULL;
18787
18788 if (offset_in_dwz == cu->per_cu->is_dwz
18789 && cu->header.offset_in_cu_p (sect_off))
18790 {
18791 pd = cu->find_partial_die (sect_off);
18792 if (pd != NULL)
18793 return { cu, pd };
18794 /* We missed recording what we needed.
18795 Load all dies and try again. */
18796 }
18797 else
18798 {
18799 /* TUs don't reference other CUs/TUs (except via type signatures). */
18800 if (cu->per_cu->is_debug_types)
18801 {
18802 error (_("Dwarf Error: Type Unit at offset %s contains"
18803 " external reference to offset %s [in module %s].\n"),
18804 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18805 bfd_get_filename (objfile->obfd));
18806 }
18807 dwarf2_per_cu_data *per_cu
18808 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18809 dwarf2_per_objfile);
18810
18811 cu = dwarf2_per_objfile->get_cu (per_cu);
18812 if (cu == NULL || cu->partial_dies == NULL)
18813 load_partial_comp_unit (per_cu, dwarf2_per_objfile, nullptr);
18814
18815 cu = dwarf2_per_objfile->get_cu (per_cu);
18816
18817 cu->last_used = 0;
18818 pd = cu->find_partial_die (sect_off);
18819 }
18820
18821 /* If we didn't find it, and not all dies have been loaded,
18822 load them all and try again. */
18823
18824 if (pd == NULL && cu->per_cu->load_all_dies == 0)
18825 {
18826 cu->per_cu->load_all_dies = 1;
18827
18828 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18829 THIS_CU->cu may already be in use. So we can't just free it and
18830 replace its DIEs with the ones we read in. Instead, we leave those
18831 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18832 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18833 set. */
18834 load_partial_comp_unit (cu->per_cu, dwarf2_per_objfile, cu);
18835
18836 pd = cu->find_partial_die (sect_off);
18837 }
18838
18839 if (pd == NULL)
18840 internal_error (__FILE__, __LINE__,
18841 _("could not find partial DIE %s "
18842 "in cache [from module %s]\n"),
18843 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18844 return { cu, pd };
18845 }
18846
18847 /* See if we can figure out if the class lives in a namespace. We do
18848 this by looking for a member function; its demangled name will
18849 contain namespace info, if there is any. */
18850
18851 static void
18852 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18853 struct dwarf2_cu *cu)
18854 {
18855 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18856 what template types look like, because the demangler
18857 frequently doesn't give the same name as the debug info. We
18858 could fix this by only using the demangled name to get the
18859 prefix (but see comment in read_structure_type). */
18860
18861 struct partial_die_info *real_pdi;
18862 struct partial_die_info *child_pdi;
18863
18864 /* If this DIE (this DIE's specification, if any) has a parent, then
18865 we should not do this. We'll prepend the parent's fully qualified
18866 name when we create the partial symbol. */
18867
18868 real_pdi = struct_pdi;
18869 while (real_pdi->has_specification)
18870 {
18871 auto res = find_partial_die (real_pdi->spec_offset,
18872 real_pdi->spec_is_dwz, cu);
18873 real_pdi = res.pdi;
18874 cu = res.cu;
18875 }
18876
18877 if (real_pdi->die_parent != NULL)
18878 return;
18879
18880 for (child_pdi = struct_pdi->die_child;
18881 child_pdi != NULL;
18882 child_pdi = child_pdi->die_sibling)
18883 {
18884 if (child_pdi->tag == DW_TAG_subprogram
18885 && child_pdi->linkage_name != NULL)
18886 {
18887 gdb::unique_xmalloc_ptr<char> actual_class_name
18888 (language_class_name_from_physname (cu->language_defn,
18889 child_pdi->linkage_name));
18890 if (actual_class_name != NULL)
18891 {
18892 struct objfile *objfile = cu->per_objfile->objfile;
18893 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
18894 struct_pdi->canonical_name = 1;
18895 }
18896 break;
18897 }
18898 }
18899 }
18900
18901 /* Return true if a DIE with TAG may have the DW_AT_const_value
18902 attribute. */
18903
18904 static bool
18905 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18906 {
18907 switch (tag)
18908 {
18909 case DW_TAG_constant:
18910 case DW_TAG_enumerator:
18911 case DW_TAG_formal_parameter:
18912 case DW_TAG_template_value_param:
18913 case DW_TAG_variable:
18914 return true;
18915 }
18916
18917 return false;
18918 }
18919
18920 void
18921 partial_die_info::fixup (struct dwarf2_cu *cu)
18922 {
18923 /* Once we've fixed up a die, there's no point in doing so again.
18924 This also avoids a memory leak if we were to call
18925 guess_partial_die_structure_name multiple times. */
18926 if (fixup_called)
18927 return;
18928
18929 /* If we found a reference attribute and the DIE has no name, try
18930 to find a name in the referred to DIE. */
18931
18932 if (raw_name == NULL && has_specification)
18933 {
18934 struct partial_die_info *spec_die;
18935
18936 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18937 spec_die = res.pdi;
18938 cu = res.cu;
18939
18940 spec_die->fixup (cu);
18941
18942 if (spec_die->raw_name)
18943 {
18944 raw_name = spec_die->raw_name;
18945 canonical_name = spec_die->canonical_name;
18946
18947 /* Copy DW_AT_external attribute if it is set. */
18948 if (spec_die->is_external)
18949 is_external = spec_die->is_external;
18950 }
18951 }
18952
18953 if (!has_const_value && has_specification
18954 && can_have_DW_AT_const_value_p (tag))
18955 {
18956 struct partial_die_info *spec_die;
18957
18958 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18959 spec_die = res.pdi;
18960 cu = res.cu;
18961
18962 spec_die->fixup (cu);
18963
18964 if (spec_die->has_const_value)
18965 {
18966 /* Copy DW_AT_const_value attribute if it is set. */
18967 has_const_value = spec_die->has_const_value;
18968 }
18969 }
18970
18971 /* Set default names for some unnamed DIEs. */
18972
18973 if (raw_name == NULL && tag == DW_TAG_namespace)
18974 {
18975 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
18976 canonical_name = 1;
18977 }
18978
18979 /* If there is no parent die to provide a namespace, and there are
18980 children, see if we can determine the namespace from their linkage
18981 name. */
18982 if (cu->language == language_cplus
18983 && !cu->per_objfile->per_bfd->types.empty ()
18984 && die_parent == NULL
18985 && has_children
18986 && (tag == DW_TAG_class_type
18987 || tag == DW_TAG_structure_type
18988 || tag == DW_TAG_union_type))
18989 guess_partial_die_structure_name (this, cu);
18990
18991 /* GCC might emit a nameless struct or union that has a linkage
18992 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18993 if (raw_name == NULL
18994 && (tag == DW_TAG_class_type
18995 || tag == DW_TAG_interface_type
18996 || tag == DW_TAG_structure_type
18997 || tag == DW_TAG_union_type)
18998 && linkage_name != NULL)
18999 {
19000 gdb::unique_xmalloc_ptr<char> demangled
19001 (gdb_demangle (linkage_name, DMGL_TYPES));
19002 if (demangled != nullptr)
19003 {
19004 const char *base;
19005
19006 /* Strip any leading namespaces/classes, keep only the base name.
19007 DW_AT_name for named DIEs does not contain the prefixes. */
19008 base = strrchr (demangled.get (), ':');
19009 if (base && base > demangled.get () && base[-1] == ':')
19010 base++;
19011 else
19012 base = demangled.get ();
19013
19014 struct objfile *objfile = cu->per_objfile->objfile;
19015 raw_name = objfile->intern (base);
19016 canonical_name = 1;
19017 }
19018 }
19019
19020 fixup_called = 1;
19021 }
19022
19023 /* Read the .debug_loclists header contents from the given SECTION in the
19024 HEADER. */
19025 static void
19026 read_loclist_header (struct loclist_header *header,
19027 struct dwarf2_section_info *section)
19028 {
19029 unsigned int bytes_read;
19030 bfd *abfd = section->get_bfd_owner ();
19031 const gdb_byte *info_ptr = section->buffer;
19032 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19033 info_ptr += bytes_read;
19034 header->version = read_2_bytes (abfd, info_ptr);
19035 info_ptr += 2;
19036 header->addr_size = read_1_byte (abfd, info_ptr);
19037 info_ptr += 1;
19038 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19039 info_ptr += 1;
19040 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19041 }
19042
19043 /* Return the DW_AT_loclists_base value for the CU. */
19044 static ULONGEST
19045 lookup_loclist_base (struct dwarf2_cu *cu)
19046 {
19047 /* For the .dwo unit, the loclist_base points to the first offset following
19048 the header. The header consists of the following entities-
19049 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19050 bit format)
19051 2. version (2 bytes)
19052 3. address size (1 byte)
19053 4. segment selector size (1 byte)
19054 5. offset entry count (4 bytes)
19055 These sizes are derived as per the DWARFv5 standard. */
19056 if (cu->dwo_unit != nullptr)
19057 {
19058 if (cu->header.initial_length_size == 4)
19059 return LOCLIST_HEADER_SIZE32;
19060 return LOCLIST_HEADER_SIZE64;
19061 }
19062 return cu->loclist_base;
19063 }
19064
19065 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19066 array of offsets in the .debug_loclists section. */
19067 static CORE_ADDR
19068 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19069 {
19070 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19071 struct objfile *objfile = dwarf2_per_objfile->objfile;
19072 bfd *abfd = objfile->obfd;
19073 ULONGEST loclist_base = lookup_loclist_base (cu);
19074 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19075
19076 section->read (objfile);
19077 if (section->buffer == NULL)
19078 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19079 "section [in module %s]"), objfile_name (objfile));
19080 struct loclist_header header;
19081 read_loclist_header (&header, section);
19082 if (loclist_index >= header.offset_entry_count)
19083 complaint (_("DW_FORM_loclistx pointing outside of "
19084 ".debug_loclists offset array [in module %s]"),
19085 objfile_name (objfile));
19086 if (loclist_base + loclist_index * cu->header.offset_size
19087 >= section->size)
19088 complaint (_("DW_FORM_loclistx pointing outside of "
19089 ".debug_loclists section [in module %s]"),
19090 objfile_name (objfile));
19091 const gdb_byte *info_ptr
19092 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19093
19094 if (cu->header.offset_size == 4)
19095 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19096 else
19097 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19098 }
19099
19100 /* Process the attributes that had to be skipped in the first round. These
19101 attributes are the ones that need str_offsets_base or addr_base attributes.
19102 They could not have been processed in the first round, because at the time
19103 the values of str_offsets_base or addr_base may not have been known. */
19104 static void
19105 read_attribute_reprocess (const struct die_reader_specs *reader,
19106 struct attribute *attr)
19107 {
19108 struct dwarf2_cu *cu = reader->cu;
19109 switch (attr->form)
19110 {
19111 case DW_FORM_addrx:
19112 case DW_FORM_GNU_addr_index:
19113 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19114 break;
19115 case DW_FORM_loclistx:
19116 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19117 break;
19118 case DW_FORM_strx:
19119 case DW_FORM_strx1:
19120 case DW_FORM_strx2:
19121 case DW_FORM_strx3:
19122 case DW_FORM_strx4:
19123 case DW_FORM_GNU_str_index:
19124 {
19125 unsigned int str_index = DW_UNSND (attr);
19126 if (reader->dwo_file != NULL)
19127 {
19128 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19129 DW_STRING_IS_CANONICAL (attr) = 0;
19130 }
19131 else
19132 {
19133 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19134 DW_STRING_IS_CANONICAL (attr) = 0;
19135 }
19136 break;
19137 }
19138 default:
19139 gdb_assert_not_reached (_("Unexpected DWARF form."));
19140 }
19141 }
19142
19143 /* Read an attribute value described by an attribute form. */
19144
19145 static const gdb_byte *
19146 read_attribute_value (const struct die_reader_specs *reader,
19147 struct attribute *attr, unsigned form,
19148 LONGEST implicit_const, const gdb_byte *info_ptr,
19149 bool *need_reprocess)
19150 {
19151 struct dwarf2_cu *cu = reader->cu;
19152 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19153 struct objfile *objfile = dwarf2_per_objfile->objfile;
19154 bfd *abfd = reader->abfd;
19155 struct comp_unit_head *cu_header = &cu->header;
19156 unsigned int bytes_read;
19157 struct dwarf_block *blk;
19158 *need_reprocess = false;
19159
19160 attr->form = (enum dwarf_form) form;
19161 switch (form)
19162 {
19163 case DW_FORM_ref_addr:
19164 if (cu->header.version == 2)
19165 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19166 &bytes_read);
19167 else
19168 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19169 &bytes_read);
19170 info_ptr += bytes_read;
19171 break;
19172 case DW_FORM_GNU_ref_alt:
19173 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19174 info_ptr += bytes_read;
19175 break;
19176 case DW_FORM_addr:
19177 {
19178 struct gdbarch *gdbarch = objfile->arch ();
19179 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19180 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19181 info_ptr += bytes_read;
19182 }
19183 break;
19184 case DW_FORM_block2:
19185 blk = dwarf_alloc_block (cu);
19186 blk->size = read_2_bytes (abfd, info_ptr);
19187 info_ptr += 2;
19188 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19189 info_ptr += blk->size;
19190 DW_BLOCK (attr) = blk;
19191 break;
19192 case DW_FORM_block4:
19193 blk = dwarf_alloc_block (cu);
19194 blk->size = read_4_bytes (abfd, info_ptr);
19195 info_ptr += 4;
19196 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19197 info_ptr += blk->size;
19198 DW_BLOCK (attr) = blk;
19199 break;
19200 case DW_FORM_data2:
19201 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19202 info_ptr += 2;
19203 break;
19204 case DW_FORM_data4:
19205 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19206 info_ptr += 4;
19207 break;
19208 case DW_FORM_data8:
19209 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19210 info_ptr += 8;
19211 break;
19212 case DW_FORM_data16:
19213 blk = dwarf_alloc_block (cu);
19214 blk->size = 16;
19215 blk->data = read_n_bytes (abfd, info_ptr, 16);
19216 info_ptr += 16;
19217 DW_BLOCK (attr) = blk;
19218 break;
19219 case DW_FORM_sec_offset:
19220 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19221 info_ptr += bytes_read;
19222 break;
19223 case DW_FORM_loclistx:
19224 {
19225 *need_reprocess = true;
19226 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19227 info_ptr += bytes_read;
19228 }
19229 break;
19230 case DW_FORM_string:
19231 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19232 DW_STRING_IS_CANONICAL (attr) = 0;
19233 info_ptr += bytes_read;
19234 break;
19235 case DW_FORM_strp:
19236 if (!cu->per_cu->is_dwz)
19237 {
19238 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19239 abfd, info_ptr, cu_header,
19240 &bytes_read);
19241 DW_STRING_IS_CANONICAL (attr) = 0;
19242 info_ptr += bytes_read;
19243 break;
19244 }
19245 /* FALLTHROUGH */
19246 case DW_FORM_line_strp:
19247 if (!cu->per_cu->is_dwz)
19248 {
19249 DW_STRING (attr)
19250 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19251 &bytes_read);
19252 DW_STRING_IS_CANONICAL (attr) = 0;
19253 info_ptr += bytes_read;
19254 break;
19255 }
19256 /* FALLTHROUGH */
19257 case DW_FORM_GNU_strp_alt:
19258 {
19259 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19260 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19261 &bytes_read);
19262
19263 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19264 DW_STRING_IS_CANONICAL (attr) = 0;
19265 info_ptr += bytes_read;
19266 }
19267 break;
19268 case DW_FORM_exprloc:
19269 case DW_FORM_block:
19270 blk = dwarf_alloc_block (cu);
19271 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19272 info_ptr += bytes_read;
19273 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19274 info_ptr += blk->size;
19275 DW_BLOCK (attr) = blk;
19276 break;
19277 case DW_FORM_block1:
19278 blk = dwarf_alloc_block (cu);
19279 blk->size = read_1_byte (abfd, info_ptr);
19280 info_ptr += 1;
19281 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19282 info_ptr += blk->size;
19283 DW_BLOCK (attr) = blk;
19284 break;
19285 case DW_FORM_data1:
19286 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19287 info_ptr += 1;
19288 break;
19289 case DW_FORM_flag:
19290 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19291 info_ptr += 1;
19292 break;
19293 case DW_FORM_flag_present:
19294 DW_UNSND (attr) = 1;
19295 break;
19296 case DW_FORM_sdata:
19297 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19298 info_ptr += bytes_read;
19299 break;
19300 case DW_FORM_udata:
19301 case DW_FORM_rnglistx:
19302 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19303 info_ptr += bytes_read;
19304 break;
19305 case DW_FORM_ref1:
19306 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19307 + read_1_byte (abfd, info_ptr));
19308 info_ptr += 1;
19309 break;
19310 case DW_FORM_ref2:
19311 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19312 + read_2_bytes (abfd, info_ptr));
19313 info_ptr += 2;
19314 break;
19315 case DW_FORM_ref4:
19316 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19317 + read_4_bytes (abfd, info_ptr));
19318 info_ptr += 4;
19319 break;
19320 case DW_FORM_ref8:
19321 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19322 + read_8_bytes (abfd, info_ptr));
19323 info_ptr += 8;
19324 break;
19325 case DW_FORM_ref_sig8:
19326 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19327 info_ptr += 8;
19328 break;
19329 case DW_FORM_ref_udata:
19330 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19331 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19332 info_ptr += bytes_read;
19333 break;
19334 case DW_FORM_indirect:
19335 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19336 info_ptr += bytes_read;
19337 if (form == DW_FORM_implicit_const)
19338 {
19339 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19340 info_ptr += bytes_read;
19341 }
19342 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19343 info_ptr, need_reprocess);
19344 break;
19345 case DW_FORM_implicit_const:
19346 DW_SND (attr) = implicit_const;
19347 break;
19348 case DW_FORM_addrx:
19349 case DW_FORM_GNU_addr_index:
19350 *need_reprocess = true;
19351 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19352 info_ptr += bytes_read;
19353 break;
19354 case DW_FORM_strx:
19355 case DW_FORM_strx1:
19356 case DW_FORM_strx2:
19357 case DW_FORM_strx3:
19358 case DW_FORM_strx4:
19359 case DW_FORM_GNU_str_index:
19360 {
19361 ULONGEST str_index;
19362 if (form == DW_FORM_strx1)
19363 {
19364 str_index = read_1_byte (abfd, info_ptr);
19365 info_ptr += 1;
19366 }
19367 else if (form == DW_FORM_strx2)
19368 {
19369 str_index = read_2_bytes (abfd, info_ptr);
19370 info_ptr += 2;
19371 }
19372 else if (form == DW_FORM_strx3)
19373 {
19374 str_index = read_3_bytes (abfd, info_ptr);
19375 info_ptr += 3;
19376 }
19377 else if (form == DW_FORM_strx4)
19378 {
19379 str_index = read_4_bytes (abfd, info_ptr);
19380 info_ptr += 4;
19381 }
19382 else
19383 {
19384 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19385 info_ptr += bytes_read;
19386 }
19387 *need_reprocess = true;
19388 DW_UNSND (attr) = str_index;
19389 }
19390 break;
19391 default:
19392 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19393 dwarf_form_name (form),
19394 bfd_get_filename (abfd));
19395 }
19396
19397 /* Super hack. */
19398 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19399 attr->form = DW_FORM_GNU_ref_alt;
19400
19401 /* We have seen instances where the compiler tried to emit a byte
19402 size attribute of -1 which ended up being encoded as an unsigned
19403 0xffffffff. Although 0xffffffff is technically a valid size value,
19404 an object of this size seems pretty unlikely so we can relatively
19405 safely treat these cases as if the size attribute was invalid and
19406 treat them as zero by default. */
19407 if (attr->name == DW_AT_byte_size
19408 && form == DW_FORM_data4
19409 && DW_UNSND (attr) >= 0xffffffff)
19410 {
19411 complaint
19412 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19413 hex_string (DW_UNSND (attr)));
19414 DW_UNSND (attr) = 0;
19415 }
19416
19417 return info_ptr;
19418 }
19419
19420 /* Read an attribute described by an abbreviated attribute. */
19421
19422 static const gdb_byte *
19423 read_attribute (const struct die_reader_specs *reader,
19424 struct attribute *attr, struct attr_abbrev *abbrev,
19425 const gdb_byte *info_ptr, bool *need_reprocess)
19426 {
19427 attr->name = abbrev->name;
19428 return read_attribute_value (reader, attr, abbrev->form,
19429 abbrev->implicit_const, info_ptr,
19430 need_reprocess);
19431 }
19432
19433 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19434
19435 static const char *
19436 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19437 LONGEST str_offset)
19438 {
19439 return dwarf2_per_objfile->per_bfd->str.read_string
19440 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19441 }
19442
19443 /* Return pointer to string at .debug_str offset as read from BUF.
19444 BUF is assumed to be in a compilation unit described by CU_HEADER.
19445 Return *BYTES_READ_PTR count of bytes read from BUF. */
19446
19447 static const char *
19448 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19449 const gdb_byte *buf,
19450 const struct comp_unit_head *cu_header,
19451 unsigned int *bytes_read_ptr)
19452 {
19453 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19454
19455 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19456 }
19457
19458 /* See read.h. */
19459
19460 const char *
19461 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19462 const struct comp_unit_head *cu_header,
19463 unsigned int *bytes_read_ptr)
19464 {
19465 bfd *abfd = objfile->obfd;
19466 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19467
19468 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19469 }
19470
19471 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19472 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19473 ADDR_SIZE is the size of addresses from the CU header. */
19474
19475 static CORE_ADDR
19476 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19477 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19478 int addr_size)
19479 {
19480 struct objfile *objfile = dwarf2_per_objfile->objfile;
19481 bfd *abfd = objfile->obfd;
19482 const gdb_byte *info_ptr;
19483 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19484
19485 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19486 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19487 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19488 objfile_name (objfile));
19489 if (addr_base_or_zero + addr_index * addr_size
19490 >= dwarf2_per_objfile->per_bfd->addr.size)
19491 error (_("DW_FORM_addr_index pointing outside of "
19492 ".debug_addr section [in module %s]"),
19493 objfile_name (objfile));
19494 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19495 + addr_base_or_zero + addr_index * addr_size);
19496 if (addr_size == 4)
19497 return bfd_get_32 (abfd, info_ptr);
19498 else
19499 return bfd_get_64 (abfd, info_ptr);
19500 }
19501
19502 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19503
19504 static CORE_ADDR
19505 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19506 {
19507 return read_addr_index_1 (cu->per_objfile, addr_index,
19508 cu->addr_base, cu->header.addr_size);
19509 }
19510
19511 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19512
19513 static CORE_ADDR
19514 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19515 unsigned int *bytes_read)
19516 {
19517 bfd *abfd = cu->per_objfile->objfile->obfd;
19518 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19519
19520 return read_addr_index (cu, addr_index);
19521 }
19522
19523 /* See read.h. */
19524
19525 CORE_ADDR
19526 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19527 dwarf2_per_objfile *dwarf2_per_objfile,
19528 unsigned int addr_index)
19529 {
19530 struct dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
19531 gdb::optional<ULONGEST> addr_base;
19532 int addr_size;
19533
19534 /* We need addr_base and addr_size.
19535 If we don't have PER_CU->cu, we have to get it.
19536 Nasty, but the alternative is storing the needed info in PER_CU,
19537 which at this point doesn't seem justified: it's not clear how frequently
19538 it would get used and it would increase the size of every PER_CU.
19539 Entry points like dwarf2_per_cu_addr_size do a similar thing
19540 so we're not in uncharted territory here.
19541 Alas we need to be a bit more complicated as addr_base is contained
19542 in the DIE.
19543
19544 We don't need to read the entire CU(/TU).
19545 We just need the header and top level die.
19546
19547 IWBN to use the aging mechanism to let us lazily later discard the CU.
19548 For now we skip this optimization. */
19549
19550 if (cu != NULL)
19551 {
19552 addr_base = cu->addr_base;
19553 addr_size = cu->header.addr_size;
19554 }
19555 else
19556 {
19557 cutu_reader reader (per_cu, dwarf2_per_objfile, nullptr, nullptr, false);
19558 addr_base = reader.cu->addr_base;
19559 addr_size = reader.cu->header.addr_size;
19560 }
19561
19562 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19563 addr_size);
19564 }
19565
19566 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19567 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19568 DWO file. */
19569
19570 static const char *
19571 read_str_index (struct dwarf2_cu *cu,
19572 struct dwarf2_section_info *str_section,
19573 struct dwarf2_section_info *str_offsets_section,
19574 ULONGEST str_offsets_base, ULONGEST str_index)
19575 {
19576 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19577 struct objfile *objfile = dwarf2_per_objfile->objfile;
19578 const char *objf_name = objfile_name (objfile);
19579 bfd *abfd = objfile->obfd;
19580 const gdb_byte *info_ptr;
19581 ULONGEST str_offset;
19582 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19583
19584 str_section->read (objfile);
19585 str_offsets_section->read (objfile);
19586 if (str_section->buffer == NULL)
19587 error (_("%s used without %s section"
19588 " in CU at offset %s [in module %s]"),
19589 form_name, str_section->get_name (),
19590 sect_offset_str (cu->header.sect_off), objf_name);
19591 if (str_offsets_section->buffer == NULL)
19592 error (_("%s used without %s section"
19593 " in CU at offset %s [in module %s]"),
19594 form_name, str_section->get_name (),
19595 sect_offset_str (cu->header.sect_off), objf_name);
19596 info_ptr = (str_offsets_section->buffer
19597 + str_offsets_base
19598 + str_index * cu->header.offset_size);
19599 if (cu->header.offset_size == 4)
19600 str_offset = bfd_get_32 (abfd, info_ptr);
19601 else
19602 str_offset = bfd_get_64 (abfd, info_ptr);
19603 if (str_offset >= str_section->size)
19604 error (_("Offset from %s pointing outside of"
19605 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19606 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19607 return (const char *) (str_section->buffer + str_offset);
19608 }
19609
19610 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19611
19612 static const char *
19613 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19614 {
19615 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19616 ? reader->cu->header.addr_size : 0;
19617 return read_str_index (reader->cu,
19618 &reader->dwo_file->sections.str,
19619 &reader->dwo_file->sections.str_offsets,
19620 str_offsets_base, str_index);
19621 }
19622
19623 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19624
19625 static const char *
19626 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19627 {
19628 struct objfile *objfile = cu->per_objfile->objfile;
19629 const char *objf_name = objfile_name (objfile);
19630 static const char form_name[] = "DW_FORM_GNU_str_index";
19631 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19632
19633 if (!cu->str_offsets_base.has_value ())
19634 error (_("%s used in Fission stub without %s"
19635 " in CU at offset 0x%lx [in module %s]"),
19636 form_name, str_offsets_attr_name,
19637 (long) cu->header.offset_size, objf_name);
19638
19639 return read_str_index (cu,
19640 &cu->per_objfile->per_bfd->str,
19641 &cu->per_objfile->per_bfd->str_offsets,
19642 *cu->str_offsets_base, str_index);
19643 }
19644
19645 /* Return the length of an LEB128 number in BUF. */
19646
19647 static int
19648 leb128_size (const gdb_byte *buf)
19649 {
19650 const gdb_byte *begin = buf;
19651 gdb_byte byte;
19652
19653 while (1)
19654 {
19655 byte = *buf++;
19656 if ((byte & 128) == 0)
19657 return buf - begin;
19658 }
19659 }
19660
19661 static void
19662 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19663 {
19664 switch (lang)
19665 {
19666 case DW_LANG_C89:
19667 case DW_LANG_C99:
19668 case DW_LANG_C11:
19669 case DW_LANG_C:
19670 case DW_LANG_UPC:
19671 cu->language = language_c;
19672 break;
19673 case DW_LANG_Java:
19674 case DW_LANG_C_plus_plus:
19675 case DW_LANG_C_plus_plus_11:
19676 case DW_LANG_C_plus_plus_14:
19677 cu->language = language_cplus;
19678 break;
19679 case DW_LANG_D:
19680 cu->language = language_d;
19681 break;
19682 case DW_LANG_Fortran77:
19683 case DW_LANG_Fortran90:
19684 case DW_LANG_Fortran95:
19685 case DW_LANG_Fortran03:
19686 case DW_LANG_Fortran08:
19687 cu->language = language_fortran;
19688 break;
19689 case DW_LANG_Go:
19690 cu->language = language_go;
19691 break;
19692 case DW_LANG_Mips_Assembler:
19693 cu->language = language_asm;
19694 break;
19695 case DW_LANG_Ada83:
19696 case DW_LANG_Ada95:
19697 cu->language = language_ada;
19698 break;
19699 case DW_LANG_Modula2:
19700 cu->language = language_m2;
19701 break;
19702 case DW_LANG_Pascal83:
19703 cu->language = language_pascal;
19704 break;
19705 case DW_LANG_ObjC:
19706 cu->language = language_objc;
19707 break;
19708 case DW_LANG_Rust:
19709 case DW_LANG_Rust_old:
19710 cu->language = language_rust;
19711 break;
19712 case DW_LANG_Cobol74:
19713 case DW_LANG_Cobol85:
19714 default:
19715 cu->language = language_minimal;
19716 break;
19717 }
19718 cu->language_defn = language_def (cu->language);
19719 }
19720
19721 /* Return the named attribute or NULL if not there. */
19722
19723 static struct attribute *
19724 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19725 {
19726 for (;;)
19727 {
19728 unsigned int i;
19729 struct attribute *spec = NULL;
19730
19731 for (i = 0; i < die->num_attrs; ++i)
19732 {
19733 if (die->attrs[i].name == name)
19734 return &die->attrs[i];
19735 if (die->attrs[i].name == DW_AT_specification
19736 || die->attrs[i].name == DW_AT_abstract_origin)
19737 spec = &die->attrs[i];
19738 }
19739
19740 if (!spec)
19741 break;
19742
19743 die = follow_die_ref (die, spec, &cu);
19744 }
19745
19746 return NULL;
19747 }
19748
19749 /* Return the string associated with a string-typed attribute, or NULL if it
19750 is either not found or is of an incorrect type. */
19751
19752 static const char *
19753 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19754 {
19755 struct attribute *attr;
19756 const char *str = NULL;
19757
19758 attr = dwarf2_attr (die, name, cu);
19759
19760 if (attr != NULL)
19761 {
19762 str = attr->value_as_string ();
19763 if (str == nullptr)
19764 complaint (_("string type expected for attribute %s for "
19765 "DIE at %s in module %s"),
19766 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19767 objfile_name (cu->per_objfile->objfile));
19768 }
19769
19770 return str;
19771 }
19772
19773 /* Return the dwo name or NULL if not present. If present, it is in either
19774 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19775 static const char *
19776 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19777 {
19778 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19779 if (dwo_name == nullptr)
19780 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19781 return dwo_name;
19782 }
19783
19784 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19785 and holds a non-zero value. This function should only be used for
19786 DW_FORM_flag or DW_FORM_flag_present attributes. */
19787
19788 static int
19789 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19790 {
19791 struct attribute *attr = dwarf2_attr (die, name, cu);
19792
19793 return (attr && DW_UNSND (attr));
19794 }
19795
19796 static int
19797 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19798 {
19799 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19800 which value is non-zero. However, we have to be careful with
19801 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19802 (via dwarf2_flag_true_p) follows this attribute. So we may
19803 end up accidently finding a declaration attribute that belongs
19804 to a different DIE referenced by the specification attribute,
19805 even though the given DIE does not have a declaration attribute. */
19806 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19807 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19808 }
19809
19810 /* Return the die giving the specification for DIE, if there is
19811 one. *SPEC_CU is the CU containing DIE on input, and the CU
19812 containing the return value on output. If there is no
19813 specification, but there is an abstract origin, that is
19814 returned. */
19815
19816 static struct die_info *
19817 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19818 {
19819 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19820 *spec_cu);
19821
19822 if (spec_attr == NULL)
19823 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19824
19825 if (spec_attr == NULL)
19826 return NULL;
19827 else
19828 return follow_die_ref (die, spec_attr, spec_cu);
19829 }
19830
19831 /* Stub for free_line_header to match void * callback types. */
19832
19833 static void
19834 free_line_header_voidp (void *arg)
19835 {
19836 struct line_header *lh = (struct line_header *) arg;
19837
19838 delete lh;
19839 }
19840
19841 /* A convenience function to find the proper .debug_line section for a CU. */
19842
19843 static struct dwarf2_section_info *
19844 get_debug_line_section (struct dwarf2_cu *cu)
19845 {
19846 struct dwarf2_section_info *section;
19847 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19848
19849 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19850 DWO file. */
19851 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19852 section = &cu->dwo_unit->dwo_file->sections.line;
19853 else if (cu->per_cu->is_dwz)
19854 {
19855 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19856
19857 section = &dwz->line;
19858 }
19859 else
19860 section = &dwarf2_per_objfile->per_bfd->line;
19861
19862 return section;
19863 }
19864
19865 /* Read the statement program header starting at OFFSET in
19866 .debug_line, or .debug_line.dwo. Return a pointer
19867 to a struct line_header, allocated using xmalloc.
19868 Returns NULL if there is a problem reading the header, e.g., if it
19869 has a version we don't understand.
19870
19871 NOTE: the strings in the include directory and file name tables of
19872 the returned object point into the dwarf line section buffer,
19873 and must not be freed. */
19874
19875 static line_header_up
19876 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19877 {
19878 struct dwarf2_section_info *section;
19879 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19880
19881 section = get_debug_line_section (cu);
19882 section->read (dwarf2_per_objfile->objfile);
19883 if (section->buffer == NULL)
19884 {
19885 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19886 complaint (_("missing .debug_line.dwo section"));
19887 else
19888 complaint (_("missing .debug_line section"));
19889 return 0;
19890 }
19891
19892 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19893 dwarf2_per_objfile, section,
19894 &cu->header);
19895 }
19896
19897 /* Subroutine of dwarf_decode_lines to simplify it.
19898 Return the file name of the psymtab for the given file_entry.
19899 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19900 If space for the result is malloc'd, *NAME_HOLDER will be set.
19901 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19902
19903 static const char *
19904 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19905 const dwarf2_psymtab *pst,
19906 const char *comp_dir,
19907 gdb::unique_xmalloc_ptr<char> *name_holder)
19908 {
19909 const char *include_name = fe.name;
19910 const char *include_name_to_compare = include_name;
19911 const char *pst_filename;
19912 int file_is_pst;
19913
19914 const char *dir_name = fe.include_dir (lh);
19915
19916 gdb::unique_xmalloc_ptr<char> hold_compare;
19917 if (!IS_ABSOLUTE_PATH (include_name)
19918 && (dir_name != NULL || comp_dir != NULL))
19919 {
19920 /* Avoid creating a duplicate psymtab for PST.
19921 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19922 Before we do the comparison, however, we need to account
19923 for DIR_NAME and COMP_DIR.
19924 First prepend dir_name (if non-NULL). If we still don't
19925 have an absolute path prepend comp_dir (if non-NULL).
19926 However, the directory we record in the include-file's
19927 psymtab does not contain COMP_DIR (to match the
19928 corresponding symtab(s)).
19929
19930 Example:
19931
19932 bash$ cd /tmp
19933 bash$ gcc -g ./hello.c
19934 include_name = "hello.c"
19935 dir_name = "."
19936 DW_AT_comp_dir = comp_dir = "/tmp"
19937 DW_AT_name = "./hello.c"
19938
19939 */
19940
19941 if (dir_name != NULL)
19942 {
19943 name_holder->reset (concat (dir_name, SLASH_STRING,
19944 include_name, (char *) NULL));
19945 include_name = name_holder->get ();
19946 include_name_to_compare = include_name;
19947 }
19948 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19949 {
19950 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19951 include_name, (char *) NULL));
19952 include_name_to_compare = hold_compare.get ();
19953 }
19954 }
19955
19956 pst_filename = pst->filename;
19957 gdb::unique_xmalloc_ptr<char> copied_name;
19958 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19959 {
19960 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19961 pst_filename, (char *) NULL));
19962 pst_filename = copied_name.get ();
19963 }
19964
19965 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19966
19967 if (file_is_pst)
19968 return NULL;
19969 return include_name;
19970 }
19971
19972 /* State machine to track the state of the line number program. */
19973
19974 class lnp_state_machine
19975 {
19976 public:
19977 /* Initialize a machine state for the start of a line number
19978 program. */
19979 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19980 bool record_lines_p);
19981
19982 file_entry *current_file ()
19983 {
19984 /* lh->file_names is 0-based, but the file name numbers in the
19985 statement program are 1-based. */
19986 return m_line_header->file_name_at (m_file);
19987 }
19988
19989 /* Record the line in the state machine. END_SEQUENCE is true if
19990 we're processing the end of a sequence. */
19991 void record_line (bool end_sequence);
19992
19993 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19994 nop-out rest of the lines in this sequence. */
19995 void check_line_address (struct dwarf2_cu *cu,
19996 const gdb_byte *line_ptr,
19997 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19998
19999 void handle_set_discriminator (unsigned int discriminator)
20000 {
20001 m_discriminator = discriminator;
20002 m_line_has_non_zero_discriminator |= discriminator != 0;
20003 }
20004
20005 /* Handle DW_LNE_set_address. */
20006 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20007 {
20008 m_op_index = 0;
20009 address += baseaddr;
20010 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20011 }
20012
20013 /* Handle DW_LNS_advance_pc. */
20014 void handle_advance_pc (CORE_ADDR adjust);
20015
20016 /* Handle a special opcode. */
20017 void handle_special_opcode (unsigned char op_code);
20018
20019 /* Handle DW_LNS_advance_line. */
20020 void handle_advance_line (int line_delta)
20021 {
20022 advance_line (line_delta);
20023 }
20024
20025 /* Handle DW_LNS_set_file. */
20026 void handle_set_file (file_name_index file);
20027
20028 /* Handle DW_LNS_negate_stmt. */
20029 void handle_negate_stmt ()
20030 {
20031 m_is_stmt = !m_is_stmt;
20032 }
20033
20034 /* Handle DW_LNS_const_add_pc. */
20035 void handle_const_add_pc ();
20036
20037 /* Handle DW_LNS_fixed_advance_pc. */
20038 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20039 {
20040 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20041 m_op_index = 0;
20042 }
20043
20044 /* Handle DW_LNS_copy. */
20045 void handle_copy ()
20046 {
20047 record_line (false);
20048 m_discriminator = 0;
20049 }
20050
20051 /* Handle DW_LNE_end_sequence. */
20052 void handle_end_sequence ()
20053 {
20054 m_currently_recording_lines = true;
20055 }
20056
20057 private:
20058 /* Advance the line by LINE_DELTA. */
20059 void advance_line (int line_delta)
20060 {
20061 m_line += line_delta;
20062
20063 if (line_delta != 0)
20064 m_line_has_non_zero_discriminator = m_discriminator != 0;
20065 }
20066
20067 struct dwarf2_cu *m_cu;
20068
20069 gdbarch *m_gdbarch;
20070
20071 /* True if we're recording lines.
20072 Otherwise we're building partial symtabs and are just interested in
20073 finding include files mentioned by the line number program. */
20074 bool m_record_lines_p;
20075
20076 /* The line number header. */
20077 line_header *m_line_header;
20078
20079 /* These are part of the standard DWARF line number state machine,
20080 and initialized according to the DWARF spec. */
20081
20082 unsigned char m_op_index = 0;
20083 /* The line table index of the current file. */
20084 file_name_index m_file = 1;
20085 unsigned int m_line = 1;
20086
20087 /* These are initialized in the constructor. */
20088
20089 CORE_ADDR m_address;
20090 bool m_is_stmt;
20091 unsigned int m_discriminator;
20092
20093 /* Additional bits of state we need to track. */
20094
20095 /* The last file that we called dwarf2_start_subfile for.
20096 This is only used for TLLs. */
20097 unsigned int m_last_file = 0;
20098 /* The last file a line number was recorded for. */
20099 struct subfile *m_last_subfile = NULL;
20100
20101 /* When true, record the lines we decode. */
20102 bool m_currently_recording_lines = false;
20103
20104 /* The last line number that was recorded, used to coalesce
20105 consecutive entries for the same line. This can happen, for
20106 example, when discriminators are present. PR 17276. */
20107 unsigned int m_last_line = 0;
20108 bool m_line_has_non_zero_discriminator = false;
20109 };
20110
20111 void
20112 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20113 {
20114 CORE_ADDR addr_adj = (((m_op_index + adjust)
20115 / m_line_header->maximum_ops_per_instruction)
20116 * m_line_header->minimum_instruction_length);
20117 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20118 m_op_index = ((m_op_index + adjust)
20119 % m_line_header->maximum_ops_per_instruction);
20120 }
20121
20122 void
20123 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20124 {
20125 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20126 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20127 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20128 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20129 / m_line_header->maximum_ops_per_instruction)
20130 * m_line_header->minimum_instruction_length);
20131 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20132 m_op_index = ((m_op_index + adj_opcode_d)
20133 % m_line_header->maximum_ops_per_instruction);
20134
20135 int line_delta = m_line_header->line_base + adj_opcode_r;
20136 advance_line (line_delta);
20137 record_line (false);
20138 m_discriminator = 0;
20139 }
20140
20141 void
20142 lnp_state_machine::handle_set_file (file_name_index file)
20143 {
20144 m_file = file;
20145
20146 const file_entry *fe = current_file ();
20147 if (fe == NULL)
20148 dwarf2_debug_line_missing_file_complaint ();
20149 else if (m_record_lines_p)
20150 {
20151 const char *dir = fe->include_dir (m_line_header);
20152
20153 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20154 m_line_has_non_zero_discriminator = m_discriminator != 0;
20155 dwarf2_start_subfile (m_cu, fe->name, dir);
20156 }
20157 }
20158
20159 void
20160 lnp_state_machine::handle_const_add_pc ()
20161 {
20162 CORE_ADDR adjust
20163 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20164
20165 CORE_ADDR addr_adj
20166 = (((m_op_index + adjust)
20167 / m_line_header->maximum_ops_per_instruction)
20168 * m_line_header->minimum_instruction_length);
20169
20170 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20171 m_op_index = ((m_op_index + adjust)
20172 % m_line_header->maximum_ops_per_instruction);
20173 }
20174
20175 /* Return non-zero if we should add LINE to the line number table.
20176 LINE is the line to add, LAST_LINE is the last line that was added,
20177 LAST_SUBFILE is the subfile for LAST_LINE.
20178 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20179 had a non-zero discriminator.
20180
20181 We have to be careful in the presence of discriminators.
20182 E.g., for this line:
20183
20184 for (i = 0; i < 100000; i++);
20185
20186 clang can emit four line number entries for that one line,
20187 each with a different discriminator.
20188 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20189
20190 However, we want gdb to coalesce all four entries into one.
20191 Otherwise the user could stepi into the middle of the line and
20192 gdb would get confused about whether the pc really was in the
20193 middle of the line.
20194
20195 Things are further complicated by the fact that two consecutive
20196 line number entries for the same line is a heuristic used by gcc
20197 to denote the end of the prologue. So we can't just discard duplicate
20198 entries, we have to be selective about it. The heuristic we use is
20199 that we only collapse consecutive entries for the same line if at least
20200 one of those entries has a non-zero discriminator. PR 17276.
20201
20202 Note: Addresses in the line number state machine can never go backwards
20203 within one sequence, thus this coalescing is ok. */
20204
20205 static int
20206 dwarf_record_line_p (struct dwarf2_cu *cu,
20207 unsigned int line, unsigned int last_line,
20208 int line_has_non_zero_discriminator,
20209 struct subfile *last_subfile)
20210 {
20211 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20212 return 1;
20213 if (line != last_line)
20214 return 1;
20215 /* Same line for the same file that we've seen already.
20216 As a last check, for pr 17276, only record the line if the line
20217 has never had a non-zero discriminator. */
20218 if (!line_has_non_zero_discriminator)
20219 return 1;
20220 return 0;
20221 }
20222
20223 /* Use the CU's builder to record line number LINE beginning at
20224 address ADDRESS in the line table of subfile SUBFILE. */
20225
20226 static void
20227 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20228 unsigned int line, CORE_ADDR address, bool is_stmt,
20229 struct dwarf2_cu *cu)
20230 {
20231 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20232
20233 if (dwarf_line_debug)
20234 {
20235 fprintf_unfiltered (gdb_stdlog,
20236 "Recording line %u, file %s, address %s\n",
20237 line, lbasename (subfile->name),
20238 paddress (gdbarch, address));
20239 }
20240
20241 if (cu != nullptr)
20242 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20243 }
20244
20245 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20246 Mark the end of a set of line number records.
20247 The arguments are the same as for dwarf_record_line_1.
20248 If SUBFILE is NULL the request is ignored. */
20249
20250 static void
20251 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20252 CORE_ADDR address, struct dwarf2_cu *cu)
20253 {
20254 if (subfile == NULL)
20255 return;
20256
20257 if (dwarf_line_debug)
20258 {
20259 fprintf_unfiltered (gdb_stdlog,
20260 "Finishing current line, file %s, address %s\n",
20261 lbasename (subfile->name),
20262 paddress (gdbarch, address));
20263 }
20264
20265 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20266 }
20267
20268 void
20269 lnp_state_machine::record_line (bool end_sequence)
20270 {
20271 if (dwarf_line_debug)
20272 {
20273 fprintf_unfiltered (gdb_stdlog,
20274 "Processing actual line %u: file %u,"
20275 " address %s, is_stmt %u, discrim %u%s\n",
20276 m_line, m_file,
20277 paddress (m_gdbarch, m_address),
20278 m_is_stmt, m_discriminator,
20279 (end_sequence ? "\t(end sequence)" : ""));
20280 }
20281
20282 file_entry *fe = current_file ();
20283
20284 if (fe == NULL)
20285 dwarf2_debug_line_missing_file_complaint ();
20286 /* For now we ignore lines not starting on an instruction boundary.
20287 But not when processing end_sequence for compatibility with the
20288 previous version of the code. */
20289 else if (m_op_index == 0 || end_sequence)
20290 {
20291 fe->included_p = 1;
20292 if (m_record_lines_p)
20293 {
20294 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20295 || end_sequence)
20296 {
20297 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20298 m_currently_recording_lines ? m_cu : nullptr);
20299 }
20300
20301 if (!end_sequence)
20302 {
20303 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20304
20305 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20306 m_line_has_non_zero_discriminator,
20307 m_last_subfile))
20308 {
20309 buildsym_compunit *builder = m_cu->get_builder ();
20310 dwarf_record_line_1 (m_gdbarch,
20311 builder->get_current_subfile (),
20312 m_line, m_address, is_stmt,
20313 m_currently_recording_lines ? m_cu : nullptr);
20314 }
20315 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20316 m_last_line = m_line;
20317 }
20318 }
20319 }
20320 }
20321
20322 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20323 line_header *lh, bool record_lines_p)
20324 {
20325 m_cu = cu;
20326 m_gdbarch = arch;
20327 m_record_lines_p = record_lines_p;
20328 m_line_header = lh;
20329
20330 m_currently_recording_lines = true;
20331
20332 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20333 was a line entry for it so that the backend has a chance to adjust it
20334 and also record it in case it needs it. This is currently used by MIPS
20335 code, cf. `mips_adjust_dwarf2_line'. */
20336 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20337 m_is_stmt = lh->default_is_stmt;
20338 m_discriminator = 0;
20339 }
20340
20341 void
20342 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20343 const gdb_byte *line_ptr,
20344 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20345 {
20346 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20347 the pc range of the CU. However, we restrict the test to only ADDRESS
20348 values of zero to preserve GDB's previous behaviour which is to handle
20349 the specific case of a function being GC'd by the linker. */
20350
20351 if (address == 0 && address < unrelocated_lowpc)
20352 {
20353 /* This line table is for a function which has been
20354 GCd by the linker. Ignore it. PR gdb/12528 */
20355
20356 struct objfile *objfile = cu->per_objfile->objfile;
20357 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20358
20359 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20360 line_offset, objfile_name (objfile));
20361 m_currently_recording_lines = false;
20362 /* Note: m_currently_recording_lines is left as false until we see
20363 DW_LNE_end_sequence. */
20364 }
20365 }
20366
20367 /* Subroutine of dwarf_decode_lines to simplify it.
20368 Process the line number information in LH.
20369 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20370 program in order to set included_p for every referenced header. */
20371
20372 static void
20373 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20374 const int decode_for_pst_p, CORE_ADDR lowpc)
20375 {
20376 const gdb_byte *line_ptr, *extended_end;
20377 const gdb_byte *line_end;
20378 unsigned int bytes_read, extended_len;
20379 unsigned char op_code, extended_op;
20380 CORE_ADDR baseaddr;
20381 struct objfile *objfile = cu->per_objfile->objfile;
20382 bfd *abfd = objfile->obfd;
20383 struct gdbarch *gdbarch = objfile->arch ();
20384 /* True if we're recording line info (as opposed to building partial
20385 symtabs and just interested in finding include files mentioned by
20386 the line number program). */
20387 bool record_lines_p = !decode_for_pst_p;
20388
20389 baseaddr = objfile->text_section_offset ();
20390
20391 line_ptr = lh->statement_program_start;
20392 line_end = lh->statement_program_end;
20393
20394 /* Read the statement sequences until there's nothing left. */
20395 while (line_ptr < line_end)
20396 {
20397 /* The DWARF line number program state machine. Reset the state
20398 machine at the start of each sequence. */
20399 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20400 bool end_sequence = false;
20401
20402 if (record_lines_p)
20403 {
20404 /* Start a subfile for the current file of the state
20405 machine. */
20406 const file_entry *fe = state_machine.current_file ();
20407
20408 if (fe != NULL)
20409 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20410 }
20411
20412 /* Decode the table. */
20413 while (line_ptr < line_end && !end_sequence)
20414 {
20415 op_code = read_1_byte (abfd, line_ptr);
20416 line_ptr += 1;
20417
20418 if (op_code >= lh->opcode_base)
20419 {
20420 /* Special opcode. */
20421 state_machine.handle_special_opcode (op_code);
20422 }
20423 else switch (op_code)
20424 {
20425 case DW_LNS_extended_op:
20426 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20427 &bytes_read);
20428 line_ptr += bytes_read;
20429 extended_end = line_ptr + extended_len;
20430 extended_op = read_1_byte (abfd, line_ptr);
20431 line_ptr += 1;
20432 switch (extended_op)
20433 {
20434 case DW_LNE_end_sequence:
20435 state_machine.handle_end_sequence ();
20436 end_sequence = true;
20437 break;
20438 case DW_LNE_set_address:
20439 {
20440 CORE_ADDR address
20441 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20442 line_ptr += bytes_read;
20443
20444 state_machine.check_line_address (cu, line_ptr,
20445 lowpc - baseaddr, address);
20446 state_machine.handle_set_address (baseaddr, address);
20447 }
20448 break;
20449 case DW_LNE_define_file:
20450 {
20451 const char *cur_file;
20452 unsigned int mod_time, length;
20453 dir_index dindex;
20454
20455 cur_file = read_direct_string (abfd, line_ptr,
20456 &bytes_read);
20457 line_ptr += bytes_read;
20458 dindex = (dir_index)
20459 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20460 line_ptr += bytes_read;
20461 mod_time =
20462 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20463 line_ptr += bytes_read;
20464 length =
20465 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20466 line_ptr += bytes_read;
20467 lh->add_file_name (cur_file, dindex, mod_time, length);
20468 }
20469 break;
20470 case DW_LNE_set_discriminator:
20471 {
20472 /* The discriminator is not interesting to the
20473 debugger; just ignore it. We still need to
20474 check its value though:
20475 if there are consecutive entries for the same
20476 (non-prologue) line we want to coalesce them.
20477 PR 17276. */
20478 unsigned int discr
20479 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20480 line_ptr += bytes_read;
20481
20482 state_machine.handle_set_discriminator (discr);
20483 }
20484 break;
20485 default:
20486 complaint (_("mangled .debug_line section"));
20487 return;
20488 }
20489 /* Make sure that we parsed the extended op correctly. If e.g.
20490 we expected a different address size than the producer used,
20491 we may have read the wrong number of bytes. */
20492 if (line_ptr != extended_end)
20493 {
20494 complaint (_("mangled .debug_line section"));
20495 return;
20496 }
20497 break;
20498 case DW_LNS_copy:
20499 state_machine.handle_copy ();
20500 break;
20501 case DW_LNS_advance_pc:
20502 {
20503 CORE_ADDR adjust
20504 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20505 line_ptr += bytes_read;
20506
20507 state_machine.handle_advance_pc (adjust);
20508 }
20509 break;
20510 case DW_LNS_advance_line:
20511 {
20512 int line_delta
20513 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20514 line_ptr += bytes_read;
20515
20516 state_machine.handle_advance_line (line_delta);
20517 }
20518 break;
20519 case DW_LNS_set_file:
20520 {
20521 file_name_index file
20522 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20523 &bytes_read);
20524 line_ptr += bytes_read;
20525
20526 state_machine.handle_set_file (file);
20527 }
20528 break;
20529 case DW_LNS_set_column:
20530 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20531 line_ptr += bytes_read;
20532 break;
20533 case DW_LNS_negate_stmt:
20534 state_machine.handle_negate_stmt ();
20535 break;
20536 case DW_LNS_set_basic_block:
20537 break;
20538 /* Add to the address register of the state machine the
20539 address increment value corresponding to special opcode
20540 255. I.e., this value is scaled by the minimum
20541 instruction length since special opcode 255 would have
20542 scaled the increment. */
20543 case DW_LNS_const_add_pc:
20544 state_machine.handle_const_add_pc ();
20545 break;
20546 case DW_LNS_fixed_advance_pc:
20547 {
20548 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20549 line_ptr += 2;
20550
20551 state_machine.handle_fixed_advance_pc (addr_adj);
20552 }
20553 break;
20554 default:
20555 {
20556 /* Unknown standard opcode, ignore it. */
20557 int i;
20558
20559 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20560 {
20561 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20562 line_ptr += bytes_read;
20563 }
20564 }
20565 }
20566 }
20567
20568 if (!end_sequence)
20569 dwarf2_debug_line_missing_end_sequence_complaint ();
20570
20571 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20572 in which case we still finish recording the last line). */
20573 state_machine.record_line (true);
20574 }
20575 }
20576
20577 /* Decode the Line Number Program (LNP) for the given line_header
20578 structure and CU. The actual information extracted and the type
20579 of structures created from the LNP depends on the value of PST.
20580
20581 1. If PST is NULL, then this procedure uses the data from the program
20582 to create all necessary symbol tables, and their linetables.
20583
20584 2. If PST is not NULL, this procedure reads the program to determine
20585 the list of files included by the unit represented by PST, and
20586 builds all the associated partial symbol tables.
20587
20588 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20589 It is used for relative paths in the line table.
20590 NOTE: When processing partial symtabs (pst != NULL),
20591 comp_dir == pst->dirname.
20592
20593 NOTE: It is important that psymtabs have the same file name (via strcmp)
20594 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20595 symtab we don't use it in the name of the psymtabs we create.
20596 E.g. expand_line_sal requires this when finding psymtabs to expand.
20597 A good testcase for this is mb-inline.exp.
20598
20599 LOWPC is the lowest address in CU (or 0 if not known).
20600
20601 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20602 for its PC<->lines mapping information. Otherwise only the filename
20603 table is read in. */
20604
20605 static void
20606 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20607 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20608 CORE_ADDR lowpc, int decode_mapping)
20609 {
20610 struct objfile *objfile = cu->per_objfile->objfile;
20611 const int decode_for_pst_p = (pst != NULL);
20612
20613 if (decode_mapping)
20614 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20615
20616 if (decode_for_pst_p)
20617 {
20618 /* Now that we're done scanning the Line Header Program, we can
20619 create the psymtab of each included file. */
20620 for (auto &file_entry : lh->file_names ())
20621 if (file_entry.included_p == 1)
20622 {
20623 gdb::unique_xmalloc_ptr<char> name_holder;
20624 const char *include_name =
20625 psymtab_include_file_name (lh, file_entry, pst,
20626 comp_dir, &name_holder);
20627 if (include_name != NULL)
20628 dwarf2_create_include_psymtab (include_name, pst, objfile);
20629 }
20630 }
20631 else
20632 {
20633 /* Make sure a symtab is created for every file, even files
20634 which contain only variables (i.e. no code with associated
20635 line numbers). */
20636 buildsym_compunit *builder = cu->get_builder ();
20637 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20638
20639 for (auto &fe : lh->file_names ())
20640 {
20641 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20642 if (builder->get_current_subfile ()->symtab == NULL)
20643 {
20644 builder->get_current_subfile ()->symtab
20645 = allocate_symtab (cust,
20646 builder->get_current_subfile ()->name);
20647 }
20648 fe.symtab = builder->get_current_subfile ()->symtab;
20649 }
20650 }
20651 }
20652
20653 /* Start a subfile for DWARF. FILENAME is the name of the file and
20654 DIRNAME the name of the source directory which contains FILENAME
20655 or NULL if not known.
20656 This routine tries to keep line numbers from identical absolute and
20657 relative file names in a common subfile.
20658
20659 Using the `list' example from the GDB testsuite, which resides in
20660 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20661 of /srcdir/list0.c yields the following debugging information for list0.c:
20662
20663 DW_AT_name: /srcdir/list0.c
20664 DW_AT_comp_dir: /compdir
20665 files.files[0].name: list0.h
20666 files.files[0].dir: /srcdir
20667 files.files[1].name: list0.c
20668 files.files[1].dir: /srcdir
20669
20670 The line number information for list0.c has to end up in a single
20671 subfile, so that `break /srcdir/list0.c:1' works as expected.
20672 start_subfile will ensure that this happens provided that we pass the
20673 concatenation of files.files[1].dir and files.files[1].name as the
20674 subfile's name. */
20675
20676 static void
20677 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20678 const char *dirname)
20679 {
20680 gdb::unique_xmalloc_ptr<char> copy;
20681
20682 /* In order not to lose the line information directory,
20683 we concatenate it to the filename when it makes sense.
20684 Note that the Dwarf3 standard says (speaking of filenames in line
20685 information): ``The directory index is ignored for file names
20686 that represent full path names''. Thus ignoring dirname in the
20687 `else' branch below isn't an issue. */
20688
20689 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20690 {
20691 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20692 filename = copy.get ();
20693 }
20694
20695 cu->get_builder ()->start_subfile (filename);
20696 }
20697
20698 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20699 buildsym_compunit constructor. */
20700
20701 struct compunit_symtab *
20702 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20703 CORE_ADDR low_pc)
20704 {
20705 gdb_assert (m_builder == nullptr);
20706
20707 m_builder.reset (new struct buildsym_compunit
20708 (this->per_objfile->objfile,
20709 name, comp_dir, language, low_pc));
20710
20711 list_in_scope = get_builder ()->get_file_symbols ();
20712
20713 get_builder ()->record_debugformat ("DWARF 2");
20714 get_builder ()->record_producer (producer);
20715
20716 processing_has_namespace_info = false;
20717
20718 return get_builder ()->get_compunit_symtab ();
20719 }
20720
20721 static void
20722 var_decode_location (struct attribute *attr, struct symbol *sym,
20723 struct dwarf2_cu *cu)
20724 {
20725 struct objfile *objfile = cu->per_objfile->objfile;
20726 struct comp_unit_head *cu_header = &cu->header;
20727
20728 /* NOTE drow/2003-01-30: There used to be a comment and some special
20729 code here to turn a symbol with DW_AT_external and a
20730 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20731 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20732 with some versions of binutils) where shared libraries could have
20733 relocations against symbols in their debug information - the
20734 minimal symbol would have the right address, but the debug info
20735 would not. It's no longer necessary, because we will explicitly
20736 apply relocations when we read in the debug information now. */
20737
20738 /* A DW_AT_location attribute with no contents indicates that a
20739 variable has been optimized away. */
20740 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20741 {
20742 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20743 return;
20744 }
20745
20746 /* Handle one degenerate form of location expression specially, to
20747 preserve GDB's previous behavior when section offsets are
20748 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20749 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20750
20751 if (attr->form_is_block ()
20752 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20753 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20754 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20755 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20756 && (DW_BLOCK (attr)->size
20757 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20758 {
20759 unsigned int dummy;
20760
20761 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20762 SET_SYMBOL_VALUE_ADDRESS
20763 (sym, cu->header.read_address (objfile->obfd,
20764 DW_BLOCK (attr)->data + 1,
20765 &dummy));
20766 else
20767 SET_SYMBOL_VALUE_ADDRESS
20768 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20769 &dummy));
20770 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20771 fixup_symbol_section (sym, objfile);
20772 SET_SYMBOL_VALUE_ADDRESS
20773 (sym,
20774 SYMBOL_VALUE_ADDRESS (sym)
20775 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20776 return;
20777 }
20778
20779 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20780 expression evaluator, and use LOC_COMPUTED only when necessary
20781 (i.e. when the value of a register or memory location is
20782 referenced, or a thread-local block, etc.). Then again, it might
20783 not be worthwhile. I'm assuming that it isn't unless performance
20784 or memory numbers show me otherwise. */
20785
20786 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20787
20788 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20789 cu->has_loclist = true;
20790 }
20791
20792 /* Given a pointer to a DWARF information entry, figure out if we need
20793 to make a symbol table entry for it, and if so, create a new entry
20794 and return a pointer to it.
20795 If TYPE is NULL, determine symbol type from the die, otherwise
20796 used the passed type.
20797 If SPACE is not NULL, use it to hold the new symbol. If it is
20798 NULL, allocate a new symbol on the objfile's obstack. */
20799
20800 static struct symbol *
20801 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20802 struct symbol *space)
20803 {
20804 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20805 struct objfile *objfile = dwarf2_per_objfile->objfile;
20806 struct gdbarch *gdbarch = objfile->arch ();
20807 struct symbol *sym = NULL;
20808 const char *name;
20809 struct attribute *attr = NULL;
20810 struct attribute *attr2 = NULL;
20811 CORE_ADDR baseaddr;
20812 struct pending **list_to_add = NULL;
20813
20814 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20815
20816 baseaddr = objfile->text_section_offset ();
20817
20818 name = dwarf2_name (die, cu);
20819 if (name)
20820 {
20821 int suppress_add = 0;
20822
20823 if (space)
20824 sym = space;
20825 else
20826 sym = new (&objfile->objfile_obstack) symbol;
20827 OBJSTAT (objfile, n_syms++);
20828
20829 /* Cache this symbol's name and the name's demangled form (if any). */
20830 sym->set_language (cu->language, &objfile->objfile_obstack);
20831 /* Fortran does not have mangling standard and the mangling does differ
20832 between gfortran, iFort etc. */
20833 const char *physname
20834 = (cu->language == language_fortran
20835 ? dwarf2_full_name (name, die, cu)
20836 : dwarf2_physname (name, die, cu));
20837 const char *linkagename = dw2_linkage_name (die, cu);
20838
20839 if (linkagename == nullptr || cu->language == language_ada)
20840 sym->set_linkage_name (physname);
20841 else
20842 {
20843 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20844 sym->set_linkage_name (linkagename);
20845 }
20846
20847 /* Default assumptions.
20848 Use the passed type or decode it from the die. */
20849 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20850 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20851 if (type != NULL)
20852 SYMBOL_TYPE (sym) = type;
20853 else
20854 SYMBOL_TYPE (sym) = die_type (die, cu);
20855 attr = dwarf2_attr (die,
20856 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20857 cu);
20858 if (attr != nullptr)
20859 {
20860 SYMBOL_LINE (sym) = DW_UNSND (attr);
20861 }
20862
20863 attr = dwarf2_attr (die,
20864 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20865 cu);
20866 if (attr != nullptr)
20867 {
20868 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20869 struct file_entry *fe;
20870
20871 if (cu->line_header != NULL)
20872 fe = cu->line_header->file_name_at (file_index);
20873 else
20874 fe = NULL;
20875
20876 if (fe == NULL)
20877 complaint (_("file index out of range"));
20878 else
20879 symbol_set_symtab (sym, fe->symtab);
20880 }
20881
20882 switch (die->tag)
20883 {
20884 case DW_TAG_label:
20885 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20886 if (attr != nullptr)
20887 {
20888 CORE_ADDR addr;
20889
20890 addr = attr->value_as_address ();
20891 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20892 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20893 }
20894 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20895 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20896 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20897 add_symbol_to_list (sym, cu->list_in_scope);
20898 break;
20899 case DW_TAG_subprogram:
20900 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20901 finish_block. */
20902 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20903 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20904 if ((attr2 && (DW_UNSND (attr2) != 0))
20905 || cu->language == language_ada
20906 || cu->language == language_fortran)
20907 {
20908 /* Subprograms marked external are stored as a global symbol.
20909 Ada and Fortran subprograms, whether marked external or
20910 not, are always stored as a global symbol, because we want
20911 to be able to access them globally. For instance, we want
20912 to be able to break on a nested subprogram without having
20913 to specify the context. */
20914 list_to_add = cu->get_builder ()->get_global_symbols ();
20915 }
20916 else
20917 {
20918 list_to_add = cu->list_in_scope;
20919 }
20920 break;
20921 case DW_TAG_inlined_subroutine:
20922 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20923 finish_block. */
20924 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20925 SYMBOL_INLINED (sym) = 1;
20926 list_to_add = cu->list_in_scope;
20927 break;
20928 case DW_TAG_template_value_param:
20929 suppress_add = 1;
20930 /* Fall through. */
20931 case DW_TAG_constant:
20932 case DW_TAG_variable:
20933 case DW_TAG_member:
20934 /* Compilation with minimal debug info may result in
20935 variables with missing type entries. Change the
20936 misleading `void' type to something sensible. */
20937 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20938 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20939
20940 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20941 /* In the case of DW_TAG_member, we should only be called for
20942 static const members. */
20943 if (die->tag == DW_TAG_member)
20944 {
20945 /* dwarf2_add_field uses die_is_declaration,
20946 so we do the same. */
20947 gdb_assert (die_is_declaration (die, cu));
20948 gdb_assert (attr);
20949 }
20950 if (attr != nullptr)
20951 {
20952 dwarf2_const_value (attr, sym, cu);
20953 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20954 if (!suppress_add)
20955 {
20956 if (attr2 && (DW_UNSND (attr2) != 0))
20957 list_to_add = cu->get_builder ()->get_global_symbols ();
20958 else
20959 list_to_add = cu->list_in_scope;
20960 }
20961 break;
20962 }
20963 attr = dwarf2_attr (die, DW_AT_location, cu);
20964 if (attr != nullptr)
20965 {
20966 var_decode_location (attr, sym, cu);
20967 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20968
20969 /* Fortran explicitly imports any global symbols to the local
20970 scope by DW_TAG_common_block. */
20971 if (cu->language == language_fortran && die->parent
20972 && die->parent->tag == DW_TAG_common_block)
20973 attr2 = NULL;
20974
20975 if (SYMBOL_CLASS (sym) == LOC_STATIC
20976 && SYMBOL_VALUE_ADDRESS (sym) == 0
20977 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20978 {
20979 /* When a static variable is eliminated by the linker,
20980 the corresponding debug information is not stripped
20981 out, but the variable address is set to null;
20982 do not add such variables into symbol table. */
20983 }
20984 else if (attr2 && (DW_UNSND (attr2) != 0))
20985 {
20986 if (SYMBOL_CLASS (sym) == LOC_STATIC
20987 && (objfile->flags & OBJF_MAINLINE) == 0
20988 && dwarf2_per_objfile->per_bfd->can_copy)
20989 {
20990 /* A global static variable might be subject to
20991 copy relocation. We first check for a local
20992 minsym, though, because maybe the symbol was
20993 marked hidden, in which case this would not
20994 apply. */
20995 bound_minimal_symbol found
20996 = (lookup_minimal_symbol_linkage
20997 (sym->linkage_name (), objfile));
20998 if (found.minsym != nullptr)
20999 sym->maybe_copied = 1;
21000 }
21001
21002 /* A variable with DW_AT_external is never static,
21003 but it may be block-scoped. */
21004 list_to_add
21005 = ((cu->list_in_scope
21006 == cu->get_builder ()->get_file_symbols ())
21007 ? cu->get_builder ()->get_global_symbols ()
21008 : cu->list_in_scope);
21009 }
21010 else
21011 list_to_add = cu->list_in_scope;
21012 }
21013 else
21014 {
21015 /* We do not know the address of this symbol.
21016 If it is an external symbol and we have type information
21017 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21018 The address of the variable will then be determined from
21019 the minimal symbol table whenever the variable is
21020 referenced. */
21021 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21022
21023 /* Fortran explicitly imports any global symbols to the local
21024 scope by DW_TAG_common_block. */
21025 if (cu->language == language_fortran && die->parent
21026 && die->parent->tag == DW_TAG_common_block)
21027 {
21028 /* SYMBOL_CLASS doesn't matter here because
21029 read_common_block is going to reset it. */
21030 if (!suppress_add)
21031 list_to_add = cu->list_in_scope;
21032 }
21033 else if (attr2 && (DW_UNSND (attr2) != 0)
21034 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21035 {
21036 /* A variable with DW_AT_external is never static, but it
21037 may be block-scoped. */
21038 list_to_add
21039 = ((cu->list_in_scope
21040 == cu->get_builder ()->get_file_symbols ())
21041 ? cu->get_builder ()->get_global_symbols ()
21042 : cu->list_in_scope);
21043
21044 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21045 }
21046 else if (!die_is_declaration (die, cu))
21047 {
21048 /* Use the default LOC_OPTIMIZED_OUT class. */
21049 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21050 if (!suppress_add)
21051 list_to_add = cu->list_in_scope;
21052 }
21053 }
21054 break;
21055 case DW_TAG_formal_parameter:
21056 {
21057 /* If we are inside a function, mark this as an argument. If
21058 not, we might be looking at an argument to an inlined function
21059 when we do not have enough information to show inlined frames;
21060 pretend it's a local variable in that case so that the user can
21061 still see it. */
21062 struct context_stack *curr
21063 = cu->get_builder ()->get_current_context_stack ();
21064 if (curr != nullptr && curr->name != nullptr)
21065 SYMBOL_IS_ARGUMENT (sym) = 1;
21066 attr = dwarf2_attr (die, DW_AT_location, cu);
21067 if (attr != nullptr)
21068 {
21069 var_decode_location (attr, sym, cu);
21070 }
21071 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21072 if (attr != nullptr)
21073 {
21074 dwarf2_const_value (attr, sym, cu);
21075 }
21076
21077 list_to_add = cu->list_in_scope;
21078 }
21079 break;
21080 case DW_TAG_unspecified_parameters:
21081 /* From varargs functions; gdb doesn't seem to have any
21082 interest in this information, so just ignore it for now.
21083 (FIXME?) */
21084 break;
21085 case DW_TAG_template_type_param:
21086 suppress_add = 1;
21087 /* Fall through. */
21088 case DW_TAG_class_type:
21089 case DW_TAG_interface_type:
21090 case DW_TAG_structure_type:
21091 case DW_TAG_union_type:
21092 case DW_TAG_set_type:
21093 case DW_TAG_enumeration_type:
21094 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21095 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21096
21097 {
21098 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21099 really ever be static objects: otherwise, if you try
21100 to, say, break of a class's method and you're in a file
21101 which doesn't mention that class, it won't work unless
21102 the check for all static symbols in lookup_symbol_aux
21103 saves you. See the OtherFileClass tests in
21104 gdb.c++/namespace.exp. */
21105
21106 if (!suppress_add)
21107 {
21108 buildsym_compunit *builder = cu->get_builder ();
21109 list_to_add
21110 = (cu->list_in_scope == builder->get_file_symbols ()
21111 && cu->language == language_cplus
21112 ? builder->get_global_symbols ()
21113 : cu->list_in_scope);
21114
21115 /* The semantics of C++ state that "struct foo {
21116 ... }" also defines a typedef for "foo". */
21117 if (cu->language == language_cplus
21118 || cu->language == language_ada
21119 || cu->language == language_d
21120 || cu->language == language_rust)
21121 {
21122 /* The symbol's name is already allocated along
21123 with this objfile, so we don't need to
21124 duplicate it for the type. */
21125 if (SYMBOL_TYPE (sym)->name () == 0)
21126 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21127 }
21128 }
21129 }
21130 break;
21131 case DW_TAG_typedef:
21132 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21133 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21134 list_to_add = cu->list_in_scope;
21135 break;
21136 case DW_TAG_base_type:
21137 case DW_TAG_subrange_type:
21138 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21139 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21140 list_to_add = cu->list_in_scope;
21141 break;
21142 case DW_TAG_enumerator:
21143 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21144 if (attr != nullptr)
21145 {
21146 dwarf2_const_value (attr, sym, cu);
21147 }
21148 {
21149 /* NOTE: carlton/2003-11-10: See comment above in the
21150 DW_TAG_class_type, etc. block. */
21151
21152 list_to_add
21153 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21154 && cu->language == language_cplus
21155 ? cu->get_builder ()->get_global_symbols ()
21156 : cu->list_in_scope);
21157 }
21158 break;
21159 case DW_TAG_imported_declaration:
21160 case DW_TAG_namespace:
21161 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21162 list_to_add = cu->get_builder ()->get_global_symbols ();
21163 break;
21164 case DW_TAG_module:
21165 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21166 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21167 list_to_add = cu->get_builder ()->get_global_symbols ();
21168 break;
21169 case DW_TAG_common_block:
21170 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21171 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21172 add_symbol_to_list (sym, cu->list_in_scope);
21173 break;
21174 default:
21175 /* Not a tag we recognize. Hopefully we aren't processing
21176 trash data, but since we must specifically ignore things
21177 we don't recognize, there is nothing else we should do at
21178 this point. */
21179 complaint (_("unsupported tag: '%s'"),
21180 dwarf_tag_name (die->tag));
21181 break;
21182 }
21183
21184 if (suppress_add)
21185 {
21186 sym->hash_next = objfile->template_symbols;
21187 objfile->template_symbols = sym;
21188 list_to_add = NULL;
21189 }
21190
21191 if (list_to_add != NULL)
21192 add_symbol_to_list (sym, list_to_add);
21193
21194 /* For the benefit of old versions of GCC, check for anonymous
21195 namespaces based on the demangled name. */
21196 if (!cu->processing_has_namespace_info
21197 && cu->language == language_cplus)
21198 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21199 }
21200 return (sym);
21201 }
21202
21203 /* Given an attr with a DW_FORM_dataN value in host byte order,
21204 zero-extend it as appropriate for the symbol's type. The DWARF
21205 standard (v4) is not entirely clear about the meaning of using
21206 DW_FORM_dataN for a constant with a signed type, where the type is
21207 wider than the data. The conclusion of a discussion on the DWARF
21208 list was that this is unspecified. We choose to always zero-extend
21209 because that is the interpretation long in use by GCC. */
21210
21211 static gdb_byte *
21212 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21213 struct dwarf2_cu *cu, LONGEST *value, int bits)
21214 {
21215 struct objfile *objfile = cu->per_objfile->objfile;
21216 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21217 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21218 LONGEST l = DW_UNSND (attr);
21219
21220 if (bits < sizeof (*value) * 8)
21221 {
21222 l &= ((LONGEST) 1 << bits) - 1;
21223 *value = l;
21224 }
21225 else if (bits == sizeof (*value) * 8)
21226 *value = l;
21227 else
21228 {
21229 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21230 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21231 return bytes;
21232 }
21233
21234 return NULL;
21235 }
21236
21237 /* Read a constant value from an attribute. Either set *VALUE, or if
21238 the value does not fit in *VALUE, set *BYTES - either already
21239 allocated on the objfile obstack, or newly allocated on OBSTACK,
21240 or, set *BATON, if we translated the constant to a location
21241 expression. */
21242
21243 static void
21244 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21245 const char *name, struct obstack *obstack,
21246 struct dwarf2_cu *cu,
21247 LONGEST *value, const gdb_byte **bytes,
21248 struct dwarf2_locexpr_baton **baton)
21249 {
21250 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21251 struct objfile *objfile = per_objfile->objfile;
21252 struct comp_unit_head *cu_header = &cu->header;
21253 struct dwarf_block *blk;
21254 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21255 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21256
21257 *value = 0;
21258 *bytes = NULL;
21259 *baton = NULL;
21260
21261 switch (attr->form)
21262 {
21263 case DW_FORM_addr:
21264 case DW_FORM_addrx:
21265 case DW_FORM_GNU_addr_index:
21266 {
21267 gdb_byte *data;
21268
21269 if (TYPE_LENGTH (type) != cu_header->addr_size)
21270 dwarf2_const_value_length_mismatch_complaint (name,
21271 cu_header->addr_size,
21272 TYPE_LENGTH (type));
21273 /* Symbols of this form are reasonably rare, so we just
21274 piggyback on the existing location code rather than writing
21275 a new implementation of symbol_computed_ops. */
21276 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21277 (*baton)->per_objfile = per_objfile;
21278 (*baton)->per_cu = cu->per_cu;
21279 gdb_assert ((*baton)->per_cu);
21280
21281 (*baton)->size = 2 + cu_header->addr_size;
21282 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21283 (*baton)->data = data;
21284
21285 data[0] = DW_OP_addr;
21286 store_unsigned_integer (&data[1], cu_header->addr_size,
21287 byte_order, DW_ADDR (attr));
21288 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21289 }
21290 break;
21291 case DW_FORM_string:
21292 case DW_FORM_strp:
21293 case DW_FORM_strx:
21294 case DW_FORM_GNU_str_index:
21295 case DW_FORM_GNU_strp_alt:
21296 /* DW_STRING is already allocated on the objfile obstack, point
21297 directly to it. */
21298 *bytes = (const gdb_byte *) DW_STRING (attr);
21299 break;
21300 case DW_FORM_block1:
21301 case DW_FORM_block2:
21302 case DW_FORM_block4:
21303 case DW_FORM_block:
21304 case DW_FORM_exprloc:
21305 case DW_FORM_data16:
21306 blk = DW_BLOCK (attr);
21307 if (TYPE_LENGTH (type) != blk->size)
21308 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21309 TYPE_LENGTH (type));
21310 *bytes = blk->data;
21311 break;
21312
21313 /* The DW_AT_const_value attributes are supposed to carry the
21314 symbol's value "represented as it would be on the target
21315 architecture." By the time we get here, it's already been
21316 converted to host endianness, so we just need to sign- or
21317 zero-extend it as appropriate. */
21318 case DW_FORM_data1:
21319 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21320 break;
21321 case DW_FORM_data2:
21322 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21323 break;
21324 case DW_FORM_data4:
21325 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21326 break;
21327 case DW_FORM_data8:
21328 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21329 break;
21330
21331 case DW_FORM_sdata:
21332 case DW_FORM_implicit_const:
21333 *value = DW_SND (attr);
21334 break;
21335
21336 case DW_FORM_udata:
21337 *value = DW_UNSND (attr);
21338 break;
21339
21340 default:
21341 complaint (_("unsupported const value attribute form: '%s'"),
21342 dwarf_form_name (attr->form));
21343 *value = 0;
21344 break;
21345 }
21346 }
21347
21348
21349 /* Copy constant value from an attribute to a symbol. */
21350
21351 static void
21352 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21353 struct dwarf2_cu *cu)
21354 {
21355 struct objfile *objfile = cu->per_objfile->objfile;
21356 LONGEST value;
21357 const gdb_byte *bytes;
21358 struct dwarf2_locexpr_baton *baton;
21359
21360 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21361 sym->print_name (),
21362 &objfile->objfile_obstack, cu,
21363 &value, &bytes, &baton);
21364
21365 if (baton != NULL)
21366 {
21367 SYMBOL_LOCATION_BATON (sym) = baton;
21368 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21369 }
21370 else if (bytes != NULL)
21371 {
21372 SYMBOL_VALUE_BYTES (sym) = bytes;
21373 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21374 }
21375 else
21376 {
21377 SYMBOL_VALUE (sym) = value;
21378 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21379 }
21380 }
21381
21382 /* Return the type of the die in question using its DW_AT_type attribute. */
21383
21384 static struct type *
21385 die_type (struct die_info *die, struct dwarf2_cu *cu)
21386 {
21387 struct attribute *type_attr;
21388
21389 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21390 if (!type_attr)
21391 {
21392 struct objfile *objfile = cu->per_objfile->objfile;
21393 /* A missing DW_AT_type represents a void type. */
21394 return objfile_type (objfile)->builtin_void;
21395 }
21396
21397 return lookup_die_type (die, type_attr, cu);
21398 }
21399
21400 /* True iff CU's producer generates GNAT Ada auxiliary information
21401 that allows to find parallel types through that information instead
21402 of having to do expensive parallel lookups by type name. */
21403
21404 static int
21405 need_gnat_info (struct dwarf2_cu *cu)
21406 {
21407 /* Assume that the Ada compiler was GNAT, which always produces
21408 the auxiliary information. */
21409 return (cu->language == language_ada);
21410 }
21411
21412 /* Return the auxiliary type of the die in question using its
21413 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21414 attribute is not present. */
21415
21416 static struct type *
21417 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21418 {
21419 struct attribute *type_attr;
21420
21421 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21422 if (!type_attr)
21423 return NULL;
21424
21425 return lookup_die_type (die, type_attr, cu);
21426 }
21427
21428 /* If DIE has a descriptive_type attribute, then set the TYPE's
21429 descriptive type accordingly. */
21430
21431 static void
21432 set_descriptive_type (struct type *type, struct die_info *die,
21433 struct dwarf2_cu *cu)
21434 {
21435 struct type *descriptive_type = die_descriptive_type (die, cu);
21436
21437 if (descriptive_type)
21438 {
21439 ALLOCATE_GNAT_AUX_TYPE (type);
21440 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21441 }
21442 }
21443
21444 /* Return the containing type of the die in question using its
21445 DW_AT_containing_type attribute. */
21446
21447 static struct type *
21448 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21449 {
21450 struct attribute *type_attr;
21451 struct objfile *objfile = cu->per_objfile->objfile;
21452
21453 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21454 if (!type_attr)
21455 error (_("Dwarf Error: Problem turning containing type into gdb type "
21456 "[in module %s]"), objfile_name (objfile));
21457
21458 return lookup_die_type (die, type_attr, cu);
21459 }
21460
21461 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21462
21463 static struct type *
21464 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21465 {
21466 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21467 struct objfile *objfile = dwarf2_per_objfile->objfile;
21468 char *saved;
21469
21470 std::string message
21471 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21472 objfile_name (objfile),
21473 sect_offset_str (cu->header.sect_off),
21474 sect_offset_str (die->sect_off));
21475 saved = obstack_strdup (&objfile->objfile_obstack, message);
21476
21477 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21478 }
21479
21480 /* Look up the type of DIE in CU using its type attribute ATTR.
21481 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21482 DW_AT_containing_type.
21483 If there is no type substitute an error marker. */
21484
21485 static struct type *
21486 lookup_die_type (struct die_info *die, const struct attribute *attr,
21487 struct dwarf2_cu *cu)
21488 {
21489 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21490 struct objfile *objfile = dwarf2_per_objfile->objfile;
21491 struct type *this_type;
21492
21493 gdb_assert (attr->name == DW_AT_type
21494 || attr->name == DW_AT_GNAT_descriptive_type
21495 || attr->name == DW_AT_containing_type);
21496
21497 /* First see if we have it cached. */
21498
21499 if (attr->form == DW_FORM_GNU_ref_alt)
21500 {
21501 struct dwarf2_per_cu_data *per_cu;
21502 sect_offset sect_off = attr->get_ref_die_offset ();
21503
21504 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21505 dwarf2_per_objfile);
21506 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
21507 }
21508 else if (attr->form_is_ref ())
21509 {
21510 sect_offset sect_off = attr->get_ref_die_offset ();
21511
21512 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21513 dwarf2_per_objfile);
21514 }
21515 else if (attr->form == DW_FORM_ref_sig8)
21516 {
21517 ULONGEST signature = DW_SIGNATURE (attr);
21518
21519 return get_signatured_type (die, signature, cu);
21520 }
21521 else
21522 {
21523 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21524 " at %s [in module %s]"),
21525 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21526 objfile_name (objfile));
21527 return build_error_marker_type (cu, die);
21528 }
21529
21530 /* If not cached we need to read it in. */
21531
21532 if (this_type == NULL)
21533 {
21534 struct die_info *type_die = NULL;
21535 struct dwarf2_cu *type_cu = cu;
21536
21537 if (attr->form_is_ref ())
21538 type_die = follow_die_ref (die, attr, &type_cu);
21539 if (type_die == NULL)
21540 return build_error_marker_type (cu, die);
21541 /* If we find the type now, it's probably because the type came
21542 from an inter-CU reference and the type's CU got expanded before
21543 ours. */
21544 this_type = read_type_die (type_die, type_cu);
21545 }
21546
21547 /* If we still don't have a type use an error marker. */
21548
21549 if (this_type == NULL)
21550 return build_error_marker_type (cu, die);
21551
21552 return this_type;
21553 }
21554
21555 /* Return the type in DIE, CU.
21556 Returns NULL for invalid types.
21557
21558 This first does a lookup in die_type_hash,
21559 and only reads the die in if necessary.
21560
21561 NOTE: This can be called when reading in partial or full symbols. */
21562
21563 static struct type *
21564 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21565 {
21566 struct type *this_type;
21567
21568 this_type = get_die_type (die, cu);
21569 if (this_type)
21570 return this_type;
21571
21572 return read_type_die_1 (die, cu);
21573 }
21574
21575 /* Read the type in DIE, CU.
21576 Returns NULL for invalid types. */
21577
21578 static struct type *
21579 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21580 {
21581 struct type *this_type = NULL;
21582
21583 switch (die->tag)
21584 {
21585 case DW_TAG_class_type:
21586 case DW_TAG_interface_type:
21587 case DW_TAG_structure_type:
21588 case DW_TAG_union_type:
21589 this_type = read_structure_type (die, cu);
21590 break;
21591 case DW_TAG_enumeration_type:
21592 this_type = read_enumeration_type (die, cu);
21593 break;
21594 case DW_TAG_subprogram:
21595 case DW_TAG_subroutine_type:
21596 case DW_TAG_inlined_subroutine:
21597 this_type = read_subroutine_type (die, cu);
21598 break;
21599 case DW_TAG_array_type:
21600 this_type = read_array_type (die, cu);
21601 break;
21602 case DW_TAG_set_type:
21603 this_type = read_set_type (die, cu);
21604 break;
21605 case DW_TAG_pointer_type:
21606 this_type = read_tag_pointer_type (die, cu);
21607 break;
21608 case DW_TAG_ptr_to_member_type:
21609 this_type = read_tag_ptr_to_member_type (die, cu);
21610 break;
21611 case DW_TAG_reference_type:
21612 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21613 break;
21614 case DW_TAG_rvalue_reference_type:
21615 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21616 break;
21617 case DW_TAG_const_type:
21618 this_type = read_tag_const_type (die, cu);
21619 break;
21620 case DW_TAG_volatile_type:
21621 this_type = read_tag_volatile_type (die, cu);
21622 break;
21623 case DW_TAG_restrict_type:
21624 this_type = read_tag_restrict_type (die, cu);
21625 break;
21626 case DW_TAG_string_type:
21627 this_type = read_tag_string_type (die, cu);
21628 break;
21629 case DW_TAG_typedef:
21630 this_type = read_typedef (die, cu);
21631 break;
21632 case DW_TAG_subrange_type:
21633 this_type = read_subrange_type (die, cu);
21634 break;
21635 case DW_TAG_base_type:
21636 this_type = read_base_type (die, cu);
21637 break;
21638 case DW_TAG_unspecified_type:
21639 this_type = read_unspecified_type (die, cu);
21640 break;
21641 case DW_TAG_namespace:
21642 this_type = read_namespace_type (die, cu);
21643 break;
21644 case DW_TAG_module:
21645 this_type = read_module_type (die, cu);
21646 break;
21647 case DW_TAG_atomic_type:
21648 this_type = read_tag_atomic_type (die, cu);
21649 break;
21650 default:
21651 complaint (_("unexpected tag in read_type_die: '%s'"),
21652 dwarf_tag_name (die->tag));
21653 break;
21654 }
21655
21656 return this_type;
21657 }
21658
21659 /* See if we can figure out if the class lives in a namespace. We do
21660 this by looking for a member function; its demangled name will
21661 contain namespace info, if there is any.
21662 Return the computed name or NULL.
21663 Space for the result is allocated on the objfile's obstack.
21664 This is the full-die version of guess_partial_die_structure_name.
21665 In this case we know DIE has no useful parent. */
21666
21667 static const char *
21668 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21669 {
21670 struct die_info *spec_die;
21671 struct dwarf2_cu *spec_cu;
21672 struct die_info *child;
21673 struct objfile *objfile = cu->per_objfile->objfile;
21674
21675 spec_cu = cu;
21676 spec_die = die_specification (die, &spec_cu);
21677 if (spec_die != NULL)
21678 {
21679 die = spec_die;
21680 cu = spec_cu;
21681 }
21682
21683 for (child = die->child;
21684 child != NULL;
21685 child = child->sibling)
21686 {
21687 if (child->tag == DW_TAG_subprogram)
21688 {
21689 const char *linkage_name = dw2_linkage_name (child, cu);
21690
21691 if (linkage_name != NULL)
21692 {
21693 gdb::unique_xmalloc_ptr<char> actual_name
21694 (language_class_name_from_physname (cu->language_defn,
21695 linkage_name));
21696 const char *name = NULL;
21697
21698 if (actual_name != NULL)
21699 {
21700 const char *die_name = dwarf2_name (die, cu);
21701
21702 if (die_name != NULL
21703 && strcmp (die_name, actual_name.get ()) != 0)
21704 {
21705 /* Strip off the class name from the full name.
21706 We want the prefix. */
21707 int die_name_len = strlen (die_name);
21708 int actual_name_len = strlen (actual_name.get ());
21709 const char *ptr = actual_name.get ();
21710
21711 /* Test for '::' as a sanity check. */
21712 if (actual_name_len > die_name_len + 2
21713 && ptr[actual_name_len - die_name_len - 1] == ':')
21714 name = obstack_strndup (
21715 &objfile->per_bfd->storage_obstack,
21716 ptr, actual_name_len - die_name_len - 2);
21717 }
21718 }
21719 return name;
21720 }
21721 }
21722 }
21723
21724 return NULL;
21725 }
21726
21727 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21728 prefix part in such case. See
21729 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21730
21731 static const char *
21732 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21733 {
21734 struct attribute *attr;
21735 const char *base;
21736
21737 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21738 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21739 return NULL;
21740
21741 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21742 return NULL;
21743
21744 attr = dw2_linkage_name_attr (die, cu);
21745 if (attr == NULL || DW_STRING (attr) == NULL)
21746 return NULL;
21747
21748 /* dwarf2_name had to be already called. */
21749 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21750
21751 /* Strip the base name, keep any leading namespaces/classes. */
21752 base = strrchr (DW_STRING (attr), ':');
21753 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21754 return "";
21755
21756 struct objfile *objfile = cu->per_objfile->objfile;
21757 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21758 DW_STRING (attr),
21759 &base[-1] - DW_STRING (attr));
21760 }
21761
21762 /* Return the name of the namespace/class that DIE is defined within,
21763 or "" if we can't tell. The caller should not xfree the result.
21764
21765 For example, if we're within the method foo() in the following
21766 code:
21767
21768 namespace N {
21769 class C {
21770 void foo () {
21771 }
21772 };
21773 }
21774
21775 then determine_prefix on foo's die will return "N::C". */
21776
21777 static const char *
21778 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21779 {
21780 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21781 struct die_info *parent, *spec_die;
21782 struct dwarf2_cu *spec_cu;
21783 struct type *parent_type;
21784 const char *retval;
21785
21786 if (cu->language != language_cplus
21787 && cu->language != language_fortran && cu->language != language_d
21788 && cu->language != language_rust)
21789 return "";
21790
21791 retval = anonymous_struct_prefix (die, cu);
21792 if (retval)
21793 return retval;
21794
21795 /* We have to be careful in the presence of DW_AT_specification.
21796 For example, with GCC 3.4, given the code
21797
21798 namespace N {
21799 void foo() {
21800 // Definition of N::foo.
21801 }
21802 }
21803
21804 then we'll have a tree of DIEs like this:
21805
21806 1: DW_TAG_compile_unit
21807 2: DW_TAG_namespace // N
21808 3: DW_TAG_subprogram // declaration of N::foo
21809 4: DW_TAG_subprogram // definition of N::foo
21810 DW_AT_specification // refers to die #3
21811
21812 Thus, when processing die #4, we have to pretend that we're in
21813 the context of its DW_AT_specification, namely the contex of die
21814 #3. */
21815 spec_cu = cu;
21816 spec_die = die_specification (die, &spec_cu);
21817 if (spec_die == NULL)
21818 parent = die->parent;
21819 else
21820 {
21821 parent = spec_die->parent;
21822 cu = spec_cu;
21823 }
21824
21825 if (parent == NULL)
21826 return "";
21827 else if (parent->building_fullname)
21828 {
21829 const char *name;
21830 const char *parent_name;
21831
21832 /* It has been seen on RealView 2.2 built binaries,
21833 DW_TAG_template_type_param types actually _defined_ as
21834 children of the parent class:
21835
21836 enum E {};
21837 template class <class Enum> Class{};
21838 Class<enum E> class_e;
21839
21840 1: DW_TAG_class_type (Class)
21841 2: DW_TAG_enumeration_type (E)
21842 3: DW_TAG_enumerator (enum1:0)
21843 3: DW_TAG_enumerator (enum2:1)
21844 ...
21845 2: DW_TAG_template_type_param
21846 DW_AT_type DW_FORM_ref_udata (E)
21847
21848 Besides being broken debug info, it can put GDB into an
21849 infinite loop. Consider:
21850
21851 When we're building the full name for Class<E>, we'll start
21852 at Class, and go look over its template type parameters,
21853 finding E. We'll then try to build the full name of E, and
21854 reach here. We're now trying to build the full name of E,
21855 and look over the parent DIE for containing scope. In the
21856 broken case, if we followed the parent DIE of E, we'd again
21857 find Class, and once again go look at its template type
21858 arguments, etc., etc. Simply don't consider such parent die
21859 as source-level parent of this die (it can't be, the language
21860 doesn't allow it), and break the loop here. */
21861 name = dwarf2_name (die, cu);
21862 parent_name = dwarf2_name (parent, cu);
21863 complaint (_("template param type '%s' defined within parent '%s'"),
21864 name ? name : "<unknown>",
21865 parent_name ? parent_name : "<unknown>");
21866 return "";
21867 }
21868 else
21869 switch (parent->tag)
21870 {
21871 case DW_TAG_namespace:
21872 parent_type = read_type_die (parent, cu);
21873 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21874 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21875 Work around this problem here. */
21876 if (cu->language == language_cplus
21877 && strcmp (parent_type->name (), "::") == 0)
21878 return "";
21879 /* We give a name to even anonymous namespaces. */
21880 return parent_type->name ();
21881 case DW_TAG_class_type:
21882 case DW_TAG_interface_type:
21883 case DW_TAG_structure_type:
21884 case DW_TAG_union_type:
21885 case DW_TAG_module:
21886 parent_type = read_type_die (parent, cu);
21887 if (parent_type->name () != NULL)
21888 return parent_type->name ();
21889 else
21890 /* An anonymous structure is only allowed non-static data
21891 members; no typedefs, no member functions, et cetera.
21892 So it does not need a prefix. */
21893 return "";
21894 case DW_TAG_compile_unit:
21895 case DW_TAG_partial_unit:
21896 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21897 if (cu->language == language_cplus
21898 && !dwarf2_per_objfile->per_bfd->types.empty ()
21899 && die->child != NULL
21900 && (die->tag == DW_TAG_class_type
21901 || die->tag == DW_TAG_structure_type
21902 || die->tag == DW_TAG_union_type))
21903 {
21904 const char *name = guess_full_die_structure_name (die, cu);
21905 if (name != NULL)
21906 return name;
21907 }
21908 return "";
21909 case DW_TAG_subprogram:
21910 /* Nested subroutines in Fortran get a prefix with the name
21911 of the parent's subroutine. */
21912 if (cu->language == language_fortran)
21913 {
21914 if ((die->tag == DW_TAG_subprogram)
21915 && (dwarf2_name (parent, cu) != NULL))
21916 return dwarf2_name (parent, cu);
21917 }
21918 return determine_prefix (parent, cu);
21919 case DW_TAG_enumeration_type:
21920 parent_type = read_type_die (parent, cu);
21921 if (TYPE_DECLARED_CLASS (parent_type))
21922 {
21923 if (parent_type->name () != NULL)
21924 return parent_type->name ();
21925 return "";
21926 }
21927 /* Fall through. */
21928 default:
21929 return determine_prefix (parent, cu);
21930 }
21931 }
21932
21933 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21934 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21935 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21936 an obconcat, otherwise allocate storage for the result. The CU argument is
21937 used to determine the language and hence, the appropriate separator. */
21938
21939 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21940
21941 static char *
21942 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21943 int physname, struct dwarf2_cu *cu)
21944 {
21945 const char *lead = "";
21946 const char *sep;
21947
21948 if (suffix == NULL || suffix[0] == '\0'
21949 || prefix == NULL || prefix[0] == '\0')
21950 sep = "";
21951 else if (cu->language == language_d)
21952 {
21953 /* For D, the 'main' function could be defined in any module, but it
21954 should never be prefixed. */
21955 if (strcmp (suffix, "D main") == 0)
21956 {
21957 prefix = "";
21958 sep = "";
21959 }
21960 else
21961 sep = ".";
21962 }
21963 else if (cu->language == language_fortran && physname)
21964 {
21965 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21966 DW_AT_MIPS_linkage_name is preferred and used instead. */
21967
21968 lead = "__";
21969 sep = "_MOD_";
21970 }
21971 else
21972 sep = "::";
21973
21974 if (prefix == NULL)
21975 prefix = "";
21976 if (suffix == NULL)
21977 suffix = "";
21978
21979 if (obs == NULL)
21980 {
21981 char *retval
21982 = ((char *)
21983 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21984
21985 strcpy (retval, lead);
21986 strcat (retval, prefix);
21987 strcat (retval, sep);
21988 strcat (retval, suffix);
21989 return retval;
21990 }
21991 else
21992 {
21993 /* We have an obstack. */
21994 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21995 }
21996 }
21997
21998 /* Get name of a die, return NULL if not found. */
21999
22000 static const char *
22001 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22002 struct objfile *objfile)
22003 {
22004 if (name && cu->language == language_cplus)
22005 {
22006 gdb::unique_xmalloc_ptr<char> canon_name
22007 = cp_canonicalize_string (name);
22008
22009 if (canon_name != nullptr)
22010 name = objfile->intern (canon_name.get ());
22011 }
22012
22013 return name;
22014 }
22015
22016 /* Get name of a die, return NULL if not found.
22017 Anonymous namespaces are converted to their magic string. */
22018
22019 static const char *
22020 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22021 {
22022 struct attribute *attr;
22023 struct objfile *objfile = cu->per_objfile->objfile;
22024
22025 attr = dwarf2_attr (die, DW_AT_name, cu);
22026 if ((!attr || !DW_STRING (attr))
22027 && die->tag != DW_TAG_namespace
22028 && die->tag != DW_TAG_class_type
22029 && die->tag != DW_TAG_interface_type
22030 && die->tag != DW_TAG_structure_type
22031 && die->tag != DW_TAG_union_type)
22032 return NULL;
22033
22034 switch (die->tag)
22035 {
22036 case DW_TAG_compile_unit:
22037 case DW_TAG_partial_unit:
22038 /* Compilation units have a DW_AT_name that is a filename, not
22039 a source language identifier. */
22040 case DW_TAG_enumeration_type:
22041 case DW_TAG_enumerator:
22042 /* These tags always have simple identifiers already; no need
22043 to canonicalize them. */
22044 return DW_STRING (attr);
22045
22046 case DW_TAG_namespace:
22047 if (attr != NULL && DW_STRING (attr) != NULL)
22048 return DW_STRING (attr);
22049 return CP_ANONYMOUS_NAMESPACE_STR;
22050
22051 case DW_TAG_class_type:
22052 case DW_TAG_interface_type:
22053 case DW_TAG_structure_type:
22054 case DW_TAG_union_type:
22055 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22056 structures or unions. These were of the form "._%d" in GCC 4.1,
22057 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22058 and GCC 4.4. We work around this problem by ignoring these. */
22059 if (attr && DW_STRING (attr)
22060 && (startswith (DW_STRING (attr), "._")
22061 || startswith (DW_STRING (attr), "<anonymous")))
22062 return NULL;
22063
22064 /* GCC might emit a nameless typedef that has a linkage name. See
22065 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22066 if (!attr || DW_STRING (attr) == NULL)
22067 {
22068 attr = dw2_linkage_name_attr (die, cu);
22069 if (attr == NULL || DW_STRING (attr) == NULL)
22070 return NULL;
22071
22072 /* Avoid demangling DW_STRING (attr) the second time on a second
22073 call for the same DIE. */
22074 if (!DW_STRING_IS_CANONICAL (attr))
22075 {
22076 gdb::unique_xmalloc_ptr<char> demangled
22077 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22078 if (demangled == nullptr)
22079 return nullptr;
22080
22081 DW_STRING (attr) = objfile->intern (demangled.get ());
22082 DW_STRING_IS_CANONICAL (attr) = 1;
22083 }
22084
22085 /* Strip any leading namespaces/classes, keep only the base name.
22086 DW_AT_name for named DIEs does not contain the prefixes. */
22087 const char *base = strrchr (DW_STRING (attr), ':');
22088 if (base && base > DW_STRING (attr) && base[-1] == ':')
22089 return &base[1];
22090 else
22091 return DW_STRING (attr);
22092 }
22093 break;
22094
22095 default:
22096 break;
22097 }
22098
22099 if (!DW_STRING_IS_CANONICAL (attr))
22100 {
22101 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22102 objfile);
22103 DW_STRING_IS_CANONICAL (attr) = 1;
22104 }
22105 return DW_STRING (attr);
22106 }
22107
22108 /* Return the die that this die in an extension of, or NULL if there
22109 is none. *EXT_CU is the CU containing DIE on input, and the CU
22110 containing the return value on output. */
22111
22112 static struct die_info *
22113 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22114 {
22115 struct attribute *attr;
22116
22117 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22118 if (attr == NULL)
22119 return NULL;
22120
22121 return follow_die_ref (die, attr, ext_cu);
22122 }
22123
22124 static void
22125 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22126 {
22127 unsigned int i;
22128
22129 print_spaces (indent, f);
22130 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22131 dwarf_tag_name (die->tag), die->abbrev,
22132 sect_offset_str (die->sect_off));
22133
22134 if (die->parent != NULL)
22135 {
22136 print_spaces (indent, f);
22137 fprintf_unfiltered (f, " parent at offset: %s\n",
22138 sect_offset_str (die->parent->sect_off));
22139 }
22140
22141 print_spaces (indent, f);
22142 fprintf_unfiltered (f, " has children: %s\n",
22143 dwarf_bool_name (die->child != NULL));
22144
22145 print_spaces (indent, f);
22146 fprintf_unfiltered (f, " attributes:\n");
22147
22148 for (i = 0; i < die->num_attrs; ++i)
22149 {
22150 print_spaces (indent, f);
22151 fprintf_unfiltered (f, " %s (%s) ",
22152 dwarf_attr_name (die->attrs[i].name),
22153 dwarf_form_name (die->attrs[i].form));
22154
22155 switch (die->attrs[i].form)
22156 {
22157 case DW_FORM_addr:
22158 case DW_FORM_addrx:
22159 case DW_FORM_GNU_addr_index:
22160 fprintf_unfiltered (f, "address: ");
22161 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22162 break;
22163 case DW_FORM_block2:
22164 case DW_FORM_block4:
22165 case DW_FORM_block:
22166 case DW_FORM_block1:
22167 fprintf_unfiltered (f, "block: size %s",
22168 pulongest (DW_BLOCK (&die->attrs[i])->size));
22169 break;
22170 case DW_FORM_exprloc:
22171 fprintf_unfiltered (f, "expression: size %s",
22172 pulongest (DW_BLOCK (&die->attrs[i])->size));
22173 break;
22174 case DW_FORM_data16:
22175 fprintf_unfiltered (f, "constant of 16 bytes");
22176 break;
22177 case DW_FORM_ref_addr:
22178 fprintf_unfiltered (f, "ref address: ");
22179 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22180 break;
22181 case DW_FORM_GNU_ref_alt:
22182 fprintf_unfiltered (f, "alt ref address: ");
22183 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22184 break;
22185 case DW_FORM_ref1:
22186 case DW_FORM_ref2:
22187 case DW_FORM_ref4:
22188 case DW_FORM_ref8:
22189 case DW_FORM_ref_udata:
22190 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22191 (long) (DW_UNSND (&die->attrs[i])));
22192 break;
22193 case DW_FORM_data1:
22194 case DW_FORM_data2:
22195 case DW_FORM_data4:
22196 case DW_FORM_data8:
22197 case DW_FORM_udata:
22198 case DW_FORM_sdata:
22199 fprintf_unfiltered (f, "constant: %s",
22200 pulongest (DW_UNSND (&die->attrs[i])));
22201 break;
22202 case DW_FORM_sec_offset:
22203 fprintf_unfiltered (f, "section offset: %s",
22204 pulongest (DW_UNSND (&die->attrs[i])));
22205 break;
22206 case DW_FORM_ref_sig8:
22207 fprintf_unfiltered (f, "signature: %s",
22208 hex_string (DW_SIGNATURE (&die->attrs[i])));
22209 break;
22210 case DW_FORM_string:
22211 case DW_FORM_strp:
22212 case DW_FORM_line_strp:
22213 case DW_FORM_strx:
22214 case DW_FORM_GNU_str_index:
22215 case DW_FORM_GNU_strp_alt:
22216 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22217 DW_STRING (&die->attrs[i])
22218 ? DW_STRING (&die->attrs[i]) : "",
22219 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22220 break;
22221 case DW_FORM_flag:
22222 if (DW_UNSND (&die->attrs[i]))
22223 fprintf_unfiltered (f, "flag: TRUE");
22224 else
22225 fprintf_unfiltered (f, "flag: FALSE");
22226 break;
22227 case DW_FORM_flag_present:
22228 fprintf_unfiltered (f, "flag: TRUE");
22229 break;
22230 case DW_FORM_indirect:
22231 /* The reader will have reduced the indirect form to
22232 the "base form" so this form should not occur. */
22233 fprintf_unfiltered (f,
22234 "unexpected attribute form: DW_FORM_indirect");
22235 break;
22236 case DW_FORM_implicit_const:
22237 fprintf_unfiltered (f, "constant: %s",
22238 plongest (DW_SND (&die->attrs[i])));
22239 break;
22240 default:
22241 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22242 die->attrs[i].form);
22243 break;
22244 }
22245 fprintf_unfiltered (f, "\n");
22246 }
22247 }
22248
22249 static void
22250 dump_die_for_error (struct die_info *die)
22251 {
22252 dump_die_shallow (gdb_stderr, 0, die);
22253 }
22254
22255 static void
22256 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22257 {
22258 int indent = level * 4;
22259
22260 gdb_assert (die != NULL);
22261
22262 if (level >= max_level)
22263 return;
22264
22265 dump_die_shallow (f, indent, die);
22266
22267 if (die->child != NULL)
22268 {
22269 print_spaces (indent, f);
22270 fprintf_unfiltered (f, " Children:");
22271 if (level + 1 < max_level)
22272 {
22273 fprintf_unfiltered (f, "\n");
22274 dump_die_1 (f, level + 1, max_level, die->child);
22275 }
22276 else
22277 {
22278 fprintf_unfiltered (f,
22279 " [not printed, max nesting level reached]\n");
22280 }
22281 }
22282
22283 if (die->sibling != NULL && level > 0)
22284 {
22285 dump_die_1 (f, level, max_level, die->sibling);
22286 }
22287 }
22288
22289 /* This is called from the pdie macro in gdbinit.in.
22290 It's not static so gcc will keep a copy callable from gdb. */
22291
22292 void
22293 dump_die (struct die_info *die, int max_level)
22294 {
22295 dump_die_1 (gdb_stdlog, 0, max_level, die);
22296 }
22297
22298 static void
22299 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22300 {
22301 void **slot;
22302
22303 slot = htab_find_slot_with_hash (cu->die_hash, die,
22304 to_underlying (die->sect_off),
22305 INSERT);
22306
22307 *slot = die;
22308 }
22309
22310 /* Follow reference or signature attribute ATTR of SRC_DIE.
22311 On entry *REF_CU is the CU of SRC_DIE.
22312 On exit *REF_CU is the CU of the result. */
22313
22314 static struct die_info *
22315 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22316 struct dwarf2_cu **ref_cu)
22317 {
22318 struct die_info *die;
22319
22320 if (attr->form_is_ref ())
22321 die = follow_die_ref (src_die, attr, ref_cu);
22322 else if (attr->form == DW_FORM_ref_sig8)
22323 die = follow_die_sig (src_die, attr, ref_cu);
22324 else
22325 {
22326 dump_die_for_error (src_die);
22327 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22328 objfile_name ((*ref_cu)->per_objfile->objfile));
22329 }
22330
22331 return die;
22332 }
22333
22334 /* Follow reference OFFSET.
22335 On entry *REF_CU is the CU of the source die referencing OFFSET.
22336 On exit *REF_CU is the CU of the result.
22337 Returns NULL if OFFSET is invalid. */
22338
22339 static struct die_info *
22340 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22341 struct dwarf2_cu **ref_cu)
22342 {
22343 struct die_info temp_die;
22344 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22345 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22346
22347 gdb_assert (cu->per_cu != NULL);
22348
22349 target_cu = cu;
22350
22351 if (cu->per_cu->is_debug_types)
22352 {
22353 /* .debug_types CUs cannot reference anything outside their CU.
22354 If they need to, they have to reference a signatured type via
22355 DW_FORM_ref_sig8. */
22356 if (!cu->header.offset_in_cu_p (sect_off))
22357 return NULL;
22358 }
22359 else if (offset_in_dwz != cu->per_cu->is_dwz
22360 || !cu->header.offset_in_cu_p (sect_off))
22361 {
22362 struct dwarf2_per_cu_data *per_cu;
22363
22364 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22365 dwarf2_per_objfile);
22366
22367 /* If necessary, add it to the queue and load its DIEs. */
22368 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
22369 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22370
22371 target_cu = dwarf2_per_objfile->get_cu (per_cu);
22372 }
22373 else if (cu->dies == NULL)
22374 {
22375 /* We're loading full DIEs during partial symbol reading. */
22376 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22377 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22378 language_minimal);
22379 }
22380
22381 *ref_cu = target_cu;
22382 temp_die.sect_off = sect_off;
22383
22384 if (target_cu != cu)
22385 target_cu->ancestor = cu;
22386
22387 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22388 &temp_die,
22389 to_underlying (sect_off));
22390 }
22391
22392 /* Follow reference attribute ATTR of SRC_DIE.
22393 On entry *REF_CU is the CU of SRC_DIE.
22394 On exit *REF_CU is the CU of the result. */
22395
22396 static struct die_info *
22397 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22398 struct dwarf2_cu **ref_cu)
22399 {
22400 sect_offset sect_off = attr->get_ref_die_offset ();
22401 struct dwarf2_cu *cu = *ref_cu;
22402 struct die_info *die;
22403
22404 die = follow_die_offset (sect_off,
22405 (attr->form == DW_FORM_GNU_ref_alt
22406 || cu->per_cu->is_dwz),
22407 ref_cu);
22408 if (!die)
22409 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22410 "at %s [in module %s]"),
22411 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22412 objfile_name (cu->per_objfile->objfile));
22413
22414 return die;
22415 }
22416
22417 /* See read.h. */
22418
22419 struct dwarf2_locexpr_baton
22420 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22421 dwarf2_per_cu_data *per_cu,
22422 dwarf2_per_objfile *dwarf2_per_objfile,
22423 CORE_ADDR (*get_frame_pc) (void *baton),
22424 void *baton, bool resolve_abstract_p)
22425 {
22426 struct die_info *die;
22427 struct attribute *attr;
22428 struct dwarf2_locexpr_baton retval;
22429 struct objfile *objfile = dwarf2_per_objfile->objfile;
22430
22431 dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
22432 if (cu == nullptr)
22433 cu = load_cu (per_cu, dwarf2_per_objfile, false);
22434
22435 if (cu == nullptr)
22436 {
22437 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22438 Instead just throw an error, not much else we can do. */
22439 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22440 sect_offset_str (sect_off), objfile_name (objfile));
22441 }
22442
22443 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22444 if (!die)
22445 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22446 sect_offset_str (sect_off), objfile_name (objfile));
22447
22448 attr = dwarf2_attr (die, DW_AT_location, cu);
22449 if (!attr && resolve_abstract_p
22450 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22451 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22452 {
22453 CORE_ADDR pc = (*get_frame_pc) (baton);
22454 CORE_ADDR baseaddr = objfile->text_section_offset ();
22455 struct gdbarch *gdbarch = objfile->arch ();
22456
22457 for (const auto &cand_off
22458 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22459 {
22460 struct dwarf2_cu *cand_cu = cu;
22461 struct die_info *cand
22462 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22463 if (!cand
22464 || !cand->parent
22465 || cand->parent->tag != DW_TAG_subprogram)
22466 continue;
22467
22468 CORE_ADDR pc_low, pc_high;
22469 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22470 if (pc_low == ((CORE_ADDR) -1))
22471 continue;
22472 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22473 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22474 if (!(pc_low <= pc && pc < pc_high))
22475 continue;
22476
22477 die = cand;
22478 attr = dwarf2_attr (die, DW_AT_location, cu);
22479 break;
22480 }
22481 }
22482
22483 if (!attr)
22484 {
22485 /* DWARF: "If there is no such attribute, then there is no effect.".
22486 DATA is ignored if SIZE is 0. */
22487
22488 retval.data = NULL;
22489 retval.size = 0;
22490 }
22491 else if (attr->form_is_section_offset ())
22492 {
22493 struct dwarf2_loclist_baton loclist_baton;
22494 CORE_ADDR pc = (*get_frame_pc) (baton);
22495 size_t size;
22496
22497 fill_in_loclist_baton (cu, &loclist_baton, attr);
22498
22499 retval.data = dwarf2_find_location_expression (&loclist_baton,
22500 &size, pc);
22501 retval.size = size;
22502 }
22503 else
22504 {
22505 if (!attr->form_is_block ())
22506 error (_("Dwarf Error: DIE at %s referenced in module %s "
22507 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22508 sect_offset_str (sect_off), objfile_name (objfile));
22509
22510 retval.data = DW_BLOCK (attr)->data;
22511 retval.size = DW_BLOCK (attr)->size;
22512 }
22513 retval.per_objfile = dwarf2_per_objfile;
22514 retval.per_cu = cu->per_cu;
22515
22516 dwarf2_per_objfile->age_comp_units ();
22517
22518 return retval;
22519 }
22520
22521 /* See read.h. */
22522
22523 struct dwarf2_locexpr_baton
22524 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22525 dwarf2_per_cu_data *per_cu,
22526 dwarf2_per_objfile *per_objfile,
22527 CORE_ADDR (*get_frame_pc) (void *baton),
22528 void *baton)
22529 {
22530 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22531
22532 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22533 get_frame_pc, baton);
22534 }
22535
22536 /* Write a constant of a given type as target-ordered bytes into
22537 OBSTACK. */
22538
22539 static const gdb_byte *
22540 write_constant_as_bytes (struct obstack *obstack,
22541 enum bfd_endian byte_order,
22542 struct type *type,
22543 ULONGEST value,
22544 LONGEST *len)
22545 {
22546 gdb_byte *result;
22547
22548 *len = TYPE_LENGTH (type);
22549 result = (gdb_byte *) obstack_alloc (obstack, *len);
22550 store_unsigned_integer (result, *len, byte_order, value);
22551
22552 return result;
22553 }
22554
22555 /* See read.h. */
22556
22557 const gdb_byte *
22558 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22559 dwarf2_per_cu_data *per_cu,
22560 dwarf2_per_objfile *per_objfile,
22561 obstack *obstack,
22562 LONGEST *len)
22563 {
22564 struct die_info *die;
22565 struct attribute *attr;
22566 const gdb_byte *result = NULL;
22567 struct type *type;
22568 LONGEST value;
22569 enum bfd_endian byte_order;
22570 struct objfile *objfile = per_objfile->objfile;
22571
22572 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22573 if (cu == nullptr)
22574 cu = load_cu (per_cu, per_objfile, false);
22575
22576 if (cu == nullptr)
22577 {
22578 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22579 Instead just throw an error, not much else we can do. */
22580 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22581 sect_offset_str (sect_off), objfile_name (objfile));
22582 }
22583
22584 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22585 if (!die)
22586 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22587 sect_offset_str (sect_off), objfile_name (objfile));
22588
22589 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22590 if (attr == NULL)
22591 return NULL;
22592
22593 byte_order = (bfd_big_endian (objfile->obfd)
22594 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22595
22596 switch (attr->form)
22597 {
22598 case DW_FORM_addr:
22599 case DW_FORM_addrx:
22600 case DW_FORM_GNU_addr_index:
22601 {
22602 gdb_byte *tem;
22603
22604 *len = cu->header.addr_size;
22605 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22606 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22607 result = tem;
22608 }
22609 break;
22610 case DW_FORM_string:
22611 case DW_FORM_strp:
22612 case DW_FORM_strx:
22613 case DW_FORM_GNU_str_index:
22614 case DW_FORM_GNU_strp_alt:
22615 /* DW_STRING is already allocated on the objfile obstack, point
22616 directly to it. */
22617 result = (const gdb_byte *) DW_STRING (attr);
22618 *len = strlen (DW_STRING (attr));
22619 break;
22620 case DW_FORM_block1:
22621 case DW_FORM_block2:
22622 case DW_FORM_block4:
22623 case DW_FORM_block:
22624 case DW_FORM_exprloc:
22625 case DW_FORM_data16:
22626 result = DW_BLOCK (attr)->data;
22627 *len = DW_BLOCK (attr)->size;
22628 break;
22629
22630 /* The DW_AT_const_value attributes are supposed to carry the
22631 symbol's value "represented as it would be on the target
22632 architecture." By the time we get here, it's already been
22633 converted to host endianness, so we just need to sign- or
22634 zero-extend it as appropriate. */
22635 case DW_FORM_data1:
22636 type = die_type (die, cu);
22637 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22638 if (result == NULL)
22639 result = write_constant_as_bytes (obstack, byte_order,
22640 type, value, len);
22641 break;
22642 case DW_FORM_data2:
22643 type = die_type (die, cu);
22644 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22645 if (result == NULL)
22646 result = write_constant_as_bytes (obstack, byte_order,
22647 type, value, len);
22648 break;
22649 case DW_FORM_data4:
22650 type = die_type (die, cu);
22651 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22652 if (result == NULL)
22653 result = write_constant_as_bytes (obstack, byte_order,
22654 type, value, len);
22655 break;
22656 case DW_FORM_data8:
22657 type = die_type (die, cu);
22658 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22659 if (result == NULL)
22660 result = write_constant_as_bytes (obstack, byte_order,
22661 type, value, len);
22662 break;
22663
22664 case DW_FORM_sdata:
22665 case DW_FORM_implicit_const:
22666 type = die_type (die, cu);
22667 result = write_constant_as_bytes (obstack, byte_order,
22668 type, DW_SND (attr), len);
22669 break;
22670
22671 case DW_FORM_udata:
22672 type = die_type (die, cu);
22673 result = write_constant_as_bytes (obstack, byte_order,
22674 type, DW_UNSND (attr), len);
22675 break;
22676
22677 default:
22678 complaint (_("unsupported const value attribute form: '%s'"),
22679 dwarf_form_name (attr->form));
22680 break;
22681 }
22682
22683 return result;
22684 }
22685
22686 /* See read.h. */
22687
22688 struct type *
22689 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22690 dwarf2_per_cu_data *per_cu,
22691 dwarf2_per_objfile *per_objfile)
22692 {
22693 struct die_info *die;
22694
22695 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22696 if (cu == nullptr)
22697 cu = load_cu (per_cu, per_objfile, false);
22698
22699 if (cu == nullptr)
22700 return nullptr;
22701
22702 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22703 if (!die)
22704 return NULL;
22705
22706 return die_type (die, cu);
22707 }
22708
22709 /* See read.h. */
22710
22711 struct type *
22712 dwarf2_get_die_type (cu_offset die_offset,
22713 dwarf2_per_cu_data *per_cu,
22714 dwarf2_per_objfile *per_objfile)
22715 {
22716 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22717 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22718 }
22719
22720 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22721 On entry *REF_CU is the CU of SRC_DIE.
22722 On exit *REF_CU is the CU of the result.
22723 Returns NULL if the referenced DIE isn't found. */
22724
22725 static struct die_info *
22726 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22727 struct dwarf2_cu **ref_cu)
22728 {
22729 struct die_info temp_die;
22730 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22731 struct die_info *die;
22732 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22733
22734
22735 /* While it might be nice to assert sig_type->type == NULL here,
22736 we can get here for DW_AT_imported_declaration where we need
22737 the DIE not the type. */
22738
22739 /* If necessary, add it to the queue and load its DIEs. */
22740
22741 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22742 language_minimal))
22743 read_signatured_type (sig_type, dwarf2_per_objfile);
22744
22745 sig_cu = dwarf2_per_objfile->get_cu (&sig_type->per_cu);
22746 gdb_assert (sig_cu != NULL);
22747 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22748 temp_die.sect_off = sig_type->type_offset_in_section;
22749 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22750 to_underlying (temp_die.sect_off));
22751 if (die)
22752 {
22753 /* For .gdb_index version 7 keep track of included TUs.
22754 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22755 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22756 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22757 {
22758 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22759 }
22760
22761 *ref_cu = sig_cu;
22762 if (sig_cu != cu)
22763 sig_cu->ancestor = cu;
22764
22765 return die;
22766 }
22767
22768 return NULL;
22769 }
22770
22771 /* Follow signatured type referenced by ATTR in SRC_DIE.
22772 On entry *REF_CU is the CU of SRC_DIE.
22773 On exit *REF_CU is the CU of the result.
22774 The result is the DIE of the type.
22775 If the referenced type cannot be found an error is thrown. */
22776
22777 static struct die_info *
22778 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22779 struct dwarf2_cu **ref_cu)
22780 {
22781 ULONGEST signature = DW_SIGNATURE (attr);
22782 struct signatured_type *sig_type;
22783 struct die_info *die;
22784
22785 gdb_assert (attr->form == DW_FORM_ref_sig8);
22786
22787 sig_type = lookup_signatured_type (*ref_cu, signature);
22788 /* sig_type will be NULL if the signatured type is missing from
22789 the debug info. */
22790 if (sig_type == NULL)
22791 {
22792 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22793 " from DIE at %s [in module %s]"),
22794 hex_string (signature), sect_offset_str (src_die->sect_off),
22795 objfile_name ((*ref_cu)->per_objfile->objfile));
22796 }
22797
22798 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22799 if (die == NULL)
22800 {
22801 dump_die_for_error (src_die);
22802 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22803 " from DIE at %s [in module %s]"),
22804 hex_string (signature), sect_offset_str (src_die->sect_off),
22805 objfile_name ((*ref_cu)->per_objfile->objfile));
22806 }
22807
22808 return die;
22809 }
22810
22811 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22812 reading in and processing the type unit if necessary. */
22813
22814 static struct type *
22815 get_signatured_type (struct die_info *die, ULONGEST signature,
22816 struct dwarf2_cu *cu)
22817 {
22818 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22819 struct signatured_type *sig_type;
22820 struct dwarf2_cu *type_cu;
22821 struct die_info *type_die;
22822 struct type *type;
22823
22824 sig_type = lookup_signatured_type (cu, signature);
22825 /* sig_type will be NULL if the signatured type is missing from
22826 the debug info. */
22827 if (sig_type == NULL)
22828 {
22829 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22830 " from DIE at %s [in module %s]"),
22831 hex_string (signature), sect_offset_str (die->sect_off),
22832 objfile_name (dwarf2_per_objfile->objfile));
22833 return build_error_marker_type (cu, die);
22834 }
22835
22836 /* If we already know the type we're done. */
22837 type = dwarf2_per_objfile->get_type_for_signatured_type (sig_type);
22838 if (type != nullptr)
22839 return type;
22840
22841 type_cu = cu;
22842 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22843 if (type_die != NULL)
22844 {
22845 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22846 is created. This is important, for example, because for c++ classes
22847 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22848 type = read_type_die (type_die, type_cu);
22849 if (type == NULL)
22850 {
22851 complaint (_("Dwarf Error: Cannot build signatured type %s"
22852 " referenced from DIE at %s [in module %s]"),
22853 hex_string (signature), sect_offset_str (die->sect_off),
22854 objfile_name (dwarf2_per_objfile->objfile));
22855 type = build_error_marker_type (cu, die);
22856 }
22857 }
22858 else
22859 {
22860 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22861 " from DIE at %s [in module %s]"),
22862 hex_string (signature), sect_offset_str (die->sect_off),
22863 objfile_name (dwarf2_per_objfile->objfile));
22864 type = build_error_marker_type (cu, die);
22865 }
22866
22867 dwarf2_per_objfile->set_type_for_signatured_type (sig_type, type);
22868
22869 return type;
22870 }
22871
22872 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22873 reading in and processing the type unit if necessary. */
22874
22875 static struct type *
22876 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22877 struct dwarf2_cu *cu) /* ARI: editCase function */
22878 {
22879 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22880 if (attr->form_is_ref ())
22881 {
22882 struct dwarf2_cu *type_cu = cu;
22883 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22884
22885 return read_type_die (type_die, type_cu);
22886 }
22887 else if (attr->form == DW_FORM_ref_sig8)
22888 {
22889 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22890 }
22891 else
22892 {
22893 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22894
22895 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22896 " at %s [in module %s]"),
22897 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22898 objfile_name (dwarf2_per_objfile->objfile));
22899 return build_error_marker_type (cu, die);
22900 }
22901 }
22902
22903 /* Load the DIEs associated with type unit PER_CU into memory. */
22904
22905 static void
22906 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22907 dwarf2_per_objfile *per_objfile)
22908 {
22909 struct signatured_type *sig_type;
22910
22911 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22912 gdb_assert (! per_cu->type_unit_group_p ());
22913
22914 /* We have the per_cu, but we need the signatured_type.
22915 Fortunately this is an easy translation. */
22916 gdb_assert (per_cu->is_debug_types);
22917 sig_type = (struct signatured_type *) per_cu;
22918
22919 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22920
22921 read_signatured_type (sig_type, per_objfile);
22922
22923 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22924 }
22925
22926 /* Read in a signatured type and build its CU and DIEs.
22927 If the type is a stub for the real type in a DWO file,
22928 read in the real type from the DWO file as well. */
22929
22930 static void
22931 read_signatured_type (signatured_type *sig_type,
22932 dwarf2_per_objfile *per_objfile)
22933 {
22934 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22935
22936 gdb_assert (per_cu->is_debug_types);
22937 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22938
22939 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
22940
22941 if (!reader.dummy_p)
22942 {
22943 struct dwarf2_cu *cu = reader.cu;
22944 const gdb_byte *info_ptr = reader.info_ptr;
22945
22946 gdb_assert (cu->die_hash == NULL);
22947 cu->die_hash =
22948 htab_create_alloc_ex (cu->header.length / 12,
22949 die_hash,
22950 die_eq,
22951 NULL,
22952 &cu->comp_unit_obstack,
22953 hashtab_obstack_allocate,
22954 dummy_obstack_deallocate);
22955
22956 if (reader.comp_unit_die->has_children)
22957 reader.comp_unit_die->child
22958 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22959 reader.comp_unit_die);
22960 cu->dies = reader.comp_unit_die;
22961 /* comp_unit_die is not stored in die_hash, no need. */
22962
22963 /* We try not to read any attributes in this function, because
22964 not all CUs needed for references have been loaded yet, and
22965 symbol table processing isn't initialized. But we have to
22966 set the CU language, or we won't be able to build types
22967 correctly. Similarly, if we do not read the producer, we can
22968 not apply producer-specific interpretation. */
22969 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22970
22971 reader.keep ();
22972 }
22973
22974 sig_type->per_cu.tu_read = 1;
22975 }
22976
22977 /* Decode simple location descriptions.
22978 Given a pointer to a dwarf block that defines a location, compute
22979 the location and return the value. If COMPUTED is non-null, it is
22980 set to true to indicate that decoding was successful, and false
22981 otherwise. If COMPUTED is null, then this function may emit a
22982 complaint. */
22983
22984 static CORE_ADDR
22985 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22986 {
22987 struct objfile *objfile = cu->per_objfile->objfile;
22988 size_t i;
22989 size_t size = blk->size;
22990 const gdb_byte *data = blk->data;
22991 CORE_ADDR stack[64];
22992 int stacki;
22993 unsigned int bytes_read, unsnd;
22994 gdb_byte op;
22995
22996 if (computed != nullptr)
22997 *computed = false;
22998
22999 i = 0;
23000 stacki = 0;
23001 stack[stacki] = 0;
23002 stack[++stacki] = 0;
23003
23004 while (i < size)
23005 {
23006 op = data[i++];
23007 switch (op)
23008 {
23009 case DW_OP_lit0:
23010 case DW_OP_lit1:
23011 case DW_OP_lit2:
23012 case DW_OP_lit3:
23013 case DW_OP_lit4:
23014 case DW_OP_lit5:
23015 case DW_OP_lit6:
23016 case DW_OP_lit7:
23017 case DW_OP_lit8:
23018 case DW_OP_lit9:
23019 case DW_OP_lit10:
23020 case DW_OP_lit11:
23021 case DW_OP_lit12:
23022 case DW_OP_lit13:
23023 case DW_OP_lit14:
23024 case DW_OP_lit15:
23025 case DW_OP_lit16:
23026 case DW_OP_lit17:
23027 case DW_OP_lit18:
23028 case DW_OP_lit19:
23029 case DW_OP_lit20:
23030 case DW_OP_lit21:
23031 case DW_OP_lit22:
23032 case DW_OP_lit23:
23033 case DW_OP_lit24:
23034 case DW_OP_lit25:
23035 case DW_OP_lit26:
23036 case DW_OP_lit27:
23037 case DW_OP_lit28:
23038 case DW_OP_lit29:
23039 case DW_OP_lit30:
23040 case DW_OP_lit31:
23041 stack[++stacki] = op - DW_OP_lit0;
23042 break;
23043
23044 case DW_OP_reg0:
23045 case DW_OP_reg1:
23046 case DW_OP_reg2:
23047 case DW_OP_reg3:
23048 case DW_OP_reg4:
23049 case DW_OP_reg5:
23050 case DW_OP_reg6:
23051 case DW_OP_reg7:
23052 case DW_OP_reg8:
23053 case DW_OP_reg9:
23054 case DW_OP_reg10:
23055 case DW_OP_reg11:
23056 case DW_OP_reg12:
23057 case DW_OP_reg13:
23058 case DW_OP_reg14:
23059 case DW_OP_reg15:
23060 case DW_OP_reg16:
23061 case DW_OP_reg17:
23062 case DW_OP_reg18:
23063 case DW_OP_reg19:
23064 case DW_OP_reg20:
23065 case DW_OP_reg21:
23066 case DW_OP_reg22:
23067 case DW_OP_reg23:
23068 case DW_OP_reg24:
23069 case DW_OP_reg25:
23070 case DW_OP_reg26:
23071 case DW_OP_reg27:
23072 case DW_OP_reg28:
23073 case DW_OP_reg29:
23074 case DW_OP_reg30:
23075 case DW_OP_reg31:
23076 stack[++stacki] = op - DW_OP_reg0;
23077 if (i < size)
23078 {
23079 if (computed == nullptr)
23080 dwarf2_complex_location_expr_complaint ();
23081 else
23082 return 0;
23083 }
23084 break;
23085
23086 case DW_OP_regx:
23087 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23088 i += bytes_read;
23089 stack[++stacki] = unsnd;
23090 if (i < size)
23091 {
23092 if (computed == nullptr)
23093 dwarf2_complex_location_expr_complaint ();
23094 else
23095 return 0;
23096 }
23097 break;
23098
23099 case DW_OP_addr:
23100 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23101 &bytes_read);
23102 i += bytes_read;
23103 break;
23104
23105 case DW_OP_const1u:
23106 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23107 i += 1;
23108 break;
23109
23110 case DW_OP_const1s:
23111 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23112 i += 1;
23113 break;
23114
23115 case DW_OP_const2u:
23116 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23117 i += 2;
23118 break;
23119
23120 case DW_OP_const2s:
23121 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23122 i += 2;
23123 break;
23124
23125 case DW_OP_const4u:
23126 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23127 i += 4;
23128 break;
23129
23130 case DW_OP_const4s:
23131 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23132 i += 4;
23133 break;
23134
23135 case DW_OP_const8u:
23136 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23137 i += 8;
23138 break;
23139
23140 case DW_OP_constu:
23141 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23142 &bytes_read);
23143 i += bytes_read;
23144 break;
23145
23146 case DW_OP_consts:
23147 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23148 i += bytes_read;
23149 break;
23150
23151 case DW_OP_dup:
23152 stack[stacki + 1] = stack[stacki];
23153 stacki++;
23154 break;
23155
23156 case DW_OP_plus:
23157 stack[stacki - 1] += stack[stacki];
23158 stacki--;
23159 break;
23160
23161 case DW_OP_plus_uconst:
23162 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23163 &bytes_read);
23164 i += bytes_read;
23165 break;
23166
23167 case DW_OP_minus:
23168 stack[stacki - 1] -= stack[stacki];
23169 stacki--;
23170 break;
23171
23172 case DW_OP_deref:
23173 /* If we're not the last op, then we definitely can't encode
23174 this using GDB's address_class enum. This is valid for partial
23175 global symbols, although the variable's address will be bogus
23176 in the psymtab. */
23177 if (i < size)
23178 {
23179 if (computed == nullptr)
23180 dwarf2_complex_location_expr_complaint ();
23181 else
23182 return 0;
23183 }
23184 break;
23185
23186 case DW_OP_GNU_push_tls_address:
23187 case DW_OP_form_tls_address:
23188 /* The top of the stack has the offset from the beginning
23189 of the thread control block at which the variable is located. */
23190 /* Nothing should follow this operator, so the top of stack would
23191 be returned. */
23192 /* This is valid for partial global symbols, but the variable's
23193 address will be bogus in the psymtab. Make it always at least
23194 non-zero to not look as a variable garbage collected by linker
23195 which have DW_OP_addr 0. */
23196 if (i < size)
23197 {
23198 if (computed == nullptr)
23199 dwarf2_complex_location_expr_complaint ();
23200 else
23201 return 0;
23202 }
23203 stack[stacki]++;
23204 break;
23205
23206 case DW_OP_GNU_uninit:
23207 if (computed != nullptr)
23208 return 0;
23209 break;
23210
23211 case DW_OP_addrx:
23212 case DW_OP_GNU_addr_index:
23213 case DW_OP_GNU_const_index:
23214 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23215 &bytes_read);
23216 i += bytes_read;
23217 break;
23218
23219 default:
23220 if (computed == nullptr)
23221 {
23222 const char *name = get_DW_OP_name (op);
23223
23224 if (name)
23225 complaint (_("unsupported stack op: '%s'"),
23226 name);
23227 else
23228 complaint (_("unsupported stack op: '%02x'"),
23229 op);
23230 }
23231
23232 return (stack[stacki]);
23233 }
23234
23235 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23236 outside of the allocated space. Also enforce minimum>0. */
23237 if (stacki >= ARRAY_SIZE (stack) - 1)
23238 {
23239 if (computed == nullptr)
23240 complaint (_("location description stack overflow"));
23241 return 0;
23242 }
23243
23244 if (stacki <= 0)
23245 {
23246 if (computed == nullptr)
23247 complaint (_("location description stack underflow"));
23248 return 0;
23249 }
23250 }
23251
23252 if (computed != nullptr)
23253 *computed = true;
23254 return (stack[stacki]);
23255 }
23256
23257 /* memory allocation interface */
23258
23259 static struct dwarf_block *
23260 dwarf_alloc_block (struct dwarf2_cu *cu)
23261 {
23262 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23263 }
23264
23265 static struct die_info *
23266 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23267 {
23268 struct die_info *die;
23269 size_t size = sizeof (struct die_info);
23270
23271 if (num_attrs > 1)
23272 size += (num_attrs - 1) * sizeof (struct attribute);
23273
23274 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23275 memset (die, 0, sizeof (struct die_info));
23276 return (die);
23277 }
23278
23279 \f
23280
23281 /* Macro support. */
23282
23283 /* An overload of dwarf_decode_macros that finds the correct section
23284 and ensures it is read in before calling the other overload. */
23285
23286 static void
23287 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23288 int section_is_gnu)
23289 {
23290 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23291 struct objfile *objfile = dwarf2_per_objfile->objfile;
23292 const struct line_header *lh = cu->line_header;
23293 unsigned int offset_size = cu->header.offset_size;
23294 struct dwarf2_section_info *section;
23295 const char *section_name;
23296
23297 if (cu->dwo_unit != nullptr)
23298 {
23299 if (section_is_gnu)
23300 {
23301 section = &cu->dwo_unit->dwo_file->sections.macro;
23302 section_name = ".debug_macro.dwo";
23303 }
23304 else
23305 {
23306 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23307 section_name = ".debug_macinfo.dwo";
23308 }
23309 }
23310 else
23311 {
23312 if (section_is_gnu)
23313 {
23314 section = &dwarf2_per_objfile->per_bfd->macro;
23315 section_name = ".debug_macro";
23316 }
23317 else
23318 {
23319 section = &dwarf2_per_objfile->per_bfd->macinfo;
23320 section_name = ".debug_macinfo";
23321 }
23322 }
23323
23324 section->read (objfile);
23325 if (section->buffer == nullptr)
23326 {
23327 complaint (_("missing %s section"), section_name);
23328 return;
23329 }
23330
23331 buildsym_compunit *builder = cu->get_builder ();
23332
23333 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23334 offset_size, offset, section_is_gnu);
23335 }
23336
23337 /* Return the .debug_loc section to use for CU.
23338 For DWO files use .debug_loc.dwo. */
23339
23340 static struct dwarf2_section_info *
23341 cu_debug_loc_section (struct dwarf2_cu *cu)
23342 {
23343 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23344
23345 if (cu->dwo_unit)
23346 {
23347 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23348
23349 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23350 }
23351 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23352 : &dwarf2_per_objfile->per_bfd->loc);
23353 }
23354
23355 /* A helper function that fills in a dwarf2_loclist_baton. */
23356
23357 static void
23358 fill_in_loclist_baton (struct dwarf2_cu *cu,
23359 struct dwarf2_loclist_baton *baton,
23360 const struct attribute *attr)
23361 {
23362 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23363 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23364
23365 section->read (dwarf2_per_objfile->objfile);
23366
23367 baton->per_objfile = dwarf2_per_objfile;
23368 baton->per_cu = cu->per_cu;
23369 gdb_assert (baton->per_cu);
23370 /* We don't know how long the location list is, but make sure we
23371 don't run off the edge of the section. */
23372 baton->size = section->size - DW_UNSND (attr);
23373 baton->data = section->buffer + DW_UNSND (attr);
23374 if (cu->base_address.has_value ())
23375 baton->base_address = *cu->base_address;
23376 else
23377 baton->base_address = 0;
23378 baton->from_dwo = cu->dwo_unit != NULL;
23379 }
23380
23381 static void
23382 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23383 struct dwarf2_cu *cu, int is_block)
23384 {
23385 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23386 struct objfile *objfile = dwarf2_per_objfile->objfile;
23387 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23388
23389 if (attr->form_is_section_offset ()
23390 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23391 the section. If so, fall through to the complaint in the
23392 other branch. */
23393 && DW_UNSND (attr) < section->get_size (objfile))
23394 {
23395 struct dwarf2_loclist_baton *baton;
23396
23397 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23398
23399 fill_in_loclist_baton (cu, baton, attr);
23400
23401 if (!cu->base_address.has_value ())
23402 complaint (_("Location list used without "
23403 "specifying the CU base address."));
23404
23405 SYMBOL_ACLASS_INDEX (sym) = (is_block
23406 ? dwarf2_loclist_block_index
23407 : dwarf2_loclist_index);
23408 SYMBOL_LOCATION_BATON (sym) = baton;
23409 }
23410 else
23411 {
23412 struct dwarf2_locexpr_baton *baton;
23413
23414 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23415 baton->per_objfile = dwarf2_per_objfile;
23416 baton->per_cu = cu->per_cu;
23417 gdb_assert (baton->per_cu);
23418
23419 if (attr->form_is_block ())
23420 {
23421 /* Note that we're just copying the block's data pointer
23422 here, not the actual data. We're still pointing into the
23423 info_buffer for SYM's objfile; right now we never release
23424 that buffer, but when we do clean up properly this may
23425 need to change. */
23426 baton->size = DW_BLOCK (attr)->size;
23427 baton->data = DW_BLOCK (attr)->data;
23428 }
23429 else
23430 {
23431 dwarf2_invalid_attrib_class_complaint ("location description",
23432 sym->natural_name ());
23433 baton->size = 0;
23434 }
23435
23436 SYMBOL_ACLASS_INDEX (sym) = (is_block
23437 ? dwarf2_locexpr_block_index
23438 : dwarf2_locexpr_index);
23439 SYMBOL_LOCATION_BATON (sym) = baton;
23440 }
23441 }
23442
23443 /* See read.h. */
23444
23445 const comp_unit_head *
23446 dwarf2_per_cu_data::get_header () const
23447 {
23448 if (!m_header_read_in)
23449 {
23450 const gdb_byte *info_ptr
23451 = this->section->buffer + to_underlying (this->sect_off);
23452
23453 memset (&m_header, 0, sizeof (m_header));
23454
23455 read_comp_unit_head (&m_header, info_ptr, this->section,
23456 rcuh_kind::COMPILE);
23457 }
23458
23459 return &m_header;
23460 }
23461
23462 /* See read.h. */
23463
23464 int
23465 dwarf2_per_cu_data::addr_size () const
23466 {
23467 return this->get_header ()->addr_size;
23468 }
23469
23470 /* See read.h. */
23471
23472 int
23473 dwarf2_per_cu_data::offset_size () const
23474 {
23475 return this->get_header ()->offset_size;
23476 }
23477
23478 /* See read.h. */
23479
23480 int
23481 dwarf2_per_cu_data::ref_addr_size () const
23482 {
23483 const comp_unit_head *header = this->get_header ();
23484
23485 if (header->version == 2)
23486 return header->addr_size;
23487 else
23488 return header->offset_size;
23489 }
23490
23491 /* See read.h. */
23492
23493 struct type *
23494 dwarf2_cu::addr_type () const
23495 {
23496 struct objfile *objfile = this->per_objfile->objfile;
23497 struct type *void_type = objfile_type (objfile)->builtin_void;
23498 struct type *addr_type = lookup_pointer_type (void_type);
23499 int addr_size = this->per_cu->addr_size ();
23500
23501 if (TYPE_LENGTH (addr_type) == addr_size)
23502 return addr_type;
23503
23504 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23505 return addr_type;
23506 }
23507
23508 /* A helper function for dwarf2_find_containing_comp_unit that returns
23509 the index of the result, and that searches a vector. It will
23510 return a result even if the offset in question does not actually
23511 occur in any CU. This is separate so that it can be unit
23512 tested. */
23513
23514 static int
23515 dwarf2_find_containing_comp_unit
23516 (sect_offset sect_off,
23517 unsigned int offset_in_dwz,
23518 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23519 {
23520 int low, high;
23521
23522 low = 0;
23523 high = all_comp_units.size () - 1;
23524 while (high > low)
23525 {
23526 struct dwarf2_per_cu_data *mid_cu;
23527 int mid = low + (high - low) / 2;
23528
23529 mid_cu = all_comp_units[mid];
23530 if (mid_cu->is_dwz > offset_in_dwz
23531 || (mid_cu->is_dwz == offset_in_dwz
23532 && mid_cu->sect_off + mid_cu->length > sect_off))
23533 high = mid;
23534 else
23535 low = mid + 1;
23536 }
23537 gdb_assert (low == high);
23538 return low;
23539 }
23540
23541 /* Locate the .debug_info compilation unit from CU's objfile which contains
23542 the DIE at OFFSET. Raises an error on failure. */
23543
23544 static struct dwarf2_per_cu_data *
23545 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23546 unsigned int offset_in_dwz,
23547 struct dwarf2_per_objfile *dwarf2_per_objfile)
23548 {
23549 int low
23550 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23551 dwarf2_per_objfile->per_bfd->all_comp_units);
23552 struct dwarf2_per_cu_data *this_cu
23553 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23554
23555 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23556 {
23557 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23558 error (_("Dwarf Error: could not find partial DIE containing "
23559 "offset %s [in module %s]"),
23560 sect_offset_str (sect_off),
23561 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23562
23563 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23564 <= sect_off);
23565 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23566 }
23567 else
23568 {
23569 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23570 && sect_off >= this_cu->sect_off + this_cu->length)
23571 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23572 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23573 return this_cu;
23574 }
23575 }
23576
23577 #if GDB_SELF_TEST
23578
23579 namespace selftests {
23580 namespace find_containing_comp_unit {
23581
23582 static void
23583 run_test ()
23584 {
23585 struct dwarf2_per_cu_data one {};
23586 struct dwarf2_per_cu_data two {};
23587 struct dwarf2_per_cu_data three {};
23588 struct dwarf2_per_cu_data four {};
23589
23590 one.length = 5;
23591 two.sect_off = sect_offset (one.length);
23592 two.length = 7;
23593
23594 three.length = 5;
23595 three.is_dwz = 1;
23596 four.sect_off = sect_offset (three.length);
23597 four.length = 7;
23598 four.is_dwz = 1;
23599
23600 std::vector<dwarf2_per_cu_data *> units;
23601 units.push_back (&one);
23602 units.push_back (&two);
23603 units.push_back (&three);
23604 units.push_back (&four);
23605
23606 int result;
23607
23608 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23609 SELF_CHECK (units[result] == &one);
23610 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23611 SELF_CHECK (units[result] == &one);
23612 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23613 SELF_CHECK (units[result] == &two);
23614
23615 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23616 SELF_CHECK (units[result] == &three);
23617 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23618 SELF_CHECK (units[result] == &three);
23619 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23620 SELF_CHECK (units[result] == &four);
23621 }
23622
23623 }
23624 }
23625
23626 #endif /* GDB_SELF_TEST */
23627
23628 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23629
23630 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23631 dwarf2_per_objfile *per_objfile)
23632 : per_cu (per_cu),
23633 per_objfile (per_objfile),
23634 mark (false),
23635 has_loclist (false),
23636 checked_producer (false),
23637 producer_is_gxx_lt_4_6 (false),
23638 producer_is_gcc_lt_4_3 (false),
23639 producer_is_icc (false),
23640 producer_is_icc_lt_14 (false),
23641 producer_is_codewarrior (false),
23642 processing_has_namespace_info (false)
23643 {
23644 }
23645
23646 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23647
23648 static void
23649 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23650 enum language pretend_language)
23651 {
23652 struct attribute *attr;
23653
23654 /* Set the language we're debugging. */
23655 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23656 if (attr != nullptr)
23657 set_cu_language (DW_UNSND (attr), cu);
23658 else
23659 {
23660 cu->language = pretend_language;
23661 cu->language_defn = language_def (cu->language);
23662 }
23663
23664 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23665 }
23666
23667 /* See read.h. */
23668
23669 dwarf2_cu *
23670 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23671 {
23672 auto it = m_dwarf2_cus.find (per_cu);
23673 if (it == m_dwarf2_cus.end ())
23674 return nullptr;
23675
23676 return it->second;
23677 }
23678
23679 /* See read.h. */
23680
23681 void
23682 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23683 {
23684 gdb_assert (this->get_cu (per_cu) == nullptr);
23685
23686 m_dwarf2_cus[per_cu] = cu;
23687 }
23688
23689 /* See read.h. */
23690
23691 void
23692 dwarf2_per_objfile::age_comp_units ()
23693 {
23694 /* Start by clearing all marks. */
23695 for (auto pair : m_dwarf2_cus)
23696 pair.second->mark = false;
23697
23698 /* Traverse all CUs, mark them and their dependencies if used recently
23699 enough. */
23700 for (auto pair : m_dwarf2_cus)
23701 {
23702 dwarf2_cu *cu = pair.second;
23703
23704 cu->last_used++;
23705 if (cu->last_used <= dwarf_max_cache_age)
23706 dwarf2_mark (cu);
23707 }
23708
23709 /* Delete all CUs still not marked. */
23710 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23711 {
23712 dwarf2_cu *cu = it->second;
23713
23714 if (!cu->mark)
23715 {
23716 delete cu;
23717 it = m_dwarf2_cus.erase (it);
23718 }
23719 else
23720 it++;
23721 }
23722 }
23723
23724 /* See read.h. */
23725
23726 void
23727 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23728 {
23729 auto it = m_dwarf2_cus.find (per_cu);
23730 if (it == m_dwarf2_cus.end ())
23731 return;
23732
23733 delete it->second;
23734
23735 m_dwarf2_cus.erase (it);
23736 }
23737
23738 dwarf2_per_objfile::~dwarf2_per_objfile ()
23739 {
23740 remove_all_cus ();
23741 }
23742
23743 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23744 We store these in a hash table separate from the DIEs, and preserve them
23745 when the DIEs are flushed out of cache.
23746
23747 The CU "per_cu" pointer is needed because offset alone is not enough to
23748 uniquely identify the type. A file may have multiple .debug_types sections,
23749 or the type may come from a DWO file. Furthermore, while it's more logical
23750 to use per_cu->section+offset, with Fission the section with the data is in
23751 the DWO file but we don't know that section at the point we need it.
23752 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23753 because we can enter the lookup routine, get_die_type_at_offset, from
23754 outside this file, and thus won't necessarily have PER_CU->cu.
23755 Fortunately, PER_CU is stable for the life of the objfile. */
23756
23757 struct dwarf2_per_cu_offset_and_type
23758 {
23759 const struct dwarf2_per_cu_data *per_cu;
23760 sect_offset sect_off;
23761 struct type *type;
23762 };
23763
23764 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23765
23766 static hashval_t
23767 per_cu_offset_and_type_hash (const void *item)
23768 {
23769 const struct dwarf2_per_cu_offset_and_type *ofs
23770 = (const struct dwarf2_per_cu_offset_and_type *) item;
23771
23772 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23773 }
23774
23775 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23776
23777 static int
23778 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23779 {
23780 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23781 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23782 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23783 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23784
23785 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23786 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23787 }
23788
23789 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23790 table if necessary. For convenience, return TYPE.
23791
23792 The DIEs reading must have careful ordering to:
23793 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23794 reading current DIE.
23795 * Not trying to dereference contents of still incompletely read in types
23796 while reading in other DIEs.
23797 * Enable referencing still incompletely read in types just by a pointer to
23798 the type without accessing its fields.
23799
23800 Therefore caller should follow these rules:
23801 * Try to fetch any prerequisite types we may need to build this DIE type
23802 before building the type and calling set_die_type.
23803 * After building type call set_die_type for current DIE as soon as
23804 possible before fetching more types to complete the current type.
23805 * Make the type as complete as possible before fetching more types. */
23806
23807 static struct type *
23808 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23809 {
23810 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23811 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23812 struct objfile *objfile = dwarf2_per_objfile->objfile;
23813 struct attribute *attr;
23814 struct dynamic_prop prop;
23815
23816 /* For Ada types, make sure that the gnat-specific data is always
23817 initialized (if not already set). There are a few types where
23818 we should not be doing so, because the type-specific area is
23819 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23820 where the type-specific area is used to store the floatformat).
23821 But this is not a problem, because the gnat-specific information
23822 is actually not needed for these types. */
23823 if (need_gnat_info (cu)
23824 && type->code () != TYPE_CODE_FUNC
23825 && type->code () != TYPE_CODE_FLT
23826 && type->code () != TYPE_CODE_METHODPTR
23827 && type->code () != TYPE_CODE_MEMBERPTR
23828 && type->code () != TYPE_CODE_METHOD
23829 && !HAVE_GNAT_AUX_INFO (type))
23830 INIT_GNAT_SPECIFIC (type);
23831
23832 /* Read DW_AT_allocated and set in type. */
23833 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23834 if (attr != NULL && attr->form_is_block ())
23835 {
23836 struct type *prop_type = cu->addr_sized_int_type (false);
23837 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23838 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23839 }
23840 else if (attr != NULL)
23841 {
23842 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23843 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23844 sect_offset_str (die->sect_off));
23845 }
23846
23847 /* Read DW_AT_associated and set in type. */
23848 attr = dwarf2_attr (die, DW_AT_associated, cu);
23849 if (attr != NULL && attr->form_is_block ())
23850 {
23851 struct type *prop_type = cu->addr_sized_int_type (false);
23852 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23853 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23854 }
23855 else if (attr != NULL)
23856 {
23857 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23858 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23859 sect_offset_str (die->sect_off));
23860 }
23861
23862 /* Read DW_AT_data_location and set in type. */
23863 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23864 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23865 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23866
23867 if (dwarf2_per_objfile->die_type_hash == NULL)
23868 dwarf2_per_objfile->die_type_hash
23869 = htab_up (htab_create_alloc (127,
23870 per_cu_offset_and_type_hash,
23871 per_cu_offset_and_type_eq,
23872 NULL, xcalloc, xfree));
23873
23874 ofs.per_cu = cu->per_cu;
23875 ofs.sect_off = die->sect_off;
23876 ofs.type = type;
23877 slot = (struct dwarf2_per_cu_offset_and_type **)
23878 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23879 if (*slot)
23880 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23881 sect_offset_str (die->sect_off));
23882 *slot = XOBNEW (&objfile->objfile_obstack,
23883 struct dwarf2_per_cu_offset_and_type);
23884 **slot = ofs;
23885 return type;
23886 }
23887
23888 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23889 or return NULL if the die does not have a saved type. */
23890
23891 static struct type *
23892 get_die_type_at_offset (sect_offset sect_off,
23893 dwarf2_per_cu_data *per_cu,
23894 dwarf2_per_objfile *dwarf2_per_objfile)
23895 {
23896 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23897
23898 if (dwarf2_per_objfile->die_type_hash == NULL)
23899 return NULL;
23900
23901 ofs.per_cu = per_cu;
23902 ofs.sect_off = sect_off;
23903 slot = ((struct dwarf2_per_cu_offset_and_type *)
23904 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23905 if (slot)
23906 return slot->type;
23907 else
23908 return NULL;
23909 }
23910
23911 /* Look up the type for DIE in CU in die_type_hash,
23912 or return NULL if DIE does not have a saved type. */
23913
23914 static struct type *
23915 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23916 {
23917 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23918 }
23919
23920 /* Add a dependence relationship from CU to REF_PER_CU. */
23921
23922 static void
23923 dwarf2_add_dependence (struct dwarf2_cu *cu,
23924 struct dwarf2_per_cu_data *ref_per_cu)
23925 {
23926 void **slot;
23927
23928 if (cu->dependencies == NULL)
23929 cu->dependencies
23930 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23931 NULL, &cu->comp_unit_obstack,
23932 hashtab_obstack_allocate,
23933 dummy_obstack_deallocate);
23934
23935 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23936 if (*slot == NULL)
23937 *slot = ref_per_cu;
23938 }
23939
23940 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23941 Set the mark field in every compilation unit in the
23942 cache that we must keep because we are keeping CU.
23943
23944 DATA is the dwarf2_per_objfile object in which to look up CUs. */
23945
23946 static int
23947 dwarf2_mark_helper (void **slot, void *data)
23948 {
23949 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23950 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23951 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23952
23953 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23954 reading of the chain. As such dependencies remain valid it is not much
23955 useful to track and undo them during QUIT cleanups. */
23956 if (cu == nullptr)
23957 return 1;
23958
23959 if (cu->mark)
23960 return 1;
23961
23962 cu->mark = true;
23963
23964 if (cu->dependencies != nullptr)
23965 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
23966
23967 return 1;
23968 }
23969
23970 /* Set the mark field in CU and in every other compilation unit in the
23971 cache that we must keep because we are keeping CU. */
23972
23973 static void
23974 dwarf2_mark (struct dwarf2_cu *cu)
23975 {
23976 if (cu->mark)
23977 return;
23978
23979 cu->mark = true;
23980
23981 if (cu->dependencies != nullptr)
23982 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
23983 }
23984
23985 /* Trivial hash function for partial_die_info: the hash value of a DIE
23986 is its offset in .debug_info for this objfile. */
23987
23988 static hashval_t
23989 partial_die_hash (const void *item)
23990 {
23991 const struct partial_die_info *part_die
23992 = (const struct partial_die_info *) item;
23993
23994 return to_underlying (part_die->sect_off);
23995 }
23996
23997 /* Trivial comparison function for partial_die_info structures: two DIEs
23998 are equal if they have the same offset. */
23999
24000 static int
24001 partial_die_eq (const void *item_lhs, const void *item_rhs)
24002 {
24003 const struct partial_die_info *part_die_lhs
24004 = (const struct partial_die_info *) item_lhs;
24005 const struct partial_die_info *part_die_rhs
24006 = (const struct partial_die_info *) item_rhs;
24007
24008 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24009 }
24010
24011 struct cmd_list_element *set_dwarf_cmdlist;
24012 struct cmd_list_element *show_dwarf_cmdlist;
24013
24014 static void
24015 show_check_physname (struct ui_file *file, int from_tty,
24016 struct cmd_list_element *c, const char *value)
24017 {
24018 fprintf_filtered (file,
24019 _("Whether to check \"physname\" is %s.\n"),
24020 value);
24021 }
24022
24023 void _initialize_dwarf2_read ();
24024 void
24025 _initialize_dwarf2_read ()
24026 {
24027 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24028 Set DWARF specific variables.\n\
24029 Configure DWARF variables such as the cache size."),
24030 &set_dwarf_cmdlist, "maintenance set dwarf ",
24031 0/*allow-unknown*/, &maintenance_set_cmdlist);
24032
24033 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24034 Show DWARF specific variables.\n\
24035 Show DWARF variables such as the cache size."),
24036 &show_dwarf_cmdlist, "maintenance show dwarf ",
24037 0/*allow-unknown*/, &maintenance_show_cmdlist);
24038
24039 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24040 &dwarf_max_cache_age, _("\
24041 Set the upper bound on the age of cached DWARF compilation units."), _("\
24042 Show the upper bound on the age of cached DWARF compilation units."), _("\
24043 A higher limit means that cached compilation units will be stored\n\
24044 in memory longer, and more total memory will be used. Zero disables\n\
24045 caching, which can slow down startup."),
24046 NULL,
24047 show_dwarf_max_cache_age,
24048 &set_dwarf_cmdlist,
24049 &show_dwarf_cmdlist);
24050
24051 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24052 Set debugging of the DWARF reader."), _("\
24053 Show debugging of the DWARF reader."), _("\
24054 When enabled (non-zero), debugging messages are printed during DWARF\n\
24055 reading and symtab expansion. A value of 1 (one) provides basic\n\
24056 information. A value greater than 1 provides more verbose information."),
24057 NULL,
24058 NULL,
24059 &setdebuglist, &showdebuglist);
24060
24061 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24062 Set debugging of the DWARF DIE reader."), _("\
24063 Show debugging of the DWARF DIE reader."), _("\
24064 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24065 The value is the maximum depth to print."),
24066 NULL,
24067 NULL,
24068 &setdebuglist, &showdebuglist);
24069
24070 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24071 Set debugging of the dwarf line reader."), _("\
24072 Show debugging of the dwarf line reader."), _("\
24073 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24074 A value of 1 (one) provides basic information.\n\
24075 A value greater than 1 provides more verbose information."),
24076 NULL,
24077 NULL,
24078 &setdebuglist, &showdebuglist);
24079
24080 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24081 Set cross-checking of \"physname\" code against demangler."), _("\
24082 Show cross-checking of \"physname\" code against demangler."), _("\
24083 When enabled, GDB's internal \"physname\" code is checked against\n\
24084 the demangler."),
24085 NULL, show_check_physname,
24086 &setdebuglist, &showdebuglist);
24087
24088 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24089 no_class, &use_deprecated_index_sections, _("\
24090 Set whether to use deprecated gdb_index sections."), _("\
24091 Show whether to use deprecated gdb_index sections."), _("\
24092 When enabled, deprecated .gdb_index sections are used anyway.\n\
24093 Normally they are ignored either because of a missing feature or\n\
24094 performance issue.\n\
24095 Warning: This option must be enabled before gdb reads the file."),
24096 NULL,
24097 NULL,
24098 &setlist, &showlist);
24099
24100 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24101 &dwarf2_locexpr_funcs);
24102 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24103 &dwarf2_loclist_funcs);
24104
24105 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24106 &dwarf2_block_frame_base_locexpr_funcs);
24107 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24108 &dwarf2_block_frame_base_loclist_funcs);
24109
24110 #if GDB_SELF_TEST
24111 selftests::register_test ("dw2_expand_symtabs_matching",
24112 selftests::dw2_expand_symtabs_matching::run_test);
24113 selftests::register_test ("dwarf2_find_containing_comp_unit",
24114 selftests::find_containing_comp_unit::run_test);
24115 #endif
24116 }
This page took 0.646576 seconds and 3 git commands to generate.