gdb: add accessors to struct dynamic_prop
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 /* This is used to store the data that is always per objfile. */
109 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
110
111 /* These are used to store the dwarf2_per_bfd objects.
112
113 objfiles having the same BFD, which doesn't require relocations, are going to
114 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
115
116 Other objfiles are not going to share a dwarf2_per_bfd with any other
117 objfiles, so they'll have their own version kept in the _objfile_data_key
118 version. */
119 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
120 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
121
122 /* The "aclass" indices for various kinds of computed DWARF symbols. */
123
124 static int dwarf2_locexpr_index;
125 static int dwarf2_loclist_index;
126 static int dwarf2_locexpr_block_index;
127 static int dwarf2_loclist_block_index;
128
129 /* Size of .debug_loclists section header for 32-bit DWARF format. */
130 #define LOCLIST_HEADER_SIZE32 12
131
132 /* Size of .debug_loclists section header for 64-bit DWARF format. */
133 #define LOCLIST_HEADER_SIZE64 20
134
135 /* An index into a (C++) symbol name component in a symbol name as
136 recorded in the mapped_index's symbol table. For each C++ symbol
137 in the symbol table, we record one entry for the start of each
138 component in the symbol in a table of name components, and then
139 sort the table, in order to be able to binary search symbol names,
140 ignoring leading namespaces, both completion and regular look up.
141 For example, for symbol "A::B::C", we'll have an entry that points
142 to "A::B::C", another that points to "B::C", and another for "C".
143 Note that function symbols in GDB index have no parameter
144 information, just the function/method names. You can convert a
145 name_component to a "const char *" using the
146 'mapped_index::symbol_name_at(offset_type)' method. */
147
148 struct name_component
149 {
150 /* Offset in the symbol name where the component starts. Stored as
151 a (32-bit) offset instead of a pointer to save memory and improve
152 locality on 64-bit architectures. */
153 offset_type name_offset;
154
155 /* The symbol's index in the symbol and constant pool tables of a
156 mapped_index. */
157 offset_type idx;
158 };
159
160 /* Base class containing bits shared by both .gdb_index and
161 .debug_name indexes. */
162
163 struct mapped_index_base
164 {
165 mapped_index_base () = default;
166 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
167
168 /* The name_component table (a sorted vector). See name_component's
169 description above. */
170 std::vector<name_component> name_components;
171
172 /* How NAME_COMPONENTS is sorted. */
173 enum case_sensitivity name_components_casing;
174
175 /* Return the number of names in the symbol table. */
176 virtual size_t symbol_name_count () const = 0;
177
178 /* Get the name of the symbol at IDX in the symbol table. */
179 virtual const char *symbol_name_at
180 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
181
182 /* Return whether the name at IDX in the symbol table should be
183 ignored. */
184 virtual bool symbol_name_slot_invalid (offset_type idx) const
185 {
186 return false;
187 }
188
189 /* Build the symbol name component sorted vector, if we haven't
190 yet. */
191 void build_name_components (dwarf2_per_objfile *per_objfile);
192
193 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
194 possible matches for LN_NO_PARAMS in the name component
195 vector. */
196 std::pair<std::vector<name_component>::const_iterator,
197 std::vector<name_component>::const_iterator>
198 find_name_components_bounds (const lookup_name_info &ln_no_params,
199 enum language lang,
200 dwarf2_per_objfile *per_objfile) const;
201
202 /* Prevent deleting/destroying via a base class pointer. */
203 protected:
204 ~mapped_index_base() = default;
205 };
206
207 /* A description of the mapped index. The file format is described in
208 a comment by the code that writes the index. */
209 struct mapped_index final : public mapped_index_base
210 {
211 /* A slot/bucket in the symbol table hash. */
212 struct symbol_table_slot
213 {
214 const offset_type name;
215 const offset_type vec;
216 };
217
218 /* Index data format version. */
219 int version = 0;
220
221 /* The address table data. */
222 gdb::array_view<const gdb_byte> address_table;
223
224 /* The symbol table, implemented as a hash table. */
225 gdb::array_view<symbol_table_slot> symbol_table;
226
227 /* A pointer to the constant pool. */
228 const char *constant_pool = nullptr;
229
230 bool symbol_name_slot_invalid (offset_type idx) const override
231 {
232 const auto &bucket = this->symbol_table[idx];
233 return bucket.name == 0 && bucket.vec == 0;
234 }
235
236 /* Convenience method to get at the name of the symbol at IDX in the
237 symbol table. */
238 const char *symbol_name_at
239 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
240 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
241
242 size_t symbol_name_count () const override
243 { return this->symbol_table.size (); }
244 };
245
246 /* A description of the mapped .debug_names.
247 Uninitialized map has CU_COUNT 0. */
248 struct mapped_debug_names final : public mapped_index_base
249 {
250 bfd_endian dwarf5_byte_order;
251 bool dwarf5_is_dwarf64;
252 bool augmentation_is_gdb;
253 uint8_t offset_size;
254 uint32_t cu_count = 0;
255 uint32_t tu_count, bucket_count, name_count;
256 const gdb_byte *cu_table_reordered, *tu_table_reordered;
257 const uint32_t *bucket_table_reordered, *hash_table_reordered;
258 const gdb_byte *name_table_string_offs_reordered;
259 const gdb_byte *name_table_entry_offs_reordered;
260 const gdb_byte *entry_pool;
261
262 struct index_val
263 {
264 ULONGEST dwarf_tag;
265 struct attr
266 {
267 /* Attribute name DW_IDX_*. */
268 ULONGEST dw_idx;
269
270 /* Attribute form DW_FORM_*. */
271 ULONGEST form;
272
273 /* Value if FORM is DW_FORM_implicit_const. */
274 LONGEST implicit_const;
275 };
276 std::vector<attr> attr_vec;
277 };
278
279 std::unordered_map<ULONGEST, index_val> abbrev_map;
280
281 const char *namei_to_name
282 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
283
284 /* Implementation of the mapped_index_base virtual interface, for
285 the name_components cache. */
286
287 const char *symbol_name_at
288 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
289 { return namei_to_name (idx, per_objfile); }
290
291 size_t symbol_name_count () const override
292 { return this->name_count; }
293 };
294
295 /* See dwarf2read.h. */
296
297 dwarf2_per_objfile *
298 get_dwarf2_per_objfile (struct objfile *objfile)
299 {
300 return dwarf2_objfile_data_key.get (objfile);
301 }
302
303 /* Default names of the debugging sections. */
304
305 /* Note that if the debugging section has been compressed, it might
306 have a name like .zdebug_info. */
307
308 static const struct dwarf2_debug_sections dwarf2_elf_names =
309 {
310 { ".debug_info", ".zdebug_info" },
311 { ".debug_abbrev", ".zdebug_abbrev" },
312 { ".debug_line", ".zdebug_line" },
313 { ".debug_loc", ".zdebug_loc" },
314 { ".debug_loclists", ".zdebug_loclists" },
315 { ".debug_macinfo", ".zdebug_macinfo" },
316 { ".debug_macro", ".zdebug_macro" },
317 { ".debug_str", ".zdebug_str" },
318 { ".debug_str_offsets", ".zdebug_str_offsets" },
319 { ".debug_line_str", ".zdebug_line_str" },
320 { ".debug_ranges", ".zdebug_ranges" },
321 { ".debug_rnglists", ".zdebug_rnglists" },
322 { ".debug_types", ".zdebug_types" },
323 { ".debug_addr", ".zdebug_addr" },
324 { ".debug_frame", ".zdebug_frame" },
325 { ".eh_frame", NULL },
326 { ".gdb_index", ".zgdb_index" },
327 { ".debug_names", ".zdebug_names" },
328 { ".debug_aranges", ".zdebug_aranges" },
329 23
330 };
331
332 /* List of DWO/DWP sections. */
333
334 static const struct dwop_section_names
335 {
336 struct dwarf2_section_names abbrev_dwo;
337 struct dwarf2_section_names info_dwo;
338 struct dwarf2_section_names line_dwo;
339 struct dwarf2_section_names loc_dwo;
340 struct dwarf2_section_names loclists_dwo;
341 struct dwarf2_section_names macinfo_dwo;
342 struct dwarf2_section_names macro_dwo;
343 struct dwarf2_section_names str_dwo;
344 struct dwarf2_section_names str_offsets_dwo;
345 struct dwarf2_section_names types_dwo;
346 struct dwarf2_section_names cu_index;
347 struct dwarf2_section_names tu_index;
348 }
349 dwop_section_names =
350 {
351 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
352 { ".debug_info.dwo", ".zdebug_info.dwo" },
353 { ".debug_line.dwo", ".zdebug_line.dwo" },
354 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
355 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
356 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
357 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
358 { ".debug_str.dwo", ".zdebug_str.dwo" },
359 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
360 { ".debug_types.dwo", ".zdebug_types.dwo" },
361 { ".debug_cu_index", ".zdebug_cu_index" },
362 { ".debug_tu_index", ".zdebug_tu_index" },
363 };
364
365 /* local data types */
366
367 /* The location list section (.debug_loclists) begins with a header,
368 which contains the following information. */
369 struct loclist_header
370 {
371 /* A 4-byte or 12-byte length containing the length of the
372 set of entries for this compilation unit, not including the
373 length field itself. */
374 unsigned int length;
375
376 /* A 2-byte version identifier. */
377 short version;
378
379 /* A 1-byte unsigned integer containing the size in bytes of an address on
380 the target system. */
381 unsigned char addr_size;
382
383 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
384 on the target system. */
385 unsigned char segment_collector_size;
386
387 /* A 4-byte count of the number of offsets that follow the header. */
388 unsigned int offset_entry_count;
389 };
390
391 /* Type used for delaying computation of method physnames.
392 See comments for compute_delayed_physnames. */
393 struct delayed_method_info
394 {
395 /* The type to which the method is attached, i.e., its parent class. */
396 struct type *type;
397
398 /* The index of the method in the type's function fieldlists. */
399 int fnfield_index;
400
401 /* The index of the method in the fieldlist. */
402 int index;
403
404 /* The name of the DIE. */
405 const char *name;
406
407 /* The DIE associated with this method. */
408 struct die_info *die;
409 };
410
411 /* Internal state when decoding a particular compilation unit. */
412 struct dwarf2_cu
413 {
414 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
415 dwarf2_per_objfile *per_objfile);
416
417 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
418
419 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
420 Create the set of symtabs used by this TU, or if this TU is sharing
421 symtabs with another TU and the symtabs have already been created
422 then restore those symtabs in the line header.
423 We don't need the pc/line-number mapping for type units. */
424 void setup_type_unit_groups (struct die_info *die);
425
426 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
427 buildsym_compunit constructor. */
428 struct compunit_symtab *start_symtab (const char *name,
429 const char *comp_dir,
430 CORE_ADDR low_pc);
431
432 /* Reset the builder. */
433 void reset_builder () { m_builder.reset (); }
434
435 /* Return a type that is a generic pointer type, the size of which
436 matches the address size given in the compilation unit header for
437 this CU. */
438 struct type *addr_type () const;
439
440 /* Find an integer type the same size as the address size given in
441 the compilation unit header for this CU. UNSIGNED_P controls if
442 the integer is unsigned or not. */
443 struct type *addr_sized_int_type (bool unsigned_p) const;
444
445 /* The header of the compilation unit. */
446 struct comp_unit_head header {};
447
448 /* Base address of this compilation unit. */
449 gdb::optional<CORE_ADDR> base_address;
450
451 /* The language we are debugging. */
452 enum language language = language_unknown;
453 const struct language_defn *language_defn = nullptr;
454
455 const char *producer = nullptr;
456
457 private:
458 /* The symtab builder for this CU. This is only non-NULL when full
459 symbols are being read. */
460 std::unique_ptr<buildsym_compunit> m_builder;
461
462 public:
463 /* The generic symbol table building routines have separate lists for
464 file scope symbols and all all other scopes (local scopes). So
465 we need to select the right one to pass to add_symbol_to_list().
466 We do it by keeping a pointer to the correct list in list_in_scope.
467
468 FIXME: The original dwarf code just treated the file scope as the
469 first local scope, and all other local scopes as nested local
470 scopes, and worked fine. Check to see if we really need to
471 distinguish these in buildsym.c. */
472 struct pending **list_in_scope = nullptr;
473
474 /* Hash table holding all the loaded partial DIEs
475 with partial_die->offset.SECT_OFF as hash. */
476 htab_t partial_dies = nullptr;
477
478 /* Storage for things with the same lifetime as this read-in compilation
479 unit, including partial DIEs. */
480 auto_obstack comp_unit_obstack;
481
482 /* Backlink to our per_cu entry. */
483 struct dwarf2_per_cu_data *per_cu;
484
485 /* The dwarf2_per_objfile that owns this. */
486 dwarf2_per_objfile *per_objfile;
487
488 /* How many compilation units ago was this CU last referenced? */
489 int last_used = 0;
490
491 /* A hash table of DIE cu_offset for following references with
492 die_info->offset.sect_off as hash. */
493 htab_t die_hash = nullptr;
494
495 /* Full DIEs if read in. */
496 struct die_info *dies = nullptr;
497
498 /* A set of pointers to dwarf2_per_cu_data objects for compilation
499 units referenced by this one. Only set during full symbol processing;
500 partial symbol tables do not have dependencies. */
501 htab_t dependencies = nullptr;
502
503 /* Header data from the line table, during full symbol processing. */
504 struct line_header *line_header = nullptr;
505 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
506 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
507 this is the DW_TAG_compile_unit die for this CU. We'll hold on
508 to the line header as long as this DIE is being processed. See
509 process_die_scope. */
510 die_info *line_header_die_owner = nullptr;
511
512 /* A list of methods which need to have physnames computed
513 after all type information has been read. */
514 std::vector<delayed_method_info> method_list;
515
516 /* To be copied to symtab->call_site_htab. */
517 htab_t call_site_htab = nullptr;
518
519 /* Non-NULL if this CU came from a DWO file.
520 There is an invariant here that is important to remember:
521 Except for attributes copied from the top level DIE in the "main"
522 (or "stub") file in preparation for reading the DWO file
523 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
524 Either there isn't a DWO file (in which case this is NULL and the point
525 is moot), or there is and either we're not going to read it (in which
526 case this is NULL) or there is and we are reading it (in which case this
527 is non-NULL). */
528 struct dwo_unit *dwo_unit = nullptr;
529
530 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
531 Note this value comes from the Fission stub CU/TU's DIE. */
532 gdb::optional<ULONGEST> addr_base;
533
534 /* The DW_AT_rnglists_base attribute if present.
535 Note this value comes from the Fission stub CU/TU's DIE.
536 Also note that the value is zero in the non-DWO case so this value can
537 be used without needing to know whether DWO files are in use or not.
538 N.B. This does not apply to DW_AT_ranges appearing in
539 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
540 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
541 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
542 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
543 ULONGEST ranges_base = 0;
544
545 /* The DW_AT_loclists_base attribute if present. */
546 ULONGEST loclist_base = 0;
547
548 /* When reading debug info generated by older versions of rustc, we
549 have to rewrite some union types to be struct types with a
550 variant part. This rewriting must be done after the CU is fully
551 read in, because otherwise at the point of rewriting some struct
552 type might not have been fully processed. So, we keep a list of
553 all such types here and process them after expansion. */
554 std::vector<struct type *> rust_unions;
555
556 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
557 files, the value is implicitly zero. For DWARF 5 version DWO files, the
558 value is often implicit and is the size of the header of
559 .debug_str_offsets section (8 or 4, depending on the address size). */
560 gdb::optional<ULONGEST> str_offsets_base;
561
562 /* Mark used when releasing cached dies. */
563 bool mark : 1;
564
565 /* This CU references .debug_loc. See the symtab->locations_valid field.
566 This test is imperfect as there may exist optimized debug code not using
567 any location list and still facing inlining issues if handled as
568 unoptimized code. For a future better test see GCC PR other/32998. */
569 bool has_loclist : 1;
570
571 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
572 if all the producer_is_* fields are valid. This information is cached
573 because profiling CU expansion showed excessive time spent in
574 producer_is_gxx_lt_4_6. */
575 bool checked_producer : 1;
576 bool producer_is_gxx_lt_4_6 : 1;
577 bool producer_is_gcc_lt_4_3 : 1;
578 bool producer_is_icc : 1;
579 bool producer_is_icc_lt_14 : 1;
580 bool producer_is_codewarrior : 1;
581
582 /* When true, the file that we're processing is known to have
583 debugging info for C++ namespaces. GCC 3.3.x did not produce
584 this information, but later versions do. */
585
586 bool processing_has_namespace_info : 1;
587
588 struct partial_die_info *find_partial_die (sect_offset sect_off);
589
590 /* If this CU was inherited by another CU (via specification,
591 abstract_origin, etc), this is the ancestor CU. */
592 dwarf2_cu *ancestor;
593
594 /* Get the buildsym_compunit for this CU. */
595 buildsym_compunit *get_builder ()
596 {
597 /* If this CU has a builder associated with it, use that. */
598 if (m_builder != nullptr)
599 return m_builder.get ();
600
601 /* Otherwise, search ancestors for a valid builder. */
602 if (ancestor != nullptr)
603 return ancestor->get_builder ();
604
605 return nullptr;
606 }
607 };
608
609 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
610 This includes type_unit_group and quick_file_names. */
611
612 struct stmt_list_hash
613 {
614 /* The DWO unit this table is from or NULL if there is none. */
615 struct dwo_unit *dwo_unit;
616
617 /* Offset in .debug_line or .debug_line.dwo. */
618 sect_offset line_sect_off;
619 };
620
621 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
622 an object of this type. This contains elements of type unit groups
623 that can be shared across objfiles. The non-shareable parts are in
624 type_unit_group_unshareable. */
625
626 struct type_unit_group
627 {
628 /* dwarf2read.c's main "handle" on a TU symtab.
629 To simplify things we create an artificial CU that "includes" all the
630 type units using this stmt_list so that the rest of the code still has
631 a "per_cu" handle on the symtab. */
632 struct dwarf2_per_cu_data per_cu;
633
634 /* The TUs that share this DW_AT_stmt_list entry.
635 This is added to while parsing type units to build partial symtabs,
636 and is deleted afterwards and not used again. */
637 std::vector<signatured_type *> *tus;
638
639 /* The data used to construct the hash key. */
640 struct stmt_list_hash hash;
641 };
642
643 /* These sections are what may appear in a (real or virtual) DWO file. */
644
645 struct dwo_sections
646 {
647 struct dwarf2_section_info abbrev;
648 struct dwarf2_section_info line;
649 struct dwarf2_section_info loc;
650 struct dwarf2_section_info loclists;
651 struct dwarf2_section_info macinfo;
652 struct dwarf2_section_info macro;
653 struct dwarf2_section_info str;
654 struct dwarf2_section_info str_offsets;
655 /* In the case of a virtual DWO file, these two are unused. */
656 struct dwarf2_section_info info;
657 std::vector<dwarf2_section_info> types;
658 };
659
660 /* CUs/TUs in DWP/DWO files. */
661
662 struct dwo_unit
663 {
664 /* Backlink to the containing struct dwo_file. */
665 struct dwo_file *dwo_file;
666
667 /* The "id" that distinguishes this CU/TU.
668 .debug_info calls this "dwo_id", .debug_types calls this "signature".
669 Since signatures came first, we stick with it for consistency. */
670 ULONGEST signature;
671
672 /* The section this CU/TU lives in, in the DWO file. */
673 struct dwarf2_section_info *section;
674
675 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
676 sect_offset sect_off;
677 unsigned int length;
678
679 /* For types, offset in the type's DIE of the type defined by this TU. */
680 cu_offset type_offset_in_tu;
681 };
682
683 /* include/dwarf2.h defines the DWP section codes.
684 It defines a max value but it doesn't define a min value, which we
685 use for error checking, so provide one. */
686
687 enum dwp_v2_section_ids
688 {
689 DW_SECT_MIN = 1
690 };
691
692 /* Data for one DWO file.
693
694 This includes virtual DWO files (a virtual DWO file is a DWO file as it
695 appears in a DWP file). DWP files don't really have DWO files per se -
696 comdat folding of types "loses" the DWO file they came from, and from
697 a high level view DWP files appear to contain a mass of random types.
698 However, to maintain consistency with the non-DWP case we pretend DWP
699 files contain virtual DWO files, and we assign each TU with one virtual
700 DWO file (generally based on the line and abbrev section offsets -
701 a heuristic that seems to work in practice). */
702
703 struct dwo_file
704 {
705 dwo_file () = default;
706 DISABLE_COPY_AND_ASSIGN (dwo_file);
707
708 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
709 For virtual DWO files the name is constructed from the section offsets
710 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
711 from related CU+TUs. */
712 const char *dwo_name = nullptr;
713
714 /* The DW_AT_comp_dir attribute. */
715 const char *comp_dir = nullptr;
716
717 /* The bfd, when the file is open. Otherwise this is NULL.
718 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
719 gdb_bfd_ref_ptr dbfd;
720
721 /* The sections that make up this DWO file.
722 Remember that for virtual DWO files in DWP V2, these are virtual
723 sections (for lack of a better name). */
724 struct dwo_sections sections {};
725
726 /* The CUs in the file.
727 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
728 an extension to handle LLVM's Link Time Optimization output (where
729 multiple source files may be compiled into a single object/dwo pair). */
730 htab_up cus;
731
732 /* Table of TUs in the file.
733 Each element is a struct dwo_unit. */
734 htab_up tus;
735 };
736
737 /* These sections are what may appear in a DWP file. */
738
739 struct dwp_sections
740 {
741 /* These are used by both DWP version 1 and 2. */
742 struct dwarf2_section_info str;
743 struct dwarf2_section_info cu_index;
744 struct dwarf2_section_info tu_index;
745
746 /* These are only used by DWP version 2 files.
747 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
748 sections are referenced by section number, and are not recorded here.
749 In DWP version 2 there is at most one copy of all these sections, each
750 section being (effectively) comprised of the concatenation of all of the
751 individual sections that exist in the version 1 format.
752 To keep the code simple we treat each of these concatenated pieces as a
753 section itself (a virtual section?). */
754 struct dwarf2_section_info abbrev;
755 struct dwarf2_section_info info;
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
760 struct dwarf2_section_info str_offsets;
761 struct dwarf2_section_info types;
762 };
763
764 /* These sections are what may appear in a virtual DWO file in DWP version 1.
765 A virtual DWO file is a DWO file as it appears in a DWP file. */
766
767 struct virtual_v1_dwo_sections
768 {
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
776 That is recorded here, and copied to dwo_unit.section. */
777 struct dwarf2_section_info info_or_types;
778 };
779
780 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
781 In version 2, the sections of the DWO files are concatenated together
782 and stored in one section of that name. Thus each ELF section contains
783 several "virtual" sections. */
784
785 struct virtual_v2_dwo_sections
786 {
787 bfd_size_type abbrev_offset;
788 bfd_size_type abbrev_size;
789
790 bfd_size_type line_offset;
791 bfd_size_type line_size;
792
793 bfd_size_type loc_offset;
794 bfd_size_type loc_size;
795
796 bfd_size_type macinfo_offset;
797 bfd_size_type macinfo_size;
798
799 bfd_size_type macro_offset;
800 bfd_size_type macro_size;
801
802 bfd_size_type str_offsets_offset;
803 bfd_size_type str_offsets_size;
804
805 /* Each DWP hash table entry records one CU or one TU.
806 That is recorded here, and copied to dwo_unit.section. */
807 bfd_size_type info_or_types_offset;
808 bfd_size_type info_or_types_size;
809 };
810
811 /* Contents of DWP hash tables. */
812
813 struct dwp_hash_table
814 {
815 uint32_t version, nr_columns;
816 uint32_t nr_units, nr_slots;
817 const gdb_byte *hash_table, *unit_table;
818 union
819 {
820 struct
821 {
822 const gdb_byte *indices;
823 } v1;
824 struct
825 {
826 /* This is indexed by column number and gives the id of the section
827 in that column. */
828 #define MAX_NR_V2_DWO_SECTIONS \
829 (1 /* .debug_info or .debug_types */ \
830 + 1 /* .debug_abbrev */ \
831 + 1 /* .debug_line */ \
832 + 1 /* .debug_loc */ \
833 + 1 /* .debug_str_offsets */ \
834 + 1 /* .debug_macro or .debug_macinfo */)
835 int section_ids[MAX_NR_V2_DWO_SECTIONS];
836 const gdb_byte *offsets;
837 const gdb_byte *sizes;
838 } v2;
839 } section_pool;
840 };
841
842 /* Data for one DWP file. */
843
844 struct dwp_file
845 {
846 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
847 : name (name_),
848 dbfd (std::move (abfd))
849 {
850 }
851
852 /* Name of the file. */
853 const char *name;
854
855 /* File format version. */
856 int version = 0;
857
858 /* The bfd. */
859 gdb_bfd_ref_ptr dbfd;
860
861 /* Section info for this file. */
862 struct dwp_sections sections {};
863
864 /* Table of CUs in the file. */
865 const struct dwp_hash_table *cus = nullptr;
866
867 /* Table of TUs in the file. */
868 const struct dwp_hash_table *tus = nullptr;
869
870 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
871 htab_up loaded_cus;
872 htab_up loaded_tus;
873
874 /* Table to map ELF section numbers to their sections.
875 This is only needed for the DWP V1 file format. */
876 unsigned int num_sections = 0;
877 asection **elf_sections = nullptr;
878 };
879
880 /* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
883 struct exists to abstract away the constant parameters of die reading. */
884
885 struct die_reader_specs
886 {
887 /* The bfd of die_section. */
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
894 struct dwo_file *dwo_file;
895
896 /* The section the die comes from.
897 This is either .debug_info or .debug_types, or the .dwo variants. */
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
901 const gdb_byte *buffer;
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
905
906 /* The abbreviation table to use when reading the DIEs. */
907 struct abbrev_table *abbrev_table;
908 };
909
910 /* A subclass of die_reader_specs that holds storage and has complex
911 constructor and destructor behavior. */
912
913 class cutu_reader : public die_reader_specs
914 {
915 public:
916
917 cutu_reader (dwarf2_per_cu_data *this_cu,
918 dwarf2_per_objfile *per_objfile,
919 struct abbrev_table *abbrev_table,
920 dwarf2_cu *existing_cu,
921 bool skip_partial);
922
923 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
924 dwarf2_per_objfile *per_objfile,
925 struct dwarf2_cu *parent_cu = nullptr,
926 struct dwo_file *dwo_file = nullptr);
927
928 DISABLE_COPY_AND_ASSIGN (cutu_reader);
929
930 const gdb_byte *info_ptr = nullptr;
931 struct die_info *comp_unit_die = nullptr;
932 bool dummy_p = false;
933
934 /* Release the new CU, putting it on the chain. This cannot be done
935 for dummy CUs. */
936 void keep ();
937
938 private:
939 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
940 dwarf2_per_objfile *per_objfile,
941 dwarf2_cu *existing_cu);
942
943 struct dwarf2_per_cu_data *m_this_cu;
944 std::unique_ptr<dwarf2_cu> m_new_cu;
945
946 /* The ordinary abbreviation table. */
947 abbrev_table_up m_abbrev_table_holder;
948
949 /* The DWO abbreviation table. */
950 abbrev_table_up m_dwo_abbrev_table;
951 };
952
953 /* When we construct a partial symbol table entry we only
954 need this much information. */
955 struct partial_die_info : public allocate_on_obstack
956 {
957 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
958
959 /* Disable assign but still keep copy ctor, which is needed
960 load_partial_dies. */
961 partial_die_info& operator=(const partial_die_info& rhs) = delete;
962
963 /* Adjust the partial die before generating a symbol for it. This
964 function may set the is_external flag or change the DIE's
965 name. */
966 void fixup (struct dwarf2_cu *cu);
967
968 /* Read a minimal amount of information into the minimal die
969 structure. */
970 const gdb_byte *read (const struct die_reader_specs *reader,
971 const struct abbrev_info &abbrev,
972 const gdb_byte *info_ptr);
973
974 /* Compute the name of this partial DIE. This memoizes the
975 result, so it is safe to call multiple times. */
976 const char *name (dwarf2_cu *cu);
977
978 /* Offset of this DIE. */
979 const sect_offset sect_off;
980
981 /* DWARF-2 tag for this DIE. */
982 const ENUM_BITFIELD(dwarf_tag) tag : 16;
983
984 /* Assorted flags describing the data found in this DIE. */
985 const unsigned int has_children : 1;
986
987 unsigned int is_external : 1;
988 unsigned int is_declaration : 1;
989 unsigned int has_type : 1;
990 unsigned int has_specification : 1;
991 unsigned int has_pc_info : 1;
992 unsigned int may_be_inlined : 1;
993
994 /* This DIE has been marked DW_AT_main_subprogram. */
995 unsigned int main_subprogram : 1;
996
997 /* Flag set if the SCOPE field of this structure has been
998 computed. */
999 unsigned int scope_set : 1;
1000
1001 /* Flag set if the DIE has a byte_size attribute. */
1002 unsigned int has_byte_size : 1;
1003
1004 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1005 unsigned int has_const_value : 1;
1006
1007 /* Flag set if any of the DIE's children are template arguments. */
1008 unsigned int has_template_arguments : 1;
1009
1010 /* Flag set if fixup has been called on this die. */
1011 unsigned int fixup_called : 1;
1012
1013 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1014 unsigned int is_dwz : 1;
1015
1016 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1017 unsigned int spec_is_dwz : 1;
1018
1019 unsigned int canonical_name : 1;
1020
1021 /* The name of this DIE. Normally the value of DW_AT_name, but
1022 sometimes a default name for unnamed DIEs. */
1023 const char *raw_name = nullptr;
1024
1025 /* The linkage name, if present. */
1026 const char *linkage_name = nullptr;
1027
1028 /* The scope to prepend to our children. This is generally
1029 allocated on the comp_unit_obstack, so will disappear
1030 when this compilation unit leaves the cache. */
1031 const char *scope = nullptr;
1032
1033 /* Some data associated with the partial DIE. The tag determines
1034 which field is live. */
1035 union
1036 {
1037 /* The location description associated with this DIE, if any. */
1038 struct dwarf_block *locdesc;
1039 /* The offset of an import, for DW_TAG_imported_unit. */
1040 sect_offset sect_off;
1041 } d {};
1042
1043 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1044 CORE_ADDR lowpc = 0;
1045 CORE_ADDR highpc = 0;
1046
1047 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1048 DW_AT_sibling, if any. */
1049 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1050 could return DW_AT_sibling values to its caller load_partial_dies. */
1051 const gdb_byte *sibling = nullptr;
1052
1053 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1054 DW_AT_specification (or DW_AT_abstract_origin or
1055 DW_AT_extension). */
1056 sect_offset spec_offset {};
1057
1058 /* Pointers to this DIE's parent, first child, and next sibling,
1059 if any. */
1060 struct partial_die_info *die_parent = nullptr;
1061 struct partial_die_info *die_child = nullptr;
1062 struct partial_die_info *die_sibling = nullptr;
1063
1064 friend struct partial_die_info *
1065 dwarf2_cu::find_partial_die (sect_offset sect_off);
1066
1067 private:
1068 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1069 partial_die_info (sect_offset sect_off)
1070 : partial_die_info (sect_off, DW_TAG_padding, 0)
1071 {
1072 }
1073
1074 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1075 int has_children_)
1076 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1077 {
1078 is_external = 0;
1079 is_declaration = 0;
1080 has_type = 0;
1081 has_specification = 0;
1082 has_pc_info = 0;
1083 may_be_inlined = 0;
1084 main_subprogram = 0;
1085 scope_set = 0;
1086 has_byte_size = 0;
1087 has_const_value = 0;
1088 has_template_arguments = 0;
1089 fixup_called = 0;
1090 is_dwz = 0;
1091 spec_is_dwz = 0;
1092 canonical_name = 0;
1093 }
1094 };
1095
1096 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1097 but this would require a corresponding change in unpack_field_as_long
1098 and friends. */
1099 static int bits_per_byte = 8;
1100
1101 struct variant_part_builder;
1102
1103 /* When reading a variant, we track a bit more information about the
1104 field, and store it in an object of this type. */
1105
1106 struct variant_field
1107 {
1108 int first_field = -1;
1109 int last_field = -1;
1110
1111 /* A variant can contain other variant parts. */
1112 std::vector<variant_part_builder> variant_parts;
1113
1114 /* If we see a DW_TAG_variant, then this will be set if this is the
1115 default branch. */
1116 bool default_branch = false;
1117 /* If we see a DW_AT_discr_value, then this will be the discriminant
1118 value. */
1119 ULONGEST discriminant_value = 0;
1120 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1121 data. */
1122 struct dwarf_block *discr_list_data = nullptr;
1123 };
1124
1125 /* This represents a DW_TAG_variant_part. */
1126
1127 struct variant_part_builder
1128 {
1129 /* The offset of the discriminant field. */
1130 sect_offset discriminant_offset {};
1131
1132 /* Variants that are direct children of this variant part. */
1133 std::vector<variant_field> variants;
1134
1135 /* True if we're currently reading a variant. */
1136 bool processing_variant = false;
1137 };
1138
1139 struct nextfield
1140 {
1141 int accessibility = 0;
1142 int virtuality = 0;
1143 /* Variant parts need to find the discriminant, which is a DIE
1144 reference. We track the section offset of each field to make
1145 this link. */
1146 sect_offset offset;
1147 struct field field {};
1148 };
1149
1150 struct fnfieldlist
1151 {
1152 const char *name = nullptr;
1153 std::vector<struct fn_field> fnfields;
1154 };
1155
1156 /* The routines that read and process dies for a C struct or C++ class
1157 pass lists of data member fields and lists of member function fields
1158 in an instance of a field_info structure, as defined below. */
1159 struct field_info
1160 {
1161 /* List of data member and baseclasses fields. */
1162 std::vector<struct nextfield> fields;
1163 std::vector<struct nextfield> baseclasses;
1164
1165 /* Set if the accessibility of one of the fields is not public. */
1166 int non_public_fields = 0;
1167
1168 /* Member function fieldlist array, contains name of possibly overloaded
1169 member function, number of overloaded member functions and a pointer
1170 to the head of the member function field chain. */
1171 std::vector<struct fnfieldlist> fnfieldlists;
1172
1173 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1174 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1175 std::vector<struct decl_field> typedef_field_list;
1176
1177 /* Nested types defined by this class and the number of elements in this
1178 list. */
1179 std::vector<struct decl_field> nested_types_list;
1180
1181 /* If non-null, this is the variant part we are currently
1182 reading. */
1183 variant_part_builder *current_variant_part = nullptr;
1184 /* This holds all the top-level variant parts attached to the type
1185 we're reading. */
1186 std::vector<variant_part_builder> variant_parts;
1187
1188 /* Return the total number of fields (including baseclasses). */
1189 int nfields () const
1190 {
1191 return fields.size () + baseclasses.size ();
1192 }
1193 };
1194
1195 /* Loaded secondary compilation units are kept in memory until they
1196 have not been referenced for the processing of this many
1197 compilation units. Set this to zero to disable caching. Cache
1198 sizes of up to at least twenty will improve startup time for
1199 typical inter-CU-reference binaries, at an obvious memory cost. */
1200 static int dwarf_max_cache_age = 5;
1201 static void
1202 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1203 struct cmd_list_element *c, const char *value)
1204 {
1205 fprintf_filtered (file, _("The upper bound on the age of cached "
1206 "DWARF compilation units is %s.\n"),
1207 value);
1208 }
1209 \f
1210 /* local function prototypes */
1211
1212 static void dwarf2_find_base_address (struct die_info *die,
1213 struct dwarf2_cu *cu);
1214
1215 static dwarf2_psymtab *create_partial_symtab
1216 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1217 const char *name);
1218
1219 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1220 const gdb_byte *info_ptr,
1221 struct die_info *type_unit_die);
1222
1223 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1224
1225 static void scan_partial_symbols (struct partial_die_info *,
1226 CORE_ADDR *, CORE_ADDR *,
1227 int, struct dwarf2_cu *);
1228
1229 static void add_partial_symbol (struct partial_die_info *,
1230 struct dwarf2_cu *);
1231
1232 static void add_partial_namespace (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1234 int set_addrmap, struct dwarf2_cu *cu);
1235
1236 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1237 CORE_ADDR *highpc, int set_addrmap,
1238 struct dwarf2_cu *cu);
1239
1240 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1241 struct dwarf2_cu *cu);
1242
1243 static void add_partial_subprogram (struct partial_die_info *pdi,
1244 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1245 int need_pc, struct dwarf2_cu *cu);
1246
1247 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1248
1249 static struct partial_die_info *load_partial_dies
1250 (const struct die_reader_specs *, const gdb_byte *, int);
1251
1252 /* A pair of partial_die_info and compilation unit. */
1253 struct cu_partial_die_info
1254 {
1255 /* The compilation unit of the partial_die_info. */
1256 struct dwarf2_cu *cu;
1257 /* A partial_die_info. */
1258 struct partial_die_info *pdi;
1259
1260 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1261 : cu (cu),
1262 pdi (pdi)
1263 { /* Nothing. */ }
1264
1265 private:
1266 cu_partial_die_info () = delete;
1267 };
1268
1269 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1270 struct dwarf2_cu *);
1271
1272 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1273 struct attribute *, struct attr_abbrev *,
1274 const gdb_byte *, bool *need_reprocess);
1275
1276 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1277 struct attribute *attr);
1278
1279 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1280
1281 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1282 dwarf2_section_info *, sect_offset);
1283
1284 static const char *read_indirect_string
1285 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1286 const struct comp_unit_head *, unsigned int *);
1287
1288 static const char *read_indirect_string_at_offset
1289 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1290
1291 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1292 const gdb_byte *,
1293 unsigned int *);
1294
1295 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1296 ULONGEST str_index);
1297
1298 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1299 ULONGEST str_index);
1300
1301 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1302
1303 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1304 struct dwarf2_cu *);
1305
1306 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1307 struct dwarf2_cu *cu);
1308
1309 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1310
1311 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1312 struct dwarf2_cu *cu);
1313
1314 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1315
1316 static struct die_info *die_specification (struct die_info *die,
1317 struct dwarf2_cu **);
1318
1319 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1320 struct dwarf2_cu *cu);
1321
1322 static void dwarf_decode_lines (struct line_header *, const char *,
1323 struct dwarf2_cu *, dwarf2_psymtab *,
1324 CORE_ADDR, int decode_mapping);
1325
1326 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1327 const char *);
1328
1329 static struct symbol *new_symbol (struct die_info *, struct type *,
1330 struct dwarf2_cu *, struct symbol * = NULL);
1331
1332 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1333 struct dwarf2_cu *);
1334
1335 static void dwarf2_const_value_attr (const struct attribute *attr,
1336 struct type *type,
1337 const char *name,
1338 struct obstack *obstack,
1339 struct dwarf2_cu *cu, LONGEST *value,
1340 const gdb_byte **bytes,
1341 struct dwarf2_locexpr_baton **baton);
1342
1343 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1344
1345 static int need_gnat_info (struct dwarf2_cu *);
1346
1347 static struct type *die_descriptive_type (struct die_info *,
1348 struct dwarf2_cu *);
1349
1350 static void set_descriptive_type (struct type *, struct die_info *,
1351 struct dwarf2_cu *);
1352
1353 static struct type *die_containing_type (struct die_info *,
1354 struct dwarf2_cu *);
1355
1356 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1357 struct dwarf2_cu *);
1358
1359 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1360
1361 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1362
1363 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1364
1365 static char *typename_concat (struct obstack *obs, const char *prefix,
1366 const char *suffix, int physname,
1367 struct dwarf2_cu *cu);
1368
1369 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1370
1371 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1372
1373 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1374
1375 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1376
1377 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1378
1379 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1380
1381 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1382 struct dwarf2_cu *, dwarf2_psymtab *);
1383
1384 /* Return the .debug_loclists section to use for cu. */
1385 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1386
1387 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1388 values. Keep the items ordered with increasing constraints compliance. */
1389 enum pc_bounds_kind
1390 {
1391 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1392 PC_BOUNDS_NOT_PRESENT,
1393
1394 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1395 were present but they do not form a valid range of PC addresses. */
1396 PC_BOUNDS_INVALID,
1397
1398 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1399 PC_BOUNDS_RANGES,
1400
1401 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1402 PC_BOUNDS_HIGH_LOW,
1403 };
1404
1405 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1406 CORE_ADDR *, CORE_ADDR *,
1407 struct dwarf2_cu *,
1408 dwarf2_psymtab *);
1409
1410 static void get_scope_pc_bounds (struct die_info *,
1411 CORE_ADDR *, CORE_ADDR *,
1412 struct dwarf2_cu *);
1413
1414 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1415 CORE_ADDR, struct dwarf2_cu *);
1416
1417 static void dwarf2_add_field (struct field_info *, struct die_info *,
1418 struct dwarf2_cu *);
1419
1420 static void dwarf2_attach_fields_to_type (struct field_info *,
1421 struct type *, struct dwarf2_cu *);
1422
1423 static void dwarf2_add_member_fn (struct field_info *,
1424 struct die_info *, struct type *,
1425 struct dwarf2_cu *);
1426
1427 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1428 struct type *,
1429 struct dwarf2_cu *);
1430
1431 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1432
1433 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1434
1435 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1436
1437 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1438
1439 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1440
1441 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1442
1443 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1444
1445 static struct type *read_module_type (struct die_info *die,
1446 struct dwarf2_cu *cu);
1447
1448 static const char *namespace_name (struct die_info *die,
1449 int *is_anonymous, struct dwarf2_cu *);
1450
1451 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1452
1453 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1454 bool * = nullptr);
1455
1456 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1457 struct dwarf2_cu *);
1458
1459 static struct die_info *read_die_and_siblings_1
1460 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1461 struct die_info *);
1462
1463 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1464 const gdb_byte *info_ptr,
1465 const gdb_byte **new_info_ptr,
1466 struct die_info *parent);
1467
1468 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1469 struct die_info **, const gdb_byte *,
1470 int);
1471
1472 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1473 struct die_info **, const gdb_byte *);
1474
1475 static void process_die (struct die_info *, struct dwarf2_cu *);
1476
1477 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1478 struct objfile *);
1479
1480 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1481
1482 static const char *dwarf2_full_name (const char *name,
1483 struct die_info *die,
1484 struct dwarf2_cu *cu);
1485
1486 static const char *dwarf2_physname (const char *name, struct die_info *die,
1487 struct dwarf2_cu *cu);
1488
1489 static struct die_info *dwarf2_extension (struct die_info *die,
1490 struct dwarf2_cu **);
1491
1492 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1493
1494 static void dump_die_for_error (struct die_info *);
1495
1496 static void dump_die_1 (struct ui_file *, int level, int max_level,
1497 struct die_info *);
1498
1499 /*static*/ void dump_die (struct die_info *, int max_level);
1500
1501 static void store_in_ref_table (struct die_info *,
1502 struct dwarf2_cu *);
1503
1504 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1505 const struct attribute *,
1506 struct dwarf2_cu **);
1507
1508 static struct die_info *follow_die_ref (struct die_info *,
1509 const struct attribute *,
1510 struct dwarf2_cu **);
1511
1512 static struct die_info *follow_die_sig (struct die_info *,
1513 const struct attribute *,
1514 struct dwarf2_cu **);
1515
1516 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1517 struct dwarf2_cu *);
1518
1519 static struct type *get_DW_AT_signature_type (struct die_info *,
1520 const struct attribute *,
1521 struct dwarf2_cu *);
1522
1523 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1524 dwarf2_per_objfile *per_objfile);
1525
1526 static void read_signatured_type (signatured_type *sig_type,
1527 dwarf2_per_objfile *per_objfile);
1528
1529 static int attr_to_dynamic_prop (const struct attribute *attr,
1530 struct die_info *die, struct dwarf2_cu *cu,
1531 struct dynamic_prop *prop, struct type *type);
1532
1533 /* memory allocation interface */
1534
1535 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1536
1537 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1538
1539 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1540
1541 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1542 struct dwarf2_loclist_baton *baton,
1543 const struct attribute *attr);
1544
1545 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1546 struct symbol *sym,
1547 struct dwarf2_cu *cu,
1548 int is_block);
1549
1550 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1551 const gdb_byte *info_ptr,
1552 struct abbrev_info *abbrev);
1553
1554 static hashval_t partial_die_hash (const void *item);
1555
1556 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1557
1558 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1559 (sect_offset sect_off, unsigned int offset_in_dwz,
1560 dwarf2_per_objfile *per_objfile);
1561
1562 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1563 struct die_info *comp_unit_die,
1564 enum language pretend_language);
1565
1566 static struct type *set_die_type (struct die_info *, struct type *,
1567 struct dwarf2_cu *);
1568
1569 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1570
1571 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1572
1573 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile,
1575 bool skip_partial,
1576 enum language pretend_language);
1577
1578 static void process_full_comp_unit (dwarf2_cu *cu,
1579 enum language pretend_language);
1580
1581 static void process_full_type_unit (dwarf2_cu *cu,
1582 enum language pretend_language);
1583
1584 static void dwarf2_add_dependence (struct dwarf2_cu *,
1585 struct dwarf2_per_cu_data *);
1586
1587 static void dwarf2_mark (struct dwarf2_cu *);
1588
1589 static struct type *get_die_type_at_offset (sect_offset,
1590 dwarf2_per_cu_data *per_cu,
1591 dwarf2_per_objfile *per_objfile);
1592
1593 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1594
1595 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1596 dwarf2_per_objfile *per_objfile,
1597 enum language pretend_language);
1598
1599 static void process_queue (dwarf2_per_objfile *per_objfile);
1600
1601 /* Class, the destructor of which frees all allocated queue entries. This
1602 will only have work to do if an error was thrown while processing the
1603 dwarf. If no error was thrown then the queue entries should have all
1604 been processed, and freed, as we went along. */
1605
1606 class dwarf2_queue_guard
1607 {
1608 public:
1609 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1610 : m_per_objfile (per_objfile)
1611 {
1612 }
1613
1614 /* Free any entries remaining on the queue. There should only be
1615 entries left if we hit an error while processing the dwarf. */
1616 ~dwarf2_queue_guard ()
1617 {
1618 /* Ensure that no memory is allocated by the queue. */
1619 std::queue<dwarf2_queue_item> empty;
1620 std::swap (m_per_objfile->per_bfd->queue, empty);
1621 }
1622
1623 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1624
1625 private:
1626 dwarf2_per_objfile *m_per_objfile;
1627 };
1628
1629 dwarf2_queue_item::~dwarf2_queue_item ()
1630 {
1631 /* Anything still marked queued is likely to be in an
1632 inconsistent state, so discard it. */
1633 if (per_cu->queued)
1634 {
1635 per_objfile->remove_cu (per_cu);
1636 per_cu->queued = 0;
1637 }
1638 }
1639
1640 /* The return type of find_file_and_directory. Note, the enclosed
1641 string pointers are only valid while this object is valid. */
1642
1643 struct file_and_directory
1644 {
1645 /* The filename. This is never NULL. */
1646 const char *name;
1647
1648 /* The compilation directory. NULL if not known. If we needed to
1649 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1650 points directly to the DW_AT_comp_dir string attribute owned by
1651 the obstack that owns the DIE. */
1652 const char *comp_dir;
1653
1654 /* If we needed to build a new string for comp_dir, this is what
1655 owns the storage. */
1656 std::string comp_dir_storage;
1657 };
1658
1659 static file_and_directory find_file_and_directory (struct die_info *die,
1660 struct dwarf2_cu *cu);
1661
1662 static htab_up allocate_signatured_type_table ();
1663
1664 static htab_up allocate_dwo_unit_table ();
1665
1666 static struct dwo_unit *lookup_dwo_unit_in_dwp
1667 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1668 const char *comp_dir, ULONGEST signature, int is_debug_types);
1669
1670 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1671
1672 static struct dwo_unit *lookup_dwo_comp_unit
1673 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1674 ULONGEST signature);
1675
1676 static struct dwo_unit *lookup_dwo_type_unit
1677 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1678
1679 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1680
1681 /* A unique pointer to a dwo_file. */
1682
1683 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1684
1685 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1686
1687 static void check_producer (struct dwarf2_cu *cu);
1688
1689 static void free_line_header_voidp (void *arg);
1690 \f
1691 /* Various complaints about symbol reading that don't abort the process. */
1692
1693 static void
1694 dwarf2_debug_line_missing_file_complaint (void)
1695 {
1696 complaint (_(".debug_line section has line data without a file"));
1697 }
1698
1699 static void
1700 dwarf2_debug_line_missing_end_sequence_complaint (void)
1701 {
1702 complaint (_(".debug_line section has line "
1703 "program sequence without an end"));
1704 }
1705
1706 static void
1707 dwarf2_complex_location_expr_complaint (void)
1708 {
1709 complaint (_("location expression too complex"));
1710 }
1711
1712 static void
1713 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1714 int arg3)
1715 {
1716 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1717 arg1, arg2, arg3);
1718 }
1719
1720 static void
1721 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1722 {
1723 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1724 arg1, arg2);
1725 }
1726
1727 /* Hash function for line_header_hash. */
1728
1729 static hashval_t
1730 line_header_hash (const struct line_header *ofs)
1731 {
1732 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1733 }
1734
1735 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1736
1737 static hashval_t
1738 line_header_hash_voidp (const void *item)
1739 {
1740 const struct line_header *ofs = (const struct line_header *) item;
1741
1742 return line_header_hash (ofs);
1743 }
1744
1745 /* Equality function for line_header_hash. */
1746
1747 static int
1748 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1749 {
1750 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1751 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1752
1753 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1754 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1755 }
1756
1757 \f
1758
1759 /* See declaration. */
1760
1761 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1762 bool can_copy_)
1763 : obfd (obfd),
1764 can_copy (can_copy_)
1765 {
1766 if (names == NULL)
1767 names = &dwarf2_elf_names;
1768
1769 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1770 locate_sections (obfd, sec, *names);
1771 }
1772
1773 dwarf2_per_bfd::~dwarf2_per_bfd ()
1774 {
1775 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1776 per_cu->imported_symtabs_free ();
1777
1778 for (signatured_type *sig_type : all_type_units)
1779 sig_type->per_cu.imported_symtabs_free ();
1780
1781 /* Everything else should be on this->obstack. */
1782 }
1783
1784 /* See read.h. */
1785
1786 void
1787 dwarf2_per_objfile::remove_all_cus ()
1788 {
1789 for (auto pair : m_dwarf2_cus)
1790 delete pair.second;
1791
1792 m_dwarf2_cus.clear ();
1793 }
1794
1795 /* A helper class that calls free_cached_comp_units on
1796 destruction. */
1797
1798 class free_cached_comp_units
1799 {
1800 public:
1801
1802 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1803 : m_per_objfile (per_objfile)
1804 {
1805 }
1806
1807 ~free_cached_comp_units ()
1808 {
1809 m_per_objfile->remove_all_cus ();
1810 }
1811
1812 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1813
1814 private:
1815
1816 dwarf2_per_objfile *m_per_objfile;
1817 };
1818
1819 /* See read.h. */
1820
1821 bool
1822 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1823 {
1824 gdb_assert (per_cu->index < this->m_symtabs.size ());
1825
1826 return this->m_symtabs[per_cu->index] != nullptr;
1827 }
1828
1829 /* See read.h. */
1830
1831 compunit_symtab *
1832 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1833 {
1834 gdb_assert (per_cu->index < this->m_symtabs.size ());
1835
1836 return this->m_symtabs[per_cu->index];
1837 }
1838
1839 /* See read.h. */
1840
1841 void
1842 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1843 compunit_symtab *symtab)
1844 {
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1847
1848 this->m_symtabs[per_cu->index] = symtab;
1849 }
1850
1851 /* Try to locate the sections we need for DWARF 2 debugging
1852 information and return true if we have enough to do something.
1853 NAMES points to the dwarf2 section names, or is NULL if the standard
1854 ELF names are used. CAN_COPY is true for formats where symbol
1855 interposition is possible and so symbol values must follow copy
1856 relocation rules. */
1857
1858 int
1859 dwarf2_has_info (struct objfile *objfile,
1860 const struct dwarf2_debug_sections *names,
1861 bool can_copy)
1862 {
1863 if (objfile->flags & OBJF_READNEVER)
1864 return 0;
1865
1866 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1867
1868 if (per_objfile == NULL)
1869 {
1870 dwarf2_per_bfd *per_bfd;
1871
1872 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1873 doesn't require relocations and if there aren't partial symbols
1874 from some other reader. */
1875 if (!objfile_has_partial_symbols (objfile)
1876 && !gdb_bfd_requires_relocations (objfile->obfd))
1877 {
1878 /* See if one has been created for this BFD yet. */
1879 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1880
1881 if (per_bfd == nullptr)
1882 {
1883 /* No, create it now. */
1884 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1885 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1886 }
1887 }
1888 else
1889 {
1890 /* No sharing possible, create one specifically for this objfile. */
1891 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1892 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1893 }
1894
1895 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1896 }
1897
1898 return (!per_objfile->per_bfd->info.is_virtual
1899 && per_objfile->per_bfd->info.s.section != NULL
1900 && !per_objfile->per_bfd->abbrev.is_virtual
1901 && per_objfile->per_bfd->abbrev.s.section != NULL);
1902 }
1903
1904 /* When loading sections, we look either for uncompressed section or for
1905 compressed section names. */
1906
1907 static int
1908 section_is_p (const char *section_name,
1909 const struct dwarf2_section_names *names)
1910 {
1911 if (names->normal != NULL
1912 && strcmp (section_name, names->normal) == 0)
1913 return 1;
1914 if (names->compressed != NULL
1915 && strcmp (section_name, names->compressed) == 0)
1916 return 1;
1917 return 0;
1918 }
1919
1920 /* See declaration. */
1921
1922 void
1923 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1924 const dwarf2_debug_sections &names)
1925 {
1926 flagword aflag = bfd_section_flags (sectp);
1927
1928 if ((aflag & SEC_HAS_CONTENTS) == 0)
1929 {
1930 }
1931 else if (elf_section_data (sectp)->this_hdr.sh_size
1932 > bfd_get_file_size (abfd))
1933 {
1934 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1935 warning (_("Discarding section %s which has a section size (%s"
1936 ") larger than the file size [in module %s]"),
1937 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1938 bfd_get_filename (abfd));
1939 }
1940 else if (section_is_p (sectp->name, &names.info))
1941 {
1942 this->info.s.section = sectp;
1943 this->info.size = bfd_section_size (sectp);
1944 }
1945 else if (section_is_p (sectp->name, &names.abbrev))
1946 {
1947 this->abbrev.s.section = sectp;
1948 this->abbrev.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.line))
1951 {
1952 this->line.s.section = sectp;
1953 this->line.size = bfd_section_size (sectp);
1954 }
1955 else if (section_is_p (sectp->name, &names.loc))
1956 {
1957 this->loc.s.section = sectp;
1958 this->loc.size = bfd_section_size (sectp);
1959 }
1960 else if (section_is_p (sectp->name, &names.loclists))
1961 {
1962 this->loclists.s.section = sectp;
1963 this->loclists.size = bfd_section_size (sectp);
1964 }
1965 else if (section_is_p (sectp->name, &names.macinfo))
1966 {
1967 this->macinfo.s.section = sectp;
1968 this->macinfo.size = bfd_section_size (sectp);
1969 }
1970 else if (section_is_p (sectp->name, &names.macro))
1971 {
1972 this->macro.s.section = sectp;
1973 this->macro.size = bfd_section_size (sectp);
1974 }
1975 else if (section_is_p (sectp->name, &names.str))
1976 {
1977 this->str.s.section = sectp;
1978 this->str.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.str_offsets))
1981 {
1982 this->str_offsets.s.section = sectp;
1983 this->str_offsets.size = bfd_section_size (sectp);
1984 }
1985 else if (section_is_p (sectp->name, &names.line_str))
1986 {
1987 this->line_str.s.section = sectp;
1988 this->line_str.size = bfd_section_size (sectp);
1989 }
1990 else if (section_is_p (sectp->name, &names.addr))
1991 {
1992 this->addr.s.section = sectp;
1993 this->addr.size = bfd_section_size (sectp);
1994 }
1995 else if (section_is_p (sectp->name, &names.frame))
1996 {
1997 this->frame.s.section = sectp;
1998 this->frame.size = bfd_section_size (sectp);
1999 }
2000 else if (section_is_p (sectp->name, &names.eh_frame))
2001 {
2002 this->eh_frame.s.section = sectp;
2003 this->eh_frame.size = bfd_section_size (sectp);
2004 }
2005 else if (section_is_p (sectp->name, &names.ranges))
2006 {
2007 this->ranges.s.section = sectp;
2008 this->ranges.size = bfd_section_size (sectp);
2009 }
2010 else if (section_is_p (sectp->name, &names.rnglists))
2011 {
2012 this->rnglists.s.section = sectp;
2013 this->rnglists.size = bfd_section_size (sectp);
2014 }
2015 else if (section_is_p (sectp->name, &names.types))
2016 {
2017 struct dwarf2_section_info type_section;
2018
2019 memset (&type_section, 0, sizeof (type_section));
2020 type_section.s.section = sectp;
2021 type_section.size = bfd_section_size (sectp);
2022
2023 this->types.push_back (type_section);
2024 }
2025 else if (section_is_p (sectp->name, &names.gdb_index))
2026 {
2027 this->gdb_index.s.section = sectp;
2028 this->gdb_index.size = bfd_section_size (sectp);
2029 }
2030 else if (section_is_p (sectp->name, &names.debug_names))
2031 {
2032 this->debug_names.s.section = sectp;
2033 this->debug_names.size = bfd_section_size (sectp);
2034 }
2035 else if (section_is_p (sectp->name, &names.debug_aranges))
2036 {
2037 this->debug_aranges.s.section = sectp;
2038 this->debug_aranges.size = bfd_section_size (sectp);
2039 }
2040
2041 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2042 && bfd_section_vma (sectp) == 0)
2043 this->has_section_at_zero = true;
2044 }
2045
2046 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2047 SECTION_NAME. */
2048
2049 void
2050 dwarf2_get_section_info (struct objfile *objfile,
2051 enum dwarf2_section_enum sect,
2052 asection **sectp, const gdb_byte **bufp,
2053 bfd_size_type *sizep)
2054 {
2055 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2056 struct dwarf2_section_info *info;
2057
2058 /* We may see an objfile without any DWARF, in which case we just
2059 return nothing. */
2060 if (per_objfile == NULL)
2061 {
2062 *sectp = NULL;
2063 *bufp = NULL;
2064 *sizep = 0;
2065 return;
2066 }
2067 switch (sect)
2068 {
2069 case DWARF2_DEBUG_FRAME:
2070 info = &per_objfile->per_bfd->frame;
2071 break;
2072 case DWARF2_EH_FRAME:
2073 info = &per_objfile->per_bfd->eh_frame;
2074 break;
2075 default:
2076 gdb_assert_not_reached ("unexpected section");
2077 }
2078
2079 info->read (objfile);
2080
2081 *sectp = info->get_bfd_section ();
2082 *bufp = info->buffer;
2083 *sizep = info->size;
2084 }
2085
2086 /* A helper function to find the sections for a .dwz file. */
2087
2088 static void
2089 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2090 {
2091 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2092
2093 /* Note that we only support the standard ELF names, because .dwz
2094 is ELF-only (at the time of writing). */
2095 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2096 {
2097 dwz_file->abbrev.s.section = sectp;
2098 dwz_file->abbrev.size = bfd_section_size (sectp);
2099 }
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2101 {
2102 dwz_file->info.s.section = sectp;
2103 dwz_file->info.size = bfd_section_size (sectp);
2104 }
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2106 {
2107 dwz_file->str.s.section = sectp;
2108 dwz_file->str.size = bfd_section_size (sectp);
2109 }
2110 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2111 {
2112 dwz_file->line.s.section = sectp;
2113 dwz_file->line.size = bfd_section_size (sectp);
2114 }
2115 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2116 {
2117 dwz_file->macro.s.section = sectp;
2118 dwz_file->macro.size = bfd_section_size (sectp);
2119 }
2120 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2121 {
2122 dwz_file->gdb_index.s.section = sectp;
2123 dwz_file->gdb_index.size = bfd_section_size (sectp);
2124 }
2125 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2126 {
2127 dwz_file->debug_names.s.section = sectp;
2128 dwz_file->debug_names.size = bfd_section_size (sectp);
2129 }
2130 }
2131
2132 /* See dwarf2read.h. */
2133
2134 struct dwz_file *
2135 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2136 {
2137 const char *filename;
2138 bfd_size_type buildid_len_arg;
2139 size_t buildid_len;
2140 bfd_byte *buildid;
2141
2142 if (per_bfd->dwz_file != NULL)
2143 return per_bfd->dwz_file.get ();
2144
2145 bfd_set_error (bfd_error_no_error);
2146 gdb::unique_xmalloc_ptr<char> data
2147 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2148 &buildid_len_arg, &buildid));
2149 if (data == NULL)
2150 {
2151 if (bfd_get_error () == bfd_error_no_error)
2152 return NULL;
2153 error (_("could not read '.gnu_debugaltlink' section: %s"),
2154 bfd_errmsg (bfd_get_error ()));
2155 }
2156
2157 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2158
2159 buildid_len = (size_t) buildid_len_arg;
2160
2161 filename = data.get ();
2162
2163 std::string abs_storage;
2164 if (!IS_ABSOLUTE_PATH (filename))
2165 {
2166 gdb::unique_xmalloc_ptr<char> abs
2167 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2168
2169 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2170 filename = abs_storage.c_str ();
2171 }
2172
2173 /* First try the file name given in the section. If that doesn't
2174 work, try to use the build-id instead. */
2175 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2176 if (dwz_bfd != NULL)
2177 {
2178 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2179 dwz_bfd.reset (nullptr);
2180 }
2181
2182 if (dwz_bfd == NULL)
2183 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2184
2185 if (dwz_bfd == nullptr)
2186 {
2187 gdb::unique_xmalloc_ptr<char> alt_filename;
2188 const char *origname = bfd_get_filename (per_bfd->obfd);
2189
2190 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2191 buildid_len,
2192 origname,
2193 &alt_filename));
2194
2195 if (fd.get () >= 0)
2196 {
2197 /* File successfully retrieved from server. */
2198 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2199
2200 if (dwz_bfd == nullptr)
2201 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2202 alt_filename.get ());
2203 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2204 dwz_bfd.reset (nullptr);
2205 }
2206 }
2207
2208 if (dwz_bfd == NULL)
2209 error (_("could not find '.gnu_debugaltlink' file for %s"),
2210 bfd_get_filename (per_bfd->obfd));
2211
2212 std::unique_ptr<struct dwz_file> result
2213 (new struct dwz_file (std::move (dwz_bfd)));
2214
2215 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2216 result.get ());
2217
2218 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2219 per_bfd->dwz_file = std::move (result);
2220 return per_bfd->dwz_file.get ();
2221 }
2222 \f
2223 /* DWARF quick_symbols_functions support. */
2224
2225 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2226 unique line tables, so we maintain a separate table of all .debug_line
2227 derived entries to support the sharing.
2228 All the quick functions need is the list of file names. We discard the
2229 line_header when we're done and don't need to record it here. */
2230 struct quick_file_names
2231 {
2232 /* The data used to construct the hash key. */
2233 struct stmt_list_hash hash;
2234
2235 /* The number of entries in file_names, real_names. */
2236 unsigned int num_file_names;
2237
2238 /* The file names from the line table, after being run through
2239 file_full_name. */
2240 const char **file_names;
2241
2242 /* The file names from the line table after being run through
2243 gdb_realpath. These are computed lazily. */
2244 const char **real_names;
2245 };
2246
2247 /* When using the index (and thus not using psymtabs), each CU has an
2248 object of this type. This is used to hold information needed by
2249 the various "quick" methods. */
2250 struct dwarf2_per_cu_quick_data
2251 {
2252 /* The file table. This can be NULL if there was no file table
2253 or it's currently not read in.
2254 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2255 struct quick_file_names *file_names;
2256
2257 /* A temporary mark bit used when iterating over all CUs in
2258 expand_symtabs_matching. */
2259 unsigned int mark : 1;
2260
2261 /* True if we've tried to read the file table and found there isn't one.
2262 There will be no point in trying to read it again next time. */
2263 unsigned int no_file_data : 1;
2264 };
2265
2266 /* Utility hash function for a stmt_list_hash. */
2267
2268 static hashval_t
2269 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2270 {
2271 hashval_t v = 0;
2272
2273 if (stmt_list_hash->dwo_unit != NULL)
2274 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2275 v += to_underlying (stmt_list_hash->line_sect_off);
2276 return v;
2277 }
2278
2279 /* Utility equality function for a stmt_list_hash. */
2280
2281 static int
2282 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2283 const struct stmt_list_hash *rhs)
2284 {
2285 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2286 return 0;
2287 if (lhs->dwo_unit != NULL
2288 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2289 return 0;
2290
2291 return lhs->line_sect_off == rhs->line_sect_off;
2292 }
2293
2294 /* Hash function for a quick_file_names. */
2295
2296 static hashval_t
2297 hash_file_name_entry (const void *e)
2298 {
2299 const struct quick_file_names *file_data
2300 = (const struct quick_file_names *) e;
2301
2302 return hash_stmt_list_entry (&file_data->hash);
2303 }
2304
2305 /* Equality function for a quick_file_names. */
2306
2307 static int
2308 eq_file_name_entry (const void *a, const void *b)
2309 {
2310 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2311 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2312
2313 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2314 }
2315
2316 /* Delete function for a quick_file_names. */
2317
2318 static void
2319 delete_file_name_entry (void *e)
2320 {
2321 struct quick_file_names *file_data = (struct quick_file_names *) e;
2322 int i;
2323
2324 for (i = 0; i < file_data->num_file_names; ++i)
2325 {
2326 xfree ((void*) file_data->file_names[i]);
2327 if (file_data->real_names)
2328 xfree ((void*) file_data->real_names[i]);
2329 }
2330
2331 /* The space for the struct itself lives on the obstack, so we don't
2332 free it here. */
2333 }
2334
2335 /* Create a quick_file_names hash table. */
2336
2337 static htab_up
2338 create_quick_file_names_table (unsigned int nr_initial_entries)
2339 {
2340 return htab_up (htab_create_alloc (nr_initial_entries,
2341 hash_file_name_entry, eq_file_name_entry,
2342 delete_file_name_entry, xcalloc, xfree));
2343 }
2344
2345 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2346 function is unrelated to symtabs, symtab would have to be created afterwards.
2347 You should call age_cached_comp_units after processing the CU. */
2348
2349 static dwarf2_cu *
2350 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2351 bool skip_partial)
2352 {
2353 if (per_cu->is_debug_types)
2354 load_full_type_unit (per_cu, per_objfile);
2355 else
2356 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2357
2358 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2359 if (cu == nullptr)
2360 return nullptr; /* Dummy CU. */
2361
2362 dwarf2_find_base_address (cu->dies, cu);
2363
2364 return cu;
2365 }
2366
2367 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2368
2369 static void
2370 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2371 dwarf2_per_objfile *per_objfile, bool skip_partial)
2372 {
2373 /* Skip type_unit_groups, reading the type units they contain
2374 is handled elsewhere. */
2375 if (per_cu->type_unit_group_p ())
2376 return;
2377
2378 /* The destructor of dwarf2_queue_guard frees any entries left on
2379 the queue. After this point we're guaranteed to leave this function
2380 with the dwarf queue empty. */
2381 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2382
2383 if (!per_objfile->symtab_set_p (per_cu))
2384 {
2385 queue_comp_unit (per_cu, per_objfile, language_minimal);
2386 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2387
2388 /* If we just loaded a CU from a DWO, and we're working with an index
2389 that may badly handle TUs, load all the TUs in that DWO as well.
2390 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2391 if (!per_cu->is_debug_types
2392 && cu != NULL
2393 && cu->dwo_unit != NULL
2394 && per_objfile->per_bfd->index_table != NULL
2395 && per_objfile->per_bfd->index_table->version <= 7
2396 /* DWP files aren't supported yet. */
2397 && get_dwp_file (per_objfile) == NULL)
2398 queue_and_load_all_dwo_tus (cu);
2399 }
2400
2401 process_queue (per_objfile);
2402
2403 /* Age the cache, releasing compilation units that have not
2404 been used recently. */
2405 per_objfile->age_comp_units ();
2406 }
2407
2408 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2409 the per-objfile for which this symtab is instantiated.
2410
2411 Returns the resulting symbol table. */
2412
2413 static struct compunit_symtab *
2414 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2415 dwarf2_per_objfile *per_objfile,
2416 bool skip_partial)
2417 {
2418 gdb_assert (per_objfile->per_bfd->using_index);
2419
2420 if (!per_objfile->symtab_set_p (per_cu))
2421 {
2422 free_cached_comp_units freer (per_objfile);
2423 scoped_restore decrementer = increment_reading_symtab ();
2424 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2425 process_cu_includes (per_objfile);
2426 }
2427
2428 return per_objfile->get_symtab (per_cu);
2429 }
2430
2431 /* See declaration. */
2432
2433 dwarf2_per_cu_data *
2434 dwarf2_per_bfd::get_cutu (int index)
2435 {
2436 if (index >= this->all_comp_units.size ())
2437 {
2438 index -= this->all_comp_units.size ();
2439 gdb_assert (index < this->all_type_units.size ());
2440 return &this->all_type_units[index]->per_cu;
2441 }
2442
2443 return this->all_comp_units[index];
2444 }
2445
2446 /* See declaration. */
2447
2448 dwarf2_per_cu_data *
2449 dwarf2_per_bfd::get_cu (int index)
2450 {
2451 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2452
2453 return this->all_comp_units[index];
2454 }
2455
2456 /* See declaration. */
2457
2458 signatured_type *
2459 dwarf2_per_bfd::get_tu (int index)
2460 {
2461 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2462
2463 return this->all_type_units[index];
2464 }
2465
2466 /* See read.h. */
2467
2468 dwarf2_per_cu_data *
2469 dwarf2_per_bfd::allocate_per_cu ()
2470 {
2471 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2472 result->per_bfd = this;
2473 result->index = m_num_psymtabs++;
2474 return result;
2475 }
2476
2477 /* See read.h. */
2478
2479 signatured_type *
2480 dwarf2_per_bfd::allocate_signatured_type ()
2481 {
2482 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2483 result->per_cu.per_bfd = this;
2484 result->per_cu.index = m_num_psymtabs++;
2485 return result;
2486 }
2487
2488 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2489 obstack, and constructed with the specified field values. */
2490
2491 static dwarf2_per_cu_data *
2492 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2493 struct dwarf2_section_info *section,
2494 int is_dwz,
2495 sect_offset sect_off, ULONGEST length)
2496 {
2497 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2498 the_cu->sect_off = sect_off;
2499 the_cu->length = length;
2500 the_cu->section = section;
2501 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2502 struct dwarf2_per_cu_quick_data);
2503 the_cu->is_dwz = is_dwz;
2504 return the_cu;
2505 }
2506
2507 /* A helper for create_cus_from_index that handles a given list of
2508 CUs. */
2509
2510 static void
2511 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2512 const gdb_byte *cu_list, offset_type n_elements,
2513 struct dwarf2_section_info *section,
2514 int is_dwz)
2515 {
2516 for (offset_type i = 0; i < n_elements; i += 2)
2517 {
2518 gdb_static_assert (sizeof (ULONGEST) >= 8);
2519
2520 sect_offset sect_off
2521 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2522 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2523 cu_list += 2 * 8;
2524
2525 dwarf2_per_cu_data *per_cu
2526 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2527 length);
2528 per_bfd->all_comp_units.push_back (per_cu);
2529 }
2530 }
2531
2532 /* Read the CU list from the mapped index, and use it to create all
2533 the CU objects for PER_BFD. */
2534
2535 static void
2536 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2537 const gdb_byte *cu_list, offset_type cu_list_elements,
2538 const gdb_byte *dwz_list, offset_type dwz_elements)
2539 {
2540 gdb_assert (per_bfd->all_comp_units.empty ());
2541 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2542
2543 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2544 &per_bfd->info, 0);
2545
2546 if (dwz_elements == 0)
2547 return;
2548
2549 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2550 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2551 &dwz->info, 1);
2552 }
2553
2554 /* Create the signatured type hash table from the index. */
2555
2556 static void
2557 create_signatured_type_table_from_index
2558 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2559 const gdb_byte *bytes, offset_type elements)
2560 {
2561 gdb_assert (per_bfd->all_type_units.empty ());
2562 per_bfd->all_type_units.reserve (elements / 3);
2563
2564 htab_up sig_types_hash = allocate_signatured_type_table ();
2565
2566 for (offset_type i = 0; i < elements; i += 3)
2567 {
2568 struct signatured_type *sig_type;
2569 ULONGEST signature;
2570 void **slot;
2571 cu_offset type_offset_in_tu;
2572
2573 gdb_static_assert (sizeof (ULONGEST) >= 8);
2574 sect_offset sect_off
2575 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2576 type_offset_in_tu
2577 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2578 BFD_ENDIAN_LITTLE);
2579 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2580 bytes += 3 * 8;
2581
2582 sig_type = per_bfd->allocate_signatured_type ();
2583 sig_type->signature = signature;
2584 sig_type->type_offset_in_tu = type_offset_in_tu;
2585 sig_type->per_cu.is_debug_types = 1;
2586 sig_type->per_cu.section = section;
2587 sig_type->per_cu.sect_off = sect_off;
2588 sig_type->per_cu.v.quick
2589 = OBSTACK_ZALLOC (&per_bfd->obstack,
2590 struct dwarf2_per_cu_quick_data);
2591
2592 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2593 *slot = sig_type;
2594
2595 per_bfd->all_type_units.push_back (sig_type);
2596 }
2597
2598 per_bfd->signatured_types = std::move (sig_types_hash);
2599 }
2600
2601 /* Create the signatured type hash table from .debug_names. */
2602
2603 static void
2604 create_signatured_type_table_from_debug_names
2605 (dwarf2_per_objfile *per_objfile,
2606 const mapped_debug_names &map,
2607 struct dwarf2_section_info *section,
2608 struct dwarf2_section_info *abbrev_section)
2609 {
2610 struct objfile *objfile = per_objfile->objfile;
2611
2612 section->read (objfile);
2613 abbrev_section->read (objfile);
2614
2615 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2616 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2617
2618 htab_up sig_types_hash = allocate_signatured_type_table ();
2619
2620 for (uint32_t i = 0; i < map.tu_count; ++i)
2621 {
2622 struct signatured_type *sig_type;
2623 void **slot;
2624
2625 sect_offset sect_off
2626 = (sect_offset) (extract_unsigned_integer
2627 (map.tu_table_reordered + i * map.offset_size,
2628 map.offset_size,
2629 map.dwarf5_byte_order));
2630
2631 comp_unit_head cu_header;
2632 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2633 abbrev_section,
2634 section->buffer + to_underlying (sect_off),
2635 rcuh_kind::TYPE);
2636
2637 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2638 sig_type->signature = cu_header.signature;
2639 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2640 sig_type->per_cu.is_debug_types = 1;
2641 sig_type->per_cu.section = section;
2642 sig_type->per_cu.sect_off = sect_off;
2643 sig_type->per_cu.v.quick
2644 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2645 struct dwarf2_per_cu_quick_data);
2646
2647 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2648 *slot = sig_type;
2649
2650 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2651 }
2652
2653 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2654 }
2655
2656 /* Read the address map data from the mapped index, and use it to
2657 populate the objfile's psymtabs_addrmap. */
2658
2659 static void
2660 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2661 struct mapped_index *index)
2662 {
2663 struct objfile *objfile = per_objfile->objfile;
2664 struct gdbarch *gdbarch = objfile->arch ();
2665 const gdb_byte *iter, *end;
2666 struct addrmap *mutable_map;
2667 CORE_ADDR baseaddr;
2668
2669 auto_obstack temp_obstack;
2670
2671 mutable_map = addrmap_create_mutable (&temp_obstack);
2672
2673 iter = index->address_table.data ();
2674 end = iter + index->address_table.size ();
2675
2676 baseaddr = objfile->text_section_offset ();
2677
2678 while (iter < end)
2679 {
2680 ULONGEST hi, lo, cu_index;
2681 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2682 iter += 8;
2683 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2684 iter += 8;
2685 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2686 iter += 4;
2687
2688 if (lo > hi)
2689 {
2690 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2691 hex_string (lo), hex_string (hi));
2692 continue;
2693 }
2694
2695 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2696 {
2697 complaint (_(".gdb_index address table has invalid CU number %u"),
2698 (unsigned) cu_index);
2699 continue;
2700 }
2701
2702 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2703 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2704 addrmap_set_empty (mutable_map, lo, hi - 1,
2705 per_objfile->per_bfd->get_cu (cu_index));
2706 }
2707
2708 objfile->partial_symtabs->psymtabs_addrmap
2709 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2710 }
2711
2712 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2713 populate the objfile's psymtabs_addrmap. */
2714
2715 static void
2716 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2717 struct dwarf2_section_info *section)
2718 {
2719 struct objfile *objfile = per_objfile->objfile;
2720 bfd *abfd = objfile->obfd;
2721 struct gdbarch *gdbarch = objfile->arch ();
2722 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2723
2724 auto_obstack temp_obstack;
2725 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2726
2727 std::unordered_map<sect_offset,
2728 dwarf2_per_cu_data *,
2729 gdb::hash_enum<sect_offset>>
2730 debug_info_offset_to_per_cu;
2731 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2732 {
2733 const auto insertpair
2734 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2735 if (!insertpair.second)
2736 {
2737 warning (_("Section .debug_aranges in %s has duplicate "
2738 "debug_info_offset %s, ignoring .debug_aranges."),
2739 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2740 return;
2741 }
2742 }
2743
2744 section->read (objfile);
2745
2746 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2747
2748 const gdb_byte *addr = section->buffer;
2749
2750 while (addr < section->buffer + section->size)
2751 {
2752 const gdb_byte *const entry_addr = addr;
2753 unsigned int bytes_read;
2754
2755 const LONGEST entry_length = read_initial_length (abfd, addr,
2756 &bytes_read);
2757 addr += bytes_read;
2758
2759 const gdb_byte *const entry_end = addr + entry_length;
2760 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2761 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2762 if (addr + entry_length > section->buffer + section->size)
2763 {
2764 warning (_("Section .debug_aranges in %s entry at offset %s "
2765 "length %s exceeds section length %s, "
2766 "ignoring .debug_aranges."),
2767 objfile_name (objfile),
2768 plongest (entry_addr - section->buffer),
2769 plongest (bytes_read + entry_length),
2770 pulongest (section->size));
2771 return;
2772 }
2773
2774 /* The version number. */
2775 const uint16_t version = read_2_bytes (abfd, addr);
2776 addr += 2;
2777 if (version != 2)
2778 {
2779 warning (_("Section .debug_aranges in %s entry at offset %s "
2780 "has unsupported version %d, ignoring .debug_aranges."),
2781 objfile_name (objfile),
2782 plongest (entry_addr - section->buffer), version);
2783 return;
2784 }
2785
2786 const uint64_t debug_info_offset
2787 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2788 addr += offset_size;
2789 const auto per_cu_it
2790 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2791 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2792 {
2793 warning (_("Section .debug_aranges in %s entry at offset %s "
2794 "debug_info_offset %s does not exists, "
2795 "ignoring .debug_aranges."),
2796 objfile_name (objfile),
2797 plongest (entry_addr - section->buffer),
2798 pulongest (debug_info_offset));
2799 return;
2800 }
2801 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2802
2803 const uint8_t address_size = *addr++;
2804 if (address_size < 1 || address_size > 8)
2805 {
2806 warning (_("Section .debug_aranges in %s entry at offset %s "
2807 "address_size %u is invalid, ignoring .debug_aranges."),
2808 objfile_name (objfile),
2809 plongest (entry_addr - section->buffer), address_size);
2810 return;
2811 }
2812
2813 const uint8_t segment_selector_size = *addr++;
2814 if (segment_selector_size != 0)
2815 {
2816 warning (_("Section .debug_aranges in %s entry at offset %s "
2817 "segment_selector_size %u is not supported, "
2818 "ignoring .debug_aranges."),
2819 objfile_name (objfile),
2820 plongest (entry_addr - section->buffer),
2821 segment_selector_size);
2822 return;
2823 }
2824
2825 /* Must pad to an alignment boundary that is twice the address
2826 size. It is undocumented by the DWARF standard but GCC does
2827 use it. */
2828 for (size_t padding = ((-(addr - section->buffer))
2829 & (2 * address_size - 1));
2830 padding > 0; padding--)
2831 if (*addr++ != 0)
2832 {
2833 warning (_("Section .debug_aranges in %s entry at offset %s "
2834 "padding is not zero, ignoring .debug_aranges."),
2835 objfile_name (objfile),
2836 plongest (entry_addr - section->buffer));
2837 return;
2838 }
2839
2840 for (;;)
2841 {
2842 if (addr + 2 * address_size > entry_end)
2843 {
2844 warning (_("Section .debug_aranges in %s entry at offset %s "
2845 "address list is not properly terminated, "
2846 "ignoring .debug_aranges."),
2847 objfile_name (objfile),
2848 plongest (entry_addr - section->buffer));
2849 return;
2850 }
2851 ULONGEST start = extract_unsigned_integer (addr, address_size,
2852 dwarf5_byte_order);
2853 addr += address_size;
2854 ULONGEST length = extract_unsigned_integer (addr, address_size,
2855 dwarf5_byte_order);
2856 addr += address_size;
2857 if (start == 0 && length == 0)
2858 break;
2859 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
2860 {
2861 /* Symbol was eliminated due to a COMDAT group. */
2862 continue;
2863 }
2864 ULONGEST end = start + length;
2865 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2866 - baseaddr);
2867 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2868 - baseaddr);
2869 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2870 }
2871 }
2872
2873 objfile->partial_symtabs->psymtabs_addrmap
2874 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2875 }
2876
2877 /* Find a slot in the mapped index INDEX for the object named NAME.
2878 If NAME is found, set *VEC_OUT to point to the CU vector in the
2879 constant pool and return true. If NAME cannot be found, return
2880 false. */
2881
2882 static bool
2883 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2884 offset_type **vec_out)
2885 {
2886 offset_type hash;
2887 offset_type slot, step;
2888 int (*cmp) (const char *, const char *);
2889
2890 gdb::unique_xmalloc_ptr<char> without_params;
2891 if (current_language->la_language == language_cplus
2892 || current_language->la_language == language_fortran
2893 || current_language->la_language == language_d)
2894 {
2895 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2896 not contain any. */
2897
2898 if (strchr (name, '(') != NULL)
2899 {
2900 without_params = cp_remove_params (name);
2901
2902 if (without_params != NULL)
2903 name = without_params.get ();
2904 }
2905 }
2906
2907 /* Index version 4 did not support case insensitive searches. But the
2908 indices for case insensitive languages are built in lowercase, therefore
2909 simulate our NAME being searched is also lowercased. */
2910 hash = mapped_index_string_hash ((index->version == 4
2911 && case_sensitivity == case_sensitive_off
2912 ? 5 : index->version),
2913 name);
2914
2915 slot = hash & (index->symbol_table.size () - 1);
2916 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2917 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2918
2919 for (;;)
2920 {
2921 const char *str;
2922
2923 const auto &bucket = index->symbol_table[slot];
2924 if (bucket.name == 0 && bucket.vec == 0)
2925 return false;
2926
2927 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2928 if (!cmp (name, str))
2929 {
2930 *vec_out = (offset_type *) (index->constant_pool
2931 + MAYBE_SWAP (bucket.vec));
2932 return true;
2933 }
2934
2935 slot = (slot + step) & (index->symbol_table.size () - 1);
2936 }
2937 }
2938
2939 /* A helper function that reads the .gdb_index from BUFFER and fills
2940 in MAP. FILENAME is the name of the file containing the data;
2941 it is used for error reporting. DEPRECATED_OK is true if it is
2942 ok to use deprecated sections.
2943
2944 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2945 out parameters that are filled in with information about the CU and
2946 TU lists in the section.
2947
2948 Returns true if all went well, false otherwise. */
2949
2950 static bool
2951 read_gdb_index_from_buffer (const char *filename,
2952 bool deprecated_ok,
2953 gdb::array_view<const gdb_byte> buffer,
2954 struct mapped_index *map,
2955 const gdb_byte **cu_list,
2956 offset_type *cu_list_elements,
2957 const gdb_byte **types_list,
2958 offset_type *types_list_elements)
2959 {
2960 const gdb_byte *addr = &buffer[0];
2961
2962 /* Version check. */
2963 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2964 /* Versions earlier than 3 emitted every copy of a psymbol. This
2965 causes the index to behave very poorly for certain requests. Version 3
2966 contained incomplete addrmap. So, it seems better to just ignore such
2967 indices. */
2968 if (version < 4)
2969 {
2970 static int warning_printed = 0;
2971 if (!warning_printed)
2972 {
2973 warning (_("Skipping obsolete .gdb_index section in %s."),
2974 filename);
2975 warning_printed = 1;
2976 }
2977 return 0;
2978 }
2979 /* Index version 4 uses a different hash function than index version
2980 5 and later.
2981
2982 Versions earlier than 6 did not emit psymbols for inlined
2983 functions. Using these files will cause GDB not to be able to
2984 set breakpoints on inlined functions by name, so we ignore these
2985 indices unless the user has done
2986 "set use-deprecated-index-sections on". */
2987 if (version < 6 && !deprecated_ok)
2988 {
2989 static int warning_printed = 0;
2990 if (!warning_printed)
2991 {
2992 warning (_("\
2993 Skipping deprecated .gdb_index section in %s.\n\
2994 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2995 to use the section anyway."),
2996 filename);
2997 warning_printed = 1;
2998 }
2999 return 0;
3000 }
3001 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3002 of the TU (for symbols coming from TUs),
3003 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3004 Plus gold-generated indices can have duplicate entries for global symbols,
3005 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3006 These are just performance bugs, and we can't distinguish gdb-generated
3007 indices from gold-generated ones, so issue no warning here. */
3008
3009 /* Indexes with higher version than the one supported by GDB may be no
3010 longer backward compatible. */
3011 if (version > 8)
3012 return 0;
3013
3014 map->version = version;
3015
3016 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3017
3018 int i = 0;
3019 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3020 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3021 / 8);
3022 ++i;
3023
3024 *types_list = addr + MAYBE_SWAP (metadata[i]);
3025 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3026 - MAYBE_SWAP (metadata[i]))
3027 / 8);
3028 ++i;
3029
3030 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3031 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3032 map->address_table
3033 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3034 ++i;
3035
3036 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3037 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3038 map->symbol_table
3039 = gdb::array_view<mapped_index::symbol_table_slot>
3040 ((mapped_index::symbol_table_slot *) symbol_table,
3041 (mapped_index::symbol_table_slot *) symbol_table_end);
3042
3043 ++i;
3044 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3045
3046 return 1;
3047 }
3048
3049 /* Callback types for dwarf2_read_gdb_index. */
3050
3051 typedef gdb::function_view
3052 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3053 get_gdb_index_contents_ftype;
3054 typedef gdb::function_view
3055 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3056 get_gdb_index_contents_dwz_ftype;
3057
3058 /* Read .gdb_index. If everything went ok, initialize the "quick"
3059 elements of all the CUs and return 1. Otherwise, return 0. */
3060
3061 static int
3062 dwarf2_read_gdb_index
3063 (dwarf2_per_objfile *per_objfile,
3064 get_gdb_index_contents_ftype get_gdb_index_contents,
3065 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3066 {
3067 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3068 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3069 struct dwz_file *dwz;
3070 struct objfile *objfile = per_objfile->objfile;
3071 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3072
3073 gdb::array_view<const gdb_byte> main_index_contents
3074 = get_gdb_index_contents (objfile, per_bfd);
3075
3076 if (main_index_contents.empty ())
3077 return 0;
3078
3079 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3080 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3081 use_deprecated_index_sections,
3082 main_index_contents, map.get (), &cu_list,
3083 &cu_list_elements, &types_list,
3084 &types_list_elements))
3085 return 0;
3086
3087 /* Don't use the index if it's empty. */
3088 if (map->symbol_table.empty ())
3089 return 0;
3090
3091 /* If there is a .dwz file, read it so we can get its CU list as
3092 well. */
3093 dwz = dwarf2_get_dwz_file (per_bfd);
3094 if (dwz != NULL)
3095 {
3096 struct mapped_index dwz_map;
3097 const gdb_byte *dwz_types_ignore;
3098 offset_type dwz_types_elements_ignore;
3099
3100 gdb::array_view<const gdb_byte> dwz_index_content
3101 = get_gdb_index_contents_dwz (objfile, dwz);
3102
3103 if (dwz_index_content.empty ())
3104 return 0;
3105
3106 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3107 1, dwz_index_content, &dwz_map,
3108 &dwz_list, &dwz_list_elements,
3109 &dwz_types_ignore,
3110 &dwz_types_elements_ignore))
3111 {
3112 warning (_("could not read '.gdb_index' section from %s; skipping"),
3113 bfd_get_filename (dwz->dwz_bfd.get ()));
3114 return 0;
3115 }
3116 }
3117
3118 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3119 dwz_list_elements);
3120
3121 if (types_list_elements)
3122 {
3123 /* We can only handle a single .debug_types when we have an
3124 index. */
3125 if (per_bfd->types.size () != 1)
3126 return 0;
3127
3128 dwarf2_section_info *section = &per_bfd->types[0];
3129
3130 create_signatured_type_table_from_index (per_bfd, section, types_list,
3131 types_list_elements);
3132 }
3133
3134 create_addrmap_from_index (per_objfile, map.get ());
3135
3136 per_bfd->index_table = std::move (map);
3137 per_bfd->using_index = 1;
3138 per_bfd->quick_file_names_table =
3139 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3140
3141 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3142 objfiles using the same BFD. */
3143 gdb_assert (per_bfd->partial_symtabs == nullptr);
3144 per_bfd->partial_symtabs = objfile->partial_symtabs;
3145
3146 return 1;
3147 }
3148
3149 /* die_reader_func for dw2_get_file_names. */
3150
3151 static void
3152 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3153 const gdb_byte *info_ptr,
3154 struct die_info *comp_unit_die)
3155 {
3156 struct dwarf2_cu *cu = reader->cu;
3157 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3158 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3159 struct dwarf2_per_cu_data *lh_cu;
3160 struct attribute *attr;
3161 void **slot;
3162 struct quick_file_names *qfn;
3163
3164 gdb_assert (! this_cu->is_debug_types);
3165
3166 /* Our callers never want to match partial units -- instead they
3167 will match the enclosing full CU. */
3168 if (comp_unit_die->tag == DW_TAG_partial_unit)
3169 {
3170 this_cu->v.quick->no_file_data = 1;
3171 return;
3172 }
3173
3174 lh_cu = this_cu;
3175 slot = NULL;
3176
3177 line_header_up lh;
3178 sect_offset line_offset {};
3179
3180 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3181 if (attr != nullptr)
3182 {
3183 struct quick_file_names find_entry;
3184
3185 line_offset = (sect_offset) DW_UNSND (attr);
3186
3187 /* We may have already read in this line header (TU line header sharing).
3188 If we have we're done. */
3189 find_entry.hash.dwo_unit = cu->dwo_unit;
3190 find_entry.hash.line_sect_off = line_offset;
3191 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3192 &find_entry, INSERT);
3193 if (*slot != NULL)
3194 {
3195 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3196 return;
3197 }
3198
3199 lh = dwarf_decode_line_header (line_offset, cu);
3200 }
3201 if (lh == NULL)
3202 {
3203 lh_cu->v.quick->no_file_data = 1;
3204 return;
3205 }
3206
3207 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3208 qfn->hash.dwo_unit = cu->dwo_unit;
3209 qfn->hash.line_sect_off = line_offset;
3210 gdb_assert (slot != NULL);
3211 *slot = qfn;
3212
3213 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3214
3215 int offset = 0;
3216 if (strcmp (fnd.name, "<unknown>") != 0)
3217 ++offset;
3218
3219 qfn->num_file_names = offset + lh->file_names_size ();
3220 qfn->file_names =
3221 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3222 qfn->num_file_names);
3223 if (offset != 0)
3224 qfn->file_names[0] = xstrdup (fnd.name);
3225 for (int i = 0; i < lh->file_names_size (); ++i)
3226 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3227 fnd.comp_dir).release ();
3228 qfn->real_names = NULL;
3229
3230 lh_cu->v.quick->file_names = qfn;
3231 }
3232
3233 /* A helper for the "quick" functions which attempts to read the line
3234 table for THIS_CU. */
3235
3236 static struct quick_file_names *
3237 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3238 dwarf2_per_objfile *per_objfile)
3239 {
3240 /* This should never be called for TUs. */
3241 gdb_assert (! this_cu->is_debug_types);
3242 /* Nor type unit groups. */
3243 gdb_assert (! this_cu->type_unit_group_p ());
3244
3245 if (this_cu->v.quick->file_names != NULL)
3246 return this_cu->v.quick->file_names;
3247 /* If we know there is no line data, no point in looking again. */
3248 if (this_cu->v.quick->no_file_data)
3249 return NULL;
3250
3251 cutu_reader reader (this_cu, per_objfile);
3252 if (!reader.dummy_p)
3253 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3254
3255 if (this_cu->v.quick->no_file_data)
3256 return NULL;
3257 return this_cu->v.quick->file_names;
3258 }
3259
3260 /* A helper for the "quick" functions which computes and caches the
3261 real path for a given file name from the line table. */
3262
3263 static const char *
3264 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3265 struct quick_file_names *qfn, int index)
3266 {
3267 if (qfn->real_names == NULL)
3268 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3269 qfn->num_file_names, const char *);
3270
3271 if (qfn->real_names[index] == NULL)
3272 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3273
3274 return qfn->real_names[index];
3275 }
3276
3277 static struct symtab *
3278 dw2_find_last_source_symtab (struct objfile *objfile)
3279 {
3280 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3281 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3282 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3283
3284 if (cust == NULL)
3285 return NULL;
3286
3287 return compunit_primary_filetab (cust);
3288 }
3289
3290 /* Traversal function for dw2_forget_cached_source_info. */
3291
3292 static int
3293 dw2_free_cached_file_names (void **slot, void *info)
3294 {
3295 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3296
3297 if (file_data->real_names)
3298 {
3299 int i;
3300
3301 for (i = 0; i < file_data->num_file_names; ++i)
3302 {
3303 xfree ((void*) file_data->real_names[i]);
3304 file_data->real_names[i] = NULL;
3305 }
3306 }
3307
3308 return 1;
3309 }
3310
3311 static void
3312 dw2_forget_cached_source_info (struct objfile *objfile)
3313 {
3314 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3315
3316 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3317 dw2_free_cached_file_names, NULL);
3318 }
3319
3320 /* Helper function for dw2_map_symtabs_matching_filename that expands
3321 the symtabs and calls the iterator. */
3322
3323 static int
3324 dw2_map_expand_apply (struct objfile *objfile,
3325 struct dwarf2_per_cu_data *per_cu,
3326 const char *name, const char *real_path,
3327 gdb::function_view<bool (symtab *)> callback)
3328 {
3329 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3330
3331 /* Don't visit already-expanded CUs. */
3332 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3333 if (per_objfile->symtab_set_p (per_cu))
3334 return 0;
3335
3336 /* This may expand more than one symtab, and we want to iterate over
3337 all of them. */
3338 dw2_instantiate_symtab (per_cu, per_objfile, false);
3339
3340 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3341 last_made, callback);
3342 }
3343
3344 /* Implementation of the map_symtabs_matching_filename method. */
3345
3346 static bool
3347 dw2_map_symtabs_matching_filename
3348 (struct objfile *objfile, const char *name, const char *real_path,
3349 gdb::function_view<bool (symtab *)> callback)
3350 {
3351 const char *name_basename = lbasename (name);
3352 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3353
3354 /* The rule is CUs specify all the files, including those used by
3355 any TU, so there's no need to scan TUs here. */
3356
3357 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3358 {
3359 /* We only need to look at symtabs not already expanded. */
3360 if (per_objfile->symtab_set_p (per_cu))
3361 continue;
3362
3363 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3364 if (file_data == NULL)
3365 continue;
3366
3367 for (int j = 0; j < file_data->num_file_names; ++j)
3368 {
3369 const char *this_name = file_data->file_names[j];
3370 const char *this_real_name;
3371
3372 if (compare_filenames_for_search (this_name, name))
3373 {
3374 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3375 callback))
3376 return true;
3377 continue;
3378 }
3379
3380 /* Before we invoke realpath, which can get expensive when many
3381 files are involved, do a quick comparison of the basenames. */
3382 if (! basenames_may_differ
3383 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3384 continue;
3385
3386 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3387 if (compare_filenames_for_search (this_real_name, name))
3388 {
3389 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3390 callback))
3391 return true;
3392 continue;
3393 }
3394
3395 if (real_path != NULL)
3396 {
3397 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3398 gdb_assert (IS_ABSOLUTE_PATH (name));
3399 if (this_real_name != NULL
3400 && FILENAME_CMP (real_path, this_real_name) == 0)
3401 {
3402 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3403 callback))
3404 return true;
3405 continue;
3406 }
3407 }
3408 }
3409 }
3410
3411 return false;
3412 }
3413
3414 /* Struct used to manage iterating over all CUs looking for a symbol. */
3415
3416 struct dw2_symtab_iterator
3417 {
3418 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3419 dwarf2_per_objfile *per_objfile;
3420 /* If set, only look for symbols that match that block. Valid values are
3421 GLOBAL_BLOCK and STATIC_BLOCK. */
3422 gdb::optional<block_enum> block_index;
3423 /* The kind of symbol we're looking for. */
3424 domain_enum domain;
3425 /* The list of CUs from the index entry of the symbol,
3426 or NULL if not found. */
3427 offset_type *vec;
3428 /* The next element in VEC to look at. */
3429 int next;
3430 /* The number of elements in VEC, or zero if there is no match. */
3431 int length;
3432 /* Have we seen a global version of the symbol?
3433 If so we can ignore all further global instances.
3434 This is to work around gold/15646, inefficient gold-generated
3435 indices. */
3436 int global_seen;
3437 };
3438
3439 /* Initialize the index symtab iterator ITER, common part. */
3440
3441 static void
3442 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3443 dwarf2_per_objfile *per_objfile,
3444 gdb::optional<block_enum> block_index,
3445 domain_enum domain)
3446 {
3447 iter->per_objfile = per_objfile;
3448 iter->block_index = block_index;
3449 iter->domain = domain;
3450 iter->next = 0;
3451 iter->global_seen = 0;
3452 iter->vec = NULL;
3453 iter->length = 0;
3454 }
3455
3456 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3457
3458 static void
3459 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3460 dwarf2_per_objfile *per_objfile,
3461 gdb::optional<block_enum> block_index,
3462 domain_enum domain,
3463 const char *name)
3464 {
3465 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3466
3467 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3468 /* index is NULL if OBJF_READNOW. */
3469 if (index == NULL)
3470 return;
3471
3472 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3473 iter->length = MAYBE_SWAP (*iter->vec);
3474 }
3475
3476 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3477
3478 static void
3479 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3480 dwarf2_per_objfile *per_objfile,
3481 gdb::optional<block_enum> block_index,
3482 domain_enum domain, offset_type namei)
3483 {
3484 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3485
3486 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3487 /* index is NULL if OBJF_READNOW. */
3488 if (index == NULL)
3489 return;
3490
3491 gdb_assert (!index->symbol_name_slot_invalid (namei));
3492 const auto &bucket = index->symbol_table[namei];
3493
3494 iter->vec = (offset_type *) (index->constant_pool
3495 + MAYBE_SWAP (bucket.vec));
3496 iter->length = MAYBE_SWAP (*iter->vec);
3497 }
3498
3499 /* Return the next matching CU or NULL if there are no more. */
3500
3501 static struct dwarf2_per_cu_data *
3502 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3503 {
3504 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3505
3506 for ( ; iter->next < iter->length; ++iter->next)
3507 {
3508 offset_type cu_index_and_attrs =
3509 MAYBE_SWAP (iter->vec[iter->next + 1]);
3510 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3511 gdb_index_symbol_kind symbol_kind =
3512 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3513 /* Only check the symbol attributes if they're present.
3514 Indices prior to version 7 don't record them,
3515 and indices >= 7 may elide them for certain symbols
3516 (gold does this). */
3517 int attrs_valid =
3518 (per_objfile->per_bfd->index_table->version >= 7
3519 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3520
3521 /* Don't crash on bad data. */
3522 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3523 + per_objfile->per_bfd->all_type_units.size ()))
3524 {
3525 complaint (_(".gdb_index entry has bad CU index"
3526 " [in module %s]"), objfile_name (per_objfile->objfile));
3527 continue;
3528 }
3529
3530 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3531
3532 /* Skip if already read in. */
3533 if (per_objfile->symtab_set_p (per_cu))
3534 continue;
3535
3536 /* Check static vs global. */
3537 if (attrs_valid)
3538 {
3539 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3540
3541 if (iter->block_index.has_value ())
3542 {
3543 bool want_static = *iter->block_index == STATIC_BLOCK;
3544
3545 if (is_static != want_static)
3546 continue;
3547 }
3548
3549 /* Work around gold/15646. */
3550 if (!is_static
3551 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3552 {
3553 if (iter->global_seen)
3554 continue;
3555
3556 iter->global_seen = 1;
3557 }
3558 }
3559
3560 /* Only check the symbol's kind if it has one. */
3561 if (attrs_valid)
3562 {
3563 switch (iter->domain)
3564 {
3565 case VAR_DOMAIN:
3566 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3567 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3568 /* Some types are also in VAR_DOMAIN. */
3569 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3570 continue;
3571 break;
3572 case STRUCT_DOMAIN:
3573 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3574 continue;
3575 break;
3576 case LABEL_DOMAIN:
3577 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3578 continue;
3579 break;
3580 case MODULE_DOMAIN:
3581 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3582 continue;
3583 break;
3584 default:
3585 break;
3586 }
3587 }
3588
3589 ++iter->next;
3590 return per_cu;
3591 }
3592
3593 return NULL;
3594 }
3595
3596 static struct compunit_symtab *
3597 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3598 const char *name, domain_enum domain)
3599 {
3600 struct compunit_symtab *stab_best = NULL;
3601 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3602
3603 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3604
3605 struct dw2_symtab_iterator iter;
3606 struct dwarf2_per_cu_data *per_cu;
3607
3608 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3609
3610 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3611 {
3612 struct symbol *sym, *with_opaque = NULL;
3613 struct compunit_symtab *stab
3614 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3615 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3616 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3617
3618 sym = block_find_symbol (block, name, domain,
3619 block_find_non_opaque_type_preferred,
3620 &with_opaque);
3621
3622 /* Some caution must be observed with overloaded functions
3623 and methods, since the index will not contain any overload
3624 information (but NAME might contain it). */
3625
3626 if (sym != NULL
3627 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3628 return stab;
3629 if (with_opaque != NULL
3630 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3631 stab_best = stab;
3632
3633 /* Keep looking through other CUs. */
3634 }
3635
3636 return stab_best;
3637 }
3638
3639 static void
3640 dw2_print_stats (struct objfile *objfile)
3641 {
3642 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3643 int total = (per_objfile->per_bfd->all_comp_units.size ()
3644 + per_objfile->per_bfd->all_type_units.size ());
3645 int count = 0;
3646
3647 for (int i = 0; i < total; ++i)
3648 {
3649 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3650
3651 if (!per_objfile->symtab_set_p (per_cu))
3652 ++count;
3653 }
3654 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3655 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3656 }
3657
3658 /* This dumps minimal information about the index.
3659 It is called via "mt print objfiles".
3660 One use is to verify .gdb_index has been loaded by the
3661 gdb.dwarf2/gdb-index.exp testcase. */
3662
3663 static void
3664 dw2_dump (struct objfile *objfile)
3665 {
3666 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3667
3668 gdb_assert (per_objfile->per_bfd->using_index);
3669 printf_filtered (".gdb_index:");
3670 if (per_objfile->per_bfd->index_table != NULL)
3671 {
3672 printf_filtered (" version %d\n",
3673 per_objfile->per_bfd->index_table->version);
3674 }
3675 else
3676 printf_filtered (" faked for \"readnow\"\n");
3677 printf_filtered ("\n");
3678 }
3679
3680 static void
3681 dw2_expand_symtabs_for_function (struct objfile *objfile,
3682 const char *func_name)
3683 {
3684 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3685
3686 struct dw2_symtab_iterator iter;
3687 struct dwarf2_per_cu_data *per_cu;
3688
3689 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3690
3691 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3692 dw2_instantiate_symtab (per_cu, per_objfile, false);
3693
3694 }
3695
3696 static void
3697 dw2_expand_all_symtabs (struct objfile *objfile)
3698 {
3699 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3700 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3701 + per_objfile->per_bfd->all_type_units.size ());
3702
3703 for (int i = 0; i < total_units; ++i)
3704 {
3705 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3706
3707 /* We don't want to directly expand a partial CU, because if we
3708 read it with the wrong language, then assertion failures can
3709 be triggered later on. See PR symtab/23010. So, tell
3710 dw2_instantiate_symtab to skip partial CUs -- any important
3711 partial CU will be read via DW_TAG_imported_unit anyway. */
3712 dw2_instantiate_symtab (per_cu, per_objfile, true);
3713 }
3714 }
3715
3716 static void
3717 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3718 const char *fullname)
3719 {
3720 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3721
3722 /* We don't need to consider type units here.
3723 This is only called for examining code, e.g. expand_line_sal.
3724 There can be an order of magnitude (or more) more type units
3725 than comp units, and we avoid them if we can. */
3726
3727 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3728 {
3729 /* We only need to look at symtabs not already expanded. */
3730 if (per_objfile->symtab_set_p (per_cu))
3731 continue;
3732
3733 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3734 if (file_data == NULL)
3735 continue;
3736
3737 for (int j = 0; j < file_data->num_file_names; ++j)
3738 {
3739 const char *this_fullname = file_data->file_names[j];
3740
3741 if (filename_cmp (this_fullname, fullname) == 0)
3742 {
3743 dw2_instantiate_symtab (per_cu, per_objfile, false);
3744 break;
3745 }
3746 }
3747 }
3748 }
3749
3750 static void
3751 dw2_expand_symtabs_matching_symbol
3752 (mapped_index_base &index,
3753 const lookup_name_info &lookup_name_in,
3754 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3755 enum search_domain kind,
3756 gdb::function_view<bool (offset_type)> match_callback,
3757 dwarf2_per_objfile *per_objfile);
3758
3759 static void
3760 dw2_expand_symtabs_matching_one
3761 (dwarf2_per_cu_data *per_cu,
3762 dwarf2_per_objfile *per_objfile,
3763 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3764 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3765
3766 static void
3767 dw2_map_matching_symbols
3768 (struct objfile *objfile,
3769 const lookup_name_info &name, domain_enum domain,
3770 int global,
3771 gdb::function_view<symbol_found_callback_ftype> callback,
3772 symbol_compare_ftype *ordered_compare)
3773 {
3774 /* Used for Ada. */
3775 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3776
3777 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3778
3779 if (per_objfile->per_bfd->index_table != nullptr)
3780 {
3781 mapped_index &index = *per_objfile->per_bfd->index_table;
3782
3783 const char *match_name = name.ada ().lookup_name ().c_str ();
3784 auto matcher = [&] (const char *symname)
3785 {
3786 if (ordered_compare == nullptr)
3787 return true;
3788 return ordered_compare (symname, match_name) == 0;
3789 };
3790
3791 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3792 [&] (offset_type namei)
3793 {
3794 struct dw2_symtab_iterator iter;
3795 struct dwarf2_per_cu_data *per_cu;
3796
3797 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3798 namei);
3799 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3800 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3801 nullptr);
3802 return true;
3803 }, per_objfile);
3804 }
3805 else
3806 {
3807 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3808 proceed assuming all symtabs have been read in. */
3809 }
3810
3811 for (compunit_symtab *cust : objfile->compunits ())
3812 {
3813 const struct block *block;
3814
3815 if (cust == NULL)
3816 continue;
3817 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3818 if (!iterate_over_symbols_terminated (block, name,
3819 domain, callback))
3820 return;
3821 }
3822 }
3823
3824 /* Starting from a search name, return the string that finds the upper
3825 bound of all strings that start with SEARCH_NAME in a sorted name
3826 list. Returns the empty string to indicate that the upper bound is
3827 the end of the list. */
3828
3829 static std::string
3830 make_sort_after_prefix_name (const char *search_name)
3831 {
3832 /* When looking to complete "func", we find the upper bound of all
3833 symbols that start with "func" by looking for where we'd insert
3834 the closest string that would follow "func" in lexicographical
3835 order. Usually, that's "func"-with-last-character-incremented,
3836 i.e. "fund". Mind non-ASCII characters, though. Usually those
3837 will be UTF-8 multi-byte sequences, but we can't be certain.
3838 Especially mind the 0xff character, which is a valid character in
3839 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3840 rule out compilers allowing it in identifiers. Note that
3841 conveniently, strcmp/strcasecmp are specified to compare
3842 characters interpreted as unsigned char. So what we do is treat
3843 the whole string as a base 256 number composed of a sequence of
3844 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3845 to 0, and carries 1 to the following more-significant position.
3846 If the very first character in SEARCH_NAME ends up incremented
3847 and carries/overflows, then the upper bound is the end of the
3848 list. The string after the empty string is also the empty
3849 string.
3850
3851 Some examples of this operation:
3852
3853 SEARCH_NAME => "+1" RESULT
3854
3855 "abc" => "abd"
3856 "ab\xff" => "ac"
3857 "\xff" "a" "\xff" => "\xff" "b"
3858 "\xff" => ""
3859 "\xff\xff" => ""
3860 "" => ""
3861
3862 Then, with these symbols for example:
3863
3864 func
3865 func1
3866 fund
3867
3868 completing "func" looks for symbols between "func" and
3869 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3870 which finds "func" and "func1", but not "fund".
3871
3872 And with:
3873
3874 funcÿ (Latin1 'ÿ' [0xff])
3875 funcÿ1
3876 fund
3877
3878 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3879 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3880
3881 And with:
3882
3883 ÿÿ (Latin1 'ÿ' [0xff])
3884 ÿÿ1
3885
3886 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3887 the end of the list.
3888 */
3889 std::string after = search_name;
3890 while (!after.empty () && (unsigned char) after.back () == 0xff)
3891 after.pop_back ();
3892 if (!after.empty ())
3893 after.back () = (unsigned char) after.back () + 1;
3894 return after;
3895 }
3896
3897 /* See declaration. */
3898
3899 std::pair<std::vector<name_component>::const_iterator,
3900 std::vector<name_component>::const_iterator>
3901 mapped_index_base::find_name_components_bounds
3902 (const lookup_name_info &lookup_name_without_params, language lang,
3903 dwarf2_per_objfile *per_objfile) const
3904 {
3905 auto *name_cmp
3906 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3907
3908 const char *lang_name
3909 = lookup_name_without_params.language_lookup_name (lang);
3910
3911 /* Comparison function object for lower_bound that matches against a
3912 given symbol name. */
3913 auto lookup_compare_lower = [&] (const name_component &elem,
3914 const char *name)
3915 {
3916 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3917 const char *elem_name = elem_qualified + elem.name_offset;
3918 return name_cmp (elem_name, name) < 0;
3919 };
3920
3921 /* Comparison function object for upper_bound that matches against a
3922 given symbol name. */
3923 auto lookup_compare_upper = [&] (const char *name,
3924 const name_component &elem)
3925 {
3926 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3927 const char *elem_name = elem_qualified + elem.name_offset;
3928 return name_cmp (name, elem_name) < 0;
3929 };
3930
3931 auto begin = this->name_components.begin ();
3932 auto end = this->name_components.end ();
3933
3934 /* Find the lower bound. */
3935 auto lower = [&] ()
3936 {
3937 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3938 return begin;
3939 else
3940 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3941 } ();
3942
3943 /* Find the upper bound. */
3944 auto upper = [&] ()
3945 {
3946 if (lookup_name_without_params.completion_mode ())
3947 {
3948 /* In completion mode, we want UPPER to point past all
3949 symbols names that have the same prefix. I.e., with
3950 these symbols, and completing "func":
3951
3952 function << lower bound
3953 function1
3954 other_function << upper bound
3955
3956 We find the upper bound by looking for the insertion
3957 point of "func"-with-last-character-incremented,
3958 i.e. "fund". */
3959 std::string after = make_sort_after_prefix_name (lang_name);
3960 if (after.empty ())
3961 return end;
3962 return std::lower_bound (lower, end, after.c_str (),
3963 lookup_compare_lower);
3964 }
3965 else
3966 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3967 } ();
3968
3969 return {lower, upper};
3970 }
3971
3972 /* See declaration. */
3973
3974 void
3975 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3976 {
3977 if (!this->name_components.empty ())
3978 return;
3979
3980 this->name_components_casing = case_sensitivity;
3981 auto *name_cmp
3982 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3983
3984 /* The code below only knows how to break apart components of C++
3985 symbol names (and other languages that use '::' as
3986 namespace/module separator) and Ada symbol names. */
3987 auto count = this->symbol_name_count ();
3988 for (offset_type idx = 0; idx < count; idx++)
3989 {
3990 if (this->symbol_name_slot_invalid (idx))
3991 continue;
3992
3993 const char *name = this->symbol_name_at (idx, per_objfile);
3994
3995 /* Add each name component to the name component table. */
3996 unsigned int previous_len = 0;
3997
3998 if (strstr (name, "::") != nullptr)
3999 {
4000 for (unsigned int current_len = cp_find_first_component (name);
4001 name[current_len] != '\0';
4002 current_len += cp_find_first_component (name + current_len))
4003 {
4004 gdb_assert (name[current_len] == ':');
4005 this->name_components.push_back ({previous_len, idx});
4006 /* Skip the '::'. */
4007 current_len += 2;
4008 previous_len = current_len;
4009 }
4010 }
4011 else
4012 {
4013 /* Handle the Ada encoded (aka mangled) form here. */
4014 for (const char *iter = strstr (name, "__");
4015 iter != nullptr;
4016 iter = strstr (iter, "__"))
4017 {
4018 this->name_components.push_back ({previous_len, idx});
4019 iter += 2;
4020 previous_len = iter - name;
4021 }
4022 }
4023
4024 this->name_components.push_back ({previous_len, idx});
4025 }
4026
4027 /* Sort name_components elements by name. */
4028 auto name_comp_compare = [&] (const name_component &left,
4029 const name_component &right)
4030 {
4031 const char *left_qualified
4032 = this->symbol_name_at (left.idx, per_objfile);
4033 const char *right_qualified
4034 = this->symbol_name_at (right.idx, per_objfile);
4035
4036 const char *left_name = left_qualified + left.name_offset;
4037 const char *right_name = right_qualified + right.name_offset;
4038
4039 return name_cmp (left_name, right_name) < 0;
4040 };
4041
4042 std::sort (this->name_components.begin (),
4043 this->name_components.end (),
4044 name_comp_compare);
4045 }
4046
4047 /* Helper for dw2_expand_symtabs_matching that works with a
4048 mapped_index_base instead of the containing objfile. This is split
4049 to a separate function in order to be able to unit test the
4050 name_components matching using a mock mapped_index_base. For each
4051 symbol name that matches, calls MATCH_CALLBACK, passing it the
4052 symbol's index in the mapped_index_base symbol table. */
4053
4054 static void
4055 dw2_expand_symtabs_matching_symbol
4056 (mapped_index_base &index,
4057 const lookup_name_info &lookup_name_in,
4058 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4059 enum search_domain kind,
4060 gdb::function_view<bool (offset_type)> match_callback,
4061 dwarf2_per_objfile *per_objfile)
4062 {
4063 lookup_name_info lookup_name_without_params
4064 = lookup_name_in.make_ignore_params ();
4065
4066 /* Build the symbol name component sorted vector, if we haven't
4067 yet. */
4068 index.build_name_components (per_objfile);
4069
4070 /* The same symbol may appear more than once in the range though.
4071 E.g., if we're looking for symbols that complete "w", and we have
4072 a symbol named "w1::w2", we'll find the two name components for
4073 that same symbol in the range. To be sure we only call the
4074 callback once per symbol, we first collect the symbol name
4075 indexes that matched in a temporary vector and ignore
4076 duplicates. */
4077 std::vector<offset_type> matches;
4078
4079 struct name_and_matcher
4080 {
4081 symbol_name_matcher_ftype *matcher;
4082 const char *name;
4083
4084 bool operator== (const name_and_matcher &other) const
4085 {
4086 return matcher == other.matcher && strcmp (name, other.name) == 0;
4087 }
4088 };
4089
4090 /* A vector holding all the different symbol name matchers, for all
4091 languages. */
4092 std::vector<name_and_matcher> matchers;
4093
4094 for (int i = 0; i < nr_languages; i++)
4095 {
4096 enum language lang_e = (enum language) i;
4097
4098 const language_defn *lang = language_def (lang_e);
4099 symbol_name_matcher_ftype *name_matcher
4100 = lang->get_symbol_name_matcher (lookup_name_without_params);
4101
4102 name_and_matcher key {
4103 name_matcher,
4104 lookup_name_without_params.language_lookup_name (lang_e)
4105 };
4106
4107 /* Don't insert the same comparison routine more than once.
4108 Note that we do this linear walk. This is not a problem in
4109 practice because the number of supported languages is
4110 low. */
4111 if (std::find (matchers.begin (), matchers.end (), key)
4112 != matchers.end ())
4113 continue;
4114 matchers.push_back (std::move (key));
4115
4116 auto bounds
4117 = index.find_name_components_bounds (lookup_name_without_params,
4118 lang_e, per_objfile);
4119
4120 /* Now for each symbol name in range, check to see if we have a name
4121 match, and if so, call the MATCH_CALLBACK callback. */
4122
4123 for (; bounds.first != bounds.second; ++bounds.first)
4124 {
4125 const char *qualified
4126 = index.symbol_name_at (bounds.first->idx, per_objfile);
4127
4128 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4129 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4130 continue;
4131
4132 matches.push_back (bounds.first->idx);
4133 }
4134 }
4135
4136 std::sort (matches.begin (), matches.end ());
4137
4138 /* Finally call the callback, once per match. */
4139 ULONGEST prev = -1;
4140 for (offset_type idx : matches)
4141 {
4142 if (prev != idx)
4143 {
4144 if (!match_callback (idx))
4145 break;
4146 prev = idx;
4147 }
4148 }
4149
4150 /* Above we use a type wider than idx's for 'prev', since 0 and
4151 (offset_type)-1 are both possible values. */
4152 static_assert (sizeof (prev) > sizeof (offset_type), "");
4153 }
4154
4155 #if GDB_SELF_TEST
4156
4157 namespace selftests { namespace dw2_expand_symtabs_matching {
4158
4159 /* A mock .gdb_index/.debug_names-like name index table, enough to
4160 exercise dw2_expand_symtabs_matching_symbol, which works with the
4161 mapped_index_base interface. Builds an index from the symbol list
4162 passed as parameter to the constructor. */
4163 class mock_mapped_index : public mapped_index_base
4164 {
4165 public:
4166 mock_mapped_index (gdb::array_view<const char *> symbols)
4167 : m_symbol_table (symbols)
4168 {}
4169
4170 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4171
4172 /* Return the number of names in the symbol table. */
4173 size_t symbol_name_count () const override
4174 {
4175 return m_symbol_table.size ();
4176 }
4177
4178 /* Get the name of the symbol at IDX in the symbol table. */
4179 const char *symbol_name_at
4180 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4181 {
4182 return m_symbol_table[idx];
4183 }
4184
4185 private:
4186 gdb::array_view<const char *> m_symbol_table;
4187 };
4188
4189 /* Convenience function that converts a NULL pointer to a "<null>"
4190 string, to pass to print routines. */
4191
4192 static const char *
4193 string_or_null (const char *str)
4194 {
4195 return str != NULL ? str : "<null>";
4196 }
4197
4198 /* Check if a lookup_name_info built from
4199 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4200 index. EXPECTED_LIST is the list of expected matches, in expected
4201 matching order. If no match expected, then an empty list is
4202 specified. Returns true on success. On failure prints a warning
4203 indicating the file:line that failed, and returns false. */
4204
4205 static bool
4206 check_match (const char *file, int line,
4207 mock_mapped_index &mock_index,
4208 const char *name, symbol_name_match_type match_type,
4209 bool completion_mode,
4210 std::initializer_list<const char *> expected_list,
4211 dwarf2_per_objfile *per_objfile)
4212 {
4213 lookup_name_info lookup_name (name, match_type, completion_mode);
4214
4215 bool matched = true;
4216
4217 auto mismatch = [&] (const char *expected_str,
4218 const char *got)
4219 {
4220 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4221 "expected=\"%s\", got=\"%s\"\n"),
4222 file, line,
4223 (match_type == symbol_name_match_type::FULL
4224 ? "FULL" : "WILD"),
4225 name, string_or_null (expected_str), string_or_null (got));
4226 matched = false;
4227 };
4228
4229 auto expected_it = expected_list.begin ();
4230 auto expected_end = expected_list.end ();
4231
4232 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4233 NULL, ALL_DOMAIN,
4234 [&] (offset_type idx)
4235 {
4236 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4237 const char *expected_str
4238 = expected_it == expected_end ? NULL : *expected_it++;
4239
4240 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4241 mismatch (expected_str, matched_name);
4242 return true;
4243 }, per_objfile);
4244
4245 const char *expected_str
4246 = expected_it == expected_end ? NULL : *expected_it++;
4247 if (expected_str != NULL)
4248 mismatch (expected_str, NULL);
4249
4250 return matched;
4251 }
4252
4253 /* The symbols added to the mock mapped_index for testing (in
4254 canonical form). */
4255 static const char *test_symbols[] = {
4256 "function",
4257 "std::bar",
4258 "std::zfunction",
4259 "std::zfunction2",
4260 "w1::w2",
4261 "ns::foo<char*>",
4262 "ns::foo<int>",
4263 "ns::foo<long>",
4264 "ns2::tmpl<int>::foo2",
4265 "(anonymous namespace)::A::B::C",
4266
4267 /* These are used to check that the increment-last-char in the
4268 matching algorithm for completion doesn't match "t1_fund" when
4269 completing "t1_func". */
4270 "t1_func",
4271 "t1_func1",
4272 "t1_fund",
4273 "t1_fund1",
4274
4275 /* A UTF-8 name with multi-byte sequences to make sure that
4276 cp-name-parser understands this as a single identifier ("função"
4277 is "function" in PT). */
4278 u8"u8função",
4279
4280 /* \377 (0xff) is Latin1 'ÿ'. */
4281 "yfunc\377",
4282
4283 /* \377 (0xff) is Latin1 'ÿ'. */
4284 "\377",
4285 "\377\377123",
4286
4287 /* A name with all sorts of complications. Starts with "z" to make
4288 it easier for the completion tests below. */
4289 #define Z_SYM_NAME \
4290 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4291 "::tuple<(anonymous namespace)::ui*, " \
4292 "std::default_delete<(anonymous namespace)::ui>, void>"
4293
4294 Z_SYM_NAME
4295 };
4296
4297 /* Returns true if the mapped_index_base::find_name_component_bounds
4298 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4299 in completion mode. */
4300
4301 static bool
4302 check_find_bounds_finds (mapped_index_base &index,
4303 const char *search_name,
4304 gdb::array_view<const char *> expected_syms,
4305 dwarf2_per_objfile *per_objfile)
4306 {
4307 lookup_name_info lookup_name (search_name,
4308 symbol_name_match_type::FULL, true);
4309
4310 auto bounds = index.find_name_components_bounds (lookup_name,
4311 language_cplus,
4312 per_objfile);
4313
4314 size_t distance = std::distance (bounds.first, bounds.second);
4315 if (distance != expected_syms.size ())
4316 return false;
4317
4318 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4319 {
4320 auto nc_elem = bounds.first + exp_elem;
4321 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4322 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4323 return false;
4324 }
4325
4326 return true;
4327 }
4328
4329 /* Test the lower-level mapped_index::find_name_component_bounds
4330 method. */
4331
4332 static void
4333 test_mapped_index_find_name_component_bounds ()
4334 {
4335 mock_mapped_index mock_index (test_symbols);
4336
4337 mock_index.build_name_components (NULL /* per_objfile */);
4338
4339 /* Test the lower-level mapped_index::find_name_component_bounds
4340 method in completion mode. */
4341 {
4342 static const char *expected_syms[] = {
4343 "t1_func",
4344 "t1_func1",
4345 };
4346
4347 SELF_CHECK (check_find_bounds_finds
4348 (mock_index, "t1_func", expected_syms,
4349 NULL /* per_objfile */));
4350 }
4351
4352 /* Check that the increment-last-char in the name matching algorithm
4353 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4354 {
4355 static const char *expected_syms1[] = {
4356 "\377",
4357 "\377\377123",
4358 };
4359 SELF_CHECK (check_find_bounds_finds
4360 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4361
4362 static const char *expected_syms2[] = {
4363 "\377\377123",
4364 };
4365 SELF_CHECK (check_find_bounds_finds
4366 (mock_index, "\377\377", expected_syms2,
4367 NULL /* per_objfile */));
4368 }
4369 }
4370
4371 /* Test dw2_expand_symtabs_matching_symbol. */
4372
4373 static void
4374 test_dw2_expand_symtabs_matching_symbol ()
4375 {
4376 mock_mapped_index mock_index (test_symbols);
4377
4378 /* We let all tests run until the end even if some fails, for debug
4379 convenience. */
4380 bool any_mismatch = false;
4381
4382 /* Create the expected symbols list (an initializer_list). Needed
4383 because lists have commas, and we need to pass them to CHECK,
4384 which is a macro. */
4385 #define EXPECT(...) { __VA_ARGS__ }
4386
4387 /* Wrapper for check_match that passes down the current
4388 __FILE__/__LINE__. */
4389 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4390 any_mismatch |= !check_match (__FILE__, __LINE__, \
4391 mock_index, \
4392 NAME, MATCH_TYPE, COMPLETION_MODE, \
4393 EXPECTED_LIST, NULL)
4394
4395 /* Identity checks. */
4396 for (const char *sym : test_symbols)
4397 {
4398 /* Should be able to match all existing symbols. */
4399 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4400 EXPECT (sym));
4401
4402 /* Should be able to match all existing symbols with
4403 parameters. */
4404 std::string with_params = std::string (sym) + "(int)";
4405 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4406 EXPECT (sym));
4407
4408 /* Should be able to match all existing symbols with
4409 parameters and qualifiers. */
4410 with_params = std::string (sym) + " ( int ) const";
4411 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4412 EXPECT (sym));
4413
4414 /* This should really find sym, but cp-name-parser.y doesn't
4415 know about lvalue/rvalue qualifiers yet. */
4416 with_params = std::string (sym) + " ( int ) &&";
4417 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4418 {});
4419 }
4420
4421 /* Check that the name matching algorithm for completion doesn't get
4422 confused with Latin1 'ÿ' / 0xff. */
4423 {
4424 static const char str[] = "\377";
4425 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4426 EXPECT ("\377", "\377\377123"));
4427 }
4428
4429 /* Check that the increment-last-char in the matching algorithm for
4430 completion doesn't match "t1_fund" when completing "t1_func". */
4431 {
4432 static const char str[] = "t1_func";
4433 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4434 EXPECT ("t1_func", "t1_func1"));
4435 }
4436
4437 /* Check that completion mode works at each prefix of the expected
4438 symbol name. */
4439 {
4440 static const char str[] = "function(int)";
4441 size_t len = strlen (str);
4442 std::string lookup;
4443
4444 for (size_t i = 1; i < len; i++)
4445 {
4446 lookup.assign (str, i);
4447 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4448 EXPECT ("function"));
4449 }
4450 }
4451
4452 /* While "w" is a prefix of both components, the match function
4453 should still only be called once. */
4454 {
4455 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4456 EXPECT ("w1::w2"));
4457 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4458 EXPECT ("w1::w2"));
4459 }
4460
4461 /* Same, with a "complicated" symbol. */
4462 {
4463 static const char str[] = Z_SYM_NAME;
4464 size_t len = strlen (str);
4465 std::string lookup;
4466
4467 for (size_t i = 1; i < len; i++)
4468 {
4469 lookup.assign (str, i);
4470 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4471 EXPECT (Z_SYM_NAME));
4472 }
4473 }
4474
4475 /* In FULL mode, an incomplete symbol doesn't match. */
4476 {
4477 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4478 {});
4479 }
4480
4481 /* A complete symbol with parameters matches any overload, since the
4482 index has no overload info. */
4483 {
4484 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4485 EXPECT ("std::zfunction", "std::zfunction2"));
4486 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4487 EXPECT ("std::zfunction", "std::zfunction2"));
4488 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4489 EXPECT ("std::zfunction", "std::zfunction2"));
4490 }
4491
4492 /* Check that whitespace is ignored appropriately. A symbol with a
4493 template argument list. */
4494 {
4495 static const char expected[] = "ns::foo<int>";
4496 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4497 EXPECT (expected));
4498 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4499 EXPECT (expected));
4500 }
4501
4502 /* Check that whitespace is ignored appropriately. A symbol with a
4503 template argument list that includes a pointer. */
4504 {
4505 static const char expected[] = "ns::foo<char*>";
4506 /* Try both completion and non-completion modes. */
4507 static const bool completion_mode[2] = {false, true};
4508 for (size_t i = 0; i < 2; i++)
4509 {
4510 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4511 completion_mode[i], EXPECT (expected));
4512 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4513 completion_mode[i], EXPECT (expected));
4514
4515 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4516 completion_mode[i], EXPECT (expected));
4517 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4518 completion_mode[i], EXPECT (expected));
4519 }
4520 }
4521
4522 {
4523 /* Check method qualifiers are ignored. */
4524 static const char expected[] = "ns::foo<char*>";
4525 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4526 symbol_name_match_type::FULL, true, EXPECT (expected));
4527 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4528 symbol_name_match_type::FULL, true, EXPECT (expected));
4529 CHECK_MATCH ("foo < char * > ( int ) const",
4530 symbol_name_match_type::WILD, true, EXPECT (expected));
4531 CHECK_MATCH ("foo < char * > ( int ) &&",
4532 symbol_name_match_type::WILD, true, EXPECT (expected));
4533 }
4534
4535 /* Test lookup names that don't match anything. */
4536 {
4537 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4538 {});
4539
4540 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4541 {});
4542 }
4543
4544 /* Some wild matching tests, exercising "(anonymous namespace)",
4545 which should not be confused with a parameter list. */
4546 {
4547 static const char *syms[] = {
4548 "A::B::C",
4549 "B::C",
4550 "C",
4551 "A :: B :: C ( int )",
4552 "B :: C ( int )",
4553 "C ( int )",
4554 };
4555
4556 for (const char *s : syms)
4557 {
4558 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4559 EXPECT ("(anonymous namespace)::A::B::C"));
4560 }
4561 }
4562
4563 {
4564 static const char expected[] = "ns2::tmpl<int>::foo2";
4565 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4566 EXPECT (expected));
4567 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4568 EXPECT (expected));
4569 }
4570
4571 SELF_CHECK (!any_mismatch);
4572
4573 #undef EXPECT
4574 #undef CHECK_MATCH
4575 }
4576
4577 static void
4578 run_test ()
4579 {
4580 test_mapped_index_find_name_component_bounds ();
4581 test_dw2_expand_symtabs_matching_symbol ();
4582 }
4583
4584 }} // namespace selftests::dw2_expand_symtabs_matching
4585
4586 #endif /* GDB_SELF_TEST */
4587
4588 /* If FILE_MATCHER is NULL or if PER_CU has
4589 dwarf2_per_cu_quick_data::MARK set (see
4590 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4591 EXPANSION_NOTIFY on it. */
4592
4593 static void
4594 dw2_expand_symtabs_matching_one
4595 (dwarf2_per_cu_data *per_cu,
4596 dwarf2_per_objfile *per_objfile,
4597 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4598 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4599 {
4600 if (file_matcher == NULL || per_cu->v.quick->mark)
4601 {
4602 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4603
4604 compunit_symtab *symtab
4605 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4606 gdb_assert (symtab != nullptr);
4607
4608 if (expansion_notify != NULL && symtab_was_null)
4609 expansion_notify (symtab);
4610 }
4611 }
4612
4613 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4614 matched, to expand corresponding CUs that were marked. IDX is the
4615 index of the symbol name that matched. */
4616
4617 static void
4618 dw2_expand_marked_cus
4619 (dwarf2_per_objfile *per_objfile, offset_type idx,
4620 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4621 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4622 search_domain kind)
4623 {
4624 offset_type *vec, vec_len, vec_idx;
4625 bool global_seen = false;
4626 mapped_index &index = *per_objfile->per_bfd->index_table;
4627
4628 vec = (offset_type *) (index.constant_pool
4629 + MAYBE_SWAP (index.symbol_table[idx].vec));
4630 vec_len = MAYBE_SWAP (vec[0]);
4631 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4632 {
4633 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4634 /* This value is only valid for index versions >= 7. */
4635 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4636 gdb_index_symbol_kind symbol_kind =
4637 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4638 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4639 /* Only check the symbol attributes if they're present.
4640 Indices prior to version 7 don't record them,
4641 and indices >= 7 may elide them for certain symbols
4642 (gold does this). */
4643 int attrs_valid =
4644 (index.version >= 7
4645 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4646
4647 /* Work around gold/15646. */
4648 if (attrs_valid
4649 && !is_static
4650 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4651 {
4652 if (global_seen)
4653 continue;
4654
4655 global_seen = true;
4656 }
4657
4658 /* Only check the symbol's kind if it has one. */
4659 if (attrs_valid)
4660 {
4661 switch (kind)
4662 {
4663 case VARIABLES_DOMAIN:
4664 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4665 continue;
4666 break;
4667 case FUNCTIONS_DOMAIN:
4668 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4669 continue;
4670 break;
4671 case TYPES_DOMAIN:
4672 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4673 continue;
4674 break;
4675 case MODULES_DOMAIN:
4676 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4677 continue;
4678 break;
4679 default:
4680 break;
4681 }
4682 }
4683
4684 /* Don't crash on bad data. */
4685 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4686 + per_objfile->per_bfd->all_type_units.size ()))
4687 {
4688 complaint (_(".gdb_index entry has bad CU index"
4689 " [in module %s]"), objfile_name (per_objfile->objfile));
4690 continue;
4691 }
4692
4693 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4694 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4695 expansion_notify);
4696 }
4697 }
4698
4699 /* If FILE_MATCHER is non-NULL, set all the
4700 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4701 that match FILE_MATCHER. */
4702
4703 static void
4704 dw_expand_symtabs_matching_file_matcher
4705 (dwarf2_per_objfile *per_objfile,
4706 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4707 {
4708 if (file_matcher == NULL)
4709 return;
4710
4711 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4712 htab_eq_pointer,
4713 NULL, xcalloc, xfree));
4714 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4715 htab_eq_pointer,
4716 NULL, xcalloc, xfree));
4717
4718 /* The rule is CUs specify all the files, including those used by
4719 any TU, so there's no need to scan TUs here. */
4720
4721 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4722 {
4723 QUIT;
4724
4725 per_cu->v.quick->mark = 0;
4726
4727 /* We only need to look at symtabs not already expanded. */
4728 if (per_objfile->symtab_set_p (per_cu))
4729 continue;
4730
4731 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4732 if (file_data == NULL)
4733 continue;
4734
4735 if (htab_find (visited_not_found.get (), file_data) != NULL)
4736 continue;
4737 else if (htab_find (visited_found.get (), file_data) != NULL)
4738 {
4739 per_cu->v.quick->mark = 1;
4740 continue;
4741 }
4742
4743 for (int j = 0; j < file_data->num_file_names; ++j)
4744 {
4745 const char *this_real_name;
4746
4747 if (file_matcher (file_data->file_names[j], false))
4748 {
4749 per_cu->v.quick->mark = 1;
4750 break;
4751 }
4752
4753 /* Before we invoke realpath, which can get expensive when many
4754 files are involved, do a quick comparison of the basenames. */
4755 if (!basenames_may_differ
4756 && !file_matcher (lbasename (file_data->file_names[j]),
4757 true))
4758 continue;
4759
4760 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4761 if (file_matcher (this_real_name, false))
4762 {
4763 per_cu->v.quick->mark = 1;
4764 break;
4765 }
4766 }
4767
4768 void **slot = htab_find_slot (per_cu->v.quick->mark
4769 ? visited_found.get ()
4770 : visited_not_found.get (),
4771 file_data, INSERT);
4772 *slot = file_data;
4773 }
4774 }
4775
4776 static void
4777 dw2_expand_symtabs_matching
4778 (struct objfile *objfile,
4779 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4780 const lookup_name_info *lookup_name,
4781 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4782 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4783 enum search_domain kind)
4784 {
4785 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4786
4787 /* index_table is NULL if OBJF_READNOW. */
4788 if (!per_objfile->per_bfd->index_table)
4789 return;
4790
4791 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4792
4793 if (symbol_matcher == NULL && lookup_name == NULL)
4794 {
4795 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4796 {
4797 QUIT;
4798
4799 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4800 file_matcher, expansion_notify);
4801 }
4802 return;
4803 }
4804
4805 mapped_index &index = *per_objfile->per_bfd->index_table;
4806
4807 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4808 symbol_matcher,
4809 kind, [&] (offset_type idx)
4810 {
4811 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4812 kind);
4813 return true;
4814 }, per_objfile);
4815 }
4816
4817 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4818 symtab. */
4819
4820 static struct compunit_symtab *
4821 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4822 CORE_ADDR pc)
4823 {
4824 int i;
4825
4826 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4827 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4828 return cust;
4829
4830 if (cust->includes == NULL)
4831 return NULL;
4832
4833 for (i = 0; cust->includes[i]; ++i)
4834 {
4835 struct compunit_symtab *s = cust->includes[i];
4836
4837 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4838 if (s != NULL)
4839 return s;
4840 }
4841
4842 return NULL;
4843 }
4844
4845 static struct compunit_symtab *
4846 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4847 struct bound_minimal_symbol msymbol,
4848 CORE_ADDR pc,
4849 struct obj_section *section,
4850 int warn_if_readin)
4851 {
4852 struct dwarf2_per_cu_data *data;
4853 struct compunit_symtab *result;
4854
4855 if (!objfile->partial_symtabs->psymtabs_addrmap)
4856 return NULL;
4857
4858 CORE_ADDR baseaddr = objfile->text_section_offset ();
4859 data = (struct dwarf2_per_cu_data *) addrmap_find
4860 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4861 if (!data)
4862 return NULL;
4863
4864 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4865 if (warn_if_readin && per_objfile->symtab_set_p (data))
4866 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4867 paddress (objfile->arch (), pc));
4868
4869 result = recursively_find_pc_sect_compunit_symtab
4870 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4871
4872 gdb_assert (result != NULL);
4873 return result;
4874 }
4875
4876 static void
4877 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4878 void *data, int need_fullname)
4879 {
4880 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4881
4882 if (!per_objfile->per_bfd->filenames_cache)
4883 {
4884 per_objfile->per_bfd->filenames_cache.emplace ();
4885
4886 htab_up visited (htab_create_alloc (10,
4887 htab_hash_pointer, htab_eq_pointer,
4888 NULL, xcalloc, xfree));
4889
4890 /* The rule is CUs specify all the files, including those used
4891 by any TU, so there's no need to scan TUs here. We can
4892 ignore file names coming from already-expanded CUs. */
4893
4894 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4895 {
4896 if (per_objfile->symtab_set_p (per_cu))
4897 {
4898 void **slot = htab_find_slot (visited.get (),
4899 per_cu->v.quick->file_names,
4900 INSERT);
4901
4902 *slot = per_cu->v.quick->file_names;
4903 }
4904 }
4905
4906 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4907 {
4908 /* We only need to look at symtabs not already expanded. */
4909 if (per_objfile->symtab_set_p (per_cu))
4910 continue;
4911
4912 quick_file_names *file_data
4913 = dw2_get_file_names (per_cu, per_objfile);
4914 if (file_data == NULL)
4915 continue;
4916
4917 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4918 if (*slot)
4919 {
4920 /* Already visited. */
4921 continue;
4922 }
4923 *slot = file_data;
4924
4925 for (int j = 0; j < file_data->num_file_names; ++j)
4926 {
4927 const char *filename = file_data->file_names[j];
4928 per_objfile->per_bfd->filenames_cache->seen (filename);
4929 }
4930 }
4931 }
4932
4933 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4934 {
4935 gdb::unique_xmalloc_ptr<char> this_real_name;
4936
4937 if (need_fullname)
4938 this_real_name = gdb_realpath (filename);
4939 (*fun) (filename, this_real_name.get (), data);
4940 });
4941 }
4942
4943 static int
4944 dw2_has_symbols (struct objfile *objfile)
4945 {
4946 return 1;
4947 }
4948
4949 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4950 {
4951 dw2_has_symbols,
4952 dw2_find_last_source_symtab,
4953 dw2_forget_cached_source_info,
4954 dw2_map_symtabs_matching_filename,
4955 dw2_lookup_symbol,
4956 NULL,
4957 dw2_print_stats,
4958 dw2_dump,
4959 dw2_expand_symtabs_for_function,
4960 dw2_expand_all_symtabs,
4961 dw2_expand_symtabs_with_fullname,
4962 dw2_map_matching_symbols,
4963 dw2_expand_symtabs_matching,
4964 dw2_find_pc_sect_compunit_symtab,
4965 NULL,
4966 dw2_map_symbol_filenames
4967 };
4968
4969 /* DWARF-5 debug_names reader. */
4970
4971 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4972 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4973
4974 /* A helper function that reads the .debug_names section in SECTION
4975 and fills in MAP. FILENAME is the name of the file containing the
4976 section; it is used for error reporting.
4977
4978 Returns true if all went well, false otherwise. */
4979
4980 static bool
4981 read_debug_names_from_section (struct objfile *objfile,
4982 const char *filename,
4983 struct dwarf2_section_info *section,
4984 mapped_debug_names &map)
4985 {
4986 if (section->empty ())
4987 return false;
4988
4989 /* Older elfutils strip versions could keep the section in the main
4990 executable while splitting it for the separate debug info file. */
4991 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4992 return false;
4993
4994 section->read (objfile);
4995
4996 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4997
4998 const gdb_byte *addr = section->buffer;
4999
5000 bfd *const abfd = section->get_bfd_owner ();
5001
5002 unsigned int bytes_read;
5003 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5004 addr += bytes_read;
5005
5006 map.dwarf5_is_dwarf64 = bytes_read != 4;
5007 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5008 if (bytes_read + length != section->size)
5009 {
5010 /* There may be multiple per-CU indices. */
5011 warning (_("Section .debug_names in %s length %s does not match "
5012 "section length %s, ignoring .debug_names."),
5013 filename, plongest (bytes_read + length),
5014 pulongest (section->size));
5015 return false;
5016 }
5017
5018 /* The version number. */
5019 uint16_t version = read_2_bytes (abfd, addr);
5020 addr += 2;
5021 if (version != 5)
5022 {
5023 warning (_("Section .debug_names in %s has unsupported version %d, "
5024 "ignoring .debug_names."),
5025 filename, version);
5026 return false;
5027 }
5028
5029 /* Padding. */
5030 uint16_t padding = read_2_bytes (abfd, addr);
5031 addr += 2;
5032 if (padding != 0)
5033 {
5034 warning (_("Section .debug_names in %s has unsupported padding %d, "
5035 "ignoring .debug_names."),
5036 filename, padding);
5037 return false;
5038 }
5039
5040 /* comp_unit_count - The number of CUs in the CU list. */
5041 map.cu_count = read_4_bytes (abfd, addr);
5042 addr += 4;
5043
5044 /* local_type_unit_count - The number of TUs in the local TU
5045 list. */
5046 map.tu_count = read_4_bytes (abfd, addr);
5047 addr += 4;
5048
5049 /* foreign_type_unit_count - The number of TUs in the foreign TU
5050 list. */
5051 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5052 addr += 4;
5053 if (foreign_tu_count != 0)
5054 {
5055 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5056 "ignoring .debug_names."),
5057 filename, static_cast<unsigned long> (foreign_tu_count));
5058 return false;
5059 }
5060
5061 /* bucket_count - The number of hash buckets in the hash lookup
5062 table. */
5063 map.bucket_count = read_4_bytes (abfd, addr);
5064 addr += 4;
5065
5066 /* name_count - The number of unique names in the index. */
5067 map.name_count = read_4_bytes (abfd, addr);
5068 addr += 4;
5069
5070 /* abbrev_table_size - The size in bytes of the abbreviations
5071 table. */
5072 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5073 addr += 4;
5074
5075 /* augmentation_string_size - The size in bytes of the augmentation
5076 string. This value is rounded up to a multiple of 4. */
5077 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5078 addr += 4;
5079 map.augmentation_is_gdb = ((augmentation_string_size
5080 == sizeof (dwarf5_augmentation))
5081 && memcmp (addr, dwarf5_augmentation,
5082 sizeof (dwarf5_augmentation)) == 0);
5083 augmentation_string_size += (-augmentation_string_size) & 3;
5084 addr += augmentation_string_size;
5085
5086 /* List of CUs */
5087 map.cu_table_reordered = addr;
5088 addr += map.cu_count * map.offset_size;
5089
5090 /* List of Local TUs */
5091 map.tu_table_reordered = addr;
5092 addr += map.tu_count * map.offset_size;
5093
5094 /* Hash Lookup Table */
5095 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5096 addr += map.bucket_count * 4;
5097 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5098 addr += map.name_count * 4;
5099
5100 /* Name Table */
5101 map.name_table_string_offs_reordered = addr;
5102 addr += map.name_count * map.offset_size;
5103 map.name_table_entry_offs_reordered = addr;
5104 addr += map.name_count * map.offset_size;
5105
5106 const gdb_byte *abbrev_table_start = addr;
5107 for (;;)
5108 {
5109 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5110 addr += bytes_read;
5111 if (index_num == 0)
5112 break;
5113
5114 const auto insertpair
5115 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5116 if (!insertpair.second)
5117 {
5118 warning (_("Section .debug_names in %s has duplicate index %s, "
5119 "ignoring .debug_names."),
5120 filename, pulongest (index_num));
5121 return false;
5122 }
5123 mapped_debug_names::index_val &indexval = insertpair.first->second;
5124 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5125 addr += bytes_read;
5126
5127 for (;;)
5128 {
5129 mapped_debug_names::index_val::attr attr;
5130 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5131 addr += bytes_read;
5132 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5133 addr += bytes_read;
5134 if (attr.form == DW_FORM_implicit_const)
5135 {
5136 attr.implicit_const = read_signed_leb128 (abfd, addr,
5137 &bytes_read);
5138 addr += bytes_read;
5139 }
5140 if (attr.dw_idx == 0 && attr.form == 0)
5141 break;
5142 indexval.attr_vec.push_back (std::move (attr));
5143 }
5144 }
5145 if (addr != abbrev_table_start + abbrev_table_size)
5146 {
5147 warning (_("Section .debug_names in %s has abbreviation_table "
5148 "of size %s vs. written as %u, ignoring .debug_names."),
5149 filename, plongest (addr - abbrev_table_start),
5150 abbrev_table_size);
5151 return false;
5152 }
5153 map.entry_pool = addr;
5154
5155 return true;
5156 }
5157
5158 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5159 list. */
5160
5161 static void
5162 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5163 const mapped_debug_names &map,
5164 dwarf2_section_info &section,
5165 bool is_dwz)
5166 {
5167 if (!map.augmentation_is_gdb)
5168 {
5169 for (uint32_t i = 0; i < map.cu_count; ++i)
5170 {
5171 sect_offset sect_off
5172 = (sect_offset) (extract_unsigned_integer
5173 (map.cu_table_reordered + i * map.offset_size,
5174 map.offset_size,
5175 map.dwarf5_byte_order));
5176 /* We don't know the length of the CU, because the CU list in a
5177 .debug_names index can be incomplete, so we can't use the start of
5178 the next CU as end of this CU. We create the CUs here with length 0,
5179 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5180 dwarf2_per_cu_data *per_cu
5181 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5182 per_bfd->all_comp_units.push_back (per_cu);
5183 }
5184 }
5185
5186 sect_offset sect_off_prev;
5187 for (uint32_t i = 0; i <= map.cu_count; ++i)
5188 {
5189 sect_offset sect_off_next;
5190 if (i < map.cu_count)
5191 {
5192 sect_off_next
5193 = (sect_offset) (extract_unsigned_integer
5194 (map.cu_table_reordered + i * map.offset_size,
5195 map.offset_size,
5196 map.dwarf5_byte_order));
5197 }
5198 else
5199 sect_off_next = (sect_offset) section.size;
5200 if (i >= 1)
5201 {
5202 const ULONGEST length = sect_off_next - sect_off_prev;
5203 dwarf2_per_cu_data *per_cu
5204 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5205 sect_off_prev, length);
5206 per_bfd->all_comp_units.push_back (per_cu);
5207 }
5208 sect_off_prev = sect_off_next;
5209 }
5210 }
5211
5212 /* Read the CU list from the mapped index, and use it to create all
5213 the CU objects for this dwarf2_per_objfile. */
5214
5215 static void
5216 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5217 const mapped_debug_names &map,
5218 const mapped_debug_names &dwz_map)
5219 {
5220 gdb_assert (per_bfd->all_comp_units.empty ());
5221 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5222
5223 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5224 false /* is_dwz */);
5225
5226 if (dwz_map.cu_count == 0)
5227 return;
5228
5229 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5230 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5231 true /* is_dwz */);
5232 }
5233
5234 /* Read .debug_names. If everything went ok, initialize the "quick"
5235 elements of all the CUs and return true. Otherwise, return false. */
5236
5237 static bool
5238 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5239 {
5240 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5241 mapped_debug_names dwz_map;
5242 struct objfile *objfile = per_objfile->objfile;
5243 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5244
5245 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5246 &per_objfile->per_bfd->debug_names, *map))
5247 return false;
5248
5249 /* Don't use the index if it's empty. */
5250 if (map->name_count == 0)
5251 return false;
5252
5253 /* If there is a .dwz file, read it so we can get its CU list as
5254 well. */
5255 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5256 if (dwz != NULL)
5257 {
5258 if (!read_debug_names_from_section (objfile,
5259 bfd_get_filename (dwz->dwz_bfd.get ()),
5260 &dwz->debug_names, dwz_map))
5261 {
5262 warning (_("could not read '.debug_names' section from %s; skipping"),
5263 bfd_get_filename (dwz->dwz_bfd.get ()));
5264 return false;
5265 }
5266 }
5267
5268 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5269
5270 if (map->tu_count != 0)
5271 {
5272 /* We can only handle a single .debug_types when we have an
5273 index. */
5274 if (per_bfd->types.size () != 1)
5275 return false;
5276
5277 dwarf2_section_info *section = &per_bfd->types[0];
5278
5279 create_signatured_type_table_from_debug_names
5280 (per_objfile, *map, section, &per_bfd->abbrev);
5281 }
5282
5283 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5284
5285 per_bfd->debug_names_table = std::move (map);
5286 per_bfd->using_index = 1;
5287 per_bfd->quick_file_names_table =
5288 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5289
5290 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5291 objfiles using the same BFD. */
5292 gdb_assert (per_bfd->partial_symtabs == nullptr);
5293 per_bfd->partial_symtabs = objfile->partial_symtabs;
5294
5295 return true;
5296 }
5297
5298 /* Type used to manage iterating over all CUs looking for a symbol for
5299 .debug_names. */
5300
5301 class dw2_debug_names_iterator
5302 {
5303 public:
5304 dw2_debug_names_iterator (const mapped_debug_names &map,
5305 gdb::optional<block_enum> block_index,
5306 domain_enum domain,
5307 const char *name, dwarf2_per_objfile *per_objfile)
5308 : m_map (map), m_block_index (block_index), m_domain (domain),
5309 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5310 m_per_objfile (per_objfile)
5311 {}
5312
5313 dw2_debug_names_iterator (const mapped_debug_names &map,
5314 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5315 : m_map (map),
5316 m_search (search),
5317 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5318 m_per_objfile (per_objfile)
5319 {}
5320
5321 dw2_debug_names_iterator (const mapped_debug_names &map,
5322 block_enum block_index, domain_enum domain,
5323 uint32_t namei, dwarf2_per_objfile *per_objfile)
5324 : m_map (map), m_block_index (block_index), m_domain (domain),
5325 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5326 m_per_objfile (per_objfile)
5327 {}
5328
5329 /* Return the next matching CU or NULL if there are no more. */
5330 dwarf2_per_cu_data *next ();
5331
5332 private:
5333 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5334 const char *name,
5335 dwarf2_per_objfile *per_objfile);
5336 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5337 uint32_t namei,
5338 dwarf2_per_objfile *per_objfile);
5339
5340 /* The internalized form of .debug_names. */
5341 const mapped_debug_names &m_map;
5342
5343 /* If set, only look for symbols that match that block. Valid values are
5344 GLOBAL_BLOCK and STATIC_BLOCK. */
5345 const gdb::optional<block_enum> m_block_index;
5346
5347 /* The kind of symbol we're looking for. */
5348 const domain_enum m_domain = UNDEF_DOMAIN;
5349 const search_domain m_search = ALL_DOMAIN;
5350
5351 /* The list of CUs from the index entry of the symbol, or NULL if
5352 not found. */
5353 const gdb_byte *m_addr;
5354
5355 dwarf2_per_objfile *m_per_objfile;
5356 };
5357
5358 const char *
5359 mapped_debug_names::namei_to_name
5360 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5361 {
5362 const ULONGEST namei_string_offs
5363 = extract_unsigned_integer ((name_table_string_offs_reordered
5364 + namei * offset_size),
5365 offset_size,
5366 dwarf5_byte_order);
5367 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5368 }
5369
5370 /* Find a slot in .debug_names for the object named NAME. If NAME is
5371 found, return pointer to its pool data. If NAME cannot be found,
5372 return NULL. */
5373
5374 const gdb_byte *
5375 dw2_debug_names_iterator::find_vec_in_debug_names
5376 (const mapped_debug_names &map, const char *name,
5377 dwarf2_per_objfile *per_objfile)
5378 {
5379 int (*cmp) (const char *, const char *);
5380
5381 gdb::unique_xmalloc_ptr<char> without_params;
5382 if (current_language->la_language == language_cplus
5383 || current_language->la_language == language_fortran
5384 || current_language->la_language == language_d)
5385 {
5386 /* NAME is already canonical. Drop any qualifiers as
5387 .debug_names does not contain any. */
5388
5389 if (strchr (name, '(') != NULL)
5390 {
5391 without_params = cp_remove_params (name);
5392 if (without_params != NULL)
5393 name = without_params.get ();
5394 }
5395 }
5396
5397 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5398
5399 const uint32_t full_hash = dwarf5_djb_hash (name);
5400 uint32_t namei
5401 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5402 (map.bucket_table_reordered
5403 + (full_hash % map.bucket_count)), 4,
5404 map.dwarf5_byte_order);
5405 if (namei == 0)
5406 return NULL;
5407 --namei;
5408 if (namei >= map.name_count)
5409 {
5410 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5411 "[in module %s]"),
5412 namei, map.name_count,
5413 objfile_name (per_objfile->objfile));
5414 return NULL;
5415 }
5416
5417 for (;;)
5418 {
5419 const uint32_t namei_full_hash
5420 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5421 (map.hash_table_reordered + namei), 4,
5422 map.dwarf5_byte_order);
5423 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5424 return NULL;
5425
5426 if (full_hash == namei_full_hash)
5427 {
5428 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5429
5430 #if 0 /* An expensive sanity check. */
5431 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5432 {
5433 complaint (_("Wrong .debug_names hash for string at index %u "
5434 "[in module %s]"),
5435 namei, objfile_name (dwarf2_per_objfile->objfile));
5436 return NULL;
5437 }
5438 #endif
5439
5440 if (cmp (namei_string, name) == 0)
5441 {
5442 const ULONGEST namei_entry_offs
5443 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5444 + namei * map.offset_size),
5445 map.offset_size, map.dwarf5_byte_order);
5446 return map.entry_pool + namei_entry_offs;
5447 }
5448 }
5449
5450 ++namei;
5451 if (namei >= map.name_count)
5452 return NULL;
5453 }
5454 }
5455
5456 const gdb_byte *
5457 dw2_debug_names_iterator::find_vec_in_debug_names
5458 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5459 {
5460 if (namei >= map.name_count)
5461 {
5462 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5463 "[in module %s]"),
5464 namei, map.name_count,
5465 objfile_name (per_objfile->objfile));
5466 return NULL;
5467 }
5468
5469 const ULONGEST namei_entry_offs
5470 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5471 + namei * map.offset_size),
5472 map.offset_size, map.dwarf5_byte_order);
5473 return map.entry_pool + namei_entry_offs;
5474 }
5475
5476 /* See dw2_debug_names_iterator. */
5477
5478 dwarf2_per_cu_data *
5479 dw2_debug_names_iterator::next ()
5480 {
5481 if (m_addr == NULL)
5482 return NULL;
5483
5484 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5485 struct objfile *objfile = m_per_objfile->objfile;
5486 bfd *const abfd = objfile->obfd;
5487
5488 again:
5489
5490 unsigned int bytes_read;
5491 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5492 m_addr += bytes_read;
5493 if (abbrev == 0)
5494 return NULL;
5495
5496 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5497 if (indexval_it == m_map.abbrev_map.cend ())
5498 {
5499 complaint (_("Wrong .debug_names undefined abbrev code %s "
5500 "[in module %s]"),
5501 pulongest (abbrev), objfile_name (objfile));
5502 return NULL;
5503 }
5504 const mapped_debug_names::index_val &indexval = indexval_it->second;
5505 enum class symbol_linkage {
5506 unknown,
5507 static_,
5508 extern_,
5509 } symbol_linkage_ = symbol_linkage::unknown;
5510 dwarf2_per_cu_data *per_cu = NULL;
5511 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5512 {
5513 ULONGEST ull;
5514 switch (attr.form)
5515 {
5516 case DW_FORM_implicit_const:
5517 ull = attr.implicit_const;
5518 break;
5519 case DW_FORM_flag_present:
5520 ull = 1;
5521 break;
5522 case DW_FORM_udata:
5523 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5524 m_addr += bytes_read;
5525 break;
5526 case DW_FORM_ref4:
5527 ull = read_4_bytes (abfd, m_addr);
5528 m_addr += 4;
5529 break;
5530 case DW_FORM_ref8:
5531 ull = read_8_bytes (abfd, m_addr);
5532 m_addr += 8;
5533 break;
5534 case DW_FORM_ref_sig8:
5535 ull = read_8_bytes (abfd, m_addr);
5536 m_addr += 8;
5537 break;
5538 default:
5539 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5540 dwarf_form_name (attr.form),
5541 objfile_name (objfile));
5542 return NULL;
5543 }
5544 switch (attr.dw_idx)
5545 {
5546 case DW_IDX_compile_unit:
5547 /* Don't crash on bad data. */
5548 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5549 {
5550 complaint (_(".debug_names entry has bad CU index %s"
5551 " [in module %s]"),
5552 pulongest (ull),
5553 objfile_name (objfile));
5554 continue;
5555 }
5556 per_cu = per_bfd->get_cutu (ull);
5557 break;
5558 case DW_IDX_type_unit:
5559 /* Don't crash on bad data. */
5560 if (ull >= per_bfd->all_type_units.size ())
5561 {
5562 complaint (_(".debug_names entry has bad TU index %s"
5563 " [in module %s]"),
5564 pulongest (ull),
5565 objfile_name (objfile));
5566 continue;
5567 }
5568 per_cu = &per_bfd->get_tu (ull)->per_cu;
5569 break;
5570 case DW_IDX_die_offset:
5571 /* In a per-CU index (as opposed to a per-module index), index
5572 entries without CU attribute implicitly refer to the single CU. */
5573 if (per_cu == NULL)
5574 per_cu = per_bfd->get_cu (0);
5575 break;
5576 case DW_IDX_GNU_internal:
5577 if (!m_map.augmentation_is_gdb)
5578 break;
5579 symbol_linkage_ = symbol_linkage::static_;
5580 break;
5581 case DW_IDX_GNU_external:
5582 if (!m_map.augmentation_is_gdb)
5583 break;
5584 symbol_linkage_ = symbol_linkage::extern_;
5585 break;
5586 }
5587 }
5588
5589 /* Skip if already read in. */
5590 if (m_per_objfile->symtab_set_p (per_cu))
5591 goto again;
5592
5593 /* Check static vs global. */
5594 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5595 {
5596 const bool want_static = *m_block_index == STATIC_BLOCK;
5597 const bool symbol_is_static =
5598 symbol_linkage_ == symbol_linkage::static_;
5599 if (want_static != symbol_is_static)
5600 goto again;
5601 }
5602
5603 /* Match dw2_symtab_iter_next, symbol_kind
5604 and debug_names::psymbol_tag. */
5605 switch (m_domain)
5606 {
5607 case VAR_DOMAIN:
5608 switch (indexval.dwarf_tag)
5609 {
5610 case DW_TAG_variable:
5611 case DW_TAG_subprogram:
5612 /* Some types are also in VAR_DOMAIN. */
5613 case DW_TAG_typedef:
5614 case DW_TAG_structure_type:
5615 break;
5616 default:
5617 goto again;
5618 }
5619 break;
5620 case STRUCT_DOMAIN:
5621 switch (indexval.dwarf_tag)
5622 {
5623 case DW_TAG_typedef:
5624 case DW_TAG_structure_type:
5625 break;
5626 default:
5627 goto again;
5628 }
5629 break;
5630 case LABEL_DOMAIN:
5631 switch (indexval.dwarf_tag)
5632 {
5633 case 0:
5634 case DW_TAG_variable:
5635 break;
5636 default:
5637 goto again;
5638 }
5639 break;
5640 case MODULE_DOMAIN:
5641 switch (indexval.dwarf_tag)
5642 {
5643 case DW_TAG_module:
5644 break;
5645 default:
5646 goto again;
5647 }
5648 break;
5649 default:
5650 break;
5651 }
5652
5653 /* Match dw2_expand_symtabs_matching, symbol_kind and
5654 debug_names::psymbol_tag. */
5655 switch (m_search)
5656 {
5657 case VARIABLES_DOMAIN:
5658 switch (indexval.dwarf_tag)
5659 {
5660 case DW_TAG_variable:
5661 break;
5662 default:
5663 goto again;
5664 }
5665 break;
5666 case FUNCTIONS_DOMAIN:
5667 switch (indexval.dwarf_tag)
5668 {
5669 case DW_TAG_subprogram:
5670 break;
5671 default:
5672 goto again;
5673 }
5674 break;
5675 case TYPES_DOMAIN:
5676 switch (indexval.dwarf_tag)
5677 {
5678 case DW_TAG_typedef:
5679 case DW_TAG_structure_type:
5680 break;
5681 default:
5682 goto again;
5683 }
5684 break;
5685 case MODULES_DOMAIN:
5686 switch (indexval.dwarf_tag)
5687 {
5688 case DW_TAG_module:
5689 break;
5690 default:
5691 goto again;
5692 }
5693 default:
5694 break;
5695 }
5696
5697 return per_cu;
5698 }
5699
5700 static struct compunit_symtab *
5701 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5702 const char *name, domain_enum domain)
5703 {
5704 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5705
5706 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5707 if (!mapp)
5708 {
5709 /* index is NULL if OBJF_READNOW. */
5710 return NULL;
5711 }
5712 const auto &map = *mapp;
5713
5714 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5715
5716 struct compunit_symtab *stab_best = NULL;
5717 struct dwarf2_per_cu_data *per_cu;
5718 while ((per_cu = iter.next ()) != NULL)
5719 {
5720 struct symbol *sym, *with_opaque = NULL;
5721 compunit_symtab *stab
5722 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5723 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5724 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5725
5726 sym = block_find_symbol (block, name, domain,
5727 block_find_non_opaque_type_preferred,
5728 &with_opaque);
5729
5730 /* Some caution must be observed with overloaded functions and
5731 methods, since the index will not contain any overload
5732 information (but NAME might contain it). */
5733
5734 if (sym != NULL
5735 && strcmp_iw (sym->search_name (), name) == 0)
5736 return stab;
5737 if (with_opaque != NULL
5738 && strcmp_iw (with_opaque->search_name (), name) == 0)
5739 stab_best = stab;
5740
5741 /* Keep looking through other CUs. */
5742 }
5743
5744 return stab_best;
5745 }
5746
5747 /* This dumps minimal information about .debug_names. It is called
5748 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5749 uses this to verify that .debug_names has been loaded. */
5750
5751 static void
5752 dw2_debug_names_dump (struct objfile *objfile)
5753 {
5754 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5755
5756 gdb_assert (per_objfile->per_bfd->using_index);
5757 printf_filtered (".debug_names:");
5758 if (per_objfile->per_bfd->debug_names_table)
5759 printf_filtered (" exists\n");
5760 else
5761 printf_filtered (" faked for \"readnow\"\n");
5762 printf_filtered ("\n");
5763 }
5764
5765 static void
5766 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5767 const char *func_name)
5768 {
5769 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5770
5771 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5772 if (per_objfile->per_bfd->debug_names_table)
5773 {
5774 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5775
5776 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5777 per_objfile);
5778
5779 struct dwarf2_per_cu_data *per_cu;
5780 while ((per_cu = iter.next ()) != NULL)
5781 dw2_instantiate_symtab (per_cu, per_objfile, false);
5782 }
5783 }
5784
5785 static void
5786 dw2_debug_names_map_matching_symbols
5787 (struct objfile *objfile,
5788 const lookup_name_info &name, domain_enum domain,
5789 int global,
5790 gdb::function_view<symbol_found_callback_ftype> callback,
5791 symbol_compare_ftype *ordered_compare)
5792 {
5793 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5794
5795 /* debug_names_table is NULL if OBJF_READNOW. */
5796 if (!per_objfile->per_bfd->debug_names_table)
5797 return;
5798
5799 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5800 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5801
5802 const char *match_name = name.ada ().lookup_name ().c_str ();
5803 auto matcher = [&] (const char *symname)
5804 {
5805 if (ordered_compare == nullptr)
5806 return true;
5807 return ordered_compare (symname, match_name) == 0;
5808 };
5809
5810 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5811 [&] (offset_type namei)
5812 {
5813 /* The name was matched, now expand corresponding CUs that were
5814 marked. */
5815 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5816 per_objfile);
5817
5818 struct dwarf2_per_cu_data *per_cu;
5819 while ((per_cu = iter.next ()) != NULL)
5820 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5821 nullptr);
5822 return true;
5823 }, per_objfile);
5824
5825 /* It's a shame we couldn't do this inside the
5826 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5827 that have already been expanded. Instead, this loop matches what
5828 the psymtab code does. */
5829 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5830 {
5831 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5832 if (symtab != nullptr)
5833 {
5834 const struct block *block
5835 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5836 if (!iterate_over_symbols_terminated (block, name,
5837 domain, callback))
5838 break;
5839 }
5840 }
5841 }
5842
5843 static void
5844 dw2_debug_names_expand_symtabs_matching
5845 (struct objfile *objfile,
5846 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5847 const lookup_name_info *lookup_name,
5848 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5849 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5850 enum search_domain kind)
5851 {
5852 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5853
5854 /* debug_names_table is NULL if OBJF_READNOW. */
5855 if (!per_objfile->per_bfd->debug_names_table)
5856 return;
5857
5858 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5859
5860 if (symbol_matcher == NULL && lookup_name == NULL)
5861 {
5862 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5863 {
5864 QUIT;
5865
5866 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5867 expansion_notify);
5868 }
5869 return;
5870 }
5871
5872 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5873
5874 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5875 symbol_matcher,
5876 kind, [&] (offset_type namei)
5877 {
5878 /* The name was matched, now expand corresponding CUs that were
5879 marked. */
5880 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5881
5882 struct dwarf2_per_cu_data *per_cu;
5883 while ((per_cu = iter.next ()) != NULL)
5884 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5885 expansion_notify);
5886 return true;
5887 }, per_objfile);
5888 }
5889
5890 const struct quick_symbol_functions dwarf2_debug_names_functions =
5891 {
5892 dw2_has_symbols,
5893 dw2_find_last_source_symtab,
5894 dw2_forget_cached_source_info,
5895 dw2_map_symtabs_matching_filename,
5896 dw2_debug_names_lookup_symbol,
5897 NULL,
5898 dw2_print_stats,
5899 dw2_debug_names_dump,
5900 dw2_debug_names_expand_symtabs_for_function,
5901 dw2_expand_all_symtabs,
5902 dw2_expand_symtabs_with_fullname,
5903 dw2_debug_names_map_matching_symbols,
5904 dw2_debug_names_expand_symtabs_matching,
5905 dw2_find_pc_sect_compunit_symtab,
5906 NULL,
5907 dw2_map_symbol_filenames
5908 };
5909
5910 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5911 to either a dwarf2_per_bfd or dwz_file object. */
5912
5913 template <typename T>
5914 static gdb::array_view<const gdb_byte>
5915 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5916 {
5917 dwarf2_section_info *section = &section_owner->gdb_index;
5918
5919 if (section->empty ())
5920 return {};
5921
5922 /* Older elfutils strip versions could keep the section in the main
5923 executable while splitting it for the separate debug info file. */
5924 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5925 return {};
5926
5927 section->read (obj);
5928
5929 /* dwarf2_section_info::size is a bfd_size_type, while
5930 gdb::array_view works with size_t. On 32-bit hosts, with
5931 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5932 is 32-bit. So we need an explicit narrowing conversion here.
5933 This is fine, because it's impossible to allocate or mmap an
5934 array/buffer larger than what size_t can represent. */
5935 return gdb::make_array_view (section->buffer, section->size);
5936 }
5937
5938 /* Lookup the index cache for the contents of the index associated to
5939 DWARF2_OBJ. */
5940
5941 static gdb::array_view<const gdb_byte>
5942 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5943 {
5944 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5945 if (build_id == nullptr)
5946 return {};
5947
5948 return global_index_cache.lookup_gdb_index (build_id,
5949 &dwarf2_per_bfd->index_cache_res);
5950 }
5951
5952 /* Same as the above, but for DWZ. */
5953
5954 static gdb::array_view<const gdb_byte>
5955 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5956 {
5957 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5958 if (build_id == nullptr)
5959 return {};
5960
5961 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5962 }
5963
5964 /* See symfile.h. */
5965
5966 bool
5967 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5968 {
5969 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5970 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5971
5972 /* If we're about to read full symbols, don't bother with the
5973 indices. In this case we also don't care if some other debug
5974 format is making psymtabs, because they are all about to be
5975 expanded anyway. */
5976 if ((objfile->flags & OBJF_READNOW))
5977 {
5978 /* When using READNOW, the using_index flag (set below) indicates that
5979 PER_BFD was already initialized, when we loaded some other objfile. */
5980 if (per_bfd->using_index)
5981 {
5982 *index_kind = dw_index_kind::GDB_INDEX;
5983 per_objfile->resize_symtabs ();
5984 return true;
5985 }
5986
5987 per_bfd->using_index = 1;
5988 create_all_comp_units (per_objfile);
5989 create_all_type_units (per_objfile);
5990 per_bfd->quick_file_names_table
5991 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5992 per_objfile->resize_symtabs ();
5993
5994 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5995 + per_bfd->all_type_units.size ()); ++i)
5996 {
5997 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
5998
5999 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6000 struct dwarf2_per_cu_quick_data);
6001 }
6002
6003 /* Return 1 so that gdb sees the "quick" functions. However,
6004 these functions will be no-ops because we will have expanded
6005 all symtabs. */
6006 *index_kind = dw_index_kind::GDB_INDEX;
6007 return true;
6008 }
6009
6010 /* Was a debug names index already read when we processed an objfile sharing
6011 PER_BFD? */
6012 if (per_bfd->debug_names_table != nullptr)
6013 {
6014 *index_kind = dw_index_kind::DEBUG_NAMES;
6015 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6016 per_objfile->resize_symtabs ();
6017 return true;
6018 }
6019
6020 /* Was a GDB index already read when we processed an objfile sharing
6021 PER_BFD? */
6022 if (per_bfd->index_table != nullptr)
6023 {
6024 *index_kind = dw_index_kind::GDB_INDEX;
6025 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6026 per_objfile->resize_symtabs ();
6027 return true;
6028 }
6029
6030 /* There might already be partial symtabs built for this BFD. This happens
6031 when loading the same binary twice with the index-cache enabled. If so,
6032 don't try to read an index. The objfile / per_objfile initialization will
6033 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6034 code path. */
6035 if (per_bfd->partial_symtabs != nullptr)
6036 return false;
6037
6038 if (dwarf2_read_debug_names (per_objfile))
6039 {
6040 *index_kind = dw_index_kind::DEBUG_NAMES;
6041 per_objfile->resize_symtabs ();
6042 return true;
6043 }
6044
6045 if (dwarf2_read_gdb_index (per_objfile,
6046 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6047 get_gdb_index_contents_from_section<dwz_file>))
6048 {
6049 *index_kind = dw_index_kind::GDB_INDEX;
6050 per_objfile->resize_symtabs ();
6051 return true;
6052 }
6053
6054 /* ... otherwise, try to find the index in the index cache. */
6055 if (dwarf2_read_gdb_index (per_objfile,
6056 get_gdb_index_contents_from_cache,
6057 get_gdb_index_contents_from_cache_dwz))
6058 {
6059 global_index_cache.hit ();
6060 *index_kind = dw_index_kind::GDB_INDEX;
6061 per_objfile->resize_symtabs ();
6062 return true;
6063 }
6064
6065 global_index_cache.miss ();
6066 return false;
6067 }
6068
6069 \f
6070
6071 /* Build a partial symbol table. */
6072
6073 void
6074 dwarf2_build_psymtabs (struct objfile *objfile)
6075 {
6076 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6077 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6078
6079 if (per_bfd->partial_symtabs != nullptr)
6080 {
6081 /* Partial symbols were already read, so now we can simply
6082 attach them. */
6083 objfile->partial_symtabs = per_bfd->partial_symtabs;
6084 per_objfile->resize_symtabs ();
6085 return;
6086 }
6087
6088 init_psymbol_list (objfile, 1024);
6089
6090 try
6091 {
6092 /* This isn't really ideal: all the data we allocate on the
6093 objfile's obstack is still uselessly kept around. However,
6094 freeing it seems unsafe. */
6095 psymtab_discarder psymtabs (objfile);
6096 dwarf2_build_psymtabs_hard (per_objfile);
6097 psymtabs.keep ();
6098
6099 per_objfile->resize_symtabs ();
6100
6101 /* (maybe) store an index in the cache. */
6102 global_index_cache.store (per_objfile);
6103 }
6104 catch (const gdb_exception_error &except)
6105 {
6106 exception_print (gdb_stderr, except);
6107 }
6108
6109 /* Finish by setting the local reference to partial symtabs, so that
6110 we don't try to read them again if reading another objfile with the same
6111 BFD. If we can't in fact share, this won't make a difference anyway as
6112 the dwarf2_per_bfd object won't be shared. */
6113 per_bfd->partial_symtabs = objfile->partial_symtabs;
6114 }
6115
6116 /* Find the base address of the compilation unit for range lists and
6117 location lists. It will normally be specified by DW_AT_low_pc.
6118 In DWARF-3 draft 4, the base address could be overridden by
6119 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6120 compilation units with discontinuous ranges. */
6121
6122 static void
6123 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6124 {
6125 struct attribute *attr;
6126
6127 cu->base_address.reset ();
6128
6129 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6130 if (attr != nullptr)
6131 cu->base_address = attr->value_as_address ();
6132 else
6133 {
6134 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6135 if (attr != nullptr)
6136 cu->base_address = attr->value_as_address ();
6137 }
6138 }
6139
6140 /* Helper function that returns the proper abbrev section for
6141 THIS_CU. */
6142
6143 static struct dwarf2_section_info *
6144 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6145 {
6146 struct dwarf2_section_info *abbrev;
6147 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6148
6149 if (this_cu->is_dwz)
6150 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6151 else
6152 abbrev = &per_bfd->abbrev;
6153
6154 return abbrev;
6155 }
6156
6157 /* Fetch the abbreviation table offset from a comp or type unit header. */
6158
6159 static sect_offset
6160 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6161 struct dwarf2_section_info *section,
6162 sect_offset sect_off)
6163 {
6164 bfd *abfd = section->get_bfd_owner ();
6165 const gdb_byte *info_ptr;
6166 unsigned int initial_length_size, offset_size;
6167 uint16_t version;
6168
6169 section->read (per_objfile->objfile);
6170 info_ptr = section->buffer + to_underlying (sect_off);
6171 read_initial_length (abfd, info_ptr, &initial_length_size);
6172 offset_size = initial_length_size == 4 ? 4 : 8;
6173 info_ptr += initial_length_size;
6174
6175 version = read_2_bytes (abfd, info_ptr);
6176 info_ptr += 2;
6177 if (version >= 5)
6178 {
6179 /* Skip unit type and address size. */
6180 info_ptr += 2;
6181 }
6182
6183 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6184 }
6185
6186 /* A partial symtab that is used only for include files. */
6187 struct dwarf2_include_psymtab : public partial_symtab
6188 {
6189 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6190 : partial_symtab (filename, objfile)
6191 {
6192 }
6193
6194 void read_symtab (struct objfile *objfile) override
6195 {
6196 /* It's an include file, no symbols to read for it.
6197 Everything is in the includer symtab. */
6198
6199 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6200 expansion of the includer psymtab. We use the dependencies[0] field to
6201 model the includer. But if we go the regular route of calling
6202 expand_psymtab here, and having expand_psymtab call expand_dependencies
6203 to expand the includer, we'll only use expand_psymtab on the includer
6204 (making it a non-toplevel psymtab), while if we expand the includer via
6205 another path, we'll use read_symtab (making it a toplevel psymtab).
6206 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6207 psymtab, and trigger read_symtab on the includer here directly. */
6208 includer ()->read_symtab (objfile);
6209 }
6210
6211 void expand_psymtab (struct objfile *objfile) override
6212 {
6213 /* This is not called by read_symtab, and should not be called by any
6214 expand_dependencies. */
6215 gdb_assert (false);
6216 }
6217
6218 bool readin_p (struct objfile *objfile) const override
6219 {
6220 return includer ()->readin_p (objfile);
6221 }
6222
6223 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6224 {
6225 return nullptr;
6226 }
6227
6228 private:
6229 partial_symtab *includer () const
6230 {
6231 /* An include psymtab has exactly one dependency: the psymtab that
6232 includes it. */
6233 gdb_assert (this->number_of_dependencies == 1);
6234 return this->dependencies[0];
6235 }
6236 };
6237
6238 /* Allocate a new partial symtab for file named NAME and mark this new
6239 partial symtab as being an include of PST. */
6240
6241 static void
6242 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6243 struct objfile *objfile)
6244 {
6245 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6246
6247 if (!IS_ABSOLUTE_PATH (subpst->filename))
6248 subpst->dirname = pst->dirname;
6249
6250 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6251 subpst->dependencies[0] = pst;
6252 subpst->number_of_dependencies = 1;
6253 }
6254
6255 /* Read the Line Number Program data and extract the list of files
6256 included by the source file represented by PST. Build an include
6257 partial symtab for each of these included files. */
6258
6259 static void
6260 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6261 struct die_info *die,
6262 dwarf2_psymtab *pst)
6263 {
6264 line_header_up lh;
6265 struct attribute *attr;
6266
6267 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6268 if (attr != nullptr)
6269 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6270 if (lh == NULL)
6271 return; /* No linetable, so no includes. */
6272
6273 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6274 that we pass in the raw text_low here; that is ok because we're
6275 only decoding the line table to make include partial symtabs, and
6276 so the addresses aren't really used. */
6277 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6278 pst->raw_text_low (), 1);
6279 }
6280
6281 static hashval_t
6282 hash_signatured_type (const void *item)
6283 {
6284 const struct signatured_type *sig_type
6285 = (const struct signatured_type *) item;
6286
6287 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6288 return sig_type->signature;
6289 }
6290
6291 static int
6292 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6293 {
6294 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6295 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6296
6297 return lhs->signature == rhs->signature;
6298 }
6299
6300 /* Allocate a hash table for signatured types. */
6301
6302 static htab_up
6303 allocate_signatured_type_table ()
6304 {
6305 return htab_up (htab_create_alloc (41,
6306 hash_signatured_type,
6307 eq_signatured_type,
6308 NULL, xcalloc, xfree));
6309 }
6310
6311 /* A helper function to add a signatured type CU to a table. */
6312
6313 static int
6314 add_signatured_type_cu_to_table (void **slot, void *datum)
6315 {
6316 struct signatured_type *sigt = (struct signatured_type *) *slot;
6317 std::vector<signatured_type *> *all_type_units
6318 = (std::vector<signatured_type *> *) datum;
6319
6320 all_type_units->push_back (sigt);
6321
6322 return 1;
6323 }
6324
6325 /* A helper for create_debug_types_hash_table. Read types from SECTION
6326 and fill them into TYPES_HTAB. It will process only type units,
6327 therefore DW_UT_type. */
6328
6329 static void
6330 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6331 struct dwo_file *dwo_file,
6332 dwarf2_section_info *section, htab_up &types_htab,
6333 rcuh_kind section_kind)
6334 {
6335 struct objfile *objfile = per_objfile->objfile;
6336 struct dwarf2_section_info *abbrev_section;
6337 bfd *abfd;
6338 const gdb_byte *info_ptr, *end_ptr;
6339
6340 abbrev_section = (dwo_file != NULL
6341 ? &dwo_file->sections.abbrev
6342 : &per_objfile->per_bfd->abbrev);
6343
6344 if (dwarf_read_debug)
6345 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6346 section->get_name (),
6347 abbrev_section->get_file_name ());
6348
6349 section->read (objfile);
6350 info_ptr = section->buffer;
6351
6352 if (info_ptr == NULL)
6353 return;
6354
6355 /* We can't set abfd until now because the section may be empty or
6356 not present, in which case the bfd is unknown. */
6357 abfd = section->get_bfd_owner ();
6358
6359 /* We don't use cutu_reader here because we don't need to read
6360 any dies: the signature is in the header. */
6361
6362 end_ptr = info_ptr + section->size;
6363 while (info_ptr < end_ptr)
6364 {
6365 struct signatured_type *sig_type;
6366 struct dwo_unit *dwo_tu;
6367 void **slot;
6368 const gdb_byte *ptr = info_ptr;
6369 struct comp_unit_head header;
6370 unsigned int length;
6371
6372 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6373
6374 /* Initialize it due to a false compiler warning. */
6375 header.signature = -1;
6376 header.type_cu_offset_in_tu = (cu_offset) -1;
6377
6378 /* We need to read the type's signature in order to build the hash
6379 table, but we don't need anything else just yet. */
6380
6381 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6382 abbrev_section, ptr, section_kind);
6383
6384 length = header.get_length ();
6385
6386 /* Skip dummy type units. */
6387 if (ptr >= info_ptr + length
6388 || peek_abbrev_code (abfd, ptr) == 0
6389 || header.unit_type != DW_UT_type)
6390 {
6391 info_ptr += length;
6392 continue;
6393 }
6394
6395 if (types_htab == NULL)
6396 {
6397 if (dwo_file)
6398 types_htab = allocate_dwo_unit_table ();
6399 else
6400 types_htab = allocate_signatured_type_table ();
6401 }
6402
6403 if (dwo_file)
6404 {
6405 sig_type = NULL;
6406 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6407 dwo_tu->dwo_file = dwo_file;
6408 dwo_tu->signature = header.signature;
6409 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6410 dwo_tu->section = section;
6411 dwo_tu->sect_off = sect_off;
6412 dwo_tu->length = length;
6413 }
6414 else
6415 {
6416 /* N.B.: type_offset is not usable if this type uses a DWO file.
6417 The real type_offset is in the DWO file. */
6418 dwo_tu = NULL;
6419 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6420 sig_type->signature = header.signature;
6421 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6422 sig_type->per_cu.is_debug_types = 1;
6423 sig_type->per_cu.section = section;
6424 sig_type->per_cu.sect_off = sect_off;
6425 sig_type->per_cu.length = length;
6426 }
6427
6428 slot = htab_find_slot (types_htab.get (),
6429 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6430 INSERT);
6431 gdb_assert (slot != NULL);
6432 if (*slot != NULL)
6433 {
6434 sect_offset dup_sect_off;
6435
6436 if (dwo_file)
6437 {
6438 const struct dwo_unit *dup_tu
6439 = (const struct dwo_unit *) *slot;
6440
6441 dup_sect_off = dup_tu->sect_off;
6442 }
6443 else
6444 {
6445 const struct signatured_type *dup_tu
6446 = (const struct signatured_type *) *slot;
6447
6448 dup_sect_off = dup_tu->per_cu.sect_off;
6449 }
6450
6451 complaint (_("debug type entry at offset %s is duplicate to"
6452 " the entry at offset %s, signature %s"),
6453 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6454 hex_string (header.signature));
6455 }
6456 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6457
6458 if (dwarf_read_debug > 1)
6459 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6460 sect_offset_str (sect_off),
6461 hex_string (header.signature));
6462
6463 info_ptr += length;
6464 }
6465 }
6466
6467 /* Create the hash table of all entries in the .debug_types
6468 (or .debug_types.dwo) section(s).
6469 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6470 otherwise it is NULL.
6471
6472 The result is a pointer to the hash table or NULL if there are no types.
6473
6474 Note: This function processes DWO files only, not DWP files. */
6475
6476 static void
6477 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6478 struct dwo_file *dwo_file,
6479 gdb::array_view<dwarf2_section_info> type_sections,
6480 htab_up &types_htab)
6481 {
6482 for (dwarf2_section_info &section : type_sections)
6483 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6484 rcuh_kind::TYPE);
6485 }
6486
6487 /* Create the hash table of all entries in the .debug_types section,
6488 and initialize all_type_units.
6489 The result is zero if there is an error (e.g. missing .debug_types section),
6490 otherwise non-zero. */
6491
6492 static int
6493 create_all_type_units (dwarf2_per_objfile *per_objfile)
6494 {
6495 htab_up types_htab;
6496
6497 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6498 types_htab, rcuh_kind::COMPILE);
6499 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6500 types_htab);
6501 if (types_htab == NULL)
6502 {
6503 per_objfile->per_bfd->signatured_types = NULL;
6504 return 0;
6505 }
6506
6507 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6508
6509 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6510 per_objfile->per_bfd->all_type_units.reserve
6511 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6512
6513 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6514 add_signatured_type_cu_to_table,
6515 &per_objfile->per_bfd->all_type_units);
6516
6517 return 1;
6518 }
6519
6520 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6521 If SLOT is non-NULL, it is the entry to use in the hash table.
6522 Otherwise we find one. */
6523
6524 static struct signatured_type *
6525 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6526 {
6527 if (per_objfile->per_bfd->all_type_units.size ()
6528 == per_objfile->per_bfd->all_type_units.capacity ())
6529 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6530
6531 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6532
6533 per_objfile->resize_symtabs ();
6534
6535 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6536 sig_type->signature = sig;
6537 sig_type->per_cu.is_debug_types = 1;
6538 if (per_objfile->per_bfd->using_index)
6539 {
6540 sig_type->per_cu.v.quick =
6541 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6542 struct dwarf2_per_cu_quick_data);
6543 }
6544
6545 if (slot == NULL)
6546 {
6547 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6548 sig_type, INSERT);
6549 }
6550 gdb_assert (*slot == NULL);
6551 *slot = sig_type;
6552 /* The rest of sig_type must be filled in by the caller. */
6553 return sig_type;
6554 }
6555
6556 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6557 Fill in SIG_ENTRY with DWO_ENTRY. */
6558
6559 static void
6560 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6561 struct signatured_type *sig_entry,
6562 struct dwo_unit *dwo_entry)
6563 {
6564 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6565
6566 /* Make sure we're not clobbering something we don't expect to. */
6567 gdb_assert (! sig_entry->per_cu.queued);
6568 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6569 if (per_bfd->using_index)
6570 {
6571 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6572 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6573 }
6574 else
6575 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6576 gdb_assert (sig_entry->signature == dwo_entry->signature);
6577 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6578 gdb_assert (sig_entry->type_unit_group == NULL);
6579 gdb_assert (sig_entry->dwo_unit == NULL);
6580
6581 sig_entry->per_cu.section = dwo_entry->section;
6582 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6583 sig_entry->per_cu.length = dwo_entry->length;
6584 sig_entry->per_cu.reading_dwo_directly = 1;
6585 sig_entry->per_cu.per_bfd = per_bfd;
6586 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6587 sig_entry->dwo_unit = dwo_entry;
6588 }
6589
6590 /* Subroutine of lookup_signatured_type.
6591 If we haven't read the TU yet, create the signatured_type data structure
6592 for a TU to be read in directly from a DWO file, bypassing the stub.
6593 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6594 using .gdb_index, then when reading a CU we want to stay in the DWO file
6595 containing that CU. Otherwise we could end up reading several other DWO
6596 files (due to comdat folding) to process the transitive closure of all the
6597 mentioned TUs, and that can be slow. The current DWO file will have every
6598 type signature that it needs.
6599 We only do this for .gdb_index because in the psymtab case we already have
6600 to read all the DWOs to build the type unit groups. */
6601
6602 static struct signatured_type *
6603 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6604 {
6605 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6606 struct dwo_file *dwo_file;
6607 struct dwo_unit find_dwo_entry, *dwo_entry;
6608 struct signatured_type find_sig_entry, *sig_entry;
6609 void **slot;
6610
6611 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6612
6613 /* If TU skeletons have been removed then we may not have read in any
6614 TUs yet. */
6615 if (per_objfile->per_bfd->signatured_types == NULL)
6616 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6617
6618 /* We only ever need to read in one copy of a signatured type.
6619 Use the global signatured_types array to do our own comdat-folding
6620 of types. If this is the first time we're reading this TU, and
6621 the TU has an entry in .gdb_index, replace the recorded data from
6622 .gdb_index with this TU. */
6623
6624 find_sig_entry.signature = sig;
6625 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6626 &find_sig_entry, INSERT);
6627 sig_entry = (struct signatured_type *) *slot;
6628
6629 /* We can get here with the TU already read, *or* in the process of being
6630 read. Don't reassign the global entry to point to this DWO if that's
6631 the case. Also note that if the TU is already being read, it may not
6632 have come from a DWO, the program may be a mix of Fission-compiled
6633 code and non-Fission-compiled code. */
6634
6635 /* Have we already tried to read this TU?
6636 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6637 needn't exist in the global table yet). */
6638 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6639 return sig_entry;
6640
6641 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6642 dwo_unit of the TU itself. */
6643 dwo_file = cu->dwo_unit->dwo_file;
6644
6645 /* Ok, this is the first time we're reading this TU. */
6646 if (dwo_file->tus == NULL)
6647 return NULL;
6648 find_dwo_entry.signature = sig;
6649 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6650 &find_dwo_entry);
6651 if (dwo_entry == NULL)
6652 return NULL;
6653
6654 /* If the global table doesn't have an entry for this TU, add one. */
6655 if (sig_entry == NULL)
6656 sig_entry = add_type_unit (per_objfile, sig, slot);
6657
6658 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6659 sig_entry->per_cu.tu_read = 1;
6660 return sig_entry;
6661 }
6662
6663 /* Subroutine of lookup_signatured_type.
6664 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6665 then try the DWP file. If the TU stub (skeleton) has been removed then
6666 it won't be in .gdb_index. */
6667
6668 static struct signatured_type *
6669 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6670 {
6671 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6672 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6673 struct dwo_unit *dwo_entry;
6674 struct signatured_type find_sig_entry, *sig_entry;
6675 void **slot;
6676
6677 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6678 gdb_assert (dwp_file != NULL);
6679
6680 /* If TU skeletons have been removed then we may not have read in any
6681 TUs yet. */
6682 if (per_objfile->per_bfd->signatured_types == NULL)
6683 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6684
6685 find_sig_entry.signature = sig;
6686 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6687 &find_sig_entry, INSERT);
6688 sig_entry = (struct signatured_type *) *slot;
6689
6690 /* Have we already tried to read this TU?
6691 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6692 needn't exist in the global table yet). */
6693 if (sig_entry != NULL)
6694 return sig_entry;
6695
6696 if (dwp_file->tus == NULL)
6697 return NULL;
6698 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6699 1 /* is_debug_types */);
6700 if (dwo_entry == NULL)
6701 return NULL;
6702
6703 sig_entry = add_type_unit (per_objfile, sig, slot);
6704 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6705
6706 return sig_entry;
6707 }
6708
6709 /* Lookup a signature based type for DW_FORM_ref_sig8.
6710 Returns NULL if signature SIG is not present in the table.
6711 It is up to the caller to complain about this. */
6712
6713 static struct signatured_type *
6714 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6715 {
6716 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6717
6718 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6719 {
6720 /* We're in a DWO/DWP file, and we're using .gdb_index.
6721 These cases require special processing. */
6722 if (get_dwp_file (per_objfile) == NULL)
6723 return lookup_dwo_signatured_type (cu, sig);
6724 else
6725 return lookup_dwp_signatured_type (cu, sig);
6726 }
6727 else
6728 {
6729 struct signatured_type find_entry, *entry;
6730
6731 if (per_objfile->per_bfd->signatured_types == NULL)
6732 return NULL;
6733 find_entry.signature = sig;
6734 entry = ((struct signatured_type *)
6735 htab_find (per_objfile->per_bfd->signatured_types.get (),
6736 &find_entry));
6737 return entry;
6738 }
6739 }
6740
6741 /* Low level DIE reading support. */
6742
6743 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6744
6745 static void
6746 init_cu_die_reader (struct die_reader_specs *reader,
6747 struct dwarf2_cu *cu,
6748 struct dwarf2_section_info *section,
6749 struct dwo_file *dwo_file,
6750 struct abbrev_table *abbrev_table)
6751 {
6752 gdb_assert (section->readin && section->buffer != NULL);
6753 reader->abfd = section->get_bfd_owner ();
6754 reader->cu = cu;
6755 reader->dwo_file = dwo_file;
6756 reader->die_section = section;
6757 reader->buffer = section->buffer;
6758 reader->buffer_end = section->buffer + section->size;
6759 reader->abbrev_table = abbrev_table;
6760 }
6761
6762 /* Subroutine of cutu_reader to simplify it.
6763 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6764 There's just a lot of work to do, and cutu_reader is big enough
6765 already.
6766
6767 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6768 from it to the DIE in the DWO. If NULL we are skipping the stub.
6769 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6770 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6771 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6772 STUB_COMP_DIR may be non-NULL.
6773 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6774 are filled in with the info of the DIE from the DWO file.
6775 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6776 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6777 kept around for at least as long as *RESULT_READER.
6778
6779 The result is non-zero if a valid (non-dummy) DIE was found. */
6780
6781 static int
6782 read_cutu_die_from_dwo (dwarf2_cu *cu,
6783 struct dwo_unit *dwo_unit,
6784 struct die_info *stub_comp_unit_die,
6785 const char *stub_comp_dir,
6786 struct die_reader_specs *result_reader,
6787 const gdb_byte **result_info_ptr,
6788 struct die_info **result_comp_unit_die,
6789 abbrev_table_up *result_dwo_abbrev_table)
6790 {
6791 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6792 dwarf2_per_cu_data *per_cu = cu->per_cu;
6793 struct objfile *objfile = per_objfile->objfile;
6794 bfd *abfd;
6795 const gdb_byte *begin_info_ptr, *info_ptr;
6796 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6797 int i,num_extra_attrs;
6798 struct dwarf2_section_info *dwo_abbrev_section;
6799 struct die_info *comp_unit_die;
6800
6801 /* At most one of these may be provided. */
6802 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6803
6804 /* These attributes aren't processed until later:
6805 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6806 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6807 referenced later. However, these attributes are found in the stub
6808 which we won't have later. In order to not impose this complication
6809 on the rest of the code, we read them here and copy them to the
6810 DWO CU/TU die. */
6811
6812 stmt_list = NULL;
6813 low_pc = NULL;
6814 high_pc = NULL;
6815 ranges = NULL;
6816 comp_dir = NULL;
6817
6818 if (stub_comp_unit_die != NULL)
6819 {
6820 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6821 DWO file. */
6822 if (!per_cu->is_debug_types)
6823 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6824 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6825 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6826 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6827 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6828
6829 cu->addr_base = stub_comp_unit_die->addr_base ();
6830
6831 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6832 here (if needed). We need the value before we can process
6833 DW_AT_ranges. */
6834 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6835 }
6836 else if (stub_comp_dir != NULL)
6837 {
6838 /* Reconstruct the comp_dir attribute to simplify the code below. */
6839 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6840 comp_dir->name = DW_AT_comp_dir;
6841 comp_dir->form = DW_FORM_string;
6842 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6843 DW_STRING (comp_dir) = stub_comp_dir;
6844 }
6845
6846 /* Set up for reading the DWO CU/TU. */
6847 cu->dwo_unit = dwo_unit;
6848 dwarf2_section_info *section = dwo_unit->section;
6849 section->read (objfile);
6850 abfd = section->get_bfd_owner ();
6851 begin_info_ptr = info_ptr = (section->buffer
6852 + to_underlying (dwo_unit->sect_off));
6853 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6854
6855 if (per_cu->is_debug_types)
6856 {
6857 signatured_type *sig_type = (struct signatured_type *) per_cu;
6858
6859 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6860 section, dwo_abbrev_section,
6861 info_ptr, rcuh_kind::TYPE);
6862 /* This is not an assert because it can be caused by bad debug info. */
6863 if (sig_type->signature != cu->header.signature)
6864 {
6865 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6866 " TU at offset %s [in module %s]"),
6867 hex_string (sig_type->signature),
6868 hex_string (cu->header.signature),
6869 sect_offset_str (dwo_unit->sect_off),
6870 bfd_get_filename (abfd));
6871 }
6872 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6873 /* For DWOs coming from DWP files, we don't know the CU length
6874 nor the type's offset in the TU until now. */
6875 dwo_unit->length = cu->header.get_length ();
6876 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6877
6878 /* Establish the type offset that can be used to lookup the type.
6879 For DWO files, we don't know it until now. */
6880 sig_type->type_offset_in_section
6881 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6882 }
6883 else
6884 {
6885 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6886 section, dwo_abbrev_section,
6887 info_ptr, rcuh_kind::COMPILE);
6888 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6889 /* For DWOs coming from DWP files, we don't know the CU length
6890 until now. */
6891 dwo_unit->length = cu->header.get_length ();
6892 }
6893
6894 *result_dwo_abbrev_table
6895 = abbrev_table::read (objfile, dwo_abbrev_section,
6896 cu->header.abbrev_sect_off);
6897 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6898 result_dwo_abbrev_table->get ());
6899
6900 /* Read in the die, but leave space to copy over the attributes
6901 from the stub. This has the benefit of simplifying the rest of
6902 the code - all the work to maintain the illusion of a single
6903 DW_TAG_{compile,type}_unit DIE is done here. */
6904 num_extra_attrs = ((stmt_list != NULL)
6905 + (low_pc != NULL)
6906 + (high_pc != NULL)
6907 + (ranges != NULL)
6908 + (comp_dir != NULL));
6909 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6910 num_extra_attrs);
6911
6912 /* Copy over the attributes from the stub to the DIE we just read in. */
6913 comp_unit_die = *result_comp_unit_die;
6914 i = comp_unit_die->num_attrs;
6915 if (stmt_list != NULL)
6916 comp_unit_die->attrs[i++] = *stmt_list;
6917 if (low_pc != NULL)
6918 comp_unit_die->attrs[i++] = *low_pc;
6919 if (high_pc != NULL)
6920 comp_unit_die->attrs[i++] = *high_pc;
6921 if (ranges != NULL)
6922 comp_unit_die->attrs[i++] = *ranges;
6923 if (comp_dir != NULL)
6924 comp_unit_die->attrs[i++] = *comp_dir;
6925 comp_unit_die->num_attrs += num_extra_attrs;
6926
6927 if (dwarf_die_debug)
6928 {
6929 fprintf_unfiltered (gdb_stdlog,
6930 "Read die from %s@0x%x of %s:\n",
6931 section->get_name (),
6932 (unsigned) (begin_info_ptr - section->buffer),
6933 bfd_get_filename (abfd));
6934 dump_die (comp_unit_die, dwarf_die_debug);
6935 }
6936
6937 /* Skip dummy compilation units. */
6938 if (info_ptr >= begin_info_ptr + dwo_unit->length
6939 || peek_abbrev_code (abfd, info_ptr) == 0)
6940 return 0;
6941
6942 *result_info_ptr = info_ptr;
6943 return 1;
6944 }
6945
6946 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6947 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6948 signature is part of the header. */
6949 static gdb::optional<ULONGEST>
6950 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6951 {
6952 if (cu->header.version >= 5)
6953 return cu->header.signature;
6954 struct attribute *attr;
6955 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6956 if (attr == nullptr)
6957 return gdb::optional<ULONGEST> ();
6958 return DW_UNSND (attr);
6959 }
6960
6961 /* Subroutine of cutu_reader to simplify it.
6962 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6963 Returns NULL if the specified DWO unit cannot be found. */
6964
6965 static struct dwo_unit *
6966 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6967 {
6968 dwarf2_per_cu_data *per_cu = cu->per_cu;
6969 struct dwo_unit *dwo_unit;
6970 const char *comp_dir;
6971
6972 gdb_assert (cu != NULL);
6973
6974 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6975 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6976 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6977
6978 if (per_cu->is_debug_types)
6979 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6980 else
6981 {
6982 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6983
6984 if (!signature.has_value ())
6985 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6986 " [in module %s]"),
6987 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6988
6989 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6990 }
6991
6992 return dwo_unit;
6993 }
6994
6995 /* Subroutine of cutu_reader to simplify it.
6996 See it for a description of the parameters.
6997 Read a TU directly from a DWO file, bypassing the stub. */
6998
6999 void
7000 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7001 dwarf2_per_objfile *per_objfile,
7002 dwarf2_cu *existing_cu)
7003 {
7004 struct signatured_type *sig_type;
7005
7006 /* Verify we can do the following downcast, and that we have the
7007 data we need. */
7008 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7009 sig_type = (struct signatured_type *) this_cu;
7010 gdb_assert (sig_type->dwo_unit != NULL);
7011
7012 dwarf2_cu *cu;
7013
7014 if (existing_cu != nullptr)
7015 {
7016 cu = existing_cu;
7017 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7018 /* There's no need to do the rereading_dwo_cu handling that
7019 cutu_reader does since we don't read the stub. */
7020 }
7021 else
7022 {
7023 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7024 in per_objfile yet. */
7025 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7026 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7027 cu = m_new_cu.get ();
7028 }
7029
7030 /* A future optimization, if needed, would be to use an existing
7031 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7032 could share abbrev tables. */
7033
7034 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7035 NULL /* stub_comp_unit_die */,
7036 sig_type->dwo_unit->dwo_file->comp_dir,
7037 this, &info_ptr,
7038 &comp_unit_die,
7039 &m_dwo_abbrev_table) == 0)
7040 {
7041 /* Dummy die. */
7042 dummy_p = true;
7043 }
7044 }
7045
7046 /* Initialize a CU (or TU) and read its DIEs.
7047 If the CU defers to a DWO file, read the DWO file as well.
7048
7049 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7050 Otherwise the table specified in the comp unit header is read in and used.
7051 This is an optimization for when we already have the abbrev table.
7052
7053 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7054 allocated. */
7055
7056 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7057 dwarf2_per_objfile *per_objfile,
7058 struct abbrev_table *abbrev_table,
7059 dwarf2_cu *existing_cu,
7060 bool skip_partial)
7061 : die_reader_specs {},
7062 m_this_cu (this_cu)
7063 {
7064 struct objfile *objfile = per_objfile->objfile;
7065 struct dwarf2_section_info *section = this_cu->section;
7066 bfd *abfd = section->get_bfd_owner ();
7067 const gdb_byte *begin_info_ptr;
7068 struct signatured_type *sig_type = NULL;
7069 struct dwarf2_section_info *abbrev_section;
7070 /* Non-zero if CU currently points to a DWO file and we need to
7071 reread it. When this happens we need to reread the skeleton die
7072 before we can reread the DWO file (this only applies to CUs, not TUs). */
7073 int rereading_dwo_cu = 0;
7074
7075 if (dwarf_die_debug)
7076 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7077 this_cu->is_debug_types ? "type" : "comp",
7078 sect_offset_str (this_cu->sect_off));
7079
7080 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7081 file (instead of going through the stub), short-circuit all of this. */
7082 if (this_cu->reading_dwo_directly)
7083 {
7084 /* Narrow down the scope of possibilities to have to understand. */
7085 gdb_assert (this_cu->is_debug_types);
7086 gdb_assert (abbrev_table == NULL);
7087 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7088 return;
7089 }
7090
7091 /* This is cheap if the section is already read in. */
7092 section->read (objfile);
7093
7094 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7095
7096 abbrev_section = get_abbrev_section_for_cu (this_cu);
7097
7098 dwarf2_cu *cu;
7099
7100 if (existing_cu != nullptr)
7101 {
7102 cu = existing_cu;
7103 /* If this CU is from a DWO file we need to start over, we need to
7104 refetch the attributes from the skeleton CU.
7105 This could be optimized by retrieving those attributes from when we
7106 were here the first time: the previous comp_unit_die was stored in
7107 comp_unit_obstack. But there's no data yet that we need this
7108 optimization. */
7109 if (cu->dwo_unit != NULL)
7110 rereading_dwo_cu = 1;
7111 }
7112 else
7113 {
7114 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7115 in per_objfile yet. */
7116 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7117 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7118 cu = m_new_cu.get ();
7119 }
7120
7121 /* Get the header. */
7122 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7123 {
7124 /* We already have the header, there's no need to read it in again. */
7125 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7126 }
7127 else
7128 {
7129 if (this_cu->is_debug_types)
7130 {
7131 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7132 section, abbrev_section,
7133 info_ptr, rcuh_kind::TYPE);
7134
7135 /* Since per_cu is the first member of struct signatured_type,
7136 we can go from a pointer to one to a pointer to the other. */
7137 sig_type = (struct signatured_type *) this_cu;
7138 gdb_assert (sig_type->signature == cu->header.signature);
7139 gdb_assert (sig_type->type_offset_in_tu
7140 == cu->header.type_cu_offset_in_tu);
7141 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7142
7143 /* LENGTH has not been set yet for type units if we're
7144 using .gdb_index. */
7145 this_cu->length = cu->header.get_length ();
7146
7147 /* Establish the type offset that can be used to lookup the type. */
7148 sig_type->type_offset_in_section =
7149 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7150
7151 this_cu->dwarf_version = cu->header.version;
7152 }
7153 else
7154 {
7155 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7156 section, abbrev_section,
7157 info_ptr,
7158 rcuh_kind::COMPILE);
7159
7160 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7161 if (this_cu->length == 0)
7162 this_cu->length = cu->header.get_length ();
7163 else
7164 gdb_assert (this_cu->length == cu->header.get_length ());
7165 this_cu->dwarf_version = cu->header.version;
7166 }
7167 }
7168
7169 /* Skip dummy compilation units. */
7170 if (info_ptr >= begin_info_ptr + this_cu->length
7171 || peek_abbrev_code (abfd, info_ptr) == 0)
7172 {
7173 dummy_p = true;
7174 return;
7175 }
7176
7177 /* If we don't have them yet, read the abbrevs for this compilation unit.
7178 And if we need to read them now, make sure they're freed when we're
7179 done. */
7180 if (abbrev_table != NULL)
7181 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7182 else
7183 {
7184 m_abbrev_table_holder
7185 = abbrev_table::read (objfile, abbrev_section,
7186 cu->header.abbrev_sect_off);
7187 abbrev_table = m_abbrev_table_holder.get ();
7188 }
7189
7190 /* Read the top level CU/TU die. */
7191 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7192 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7193
7194 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7195 {
7196 dummy_p = true;
7197 return;
7198 }
7199
7200 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7201 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7202 table from the DWO file and pass the ownership over to us. It will be
7203 referenced from READER, so we must make sure to free it after we're done
7204 with READER.
7205
7206 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7207 DWO CU, that this test will fail (the attribute will not be present). */
7208 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7209 if (dwo_name != nullptr)
7210 {
7211 struct dwo_unit *dwo_unit;
7212 struct die_info *dwo_comp_unit_die;
7213
7214 if (comp_unit_die->has_children)
7215 {
7216 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7217 " has children (offset %s) [in module %s]"),
7218 sect_offset_str (this_cu->sect_off),
7219 bfd_get_filename (abfd));
7220 }
7221 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7222 if (dwo_unit != NULL)
7223 {
7224 if (read_cutu_die_from_dwo (cu, dwo_unit,
7225 comp_unit_die, NULL,
7226 this, &info_ptr,
7227 &dwo_comp_unit_die,
7228 &m_dwo_abbrev_table) == 0)
7229 {
7230 /* Dummy die. */
7231 dummy_p = true;
7232 return;
7233 }
7234 comp_unit_die = dwo_comp_unit_die;
7235 }
7236 else
7237 {
7238 /* Yikes, we couldn't find the rest of the DIE, we only have
7239 the stub. A complaint has already been logged. There's
7240 not much more we can do except pass on the stub DIE to
7241 die_reader_func. We don't want to throw an error on bad
7242 debug info. */
7243 }
7244 }
7245 }
7246
7247 void
7248 cutu_reader::keep ()
7249 {
7250 /* Done, clean up. */
7251 gdb_assert (!dummy_p);
7252 if (m_new_cu != NULL)
7253 {
7254 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7255 now. */
7256 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7257 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7258 }
7259 }
7260
7261 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7262 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7263 assumed to have already done the lookup to find the DWO file).
7264
7265 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7266 THIS_CU->is_debug_types, but nothing else.
7267
7268 We fill in THIS_CU->length.
7269
7270 THIS_CU->cu is always freed when done.
7271 This is done in order to not leave THIS_CU->cu in a state where we have
7272 to care whether it refers to the "main" CU or the DWO CU.
7273
7274 When parent_cu is passed, it is used to provide a default value for
7275 str_offsets_base and addr_base from the parent. */
7276
7277 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7278 dwarf2_per_objfile *per_objfile,
7279 struct dwarf2_cu *parent_cu,
7280 struct dwo_file *dwo_file)
7281 : die_reader_specs {},
7282 m_this_cu (this_cu)
7283 {
7284 struct objfile *objfile = per_objfile->objfile;
7285 struct dwarf2_section_info *section = this_cu->section;
7286 bfd *abfd = section->get_bfd_owner ();
7287 struct dwarf2_section_info *abbrev_section;
7288 const gdb_byte *begin_info_ptr, *info_ptr;
7289
7290 if (dwarf_die_debug)
7291 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7292 this_cu->is_debug_types ? "type" : "comp",
7293 sect_offset_str (this_cu->sect_off));
7294
7295 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7296
7297 abbrev_section = (dwo_file != NULL
7298 ? &dwo_file->sections.abbrev
7299 : get_abbrev_section_for_cu (this_cu));
7300
7301 /* This is cheap if the section is already read in. */
7302 section->read (objfile);
7303
7304 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7305
7306 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7307 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7308 section, abbrev_section, info_ptr,
7309 (this_cu->is_debug_types
7310 ? rcuh_kind::TYPE
7311 : rcuh_kind::COMPILE));
7312
7313 if (parent_cu != nullptr)
7314 {
7315 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7316 m_new_cu->addr_base = parent_cu->addr_base;
7317 }
7318 this_cu->length = m_new_cu->header.get_length ();
7319
7320 /* Skip dummy compilation units. */
7321 if (info_ptr >= begin_info_ptr + this_cu->length
7322 || peek_abbrev_code (abfd, info_ptr) == 0)
7323 {
7324 dummy_p = true;
7325 return;
7326 }
7327
7328 m_abbrev_table_holder
7329 = abbrev_table::read (objfile, abbrev_section,
7330 m_new_cu->header.abbrev_sect_off);
7331
7332 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7333 m_abbrev_table_holder.get ());
7334 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7335 }
7336
7337 \f
7338 /* Type Unit Groups.
7339
7340 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7341 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7342 so that all types coming from the same compilation (.o file) are grouped
7343 together. A future step could be to put the types in the same symtab as
7344 the CU the types ultimately came from. */
7345
7346 static hashval_t
7347 hash_type_unit_group (const void *item)
7348 {
7349 const struct type_unit_group *tu_group
7350 = (const struct type_unit_group *) item;
7351
7352 return hash_stmt_list_entry (&tu_group->hash);
7353 }
7354
7355 static int
7356 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7357 {
7358 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7359 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7360
7361 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7362 }
7363
7364 /* Allocate a hash table for type unit groups. */
7365
7366 static htab_up
7367 allocate_type_unit_groups_table ()
7368 {
7369 return htab_up (htab_create_alloc (3,
7370 hash_type_unit_group,
7371 eq_type_unit_group,
7372 NULL, xcalloc, xfree));
7373 }
7374
7375 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7376 partial symtabs. We combine several TUs per psymtab to not let the size
7377 of any one psymtab grow too big. */
7378 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7379 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7380
7381 /* Helper routine for get_type_unit_group.
7382 Create the type_unit_group object used to hold one or more TUs. */
7383
7384 static struct type_unit_group *
7385 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7386 {
7387 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7388 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7389 struct dwarf2_per_cu_data *per_cu;
7390 struct type_unit_group *tu_group;
7391
7392 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7393 per_cu = &tu_group->per_cu;
7394 per_cu->per_bfd = per_bfd;
7395
7396 if (per_bfd->using_index)
7397 {
7398 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7399 struct dwarf2_per_cu_quick_data);
7400 }
7401 else
7402 {
7403 unsigned int line_offset = to_underlying (line_offset_struct);
7404 dwarf2_psymtab *pst;
7405 std::string name;
7406
7407 /* Give the symtab a useful name for debug purposes. */
7408 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7409 name = string_printf ("<type_units_%d>",
7410 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7411 else
7412 name = string_printf ("<type_units_at_0x%x>", line_offset);
7413
7414 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7415 pst->anonymous = true;
7416 }
7417
7418 tu_group->hash.dwo_unit = cu->dwo_unit;
7419 tu_group->hash.line_sect_off = line_offset_struct;
7420
7421 return tu_group;
7422 }
7423
7424 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7425 STMT_LIST is a DW_AT_stmt_list attribute. */
7426
7427 static struct type_unit_group *
7428 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7429 {
7430 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7431 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7432 struct type_unit_group *tu_group;
7433 void **slot;
7434 unsigned int line_offset;
7435 struct type_unit_group type_unit_group_for_lookup;
7436
7437 if (per_objfile->per_bfd->type_unit_groups == NULL)
7438 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7439
7440 /* Do we need to create a new group, or can we use an existing one? */
7441
7442 if (stmt_list)
7443 {
7444 line_offset = DW_UNSND (stmt_list);
7445 ++tu_stats->nr_symtab_sharers;
7446 }
7447 else
7448 {
7449 /* Ugh, no stmt_list. Rare, but we have to handle it.
7450 We can do various things here like create one group per TU or
7451 spread them over multiple groups to split up the expansion work.
7452 To avoid worst case scenarios (too many groups or too large groups)
7453 we, umm, group them in bunches. */
7454 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7455 | (tu_stats->nr_stmt_less_type_units
7456 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7457 ++tu_stats->nr_stmt_less_type_units;
7458 }
7459
7460 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7461 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7462 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7463 &type_unit_group_for_lookup, INSERT);
7464 if (*slot != NULL)
7465 {
7466 tu_group = (struct type_unit_group *) *slot;
7467 gdb_assert (tu_group != NULL);
7468 }
7469 else
7470 {
7471 sect_offset line_offset_struct = (sect_offset) line_offset;
7472 tu_group = create_type_unit_group (cu, line_offset_struct);
7473 *slot = tu_group;
7474 ++tu_stats->nr_symtabs;
7475 }
7476
7477 return tu_group;
7478 }
7479 \f
7480 /* Partial symbol tables. */
7481
7482 /* Create a psymtab named NAME and assign it to PER_CU.
7483
7484 The caller must fill in the following details:
7485 dirname, textlow, texthigh. */
7486
7487 static dwarf2_psymtab *
7488 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7489 dwarf2_per_objfile *per_objfile,
7490 const char *name)
7491 {
7492 struct objfile *objfile = per_objfile->objfile;
7493 dwarf2_psymtab *pst;
7494
7495 pst = new dwarf2_psymtab (name, objfile, per_cu);
7496
7497 pst->psymtabs_addrmap_supported = true;
7498
7499 /* This is the glue that links PST into GDB's symbol API. */
7500 per_cu->v.psymtab = pst;
7501
7502 return pst;
7503 }
7504
7505 /* DIE reader function for process_psymtab_comp_unit. */
7506
7507 static void
7508 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7509 const gdb_byte *info_ptr,
7510 struct die_info *comp_unit_die,
7511 enum language pretend_language)
7512 {
7513 struct dwarf2_cu *cu = reader->cu;
7514 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7515 struct objfile *objfile = per_objfile->objfile;
7516 struct gdbarch *gdbarch = objfile->arch ();
7517 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7518 CORE_ADDR baseaddr;
7519 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7520 dwarf2_psymtab *pst;
7521 enum pc_bounds_kind cu_bounds_kind;
7522 const char *filename;
7523
7524 gdb_assert (! per_cu->is_debug_types);
7525
7526 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7527
7528 /* Allocate a new partial symbol table structure. */
7529 gdb::unique_xmalloc_ptr<char> debug_filename;
7530 static const char artificial[] = "<artificial>";
7531 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7532 if (filename == NULL)
7533 filename = "";
7534 else if (strcmp (filename, artificial) == 0)
7535 {
7536 debug_filename.reset (concat (artificial, "@",
7537 sect_offset_str (per_cu->sect_off),
7538 (char *) NULL));
7539 filename = debug_filename.get ();
7540 }
7541
7542 pst = create_partial_symtab (per_cu, per_objfile, filename);
7543
7544 /* This must be done before calling dwarf2_build_include_psymtabs. */
7545 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7546
7547 baseaddr = objfile->text_section_offset ();
7548
7549 dwarf2_find_base_address (comp_unit_die, cu);
7550
7551 /* Possibly set the default values of LOWPC and HIGHPC from
7552 `DW_AT_ranges'. */
7553 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7554 &best_highpc, cu, pst);
7555 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7556 {
7557 CORE_ADDR low
7558 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7559 - baseaddr);
7560 CORE_ADDR high
7561 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7562 - baseaddr - 1);
7563 /* Store the contiguous range if it is not empty; it can be
7564 empty for CUs with no code. */
7565 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7566 low, high, pst);
7567 }
7568
7569 /* Check if comp unit has_children.
7570 If so, read the rest of the partial symbols from this comp unit.
7571 If not, there's no more debug_info for this comp unit. */
7572 if (comp_unit_die->has_children)
7573 {
7574 struct partial_die_info *first_die;
7575 CORE_ADDR lowpc, highpc;
7576
7577 lowpc = ((CORE_ADDR) -1);
7578 highpc = ((CORE_ADDR) 0);
7579
7580 first_die = load_partial_dies (reader, info_ptr, 1);
7581
7582 scan_partial_symbols (first_die, &lowpc, &highpc,
7583 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7584
7585 /* If we didn't find a lowpc, set it to highpc to avoid
7586 complaints from `maint check'. */
7587 if (lowpc == ((CORE_ADDR) -1))
7588 lowpc = highpc;
7589
7590 /* If the compilation unit didn't have an explicit address range,
7591 then use the information extracted from its child dies. */
7592 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7593 {
7594 best_lowpc = lowpc;
7595 best_highpc = highpc;
7596 }
7597 }
7598 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7599 best_lowpc + baseaddr)
7600 - baseaddr);
7601 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7602 best_highpc + baseaddr)
7603 - baseaddr);
7604
7605 end_psymtab_common (objfile, pst);
7606
7607 if (!cu->per_cu->imported_symtabs_empty ())
7608 {
7609 int i;
7610 int len = cu->per_cu->imported_symtabs_size ();
7611
7612 /* Fill in 'dependencies' here; we fill in 'users' in a
7613 post-pass. */
7614 pst->number_of_dependencies = len;
7615 pst->dependencies
7616 = objfile->partial_symtabs->allocate_dependencies (len);
7617 for (i = 0; i < len; ++i)
7618 {
7619 pst->dependencies[i]
7620 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7621 }
7622
7623 cu->per_cu->imported_symtabs_free ();
7624 }
7625
7626 /* Get the list of files included in the current compilation unit,
7627 and build a psymtab for each of them. */
7628 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7629
7630 if (dwarf_read_debug)
7631 fprintf_unfiltered (gdb_stdlog,
7632 "Psymtab for %s unit @%s: %s - %s"
7633 ", %d global, %d static syms\n",
7634 per_cu->is_debug_types ? "type" : "comp",
7635 sect_offset_str (per_cu->sect_off),
7636 paddress (gdbarch, pst->text_low (objfile)),
7637 paddress (gdbarch, pst->text_high (objfile)),
7638 pst->n_global_syms, pst->n_static_syms);
7639 }
7640
7641 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7642 Process compilation unit THIS_CU for a psymtab. */
7643
7644 static void
7645 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7646 dwarf2_per_objfile *per_objfile,
7647 bool want_partial_unit,
7648 enum language pretend_language)
7649 {
7650 /* If this compilation unit was already read in, free the
7651 cached copy in order to read it in again. This is
7652 necessary because we skipped some symbols when we first
7653 read in the compilation unit (see load_partial_dies).
7654 This problem could be avoided, but the benefit is unclear. */
7655 per_objfile->remove_cu (this_cu);
7656
7657 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7658
7659 switch (reader.comp_unit_die->tag)
7660 {
7661 case DW_TAG_compile_unit:
7662 this_cu->unit_type = DW_UT_compile;
7663 break;
7664 case DW_TAG_partial_unit:
7665 this_cu->unit_type = DW_UT_partial;
7666 break;
7667 default:
7668 abort ();
7669 }
7670
7671 if (reader.dummy_p)
7672 {
7673 /* Nothing. */
7674 }
7675 else if (this_cu->is_debug_types)
7676 build_type_psymtabs_reader (&reader, reader.info_ptr,
7677 reader.comp_unit_die);
7678 else if (want_partial_unit
7679 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7680 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7681 reader.comp_unit_die,
7682 pretend_language);
7683
7684 this_cu->lang = reader.cu->language;
7685
7686 /* Age out any secondary CUs. */
7687 per_objfile->age_comp_units ();
7688 }
7689
7690 /* Reader function for build_type_psymtabs. */
7691
7692 static void
7693 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7694 const gdb_byte *info_ptr,
7695 struct die_info *type_unit_die)
7696 {
7697 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7698 struct objfile *objfile = per_objfile->objfile;
7699 struct dwarf2_cu *cu = reader->cu;
7700 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7701 struct signatured_type *sig_type;
7702 struct type_unit_group *tu_group;
7703 struct attribute *attr;
7704 struct partial_die_info *first_die;
7705 CORE_ADDR lowpc, highpc;
7706 dwarf2_psymtab *pst;
7707
7708 gdb_assert (per_cu->is_debug_types);
7709 sig_type = (struct signatured_type *) per_cu;
7710
7711 if (! type_unit_die->has_children)
7712 return;
7713
7714 attr = type_unit_die->attr (DW_AT_stmt_list);
7715 tu_group = get_type_unit_group (cu, attr);
7716
7717 if (tu_group->tus == nullptr)
7718 tu_group->tus = new std::vector<signatured_type *>;
7719 tu_group->tus->push_back (sig_type);
7720
7721 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7722 pst = create_partial_symtab (per_cu, per_objfile, "");
7723 pst->anonymous = true;
7724
7725 first_die = load_partial_dies (reader, info_ptr, 1);
7726
7727 lowpc = (CORE_ADDR) -1;
7728 highpc = (CORE_ADDR) 0;
7729 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7730
7731 end_psymtab_common (objfile, pst);
7732 }
7733
7734 /* Struct used to sort TUs by their abbreviation table offset. */
7735
7736 struct tu_abbrev_offset
7737 {
7738 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7739 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7740 {}
7741
7742 signatured_type *sig_type;
7743 sect_offset abbrev_offset;
7744 };
7745
7746 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7747
7748 static bool
7749 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7750 const struct tu_abbrev_offset &b)
7751 {
7752 return a.abbrev_offset < b.abbrev_offset;
7753 }
7754
7755 /* Efficiently read all the type units.
7756 This does the bulk of the work for build_type_psymtabs.
7757
7758 The efficiency is because we sort TUs by the abbrev table they use and
7759 only read each abbrev table once. In one program there are 200K TUs
7760 sharing 8K abbrev tables.
7761
7762 The main purpose of this function is to support building the
7763 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7764 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7765 can collapse the search space by grouping them by stmt_list.
7766 The savings can be significant, in the same program from above the 200K TUs
7767 share 8K stmt_list tables.
7768
7769 FUNC is expected to call get_type_unit_group, which will create the
7770 struct type_unit_group if necessary and add it to
7771 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7772
7773 static void
7774 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7775 {
7776 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7777 abbrev_table_up abbrev_table;
7778 sect_offset abbrev_offset;
7779
7780 /* It's up to the caller to not call us multiple times. */
7781 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7782
7783 if (per_objfile->per_bfd->all_type_units.empty ())
7784 return;
7785
7786 /* TUs typically share abbrev tables, and there can be way more TUs than
7787 abbrev tables. Sort by abbrev table to reduce the number of times we
7788 read each abbrev table in.
7789 Alternatives are to punt or to maintain a cache of abbrev tables.
7790 This is simpler and efficient enough for now.
7791
7792 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7793 symtab to use). Typically TUs with the same abbrev offset have the same
7794 stmt_list value too so in practice this should work well.
7795
7796 The basic algorithm here is:
7797
7798 sort TUs by abbrev table
7799 for each TU with same abbrev table:
7800 read abbrev table if first user
7801 read TU top level DIE
7802 [IWBN if DWO skeletons had DW_AT_stmt_list]
7803 call FUNC */
7804
7805 if (dwarf_read_debug)
7806 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7807
7808 /* Sort in a separate table to maintain the order of all_type_units
7809 for .gdb_index: TU indices directly index all_type_units. */
7810 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7811 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7812
7813 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7814 sorted_by_abbrev.emplace_back
7815 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7816 sig_type->per_cu.sect_off));
7817
7818 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7819 sort_tu_by_abbrev_offset);
7820
7821 abbrev_offset = (sect_offset) ~(unsigned) 0;
7822
7823 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7824 {
7825 /* Switch to the next abbrev table if necessary. */
7826 if (abbrev_table == NULL
7827 || tu.abbrev_offset != abbrev_offset)
7828 {
7829 abbrev_offset = tu.abbrev_offset;
7830 abbrev_table =
7831 abbrev_table::read (per_objfile->objfile,
7832 &per_objfile->per_bfd->abbrev, abbrev_offset);
7833 ++tu_stats->nr_uniq_abbrev_tables;
7834 }
7835
7836 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7837 abbrev_table.get (), nullptr, false);
7838 if (!reader.dummy_p)
7839 build_type_psymtabs_reader (&reader, reader.info_ptr,
7840 reader.comp_unit_die);
7841 }
7842 }
7843
7844 /* Print collected type unit statistics. */
7845
7846 static void
7847 print_tu_stats (dwarf2_per_objfile *per_objfile)
7848 {
7849 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7850
7851 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7852 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7853 per_objfile->per_bfd->all_type_units.size ());
7854 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7855 tu_stats->nr_uniq_abbrev_tables);
7856 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7857 tu_stats->nr_symtabs);
7858 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7859 tu_stats->nr_symtab_sharers);
7860 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7861 tu_stats->nr_stmt_less_type_units);
7862 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7863 tu_stats->nr_all_type_units_reallocs);
7864 }
7865
7866 /* Traversal function for build_type_psymtabs. */
7867
7868 static int
7869 build_type_psymtab_dependencies (void **slot, void *info)
7870 {
7871 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7872 struct objfile *objfile = per_objfile->objfile;
7873 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7874 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7875 dwarf2_psymtab *pst = per_cu->v.psymtab;
7876 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7877 int i;
7878
7879 gdb_assert (len > 0);
7880 gdb_assert (per_cu->type_unit_group_p ());
7881
7882 pst->number_of_dependencies = len;
7883 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7884 for (i = 0; i < len; ++i)
7885 {
7886 struct signatured_type *iter = tu_group->tus->at (i);
7887 gdb_assert (iter->per_cu.is_debug_types);
7888 pst->dependencies[i] = iter->per_cu.v.psymtab;
7889 iter->type_unit_group = tu_group;
7890 }
7891
7892 delete tu_group->tus;
7893 tu_group->tus = nullptr;
7894
7895 return 1;
7896 }
7897
7898 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7899 Build partial symbol tables for the .debug_types comp-units. */
7900
7901 static void
7902 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7903 {
7904 if (! create_all_type_units (per_objfile))
7905 return;
7906
7907 build_type_psymtabs_1 (per_objfile);
7908 }
7909
7910 /* Traversal function for process_skeletonless_type_unit.
7911 Read a TU in a DWO file and build partial symbols for it. */
7912
7913 static int
7914 process_skeletonless_type_unit (void **slot, void *info)
7915 {
7916 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7917 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7918 struct signatured_type find_entry, *entry;
7919
7920 /* If this TU doesn't exist in the global table, add it and read it in. */
7921
7922 if (per_objfile->per_bfd->signatured_types == NULL)
7923 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7924
7925 find_entry.signature = dwo_unit->signature;
7926 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7927 &find_entry, INSERT);
7928 /* If we've already seen this type there's nothing to do. What's happening
7929 is we're doing our own version of comdat-folding here. */
7930 if (*slot != NULL)
7931 return 1;
7932
7933 /* This does the job that create_all_type_units would have done for
7934 this TU. */
7935 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7936 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7937 *slot = entry;
7938
7939 /* This does the job that build_type_psymtabs_1 would have done. */
7940 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7941 if (!reader.dummy_p)
7942 build_type_psymtabs_reader (&reader, reader.info_ptr,
7943 reader.comp_unit_die);
7944
7945 return 1;
7946 }
7947
7948 /* Traversal function for process_skeletonless_type_units. */
7949
7950 static int
7951 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7952 {
7953 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7954
7955 if (dwo_file->tus != NULL)
7956 htab_traverse_noresize (dwo_file->tus.get (),
7957 process_skeletonless_type_unit, info);
7958
7959 return 1;
7960 }
7961
7962 /* Scan all TUs of DWO files, verifying we've processed them.
7963 This is needed in case a TU was emitted without its skeleton.
7964 Note: This can't be done until we know what all the DWO files are. */
7965
7966 static void
7967 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7968 {
7969 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7970 if (get_dwp_file (per_objfile) == NULL
7971 && per_objfile->per_bfd->dwo_files != NULL)
7972 {
7973 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7974 process_dwo_file_for_skeletonless_type_units,
7975 per_objfile);
7976 }
7977 }
7978
7979 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7980
7981 static void
7982 set_partial_user (dwarf2_per_objfile *per_objfile)
7983 {
7984 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
7985 {
7986 dwarf2_psymtab *pst = per_cu->v.psymtab;
7987
7988 if (pst == NULL)
7989 continue;
7990
7991 for (int j = 0; j < pst->number_of_dependencies; ++j)
7992 {
7993 /* Set the 'user' field only if it is not already set. */
7994 if (pst->dependencies[j]->user == NULL)
7995 pst->dependencies[j]->user = pst;
7996 }
7997 }
7998 }
7999
8000 /* Build the partial symbol table by doing a quick pass through the
8001 .debug_info and .debug_abbrev sections. */
8002
8003 static void
8004 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8005 {
8006 struct objfile *objfile = per_objfile->objfile;
8007
8008 if (dwarf_read_debug)
8009 {
8010 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8011 objfile_name (objfile));
8012 }
8013
8014 scoped_restore restore_reading_psyms
8015 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8016 true);
8017
8018 per_objfile->per_bfd->info.read (objfile);
8019
8020 /* Any cached compilation units will be linked by the per-objfile
8021 read_in_chain. Make sure to free them when we're done. */
8022 free_cached_comp_units freer (per_objfile);
8023
8024 build_type_psymtabs (per_objfile);
8025
8026 create_all_comp_units (per_objfile);
8027
8028 /* Create a temporary address map on a temporary obstack. We later
8029 copy this to the final obstack. */
8030 auto_obstack temp_obstack;
8031
8032 scoped_restore save_psymtabs_addrmap
8033 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8034 addrmap_create_mutable (&temp_obstack));
8035
8036 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8037 {
8038 if (per_cu->v.psymtab != NULL)
8039 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8040 continue;
8041 process_psymtab_comp_unit (per_cu, per_objfile, false,
8042 language_minimal);
8043 }
8044
8045 /* This has to wait until we read the CUs, we need the list of DWOs. */
8046 process_skeletonless_type_units (per_objfile);
8047
8048 /* Now that all TUs have been processed we can fill in the dependencies. */
8049 if (per_objfile->per_bfd->type_unit_groups != NULL)
8050 {
8051 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8052 build_type_psymtab_dependencies, per_objfile);
8053 }
8054
8055 if (dwarf_read_debug)
8056 print_tu_stats (per_objfile);
8057
8058 set_partial_user (per_objfile);
8059
8060 objfile->partial_symtabs->psymtabs_addrmap
8061 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8062 objfile->partial_symtabs->obstack ());
8063 /* At this point we want to keep the address map. */
8064 save_psymtabs_addrmap.release ();
8065
8066 if (dwarf_read_debug)
8067 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8068 objfile_name (objfile));
8069 }
8070
8071 /* Load the partial DIEs for a secondary CU into memory.
8072 This is also used when rereading a primary CU with load_all_dies. */
8073
8074 static void
8075 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8076 dwarf2_per_objfile *per_objfile,
8077 dwarf2_cu *existing_cu)
8078 {
8079 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8080
8081 if (!reader.dummy_p)
8082 {
8083 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8084 language_minimal);
8085
8086 /* Check if comp unit has_children.
8087 If so, read the rest of the partial symbols from this comp unit.
8088 If not, there's no more debug_info for this comp unit. */
8089 if (reader.comp_unit_die->has_children)
8090 load_partial_dies (&reader, reader.info_ptr, 0);
8091
8092 reader.keep ();
8093 }
8094 }
8095
8096 static void
8097 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8098 struct dwarf2_section_info *section,
8099 struct dwarf2_section_info *abbrev_section,
8100 unsigned int is_dwz)
8101 {
8102 const gdb_byte *info_ptr;
8103 struct objfile *objfile = per_objfile->objfile;
8104
8105 if (dwarf_read_debug)
8106 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8107 section->get_name (),
8108 section->get_file_name ());
8109
8110 section->read (objfile);
8111
8112 info_ptr = section->buffer;
8113
8114 while (info_ptr < section->buffer + section->size)
8115 {
8116 struct dwarf2_per_cu_data *this_cu;
8117
8118 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8119
8120 comp_unit_head cu_header;
8121 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8122 abbrev_section, info_ptr,
8123 rcuh_kind::COMPILE);
8124
8125 /* Save the compilation unit for later lookup. */
8126 if (cu_header.unit_type != DW_UT_type)
8127 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8128 else
8129 {
8130 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8131 sig_type->signature = cu_header.signature;
8132 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8133 this_cu = &sig_type->per_cu;
8134 }
8135 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8136 this_cu->sect_off = sect_off;
8137 this_cu->length = cu_header.length + cu_header.initial_length_size;
8138 this_cu->is_dwz = is_dwz;
8139 this_cu->section = section;
8140
8141 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8142
8143 info_ptr = info_ptr + this_cu->length;
8144 }
8145 }
8146
8147 /* Create a list of all compilation units in OBJFILE.
8148 This is only done for -readnow and building partial symtabs. */
8149
8150 static void
8151 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8152 {
8153 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8154 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8155 &per_objfile->per_bfd->abbrev, 0);
8156
8157 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8158 if (dwz != NULL)
8159 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8160 }
8161
8162 /* Process all loaded DIEs for compilation unit CU, starting at
8163 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8164 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8165 DW_AT_ranges). See the comments of add_partial_subprogram on how
8166 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8167
8168 static void
8169 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8170 CORE_ADDR *highpc, int set_addrmap,
8171 struct dwarf2_cu *cu)
8172 {
8173 struct partial_die_info *pdi;
8174
8175 /* Now, march along the PDI's, descending into ones which have
8176 interesting children but skipping the children of the other ones,
8177 until we reach the end of the compilation unit. */
8178
8179 pdi = first_die;
8180
8181 while (pdi != NULL)
8182 {
8183 pdi->fixup (cu);
8184
8185 /* Anonymous namespaces or modules have no name but have interesting
8186 children, so we need to look at them. Ditto for anonymous
8187 enums. */
8188
8189 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8190 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8191 || pdi->tag == DW_TAG_imported_unit
8192 || pdi->tag == DW_TAG_inlined_subroutine)
8193 {
8194 switch (pdi->tag)
8195 {
8196 case DW_TAG_subprogram:
8197 case DW_TAG_inlined_subroutine:
8198 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8199 if (cu->language == language_cplus)
8200 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8201 set_addrmap, cu);
8202 break;
8203 case DW_TAG_constant:
8204 case DW_TAG_variable:
8205 case DW_TAG_typedef:
8206 case DW_TAG_union_type:
8207 if (!pdi->is_declaration
8208 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8209 {
8210 add_partial_symbol (pdi, cu);
8211 }
8212 break;
8213 case DW_TAG_class_type:
8214 case DW_TAG_interface_type:
8215 case DW_TAG_structure_type:
8216 if (!pdi->is_declaration)
8217 {
8218 add_partial_symbol (pdi, cu);
8219 }
8220 if ((cu->language == language_rust
8221 || cu->language == language_cplus) && pdi->has_children)
8222 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8223 set_addrmap, cu);
8224 break;
8225 case DW_TAG_enumeration_type:
8226 if (!pdi->is_declaration)
8227 add_partial_enumeration (pdi, cu);
8228 break;
8229 case DW_TAG_base_type:
8230 case DW_TAG_subrange_type:
8231 /* File scope base type definitions are added to the partial
8232 symbol table. */
8233 add_partial_symbol (pdi, cu);
8234 break;
8235 case DW_TAG_namespace:
8236 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8237 break;
8238 case DW_TAG_module:
8239 if (!pdi->is_declaration)
8240 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8241 break;
8242 case DW_TAG_imported_unit:
8243 {
8244 struct dwarf2_per_cu_data *per_cu;
8245
8246 /* For now we don't handle imported units in type units. */
8247 if (cu->per_cu->is_debug_types)
8248 {
8249 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8250 " supported in type units [in module %s]"),
8251 objfile_name (cu->per_objfile->objfile));
8252 }
8253
8254 per_cu = dwarf2_find_containing_comp_unit
8255 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8256
8257 /* Go read the partial unit, if needed. */
8258 if (per_cu->v.psymtab == NULL)
8259 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8260 cu->language);
8261
8262 cu->per_cu->imported_symtabs_push (per_cu);
8263 }
8264 break;
8265 case DW_TAG_imported_declaration:
8266 add_partial_symbol (pdi, cu);
8267 break;
8268 default:
8269 break;
8270 }
8271 }
8272
8273 /* If the die has a sibling, skip to the sibling. */
8274
8275 pdi = pdi->die_sibling;
8276 }
8277 }
8278
8279 /* Functions used to compute the fully scoped name of a partial DIE.
8280
8281 Normally, this is simple. For C++, the parent DIE's fully scoped
8282 name is concatenated with "::" and the partial DIE's name.
8283 Enumerators are an exception; they use the scope of their parent
8284 enumeration type, i.e. the name of the enumeration type is not
8285 prepended to the enumerator.
8286
8287 There are two complexities. One is DW_AT_specification; in this
8288 case "parent" means the parent of the target of the specification,
8289 instead of the direct parent of the DIE. The other is compilers
8290 which do not emit DW_TAG_namespace; in this case we try to guess
8291 the fully qualified name of structure types from their members'
8292 linkage names. This must be done using the DIE's children rather
8293 than the children of any DW_AT_specification target. We only need
8294 to do this for structures at the top level, i.e. if the target of
8295 any DW_AT_specification (if any; otherwise the DIE itself) does not
8296 have a parent. */
8297
8298 /* Compute the scope prefix associated with PDI's parent, in
8299 compilation unit CU. The result will be allocated on CU's
8300 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8301 field. NULL is returned if no prefix is necessary. */
8302 static const char *
8303 partial_die_parent_scope (struct partial_die_info *pdi,
8304 struct dwarf2_cu *cu)
8305 {
8306 const char *grandparent_scope;
8307 struct partial_die_info *parent, *real_pdi;
8308
8309 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8310 then this means the parent of the specification DIE. */
8311
8312 real_pdi = pdi;
8313 while (real_pdi->has_specification)
8314 {
8315 auto res = find_partial_die (real_pdi->spec_offset,
8316 real_pdi->spec_is_dwz, cu);
8317 real_pdi = res.pdi;
8318 cu = res.cu;
8319 }
8320
8321 parent = real_pdi->die_parent;
8322 if (parent == NULL)
8323 return NULL;
8324
8325 if (parent->scope_set)
8326 return parent->scope;
8327
8328 parent->fixup (cu);
8329
8330 grandparent_scope = partial_die_parent_scope (parent, cu);
8331
8332 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8333 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8334 Work around this problem here. */
8335 if (cu->language == language_cplus
8336 && parent->tag == DW_TAG_namespace
8337 && strcmp (parent->name (cu), "::") == 0
8338 && grandparent_scope == NULL)
8339 {
8340 parent->scope = NULL;
8341 parent->scope_set = 1;
8342 return NULL;
8343 }
8344
8345 /* Nested subroutines in Fortran get a prefix. */
8346 if (pdi->tag == DW_TAG_enumerator)
8347 /* Enumerators should not get the name of the enumeration as a prefix. */
8348 parent->scope = grandparent_scope;
8349 else if (parent->tag == DW_TAG_namespace
8350 || parent->tag == DW_TAG_module
8351 || parent->tag == DW_TAG_structure_type
8352 || parent->tag == DW_TAG_class_type
8353 || parent->tag == DW_TAG_interface_type
8354 || parent->tag == DW_TAG_union_type
8355 || parent->tag == DW_TAG_enumeration_type
8356 || (cu->language == language_fortran
8357 && parent->tag == DW_TAG_subprogram
8358 && pdi->tag == DW_TAG_subprogram))
8359 {
8360 if (grandparent_scope == NULL)
8361 parent->scope = parent->name (cu);
8362 else
8363 parent->scope = typename_concat (&cu->comp_unit_obstack,
8364 grandparent_scope,
8365 parent->name (cu), 0, cu);
8366 }
8367 else
8368 {
8369 /* FIXME drow/2004-04-01: What should we be doing with
8370 function-local names? For partial symbols, we should probably be
8371 ignoring them. */
8372 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8373 dwarf_tag_name (parent->tag),
8374 sect_offset_str (pdi->sect_off));
8375 parent->scope = grandparent_scope;
8376 }
8377
8378 parent->scope_set = 1;
8379 return parent->scope;
8380 }
8381
8382 /* Return the fully scoped name associated with PDI, from compilation unit
8383 CU. The result will be allocated with malloc. */
8384
8385 static gdb::unique_xmalloc_ptr<char>
8386 partial_die_full_name (struct partial_die_info *pdi,
8387 struct dwarf2_cu *cu)
8388 {
8389 const char *parent_scope;
8390
8391 /* If this is a template instantiation, we can not work out the
8392 template arguments from partial DIEs. So, unfortunately, we have
8393 to go through the full DIEs. At least any work we do building
8394 types here will be reused if full symbols are loaded later. */
8395 if (pdi->has_template_arguments)
8396 {
8397 pdi->fixup (cu);
8398
8399 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8400 {
8401 struct die_info *die;
8402 struct attribute attr;
8403 struct dwarf2_cu *ref_cu = cu;
8404
8405 /* DW_FORM_ref_addr is using section offset. */
8406 attr.name = (enum dwarf_attribute) 0;
8407 attr.form = DW_FORM_ref_addr;
8408 attr.u.unsnd = to_underlying (pdi->sect_off);
8409 die = follow_die_ref (NULL, &attr, &ref_cu);
8410
8411 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8412 }
8413 }
8414
8415 parent_scope = partial_die_parent_scope (pdi, cu);
8416 if (parent_scope == NULL)
8417 return NULL;
8418 else
8419 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8420 pdi->name (cu),
8421 0, cu));
8422 }
8423
8424 static void
8425 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8426 {
8427 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8428 struct objfile *objfile = per_objfile->objfile;
8429 struct gdbarch *gdbarch = objfile->arch ();
8430 CORE_ADDR addr = 0;
8431 const char *actual_name = NULL;
8432 CORE_ADDR baseaddr;
8433
8434 baseaddr = objfile->text_section_offset ();
8435
8436 gdb::unique_xmalloc_ptr<char> built_actual_name
8437 = partial_die_full_name (pdi, cu);
8438 if (built_actual_name != NULL)
8439 actual_name = built_actual_name.get ();
8440
8441 if (actual_name == NULL)
8442 actual_name = pdi->name (cu);
8443
8444 partial_symbol psymbol;
8445 memset (&psymbol, 0, sizeof (psymbol));
8446 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8447 psymbol.ginfo.section = -1;
8448
8449 /* The code below indicates that the psymbol should be installed by
8450 setting this. */
8451 gdb::optional<psymbol_placement> where;
8452
8453 switch (pdi->tag)
8454 {
8455 case DW_TAG_inlined_subroutine:
8456 case DW_TAG_subprogram:
8457 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8458 - baseaddr);
8459 if (pdi->is_external
8460 || cu->language == language_ada
8461 || (cu->language == language_fortran
8462 && pdi->die_parent != NULL
8463 && pdi->die_parent->tag == DW_TAG_subprogram))
8464 {
8465 /* Normally, only "external" DIEs are part of the global scope.
8466 But in Ada and Fortran, we want to be able to access nested
8467 procedures globally. So all Ada and Fortran subprograms are
8468 stored in the global scope. */
8469 where = psymbol_placement::GLOBAL;
8470 }
8471 else
8472 where = psymbol_placement::STATIC;
8473
8474 psymbol.domain = VAR_DOMAIN;
8475 psymbol.aclass = LOC_BLOCK;
8476 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8477 psymbol.ginfo.value.address = addr;
8478
8479 if (pdi->main_subprogram && actual_name != NULL)
8480 set_objfile_main_name (objfile, actual_name, cu->language);
8481 break;
8482 case DW_TAG_constant:
8483 psymbol.domain = VAR_DOMAIN;
8484 psymbol.aclass = LOC_STATIC;
8485 where = (pdi->is_external
8486 ? psymbol_placement::GLOBAL
8487 : psymbol_placement::STATIC);
8488 break;
8489 case DW_TAG_variable:
8490 if (pdi->d.locdesc)
8491 addr = decode_locdesc (pdi->d.locdesc, cu);
8492
8493 if (pdi->d.locdesc
8494 && addr == 0
8495 && !per_objfile->per_bfd->has_section_at_zero)
8496 {
8497 /* A global or static variable may also have been stripped
8498 out by the linker if unused, in which case its address
8499 will be nullified; do not add such variables into partial
8500 symbol table then. */
8501 }
8502 else if (pdi->is_external)
8503 {
8504 /* Global Variable.
8505 Don't enter into the minimal symbol tables as there is
8506 a minimal symbol table entry from the ELF symbols already.
8507 Enter into partial symbol table if it has a location
8508 descriptor or a type.
8509 If the location descriptor is missing, new_symbol will create
8510 a LOC_UNRESOLVED symbol, the address of the variable will then
8511 be determined from the minimal symbol table whenever the variable
8512 is referenced.
8513 The address for the partial symbol table entry is not
8514 used by GDB, but it comes in handy for debugging partial symbol
8515 table building. */
8516
8517 if (pdi->d.locdesc || pdi->has_type)
8518 {
8519 psymbol.domain = VAR_DOMAIN;
8520 psymbol.aclass = LOC_STATIC;
8521 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8522 psymbol.ginfo.value.address = addr;
8523 where = psymbol_placement::GLOBAL;
8524 }
8525 }
8526 else
8527 {
8528 int has_loc = pdi->d.locdesc != NULL;
8529
8530 /* Static Variable. Skip symbols whose value we cannot know (those
8531 without location descriptors or constant values). */
8532 if (!has_loc && !pdi->has_const_value)
8533 return;
8534
8535 psymbol.domain = VAR_DOMAIN;
8536 psymbol.aclass = LOC_STATIC;
8537 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8538 if (has_loc)
8539 psymbol.ginfo.value.address = addr;
8540 where = psymbol_placement::STATIC;
8541 }
8542 break;
8543 case DW_TAG_typedef:
8544 case DW_TAG_base_type:
8545 case DW_TAG_subrange_type:
8546 psymbol.domain = VAR_DOMAIN;
8547 psymbol.aclass = LOC_TYPEDEF;
8548 where = psymbol_placement::STATIC;
8549 break;
8550 case DW_TAG_imported_declaration:
8551 case DW_TAG_namespace:
8552 psymbol.domain = VAR_DOMAIN;
8553 psymbol.aclass = LOC_TYPEDEF;
8554 where = psymbol_placement::GLOBAL;
8555 break;
8556 case DW_TAG_module:
8557 /* With Fortran 77 there might be a "BLOCK DATA" module
8558 available without any name. If so, we skip the module as it
8559 doesn't bring any value. */
8560 if (actual_name != nullptr)
8561 {
8562 psymbol.domain = MODULE_DOMAIN;
8563 psymbol.aclass = LOC_TYPEDEF;
8564 where = psymbol_placement::GLOBAL;
8565 }
8566 break;
8567 case DW_TAG_class_type:
8568 case DW_TAG_interface_type:
8569 case DW_TAG_structure_type:
8570 case DW_TAG_union_type:
8571 case DW_TAG_enumeration_type:
8572 /* Skip external references. The DWARF standard says in the section
8573 about "Structure, Union, and Class Type Entries": "An incomplete
8574 structure, union or class type is represented by a structure,
8575 union or class entry that does not have a byte size attribute
8576 and that has a DW_AT_declaration attribute." */
8577 if (!pdi->has_byte_size && pdi->is_declaration)
8578 return;
8579
8580 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8581 static vs. global. */
8582 psymbol.domain = STRUCT_DOMAIN;
8583 psymbol.aclass = LOC_TYPEDEF;
8584 where = (cu->language == language_cplus
8585 ? psymbol_placement::GLOBAL
8586 : psymbol_placement::STATIC);
8587 break;
8588 case DW_TAG_enumerator:
8589 psymbol.domain = VAR_DOMAIN;
8590 psymbol.aclass = LOC_CONST;
8591 where = (cu->language == language_cplus
8592 ? psymbol_placement::GLOBAL
8593 : psymbol_placement::STATIC);
8594 break;
8595 default:
8596 break;
8597 }
8598
8599 if (where.has_value ())
8600 {
8601 if (built_actual_name != nullptr)
8602 actual_name = objfile->intern (actual_name);
8603 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8604 psymbol.ginfo.set_linkage_name (actual_name);
8605 else
8606 {
8607 psymbol.ginfo.set_demangled_name (actual_name,
8608 &objfile->objfile_obstack);
8609 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8610 }
8611 add_psymbol_to_list (psymbol, *where, objfile);
8612 }
8613 }
8614
8615 /* Read a partial die corresponding to a namespace; also, add a symbol
8616 corresponding to that namespace to the symbol table. NAMESPACE is
8617 the name of the enclosing namespace. */
8618
8619 static void
8620 add_partial_namespace (struct partial_die_info *pdi,
8621 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8622 int set_addrmap, struct dwarf2_cu *cu)
8623 {
8624 /* Add a symbol for the namespace. */
8625
8626 add_partial_symbol (pdi, cu);
8627
8628 /* Now scan partial symbols in that namespace. */
8629
8630 if (pdi->has_children)
8631 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8632 }
8633
8634 /* Read a partial die corresponding to a Fortran module. */
8635
8636 static void
8637 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8638 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8639 {
8640 /* Add a symbol for the namespace. */
8641
8642 add_partial_symbol (pdi, cu);
8643
8644 /* Now scan partial symbols in that module. */
8645
8646 if (pdi->has_children)
8647 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8648 }
8649
8650 /* Read a partial die corresponding to a subprogram or an inlined
8651 subprogram and create a partial symbol for that subprogram.
8652 When the CU language allows it, this routine also defines a partial
8653 symbol for each nested subprogram that this subprogram contains.
8654 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8655 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8656
8657 PDI may also be a lexical block, in which case we simply search
8658 recursively for subprograms defined inside that lexical block.
8659 Again, this is only performed when the CU language allows this
8660 type of definitions. */
8661
8662 static void
8663 add_partial_subprogram (struct partial_die_info *pdi,
8664 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8665 int set_addrmap, struct dwarf2_cu *cu)
8666 {
8667 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8668 {
8669 if (pdi->has_pc_info)
8670 {
8671 if (pdi->lowpc < *lowpc)
8672 *lowpc = pdi->lowpc;
8673 if (pdi->highpc > *highpc)
8674 *highpc = pdi->highpc;
8675 if (set_addrmap)
8676 {
8677 struct objfile *objfile = cu->per_objfile->objfile;
8678 struct gdbarch *gdbarch = objfile->arch ();
8679 CORE_ADDR baseaddr;
8680 CORE_ADDR this_highpc;
8681 CORE_ADDR this_lowpc;
8682
8683 baseaddr = objfile->text_section_offset ();
8684 this_lowpc
8685 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8686 pdi->lowpc + baseaddr)
8687 - baseaddr);
8688 this_highpc
8689 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8690 pdi->highpc + baseaddr)
8691 - baseaddr);
8692 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8693 this_lowpc, this_highpc - 1,
8694 cu->per_cu->v.psymtab);
8695 }
8696 }
8697
8698 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8699 {
8700 if (!pdi->is_declaration)
8701 /* Ignore subprogram DIEs that do not have a name, they are
8702 illegal. Do not emit a complaint at this point, we will
8703 do so when we convert this psymtab into a symtab. */
8704 if (pdi->name (cu))
8705 add_partial_symbol (pdi, cu);
8706 }
8707 }
8708
8709 if (! pdi->has_children)
8710 return;
8711
8712 if (cu->language == language_ada || cu->language == language_fortran)
8713 {
8714 pdi = pdi->die_child;
8715 while (pdi != NULL)
8716 {
8717 pdi->fixup (cu);
8718 if (pdi->tag == DW_TAG_subprogram
8719 || pdi->tag == DW_TAG_inlined_subroutine
8720 || pdi->tag == DW_TAG_lexical_block)
8721 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8722 pdi = pdi->die_sibling;
8723 }
8724 }
8725 }
8726
8727 /* Read a partial die corresponding to an enumeration type. */
8728
8729 static void
8730 add_partial_enumeration (struct partial_die_info *enum_pdi,
8731 struct dwarf2_cu *cu)
8732 {
8733 struct partial_die_info *pdi;
8734
8735 if (enum_pdi->name (cu) != NULL)
8736 add_partial_symbol (enum_pdi, cu);
8737
8738 pdi = enum_pdi->die_child;
8739 while (pdi)
8740 {
8741 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8742 complaint (_("malformed enumerator DIE ignored"));
8743 else
8744 add_partial_symbol (pdi, cu);
8745 pdi = pdi->die_sibling;
8746 }
8747 }
8748
8749 /* Return the initial uleb128 in the die at INFO_PTR. */
8750
8751 static unsigned int
8752 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8753 {
8754 unsigned int bytes_read;
8755
8756 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8757 }
8758
8759 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8760 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8761
8762 Return the corresponding abbrev, or NULL if the number is zero (indicating
8763 an empty DIE). In either case *BYTES_READ will be set to the length of
8764 the initial number. */
8765
8766 static struct abbrev_info *
8767 peek_die_abbrev (const die_reader_specs &reader,
8768 const gdb_byte *info_ptr, unsigned int *bytes_read)
8769 {
8770 dwarf2_cu *cu = reader.cu;
8771 bfd *abfd = cu->per_objfile->objfile->obfd;
8772 unsigned int abbrev_number
8773 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8774
8775 if (abbrev_number == 0)
8776 return NULL;
8777
8778 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8779 if (!abbrev)
8780 {
8781 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8782 " at offset %s [in module %s]"),
8783 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8784 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8785 }
8786
8787 return abbrev;
8788 }
8789
8790 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8791 Returns a pointer to the end of a series of DIEs, terminated by an empty
8792 DIE. Any children of the skipped DIEs will also be skipped. */
8793
8794 static const gdb_byte *
8795 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8796 {
8797 while (1)
8798 {
8799 unsigned int bytes_read;
8800 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8801
8802 if (abbrev == NULL)
8803 return info_ptr + bytes_read;
8804 else
8805 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8806 }
8807 }
8808
8809 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8810 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8811 abbrev corresponding to that skipped uleb128 should be passed in
8812 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8813 children. */
8814
8815 static const gdb_byte *
8816 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8817 struct abbrev_info *abbrev)
8818 {
8819 unsigned int bytes_read;
8820 struct attribute attr;
8821 bfd *abfd = reader->abfd;
8822 struct dwarf2_cu *cu = reader->cu;
8823 const gdb_byte *buffer = reader->buffer;
8824 const gdb_byte *buffer_end = reader->buffer_end;
8825 unsigned int form, i;
8826
8827 for (i = 0; i < abbrev->num_attrs; i++)
8828 {
8829 /* The only abbrev we care about is DW_AT_sibling. */
8830 if (abbrev->attrs[i].name == DW_AT_sibling)
8831 {
8832 bool ignored;
8833 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8834 &ignored);
8835 if (attr.form == DW_FORM_ref_addr)
8836 complaint (_("ignoring absolute DW_AT_sibling"));
8837 else
8838 {
8839 sect_offset off = attr.get_ref_die_offset ();
8840 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8841
8842 if (sibling_ptr < info_ptr)
8843 complaint (_("DW_AT_sibling points backwards"));
8844 else if (sibling_ptr > reader->buffer_end)
8845 reader->die_section->overflow_complaint ();
8846 else
8847 return sibling_ptr;
8848 }
8849 }
8850
8851 /* If it isn't DW_AT_sibling, skip this attribute. */
8852 form = abbrev->attrs[i].form;
8853 skip_attribute:
8854 switch (form)
8855 {
8856 case DW_FORM_ref_addr:
8857 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8858 and later it is offset sized. */
8859 if (cu->header.version == 2)
8860 info_ptr += cu->header.addr_size;
8861 else
8862 info_ptr += cu->header.offset_size;
8863 break;
8864 case DW_FORM_GNU_ref_alt:
8865 info_ptr += cu->header.offset_size;
8866 break;
8867 case DW_FORM_addr:
8868 info_ptr += cu->header.addr_size;
8869 break;
8870 case DW_FORM_data1:
8871 case DW_FORM_ref1:
8872 case DW_FORM_flag:
8873 case DW_FORM_strx1:
8874 info_ptr += 1;
8875 break;
8876 case DW_FORM_flag_present:
8877 case DW_FORM_implicit_const:
8878 break;
8879 case DW_FORM_data2:
8880 case DW_FORM_ref2:
8881 case DW_FORM_strx2:
8882 info_ptr += 2;
8883 break;
8884 case DW_FORM_strx3:
8885 info_ptr += 3;
8886 break;
8887 case DW_FORM_data4:
8888 case DW_FORM_ref4:
8889 case DW_FORM_strx4:
8890 info_ptr += 4;
8891 break;
8892 case DW_FORM_data8:
8893 case DW_FORM_ref8:
8894 case DW_FORM_ref_sig8:
8895 info_ptr += 8;
8896 break;
8897 case DW_FORM_data16:
8898 info_ptr += 16;
8899 break;
8900 case DW_FORM_string:
8901 read_direct_string (abfd, info_ptr, &bytes_read);
8902 info_ptr += bytes_read;
8903 break;
8904 case DW_FORM_sec_offset:
8905 case DW_FORM_strp:
8906 case DW_FORM_GNU_strp_alt:
8907 info_ptr += cu->header.offset_size;
8908 break;
8909 case DW_FORM_exprloc:
8910 case DW_FORM_block:
8911 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8912 info_ptr += bytes_read;
8913 break;
8914 case DW_FORM_block1:
8915 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8916 break;
8917 case DW_FORM_block2:
8918 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8919 break;
8920 case DW_FORM_block4:
8921 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8922 break;
8923 case DW_FORM_addrx:
8924 case DW_FORM_strx:
8925 case DW_FORM_sdata:
8926 case DW_FORM_udata:
8927 case DW_FORM_ref_udata:
8928 case DW_FORM_GNU_addr_index:
8929 case DW_FORM_GNU_str_index:
8930 case DW_FORM_rnglistx:
8931 case DW_FORM_loclistx:
8932 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8933 break;
8934 case DW_FORM_indirect:
8935 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8936 info_ptr += bytes_read;
8937 /* We need to continue parsing from here, so just go back to
8938 the top. */
8939 goto skip_attribute;
8940
8941 default:
8942 error (_("Dwarf Error: Cannot handle %s "
8943 "in DWARF reader [in module %s]"),
8944 dwarf_form_name (form),
8945 bfd_get_filename (abfd));
8946 }
8947 }
8948
8949 if (abbrev->has_children)
8950 return skip_children (reader, info_ptr);
8951 else
8952 return info_ptr;
8953 }
8954
8955 /* Locate ORIG_PDI's sibling.
8956 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8957
8958 static const gdb_byte *
8959 locate_pdi_sibling (const struct die_reader_specs *reader,
8960 struct partial_die_info *orig_pdi,
8961 const gdb_byte *info_ptr)
8962 {
8963 /* Do we know the sibling already? */
8964
8965 if (orig_pdi->sibling)
8966 return orig_pdi->sibling;
8967
8968 /* Are there any children to deal with? */
8969
8970 if (!orig_pdi->has_children)
8971 return info_ptr;
8972
8973 /* Skip the children the long way. */
8974
8975 return skip_children (reader, info_ptr);
8976 }
8977
8978 /* Expand this partial symbol table into a full symbol table. SELF is
8979 not NULL. */
8980
8981 void
8982 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8983 {
8984 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8985
8986 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8987
8988 /* If this psymtab is constructed from a debug-only objfile, the
8989 has_section_at_zero flag will not necessarily be correct. We
8990 can get the correct value for this flag by looking at the data
8991 associated with the (presumably stripped) associated objfile. */
8992 if (objfile->separate_debug_objfile_backlink)
8993 {
8994 dwarf2_per_objfile *per_objfile_backlink
8995 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8996
8997 per_objfile->per_bfd->has_section_at_zero
8998 = per_objfile_backlink->per_bfd->has_section_at_zero;
8999 }
9000
9001 expand_psymtab (objfile);
9002
9003 process_cu_includes (per_objfile);
9004 }
9005 \f
9006 /* Reading in full CUs. */
9007
9008 /* Add PER_CU to the queue. */
9009
9010 static void
9011 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9012 dwarf2_per_objfile *per_objfile,
9013 enum language pretend_language)
9014 {
9015 per_cu->queued = 1;
9016 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
9017 }
9018
9019 /* If PER_CU is not yet queued, add it to the queue.
9020 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9021 dependency.
9022 The result is non-zero if PER_CU was queued, otherwise the result is zero
9023 meaning either PER_CU is already queued or it is already loaded.
9024
9025 N.B. There is an invariant here that if a CU is queued then it is loaded.
9026 The caller is required to load PER_CU if we return non-zero. */
9027
9028 static int
9029 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9030 dwarf2_per_cu_data *per_cu,
9031 dwarf2_per_objfile *per_objfile,
9032 enum language pretend_language)
9033 {
9034 /* We may arrive here during partial symbol reading, if we need full
9035 DIEs to process an unusual case (e.g. template arguments). Do
9036 not queue PER_CU, just tell our caller to load its DIEs. */
9037 if (per_cu->per_bfd->reading_partial_symbols)
9038 {
9039 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9040
9041 if (cu == NULL || cu->dies == NULL)
9042 return 1;
9043 return 0;
9044 }
9045
9046 /* Mark the dependence relation so that we don't flush PER_CU
9047 too early. */
9048 if (dependent_cu != NULL)
9049 dwarf2_add_dependence (dependent_cu, per_cu);
9050
9051 /* If it's already on the queue, we have nothing to do. */
9052 if (per_cu->queued)
9053 return 0;
9054
9055 /* If the compilation unit is already loaded, just mark it as
9056 used. */
9057 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9058 if (cu != nullptr)
9059 {
9060 cu->last_used = 0;
9061 return 0;
9062 }
9063
9064 /* Add it to the queue. */
9065 queue_comp_unit (per_cu, per_objfile, pretend_language);
9066
9067 return 1;
9068 }
9069
9070 /* Process the queue. */
9071
9072 static void
9073 process_queue (dwarf2_per_objfile *per_objfile)
9074 {
9075 if (dwarf_read_debug)
9076 {
9077 fprintf_unfiltered (gdb_stdlog,
9078 "Expanding one or more symtabs of objfile %s ...\n",
9079 objfile_name (per_objfile->objfile));
9080 }
9081
9082 /* The queue starts out with one item, but following a DIE reference
9083 may load a new CU, adding it to the end of the queue. */
9084 while (!per_objfile->per_bfd->queue.empty ())
9085 {
9086 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
9087 dwarf2_per_cu_data *per_cu = item.per_cu;
9088
9089 if (!per_objfile->symtab_set_p (per_cu))
9090 {
9091 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9092
9093 /* Skip dummy CUs. */
9094 if (cu != nullptr)
9095 {
9096 unsigned int debug_print_threshold;
9097 char buf[100];
9098
9099 if (per_cu->is_debug_types)
9100 {
9101 struct signatured_type *sig_type =
9102 (struct signatured_type *) per_cu;
9103
9104 sprintf (buf, "TU %s at offset %s",
9105 hex_string (sig_type->signature),
9106 sect_offset_str (per_cu->sect_off));
9107 /* There can be 100s of TUs.
9108 Only print them in verbose mode. */
9109 debug_print_threshold = 2;
9110 }
9111 else
9112 {
9113 sprintf (buf, "CU at offset %s",
9114 sect_offset_str (per_cu->sect_off));
9115 debug_print_threshold = 1;
9116 }
9117
9118 if (dwarf_read_debug >= debug_print_threshold)
9119 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9120
9121 if (per_cu->is_debug_types)
9122 process_full_type_unit (cu, item.pretend_language);
9123 else
9124 process_full_comp_unit (cu, item.pretend_language);
9125
9126 if (dwarf_read_debug >= debug_print_threshold)
9127 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9128 }
9129 }
9130
9131 per_cu->queued = 0;
9132 per_objfile->per_bfd->queue.pop ();
9133 }
9134
9135 if (dwarf_read_debug)
9136 {
9137 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9138 objfile_name (per_objfile->objfile));
9139 }
9140 }
9141
9142 /* Read in full symbols for PST, and anything it depends on. */
9143
9144 void
9145 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9146 {
9147 gdb_assert (!readin_p (objfile));
9148
9149 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9150 free_cached_comp_units freer (per_objfile);
9151 expand_dependencies (objfile);
9152
9153 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9154 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9155 }
9156
9157 /* See psympriv.h. */
9158
9159 bool
9160 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9161 {
9162 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9163 return per_objfile->symtab_set_p (per_cu_data);
9164 }
9165
9166 /* See psympriv.h. */
9167
9168 compunit_symtab *
9169 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9170 {
9171 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9172 return per_objfile->get_symtab (per_cu_data);
9173 }
9174
9175 /* Trivial hash function for die_info: the hash value of a DIE
9176 is its offset in .debug_info for this objfile. */
9177
9178 static hashval_t
9179 die_hash (const void *item)
9180 {
9181 const struct die_info *die = (const struct die_info *) item;
9182
9183 return to_underlying (die->sect_off);
9184 }
9185
9186 /* Trivial comparison function for die_info structures: two DIEs
9187 are equal if they have the same offset. */
9188
9189 static int
9190 die_eq (const void *item_lhs, const void *item_rhs)
9191 {
9192 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9193 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9194
9195 return die_lhs->sect_off == die_rhs->sect_off;
9196 }
9197
9198 /* Load the DIEs associated with PER_CU into memory. */
9199
9200 static void
9201 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9202 dwarf2_per_objfile *per_objfile,
9203 bool skip_partial,
9204 enum language pretend_language)
9205 {
9206 gdb_assert (! this_cu->is_debug_types);
9207
9208 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9209 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9210 if (reader.dummy_p)
9211 return;
9212
9213 struct dwarf2_cu *cu = reader.cu;
9214 const gdb_byte *info_ptr = reader.info_ptr;
9215
9216 gdb_assert (cu->die_hash == NULL);
9217 cu->die_hash =
9218 htab_create_alloc_ex (cu->header.length / 12,
9219 die_hash,
9220 die_eq,
9221 NULL,
9222 &cu->comp_unit_obstack,
9223 hashtab_obstack_allocate,
9224 dummy_obstack_deallocate);
9225
9226 if (reader.comp_unit_die->has_children)
9227 reader.comp_unit_die->child
9228 = read_die_and_siblings (&reader, reader.info_ptr,
9229 &info_ptr, reader.comp_unit_die);
9230 cu->dies = reader.comp_unit_die;
9231 /* comp_unit_die is not stored in die_hash, no need. */
9232
9233 /* We try not to read any attributes in this function, because not
9234 all CUs needed for references have been loaded yet, and symbol
9235 table processing isn't initialized. But we have to set the CU language,
9236 or we won't be able to build types correctly.
9237 Similarly, if we do not read the producer, we can not apply
9238 producer-specific interpretation. */
9239 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9240
9241 reader.keep ();
9242 }
9243
9244 /* Add a DIE to the delayed physname list. */
9245
9246 static void
9247 add_to_method_list (struct type *type, int fnfield_index, int index,
9248 const char *name, struct die_info *die,
9249 struct dwarf2_cu *cu)
9250 {
9251 struct delayed_method_info mi;
9252 mi.type = type;
9253 mi.fnfield_index = fnfield_index;
9254 mi.index = index;
9255 mi.name = name;
9256 mi.die = die;
9257 cu->method_list.push_back (mi);
9258 }
9259
9260 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9261 "const" / "volatile". If so, decrements LEN by the length of the
9262 modifier and return true. Otherwise return false. */
9263
9264 template<size_t N>
9265 static bool
9266 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9267 {
9268 size_t mod_len = sizeof (mod) - 1;
9269 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9270 {
9271 len -= mod_len;
9272 return true;
9273 }
9274 return false;
9275 }
9276
9277 /* Compute the physnames of any methods on the CU's method list.
9278
9279 The computation of method physnames is delayed in order to avoid the
9280 (bad) condition that one of the method's formal parameters is of an as yet
9281 incomplete type. */
9282
9283 static void
9284 compute_delayed_physnames (struct dwarf2_cu *cu)
9285 {
9286 /* Only C++ delays computing physnames. */
9287 if (cu->method_list.empty ())
9288 return;
9289 gdb_assert (cu->language == language_cplus);
9290
9291 for (const delayed_method_info &mi : cu->method_list)
9292 {
9293 const char *physname;
9294 struct fn_fieldlist *fn_flp
9295 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9296 physname = dwarf2_physname (mi.name, mi.die, cu);
9297 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9298 = physname ? physname : "";
9299
9300 /* Since there's no tag to indicate whether a method is a
9301 const/volatile overload, extract that information out of the
9302 demangled name. */
9303 if (physname != NULL)
9304 {
9305 size_t len = strlen (physname);
9306
9307 while (1)
9308 {
9309 if (physname[len] == ')') /* shortcut */
9310 break;
9311 else if (check_modifier (physname, len, " const"))
9312 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9313 else if (check_modifier (physname, len, " volatile"))
9314 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9315 else
9316 break;
9317 }
9318 }
9319 }
9320
9321 /* The list is no longer needed. */
9322 cu->method_list.clear ();
9323 }
9324
9325 /* Go objects should be embedded in a DW_TAG_module DIE,
9326 and it's not clear if/how imported objects will appear.
9327 To keep Go support simple until that's worked out,
9328 go back through what we've read and create something usable.
9329 We could do this while processing each DIE, and feels kinda cleaner,
9330 but that way is more invasive.
9331 This is to, for example, allow the user to type "p var" or "b main"
9332 without having to specify the package name, and allow lookups
9333 of module.object to work in contexts that use the expression
9334 parser. */
9335
9336 static void
9337 fixup_go_packaging (struct dwarf2_cu *cu)
9338 {
9339 gdb::unique_xmalloc_ptr<char> package_name;
9340 struct pending *list;
9341 int i;
9342
9343 for (list = *cu->get_builder ()->get_global_symbols ();
9344 list != NULL;
9345 list = list->next)
9346 {
9347 for (i = 0; i < list->nsyms; ++i)
9348 {
9349 struct symbol *sym = list->symbol[i];
9350
9351 if (sym->language () == language_go
9352 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9353 {
9354 gdb::unique_xmalloc_ptr<char> this_package_name
9355 (go_symbol_package_name (sym));
9356
9357 if (this_package_name == NULL)
9358 continue;
9359 if (package_name == NULL)
9360 package_name = std::move (this_package_name);
9361 else
9362 {
9363 struct objfile *objfile = cu->per_objfile->objfile;
9364 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9365 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9366 (symbol_symtab (sym) != NULL
9367 ? symtab_to_filename_for_display
9368 (symbol_symtab (sym))
9369 : objfile_name (objfile)),
9370 this_package_name.get (), package_name.get ());
9371 }
9372 }
9373 }
9374 }
9375
9376 if (package_name != NULL)
9377 {
9378 struct objfile *objfile = cu->per_objfile->objfile;
9379 const char *saved_package_name = objfile->intern (package_name.get ());
9380 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9381 saved_package_name);
9382 struct symbol *sym;
9383
9384 sym = new (&objfile->objfile_obstack) symbol;
9385 sym->set_language (language_go, &objfile->objfile_obstack);
9386 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9387 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9388 e.g., "main" finds the "main" module and not C's main(). */
9389 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9390 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9391 SYMBOL_TYPE (sym) = type;
9392
9393 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9394 }
9395 }
9396
9397 /* Allocate a fully-qualified name consisting of the two parts on the
9398 obstack. */
9399
9400 static const char *
9401 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9402 {
9403 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9404 }
9405
9406 /* A helper that allocates a variant part to attach to a Rust enum
9407 type. OBSTACK is where the results should be allocated. TYPE is
9408 the type we're processing. DISCRIMINANT_INDEX is the index of the
9409 discriminant. It must be the index of one of the fields of TYPE.
9410 DEFAULT_INDEX is the index of the default field; or -1 if there is
9411 no default. RANGES is indexed by "effective" field number (the
9412 field index, but omitting the discriminant and default fields) and
9413 must hold the discriminant values used by the variants. Note that
9414 RANGES must have a lifetime at least as long as OBSTACK -- either
9415 already allocated on it, or static. */
9416
9417 static void
9418 alloc_rust_variant (struct obstack *obstack, struct type *type,
9419 int discriminant_index, int default_index,
9420 gdb::array_view<discriminant_range> ranges)
9421 {
9422 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9423 must be handled by the caller. */
9424 gdb_assert (discriminant_index >= 0
9425 && discriminant_index < type->num_fields ());
9426 gdb_assert (default_index == -1
9427 || (default_index >= 0 && default_index < type->num_fields ()));
9428
9429 /* We have one variant for each non-discriminant field. */
9430 int n_variants = type->num_fields () - 1;
9431
9432 variant *variants = new (obstack) variant[n_variants];
9433 int var_idx = 0;
9434 int range_idx = 0;
9435 for (int i = 0; i < type->num_fields (); ++i)
9436 {
9437 if (i == discriminant_index)
9438 continue;
9439
9440 variants[var_idx].first_field = i;
9441 variants[var_idx].last_field = i + 1;
9442
9443 /* The default field does not need a range, but other fields do.
9444 We skipped the discriminant above. */
9445 if (i != default_index)
9446 {
9447 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9448 ++range_idx;
9449 }
9450
9451 ++var_idx;
9452 }
9453
9454 gdb_assert (range_idx == ranges.size ());
9455 gdb_assert (var_idx == n_variants);
9456
9457 variant_part *part = new (obstack) variant_part;
9458 part->discriminant_index = discriminant_index;
9459 part->is_unsigned = TYPE_UNSIGNED (type->field (discriminant_index).type ());
9460 part->variants = gdb::array_view<variant> (variants, n_variants);
9461
9462 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9463 gdb::array_view<variant_part> *prop_value
9464 = new (storage) gdb::array_view<variant_part> (part, 1);
9465
9466 struct dynamic_prop prop;
9467 prop.set_variant_parts (prop_value);
9468
9469 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9470 }
9471
9472 /* Some versions of rustc emitted enums in an unusual way.
9473
9474 Ordinary enums were emitted as unions. The first element of each
9475 structure in the union was named "RUST$ENUM$DISR". This element
9476 held the discriminant.
9477
9478 These versions of Rust also implemented the "non-zero"
9479 optimization. When the enum had two values, and one is empty and
9480 the other holds a pointer that cannot be zero, the pointer is used
9481 as the discriminant, with a zero value meaning the empty variant.
9482 Here, the union's first member is of the form
9483 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9484 where the fieldnos are the indices of the fields that should be
9485 traversed in order to find the field (which may be several fields deep)
9486 and the variantname is the name of the variant of the case when the
9487 field is zero.
9488
9489 This function recognizes whether TYPE is of one of these forms,
9490 and, if so, smashes it to be a variant type. */
9491
9492 static void
9493 quirk_rust_enum (struct type *type, struct objfile *objfile)
9494 {
9495 gdb_assert (type->code () == TYPE_CODE_UNION);
9496
9497 /* We don't need to deal with empty enums. */
9498 if (type->num_fields () == 0)
9499 return;
9500
9501 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9502 if (type->num_fields () == 1
9503 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9504 {
9505 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9506
9507 /* Decode the field name to find the offset of the
9508 discriminant. */
9509 ULONGEST bit_offset = 0;
9510 struct type *field_type = type->field (0).type ();
9511 while (name[0] >= '0' && name[0] <= '9')
9512 {
9513 char *tail;
9514 unsigned long index = strtoul (name, &tail, 10);
9515 name = tail;
9516 if (*name != '$'
9517 || index >= field_type->num_fields ()
9518 || (TYPE_FIELD_LOC_KIND (field_type, index)
9519 != FIELD_LOC_KIND_BITPOS))
9520 {
9521 complaint (_("Could not parse Rust enum encoding string \"%s\""
9522 "[in module %s]"),
9523 TYPE_FIELD_NAME (type, 0),
9524 objfile_name (objfile));
9525 return;
9526 }
9527 ++name;
9528
9529 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9530 field_type = field_type->field (index).type ();
9531 }
9532
9533 /* Smash this type to be a structure type. We have to do this
9534 because the type has already been recorded. */
9535 type->set_code (TYPE_CODE_STRUCT);
9536 type->set_num_fields (3);
9537 /* Save the field we care about. */
9538 struct field saved_field = type->field (0);
9539 type->set_fields
9540 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9541
9542 /* Put the discriminant at index 0. */
9543 type->field (0).set_type (field_type);
9544 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9545 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9546 SET_FIELD_BITPOS (type->field (0), bit_offset);
9547
9548 /* The order of fields doesn't really matter, so put the real
9549 field at index 1 and the data-less field at index 2. */
9550 type->field (1) = saved_field;
9551 TYPE_FIELD_NAME (type, 1)
9552 = rust_last_path_segment (type->field (1).type ()->name ());
9553 type->field (1).type ()->set_name
9554 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9555 TYPE_FIELD_NAME (type, 1)));
9556
9557 const char *dataless_name
9558 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9559 name);
9560 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9561 dataless_name);
9562 type->field (2).set_type (dataless_type);
9563 /* NAME points into the original discriminant name, which
9564 already has the correct lifetime. */
9565 TYPE_FIELD_NAME (type, 2) = name;
9566 SET_FIELD_BITPOS (type->field (2), 0);
9567
9568 /* Indicate that this is a variant type. */
9569 static discriminant_range ranges[1] = { { 0, 0 } };
9570 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9571 }
9572 /* A union with a single anonymous field is probably an old-style
9573 univariant enum. */
9574 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9575 {
9576 /* Smash this type to be a structure type. We have to do this
9577 because the type has already been recorded. */
9578 type->set_code (TYPE_CODE_STRUCT);
9579
9580 struct type *field_type = type->field (0).type ();
9581 const char *variant_name
9582 = rust_last_path_segment (field_type->name ());
9583 TYPE_FIELD_NAME (type, 0) = variant_name;
9584 field_type->set_name
9585 (rust_fully_qualify (&objfile->objfile_obstack,
9586 type->name (), variant_name));
9587 }
9588 else
9589 {
9590 struct type *disr_type = nullptr;
9591 for (int i = 0; i < type->num_fields (); ++i)
9592 {
9593 disr_type = type->field (i).type ();
9594
9595 if (disr_type->code () != TYPE_CODE_STRUCT)
9596 {
9597 /* All fields of a true enum will be structs. */
9598 return;
9599 }
9600 else if (disr_type->num_fields () == 0)
9601 {
9602 /* Could be data-less variant, so keep going. */
9603 disr_type = nullptr;
9604 }
9605 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9606 "RUST$ENUM$DISR") != 0)
9607 {
9608 /* Not a Rust enum. */
9609 return;
9610 }
9611 else
9612 {
9613 /* Found one. */
9614 break;
9615 }
9616 }
9617
9618 /* If we got here without a discriminant, then it's probably
9619 just a union. */
9620 if (disr_type == nullptr)
9621 return;
9622
9623 /* Smash this type to be a structure type. We have to do this
9624 because the type has already been recorded. */
9625 type->set_code (TYPE_CODE_STRUCT);
9626
9627 /* Make space for the discriminant field. */
9628 struct field *disr_field = &disr_type->field (0);
9629 field *new_fields
9630 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9631 * sizeof (struct field)));
9632 memcpy (new_fields + 1, type->fields (),
9633 type->num_fields () * sizeof (struct field));
9634 type->set_fields (new_fields);
9635 type->set_num_fields (type->num_fields () + 1);
9636
9637 /* Install the discriminant at index 0 in the union. */
9638 type->field (0) = *disr_field;
9639 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9640 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9641
9642 /* We need a way to find the correct discriminant given a
9643 variant name. For convenience we build a map here. */
9644 struct type *enum_type = disr_field->type ();
9645 std::unordered_map<std::string, ULONGEST> discriminant_map;
9646 for (int i = 0; i < enum_type->num_fields (); ++i)
9647 {
9648 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9649 {
9650 const char *name
9651 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9652 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9653 }
9654 }
9655
9656 int n_fields = type->num_fields ();
9657 /* We don't need a range entry for the discriminant, but we do
9658 need one for every other field, as there is no default
9659 variant. */
9660 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9661 discriminant_range,
9662 n_fields - 1);
9663 /* Skip the discriminant here. */
9664 for (int i = 1; i < n_fields; ++i)
9665 {
9666 /* Find the final word in the name of this variant's type.
9667 That name can be used to look up the correct
9668 discriminant. */
9669 const char *variant_name
9670 = rust_last_path_segment (type->field (i).type ()->name ());
9671
9672 auto iter = discriminant_map.find (variant_name);
9673 if (iter != discriminant_map.end ())
9674 {
9675 ranges[i].low = iter->second;
9676 ranges[i].high = iter->second;
9677 }
9678
9679 /* Remove the discriminant field, if it exists. */
9680 struct type *sub_type = type->field (i).type ();
9681 if (sub_type->num_fields () > 0)
9682 {
9683 sub_type->set_num_fields (sub_type->num_fields () - 1);
9684 sub_type->set_fields (sub_type->fields () + 1);
9685 }
9686 TYPE_FIELD_NAME (type, i) = variant_name;
9687 sub_type->set_name
9688 (rust_fully_qualify (&objfile->objfile_obstack,
9689 type->name (), variant_name));
9690 }
9691
9692 /* Indicate that this is a variant type. */
9693 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9694 gdb::array_view<discriminant_range> (ranges,
9695 n_fields - 1));
9696 }
9697 }
9698
9699 /* Rewrite some Rust unions to be structures with variants parts. */
9700
9701 static void
9702 rust_union_quirks (struct dwarf2_cu *cu)
9703 {
9704 gdb_assert (cu->language == language_rust);
9705 for (type *type_ : cu->rust_unions)
9706 quirk_rust_enum (type_, cu->per_objfile->objfile);
9707 /* We don't need this any more. */
9708 cu->rust_unions.clear ();
9709 }
9710
9711 /* See read.h. */
9712
9713 type_unit_group_unshareable *
9714 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9715 {
9716 auto iter = this->m_type_units.find (tu_group);
9717 if (iter != this->m_type_units.end ())
9718 return iter->second.get ();
9719
9720 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9721 type_unit_group_unshareable *result = uniq.get ();
9722 this->m_type_units[tu_group] = std::move (uniq);
9723 return result;
9724 }
9725
9726 struct type *
9727 dwarf2_per_objfile::get_type_for_signatured_type
9728 (signatured_type *sig_type) const
9729 {
9730 auto iter = this->m_type_map.find (sig_type);
9731 if (iter == this->m_type_map.end ())
9732 return nullptr;
9733
9734 return iter->second;
9735 }
9736
9737 void dwarf2_per_objfile::set_type_for_signatured_type
9738 (signatured_type *sig_type, struct type *type)
9739 {
9740 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9741
9742 this->m_type_map[sig_type] = type;
9743 }
9744
9745 /* A helper function for computing the list of all symbol tables
9746 included by PER_CU. */
9747
9748 static void
9749 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9750 htab_t all_children, htab_t all_type_symtabs,
9751 dwarf2_per_cu_data *per_cu,
9752 dwarf2_per_objfile *per_objfile,
9753 struct compunit_symtab *immediate_parent)
9754 {
9755 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9756 if (*slot != NULL)
9757 {
9758 /* This inclusion and its children have been processed. */
9759 return;
9760 }
9761
9762 *slot = per_cu;
9763
9764 /* Only add a CU if it has a symbol table. */
9765 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9766 if (cust != NULL)
9767 {
9768 /* If this is a type unit only add its symbol table if we haven't
9769 seen it yet (type unit per_cu's can share symtabs). */
9770 if (per_cu->is_debug_types)
9771 {
9772 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9773 if (*slot == NULL)
9774 {
9775 *slot = cust;
9776 result->push_back (cust);
9777 if (cust->user == NULL)
9778 cust->user = immediate_parent;
9779 }
9780 }
9781 else
9782 {
9783 result->push_back (cust);
9784 if (cust->user == NULL)
9785 cust->user = immediate_parent;
9786 }
9787 }
9788
9789 if (!per_cu->imported_symtabs_empty ())
9790 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9791 {
9792 recursively_compute_inclusions (result, all_children,
9793 all_type_symtabs, ptr, per_objfile,
9794 cust);
9795 }
9796 }
9797
9798 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9799 PER_CU. */
9800
9801 static void
9802 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9803 dwarf2_per_objfile *per_objfile)
9804 {
9805 gdb_assert (! per_cu->is_debug_types);
9806
9807 if (!per_cu->imported_symtabs_empty ())
9808 {
9809 int len;
9810 std::vector<compunit_symtab *> result_symtabs;
9811 htab_t all_children, all_type_symtabs;
9812 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9813
9814 /* If we don't have a symtab, we can just skip this case. */
9815 if (cust == NULL)
9816 return;
9817
9818 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9819 NULL, xcalloc, xfree);
9820 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9821 NULL, xcalloc, xfree);
9822
9823 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9824 {
9825 recursively_compute_inclusions (&result_symtabs, all_children,
9826 all_type_symtabs, ptr, per_objfile,
9827 cust);
9828 }
9829
9830 /* Now we have a transitive closure of all the included symtabs. */
9831 len = result_symtabs.size ();
9832 cust->includes
9833 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9834 struct compunit_symtab *, len + 1);
9835 memcpy (cust->includes, result_symtabs.data (),
9836 len * sizeof (compunit_symtab *));
9837 cust->includes[len] = NULL;
9838
9839 htab_delete (all_children);
9840 htab_delete (all_type_symtabs);
9841 }
9842 }
9843
9844 /* Compute the 'includes' field for the symtabs of all the CUs we just
9845 read. */
9846
9847 static void
9848 process_cu_includes (dwarf2_per_objfile *per_objfile)
9849 {
9850 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9851 {
9852 if (! iter->is_debug_types)
9853 compute_compunit_symtab_includes (iter, per_objfile);
9854 }
9855
9856 per_objfile->per_bfd->just_read_cus.clear ();
9857 }
9858
9859 /* Generate full symbol information for CU, whose DIEs have
9860 already been loaded into memory. */
9861
9862 static void
9863 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9864 {
9865 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9866 struct objfile *objfile = per_objfile->objfile;
9867 struct gdbarch *gdbarch = objfile->arch ();
9868 CORE_ADDR lowpc, highpc;
9869 struct compunit_symtab *cust;
9870 CORE_ADDR baseaddr;
9871 struct block *static_block;
9872 CORE_ADDR addr;
9873
9874 baseaddr = objfile->text_section_offset ();
9875
9876 /* Clear the list here in case something was left over. */
9877 cu->method_list.clear ();
9878
9879 cu->language = pretend_language;
9880 cu->language_defn = language_def (cu->language);
9881
9882 /* Do line number decoding in read_file_scope () */
9883 process_die (cu->dies, cu);
9884
9885 /* For now fudge the Go package. */
9886 if (cu->language == language_go)
9887 fixup_go_packaging (cu);
9888
9889 /* Now that we have processed all the DIEs in the CU, all the types
9890 should be complete, and it should now be safe to compute all of the
9891 physnames. */
9892 compute_delayed_physnames (cu);
9893
9894 if (cu->language == language_rust)
9895 rust_union_quirks (cu);
9896
9897 /* Some compilers don't define a DW_AT_high_pc attribute for the
9898 compilation unit. If the DW_AT_high_pc is missing, synthesize
9899 it, by scanning the DIE's below the compilation unit. */
9900 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9901
9902 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9903 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9904
9905 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9906 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9907 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9908 addrmap to help ensure it has an accurate map of pc values belonging to
9909 this comp unit. */
9910 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9911
9912 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9913 SECT_OFF_TEXT (objfile),
9914 0);
9915
9916 if (cust != NULL)
9917 {
9918 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9919
9920 /* Set symtab language to language from DW_AT_language. If the
9921 compilation is from a C file generated by language preprocessors, do
9922 not set the language if it was already deduced by start_subfile. */
9923 if (!(cu->language == language_c
9924 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9925 COMPUNIT_FILETABS (cust)->language = cu->language;
9926
9927 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9928 produce DW_AT_location with location lists but it can be possibly
9929 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9930 there were bugs in prologue debug info, fixed later in GCC-4.5
9931 by "unwind info for epilogues" patch (which is not directly related).
9932
9933 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9934 needed, it would be wrong due to missing DW_AT_producer there.
9935
9936 Still one can confuse GDB by using non-standard GCC compilation
9937 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9938 */
9939 if (cu->has_loclist && gcc_4_minor >= 5)
9940 cust->locations_valid = 1;
9941
9942 if (gcc_4_minor >= 5)
9943 cust->epilogue_unwind_valid = 1;
9944
9945 cust->call_site_htab = cu->call_site_htab;
9946 }
9947
9948 per_objfile->set_symtab (cu->per_cu, cust);
9949
9950 /* Push it for inclusion processing later. */
9951 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9952
9953 /* Not needed any more. */
9954 cu->reset_builder ();
9955 }
9956
9957 /* Generate full symbol information for type unit CU, whose DIEs have
9958 already been loaded into memory. */
9959
9960 static void
9961 process_full_type_unit (dwarf2_cu *cu,
9962 enum language pretend_language)
9963 {
9964 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9965 struct objfile *objfile = per_objfile->objfile;
9966 struct compunit_symtab *cust;
9967 struct signatured_type *sig_type;
9968
9969 gdb_assert (cu->per_cu->is_debug_types);
9970 sig_type = (struct signatured_type *) cu->per_cu;
9971
9972 /* Clear the list here in case something was left over. */
9973 cu->method_list.clear ();
9974
9975 cu->language = pretend_language;
9976 cu->language_defn = language_def (cu->language);
9977
9978 /* The symbol tables are set up in read_type_unit_scope. */
9979 process_die (cu->dies, cu);
9980
9981 /* For now fudge the Go package. */
9982 if (cu->language == language_go)
9983 fixup_go_packaging (cu);
9984
9985 /* Now that we have processed all the DIEs in the CU, all the types
9986 should be complete, and it should now be safe to compute all of the
9987 physnames. */
9988 compute_delayed_physnames (cu);
9989
9990 if (cu->language == language_rust)
9991 rust_union_quirks (cu);
9992
9993 /* TUs share symbol tables.
9994 If this is the first TU to use this symtab, complete the construction
9995 of it with end_expandable_symtab. Otherwise, complete the addition of
9996 this TU's symbols to the existing symtab. */
9997 type_unit_group_unshareable *tug_unshare =
9998 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9999 if (tug_unshare->compunit_symtab == NULL)
10000 {
10001 buildsym_compunit *builder = cu->get_builder ();
10002 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10003 tug_unshare->compunit_symtab = cust;
10004
10005 if (cust != NULL)
10006 {
10007 /* Set symtab language to language from DW_AT_language. If the
10008 compilation is from a C file generated by language preprocessors,
10009 do not set the language if it was already deduced by
10010 start_subfile. */
10011 if (!(cu->language == language_c
10012 && COMPUNIT_FILETABS (cust)->language != language_c))
10013 COMPUNIT_FILETABS (cust)->language = cu->language;
10014 }
10015 }
10016 else
10017 {
10018 cu->get_builder ()->augment_type_symtab ();
10019 cust = tug_unshare->compunit_symtab;
10020 }
10021
10022 per_objfile->set_symtab (cu->per_cu, cust);
10023
10024 /* Not needed any more. */
10025 cu->reset_builder ();
10026 }
10027
10028 /* Process an imported unit DIE. */
10029
10030 static void
10031 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10032 {
10033 struct attribute *attr;
10034
10035 /* For now we don't handle imported units in type units. */
10036 if (cu->per_cu->is_debug_types)
10037 {
10038 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10039 " supported in type units [in module %s]"),
10040 objfile_name (cu->per_objfile->objfile));
10041 }
10042
10043 attr = dwarf2_attr (die, DW_AT_import, cu);
10044 if (attr != NULL)
10045 {
10046 sect_offset sect_off = attr->get_ref_die_offset ();
10047 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10048 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10049 dwarf2_per_cu_data *per_cu
10050 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10051
10052 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10053 into another compilation unit, at root level. Regard this as a hint,
10054 and ignore it. */
10055 if (die->parent && die->parent->parent == NULL
10056 && per_cu->unit_type == DW_UT_compile
10057 && per_cu->lang == language_cplus)
10058 return;
10059
10060 /* If necessary, add it to the queue and load its DIEs. */
10061 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10062 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
10063
10064 cu->per_cu->imported_symtabs_push (per_cu);
10065 }
10066 }
10067
10068 /* RAII object that represents a process_die scope: i.e.,
10069 starts/finishes processing a DIE. */
10070 class process_die_scope
10071 {
10072 public:
10073 process_die_scope (die_info *die, dwarf2_cu *cu)
10074 : m_die (die), m_cu (cu)
10075 {
10076 /* We should only be processing DIEs not already in process. */
10077 gdb_assert (!m_die->in_process);
10078 m_die->in_process = true;
10079 }
10080
10081 ~process_die_scope ()
10082 {
10083 m_die->in_process = false;
10084
10085 /* If we're done processing the DIE for the CU that owns the line
10086 header, we don't need the line header anymore. */
10087 if (m_cu->line_header_die_owner == m_die)
10088 {
10089 delete m_cu->line_header;
10090 m_cu->line_header = NULL;
10091 m_cu->line_header_die_owner = NULL;
10092 }
10093 }
10094
10095 private:
10096 die_info *m_die;
10097 dwarf2_cu *m_cu;
10098 };
10099
10100 /* Process a die and its children. */
10101
10102 static void
10103 process_die (struct die_info *die, struct dwarf2_cu *cu)
10104 {
10105 process_die_scope scope (die, cu);
10106
10107 switch (die->tag)
10108 {
10109 case DW_TAG_padding:
10110 break;
10111 case DW_TAG_compile_unit:
10112 case DW_TAG_partial_unit:
10113 read_file_scope (die, cu);
10114 break;
10115 case DW_TAG_type_unit:
10116 read_type_unit_scope (die, cu);
10117 break;
10118 case DW_TAG_subprogram:
10119 /* Nested subprograms in Fortran get a prefix. */
10120 if (cu->language == language_fortran
10121 && die->parent != NULL
10122 && die->parent->tag == DW_TAG_subprogram)
10123 cu->processing_has_namespace_info = true;
10124 /* Fall through. */
10125 case DW_TAG_inlined_subroutine:
10126 read_func_scope (die, cu);
10127 break;
10128 case DW_TAG_lexical_block:
10129 case DW_TAG_try_block:
10130 case DW_TAG_catch_block:
10131 read_lexical_block_scope (die, cu);
10132 break;
10133 case DW_TAG_call_site:
10134 case DW_TAG_GNU_call_site:
10135 read_call_site_scope (die, cu);
10136 break;
10137 case DW_TAG_class_type:
10138 case DW_TAG_interface_type:
10139 case DW_TAG_structure_type:
10140 case DW_TAG_union_type:
10141 process_structure_scope (die, cu);
10142 break;
10143 case DW_TAG_enumeration_type:
10144 process_enumeration_scope (die, cu);
10145 break;
10146
10147 /* These dies have a type, but processing them does not create
10148 a symbol or recurse to process the children. Therefore we can
10149 read them on-demand through read_type_die. */
10150 case DW_TAG_subroutine_type:
10151 case DW_TAG_set_type:
10152 case DW_TAG_array_type:
10153 case DW_TAG_pointer_type:
10154 case DW_TAG_ptr_to_member_type:
10155 case DW_TAG_reference_type:
10156 case DW_TAG_rvalue_reference_type:
10157 case DW_TAG_string_type:
10158 break;
10159
10160 case DW_TAG_base_type:
10161 case DW_TAG_subrange_type:
10162 case DW_TAG_typedef:
10163 /* Add a typedef symbol for the type definition, if it has a
10164 DW_AT_name. */
10165 new_symbol (die, read_type_die (die, cu), cu);
10166 break;
10167 case DW_TAG_common_block:
10168 read_common_block (die, cu);
10169 break;
10170 case DW_TAG_common_inclusion:
10171 break;
10172 case DW_TAG_namespace:
10173 cu->processing_has_namespace_info = true;
10174 read_namespace (die, cu);
10175 break;
10176 case DW_TAG_module:
10177 cu->processing_has_namespace_info = true;
10178 read_module (die, cu);
10179 break;
10180 case DW_TAG_imported_declaration:
10181 cu->processing_has_namespace_info = true;
10182 if (read_namespace_alias (die, cu))
10183 break;
10184 /* The declaration is not a global namespace alias. */
10185 /* Fall through. */
10186 case DW_TAG_imported_module:
10187 cu->processing_has_namespace_info = true;
10188 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10189 || cu->language != language_fortran))
10190 complaint (_("Tag '%s' has unexpected children"),
10191 dwarf_tag_name (die->tag));
10192 read_import_statement (die, cu);
10193 break;
10194
10195 case DW_TAG_imported_unit:
10196 process_imported_unit_die (die, cu);
10197 break;
10198
10199 case DW_TAG_variable:
10200 read_variable (die, cu);
10201 break;
10202
10203 default:
10204 new_symbol (die, NULL, cu);
10205 break;
10206 }
10207 }
10208 \f
10209 /* DWARF name computation. */
10210
10211 /* A helper function for dwarf2_compute_name which determines whether DIE
10212 needs to have the name of the scope prepended to the name listed in the
10213 die. */
10214
10215 static int
10216 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10217 {
10218 struct attribute *attr;
10219
10220 switch (die->tag)
10221 {
10222 case DW_TAG_namespace:
10223 case DW_TAG_typedef:
10224 case DW_TAG_class_type:
10225 case DW_TAG_interface_type:
10226 case DW_TAG_structure_type:
10227 case DW_TAG_union_type:
10228 case DW_TAG_enumeration_type:
10229 case DW_TAG_enumerator:
10230 case DW_TAG_subprogram:
10231 case DW_TAG_inlined_subroutine:
10232 case DW_TAG_member:
10233 case DW_TAG_imported_declaration:
10234 return 1;
10235
10236 case DW_TAG_variable:
10237 case DW_TAG_constant:
10238 /* We only need to prefix "globally" visible variables. These include
10239 any variable marked with DW_AT_external or any variable that
10240 lives in a namespace. [Variables in anonymous namespaces
10241 require prefixing, but they are not DW_AT_external.] */
10242
10243 if (dwarf2_attr (die, DW_AT_specification, cu))
10244 {
10245 struct dwarf2_cu *spec_cu = cu;
10246
10247 return die_needs_namespace (die_specification (die, &spec_cu),
10248 spec_cu);
10249 }
10250
10251 attr = dwarf2_attr (die, DW_AT_external, cu);
10252 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10253 && die->parent->tag != DW_TAG_module)
10254 return 0;
10255 /* A variable in a lexical block of some kind does not need a
10256 namespace, even though in C++ such variables may be external
10257 and have a mangled name. */
10258 if (die->parent->tag == DW_TAG_lexical_block
10259 || die->parent->tag == DW_TAG_try_block
10260 || die->parent->tag == DW_TAG_catch_block
10261 || die->parent->tag == DW_TAG_subprogram)
10262 return 0;
10263 return 1;
10264
10265 default:
10266 return 0;
10267 }
10268 }
10269
10270 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10271 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10272 defined for the given DIE. */
10273
10274 static struct attribute *
10275 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10276 {
10277 struct attribute *attr;
10278
10279 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10280 if (attr == NULL)
10281 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10282
10283 return attr;
10284 }
10285
10286 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10287 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10288 defined for the given DIE. */
10289
10290 static const char *
10291 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10292 {
10293 const char *linkage_name;
10294
10295 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10296 if (linkage_name == NULL)
10297 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10298
10299 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10300 See https://github.com/rust-lang/rust/issues/32925. */
10301 if (cu->language == language_rust && linkage_name != NULL
10302 && strchr (linkage_name, '{') != NULL)
10303 linkage_name = NULL;
10304
10305 return linkage_name;
10306 }
10307
10308 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10309 compute the physname for the object, which include a method's:
10310 - formal parameters (C++),
10311 - receiver type (Go),
10312
10313 The term "physname" is a bit confusing.
10314 For C++, for example, it is the demangled name.
10315 For Go, for example, it's the mangled name.
10316
10317 For Ada, return the DIE's linkage name rather than the fully qualified
10318 name. PHYSNAME is ignored..
10319
10320 The result is allocated on the objfile->per_bfd's obstack and
10321 canonicalized. */
10322
10323 static const char *
10324 dwarf2_compute_name (const char *name,
10325 struct die_info *die, struct dwarf2_cu *cu,
10326 int physname)
10327 {
10328 struct objfile *objfile = cu->per_objfile->objfile;
10329
10330 if (name == NULL)
10331 name = dwarf2_name (die, cu);
10332
10333 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10334 but otherwise compute it by typename_concat inside GDB.
10335 FIXME: Actually this is not really true, or at least not always true.
10336 It's all very confusing. compute_and_set_names doesn't try to demangle
10337 Fortran names because there is no mangling standard. So new_symbol
10338 will set the demangled name to the result of dwarf2_full_name, and it is
10339 the demangled name that GDB uses if it exists. */
10340 if (cu->language == language_ada
10341 || (cu->language == language_fortran && physname))
10342 {
10343 /* For Ada unit, we prefer the linkage name over the name, as
10344 the former contains the exported name, which the user expects
10345 to be able to reference. Ideally, we want the user to be able
10346 to reference this entity using either natural or linkage name,
10347 but we haven't started looking at this enhancement yet. */
10348 const char *linkage_name = dw2_linkage_name (die, cu);
10349
10350 if (linkage_name != NULL)
10351 return linkage_name;
10352 }
10353
10354 /* These are the only languages we know how to qualify names in. */
10355 if (name != NULL
10356 && (cu->language == language_cplus
10357 || cu->language == language_fortran || cu->language == language_d
10358 || cu->language == language_rust))
10359 {
10360 if (die_needs_namespace (die, cu))
10361 {
10362 const char *prefix;
10363 const char *canonical_name = NULL;
10364
10365 string_file buf;
10366
10367 prefix = determine_prefix (die, cu);
10368 if (*prefix != '\0')
10369 {
10370 gdb::unique_xmalloc_ptr<char> prefixed_name
10371 (typename_concat (NULL, prefix, name, physname, cu));
10372
10373 buf.puts (prefixed_name.get ());
10374 }
10375 else
10376 buf.puts (name);
10377
10378 /* Template parameters may be specified in the DIE's DW_AT_name, or
10379 as children with DW_TAG_template_type_param or
10380 DW_TAG_value_type_param. If the latter, add them to the name
10381 here. If the name already has template parameters, then
10382 skip this step; some versions of GCC emit both, and
10383 it is more efficient to use the pre-computed name.
10384
10385 Something to keep in mind about this process: it is very
10386 unlikely, or in some cases downright impossible, to produce
10387 something that will match the mangled name of a function.
10388 If the definition of the function has the same debug info,
10389 we should be able to match up with it anyway. But fallbacks
10390 using the minimal symbol, for instance to find a method
10391 implemented in a stripped copy of libstdc++, will not work.
10392 If we do not have debug info for the definition, we will have to
10393 match them up some other way.
10394
10395 When we do name matching there is a related problem with function
10396 templates; two instantiated function templates are allowed to
10397 differ only by their return types, which we do not add here. */
10398
10399 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10400 {
10401 struct attribute *attr;
10402 struct die_info *child;
10403 int first = 1;
10404
10405 die->building_fullname = 1;
10406
10407 for (child = die->child; child != NULL; child = child->sibling)
10408 {
10409 struct type *type;
10410 LONGEST value;
10411 const gdb_byte *bytes;
10412 struct dwarf2_locexpr_baton *baton;
10413 struct value *v;
10414
10415 if (child->tag != DW_TAG_template_type_param
10416 && child->tag != DW_TAG_template_value_param)
10417 continue;
10418
10419 if (first)
10420 {
10421 buf.puts ("<");
10422 first = 0;
10423 }
10424 else
10425 buf.puts (", ");
10426
10427 attr = dwarf2_attr (child, DW_AT_type, cu);
10428 if (attr == NULL)
10429 {
10430 complaint (_("template parameter missing DW_AT_type"));
10431 buf.puts ("UNKNOWN_TYPE");
10432 continue;
10433 }
10434 type = die_type (child, cu);
10435
10436 if (child->tag == DW_TAG_template_type_param)
10437 {
10438 c_print_type (type, "", &buf, -1, 0, cu->language,
10439 &type_print_raw_options);
10440 continue;
10441 }
10442
10443 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10444 if (attr == NULL)
10445 {
10446 complaint (_("template parameter missing "
10447 "DW_AT_const_value"));
10448 buf.puts ("UNKNOWN_VALUE");
10449 continue;
10450 }
10451
10452 dwarf2_const_value_attr (attr, type, name,
10453 &cu->comp_unit_obstack, cu,
10454 &value, &bytes, &baton);
10455
10456 if (TYPE_NOSIGN (type))
10457 /* GDB prints characters as NUMBER 'CHAR'. If that's
10458 changed, this can use value_print instead. */
10459 c_printchar (value, type, &buf);
10460 else
10461 {
10462 struct value_print_options opts;
10463
10464 if (baton != NULL)
10465 v = dwarf2_evaluate_loc_desc (type, NULL,
10466 baton->data,
10467 baton->size,
10468 baton->per_cu,
10469 baton->per_objfile);
10470 else if (bytes != NULL)
10471 {
10472 v = allocate_value (type);
10473 memcpy (value_contents_writeable (v), bytes,
10474 TYPE_LENGTH (type));
10475 }
10476 else
10477 v = value_from_longest (type, value);
10478
10479 /* Specify decimal so that we do not depend on
10480 the radix. */
10481 get_formatted_print_options (&opts, 'd');
10482 opts.raw = 1;
10483 value_print (v, &buf, &opts);
10484 release_value (v);
10485 }
10486 }
10487
10488 die->building_fullname = 0;
10489
10490 if (!first)
10491 {
10492 /* Close the argument list, with a space if necessary
10493 (nested templates). */
10494 if (!buf.empty () && buf.string ().back () == '>')
10495 buf.puts (" >");
10496 else
10497 buf.puts (">");
10498 }
10499 }
10500
10501 /* For C++ methods, append formal parameter type
10502 information, if PHYSNAME. */
10503
10504 if (physname && die->tag == DW_TAG_subprogram
10505 && cu->language == language_cplus)
10506 {
10507 struct type *type = read_type_die (die, cu);
10508
10509 c_type_print_args (type, &buf, 1, cu->language,
10510 &type_print_raw_options);
10511
10512 if (cu->language == language_cplus)
10513 {
10514 /* Assume that an artificial first parameter is
10515 "this", but do not crash if it is not. RealView
10516 marks unnamed (and thus unused) parameters as
10517 artificial; there is no way to differentiate
10518 the two cases. */
10519 if (type->num_fields () > 0
10520 && TYPE_FIELD_ARTIFICIAL (type, 0)
10521 && type->field (0).type ()->code () == TYPE_CODE_PTR
10522 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10523 buf.puts (" const");
10524 }
10525 }
10526
10527 const std::string &intermediate_name = buf.string ();
10528
10529 if (cu->language == language_cplus)
10530 canonical_name
10531 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10532 objfile);
10533
10534 /* If we only computed INTERMEDIATE_NAME, or if
10535 INTERMEDIATE_NAME is already canonical, then we need to
10536 intern it. */
10537 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10538 name = objfile->intern (intermediate_name);
10539 else
10540 name = canonical_name;
10541 }
10542 }
10543
10544 return name;
10545 }
10546
10547 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10548 If scope qualifiers are appropriate they will be added. The result
10549 will be allocated on the storage_obstack, or NULL if the DIE does
10550 not have a name. NAME may either be from a previous call to
10551 dwarf2_name or NULL.
10552
10553 The output string will be canonicalized (if C++). */
10554
10555 static const char *
10556 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10557 {
10558 return dwarf2_compute_name (name, die, cu, 0);
10559 }
10560
10561 /* Construct a physname for the given DIE in CU. NAME may either be
10562 from a previous call to dwarf2_name or NULL. The result will be
10563 allocated on the objfile_objstack or NULL if the DIE does not have a
10564 name.
10565
10566 The output string will be canonicalized (if C++). */
10567
10568 static const char *
10569 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10570 {
10571 struct objfile *objfile = cu->per_objfile->objfile;
10572 const char *retval, *mangled = NULL, *canon = NULL;
10573 int need_copy = 1;
10574
10575 /* In this case dwarf2_compute_name is just a shortcut not building anything
10576 on its own. */
10577 if (!die_needs_namespace (die, cu))
10578 return dwarf2_compute_name (name, die, cu, 1);
10579
10580 if (cu->language != language_rust)
10581 mangled = dw2_linkage_name (die, cu);
10582
10583 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10584 has computed. */
10585 gdb::unique_xmalloc_ptr<char> demangled;
10586 if (mangled != NULL)
10587 {
10588
10589 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10590 {
10591 /* Do nothing (do not demangle the symbol name). */
10592 }
10593 else if (cu->language == language_go)
10594 {
10595 /* This is a lie, but we already lie to the caller new_symbol.
10596 new_symbol assumes we return the mangled name.
10597 This just undoes that lie until things are cleaned up. */
10598 }
10599 else
10600 {
10601 /* Use DMGL_RET_DROP for C++ template functions to suppress
10602 their return type. It is easier for GDB users to search
10603 for such functions as `name(params)' than `long name(params)'.
10604 In such case the minimal symbol names do not match the full
10605 symbol names but for template functions there is never a need
10606 to look up their definition from their declaration so
10607 the only disadvantage remains the minimal symbol variant
10608 `long name(params)' does not have the proper inferior type. */
10609 demangled.reset (gdb_demangle (mangled,
10610 (DMGL_PARAMS | DMGL_ANSI
10611 | DMGL_RET_DROP)));
10612 }
10613 if (demangled)
10614 canon = demangled.get ();
10615 else
10616 {
10617 canon = mangled;
10618 need_copy = 0;
10619 }
10620 }
10621
10622 if (canon == NULL || check_physname)
10623 {
10624 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10625
10626 if (canon != NULL && strcmp (physname, canon) != 0)
10627 {
10628 /* It may not mean a bug in GDB. The compiler could also
10629 compute DW_AT_linkage_name incorrectly. But in such case
10630 GDB would need to be bug-to-bug compatible. */
10631
10632 complaint (_("Computed physname <%s> does not match demangled <%s> "
10633 "(from linkage <%s>) - DIE at %s [in module %s]"),
10634 physname, canon, mangled, sect_offset_str (die->sect_off),
10635 objfile_name (objfile));
10636
10637 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10638 is available here - over computed PHYSNAME. It is safer
10639 against both buggy GDB and buggy compilers. */
10640
10641 retval = canon;
10642 }
10643 else
10644 {
10645 retval = physname;
10646 need_copy = 0;
10647 }
10648 }
10649 else
10650 retval = canon;
10651
10652 if (need_copy)
10653 retval = objfile->intern (retval);
10654
10655 return retval;
10656 }
10657
10658 /* Inspect DIE in CU for a namespace alias. If one exists, record
10659 a new symbol for it.
10660
10661 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10662
10663 static int
10664 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10665 {
10666 struct attribute *attr;
10667
10668 /* If the die does not have a name, this is not a namespace
10669 alias. */
10670 attr = dwarf2_attr (die, DW_AT_name, cu);
10671 if (attr != NULL)
10672 {
10673 int num;
10674 struct die_info *d = die;
10675 struct dwarf2_cu *imported_cu = cu;
10676
10677 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10678 keep inspecting DIEs until we hit the underlying import. */
10679 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10680 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10681 {
10682 attr = dwarf2_attr (d, DW_AT_import, cu);
10683 if (attr == NULL)
10684 break;
10685
10686 d = follow_die_ref (d, attr, &imported_cu);
10687 if (d->tag != DW_TAG_imported_declaration)
10688 break;
10689 }
10690
10691 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10692 {
10693 complaint (_("DIE at %s has too many recursively imported "
10694 "declarations"), sect_offset_str (d->sect_off));
10695 return 0;
10696 }
10697
10698 if (attr != NULL)
10699 {
10700 struct type *type;
10701 sect_offset sect_off = attr->get_ref_die_offset ();
10702
10703 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10704 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10705 {
10706 /* This declaration is a global namespace alias. Add
10707 a symbol for it whose type is the aliased namespace. */
10708 new_symbol (die, type, cu);
10709 return 1;
10710 }
10711 }
10712 }
10713
10714 return 0;
10715 }
10716
10717 /* Return the using directives repository (global or local?) to use in the
10718 current context for CU.
10719
10720 For Ada, imported declarations can materialize renamings, which *may* be
10721 global. However it is impossible (for now?) in DWARF to distinguish
10722 "external" imported declarations and "static" ones. As all imported
10723 declarations seem to be static in all other languages, make them all CU-wide
10724 global only in Ada. */
10725
10726 static struct using_direct **
10727 using_directives (struct dwarf2_cu *cu)
10728 {
10729 if (cu->language == language_ada
10730 && cu->get_builder ()->outermost_context_p ())
10731 return cu->get_builder ()->get_global_using_directives ();
10732 else
10733 return cu->get_builder ()->get_local_using_directives ();
10734 }
10735
10736 /* Read the import statement specified by the given die and record it. */
10737
10738 static void
10739 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10740 {
10741 struct objfile *objfile = cu->per_objfile->objfile;
10742 struct attribute *import_attr;
10743 struct die_info *imported_die, *child_die;
10744 struct dwarf2_cu *imported_cu;
10745 const char *imported_name;
10746 const char *imported_name_prefix;
10747 const char *canonical_name;
10748 const char *import_alias;
10749 const char *imported_declaration = NULL;
10750 const char *import_prefix;
10751 std::vector<const char *> excludes;
10752
10753 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10754 if (import_attr == NULL)
10755 {
10756 complaint (_("Tag '%s' has no DW_AT_import"),
10757 dwarf_tag_name (die->tag));
10758 return;
10759 }
10760
10761 imported_cu = cu;
10762 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10763 imported_name = dwarf2_name (imported_die, imported_cu);
10764 if (imported_name == NULL)
10765 {
10766 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10767
10768 The import in the following code:
10769 namespace A
10770 {
10771 typedef int B;
10772 }
10773
10774 int main ()
10775 {
10776 using A::B;
10777 B b;
10778 return b;
10779 }
10780
10781 ...
10782 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10783 <52> DW_AT_decl_file : 1
10784 <53> DW_AT_decl_line : 6
10785 <54> DW_AT_import : <0x75>
10786 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10787 <59> DW_AT_name : B
10788 <5b> DW_AT_decl_file : 1
10789 <5c> DW_AT_decl_line : 2
10790 <5d> DW_AT_type : <0x6e>
10791 ...
10792 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10793 <76> DW_AT_byte_size : 4
10794 <77> DW_AT_encoding : 5 (signed)
10795
10796 imports the wrong die ( 0x75 instead of 0x58 ).
10797 This case will be ignored until the gcc bug is fixed. */
10798 return;
10799 }
10800
10801 /* Figure out the local name after import. */
10802 import_alias = dwarf2_name (die, cu);
10803
10804 /* Figure out where the statement is being imported to. */
10805 import_prefix = determine_prefix (die, cu);
10806
10807 /* Figure out what the scope of the imported die is and prepend it
10808 to the name of the imported die. */
10809 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10810
10811 if (imported_die->tag != DW_TAG_namespace
10812 && imported_die->tag != DW_TAG_module)
10813 {
10814 imported_declaration = imported_name;
10815 canonical_name = imported_name_prefix;
10816 }
10817 else if (strlen (imported_name_prefix) > 0)
10818 canonical_name = obconcat (&objfile->objfile_obstack,
10819 imported_name_prefix,
10820 (cu->language == language_d ? "." : "::"),
10821 imported_name, (char *) NULL);
10822 else
10823 canonical_name = imported_name;
10824
10825 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10826 for (child_die = die->child; child_die && child_die->tag;
10827 child_die = child_die->sibling)
10828 {
10829 /* DWARF-4: A Fortran use statement with a “rename list” may be
10830 represented by an imported module entry with an import attribute
10831 referring to the module and owned entries corresponding to those
10832 entities that are renamed as part of being imported. */
10833
10834 if (child_die->tag != DW_TAG_imported_declaration)
10835 {
10836 complaint (_("child DW_TAG_imported_declaration expected "
10837 "- DIE at %s [in module %s]"),
10838 sect_offset_str (child_die->sect_off),
10839 objfile_name (objfile));
10840 continue;
10841 }
10842
10843 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10844 if (import_attr == NULL)
10845 {
10846 complaint (_("Tag '%s' has no DW_AT_import"),
10847 dwarf_tag_name (child_die->tag));
10848 continue;
10849 }
10850
10851 imported_cu = cu;
10852 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10853 &imported_cu);
10854 imported_name = dwarf2_name (imported_die, imported_cu);
10855 if (imported_name == NULL)
10856 {
10857 complaint (_("child DW_TAG_imported_declaration has unknown "
10858 "imported name - DIE at %s [in module %s]"),
10859 sect_offset_str (child_die->sect_off),
10860 objfile_name (objfile));
10861 continue;
10862 }
10863
10864 excludes.push_back (imported_name);
10865
10866 process_die (child_die, cu);
10867 }
10868
10869 add_using_directive (using_directives (cu),
10870 import_prefix,
10871 canonical_name,
10872 import_alias,
10873 imported_declaration,
10874 excludes,
10875 0,
10876 &objfile->objfile_obstack);
10877 }
10878
10879 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10880 types, but gives them a size of zero. Starting with version 14,
10881 ICC is compatible with GCC. */
10882
10883 static bool
10884 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10885 {
10886 if (!cu->checked_producer)
10887 check_producer (cu);
10888
10889 return cu->producer_is_icc_lt_14;
10890 }
10891
10892 /* ICC generates a DW_AT_type for C void functions. This was observed on
10893 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10894 which says that void functions should not have a DW_AT_type. */
10895
10896 static bool
10897 producer_is_icc (struct dwarf2_cu *cu)
10898 {
10899 if (!cu->checked_producer)
10900 check_producer (cu);
10901
10902 return cu->producer_is_icc;
10903 }
10904
10905 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10906 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10907 this, it was first present in GCC release 4.3.0. */
10908
10909 static bool
10910 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10911 {
10912 if (!cu->checked_producer)
10913 check_producer (cu);
10914
10915 return cu->producer_is_gcc_lt_4_3;
10916 }
10917
10918 static file_and_directory
10919 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10920 {
10921 file_and_directory res;
10922
10923 /* Find the filename. Do not use dwarf2_name here, since the filename
10924 is not a source language identifier. */
10925 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10926 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10927
10928 if (res.comp_dir == NULL
10929 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10930 && IS_ABSOLUTE_PATH (res.name))
10931 {
10932 res.comp_dir_storage = ldirname (res.name);
10933 if (!res.comp_dir_storage.empty ())
10934 res.comp_dir = res.comp_dir_storage.c_str ();
10935 }
10936 if (res.comp_dir != NULL)
10937 {
10938 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10939 directory, get rid of it. */
10940 const char *cp = strchr (res.comp_dir, ':');
10941
10942 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10943 res.comp_dir = cp + 1;
10944 }
10945
10946 if (res.name == NULL)
10947 res.name = "<unknown>";
10948
10949 return res;
10950 }
10951
10952 /* Handle DW_AT_stmt_list for a compilation unit.
10953 DIE is the DW_TAG_compile_unit die for CU.
10954 COMP_DIR is the compilation directory. LOWPC is passed to
10955 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10956
10957 static void
10958 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10959 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10960 {
10961 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10962 struct attribute *attr;
10963 struct line_header line_header_local;
10964 hashval_t line_header_local_hash;
10965 void **slot;
10966 int decode_mapping;
10967
10968 gdb_assert (! cu->per_cu->is_debug_types);
10969
10970 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10971 if (attr == NULL)
10972 return;
10973
10974 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10975
10976 /* The line header hash table is only created if needed (it exists to
10977 prevent redundant reading of the line table for partial_units).
10978 If we're given a partial_unit, we'll need it. If we're given a
10979 compile_unit, then use the line header hash table if it's already
10980 created, but don't create one just yet. */
10981
10982 if (per_objfile->line_header_hash == NULL
10983 && die->tag == DW_TAG_partial_unit)
10984 {
10985 per_objfile->line_header_hash
10986 .reset (htab_create_alloc (127, line_header_hash_voidp,
10987 line_header_eq_voidp,
10988 free_line_header_voidp,
10989 xcalloc, xfree));
10990 }
10991
10992 line_header_local.sect_off = line_offset;
10993 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10994 line_header_local_hash = line_header_hash (&line_header_local);
10995 if (per_objfile->line_header_hash != NULL)
10996 {
10997 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10998 &line_header_local,
10999 line_header_local_hash, NO_INSERT);
11000
11001 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11002 is not present in *SLOT (since if there is something in *SLOT then
11003 it will be for a partial_unit). */
11004 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11005 {
11006 gdb_assert (*slot != NULL);
11007 cu->line_header = (struct line_header *) *slot;
11008 return;
11009 }
11010 }
11011
11012 /* dwarf_decode_line_header does not yet provide sufficient information.
11013 We always have to call also dwarf_decode_lines for it. */
11014 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11015 if (lh == NULL)
11016 return;
11017
11018 cu->line_header = lh.release ();
11019 cu->line_header_die_owner = die;
11020
11021 if (per_objfile->line_header_hash == NULL)
11022 slot = NULL;
11023 else
11024 {
11025 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11026 &line_header_local,
11027 line_header_local_hash, INSERT);
11028 gdb_assert (slot != NULL);
11029 }
11030 if (slot != NULL && *slot == NULL)
11031 {
11032 /* This newly decoded line number information unit will be owned
11033 by line_header_hash hash table. */
11034 *slot = cu->line_header;
11035 cu->line_header_die_owner = NULL;
11036 }
11037 else
11038 {
11039 /* We cannot free any current entry in (*slot) as that struct line_header
11040 may be already used by multiple CUs. Create only temporary decoded
11041 line_header for this CU - it may happen at most once for each line
11042 number information unit. And if we're not using line_header_hash
11043 then this is what we want as well. */
11044 gdb_assert (die->tag != DW_TAG_partial_unit);
11045 }
11046 decode_mapping = (die->tag != DW_TAG_partial_unit);
11047 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11048 decode_mapping);
11049
11050 }
11051
11052 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11053
11054 static void
11055 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11056 {
11057 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11058 struct objfile *objfile = per_objfile->objfile;
11059 struct gdbarch *gdbarch = objfile->arch ();
11060 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11061 CORE_ADDR highpc = ((CORE_ADDR) 0);
11062 struct attribute *attr;
11063 struct die_info *child_die;
11064 CORE_ADDR baseaddr;
11065
11066 prepare_one_comp_unit (cu, die, cu->language);
11067 baseaddr = objfile->text_section_offset ();
11068
11069 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11070
11071 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11072 from finish_block. */
11073 if (lowpc == ((CORE_ADDR) -1))
11074 lowpc = highpc;
11075 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11076
11077 file_and_directory fnd = find_file_and_directory (die, cu);
11078
11079 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11080 standardised yet. As a workaround for the language detection we fall
11081 back to the DW_AT_producer string. */
11082 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11083 cu->language = language_opencl;
11084
11085 /* Similar hack for Go. */
11086 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11087 set_cu_language (DW_LANG_Go, cu);
11088
11089 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11090
11091 /* Decode line number information if present. We do this before
11092 processing child DIEs, so that the line header table is available
11093 for DW_AT_decl_file. */
11094 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11095
11096 /* Process all dies in compilation unit. */
11097 if (die->child != NULL)
11098 {
11099 child_die = die->child;
11100 while (child_die && child_die->tag)
11101 {
11102 process_die (child_die, cu);
11103 child_die = child_die->sibling;
11104 }
11105 }
11106
11107 /* Decode macro information, if present. Dwarf 2 macro information
11108 refers to information in the line number info statement program
11109 header, so we can only read it if we've read the header
11110 successfully. */
11111 attr = dwarf2_attr (die, DW_AT_macros, cu);
11112 if (attr == NULL)
11113 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11114 if (attr && cu->line_header)
11115 {
11116 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11117 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11118
11119 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11120 }
11121 else
11122 {
11123 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11124 if (attr && cu->line_header)
11125 {
11126 unsigned int macro_offset = DW_UNSND (attr);
11127
11128 dwarf_decode_macros (cu, macro_offset, 0);
11129 }
11130 }
11131 }
11132
11133 void
11134 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11135 {
11136 struct type_unit_group *tu_group;
11137 int first_time;
11138 struct attribute *attr;
11139 unsigned int i;
11140 struct signatured_type *sig_type;
11141
11142 gdb_assert (per_cu->is_debug_types);
11143 sig_type = (struct signatured_type *) per_cu;
11144
11145 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11146
11147 /* If we're using .gdb_index (includes -readnow) then
11148 per_cu->type_unit_group may not have been set up yet. */
11149 if (sig_type->type_unit_group == NULL)
11150 sig_type->type_unit_group = get_type_unit_group (this, attr);
11151 tu_group = sig_type->type_unit_group;
11152
11153 /* If we've already processed this stmt_list there's no real need to
11154 do it again, we could fake it and just recreate the part we need
11155 (file name,index -> symtab mapping). If data shows this optimization
11156 is useful we can do it then. */
11157 type_unit_group_unshareable *tug_unshare
11158 = per_objfile->get_type_unit_group_unshareable (tu_group);
11159 first_time = tug_unshare->compunit_symtab == NULL;
11160
11161 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11162 debug info. */
11163 line_header_up lh;
11164 if (attr != NULL)
11165 {
11166 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11167 lh = dwarf_decode_line_header (line_offset, this);
11168 }
11169 if (lh == NULL)
11170 {
11171 if (first_time)
11172 start_symtab ("", NULL, 0);
11173 else
11174 {
11175 gdb_assert (tug_unshare->symtabs == NULL);
11176 gdb_assert (m_builder == nullptr);
11177 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11178 m_builder.reset (new struct buildsym_compunit
11179 (COMPUNIT_OBJFILE (cust), "",
11180 COMPUNIT_DIRNAME (cust),
11181 compunit_language (cust),
11182 0, cust));
11183 list_in_scope = get_builder ()->get_file_symbols ();
11184 }
11185 return;
11186 }
11187
11188 line_header = lh.release ();
11189 line_header_die_owner = die;
11190
11191 if (first_time)
11192 {
11193 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11194
11195 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11196 still initializing it, and our caller (a few levels up)
11197 process_full_type_unit still needs to know if this is the first
11198 time. */
11199
11200 tug_unshare->symtabs
11201 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11202 struct symtab *, line_header->file_names_size ());
11203
11204 auto &file_names = line_header->file_names ();
11205 for (i = 0; i < file_names.size (); ++i)
11206 {
11207 file_entry &fe = file_names[i];
11208 dwarf2_start_subfile (this, fe.name,
11209 fe.include_dir (line_header));
11210 buildsym_compunit *b = get_builder ();
11211 if (b->get_current_subfile ()->symtab == NULL)
11212 {
11213 /* NOTE: start_subfile will recognize when it's been
11214 passed a file it has already seen. So we can't
11215 assume there's a simple mapping from
11216 cu->line_header->file_names to subfiles, plus
11217 cu->line_header->file_names may contain dups. */
11218 b->get_current_subfile ()->symtab
11219 = allocate_symtab (cust, b->get_current_subfile ()->name);
11220 }
11221
11222 fe.symtab = b->get_current_subfile ()->symtab;
11223 tug_unshare->symtabs[i] = fe.symtab;
11224 }
11225 }
11226 else
11227 {
11228 gdb_assert (m_builder == nullptr);
11229 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11230 m_builder.reset (new struct buildsym_compunit
11231 (COMPUNIT_OBJFILE (cust), "",
11232 COMPUNIT_DIRNAME (cust),
11233 compunit_language (cust),
11234 0, cust));
11235 list_in_scope = get_builder ()->get_file_symbols ();
11236
11237 auto &file_names = line_header->file_names ();
11238 for (i = 0; i < file_names.size (); ++i)
11239 {
11240 file_entry &fe = file_names[i];
11241 fe.symtab = tug_unshare->symtabs[i];
11242 }
11243 }
11244
11245 /* The main symtab is allocated last. Type units don't have DW_AT_name
11246 so they don't have a "real" (so to speak) symtab anyway.
11247 There is later code that will assign the main symtab to all symbols
11248 that don't have one. We need to handle the case of a symbol with a
11249 missing symtab (DW_AT_decl_file) anyway. */
11250 }
11251
11252 /* Process DW_TAG_type_unit.
11253 For TUs we want to skip the first top level sibling if it's not the
11254 actual type being defined by this TU. In this case the first top
11255 level sibling is there to provide context only. */
11256
11257 static void
11258 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11259 {
11260 struct die_info *child_die;
11261
11262 prepare_one_comp_unit (cu, die, language_minimal);
11263
11264 /* Initialize (or reinitialize) the machinery for building symtabs.
11265 We do this before processing child DIEs, so that the line header table
11266 is available for DW_AT_decl_file. */
11267 cu->setup_type_unit_groups (die);
11268
11269 if (die->child != NULL)
11270 {
11271 child_die = die->child;
11272 while (child_die && child_die->tag)
11273 {
11274 process_die (child_die, cu);
11275 child_die = child_die->sibling;
11276 }
11277 }
11278 }
11279 \f
11280 /* DWO/DWP files.
11281
11282 http://gcc.gnu.org/wiki/DebugFission
11283 http://gcc.gnu.org/wiki/DebugFissionDWP
11284
11285 To simplify handling of both DWO files ("object" files with the DWARF info)
11286 and DWP files (a file with the DWOs packaged up into one file), we treat
11287 DWP files as having a collection of virtual DWO files. */
11288
11289 static hashval_t
11290 hash_dwo_file (const void *item)
11291 {
11292 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11293 hashval_t hash;
11294
11295 hash = htab_hash_string (dwo_file->dwo_name);
11296 if (dwo_file->comp_dir != NULL)
11297 hash += htab_hash_string (dwo_file->comp_dir);
11298 return hash;
11299 }
11300
11301 static int
11302 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11303 {
11304 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11305 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11306
11307 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11308 return 0;
11309 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11310 return lhs->comp_dir == rhs->comp_dir;
11311 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11312 }
11313
11314 /* Allocate a hash table for DWO files. */
11315
11316 static htab_up
11317 allocate_dwo_file_hash_table ()
11318 {
11319 auto delete_dwo_file = [] (void *item)
11320 {
11321 struct dwo_file *dwo_file = (struct dwo_file *) item;
11322
11323 delete dwo_file;
11324 };
11325
11326 return htab_up (htab_create_alloc (41,
11327 hash_dwo_file,
11328 eq_dwo_file,
11329 delete_dwo_file,
11330 xcalloc, xfree));
11331 }
11332
11333 /* Lookup DWO file DWO_NAME. */
11334
11335 static void **
11336 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11337 const char *dwo_name,
11338 const char *comp_dir)
11339 {
11340 struct dwo_file find_entry;
11341 void **slot;
11342
11343 if (per_objfile->per_bfd->dwo_files == NULL)
11344 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11345
11346 find_entry.dwo_name = dwo_name;
11347 find_entry.comp_dir = comp_dir;
11348 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11349 INSERT);
11350
11351 return slot;
11352 }
11353
11354 static hashval_t
11355 hash_dwo_unit (const void *item)
11356 {
11357 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11358
11359 /* This drops the top 32 bits of the id, but is ok for a hash. */
11360 return dwo_unit->signature;
11361 }
11362
11363 static int
11364 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11365 {
11366 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11367 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11368
11369 /* The signature is assumed to be unique within the DWO file.
11370 So while object file CU dwo_id's always have the value zero,
11371 that's OK, assuming each object file DWO file has only one CU,
11372 and that's the rule for now. */
11373 return lhs->signature == rhs->signature;
11374 }
11375
11376 /* Allocate a hash table for DWO CUs,TUs.
11377 There is one of these tables for each of CUs,TUs for each DWO file. */
11378
11379 static htab_up
11380 allocate_dwo_unit_table ()
11381 {
11382 /* Start out with a pretty small number.
11383 Generally DWO files contain only one CU and maybe some TUs. */
11384 return htab_up (htab_create_alloc (3,
11385 hash_dwo_unit,
11386 eq_dwo_unit,
11387 NULL, xcalloc, xfree));
11388 }
11389
11390 /* die_reader_func for create_dwo_cu. */
11391
11392 static void
11393 create_dwo_cu_reader (const struct die_reader_specs *reader,
11394 const gdb_byte *info_ptr,
11395 struct die_info *comp_unit_die,
11396 struct dwo_file *dwo_file,
11397 struct dwo_unit *dwo_unit)
11398 {
11399 struct dwarf2_cu *cu = reader->cu;
11400 sect_offset sect_off = cu->per_cu->sect_off;
11401 struct dwarf2_section_info *section = cu->per_cu->section;
11402
11403 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11404 if (!signature.has_value ())
11405 {
11406 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11407 " its dwo_id [in module %s]"),
11408 sect_offset_str (sect_off), dwo_file->dwo_name);
11409 return;
11410 }
11411
11412 dwo_unit->dwo_file = dwo_file;
11413 dwo_unit->signature = *signature;
11414 dwo_unit->section = section;
11415 dwo_unit->sect_off = sect_off;
11416 dwo_unit->length = cu->per_cu->length;
11417
11418 if (dwarf_read_debug)
11419 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11420 sect_offset_str (sect_off),
11421 hex_string (dwo_unit->signature));
11422 }
11423
11424 /* Create the dwo_units for the CUs in a DWO_FILE.
11425 Note: This function processes DWO files only, not DWP files. */
11426
11427 static void
11428 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11429 dwarf2_cu *cu, struct dwo_file &dwo_file,
11430 dwarf2_section_info &section, htab_up &cus_htab)
11431 {
11432 struct objfile *objfile = per_objfile->objfile;
11433 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11434 const gdb_byte *info_ptr, *end_ptr;
11435
11436 section.read (objfile);
11437 info_ptr = section.buffer;
11438
11439 if (info_ptr == NULL)
11440 return;
11441
11442 if (dwarf_read_debug)
11443 {
11444 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11445 section.get_name (),
11446 section.get_file_name ());
11447 }
11448
11449 end_ptr = info_ptr + section.size;
11450 while (info_ptr < end_ptr)
11451 {
11452 struct dwarf2_per_cu_data per_cu;
11453 struct dwo_unit read_unit {};
11454 struct dwo_unit *dwo_unit;
11455 void **slot;
11456 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11457
11458 memset (&per_cu, 0, sizeof (per_cu));
11459 per_cu.per_bfd = per_bfd;
11460 per_cu.is_debug_types = 0;
11461 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11462 per_cu.section = &section;
11463
11464 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11465 if (!reader.dummy_p)
11466 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11467 &dwo_file, &read_unit);
11468 info_ptr += per_cu.length;
11469
11470 // If the unit could not be parsed, skip it.
11471 if (read_unit.dwo_file == NULL)
11472 continue;
11473
11474 if (cus_htab == NULL)
11475 cus_htab = allocate_dwo_unit_table ();
11476
11477 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11478 struct dwo_unit);
11479 *dwo_unit = read_unit;
11480 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11481 gdb_assert (slot != NULL);
11482 if (*slot != NULL)
11483 {
11484 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11485 sect_offset dup_sect_off = dup_cu->sect_off;
11486
11487 complaint (_("debug cu entry at offset %s is duplicate to"
11488 " the entry at offset %s, signature %s"),
11489 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11490 hex_string (dwo_unit->signature));
11491 }
11492 *slot = (void *)dwo_unit;
11493 }
11494 }
11495
11496 /* DWP file .debug_{cu,tu}_index section format:
11497 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11498
11499 DWP Version 1:
11500
11501 Both index sections have the same format, and serve to map a 64-bit
11502 signature to a set of section numbers. Each section begins with a header,
11503 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11504 indexes, and a pool of 32-bit section numbers. The index sections will be
11505 aligned at 8-byte boundaries in the file.
11506
11507 The index section header consists of:
11508
11509 V, 32 bit version number
11510 -, 32 bits unused
11511 N, 32 bit number of compilation units or type units in the index
11512 M, 32 bit number of slots in the hash table
11513
11514 Numbers are recorded using the byte order of the application binary.
11515
11516 The hash table begins at offset 16 in the section, and consists of an array
11517 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11518 order of the application binary). Unused slots in the hash table are 0.
11519 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11520
11521 The parallel table begins immediately after the hash table
11522 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11523 array of 32-bit indexes (using the byte order of the application binary),
11524 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11525 table contains a 32-bit index into the pool of section numbers. For unused
11526 hash table slots, the corresponding entry in the parallel table will be 0.
11527
11528 The pool of section numbers begins immediately following the hash table
11529 (at offset 16 + 12 * M from the beginning of the section). The pool of
11530 section numbers consists of an array of 32-bit words (using the byte order
11531 of the application binary). Each item in the array is indexed starting
11532 from 0. The hash table entry provides the index of the first section
11533 number in the set. Additional section numbers in the set follow, and the
11534 set is terminated by a 0 entry (section number 0 is not used in ELF).
11535
11536 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11537 section must be the first entry in the set, and the .debug_abbrev.dwo must
11538 be the second entry. Other members of the set may follow in any order.
11539
11540 ---
11541
11542 DWP Version 2:
11543
11544 DWP Version 2 combines all the .debug_info, etc. sections into one,
11545 and the entries in the index tables are now offsets into these sections.
11546 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11547 section.
11548
11549 Index Section Contents:
11550 Header
11551 Hash Table of Signatures dwp_hash_table.hash_table
11552 Parallel Table of Indices dwp_hash_table.unit_table
11553 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11554 Table of Section Sizes dwp_hash_table.v2.sizes
11555
11556 The index section header consists of:
11557
11558 V, 32 bit version number
11559 L, 32 bit number of columns in the table of section offsets
11560 N, 32 bit number of compilation units or type units in the index
11561 M, 32 bit number of slots in the hash table
11562
11563 Numbers are recorded using the byte order of the application binary.
11564
11565 The hash table has the same format as version 1.
11566 The parallel table of indices has the same format as version 1,
11567 except that the entries are origin-1 indices into the table of sections
11568 offsets and the table of section sizes.
11569
11570 The table of offsets begins immediately following the parallel table
11571 (at offset 16 + 12 * M from the beginning of the section). The table is
11572 a two-dimensional array of 32-bit words (using the byte order of the
11573 application binary), with L columns and N+1 rows, in row-major order.
11574 Each row in the array is indexed starting from 0. The first row provides
11575 a key to the remaining rows: each column in this row provides an identifier
11576 for a debug section, and the offsets in the same column of subsequent rows
11577 refer to that section. The section identifiers are:
11578
11579 DW_SECT_INFO 1 .debug_info.dwo
11580 DW_SECT_TYPES 2 .debug_types.dwo
11581 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11582 DW_SECT_LINE 4 .debug_line.dwo
11583 DW_SECT_LOC 5 .debug_loc.dwo
11584 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11585 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11586 DW_SECT_MACRO 8 .debug_macro.dwo
11587
11588 The offsets provided by the CU and TU index sections are the base offsets
11589 for the contributions made by each CU or TU to the corresponding section
11590 in the package file. Each CU and TU header contains an abbrev_offset
11591 field, used to find the abbreviations table for that CU or TU within the
11592 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11593 be interpreted as relative to the base offset given in the index section.
11594 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11595 should be interpreted as relative to the base offset for .debug_line.dwo,
11596 and offsets into other debug sections obtained from DWARF attributes should
11597 also be interpreted as relative to the corresponding base offset.
11598
11599 The table of sizes begins immediately following the table of offsets.
11600 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11601 with L columns and N rows, in row-major order. Each row in the array is
11602 indexed starting from 1 (row 0 is shared by the two tables).
11603
11604 ---
11605
11606 Hash table lookup is handled the same in version 1 and 2:
11607
11608 We assume that N and M will not exceed 2^32 - 1.
11609 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11610
11611 Given a 64-bit compilation unit signature or a type signature S, an entry
11612 in the hash table is located as follows:
11613
11614 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11615 the low-order k bits all set to 1.
11616
11617 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11618
11619 3) If the hash table entry at index H matches the signature, use that
11620 entry. If the hash table entry at index H is unused (all zeroes),
11621 terminate the search: the signature is not present in the table.
11622
11623 4) Let H = (H + H') modulo M. Repeat at Step 3.
11624
11625 Because M > N and H' and M are relatively prime, the search is guaranteed
11626 to stop at an unused slot or find the match. */
11627
11628 /* Create a hash table to map DWO IDs to their CU/TU entry in
11629 .debug_{info,types}.dwo in DWP_FILE.
11630 Returns NULL if there isn't one.
11631 Note: This function processes DWP files only, not DWO files. */
11632
11633 static struct dwp_hash_table *
11634 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11635 struct dwp_file *dwp_file, int is_debug_types)
11636 {
11637 struct objfile *objfile = per_objfile->objfile;
11638 bfd *dbfd = dwp_file->dbfd.get ();
11639 const gdb_byte *index_ptr, *index_end;
11640 struct dwarf2_section_info *index;
11641 uint32_t version, nr_columns, nr_units, nr_slots;
11642 struct dwp_hash_table *htab;
11643
11644 if (is_debug_types)
11645 index = &dwp_file->sections.tu_index;
11646 else
11647 index = &dwp_file->sections.cu_index;
11648
11649 if (index->empty ())
11650 return NULL;
11651 index->read (objfile);
11652
11653 index_ptr = index->buffer;
11654 index_end = index_ptr + index->size;
11655
11656 version = read_4_bytes (dbfd, index_ptr);
11657 index_ptr += 4;
11658 if (version == 2)
11659 nr_columns = read_4_bytes (dbfd, index_ptr);
11660 else
11661 nr_columns = 0;
11662 index_ptr += 4;
11663 nr_units = read_4_bytes (dbfd, index_ptr);
11664 index_ptr += 4;
11665 nr_slots = read_4_bytes (dbfd, index_ptr);
11666 index_ptr += 4;
11667
11668 if (version != 1 && version != 2)
11669 {
11670 error (_("Dwarf Error: unsupported DWP file version (%s)"
11671 " [in module %s]"),
11672 pulongest (version), dwp_file->name);
11673 }
11674 if (nr_slots != (nr_slots & -nr_slots))
11675 {
11676 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11677 " is not power of 2 [in module %s]"),
11678 pulongest (nr_slots), dwp_file->name);
11679 }
11680
11681 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11682 htab->version = version;
11683 htab->nr_columns = nr_columns;
11684 htab->nr_units = nr_units;
11685 htab->nr_slots = nr_slots;
11686 htab->hash_table = index_ptr;
11687 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11688
11689 /* Exit early if the table is empty. */
11690 if (nr_slots == 0 || nr_units == 0
11691 || (version == 2 && nr_columns == 0))
11692 {
11693 /* All must be zero. */
11694 if (nr_slots != 0 || nr_units != 0
11695 || (version == 2 && nr_columns != 0))
11696 {
11697 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11698 " all zero [in modules %s]"),
11699 dwp_file->name);
11700 }
11701 return htab;
11702 }
11703
11704 if (version == 1)
11705 {
11706 htab->section_pool.v1.indices =
11707 htab->unit_table + sizeof (uint32_t) * nr_slots;
11708 /* It's harder to decide whether the section is too small in v1.
11709 V1 is deprecated anyway so we punt. */
11710 }
11711 else
11712 {
11713 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11714 int *ids = htab->section_pool.v2.section_ids;
11715 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11716 /* Reverse map for error checking. */
11717 int ids_seen[DW_SECT_MAX + 1];
11718 int i;
11719
11720 if (nr_columns < 2)
11721 {
11722 error (_("Dwarf Error: bad DWP hash table, too few columns"
11723 " in section table [in module %s]"),
11724 dwp_file->name);
11725 }
11726 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11727 {
11728 error (_("Dwarf Error: bad DWP hash table, too many columns"
11729 " in section table [in module %s]"),
11730 dwp_file->name);
11731 }
11732 memset (ids, 255, sizeof_ids);
11733 memset (ids_seen, 255, sizeof (ids_seen));
11734 for (i = 0; i < nr_columns; ++i)
11735 {
11736 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11737
11738 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11739 {
11740 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11741 " in section table [in module %s]"),
11742 id, dwp_file->name);
11743 }
11744 if (ids_seen[id] != -1)
11745 {
11746 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11747 " id %d in section table [in module %s]"),
11748 id, dwp_file->name);
11749 }
11750 ids_seen[id] = i;
11751 ids[i] = id;
11752 }
11753 /* Must have exactly one info or types section. */
11754 if (((ids_seen[DW_SECT_INFO] != -1)
11755 + (ids_seen[DW_SECT_TYPES] != -1))
11756 != 1)
11757 {
11758 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11759 " DWO info/types section [in module %s]"),
11760 dwp_file->name);
11761 }
11762 /* Must have an abbrev section. */
11763 if (ids_seen[DW_SECT_ABBREV] == -1)
11764 {
11765 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11766 " section [in module %s]"),
11767 dwp_file->name);
11768 }
11769 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11770 htab->section_pool.v2.sizes =
11771 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11772 * nr_units * nr_columns);
11773 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11774 * nr_units * nr_columns))
11775 > index_end)
11776 {
11777 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11778 " [in module %s]"),
11779 dwp_file->name);
11780 }
11781 }
11782
11783 return htab;
11784 }
11785
11786 /* Update SECTIONS with the data from SECTP.
11787
11788 This function is like the other "locate" section routines that are
11789 passed to bfd_map_over_sections, but in this context the sections to
11790 read comes from the DWP V1 hash table, not the full ELF section table.
11791
11792 The result is non-zero for success, or zero if an error was found. */
11793
11794 static int
11795 locate_v1_virtual_dwo_sections (asection *sectp,
11796 struct virtual_v1_dwo_sections *sections)
11797 {
11798 const struct dwop_section_names *names = &dwop_section_names;
11799
11800 if (section_is_p (sectp->name, &names->abbrev_dwo))
11801 {
11802 /* There can be only one. */
11803 if (sections->abbrev.s.section != NULL)
11804 return 0;
11805 sections->abbrev.s.section = sectp;
11806 sections->abbrev.size = bfd_section_size (sectp);
11807 }
11808 else if (section_is_p (sectp->name, &names->info_dwo)
11809 || section_is_p (sectp->name, &names->types_dwo))
11810 {
11811 /* There can be only one. */
11812 if (sections->info_or_types.s.section != NULL)
11813 return 0;
11814 sections->info_or_types.s.section = sectp;
11815 sections->info_or_types.size = bfd_section_size (sectp);
11816 }
11817 else if (section_is_p (sectp->name, &names->line_dwo))
11818 {
11819 /* There can be only one. */
11820 if (sections->line.s.section != NULL)
11821 return 0;
11822 sections->line.s.section = sectp;
11823 sections->line.size = bfd_section_size (sectp);
11824 }
11825 else if (section_is_p (sectp->name, &names->loc_dwo))
11826 {
11827 /* There can be only one. */
11828 if (sections->loc.s.section != NULL)
11829 return 0;
11830 sections->loc.s.section = sectp;
11831 sections->loc.size = bfd_section_size (sectp);
11832 }
11833 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11834 {
11835 /* There can be only one. */
11836 if (sections->macinfo.s.section != NULL)
11837 return 0;
11838 sections->macinfo.s.section = sectp;
11839 sections->macinfo.size = bfd_section_size (sectp);
11840 }
11841 else if (section_is_p (sectp->name, &names->macro_dwo))
11842 {
11843 /* There can be only one. */
11844 if (sections->macro.s.section != NULL)
11845 return 0;
11846 sections->macro.s.section = sectp;
11847 sections->macro.size = bfd_section_size (sectp);
11848 }
11849 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11850 {
11851 /* There can be only one. */
11852 if (sections->str_offsets.s.section != NULL)
11853 return 0;
11854 sections->str_offsets.s.section = sectp;
11855 sections->str_offsets.size = bfd_section_size (sectp);
11856 }
11857 else
11858 {
11859 /* No other kind of section is valid. */
11860 return 0;
11861 }
11862
11863 return 1;
11864 }
11865
11866 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11867 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11868 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11869 This is for DWP version 1 files. */
11870
11871 static struct dwo_unit *
11872 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11873 struct dwp_file *dwp_file,
11874 uint32_t unit_index,
11875 const char *comp_dir,
11876 ULONGEST signature, int is_debug_types)
11877 {
11878 const struct dwp_hash_table *dwp_htab =
11879 is_debug_types ? dwp_file->tus : dwp_file->cus;
11880 bfd *dbfd = dwp_file->dbfd.get ();
11881 const char *kind = is_debug_types ? "TU" : "CU";
11882 struct dwo_file *dwo_file;
11883 struct dwo_unit *dwo_unit;
11884 struct virtual_v1_dwo_sections sections;
11885 void **dwo_file_slot;
11886 int i;
11887
11888 gdb_assert (dwp_file->version == 1);
11889
11890 if (dwarf_read_debug)
11891 {
11892 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11893 kind,
11894 pulongest (unit_index), hex_string (signature),
11895 dwp_file->name);
11896 }
11897
11898 /* Fetch the sections of this DWO unit.
11899 Put a limit on the number of sections we look for so that bad data
11900 doesn't cause us to loop forever. */
11901
11902 #define MAX_NR_V1_DWO_SECTIONS \
11903 (1 /* .debug_info or .debug_types */ \
11904 + 1 /* .debug_abbrev */ \
11905 + 1 /* .debug_line */ \
11906 + 1 /* .debug_loc */ \
11907 + 1 /* .debug_str_offsets */ \
11908 + 1 /* .debug_macro or .debug_macinfo */ \
11909 + 1 /* trailing zero */)
11910
11911 memset (&sections, 0, sizeof (sections));
11912
11913 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11914 {
11915 asection *sectp;
11916 uint32_t section_nr =
11917 read_4_bytes (dbfd,
11918 dwp_htab->section_pool.v1.indices
11919 + (unit_index + i) * sizeof (uint32_t));
11920
11921 if (section_nr == 0)
11922 break;
11923 if (section_nr >= dwp_file->num_sections)
11924 {
11925 error (_("Dwarf Error: bad DWP hash table, section number too large"
11926 " [in module %s]"),
11927 dwp_file->name);
11928 }
11929
11930 sectp = dwp_file->elf_sections[section_nr];
11931 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11932 {
11933 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11934 " [in module %s]"),
11935 dwp_file->name);
11936 }
11937 }
11938
11939 if (i < 2
11940 || sections.info_or_types.empty ()
11941 || sections.abbrev.empty ())
11942 {
11943 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11944 " [in module %s]"),
11945 dwp_file->name);
11946 }
11947 if (i == MAX_NR_V1_DWO_SECTIONS)
11948 {
11949 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11950 " [in module %s]"),
11951 dwp_file->name);
11952 }
11953
11954 /* It's easier for the rest of the code if we fake a struct dwo_file and
11955 have dwo_unit "live" in that. At least for now.
11956
11957 The DWP file can be made up of a random collection of CUs and TUs.
11958 However, for each CU + set of TUs that came from the same original DWO
11959 file, we can combine them back into a virtual DWO file to save space
11960 (fewer struct dwo_file objects to allocate). Remember that for really
11961 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11962
11963 std::string virtual_dwo_name =
11964 string_printf ("virtual-dwo/%d-%d-%d-%d",
11965 sections.abbrev.get_id (),
11966 sections.line.get_id (),
11967 sections.loc.get_id (),
11968 sections.str_offsets.get_id ());
11969 /* Can we use an existing virtual DWO file? */
11970 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11971 comp_dir);
11972 /* Create one if necessary. */
11973 if (*dwo_file_slot == NULL)
11974 {
11975 if (dwarf_read_debug)
11976 {
11977 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11978 virtual_dwo_name.c_str ());
11979 }
11980 dwo_file = new struct dwo_file;
11981 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11982 dwo_file->comp_dir = comp_dir;
11983 dwo_file->sections.abbrev = sections.abbrev;
11984 dwo_file->sections.line = sections.line;
11985 dwo_file->sections.loc = sections.loc;
11986 dwo_file->sections.macinfo = sections.macinfo;
11987 dwo_file->sections.macro = sections.macro;
11988 dwo_file->sections.str_offsets = sections.str_offsets;
11989 /* The "str" section is global to the entire DWP file. */
11990 dwo_file->sections.str = dwp_file->sections.str;
11991 /* The info or types section is assigned below to dwo_unit,
11992 there's no need to record it in dwo_file.
11993 Also, we can't simply record type sections in dwo_file because
11994 we record a pointer into the vector in dwo_unit. As we collect more
11995 types we'll grow the vector and eventually have to reallocate space
11996 for it, invalidating all copies of pointers into the previous
11997 contents. */
11998 *dwo_file_slot = dwo_file;
11999 }
12000 else
12001 {
12002 if (dwarf_read_debug)
12003 {
12004 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12005 virtual_dwo_name.c_str ());
12006 }
12007 dwo_file = (struct dwo_file *) *dwo_file_slot;
12008 }
12009
12010 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12011 dwo_unit->dwo_file = dwo_file;
12012 dwo_unit->signature = signature;
12013 dwo_unit->section =
12014 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12015 *dwo_unit->section = sections.info_or_types;
12016 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12017
12018 return dwo_unit;
12019 }
12020
12021 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12022 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12023 piece within that section used by a TU/CU, return a virtual section
12024 of just that piece. */
12025
12026 static struct dwarf2_section_info
12027 create_dwp_v2_section (dwarf2_per_objfile *per_objfile,
12028 struct dwarf2_section_info *section,
12029 bfd_size_type offset, bfd_size_type size)
12030 {
12031 struct dwarf2_section_info result;
12032 asection *sectp;
12033
12034 gdb_assert (section != NULL);
12035 gdb_assert (!section->is_virtual);
12036
12037 memset (&result, 0, sizeof (result));
12038 result.s.containing_section = section;
12039 result.is_virtual = true;
12040
12041 if (size == 0)
12042 return result;
12043
12044 sectp = section->get_bfd_section ();
12045
12046 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12047 bounds of the real section. This is a pretty-rare event, so just
12048 flag an error (easier) instead of a warning and trying to cope. */
12049 if (sectp == NULL
12050 || offset + size > bfd_section_size (sectp))
12051 {
12052 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12053 " in section %s [in module %s]"),
12054 sectp ? bfd_section_name (sectp) : "<unknown>",
12055 objfile_name (per_objfile->objfile));
12056 }
12057
12058 result.virtual_offset = offset;
12059 result.size = size;
12060 return result;
12061 }
12062
12063 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12064 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12065 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12066 This is for DWP version 2 files. */
12067
12068 static struct dwo_unit *
12069 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12070 struct dwp_file *dwp_file,
12071 uint32_t unit_index,
12072 const char *comp_dir,
12073 ULONGEST signature, int is_debug_types)
12074 {
12075 const struct dwp_hash_table *dwp_htab =
12076 is_debug_types ? dwp_file->tus : dwp_file->cus;
12077 bfd *dbfd = dwp_file->dbfd.get ();
12078 const char *kind = is_debug_types ? "TU" : "CU";
12079 struct dwo_file *dwo_file;
12080 struct dwo_unit *dwo_unit;
12081 struct virtual_v2_dwo_sections sections;
12082 void **dwo_file_slot;
12083 int i;
12084
12085 gdb_assert (dwp_file->version == 2);
12086
12087 if (dwarf_read_debug)
12088 {
12089 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12090 kind,
12091 pulongest (unit_index), hex_string (signature),
12092 dwp_file->name);
12093 }
12094
12095 /* Fetch the section offsets of this DWO unit. */
12096
12097 memset (&sections, 0, sizeof (sections));
12098
12099 for (i = 0; i < dwp_htab->nr_columns; ++i)
12100 {
12101 uint32_t offset = read_4_bytes (dbfd,
12102 dwp_htab->section_pool.v2.offsets
12103 + (((unit_index - 1) * dwp_htab->nr_columns
12104 + i)
12105 * sizeof (uint32_t)));
12106 uint32_t size = read_4_bytes (dbfd,
12107 dwp_htab->section_pool.v2.sizes
12108 + (((unit_index - 1) * dwp_htab->nr_columns
12109 + i)
12110 * sizeof (uint32_t)));
12111
12112 switch (dwp_htab->section_pool.v2.section_ids[i])
12113 {
12114 case DW_SECT_INFO:
12115 case DW_SECT_TYPES:
12116 sections.info_or_types_offset = offset;
12117 sections.info_or_types_size = size;
12118 break;
12119 case DW_SECT_ABBREV:
12120 sections.abbrev_offset = offset;
12121 sections.abbrev_size = size;
12122 break;
12123 case DW_SECT_LINE:
12124 sections.line_offset = offset;
12125 sections.line_size = size;
12126 break;
12127 case DW_SECT_LOC:
12128 sections.loc_offset = offset;
12129 sections.loc_size = size;
12130 break;
12131 case DW_SECT_STR_OFFSETS:
12132 sections.str_offsets_offset = offset;
12133 sections.str_offsets_size = size;
12134 break;
12135 case DW_SECT_MACINFO:
12136 sections.macinfo_offset = offset;
12137 sections.macinfo_size = size;
12138 break;
12139 case DW_SECT_MACRO:
12140 sections.macro_offset = offset;
12141 sections.macro_size = size;
12142 break;
12143 }
12144 }
12145
12146 /* It's easier for the rest of the code if we fake a struct dwo_file and
12147 have dwo_unit "live" in that. At least for now.
12148
12149 The DWP file can be made up of a random collection of CUs and TUs.
12150 However, for each CU + set of TUs that came from the same original DWO
12151 file, we can combine them back into a virtual DWO file to save space
12152 (fewer struct dwo_file objects to allocate). Remember that for really
12153 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12154
12155 std::string virtual_dwo_name =
12156 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12157 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12158 (long) (sections.line_size ? sections.line_offset : 0),
12159 (long) (sections.loc_size ? sections.loc_offset : 0),
12160 (long) (sections.str_offsets_size
12161 ? sections.str_offsets_offset : 0));
12162 /* Can we use an existing virtual DWO file? */
12163 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12164 comp_dir);
12165 /* Create one if necessary. */
12166 if (*dwo_file_slot == NULL)
12167 {
12168 if (dwarf_read_debug)
12169 {
12170 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12171 virtual_dwo_name.c_str ());
12172 }
12173 dwo_file = new struct dwo_file;
12174 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12175 dwo_file->comp_dir = comp_dir;
12176 dwo_file->sections.abbrev =
12177 create_dwp_v2_section (per_objfile, &dwp_file->sections.abbrev,
12178 sections.abbrev_offset, sections.abbrev_size);
12179 dwo_file->sections.line =
12180 create_dwp_v2_section (per_objfile, &dwp_file->sections.line,
12181 sections.line_offset, sections.line_size);
12182 dwo_file->sections.loc =
12183 create_dwp_v2_section (per_objfile, &dwp_file->sections.loc,
12184 sections.loc_offset, sections.loc_size);
12185 dwo_file->sections.macinfo =
12186 create_dwp_v2_section (per_objfile, &dwp_file->sections.macinfo,
12187 sections.macinfo_offset, sections.macinfo_size);
12188 dwo_file->sections.macro =
12189 create_dwp_v2_section (per_objfile, &dwp_file->sections.macro,
12190 sections.macro_offset, sections.macro_size);
12191 dwo_file->sections.str_offsets =
12192 create_dwp_v2_section (per_objfile,
12193 &dwp_file->sections.str_offsets,
12194 sections.str_offsets_offset,
12195 sections.str_offsets_size);
12196 /* The "str" section is global to the entire DWP file. */
12197 dwo_file->sections.str = dwp_file->sections.str;
12198 /* The info or types section is assigned below to dwo_unit,
12199 there's no need to record it in dwo_file.
12200 Also, we can't simply record type sections in dwo_file because
12201 we record a pointer into the vector in dwo_unit. As we collect more
12202 types we'll grow the vector and eventually have to reallocate space
12203 for it, invalidating all copies of pointers into the previous
12204 contents. */
12205 *dwo_file_slot = dwo_file;
12206 }
12207 else
12208 {
12209 if (dwarf_read_debug)
12210 {
12211 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12212 virtual_dwo_name.c_str ());
12213 }
12214 dwo_file = (struct dwo_file *) *dwo_file_slot;
12215 }
12216
12217 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12218 dwo_unit->dwo_file = dwo_file;
12219 dwo_unit->signature = signature;
12220 dwo_unit->section =
12221 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12222 *dwo_unit->section = create_dwp_v2_section (per_objfile,
12223 is_debug_types
12224 ? &dwp_file->sections.types
12225 : &dwp_file->sections.info,
12226 sections.info_or_types_offset,
12227 sections.info_or_types_size);
12228 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12229
12230 return dwo_unit;
12231 }
12232
12233 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12234 Returns NULL if the signature isn't found. */
12235
12236 static struct dwo_unit *
12237 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12238 struct dwp_file *dwp_file, const char *comp_dir,
12239 ULONGEST signature, int is_debug_types)
12240 {
12241 const struct dwp_hash_table *dwp_htab =
12242 is_debug_types ? dwp_file->tus : dwp_file->cus;
12243 bfd *dbfd = dwp_file->dbfd.get ();
12244 uint32_t mask = dwp_htab->nr_slots - 1;
12245 uint32_t hash = signature & mask;
12246 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12247 unsigned int i;
12248 void **slot;
12249 struct dwo_unit find_dwo_cu;
12250
12251 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12252 find_dwo_cu.signature = signature;
12253 slot = htab_find_slot (is_debug_types
12254 ? dwp_file->loaded_tus.get ()
12255 : dwp_file->loaded_cus.get (),
12256 &find_dwo_cu, INSERT);
12257
12258 if (*slot != NULL)
12259 return (struct dwo_unit *) *slot;
12260
12261 /* Use a for loop so that we don't loop forever on bad debug info. */
12262 for (i = 0; i < dwp_htab->nr_slots; ++i)
12263 {
12264 ULONGEST signature_in_table;
12265
12266 signature_in_table =
12267 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12268 if (signature_in_table == signature)
12269 {
12270 uint32_t unit_index =
12271 read_4_bytes (dbfd,
12272 dwp_htab->unit_table + hash * sizeof (uint32_t));
12273
12274 if (dwp_file->version == 1)
12275 {
12276 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12277 unit_index, comp_dir,
12278 signature, is_debug_types);
12279 }
12280 else
12281 {
12282 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12283 unit_index, comp_dir,
12284 signature, is_debug_types);
12285 }
12286 return (struct dwo_unit *) *slot;
12287 }
12288 if (signature_in_table == 0)
12289 return NULL;
12290 hash = (hash + hash2) & mask;
12291 }
12292
12293 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12294 " [in module %s]"),
12295 dwp_file->name);
12296 }
12297
12298 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12299 Open the file specified by FILE_NAME and hand it off to BFD for
12300 preliminary analysis. Return a newly initialized bfd *, which
12301 includes a canonicalized copy of FILE_NAME.
12302 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12303 SEARCH_CWD is true if the current directory is to be searched.
12304 It will be searched before debug-file-directory.
12305 If successful, the file is added to the bfd include table of the
12306 objfile's bfd (see gdb_bfd_record_inclusion).
12307 If unable to find/open the file, return NULL.
12308 NOTE: This function is derived from symfile_bfd_open. */
12309
12310 static gdb_bfd_ref_ptr
12311 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12312 const char *file_name, int is_dwp, int search_cwd)
12313 {
12314 int desc;
12315 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12316 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12317 to debug_file_directory. */
12318 const char *search_path;
12319 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12320
12321 gdb::unique_xmalloc_ptr<char> search_path_holder;
12322 if (search_cwd)
12323 {
12324 if (*debug_file_directory != '\0')
12325 {
12326 search_path_holder.reset (concat (".", dirname_separator_string,
12327 debug_file_directory,
12328 (char *) NULL));
12329 search_path = search_path_holder.get ();
12330 }
12331 else
12332 search_path = ".";
12333 }
12334 else
12335 search_path = debug_file_directory;
12336
12337 openp_flags flags = OPF_RETURN_REALPATH;
12338 if (is_dwp)
12339 flags |= OPF_SEARCH_IN_PATH;
12340
12341 gdb::unique_xmalloc_ptr<char> absolute_name;
12342 desc = openp (search_path, flags, file_name,
12343 O_RDONLY | O_BINARY, &absolute_name);
12344 if (desc < 0)
12345 return NULL;
12346
12347 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12348 gnutarget, desc));
12349 if (sym_bfd == NULL)
12350 return NULL;
12351 bfd_set_cacheable (sym_bfd.get (), 1);
12352
12353 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12354 return NULL;
12355
12356 /* Success. Record the bfd as having been included by the objfile's bfd.
12357 This is important because things like demangled_names_hash lives in the
12358 objfile's per_bfd space and may have references to things like symbol
12359 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12360 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12361
12362 return sym_bfd;
12363 }
12364
12365 /* Try to open DWO file FILE_NAME.
12366 COMP_DIR is the DW_AT_comp_dir attribute.
12367 The result is the bfd handle of the file.
12368 If there is a problem finding or opening the file, return NULL.
12369 Upon success, the canonicalized path of the file is stored in the bfd,
12370 same as symfile_bfd_open. */
12371
12372 static gdb_bfd_ref_ptr
12373 open_dwo_file (dwarf2_per_objfile *per_objfile,
12374 const char *file_name, const char *comp_dir)
12375 {
12376 if (IS_ABSOLUTE_PATH (file_name))
12377 return try_open_dwop_file (per_objfile, file_name,
12378 0 /*is_dwp*/, 0 /*search_cwd*/);
12379
12380 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12381
12382 if (comp_dir != NULL)
12383 {
12384 gdb::unique_xmalloc_ptr<char> path_to_try
12385 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12386
12387 /* NOTE: If comp_dir is a relative path, this will also try the
12388 search path, which seems useful. */
12389 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12390 0 /*is_dwp*/,
12391 1 /*search_cwd*/));
12392 if (abfd != NULL)
12393 return abfd;
12394 }
12395
12396 /* That didn't work, try debug-file-directory, which, despite its name,
12397 is a list of paths. */
12398
12399 if (*debug_file_directory == '\0')
12400 return NULL;
12401
12402 return try_open_dwop_file (per_objfile, file_name,
12403 0 /*is_dwp*/, 1 /*search_cwd*/);
12404 }
12405
12406 /* This function is mapped across the sections and remembers the offset and
12407 size of each of the DWO debugging sections we are interested in. */
12408
12409 static void
12410 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12411 {
12412 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12413 const struct dwop_section_names *names = &dwop_section_names;
12414
12415 if (section_is_p (sectp->name, &names->abbrev_dwo))
12416 {
12417 dwo_sections->abbrev.s.section = sectp;
12418 dwo_sections->abbrev.size = bfd_section_size (sectp);
12419 }
12420 else if (section_is_p (sectp->name, &names->info_dwo))
12421 {
12422 dwo_sections->info.s.section = sectp;
12423 dwo_sections->info.size = bfd_section_size (sectp);
12424 }
12425 else if (section_is_p (sectp->name, &names->line_dwo))
12426 {
12427 dwo_sections->line.s.section = sectp;
12428 dwo_sections->line.size = bfd_section_size (sectp);
12429 }
12430 else if (section_is_p (sectp->name, &names->loc_dwo))
12431 {
12432 dwo_sections->loc.s.section = sectp;
12433 dwo_sections->loc.size = bfd_section_size (sectp);
12434 }
12435 else if (section_is_p (sectp->name, &names->loclists_dwo))
12436 {
12437 dwo_sections->loclists.s.section = sectp;
12438 dwo_sections->loclists.size = bfd_section_size (sectp);
12439 }
12440 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12441 {
12442 dwo_sections->macinfo.s.section = sectp;
12443 dwo_sections->macinfo.size = bfd_section_size (sectp);
12444 }
12445 else if (section_is_p (sectp->name, &names->macro_dwo))
12446 {
12447 dwo_sections->macro.s.section = sectp;
12448 dwo_sections->macro.size = bfd_section_size (sectp);
12449 }
12450 else if (section_is_p (sectp->name, &names->str_dwo))
12451 {
12452 dwo_sections->str.s.section = sectp;
12453 dwo_sections->str.size = bfd_section_size (sectp);
12454 }
12455 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12456 {
12457 dwo_sections->str_offsets.s.section = sectp;
12458 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12459 }
12460 else if (section_is_p (sectp->name, &names->types_dwo))
12461 {
12462 struct dwarf2_section_info type_section;
12463
12464 memset (&type_section, 0, sizeof (type_section));
12465 type_section.s.section = sectp;
12466 type_section.size = bfd_section_size (sectp);
12467 dwo_sections->types.push_back (type_section);
12468 }
12469 }
12470
12471 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12472 by PER_CU. This is for the non-DWP case.
12473 The result is NULL if DWO_NAME can't be found. */
12474
12475 static struct dwo_file *
12476 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12477 const char *comp_dir)
12478 {
12479 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12480
12481 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12482 if (dbfd == NULL)
12483 {
12484 if (dwarf_read_debug)
12485 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12486 return NULL;
12487 }
12488
12489 dwo_file_up dwo_file (new struct dwo_file);
12490 dwo_file->dwo_name = dwo_name;
12491 dwo_file->comp_dir = comp_dir;
12492 dwo_file->dbfd = std::move (dbfd);
12493
12494 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12495 &dwo_file->sections);
12496
12497 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12498 dwo_file->cus);
12499
12500 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12501 dwo_file->sections.types, dwo_file->tus);
12502
12503 if (dwarf_read_debug)
12504 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12505
12506 return dwo_file.release ();
12507 }
12508
12509 /* This function is mapped across the sections and remembers the offset and
12510 size of each of the DWP debugging sections common to version 1 and 2 that
12511 we are interested in. */
12512
12513 static void
12514 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12515 void *dwp_file_ptr)
12516 {
12517 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12518 const struct dwop_section_names *names = &dwop_section_names;
12519 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12520
12521 /* Record the ELF section number for later lookup: this is what the
12522 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12523 gdb_assert (elf_section_nr < dwp_file->num_sections);
12524 dwp_file->elf_sections[elf_section_nr] = sectp;
12525
12526 /* Look for specific sections that we need. */
12527 if (section_is_p (sectp->name, &names->str_dwo))
12528 {
12529 dwp_file->sections.str.s.section = sectp;
12530 dwp_file->sections.str.size = bfd_section_size (sectp);
12531 }
12532 else if (section_is_p (sectp->name, &names->cu_index))
12533 {
12534 dwp_file->sections.cu_index.s.section = sectp;
12535 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12536 }
12537 else if (section_is_p (sectp->name, &names->tu_index))
12538 {
12539 dwp_file->sections.tu_index.s.section = sectp;
12540 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12541 }
12542 }
12543
12544 /* This function is mapped across the sections and remembers the offset and
12545 size of each of the DWP version 2 debugging sections that we are interested
12546 in. This is split into a separate function because we don't know if we
12547 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12548
12549 static void
12550 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12551 {
12552 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12553 const struct dwop_section_names *names = &dwop_section_names;
12554 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12555
12556 /* Record the ELF section number for later lookup: this is what the
12557 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12558 gdb_assert (elf_section_nr < dwp_file->num_sections);
12559 dwp_file->elf_sections[elf_section_nr] = sectp;
12560
12561 /* Look for specific sections that we need. */
12562 if (section_is_p (sectp->name, &names->abbrev_dwo))
12563 {
12564 dwp_file->sections.abbrev.s.section = sectp;
12565 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12566 }
12567 else if (section_is_p (sectp->name, &names->info_dwo))
12568 {
12569 dwp_file->sections.info.s.section = sectp;
12570 dwp_file->sections.info.size = bfd_section_size (sectp);
12571 }
12572 else if (section_is_p (sectp->name, &names->line_dwo))
12573 {
12574 dwp_file->sections.line.s.section = sectp;
12575 dwp_file->sections.line.size = bfd_section_size (sectp);
12576 }
12577 else if (section_is_p (sectp->name, &names->loc_dwo))
12578 {
12579 dwp_file->sections.loc.s.section = sectp;
12580 dwp_file->sections.loc.size = bfd_section_size (sectp);
12581 }
12582 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12583 {
12584 dwp_file->sections.macinfo.s.section = sectp;
12585 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12586 }
12587 else if (section_is_p (sectp->name, &names->macro_dwo))
12588 {
12589 dwp_file->sections.macro.s.section = sectp;
12590 dwp_file->sections.macro.size = bfd_section_size (sectp);
12591 }
12592 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12593 {
12594 dwp_file->sections.str_offsets.s.section = sectp;
12595 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12596 }
12597 else if (section_is_p (sectp->name, &names->types_dwo))
12598 {
12599 dwp_file->sections.types.s.section = sectp;
12600 dwp_file->sections.types.size = bfd_section_size (sectp);
12601 }
12602 }
12603
12604 /* Hash function for dwp_file loaded CUs/TUs. */
12605
12606 static hashval_t
12607 hash_dwp_loaded_cutus (const void *item)
12608 {
12609 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12610
12611 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12612 return dwo_unit->signature;
12613 }
12614
12615 /* Equality function for dwp_file loaded CUs/TUs. */
12616
12617 static int
12618 eq_dwp_loaded_cutus (const void *a, const void *b)
12619 {
12620 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12621 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12622
12623 return dua->signature == dub->signature;
12624 }
12625
12626 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12627
12628 static htab_up
12629 allocate_dwp_loaded_cutus_table ()
12630 {
12631 return htab_up (htab_create_alloc (3,
12632 hash_dwp_loaded_cutus,
12633 eq_dwp_loaded_cutus,
12634 NULL, xcalloc, xfree));
12635 }
12636
12637 /* Try to open DWP file FILE_NAME.
12638 The result is the bfd handle of the file.
12639 If there is a problem finding or opening the file, return NULL.
12640 Upon success, the canonicalized path of the file is stored in the bfd,
12641 same as symfile_bfd_open. */
12642
12643 static gdb_bfd_ref_ptr
12644 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12645 {
12646 gdb_bfd_ref_ptr abfd (try_open_dwop_file (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 (per_objfile, lbasename (file_name),
12666 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 (dwarf2_per_objfile *per_objfile)
12679 {
12680 struct objfile *objfile = 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 (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 (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 (&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 (per_objfile, dwp_file.get (), 0);
12731
12732 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12733
12734 /* The DWP file version is stored in the hash table. Oh well. */
12735 if (dwp_file->cus && dwp_file->tus
12736 && dwp_file->cus->version != dwp_file->tus->version)
12737 {
12738 /* Technically speaking, we should try to limp along, but this is
12739 pretty bizarre. We use pulongest here because that's the established
12740 portability solution (e.g, we cannot use %u for uint32_t). */
12741 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12742 " TU version %s [in DWP file %s]"),
12743 pulongest (dwp_file->cus->version),
12744 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12745 }
12746
12747 if (dwp_file->cus)
12748 dwp_file->version = dwp_file->cus->version;
12749 else if (dwp_file->tus)
12750 dwp_file->version = dwp_file->tus->version;
12751 else
12752 dwp_file->version = 2;
12753
12754 if (dwp_file->version == 2)
12755 bfd_map_over_sections (dwp_file->dbfd.get (),
12756 dwarf2_locate_v2_dwp_sections,
12757 dwp_file.get ());
12758
12759 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12760 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12761
12762 if (dwarf_read_debug)
12763 {
12764 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12765 fprintf_unfiltered (gdb_stdlog,
12766 " %s CUs, %s TUs\n",
12767 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12768 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12769 }
12770
12771 return dwp_file;
12772 }
12773
12774 /* Wrapper around open_and_init_dwp_file, only open it once. */
12775
12776 static struct dwp_file *
12777 get_dwp_file (dwarf2_per_objfile *per_objfile)
12778 {
12779 if (!per_objfile->per_bfd->dwp_checked)
12780 {
12781 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12782 per_objfile->per_bfd->dwp_checked = 1;
12783 }
12784 return per_objfile->per_bfd->dwp_file.get ();
12785 }
12786
12787 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12788 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12789 or in the DWP file for the objfile, referenced by THIS_UNIT.
12790 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12791 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12792
12793 This is called, for example, when wanting to read a variable with a
12794 complex location. Therefore we don't want to do file i/o for every call.
12795 Therefore we don't want to look for a DWO file on every call.
12796 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12797 then we check if we've already seen DWO_NAME, and only THEN do we check
12798 for a DWO file.
12799
12800 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12801 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12802
12803 static struct dwo_unit *
12804 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12805 ULONGEST signature, int is_debug_types)
12806 {
12807 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12808 struct objfile *objfile = per_objfile->objfile;
12809 const char *kind = is_debug_types ? "TU" : "CU";
12810 void **dwo_file_slot;
12811 struct dwo_file *dwo_file;
12812 struct dwp_file *dwp_file;
12813
12814 /* First see if there's a DWP file.
12815 If we have a DWP file but didn't find the DWO inside it, don't
12816 look for the original DWO file. It makes gdb behave differently
12817 depending on whether one is debugging in the build tree. */
12818
12819 dwp_file = get_dwp_file (per_objfile);
12820 if (dwp_file != NULL)
12821 {
12822 const struct dwp_hash_table *dwp_htab =
12823 is_debug_types ? dwp_file->tus : dwp_file->cus;
12824
12825 if (dwp_htab != NULL)
12826 {
12827 struct dwo_unit *dwo_cutu =
12828 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12829 is_debug_types);
12830
12831 if (dwo_cutu != NULL)
12832 {
12833 if (dwarf_read_debug)
12834 {
12835 fprintf_unfiltered (gdb_stdlog,
12836 "Virtual DWO %s %s found: @%s\n",
12837 kind, hex_string (signature),
12838 host_address_to_string (dwo_cutu));
12839 }
12840 return dwo_cutu;
12841 }
12842 }
12843 }
12844 else
12845 {
12846 /* No DWP file, look for the DWO file. */
12847
12848 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12849 if (*dwo_file_slot == NULL)
12850 {
12851 /* Read in the file and build a table of the CUs/TUs it contains. */
12852 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12853 }
12854 /* NOTE: This will be NULL if unable to open the file. */
12855 dwo_file = (struct dwo_file *) *dwo_file_slot;
12856
12857 if (dwo_file != NULL)
12858 {
12859 struct dwo_unit *dwo_cutu = NULL;
12860
12861 if (is_debug_types && dwo_file->tus)
12862 {
12863 struct dwo_unit find_dwo_cutu;
12864
12865 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12866 find_dwo_cutu.signature = signature;
12867 dwo_cutu
12868 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12869 &find_dwo_cutu);
12870 }
12871 else if (!is_debug_types && dwo_file->cus)
12872 {
12873 struct dwo_unit find_dwo_cutu;
12874
12875 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12876 find_dwo_cutu.signature = signature;
12877 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12878 &find_dwo_cutu);
12879 }
12880
12881 if (dwo_cutu != NULL)
12882 {
12883 if (dwarf_read_debug)
12884 {
12885 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12886 kind, dwo_name, hex_string (signature),
12887 host_address_to_string (dwo_cutu));
12888 }
12889 return dwo_cutu;
12890 }
12891 }
12892 }
12893
12894 /* We didn't find it. This could mean a dwo_id mismatch, or
12895 someone deleted the DWO/DWP file, or the search path isn't set up
12896 correctly to find the file. */
12897
12898 if (dwarf_read_debug)
12899 {
12900 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12901 kind, dwo_name, hex_string (signature));
12902 }
12903
12904 /* This is a warning and not a complaint because it can be caused by
12905 pilot error (e.g., user accidentally deleting the DWO). */
12906 {
12907 /* Print the name of the DWP file if we looked there, helps the user
12908 better diagnose the problem. */
12909 std::string dwp_text;
12910
12911 if (dwp_file != NULL)
12912 dwp_text = string_printf (" [in DWP file %s]",
12913 lbasename (dwp_file->name));
12914
12915 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12916 " [in module %s]"),
12917 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12918 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12919 }
12920 return NULL;
12921 }
12922
12923 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12924 See lookup_dwo_cutu_unit for details. */
12925
12926 static struct dwo_unit *
12927 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12928 ULONGEST signature)
12929 {
12930 gdb_assert (!cu->per_cu->is_debug_types);
12931
12932 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12933 }
12934
12935 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12936 See lookup_dwo_cutu_unit for details. */
12937
12938 static struct dwo_unit *
12939 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12940 {
12941 gdb_assert (cu->per_cu->is_debug_types);
12942
12943 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12944
12945 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12946 }
12947
12948 /* Traversal function for queue_and_load_all_dwo_tus. */
12949
12950 static int
12951 queue_and_load_dwo_tu (void **slot, void *info)
12952 {
12953 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12954 dwarf2_cu *cu = (dwarf2_cu *) info;
12955 ULONGEST signature = dwo_unit->signature;
12956 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12957
12958 if (sig_type != NULL)
12959 {
12960 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12961
12962 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12963 a real dependency of PER_CU on SIG_TYPE. That is detected later
12964 while processing PER_CU. */
12965 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12966 load_full_type_unit (sig_cu, cu->per_objfile);
12967 cu->per_cu->imported_symtabs_push (sig_cu);
12968 }
12969
12970 return 1;
12971 }
12972
12973 /* Queue all TUs contained in the DWO of CU to be read in.
12974 The DWO may have the only definition of the type, though it may not be
12975 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12976 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12977
12978 static void
12979 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12980 {
12981 struct dwo_unit *dwo_unit;
12982 struct dwo_file *dwo_file;
12983
12984 gdb_assert (cu != nullptr);
12985 gdb_assert (!cu->per_cu->is_debug_types);
12986 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12987
12988 dwo_unit = cu->dwo_unit;
12989 gdb_assert (dwo_unit != NULL);
12990
12991 dwo_file = dwo_unit->dwo_file;
12992 if (dwo_file->tus != NULL)
12993 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12994 }
12995
12996 /* Read in various DIEs. */
12997
12998 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12999 Inherit only the children of the DW_AT_abstract_origin DIE not being
13000 already referenced by DW_AT_abstract_origin from the children of the
13001 current DIE. */
13002
13003 static void
13004 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13005 {
13006 struct die_info *child_die;
13007 sect_offset *offsetp;
13008 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13009 struct die_info *origin_die;
13010 /* Iterator of the ORIGIN_DIE children. */
13011 struct die_info *origin_child_die;
13012 struct attribute *attr;
13013 struct dwarf2_cu *origin_cu;
13014 struct pending **origin_previous_list_in_scope;
13015
13016 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13017 if (!attr)
13018 return;
13019
13020 /* Note that following die references may follow to a die in a
13021 different cu. */
13022
13023 origin_cu = cu;
13024 origin_die = follow_die_ref (die, attr, &origin_cu);
13025
13026 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13027 symbols in. */
13028 origin_previous_list_in_scope = origin_cu->list_in_scope;
13029 origin_cu->list_in_scope = cu->list_in_scope;
13030
13031 if (die->tag != origin_die->tag
13032 && !(die->tag == DW_TAG_inlined_subroutine
13033 && origin_die->tag == DW_TAG_subprogram))
13034 complaint (_("DIE %s and its abstract origin %s have different tags"),
13035 sect_offset_str (die->sect_off),
13036 sect_offset_str (origin_die->sect_off));
13037
13038 std::vector<sect_offset> offsets;
13039
13040 for (child_die = die->child;
13041 child_die && child_die->tag;
13042 child_die = child_die->sibling)
13043 {
13044 struct die_info *child_origin_die;
13045 struct dwarf2_cu *child_origin_cu;
13046
13047 /* We are trying to process concrete instance entries:
13048 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13049 it's not relevant to our analysis here. i.e. detecting DIEs that are
13050 present in the abstract instance but not referenced in the concrete
13051 one. */
13052 if (child_die->tag == DW_TAG_call_site
13053 || child_die->tag == DW_TAG_GNU_call_site)
13054 continue;
13055
13056 /* For each CHILD_DIE, find the corresponding child of
13057 ORIGIN_DIE. If there is more than one layer of
13058 DW_AT_abstract_origin, follow them all; there shouldn't be,
13059 but GCC versions at least through 4.4 generate this (GCC PR
13060 40573). */
13061 child_origin_die = child_die;
13062 child_origin_cu = cu;
13063 while (1)
13064 {
13065 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13066 child_origin_cu);
13067 if (attr == NULL)
13068 break;
13069 child_origin_die = follow_die_ref (child_origin_die, attr,
13070 &child_origin_cu);
13071 }
13072
13073 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13074 counterpart may exist. */
13075 if (child_origin_die != child_die)
13076 {
13077 if (child_die->tag != child_origin_die->tag
13078 && !(child_die->tag == DW_TAG_inlined_subroutine
13079 && child_origin_die->tag == DW_TAG_subprogram))
13080 complaint (_("Child DIE %s and its abstract origin %s have "
13081 "different tags"),
13082 sect_offset_str (child_die->sect_off),
13083 sect_offset_str (child_origin_die->sect_off));
13084 if (child_origin_die->parent != origin_die)
13085 complaint (_("Child DIE %s and its abstract origin %s have "
13086 "different parents"),
13087 sect_offset_str (child_die->sect_off),
13088 sect_offset_str (child_origin_die->sect_off));
13089 else
13090 offsets.push_back (child_origin_die->sect_off);
13091 }
13092 }
13093 std::sort (offsets.begin (), offsets.end ());
13094 sect_offset *offsets_end = offsets.data () + offsets.size ();
13095 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13096 if (offsetp[-1] == *offsetp)
13097 complaint (_("Multiple children of DIE %s refer "
13098 "to DIE %s as their abstract origin"),
13099 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13100
13101 offsetp = offsets.data ();
13102 origin_child_die = origin_die->child;
13103 while (origin_child_die && origin_child_die->tag)
13104 {
13105 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13106 while (offsetp < offsets_end
13107 && *offsetp < origin_child_die->sect_off)
13108 offsetp++;
13109 if (offsetp >= offsets_end
13110 || *offsetp > origin_child_die->sect_off)
13111 {
13112 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13113 Check whether we're already processing ORIGIN_CHILD_DIE.
13114 This can happen with mutually referenced abstract_origins.
13115 PR 16581. */
13116 if (!origin_child_die->in_process)
13117 process_die (origin_child_die, origin_cu);
13118 }
13119 origin_child_die = origin_child_die->sibling;
13120 }
13121 origin_cu->list_in_scope = origin_previous_list_in_scope;
13122
13123 if (cu != origin_cu)
13124 compute_delayed_physnames (origin_cu);
13125 }
13126
13127 static void
13128 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13129 {
13130 struct objfile *objfile = cu->per_objfile->objfile;
13131 struct gdbarch *gdbarch = objfile->arch ();
13132 struct context_stack *newobj;
13133 CORE_ADDR lowpc;
13134 CORE_ADDR highpc;
13135 struct die_info *child_die;
13136 struct attribute *attr, *call_line, *call_file;
13137 const char *name;
13138 CORE_ADDR baseaddr;
13139 struct block *block;
13140 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13141 std::vector<struct symbol *> template_args;
13142 struct template_symbol *templ_func = NULL;
13143
13144 if (inlined_func)
13145 {
13146 /* If we do not have call site information, we can't show the
13147 caller of this inlined function. That's too confusing, so
13148 only use the scope for local variables. */
13149 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13150 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13151 if (call_line == NULL || call_file == NULL)
13152 {
13153 read_lexical_block_scope (die, cu);
13154 return;
13155 }
13156 }
13157
13158 baseaddr = objfile->text_section_offset ();
13159
13160 name = dwarf2_name (die, cu);
13161
13162 /* Ignore functions with missing or empty names. These are actually
13163 illegal according to the DWARF standard. */
13164 if (name == NULL)
13165 {
13166 complaint (_("missing name for subprogram DIE at %s"),
13167 sect_offset_str (die->sect_off));
13168 return;
13169 }
13170
13171 /* Ignore functions with missing or invalid low and high pc attributes. */
13172 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13173 <= PC_BOUNDS_INVALID)
13174 {
13175 attr = dwarf2_attr (die, DW_AT_external, cu);
13176 if (!attr || !DW_UNSND (attr))
13177 complaint (_("cannot get low and high bounds "
13178 "for subprogram DIE at %s"),
13179 sect_offset_str (die->sect_off));
13180 return;
13181 }
13182
13183 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13184 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13185
13186 /* If we have any template arguments, then we must allocate a
13187 different sort of symbol. */
13188 for (child_die = die->child; child_die; child_die = child_die->sibling)
13189 {
13190 if (child_die->tag == DW_TAG_template_type_param
13191 || child_die->tag == DW_TAG_template_value_param)
13192 {
13193 templ_func = new (&objfile->objfile_obstack) template_symbol;
13194 templ_func->subclass = SYMBOL_TEMPLATE;
13195 break;
13196 }
13197 }
13198
13199 newobj = cu->get_builder ()->push_context (0, lowpc);
13200 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13201 (struct symbol *) templ_func);
13202
13203 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13204 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13205 cu->language);
13206
13207 /* If there is a location expression for DW_AT_frame_base, record
13208 it. */
13209 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13210 if (attr != nullptr)
13211 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13212
13213 /* If there is a location for the static link, record it. */
13214 newobj->static_link = NULL;
13215 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13216 if (attr != nullptr)
13217 {
13218 newobj->static_link
13219 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13220 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13221 cu->addr_type ());
13222 }
13223
13224 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13225
13226 if (die->child != NULL)
13227 {
13228 child_die = die->child;
13229 while (child_die && child_die->tag)
13230 {
13231 if (child_die->tag == DW_TAG_template_type_param
13232 || child_die->tag == DW_TAG_template_value_param)
13233 {
13234 struct symbol *arg = new_symbol (child_die, NULL, cu);
13235
13236 if (arg != NULL)
13237 template_args.push_back (arg);
13238 }
13239 else
13240 process_die (child_die, cu);
13241 child_die = child_die->sibling;
13242 }
13243 }
13244
13245 inherit_abstract_dies (die, cu);
13246
13247 /* If we have a DW_AT_specification, we might need to import using
13248 directives from the context of the specification DIE. See the
13249 comment in determine_prefix. */
13250 if (cu->language == language_cplus
13251 && dwarf2_attr (die, DW_AT_specification, cu))
13252 {
13253 struct dwarf2_cu *spec_cu = cu;
13254 struct die_info *spec_die = die_specification (die, &spec_cu);
13255
13256 while (spec_die)
13257 {
13258 child_die = spec_die->child;
13259 while (child_die && child_die->tag)
13260 {
13261 if (child_die->tag == DW_TAG_imported_module)
13262 process_die (child_die, spec_cu);
13263 child_die = child_die->sibling;
13264 }
13265
13266 /* In some cases, GCC generates specification DIEs that
13267 themselves contain DW_AT_specification attributes. */
13268 spec_die = die_specification (spec_die, &spec_cu);
13269 }
13270 }
13271
13272 struct context_stack cstk = cu->get_builder ()->pop_context ();
13273 /* Make a block for the local symbols within. */
13274 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13275 cstk.static_link, lowpc, highpc);
13276
13277 /* For C++, set the block's scope. */
13278 if ((cu->language == language_cplus
13279 || cu->language == language_fortran
13280 || cu->language == language_d
13281 || cu->language == language_rust)
13282 && cu->processing_has_namespace_info)
13283 block_set_scope (block, determine_prefix (die, cu),
13284 &objfile->objfile_obstack);
13285
13286 /* If we have address ranges, record them. */
13287 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13288
13289 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13290
13291 /* Attach template arguments to function. */
13292 if (!template_args.empty ())
13293 {
13294 gdb_assert (templ_func != NULL);
13295
13296 templ_func->n_template_arguments = template_args.size ();
13297 templ_func->template_arguments
13298 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13299 templ_func->n_template_arguments);
13300 memcpy (templ_func->template_arguments,
13301 template_args.data (),
13302 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13303
13304 /* Make sure that the symtab is set on the new symbols. Even
13305 though they don't appear in this symtab directly, other parts
13306 of gdb assume that symbols do, and this is reasonably
13307 true. */
13308 for (symbol *sym : template_args)
13309 symbol_set_symtab (sym, symbol_symtab (templ_func));
13310 }
13311
13312 /* In C++, we can have functions nested inside functions (e.g., when
13313 a function declares a class that has methods). This means that
13314 when we finish processing a function scope, we may need to go
13315 back to building a containing block's symbol lists. */
13316 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13317 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13318
13319 /* If we've finished processing a top-level function, subsequent
13320 symbols go in the file symbol list. */
13321 if (cu->get_builder ()->outermost_context_p ())
13322 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13323 }
13324
13325 /* Process all the DIES contained within a lexical block scope. Start
13326 a new scope, process the dies, and then close the scope. */
13327
13328 static void
13329 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13330 {
13331 struct objfile *objfile = cu->per_objfile->objfile;
13332 struct gdbarch *gdbarch = objfile->arch ();
13333 CORE_ADDR lowpc, highpc;
13334 struct die_info *child_die;
13335 CORE_ADDR baseaddr;
13336
13337 baseaddr = objfile->text_section_offset ();
13338
13339 /* Ignore blocks with missing or invalid low and high pc attributes. */
13340 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13341 as multiple lexical blocks? Handling children in a sane way would
13342 be nasty. Might be easier to properly extend generic blocks to
13343 describe ranges. */
13344 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13345 {
13346 case PC_BOUNDS_NOT_PRESENT:
13347 /* DW_TAG_lexical_block has no attributes, process its children as if
13348 there was no wrapping by that DW_TAG_lexical_block.
13349 GCC does no longer produces such DWARF since GCC r224161. */
13350 for (child_die = die->child;
13351 child_die != NULL && child_die->tag;
13352 child_die = child_die->sibling)
13353 {
13354 /* We might already be processing this DIE. This can happen
13355 in an unusual circumstance -- where a subroutine A
13356 appears lexically in another subroutine B, but A actually
13357 inlines B. The recursion is broken here, rather than in
13358 inherit_abstract_dies, because it seems better to simply
13359 drop concrete children here. */
13360 if (!child_die->in_process)
13361 process_die (child_die, cu);
13362 }
13363 return;
13364 case PC_BOUNDS_INVALID:
13365 return;
13366 }
13367 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13368 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13369
13370 cu->get_builder ()->push_context (0, lowpc);
13371 if (die->child != NULL)
13372 {
13373 child_die = die->child;
13374 while (child_die && child_die->tag)
13375 {
13376 process_die (child_die, cu);
13377 child_die = child_die->sibling;
13378 }
13379 }
13380 inherit_abstract_dies (die, cu);
13381 struct context_stack cstk = cu->get_builder ()->pop_context ();
13382
13383 if (*cu->get_builder ()->get_local_symbols () != NULL
13384 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13385 {
13386 struct block *block
13387 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13388 cstk.start_addr, highpc);
13389
13390 /* Note that recording ranges after traversing children, as we
13391 do here, means that recording a parent's ranges entails
13392 walking across all its children's ranges as they appear in
13393 the address map, which is quadratic behavior.
13394
13395 It would be nicer to record the parent's ranges before
13396 traversing its children, simply overriding whatever you find
13397 there. But since we don't even decide whether to create a
13398 block until after we've traversed its children, that's hard
13399 to do. */
13400 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13401 }
13402 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13403 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13404 }
13405
13406 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13407
13408 static void
13409 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13410 {
13411 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13412 struct objfile *objfile = per_objfile->objfile;
13413 struct gdbarch *gdbarch = objfile->arch ();
13414 CORE_ADDR pc, baseaddr;
13415 struct attribute *attr;
13416 struct call_site *call_site, call_site_local;
13417 void **slot;
13418 int nparams;
13419 struct die_info *child_die;
13420
13421 baseaddr = objfile->text_section_offset ();
13422
13423 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13424 if (attr == NULL)
13425 {
13426 /* This was a pre-DWARF-5 GNU extension alias
13427 for DW_AT_call_return_pc. */
13428 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13429 }
13430 if (!attr)
13431 {
13432 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13433 "DIE %s [in module %s]"),
13434 sect_offset_str (die->sect_off), objfile_name (objfile));
13435 return;
13436 }
13437 pc = attr->value_as_address () + baseaddr;
13438 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13439
13440 if (cu->call_site_htab == NULL)
13441 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13442 NULL, &objfile->objfile_obstack,
13443 hashtab_obstack_allocate, NULL);
13444 call_site_local.pc = pc;
13445 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13446 if (*slot != NULL)
13447 {
13448 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13449 "DIE %s [in module %s]"),
13450 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13451 objfile_name (objfile));
13452 return;
13453 }
13454
13455 /* Count parameters at the caller. */
13456
13457 nparams = 0;
13458 for (child_die = die->child; child_die && child_die->tag;
13459 child_die = child_die->sibling)
13460 {
13461 if (child_die->tag != DW_TAG_call_site_parameter
13462 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13463 {
13464 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13465 "DW_TAG_call_site child DIE %s [in module %s]"),
13466 child_die->tag, sect_offset_str (child_die->sect_off),
13467 objfile_name (objfile));
13468 continue;
13469 }
13470
13471 nparams++;
13472 }
13473
13474 call_site
13475 = ((struct call_site *)
13476 obstack_alloc (&objfile->objfile_obstack,
13477 sizeof (*call_site)
13478 + (sizeof (*call_site->parameter) * (nparams - 1))));
13479 *slot = call_site;
13480 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13481 call_site->pc = pc;
13482
13483 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13484 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13485 {
13486 struct die_info *func_die;
13487
13488 /* Skip also over DW_TAG_inlined_subroutine. */
13489 for (func_die = die->parent;
13490 func_die && func_die->tag != DW_TAG_subprogram
13491 && func_die->tag != DW_TAG_subroutine_type;
13492 func_die = func_die->parent);
13493
13494 /* DW_AT_call_all_calls is a superset
13495 of DW_AT_call_all_tail_calls. */
13496 if (func_die
13497 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13498 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13499 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13500 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13501 {
13502 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13503 not complete. But keep CALL_SITE for look ups via call_site_htab,
13504 both the initial caller containing the real return address PC and
13505 the final callee containing the current PC of a chain of tail
13506 calls do not need to have the tail call list complete. But any
13507 function candidate for a virtual tail call frame searched via
13508 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13509 determined unambiguously. */
13510 }
13511 else
13512 {
13513 struct type *func_type = NULL;
13514
13515 if (func_die)
13516 func_type = get_die_type (func_die, cu);
13517 if (func_type != NULL)
13518 {
13519 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13520
13521 /* Enlist this call site to the function. */
13522 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13523 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13524 }
13525 else
13526 complaint (_("Cannot find function owning DW_TAG_call_site "
13527 "DIE %s [in module %s]"),
13528 sect_offset_str (die->sect_off), objfile_name (objfile));
13529 }
13530 }
13531
13532 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13533 if (attr == NULL)
13534 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13535 if (attr == NULL)
13536 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13537 if (attr == NULL)
13538 {
13539 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13540 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13541 }
13542 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13543 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13544 /* Keep NULL DWARF_BLOCK. */;
13545 else if (attr->form_is_block ())
13546 {
13547 struct dwarf2_locexpr_baton *dlbaton;
13548
13549 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13550 dlbaton->data = DW_BLOCK (attr)->data;
13551 dlbaton->size = DW_BLOCK (attr)->size;
13552 dlbaton->per_objfile = per_objfile;
13553 dlbaton->per_cu = cu->per_cu;
13554
13555 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13556 }
13557 else if (attr->form_is_ref ())
13558 {
13559 struct dwarf2_cu *target_cu = cu;
13560 struct die_info *target_die;
13561
13562 target_die = follow_die_ref (die, attr, &target_cu);
13563 gdb_assert (target_cu->per_objfile->objfile == objfile);
13564 if (die_is_declaration (target_die, target_cu))
13565 {
13566 const char *target_physname;
13567
13568 /* Prefer the mangled name; otherwise compute the demangled one. */
13569 target_physname = dw2_linkage_name (target_die, target_cu);
13570 if (target_physname == NULL)
13571 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13572 if (target_physname == NULL)
13573 complaint (_("DW_AT_call_target target DIE has invalid "
13574 "physname, for referencing DIE %s [in module %s]"),
13575 sect_offset_str (die->sect_off), objfile_name (objfile));
13576 else
13577 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13578 }
13579 else
13580 {
13581 CORE_ADDR lowpc;
13582
13583 /* DW_AT_entry_pc should be preferred. */
13584 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13585 <= PC_BOUNDS_INVALID)
13586 complaint (_("DW_AT_call_target target DIE has invalid "
13587 "low pc, for referencing DIE %s [in module %s]"),
13588 sect_offset_str (die->sect_off), objfile_name (objfile));
13589 else
13590 {
13591 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13592 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13593 }
13594 }
13595 }
13596 else
13597 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13598 "block nor reference, for DIE %s [in module %s]"),
13599 sect_offset_str (die->sect_off), objfile_name (objfile));
13600
13601 call_site->per_cu = cu->per_cu;
13602 call_site->per_objfile = per_objfile;
13603
13604 for (child_die = die->child;
13605 child_die && child_die->tag;
13606 child_die = child_die->sibling)
13607 {
13608 struct call_site_parameter *parameter;
13609 struct attribute *loc, *origin;
13610
13611 if (child_die->tag != DW_TAG_call_site_parameter
13612 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13613 {
13614 /* Already printed the complaint above. */
13615 continue;
13616 }
13617
13618 gdb_assert (call_site->parameter_count < nparams);
13619 parameter = &call_site->parameter[call_site->parameter_count];
13620
13621 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13622 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13623 register is contained in DW_AT_call_value. */
13624
13625 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13626 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13627 if (origin == NULL)
13628 {
13629 /* This was a pre-DWARF-5 GNU extension alias
13630 for DW_AT_call_parameter. */
13631 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13632 }
13633 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13634 {
13635 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13636
13637 sect_offset sect_off = origin->get_ref_die_offset ();
13638 if (!cu->header.offset_in_cu_p (sect_off))
13639 {
13640 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13641 binding can be done only inside one CU. Such referenced DIE
13642 therefore cannot be even moved to DW_TAG_partial_unit. */
13643 complaint (_("DW_AT_call_parameter offset is not in CU for "
13644 "DW_TAG_call_site child DIE %s [in module %s]"),
13645 sect_offset_str (child_die->sect_off),
13646 objfile_name (objfile));
13647 continue;
13648 }
13649 parameter->u.param_cu_off
13650 = (cu_offset) (sect_off - cu->header.sect_off);
13651 }
13652 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13653 {
13654 complaint (_("No DW_FORM_block* DW_AT_location for "
13655 "DW_TAG_call_site child DIE %s [in module %s]"),
13656 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13657 continue;
13658 }
13659 else
13660 {
13661 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13662 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13663 if (parameter->u.dwarf_reg != -1)
13664 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13665 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13666 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13667 &parameter->u.fb_offset))
13668 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13669 else
13670 {
13671 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13672 "for DW_FORM_block* DW_AT_location is supported for "
13673 "DW_TAG_call_site child DIE %s "
13674 "[in module %s]"),
13675 sect_offset_str (child_die->sect_off),
13676 objfile_name (objfile));
13677 continue;
13678 }
13679 }
13680
13681 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13682 if (attr == NULL)
13683 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13684 if (attr == NULL || !attr->form_is_block ())
13685 {
13686 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13687 "DW_TAG_call_site child DIE %s [in module %s]"),
13688 sect_offset_str (child_die->sect_off),
13689 objfile_name (objfile));
13690 continue;
13691 }
13692 parameter->value = DW_BLOCK (attr)->data;
13693 parameter->value_size = DW_BLOCK (attr)->size;
13694
13695 /* Parameters are not pre-cleared by memset above. */
13696 parameter->data_value = NULL;
13697 parameter->data_value_size = 0;
13698 call_site->parameter_count++;
13699
13700 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13701 if (attr == NULL)
13702 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13703 if (attr != nullptr)
13704 {
13705 if (!attr->form_is_block ())
13706 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13707 "DW_TAG_call_site child DIE %s [in module %s]"),
13708 sect_offset_str (child_die->sect_off),
13709 objfile_name (objfile));
13710 else
13711 {
13712 parameter->data_value = DW_BLOCK (attr)->data;
13713 parameter->data_value_size = DW_BLOCK (attr)->size;
13714 }
13715 }
13716 }
13717 }
13718
13719 /* Helper function for read_variable. If DIE represents a virtual
13720 table, then return the type of the concrete object that is
13721 associated with the virtual table. Otherwise, return NULL. */
13722
13723 static struct type *
13724 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13725 {
13726 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13727 if (attr == NULL)
13728 return NULL;
13729
13730 /* Find the type DIE. */
13731 struct die_info *type_die = NULL;
13732 struct dwarf2_cu *type_cu = cu;
13733
13734 if (attr->form_is_ref ())
13735 type_die = follow_die_ref (die, attr, &type_cu);
13736 if (type_die == NULL)
13737 return NULL;
13738
13739 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13740 return NULL;
13741 return die_containing_type (type_die, type_cu);
13742 }
13743
13744 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13745
13746 static void
13747 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13748 {
13749 struct rust_vtable_symbol *storage = NULL;
13750
13751 if (cu->language == language_rust)
13752 {
13753 struct type *containing_type = rust_containing_type (die, cu);
13754
13755 if (containing_type != NULL)
13756 {
13757 struct objfile *objfile = cu->per_objfile->objfile;
13758
13759 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13760 storage->concrete_type = containing_type;
13761 storage->subclass = SYMBOL_RUST_VTABLE;
13762 }
13763 }
13764
13765 struct symbol *res = new_symbol (die, NULL, cu, storage);
13766 struct attribute *abstract_origin
13767 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13768 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13769 if (res == NULL && loc && abstract_origin)
13770 {
13771 /* We have a variable without a name, but with a location and an abstract
13772 origin. This may be a concrete instance of an abstract variable
13773 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13774 later. */
13775 struct dwarf2_cu *origin_cu = cu;
13776 struct die_info *origin_die
13777 = follow_die_ref (die, abstract_origin, &origin_cu);
13778 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13779 per_objfile->per_bfd->abstract_to_concrete
13780 [origin_die->sect_off].push_back (die->sect_off);
13781 }
13782 }
13783
13784 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13785 reading .debug_rnglists.
13786 Callback's type should be:
13787 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13788 Return true if the attributes are present and valid, otherwise,
13789 return false. */
13790
13791 template <typename Callback>
13792 static bool
13793 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13794 Callback &&callback)
13795 {
13796 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13797 struct objfile *objfile = per_objfile->objfile;
13798 bfd *obfd = objfile->obfd;
13799 /* Base address selection entry. */
13800 gdb::optional<CORE_ADDR> base;
13801 const gdb_byte *buffer;
13802 CORE_ADDR baseaddr;
13803 bool overflow = false;
13804
13805 base = cu->base_address;
13806
13807 per_objfile->per_bfd->rnglists.read (objfile);
13808 if (offset >= per_objfile->per_bfd->rnglists.size)
13809 {
13810 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13811 offset);
13812 return false;
13813 }
13814 buffer = per_objfile->per_bfd->rnglists.buffer + offset;
13815
13816 baseaddr = objfile->text_section_offset ();
13817
13818 while (1)
13819 {
13820 /* Initialize it due to a false compiler warning. */
13821 CORE_ADDR range_beginning = 0, range_end = 0;
13822 const gdb_byte *buf_end = (per_objfile->per_bfd->rnglists.buffer
13823 + per_objfile->per_bfd->rnglists.size);
13824 unsigned int bytes_read;
13825
13826 if (buffer == buf_end)
13827 {
13828 overflow = true;
13829 break;
13830 }
13831 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13832 switch (rlet)
13833 {
13834 case DW_RLE_end_of_list:
13835 break;
13836 case DW_RLE_base_address:
13837 if (buffer + cu->header.addr_size > buf_end)
13838 {
13839 overflow = true;
13840 break;
13841 }
13842 base = cu->header.read_address (obfd, buffer, &bytes_read);
13843 buffer += bytes_read;
13844 break;
13845 case DW_RLE_start_length:
13846 if (buffer + cu->header.addr_size > buf_end)
13847 {
13848 overflow = true;
13849 break;
13850 }
13851 range_beginning = cu->header.read_address (obfd, buffer,
13852 &bytes_read);
13853 buffer += bytes_read;
13854 range_end = (range_beginning
13855 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13856 buffer += bytes_read;
13857 if (buffer > buf_end)
13858 {
13859 overflow = true;
13860 break;
13861 }
13862 break;
13863 case DW_RLE_offset_pair:
13864 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13865 buffer += bytes_read;
13866 if (buffer > buf_end)
13867 {
13868 overflow = true;
13869 break;
13870 }
13871 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13872 buffer += bytes_read;
13873 if (buffer > buf_end)
13874 {
13875 overflow = true;
13876 break;
13877 }
13878 break;
13879 case DW_RLE_start_end:
13880 if (buffer + 2 * cu->header.addr_size > buf_end)
13881 {
13882 overflow = true;
13883 break;
13884 }
13885 range_beginning = cu->header.read_address (obfd, buffer,
13886 &bytes_read);
13887 buffer += bytes_read;
13888 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13889 buffer += bytes_read;
13890 break;
13891 default:
13892 complaint (_("Invalid .debug_rnglists data (no base address)"));
13893 return false;
13894 }
13895 if (rlet == DW_RLE_end_of_list || overflow)
13896 break;
13897 if (rlet == DW_RLE_base_address)
13898 continue;
13899
13900 if (!base.has_value ())
13901 {
13902 /* We have no valid base address for the ranges
13903 data. */
13904 complaint (_("Invalid .debug_rnglists data (no base address)"));
13905 return false;
13906 }
13907
13908 if (range_beginning > range_end)
13909 {
13910 /* Inverted range entries are invalid. */
13911 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13912 return false;
13913 }
13914
13915 /* Empty range entries have no effect. */
13916 if (range_beginning == range_end)
13917 continue;
13918
13919 range_beginning += *base;
13920 range_end += *base;
13921
13922 /* A not-uncommon case of bad debug info.
13923 Don't pollute the addrmap with bad data. */
13924 if (range_beginning + baseaddr == 0
13925 && !per_objfile->per_bfd->has_section_at_zero)
13926 {
13927 complaint (_(".debug_rnglists entry has start address of zero"
13928 " [in module %s]"), objfile_name (objfile));
13929 continue;
13930 }
13931
13932 callback (range_beginning, range_end);
13933 }
13934
13935 if (overflow)
13936 {
13937 complaint (_("Offset %d is not terminated "
13938 "for DW_AT_ranges attribute"),
13939 offset);
13940 return false;
13941 }
13942
13943 return true;
13944 }
13945
13946 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13947 Callback's type should be:
13948 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13949 Return 1 if the attributes are present and valid, otherwise, return 0. */
13950
13951 template <typename Callback>
13952 static int
13953 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13954 Callback &&callback)
13955 {
13956 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13957 struct objfile *objfile = per_objfile->objfile;
13958 struct comp_unit_head *cu_header = &cu->header;
13959 bfd *obfd = objfile->obfd;
13960 unsigned int addr_size = cu_header->addr_size;
13961 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13962 /* Base address selection entry. */
13963 gdb::optional<CORE_ADDR> base;
13964 unsigned int dummy;
13965 const gdb_byte *buffer;
13966 CORE_ADDR baseaddr;
13967
13968 if (cu_header->version >= 5)
13969 return dwarf2_rnglists_process (offset, cu, callback);
13970
13971 base = cu->base_address;
13972
13973 per_objfile->per_bfd->ranges.read (objfile);
13974 if (offset >= per_objfile->per_bfd->ranges.size)
13975 {
13976 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13977 offset);
13978 return 0;
13979 }
13980 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13981
13982 baseaddr = objfile->text_section_offset ();
13983
13984 while (1)
13985 {
13986 CORE_ADDR range_beginning, range_end;
13987
13988 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13989 buffer += addr_size;
13990 range_end = cu->header.read_address (obfd, buffer, &dummy);
13991 buffer += addr_size;
13992 offset += 2 * addr_size;
13993
13994 /* An end of list marker is a pair of zero addresses. */
13995 if (range_beginning == 0 && range_end == 0)
13996 /* Found the end of list entry. */
13997 break;
13998
13999 /* Each base address selection entry is a pair of 2 values.
14000 The first is the largest possible address, the second is
14001 the base address. Check for a base address here. */
14002 if ((range_beginning & mask) == mask)
14003 {
14004 /* If we found the largest possible address, then we already
14005 have the base address in range_end. */
14006 base = range_end;
14007 continue;
14008 }
14009
14010 if (!base.has_value ())
14011 {
14012 /* We have no valid base address for the ranges
14013 data. */
14014 complaint (_("Invalid .debug_ranges data (no base address)"));
14015 return 0;
14016 }
14017
14018 if (range_beginning > range_end)
14019 {
14020 /* Inverted range entries are invalid. */
14021 complaint (_("Invalid .debug_ranges data (inverted range)"));
14022 return 0;
14023 }
14024
14025 /* Empty range entries have no effect. */
14026 if (range_beginning == range_end)
14027 continue;
14028
14029 range_beginning += *base;
14030 range_end += *base;
14031
14032 /* A not-uncommon case of bad debug info.
14033 Don't pollute the addrmap with bad data. */
14034 if (range_beginning + baseaddr == 0
14035 && !per_objfile->per_bfd->has_section_at_zero)
14036 {
14037 complaint (_(".debug_ranges entry has start address of zero"
14038 " [in module %s]"), objfile_name (objfile));
14039 continue;
14040 }
14041
14042 callback (range_beginning, range_end);
14043 }
14044
14045 return 1;
14046 }
14047
14048 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14049 Return 1 if the attributes are present and valid, otherwise, return 0.
14050 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14051
14052 static int
14053 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14054 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14055 dwarf2_psymtab *ranges_pst)
14056 {
14057 struct objfile *objfile = cu->per_objfile->objfile;
14058 struct gdbarch *gdbarch = objfile->arch ();
14059 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14060 int low_set = 0;
14061 CORE_ADDR low = 0;
14062 CORE_ADDR high = 0;
14063 int retval;
14064
14065 retval = dwarf2_ranges_process (offset, cu,
14066 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14067 {
14068 if (ranges_pst != NULL)
14069 {
14070 CORE_ADDR lowpc;
14071 CORE_ADDR highpc;
14072
14073 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14074 range_beginning + baseaddr)
14075 - baseaddr);
14076 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14077 range_end + baseaddr)
14078 - baseaddr);
14079 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14080 lowpc, highpc - 1, ranges_pst);
14081 }
14082
14083 /* FIXME: This is recording everything as a low-high
14084 segment of consecutive addresses. We should have a
14085 data structure for discontiguous block ranges
14086 instead. */
14087 if (! low_set)
14088 {
14089 low = range_beginning;
14090 high = range_end;
14091 low_set = 1;
14092 }
14093 else
14094 {
14095 if (range_beginning < low)
14096 low = range_beginning;
14097 if (range_end > high)
14098 high = range_end;
14099 }
14100 });
14101 if (!retval)
14102 return 0;
14103
14104 if (! low_set)
14105 /* If the first entry is an end-of-list marker, the range
14106 describes an empty scope, i.e. no instructions. */
14107 return 0;
14108
14109 if (low_return)
14110 *low_return = low;
14111 if (high_return)
14112 *high_return = high;
14113 return 1;
14114 }
14115
14116 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14117 definition for the return value. *LOWPC and *HIGHPC are set iff
14118 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14119
14120 static enum pc_bounds_kind
14121 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14122 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14123 dwarf2_psymtab *pst)
14124 {
14125 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14126 struct attribute *attr;
14127 struct attribute *attr_high;
14128 CORE_ADDR low = 0;
14129 CORE_ADDR high = 0;
14130 enum pc_bounds_kind ret;
14131
14132 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14133 if (attr_high)
14134 {
14135 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14136 if (attr != nullptr)
14137 {
14138 low = attr->value_as_address ();
14139 high = attr_high->value_as_address ();
14140 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14141 high += low;
14142 }
14143 else
14144 /* Found high w/o low attribute. */
14145 return PC_BOUNDS_INVALID;
14146
14147 /* Found consecutive range of addresses. */
14148 ret = PC_BOUNDS_HIGH_LOW;
14149 }
14150 else
14151 {
14152 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14153 if (attr != NULL)
14154 {
14155 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14156 We take advantage of the fact that DW_AT_ranges does not appear
14157 in DW_TAG_compile_unit of DWO files. */
14158 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14159 unsigned int ranges_offset = (DW_UNSND (attr)
14160 + (need_ranges_base
14161 ? cu->ranges_base
14162 : 0));
14163
14164 /* Value of the DW_AT_ranges attribute is the offset in the
14165 .debug_ranges section. */
14166 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14167 return PC_BOUNDS_INVALID;
14168 /* Found discontinuous range of addresses. */
14169 ret = PC_BOUNDS_RANGES;
14170 }
14171 else
14172 return PC_BOUNDS_NOT_PRESENT;
14173 }
14174
14175 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14176 if (high <= low)
14177 return PC_BOUNDS_INVALID;
14178
14179 /* When using the GNU linker, .gnu.linkonce. sections are used to
14180 eliminate duplicate copies of functions and vtables and such.
14181 The linker will arbitrarily choose one and discard the others.
14182 The AT_*_pc values for such functions refer to local labels in
14183 these sections. If the section from that file was discarded, the
14184 labels are not in the output, so the relocs get a value of 0.
14185 If this is a discarded function, mark the pc bounds as invalid,
14186 so that GDB will ignore it. */
14187 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14188 return PC_BOUNDS_INVALID;
14189
14190 *lowpc = low;
14191 if (highpc)
14192 *highpc = high;
14193 return ret;
14194 }
14195
14196 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14197 its low and high PC addresses. Do nothing if these addresses could not
14198 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14199 and HIGHPC to the high address if greater than HIGHPC. */
14200
14201 static void
14202 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14203 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14204 struct dwarf2_cu *cu)
14205 {
14206 CORE_ADDR low, high;
14207 struct die_info *child = die->child;
14208
14209 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14210 {
14211 *lowpc = std::min (*lowpc, low);
14212 *highpc = std::max (*highpc, high);
14213 }
14214
14215 /* If the language does not allow nested subprograms (either inside
14216 subprograms or lexical blocks), we're done. */
14217 if (cu->language != language_ada)
14218 return;
14219
14220 /* Check all the children of the given DIE. If it contains nested
14221 subprograms, then check their pc bounds. Likewise, we need to
14222 check lexical blocks as well, as they may also contain subprogram
14223 definitions. */
14224 while (child && child->tag)
14225 {
14226 if (child->tag == DW_TAG_subprogram
14227 || child->tag == DW_TAG_lexical_block)
14228 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14229 child = child->sibling;
14230 }
14231 }
14232
14233 /* Get the low and high pc's represented by the scope DIE, and store
14234 them in *LOWPC and *HIGHPC. If the correct values can't be
14235 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14236
14237 static void
14238 get_scope_pc_bounds (struct die_info *die,
14239 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14240 struct dwarf2_cu *cu)
14241 {
14242 CORE_ADDR best_low = (CORE_ADDR) -1;
14243 CORE_ADDR best_high = (CORE_ADDR) 0;
14244 CORE_ADDR current_low, current_high;
14245
14246 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14247 >= PC_BOUNDS_RANGES)
14248 {
14249 best_low = current_low;
14250 best_high = current_high;
14251 }
14252 else
14253 {
14254 struct die_info *child = die->child;
14255
14256 while (child && child->tag)
14257 {
14258 switch (child->tag) {
14259 case DW_TAG_subprogram:
14260 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14261 break;
14262 case DW_TAG_namespace:
14263 case DW_TAG_module:
14264 /* FIXME: carlton/2004-01-16: Should we do this for
14265 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14266 that current GCC's always emit the DIEs corresponding
14267 to definitions of methods of classes as children of a
14268 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14269 the DIEs giving the declarations, which could be
14270 anywhere). But I don't see any reason why the
14271 standards says that they have to be there. */
14272 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14273
14274 if (current_low != ((CORE_ADDR) -1))
14275 {
14276 best_low = std::min (best_low, current_low);
14277 best_high = std::max (best_high, current_high);
14278 }
14279 break;
14280 default:
14281 /* Ignore. */
14282 break;
14283 }
14284
14285 child = child->sibling;
14286 }
14287 }
14288
14289 *lowpc = best_low;
14290 *highpc = best_high;
14291 }
14292
14293 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14294 in DIE. */
14295
14296 static void
14297 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14298 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14299 {
14300 struct objfile *objfile = cu->per_objfile->objfile;
14301 struct gdbarch *gdbarch = objfile->arch ();
14302 struct attribute *attr;
14303 struct attribute *attr_high;
14304
14305 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14306 if (attr_high)
14307 {
14308 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14309 if (attr != nullptr)
14310 {
14311 CORE_ADDR low = attr->value_as_address ();
14312 CORE_ADDR high = attr_high->value_as_address ();
14313
14314 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14315 high += low;
14316
14317 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14318 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14319 cu->get_builder ()->record_block_range (block, low, high - 1);
14320 }
14321 }
14322
14323 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14324 if (attr != nullptr)
14325 {
14326 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14327 We take advantage of the fact that DW_AT_ranges does not appear
14328 in DW_TAG_compile_unit of DWO files. */
14329 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14330
14331 /* The value of the DW_AT_ranges attribute is the offset of the
14332 address range list in the .debug_ranges section. */
14333 unsigned long offset = (DW_UNSND (attr)
14334 + (need_ranges_base ? cu->ranges_base : 0));
14335
14336 std::vector<blockrange> blockvec;
14337 dwarf2_ranges_process (offset, cu,
14338 [&] (CORE_ADDR start, CORE_ADDR end)
14339 {
14340 start += baseaddr;
14341 end += baseaddr;
14342 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14343 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14344 cu->get_builder ()->record_block_range (block, start, end - 1);
14345 blockvec.emplace_back (start, end);
14346 });
14347
14348 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14349 }
14350 }
14351
14352 /* Check whether the producer field indicates either of GCC < 4.6, or the
14353 Intel C/C++ compiler, and cache the result in CU. */
14354
14355 static void
14356 check_producer (struct dwarf2_cu *cu)
14357 {
14358 int major, minor;
14359
14360 if (cu->producer == NULL)
14361 {
14362 /* For unknown compilers expect their behavior is DWARF version
14363 compliant.
14364
14365 GCC started to support .debug_types sections by -gdwarf-4 since
14366 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14367 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14368 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14369 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14370 }
14371 else if (producer_is_gcc (cu->producer, &major, &minor))
14372 {
14373 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14374 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14375 }
14376 else if (producer_is_icc (cu->producer, &major, &minor))
14377 {
14378 cu->producer_is_icc = true;
14379 cu->producer_is_icc_lt_14 = major < 14;
14380 }
14381 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14382 cu->producer_is_codewarrior = true;
14383 else
14384 {
14385 /* For other non-GCC compilers, expect their behavior is DWARF version
14386 compliant. */
14387 }
14388
14389 cu->checked_producer = true;
14390 }
14391
14392 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14393 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14394 during 4.6.0 experimental. */
14395
14396 static bool
14397 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14398 {
14399 if (!cu->checked_producer)
14400 check_producer (cu);
14401
14402 return cu->producer_is_gxx_lt_4_6;
14403 }
14404
14405
14406 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14407 with incorrect is_stmt attributes. */
14408
14409 static bool
14410 producer_is_codewarrior (struct dwarf2_cu *cu)
14411 {
14412 if (!cu->checked_producer)
14413 check_producer (cu);
14414
14415 return cu->producer_is_codewarrior;
14416 }
14417
14418 /* Return the default accessibility type if it is not overridden by
14419 DW_AT_accessibility. */
14420
14421 static enum dwarf_access_attribute
14422 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14423 {
14424 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14425 {
14426 /* The default DWARF 2 accessibility for members is public, the default
14427 accessibility for inheritance is private. */
14428
14429 if (die->tag != DW_TAG_inheritance)
14430 return DW_ACCESS_public;
14431 else
14432 return DW_ACCESS_private;
14433 }
14434 else
14435 {
14436 /* DWARF 3+ defines the default accessibility a different way. The same
14437 rules apply now for DW_TAG_inheritance as for the members and it only
14438 depends on the container kind. */
14439
14440 if (die->parent->tag == DW_TAG_class_type)
14441 return DW_ACCESS_private;
14442 else
14443 return DW_ACCESS_public;
14444 }
14445 }
14446
14447 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14448 offset. If the attribute was not found return 0, otherwise return
14449 1. If it was found but could not properly be handled, set *OFFSET
14450 to 0. */
14451
14452 static int
14453 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14454 LONGEST *offset)
14455 {
14456 struct attribute *attr;
14457
14458 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14459 if (attr != NULL)
14460 {
14461 *offset = 0;
14462
14463 /* Note that we do not check for a section offset first here.
14464 This is because DW_AT_data_member_location is new in DWARF 4,
14465 so if we see it, we can assume that a constant form is really
14466 a constant and not a section offset. */
14467 if (attr->form_is_constant ())
14468 *offset = attr->constant_value (0);
14469 else if (attr->form_is_section_offset ())
14470 dwarf2_complex_location_expr_complaint ();
14471 else if (attr->form_is_block ())
14472 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14473 else
14474 dwarf2_complex_location_expr_complaint ();
14475
14476 return 1;
14477 }
14478
14479 return 0;
14480 }
14481
14482 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14483
14484 static void
14485 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14486 struct field *field)
14487 {
14488 struct attribute *attr;
14489
14490 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14491 if (attr != NULL)
14492 {
14493 if (attr->form_is_constant ())
14494 {
14495 LONGEST offset = attr->constant_value (0);
14496 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14497 }
14498 else if (attr->form_is_section_offset ())
14499 dwarf2_complex_location_expr_complaint ();
14500 else if (attr->form_is_block ())
14501 {
14502 bool handled;
14503 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14504 if (handled)
14505 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14506 else
14507 {
14508 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14509 struct objfile *objfile = per_objfile->objfile;
14510 struct dwarf2_locexpr_baton *dlbaton
14511 = XOBNEW (&objfile->objfile_obstack,
14512 struct dwarf2_locexpr_baton);
14513 dlbaton->data = DW_BLOCK (attr)->data;
14514 dlbaton->size = DW_BLOCK (attr)->size;
14515 /* When using this baton, we want to compute the address
14516 of the field, not the value. This is why
14517 is_reference is set to false here. */
14518 dlbaton->is_reference = false;
14519 dlbaton->per_objfile = per_objfile;
14520 dlbaton->per_cu = cu->per_cu;
14521
14522 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14523 }
14524 }
14525 else
14526 dwarf2_complex_location_expr_complaint ();
14527 }
14528 }
14529
14530 /* Add an aggregate field to the field list. */
14531
14532 static void
14533 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14534 struct dwarf2_cu *cu)
14535 {
14536 struct objfile *objfile = cu->per_objfile->objfile;
14537 struct gdbarch *gdbarch = objfile->arch ();
14538 struct nextfield *new_field;
14539 struct attribute *attr;
14540 struct field *fp;
14541 const char *fieldname = "";
14542
14543 if (die->tag == DW_TAG_inheritance)
14544 {
14545 fip->baseclasses.emplace_back ();
14546 new_field = &fip->baseclasses.back ();
14547 }
14548 else
14549 {
14550 fip->fields.emplace_back ();
14551 new_field = &fip->fields.back ();
14552 }
14553
14554 new_field->offset = die->sect_off;
14555
14556 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14557 if (attr != nullptr)
14558 new_field->accessibility = DW_UNSND (attr);
14559 else
14560 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14561 if (new_field->accessibility != DW_ACCESS_public)
14562 fip->non_public_fields = 1;
14563
14564 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14565 if (attr != nullptr)
14566 new_field->virtuality = DW_UNSND (attr);
14567 else
14568 new_field->virtuality = DW_VIRTUALITY_none;
14569
14570 fp = &new_field->field;
14571
14572 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14573 {
14574 /* Data member other than a C++ static data member. */
14575
14576 /* Get type of field. */
14577 fp->set_type (die_type (die, cu));
14578
14579 SET_FIELD_BITPOS (*fp, 0);
14580
14581 /* Get bit size of field (zero if none). */
14582 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14583 if (attr != nullptr)
14584 {
14585 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14586 }
14587 else
14588 {
14589 FIELD_BITSIZE (*fp) = 0;
14590 }
14591
14592 /* Get bit offset of field. */
14593 handle_data_member_location (die, cu, fp);
14594 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14595 if (attr != nullptr)
14596 {
14597 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14598 {
14599 /* For big endian bits, the DW_AT_bit_offset gives the
14600 additional bit offset from the MSB of the containing
14601 anonymous object to the MSB of the field. We don't
14602 have to do anything special since we don't need to
14603 know the size of the anonymous object. */
14604 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14605 }
14606 else
14607 {
14608 /* For little endian bits, compute the bit offset to the
14609 MSB of the anonymous object, subtract off the number of
14610 bits from the MSB of the field to the MSB of the
14611 object, and then subtract off the number of bits of
14612 the field itself. The result is the bit offset of
14613 the LSB of the field. */
14614 int anonymous_size;
14615 int bit_offset = DW_UNSND (attr);
14616
14617 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14618 if (attr != nullptr)
14619 {
14620 /* The size of the anonymous object containing
14621 the bit field is explicit, so use the
14622 indicated size (in bytes). */
14623 anonymous_size = DW_UNSND (attr);
14624 }
14625 else
14626 {
14627 /* The size of the anonymous object containing
14628 the bit field must be inferred from the type
14629 attribute of the data member containing the
14630 bit field. */
14631 anonymous_size = TYPE_LENGTH (fp->type ());
14632 }
14633 SET_FIELD_BITPOS (*fp,
14634 (FIELD_BITPOS (*fp)
14635 + anonymous_size * bits_per_byte
14636 - bit_offset - FIELD_BITSIZE (*fp)));
14637 }
14638 }
14639 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14640 if (attr != NULL)
14641 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14642 + attr->constant_value (0)));
14643
14644 /* Get name of field. */
14645 fieldname = dwarf2_name (die, cu);
14646 if (fieldname == NULL)
14647 fieldname = "";
14648
14649 /* The name is already allocated along with this objfile, so we don't
14650 need to duplicate it for the type. */
14651 fp->name = fieldname;
14652
14653 /* Change accessibility for artificial fields (e.g. virtual table
14654 pointer or virtual base class pointer) to private. */
14655 if (dwarf2_attr (die, DW_AT_artificial, cu))
14656 {
14657 FIELD_ARTIFICIAL (*fp) = 1;
14658 new_field->accessibility = DW_ACCESS_private;
14659 fip->non_public_fields = 1;
14660 }
14661 }
14662 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14663 {
14664 /* C++ static member. */
14665
14666 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14667 is a declaration, but all versions of G++ as of this writing
14668 (so through at least 3.2.1) incorrectly generate
14669 DW_TAG_variable tags. */
14670
14671 const char *physname;
14672
14673 /* Get name of field. */
14674 fieldname = dwarf2_name (die, cu);
14675 if (fieldname == NULL)
14676 return;
14677
14678 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14679 if (attr
14680 /* Only create a symbol if this is an external value.
14681 new_symbol checks this and puts the value in the global symbol
14682 table, which we want. If it is not external, new_symbol
14683 will try to put the value in cu->list_in_scope which is wrong. */
14684 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14685 {
14686 /* A static const member, not much different than an enum as far as
14687 we're concerned, except that we can support more types. */
14688 new_symbol (die, NULL, cu);
14689 }
14690
14691 /* Get physical name. */
14692 physname = dwarf2_physname (fieldname, die, cu);
14693
14694 /* The name is already allocated along with this objfile, so we don't
14695 need to duplicate it for the type. */
14696 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14697 fp->set_type (die_type (die, cu));
14698 FIELD_NAME (*fp) = fieldname;
14699 }
14700 else if (die->tag == DW_TAG_inheritance)
14701 {
14702 /* C++ base class field. */
14703 handle_data_member_location (die, cu, fp);
14704 FIELD_BITSIZE (*fp) = 0;
14705 fp->set_type (die_type (die, cu));
14706 FIELD_NAME (*fp) = fp->type ()->name ();
14707 }
14708 else
14709 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14710 }
14711
14712 /* Can the type given by DIE define another type? */
14713
14714 static bool
14715 type_can_define_types (const struct die_info *die)
14716 {
14717 switch (die->tag)
14718 {
14719 case DW_TAG_typedef:
14720 case DW_TAG_class_type:
14721 case DW_TAG_structure_type:
14722 case DW_TAG_union_type:
14723 case DW_TAG_enumeration_type:
14724 return true;
14725
14726 default:
14727 return false;
14728 }
14729 }
14730
14731 /* Add a type definition defined in the scope of the FIP's class. */
14732
14733 static void
14734 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14735 struct dwarf2_cu *cu)
14736 {
14737 struct decl_field fp;
14738 memset (&fp, 0, sizeof (fp));
14739
14740 gdb_assert (type_can_define_types (die));
14741
14742 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14743 fp.name = dwarf2_name (die, cu);
14744 fp.type = read_type_die (die, cu);
14745
14746 /* Save accessibility. */
14747 enum dwarf_access_attribute accessibility;
14748 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14749 if (attr != NULL)
14750 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14751 else
14752 accessibility = dwarf2_default_access_attribute (die, cu);
14753 switch (accessibility)
14754 {
14755 case DW_ACCESS_public:
14756 /* The assumed value if neither private nor protected. */
14757 break;
14758 case DW_ACCESS_private:
14759 fp.is_private = 1;
14760 break;
14761 case DW_ACCESS_protected:
14762 fp.is_protected = 1;
14763 break;
14764 default:
14765 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14766 }
14767
14768 if (die->tag == DW_TAG_typedef)
14769 fip->typedef_field_list.push_back (fp);
14770 else
14771 fip->nested_types_list.push_back (fp);
14772 }
14773
14774 /* A convenience typedef that's used when finding the discriminant
14775 field for a variant part. */
14776 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14777 offset_map_type;
14778
14779 /* Compute the discriminant range for a given variant. OBSTACK is
14780 where the results will be stored. VARIANT is the variant to
14781 process. IS_UNSIGNED indicates whether the discriminant is signed
14782 or unsigned. */
14783
14784 static const gdb::array_view<discriminant_range>
14785 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14786 bool is_unsigned)
14787 {
14788 std::vector<discriminant_range> ranges;
14789
14790 if (variant.default_branch)
14791 return {};
14792
14793 if (variant.discr_list_data == nullptr)
14794 {
14795 discriminant_range r
14796 = {variant.discriminant_value, variant.discriminant_value};
14797 ranges.push_back (r);
14798 }
14799 else
14800 {
14801 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14802 variant.discr_list_data->size);
14803 while (!data.empty ())
14804 {
14805 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14806 {
14807 complaint (_("invalid discriminant marker: %d"), data[0]);
14808 break;
14809 }
14810 bool is_range = data[0] == DW_DSC_range;
14811 data = data.slice (1);
14812
14813 ULONGEST low, high;
14814 unsigned int bytes_read;
14815
14816 if (data.empty ())
14817 {
14818 complaint (_("DW_AT_discr_list missing low value"));
14819 break;
14820 }
14821 if (is_unsigned)
14822 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14823 else
14824 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14825 &bytes_read);
14826 data = data.slice (bytes_read);
14827
14828 if (is_range)
14829 {
14830 if (data.empty ())
14831 {
14832 complaint (_("DW_AT_discr_list missing high value"));
14833 break;
14834 }
14835 if (is_unsigned)
14836 high = read_unsigned_leb128 (nullptr, data.data (),
14837 &bytes_read);
14838 else
14839 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14840 &bytes_read);
14841 data = data.slice (bytes_read);
14842 }
14843 else
14844 high = low;
14845
14846 ranges.push_back ({ low, high });
14847 }
14848 }
14849
14850 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14851 ranges.size ());
14852 std::copy (ranges.begin (), ranges.end (), result);
14853 return gdb::array_view<discriminant_range> (result, ranges.size ());
14854 }
14855
14856 static const gdb::array_view<variant_part> create_variant_parts
14857 (struct obstack *obstack,
14858 const offset_map_type &offset_map,
14859 struct field_info *fi,
14860 const std::vector<variant_part_builder> &variant_parts);
14861
14862 /* Fill in a "struct variant" for a given variant field. RESULT is
14863 the variant to fill in. OBSTACK is where any needed allocations
14864 will be done. OFFSET_MAP holds the mapping from section offsets to
14865 fields for the type. FI describes the fields of the type we're
14866 processing. FIELD is the variant field we're converting. */
14867
14868 static void
14869 create_one_variant (variant &result, struct obstack *obstack,
14870 const offset_map_type &offset_map,
14871 struct field_info *fi, const variant_field &field)
14872 {
14873 result.discriminants = convert_variant_range (obstack, field, false);
14874 result.first_field = field.first_field + fi->baseclasses.size ();
14875 result.last_field = field.last_field + fi->baseclasses.size ();
14876 result.parts = create_variant_parts (obstack, offset_map, fi,
14877 field.variant_parts);
14878 }
14879
14880 /* Fill in a "struct variant_part" for a given variant part. RESULT
14881 is the variant part to fill in. OBSTACK is where any needed
14882 allocations will be done. OFFSET_MAP holds the mapping from
14883 section offsets to fields for the type. FI describes the fields of
14884 the type we're processing. BUILDER is the variant part to be
14885 converted. */
14886
14887 static void
14888 create_one_variant_part (variant_part &result,
14889 struct obstack *obstack,
14890 const offset_map_type &offset_map,
14891 struct field_info *fi,
14892 const variant_part_builder &builder)
14893 {
14894 auto iter = offset_map.find (builder.discriminant_offset);
14895 if (iter == offset_map.end ())
14896 {
14897 result.discriminant_index = -1;
14898 /* Doesn't matter. */
14899 result.is_unsigned = false;
14900 }
14901 else
14902 {
14903 result.discriminant_index = iter->second;
14904 result.is_unsigned
14905 = TYPE_UNSIGNED (fi->fields[result.discriminant_index].field.type ());
14906 }
14907
14908 size_t n = builder.variants.size ();
14909 variant *output = new (obstack) variant[n];
14910 for (size_t i = 0; i < n; ++i)
14911 create_one_variant (output[i], obstack, offset_map, fi,
14912 builder.variants[i]);
14913
14914 result.variants = gdb::array_view<variant> (output, n);
14915 }
14916
14917 /* Create a vector of variant parts that can be attached to a type.
14918 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14919 holds the mapping from section offsets to fields for the type. FI
14920 describes the fields of the type we're processing. VARIANT_PARTS
14921 is the vector to convert. */
14922
14923 static const gdb::array_view<variant_part>
14924 create_variant_parts (struct obstack *obstack,
14925 const offset_map_type &offset_map,
14926 struct field_info *fi,
14927 const std::vector<variant_part_builder> &variant_parts)
14928 {
14929 if (variant_parts.empty ())
14930 return {};
14931
14932 size_t n = variant_parts.size ();
14933 variant_part *result = new (obstack) variant_part[n];
14934 for (size_t i = 0; i < n; ++i)
14935 create_one_variant_part (result[i], obstack, offset_map, fi,
14936 variant_parts[i]);
14937
14938 return gdb::array_view<variant_part> (result, n);
14939 }
14940
14941 /* Compute the variant part vector for FIP, attaching it to TYPE when
14942 done. */
14943
14944 static void
14945 add_variant_property (struct field_info *fip, struct type *type,
14946 struct dwarf2_cu *cu)
14947 {
14948 /* Map section offsets of fields to their field index. Note the
14949 field index here does not take the number of baseclasses into
14950 account. */
14951 offset_map_type offset_map;
14952 for (int i = 0; i < fip->fields.size (); ++i)
14953 offset_map[fip->fields[i].offset] = i;
14954
14955 struct objfile *objfile = cu->per_objfile->objfile;
14956 gdb::array_view<variant_part> parts
14957 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14958 fip->variant_parts);
14959
14960 struct dynamic_prop prop;
14961 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14962 obstack_copy (&objfile->objfile_obstack, &parts,
14963 sizeof (parts)));
14964
14965 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14966 }
14967
14968 /* Create the vector of fields, and attach it to the type. */
14969
14970 static void
14971 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14972 struct dwarf2_cu *cu)
14973 {
14974 int nfields = fip->nfields ();
14975
14976 /* Record the field count, allocate space for the array of fields,
14977 and create blank accessibility bitfields if necessary. */
14978 type->set_num_fields (nfields);
14979 type->set_fields
14980 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14981
14982 if (fip->non_public_fields && cu->language != language_ada)
14983 {
14984 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14985
14986 TYPE_FIELD_PRIVATE_BITS (type) =
14987 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14988 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14989
14990 TYPE_FIELD_PROTECTED_BITS (type) =
14991 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14992 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14993
14994 TYPE_FIELD_IGNORE_BITS (type) =
14995 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14996 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14997 }
14998
14999 /* If the type has baseclasses, allocate and clear a bit vector for
15000 TYPE_FIELD_VIRTUAL_BITS. */
15001 if (!fip->baseclasses.empty () && cu->language != language_ada)
15002 {
15003 int num_bytes = B_BYTES (fip->baseclasses.size ());
15004 unsigned char *pointer;
15005
15006 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15007 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15008 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15009 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15010 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15011 }
15012
15013 if (!fip->variant_parts.empty ())
15014 add_variant_property (fip, type, cu);
15015
15016 /* Copy the saved-up fields into the field vector. */
15017 for (int i = 0; i < nfields; ++i)
15018 {
15019 struct nextfield &field
15020 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15021 : fip->fields[i - fip->baseclasses.size ()]);
15022
15023 type->field (i) = field.field;
15024 switch (field.accessibility)
15025 {
15026 case DW_ACCESS_private:
15027 if (cu->language != language_ada)
15028 SET_TYPE_FIELD_PRIVATE (type, i);
15029 break;
15030
15031 case DW_ACCESS_protected:
15032 if (cu->language != language_ada)
15033 SET_TYPE_FIELD_PROTECTED (type, i);
15034 break;
15035
15036 case DW_ACCESS_public:
15037 break;
15038
15039 default:
15040 /* Unknown accessibility. Complain and treat it as public. */
15041 {
15042 complaint (_("unsupported accessibility %d"),
15043 field.accessibility);
15044 }
15045 break;
15046 }
15047 if (i < fip->baseclasses.size ())
15048 {
15049 switch (field.virtuality)
15050 {
15051 case DW_VIRTUALITY_virtual:
15052 case DW_VIRTUALITY_pure_virtual:
15053 if (cu->language == language_ada)
15054 error (_("unexpected virtuality in component of Ada type"));
15055 SET_TYPE_FIELD_VIRTUAL (type, i);
15056 break;
15057 }
15058 }
15059 }
15060 }
15061
15062 /* Return true if this member function is a constructor, false
15063 otherwise. */
15064
15065 static int
15066 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15067 {
15068 const char *fieldname;
15069 const char *type_name;
15070 int len;
15071
15072 if (die->parent == NULL)
15073 return 0;
15074
15075 if (die->parent->tag != DW_TAG_structure_type
15076 && die->parent->tag != DW_TAG_union_type
15077 && die->parent->tag != DW_TAG_class_type)
15078 return 0;
15079
15080 fieldname = dwarf2_name (die, cu);
15081 type_name = dwarf2_name (die->parent, cu);
15082 if (fieldname == NULL || type_name == NULL)
15083 return 0;
15084
15085 len = strlen (fieldname);
15086 return (strncmp (fieldname, type_name, len) == 0
15087 && (type_name[len] == '\0' || type_name[len] == '<'));
15088 }
15089
15090 /* Check if the given VALUE is a recognized enum
15091 dwarf_defaulted_attribute constant according to DWARF5 spec,
15092 Table 7.24. */
15093
15094 static bool
15095 is_valid_DW_AT_defaulted (ULONGEST value)
15096 {
15097 switch (value)
15098 {
15099 case DW_DEFAULTED_no:
15100 case DW_DEFAULTED_in_class:
15101 case DW_DEFAULTED_out_of_class:
15102 return true;
15103 }
15104
15105 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15106 return false;
15107 }
15108
15109 /* Add a member function to the proper fieldlist. */
15110
15111 static void
15112 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15113 struct type *type, struct dwarf2_cu *cu)
15114 {
15115 struct objfile *objfile = cu->per_objfile->objfile;
15116 struct attribute *attr;
15117 int i;
15118 struct fnfieldlist *flp = nullptr;
15119 struct fn_field *fnp;
15120 const char *fieldname;
15121 struct type *this_type;
15122 enum dwarf_access_attribute accessibility;
15123
15124 if (cu->language == language_ada)
15125 error (_("unexpected member function in Ada type"));
15126
15127 /* Get name of member function. */
15128 fieldname = dwarf2_name (die, cu);
15129 if (fieldname == NULL)
15130 return;
15131
15132 /* Look up member function name in fieldlist. */
15133 for (i = 0; i < fip->fnfieldlists.size (); i++)
15134 {
15135 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15136 {
15137 flp = &fip->fnfieldlists[i];
15138 break;
15139 }
15140 }
15141
15142 /* Create a new fnfieldlist if necessary. */
15143 if (flp == nullptr)
15144 {
15145 fip->fnfieldlists.emplace_back ();
15146 flp = &fip->fnfieldlists.back ();
15147 flp->name = fieldname;
15148 i = fip->fnfieldlists.size () - 1;
15149 }
15150
15151 /* Create a new member function field and add it to the vector of
15152 fnfieldlists. */
15153 flp->fnfields.emplace_back ();
15154 fnp = &flp->fnfields.back ();
15155
15156 /* Delay processing of the physname until later. */
15157 if (cu->language == language_cplus)
15158 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15159 die, cu);
15160 else
15161 {
15162 const char *physname = dwarf2_physname (fieldname, die, cu);
15163 fnp->physname = physname ? physname : "";
15164 }
15165
15166 fnp->type = alloc_type (objfile);
15167 this_type = read_type_die (die, cu);
15168 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15169 {
15170 int nparams = this_type->num_fields ();
15171
15172 /* TYPE is the domain of this method, and THIS_TYPE is the type
15173 of the method itself (TYPE_CODE_METHOD). */
15174 smash_to_method_type (fnp->type, type,
15175 TYPE_TARGET_TYPE (this_type),
15176 this_type->fields (),
15177 this_type->num_fields (),
15178 TYPE_VARARGS (this_type));
15179
15180 /* Handle static member functions.
15181 Dwarf2 has no clean way to discern C++ static and non-static
15182 member functions. G++ helps GDB by marking the first
15183 parameter for non-static member functions (which is the this
15184 pointer) as artificial. We obtain this information from
15185 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15186 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15187 fnp->voffset = VOFFSET_STATIC;
15188 }
15189 else
15190 complaint (_("member function type missing for '%s'"),
15191 dwarf2_full_name (fieldname, die, cu));
15192
15193 /* Get fcontext from DW_AT_containing_type if present. */
15194 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15195 fnp->fcontext = die_containing_type (die, cu);
15196
15197 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15198 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15199
15200 /* Get accessibility. */
15201 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15202 if (attr != nullptr)
15203 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15204 else
15205 accessibility = dwarf2_default_access_attribute (die, cu);
15206 switch (accessibility)
15207 {
15208 case DW_ACCESS_private:
15209 fnp->is_private = 1;
15210 break;
15211 case DW_ACCESS_protected:
15212 fnp->is_protected = 1;
15213 break;
15214 }
15215
15216 /* Check for artificial methods. */
15217 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15218 if (attr && DW_UNSND (attr) != 0)
15219 fnp->is_artificial = 1;
15220
15221 /* Check for defaulted methods. */
15222 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15223 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15224 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15225
15226 /* Check for deleted methods. */
15227 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15228 if (attr != nullptr && DW_UNSND (attr) != 0)
15229 fnp->is_deleted = 1;
15230
15231 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15232
15233 /* Get index in virtual function table if it is a virtual member
15234 function. For older versions of GCC, this is an offset in the
15235 appropriate virtual table, as specified by DW_AT_containing_type.
15236 For everyone else, it is an expression to be evaluated relative
15237 to the object address. */
15238
15239 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15240 if (attr != nullptr)
15241 {
15242 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15243 {
15244 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15245 {
15246 /* Old-style GCC. */
15247 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15248 }
15249 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15250 || (DW_BLOCK (attr)->size > 1
15251 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15252 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15253 {
15254 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15255 if ((fnp->voffset % cu->header.addr_size) != 0)
15256 dwarf2_complex_location_expr_complaint ();
15257 else
15258 fnp->voffset /= cu->header.addr_size;
15259 fnp->voffset += 2;
15260 }
15261 else
15262 dwarf2_complex_location_expr_complaint ();
15263
15264 if (!fnp->fcontext)
15265 {
15266 /* If there is no `this' field and no DW_AT_containing_type,
15267 we cannot actually find a base class context for the
15268 vtable! */
15269 if (this_type->num_fields () == 0
15270 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15271 {
15272 complaint (_("cannot determine context for virtual member "
15273 "function \"%s\" (offset %s)"),
15274 fieldname, sect_offset_str (die->sect_off));
15275 }
15276 else
15277 {
15278 fnp->fcontext
15279 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15280 }
15281 }
15282 }
15283 else if (attr->form_is_section_offset ())
15284 {
15285 dwarf2_complex_location_expr_complaint ();
15286 }
15287 else
15288 {
15289 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15290 fieldname);
15291 }
15292 }
15293 else
15294 {
15295 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15296 if (attr && DW_UNSND (attr))
15297 {
15298 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15299 complaint (_("Member function \"%s\" (offset %s) is virtual "
15300 "but the vtable offset is not specified"),
15301 fieldname, sect_offset_str (die->sect_off));
15302 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15303 TYPE_CPLUS_DYNAMIC (type) = 1;
15304 }
15305 }
15306 }
15307
15308 /* Create the vector of member function fields, and attach it to the type. */
15309
15310 static void
15311 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15312 struct dwarf2_cu *cu)
15313 {
15314 if (cu->language == language_ada)
15315 error (_("unexpected member functions in Ada type"));
15316
15317 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15318 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15319 TYPE_ALLOC (type,
15320 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15321
15322 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15323 {
15324 struct fnfieldlist &nf = fip->fnfieldlists[i];
15325 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15326
15327 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15328 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15329 fn_flp->fn_fields = (struct fn_field *)
15330 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15331
15332 for (int k = 0; k < nf.fnfields.size (); ++k)
15333 fn_flp->fn_fields[k] = nf.fnfields[k];
15334 }
15335
15336 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15337 }
15338
15339 /* Returns non-zero if NAME is the name of a vtable member in CU's
15340 language, zero otherwise. */
15341 static int
15342 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15343 {
15344 static const char vptr[] = "_vptr";
15345
15346 /* Look for the C++ form of the vtable. */
15347 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15348 return 1;
15349
15350 return 0;
15351 }
15352
15353 /* GCC outputs unnamed structures that are really pointers to member
15354 functions, with the ABI-specified layout. If TYPE describes
15355 such a structure, smash it into a member function type.
15356
15357 GCC shouldn't do this; it should just output pointer to member DIEs.
15358 This is GCC PR debug/28767. */
15359
15360 static void
15361 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15362 {
15363 struct type *pfn_type, *self_type, *new_type;
15364
15365 /* Check for a structure with no name and two children. */
15366 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15367 return;
15368
15369 /* Check for __pfn and __delta members. */
15370 if (TYPE_FIELD_NAME (type, 0) == NULL
15371 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15372 || TYPE_FIELD_NAME (type, 1) == NULL
15373 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15374 return;
15375
15376 /* Find the type of the method. */
15377 pfn_type = type->field (0).type ();
15378 if (pfn_type == NULL
15379 || pfn_type->code () != TYPE_CODE_PTR
15380 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15381 return;
15382
15383 /* Look for the "this" argument. */
15384 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15385 if (pfn_type->num_fields () == 0
15386 /* || pfn_type->field (0).type () == NULL */
15387 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15388 return;
15389
15390 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15391 new_type = alloc_type (objfile);
15392 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15393 pfn_type->fields (), pfn_type->num_fields (),
15394 TYPE_VARARGS (pfn_type));
15395 smash_to_methodptr_type (type, new_type);
15396 }
15397
15398 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15399 appropriate error checking and issuing complaints if there is a
15400 problem. */
15401
15402 static ULONGEST
15403 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15404 {
15405 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15406
15407 if (attr == nullptr)
15408 return 0;
15409
15410 if (!attr->form_is_constant ())
15411 {
15412 complaint (_("DW_AT_alignment must have constant form"
15413 " - DIE at %s [in module %s]"),
15414 sect_offset_str (die->sect_off),
15415 objfile_name (cu->per_objfile->objfile));
15416 return 0;
15417 }
15418
15419 ULONGEST align;
15420 if (attr->form == DW_FORM_sdata)
15421 {
15422 LONGEST val = DW_SND (attr);
15423 if (val < 0)
15424 {
15425 complaint (_("DW_AT_alignment value must not be negative"
15426 " - DIE at %s [in module %s]"),
15427 sect_offset_str (die->sect_off),
15428 objfile_name (cu->per_objfile->objfile));
15429 return 0;
15430 }
15431 align = val;
15432 }
15433 else
15434 align = DW_UNSND (attr);
15435
15436 if (align == 0)
15437 {
15438 complaint (_("DW_AT_alignment value must not be zero"
15439 " - DIE at %s [in module %s]"),
15440 sect_offset_str (die->sect_off),
15441 objfile_name (cu->per_objfile->objfile));
15442 return 0;
15443 }
15444 if ((align & (align - 1)) != 0)
15445 {
15446 complaint (_("DW_AT_alignment value must be a power of 2"
15447 " - DIE at %s [in module %s]"),
15448 sect_offset_str (die->sect_off),
15449 objfile_name (cu->per_objfile->objfile));
15450 return 0;
15451 }
15452
15453 return align;
15454 }
15455
15456 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15457 the alignment for TYPE. */
15458
15459 static void
15460 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15461 struct type *type)
15462 {
15463 if (!set_type_align (type, get_alignment (cu, die)))
15464 complaint (_("DW_AT_alignment value too large"
15465 " - DIE at %s [in module %s]"),
15466 sect_offset_str (die->sect_off),
15467 objfile_name (cu->per_objfile->objfile));
15468 }
15469
15470 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15471 constant for a type, according to DWARF5 spec, Table 5.5. */
15472
15473 static bool
15474 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15475 {
15476 switch (value)
15477 {
15478 case DW_CC_normal:
15479 case DW_CC_pass_by_reference:
15480 case DW_CC_pass_by_value:
15481 return true;
15482
15483 default:
15484 complaint (_("unrecognized DW_AT_calling_convention value "
15485 "(%s) for a type"), pulongest (value));
15486 return false;
15487 }
15488 }
15489
15490 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15491 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15492 also according to GNU-specific values (see include/dwarf2.h). */
15493
15494 static bool
15495 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15496 {
15497 switch (value)
15498 {
15499 case DW_CC_normal:
15500 case DW_CC_program:
15501 case DW_CC_nocall:
15502 return true;
15503
15504 case DW_CC_GNU_renesas_sh:
15505 case DW_CC_GNU_borland_fastcall_i386:
15506 case DW_CC_GDB_IBM_OpenCL:
15507 return true;
15508
15509 default:
15510 complaint (_("unrecognized DW_AT_calling_convention value "
15511 "(%s) for a subroutine"), pulongest (value));
15512 return false;
15513 }
15514 }
15515
15516 /* Called when we find the DIE that starts a structure or union scope
15517 (definition) to create a type for the structure or union. Fill in
15518 the type's name and general properties; the members will not be
15519 processed until process_structure_scope. A symbol table entry for
15520 the type will also not be done until process_structure_scope (assuming
15521 the type has a name).
15522
15523 NOTE: we need to call these functions regardless of whether or not the
15524 DIE has a DW_AT_name attribute, since it might be an anonymous
15525 structure or union. This gets the type entered into our set of
15526 user defined types. */
15527
15528 static struct type *
15529 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15530 {
15531 struct objfile *objfile = cu->per_objfile->objfile;
15532 struct type *type;
15533 struct attribute *attr;
15534 const char *name;
15535
15536 /* If the definition of this type lives in .debug_types, read that type.
15537 Don't follow DW_AT_specification though, that will take us back up
15538 the chain and we want to go down. */
15539 attr = die->attr (DW_AT_signature);
15540 if (attr != nullptr)
15541 {
15542 type = get_DW_AT_signature_type (die, attr, cu);
15543
15544 /* The type's CU may not be the same as CU.
15545 Ensure TYPE is recorded with CU in die_type_hash. */
15546 return set_die_type (die, type, cu);
15547 }
15548
15549 type = alloc_type (objfile);
15550 INIT_CPLUS_SPECIFIC (type);
15551
15552 name = dwarf2_name (die, cu);
15553 if (name != NULL)
15554 {
15555 if (cu->language == language_cplus
15556 || cu->language == language_d
15557 || cu->language == language_rust)
15558 {
15559 const char *full_name = dwarf2_full_name (name, die, cu);
15560
15561 /* dwarf2_full_name might have already finished building the DIE's
15562 type. If so, there is no need to continue. */
15563 if (get_die_type (die, cu) != NULL)
15564 return get_die_type (die, cu);
15565
15566 type->set_name (full_name);
15567 }
15568 else
15569 {
15570 /* The name is already allocated along with this objfile, so
15571 we don't need to duplicate it for the type. */
15572 type->set_name (name);
15573 }
15574 }
15575
15576 if (die->tag == DW_TAG_structure_type)
15577 {
15578 type->set_code (TYPE_CODE_STRUCT);
15579 }
15580 else if (die->tag == DW_TAG_union_type)
15581 {
15582 type->set_code (TYPE_CODE_UNION);
15583 }
15584 else
15585 {
15586 type->set_code (TYPE_CODE_STRUCT);
15587 }
15588
15589 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15590 TYPE_DECLARED_CLASS (type) = 1;
15591
15592 /* Store the calling convention in the type if it's available in
15593 the die. Otherwise the calling convention remains set to
15594 the default value DW_CC_normal. */
15595 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15596 if (attr != nullptr
15597 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15598 {
15599 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15600 TYPE_CPLUS_CALLING_CONVENTION (type)
15601 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15602 }
15603
15604 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15605 if (attr != nullptr)
15606 {
15607 if (attr->form_is_constant ())
15608 TYPE_LENGTH (type) = DW_UNSND (attr);
15609 else
15610 {
15611 struct dynamic_prop prop;
15612 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15613 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15614 TYPE_LENGTH (type) = 0;
15615 }
15616 }
15617 else
15618 {
15619 TYPE_LENGTH (type) = 0;
15620 }
15621
15622 maybe_set_alignment (cu, die, type);
15623
15624 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15625 {
15626 /* ICC<14 does not output the required DW_AT_declaration on
15627 incomplete types, but gives them a size of zero. */
15628 TYPE_STUB (type) = 1;
15629 }
15630 else
15631 TYPE_STUB_SUPPORTED (type) = 1;
15632
15633 if (die_is_declaration (die, cu))
15634 TYPE_STUB (type) = 1;
15635 else if (attr == NULL && die->child == NULL
15636 && producer_is_realview (cu->producer))
15637 /* RealView does not output the required DW_AT_declaration
15638 on incomplete types. */
15639 TYPE_STUB (type) = 1;
15640
15641 /* We need to add the type field to the die immediately so we don't
15642 infinitely recurse when dealing with pointers to the structure
15643 type within the structure itself. */
15644 set_die_type (die, type, cu);
15645
15646 /* set_die_type should be already done. */
15647 set_descriptive_type (type, die, cu);
15648
15649 return type;
15650 }
15651
15652 static void handle_struct_member_die
15653 (struct die_info *child_die,
15654 struct type *type,
15655 struct field_info *fi,
15656 std::vector<struct symbol *> *template_args,
15657 struct dwarf2_cu *cu);
15658
15659 /* A helper for handle_struct_member_die that handles
15660 DW_TAG_variant_part. */
15661
15662 static void
15663 handle_variant_part (struct die_info *die, struct type *type,
15664 struct field_info *fi,
15665 std::vector<struct symbol *> *template_args,
15666 struct dwarf2_cu *cu)
15667 {
15668 variant_part_builder *new_part;
15669 if (fi->current_variant_part == nullptr)
15670 {
15671 fi->variant_parts.emplace_back ();
15672 new_part = &fi->variant_parts.back ();
15673 }
15674 else if (!fi->current_variant_part->processing_variant)
15675 {
15676 complaint (_("nested DW_TAG_variant_part seen "
15677 "- DIE at %s [in module %s]"),
15678 sect_offset_str (die->sect_off),
15679 objfile_name (cu->per_objfile->objfile));
15680 return;
15681 }
15682 else
15683 {
15684 variant_field &current = fi->current_variant_part->variants.back ();
15685 current.variant_parts.emplace_back ();
15686 new_part = &current.variant_parts.back ();
15687 }
15688
15689 /* When we recurse, we want callees to add to this new variant
15690 part. */
15691 scoped_restore save_current_variant_part
15692 = make_scoped_restore (&fi->current_variant_part, new_part);
15693
15694 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15695 if (discr == NULL)
15696 {
15697 /* It's a univariant form, an extension we support. */
15698 }
15699 else if (discr->form_is_ref ())
15700 {
15701 struct dwarf2_cu *target_cu = cu;
15702 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15703
15704 new_part->discriminant_offset = target_die->sect_off;
15705 }
15706 else
15707 {
15708 complaint (_("DW_AT_discr does not have DIE reference form"
15709 " - DIE at %s [in module %s]"),
15710 sect_offset_str (die->sect_off),
15711 objfile_name (cu->per_objfile->objfile));
15712 }
15713
15714 for (die_info *child_die = die->child;
15715 child_die != NULL;
15716 child_die = child_die->sibling)
15717 handle_struct_member_die (child_die, type, fi, template_args, cu);
15718 }
15719
15720 /* A helper for handle_struct_member_die that handles
15721 DW_TAG_variant. */
15722
15723 static void
15724 handle_variant (struct die_info *die, struct type *type,
15725 struct field_info *fi,
15726 std::vector<struct symbol *> *template_args,
15727 struct dwarf2_cu *cu)
15728 {
15729 if (fi->current_variant_part == nullptr)
15730 {
15731 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15732 "- DIE at %s [in module %s]"),
15733 sect_offset_str (die->sect_off),
15734 objfile_name (cu->per_objfile->objfile));
15735 return;
15736 }
15737 if (fi->current_variant_part->processing_variant)
15738 {
15739 complaint (_("nested DW_TAG_variant seen "
15740 "- DIE at %s [in module %s]"),
15741 sect_offset_str (die->sect_off),
15742 objfile_name (cu->per_objfile->objfile));
15743 return;
15744 }
15745
15746 scoped_restore save_processing_variant
15747 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15748 true);
15749
15750 fi->current_variant_part->variants.emplace_back ();
15751 variant_field &variant = fi->current_variant_part->variants.back ();
15752 variant.first_field = fi->fields.size ();
15753
15754 /* In a variant we want to get the discriminant and also add a
15755 field for our sole member child. */
15756 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15757 if (discr == nullptr)
15758 {
15759 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15760 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15761 variant.default_branch = true;
15762 else
15763 variant.discr_list_data = DW_BLOCK (discr);
15764 }
15765 else
15766 variant.discriminant_value = DW_UNSND (discr);
15767
15768 for (die_info *variant_child = die->child;
15769 variant_child != NULL;
15770 variant_child = variant_child->sibling)
15771 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15772
15773 variant.last_field = fi->fields.size ();
15774 }
15775
15776 /* A helper for process_structure_scope that handles a single member
15777 DIE. */
15778
15779 static void
15780 handle_struct_member_die (struct die_info *child_die, struct type *type,
15781 struct field_info *fi,
15782 std::vector<struct symbol *> *template_args,
15783 struct dwarf2_cu *cu)
15784 {
15785 if (child_die->tag == DW_TAG_member
15786 || child_die->tag == DW_TAG_variable)
15787 {
15788 /* NOTE: carlton/2002-11-05: A C++ static data member
15789 should be a DW_TAG_member that is a declaration, but
15790 all versions of G++ as of this writing (so through at
15791 least 3.2.1) incorrectly generate DW_TAG_variable
15792 tags for them instead. */
15793 dwarf2_add_field (fi, child_die, cu);
15794 }
15795 else if (child_die->tag == DW_TAG_subprogram)
15796 {
15797 /* Rust doesn't have member functions in the C++ sense.
15798 However, it does emit ordinary functions as children
15799 of a struct DIE. */
15800 if (cu->language == language_rust)
15801 read_func_scope (child_die, cu);
15802 else
15803 {
15804 /* C++ member function. */
15805 dwarf2_add_member_fn (fi, child_die, type, cu);
15806 }
15807 }
15808 else if (child_die->tag == DW_TAG_inheritance)
15809 {
15810 /* C++ base class field. */
15811 dwarf2_add_field (fi, child_die, cu);
15812 }
15813 else if (type_can_define_types (child_die))
15814 dwarf2_add_type_defn (fi, child_die, cu);
15815 else if (child_die->tag == DW_TAG_template_type_param
15816 || child_die->tag == DW_TAG_template_value_param)
15817 {
15818 struct symbol *arg = new_symbol (child_die, NULL, cu);
15819
15820 if (arg != NULL)
15821 template_args->push_back (arg);
15822 }
15823 else if (child_die->tag == DW_TAG_variant_part)
15824 handle_variant_part (child_die, type, fi, template_args, cu);
15825 else if (child_die->tag == DW_TAG_variant)
15826 handle_variant (child_die, type, fi, template_args, cu);
15827 }
15828
15829 /* Finish creating a structure or union type, including filling in
15830 its members and creating a symbol for it. */
15831
15832 static void
15833 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15834 {
15835 struct objfile *objfile = cu->per_objfile->objfile;
15836 struct die_info *child_die;
15837 struct type *type;
15838
15839 type = get_die_type (die, cu);
15840 if (type == NULL)
15841 type = read_structure_type (die, cu);
15842
15843 bool has_template_parameters = false;
15844 if (die->child != NULL && ! die_is_declaration (die, cu))
15845 {
15846 struct field_info fi;
15847 std::vector<struct symbol *> template_args;
15848
15849 child_die = die->child;
15850
15851 while (child_die && child_die->tag)
15852 {
15853 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15854 child_die = child_die->sibling;
15855 }
15856
15857 /* Attach template arguments to type. */
15858 if (!template_args.empty ())
15859 {
15860 has_template_parameters = true;
15861 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15862 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15863 TYPE_TEMPLATE_ARGUMENTS (type)
15864 = XOBNEWVEC (&objfile->objfile_obstack,
15865 struct symbol *,
15866 TYPE_N_TEMPLATE_ARGUMENTS (type));
15867 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15868 template_args.data (),
15869 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15870 * sizeof (struct symbol *)));
15871 }
15872
15873 /* Attach fields and member functions to the type. */
15874 if (fi.nfields () > 0)
15875 dwarf2_attach_fields_to_type (&fi, type, cu);
15876 if (!fi.fnfieldlists.empty ())
15877 {
15878 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15879
15880 /* Get the type which refers to the base class (possibly this
15881 class itself) which contains the vtable pointer for the current
15882 class from the DW_AT_containing_type attribute. This use of
15883 DW_AT_containing_type is a GNU extension. */
15884
15885 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15886 {
15887 struct type *t = die_containing_type (die, cu);
15888
15889 set_type_vptr_basetype (type, t);
15890 if (type == t)
15891 {
15892 int i;
15893
15894 /* Our own class provides vtbl ptr. */
15895 for (i = t->num_fields () - 1;
15896 i >= TYPE_N_BASECLASSES (t);
15897 --i)
15898 {
15899 const char *fieldname = TYPE_FIELD_NAME (t, i);
15900
15901 if (is_vtable_name (fieldname, cu))
15902 {
15903 set_type_vptr_fieldno (type, i);
15904 break;
15905 }
15906 }
15907
15908 /* Complain if virtual function table field not found. */
15909 if (i < TYPE_N_BASECLASSES (t))
15910 complaint (_("virtual function table pointer "
15911 "not found when defining class '%s'"),
15912 type->name () ? type->name () : "");
15913 }
15914 else
15915 {
15916 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15917 }
15918 }
15919 else if (cu->producer
15920 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15921 {
15922 /* The IBM XLC compiler does not provide direct indication
15923 of the containing type, but the vtable pointer is
15924 always named __vfp. */
15925
15926 int i;
15927
15928 for (i = type->num_fields () - 1;
15929 i >= TYPE_N_BASECLASSES (type);
15930 --i)
15931 {
15932 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15933 {
15934 set_type_vptr_fieldno (type, i);
15935 set_type_vptr_basetype (type, type);
15936 break;
15937 }
15938 }
15939 }
15940 }
15941
15942 /* Copy fi.typedef_field_list linked list elements content into the
15943 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15944 if (!fi.typedef_field_list.empty ())
15945 {
15946 int count = fi.typedef_field_list.size ();
15947
15948 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15949 TYPE_TYPEDEF_FIELD_ARRAY (type)
15950 = ((struct decl_field *)
15951 TYPE_ALLOC (type,
15952 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15953 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15954
15955 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15956 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15957 }
15958
15959 /* Copy fi.nested_types_list linked list elements content into the
15960 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15961 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15962 {
15963 int count = fi.nested_types_list.size ();
15964
15965 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15966 TYPE_NESTED_TYPES_ARRAY (type)
15967 = ((struct decl_field *)
15968 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15969 TYPE_NESTED_TYPES_COUNT (type) = count;
15970
15971 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15972 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15973 }
15974 }
15975
15976 quirk_gcc_member_function_pointer (type, objfile);
15977 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15978 cu->rust_unions.push_back (type);
15979
15980 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15981 snapshots) has been known to create a die giving a declaration
15982 for a class that has, as a child, a die giving a definition for a
15983 nested class. So we have to process our children even if the
15984 current die is a declaration. Normally, of course, a declaration
15985 won't have any children at all. */
15986
15987 child_die = die->child;
15988
15989 while (child_die != NULL && child_die->tag)
15990 {
15991 if (child_die->tag == DW_TAG_member
15992 || child_die->tag == DW_TAG_variable
15993 || child_die->tag == DW_TAG_inheritance
15994 || child_die->tag == DW_TAG_template_value_param
15995 || child_die->tag == DW_TAG_template_type_param)
15996 {
15997 /* Do nothing. */
15998 }
15999 else
16000 process_die (child_die, cu);
16001
16002 child_die = child_die->sibling;
16003 }
16004
16005 /* Do not consider external references. According to the DWARF standard,
16006 these DIEs are identified by the fact that they have no byte_size
16007 attribute, and a declaration attribute. */
16008 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16009 || !die_is_declaration (die, cu)
16010 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16011 {
16012 struct symbol *sym = new_symbol (die, type, cu);
16013
16014 if (has_template_parameters)
16015 {
16016 struct symtab *symtab;
16017 if (sym != nullptr)
16018 symtab = symbol_symtab (sym);
16019 else if (cu->line_header != nullptr)
16020 {
16021 /* Any related symtab will do. */
16022 symtab
16023 = cu->line_header->file_names ()[0].symtab;
16024 }
16025 else
16026 {
16027 symtab = nullptr;
16028 complaint (_("could not find suitable "
16029 "symtab for template parameter"
16030 " - DIE at %s [in module %s]"),
16031 sect_offset_str (die->sect_off),
16032 objfile_name (objfile));
16033 }
16034
16035 if (symtab != nullptr)
16036 {
16037 /* Make sure that the symtab is set on the new symbols.
16038 Even though they don't appear in this symtab directly,
16039 other parts of gdb assume that symbols do, and this is
16040 reasonably true. */
16041 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16042 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16043 }
16044 }
16045 }
16046 }
16047
16048 /* Assuming DIE is an enumeration type, and TYPE is its associated
16049 type, update TYPE using some information only available in DIE's
16050 children. In particular, the fields are computed. */
16051
16052 static void
16053 update_enumeration_type_from_children (struct die_info *die,
16054 struct type *type,
16055 struct dwarf2_cu *cu)
16056 {
16057 struct die_info *child_die;
16058 int unsigned_enum = 1;
16059 int flag_enum = 1;
16060
16061 auto_obstack obstack;
16062 std::vector<struct field> fields;
16063
16064 for (child_die = die->child;
16065 child_die != NULL && child_die->tag;
16066 child_die = child_die->sibling)
16067 {
16068 struct attribute *attr;
16069 LONGEST value;
16070 const gdb_byte *bytes;
16071 struct dwarf2_locexpr_baton *baton;
16072 const char *name;
16073
16074 if (child_die->tag != DW_TAG_enumerator)
16075 continue;
16076
16077 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16078 if (attr == NULL)
16079 continue;
16080
16081 name = dwarf2_name (child_die, cu);
16082 if (name == NULL)
16083 name = "<anonymous enumerator>";
16084
16085 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16086 &value, &bytes, &baton);
16087 if (value < 0)
16088 {
16089 unsigned_enum = 0;
16090 flag_enum = 0;
16091 }
16092 else
16093 {
16094 if (count_one_bits_ll (value) >= 2)
16095 flag_enum = 0;
16096 }
16097
16098 fields.emplace_back ();
16099 struct field &field = fields.back ();
16100 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16101 SET_FIELD_ENUMVAL (field, value);
16102 }
16103
16104 if (!fields.empty ())
16105 {
16106 type->set_num_fields (fields.size ());
16107 type->set_fields
16108 ((struct field *)
16109 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16110 memcpy (type->fields (), fields.data (),
16111 sizeof (struct field) * fields.size ());
16112 }
16113
16114 if (unsigned_enum)
16115 TYPE_UNSIGNED (type) = 1;
16116 if (flag_enum)
16117 TYPE_FLAG_ENUM (type) = 1;
16118 }
16119
16120 /* Given a DW_AT_enumeration_type die, set its type. We do not
16121 complete the type's fields yet, or create any symbols. */
16122
16123 static struct type *
16124 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16125 {
16126 struct objfile *objfile = cu->per_objfile->objfile;
16127 struct type *type;
16128 struct attribute *attr;
16129 const char *name;
16130
16131 /* If the definition of this type lives in .debug_types, read that type.
16132 Don't follow DW_AT_specification though, that will take us back up
16133 the chain and we want to go down. */
16134 attr = die->attr (DW_AT_signature);
16135 if (attr != nullptr)
16136 {
16137 type = get_DW_AT_signature_type (die, attr, cu);
16138
16139 /* The type's CU may not be the same as CU.
16140 Ensure TYPE is recorded with CU in die_type_hash. */
16141 return set_die_type (die, type, cu);
16142 }
16143
16144 type = alloc_type (objfile);
16145
16146 type->set_code (TYPE_CODE_ENUM);
16147 name = dwarf2_full_name (NULL, die, cu);
16148 if (name != NULL)
16149 type->set_name (name);
16150
16151 attr = dwarf2_attr (die, DW_AT_type, cu);
16152 if (attr != NULL)
16153 {
16154 struct type *underlying_type = die_type (die, cu);
16155
16156 TYPE_TARGET_TYPE (type) = underlying_type;
16157 }
16158
16159 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16160 if (attr != nullptr)
16161 {
16162 TYPE_LENGTH (type) = DW_UNSND (attr);
16163 }
16164 else
16165 {
16166 TYPE_LENGTH (type) = 0;
16167 }
16168
16169 maybe_set_alignment (cu, die, type);
16170
16171 /* The enumeration DIE can be incomplete. In Ada, any type can be
16172 declared as private in the package spec, and then defined only
16173 inside the package body. Such types are known as Taft Amendment
16174 Types. When another package uses such a type, an incomplete DIE
16175 may be generated by the compiler. */
16176 if (die_is_declaration (die, cu))
16177 TYPE_STUB (type) = 1;
16178
16179 /* If this type has an underlying type that is not a stub, then we
16180 may use its attributes. We always use the "unsigned" attribute
16181 in this situation, because ordinarily we guess whether the type
16182 is unsigned -- but the guess can be wrong and the underlying type
16183 can tell us the reality. However, we defer to a local size
16184 attribute if one exists, because this lets the compiler override
16185 the underlying type if needed. */
16186 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16187 {
16188 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16189 underlying_type = check_typedef (underlying_type);
16190 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16191 if (TYPE_LENGTH (type) == 0)
16192 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16193 if (TYPE_RAW_ALIGN (type) == 0
16194 && TYPE_RAW_ALIGN (underlying_type) != 0)
16195 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16196 }
16197
16198 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16199
16200 set_die_type (die, type, cu);
16201
16202 /* Finish the creation of this type by using the enum's children.
16203 Note that, as usual, this must come after set_die_type to avoid
16204 infinite recursion when trying to compute the names of the
16205 enumerators. */
16206 update_enumeration_type_from_children (die, type, cu);
16207
16208 return type;
16209 }
16210
16211 /* Given a pointer to a die which begins an enumeration, process all
16212 the dies that define the members of the enumeration, and create the
16213 symbol for the enumeration type.
16214
16215 NOTE: We reverse the order of the element list. */
16216
16217 static void
16218 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16219 {
16220 struct type *this_type;
16221
16222 this_type = get_die_type (die, cu);
16223 if (this_type == NULL)
16224 this_type = read_enumeration_type (die, cu);
16225
16226 if (die->child != NULL)
16227 {
16228 struct die_info *child_die;
16229 const char *name;
16230
16231 child_die = die->child;
16232 while (child_die && child_die->tag)
16233 {
16234 if (child_die->tag != DW_TAG_enumerator)
16235 {
16236 process_die (child_die, cu);
16237 }
16238 else
16239 {
16240 name = dwarf2_name (child_die, cu);
16241 if (name)
16242 new_symbol (child_die, this_type, cu);
16243 }
16244
16245 child_die = child_die->sibling;
16246 }
16247 }
16248
16249 /* If we are reading an enum from a .debug_types unit, and the enum
16250 is a declaration, and the enum is not the signatured type in the
16251 unit, then we do not want to add a symbol for it. Adding a
16252 symbol would in some cases obscure the true definition of the
16253 enum, giving users an incomplete type when the definition is
16254 actually available. Note that we do not want to do this for all
16255 enums which are just declarations, because C++0x allows forward
16256 enum declarations. */
16257 if (cu->per_cu->is_debug_types
16258 && die_is_declaration (die, cu))
16259 {
16260 struct signatured_type *sig_type;
16261
16262 sig_type = (struct signatured_type *) cu->per_cu;
16263 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16264 if (sig_type->type_offset_in_section != die->sect_off)
16265 return;
16266 }
16267
16268 new_symbol (die, this_type, cu);
16269 }
16270
16271 /* Extract all information from a DW_TAG_array_type DIE and put it in
16272 the DIE's type field. For now, this only handles one dimensional
16273 arrays. */
16274
16275 static struct type *
16276 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16277 {
16278 struct objfile *objfile = cu->per_objfile->objfile;
16279 struct die_info *child_die;
16280 struct type *type;
16281 struct type *element_type, *range_type, *index_type;
16282 struct attribute *attr;
16283 const char *name;
16284 struct dynamic_prop *byte_stride_prop = NULL;
16285 unsigned int bit_stride = 0;
16286
16287 element_type = die_type (die, cu);
16288
16289 /* The die_type call above may have already set the type for this DIE. */
16290 type = get_die_type (die, cu);
16291 if (type)
16292 return type;
16293
16294 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16295 if (attr != NULL)
16296 {
16297 int stride_ok;
16298 struct type *prop_type = cu->addr_sized_int_type (false);
16299
16300 byte_stride_prop
16301 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16302 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16303 prop_type);
16304 if (!stride_ok)
16305 {
16306 complaint (_("unable to read array DW_AT_byte_stride "
16307 " - DIE at %s [in module %s]"),
16308 sect_offset_str (die->sect_off),
16309 objfile_name (cu->per_objfile->objfile));
16310 /* Ignore this attribute. We will likely not be able to print
16311 arrays of this type correctly, but there is little we can do
16312 to help if we cannot read the attribute's value. */
16313 byte_stride_prop = NULL;
16314 }
16315 }
16316
16317 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16318 if (attr != NULL)
16319 bit_stride = DW_UNSND (attr);
16320
16321 /* Irix 6.2 native cc creates array types without children for
16322 arrays with unspecified length. */
16323 if (die->child == NULL)
16324 {
16325 index_type = objfile_type (objfile)->builtin_int;
16326 range_type = create_static_range_type (NULL, index_type, 0, -1);
16327 type = create_array_type_with_stride (NULL, element_type, range_type,
16328 byte_stride_prop, bit_stride);
16329 return set_die_type (die, type, cu);
16330 }
16331
16332 std::vector<struct type *> range_types;
16333 child_die = die->child;
16334 while (child_die && child_die->tag)
16335 {
16336 if (child_die->tag == DW_TAG_subrange_type)
16337 {
16338 struct type *child_type = read_type_die (child_die, cu);
16339
16340 if (child_type != NULL)
16341 {
16342 /* The range type was succesfully read. Save it for the
16343 array type creation. */
16344 range_types.push_back (child_type);
16345 }
16346 }
16347 child_die = child_die->sibling;
16348 }
16349
16350 /* Dwarf2 dimensions are output from left to right, create the
16351 necessary array types in backwards order. */
16352
16353 type = element_type;
16354
16355 if (read_array_order (die, cu) == DW_ORD_col_major)
16356 {
16357 int i = 0;
16358
16359 while (i < range_types.size ())
16360 type = create_array_type_with_stride (NULL, type, range_types[i++],
16361 byte_stride_prop, bit_stride);
16362 }
16363 else
16364 {
16365 size_t ndim = range_types.size ();
16366 while (ndim-- > 0)
16367 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16368 byte_stride_prop, bit_stride);
16369 }
16370
16371 /* Understand Dwarf2 support for vector types (like they occur on
16372 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16373 array type. This is not part of the Dwarf2/3 standard yet, but a
16374 custom vendor extension. The main difference between a regular
16375 array and the vector variant is that vectors are passed by value
16376 to functions. */
16377 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16378 if (attr != nullptr)
16379 make_vector_type (type);
16380
16381 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16382 implementation may choose to implement triple vectors using this
16383 attribute. */
16384 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16385 if (attr != nullptr)
16386 {
16387 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16388 TYPE_LENGTH (type) = DW_UNSND (attr);
16389 else
16390 complaint (_("DW_AT_byte_size for array type smaller "
16391 "than the total size of elements"));
16392 }
16393
16394 name = dwarf2_name (die, cu);
16395 if (name)
16396 type->set_name (name);
16397
16398 maybe_set_alignment (cu, die, type);
16399
16400 /* Install the type in the die. */
16401 set_die_type (die, type, cu);
16402
16403 /* set_die_type should be already done. */
16404 set_descriptive_type (type, die, cu);
16405
16406 return type;
16407 }
16408
16409 static enum dwarf_array_dim_ordering
16410 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16411 {
16412 struct attribute *attr;
16413
16414 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16415
16416 if (attr != nullptr)
16417 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16418
16419 /* GNU F77 is a special case, as at 08/2004 array type info is the
16420 opposite order to the dwarf2 specification, but data is still
16421 laid out as per normal fortran.
16422
16423 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16424 version checking. */
16425
16426 if (cu->language == language_fortran
16427 && cu->producer && strstr (cu->producer, "GNU F77"))
16428 {
16429 return DW_ORD_row_major;
16430 }
16431
16432 switch (cu->language_defn->la_array_ordering)
16433 {
16434 case array_column_major:
16435 return DW_ORD_col_major;
16436 case array_row_major:
16437 default:
16438 return DW_ORD_row_major;
16439 };
16440 }
16441
16442 /* Extract all information from a DW_TAG_set_type DIE and put it in
16443 the DIE's type field. */
16444
16445 static struct type *
16446 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16447 {
16448 struct type *domain_type, *set_type;
16449 struct attribute *attr;
16450
16451 domain_type = die_type (die, cu);
16452
16453 /* The die_type call above may have already set the type for this DIE. */
16454 set_type = get_die_type (die, cu);
16455 if (set_type)
16456 return set_type;
16457
16458 set_type = create_set_type (NULL, domain_type);
16459
16460 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16461 if (attr != nullptr)
16462 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16463
16464 maybe_set_alignment (cu, die, set_type);
16465
16466 return set_die_type (die, set_type, cu);
16467 }
16468
16469 /* A helper for read_common_block that creates a locexpr baton.
16470 SYM is the symbol which we are marking as computed.
16471 COMMON_DIE is the DIE for the common block.
16472 COMMON_LOC is the location expression attribute for the common
16473 block itself.
16474 MEMBER_LOC is the location expression attribute for the particular
16475 member of the common block that we are processing.
16476 CU is the CU from which the above come. */
16477
16478 static void
16479 mark_common_block_symbol_computed (struct symbol *sym,
16480 struct die_info *common_die,
16481 struct attribute *common_loc,
16482 struct attribute *member_loc,
16483 struct dwarf2_cu *cu)
16484 {
16485 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16486 struct objfile *objfile = per_objfile->objfile;
16487 struct dwarf2_locexpr_baton *baton;
16488 gdb_byte *ptr;
16489 unsigned int cu_off;
16490 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16491 LONGEST offset = 0;
16492
16493 gdb_assert (common_loc && member_loc);
16494 gdb_assert (common_loc->form_is_block ());
16495 gdb_assert (member_loc->form_is_block ()
16496 || member_loc->form_is_constant ());
16497
16498 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16499 baton->per_objfile = per_objfile;
16500 baton->per_cu = cu->per_cu;
16501 gdb_assert (baton->per_cu);
16502
16503 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16504
16505 if (member_loc->form_is_constant ())
16506 {
16507 offset = member_loc->constant_value (0);
16508 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16509 }
16510 else
16511 baton->size += DW_BLOCK (member_loc)->size;
16512
16513 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16514 baton->data = ptr;
16515
16516 *ptr++ = DW_OP_call4;
16517 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16518 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16519 ptr += 4;
16520
16521 if (member_loc->form_is_constant ())
16522 {
16523 *ptr++ = DW_OP_addr;
16524 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16525 ptr += cu->header.addr_size;
16526 }
16527 else
16528 {
16529 /* We have to copy the data here, because DW_OP_call4 will only
16530 use a DW_AT_location attribute. */
16531 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16532 ptr += DW_BLOCK (member_loc)->size;
16533 }
16534
16535 *ptr++ = DW_OP_plus;
16536 gdb_assert (ptr - baton->data == baton->size);
16537
16538 SYMBOL_LOCATION_BATON (sym) = baton;
16539 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16540 }
16541
16542 /* Create appropriate locally-scoped variables for all the
16543 DW_TAG_common_block entries. Also create a struct common_block
16544 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16545 is used to separate the common blocks name namespace from regular
16546 variable names. */
16547
16548 static void
16549 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16550 {
16551 struct attribute *attr;
16552
16553 attr = dwarf2_attr (die, DW_AT_location, cu);
16554 if (attr != nullptr)
16555 {
16556 /* Support the .debug_loc offsets. */
16557 if (attr->form_is_block ())
16558 {
16559 /* Ok. */
16560 }
16561 else if (attr->form_is_section_offset ())
16562 {
16563 dwarf2_complex_location_expr_complaint ();
16564 attr = NULL;
16565 }
16566 else
16567 {
16568 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16569 "common block member");
16570 attr = NULL;
16571 }
16572 }
16573
16574 if (die->child != NULL)
16575 {
16576 struct objfile *objfile = cu->per_objfile->objfile;
16577 struct die_info *child_die;
16578 size_t n_entries = 0, size;
16579 struct common_block *common_block;
16580 struct symbol *sym;
16581
16582 for (child_die = die->child;
16583 child_die && child_die->tag;
16584 child_die = child_die->sibling)
16585 ++n_entries;
16586
16587 size = (sizeof (struct common_block)
16588 + (n_entries - 1) * sizeof (struct symbol *));
16589 common_block
16590 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16591 size);
16592 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16593 common_block->n_entries = 0;
16594
16595 for (child_die = die->child;
16596 child_die && child_die->tag;
16597 child_die = child_die->sibling)
16598 {
16599 /* Create the symbol in the DW_TAG_common_block block in the current
16600 symbol scope. */
16601 sym = new_symbol (child_die, NULL, cu);
16602 if (sym != NULL)
16603 {
16604 struct attribute *member_loc;
16605
16606 common_block->contents[common_block->n_entries++] = sym;
16607
16608 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16609 cu);
16610 if (member_loc)
16611 {
16612 /* GDB has handled this for a long time, but it is
16613 not specified by DWARF. It seems to have been
16614 emitted by gfortran at least as recently as:
16615 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16616 complaint (_("Variable in common block has "
16617 "DW_AT_data_member_location "
16618 "- DIE at %s [in module %s]"),
16619 sect_offset_str (child_die->sect_off),
16620 objfile_name (objfile));
16621
16622 if (member_loc->form_is_section_offset ())
16623 dwarf2_complex_location_expr_complaint ();
16624 else if (member_loc->form_is_constant ()
16625 || member_loc->form_is_block ())
16626 {
16627 if (attr != nullptr)
16628 mark_common_block_symbol_computed (sym, die, attr,
16629 member_loc, cu);
16630 }
16631 else
16632 dwarf2_complex_location_expr_complaint ();
16633 }
16634 }
16635 }
16636
16637 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16638 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16639 }
16640 }
16641
16642 /* Create a type for a C++ namespace. */
16643
16644 static struct type *
16645 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16646 {
16647 struct objfile *objfile = cu->per_objfile->objfile;
16648 const char *previous_prefix, *name;
16649 int is_anonymous;
16650 struct type *type;
16651
16652 /* For extensions, reuse the type of the original namespace. */
16653 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16654 {
16655 struct die_info *ext_die;
16656 struct dwarf2_cu *ext_cu = cu;
16657
16658 ext_die = dwarf2_extension (die, &ext_cu);
16659 type = read_type_die (ext_die, ext_cu);
16660
16661 /* EXT_CU may not be the same as CU.
16662 Ensure TYPE is recorded with CU in die_type_hash. */
16663 return set_die_type (die, type, cu);
16664 }
16665
16666 name = namespace_name (die, &is_anonymous, cu);
16667
16668 /* Now build the name of the current namespace. */
16669
16670 previous_prefix = determine_prefix (die, cu);
16671 if (previous_prefix[0] != '\0')
16672 name = typename_concat (&objfile->objfile_obstack,
16673 previous_prefix, name, 0, cu);
16674
16675 /* Create the type. */
16676 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16677
16678 return set_die_type (die, type, cu);
16679 }
16680
16681 /* Read a namespace scope. */
16682
16683 static void
16684 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16685 {
16686 struct objfile *objfile = cu->per_objfile->objfile;
16687 int is_anonymous;
16688
16689 /* Add a symbol associated to this if we haven't seen the namespace
16690 before. Also, add a using directive if it's an anonymous
16691 namespace. */
16692
16693 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16694 {
16695 struct type *type;
16696
16697 type = read_type_die (die, cu);
16698 new_symbol (die, type, cu);
16699
16700 namespace_name (die, &is_anonymous, cu);
16701 if (is_anonymous)
16702 {
16703 const char *previous_prefix = determine_prefix (die, cu);
16704
16705 std::vector<const char *> excludes;
16706 add_using_directive (using_directives (cu),
16707 previous_prefix, type->name (), NULL,
16708 NULL, excludes, 0, &objfile->objfile_obstack);
16709 }
16710 }
16711
16712 if (die->child != NULL)
16713 {
16714 struct die_info *child_die = die->child;
16715
16716 while (child_die && child_die->tag)
16717 {
16718 process_die (child_die, cu);
16719 child_die = child_die->sibling;
16720 }
16721 }
16722 }
16723
16724 /* Read a Fortran module as type. This DIE can be only a declaration used for
16725 imported module. Still we need that type as local Fortran "use ... only"
16726 declaration imports depend on the created type in determine_prefix. */
16727
16728 static struct type *
16729 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16730 {
16731 struct objfile *objfile = cu->per_objfile->objfile;
16732 const char *module_name;
16733 struct type *type;
16734
16735 module_name = dwarf2_name (die, cu);
16736 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16737
16738 return set_die_type (die, type, cu);
16739 }
16740
16741 /* Read a Fortran module. */
16742
16743 static void
16744 read_module (struct die_info *die, struct dwarf2_cu *cu)
16745 {
16746 struct die_info *child_die = die->child;
16747 struct type *type;
16748
16749 type = read_type_die (die, cu);
16750 new_symbol (die, type, cu);
16751
16752 while (child_die && child_die->tag)
16753 {
16754 process_die (child_die, cu);
16755 child_die = child_die->sibling;
16756 }
16757 }
16758
16759 /* Return the name of the namespace represented by DIE. Set
16760 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16761 namespace. */
16762
16763 static const char *
16764 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16765 {
16766 struct die_info *current_die;
16767 const char *name = NULL;
16768
16769 /* Loop through the extensions until we find a name. */
16770
16771 for (current_die = die;
16772 current_die != NULL;
16773 current_die = dwarf2_extension (die, &cu))
16774 {
16775 /* We don't use dwarf2_name here so that we can detect the absence
16776 of a name -> anonymous namespace. */
16777 name = dwarf2_string_attr (die, DW_AT_name, cu);
16778
16779 if (name != NULL)
16780 break;
16781 }
16782
16783 /* Is it an anonymous namespace? */
16784
16785 *is_anonymous = (name == NULL);
16786 if (*is_anonymous)
16787 name = CP_ANONYMOUS_NAMESPACE_STR;
16788
16789 return name;
16790 }
16791
16792 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16793 the user defined type vector. */
16794
16795 static struct type *
16796 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16797 {
16798 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16799 struct comp_unit_head *cu_header = &cu->header;
16800 struct type *type;
16801 struct attribute *attr_byte_size;
16802 struct attribute *attr_address_class;
16803 int byte_size, addr_class;
16804 struct type *target_type;
16805
16806 target_type = die_type (die, cu);
16807
16808 /* The die_type call above may have already set the type for this DIE. */
16809 type = get_die_type (die, cu);
16810 if (type)
16811 return type;
16812
16813 type = lookup_pointer_type (target_type);
16814
16815 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16816 if (attr_byte_size)
16817 byte_size = DW_UNSND (attr_byte_size);
16818 else
16819 byte_size = cu_header->addr_size;
16820
16821 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16822 if (attr_address_class)
16823 addr_class = DW_UNSND (attr_address_class);
16824 else
16825 addr_class = DW_ADDR_none;
16826
16827 ULONGEST alignment = get_alignment (cu, die);
16828
16829 /* If the pointer size, alignment, or address class is different
16830 than the default, create a type variant marked as such and set
16831 the length accordingly. */
16832 if (TYPE_LENGTH (type) != byte_size
16833 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16834 && alignment != TYPE_RAW_ALIGN (type))
16835 || addr_class != DW_ADDR_none)
16836 {
16837 if (gdbarch_address_class_type_flags_p (gdbarch))
16838 {
16839 int type_flags;
16840
16841 type_flags = gdbarch_address_class_type_flags
16842 (gdbarch, byte_size, addr_class);
16843 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16844 == 0);
16845 type = make_type_with_address_space (type, type_flags);
16846 }
16847 else if (TYPE_LENGTH (type) != byte_size)
16848 {
16849 complaint (_("invalid pointer size %d"), byte_size);
16850 }
16851 else if (TYPE_RAW_ALIGN (type) != alignment)
16852 {
16853 complaint (_("Invalid DW_AT_alignment"
16854 " - DIE at %s [in module %s]"),
16855 sect_offset_str (die->sect_off),
16856 objfile_name (cu->per_objfile->objfile));
16857 }
16858 else
16859 {
16860 /* Should we also complain about unhandled address classes? */
16861 }
16862 }
16863
16864 TYPE_LENGTH (type) = byte_size;
16865 set_type_align (type, alignment);
16866 return set_die_type (die, type, cu);
16867 }
16868
16869 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16870 the user defined type vector. */
16871
16872 static struct type *
16873 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16874 {
16875 struct type *type;
16876 struct type *to_type;
16877 struct type *domain;
16878
16879 to_type = die_type (die, cu);
16880 domain = die_containing_type (die, cu);
16881
16882 /* The calls above may have already set the type for this DIE. */
16883 type = get_die_type (die, cu);
16884 if (type)
16885 return type;
16886
16887 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16888 type = lookup_methodptr_type (to_type);
16889 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16890 {
16891 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16892
16893 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16894 to_type->fields (), to_type->num_fields (),
16895 TYPE_VARARGS (to_type));
16896 type = lookup_methodptr_type (new_type);
16897 }
16898 else
16899 type = lookup_memberptr_type (to_type, domain);
16900
16901 return set_die_type (die, type, cu);
16902 }
16903
16904 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16905 the user defined type vector. */
16906
16907 static struct type *
16908 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16909 enum type_code refcode)
16910 {
16911 struct comp_unit_head *cu_header = &cu->header;
16912 struct type *type, *target_type;
16913 struct attribute *attr;
16914
16915 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16916
16917 target_type = die_type (die, cu);
16918
16919 /* The die_type call above may have already set the type for this DIE. */
16920 type = get_die_type (die, cu);
16921 if (type)
16922 return type;
16923
16924 type = lookup_reference_type (target_type, refcode);
16925 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16926 if (attr != nullptr)
16927 {
16928 TYPE_LENGTH (type) = DW_UNSND (attr);
16929 }
16930 else
16931 {
16932 TYPE_LENGTH (type) = cu_header->addr_size;
16933 }
16934 maybe_set_alignment (cu, die, type);
16935 return set_die_type (die, type, cu);
16936 }
16937
16938 /* Add the given cv-qualifiers to the element type of the array. GCC
16939 outputs DWARF type qualifiers that apply to an array, not the
16940 element type. But GDB relies on the array element type to carry
16941 the cv-qualifiers. This mimics section 6.7.3 of the C99
16942 specification. */
16943
16944 static struct type *
16945 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16946 struct type *base_type, int cnst, int voltl)
16947 {
16948 struct type *el_type, *inner_array;
16949
16950 base_type = copy_type (base_type);
16951 inner_array = base_type;
16952
16953 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16954 {
16955 TYPE_TARGET_TYPE (inner_array) =
16956 copy_type (TYPE_TARGET_TYPE (inner_array));
16957 inner_array = TYPE_TARGET_TYPE (inner_array);
16958 }
16959
16960 el_type = TYPE_TARGET_TYPE (inner_array);
16961 cnst |= TYPE_CONST (el_type);
16962 voltl |= TYPE_VOLATILE (el_type);
16963 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16964
16965 return set_die_type (die, base_type, cu);
16966 }
16967
16968 static struct type *
16969 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16970 {
16971 struct type *base_type, *cv_type;
16972
16973 base_type = die_type (die, cu);
16974
16975 /* The die_type call above may have already set the type for this DIE. */
16976 cv_type = get_die_type (die, cu);
16977 if (cv_type)
16978 return cv_type;
16979
16980 /* In case the const qualifier is applied to an array type, the element type
16981 is so qualified, not the array type (section 6.7.3 of C99). */
16982 if (base_type->code () == TYPE_CODE_ARRAY)
16983 return add_array_cv_type (die, cu, base_type, 1, 0);
16984
16985 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16986 return set_die_type (die, cv_type, cu);
16987 }
16988
16989 static struct type *
16990 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16991 {
16992 struct type *base_type, *cv_type;
16993
16994 base_type = die_type (die, cu);
16995
16996 /* The die_type call above may have already set the type for this DIE. */
16997 cv_type = get_die_type (die, cu);
16998 if (cv_type)
16999 return cv_type;
17000
17001 /* In case the volatile qualifier is applied to an array type, the
17002 element type is so qualified, not the array type (section 6.7.3
17003 of C99). */
17004 if (base_type->code () == TYPE_CODE_ARRAY)
17005 return add_array_cv_type (die, cu, base_type, 0, 1);
17006
17007 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17008 return set_die_type (die, cv_type, cu);
17009 }
17010
17011 /* Handle DW_TAG_restrict_type. */
17012
17013 static struct type *
17014 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17015 {
17016 struct type *base_type, *cv_type;
17017
17018 base_type = die_type (die, cu);
17019
17020 /* The die_type call above may have already set the type for this DIE. */
17021 cv_type = get_die_type (die, cu);
17022 if (cv_type)
17023 return cv_type;
17024
17025 cv_type = make_restrict_type (base_type);
17026 return set_die_type (die, cv_type, cu);
17027 }
17028
17029 /* Handle DW_TAG_atomic_type. */
17030
17031 static struct type *
17032 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17033 {
17034 struct type *base_type, *cv_type;
17035
17036 base_type = die_type (die, cu);
17037
17038 /* The die_type call above may have already set the type for this DIE. */
17039 cv_type = get_die_type (die, cu);
17040 if (cv_type)
17041 return cv_type;
17042
17043 cv_type = make_atomic_type (base_type);
17044 return set_die_type (die, cv_type, cu);
17045 }
17046
17047 /* Extract all information from a DW_TAG_string_type DIE and add to
17048 the user defined type vector. It isn't really a user defined type,
17049 but it behaves like one, with other DIE's using an AT_user_def_type
17050 attribute to reference it. */
17051
17052 static struct type *
17053 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17054 {
17055 struct objfile *objfile = cu->per_objfile->objfile;
17056 struct gdbarch *gdbarch = objfile->arch ();
17057 struct type *type, *range_type, *index_type, *char_type;
17058 struct attribute *attr;
17059 struct dynamic_prop prop;
17060 bool length_is_constant = true;
17061 LONGEST length;
17062
17063 /* There are a couple of places where bit sizes might be made use of
17064 when parsing a DW_TAG_string_type, however, no producer that we know
17065 of make use of these. Handling bit sizes that are a multiple of the
17066 byte size is easy enough, but what about other bit sizes? Lets deal
17067 with that problem when we have to. Warn about these attributes being
17068 unsupported, then parse the type and ignore them like we always
17069 have. */
17070 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17071 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17072 {
17073 static bool warning_printed = false;
17074 if (!warning_printed)
17075 {
17076 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17077 "currently supported on DW_TAG_string_type."));
17078 warning_printed = true;
17079 }
17080 }
17081
17082 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17083 if (attr != nullptr && !attr->form_is_constant ())
17084 {
17085 /* The string length describes the location at which the length of
17086 the string can be found. The size of the length field can be
17087 specified with one of the attributes below. */
17088 struct type *prop_type;
17089 struct attribute *len
17090 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17091 if (len == nullptr)
17092 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17093 if (len != nullptr && len->form_is_constant ())
17094 {
17095 /* Pass 0 as the default as we know this attribute is constant
17096 and the default value will not be returned. */
17097 LONGEST sz = len->constant_value (0);
17098 prop_type = cu->per_objfile->int_type (sz, true);
17099 }
17100 else
17101 {
17102 /* If the size is not specified then we assume it is the size of
17103 an address on this target. */
17104 prop_type = cu->addr_sized_int_type (true);
17105 }
17106
17107 /* Convert the attribute into a dynamic property. */
17108 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17109 length = 1;
17110 else
17111 length_is_constant = false;
17112 }
17113 else if (attr != nullptr)
17114 {
17115 /* This DW_AT_string_length just contains the length with no
17116 indirection. There's no need to create a dynamic property in this
17117 case. Pass 0 for the default value as we know it will not be
17118 returned in this case. */
17119 length = attr->constant_value (0);
17120 }
17121 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17122 {
17123 /* We don't currently support non-constant byte sizes for strings. */
17124 length = attr->constant_value (1);
17125 }
17126 else
17127 {
17128 /* Use 1 as a fallback length if we have nothing else. */
17129 length = 1;
17130 }
17131
17132 index_type = objfile_type (objfile)->builtin_int;
17133 if (length_is_constant)
17134 range_type = create_static_range_type (NULL, index_type, 1, length);
17135 else
17136 {
17137 struct dynamic_prop low_bound;
17138
17139 low_bound.set_const_val (1);
17140 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17141 }
17142 char_type = language_string_char_type (cu->language_defn, gdbarch);
17143 type = create_string_type (NULL, char_type, range_type);
17144
17145 return set_die_type (die, type, cu);
17146 }
17147
17148 /* Assuming that DIE corresponds to a function, returns nonzero
17149 if the function is prototyped. */
17150
17151 static int
17152 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17153 {
17154 struct attribute *attr;
17155
17156 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17157 if (attr && (DW_UNSND (attr) != 0))
17158 return 1;
17159
17160 /* The DWARF standard implies that the DW_AT_prototyped attribute
17161 is only meaningful for C, but the concept also extends to other
17162 languages that allow unprototyped functions (Eg: Objective C).
17163 For all other languages, assume that functions are always
17164 prototyped. */
17165 if (cu->language != language_c
17166 && cu->language != language_objc
17167 && cu->language != language_opencl)
17168 return 1;
17169
17170 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17171 prototyped and unprototyped functions; default to prototyped,
17172 since that is more common in modern code (and RealView warns
17173 about unprototyped functions). */
17174 if (producer_is_realview (cu->producer))
17175 return 1;
17176
17177 return 0;
17178 }
17179
17180 /* Handle DIES due to C code like:
17181
17182 struct foo
17183 {
17184 int (*funcp)(int a, long l);
17185 int b;
17186 };
17187
17188 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17189
17190 static struct type *
17191 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17192 {
17193 struct objfile *objfile = cu->per_objfile->objfile;
17194 struct type *type; /* Type that this function returns. */
17195 struct type *ftype; /* Function that returns above type. */
17196 struct attribute *attr;
17197
17198 type = die_type (die, cu);
17199
17200 /* The die_type call above may have already set the type for this DIE. */
17201 ftype = get_die_type (die, cu);
17202 if (ftype)
17203 return ftype;
17204
17205 ftype = lookup_function_type (type);
17206
17207 if (prototyped_function_p (die, cu))
17208 TYPE_PROTOTYPED (ftype) = 1;
17209
17210 /* Store the calling convention in the type if it's available in
17211 the subroutine die. Otherwise set the calling convention to
17212 the default value DW_CC_normal. */
17213 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17214 if (attr != nullptr
17215 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17216 TYPE_CALLING_CONVENTION (ftype)
17217 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17218 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17219 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17220 else
17221 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17222
17223 /* Record whether the function returns normally to its caller or not
17224 if the DWARF producer set that information. */
17225 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17226 if (attr && (DW_UNSND (attr) != 0))
17227 TYPE_NO_RETURN (ftype) = 1;
17228
17229 /* We need to add the subroutine type to the die immediately so
17230 we don't infinitely recurse when dealing with parameters
17231 declared as the same subroutine type. */
17232 set_die_type (die, ftype, cu);
17233
17234 if (die->child != NULL)
17235 {
17236 struct type *void_type = objfile_type (objfile)->builtin_void;
17237 struct die_info *child_die;
17238 int nparams, iparams;
17239
17240 /* Count the number of parameters.
17241 FIXME: GDB currently ignores vararg functions, but knows about
17242 vararg member functions. */
17243 nparams = 0;
17244 child_die = die->child;
17245 while (child_die && child_die->tag)
17246 {
17247 if (child_die->tag == DW_TAG_formal_parameter)
17248 nparams++;
17249 else if (child_die->tag == DW_TAG_unspecified_parameters)
17250 TYPE_VARARGS (ftype) = 1;
17251 child_die = child_die->sibling;
17252 }
17253
17254 /* Allocate storage for parameters and fill them in. */
17255 ftype->set_num_fields (nparams);
17256 ftype->set_fields
17257 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17258
17259 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17260 even if we error out during the parameters reading below. */
17261 for (iparams = 0; iparams < nparams; iparams++)
17262 ftype->field (iparams).set_type (void_type);
17263
17264 iparams = 0;
17265 child_die = die->child;
17266 while (child_die && child_die->tag)
17267 {
17268 if (child_die->tag == DW_TAG_formal_parameter)
17269 {
17270 struct type *arg_type;
17271
17272 /* DWARF version 2 has no clean way to discern C++
17273 static and non-static member functions. G++ helps
17274 GDB by marking the first parameter for non-static
17275 member functions (which is the this pointer) as
17276 artificial. We pass this information to
17277 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17278
17279 DWARF version 3 added DW_AT_object_pointer, which GCC
17280 4.5 does not yet generate. */
17281 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17282 if (attr != nullptr)
17283 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17284 else
17285 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17286 arg_type = die_type (child_die, cu);
17287
17288 /* RealView does not mark THIS as const, which the testsuite
17289 expects. GCC marks THIS as const in method definitions,
17290 but not in the class specifications (GCC PR 43053). */
17291 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17292 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17293 {
17294 int is_this = 0;
17295 struct dwarf2_cu *arg_cu = cu;
17296 const char *name = dwarf2_name (child_die, cu);
17297
17298 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17299 if (attr != nullptr)
17300 {
17301 /* If the compiler emits this, use it. */
17302 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17303 is_this = 1;
17304 }
17305 else if (name && strcmp (name, "this") == 0)
17306 /* Function definitions will have the argument names. */
17307 is_this = 1;
17308 else if (name == NULL && iparams == 0)
17309 /* Declarations may not have the names, so like
17310 elsewhere in GDB, assume an artificial first
17311 argument is "this". */
17312 is_this = 1;
17313
17314 if (is_this)
17315 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17316 arg_type, 0);
17317 }
17318
17319 ftype->field (iparams).set_type (arg_type);
17320 iparams++;
17321 }
17322 child_die = child_die->sibling;
17323 }
17324 }
17325
17326 return ftype;
17327 }
17328
17329 static struct type *
17330 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17331 {
17332 struct objfile *objfile = cu->per_objfile->objfile;
17333 const char *name = NULL;
17334 struct type *this_type, *target_type;
17335
17336 name = dwarf2_full_name (NULL, die, cu);
17337 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17338 TYPE_TARGET_STUB (this_type) = 1;
17339 set_die_type (die, this_type, cu);
17340 target_type = die_type (die, cu);
17341 if (target_type != this_type)
17342 TYPE_TARGET_TYPE (this_type) = target_type;
17343 else
17344 {
17345 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17346 spec and cause infinite loops in GDB. */
17347 complaint (_("Self-referential DW_TAG_typedef "
17348 "- DIE at %s [in module %s]"),
17349 sect_offset_str (die->sect_off), objfile_name (objfile));
17350 TYPE_TARGET_TYPE (this_type) = NULL;
17351 }
17352 if (name == NULL)
17353 {
17354 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17355 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17356 Handle these by just returning the target type, rather than
17357 constructing an anonymous typedef type and trying to handle this
17358 elsewhere. */
17359 set_die_type (die, target_type, cu);
17360 return target_type;
17361 }
17362 return this_type;
17363 }
17364
17365 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17366 (which may be different from NAME) to the architecture back-end to allow
17367 it to guess the correct format if necessary. */
17368
17369 static struct type *
17370 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17371 const char *name_hint, enum bfd_endian byte_order)
17372 {
17373 struct gdbarch *gdbarch = objfile->arch ();
17374 const struct floatformat **format;
17375 struct type *type;
17376
17377 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17378 if (format)
17379 type = init_float_type (objfile, bits, name, format, byte_order);
17380 else
17381 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17382
17383 return type;
17384 }
17385
17386 /* Allocate an integer type of size BITS and name NAME. */
17387
17388 static struct type *
17389 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17390 int bits, int unsigned_p, const char *name)
17391 {
17392 struct type *type;
17393
17394 /* Versions of Intel's C Compiler generate an integer type called "void"
17395 instead of using DW_TAG_unspecified_type. This has been seen on
17396 at least versions 14, 17, and 18. */
17397 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17398 && strcmp (name, "void") == 0)
17399 type = objfile_type (objfile)->builtin_void;
17400 else
17401 type = init_integer_type (objfile, bits, unsigned_p, name);
17402
17403 return type;
17404 }
17405
17406 /* Initialise and return a floating point type of size BITS suitable for
17407 use as a component of a complex number. The NAME_HINT is passed through
17408 when initialising the floating point type and is the name of the complex
17409 type.
17410
17411 As DWARF doesn't currently provide an explicit name for the components
17412 of a complex number, but it can be helpful to have these components
17413 named, we try to select a suitable name based on the size of the
17414 component. */
17415 static struct type *
17416 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17417 struct objfile *objfile,
17418 int bits, const char *name_hint,
17419 enum bfd_endian byte_order)
17420 {
17421 gdbarch *gdbarch = objfile->arch ();
17422 struct type *tt = nullptr;
17423
17424 /* Try to find a suitable floating point builtin type of size BITS.
17425 We're going to use the name of this type as the name for the complex
17426 target type that we are about to create. */
17427 switch (cu->language)
17428 {
17429 case language_fortran:
17430 switch (bits)
17431 {
17432 case 32:
17433 tt = builtin_f_type (gdbarch)->builtin_real;
17434 break;
17435 case 64:
17436 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17437 break;
17438 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17439 case 128:
17440 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17441 break;
17442 }
17443 break;
17444 default:
17445 switch (bits)
17446 {
17447 case 32:
17448 tt = builtin_type (gdbarch)->builtin_float;
17449 break;
17450 case 64:
17451 tt = builtin_type (gdbarch)->builtin_double;
17452 break;
17453 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17454 case 128:
17455 tt = builtin_type (gdbarch)->builtin_long_double;
17456 break;
17457 }
17458 break;
17459 }
17460
17461 /* If the type we found doesn't match the size we were looking for, then
17462 pretend we didn't find a type at all, the complex target type we
17463 create will then be nameless. */
17464 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17465 tt = nullptr;
17466
17467 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17468 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17469 }
17470
17471 /* Find a representation of a given base type and install
17472 it in the TYPE field of the die. */
17473
17474 static struct type *
17475 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17476 {
17477 struct objfile *objfile = cu->per_objfile->objfile;
17478 struct type *type;
17479 struct attribute *attr;
17480 int encoding = 0, bits = 0;
17481 const char *name;
17482 gdbarch *arch;
17483
17484 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17485 if (attr != nullptr)
17486 encoding = DW_UNSND (attr);
17487 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17488 if (attr != nullptr)
17489 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17490 name = dwarf2_name (die, cu);
17491 if (!name)
17492 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17493
17494 arch = objfile->arch ();
17495 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17496
17497 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17498 if (attr)
17499 {
17500 int endianity = DW_UNSND (attr);
17501
17502 switch (endianity)
17503 {
17504 case DW_END_big:
17505 byte_order = BFD_ENDIAN_BIG;
17506 break;
17507 case DW_END_little:
17508 byte_order = BFD_ENDIAN_LITTLE;
17509 break;
17510 default:
17511 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17512 break;
17513 }
17514 }
17515
17516 switch (encoding)
17517 {
17518 case DW_ATE_address:
17519 /* Turn DW_ATE_address into a void * pointer. */
17520 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17521 type = init_pointer_type (objfile, bits, name, type);
17522 break;
17523 case DW_ATE_boolean:
17524 type = init_boolean_type (objfile, bits, 1, name);
17525 break;
17526 case DW_ATE_complex_float:
17527 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17528 byte_order);
17529 if (type->code () == TYPE_CODE_ERROR)
17530 {
17531 if (name == nullptr)
17532 {
17533 struct obstack *obstack
17534 = &cu->per_objfile->objfile->objfile_obstack;
17535 name = obconcat (obstack, "_Complex ", type->name (),
17536 nullptr);
17537 }
17538 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17539 }
17540 else
17541 type = init_complex_type (name, type);
17542 break;
17543 case DW_ATE_decimal_float:
17544 type = init_decfloat_type (objfile, bits, name);
17545 break;
17546 case DW_ATE_float:
17547 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17548 break;
17549 case DW_ATE_signed:
17550 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17551 break;
17552 case DW_ATE_unsigned:
17553 if (cu->language == language_fortran
17554 && name
17555 && startswith (name, "character("))
17556 type = init_character_type (objfile, bits, 1, name);
17557 else
17558 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17559 break;
17560 case DW_ATE_signed_char:
17561 if (cu->language == language_ada || cu->language == language_m2
17562 || cu->language == language_pascal
17563 || cu->language == language_fortran)
17564 type = init_character_type (objfile, bits, 0, name);
17565 else
17566 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17567 break;
17568 case DW_ATE_unsigned_char:
17569 if (cu->language == language_ada || cu->language == language_m2
17570 || cu->language == language_pascal
17571 || cu->language == language_fortran
17572 || cu->language == language_rust)
17573 type = init_character_type (objfile, bits, 1, name);
17574 else
17575 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17576 break;
17577 case DW_ATE_UTF:
17578 {
17579 if (bits == 16)
17580 type = builtin_type (arch)->builtin_char16;
17581 else if (bits == 32)
17582 type = builtin_type (arch)->builtin_char32;
17583 else
17584 {
17585 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17586 bits);
17587 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17588 }
17589 return set_die_type (die, type, cu);
17590 }
17591 break;
17592
17593 default:
17594 complaint (_("unsupported DW_AT_encoding: '%s'"),
17595 dwarf_type_encoding_name (encoding));
17596 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17597 break;
17598 }
17599
17600 if (name && strcmp (name, "char") == 0)
17601 TYPE_NOSIGN (type) = 1;
17602
17603 maybe_set_alignment (cu, die, type);
17604
17605 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17606
17607 return set_die_type (die, type, cu);
17608 }
17609
17610 /* Parse dwarf attribute if it's a block, reference or constant and put the
17611 resulting value of the attribute into struct bound_prop.
17612 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17613
17614 static int
17615 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17616 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17617 struct type *default_type)
17618 {
17619 struct dwarf2_property_baton *baton;
17620 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17621 struct objfile *objfile = per_objfile->objfile;
17622 struct obstack *obstack = &objfile->objfile_obstack;
17623
17624 gdb_assert (default_type != NULL);
17625
17626 if (attr == NULL || prop == NULL)
17627 return 0;
17628
17629 if (attr->form_is_block ())
17630 {
17631 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17632 baton->property_type = default_type;
17633 baton->locexpr.per_cu = cu->per_cu;
17634 baton->locexpr.per_objfile = per_objfile;
17635 baton->locexpr.size = DW_BLOCK (attr)->size;
17636 baton->locexpr.data = DW_BLOCK (attr)->data;
17637 switch (attr->name)
17638 {
17639 case DW_AT_string_length:
17640 baton->locexpr.is_reference = true;
17641 break;
17642 default:
17643 baton->locexpr.is_reference = false;
17644 break;
17645 }
17646
17647 prop->set_locexpr (baton);
17648 gdb_assert (prop->baton () != NULL);
17649 }
17650 else if (attr->form_is_ref ())
17651 {
17652 struct dwarf2_cu *target_cu = cu;
17653 struct die_info *target_die;
17654 struct attribute *target_attr;
17655
17656 target_die = follow_die_ref (die, attr, &target_cu);
17657 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17658 if (target_attr == NULL)
17659 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17660 target_cu);
17661 if (target_attr == NULL)
17662 return 0;
17663
17664 switch (target_attr->name)
17665 {
17666 case DW_AT_location:
17667 if (target_attr->form_is_section_offset ())
17668 {
17669 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17670 baton->property_type = die_type (target_die, target_cu);
17671 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17672 prop->set_loclist (baton);
17673 gdb_assert (prop->baton () != NULL);
17674 }
17675 else if (target_attr->form_is_block ())
17676 {
17677 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17678 baton->property_type = die_type (target_die, target_cu);
17679 baton->locexpr.per_cu = cu->per_cu;
17680 baton->locexpr.per_objfile = per_objfile;
17681 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17682 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17683 baton->locexpr.is_reference = true;
17684 prop->set_locexpr (baton);
17685 gdb_assert (prop->baton () != NULL);
17686 }
17687 else
17688 {
17689 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17690 "dynamic property");
17691 return 0;
17692 }
17693 break;
17694 case DW_AT_data_member_location:
17695 {
17696 LONGEST offset;
17697
17698 if (!handle_data_member_location (target_die, target_cu,
17699 &offset))
17700 return 0;
17701
17702 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17703 baton->property_type = read_type_die (target_die->parent,
17704 target_cu);
17705 baton->offset_info.offset = offset;
17706 baton->offset_info.type = die_type (target_die, target_cu);
17707 prop->set_addr_offset (baton);
17708 break;
17709 }
17710 }
17711 }
17712 else if (attr->form_is_constant ())
17713 prop->set_const_val (attr->constant_value (0));
17714 else
17715 {
17716 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17717 dwarf2_name (die, cu));
17718 return 0;
17719 }
17720
17721 return 1;
17722 }
17723
17724 /* See read.h. */
17725
17726 struct type *
17727 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17728 {
17729 struct type *int_type;
17730
17731 /* Helper macro to examine the various builtin types. */
17732 #define TRY_TYPE(F) \
17733 int_type = (unsigned_p \
17734 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17735 : objfile_type (objfile)->builtin_ ## F); \
17736 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17737 return int_type
17738
17739 TRY_TYPE (char);
17740 TRY_TYPE (short);
17741 TRY_TYPE (int);
17742 TRY_TYPE (long);
17743 TRY_TYPE (long_long);
17744
17745 #undef TRY_TYPE
17746
17747 gdb_assert_not_reached ("unable to find suitable integer type");
17748 }
17749
17750 /* See read.h. */
17751
17752 struct type *
17753 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17754 {
17755 int addr_size = this->per_cu->addr_size ();
17756 return this->per_objfile->int_type (addr_size, unsigned_p);
17757 }
17758
17759 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17760 present (which is valid) then compute the default type based on the
17761 compilation units address size. */
17762
17763 static struct type *
17764 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17765 {
17766 struct type *index_type = die_type (die, cu);
17767
17768 /* Dwarf-2 specifications explicitly allows to create subrange types
17769 without specifying a base type.
17770 In that case, the base type must be set to the type of
17771 the lower bound, upper bound or count, in that order, if any of these
17772 three attributes references an object that has a type.
17773 If no base type is found, the Dwarf-2 specifications say that
17774 a signed integer type of size equal to the size of an address should
17775 be used.
17776 For the following C code: `extern char gdb_int [];'
17777 GCC produces an empty range DIE.
17778 FIXME: muller/2010-05-28: Possible references to object for low bound,
17779 high bound or count are not yet handled by this code. */
17780 if (index_type->code () == TYPE_CODE_VOID)
17781 index_type = cu->addr_sized_int_type (false);
17782
17783 return index_type;
17784 }
17785
17786 /* Read the given DW_AT_subrange DIE. */
17787
17788 static struct type *
17789 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17790 {
17791 struct type *base_type, *orig_base_type;
17792 struct type *range_type;
17793 struct attribute *attr;
17794 struct dynamic_prop low, high;
17795 int low_default_is_valid;
17796 int high_bound_is_count = 0;
17797 const char *name;
17798 ULONGEST negative_mask;
17799
17800 orig_base_type = read_subrange_index_type (die, cu);
17801
17802 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17803 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17804 creating the range type, but we use the result of check_typedef
17805 when examining properties of the type. */
17806 base_type = check_typedef (orig_base_type);
17807
17808 /* The die_type call above may have already set the type for this DIE. */
17809 range_type = get_die_type (die, cu);
17810 if (range_type)
17811 return range_type;
17812
17813 high.set_const_val (0);
17814
17815 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17816 omitting DW_AT_lower_bound. */
17817 switch (cu->language)
17818 {
17819 case language_c:
17820 case language_cplus:
17821 low.set_const_val (0);
17822 low_default_is_valid = 1;
17823 break;
17824 case language_fortran:
17825 low.set_const_val (1);
17826 low_default_is_valid = 1;
17827 break;
17828 case language_d:
17829 case language_objc:
17830 case language_rust:
17831 low.set_const_val (0);
17832 low_default_is_valid = (cu->header.version >= 4);
17833 break;
17834 case language_ada:
17835 case language_m2:
17836 case language_pascal:
17837 low.set_const_val (1);
17838 low_default_is_valid = (cu->header.version >= 4);
17839 break;
17840 default:
17841 low.set_const_val (0);
17842 low_default_is_valid = 0;
17843 break;
17844 }
17845
17846 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17847 if (attr != nullptr)
17848 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17849 else if (!low_default_is_valid)
17850 complaint (_("Missing DW_AT_lower_bound "
17851 "- DIE at %s [in module %s]"),
17852 sect_offset_str (die->sect_off),
17853 objfile_name (cu->per_objfile->objfile));
17854
17855 struct attribute *attr_ub, *attr_count;
17856 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17857 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17858 {
17859 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17860 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17861 {
17862 /* If bounds are constant do the final calculation here. */
17863 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
17864 high.set_const_val (low.const_val () + high.const_val () - 1);
17865 else
17866 high_bound_is_count = 1;
17867 }
17868 else
17869 {
17870 if (attr_ub != NULL)
17871 complaint (_("Unresolved DW_AT_upper_bound "
17872 "- DIE at %s [in module %s]"),
17873 sect_offset_str (die->sect_off),
17874 objfile_name (cu->per_objfile->objfile));
17875 if (attr_count != NULL)
17876 complaint (_("Unresolved DW_AT_count "
17877 "- DIE at %s [in module %s]"),
17878 sect_offset_str (die->sect_off),
17879 objfile_name (cu->per_objfile->objfile));
17880 }
17881 }
17882
17883 LONGEST bias = 0;
17884 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17885 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17886 bias = bias_attr->constant_value (0);
17887
17888 /* Normally, the DWARF producers are expected to use a signed
17889 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17890 But this is unfortunately not always the case, as witnessed
17891 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17892 is used instead. To work around that ambiguity, we treat
17893 the bounds as signed, and thus sign-extend their values, when
17894 the base type is signed. */
17895 negative_mask =
17896 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17897 if (low.kind () == PROP_CONST
17898 && !TYPE_UNSIGNED (base_type) && (low.const_val () & negative_mask))
17899 low.set_const_val (low.const_val () | negative_mask);
17900 if (high.kind () == PROP_CONST
17901 && !TYPE_UNSIGNED (base_type) && (high.const_val () & negative_mask))
17902 high.set_const_val (high.const_val () | negative_mask);
17903
17904 /* Check for bit and byte strides. */
17905 struct dynamic_prop byte_stride_prop;
17906 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17907 if (attr_byte_stride != nullptr)
17908 {
17909 struct type *prop_type = cu->addr_sized_int_type (false);
17910 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17911 prop_type);
17912 }
17913
17914 struct dynamic_prop bit_stride_prop;
17915 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17916 if (attr_bit_stride != nullptr)
17917 {
17918 /* It only makes sense to have either a bit or byte stride. */
17919 if (attr_byte_stride != nullptr)
17920 {
17921 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17922 "- DIE at %s [in module %s]"),
17923 sect_offset_str (die->sect_off),
17924 objfile_name (cu->per_objfile->objfile));
17925 attr_bit_stride = nullptr;
17926 }
17927 else
17928 {
17929 struct type *prop_type = cu->addr_sized_int_type (false);
17930 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17931 prop_type);
17932 }
17933 }
17934
17935 if (attr_byte_stride != nullptr
17936 || attr_bit_stride != nullptr)
17937 {
17938 bool byte_stride_p = (attr_byte_stride != nullptr);
17939 struct dynamic_prop *stride
17940 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17941
17942 range_type
17943 = create_range_type_with_stride (NULL, orig_base_type, &low,
17944 &high, bias, stride, byte_stride_p);
17945 }
17946 else
17947 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17948
17949 if (high_bound_is_count)
17950 range_type->bounds ()->flag_upper_bound_is_count = 1;
17951
17952 /* Ada expects an empty array on no boundary attributes. */
17953 if (attr == NULL && cu->language != language_ada)
17954 range_type->bounds ()->high.set_undefined ();
17955
17956 name = dwarf2_name (die, cu);
17957 if (name)
17958 range_type->set_name (name);
17959
17960 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17961 if (attr != nullptr)
17962 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17963
17964 maybe_set_alignment (cu, die, range_type);
17965
17966 set_die_type (die, range_type, cu);
17967
17968 /* set_die_type should be already done. */
17969 set_descriptive_type (range_type, die, cu);
17970
17971 return range_type;
17972 }
17973
17974 static struct type *
17975 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17976 {
17977 struct type *type;
17978
17979 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17980 type->set_name (dwarf2_name (die, cu));
17981
17982 /* In Ada, an unspecified type is typically used when the description
17983 of the type is deferred to a different unit. When encountering
17984 such a type, we treat it as a stub, and try to resolve it later on,
17985 when needed. */
17986 if (cu->language == language_ada)
17987 TYPE_STUB (type) = 1;
17988
17989 return set_die_type (die, type, cu);
17990 }
17991
17992 /* Read a single die and all its descendents. Set the die's sibling
17993 field to NULL; set other fields in the die correctly, and set all
17994 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17995 location of the info_ptr after reading all of those dies. PARENT
17996 is the parent of the die in question. */
17997
17998 static struct die_info *
17999 read_die_and_children (const struct die_reader_specs *reader,
18000 const gdb_byte *info_ptr,
18001 const gdb_byte **new_info_ptr,
18002 struct die_info *parent)
18003 {
18004 struct die_info *die;
18005 const gdb_byte *cur_ptr;
18006
18007 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18008 if (die == NULL)
18009 {
18010 *new_info_ptr = cur_ptr;
18011 return NULL;
18012 }
18013 store_in_ref_table (die, reader->cu);
18014
18015 if (die->has_children)
18016 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18017 else
18018 {
18019 die->child = NULL;
18020 *new_info_ptr = cur_ptr;
18021 }
18022
18023 die->sibling = NULL;
18024 die->parent = parent;
18025 return die;
18026 }
18027
18028 /* Read a die, all of its descendents, and all of its siblings; set
18029 all of the fields of all of the dies correctly. Arguments are as
18030 in read_die_and_children. */
18031
18032 static struct die_info *
18033 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18034 const gdb_byte *info_ptr,
18035 const gdb_byte **new_info_ptr,
18036 struct die_info *parent)
18037 {
18038 struct die_info *first_die, *last_sibling;
18039 const gdb_byte *cur_ptr;
18040
18041 cur_ptr = info_ptr;
18042 first_die = last_sibling = NULL;
18043
18044 while (1)
18045 {
18046 struct die_info *die
18047 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18048
18049 if (die == NULL)
18050 {
18051 *new_info_ptr = cur_ptr;
18052 return first_die;
18053 }
18054
18055 if (!first_die)
18056 first_die = die;
18057 else
18058 last_sibling->sibling = die;
18059
18060 last_sibling = die;
18061 }
18062 }
18063
18064 /* Read a die, all of its descendents, and all of its siblings; set
18065 all of the fields of all of the dies correctly. Arguments are as
18066 in read_die_and_children.
18067 This the main entry point for reading a DIE and all its children. */
18068
18069 static struct die_info *
18070 read_die_and_siblings (const struct die_reader_specs *reader,
18071 const gdb_byte *info_ptr,
18072 const gdb_byte **new_info_ptr,
18073 struct die_info *parent)
18074 {
18075 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18076 new_info_ptr, parent);
18077
18078 if (dwarf_die_debug)
18079 {
18080 fprintf_unfiltered (gdb_stdlog,
18081 "Read die from %s@0x%x of %s:\n",
18082 reader->die_section->get_name (),
18083 (unsigned) (info_ptr - reader->die_section->buffer),
18084 bfd_get_filename (reader->abfd));
18085 dump_die (die, dwarf_die_debug);
18086 }
18087
18088 return die;
18089 }
18090
18091 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18092 attributes.
18093 The caller is responsible for filling in the extra attributes
18094 and updating (*DIEP)->num_attrs.
18095 Set DIEP to point to a newly allocated die with its information,
18096 except for its child, sibling, and parent fields. */
18097
18098 static const gdb_byte *
18099 read_full_die_1 (const struct die_reader_specs *reader,
18100 struct die_info **diep, const gdb_byte *info_ptr,
18101 int num_extra_attrs)
18102 {
18103 unsigned int abbrev_number, bytes_read, i;
18104 struct abbrev_info *abbrev;
18105 struct die_info *die;
18106 struct dwarf2_cu *cu = reader->cu;
18107 bfd *abfd = reader->abfd;
18108
18109 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18110 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18111 info_ptr += bytes_read;
18112 if (!abbrev_number)
18113 {
18114 *diep = NULL;
18115 return info_ptr;
18116 }
18117
18118 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18119 if (!abbrev)
18120 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18121 abbrev_number,
18122 bfd_get_filename (abfd));
18123
18124 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18125 die->sect_off = sect_off;
18126 die->tag = abbrev->tag;
18127 die->abbrev = abbrev_number;
18128 die->has_children = abbrev->has_children;
18129
18130 /* Make the result usable.
18131 The caller needs to update num_attrs after adding the extra
18132 attributes. */
18133 die->num_attrs = abbrev->num_attrs;
18134
18135 std::vector<int> indexes_that_need_reprocess;
18136 for (i = 0; i < abbrev->num_attrs; ++i)
18137 {
18138 bool need_reprocess;
18139 info_ptr =
18140 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18141 info_ptr, &need_reprocess);
18142 if (need_reprocess)
18143 indexes_that_need_reprocess.push_back (i);
18144 }
18145
18146 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18147 if (attr != nullptr)
18148 cu->str_offsets_base = DW_UNSND (attr);
18149
18150 attr = die->attr (DW_AT_loclists_base);
18151 if (attr != nullptr)
18152 cu->loclist_base = DW_UNSND (attr);
18153
18154 auto maybe_addr_base = die->addr_base ();
18155 if (maybe_addr_base.has_value ())
18156 cu->addr_base = *maybe_addr_base;
18157 for (int index : indexes_that_need_reprocess)
18158 read_attribute_reprocess (reader, &die->attrs[index]);
18159 *diep = die;
18160 return info_ptr;
18161 }
18162
18163 /* Read a die and all its attributes.
18164 Set DIEP to point to a newly allocated die with its information,
18165 except for its child, sibling, and parent fields. */
18166
18167 static const gdb_byte *
18168 read_full_die (const struct die_reader_specs *reader,
18169 struct die_info **diep, const gdb_byte *info_ptr)
18170 {
18171 const gdb_byte *result;
18172
18173 result = read_full_die_1 (reader, diep, info_ptr, 0);
18174
18175 if (dwarf_die_debug)
18176 {
18177 fprintf_unfiltered (gdb_stdlog,
18178 "Read die from %s@0x%x of %s:\n",
18179 reader->die_section->get_name (),
18180 (unsigned) (info_ptr - reader->die_section->buffer),
18181 bfd_get_filename (reader->abfd));
18182 dump_die (*diep, dwarf_die_debug);
18183 }
18184
18185 return result;
18186 }
18187 \f
18188
18189 /* Returns nonzero if TAG represents a type that we might generate a partial
18190 symbol for. */
18191
18192 static int
18193 is_type_tag_for_partial (int tag)
18194 {
18195 switch (tag)
18196 {
18197 #if 0
18198 /* Some types that would be reasonable to generate partial symbols for,
18199 that we don't at present. */
18200 case DW_TAG_array_type:
18201 case DW_TAG_file_type:
18202 case DW_TAG_ptr_to_member_type:
18203 case DW_TAG_set_type:
18204 case DW_TAG_string_type:
18205 case DW_TAG_subroutine_type:
18206 #endif
18207 case DW_TAG_base_type:
18208 case DW_TAG_class_type:
18209 case DW_TAG_interface_type:
18210 case DW_TAG_enumeration_type:
18211 case DW_TAG_structure_type:
18212 case DW_TAG_subrange_type:
18213 case DW_TAG_typedef:
18214 case DW_TAG_union_type:
18215 return 1;
18216 default:
18217 return 0;
18218 }
18219 }
18220
18221 /* Load all DIEs that are interesting for partial symbols into memory. */
18222
18223 static struct partial_die_info *
18224 load_partial_dies (const struct die_reader_specs *reader,
18225 const gdb_byte *info_ptr, int building_psymtab)
18226 {
18227 struct dwarf2_cu *cu = reader->cu;
18228 struct objfile *objfile = cu->per_objfile->objfile;
18229 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18230 unsigned int bytes_read;
18231 unsigned int load_all = 0;
18232 int nesting_level = 1;
18233
18234 parent_die = NULL;
18235 last_die = NULL;
18236
18237 gdb_assert (cu->per_cu != NULL);
18238 if (cu->per_cu->load_all_dies)
18239 load_all = 1;
18240
18241 cu->partial_dies
18242 = htab_create_alloc_ex (cu->header.length / 12,
18243 partial_die_hash,
18244 partial_die_eq,
18245 NULL,
18246 &cu->comp_unit_obstack,
18247 hashtab_obstack_allocate,
18248 dummy_obstack_deallocate);
18249
18250 while (1)
18251 {
18252 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18253
18254 /* A NULL abbrev means the end of a series of children. */
18255 if (abbrev == NULL)
18256 {
18257 if (--nesting_level == 0)
18258 return first_die;
18259
18260 info_ptr += bytes_read;
18261 last_die = parent_die;
18262 parent_die = parent_die->die_parent;
18263 continue;
18264 }
18265
18266 /* Check for template arguments. We never save these; if
18267 they're seen, we just mark the parent, and go on our way. */
18268 if (parent_die != NULL
18269 && cu->language == language_cplus
18270 && (abbrev->tag == DW_TAG_template_type_param
18271 || abbrev->tag == DW_TAG_template_value_param))
18272 {
18273 parent_die->has_template_arguments = 1;
18274
18275 if (!load_all)
18276 {
18277 /* We don't need a partial DIE for the template argument. */
18278 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18279 continue;
18280 }
18281 }
18282
18283 /* We only recurse into c++ subprograms looking for template arguments.
18284 Skip their other children. */
18285 if (!load_all
18286 && cu->language == language_cplus
18287 && parent_die != NULL
18288 && parent_die->tag == DW_TAG_subprogram
18289 && abbrev->tag != DW_TAG_inlined_subroutine)
18290 {
18291 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18292 continue;
18293 }
18294
18295 /* Check whether this DIE is interesting enough to save. Normally
18296 we would not be interested in members here, but there may be
18297 later variables referencing them via DW_AT_specification (for
18298 static members). */
18299 if (!load_all
18300 && !is_type_tag_for_partial (abbrev->tag)
18301 && abbrev->tag != DW_TAG_constant
18302 && abbrev->tag != DW_TAG_enumerator
18303 && abbrev->tag != DW_TAG_subprogram
18304 && abbrev->tag != DW_TAG_inlined_subroutine
18305 && abbrev->tag != DW_TAG_lexical_block
18306 && abbrev->tag != DW_TAG_variable
18307 && abbrev->tag != DW_TAG_namespace
18308 && abbrev->tag != DW_TAG_module
18309 && abbrev->tag != DW_TAG_member
18310 && abbrev->tag != DW_TAG_imported_unit
18311 && abbrev->tag != DW_TAG_imported_declaration)
18312 {
18313 /* Otherwise we skip to the next sibling, if any. */
18314 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18315 continue;
18316 }
18317
18318 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18319 abbrev);
18320
18321 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18322
18323 /* This two-pass algorithm for processing partial symbols has a
18324 high cost in cache pressure. Thus, handle some simple cases
18325 here which cover the majority of C partial symbols. DIEs
18326 which neither have specification tags in them, nor could have
18327 specification tags elsewhere pointing at them, can simply be
18328 processed and discarded.
18329
18330 This segment is also optional; scan_partial_symbols and
18331 add_partial_symbol will handle these DIEs if we chain
18332 them in normally. When compilers which do not emit large
18333 quantities of duplicate debug information are more common,
18334 this code can probably be removed. */
18335
18336 /* Any complete simple types at the top level (pretty much all
18337 of them, for a language without namespaces), can be processed
18338 directly. */
18339 if (parent_die == NULL
18340 && pdi.has_specification == 0
18341 && pdi.is_declaration == 0
18342 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18343 || pdi.tag == DW_TAG_base_type
18344 || pdi.tag == DW_TAG_subrange_type))
18345 {
18346 if (building_psymtab && pdi.raw_name != NULL)
18347 add_partial_symbol (&pdi, cu);
18348
18349 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18350 continue;
18351 }
18352
18353 /* The exception for DW_TAG_typedef with has_children above is
18354 a workaround of GCC PR debug/47510. In the case of this complaint
18355 type_name_or_error will error on such types later.
18356
18357 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18358 it could not find the child DIEs referenced later, this is checked
18359 above. In correct DWARF DW_TAG_typedef should have no children. */
18360
18361 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18362 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18363 "- DIE at %s [in module %s]"),
18364 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18365
18366 /* If we're at the second level, and we're an enumerator, and
18367 our parent has no specification (meaning possibly lives in a
18368 namespace elsewhere), then we can add the partial symbol now
18369 instead of queueing it. */
18370 if (pdi.tag == DW_TAG_enumerator
18371 && parent_die != NULL
18372 && parent_die->die_parent == NULL
18373 && parent_die->tag == DW_TAG_enumeration_type
18374 && parent_die->has_specification == 0)
18375 {
18376 if (pdi.raw_name == NULL)
18377 complaint (_("malformed enumerator DIE ignored"));
18378 else if (building_psymtab)
18379 add_partial_symbol (&pdi, cu);
18380
18381 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18382 continue;
18383 }
18384
18385 struct partial_die_info *part_die
18386 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18387
18388 /* We'll save this DIE so link it in. */
18389 part_die->die_parent = parent_die;
18390 part_die->die_sibling = NULL;
18391 part_die->die_child = NULL;
18392
18393 if (last_die && last_die == parent_die)
18394 last_die->die_child = part_die;
18395 else if (last_die)
18396 last_die->die_sibling = part_die;
18397
18398 last_die = part_die;
18399
18400 if (first_die == NULL)
18401 first_die = part_die;
18402
18403 /* Maybe add the DIE to the hash table. Not all DIEs that we
18404 find interesting need to be in the hash table, because we
18405 also have the parent/sibling/child chains; only those that we
18406 might refer to by offset later during partial symbol reading.
18407
18408 For now this means things that might have be the target of a
18409 DW_AT_specification, DW_AT_abstract_origin, or
18410 DW_AT_extension. DW_AT_extension will refer only to
18411 namespaces; DW_AT_abstract_origin refers to functions (and
18412 many things under the function DIE, but we do not recurse
18413 into function DIEs during partial symbol reading) and
18414 possibly variables as well; DW_AT_specification refers to
18415 declarations. Declarations ought to have the DW_AT_declaration
18416 flag. It happens that GCC forgets to put it in sometimes, but
18417 only for functions, not for types.
18418
18419 Adding more things than necessary to the hash table is harmless
18420 except for the performance cost. Adding too few will result in
18421 wasted time in find_partial_die, when we reread the compilation
18422 unit with load_all_dies set. */
18423
18424 if (load_all
18425 || abbrev->tag == DW_TAG_constant
18426 || abbrev->tag == DW_TAG_subprogram
18427 || abbrev->tag == DW_TAG_variable
18428 || abbrev->tag == DW_TAG_namespace
18429 || part_die->is_declaration)
18430 {
18431 void **slot;
18432
18433 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18434 to_underlying (part_die->sect_off),
18435 INSERT);
18436 *slot = part_die;
18437 }
18438
18439 /* For some DIEs we want to follow their children (if any). For C
18440 we have no reason to follow the children of structures; for other
18441 languages we have to, so that we can get at method physnames
18442 to infer fully qualified class names, for DW_AT_specification,
18443 and for C++ template arguments. For C++, we also look one level
18444 inside functions to find template arguments (if the name of the
18445 function does not already contain the template arguments).
18446
18447 For Ada and Fortran, we need to scan the children of subprograms
18448 and lexical blocks as well because these languages allow the
18449 definition of nested entities that could be interesting for the
18450 debugger, such as nested subprograms for instance. */
18451 if (last_die->has_children
18452 && (load_all
18453 || last_die->tag == DW_TAG_namespace
18454 || last_die->tag == DW_TAG_module
18455 || last_die->tag == DW_TAG_enumeration_type
18456 || (cu->language == language_cplus
18457 && last_die->tag == DW_TAG_subprogram
18458 && (last_die->raw_name == NULL
18459 || strchr (last_die->raw_name, '<') == NULL))
18460 || (cu->language != language_c
18461 && (last_die->tag == DW_TAG_class_type
18462 || last_die->tag == DW_TAG_interface_type
18463 || last_die->tag == DW_TAG_structure_type
18464 || last_die->tag == DW_TAG_union_type))
18465 || ((cu->language == language_ada
18466 || cu->language == language_fortran)
18467 && (last_die->tag == DW_TAG_subprogram
18468 || last_die->tag == DW_TAG_lexical_block))))
18469 {
18470 nesting_level++;
18471 parent_die = last_die;
18472 continue;
18473 }
18474
18475 /* Otherwise we skip to the next sibling, if any. */
18476 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18477
18478 /* Back to the top, do it again. */
18479 }
18480 }
18481
18482 partial_die_info::partial_die_info (sect_offset sect_off_,
18483 struct abbrev_info *abbrev)
18484 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18485 {
18486 }
18487
18488 /* See class definition. */
18489
18490 const char *
18491 partial_die_info::name (dwarf2_cu *cu)
18492 {
18493 if (!canonical_name && raw_name != nullptr)
18494 {
18495 struct objfile *objfile = cu->per_objfile->objfile;
18496 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18497 canonical_name = 1;
18498 }
18499
18500 return raw_name;
18501 }
18502
18503 /* Read a minimal amount of information into the minimal die structure.
18504 INFO_PTR should point just after the initial uleb128 of a DIE. */
18505
18506 const gdb_byte *
18507 partial_die_info::read (const struct die_reader_specs *reader,
18508 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18509 {
18510 struct dwarf2_cu *cu = reader->cu;
18511 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18512 unsigned int i;
18513 int has_low_pc_attr = 0;
18514 int has_high_pc_attr = 0;
18515 int high_pc_relative = 0;
18516
18517 for (i = 0; i < abbrev.num_attrs; ++i)
18518 {
18519 attribute attr;
18520 bool need_reprocess;
18521 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18522 info_ptr, &need_reprocess);
18523 /* String and address offsets that need to do the reprocessing have
18524 already been read at this point, so there is no need to wait until
18525 the loop terminates to do the reprocessing. */
18526 if (need_reprocess)
18527 read_attribute_reprocess (reader, &attr);
18528 /* Store the data if it is of an attribute we want to keep in a
18529 partial symbol table. */
18530 switch (attr.name)
18531 {
18532 case DW_AT_name:
18533 switch (tag)
18534 {
18535 case DW_TAG_compile_unit:
18536 case DW_TAG_partial_unit:
18537 case DW_TAG_type_unit:
18538 /* Compilation units have a DW_AT_name that is a filename, not
18539 a source language identifier. */
18540 case DW_TAG_enumeration_type:
18541 case DW_TAG_enumerator:
18542 /* These tags always have simple identifiers already; no need
18543 to canonicalize them. */
18544 canonical_name = 1;
18545 raw_name = DW_STRING (&attr);
18546 break;
18547 default:
18548 canonical_name = 0;
18549 raw_name = DW_STRING (&attr);
18550 break;
18551 }
18552 break;
18553 case DW_AT_linkage_name:
18554 case DW_AT_MIPS_linkage_name:
18555 /* Note that both forms of linkage name might appear. We
18556 assume they will be the same, and we only store the last
18557 one we see. */
18558 linkage_name = attr.value_as_string ();
18559 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18560 See https://github.com/rust-lang/rust/issues/32925. */
18561 if (cu->language == language_rust && linkage_name != NULL
18562 && strchr (linkage_name, '{') != NULL)
18563 linkage_name = NULL;
18564 break;
18565 case DW_AT_low_pc:
18566 has_low_pc_attr = 1;
18567 lowpc = attr.value_as_address ();
18568 break;
18569 case DW_AT_high_pc:
18570 has_high_pc_attr = 1;
18571 highpc = attr.value_as_address ();
18572 if (cu->header.version >= 4 && attr.form_is_constant ())
18573 high_pc_relative = 1;
18574 break;
18575 case DW_AT_location:
18576 /* Support the .debug_loc offsets. */
18577 if (attr.form_is_block ())
18578 {
18579 d.locdesc = DW_BLOCK (&attr);
18580 }
18581 else if (attr.form_is_section_offset ())
18582 {
18583 dwarf2_complex_location_expr_complaint ();
18584 }
18585 else
18586 {
18587 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18588 "partial symbol information");
18589 }
18590 break;
18591 case DW_AT_external:
18592 is_external = DW_UNSND (&attr);
18593 break;
18594 case DW_AT_declaration:
18595 is_declaration = DW_UNSND (&attr);
18596 break;
18597 case DW_AT_type:
18598 has_type = 1;
18599 break;
18600 case DW_AT_abstract_origin:
18601 case DW_AT_specification:
18602 case DW_AT_extension:
18603 has_specification = 1;
18604 spec_offset = attr.get_ref_die_offset ();
18605 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18606 || cu->per_cu->is_dwz);
18607 break;
18608 case DW_AT_sibling:
18609 /* Ignore absolute siblings, they might point outside of
18610 the current compile unit. */
18611 if (attr.form == DW_FORM_ref_addr)
18612 complaint (_("ignoring absolute DW_AT_sibling"));
18613 else
18614 {
18615 const gdb_byte *buffer = reader->buffer;
18616 sect_offset off = attr.get_ref_die_offset ();
18617 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18618
18619 if (sibling_ptr < info_ptr)
18620 complaint (_("DW_AT_sibling points backwards"));
18621 else if (sibling_ptr > reader->buffer_end)
18622 reader->die_section->overflow_complaint ();
18623 else
18624 sibling = sibling_ptr;
18625 }
18626 break;
18627 case DW_AT_byte_size:
18628 has_byte_size = 1;
18629 break;
18630 case DW_AT_const_value:
18631 has_const_value = 1;
18632 break;
18633 case DW_AT_calling_convention:
18634 /* DWARF doesn't provide a way to identify a program's source-level
18635 entry point. DW_AT_calling_convention attributes are only meant
18636 to describe functions' calling conventions.
18637
18638 However, because it's a necessary piece of information in
18639 Fortran, and before DWARF 4 DW_CC_program was the only
18640 piece of debugging information whose definition refers to
18641 a 'main program' at all, several compilers marked Fortran
18642 main programs with DW_CC_program --- even when those
18643 functions use the standard calling conventions.
18644
18645 Although DWARF now specifies a way to provide this
18646 information, we support this practice for backward
18647 compatibility. */
18648 if (DW_UNSND (&attr) == DW_CC_program
18649 && cu->language == language_fortran)
18650 main_subprogram = 1;
18651 break;
18652 case DW_AT_inline:
18653 if (DW_UNSND (&attr) == DW_INL_inlined
18654 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18655 may_be_inlined = 1;
18656 break;
18657
18658 case DW_AT_import:
18659 if (tag == DW_TAG_imported_unit)
18660 {
18661 d.sect_off = attr.get_ref_die_offset ();
18662 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18663 || cu->per_cu->is_dwz);
18664 }
18665 break;
18666
18667 case DW_AT_main_subprogram:
18668 main_subprogram = DW_UNSND (&attr);
18669 break;
18670
18671 case DW_AT_ranges:
18672 {
18673 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18674 but that requires a full DIE, so instead we just
18675 reimplement it. */
18676 int need_ranges_base = tag != DW_TAG_compile_unit;
18677 unsigned int ranges_offset = (DW_UNSND (&attr)
18678 + (need_ranges_base
18679 ? cu->ranges_base
18680 : 0));
18681
18682 /* Value of the DW_AT_ranges attribute is the offset in the
18683 .debug_ranges section. */
18684 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18685 nullptr))
18686 has_pc_info = 1;
18687 }
18688 break;
18689
18690 default:
18691 break;
18692 }
18693 }
18694
18695 /* For Ada, if both the name and the linkage name appear, we prefer
18696 the latter. This lets "catch exception" work better, regardless
18697 of the order in which the name and linkage name were emitted.
18698 Really, though, this is just a workaround for the fact that gdb
18699 doesn't store both the name and the linkage name. */
18700 if (cu->language == language_ada && linkage_name != nullptr)
18701 raw_name = linkage_name;
18702
18703 if (high_pc_relative)
18704 highpc += lowpc;
18705
18706 if (has_low_pc_attr && has_high_pc_attr)
18707 {
18708 /* When using the GNU linker, .gnu.linkonce. sections are used to
18709 eliminate duplicate copies of functions and vtables and such.
18710 The linker will arbitrarily choose one and discard the others.
18711 The AT_*_pc values for such functions refer to local labels in
18712 these sections. If the section from that file was discarded, the
18713 labels are not in the output, so the relocs get a value of 0.
18714 If this is a discarded function, mark the pc bounds as invalid,
18715 so that GDB will ignore it. */
18716 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
18717 {
18718 struct objfile *objfile = per_objfile->objfile;
18719 struct gdbarch *gdbarch = objfile->arch ();
18720
18721 complaint (_("DW_AT_low_pc %s is zero "
18722 "for DIE at %s [in module %s]"),
18723 paddress (gdbarch, lowpc),
18724 sect_offset_str (sect_off),
18725 objfile_name (objfile));
18726 }
18727 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18728 else if (lowpc >= highpc)
18729 {
18730 struct objfile *objfile = per_objfile->objfile;
18731 struct gdbarch *gdbarch = objfile->arch ();
18732
18733 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18734 "for DIE at %s [in module %s]"),
18735 paddress (gdbarch, lowpc),
18736 paddress (gdbarch, highpc),
18737 sect_offset_str (sect_off),
18738 objfile_name (objfile));
18739 }
18740 else
18741 has_pc_info = 1;
18742 }
18743
18744 return info_ptr;
18745 }
18746
18747 /* Find a cached partial DIE at OFFSET in CU. */
18748
18749 struct partial_die_info *
18750 dwarf2_cu::find_partial_die (sect_offset sect_off)
18751 {
18752 struct partial_die_info *lookup_die = NULL;
18753 struct partial_die_info part_die (sect_off);
18754
18755 lookup_die = ((struct partial_die_info *)
18756 htab_find_with_hash (partial_dies, &part_die,
18757 to_underlying (sect_off)));
18758
18759 return lookup_die;
18760 }
18761
18762 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18763 except in the case of .debug_types DIEs which do not reference
18764 outside their CU (they do however referencing other types via
18765 DW_FORM_ref_sig8). */
18766
18767 static const struct cu_partial_die_info
18768 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18769 {
18770 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18771 struct objfile *objfile = per_objfile->objfile;
18772 struct partial_die_info *pd = NULL;
18773
18774 if (offset_in_dwz == cu->per_cu->is_dwz
18775 && cu->header.offset_in_cu_p (sect_off))
18776 {
18777 pd = cu->find_partial_die (sect_off);
18778 if (pd != NULL)
18779 return { cu, pd };
18780 /* We missed recording what we needed.
18781 Load all dies and try again. */
18782 }
18783 else
18784 {
18785 /* TUs don't reference other CUs/TUs (except via type signatures). */
18786 if (cu->per_cu->is_debug_types)
18787 {
18788 error (_("Dwarf Error: Type Unit at offset %s contains"
18789 " external reference to offset %s [in module %s].\n"),
18790 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18791 bfd_get_filename (objfile->obfd));
18792 }
18793 dwarf2_per_cu_data *per_cu
18794 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18795 per_objfile);
18796
18797 cu = per_objfile->get_cu (per_cu);
18798 if (cu == NULL || cu->partial_dies == NULL)
18799 load_partial_comp_unit (per_cu, per_objfile, nullptr);
18800
18801 cu = per_objfile->get_cu (per_cu);
18802
18803 cu->last_used = 0;
18804 pd = cu->find_partial_die (sect_off);
18805 }
18806
18807 /* If we didn't find it, and not all dies have been loaded,
18808 load them all and try again. */
18809
18810 if (pd == NULL && cu->per_cu->load_all_dies == 0)
18811 {
18812 cu->per_cu->load_all_dies = 1;
18813
18814 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18815 THIS_CU->cu may already be in use. So we can't just free it and
18816 replace its DIEs with the ones we read in. Instead, we leave those
18817 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18818 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18819 set. */
18820 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
18821
18822 pd = cu->find_partial_die (sect_off);
18823 }
18824
18825 if (pd == NULL)
18826 internal_error (__FILE__, __LINE__,
18827 _("could not find partial DIE %s "
18828 "in cache [from module %s]\n"),
18829 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18830 return { cu, pd };
18831 }
18832
18833 /* See if we can figure out if the class lives in a namespace. We do
18834 this by looking for a member function; its demangled name will
18835 contain namespace info, if there is any. */
18836
18837 static void
18838 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18839 struct dwarf2_cu *cu)
18840 {
18841 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18842 what template types look like, because the demangler
18843 frequently doesn't give the same name as the debug info. We
18844 could fix this by only using the demangled name to get the
18845 prefix (but see comment in read_structure_type). */
18846
18847 struct partial_die_info *real_pdi;
18848 struct partial_die_info *child_pdi;
18849
18850 /* If this DIE (this DIE's specification, if any) has a parent, then
18851 we should not do this. We'll prepend the parent's fully qualified
18852 name when we create the partial symbol. */
18853
18854 real_pdi = struct_pdi;
18855 while (real_pdi->has_specification)
18856 {
18857 auto res = find_partial_die (real_pdi->spec_offset,
18858 real_pdi->spec_is_dwz, cu);
18859 real_pdi = res.pdi;
18860 cu = res.cu;
18861 }
18862
18863 if (real_pdi->die_parent != NULL)
18864 return;
18865
18866 for (child_pdi = struct_pdi->die_child;
18867 child_pdi != NULL;
18868 child_pdi = child_pdi->die_sibling)
18869 {
18870 if (child_pdi->tag == DW_TAG_subprogram
18871 && child_pdi->linkage_name != NULL)
18872 {
18873 gdb::unique_xmalloc_ptr<char> actual_class_name
18874 (cu->language_defn->class_name_from_physname
18875 (child_pdi->linkage_name));
18876 if (actual_class_name != NULL)
18877 {
18878 struct objfile *objfile = cu->per_objfile->objfile;
18879 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
18880 struct_pdi->canonical_name = 1;
18881 }
18882 break;
18883 }
18884 }
18885 }
18886
18887 /* Return true if a DIE with TAG may have the DW_AT_const_value
18888 attribute. */
18889
18890 static bool
18891 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18892 {
18893 switch (tag)
18894 {
18895 case DW_TAG_constant:
18896 case DW_TAG_enumerator:
18897 case DW_TAG_formal_parameter:
18898 case DW_TAG_template_value_param:
18899 case DW_TAG_variable:
18900 return true;
18901 }
18902
18903 return false;
18904 }
18905
18906 void
18907 partial_die_info::fixup (struct dwarf2_cu *cu)
18908 {
18909 /* Once we've fixed up a die, there's no point in doing so again.
18910 This also avoids a memory leak if we were to call
18911 guess_partial_die_structure_name multiple times. */
18912 if (fixup_called)
18913 return;
18914
18915 /* If we found a reference attribute and the DIE has no name, try
18916 to find a name in the referred to DIE. */
18917
18918 if (raw_name == NULL && has_specification)
18919 {
18920 struct partial_die_info *spec_die;
18921
18922 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18923 spec_die = res.pdi;
18924 cu = res.cu;
18925
18926 spec_die->fixup (cu);
18927
18928 if (spec_die->raw_name)
18929 {
18930 raw_name = spec_die->raw_name;
18931 canonical_name = spec_die->canonical_name;
18932
18933 /* Copy DW_AT_external attribute if it is set. */
18934 if (spec_die->is_external)
18935 is_external = spec_die->is_external;
18936 }
18937 }
18938
18939 if (!has_const_value && has_specification
18940 && can_have_DW_AT_const_value_p (tag))
18941 {
18942 struct partial_die_info *spec_die;
18943
18944 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18945 spec_die = res.pdi;
18946 cu = res.cu;
18947
18948 spec_die->fixup (cu);
18949
18950 if (spec_die->has_const_value)
18951 {
18952 /* Copy DW_AT_const_value attribute if it is set. */
18953 has_const_value = spec_die->has_const_value;
18954 }
18955 }
18956
18957 /* Set default names for some unnamed DIEs. */
18958
18959 if (raw_name == NULL && tag == DW_TAG_namespace)
18960 {
18961 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
18962 canonical_name = 1;
18963 }
18964
18965 /* If there is no parent die to provide a namespace, and there are
18966 children, see if we can determine the namespace from their linkage
18967 name. */
18968 if (cu->language == language_cplus
18969 && !cu->per_objfile->per_bfd->types.empty ()
18970 && die_parent == NULL
18971 && has_children
18972 && (tag == DW_TAG_class_type
18973 || tag == DW_TAG_structure_type
18974 || tag == DW_TAG_union_type))
18975 guess_partial_die_structure_name (this, cu);
18976
18977 /* GCC might emit a nameless struct or union that has a linkage
18978 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18979 if (raw_name == NULL
18980 && (tag == DW_TAG_class_type
18981 || tag == DW_TAG_interface_type
18982 || tag == DW_TAG_structure_type
18983 || tag == DW_TAG_union_type)
18984 && linkage_name != NULL)
18985 {
18986 gdb::unique_xmalloc_ptr<char> demangled
18987 (gdb_demangle (linkage_name, DMGL_TYPES));
18988 if (demangled != nullptr)
18989 {
18990 const char *base;
18991
18992 /* Strip any leading namespaces/classes, keep only the base name.
18993 DW_AT_name for named DIEs does not contain the prefixes. */
18994 base = strrchr (demangled.get (), ':');
18995 if (base && base > demangled.get () && base[-1] == ':')
18996 base++;
18997 else
18998 base = demangled.get ();
18999
19000 struct objfile *objfile = cu->per_objfile->objfile;
19001 raw_name = objfile->intern (base);
19002 canonical_name = 1;
19003 }
19004 }
19005
19006 fixup_called = 1;
19007 }
19008
19009 /* Read the .debug_loclists header contents from the given SECTION in the
19010 HEADER. */
19011 static void
19012 read_loclist_header (struct loclist_header *header,
19013 struct dwarf2_section_info *section)
19014 {
19015 unsigned int bytes_read;
19016 bfd *abfd = section->get_bfd_owner ();
19017 const gdb_byte *info_ptr = section->buffer;
19018 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19019 info_ptr += bytes_read;
19020 header->version = read_2_bytes (abfd, info_ptr);
19021 info_ptr += 2;
19022 header->addr_size = read_1_byte (abfd, info_ptr);
19023 info_ptr += 1;
19024 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19025 info_ptr += 1;
19026 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19027 }
19028
19029 /* Return the DW_AT_loclists_base value for the CU. */
19030 static ULONGEST
19031 lookup_loclist_base (struct dwarf2_cu *cu)
19032 {
19033 /* For the .dwo unit, the loclist_base points to the first offset following
19034 the header. The header consists of the following entities-
19035 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19036 bit format)
19037 2. version (2 bytes)
19038 3. address size (1 byte)
19039 4. segment selector size (1 byte)
19040 5. offset entry count (4 bytes)
19041 These sizes are derived as per the DWARFv5 standard. */
19042 if (cu->dwo_unit != nullptr)
19043 {
19044 if (cu->header.initial_length_size == 4)
19045 return LOCLIST_HEADER_SIZE32;
19046 return LOCLIST_HEADER_SIZE64;
19047 }
19048 return cu->loclist_base;
19049 }
19050
19051 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19052 array of offsets in the .debug_loclists section. */
19053 static CORE_ADDR
19054 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19055 {
19056 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19057 struct objfile *objfile = per_objfile->objfile;
19058 bfd *abfd = objfile->obfd;
19059 ULONGEST loclist_base = lookup_loclist_base (cu);
19060 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19061
19062 section->read (objfile);
19063 if (section->buffer == NULL)
19064 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19065 "section [in module %s]"), objfile_name (objfile));
19066 struct loclist_header header;
19067 read_loclist_header (&header, section);
19068 if (loclist_index >= header.offset_entry_count)
19069 complaint (_("DW_FORM_loclistx pointing outside of "
19070 ".debug_loclists offset array [in module %s]"),
19071 objfile_name (objfile));
19072 if (loclist_base + loclist_index * cu->header.offset_size
19073 >= section->size)
19074 complaint (_("DW_FORM_loclistx pointing outside of "
19075 ".debug_loclists section [in module %s]"),
19076 objfile_name (objfile));
19077 const gdb_byte *info_ptr
19078 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19079
19080 if (cu->header.offset_size == 4)
19081 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19082 else
19083 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19084 }
19085
19086 /* Process the attributes that had to be skipped in the first round. These
19087 attributes are the ones that need str_offsets_base or addr_base attributes.
19088 They could not have been processed in the first round, because at the time
19089 the values of str_offsets_base or addr_base may not have been known. */
19090 static void
19091 read_attribute_reprocess (const struct die_reader_specs *reader,
19092 struct attribute *attr)
19093 {
19094 struct dwarf2_cu *cu = reader->cu;
19095 switch (attr->form)
19096 {
19097 case DW_FORM_addrx:
19098 case DW_FORM_GNU_addr_index:
19099 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19100 break;
19101 case DW_FORM_loclistx:
19102 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19103 break;
19104 case DW_FORM_strx:
19105 case DW_FORM_strx1:
19106 case DW_FORM_strx2:
19107 case DW_FORM_strx3:
19108 case DW_FORM_strx4:
19109 case DW_FORM_GNU_str_index:
19110 {
19111 unsigned int str_index = DW_UNSND (attr);
19112 if (reader->dwo_file != NULL)
19113 {
19114 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19115 DW_STRING_IS_CANONICAL (attr) = 0;
19116 }
19117 else
19118 {
19119 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19120 DW_STRING_IS_CANONICAL (attr) = 0;
19121 }
19122 break;
19123 }
19124 default:
19125 gdb_assert_not_reached (_("Unexpected DWARF form."));
19126 }
19127 }
19128
19129 /* Read an attribute value described by an attribute form. */
19130
19131 static const gdb_byte *
19132 read_attribute_value (const struct die_reader_specs *reader,
19133 struct attribute *attr, unsigned form,
19134 LONGEST implicit_const, const gdb_byte *info_ptr,
19135 bool *need_reprocess)
19136 {
19137 struct dwarf2_cu *cu = reader->cu;
19138 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19139 struct objfile *objfile = per_objfile->objfile;
19140 bfd *abfd = reader->abfd;
19141 struct comp_unit_head *cu_header = &cu->header;
19142 unsigned int bytes_read;
19143 struct dwarf_block *blk;
19144 *need_reprocess = false;
19145
19146 attr->form = (enum dwarf_form) form;
19147 switch (form)
19148 {
19149 case DW_FORM_ref_addr:
19150 if (cu->header.version == 2)
19151 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19152 &bytes_read);
19153 else
19154 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19155 &bytes_read);
19156 info_ptr += bytes_read;
19157 break;
19158 case DW_FORM_GNU_ref_alt:
19159 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19160 info_ptr += bytes_read;
19161 break;
19162 case DW_FORM_addr:
19163 {
19164 struct gdbarch *gdbarch = objfile->arch ();
19165 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19166 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19167 info_ptr += bytes_read;
19168 }
19169 break;
19170 case DW_FORM_block2:
19171 blk = dwarf_alloc_block (cu);
19172 blk->size = read_2_bytes (abfd, info_ptr);
19173 info_ptr += 2;
19174 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19175 info_ptr += blk->size;
19176 DW_BLOCK (attr) = blk;
19177 break;
19178 case DW_FORM_block4:
19179 blk = dwarf_alloc_block (cu);
19180 blk->size = read_4_bytes (abfd, info_ptr);
19181 info_ptr += 4;
19182 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19183 info_ptr += blk->size;
19184 DW_BLOCK (attr) = blk;
19185 break;
19186 case DW_FORM_data2:
19187 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19188 info_ptr += 2;
19189 break;
19190 case DW_FORM_data4:
19191 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19192 info_ptr += 4;
19193 break;
19194 case DW_FORM_data8:
19195 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19196 info_ptr += 8;
19197 break;
19198 case DW_FORM_data16:
19199 blk = dwarf_alloc_block (cu);
19200 blk->size = 16;
19201 blk->data = read_n_bytes (abfd, info_ptr, 16);
19202 info_ptr += 16;
19203 DW_BLOCK (attr) = blk;
19204 break;
19205 case DW_FORM_sec_offset:
19206 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19207 info_ptr += bytes_read;
19208 break;
19209 case DW_FORM_loclistx:
19210 {
19211 *need_reprocess = true;
19212 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19213 info_ptr += bytes_read;
19214 }
19215 break;
19216 case DW_FORM_string:
19217 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19218 DW_STRING_IS_CANONICAL (attr) = 0;
19219 info_ptr += bytes_read;
19220 break;
19221 case DW_FORM_strp:
19222 if (!cu->per_cu->is_dwz)
19223 {
19224 DW_STRING (attr) = read_indirect_string (per_objfile,
19225 abfd, info_ptr, cu_header,
19226 &bytes_read);
19227 DW_STRING_IS_CANONICAL (attr) = 0;
19228 info_ptr += bytes_read;
19229 break;
19230 }
19231 /* FALLTHROUGH */
19232 case DW_FORM_line_strp:
19233 if (!cu->per_cu->is_dwz)
19234 {
19235 DW_STRING (attr) = per_objfile->read_line_string (info_ptr, cu_header,
19236 &bytes_read);
19237 DW_STRING_IS_CANONICAL (attr) = 0;
19238 info_ptr += bytes_read;
19239 break;
19240 }
19241 /* FALLTHROUGH */
19242 case DW_FORM_GNU_strp_alt:
19243 {
19244 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
19245 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19246 &bytes_read);
19247
19248 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19249 DW_STRING_IS_CANONICAL (attr) = 0;
19250 info_ptr += bytes_read;
19251 }
19252 break;
19253 case DW_FORM_exprloc:
19254 case DW_FORM_block:
19255 blk = dwarf_alloc_block (cu);
19256 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19257 info_ptr += bytes_read;
19258 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19259 info_ptr += blk->size;
19260 DW_BLOCK (attr) = blk;
19261 break;
19262 case DW_FORM_block1:
19263 blk = dwarf_alloc_block (cu);
19264 blk->size = read_1_byte (abfd, info_ptr);
19265 info_ptr += 1;
19266 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19267 info_ptr += blk->size;
19268 DW_BLOCK (attr) = blk;
19269 break;
19270 case DW_FORM_data1:
19271 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19272 info_ptr += 1;
19273 break;
19274 case DW_FORM_flag:
19275 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19276 info_ptr += 1;
19277 break;
19278 case DW_FORM_flag_present:
19279 DW_UNSND (attr) = 1;
19280 break;
19281 case DW_FORM_sdata:
19282 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19283 info_ptr += bytes_read;
19284 break;
19285 case DW_FORM_udata:
19286 case DW_FORM_rnglistx:
19287 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19288 info_ptr += bytes_read;
19289 break;
19290 case DW_FORM_ref1:
19291 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19292 + read_1_byte (abfd, info_ptr));
19293 info_ptr += 1;
19294 break;
19295 case DW_FORM_ref2:
19296 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19297 + read_2_bytes (abfd, info_ptr));
19298 info_ptr += 2;
19299 break;
19300 case DW_FORM_ref4:
19301 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19302 + read_4_bytes (abfd, info_ptr));
19303 info_ptr += 4;
19304 break;
19305 case DW_FORM_ref8:
19306 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19307 + read_8_bytes (abfd, info_ptr));
19308 info_ptr += 8;
19309 break;
19310 case DW_FORM_ref_sig8:
19311 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19312 info_ptr += 8;
19313 break;
19314 case DW_FORM_ref_udata:
19315 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19316 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19317 info_ptr += bytes_read;
19318 break;
19319 case DW_FORM_indirect:
19320 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19321 info_ptr += bytes_read;
19322 if (form == DW_FORM_implicit_const)
19323 {
19324 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19325 info_ptr += bytes_read;
19326 }
19327 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19328 info_ptr, need_reprocess);
19329 break;
19330 case DW_FORM_implicit_const:
19331 DW_SND (attr) = implicit_const;
19332 break;
19333 case DW_FORM_addrx:
19334 case DW_FORM_GNU_addr_index:
19335 *need_reprocess = true;
19336 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19337 info_ptr += bytes_read;
19338 break;
19339 case DW_FORM_strx:
19340 case DW_FORM_strx1:
19341 case DW_FORM_strx2:
19342 case DW_FORM_strx3:
19343 case DW_FORM_strx4:
19344 case DW_FORM_GNU_str_index:
19345 {
19346 ULONGEST str_index;
19347 if (form == DW_FORM_strx1)
19348 {
19349 str_index = read_1_byte (abfd, info_ptr);
19350 info_ptr += 1;
19351 }
19352 else if (form == DW_FORM_strx2)
19353 {
19354 str_index = read_2_bytes (abfd, info_ptr);
19355 info_ptr += 2;
19356 }
19357 else if (form == DW_FORM_strx3)
19358 {
19359 str_index = read_3_bytes (abfd, info_ptr);
19360 info_ptr += 3;
19361 }
19362 else if (form == DW_FORM_strx4)
19363 {
19364 str_index = read_4_bytes (abfd, info_ptr);
19365 info_ptr += 4;
19366 }
19367 else
19368 {
19369 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19370 info_ptr += bytes_read;
19371 }
19372 *need_reprocess = true;
19373 DW_UNSND (attr) = str_index;
19374 }
19375 break;
19376 default:
19377 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19378 dwarf_form_name (form),
19379 bfd_get_filename (abfd));
19380 }
19381
19382 /* Super hack. */
19383 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19384 attr->form = DW_FORM_GNU_ref_alt;
19385
19386 /* We have seen instances where the compiler tried to emit a byte
19387 size attribute of -1 which ended up being encoded as an unsigned
19388 0xffffffff. Although 0xffffffff is technically a valid size value,
19389 an object of this size seems pretty unlikely so we can relatively
19390 safely treat these cases as if the size attribute was invalid and
19391 treat them as zero by default. */
19392 if (attr->name == DW_AT_byte_size
19393 && form == DW_FORM_data4
19394 && DW_UNSND (attr) >= 0xffffffff)
19395 {
19396 complaint
19397 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19398 hex_string (DW_UNSND (attr)));
19399 DW_UNSND (attr) = 0;
19400 }
19401
19402 return info_ptr;
19403 }
19404
19405 /* Read an attribute described by an abbreviated attribute. */
19406
19407 static const gdb_byte *
19408 read_attribute (const struct die_reader_specs *reader,
19409 struct attribute *attr, struct attr_abbrev *abbrev,
19410 const gdb_byte *info_ptr, bool *need_reprocess)
19411 {
19412 attr->name = abbrev->name;
19413 return read_attribute_value (reader, attr, abbrev->form,
19414 abbrev->implicit_const, info_ptr,
19415 need_reprocess);
19416 }
19417
19418 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19419
19420 static const char *
19421 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19422 LONGEST str_offset)
19423 {
19424 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19425 str_offset, "DW_FORM_strp");
19426 }
19427
19428 /* Return pointer to string at .debug_str offset as read from BUF.
19429 BUF is assumed to be in a compilation unit described by CU_HEADER.
19430 Return *BYTES_READ_PTR count of bytes read from BUF. */
19431
19432 static const char *
19433 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19434 const gdb_byte *buf,
19435 const struct comp_unit_head *cu_header,
19436 unsigned int *bytes_read_ptr)
19437 {
19438 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19439
19440 return read_indirect_string_at_offset (per_objfile, str_offset);
19441 }
19442
19443 /* See read.h. */
19444
19445 const char *
19446 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19447 const struct comp_unit_head *cu_header,
19448 unsigned int *bytes_read_ptr)
19449 {
19450 bfd *abfd = objfile->obfd;
19451 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19452
19453 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19454 }
19455
19456 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19457 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19458 ADDR_SIZE is the size of addresses from the CU header. */
19459
19460 static CORE_ADDR
19461 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19462 gdb::optional<ULONGEST> addr_base, int addr_size)
19463 {
19464 struct objfile *objfile = per_objfile->objfile;
19465 bfd *abfd = objfile->obfd;
19466 const gdb_byte *info_ptr;
19467 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19468
19469 per_objfile->per_bfd->addr.read (objfile);
19470 if (per_objfile->per_bfd->addr.buffer == NULL)
19471 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19472 objfile_name (objfile));
19473 if (addr_base_or_zero + addr_index * addr_size
19474 >= per_objfile->per_bfd->addr.size)
19475 error (_("DW_FORM_addr_index pointing outside of "
19476 ".debug_addr section [in module %s]"),
19477 objfile_name (objfile));
19478 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19479 + addr_index * addr_size);
19480 if (addr_size == 4)
19481 return bfd_get_32 (abfd, info_ptr);
19482 else
19483 return bfd_get_64 (abfd, info_ptr);
19484 }
19485
19486 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19487
19488 static CORE_ADDR
19489 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19490 {
19491 return read_addr_index_1 (cu->per_objfile, addr_index,
19492 cu->addr_base, cu->header.addr_size);
19493 }
19494
19495 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19496
19497 static CORE_ADDR
19498 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19499 unsigned int *bytes_read)
19500 {
19501 bfd *abfd = cu->per_objfile->objfile->obfd;
19502 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19503
19504 return read_addr_index (cu, addr_index);
19505 }
19506
19507 /* See read.h. */
19508
19509 CORE_ADDR
19510 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19511 dwarf2_per_objfile *per_objfile,
19512 unsigned int addr_index)
19513 {
19514 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
19515 gdb::optional<ULONGEST> addr_base;
19516 int addr_size;
19517
19518 /* We need addr_base and addr_size.
19519 If we don't have PER_CU->cu, we have to get it.
19520 Nasty, but the alternative is storing the needed info in PER_CU,
19521 which at this point doesn't seem justified: it's not clear how frequently
19522 it would get used and it would increase the size of every PER_CU.
19523 Entry points like dwarf2_per_cu_addr_size do a similar thing
19524 so we're not in uncharted territory here.
19525 Alas we need to be a bit more complicated as addr_base is contained
19526 in the DIE.
19527
19528 We don't need to read the entire CU(/TU).
19529 We just need the header and top level die.
19530
19531 IWBN to use the aging mechanism to let us lazily later discard the CU.
19532 For now we skip this optimization. */
19533
19534 if (cu != NULL)
19535 {
19536 addr_base = cu->addr_base;
19537 addr_size = cu->header.addr_size;
19538 }
19539 else
19540 {
19541 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
19542 addr_base = reader.cu->addr_base;
19543 addr_size = reader.cu->header.addr_size;
19544 }
19545
19546 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
19547 }
19548
19549 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19550 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19551 DWO file. */
19552
19553 static const char *
19554 read_str_index (struct dwarf2_cu *cu,
19555 struct dwarf2_section_info *str_section,
19556 struct dwarf2_section_info *str_offsets_section,
19557 ULONGEST str_offsets_base, ULONGEST str_index)
19558 {
19559 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19560 struct objfile *objfile = per_objfile->objfile;
19561 const char *objf_name = objfile_name (objfile);
19562 bfd *abfd = objfile->obfd;
19563 const gdb_byte *info_ptr;
19564 ULONGEST str_offset;
19565 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19566
19567 str_section->read (objfile);
19568 str_offsets_section->read (objfile);
19569 if (str_section->buffer == NULL)
19570 error (_("%s used without %s section"
19571 " in CU at offset %s [in module %s]"),
19572 form_name, str_section->get_name (),
19573 sect_offset_str (cu->header.sect_off), objf_name);
19574 if (str_offsets_section->buffer == NULL)
19575 error (_("%s used without %s section"
19576 " in CU at offset %s [in module %s]"),
19577 form_name, str_section->get_name (),
19578 sect_offset_str (cu->header.sect_off), objf_name);
19579 info_ptr = (str_offsets_section->buffer
19580 + str_offsets_base
19581 + str_index * cu->header.offset_size);
19582 if (cu->header.offset_size == 4)
19583 str_offset = bfd_get_32 (abfd, info_ptr);
19584 else
19585 str_offset = bfd_get_64 (abfd, info_ptr);
19586 if (str_offset >= str_section->size)
19587 error (_("Offset from %s pointing outside of"
19588 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19589 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19590 return (const char *) (str_section->buffer + str_offset);
19591 }
19592
19593 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19594
19595 static const char *
19596 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19597 {
19598 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19599 ? reader->cu->header.addr_size : 0;
19600 return read_str_index (reader->cu,
19601 &reader->dwo_file->sections.str,
19602 &reader->dwo_file->sections.str_offsets,
19603 str_offsets_base, str_index);
19604 }
19605
19606 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19607
19608 static const char *
19609 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19610 {
19611 struct objfile *objfile = cu->per_objfile->objfile;
19612 const char *objf_name = objfile_name (objfile);
19613 static const char form_name[] = "DW_FORM_GNU_str_index";
19614 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19615
19616 if (!cu->str_offsets_base.has_value ())
19617 error (_("%s used in Fission stub without %s"
19618 " in CU at offset 0x%lx [in module %s]"),
19619 form_name, str_offsets_attr_name,
19620 (long) cu->header.offset_size, objf_name);
19621
19622 return read_str_index (cu,
19623 &cu->per_objfile->per_bfd->str,
19624 &cu->per_objfile->per_bfd->str_offsets,
19625 *cu->str_offsets_base, str_index);
19626 }
19627
19628 /* Return the length of an LEB128 number in BUF. */
19629
19630 static int
19631 leb128_size (const gdb_byte *buf)
19632 {
19633 const gdb_byte *begin = buf;
19634 gdb_byte byte;
19635
19636 while (1)
19637 {
19638 byte = *buf++;
19639 if ((byte & 128) == 0)
19640 return buf - begin;
19641 }
19642 }
19643
19644 static void
19645 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19646 {
19647 switch (lang)
19648 {
19649 case DW_LANG_C89:
19650 case DW_LANG_C99:
19651 case DW_LANG_C11:
19652 case DW_LANG_C:
19653 case DW_LANG_UPC:
19654 cu->language = language_c;
19655 break;
19656 case DW_LANG_Java:
19657 case DW_LANG_C_plus_plus:
19658 case DW_LANG_C_plus_plus_11:
19659 case DW_LANG_C_plus_plus_14:
19660 cu->language = language_cplus;
19661 break;
19662 case DW_LANG_D:
19663 cu->language = language_d;
19664 break;
19665 case DW_LANG_Fortran77:
19666 case DW_LANG_Fortran90:
19667 case DW_LANG_Fortran95:
19668 case DW_LANG_Fortran03:
19669 case DW_LANG_Fortran08:
19670 cu->language = language_fortran;
19671 break;
19672 case DW_LANG_Go:
19673 cu->language = language_go;
19674 break;
19675 case DW_LANG_Mips_Assembler:
19676 cu->language = language_asm;
19677 break;
19678 case DW_LANG_Ada83:
19679 case DW_LANG_Ada95:
19680 cu->language = language_ada;
19681 break;
19682 case DW_LANG_Modula2:
19683 cu->language = language_m2;
19684 break;
19685 case DW_LANG_Pascal83:
19686 cu->language = language_pascal;
19687 break;
19688 case DW_LANG_ObjC:
19689 cu->language = language_objc;
19690 break;
19691 case DW_LANG_Rust:
19692 case DW_LANG_Rust_old:
19693 cu->language = language_rust;
19694 break;
19695 case DW_LANG_Cobol74:
19696 case DW_LANG_Cobol85:
19697 default:
19698 cu->language = language_minimal;
19699 break;
19700 }
19701 cu->language_defn = language_def (cu->language);
19702 }
19703
19704 /* Return the named attribute or NULL if not there. */
19705
19706 static struct attribute *
19707 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19708 {
19709 for (;;)
19710 {
19711 unsigned int i;
19712 struct attribute *spec = NULL;
19713
19714 for (i = 0; i < die->num_attrs; ++i)
19715 {
19716 if (die->attrs[i].name == name)
19717 return &die->attrs[i];
19718 if (die->attrs[i].name == DW_AT_specification
19719 || die->attrs[i].name == DW_AT_abstract_origin)
19720 spec = &die->attrs[i];
19721 }
19722
19723 if (!spec)
19724 break;
19725
19726 die = follow_die_ref (die, spec, &cu);
19727 }
19728
19729 return NULL;
19730 }
19731
19732 /* Return the string associated with a string-typed attribute, or NULL if it
19733 is either not found or is of an incorrect type. */
19734
19735 static const char *
19736 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19737 {
19738 struct attribute *attr;
19739 const char *str = NULL;
19740
19741 attr = dwarf2_attr (die, name, cu);
19742
19743 if (attr != NULL)
19744 {
19745 str = attr->value_as_string ();
19746 if (str == nullptr)
19747 complaint (_("string type expected for attribute %s for "
19748 "DIE at %s in module %s"),
19749 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19750 objfile_name (cu->per_objfile->objfile));
19751 }
19752
19753 return str;
19754 }
19755
19756 /* Return the dwo name or NULL if not present. If present, it is in either
19757 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19758 static const char *
19759 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19760 {
19761 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19762 if (dwo_name == nullptr)
19763 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19764 return dwo_name;
19765 }
19766
19767 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19768 and holds a non-zero value. This function should only be used for
19769 DW_FORM_flag or DW_FORM_flag_present attributes. */
19770
19771 static int
19772 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19773 {
19774 struct attribute *attr = dwarf2_attr (die, name, cu);
19775
19776 return (attr && DW_UNSND (attr));
19777 }
19778
19779 static int
19780 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19781 {
19782 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19783 which value is non-zero. However, we have to be careful with
19784 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19785 (via dwarf2_flag_true_p) follows this attribute. So we may
19786 end up accidently finding a declaration attribute that belongs
19787 to a different DIE referenced by the specification attribute,
19788 even though the given DIE does not have a declaration attribute. */
19789 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19790 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19791 }
19792
19793 /* Return the die giving the specification for DIE, if there is
19794 one. *SPEC_CU is the CU containing DIE on input, and the CU
19795 containing the return value on output. If there is no
19796 specification, but there is an abstract origin, that is
19797 returned. */
19798
19799 static struct die_info *
19800 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19801 {
19802 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19803 *spec_cu);
19804
19805 if (spec_attr == NULL)
19806 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19807
19808 if (spec_attr == NULL)
19809 return NULL;
19810 else
19811 return follow_die_ref (die, spec_attr, spec_cu);
19812 }
19813
19814 /* Stub for free_line_header to match void * callback types. */
19815
19816 static void
19817 free_line_header_voidp (void *arg)
19818 {
19819 struct line_header *lh = (struct line_header *) arg;
19820
19821 delete lh;
19822 }
19823
19824 /* A convenience function to find the proper .debug_line section for a CU. */
19825
19826 static struct dwarf2_section_info *
19827 get_debug_line_section (struct dwarf2_cu *cu)
19828 {
19829 struct dwarf2_section_info *section;
19830 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19831
19832 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19833 DWO file. */
19834 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19835 section = &cu->dwo_unit->dwo_file->sections.line;
19836 else if (cu->per_cu->is_dwz)
19837 {
19838 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
19839
19840 section = &dwz->line;
19841 }
19842 else
19843 section = &per_objfile->per_bfd->line;
19844
19845 return section;
19846 }
19847
19848 /* Read the statement program header starting at OFFSET in
19849 .debug_line, or .debug_line.dwo. Return a pointer
19850 to a struct line_header, allocated using xmalloc.
19851 Returns NULL if there is a problem reading the header, e.g., if it
19852 has a version we don't understand.
19853
19854 NOTE: the strings in the include directory and file name tables of
19855 the returned object point into the dwarf line section buffer,
19856 and must not be freed. */
19857
19858 static line_header_up
19859 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19860 {
19861 struct dwarf2_section_info *section;
19862 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19863
19864 section = get_debug_line_section (cu);
19865 section->read (per_objfile->objfile);
19866 if (section->buffer == NULL)
19867 {
19868 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19869 complaint (_("missing .debug_line.dwo section"));
19870 else
19871 complaint (_("missing .debug_line section"));
19872 return 0;
19873 }
19874
19875 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19876 per_objfile, section, &cu->header);
19877 }
19878
19879 /* Subroutine of dwarf_decode_lines to simplify it.
19880 Return the file name of the psymtab for the given file_entry.
19881 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19882 If space for the result is malloc'd, *NAME_HOLDER will be set.
19883 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19884
19885 static const char *
19886 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19887 const dwarf2_psymtab *pst,
19888 const char *comp_dir,
19889 gdb::unique_xmalloc_ptr<char> *name_holder)
19890 {
19891 const char *include_name = fe.name;
19892 const char *include_name_to_compare = include_name;
19893 const char *pst_filename;
19894 int file_is_pst;
19895
19896 const char *dir_name = fe.include_dir (lh);
19897
19898 gdb::unique_xmalloc_ptr<char> hold_compare;
19899 if (!IS_ABSOLUTE_PATH (include_name)
19900 && (dir_name != NULL || comp_dir != NULL))
19901 {
19902 /* Avoid creating a duplicate psymtab for PST.
19903 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19904 Before we do the comparison, however, we need to account
19905 for DIR_NAME and COMP_DIR.
19906 First prepend dir_name (if non-NULL). If we still don't
19907 have an absolute path prepend comp_dir (if non-NULL).
19908 However, the directory we record in the include-file's
19909 psymtab does not contain COMP_DIR (to match the
19910 corresponding symtab(s)).
19911
19912 Example:
19913
19914 bash$ cd /tmp
19915 bash$ gcc -g ./hello.c
19916 include_name = "hello.c"
19917 dir_name = "."
19918 DW_AT_comp_dir = comp_dir = "/tmp"
19919 DW_AT_name = "./hello.c"
19920
19921 */
19922
19923 if (dir_name != NULL)
19924 {
19925 name_holder->reset (concat (dir_name, SLASH_STRING,
19926 include_name, (char *) NULL));
19927 include_name = name_holder->get ();
19928 include_name_to_compare = include_name;
19929 }
19930 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19931 {
19932 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19933 include_name, (char *) NULL));
19934 include_name_to_compare = hold_compare.get ();
19935 }
19936 }
19937
19938 pst_filename = pst->filename;
19939 gdb::unique_xmalloc_ptr<char> copied_name;
19940 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19941 {
19942 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19943 pst_filename, (char *) NULL));
19944 pst_filename = copied_name.get ();
19945 }
19946
19947 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19948
19949 if (file_is_pst)
19950 return NULL;
19951 return include_name;
19952 }
19953
19954 /* State machine to track the state of the line number program. */
19955
19956 class lnp_state_machine
19957 {
19958 public:
19959 /* Initialize a machine state for the start of a line number
19960 program. */
19961 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19962 bool record_lines_p);
19963
19964 file_entry *current_file ()
19965 {
19966 /* lh->file_names is 0-based, but the file name numbers in the
19967 statement program are 1-based. */
19968 return m_line_header->file_name_at (m_file);
19969 }
19970
19971 /* Record the line in the state machine. END_SEQUENCE is true if
19972 we're processing the end of a sequence. */
19973 void record_line (bool end_sequence);
19974
19975 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
19976 nop-out rest of the lines in this sequence. */
19977 void check_line_address (struct dwarf2_cu *cu,
19978 const gdb_byte *line_ptr,
19979 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19980
19981 void handle_set_discriminator (unsigned int discriminator)
19982 {
19983 m_discriminator = discriminator;
19984 m_line_has_non_zero_discriminator |= discriminator != 0;
19985 }
19986
19987 /* Handle DW_LNE_set_address. */
19988 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19989 {
19990 m_op_index = 0;
19991 address += baseaddr;
19992 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19993 }
19994
19995 /* Handle DW_LNS_advance_pc. */
19996 void handle_advance_pc (CORE_ADDR adjust);
19997
19998 /* Handle a special opcode. */
19999 void handle_special_opcode (unsigned char op_code);
20000
20001 /* Handle DW_LNS_advance_line. */
20002 void handle_advance_line (int line_delta)
20003 {
20004 advance_line (line_delta);
20005 }
20006
20007 /* Handle DW_LNS_set_file. */
20008 void handle_set_file (file_name_index file);
20009
20010 /* Handle DW_LNS_negate_stmt. */
20011 void handle_negate_stmt ()
20012 {
20013 m_is_stmt = !m_is_stmt;
20014 }
20015
20016 /* Handle DW_LNS_const_add_pc. */
20017 void handle_const_add_pc ();
20018
20019 /* Handle DW_LNS_fixed_advance_pc. */
20020 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20021 {
20022 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20023 m_op_index = 0;
20024 }
20025
20026 /* Handle DW_LNS_copy. */
20027 void handle_copy ()
20028 {
20029 record_line (false);
20030 m_discriminator = 0;
20031 }
20032
20033 /* Handle DW_LNE_end_sequence. */
20034 void handle_end_sequence ()
20035 {
20036 m_currently_recording_lines = true;
20037 }
20038
20039 private:
20040 /* Advance the line by LINE_DELTA. */
20041 void advance_line (int line_delta)
20042 {
20043 m_line += line_delta;
20044
20045 if (line_delta != 0)
20046 m_line_has_non_zero_discriminator = m_discriminator != 0;
20047 }
20048
20049 struct dwarf2_cu *m_cu;
20050
20051 gdbarch *m_gdbarch;
20052
20053 /* True if we're recording lines.
20054 Otherwise we're building partial symtabs and are just interested in
20055 finding include files mentioned by the line number program. */
20056 bool m_record_lines_p;
20057
20058 /* The line number header. */
20059 line_header *m_line_header;
20060
20061 /* These are part of the standard DWARF line number state machine,
20062 and initialized according to the DWARF spec. */
20063
20064 unsigned char m_op_index = 0;
20065 /* The line table index of the current file. */
20066 file_name_index m_file = 1;
20067 unsigned int m_line = 1;
20068
20069 /* These are initialized in the constructor. */
20070
20071 CORE_ADDR m_address;
20072 bool m_is_stmt;
20073 unsigned int m_discriminator;
20074
20075 /* Additional bits of state we need to track. */
20076
20077 /* The last file that we called dwarf2_start_subfile for.
20078 This is only used for TLLs. */
20079 unsigned int m_last_file = 0;
20080 /* The last file a line number was recorded for. */
20081 struct subfile *m_last_subfile = NULL;
20082
20083 /* The address of the last line entry. */
20084 CORE_ADDR m_last_address;
20085
20086 /* Set to true when a previous line at the same address (using
20087 m_last_address) had m_is_stmt true. This is reset to false when a
20088 line entry at a new address (m_address different to m_last_address) is
20089 processed. */
20090 bool m_stmt_at_address = false;
20091
20092 /* When true, record the lines we decode. */
20093 bool m_currently_recording_lines = false;
20094
20095 /* The last line number that was recorded, used to coalesce
20096 consecutive entries for the same line. This can happen, for
20097 example, when discriminators are present. PR 17276. */
20098 unsigned int m_last_line = 0;
20099 bool m_line_has_non_zero_discriminator = false;
20100 };
20101
20102 void
20103 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20104 {
20105 CORE_ADDR addr_adj = (((m_op_index + adjust)
20106 / m_line_header->maximum_ops_per_instruction)
20107 * m_line_header->minimum_instruction_length);
20108 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20109 m_op_index = ((m_op_index + adjust)
20110 % m_line_header->maximum_ops_per_instruction);
20111 }
20112
20113 void
20114 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20115 {
20116 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20117 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20118 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20119 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20120 / m_line_header->maximum_ops_per_instruction)
20121 * m_line_header->minimum_instruction_length);
20122 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20123 m_op_index = ((m_op_index + adj_opcode_d)
20124 % m_line_header->maximum_ops_per_instruction);
20125
20126 int line_delta = m_line_header->line_base + adj_opcode_r;
20127 advance_line (line_delta);
20128 record_line (false);
20129 m_discriminator = 0;
20130 }
20131
20132 void
20133 lnp_state_machine::handle_set_file (file_name_index file)
20134 {
20135 m_file = file;
20136
20137 const file_entry *fe = current_file ();
20138 if (fe == NULL)
20139 dwarf2_debug_line_missing_file_complaint ();
20140 else if (m_record_lines_p)
20141 {
20142 const char *dir = fe->include_dir (m_line_header);
20143
20144 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20145 m_line_has_non_zero_discriminator = m_discriminator != 0;
20146 dwarf2_start_subfile (m_cu, fe->name, dir);
20147 }
20148 }
20149
20150 void
20151 lnp_state_machine::handle_const_add_pc ()
20152 {
20153 CORE_ADDR adjust
20154 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20155
20156 CORE_ADDR addr_adj
20157 = (((m_op_index + adjust)
20158 / m_line_header->maximum_ops_per_instruction)
20159 * m_line_header->minimum_instruction_length);
20160
20161 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20162 m_op_index = ((m_op_index + adjust)
20163 % m_line_header->maximum_ops_per_instruction);
20164 }
20165
20166 /* Return non-zero if we should add LINE to the line number table.
20167 LINE is the line to add, LAST_LINE is the last line that was added,
20168 LAST_SUBFILE is the subfile for LAST_LINE.
20169 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20170 had a non-zero discriminator.
20171
20172 We have to be careful in the presence of discriminators.
20173 E.g., for this line:
20174
20175 for (i = 0; i < 100000; i++);
20176
20177 clang can emit four line number entries for that one line,
20178 each with a different discriminator.
20179 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20180
20181 However, we want gdb to coalesce all four entries into one.
20182 Otherwise the user could stepi into the middle of the line and
20183 gdb would get confused about whether the pc really was in the
20184 middle of the line.
20185
20186 Things are further complicated by the fact that two consecutive
20187 line number entries for the same line is a heuristic used by gcc
20188 to denote the end of the prologue. So we can't just discard duplicate
20189 entries, we have to be selective about it. The heuristic we use is
20190 that we only collapse consecutive entries for the same line if at least
20191 one of those entries has a non-zero discriminator. PR 17276.
20192
20193 Note: Addresses in the line number state machine can never go backwards
20194 within one sequence, thus this coalescing is ok. */
20195
20196 static int
20197 dwarf_record_line_p (struct dwarf2_cu *cu,
20198 unsigned int line, unsigned int last_line,
20199 int line_has_non_zero_discriminator,
20200 struct subfile *last_subfile)
20201 {
20202 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20203 return 1;
20204 if (line != last_line)
20205 return 1;
20206 /* Same line for the same file that we've seen already.
20207 As a last check, for pr 17276, only record the line if the line
20208 has never had a non-zero discriminator. */
20209 if (!line_has_non_zero_discriminator)
20210 return 1;
20211 return 0;
20212 }
20213
20214 /* Use the CU's builder to record line number LINE beginning at
20215 address ADDRESS in the line table of subfile SUBFILE. */
20216
20217 static void
20218 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20219 unsigned int line, CORE_ADDR address, bool is_stmt,
20220 struct dwarf2_cu *cu)
20221 {
20222 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20223
20224 if (dwarf_line_debug)
20225 {
20226 fprintf_unfiltered (gdb_stdlog,
20227 "Recording line %u, file %s, address %s\n",
20228 line, lbasename (subfile->name),
20229 paddress (gdbarch, address));
20230 }
20231
20232 if (cu != nullptr)
20233 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20234 }
20235
20236 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20237 Mark the end of a set of line number records.
20238 The arguments are the same as for dwarf_record_line_1.
20239 If SUBFILE is NULL the request is ignored. */
20240
20241 static void
20242 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20243 CORE_ADDR address, struct dwarf2_cu *cu)
20244 {
20245 if (subfile == NULL)
20246 return;
20247
20248 if (dwarf_line_debug)
20249 {
20250 fprintf_unfiltered (gdb_stdlog,
20251 "Finishing current line, file %s, address %s\n",
20252 lbasename (subfile->name),
20253 paddress (gdbarch, address));
20254 }
20255
20256 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20257 }
20258
20259 void
20260 lnp_state_machine::record_line (bool end_sequence)
20261 {
20262 if (dwarf_line_debug)
20263 {
20264 fprintf_unfiltered (gdb_stdlog,
20265 "Processing actual line %u: file %u,"
20266 " address %s, is_stmt %u, discrim %u%s\n",
20267 m_line, m_file,
20268 paddress (m_gdbarch, m_address),
20269 m_is_stmt, m_discriminator,
20270 (end_sequence ? "\t(end sequence)" : ""));
20271 }
20272
20273 file_entry *fe = current_file ();
20274
20275 if (fe == NULL)
20276 dwarf2_debug_line_missing_file_complaint ();
20277 /* For now we ignore lines not starting on an instruction boundary.
20278 But not when processing end_sequence for compatibility with the
20279 previous version of the code. */
20280 else if (m_op_index == 0 || end_sequence)
20281 {
20282 fe->included_p = 1;
20283 if (m_record_lines_p)
20284 {
20285 /* When we switch files we insert an end maker in the first file,
20286 switch to the second file and add a new line entry. The
20287 problem is that the end marker inserted in the first file will
20288 discard any previous line entries at the same address. If the
20289 line entries in the first file are marked as is-stmt, while
20290 the new line in the second file is non-stmt, then this means
20291 the end marker will discard is-stmt lines so we can have a
20292 non-stmt line. This means that there are less addresses at
20293 which the user can insert a breakpoint.
20294
20295 To improve this we track the last address in m_last_address,
20296 and whether we have seen an is-stmt at this address. Then
20297 when switching files, if we have seen a stmt at the current
20298 address, and we are switching to create a non-stmt line, then
20299 discard the new line. */
20300 bool file_changed
20301 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20302 bool ignore_this_line
20303 = (file_changed && !end_sequence && m_last_address == m_address
20304 && !m_is_stmt && m_stmt_at_address);
20305
20306 if ((file_changed && !ignore_this_line) || end_sequence)
20307 {
20308 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20309 m_currently_recording_lines ? m_cu : nullptr);
20310 }
20311
20312 if (!end_sequence && !ignore_this_line)
20313 {
20314 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20315
20316 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20317 m_line_has_non_zero_discriminator,
20318 m_last_subfile))
20319 {
20320 buildsym_compunit *builder = m_cu->get_builder ();
20321 dwarf_record_line_1 (m_gdbarch,
20322 builder->get_current_subfile (),
20323 m_line, m_address, is_stmt,
20324 m_currently_recording_lines ? m_cu : nullptr);
20325 }
20326 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20327 m_last_line = m_line;
20328 }
20329 }
20330 }
20331
20332 /* Track whether we have seen any m_is_stmt true at m_address in case we
20333 have multiple line table entries all at m_address. */
20334 if (m_last_address != m_address)
20335 {
20336 m_stmt_at_address = false;
20337 m_last_address = m_address;
20338 }
20339 m_stmt_at_address |= m_is_stmt;
20340 }
20341
20342 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20343 line_header *lh, bool record_lines_p)
20344 {
20345 m_cu = cu;
20346 m_gdbarch = arch;
20347 m_record_lines_p = record_lines_p;
20348 m_line_header = lh;
20349
20350 m_currently_recording_lines = true;
20351
20352 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20353 was a line entry for it so that the backend has a chance to adjust it
20354 and also record it in case it needs it. This is currently used by MIPS
20355 code, cf. `mips_adjust_dwarf2_line'. */
20356 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20357 m_is_stmt = lh->default_is_stmt;
20358 m_discriminator = 0;
20359
20360 m_last_address = m_address;
20361 m_stmt_at_address = false;
20362 }
20363
20364 void
20365 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20366 const gdb_byte *line_ptr,
20367 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20368 {
20369 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20370 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20371 located at 0x0. In this case, additionally check that if
20372 ADDRESS < UNRELOCATED_LOWPC. */
20373
20374 if ((address == 0 && address < unrelocated_lowpc)
20375 || address == (CORE_ADDR) -1)
20376 {
20377 /* This line table is for a function which has been
20378 GCd by the linker. Ignore it. PR gdb/12528 */
20379
20380 struct objfile *objfile = cu->per_objfile->objfile;
20381 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20382
20383 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20384 line_offset, objfile_name (objfile));
20385 m_currently_recording_lines = false;
20386 /* Note: m_currently_recording_lines is left as false until we see
20387 DW_LNE_end_sequence. */
20388 }
20389 }
20390
20391 /* Subroutine of dwarf_decode_lines to simplify it.
20392 Process the line number information in LH.
20393 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20394 program in order to set included_p for every referenced header. */
20395
20396 static void
20397 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20398 const int decode_for_pst_p, CORE_ADDR lowpc)
20399 {
20400 const gdb_byte *line_ptr, *extended_end;
20401 const gdb_byte *line_end;
20402 unsigned int bytes_read, extended_len;
20403 unsigned char op_code, extended_op;
20404 CORE_ADDR baseaddr;
20405 struct objfile *objfile = cu->per_objfile->objfile;
20406 bfd *abfd = objfile->obfd;
20407 struct gdbarch *gdbarch = objfile->arch ();
20408 /* True if we're recording line info (as opposed to building partial
20409 symtabs and just interested in finding include files mentioned by
20410 the line number program). */
20411 bool record_lines_p = !decode_for_pst_p;
20412
20413 baseaddr = objfile->text_section_offset ();
20414
20415 line_ptr = lh->statement_program_start;
20416 line_end = lh->statement_program_end;
20417
20418 /* Read the statement sequences until there's nothing left. */
20419 while (line_ptr < line_end)
20420 {
20421 /* The DWARF line number program state machine. Reset the state
20422 machine at the start of each sequence. */
20423 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20424 bool end_sequence = false;
20425
20426 if (record_lines_p)
20427 {
20428 /* Start a subfile for the current file of the state
20429 machine. */
20430 const file_entry *fe = state_machine.current_file ();
20431
20432 if (fe != NULL)
20433 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20434 }
20435
20436 /* Decode the table. */
20437 while (line_ptr < line_end && !end_sequence)
20438 {
20439 op_code = read_1_byte (abfd, line_ptr);
20440 line_ptr += 1;
20441
20442 if (op_code >= lh->opcode_base)
20443 {
20444 /* Special opcode. */
20445 state_machine.handle_special_opcode (op_code);
20446 }
20447 else switch (op_code)
20448 {
20449 case DW_LNS_extended_op:
20450 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20451 &bytes_read);
20452 line_ptr += bytes_read;
20453 extended_end = line_ptr + extended_len;
20454 extended_op = read_1_byte (abfd, line_ptr);
20455 line_ptr += 1;
20456 switch (extended_op)
20457 {
20458 case DW_LNE_end_sequence:
20459 state_machine.handle_end_sequence ();
20460 end_sequence = true;
20461 break;
20462 case DW_LNE_set_address:
20463 {
20464 CORE_ADDR address
20465 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20466 line_ptr += bytes_read;
20467
20468 state_machine.check_line_address (cu, line_ptr,
20469 lowpc - baseaddr, address);
20470 state_machine.handle_set_address (baseaddr, address);
20471 }
20472 break;
20473 case DW_LNE_define_file:
20474 {
20475 const char *cur_file;
20476 unsigned int mod_time, length;
20477 dir_index dindex;
20478
20479 cur_file = read_direct_string (abfd, line_ptr,
20480 &bytes_read);
20481 line_ptr += bytes_read;
20482 dindex = (dir_index)
20483 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20484 line_ptr += bytes_read;
20485 mod_time =
20486 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20487 line_ptr += bytes_read;
20488 length =
20489 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20490 line_ptr += bytes_read;
20491 lh->add_file_name (cur_file, dindex, mod_time, length);
20492 }
20493 break;
20494 case DW_LNE_set_discriminator:
20495 {
20496 /* The discriminator is not interesting to the
20497 debugger; just ignore it. We still need to
20498 check its value though:
20499 if there are consecutive entries for the same
20500 (non-prologue) line we want to coalesce them.
20501 PR 17276. */
20502 unsigned int discr
20503 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20504 line_ptr += bytes_read;
20505
20506 state_machine.handle_set_discriminator (discr);
20507 }
20508 break;
20509 default:
20510 complaint (_("mangled .debug_line section"));
20511 return;
20512 }
20513 /* Make sure that we parsed the extended op correctly. If e.g.
20514 we expected a different address size than the producer used,
20515 we may have read the wrong number of bytes. */
20516 if (line_ptr != extended_end)
20517 {
20518 complaint (_("mangled .debug_line section"));
20519 return;
20520 }
20521 break;
20522 case DW_LNS_copy:
20523 state_machine.handle_copy ();
20524 break;
20525 case DW_LNS_advance_pc:
20526 {
20527 CORE_ADDR adjust
20528 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20529 line_ptr += bytes_read;
20530
20531 state_machine.handle_advance_pc (adjust);
20532 }
20533 break;
20534 case DW_LNS_advance_line:
20535 {
20536 int line_delta
20537 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20538 line_ptr += bytes_read;
20539
20540 state_machine.handle_advance_line (line_delta);
20541 }
20542 break;
20543 case DW_LNS_set_file:
20544 {
20545 file_name_index file
20546 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20547 &bytes_read);
20548 line_ptr += bytes_read;
20549
20550 state_machine.handle_set_file (file);
20551 }
20552 break;
20553 case DW_LNS_set_column:
20554 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20555 line_ptr += bytes_read;
20556 break;
20557 case DW_LNS_negate_stmt:
20558 state_machine.handle_negate_stmt ();
20559 break;
20560 case DW_LNS_set_basic_block:
20561 break;
20562 /* Add to the address register of the state machine the
20563 address increment value corresponding to special opcode
20564 255. I.e., this value is scaled by the minimum
20565 instruction length since special opcode 255 would have
20566 scaled the increment. */
20567 case DW_LNS_const_add_pc:
20568 state_machine.handle_const_add_pc ();
20569 break;
20570 case DW_LNS_fixed_advance_pc:
20571 {
20572 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20573 line_ptr += 2;
20574
20575 state_machine.handle_fixed_advance_pc (addr_adj);
20576 }
20577 break;
20578 default:
20579 {
20580 /* Unknown standard opcode, ignore it. */
20581 int i;
20582
20583 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20584 {
20585 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20586 line_ptr += bytes_read;
20587 }
20588 }
20589 }
20590 }
20591
20592 if (!end_sequence)
20593 dwarf2_debug_line_missing_end_sequence_complaint ();
20594
20595 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20596 in which case we still finish recording the last line). */
20597 state_machine.record_line (true);
20598 }
20599 }
20600
20601 /* Decode the Line Number Program (LNP) for the given line_header
20602 structure and CU. The actual information extracted and the type
20603 of structures created from the LNP depends on the value of PST.
20604
20605 1. If PST is NULL, then this procedure uses the data from the program
20606 to create all necessary symbol tables, and their linetables.
20607
20608 2. If PST is not NULL, this procedure reads the program to determine
20609 the list of files included by the unit represented by PST, and
20610 builds all the associated partial symbol tables.
20611
20612 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20613 It is used for relative paths in the line table.
20614 NOTE: When processing partial symtabs (pst != NULL),
20615 comp_dir == pst->dirname.
20616
20617 NOTE: It is important that psymtabs have the same file name (via strcmp)
20618 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20619 symtab we don't use it in the name of the psymtabs we create.
20620 E.g. expand_line_sal requires this when finding psymtabs to expand.
20621 A good testcase for this is mb-inline.exp.
20622
20623 LOWPC is the lowest address in CU (or 0 if not known).
20624
20625 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20626 for its PC<->lines mapping information. Otherwise only the filename
20627 table is read in. */
20628
20629 static void
20630 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20631 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20632 CORE_ADDR lowpc, int decode_mapping)
20633 {
20634 struct objfile *objfile = cu->per_objfile->objfile;
20635 const int decode_for_pst_p = (pst != NULL);
20636
20637 if (decode_mapping)
20638 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20639
20640 if (decode_for_pst_p)
20641 {
20642 /* Now that we're done scanning the Line Header Program, we can
20643 create the psymtab of each included file. */
20644 for (auto &file_entry : lh->file_names ())
20645 if (file_entry.included_p == 1)
20646 {
20647 gdb::unique_xmalloc_ptr<char> name_holder;
20648 const char *include_name =
20649 psymtab_include_file_name (lh, file_entry, pst,
20650 comp_dir, &name_holder);
20651 if (include_name != NULL)
20652 dwarf2_create_include_psymtab (include_name, pst, objfile);
20653 }
20654 }
20655 else
20656 {
20657 /* Make sure a symtab is created for every file, even files
20658 which contain only variables (i.e. no code with associated
20659 line numbers). */
20660 buildsym_compunit *builder = cu->get_builder ();
20661 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20662
20663 for (auto &fe : lh->file_names ())
20664 {
20665 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20666 if (builder->get_current_subfile ()->symtab == NULL)
20667 {
20668 builder->get_current_subfile ()->symtab
20669 = allocate_symtab (cust,
20670 builder->get_current_subfile ()->name);
20671 }
20672 fe.symtab = builder->get_current_subfile ()->symtab;
20673 }
20674 }
20675 }
20676
20677 /* Start a subfile for DWARF. FILENAME is the name of the file and
20678 DIRNAME the name of the source directory which contains FILENAME
20679 or NULL if not known.
20680 This routine tries to keep line numbers from identical absolute and
20681 relative file names in a common subfile.
20682
20683 Using the `list' example from the GDB testsuite, which resides in
20684 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20685 of /srcdir/list0.c yields the following debugging information for list0.c:
20686
20687 DW_AT_name: /srcdir/list0.c
20688 DW_AT_comp_dir: /compdir
20689 files.files[0].name: list0.h
20690 files.files[0].dir: /srcdir
20691 files.files[1].name: list0.c
20692 files.files[1].dir: /srcdir
20693
20694 The line number information for list0.c has to end up in a single
20695 subfile, so that `break /srcdir/list0.c:1' works as expected.
20696 start_subfile will ensure that this happens provided that we pass the
20697 concatenation of files.files[1].dir and files.files[1].name as the
20698 subfile's name. */
20699
20700 static void
20701 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20702 const char *dirname)
20703 {
20704 gdb::unique_xmalloc_ptr<char> copy;
20705
20706 /* In order not to lose the line information directory,
20707 we concatenate it to the filename when it makes sense.
20708 Note that the Dwarf3 standard says (speaking of filenames in line
20709 information): ``The directory index is ignored for file names
20710 that represent full path names''. Thus ignoring dirname in the
20711 `else' branch below isn't an issue. */
20712
20713 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20714 {
20715 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20716 filename = copy.get ();
20717 }
20718
20719 cu->get_builder ()->start_subfile (filename);
20720 }
20721
20722 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20723 buildsym_compunit constructor. */
20724
20725 struct compunit_symtab *
20726 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20727 CORE_ADDR low_pc)
20728 {
20729 gdb_assert (m_builder == nullptr);
20730
20731 m_builder.reset (new struct buildsym_compunit
20732 (this->per_objfile->objfile,
20733 name, comp_dir, language, low_pc));
20734
20735 list_in_scope = get_builder ()->get_file_symbols ();
20736
20737 get_builder ()->record_debugformat ("DWARF 2");
20738 get_builder ()->record_producer (producer);
20739
20740 processing_has_namespace_info = false;
20741
20742 return get_builder ()->get_compunit_symtab ();
20743 }
20744
20745 static void
20746 var_decode_location (struct attribute *attr, struct symbol *sym,
20747 struct dwarf2_cu *cu)
20748 {
20749 struct objfile *objfile = cu->per_objfile->objfile;
20750 struct comp_unit_head *cu_header = &cu->header;
20751
20752 /* NOTE drow/2003-01-30: There used to be a comment and some special
20753 code here to turn a symbol with DW_AT_external and a
20754 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20755 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20756 with some versions of binutils) where shared libraries could have
20757 relocations against symbols in their debug information - the
20758 minimal symbol would have the right address, but the debug info
20759 would not. It's no longer necessary, because we will explicitly
20760 apply relocations when we read in the debug information now. */
20761
20762 /* A DW_AT_location attribute with no contents indicates that a
20763 variable has been optimized away. */
20764 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20765 {
20766 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20767 return;
20768 }
20769
20770 /* Handle one degenerate form of location expression specially, to
20771 preserve GDB's previous behavior when section offsets are
20772 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20773 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20774
20775 if (attr->form_is_block ()
20776 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20777 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20778 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20779 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20780 && (DW_BLOCK (attr)->size
20781 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20782 {
20783 unsigned int dummy;
20784
20785 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20786 SET_SYMBOL_VALUE_ADDRESS
20787 (sym, cu->header.read_address (objfile->obfd,
20788 DW_BLOCK (attr)->data + 1,
20789 &dummy));
20790 else
20791 SET_SYMBOL_VALUE_ADDRESS
20792 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20793 &dummy));
20794 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20795 fixup_symbol_section (sym, objfile);
20796 SET_SYMBOL_VALUE_ADDRESS
20797 (sym,
20798 SYMBOL_VALUE_ADDRESS (sym)
20799 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20800 return;
20801 }
20802
20803 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20804 expression evaluator, and use LOC_COMPUTED only when necessary
20805 (i.e. when the value of a register or memory location is
20806 referenced, or a thread-local block, etc.). Then again, it might
20807 not be worthwhile. I'm assuming that it isn't unless performance
20808 or memory numbers show me otherwise. */
20809
20810 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20811
20812 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20813 cu->has_loclist = true;
20814 }
20815
20816 /* Given a pointer to a DWARF information entry, figure out if we need
20817 to make a symbol table entry for it, and if so, create a new entry
20818 and return a pointer to it.
20819 If TYPE is NULL, determine symbol type from the die, otherwise
20820 used the passed type.
20821 If SPACE is not NULL, use it to hold the new symbol. If it is
20822 NULL, allocate a new symbol on the objfile's obstack. */
20823
20824 static struct symbol *
20825 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20826 struct symbol *space)
20827 {
20828 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20829 struct objfile *objfile = per_objfile->objfile;
20830 struct gdbarch *gdbarch = objfile->arch ();
20831 struct symbol *sym = NULL;
20832 const char *name;
20833 struct attribute *attr = NULL;
20834 struct attribute *attr2 = NULL;
20835 CORE_ADDR baseaddr;
20836 struct pending **list_to_add = NULL;
20837
20838 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20839
20840 baseaddr = objfile->text_section_offset ();
20841
20842 name = dwarf2_name (die, cu);
20843 if (name)
20844 {
20845 int suppress_add = 0;
20846
20847 if (space)
20848 sym = space;
20849 else
20850 sym = new (&objfile->objfile_obstack) symbol;
20851 OBJSTAT (objfile, n_syms++);
20852
20853 /* Cache this symbol's name and the name's demangled form (if any). */
20854 sym->set_language (cu->language, &objfile->objfile_obstack);
20855 /* Fortran does not have mangling standard and the mangling does differ
20856 between gfortran, iFort etc. */
20857 const char *physname
20858 = (cu->language == language_fortran
20859 ? dwarf2_full_name (name, die, cu)
20860 : dwarf2_physname (name, die, cu));
20861 const char *linkagename = dw2_linkage_name (die, cu);
20862
20863 if (linkagename == nullptr || cu->language == language_ada)
20864 sym->set_linkage_name (physname);
20865 else
20866 {
20867 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20868 sym->set_linkage_name (linkagename);
20869 }
20870
20871 /* Default assumptions.
20872 Use the passed type or decode it from the die. */
20873 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20874 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20875 if (type != NULL)
20876 SYMBOL_TYPE (sym) = type;
20877 else
20878 SYMBOL_TYPE (sym) = die_type (die, cu);
20879 attr = dwarf2_attr (die,
20880 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20881 cu);
20882 if (attr != nullptr)
20883 {
20884 SYMBOL_LINE (sym) = DW_UNSND (attr);
20885 }
20886
20887 attr = dwarf2_attr (die,
20888 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20889 cu);
20890 if (attr != nullptr)
20891 {
20892 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20893 struct file_entry *fe;
20894
20895 if (cu->line_header != NULL)
20896 fe = cu->line_header->file_name_at (file_index);
20897 else
20898 fe = NULL;
20899
20900 if (fe == NULL)
20901 complaint (_("file index out of range"));
20902 else
20903 symbol_set_symtab (sym, fe->symtab);
20904 }
20905
20906 switch (die->tag)
20907 {
20908 case DW_TAG_label:
20909 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20910 if (attr != nullptr)
20911 {
20912 CORE_ADDR addr;
20913
20914 addr = attr->value_as_address ();
20915 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20916 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20917 }
20918 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20919 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20920 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20921 add_symbol_to_list (sym, cu->list_in_scope);
20922 break;
20923 case DW_TAG_subprogram:
20924 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20925 finish_block. */
20926 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20927 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20928 if ((attr2 && (DW_UNSND (attr2) != 0))
20929 || cu->language == language_ada
20930 || cu->language == language_fortran)
20931 {
20932 /* Subprograms marked external are stored as a global symbol.
20933 Ada and Fortran subprograms, whether marked external or
20934 not, are always stored as a global symbol, because we want
20935 to be able to access them globally. For instance, we want
20936 to be able to break on a nested subprogram without having
20937 to specify the context. */
20938 list_to_add = cu->get_builder ()->get_global_symbols ();
20939 }
20940 else
20941 {
20942 list_to_add = cu->list_in_scope;
20943 }
20944 break;
20945 case DW_TAG_inlined_subroutine:
20946 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20947 finish_block. */
20948 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20949 SYMBOL_INLINED (sym) = 1;
20950 list_to_add = cu->list_in_scope;
20951 break;
20952 case DW_TAG_template_value_param:
20953 suppress_add = 1;
20954 /* Fall through. */
20955 case DW_TAG_constant:
20956 case DW_TAG_variable:
20957 case DW_TAG_member:
20958 /* Compilation with minimal debug info may result in
20959 variables with missing type entries. Change the
20960 misleading `void' type to something sensible. */
20961 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20962 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20963
20964 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20965 /* In the case of DW_TAG_member, we should only be called for
20966 static const members. */
20967 if (die->tag == DW_TAG_member)
20968 {
20969 /* dwarf2_add_field uses die_is_declaration,
20970 so we do the same. */
20971 gdb_assert (die_is_declaration (die, cu));
20972 gdb_assert (attr);
20973 }
20974 if (attr != nullptr)
20975 {
20976 dwarf2_const_value (attr, sym, cu);
20977 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20978 if (!suppress_add)
20979 {
20980 if (attr2 && (DW_UNSND (attr2) != 0))
20981 list_to_add = cu->get_builder ()->get_global_symbols ();
20982 else
20983 list_to_add = cu->list_in_scope;
20984 }
20985 break;
20986 }
20987 attr = dwarf2_attr (die, DW_AT_location, cu);
20988 if (attr != nullptr)
20989 {
20990 var_decode_location (attr, sym, cu);
20991 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20992
20993 /* Fortran explicitly imports any global symbols to the local
20994 scope by DW_TAG_common_block. */
20995 if (cu->language == language_fortran && die->parent
20996 && die->parent->tag == DW_TAG_common_block)
20997 attr2 = NULL;
20998
20999 if (SYMBOL_CLASS (sym) == LOC_STATIC
21000 && SYMBOL_VALUE_ADDRESS (sym) == 0
21001 && !per_objfile->per_bfd->has_section_at_zero)
21002 {
21003 /* When a static variable is eliminated by the linker,
21004 the corresponding debug information is not stripped
21005 out, but the variable address is set to null;
21006 do not add such variables into symbol table. */
21007 }
21008 else if (attr2 && (DW_UNSND (attr2) != 0))
21009 {
21010 if (SYMBOL_CLASS (sym) == LOC_STATIC
21011 && (objfile->flags & OBJF_MAINLINE) == 0
21012 && per_objfile->per_bfd->can_copy)
21013 {
21014 /* A global static variable might be subject to
21015 copy relocation. We first check for a local
21016 minsym, though, because maybe the symbol was
21017 marked hidden, in which case this would not
21018 apply. */
21019 bound_minimal_symbol found
21020 = (lookup_minimal_symbol_linkage
21021 (sym->linkage_name (), objfile));
21022 if (found.minsym != nullptr)
21023 sym->maybe_copied = 1;
21024 }
21025
21026 /* A variable with DW_AT_external is never static,
21027 but it may be block-scoped. */
21028 list_to_add
21029 = ((cu->list_in_scope
21030 == cu->get_builder ()->get_file_symbols ())
21031 ? cu->get_builder ()->get_global_symbols ()
21032 : cu->list_in_scope);
21033 }
21034 else
21035 list_to_add = cu->list_in_scope;
21036 }
21037 else
21038 {
21039 /* We do not know the address of this symbol.
21040 If it is an external symbol and we have type information
21041 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21042 The address of the variable will then be determined from
21043 the minimal symbol table whenever the variable is
21044 referenced. */
21045 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21046
21047 /* Fortran explicitly imports any global symbols to the local
21048 scope by DW_TAG_common_block. */
21049 if (cu->language == language_fortran && die->parent
21050 && die->parent->tag == DW_TAG_common_block)
21051 {
21052 /* SYMBOL_CLASS doesn't matter here because
21053 read_common_block is going to reset it. */
21054 if (!suppress_add)
21055 list_to_add = cu->list_in_scope;
21056 }
21057 else if (attr2 && (DW_UNSND (attr2) != 0)
21058 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21059 {
21060 /* A variable with DW_AT_external is never static, but it
21061 may be block-scoped. */
21062 list_to_add
21063 = ((cu->list_in_scope
21064 == cu->get_builder ()->get_file_symbols ())
21065 ? cu->get_builder ()->get_global_symbols ()
21066 : cu->list_in_scope);
21067
21068 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21069 }
21070 else if (!die_is_declaration (die, cu))
21071 {
21072 /* Use the default LOC_OPTIMIZED_OUT class. */
21073 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21074 if (!suppress_add)
21075 list_to_add = cu->list_in_scope;
21076 }
21077 }
21078 break;
21079 case DW_TAG_formal_parameter:
21080 {
21081 /* If we are inside a function, mark this as an argument. If
21082 not, we might be looking at an argument to an inlined function
21083 when we do not have enough information to show inlined frames;
21084 pretend it's a local variable in that case so that the user can
21085 still see it. */
21086 struct context_stack *curr
21087 = cu->get_builder ()->get_current_context_stack ();
21088 if (curr != nullptr && curr->name != nullptr)
21089 SYMBOL_IS_ARGUMENT (sym) = 1;
21090 attr = dwarf2_attr (die, DW_AT_location, cu);
21091 if (attr != nullptr)
21092 {
21093 var_decode_location (attr, sym, cu);
21094 }
21095 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21096 if (attr != nullptr)
21097 {
21098 dwarf2_const_value (attr, sym, cu);
21099 }
21100
21101 list_to_add = cu->list_in_scope;
21102 }
21103 break;
21104 case DW_TAG_unspecified_parameters:
21105 /* From varargs functions; gdb doesn't seem to have any
21106 interest in this information, so just ignore it for now.
21107 (FIXME?) */
21108 break;
21109 case DW_TAG_template_type_param:
21110 suppress_add = 1;
21111 /* Fall through. */
21112 case DW_TAG_class_type:
21113 case DW_TAG_interface_type:
21114 case DW_TAG_structure_type:
21115 case DW_TAG_union_type:
21116 case DW_TAG_set_type:
21117 case DW_TAG_enumeration_type:
21118 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21119 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21120
21121 {
21122 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21123 really ever be static objects: otherwise, if you try
21124 to, say, break of a class's method and you're in a file
21125 which doesn't mention that class, it won't work unless
21126 the check for all static symbols in lookup_symbol_aux
21127 saves you. See the OtherFileClass tests in
21128 gdb.c++/namespace.exp. */
21129
21130 if (!suppress_add)
21131 {
21132 buildsym_compunit *builder = cu->get_builder ();
21133 list_to_add
21134 = (cu->list_in_scope == builder->get_file_symbols ()
21135 && cu->language == language_cplus
21136 ? builder->get_global_symbols ()
21137 : cu->list_in_scope);
21138
21139 /* The semantics of C++ state that "struct foo {
21140 ... }" also defines a typedef for "foo". */
21141 if (cu->language == language_cplus
21142 || cu->language == language_ada
21143 || cu->language == language_d
21144 || cu->language == language_rust)
21145 {
21146 /* The symbol's name is already allocated along
21147 with this objfile, so we don't need to
21148 duplicate it for the type. */
21149 if (SYMBOL_TYPE (sym)->name () == 0)
21150 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21151 }
21152 }
21153 }
21154 break;
21155 case DW_TAG_typedef:
21156 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21157 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21158 list_to_add = cu->list_in_scope;
21159 break;
21160 case DW_TAG_base_type:
21161 case DW_TAG_subrange_type:
21162 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21163 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21164 list_to_add = cu->list_in_scope;
21165 break;
21166 case DW_TAG_enumerator:
21167 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21168 if (attr != nullptr)
21169 {
21170 dwarf2_const_value (attr, sym, cu);
21171 }
21172 {
21173 /* NOTE: carlton/2003-11-10: See comment above in the
21174 DW_TAG_class_type, etc. block. */
21175
21176 list_to_add
21177 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21178 && cu->language == language_cplus
21179 ? cu->get_builder ()->get_global_symbols ()
21180 : cu->list_in_scope);
21181 }
21182 break;
21183 case DW_TAG_imported_declaration:
21184 case DW_TAG_namespace:
21185 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21186 list_to_add = cu->get_builder ()->get_global_symbols ();
21187 break;
21188 case DW_TAG_module:
21189 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21190 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21191 list_to_add = cu->get_builder ()->get_global_symbols ();
21192 break;
21193 case DW_TAG_common_block:
21194 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21195 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21196 add_symbol_to_list (sym, cu->list_in_scope);
21197 break;
21198 default:
21199 /* Not a tag we recognize. Hopefully we aren't processing
21200 trash data, but since we must specifically ignore things
21201 we don't recognize, there is nothing else we should do at
21202 this point. */
21203 complaint (_("unsupported tag: '%s'"),
21204 dwarf_tag_name (die->tag));
21205 break;
21206 }
21207
21208 if (suppress_add)
21209 {
21210 sym->hash_next = objfile->template_symbols;
21211 objfile->template_symbols = sym;
21212 list_to_add = NULL;
21213 }
21214
21215 if (list_to_add != NULL)
21216 add_symbol_to_list (sym, list_to_add);
21217
21218 /* For the benefit of old versions of GCC, check for anonymous
21219 namespaces based on the demangled name. */
21220 if (!cu->processing_has_namespace_info
21221 && cu->language == language_cplus)
21222 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21223 }
21224 return (sym);
21225 }
21226
21227 /* Given an attr with a DW_FORM_dataN value in host byte order,
21228 zero-extend it as appropriate for the symbol's type. The DWARF
21229 standard (v4) is not entirely clear about the meaning of using
21230 DW_FORM_dataN for a constant with a signed type, where the type is
21231 wider than the data. The conclusion of a discussion on the DWARF
21232 list was that this is unspecified. We choose to always zero-extend
21233 because that is the interpretation long in use by GCC. */
21234
21235 static gdb_byte *
21236 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21237 struct dwarf2_cu *cu, LONGEST *value, int bits)
21238 {
21239 struct objfile *objfile = cu->per_objfile->objfile;
21240 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21241 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21242 LONGEST l = DW_UNSND (attr);
21243
21244 if (bits < sizeof (*value) * 8)
21245 {
21246 l &= ((LONGEST) 1 << bits) - 1;
21247 *value = l;
21248 }
21249 else if (bits == sizeof (*value) * 8)
21250 *value = l;
21251 else
21252 {
21253 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21254 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21255 return bytes;
21256 }
21257
21258 return NULL;
21259 }
21260
21261 /* Read a constant value from an attribute. Either set *VALUE, or if
21262 the value does not fit in *VALUE, set *BYTES - either already
21263 allocated on the objfile obstack, or newly allocated on OBSTACK,
21264 or, set *BATON, if we translated the constant to a location
21265 expression. */
21266
21267 static void
21268 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21269 const char *name, struct obstack *obstack,
21270 struct dwarf2_cu *cu,
21271 LONGEST *value, const gdb_byte **bytes,
21272 struct dwarf2_locexpr_baton **baton)
21273 {
21274 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21275 struct objfile *objfile = per_objfile->objfile;
21276 struct comp_unit_head *cu_header = &cu->header;
21277 struct dwarf_block *blk;
21278 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21279 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21280
21281 *value = 0;
21282 *bytes = NULL;
21283 *baton = NULL;
21284
21285 switch (attr->form)
21286 {
21287 case DW_FORM_addr:
21288 case DW_FORM_addrx:
21289 case DW_FORM_GNU_addr_index:
21290 {
21291 gdb_byte *data;
21292
21293 if (TYPE_LENGTH (type) != cu_header->addr_size)
21294 dwarf2_const_value_length_mismatch_complaint (name,
21295 cu_header->addr_size,
21296 TYPE_LENGTH (type));
21297 /* Symbols of this form are reasonably rare, so we just
21298 piggyback on the existing location code rather than writing
21299 a new implementation of symbol_computed_ops. */
21300 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21301 (*baton)->per_objfile = per_objfile;
21302 (*baton)->per_cu = cu->per_cu;
21303 gdb_assert ((*baton)->per_cu);
21304
21305 (*baton)->size = 2 + cu_header->addr_size;
21306 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21307 (*baton)->data = data;
21308
21309 data[0] = DW_OP_addr;
21310 store_unsigned_integer (&data[1], cu_header->addr_size,
21311 byte_order, DW_ADDR (attr));
21312 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21313 }
21314 break;
21315 case DW_FORM_string:
21316 case DW_FORM_strp:
21317 case DW_FORM_strx:
21318 case DW_FORM_GNU_str_index:
21319 case DW_FORM_GNU_strp_alt:
21320 /* DW_STRING is already allocated on the objfile obstack, point
21321 directly to it. */
21322 *bytes = (const gdb_byte *) DW_STRING (attr);
21323 break;
21324 case DW_FORM_block1:
21325 case DW_FORM_block2:
21326 case DW_FORM_block4:
21327 case DW_FORM_block:
21328 case DW_FORM_exprloc:
21329 case DW_FORM_data16:
21330 blk = DW_BLOCK (attr);
21331 if (TYPE_LENGTH (type) != blk->size)
21332 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21333 TYPE_LENGTH (type));
21334 *bytes = blk->data;
21335 break;
21336
21337 /* The DW_AT_const_value attributes are supposed to carry the
21338 symbol's value "represented as it would be on the target
21339 architecture." By the time we get here, it's already been
21340 converted to host endianness, so we just need to sign- or
21341 zero-extend it as appropriate. */
21342 case DW_FORM_data1:
21343 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21344 break;
21345 case DW_FORM_data2:
21346 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21347 break;
21348 case DW_FORM_data4:
21349 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21350 break;
21351 case DW_FORM_data8:
21352 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21353 break;
21354
21355 case DW_FORM_sdata:
21356 case DW_FORM_implicit_const:
21357 *value = DW_SND (attr);
21358 break;
21359
21360 case DW_FORM_udata:
21361 *value = DW_UNSND (attr);
21362 break;
21363
21364 default:
21365 complaint (_("unsupported const value attribute form: '%s'"),
21366 dwarf_form_name (attr->form));
21367 *value = 0;
21368 break;
21369 }
21370 }
21371
21372
21373 /* Copy constant value from an attribute to a symbol. */
21374
21375 static void
21376 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21377 struct dwarf2_cu *cu)
21378 {
21379 struct objfile *objfile = cu->per_objfile->objfile;
21380 LONGEST value;
21381 const gdb_byte *bytes;
21382 struct dwarf2_locexpr_baton *baton;
21383
21384 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21385 sym->print_name (),
21386 &objfile->objfile_obstack, cu,
21387 &value, &bytes, &baton);
21388
21389 if (baton != NULL)
21390 {
21391 SYMBOL_LOCATION_BATON (sym) = baton;
21392 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21393 }
21394 else if (bytes != NULL)
21395 {
21396 SYMBOL_VALUE_BYTES (sym) = bytes;
21397 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21398 }
21399 else
21400 {
21401 SYMBOL_VALUE (sym) = value;
21402 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21403 }
21404 }
21405
21406 /* Return the type of the die in question using its DW_AT_type attribute. */
21407
21408 static struct type *
21409 die_type (struct die_info *die, struct dwarf2_cu *cu)
21410 {
21411 struct attribute *type_attr;
21412
21413 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21414 if (!type_attr)
21415 {
21416 struct objfile *objfile = cu->per_objfile->objfile;
21417 /* A missing DW_AT_type represents a void type. */
21418 return objfile_type (objfile)->builtin_void;
21419 }
21420
21421 return lookup_die_type (die, type_attr, cu);
21422 }
21423
21424 /* True iff CU's producer generates GNAT Ada auxiliary information
21425 that allows to find parallel types through that information instead
21426 of having to do expensive parallel lookups by type name. */
21427
21428 static int
21429 need_gnat_info (struct dwarf2_cu *cu)
21430 {
21431 /* Assume that the Ada compiler was GNAT, which always produces
21432 the auxiliary information. */
21433 return (cu->language == language_ada);
21434 }
21435
21436 /* Return the auxiliary type of the die in question using its
21437 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21438 attribute is not present. */
21439
21440 static struct type *
21441 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21442 {
21443 struct attribute *type_attr;
21444
21445 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21446 if (!type_attr)
21447 return NULL;
21448
21449 return lookup_die_type (die, type_attr, cu);
21450 }
21451
21452 /* If DIE has a descriptive_type attribute, then set the TYPE's
21453 descriptive type accordingly. */
21454
21455 static void
21456 set_descriptive_type (struct type *type, struct die_info *die,
21457 struct dwarf2_cu *cu)
21458 {
21459 struct type *descriptive_type = die_descriptive_type (die, cu);
21460
21461 if (descriptive_type)
21462 {
21463 ALLOCATE_GNAT_AUX_TYPE (type);
21464 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21465 }
21466 }
21467
21468 /* Return the containing type of the die in question using its
21469 DW_AT_containing_type attribute. */
21470
21471 static struct type *
21472 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21473 {
21474 struct attribute *type_attr;
21475 struct objfile *objfile = cu->per_objfile->objfile;
21476
21477 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21478 if (!type_attr)
21479 error (_("Dwarf Error: Problem turning containing type into gdb type "
21480 "[in module %s]"), objfile_name (objfile));
21481
21482 return lookup_die_type (die, type_attr, cu);
21483 }
21484
21485 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21486
21487 static struct type *
21488 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21489 {
21490 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21491 struct objfile *objfile = per_objfile->objfile;
21492 char *saved;
21493
21494 std::string message
21495 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21496 objfile_name (objfile),
21497 sect_offset_str (cu->header.sect_off),
21498 sect_offset_str (die->sect_off));
21499 saved = obstack_strdup (&objfile->objfile_obstack, message);
21500
21501 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21502 }
21503
21504 /* Look up the type of DIE in CU using its type attribute ATTR.
21505 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21506 DW_AT_containing_type.
21507 If there is no type substitute an error marker. */
21508
21509 static struct type *
21510 lookup_die_type (struct die_info *die, const struct attribute *attr,
21511 struct dwarf2_cu *cu)
21512 {
21513 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21514 struct objfile *objfile = per_objfile->objfile;
21515 struct type *this_type;
21516
21517 gdb_assert (attr->name == DW_AT_type
21518 || attr->name == DW_AT_GNAT_descriptive_type
21519 || attr->name == DW_AT_containing_type);
21520
21521 /* First see if we have it cached. */
21522
21523 if (attr->form == DW_FORM_GNU_ref_alt)
21524 {
21525 struct dwarf2_per_cu_data *per_cu;
21526 sect_offset sect_off = attr->get_ref_die_offset ();
21527
21528 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
21529 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
21530 }
21531 else if (attr->form_is_ref ())
21532 {
21533 sect_offset sect_off = attr->get_ref_die_offset ();
21534
21535 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
21536 }
21537 else if (attr->form == DW_FORM_ref_sig8)
21538 {
21539 ULONGEST signature = DW_SIGNATURE (attr);
21540
21541 return get_signatured_type (die, signature, cu);
21542 }
21543 else
21544 {
21545 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21546 " at %s [in module %s]"),
21547 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21548 objfile_name (objfile));
21549 return build_error_marker_type (cu, die);
21550 }
21551
21552 /* If not cached we need to read it in. */
21553
21554 if (this_type == NULL)
21555 {
21556 struct die_info *type_die = NULL;
21557 struct dwarf2_cu *type_cu = cu;
21558
21559 if (attr->form_is_ref ())
21560 type_die = follow_die_ref (die, attr, &type_cu);
21561 if (type_die == NULL)
21562 return build_error_marker_type (cu, die);
21563 /* If we find the type now, it's probably because the type came
21564 from an inter-CU reference and the type's CU got expanded before
21565 ours. */
21566 this_type = read_type_die (type_die, type_cu);
21567 }
21568
21569 /* If we still don't have a type use an error marker. */
21570
21571 if (this_type == NULL)
21572 return build_error_marker_type (cu, die);
21573
21574 return this_type;
21575 }
21576
21577 /* Return the type in DIE, CU.
21578 Returns NULL for invalid types.
21579
21580 This first does a lookup in die_type_hash,
21581 and only reads the die in if necessary.
21582
21583 NOTE: This can be called when reading in partial or full symbols. */
21584
21585 static struct type *
21586 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21587 {
21588 struct type *this_type;
21589
21590 this_type = get_die_type (die, cu);
21591 if (this_type)
21592 return this_type;
21593
21594 return read_type_die_1 (die, cu);
21595 }
21596
21597 /* Read the type in DIE, CU.
21598 Returns NULL for invalid types. */
21599
21600 static struct type *
21601 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21602 {
21603 struct type *this_type = NULL;
21604
21605 switch (die->tag)
21606 {
21607 case DW_TAG_class_type:
21608 case DW_TAG_interface_type:
21609 case DW_TAG_structure_type:
21610 case DW_TAG_union_type:
21611 this_type = read_structure_type (die, cu);
21612 break;
21613 case DW_TAG_enumeration_type:
21614 this_type = read_enumeration_type (die, cu);
21615 break;
21616 case DW_TAG_subprogram:
21617 case DW_TAG_subroutine_type:
21618 case DW_TAG_inlined_subroutine:
21619 this_type = read_subroutine_type (die, cu);
21620 break;
21621 case DW_TAG_array_type:
21622 this_type = read_array_type (die, cu);
21623 break;
21624 case DW_TAG_set_type:
21625 this_type = read_set_type (die, cu);
21626 break;
21627 case DW_TAG_pointer_type:
21628 this_type = read_tag_pointer_type (die, cu);
21629 break;
21630 case DW_TAG_ptr_to_member_type:
21631 this_type = read_tag_ptr_to_member_type (die, cu);
21632 break;
21633 case DW_TAG_reference_type:
21634 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21635 break;
21636 case DW_TAG_rvalue_reference_type:
21637 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21638 break;
21639 case DW_TAG_const_type:
21640 this_type = read_tag_const_type (die, cu);
21641 break;
21642 case DW_TAG_volatile_type:
21643 this_type = read_tag_volatile_type (die, cu);
21644 break;
21645 case DW_TAG_restrict_type:
21646 this_type = read_tag_restrict_type (die, cu);
21647 break;
21648 case DW_TAG_string_type:
21649 this_type = read_tag_string_type (die, cu);
21650 break;
21651 case DW_TAG_typedef:
21652 this_type = read_typedef (die, cu);
21653 break;
21654 case DW_TAG_subrange_type:
21655 this_type = read_subrange_type (die, cu);
21656 break;
21657 case DW_TAG_base_type:
21658 this_type = read_base_type (die, cu);
21659 break;
21660 case DW_TAG_unspecified_type:
21661 this_type = read_unspecified_type (die, cu);
21662 break;
21663 case DW_TAG_namespace:
21664 this_type = read_namespace_type (die, cu);
21665 break;
21666 case DW_TAG_module:
21667 this_type = read_module_type (die, cu);
21668 break;
21669 case DW_TAG_atomic_type:
21670 this_type = read_tag_atomic_type (die, cu);
21671 break;
21672 default:
21673 complaint (_("unexpected tag in read_type_die: '%s'"),
21674 dwarf_tag_name (die->tag));
21675 break;
21676 }
21677
21678 return this_type;
21679 }
21680
21681 /* See if we can figure out if the class lives in a namespace. We do
21682 this by looking for a member function; its demangled name will
21683 contain namespace info, if there is any.
21684 Return the computed name or NULL.
21685 Space for the result is allocated on the objfile's obstack.
21686 This is the full-die version of guess_partial_die_structure_name.
21687 In this case we know DIE has no useful parent. */
21688
21689 static const char *
21690 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21691 {
21692 struct die_info *spec_die;
21693 struct dwarf2_cu *spec_cu;
21694 struct die_info *child;
21695 struct objfile *objfile = cu->per_objfile->objfile;
21696
21697 spec_cu = cu;
21698 spec_die = die_specification (die, &spec_cu);
21699 if (spec_die != NULL)
21700 {
21701 die = spec_die;
21702 cu = spec_cu;
21703 }
21704
21705 for (child = die->child;
21706 child != NULL;
21707 child = child->sibling)
21708 {
21709 if (child->tag == DW_TAG_subprogram)
21710 {
21711 const char *linkage_name = dw2_linkage_name (child, cu);
21712
21713 if (linkage_name != NULL)
21714 {
21715 gdb::unique_xmalloc_ptr<char> actual_name
21716 (cu->language_defn->class_name_from_physname (linkage_name));
21717 const char *name = NULL;
21718
21719 if (actual_name != NULL)
21720 {
21721 const char *die_name = dwarf2_name (die, cu);
21722
21723 if (die_name != NULL
21724 && strcmp (die_name, actual_name.get ()) != 0)
21725 {
21726 /* Strip off the class name from the full name.
21727 We want the prefix. */
21728 int die_name_len = strlen (die_name);
21729 int actual_name_len = strlen (actual_name.get ());
21730 const char *ptr = actual_name.get ();
21731
21732 /* Test for '::' as a sanity check. */
21733 if (actual_name_len > die_name_len + 2
21734 && ptr[actual_name_len - die_name_len - 1] == ':')
21735 name = obstack_strndup (
21736 &objfile->per_bfd->storage_obstack,
21737 ptr, actual_name_len - die_name_len - 2);
21738 }
21739 }
21740 return name;
21741 }
21742 }
21743 }
21744
21745 return NULL;
21746 }
21747
21748 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21749 prefix part in such case. See
21750 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21751
21752 static const char *
21753 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21754 {
21755 struct attribute *attr;
21756 const char *base;
21757
21758 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21759 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21760 return NULL;
21761
21762 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21763 return NULL;
21764
21765 attr = dw2_linkage_name_attr (die, cu);
21766 if (attr == NULL || DW_STRING (attr) == NULL)
21767 return NULL;
21768
21769 /* dwarf2_name had to be already called. */
21770 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21771
21772 /* Strip the base name, keep any leading namespaces/classes. */
21773 base = strrchr (DW_STRING (attr), ':');
21774 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21775 return "";
21776
21777 struct objfile *objfile = cu->per_objfile->objfile;
21778 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21779 DW_STRING (attr),
21780 &base[-1] - DW_STRING (attr));
21781 }
21782
21783 /* Return the name of the namespace/class that DIE is defined within,
21784 or "" if we can't tell. The caller should not xfree the result.
21785
21786 For example, if we're within the method foo() in the following
21787 code:
21788
21789 namespace N {
21790 class C {
21791 void foo () {
21792 }
21793 };
21794 }
21795
21796 then determine_prefix on foo's die will return "N::C". */
21797
21798 static const char *
21799 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21800 {
21801 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21802 struct die_info *parent, *spec_die;
21803 struct dwarf2_cu *spec_cu;
21804 struct type *parent_type;
21805 const char *retval;
21806
21807 if (cu->language != language_cplus
21808 && cu->language != language_fortran && cu->language != language_d
21809 && cu->language != language_rust)
21810 return "";
21811
21812 retval = anonymous_struct_prefix (die, cu);
21813 if (retval)
21814 return retval;
21815
21816 /* We have to be careful in the presence of DW_AT_specification.
21817 For example, with GCC 3.4, given the code
21818
21819 namespace N {
21820 void foo() {
21821 // Definition of N::foo.
21822 }
21823 }
21824
21825 then we'll have a tree of DIEs like this:
21826
21827 1: DW_TAG_compile_unit
21828 2: DW_TAG_namespace // N
21829 3: DW_TAG_subprogram // declaration of N::foo
21830 4: DW_TAG_subprogram // definition of N::foo
21831 DW_AT_specification // refers to die #3
21832
21833 Thus, when processing die #4, we have to pretend that we're in
21834 the context of its DW_AT_specification, namely the contex of die
21835 #3. */
21836 spec_cu = cu;
21837 spec_die = die_specification (die, &spec_cu);
21838 if (spec_die == NULL)
21839 parent = die->parent;
21840 else
21841 {
21842 parent = spec_die->parent;
21843 cu = spec_cu;
21844 }
21845
21846 if (parent == NULL)
21847 return "";
21848 else if (parent->building_fullname)
21849 {
21850 const char *name;
21851 const char *parent_name;
21852
21853 /* It has been seen on RealView 2.2 built binaries,
21854 DW_TAG_template_type_param types actually _defined_ as
21855 children of the parent class:
21856
21857 enum E {};
21858 template class <class Enum> Class{};
21859 Class<enum E> class_e;
21860
21861 1: DW_TAG_class_type (Class)
21862 2: DW_TAG_enumeration_type (E)
21863 3: DW_TAG_enumerator (enum1:0)
21864 3: DW_TAG_enumerator (enum2:1)
21865 ...
21866 2: DW_TAG_template_type_param
21867 DW_AT_type DW_FORM_ref_udata (E)
21868
21869 Besides being broken debug info, it can put GDB into an
21870 infinite loop. Consider:
21871
21872 When we're building the full name for Class<E>, we'll start
21873 at Class, and go look over its template type parameters,
21874 finding E. We'll then try to build the full name of E, and
21875 reach here. We're now trying to build the full name of E,
21876 and look over the parent DIE for containing scope. In the
21877 broken case, if we followed the parent DIE of E, we'd again
21878 find Class, and once again go look at its template type
21879 arguments, etc., etc. Simply don't consider such parent die
21880 as source-level parent of this die (it can't be, the language
21881 doesn't allow it), and break the loop here. */
21882 name = dwarf2_name (die, cu);
21883 parent_name = dwarf2_name (parent, cu);
21884 complaint (_("template param type '%s' defined within parent '%s'"),
21885 name ? name : "<unknown>",
21886 parent_name ? parent_name : "<unknown>");
21887 return "";
21888 }
21889 else
21890 switch (parent->tag)
21891 {
21892 case DW_TAG_namespace:
21893 parent_type = read_type_die (parent, cu);
21894 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21895 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21896 Work around this problem here. */
21897 if (cu->language == language_cplus
21898 && strcmp (parent_type->name (), "::") == 0)
21899 return "";
21900 /* We give a name to even anonymous namespaces. */
21901 return parent_type->name ();
21902 case DW_TAG_class_type:
21903 case DW_TAG_interface_type:
21904 case DW_TAG_structure_type:
21905 case DW_TAG_union_type:
21906 case DW_TAG_module:
21907 parent_type = read_type_die (parent, cu);
21908 if (parent_type->name () != NULL)
21909 return parent_type->name ();
21910 else
21911 /* An anonymous structure is only allowed non-static data
21912 members; no typedefs, no member functions, et cetera.
21913 So it does not need a prefix. */
21914 return "";
21915 case DW_TAG_compile_unit:
21916 case DW_TAG_partial_unit:
21917 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21918 if (cu->language == language_cplus
21919 && !per_objfile->per_bfd->types.empty ()
21920 && die->child != NULL
21921 && (die->tag == DW_TAG_class_type
21922 || die->tag == DW_TAG_structure_type
21923 || die->tag == DW_TAG_union_type))
21924 {
21925 const char *name = guess_full_die_structure_name (die, cu);
21926 if (name != NULL)
21927 return name;
21928 }
21929 return "";
21930 case DW_TAG_subprogram:
21931 /* Nested subroutines in Fortran get a prefix with the name
21932 of the parent's subroutine. */
21933 if (cu->language == language_fortran)
21934 {
21935 if ((die->tag == DW_TAG_subprogram)
21936 && (dwarf2_name (parent, cu) != NULL))
21937 return dwarf2_name (parent, cu);
21938 }
21939 return determine_prefix (parent, cu);
21940 case DW_TAG_enumeration_type:
21941 parent_type = read_type_die (parent, cu);
21942 if (TYPE_DECLARED_CLASS (parent_type))
21943 {
21944 if (parent_type->name () != NULL)
21945 return parent_type->name ();
21946 return "";
21947 }
21948 /* Fall through. */
21949 default:
21950 return determine_prefix (parent, cu);
21951 }
21952 }
21953
21954 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21955 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21956 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21957 an obconcat, otherwise allocate storage for the result. The CU argument is
21958 used to determine the language and hence, the appropriate separator. */
21959
21960 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21961
21962 static char *
21963 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21964 int physname, struct dwarf2_cu *cu)
21965 {
21966 const char *lead = "";
21967 const char *sep;
21968
21969 if (suffix == NULL || suffix[0] == '\0'
21970 || prefix == NULL || prefix[0] == '\0')
21971 sep = "";
21972 else if (cu->language == language_d)
21973 {
21974 /* For D, the 'main' function could be defined in any module, but it
21975 should never be prefixed. */
21976 if (strcmp (suffix, "D main") == 0)
21977 {
21978 prefix = "";
21979 sep = "";
21980 }
21981 else
21982 sep = ".";
21983 }
21984 else if (cu->language == language_fortran && physname)
21985 {
21986 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21987 DW_AT_MIPS_linkage_name is preferred and used instead. */
21988
21989 lead = "__";
21990 sep = "_MOD_";
21991 }
21992 else
21993 sep = "::";
21994
21995 if (prefix == NULL)
21996 prefix = "";
21997 if (suffix == NULL)
21998 suffix = "";
21999
22000 if (obs == NULL)
22001 {
22002 char *retval
22003 = ((char *)
22004 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22005
22006 strcpy (retval, lead);
22007 strcat (retval, prefix);
22008 strcat (retval, sep);
22009 strcat (retval, suffix);
22010 return retval;
22011 }
22012 else
22013 {
22014 /* We have an obstack. */
22015 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22016 }
22017 }
22018
22019 /* Get name of a die, return NULL if not found. */
22020
22021 static const char *
22022 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22023 struct objfile *objfile)
22024 {
22025 if (name && cu->language == language_cplus)
22026 {
22027 gdb::unique_xmalloc_ptr<char> canon_name
22028 = cp_canonicalize_string (name);
22029
22030 if (canon_name != nullptr)
22031 name = objfile->intern (canon_name.get ());
22032 }
22033
22034 return name;
22035 }
22036
22037 /* Get name of a die, return NULL if not found.
22038 Anonymous namespaces are converted to their magic string. */
22039
22040 static const char *
22041 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22042 {
22043 struct attribute *attr;
22044 struct objfile *objfile = cu->per_objfile->objfile;
22045
22046 attr = dwarf2_attr (die, DW_AT_name, cu);
22047 if ((!attr || !DW_STRING (attr))
22048 && die->tag != DW_TAG_namespace
22049 && die->tag != DW_TAG_class_type
22050 && die->tag != DW_TAG_interface_type
22051 && die->tag != DW_TAG_structure_type
22052 && die->tag != DW_TAG_union_type)
22053 return NULL;
22054
22055 switch (die->tag)
22056 {
22057 case DW_TAG_compile_unit:
22058 case DW_TAG_partial_unit:
22059 /* Compilation units have a DW_AT_name that is a filename, not
22060 a source language identifier. */
22061 case DW_TAG_enumeration_type:
22062 case DW_TAG_enumerator:
22063 /* These tags always have simple identifiers already; no need
22064 to canonicalize them. */
22065 return DW_STRING (attr);
22066
22067 case DW_TAG_namespace:
22068 if (attr != NULL && DW_STRING (attr) != NULL)
22069 return DW_STRING (attr);
22070 return CP_ANONYMOUS_NAMESPACE_STR;
22071
22072 case DW_TAG_class_type:
22073 case DW_TAG_interface_type:
22074 case DW_TAG_structure_type:
22075 case DW_TAG_union_type:
22076 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22077 structures or unions. These were of the form "._%d" in GCC 4.1,
22078 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22079 and GCC 4.4. We work around this problem by ignoring these. */
22080 if (attr && DW_STRING (attr)
22081 && (startswith (DW_STRING (attr), "._")
22082 || startswith (DW_STRING (attr), "<anonymous")))
22083 return NULL;
22084
22085 /* GCC might emit a nameless typedef that has a linkage name. See
22086 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22087 if (!attr || DW_STRING (attr) == NULL)
22088 {
22089 attr = dw2_linkage_name_attr (die, cu);
22090 if (attr == NULL || DW_STRING (attr) == NULL)
22091 return NULL;
22092
22093 /* Avoid demangling DW_STRING (attr) the second time on a second
22094 call for the same DIE. */
22095 if (!DW_STRING_IS_CANONICAL (attr))
22096 {
22097 gdb::unique_xmalloc_ptr<char> demangled
22098 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22099 if (demangled == nullptr)
22100 return nullptr;
22101
22102 DW_STRING (attr) = objfile->intern (demangled.get ());
22103 DW_STRING_IS_CANONICAL (attr) = 1;
22104 }
22105
22106 /* Strip any leading namespaces/classes, keep only the base name.
22107 DW_AT_name for named DIEs does not contain the prefixes. */
22108 const char *base = strrchr (DW_STRING (attr), ':');
22109 if (base && base > DW_STRING (attr) && base[-1] == ':')
22110 return &base[1];
22111 else
22112 return DW_STRING (attr);
22113 }
22114 break;
22115
22116 default:
22117 break;
22118 }
22119
22120 if (!DW_STRING_IS_CANONICAL (attr))
22121 {
22122 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22123 objfile);
22124 DW_STRING_IS_CANONICAL (attr) = 1;
22125 }
22126 return DW_STRING (attr);
22127 }
22128
22129 /* Return the die that this die in an extension of, or NULL if there
22130 is none. *EXT_CU is the CU containing DIE on input, and the CU
22131 containing the return value on output. */
22132
22133 static struct die_info *
22134 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22135 {
22136 struct attribute *attr;
22137
22138 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22139 if (attr == NULL)
22140 return NULL;
22141
22142 return follow_die_ref (die, attr, ext_cu);
22143 }
22144
22145 static void
22146 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22147 {
22148 unsigned int i;
22149
22150 print_spaces (indent, f);
22151 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22152 dwarf_tag_name (die->tag), die->abbrev,
22153 sect_offset_str (die->sect_off));
22154
22155 if (die->parent != NULL)
22156 {
22157 print_spaces (indent, f);
22158 fprintf_unfiltered (f, " parent at offset: %s\n",
22159 sect_offset_str (die->parent->sect_off));
22160 }
22161
22162 print_spaces (indent, f);
22163 fprintf_unfiltered (f, " has children: %s\n",
22164 dwarf_bool_name (die->child != NULL));
22165
22166 print_spaces (indent, f);
22167 fprintf_unfiltered (f, " attributes:\n");
22168
22169 for (i = 0; i < die->num_attrs; ++i)
22170 {
22171 print_spaces (indent, f);
22172 fprintf_unfiltered (f, " %s (%s) ",
22173 dwarf_attr_name (die->attrs[i].name),
22174 dwarf_form_name (die->attrs[i].form));
22175
22176 switch (die->attrs[i].form)
22177 {
22178 case DW_FORM_addr:
22179 case DW_FORM_addrx:
22180 case DW_FORM_GNU_addr_index:
22181 fprintf_unfiltered (f, "address: ");
22182 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22183 break;
22184 case DW_FORM_block2:
22185 case DW_FORM_block4:
22186 case DW_FORM_block:
22187 case DW_FORM_block1:
22188 fprintf_unfiltered (f, "block: size %s",
22189 pulongest (DW_BLOCK (&die->attrs[i])->size));
22190 break;
22191 case DW_FORM_exprloc:
22192 fprintf_unfiltered (f, "expression: size %s",
22193 pulongest (DW_BLOCK (&die->attrs[i])->size));
22194 break;
22195 case DW_FORM_data16:
22196 fprintf_unfiltered (f, "constant of 16 bytes");
22197 break;
22198 case DW_FORM_ref_addr:
22199 fprintf_unfiltered (f, "ref address: ");
22200 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22201 break;
22202 case DW_FORM_GNU_ref_alt:
22203 fprintf_unfiltered (f, "alt ref address: ");
22204 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22205 break;
22206 case DW_FORM_ref1:
22207 case DW_FORM_ref2:
22208 case DW_FORM_ref4:
22209 case DW_FORM_ref8:
22210 case DW_FORM_ref_udata:
22211 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22212 (long) (DW_UNSND (&die->attrs[i])));
22213 break;
22214 case DW_FORM_data1:
22215 case DW_FORM_data2:
22216 case DW_FORM_data4:
22217 case DW_FORM_data8:
22218 case DW_FORM_udata:
22219 case DW_FORM_sdata:
22220 fprintf_unfiltered (f, "constant: %s",
22221 pulongest (DW_UNSND (&die->attrs[i])));
22222 break;
22223 case DW_FORM_sec_offset:
22224 fprintf_unfiltered (f, "section offset: %s",
22225 pulongest (DW_UNSND (&die->attrs[i])));
22226 break;
22227 case DW_FORM_ref_sig8:
22228 fprintf_unfiltered (f, "signature: %s",
22229 hex_string (DW_SIGNATURE (&die->attrs[i])));
22230 break;
22231 case DW_FORM_string:
22232 case DW_FORM_strp:
22233 case DW_FORM_line_strp:
22234 case DW_FORM_strx:
22235 case DW_FORM_GNU_str_index:
22236 case DW_FORM_GNU_strp_alt:
22237 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22238 DW_STRING (&die->attrs[i])
22239 ? DW_STRING (&die->attrs[i]) : "",
22240 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22241 break;
22242 case DW_FORM_flag:
22243 if (DW_UNSND (&die->attrs[i]))
22244 fprintf_unfiltered (f, "flag: TRUE");
22245 else
22246 fprintf_unfiltered (f, "flag: FALSE");
22247 break;
22248 case DW_FORM_flag_present:
22249 fprintf_unfiltered (f, "flag: TRUE");
22250 break;
22251 case DW_FORM_indirect:
22252 /* The reader will have reduced the indirect form to
22253 the "base form" so this form should not occur. */
22254 fprintf_unfiltered (f,
22255 "unexpected attribute form: DW_FORM_indirect");
22256 break;
22257 case DW_FORM_implicit_const:
22258 fprintf_unfiltered (f, "constant: %s",
22259 plongest (DW_SND (&die->attrs[i])));
22260 break;
22261 default:
22262 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22263 die->attrs[i].form);
22264 break;
22265 }
22266 fprintf_unfiltered (f, "\n");
22267 }
22268 }
22269
22270 static void
22271 dump_die_for_error (struct die_info *die)
22272 {
22273 dump_die_shallow (gdb_stderr, 0, die);
22274 }
22275
22276 static void
22277 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22278 {
22279 int indent = level * 4;
22280
22281 gdb_assert (die != NULL);
22282
22283 if (level >= max_level)
22284 return;
22285
22286 dump_die_shallow (f, indent, die);
22287
22288 if (die->child != NULL)
22289 {
22290 print_spaces (indent, f);
22291 fprintf_unfiltered (f, " Children:");
22292 if (level + 1 < max_level)
22293 {
22294 fprintf_unfiltered (f, "\n");
22295 dump_die_1 (f, level + 1, max_level, die->child);
22296 }
22297 else
22298 {
22299 fprintf_unfiltered (f,
22300 " [not printed, max nesting level reached]\n");
22301 }
22302 }
22303
22304 if (die->sibling != NULL && level > 0)
22305 {
22306 dump_die_1 (f, level, max_level, die->sibling);
22307 }
22308 }
22309
22310 /* This is called from the pdie macro in gdbinit.in.
22311 It's not static so gcc will keep a copy callable from gdb. */
22312
22313 void
22314 dump_die (struct die_info *die, int max_level)
22315 {
22316 dump_die_1 (gdb_stdlog, 0, max_level, die);
22317 }
22318
22319 static void
22320 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22321 {
22322 void **slot;
22323
22324 slot = htab_find_slot_with_hash (cu->die_hash, die,
22325 to_underlying (die->sect_off),
22326 INSERT);
22327
22328 *slot = die;
22329 }
22330
22331 /* Follow reference or signature attribute ATTR of SRC_DIE.
22332 On entry *REF_CU is the CU of SRC_DIE.
22333 On exit *REF_CU is the CU of the result. */
22334
22335 static struct die_info *
22336 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22337 struct dwarf2_cu **ref_cu)
22338 {
22339 struct die_info *die;
22340
22341 if (attr->form_is_ref ())
22342 die = follow_die_ref (src_die, attr, ref_cu);
22343 else if (attr->form == DW_FORM_ref_sig8)
22344 die = follow_die_sig (src_die, attr, ref_cu);
22345 else
22346 {
22347 dump_die_for_error (src_die);
22348 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22349 objfile_name ((*ref_cu)->per_objfile->objfile));
22350 }
22351
22352 return die;
22353 }
22354
22355 /* Follow reference OFFSET.
22356 On entry *REF_CU is the CU of the source die referencing OFFSET.
22357 On exit *REF_CU is the CU of the result.
22358 Returns NULL if OFFSET is invalid. */
22359
22360 static struct die_info *
22361 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22362 struct dwarf2_cu **ref_cu)
22363 {
22364 struct die_info temp_die;
22365 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22366 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22367
22368 gdb_assert (cu->per_cu != NULL);
22369
22370 target_cu = cu;
22371
22372 if (cu->per_cu->is_debug_types)
22373 {
22374 /* .debug_types CUs cannot reference anything outside their CU.
22375 If they need to, they have to reference a signatured type via
22376 DW_FORM_ref_sig8. */
22377 if (!cu->header.offset_in_cu_p (sect_off))
22378 return NULL;
22379 }
22380 else if (offset_in_dwz != cu->per_cu->is_dwz
22381 || !cu->header.offset_in_cu_p (sect_off))
22382 {
22383 struct dwarf2_per_cu_data *per_cu;
22384
22385 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22386 per_objfile);
22387
22388 /* If necessary, add it to the queue and load its DIEs. */
22389 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22390 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
22391
22392 target_cu = per_objfile->get_cu (per_cu);
22393 }
22394 else if (cu->dies == NULL)
22395 {
22396 /* We're loading full DIEs during partial symbol reading. */
22397 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22398 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
22399 }
22400
22401 *ref_cu = target_cu;
22402 temp_die.sect_off = sect_off;
22403
22404 if (target_cu != cu)
22405 target_cu->ancestor = cu;
22406
22407 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22408 &temp_die,
22409 to_underlying (sect_off));
22410 }
22411
22412 /* Follow reference attribute ATTR of SRC_DIE.
22413 On entry *REF_CU is the CU of SRC_DIE.
22414 On exit *REF_CU is the CU of the result. */
22415
22416 static struct die_info *
22417 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22418 struct dwarf2_cu **ref_cu)
22419 {
22420 sect_offset sect_off = attr->get_ref_die_offset ();
22421 struct dwarf2_cu *cu = *ref_cu;
22422 struct die_info *die;
22423
22424 die = follow_die_offset (sect_off,
22425 (attr->form == DW_FORM_GNU_ref_alt
22426 || cu->per_cu->is_dwz),
22427 ref_cu);
22428 if (!die)
22429 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22430 "at %s [in module %s]"),
22431 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22432 objfile_name (cu->per_objfile->objfile));
22433
22434 return die;
22435 }
22436
22437 /* See read.h. */
22438
22439 struct dwarf2_locexpr_baton
22440 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22441 dwarf2_per_cu_data *per_cu,
22442 dwarf2_per_objfile *per_objfile,
22443 CORE_ADDR (*get_frame_pc) (void *baton),
22444 void *baton, bool resolve_abstract_p)
22445 {
22446 struct die_info *die;
22447 struct attribute *attr;
22448 struct dwarf2_locexpr_baton retval;
22449 struct objfile *objfile = per_objfile->objfile;
22450
22451 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22452 if (cu == nullptr)
22453 cu = load_cu (per_cu, per_objfile, false);
22454
22455 if (cu == nullptr)
22456 {
22457 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22458 Instead just throw an error, not much else we can do. */
22459 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22460 sect_offset_str (sect_off), objfile_name (objfile));
22461 }
22462
22463 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22464 if (!die)
22465 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22466 sect_offset_str (sect_off), objfile_name (objfile));
22467
22468 attr = dwarf2_attr (die, DW_AT_location, cu);
22469 if (!attr && resolve_abstract_p
22470 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22471 != per_objfile->per_bfd->abstract_to_concrete.end ()))
22472 {
22473 CORE_ADDR pc = (*get_frame_pc) (baton);
22474 CORE_ADDR baseaddr = objfile->text_section_offset ();
22475 struct gdbarch *gdbarch = objfile->arch ();
22476
22477 for (const auto &cand_off
22478 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22479 {
22480 struct dwarf2_cu *cand_cu = cu;
22481 struct die_info *cand
22482 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22483 if (!cand
22484 || !cand->parent
22485 || cand->parent->tag != DW_TAG_subprogram)
22486 continue;
22487
22488 CORE_ADDR pc_low, pc_high;
22489 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22490 if (pc_low == ((CORE_ADDR) -1))
22491 continue;
22492 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22493 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22494 if (!(pc_low <= pc && pc < pc_high))
22495 continue;
22496
22497 die = cand;
22498 attr = dwarf2_attr (die, DW_AT_location, cu);
22499 break;
22500 }
22501 }
22502
22503 if (!attr)
22504 {
22505 /* DWARF: "If there is no such attribute, then there is no effect.".
22506 DATA is ignored if SIZE is 0. */
22507
22508 retval.data = NULL;
22509 retval.size = 0;
22510 }
22511 else if (attr->form_is_section_offset ())
22512 {
22513 struct dwarf2_loclist_baton loclist_baton;
22514 CORE_ADDR pc = (*get_frame_pc) (baton);
22515 size_t size;
22516
22517 fill_in_loclist_baton (cu, &loclist_baton, attr);
22518
22519 retval.data = dwarf2_find_location_expression (&loclist_baton,
22520 &size, pc);
22521 retval.size = size;
22522 }
22523 else
22524 {
22525 if (!attr->form_is_block ())
22526 error (_("Dwarf Error: DIE at %s referenced in module %s "
22527 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22528 sect_offset_str (sect_off), objfile_name (objfile));
22529
22530 retval.data = DW_BLOCK (attr)->data;
22531 retval.size = DW_BLOCK (attr)->size;
22532 }
22533 retval.per_objfile = per_objfile;
22534 retval.per_cu = cu->per_cu;
22535
22536 per_objfile->age_comp_units ();
22537
22538 return retval;
22539 }
22540
22541 /* See read.h. */
22542
22543 struct dwarf2_locexpr_baton
22544 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22545 dwarf2_per_cu_data *per_cu,
22546 dwarf2_per_objfile *per_objfile,
22547 CORE_ADDR (*get_frame_pc) (void *baton),
22548 void *baton)
22549 {
22550 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22551
22552 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22553 get_frame_pc, baton);
22554 }
22555
22556 /* Write a constant of a given type as target-ordered bytes into
22557 OBSTACK. */
22558
22559 static const gdb_byte *
22560 write_constant_as_bytes (struct obstack *obstack,
22561 enum bfd_endian byte_order,
22562 struct type *type,
22563 ULONGEST value,
22564 LONGEST *len)
22565 {
22566 gdb_byte *result;
22567
22568 *len = TYPE_LENGTH (type);
22569 result = (gdb_byte *) obstack_alloc (obstack, *len);
22570 store_unsigned_integer (result, *len, byte_order, value);
22571
22572 return result;
22573 }
22574
22575 /* See read.h. */
22576
22577 const gdb_byte *
22578 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22579 dwarf2_per_cu_data *per_cu,
22580 dwarf2_per_objfile *per_objfile,
22581 obstack *obstack,
22582 LONGEST *len)
22583 {
22584 struct die_info *die;
22585 struct attribute *attr;
22586 const gdb_byte *result = NULL;
22587 struct type *type;
22588 LONGEST value;
22589 enum bfd_endian byte_order;
22590 struct objfile *objfile = per_objfile->objfile;
22591
22592 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22593 if (cu == nullptr)
22594 cu = load_cu (per_cu, per_objfile, false);
22595
22596 if (cu == nullptr)
22597 {
22598 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22599 Instead just throw an error, not much else we can do. */
22600 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22601 sect_offset_str (sect_off), objfile_name (objfile));
22602 }
22603
22604 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22605 if (!die)
22606 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22607 sect_offset_str (sect_off), objfile_name (objfile));
22608
22609 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22610 if (attr == NULL)
22611 return NULL;
22612
22613 byte_order = (bfd_big_endian (objfile->obfd)
22614 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22615
22616 switch (attr->form)
22617 {
22618 case DW_FORM_addr:
22619 case DW_FORM_addrx:
22620 case DW_FORM_GNU_addr_index:
22621 {
22622 gdb_byte *tem;
22623
22624 *len = cu->header.addr_size;
22625 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22626 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22627 result = tem;
22628 }
22629 break;
22630 case DW_FORM_string:
22631 case DW_FORM_strp:
22632 case DW_FORM_strx:
22633 case DW_FORM_GNU_str_index:
22634 case DW_FORM_GNU_strp_alt:
22635 /* DW_STRING is already allocated on the objfile obstack, point
22636 directly to it. */
22637 result = (const gdb_byte *) DW_STRING (attr);
22638 *len = strlen (DW_STRING (attr));
22639 break;
22640 case DW_FORM_block1:
22641 case DW_FORM_block2:
22642 case DW_FORM_block4:
22643 case DW_FORM_block:
22644 case DW_FORM_exprloc:
22645 case DW_FORM_data16:
22646 result = DW_BLOCK (attr)->data;
22647 *len = DW_BLOCK (attr)->size;
22648 break;
22649
22650 /* The DW_AT_const_value attributes are supposed to carry the
22651 symbol's value "represented as it would be on the target
22652 architecture." By the time we get here, it's already been
22653 converted to host endianness, so we just need to sign- or
22654 zero-extend it as appropriate. */
22655 case DW_FORM_data1:
22656 type = die_type (die, cu);
22657 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22658 if (result == NULL)
22659 result = write_constant_as_bytes (obstack, byte_order,
22660 type, value, len);
22661 break;
22662 case DW_FORM_data2:
22663 type = die_type (die, cu);
22664 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22665 if (result == NULL)
22666 result = write_constant_as_bytes (obstack, byte_order,
22667 type, value, len);
22668 break;
22669 case DW_FORM_data4:
22670 type = die_type (die, cu);
22671 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22672 if (result == NULL)
22673 result = write_constant_as_bytes (obstack, byte_order,
22674 type, value, len);
22675 break;
22676 case DW_FORM_data8:
22677 type = die_type (die, cu);
22678 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22679 if (result == NULL)
22680 result = write_constant_as_bytes (obstack, byte_order,
22681 type, value, len);
22682 break;
22683
22684 case DW_FORM_sdata:
22685 case DW_FORM_implicit_const:
22686 type = die_type (die, cu);
22687 result = write_constant_as_bytes (obstack, byte_order,
22688 type, DW_SND (attr), len);
22689 break;
22690
22691 case DW_FORM_udata:
22692 type = die_type (die, cu);
22693 result = write_constant_as_bytes (obstack, byte_order,
22694 type, DW_UNSND (attr), len);
22695 break;
22696
22697 default:
22698 complaint (_("unsupported const value attribute form: '%s'"),
22699 dwarf_form_name (attr->form));
22700 break;
22701 }
22702
22703 return result;
22704 }
22705
22706 /* See read.h. */
22707
22708 struct type *
22709 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22710 dwarf2_per_cu_data *per_cu,
22711 dwarf2_per_objfile *per_objfile)
22712 {
22713 struct die_info *die;
22714
22715 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22716 if (cu == nullptr)
22717 cu = load_cu (per_cu, per_objfile, false);
22718
22719 if (cu == nullptr)
22720 return nullptr;
22721
22722 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22723 if (!die)
22724 return NULL;
22725
22726 return die_type (die, cu);
22727 }
22728
22729 /* See read.h. */
22730
22731 struct type *
22732 dwarf2_get_die_type (cu_offset die_offset,
22733 dwarf2_per_cu_data *per_cu,
22734 dwarf2_per_objfile *per_objfile)
22735 {
22736 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22737 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22738 }
22739
22740 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22741 On entry *REF_CU is the CU of SRC_DIE.
22742 On exit *REF_CU is the CU of the result.
22743 Returns NULL if the referenced DIE isn't found. */
22744
22745 static struct die_info *
22746 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22747 struct dwarf2_cu **ref_cu)
22748 {
22749 struct die_info temp_die;
22750 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22751 struct die_info *die;
22752 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
22753
22754
22755 /* While it might be nice to assert sig_type->type == NULL here,
22756 we can get here for DW_AT_imported_declaration where we need
22757 the DIE not the type. */
22758
22759 /* If necessary, add it to the queue and load its DIEs. */
22760
22761 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
22762 language_minimal))
22763 read_signatured_type (sig_type, per_objfile);
22764
22765 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
22766 gdb_assert (sig_cu != NULL);
22767 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22768 temp_die.sect_off = sig_type->type_offset_in_section;
22769 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22770 to_underlying (temp_die.sect_off));
22771 if (die)
22772 {
22773 /* For .gdb_index version 7 keep track of included TUs.
22774 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22775 if (per_objfile->per_bfd->index_table != NULL
22776 && per_objfile->per_bfd->index_table->version <= 7)
22777 {
22778 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22779 }
22780
22781 *ref_cu = sig_cu;
22782 if (sig_cu != cu)
22783 sig_cu->ancestor = cu;
22784
22785 return die;
22786 }
22787
22788 return NULL;
22789 }
22790
22791 /* Follow signatured type referenced by ATTR in SRC_DIE.
22792 On entry *REF_CU is the CU of SRC_DIE.
22793 On exit *REF_CU is the CU of the result.
22794 The result is the DIE of the type.
22795 If the referenced type cannot be found an error is thrown. */
22796
22797 static struct die_info *
22798 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22799 struct dwarf2_cu **ref_cu)
22800 {
22801 ULONGEST signature = DW_SIGNATURE (attr);
22802 struct signatured_type *sig_type;
22803 struct die_info *die;
22804
22805 gdb_assert (attr->form == DW_FORM_ref_sig8);
22806
22807 sig_type = lookup_signatured_type (*ref_cu, signature);
22808 /* sig_type will be NULL if the signatured type is missing from
22809 the debug info. */
22810 if (sig_type == NULL)
22811 {
22812 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22813 " from DIE at %s [in module %s]"),
22814 hex_string (signature), sect_offset_str (src_die->sect_off),
22815 objfile_name ((*ref_cu)->per_objfile->objfile));
22816 }
22817
22818 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22819 if (die == NULL)
22820 {
22821 dump_die_for_error (src_die);
22822 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22823 " from DIE at %s [in module %s]"),
22824 hex_string (signature), sect_offset_str (src_die->sect_off),
22825 objfile_name ((*ref_cu)->per_objfile->objfile));
22826 }
22827
22828 return die;
22829 }
22830
22831 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22832 reading in and processing the type unit if necessary. */
22833
22834 static struct type *
22835 get_signatured_type (struct die_info *die, ULONGEST signature,
22836 struct dwarf2_cu *cu)
22837 {
22838 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22839 struct signatured_type *sig_type;
22840 struct dwarf2_cu *type_cu;
22841 struct die_info *type_die;
22842 struct type *type;
22843
22844 sig_type = lookup_signatured_type (cu, signature);
22845 /* sig_type will be NULL if the signatured type is missing from
22846 the debug info. */
22847 if (sig_type == NULL)
22848 {
22849 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22850 " from DIE at %s [in module %s]"),
22851 hex_string (signature), sect_offset_str (die->sect_off),
22852 objfile_name (per_objfile->objfile));
22853 return build_error_marker_type (cu, die);
22854 }
22855
22856 /* If we already know the type we're done. */
22857 type = per_objfile->get_type_for_signatured_type (sig_type);
22858 if (type != nullptr)
22859 return type;
22860
22861 type_cu = cu;
22862 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22863 if (type_die != NULL)
22864 {
22865 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22866 is created. This is important, for example, because for c++ classes
22867 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22868 type = read_type_die (type_die, type_cu);
22869 if (type == NULL)
22870 {
22871 complaint (_("Dwarf Error: Cannot build signatured type %s"
22872 " referenced from DIE at %s [in module %s]"),
22873 hex_string (signature), sect_offset_str (die->sect_off),
22874 objfile_name (per_objfile->objfile));
22875 type = build_error_marker_type (cu, die);
22876 }
22877 }
22878 else
22879 {
22880 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22881 " from DIE at %s [in module %s]"),
22882 hex_string (signature), sect_offset_str (die->sect_off),
22883 objfile_name (per_objfile->objfile));
22884 type = build_error_marker_type (cu, die);
22885 }
22886
22887 per_objfile->set_type_for_signatured_type (sig_type, type);
22888
22889 return type;
22890 }
22891
22892 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22893 reading in and processing the type unit if necessary. */
22894
22895 static struct type *
22896 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22897 struct dwarf2_cu *cu) /* ARI: editCase function */
22898 {
22899 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22900 if (attr->form_is_ref ())
22901 {
22902 struct dwarf2_cu *type_cu = cu;
22903 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22904
22905 return read_type_die (type_die, type_cu);
22906 }
22907 else if (attr->form == DW_FORM_ref_sig8)
22908 {
22909 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22910 }
22911 else
22912 {
22913 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22914
22915 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22916 " at %s [in module %s]"),
22917 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22918 objfile_name (per_objfile->objfile));
22919 return build_error_marker_type (cu, die);
22920 }
22921 }
22922
22923 /* Load the DIEs associated with type unit PER_CU into memory. */
22924
22925 static void
22926 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22927 dwarf2_per_objfile *per_objfile)
22928 {
22929 struct signatured_type *sig_type;
22930
22931 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22932 gdb_assert (! per_cu->type_unit_group_p ());
22933
22934 /* We have the per_cu, but we need the signatured_type.
22935 Fortunately this is an easy translation. */
22936 gdb_assert (per_cu->is_debug_types);
22937 sig_type = (struct signatured_type *) per_cu;
22938
22939 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22940
22941 read_signatured_type (sig_type, per_objfile);
22942
22943 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22944 }
22945
22946 /* Read in a signatured type and build its CU and DIEs.
22947 If the type is a stub for the real type in a DWO file,
22948 read in the real type from the DWO file as well. */
22949
22950 static void
22951 read_signatured_type (signatured_type *sig_type,
22952 dwarf2_per_objfile *per_objfile)
22953 {
22954 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22955
22956 gdb_assert (per_cu->is_debug_types);
22957 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22958
22959 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
22960
22961 if (!reader.dummy_p)
22962 {
22963 struct dwarf2_cu *cu = reader.cu;
22964 const gdb_byte *info_ptr = reader.info_ptr;
22965
22966 gdb_assert (cu->die_hash == NULL);
22967 cu->die_hash =
22968 htab_create_alloc_ex (cu->header.length / 12,
22969 die_hash,
22970 die_eq,
22971 NULL,
22972 &cu->comp_unit_obstack,
22973 hashtab_obstack_allocate,
22974 dummy_obstack_deallocate);
22975
22976 if (reader.comp_unit_die->has_children)
22977 reader.comp_unit_die->child
22978 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22979 reader.comp_unit_die);
22980 cu->dies = reader.comp_unit_die;
22981 /* comp_unit_die is not stored in die_hash, no need. */
22982
22983 /* We try not to read any attributes in this function, because
22984 not all CUs needed for references have been loaded yet, and
22985 symbol table processing isn't initialized. But we have to
22986 set the CU language, or we won't be able to build types
22987 correctly. Similarly, if we do not read the producer, we can
22988 not apply producer-specific interpretation. */
22989 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22990
22991 reader.keep ();
22992 }
22993
22994 sig_type->per_cu.tu_read = 1;
22995 }
22996
22997 /* Decode simple location descriptions.
22998 Given a pointer to a dwarf block that defines a location, compute
22999 the location and return the value. If COMPUTED is non-null, it is
23000 set to true to indicate that decoding was successful, and false
23001 otherwise. If COMPUTED is null, then this function may emit a
23002 complaint. */
23003
23004 static CORE_ADDR
23005 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23006 {
23007 struct objfile *objfile = cu->per_objfile->objfile;
23008 size_t i;
23009 size_t size = blk->size;
23010 const gdb_byte *data = blk->data;
23011 CORE_ADDR stack[64];
23012 int stacki;
23013 unsigned int bytes_read, unsnd;
23014 gdb_byte op;
23015
23016 if (computed != nullptr)
23017 *computed = false;
23018
23019 i = 0;
23020 stacki = 0;
23021 stack[stacki] = 0;
23022 stack[++stacki] = 0;
23023
23024 while (i < size)
23025 {
23026 op = data[i++];
23027 switch (op)
23028 {
23029 case DW_OP_lit0:
23030 case DW_OP_lit1:
23031 case DW_OP_lit2:
23032 case DW_OP_lit3:
23033 case DW_OP_lit4:
23034 case DW_OP_lit5:
23035 case DW_OP_lit6:
23036 case DW_OP_lit7:
23037 case DW_OP_lit8:
23038 case DW_OP_lit9:
23039 case DW_OP_lit10:
23040 case DW_OP_lit11:
23041 case DW_OP_lit12:
23042 case DW_OP_lit13:
23043 case DW_OP_lit14:
23044 case DW_OP_lit15:
23045 case DW_OP_lit16:
23046 case DW_OP_lit17:
23047 case DW_OP_lit18:
23048 case DW_OP_lit19:
23049 case DW_OP_lit20:
23050 case DW_OP_lit21:
23051 case DW_OP_lit22:
23052 case DW_OP_lit23:
23053 case DW_OP_lit24:
23054 case DW_OP_lit25:
23055 case DW_OP_lit26:
23056 case DW_OP_lit27:
23057 case DW_OP_lit28:
23058 case DW_OP_lit29:
23059 case DW_OP_lit30:
23060 case DW_OP_lit31:
23061 stack[++stacki] = op - DW_OP_lit0;
23062 break;
23063
23064 case DW_OP_reg0:
23065 case DW_OP_reg1:
23066 case DW_OP_reg2:
23067 case DW_OP_reg3:
23068 case DW_OP_reg4:
23069 case DW_OP_reg5:
23070 case DW_OP_reg6:
23071 case DW_OP_reg7:
23072 case DW_OP_reg8:
23073 case DW_OP_reg9:
23074 case DW_OP_reg10:
23075 case DW_OP_reg11:
23076 case DW_OP_reg12:
23077 case DW_OP_reg13:
23078 case DW_OP_reg14:
23079 case DW_OP_reg15:
23080 case DW_OP_reg16:
23081 case DW_OP_reg17:
23082 case DW_OP_reg18:
23083 case DW_OP_reg19:
23084 case DW_OP_reg20:
23085 case DW_OP_reg21:
23086 case DW_OP_reg22:
23087 case DW_OP_reg23:
23088 case DW_OP_reg24:
23089 case DW_OP_reg25:
23090 case DW_OP_reg26:
23091 case DW_OP_reg27:
23092 case DW_OP_reg28:
23093 case DW_OP_reg29:
23094 case DW_OP_reg30:
23095 case DW_OP_reg31:
23096 stack[++stacki] = op - DW_OP_reg0;
23097 if (i < size)
23098 {
23099 if (computed == nullptr)
23100 dwarf2_complex_location_expr_complaint ();
23101 else
23102 return 0;
23103 }
23104 break;
23105
23106 case DW_OP_regx:
23107 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23108 i += bytes_read;
23109 stack[++stacki] = unsnd;
23110 if (i < size)
23111 {
23112 if (computed == nullptr)
23113 dwarf2_complex_location_expr_complaint ();
23114 else
23115 return 0;
23116 }
23117 break;
23118
23119 case DW_OP_addr:
23120 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23121 &bytes_read);
23122 i += bytes_read;
23123 break;
23124
23125 case DW_OP_const1u:
23126 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23127 i += 1;
23128 break;
23129
23130 case DW_OP_const1s:
23131 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23132 i += 1;
23133 break;
23134
23135 case DW_OP_const2u:
23136 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23137 i += 2;
23138 break;
23139
23140 case DW_OP_const2s:
23141 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23142 i += 2;
23143 break;
23144
23145 case DW_OP_const4u:
23146 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23147 i += 4;
23148 break;
23149
23150 case DW_OP_const4s:
23151 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23152 i += 4;
23153 break;
23154
23155 case DW_OP_const8u:
23156 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23157 i += 8;
23158 break;
23159
23160 case DW_OP_constu:
23161 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23162 &bytes_read);
23163 i += bytes_read;
23164 break;
23165
23166 case DW_OP_consts:
23167 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23168 i += bytes_read;
23169 break;
23170
23171 case DW_OP_dup:
23172 stack[stacki + 1] = stack[stacki];
23173 stacki++;
23174 break;
23175
23176 case DW_OP_plus:
23177 stack[stacki - 1] += stack[stacki];
23178 stacki--;
23179 break;
23180
23181 case DW_OP_plus_uconst:
23182 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23183 &bytes_read);
23184 i += bytes_read;
23185 break;
23186
23187 case DW_OP_minus:
23188 stack[stacki - 1] -= stack[stacki];
23189 stacki--;
23190 break;
23191
23192 case DW_OP_deref:
23193 /* If we're not the last op, then we definitely can't encode
23194 this using GDB's address_class enum. This is valid for partial
23195 global symbols, although the variable's address will be bogus
23196 in the psymtab. */
23197 if (i < size)
23198 {
23199 if (computed == nullptr)
23200 dwarf2_complex_location_expr_complaint ();
23201 else
23202 return 0;
23203 }
23204 break;
23205
23206 case DW_OP_GNU_push_tls_address:
23207 case DW_OP_form_tls_address:
23208 /* The top of the stack has the offset from the beginning
23209 of the thread control block at which the variable is located. */
23210 /* Nothing should follow this operator, so the top of stack would
23211 be returned. */
23212 /* This is valid for partial global symbols, but the variable's
23213 address will be bogus in the psymtab. Make it always at least
23214 non-zero to not look as a variable garbage collected by linker
23215 which have DW_OP_addr 0. */
23216 if (i < size)
23217 {
23218 if (computed == nullptr)
23219 dwarf2_complex_location_expr_complaint ();
23220 else
23221 return 0;
23222 }
23223 stack[stacki]++;
23224 break;
23225
23226 case DW_OP_GNU_uninit:
23227 if (computed != nullptr)
23228 return 0;
23229 break;
23230
23231 case DW_OP_addrx:
23232 case DW_OP_GNU_addr_index:
23233 case DW_OP_GNU_const_index:
23234 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23235 &bytes_read);
23236 i += bytes_read;
23237 break;
23238
23239 default:
23240 if (computed == nullptr)
23241 {
23242 const char *name = get_DW_OP_name (op);
23243
23244 if (name)
23245 complaint (_("unsupported stack op: '%s'"),
23246 name);
23247 else
23248 complaint (_("unsupported stack op: '%02x'"),
23249 op);
23250 }
23251
23252 return (stack[stacki]);
23253 }
23254
23255 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23256 outside of the allocated space. Also enforce minimum>0. */
23257 if (stacki >= ARRAY_SIZE (stack) - 1)
23258 {
23259 if (computed == nullptr)
23260 complaint (_("location description stack overflow"));
23261 return 0;
23262 }
23263
23264 if (stacki <= 0)
23265 {
23266 if (computed == nullptr)
23267 complaint (_("location description stack underflow"));
23268 return 0;
23269 }
23270 }
23271
23272 if (computed != nullptr)
23273 *computed = true;
23274 return (stack[stacki]);
23275 }
23276
23277 /* memory allocation interface */
23278
23279 static struct dwarf_block *
23280 dwarf_alloc_block (struct dwarf2_cu *cu)
23281 {
23282 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23283 }
23284
23285 static struct die_info *
23286 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23287 {
23288 struct die_info *die;
23289 size_t size = sizeof (struct die_info);
23290
23291 if (num_attrs > 1)
23292 size += (num_attrs - 1) * sizeof (struct attribute);
23293
23294 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23295 memset (die, 0, sizeof (struct die_info));
23296 return (die);
23297 }
23298
23299 \f
23300
23301 /* Macro support. */
23302
23303 /* An overload of dwarf_decode_macros that finds the correct section
23304 and ensures it is read in before calling the other overload. */
23305
23306 static void
23307 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23308 int section_is_gnu)
23309 {
23310 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23311 struct objfile *objfile = per_objfile->objfile;
23312 const struct line_header *lh = cu->line_header;
23313 unsigned int offset_size = cu->header.offset_size;
23314 struct dwarf2_section_info *section;
23315 const char *section_name;
23316
23317 if (cu->dwo_unit != nullptr)
23318 {
23319 if (section_is_gnu)
23320 {
23321 section = &cu->dwo_unit->dwo_file->sections.macro;
23322 section_name = ".debug_macro.dwo";
23323 }
23324 else
23325 {
23326 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23327 section_name = ".debug_macinfo.dwo";
23328 }
23329 }
23330 else
23331 {
23332 if (section_is_gnu)
23333 {
23334 section = &per_objfile->per_bfd->macro;
23335 section_name = ".debug_macro";
23336 }
23337 else
23338 {
23339 section = &per_objfile->per_bfd->macinfo;
23340 section_name = ".debug_macinfo";
23341 }
23342 }
23343
23344 section->read (objfile);
23345 if (section->buffer == nullptr)
23346 {
23347 complaint (_("missing %s section"), section_name);
23348 return;
23349 }
23350
23351 buildsym_compunit *builder = cu->get_builder ();
23352
23353 dwarf_decode_macros (per_objfile, builder, section, lh,
23354 offset_size, offset, section_is_gnu);
23355 }
23356
23357 /* Return the .debug_loc section to use for CU.
23358 For DWO files use .debug_loc.dwo. */
23359
23360 static struct dwarf2_section_info *
23361 cu_debug_loc_section (struct dwarf2_cu *cu)
23362 {
23363 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23364
23365 if (cu->dwo_unit)
23366 {
23367 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23368
23369 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23370 }
23371 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23372 : &per_objfile->per_bfd->loc);
23373 }
23374
23375 /* A helper function that fills in a dwarf2_loclist_baton. */
23376
23377 static void
23378 fill_in_loclist_baton (struct dwarf2_cu *cu,
23379 struct dwarf2_loclist_baton *baton,
23380 const struct attribute *attr)
23381 {
23382 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23383 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23384
23385 section->read (per_objfile->objfile);
23386
23387 baton->per_objfile = per_objfile;
23388 baton->per_cu = cu->per_cu;
23389 gdb_assert (baton->per_cu);
23390 /* We don't know how long the location list is, but make sure we
23391 don't run off the edge of the section. */
23392 baton->size = section->size - DW_UNSND (attr);
23393 baton->data = section->buffer + DW_UNSND (attr);
23394 if (cu->base_address.has_value ())
23395 baton->base_address = *cu->base_address;
23396 else
23397 baton->base_address = 0;
23398 baton->from_dwo = cu->dwo_unit != NULL;
23399 }
23400
23401 static void
23402 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23403 struct dwarf2_cu *cu, int is_block)
23404 {
23405 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23406 struct objfile *objfile = per_objfile->objfile;
23407 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23408
23409 if (attr->form_is_section_offset ()
23410 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23411 the section. If so, fall through to the complaint in the
23412 other branch. */
23413 && DW_UNSND (attr) < section->get_size (objfile))
23414 {
23415 struct dwarf2_loclist_baton *baton;
23416
23417 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23418
23419 fill_in_loclist_baton (cu, baton, attr);
23420
23421 if (!cu->base_address.has_value ())
23422 complaint (_("Location list used without "
23423 "specifying the CU base address."));
23424
23425 SYMBOL_ACLASS_INDEX (sym) = (is_block
23426 ? dwarf2_loclist_block_index
23427 : dwarf2_loclist_index);
23428 SYMBOL_LOCATION_BATON (sym) = baton;
23429 }
23430 else
23431 {
23432 struct dwarf2_locexpr_baton *baton;
23433
23434 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23435 baton->per_objfile = per_objfile;
23436 baton->per_cu = cu->per_cu;
23437 gdb_assert (baton->per_cu);
23438
23439 if (attr->form_is_block ())
23440 {
23441 /* Note that we're just copying the block's data pointer
23442 here, not the actual data. We're still pointing into the
23443 info_buffer for SYM's objfile; right now we never release
23444 that buffer, but when we do clean up properly this may
23445 need to change. */
23446 baton->size = DW_BLOCK (attr)->size;
23447 baton->data = DW_BLOCK (attr)->data;
23448 }
23449 else
23450 {
23451 dwarf2_invalid_attrib_class_complaint ("location description",
23452 sym->natural_name ());
23453 baton->size = 0;
23454 }
23455
23456 SYMBOL_ACLASS_INDEX (sym) = (is_block
23457 ? dwarf2_locexpr_block_index
23458 : dwarf2_locexpr_index);
23459 SYMBOL_LOCATION_BATON (sym) = baton;
23460 }
23461 }
23462
23463 /* See read.h. */
23464
23465 const comp_unit_head *
23466 dwarf2_per_cu_data::get_header () const
23467 {
23468 if (!m_header_read_in)
23469 {
23470 const gdb_byte *info_ptr
23471 = this->section->buffer + to_underlying (this->sect_off);
23472
23473 memset (&m_header, 0, sizeof (m_header));
23474
23475 read_comp_unit_head (&m_header, info_ptr, this->section,
23476 rcuh_kind::COMPILE);
23477 }
23478
23479 return &m_header;
23480 }
23481
23482 /* See read.h. */
23483
23484 int
23485 dwarf2_per_cu_data::addr_size () const
23486 {
23487 return this->get_header ()->addr_size;
23488 }
23489
23490 /* See read.h. */
23491
23492 int
23493 dwarf2_per_cu_data::offset_size () const
23494 {
23495 return this->get_header ()->offset_size;
23496 }
23497
23498 /* See read.h. */
23499
23500 int
23501 dwarf2_per_cu_data::ref_addr_size () const
23502 {
23503 const comp_unit_head *header = this->get_header ();
23504
23505 if (header->version == 2)
23506 return header->addr_size;
23507 else
23508 return header->offset_size;
23509 }
23510
23511 /* See read.h. */
23512
23513 struct type *
23514 dwarf2_cu::addr_type () const
23515 {
23516 struct objfile *objfile = this->per_objfile->objfile;
23517 struct type *void_type = objfile_type (objfile)->builtin_void;
23518 struct type *addr_type = lookup_pointer_type (void_type);
23519 int addr_size = this->per_cu->addr_size ();
23520
23521 if (TYPE_LENGTH (addr_type) == addr_size)
23522 return addr_type;
23523
23524 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23525 return addr_type;
23526 }
23527
23528 /* A helper function for dwarf2_find_containing_comp_unit that returns
23529 the index of the result, and that searches a vector. It will
23530 return a result even if the offset in question does not actually
23531 occur in any CU. This is separate so that it can be unit
23532 tested. */
23533
23534 static int
23535 dwarf2_find_containing_comp_unit
23536 (sect_offset sect_off,
23537 unsigned int offset_in_dwz,
23538 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23539 {
23540 int low, high;
23541
23542 low = 0;
23543 high = all_comp_units.size () - 1;
23544 while (high > low)
23545 {
23546 struct dwarf2_per_cu_data *mid_cu;
23547 int mid = low + (high - low) / 2;
23548
23549 mid_cu = all_comp_units[mid];
23550 if (mid_cu->is_dwz > offset_in_dwz
23551 || (mid_cu->is_dwz == offset_in_dwz
23552 && mid_cu->sect_off + mid_cu->length > sect_off))
23553 high = mid;
23554 else
23555 low = mid + 1;
23556 }
23557 gdb_assert (low == high);
23558 return low;
23559 }
23560
23561 /* Locate the .debug_info compilation unit from CU's objfile which contains
23562 the DIE at OFFSET. Raises an error on failure. */
23563
23564 static struct dwarf2_per_cu_data *
23565 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23566 unsigned int offset_in_dwz,
23567 dwarf2_per_objfile *per_objfile)
23568 {
23569 int low = dwarf2_find_containing_comp_unit
23570 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
23571 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
23572
23573 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23574 {
23575 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23576 error (_("Dwarf Error: could not find partial DIE containing "
23577 "offset %s [in module %s]"),
23578 sect_offset_str (sect_off),
23579 bfd_get_filename (per_objfile->objfile->obfd));
23580
23581 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23582 <= sect_off);
23583 return per_objfile->per_bfd->all_comp_units[low-1];
23584 }
23585 else
23586 {
23587 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
23588 && sect_off >= this_cu->sect_off + this_cu->length)
23589 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23590 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23591 return this_cu;
23592 }
23593 }
23594
23595 #if GDB_SELF_TEST
23596
23597 namespace selftests {
23598 namespace find_containing_comp_unit {
23599
23600 static void
23601 run_test ()
23602 {
23603 struct dwarf2_per_cu_data one {};
23604 struct dwarf2_per_cu_data two {};
23605 struct dwarf2_per_cu_data three {};
23606 struct dwarf2_per_cu_data four {};
23607
23608 one.length = 5;
23609 two.sect_off = sect_offset (one.length);
23610 two.length = 7;
23611
23612 three.length = 5;
23613 three.is_dwz = 1;
23614 four.sect_off = sect_offset (three.length);
23615 four.length = 7;
23616 four.is_dwz = 1;
23617
23618 std::vector<dwarf2_per_cu_data *> units;
23619 units.push_back (&one);
23620 units.push_back (&two);
23621 units.push_back (&three);
23622 units.push_back (&four);
23623
23624 int result;
23625
23626 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23627 SELF_CHECK (units[result] == &one);
23628 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23629 SELF_CHECK (units[result] == &one);
23630 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23631 SELF_CHECK (units[result] == &two);
23632
23633 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23634 SELF_CHECK (units[result] == &three);
23635 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23636 SELF_CHECK (units[result] == &three);
23637 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23638 SELF_CHECK (units[result] == &four);
23639 }
23640
23641 }
23642 }
23643
23644 #endif /* GDB_SELF_TEST */
23645
23646 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23647
23648 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23649 dwarf2_per_objfile *per_objfile)
23650 : per_cu (per_cu),
23651 per_objfile (per_objfile),
23652 mark (false),
23653 has_loclist (false),
23654 checked_producer (false),
23655 producer_is_gxx_lt_4_6 (false),
23656 producer_is_gcc_lt_4_3 (false),
23657 producer_is_icc (false),
23658 producer_is_icc_lt_14 (false),
23659 producer_is_codewarrior (false),
23660 processing_has_namespace_info (false)
23661 {
23662 }
23663
23664 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23665
23666 static void
23667 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23668 enum language pretend_language)
23669 {
23670 struct attribute *attr;
23671
23672 /* Set the language we're debugging. */
23673 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23674 if (attr != nullptr)
23675 set_cu_language (DW_UNSND (attr), cu);
23676 else
23677 {
23678 cu->language = pretend_language;
23679 cu->language_defn = language_def (cu->language);
23680 }
23681
23682 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23683 }
23684
23685 /* See read.h. */
23686
23687 dwarf2_cu *
23688 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23689 {
23690 auto it = m_dwarf2_cus.find (per_cu);
23691 if (it == m_dwarf2_cus.end ())
23692 return nullptr;
23693
23694 return it->second;
23695 }
23696
23697 /* See read.h. */
23698
23699 void
23700 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23701 {
23702 gdb_assert (this->get_cu (per_cu) == nullptr);
23703
23704 m_dwarf2_cus[per_cu] = cu;
23705 }
23706
23707 /* See read.h. */
23708
23709 void
23710 dwarf2_per_objfile::age_comp_units ()
23711 {
23712 /* Start by clearing all marks. */
23713 for (auto pair : m_dwarf2_cus)
23714 pair.second->mark = false;
23715
23716 /* Traverse all CUs, mark them and their dependencies if used recently
23717 enough. */
23718 for (auto pair : m_dwarf2_cus)
23719 {
23720 dwarf2_cu *cu = pair.second;
23721
23722 cu->last_used++;
23723 if (cu->last_used <= dwarf_max_cache_age)
23724 dwarf2_mark (cu);
23725 }
23726
23727 /* Delete all CUs still not marked. */
23728 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23729 {
23730 dwarf2_cu *cu = it->second;
23731
23732 if (!cu->mark)
23733 {
23734 delete cu;
23735 it = m_dwarf2_cus.erase (it);
23736 }
23737 else
23738 it++;
23739 }
23740 }
23741
23742 /* See read.h. */
23743
23744 void
23745 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23746 {
23747 auto it = m_dwarf2_cus.find (per_cu);
23748 if (it == m_dwarf2_cus.end ())
23749 return;
23750
23751 delete it->second;
23752
23753 m_dwarf2_cus.erase (it);
23754 }
23755
23756 dwarf2_per_objfile::~dwarf2_per_objfile ()
23757 {
23758 remove_all_cus ();
23759 }
23760
23761 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23762 We store these in a hash table separate from the DIEs, and preserve them
23763 when the DIEs are flushed out of cache.
23764
23765 The CU "per_cu" pointer is needed because offset alone is not enough to
23766 uniquely identify the type. A file may have multiple .debug_types sections,
23767 or the type may come from a DWO file. Furthermore, while it's more logical
23768 to use per_cu->section+offset, with Fission the section with the data is in
23769 the DWO file but we don't know that section at the point we need it.
23770 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23771 because we can enter the lookup routine, get_die_type_at_offset, from
23772 outside this file, and thus won't necessarily have PER_CU->cu.
23773 Fortunately, PER_CU is stable for the life of the objfile. */
23774
23775 struct dwarf2_per_cu_offset_and_type
23776 {
23777 const struct dwarf2_per_cu_data *per_cu;
23778 sect_offset sect_off;
23779 struct type *type;
23780 };
23781
23782 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23783
23784 static hashval_t
23785 per_cu_offset_and_type_hash (const void *item)
23786 {
23787 const struct dwarf2_per_cu_offset_and_type *ofs
23788 = (const struct dwarf2_per_cu_offset_and_type *) item;
23789
23790 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23791 }
23792
23793 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23794
23795 static int
23796 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23797 {
23798 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23799 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23800 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23801 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23802
23803 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23804 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23805 }
23806
23807 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23808 table if necessary. For convenience, return TYPE.
23809
23810 The DIEs reading must have careful ordering to:
23811 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23812 reading current DIE.
23813 * Not trying to dereference contents of still incompletely read in types
23814 while reading in other DIEs.
23815 * Enable referencing still incompletely read in types just by a pointer to
23816 the type without accessing its fields.
23817
23818 Therefore caller should follow these rules:
23819 * Try to fetch any prerequisite types we may need to build this DIE type
23820 before building the type and calling set_die_type.
23821 * After building type call set_die_type for current DIE as soon as
23822 possible before fetching more types to complete the current type.
23823 * Make the type as complete as possible before fetching more types. */
23824
23825 static struct type *
23826 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23827 {
23828 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23829 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23830 struct objfile *objfile = per_objfile->objfile;
23831 struct attribute *attr;
23832 struct dynamic_prop prop;
23833
23834 /* For Ada types, make sure that the gnat-specific data is always
23835 initialized (if not already set). There are a few types where
23836 we should not be doing so, because the type-specific area is
23837 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23838 where the type-specific area is used to store the floatformat).
23839 But this is not a problem, because the gnat-specific information
23840 is actually not needed for these types. */
23841 if (need_gnat_info (cu)
23842 && type->code () != TYPE_CODE_FUNC
23843 && type->code () != TYPE_CODE_FLT
23844 && type->code () != TYPE_CODE_METHODPTR
23845 && type->code () != TYPE_CODE_MEMBERPTR
23846 && type->code () != TYPE_CODE_METHOD
23847 && !HAVE_GNAT_AUX_INFO (type))
23848 INIT_GNAT_SPECIFIC (type);
23849
23850 /* Read DW_AT_allocated and set in type. */
23851 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23852 if (attr != NULL)
23853 {
23854 struct type *prop_type = cu->addr_sized_int_type (false);
23855 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23856 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23857 }
23858
23859 /* Read DW_AT_associated and set in type. */
23860 attr = dwarf2_attr (die, DW_AT_associated, cu);
23861 if (attr != NULL)
23862 {
23863 struct type *prop_type = cu->addr_sized_int_type (false);
23864 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23865 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23866 }
23867
23868 /* Read DW_AT_data_location and set in type. */
23869 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23870 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23871 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23872
23873 if (per_objfile->die_type_hash == NULL)
23874 per_objfile->die_type_hash
23875 = htab_up (htab_create_alloc (127,
23876 per_cu_offset_and_type_hash,
23877 per_cu_offset_and_type_eq,
23878 NULL, xcalloc, xfree));
23879
23880 ofs.per_cu = cu->per_cu;
23881 ofs.sect_off = die->sect_off;
23882 ofs.type = type;
23883 slot = (struct dwarf2_per_cu_offset_and_type **)
23884 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
23885 if (*slot)
23886 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23887 sect_offset_str (die->sect_off));
23888 *slot = XOBNEW (&objfile->objfile_obstack,
23889 struct dwarf2_per_cu_offset_and_type);
23890 **slot = ofs;
23891 return type;
23892 }
23893
23894 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23895 or return NULL if the die does not have a saved type. */
23896
23897 static struct type *
23898 get_die_type_at_offset (sect_offset sect_off,
23899 dwarf2_per_cu_data *per_cu,
23900 dwarf2_per_objfile *per_objfile)
23901 {
23902 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23903
23904 if (per_objfile->die_type_hash == NULL)
23905 return NULL;
23906
23907 ofs.per_cu = per_cu;
23908 ofs.sect_off = sect_off;
23909 slot = ((struct dwarf2_per_cu_offset_and_type *)
23910 htab_find (per_objfile->die_type_hash.get (), &ofs));
23911 if (slot)
23912 return slot->type;
23913 else
23914 return NULL;
23915 }
23916
23917 /* Look up the type for DIE in CU in die_type_hash,
23918 or return NULL if DIE does not have a saved type. */
23919
23920 static struct type *
23921 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23922 {
23923 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23924 }
23925
23926 /* Add a dependence relationship from CU to REF_PER_CU. */
23927
23928 static void
23929 dwarf2_add_dependence (struct dwarf2_cu *cu,
23930 struct dwarf2_per_cu_data *ref_per_cu)
23931 {
23932 void **slot;
23933
23934 if (cu->dependencies == NULL)
23935 cu->dependencies
23936 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23937 NULL, &cu->comp_unit_obstack,
23938 hashtab_obstack_allocate,
23939 dummy_obstack_deallocate);
23940
23941 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23942 if (*slot == NULL)
23943 *slot = ref_per_cu;
23944 }
23945
23946 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23947 Set the mark field in every compilation unit in the
23948 cache that we must keep because we are keeping CU.
23949
23950 DATA is the dwarf2_per_objfile object in which to look up CUs. */
23951
23952 static int
23953 dwarf2_mark_helper (void **slot, void *data)
23954 {
23955 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23956 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23957 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23958
23959 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23960 reading of the chain. As such dependencies remain valid it is not much
23961 useful to track and undo them during QUIT cleanups. */
23962 if (cu == nullptr)
23963 return 1;
23964
23965 if (cu->mark)
23966 return 1;
23967
23968 cu->mark = true;
23969
23970 if (cu->dependencies != nullptr)
23971 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
23972
23973 return 1;
23974 }
23975
23976 /* Set the mark field in CU and in every other compilation unit in the
23977 cache that we must keep because we are keeping CU. */
23978
23979 static void
23980 dwarf2_mark (struct dwarf2_cu *cu)
23981 {
23982 if (cu->mark)
23983 return;
23984
23985 cu->mark = true;
23986
23987 if (cu->dependencies != nullptr)
23988 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
23989 }
23990
23991 /* Trivial hash function for partial_die_info: the hash value of a DIE
23992 is its offset in .debug_info for this objfile. */
23993
23994 static hashval_t
23995 partial_die_hash (const void *item)
23996 {
23997 const struct partial_die_info *part_die
23998 = (const struct partial_die_info *) item;
23999
24000 return to_underlying (part_die->sect_off);
24001 }
24002
24003 /* Trivial comparison function for partial_die_info structures: two DIEs
24004 are equal if they have the same offset. */
24005
24006 static int
24007 partial_die_eq (const void *item_lhs, const void *item_rhs)
24008 {
24009 const struct partial_die_info *part_die_lhs
24010 = (const struct partial_die_info *) item_lhs;
24011 const struct partial_die_info *part_die_rhs
24012 = (const struct partial_die_info *) item_rhs;
24013
24014 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24015 }
24016
24017 struct cmd_list_element *set_dwarf_cmdlist;
24018 struct cmd_list_element *show_dwarf_cmdlist;
24019
24020 static void
24021 show_check_physname (struct ui_file *file, int from_tty,
24022 struct cmd_list_element *c, const char *value)
24023 {
24024 fprintf_filtered (file,
24025 _("Whether to check \"physname\" is %s.\n"),
24026 value);
24027 }
24028
24029 void _initialize_dwarf2_read ();
24030 void
24031 _initialize_dwarf2_read ()
24032 {
24033 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24034 Set DWARF specific variables.\n\
24035 Configure DWARF variables such as the cache size."),
24036 &set_dwarf_cmdlist, "maintenance set dwarf ",
24037 0/*allow-unknown*/, &maintenance_set_cmdlist);
24038
24039 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24040 Show DWARF specific variables.\n\
24041 Show DWARF variables such as the cache size."),
24042 &show_dwarf_cmdlist, "maintenance show dwarf ",
24043 0/*allow-unknown*/, &maintenance_show_cmdlist);
24044
24045 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24046 &dwarf_max_cache_age, _("\
24047 Set the upper bound on the age of cached DWARF compilation units."), _("\
24048 Show the upper bound on the age of cached DWARF compilation units."), _("\
24049 A higher limit means that cached compilation units will be stored\n\
24050 in memory longer, and more total memory will be used. Zero disables\n\
24051 caching, which can slow down startup."),
24052 NULL,
24053 show_dwarf_max_cache_age,
24054 &set_dwarf_cmdlist,
24055 &show_dwarf_cmdlist);
24056
24057 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24058 Set debugging of the DWARF reader."), _("\
24059 Show debugging of the DWARF reader."), _("\
24060 When enabled (non-zero), debugging messages are printed during DWARF\n\
24061 reading and symtab expansion. A value of 1 (one) provides basic\n\
24062 information. A value greater than 1 provides more verbose information."),
24063 NULL,
24064 NULL,
24065 &setdebuglist, &showdebuglist);
24066
24067 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24068 Set debugging of the DWARF DIE reader."), _("\
24069 Show debugging of the DWARF DIE reader."), _("\
24070 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24071 The value is the maximum depth to print."),
24072 NULL,
24073 NULL,
24074 &setdebuglist, &showdebuglist);
24075
24076 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24077 Set debugging of the dwarf line reader."), _("\
24078 Show debugging of the dwarf line reader."), _("\
24079 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24080 A value of 1 (one) provides basic information.\n\
24081 A value greater than 1 provides more verbose information."),
24082 NULL,
24083 NULL,
24084 &setdebuglist, &showdebuglist);
24085
24086 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24087 Set cross-checking of \"physname\" code against demangler."), _("\
24088 Show cross-checking of \"physname\" code against demangler."), _("\
24089 When enabled, GDB's internal \"physname\" code is checked against\n\
24090 the demangler."),
24091 NULL, show_check_physname,
24092 &setdebuglist, &showdebuglist);
24093
24094 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24095 no_class, &use_deprecated_index_sections, _("\
24096 Set whether to use deprecated gdb_index sections."), _("\
24097 Show whether to use deprecated gdb_index sections."), _("\
24098 When enabled, deprecated .gdb_index sections are used anyway.\n\
24099 Normally they are ignored either because of a missing feature or\n\
24100 performance issue.\n\
24101 Warning: This option must be enabled before gdb reads the file."),
24102 NULL,
24103 NULL,
24104 &setlist, &showlist);
24105
24106 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24107 &dwarf2_locexpr_funcs);
24108 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24109 &dwarf2_loclist_funcs);
24110
24111 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24112 &dwarf2_block_frame_base_locexpr_funcs);
24113 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24114 &dwarf2_block_frame_base_loclist_funcs);
24115
24116 #if GDB_SELF_TEST
24117 selftests::register_test ("dw2_expand_symtabs_matching",
24118 selftests::dw2_expand_symtabs_matching::run_test);
24119 selftests::register_test ("dwarf2_find_containing_comp_unit",
24120 selftests::find_containing_comp_unit::run_test);
24121 #endif
24122 }
This page took 0.489308 seconds and 5 git commands to generate.