Share DWARF partial symtabs
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 /* This is used to store the data that is always per objfile. */
109 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
110
111 /* These are used to store the dwarf2_per_bfd objects.
112
113 objfiles having the same BFD, which doesn't require relocations, are going to
114 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
115
116 Other objfiles are not going to share a dwarf2_per_bfd with any other
117 objfiles, so they'll have their own version kept in the _objfile_data_key
118 version. */
119 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
120 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
121
122 /* The "aclass" indices for various kinds of computed DWARF symbols. */
123
124 static int dwarf2_locexpr_index;
125 static int dwarf2_loclist_index;
126 static int dwarf2_locexpr_block_index;
127 static int dwarf2_loclist_block_index;
128
129 /* Size of .debug_loclists section header for 32-bit DWARF format. */
130 #define LOCLIST_HEADER_SIZE32 12
131
132 /* Size of .debug_loclists section header for 64-bit DWARF format. */
133 #define LOCLIST_HEADER_SIZE64 20
134
135 /* An index into a (C++) symbol name component in a symbol name as
136 recorded in the mapped_index's symbol table. For each C++ symbol
137 in the symbol table, we record one entry for the start of each
138 component in the symbol in a table of name components, and then
139 sort the table, in order to be able to binary search symbol names,
140 ignoring leading namespaces, both completion and regular look up.
141 For example, for symbol "A::B::C", we'll have an entry that points
142 to "A::B::C", another that points to "B::C", and another for "C".
143 Note that function symbols in GDB index have no parameter
144 information, just the function/method names. You can convert a
145 name_component to a "const char *" using the
146 'mapped_index::symbol_name_at(offset_type)' method. */
147
148 struct name_component
149 {
150 /* Offset in the symbol name where the component starts. Stored as
151 a (32-bit) offset instead of a pointer to save memory and improve
152 locality on 64-bit architectures. */
153 offset_type name_offset;
154
155 /* The symbol's index in the symbol and constant pool tables of a
156 mapped_index. */
157 offset_type idx;
158 };
159
160 /* Base class containing bits shared by both .gdb_index and
161 .debug_name indexes. */
162
163 struct mapped_index_base
164 {
165 mapped_index_base () = default;
166 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
167
168 /* The name_component table (a sorted vector). See name_component's
169 description above. */
170 std::vector<name_component> name_components;
171
172 /* How NAME_COMPONENTS is sorted. */
173 enum case_sensitivity name_components_casing;
174
175 /* Return the number of names in the symbol table. */
176 virtual size_t symbol_name_count () const = 0;
177
178 /* Get the name of the symbol at IDX in the symbol table. */
179 virtual const char *symbol_name_at
180 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
181
182 /* Return whether the name at IDX in the symbol table should be
183 ignored. */
184 virtual bool symbol_name_slot_invalid (offset_type idx) const
185 {
186 return false;
187 }
188
189 /* Build the symbol name component sorted vector, if we haven't
190 yet. */
191 void build_name_components (dwarf2_per_objfile *per_objfile);
192
193 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
194 possible matches for LN_NO_PARAMS in the name component
195 vector. */
196 std::pair<std::vector<name_component>::const_iterator,
197 std::vector<name_component>::const_iterator>
198 find_name_components_bounds (const lookup_name_info &ln_no_params,
199 enum language lang,
200 dwarf2_per_objfile *per_objfile) const;
201
202 /* Prevent deleting/destroying via a base class pointer. */
203 protected:
204 ~mapped_index_base() = default;
205 };
206
207 /* A description of the mapped index. The file format is described in
208 a comment by the code that writes the index. */
209 struct mapped_index final : public mapped_index_base
210 {
211 /* A slot/bucket in the symbol table hash. */
212 struct symbol_table_slot
213 {
214 const offset_type name;
215 const offset_type vec;
216 };
217
218 /* Index data format version. */
219 int version = 0;
220
221 /* The address table data. */
222 gdb::array_view<const gdb_byte> address_table;
223
224 /* The symbol table, implemented as a hash table. */
225 gdb::array_view<symbol_table_slot> symbol_table;
226
227 /* A pointer to the constant pool. */
228 const char *constant_pool = nullptr;
229
230 bool symbol_name_slot_invalid (offset_type idx) const override
231 {
232 const auto &bucket = this->symbol_table[idx];
233 return bucket.name == 0 && bucket.vec == 0;
234 }
235
236 /* Convenience method to get at the name of the symbol at IDX in the
237 symbol table. */
238 const char *symbol_name_at
239 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
240 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
241
242 size_t symbol_name_count () const override
243 { return this->symbol_table.size (); }
244 };
245
246 /* A description of the mapped .debug_names.
247 Uninitialized map has CU_COUNT 0. */
248 struct mapped_debug_names final : public mapped_index_base
249 {
250 bfd_endian dwarf5_byte_order;
251 bool dwarf5_is_dwarf64;
252 bool augmentation_is_gdb;
253 uint8_t offset_size;
254 uint32_t cu_count = 0;
255 uint32_t tu_count, bucket_count, name_count;
256 const gdb_byte *cu_table_reordered, *tu_table_reordered;
257 const uint32_t *bucket_table_reordered, *hash_table_reordered;
258 const gdb_byte *name_table_string_offs_reordered;
259 const gdb_byte *name_table_entry_offs_reordered;
260 const gdb_byte *entry_pool;
261
262 struct index_val
263 {
264 ULONGEST dwarf_tag;
265 struct attr
266 {
267 /* Attribute name DW_IDX_*. */
268 ULONGEST dw_idx;
269
270 /* Attribute form DW_FORM_*. */
271 ULONGEST form;
272
273 /* Value if FORM is DW_FORM_implicit_const. */
274 LONGEST implicit_const;
275 };
276 std::vector<attr> attr_vec;
277 };
278
279 std::unordered_map<ULONGEST, index_val> abbrev_map;
280
281 const char *namei_to_name
282 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
283
284 /* Implementation of the mapped_index_base virtual interface, for
285 the name_components cache. */
286
287 const char *symbol_name_at
288 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
289 { return namei_to_name (idx, per_objfile); }
290
291 size_t symbol_name_count () const override
292 { return this->name_count; }
293 };
294
295 /* See dwarf2read.h. */
296
297 dwarf2_per_objfile *
298 get_dwarf2_per_objfile (struct objfile *objfile)
299 {
300 return dwarf2_objfile_data_key.get (objfile);
301 }
302
303 /* Default names of the debugging sections. */
304
305 /* Note that if the debugging section has been compressed, it might
306 have a name like .zdebug_info. */
307
308 static const struct dwarf2_debug_sections dwarf2_elf_names =
309 {
310 { ".debug_info", ".zdebug_info" },
311 { ".debug_abbrev", ".zdebug_abbrev" },
312 { ".debug_line", ".zdebug_line" },
313 { ".debug_loc", ".zdebug_loc" },
314 { ".debug_loclists", ".zdebug_loclists" },
315 { ".debug_macinfo", ".zdebug_macinfo" },
316 { ".debug_macro", ".zdebug_macro" },
317 { ".debug_str", ".zdebug_str" },
318 { ".debug_str_offsets", ".zdebug_str_offsets" },
319 { ".debug_line_str", ".zdebug_line_str" },
320 { ".debug_ranges", ".zdebug_ranges" },
321 { ".debug_rnglists", ".zdebug_rnglists" },
322 { ".debug_types", ".zdebug_types" },
323 { ".debug_addr", ".zdebug_addr" },
324 { ".debug_frame", ".zdebug_frame" },
325 { ".eh_frame", NULL },
326 { ".gdb_index", ".zgdb_index" },
327 { ".debug_names", ".zdebug_names" },
328 { ".debug_aranges", ".zdebug_aranges" },
329 23
330 };
331
332 /* List of DWO/DWP sections. */
333
334 static const struct dwop_section_names
335 {
336 struct dwarf2_section_names abbrev_dwo;
337 struct dwarf2_section_names info_dwo;
338 struct dwarf2_section_names line_dwo;
339 struct dwarf2_section_names loc_dwo;
340 struct dwarf2_section_names loclists_dwo;
341 struct dwarf2_section_names macinfo_dwo;
342 struct dwarf2_section_names macro_dwo;
343 struct dwarf2_section_names str_dwo;
344 struct dwarf2_section_names str_offsets_dwo;
345 struct dwarf2_section_names types_dwo;
346 struct dwarf2_section_names cu_index;
347 struct dwarf2_section_names tu_index;
348 }
349 dwop_section_names =
350 {
351 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
352 { ".debug_info.dwo", ".zdebug_info.dwo" },
353 { ".debug_line.dwo", ".zdebug_line.dwo" },
354 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
355 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
356 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
357 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
358 { ".debug_str.dwo", ".zdebug_str.dwo" },
359 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
360 { ".debug_types.dwo", ".zdebug_types.dwo" },
361 { ".debug_cu_index", ".zdebug_cu_index" },
362 { ".debug_tu_index", ".zdebug_tu_index" },
363 };
364
365 /* local data types */
366
367 /* The location list section (.debug_loclists) begins with a header,
368 which contains the following information. */
369 struct loclist_header
370 {
371 /* A 4-byte or 12-byte length containing the length of the
372 set of entries for this compilation unit, not including the
373 length field itself. */
374 unsigned int length;
375
376 /* A 2-byte version identifier. */
377 short version;
378
379 /* A 1-byte unsigned integer containing the size in bytes of an address on
380 the target system. */
381 unsigned char addr_size;
382
383 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
384 on the target system. */
385 unsigned char segment_collector_size;
386
387 /* A 4-byte count of the number of offsets that follow the header. */
388 unsigned int offset_entry_count;
389 };
390
391 /* Type used for delaying computation of method physnames.
392 See comments for compute_delayed_physnames. */
393 struct delayed_method_info
394 {
395 /* The type to which the method is attached, i.e., its parent class. */
396 struct type *type;
397
398 /* The index of the method in the type's function fieldlists. */
399 int fnfield_index;
400
401 /* The index of the method in the fieldlist. */
402 int index;
403
404 /* The name of the DIE. */
405 const char *name;
406
407 /* The DIE associated with this method. */
408 struct die_info *die;
409 };
410
411 /* Internal state when decoding a particular compilation unit. */
412 struct dwarf2_cu
413 {
414 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
415 dwarf2_per_objfile *per_objfile);
416
417 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
418
419 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
420 Create the set of symtabs used by this TU, or if this TU is sharing
421 symtabs with another TU and the symtabs have already been created
422 then restore those symtabs in the line header.
423 We don't need the pc/line-number mapping for type units. */
424 void setup_type_unit_groups (struct die_info *die);
425
426 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
427 buildsym_compunit constructor. */
428 struct compunit_symtab *start_symtab (const char *name,
429 const char *comp_dir,
430 CORE_ADDR low_pc);
431
432 /* Reset the builder. */
433 void reset_builder () { m_builder.reset (); }
434
435 /* Return a type that is a generic pointer type, the size of which
436 matches the address size given in the compilation unit header for
437 this CU. */
438 struct type *addr_type () const;
439
440 /* Find an integer type the same size as the address size given in
441 the compilation unit header for this CU. UNSIGNED_P controls if
442 the integer is unsigned or not. */
443 struct type *addr_sized_int_type (bool unsigned_p) const;
444
445 /* The header of the compilation unit. */
446 struct comp_unit_head header {};
447
448 /* Base address of this compilation unit. */
449 gdb::optional<CORE_ADDR> base_address;
450
451 /* The language we are debugging. */
452 enum language language = language_unknown;
453 const struct language_defn *language_defn = nullptr;
454
455 const char *producer = nullptr;
456
457 private:
458 /* The symtab builder for this CU. This is only non-NULL when full
459 symbols are being read. */
460 std::unique_ptr<buildsym_compunit> m_builder;
461
462 public:
463 /* The generic symbol table building routines have separate lists for
464 file scope symbols and all all other scopes (local scopes). So
465 we need to select the right one to pass to add_symbol_to_list().
466 We do it by keeping a pointer to the correct list in list_in_scope.
467
468 FIXME: The original dwarf code just treated the file scope as the
469 first local scope, and all other local scopes as nested local
470 scopes, and worked fine. Check to see if we really need to
471 distinguish these in buildsym.c. */
472 struct pending **list_in_scope = nullptr;
473
474 /* Hash table holding all the loaded partial DIEs
475 with partial_die->offset.SECT_OFF as hash. */
476 htab_t partial_dies = nullptr;
477
478 /* Storage for things with the same lifetime as this read-in compilation
479 unit, including partial DIEs. */
480 auto_obstack comp_unit_obstack;
481
482 /* Backlink to our per_cu entry. */
483 struct dwarf2_per_cu_data *per_cu;
484
485 /* The dwarf2_per_objfile that owns this. */
486 struct dwarf2_per_objfile *per_objfile;
487
488 /* How many compilation units ago was this CU last referenced? */
489 int last_used = 0;
490
491 /* A hash table of DIE cu_offset for following references with
492 die_info->offset.sect_off as hash. */
493 htab_t die_hash = nullptr;
494
495 /* Full DIEs if read in. */
496 struct die_info *dies = nullptr;
497
498 /* A set of pointers to dwarf2_per_cu_data objects for compilation
499 units referenced by this one. Only set during full symbol processing;
500 partial symbol tables do not have dependencies. */
501 htab_t dependencies = nullptr;
502
503 /* Header data from the line table, during full symbol processing. */
504 struct line_header *line_header = nullptr;
505 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
506 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
507 this is the DW_TAG_compile_unit die for this CU. We'll hold on
508 to the line header as long as this DIE is being processed. See
509 process_die_scope. */
510 die_info *line_header_die_owner = nullptr;
511
512 /* A list of methods which need to have physnames computed
513 after all type information has been read. */
514 std::vector<delayed_method_info> method_list;
515
516 /* To be copied to symtab->call_site_htab. */
517 htab_t call_site_htab = nullptr;
518
519 /* Non-NULL if this CU came from a DWO file.
520 There is an invariant here that is important to remember:
521 Except for attributes copied from the top level DIE in the "main"
522 (or "stub") file in preparation for reading the DWO file
523 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
524 Either there isn't a DWO file (in which case this is NULL and the point
525 is moot), or there is and either we're not going to read it (in which
526 case this is NULL) or there is and we are reading it (in which case this
527 is non-NULL). */
528 struct dwo_unit *dwo_unit = nullptr;
529
530 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
531 Note this value comes from the Fission stub CU/TU's DIE. */
532 gdb::optional<ULONGEST> addr_base;
533
534 /* The DW_AT_rnglists_base attribute if present.
535 Note this value comes from the Fission stub CU/TU's DIE.
536 Also note that the value is zero in the non-DWO case so this value can
537 be used without needing to know whether DWO files are in use or not.
538 N.B. This does not apply to DW_AT_ranges appearing in
539 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
540 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
541 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
542 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
543 ULONGEST ranges_base = 0;
544
545 /* The DW_AT_loclists_base attribute if present. */
546 ULONGEST loclist_base = 0;
547
548 /* When reading debug info generated by older versions of rustc, we
549 have to rewrite some union types to be struct types with a
550 variant part. This rewriting must be done after the CU is fully
551 read in, because otherwise at the point of rewriting some struct
552 type might not have been fully processed. So, we keep a list of
553 all such types here and process them after expansion. */
554 std::vector<struct type *> rust_unions;
555
556 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
557 files, the value is implicitly zero. For DWARF 5 version DWO files, the
558 value is often implicit and is the size of the header of
559 .debug_str_offsets section (8 or 4, depending on the address size). */
560 gdb::optional<ULONGEST> str_offsets_base;
561
562 /* Mark used when releasing cached dies. */
563 bool mark : 1;
564
565 /* This CU references .debug_loc. See the symtab->locations_valid field.
566 This test is imperfect as there may exist optimized debug code not using
567 any location list and still facing inlining issues if handled as
568 unoptimized code. For a future better test see GCC PR other/32998. */
569 bool has_loclist : 1;
570
571 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
572 if all the producer_is_* fields are valid. This information is cached
573 because profiling CU expansion showed excessive time spent in
574 producer_is_gxx_lt_4_6. */
575 bool checked_producer : 1;
576 bool producer_is_gxx_lt_4_6 : 1;
577 bool producer_is_gcc_lt_4_3 : 1;
578 bool producer_is_icc : 1;
579 bool producer_is_icc_lt_14 : 1;
580 bool producer_is_codewarrior : 1;
581
582 /* When true, the file that we're processing is known to have
583 debugging info for C++ namespaces. GCC 3.3.x did not produce
584 this information, but later versions do. */
585
586 bool processing_has_namespace_info : 1;
587
588 struct partial_die_info *find_partial_die (sect_offset sect_off);
589
590 /* If this CU was inherited by another CU (via specification,
591 abstract_origin, etc), this is the ancestor CU. */
592 dwarf2_cu *ancestor;
593
594 /* Get the buildsym_compunit for this CU. */
595 buildsym_compunit *get_builder ()
596 {
597 /* If this CU has a builder associated with it, use that. */
598 if (m_builder != nullptr)
599 return m_builder.get ();
600
601 /* Otherwise, search ancestors for a valid builder. */
602 if (ancestor != nullptr)
603 return ancestor->get_builder ();
604
605 return nullptr;
606 }
607 };
608
609 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
610 This includes type_unit_group and quick_file_names. */
611
612 struct stmt_list_hash
613 {
614 /* The DWO unit this table is from or NULL if there is none. */
615 struct dwo_unit *dwo_unit;
616
617 /* Offset in .debug_line or .debug_line.dwo. */
618 sect_offset line_sect_off;
619 };
620
621 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
622 an object of this type. This contains elements of type unit groups
623 that can be shared across objfiles. The non-shareable parts are in
624 type_unit_group_unshareable. */
625
626 struct type_unit_group
627 {
628 /* dwarf2read.c's main "handle" on a TU symtab.
629 To simplify things we create an artificial CU that "includes" all the
630 type units using this stmt_list so that the rest of the code still has
631 a "per_cu" handle on the symtab. */
632 struct dwarf2_per_cu_data per_cu;
633
634 /* The TUs that share this DW_AT_stmt_list entry.
635 This is added to while parsing type units to build partial symtabs,
636 and is deleted afterwards and not used again. */
637 std::vector<signatured_type *> *tus;
638
639 /* The data used to construct the hash key. */
640 struct stmt_list_hash hash;
641 };
642
643 /* These sections are what may appear in a (real or virtual) DWO file. */
644
645 struct dwo_sections
646 {
647 struct dwarf2_section_info abbrev;
648 struct dwarf2_section_info line;
649 struct dwarf2_section_info loc;
650 struct dwarf2_section_info loclists;
651 struct dwarf2_section_info macinfo;
652 struct dwarf2_section_info macro;
653 struct dwarf2_section_info str;
654 struct dwarf2_section_info str_offsets;
655 /* In the case of a virtual DWO file, these two are unused. */
656 struct dwarf2_section_info info;
657 std::vector<dwarf2_section_info> types;
658 };
659
660 /* CUs/TUs in DWP/DWO files. */
661
662 struct dwo_unit
663 {
664 /* Backlink to the containing struct dwo_file. */
665 struct dwo_file *dwo_file;
666
667 /* The "id" that distinguishes this CU/TU.
668 .debug_info calls this "dwo_id", .debug_types calls this "signature".
669 Since signatures came first, we stick with it for consistency. */
670 ULONGEST signature;
671
672 /* The section this CU/TU lives in, in the DWO file. */
673 struct dwarf2_section_info *section;
674
675 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
676 sect_offset sect_off;
677 unsigned int length;
678
679 /* For types, offset in the type's DIE of the type defined by this TU. */
680 cu_offset type_offset_in_tu;
681 };
682
683 /* include/dwarf2.h defines the DWP section codes.
684 It defines a max value but it doesn't define a min value, which we
685 use for error checking, so provide one. */
686
687 enum dwp_v2_section_ids
688 {
689 DW_SECT_MIN = 1
690 };
691
692 /* Data for one DWO file.
693
694 This includes virtual DWO files (a virtual DWO file is a DWO file as it
695 appears in a DWP file). DWP files don't really have DWO files per se -
696 comdat folding of types "loses" the DWO file they came from, and from
697 a high level view DWP files appear to contain a mass of random types.
698 However, to maintain consistency with the non-DWP case we pretend DWP
699 files contain virtual DWO files, and we assign each TU with one virtual
700 DWO file (generally based on the line and abbrev section offsets -
701 a heuristic that seems to work in practice). */
702
703 struct dwo_file
704 {
705 dwo_file () = default;
706 DISABLE_COPY_AND_ASSIGN (dwo_file);
707
708 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
709 For virtual DWO files the name is constructed from the section offsets
710 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
711 from related CU+TUs. */
712 const char *dwo_name = nullptr;
713
714 /* The DW_AT_comp_dir attribute. */
715 const char *comp_dir = nullptr;
716
717 /* The bfd, when the file is open. Otherwise this is NULL.
718 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
719 gdb_bfd_ref_ptr dbfd;
720
721 /* The sections that make up this DWO file.
722 Remember that for virtual DWO files in DWP V2, these are virtual
723 sections (for lack of a better name). */
724 struct dwo_sections sections {};
725
726 /* The CUs in the file.
727 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
728 an extension to handle LLVM's Link Time Optimization output (where
729 multiple source files may be compiled into a single object/dwo pair). */
730 htab_up cus;
731
732 /* Table of TUs in the file.
733 Each element is a struct dwo_unit. */
734 htab_up tus;
735 };
736
737 /* These sections are what may appear in a DWP file. */
738
739 struct dwp_sections
740 {
741 /* These are used by both DWP version 1 and 2. */
742 struct dwarf2_section_info str;
743 struct dwarf2_section_info cu_index;
744 struct dwarf2_section_info tu_index;
745
746 /* These are only used by DWP version 2 files.
747 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
748 sections are referenced by section number, and are not recorded here.
749 In DWP version 2 there is at most one copy of all these sections, each
750 section being (effectively) comprised of the concatenation of all of the
751 individual sections that exist in the version 1 format.
752 To keep the code simple we treat each of these concatenated pieces as a
753 section itself (a virtual section?). */
754 struct dwarf2_section_info abbrev;
755 struct dwarf2_section_info info;
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
760 struct dwarf2_section_info str_offsets;
761 struct dwarf2_section_info types;
762 };
763
764 /* These sections are what may appear in a virtual DWO file in DWP version 1.
765 A virtual DWO file is a DWO file as it appears in a DWP file. */
766
767 struct virtual_v1_dwo_sections
768 {
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
776 That is recorded here, and copied to dwo_unit.section. */
777 struct dwarf2_section_info info_or_types;
778 };
779
780 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
781 In version 2, the sections of the DWO files are concatenated together
782 and stored in one section of that name. Thus each ELF section contains
783 several "virtual" sections. */
784
785 struct virtual_v2_dwo_sections
786 {
787 bfd_size_type abbrev_offset;
788 bfd_size_type abbrev_size;
789
790 bfd_size_type line_offset;
791 bfd_size_type line_size;
792
793 bfd_size_type loc_offset;
794 bfd_size_type loc_size;
795
796 bfd_size_type macinfo_offset;
797 bfd_size_type macinfo_size;
798
799 bfd_size_type macro_offset;
800 bfd_size_type macro_size;
801
802 bfd_size_type str_offsets_offset;
803 bfd_size_type str_offsets_size;
804
805 /* Each DWP hash table entry records one CU or one TU.
806 That is recorded here, and copied to dwo_unit.section. */
807 bfd_size_type info_or_types_offset;
808 bfd_size_type info_or_types_size;
809 };
810
811 /* Contents of DWP hash tables. */
812
813 struct dwp_hash_table
814 {
815 uint32_t version, nr_columns;
816 uint32_t nr_units, nr_slots;
817 const gdb_byte *hash_table, *unit_table;
818 union
819 {
820 struct
821 {
822 const gdb_byte *indices;
823 } v1;
824 struct
825 {
826 /* This is indexed by column number and gives the id of the section
827 in that column. */
828 #define MAX_NR_V2_DWO_SECTIONS \
829 (1 /* .debug_info or .debug_types */ \
830 + 1 /* .debug_abbrev */ \
831 + 1 /* .debug_line */ \
832 + 1 /* .debug_loc */ \
833 + 1 /* .debug_str_offsets */ \
834 + 1 /* .debug_macro or .debug_macinfo */)
835 int section_ids[MAX_NR_V2_DWO_SECTIONS];
836 const gdb_byte *offsets;
837 const gdb_byte *sizes;
838 } v2;
839 } section_pool;
840 };
841
842 /* Data for one DWP file. */
843
844 struct dwp_file
845 {
846 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
847 : name (name_),
848 dbfd (std::move (abfd))
849 {
850 }
851
852 /* Name of the file. */
853 const char *name;
854
855 /* File format version. */
856 int version = 0;
857
858 /* The bfd. */
859 gdb_bfd_ref_ptr dbfd;
860
861 /* Section info for this file. */
862 struct dwp_sections sections {};
863
864 /* Table of CUs in the file. */
865 const struct dwp_hash_table *cus = nullptr;
866
867 /* Table of TUs in the file. */
868 const struct dwp_hash_table *tus = nullptr;
869
870 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
871 htab_up loaded_cus;
872 htab_up loaded_tus;
873
874 /* Table to map ELF section numbers to their sections.
875 This is only needed for the DWP V1 file format. */
876 unsigned int num_sections = 0;
877 asection **elf_sections = nullptr;
878 };
879
880 /* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
883 struct exists to abstract away the constant parameters of die reading. */
884
885 struct die_reader_specs
886 {
887 /* The bfd of die_section. */
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
894 struct dwo_file *dwo_file;
895
896 /* The section the die comes from.
897 This is either .debug_info or .debug_types, or the .dwo variants. */
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
901 const gdb_byte *buffer;
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
905
906 /* The abbreviation table to use when reading the DIEs. */
907 struct abbrev_table *abbrev_table;
908 };
909
910 /* A subclass of die_reader_specs that holds storage and has complex
911 constructor and destructor behavior. */
912
913 class cutu_reader : public die_reader_specs
914 {
915 public:
916
917 cutu_reader (dwarf2_per_cu_data *this_cu,
918 dwarf2_per_objfile *per_objfile,
919 struct abbrev_table *abbrev_table,
920 dwarf2_cu *existing_cu,
921 bool skip_partial);
922
923 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
924 dwarf2_per_objfile *per_objfile,
925 struct dwarf2_cu *parent_cu = nullptr,
926 struct dwo_file *dwo_file = nullptr);
927
928 DISABLE_COPY_AND_ASSIGN (cutu_reader);
929
930 const gdb_byte *info_ptr = nullptr;
931 struct die_info *comp_unit_die = nullptr;
932 bool dummy_p = false;
933
934 /* Release the new CU, putting it on the chain. This cannot be done
935 for dummy CUs. */
936 void keep ();
937
938 private:
939 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
940 dwarf2_per_objfile *per_objfile,
941 dwarf2_cu *existing_cu);
942
943 struct dwarf2_per_cu_data *m_this_cu;
944 std::unique_ptr<dwarf2_cu> m_new_cu;
945
946 /* The ordinary abbreviation table. */
947 abbrev_table_up m_abbrev_table_holder;
948
949 /* The DWO abbreviation table. */
950 abbrev_table_up m_dwo_abbrev_table;
951 };
952
953 /* When we construct a partial symbol table entry we only
954 need this much information. */
955 struct partial_die_info : public allocate_on_obstack
956 {
957 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
958
959 /* Disable assign but still keep copy ctor, which is needed
960 load_partial_dies. */
961 partial_die_info& operator=(const partial_die_info& rhs) = delete;
962
963 /* Adjust the partial die before generating a symbol for it. This
964 function may set the is_external flag or change the DIE's
965 name. */
966 void fixup (struct dwarf2_cu *cu);
967
968 /* Read a minimal amount of information into the minimal die
969 structure. */
970 const gdb_byte *read (const struct die_reader_specs *reader,
971 const struct abbrev_info &abbrev,
972 const gdb_byte *info_ptr);
973
974 /* Offset of this DIE. */
975 const sect_offset sect_off;
976
977 /* DWARF-2 tag for this DIE. */
978 const ENUM_BITFIELD(dwarf_tag) tag : 16;
979
980 /* Assorted flags describing the data found in this DIE. */
981 const unsigned int has_children : 1;
982
983 unsigned int is_external : 1;
984 unsigned int is_declaration : 1;
985 unsigned int has_type : 1;
986 unsigned int has_specification : 1;
987 unsigned int has_pc_info : 1;
988 unsigned int may_be_inlined : 1;
989
990 /* This DIE has been marked DW_AT_main_subprogram. */
991 unsigned int main_subprogram : 1;
992
993 /* Flag set if the SCOPE field of this structure has been
994 computed. */
995 unsigned int scope_set : 1;
996
997 /* Flag set if the DIE has a byte_size attribute. */
998 unsigned int has_byte_size : 1;
999
1000 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1001 unsigned int has_const_value : 1;
1002
1003 /* Flag set if any of the DIE's children are template arguments. */
1004 unsigned int has_template_arguments : 1;
1005
1006 /* Flag set if fixup has been called on this die. */
1007 unsigned int fixup_called : 1;
1008
1009 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1010 unsigned int is_dwz : 1;
1011
1012 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1013 unsigned int spec_is_dwz : 1;
1014
1015 /* The name of this DIE. Normally the value of DW_AT_name, but
1016 sometimes a default name for unnamed DIEs. */
1017 const char *name = nullptr;
1018
1019 /* The linkage name, if present. */
1020 const char *linkage_name = nullptr;
1021
1022 /* The scope to prepend to our children. This is generally
1023 allocated on the comp_unit_obstack, so will disappear
1024 when this compilation unit leaves the cache. */
1025 const char *scope = nullptr;
1026
1027 /* Some data associated with the partial DIE. The tag determines
1028 which field is live. */
1029 union
1030 {
1031 /* The location description associated with this DIE, if any. */
1032 struct dwarf_block *locdesc;
1033 /* The offset of an import, for DW_TAG_imported_unit. */
1034 sect_offset sect_off;
1035 } d {};
1036
1037 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1038 CORE_ADDR lowpc = 0;
1039 CORE_ADDR highpc = 0;
1040
1041 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1042 DW_AT_sibling, if any. */
1043 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1044 could return DW_AT_sibling values to its caller load_partial_dies. */
1045 const gdb_byte *sibling = nullptr;
1046
1047 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1048 DW_AT_specification (or DW_AT_abstract_origin or
1049 DW_AT_extension). */
1050 sect_offset spec_offset {};
1051
1052 /* Pointers to this DIE's parent, first child, and next sibling,
1053 if any. */
1054 struct partial_die_info *die_parent = nullptr;
1055 struct partial_die_info *die_child = nullptr;
1056 struct partial_die_info *die_sibling = nullptr;
1057
1058 friend struct partial_die_info *
1059 dwarf2_cu::find_partial_die (sect_offset sect_off);
1060
1061 private:
1062 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1063 partial_die_info (sect_offset sect_off)
1064 : partial_die_info (sect_off, DW_TAG_padding, 0)
1065 {
1066 }
1067
1068 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1069 int has_children_)
1070 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1071 {
1072 is_external = 0;
1073 is_declaration = 0;
1074 has_type = 0;
1075 has_specification = 0;
1076 has_pc_info = 0;
1077 may_be_inlined = 0;
1078 main_subprogram = 0;
1079 scope_set = 0;
1080 has_byte_size = 0;
1081 has_const_value = 0;
1082 has_template_arguments = 0;
1083 fixup_called = 0;
1084 is_dwz = 0;
1085 spec_is_dwz = 0;
1086 }
1087 };
1088
1089 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1090 but this would require a corresponding change in unpack_field_as_long
1091 and friends. */
1092 static int bits_per_byte = 8;
1093
1094 struct variant_part_builder;
1095
1096 /* When reading a variant, we track a bit more information about the
1097 field, and store it in an object of this type. */
1098
1099 struct variant_field
1100 {
1101 int first_field = -1;
1102 int last_field = -1;
1103
1104 /* A variant can contain other variant parts. */
1105 std::vector<variant_part_builder> variant_parts;
1106
1107 /* If we see a DW_TAG_variant, then this will be set if this is the
1108 default branch. */
1109 bool default_branch = false;
1110 /* If we see a DW_AT_discr_value, then this will be the discriminant
1111 value. */
1112 ULONGEST discriminant_value = 0;
1113 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1114 data. */
1115 struct dwarf_block *discr_list_data = nullptr;
1116 };
1117
1118 /* This represents a DW_TAG_variant_part. */
1119
1120 struct variant_part_builder
1121 {
1122 /* The offset of the discriminant field. */
1123 sect_offset discriminant_offset {};
1124
1125 /* Variants that are direct children of this variant part. */
1126 std::vector<variant_field> variants;
1127
1128 /* True if we're currently reading a variant. */
1129 bool processing_variant = false;
1130 };
1131
1132 struct nextfield
1133 {
1134 int accessibility = 0;
1135 int virtuality = 0;
1136 /* Variant parts need to find the discriminant, which is a DIE
1137 reference. We track the section offset of each field to make
1138 this link. */
1139 sect_offset offset;
1140 struct field field {};
1141 };
1142
1143 struct fnfieldlist
1144 {
1145 const char *name = nullptr;
1146 std::vector<struct fn_field> fnfields;
1147 };
1148
1149 /* The routines that read and process dies for a C struct or C++ class
1150 pass lists of data member fields and lists of member function fields
1151 in an instance of a field_info structure, as defined below. */
1152 struct field_info
1153 {
1154 /* List of data member and baseclasses fields. */
1155 std::vector<struct nextfield> fields;
1156 std::vector<struct nextfield> baseclasses;
1157
1158 /* Set if the accessibility of one of the fields is not public. */
1159 int non_public_fields = 0;
1160
1161 /* Member function fieldlist array, contains name of possibly overloaded
1162 member function, number of overloaded member functions and a pointer
1163 to the head of the member function field chain. */
1164 std::vector<struct fnfieldlist> fnfieldlists;
1165
1166 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1167 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1168 std::vector<struct decl_field> typedef_field_list;
1169
1170 /* Nested types defined by this class and the number of elements in this
1171 list. */
1172 std::vector<struct decl_field> nested_types_list;
1173
1174 /* If non-null, this is the variant part we are currently
1175 reading. */
1176 variant_part_builder *current_variant_part = nullptr;
1177 /* This holds all the top-level variant parts attached to the type
1178 we're reading. */
1179 std::vector<variant_part_builder> variant_parts;
1180
1181 /* Return the total number of fields (including baseclasses). */
1182 int nfields () const
1183 {
1184 return fields.size () + baseclasses.size ();
1185 }
1186 };
1187
1188 /* Loaded secondary compilation units are kept in memory until they
1189 have not been referenced for the processing of this many
1190 compilation units. Set this to zero to disable caching. Cache
1191 sizes of up to at least twenty will improve startup time for
1192 typical inter-CU-reference binaries, at an obvious memory cost. */
1193 static int dwarf_max_cache_age = 5;
1194 static void
1195 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1196 struct cmd_list_element *c, const char *value)
1197 {
1198 fprintf_filtered (file, _("The upper bound on the age of cached "
1199 "DWARF compilation units is %s.\n"),
1200 value);
1201 }
1202 \f
1203 /* local function prototypes */
1204
1205 static void dwarf2_find_base_address (struct die_info *die,
1206 struct dwarf2_cu *cu);
1207
1208 static dwarf2_psymtab *create_partial_symtab
1209 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1210 const char *name);
1211
1212 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1213 const gdb_byte *info_ptr,
1214 struct die_info *type_unit_die);
1215
1216 static void dwarf2_build_psymtabs_hard
1217 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1218
1219 static void scan_partial_symbols (struct partial_die_info *,
1220 CORE_ADDR *, CORE_ADDR *,
1221 int, struct dwarf2_cu *);
1222
1223 static void add_partial_symbol (struct partial_die_info *,
1224 struct dwarf2_cu *);
1225
1226 static void add_partial_namespace (struct partial_die_info *pdi,
1227 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1228 int set_addrmap, struct dwarf2_cu *cu);
1229
1230 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1231 CORE_ADDR *highpc, int set_addrmap,
1232 struct dwarf2_cu *cu);
1233
1234 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1235 struct dwarf2_cu *cu);
1236
1237 static void add_partial_subprogram (struct partial_die_info *pdi,
1238 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1239 int need_pc, struct dwarf2_cu *cu);
1240
1241 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1242
1243 static struct partial_die_info *load_partial_dies
1244 (const struct die_reader_specs *, const gdb_byte *, int);
1245
1246 /* A pair of partial_die_info and compilation unit. */
1247 struct cu_partial_die_info
1248 {
1249 /* The compilation unit of the partial_die_info. */
1250 struct dwarf2_cu *cu;
1251 /* A partial_die_info. */
1252 struct partial_die_info *pdi;
1253
1254 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1255 : cu (cu),
1256 pdi (pdi)
1257 { /* Nothing. */ }
1258
1259 private:
1260 cu_partial_die_info () = delete;
1261 };
1262
1263 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1264 struct dwarf2_cu *);
1265
1266 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1267 struct attribute *, struct attr_abbrev *,
1268 const gdb_byte *, bool *need_reprocess);
1269
1270 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1271 struct attribute *attr);
1272
1273 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1274
1275 static sect_offset read_abbrev_offset
1276 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1277 struct dwarf2_section_info *, sect_offset);
1278
1279 static const char *read_indirect_string
1280 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1281 const struct comp_unit_head *, unsigned int *);
1282
1283 static const char *read_indirect_string_at_offset
1284 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1285
1286 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1287 const gdb_byte *,
1288 unsigned int *);
1289
1290 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1291 ULONGEST str_index);
1292
1293 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1294 ULONGEST str_index);
1295
1296 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1297
1298 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1299 struct dwarf2_cu *);
1300
1301 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1302 struct dwarf2_cu *cu);
1303
1304 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1305
1306 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1307 struct dwarf2_cu *cu);
1308
1309 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1310
1311 static struct die_info *die_specification (struct die_info *die,
1312 struct dwarf2_cu **);
1313
1314 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1315 struct dwarf2_cu *cu);
1316
1317 static void dwarf_decode_lines (struct line_header *, const char *,
1318 struct dwarf2_cu *, dwarf2_psymtab *,
1319 CORE_ADDR, int decode_mapping);
1320
1321 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1322 const char *);
1323
1324 static struct symbol *new_symbol (struct die_info *, struct type *,
1325 struct dwarf2_cu *, struct symbol * = NULL);
1326
1327 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1328 struct dwarf2_cu *);
1329
1330 static void dwarf2_const_value_attr (const struct attribute *attr,
1331 struct type *type,
1332 const char *name,
1333 struct obstack *obstack,
1334 struct dwarf2_cu *cu, LONGEST *value,
1335 const gdb_byte **bytes,
1336 struct dwarf2_locexpr_baton **baton);
1337
1338 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1339
1340 static int need_gnat_info (struct dwarf2_cu *);
1341
1342 static struct type *die_descriptive_type (struct die_info *,
1343 struct dwarf2_cu *);
1344
1345 static void set_descriptive_type (struct type *, struct die_info *,
1346 struct dwarf2_cu *);
1347
1348 static struct type *die_containing_type (struct die_info *,
1349 struct dwarf2_cu *);
1350
1351 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1352 struct dwarf2_cu *);
1353
1354 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1355
1356 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1357
1358 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1359
1360 static char *typename_concat (struct obstack *obs, const char *prefix,
1361 const char *suffix, int physname,
1362 struct dwarf2_cu *cu);
1363
1364 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1365
1366 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1367
1368 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1369
1370 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1371
1372 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1373
1374 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1375
1376 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1377 struct dwarf2_cu *, dwarf2_psymtab *);
1378
1379 /* Return the .debug_loclists section to use for cu. */
1380 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1381
1382 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1383 values. Keep the items ordered with increasing constraints compliance. */
1384 enum pc_bounds_kind
1385 {
1386 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1387 PC_BOUNDS_NOT_PRESENT,
1388
1389 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1390 were present but they do not form a valid range of PC addresses. */
1391 PC_BOUNDS_INVALID,
1392
1393 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1394 PC_BOUNDS_RANGES,
1395
1396 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1397 PC_BOUNDS_HIGH_LOW,
1398 };
1399
1400 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1401 CORE_ADDR *, CORE_ADDR *,
1402 struct dwarf2_cu *,
1403 dwarf2_psymtab *);
1404
1405 static void get_scope_pc_bounds (struct die_info *,
1406 CORE_ADDR *, CORE_ADDR *,
1407 struct dwarf2_cu *);
1408
1409 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1410 CORE_ADDR, struct dwarf2_cu *);
1411
1412 static void dwarf2_add_field (struct field_info *, struct die_info *,
1413 struct dwarf2_cu *);
1414
1415 static void dwarf2_attach_fields_to_type (struct field_info *,
1416 struct type *, struct dwarf2_cu *);
1417
1418 static void dwarf2_add_member_fn (struct field_info *,
1419 struct die_info *, struct type *,
1420 struct dwarf2_cu *);
1421
1422 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1423 struct type *,
1424 struct dwarf2_cu *);
1425
1426 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1427
1428 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1429
1430 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1431
1432 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1433
1434 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1435
1436 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1437
1438 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1439
1440 static struct type *read_module_type (struct die_info *die,
1441 struct dwarf2_cu *cu);
1442
1443 static const char *namespace_name (struct die_info *die,
1444 int *is_anonymous, struct dwarf2_cu *);
1445
1446 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1447
1448 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1449 bool * = nullptr);
1450
1451 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1452 struct dwarf2_cu *);
1453
1454 static struct die_info *read_die_and_siblings_1
1455 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1456 struct die_info *);
1457
1458 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1459 const gdb_byte *info_ptr,
1460 const gdb_byte **new_info_ptr,
1461 struct die_info *parent);
1462
1463 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1464 struct die_info **, const gdb_byte *,
1465 int);
1466
1467 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1468 struct die_info **, const gdb_byte *);
1469
1470 static void process_die (struct die_info *, struct dwarf2_cu *);
1471
1472 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1473 struct objfile *);
1474
1475 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1476
1477 static const char *dwarf2_full_name (const char *name,
1478 struct die_info *die,
1479 struct dwarf2_cu *cu);
1480
1481 static const char *dwarf2_physname (const char *name, struct die_info *die,
1482 struct dwarf2_cu *cu);
1483
1484 static struct die_info *dwarf2_extension (struct die_info *die,
1485 struct dwarf2_cu **);
1486
1487 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1488
1489 static void dump_die_for_error (struct die_info *);
1490
1491 static void dump_die_1 (struct ui_file *, int level, int max_level,
1492 struct die_info *);
1493
1494 /*static*/ void dump_die (struct die_info *, int max_level);
1495
1496 static void store_in_ref_table (struct die_info *,
1497 struct dwarf2_cu *);
1498
1499 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1500 const struct attribute *,
1501 struct dwarf2_cu **);
1502
1503 static struct die_info *follow_die_ref (struct die_info *,
1504 const struct attribute *,
1505 struct dwarf2_cu **);
1506
1507 static struct die_info *follow_die_sig (struct die_info *,
1508 const struct attribute *,
1509 struct dwarf2_cu **);
1510
1511 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1512 struct dwarf2_cu *);
1513
1514 static struct type *get_DW_AT_signature_type (struct die_info *,
1515 const struct attribute *,
1516 struct dwarf2_cu *);
1517
1518 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1519 dwarf2_per_objfile *per_objfile);
1520
1521 static void read_signatured_type (signatured_type *sig_type,
1522 dwarf2_per_objfile *per_objfile);
1523
1524 static int attr_to_dynamic_prop (const struct attribute *attr,
1525 struct die_info *die, struct dwarf2_cu *cu,
1526 struct dynamic_prop *prop, struct type *type);
1527
1528 /* memory allocation interface */
1529
1530 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1531
1532 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1533
1534 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1535
1536 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1537 struct dwarf2_loclist_baton *baton,
1538 const struct attribute *attr);
1539
1540 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1541 struct symbol *sym,
1542 struct dwarf2_cu *cu,
1543 int is_block);
1544
1545 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1546 const gdb_byte *info_ptr,
1547 struct abbrev_info *abbrev);
1548
1549 static hashval_t partial_die_hash (const void *item);
1550
1551 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1552
1553 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1554 (sect_offset sect_off, unsigned int offset_in_dwz,
1555 struct dwarf2_per_objfile *dwarf2_per_objfile);
1556
1557 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1558 struct die_info *comp_unit_die,
1559 enum language pretend_language);
1560
1561 static struct type *set_die_type (struct die_info *, struct type *,
1562 struct dwarf2_cu *);
1563
1564 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1565
1566 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1567
1568 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1569 dwarf2_per_objfile *per_objfile,
1570 bool skip_partial,
1571 enum language pretend_language);
1572
1573 static void process_full_comp_unit (dwarf2_cu *cu,
1574 enum language pretend_language);
1575
1576 static void process_full_type_unit (dwarf2_cu *cu,
1577 enum language pretend_language);
1578
1579 static void dwarf2_add_dependence (struct dwarf2_cu *,
1580 struct dwarf2_per_cu_data *);
1581
1582 static void dwarf2_mark (struct dwarf2_cu *);
1583
1584 static struct type *get_die_type_at_offset (sect_offset,
1585 dwarf2_per_cu_data *per_cu,
1586 dwarf2_per_objfile *per_objfile);
1587
1588 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1589
1590 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1591 dwarf2_per_objfile *per_objfile,
1592 enum language pretend_language);
1593
1594 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1595
1596 /* Class, the destructor of which frees all allocated queue entries. This
1597 will only have work to do if an error was thrown while processing the
1598 dwarf. If no error was thrown then the queue entries should have all
1599 been processed, and freed, as we went along. */
1600
1601 class dwarf2_queue_guard
1602 {
1603 public:
1604 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1605 : m_per_objfile (per_objfile)
1606 {
1607 }
1608
1609 /* Free any entries remaining on the queue. There should only be
1610 entries left if we hit an error while processing the dwarf. */
1611 ~dwarf2_queue_guard ()
1612 {
1613 /* Ensure that no memory is allocated by the queue. */
1614 std::queue<dwarf2_queue_item> empty;
1615 std::swap (m_per_objfile->per_bfd->queue, empty);
1616 }
1617
1618 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1619
1620 private:
1621 dwarf2_per_objfile *m_per_objfile;
1622 };
1623
1624 dwarf2_queue_item::~dwarf2_queue_item ()
1625 {
1626 /* Anything still marked queued is likely to be in an
1627 inconsistent state, so discard it. */
1628 if (per_cu->queued)
1629 {
1630 per_objfile->remove_cu (per_cu);
1631 per_cu->queued = 0;
1632 }
1633 }
1634
1635 /* The return type of find_file_and_directory. Note, the enclosed
1636 string pointers are only valid while this object is valid. */
1637
1638 struct file_and_directory
1639 {
1640 /* The filename. This is never NULL. */
1641 const char *name;
1642
1643 /* The compilation directory. NULL if not known. If we needed to
1644 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1645 points directly to the DW_AT_comp_dir string attribute owned by
1646 the obstack that owns the DIE. */
1647 const char *comp_dir;
1648
1649 /* If we needed to build a new string for comp_dir, this is what
1650 owns the storage. */
1651 std::string comp_dir_storage;
1652 };
1653
1654 static file_and_directory find_file_and_directory (struct die_info *die,
1655 struct dwarf2_cu *cu);
1656
1657 static htab_up allocate_signatured_type_table ();
1658
1659 static htab_up allocate_dwo_unit_table ();
1660
1661 static struct dwo_unit *lookup_dwo_unit_in_dwp
1662 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1663 struct dwp_file *dwp_file, const char *comp_dir,
1664 ULONGEST signature, int is_debug_types);
1665
1666 static struct dwp_file *get_dwp_file
1667 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1668
1669 static struct dwo_unit *lookup_dwo_comp_unit
1670 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1671 ULONGEST signature);
1672
1673 static struct dwo_unit *lookup_dwo_type_unit
1674 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1675
1676 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1677
1678 /* A unique pointer to a dwo_file. */
1679
1680 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1681
1682 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1683
1684 static void check_producer (struct dwarf2_cu *cu);
1685
1686 static void free_line_header_voidp (void *arg);
1687 \f
1688 /* Various complaints about symbol reading that don't abort the process. */
1689
1690 static void
1691 dwarf2_debug_line_missing_file_complaint (void)
1692 {
1693 complaint (_(".debug_line section has line data without a file"));
1694 }
1695
1696 static void
1697 dwarf2_debug_line_missing_end_sequence_complaint (void)
1698 {
1699 complaint (_(".debug_line section has line "
1700 "program sequence without an end"));
1701 }
1702
1703 static void
1704 dwarf2_complex_location_expr_complaint (void)
1705 {
1706 complaint (_("location expression too complex"));
1707 }
1708
1709 static void
1710 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1711 int arg3)
1712 {
1713 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1714 arg1, arg2, arg3);
1715 }
1716
1717 static void
1718 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1719 {
1720 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1721 arg1, arg2);
1722 }
1723
1724 /* Hash function for line_header_hash. */
1725
1726 static hashval_t
1727 line_header_hash (const struct line_header *ofs)
1728 {
1729 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1730 }
1731
1732 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1733
1734 static hashval_t
1735 line_header_hash_voidp (const void *item)
1736 {
1737 const struct line_header *ofs = (const struct line_header *) item;
1738
1739 return line_header_hash (ofs);
1740 }
1741
1742 /* Equality function for line_header_hash. */
1743
1744 static int
1745 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1746 {
1747 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1748 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1749
1750 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1751 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1752 }
1753
1754 \f
1755
1756 /* See declaration. */
1757
1758 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1759 bool can_copy_)
1760 : obfd (obfd),
1761 can_copy (can_copy_)
1762 {
1763 if (names == NULL)
1764 names = &dwarf2_elf_names;
1765
1766 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1767 locate_sections (obfd, sec, *names);
1768 }
1769
1770 dwarf2_per_bfd::~dwarf2_per_bfd ()
1771 {
1772 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1773 per_cu->imported_symtabs_free ();
1774
1775 for (signatured_type *sig_type : all_type_units)
1776 sig_type->per_cu.imported_symtabs_free ();
1777
1778 /* Everything else should be on this->obstack. */
1779 }
1780
1781 /* See read.h. */
1782
1783 void
1784 dwarf2_per_objfile::remove_all_cus ()
1785 {
1786 for (auto pair : m_dwarf2_cus)
1787 delete pair.second;
1788
1789 m_dwarf2_cus.clear ();
1790 }
1791
1792 /* A helper class that calls free_cached_comp_units on
1793 destruction. */
1794
1795 class free_cached_comp_units
1796 {
1797 public:
1798
1799 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1800 : m_per_objfile (per_objfile)
1801 {
1802 }
1803
1804 ~free_cached_comp_units ()
1805 {
1806 m_per_objfile->remove_all_cus ();
1807 }
1808
1809 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1810
1811 private:
1812
1813 dwarf2_per_objfile *m_per_objfile;
1814 };
1815
1816 /* See read.h. */
1817
1818 bool
1819 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1820 {
1821 gdb_assert (per_cu->index < this->m_symtabs.size ());
1822
1823 return this->m_symtabs[per_cu->index] != nullptr;
1824 }
1825
1826 /* See read.h. */
1827
1828 compunit_symtab *
1829 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1830 {
1831 gdb_assert (per_cu->index < this->m_symtabs.size ());
1832
1833 return this->m_symtabs[per_cu->index];
1834 }
1835
1836 /* See read.h. */
1837
1838 void
1839 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1840 compunit_symtab *symtab)
1841 {
1842 gdb_assert (per_cu->index < this->m_symtabs.size ());
1843 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1844
1845 this->m_symtabs[per_cu->index] = symtab;
1846 }
1847
1848 /* Try to locate the sections we need for DWARF 2 debugging
1849 information and return true if we have enough to do something.
1850 NAMES points to the dwarf2 section names, or is NULL if the standard
1851 ELF names are used. CAN_COPY is true for formats where symbol
1852 interposition is possible and so symbol values must follow copy
1853 relocation rules. */
1854
1855 int
1856 dwarf2_has_info (struct objfile *objfile,
1857 const struct dwarf2_debug_sections *names,
1858 bool can_copy)
1859 {
1860 if (objfile->flags & OBJF_READNEVER)
1861 return 0;
1862
1863 struct dwarf2_per_objfile *dwarf2_per_objfile
1864 = get_dwarf2_per_objfile (objfile);
1865
1866 if (dwarf2_per_objfile == NULL)
1867 {
1868 dwarf2_per_bfd *per_bfd;
1869
1870 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1871 doesn't require relocations and if there aren't partial symbols
1872 from some other reader. */
1873 if (!objfile_has_partial_symbols (objfile)
1874 && !gdb_bfd_requires_relocations (objfile->obfd))
1875 {
1876 /* See if one has been created for this BFD yet. */
1877 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1878
1879 if (per_bfd == nullptr)
1880 {
1881 /* No, create it now. */
1882 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1883 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1884 }
1885 }
1886 else
1887 {
1888 /* No sharing possible, create one specifically for this objfile. */
1889 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1890 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1891 }
1892
1893 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1894 }
1895
1896 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1897 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1898 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1899 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1900 }
1901
1902 /* When loading sections, we look either for uncompressed section or for
1903 compressed section names. */
1904
1905 static int
1906 section_is_p (const char *section_name,
1907 const struct dwarf2_section_names *names)
1908 {
1909 if (names->normal != NULL
1910 && strcmp (section_name, names->normal) == 0)
1911 return 1;
1912 if (names->compressed != NULL
1913 && strcmp (section_name, names->compressed) == 0)
1914 return 1;
1915 return 0;
1916 }
1917
1918 /* See declaration. */
1919
1920 void
1921 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1922 const dwarf2_debug_sections &names)
1923 {
1924 flagword aflag = bfd_section_flags (sectp);
1925
1926 if ((aflag & SEC_HAS_CONTENTS) == 0)
1927 {
1928 }
1929 else if (elf_section_data (sectp)->this_hdr.sh_size
1930 > bfd_get_file_size (abfd))
1931 {
1932 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1933 warning (_("Discarding section %s which has a section size (%s"
1934 ") larger than the file size [in module %s]"),
1935 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1936 bfd_get_filename (abfd));
1937 }
1938 else if (section_is_p (sectp->name, &names.info))
1939 {
1940 this->info.s.section = sectp;
1941 this->info.size = bfd_section_size (sectp);
1942 }
1943 else if (section_is_p (sectp->name, &names.abbrev))
1944 {
1945 this->abbrev.s.section = sectp;
1946 this->abbrev.size = bfd_section_size (sectp);
1947 }
1948 else if (section_is_p (sectp->name, &names.line))
1949 {
1950 this->line.s.section = sectp;
1951 this->line.size = bfd_section_size (sectp);
1952 }
1953 else if (section_is_p (sectp->name, &names.loc))
1954 {
1955 this->loc.s.section = sectp;
1956 this->loc.size = bfd_section_size (sectp);
1957 }
1958 else if (section_is_p (sectp->name, &names.loclists))
1959 {
1960 this->loclists.s.section = sectp;
1961 this->loclists.size = bfd_section_size (sectp);
1962 }
1963 else if (section_is_p (sectp->name, &names.macinfo))
1964 {
1965 this->macinfo.s.section = sectp;
1966 this->macinfo.size = bfd_section_size (sectp);
1967 }
1968 else if (section_is_p (sectp->name, &names.macro))
1969 {
1970 this->macro.s.section = sectp;
1971 this->macro.size = bfd_section_size (sectp);
1972 }
1973 else if (section_is_p (sectp->name, &names.str))
1974 {
1975 this->str.s.section = sectp;
1976 this->str.size = bfd_section_size (sectp);
1977 }
1978 else if (section_is_p (sectp->name, &names.str_offsets))
1979 {
1980 this->str_offsets.s.section = sectp;
1981 this->str_offsets.size = bfd_section_size (sectp);
1982 }
1983 else if (section_is_p (sectp->name, &names.line_str))
1984 {
1985 this->line_str.s.section = sectp;
1986 this->line_str.size = bfd_section_size (sectp);
1987 }
1988 else if (section_is_p (sectp->name, &names.addr))
1989 {
1990 this->addr.s.section = sectp;
1991 this->addr.size = bfd_section_size (sectp);
1992 }
1993 else if (section_is_p (sectp->name, &names.frame))
1994 {
1995 this->frame.s.section = sectp;
1996 this->frame.size = bfd_section_size (sectp);
1997 }
1998 else if (section_is_p (sectp->name, &names.eh_frame))
1999 {
2000 this->eh_frame.s.section = sectp;
2001 this->eh_frame.size = bfd_section_size (sectp);
2002 }
2003 else if (section_is_p (sectp->name, &names.ranges))
2004 {
2005 this->ranges.s.section = sectp;
2006 this->ranges.size = bfd_section_size (sectp);
2007 }
2008 else if (section_is_p (sectp->name, &names.rnglists))
2009 {
2010 this->rnglists.s.section = sectp;
2011 this->rnglists.size = bfd_section_size (sectp);
2012 }
2013 else if (section_is_p (sectp->name, &names.types))
2014 {
2015 struct dwarf2_section_info type_section;
2016
2017 memset (&type_section, 0, sizeof (type_section));
2018 type_section.s.section = sectp;
2019 type_section.size = bfd_section_size (sectp);
2020
2021 this->types.push_back (type_section);
2022 }
2023 else if (section_is_p (sectp->name, &names.gdb_index))
2024 {
2025 this->gdb_index.s.section = sectp;
2026 this->gdb_index.size = bfd_section_size (sectp);
2027 }
2028 else if (section_is_p (sectp->name, &names.debug_names))
2029 {
2030 this->debug_names.s.section = sectp;
2031 this->debug_names.size = bfd_section_size (sectp);
2032 }
2033 else if (section_is_p (sectp->name, &names.debug_aranges))
2034 {
2035 this->debug_aranges.s.section = sectp;
2036 this->debug_aranges.size = bfd_section_size (sectp);
2037 }
2038
2039 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2040 && bfd_section_vma (sectp) == 0)
2041 this->has_section_at_zero = true;
2042 }
2043
2044 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2045 SECTION_NAME. */
2046
2047 void
2048 dwarf2_get_section_info (struct objfile *objfile,
2049 enum dwarf2_section_enum sect,
2050 asection **sectp, const gdb_byte **bufp,
2051 bfd_size_type *sizep)
2052 {
2053 struct dwarf2_per_objfile *data = get_dwarf2_per_objfile (objfile);
2054 struct dwarf2_section_info *info;
2055
2056 /* We may see an objfile without any DWARF, in which case we just
2057 return nothing. */
2058 if (data == NULL)
2059 {
2060 *sectp = NULL;
2061 *bufp = NULL;
2062 *sizep = 0;
2063 return;
2064 }
2065 switch (sect)
2066 {
2067 case DWARF2_DEBUG_FRAME:
2068 info = &data->per_bfd->frame;
2069 break;
2070 case DWARF2_EH_FRAME:
2071 info = &data->per_bfd->eh_frame;
2072 break;
2073 default:
2074 gdb_assert_not_reached ("unexpected section");
2075 }
2076
2077 info->read (objfile);
2078
2079 *sectp = info->get_bfd_section ();
2080 *bufp = info->buffer;
2081 *sizep = info->size;
2082 }
2083
2084 /* A helper function to find the sections for a .dwz file. */
2085
2086 static void
2087 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2088 {
2089 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2090
2091 /* Note that we only support the standard ELF names, because .dwz
2092 is ELF-only (at the time of writing). */
2093 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2094 {
2095 dwz_file->abbrev.s.section = sectp;
2096 dwz_file->abbrev.size = bfd_section_size (sectp);
2097 }
2098 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2099 {
2100 dwz_file->info.s.section = sectp;
2101 dwz_file->info.size = bfd_section_size (sectp);
2102 }
2103 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2104 {
2105 dwz_file->str.s.section = sectp;
2106 dwz_file->str.size = bfd_section_size (sectp);
2107 }
2108 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2109 {
2110 dwz_file->line.s.section = sectp;
2111 dwz_file->line.size = bfd_section_size (sectp);
2112 }
2113 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2114 {
2115 dwz_file->macro.s.section = sectp;
2116 dwz_file->macro.size = bfd_section_size (sectp);
2117 }
2118 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2119 {
2120 dwz_file->gdb_index.s.section = sectp;
2121 dwz_file->gdb_index.size = bfd_section_size (sectp);
2122 }
2123 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2124 {
2125 dwz_file->debug_names.s.section = sectp;
2126 dwz_file->debug_names.size = bfd_section_size (sectp);
2127 }
2128 }
2129
2130 /* See dwarf2read.h. */
2131
2132 struct dwz_file *
2133 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2134 {
2135 const char *filename;
2136 bfd_size_type buildid_len_arg;
2137 size_t buildid_len;
2138 bfd_byte *buildid;
2139
2140 if (per_bfd->dwz_file != NULL)
2141 return per_bfd->dwz_file.get ();
2142
2143 bfd_set_error (bfd_error_no_error);
2144 gdb::unique_xmalloc_ptr<char> data
2145 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2146 &buildid_len_arg, &buildid));
2147 if (data == NULL)
2148 {
2149 if (bfd_get_error () == bfd_error_no_error)
2150 return NULL;
2151 error (_("could not read '.gnu_debugaltlink' section: %s"),
2152 bfd_errmsg (bfd_get_error ()));
2153 }
2154
2155 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2156
2157 buildid_len = (size_t) buildid_len_arg;
2158
2159 filename = data.get ();
2160
2161 std::string abs_storage;
2162 if (!IS_ABSOLUTE_PATH (filename))
2163 {
2164 gdb::unique_xmalloc_ptr<char> abs
2165 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2166
2167 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2168 filename = abs_storage.c_str ();
2169 }
2170
2171 /* First try the file name given in the section. If that doesn't
2172 work, try to use the build-id instead. */
2173 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2174 if (dwz_bfd != NULL)
2175 {
2176 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2177 dwz_bfd.reset (nullptr);
2178 }
2179
2180 if (dwz_bfd == NULL)
2181 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2182
2183 if (dwz_bfd == nullptr)
2184 {
2185 gdb::unique_xmalloc_ptr<char> alt_filename;
2186 const char *origname = bfd_get_filename (per_bfd->obfd);
2187
2188 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2189 buildid_len,
2190 origname,
2191 &alt_filename));
2192
2193 if (fd.get () >= 0)
2194 {
2195 /* File successfully retrieved from server. */
2196 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2197
2198 if (dwz_bfd == nullptr)
2199 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2200 alt_filename.get ());
2201 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2202 dwz_bfd.reset (nullptr);
2203 }
2204 }
2205
2206 if (dwz_bfd == NULL)
2207 error (_("could not find '.gnu_debugaltlink' file for %s"),
2208 bfd_get_filename (per_bfd->obfd));
2209
2210 std::unique_ptr<struct dwz_file> result
2211 (new struct dwz_file (std::move (dwz_bfd)));
2212
2213 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2214 result.get ());
2215
2216 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2217 per_bfd->dwz_file = std::move (result);
2218 return per_bfd->dwz_file.get ();
2219 }
2220 \f
2221 /* DWARF quick_symbols_functions support. */
2222
2223 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2224 unique line tables, so we maintain a separate table of all .debug_line
2225 derived entries to support the sharing.
2226 All the quick functions need is the list of file names. We discard the
2227 line_header when we're done and don't need to record it here. */
2228 struct quick_file_names
2229 {
2230 /* The data used to construct the hash key. */
2231 struct stmt_list_hash hash;
2232
2233 /* The number of entries in file_names, real_names. */
2234 unsigned int num_file_names;
2235
2236 /* The file names from the line table, after being run through
2237 file_full_name. */
2238 const char **file_names;
2239
2240 /* The file names from the line table after being run through
2241 gdb_realpath. These are computed lazily. */
2242 const char **real_names;
2243 };
2244
2245 /* When using the index (and thus not using psymtabs), each CU has an
2246 object of this type. This is used to hold information needed by
2247 the various "quick" methods. */
2248 struct dwarf2_per_cu_quick_data
2249 {
2250 /* The file table. This can be NULL if there was no file table
2251 or it's currently not read in.
2252 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2253 struct quick_file_names *file_names;
2254
2255 /* A temporary mark bit used when iterating over all CUs in
2256 expand_symtabs_matching. */
2257 unsigned int mark : 1;
2258
2259 /* True if we've tried to read the file table and found there isn't one.
2260 There will be no point in trying to read it again next time. */
2261 unsigned int no_file_data : 1;
2262 };
2263
2264 /* Utility hash function for a stmt_list_hash. */
2265
2266 static hashval_t
2267 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2268 {
2269 hashval_t v = 0;
2270
2271 if (stmt_list_hash->dwo_unit != NULL)
2272 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2273 v += to_underlying (stmt_list_hash->line_sect_off);
2274 return v;
2275 }
2276
2277 /* Utility equality function for a stmt_list_hash. */
2278
2279 static int
2280 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2281 const struct stmt_list_hash *rhs)
2282 {
2283 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2284 return 0;
2285 if (lhs->dwo_unit != NULL
2286 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2287 return 0;
2288
2289 return lhs->line_sect_off == rhs->line_sect_off;
2290 }
2291
2292 /* Hash function for a quick_file_names. */
2293
2294 static hashval_t
2295 hash_file_name_entry (const void *e)
2296 {
2297 const struct quick_file_names *file_data
2298 = (const struct quick_file_names *) e;
2299
2300 return hash_stmt_list_entry (&file_data->hash);
2301 }
2302
2303 /* Equality function for a quick_file_names. */
2304
2305 static int
2306 eq_file_name_entry (const void *a, const void *b)
2307 {
2308 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2309 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2310
2311 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2312 }
2313
2314 /* Delete function for a quick_file_names. */
2315
2316 static void
2317 delete_file_name_entry (void *e)
2318 {
2319 struct quick_file_names *file_data = (struct quick_file_names *) e;
2320 int i;
2321
2322 for (i = 0; i < file_data->num_file_names; ++i)
2323 {
2324 xfree ((void*) file_data->file_names[i]);
2325 if (file_data->real_names)
2326 xfree ((void*) file_data->real_names[i]);
2327 }
2328
2329 /* The space for the struct itself lives on the obstack, so we don't
2330 free it here. */
2331 }
2332
2333 /* Create a quick_file_names hash table. */
2334
2335 static htab_up
2336 create_quick_file_names_table (unsigned int nr_initial_entries)
2337 {
2338 return htab_up (htab_create_alloc (nr_initial_entries,
2339 hash_file_name_entry, eq_file_name_entry,
2340 delete_file_name_entry, xcalloc, xfree));
2341 }
2342
2343 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2344 function is unrelated to symtabs, symtab would have to be created afterwards.
2345 You should call age_cached_comp_units after processing the CU. */
2346
2347 static dwarf2_cu *
2348 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2349 bool skip_partial)
2350 {
2351 if (per_cu->is_debug_types)
2352 load_full_type_unit (per_cu, per_objfile);
2353 else
2354 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2355
2356 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2357 if (cu == nullptr)
2358 return nullptr; /* Dummy CU. */
2359
2360 dwarf2_find_base_address (cu->dies, cu);
2361
2362 return cu;
2363 }
2364
2365 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2366
2367 static void
2368 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2369 dwarf2_per_objfile *dwarf2_per_objfile,
2370 bool skip_partial)
2371 {
2372 /* Skip type_unit_groups, reading the type units they contain
2373 is handled elsewhere. */
2374 if (per_cu->type_unit_group_p ())
2375 return;
2376
2377 /* The destructor of dwarf2_queue_guard frees any entries left on
2378 the queue. After this point we're guaranteed to leave this function
2379 with the dwarf queue empty. */
2380 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2381
2382 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2383 {
2384 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
2385 dwarf2_cu *cu = load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2386
2387 /* If we just loaded a CU from a DWO, and we're working with an index
2388 that may badly handle TUs, load all the TUs in that DWO as well.
2389 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2390 if (!per_cu->is_debug_types
2391 && cu != NULL
2392 && cu->dwo_unit != NULL
2393 && dwarf2_per_objfile->per_bfd->index_table != NULL
2394 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2395 /* DWP files aren't supported yet. */
2396 && get_dwp_file (dwarf2_per_objfile) == NULL)
2397 queue_and_load_all_dwo_tus (cu);
2398 }
2399
2400 process_queue (dwarf2_per_objfile);
2401
2402 /* Age the cache, releasing compilation units that have not
2403 been used recently. */
2404 dwarf2_per_objfile->age_comp_units ();
2405 }
2406
2407 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2408 the per-objfile for which this symtab is instantiated.
2409
2410 Returns the resulting symbol table. */
2411
2412 static struct compunit_symtab *
2413 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2414 dwarf2_per_objfile *dwarf2_per_objfile,
2415 bool skip_partial)
2416 {
2417 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2418
2419 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2420 {
2421 free_cached_comp_units freer (dwarf2_per_objfile);
2422 scoped_restore decrementer = increment_reading_symtab ();
2423 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2424 process_cu_includes (dwarf2_per_objfile);
2425 }
2426
2427 return dwarf2_per_objfile->get_symtab (per_cu);
2428 }
2429
2430 /* See declaration. */
2431
2432 dwarf2_per_cu_data *
2433 dwarf2_per_bfd::get_cutu (int index)
2434 {
2435 if (index >= this->all_comp_units.size ())
2436 {
2437 index -= this->all_comp_units.size ();
2438 gdb_assert (index < this->all_type_units.size ());
2439 return &this->all_type_units[index]->per_cu;
2440 }
2441
2442 return this->all_comp_units[index];
2443 }
2444
2445 /* See declaration. */
2446
2447 dwarf2_per_cu_data *
2448 dwarf2_per_bfd::get_cu (int index)
2449 {
2450 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2451
2452 return this->all_comp_units[index];
2453 }
2454
2455 /* See declaration. */
2456
2457 signatured_type *
2458 dwarf2_per_bfd::get_tu (int index)
2459 {
2460 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2461
2462 return this->all_type_units[index];
2463 }
2464
2465 /* See read.h. */
2466
2467 dwarf2_per_cu_data *
2468 dwarf2_per_bfd::allocate_per_cu ()
2469 {
2470 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2471 result->per_bfd = this;
2472 result->index = m_num_psymtabs++;
2473 return result;
2474 }
2475
2476 /* See read.h. */
2477
2478 signatured_type *
2479 dwarf2_per_bfd::allocate_signatured_type ()
2480 {
2481 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2482 result->per_cu.per_bfd = this;
2483 result->per_cu.index = m_num_psymtabs++;
2484 return result;
2485 }
2486
2487 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2488 obstack, and constructed with the specified field values. */
2489
2490 static dwarf2_per_cu_data *
2491 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2492 struct dwarf2_section_info *section,
2493 int is_dwz,
2494 sect_offset sect_off, ULONGEST length)
2495 {
2496 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2497 the_cu->sect_off = sect_off;
2498 the_cu->length = length;
2499 the_cu->section = section;
2500 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2501 struct dwarf2_per_cu_quick_data);
2502 the_cu->is_dwz = is_dwz;
2503 return the_cu;
2504 }
2505
2506 /* A helper for create_cus_from_index that handles a given list of
2507 CUs. */
2508
2509 static void
2510 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2511 const gdb_byte *cu_list, offset_type n_elements,
2512 struct dwarf2_section_info *section,
2513 int is_dwz)
2514 {
2515 for (offset_type i = 0; i < n_elements; i += 2)
2516 {
2517 gdb_static_assert (sizeof (ULONGEST) >= 8);
2518
2519 sect_offset sect_off
2520 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2521 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2522 cu_list += 2 * 8;
2523
2524 dwarf2_per_cu_data *per_cu
2525 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2526 length);
2527 per_bfd->all_comp_units.push_back (per_cu);
2528 }
2529 }
2530
2531 /* Read the CU list from the mapped index, and use it to create all
2532 the CU objects for PER_BFD. */
2533
2534 static void
2535 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2536 const gdb_byte *cu_list, offset_type cu_list_elements,
2537 const gdb_byte *dwz_list, offset_type dwz_elements)
2538 {
2539 gdb_assert (per_bfd->all_comp_units.empty ());
2540 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2541
2542 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2543 &per_bfd->info, 0);
2544
2545 if (dwz_elements == 0)
2546 return;
2547
2548 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2549 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2550 &dwz->info, 1);
2551 }
2552
2553 /* Create the signatured type hash table from the index. */
2554
2555 static void
2556 create_signatured_type_table_from_index
2557 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2558 const gdb_byte *bytes, offset_type elements)
2559 {
2560 gdb_assert (per_bfd->all_type_units.empty ());
2561 per_bfd->all_type_units.reserve (elements / 3);
2562
2563 htab_up sig_types_hash = allocate_signatured_type_table ();
2564
2565 for (offset_type i = 0; i < elements; i += 3)
2566 {
2567 struct signatured_type *sig_type;
2568 ULONGEST signature;
2569 void **slot;
2570 cu_offset type_offset_in_tu;
2571
2572 gdb_static_assert (sizeof (ULONGEST) >= 8);
2573 sect_offset sect_off
2574 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2575 type_offset_in_tu
2576 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2577 BFD_ENDIAN_LITTLE);
2578 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2579 bytes += 3 * 8;
2580
2581 sig_type = per_bfd->allocate_signatured_type ();
2582 sig_type->signature = signature;
2583 sig_type->type_offset_in_tu = type_offset_in_tu;
2584 sig_type->per_cu.is_debug_types = 1;
2585 sig_type->per_cu.section = section;
2586 sig_type->per_cu.sect_off = sect_off;
2587 sig_type->per_cu.v.quick
2588 = OBSTACK_ZALLOC (&per_bfd->obstack,
2589 struct dwarf2_per_cu_quick_data);
2590
2591 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2592 *slot = sig_type;
2593
2594 per_bfd->all_type_units.push_back (sig_type);
2595 }
2596
2597 per_bfd->signatured_types = std::move (sig_types_hash);
2598 }
2599
2600 /* Create the signatured type hash table from .debug_names. */
2601
2602 static void
2603 create_signatured_type_table_from_debug_names
2604 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2605 const mapped_debug_names &map,
2606 struct dwarf2_section_info *section,
2607 struct dwarf2_section_info *abbrev_section)
2608 {
2609 struct objfile *objfile = dwarf2_per_objfile->objfile;
2610
2611 section->read (objfile);
2612 abbrev_section->read (objfile);
2613
2614 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2615 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2616
2617 htab_up sig_types_hash = allocate_signatured_type_table ();
2618
2619 for (uint32_t i = 0; i < map.tu_count; ++i)
2620 {
2621 struct signatured_type *sig_type;
2622 void **slot;
2623
2624 sect_offset sect_off
2625 = (sect_offset) (extract_unsigned_integer
2626 (map.tu_table_reordered + i * map.offset_size,
2627 map.offset_size,
2628 map.dwarf5_byte_order));
2629
2630 comp_unit_head cu_header;
2631 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2632 abbrev_section,
2633 section->buffer + to_underlying (sect_off),
2634 rcuh_kind::TYPE);
2635
2636 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2637 sig_type->signature = cu_header.signature;
2638 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2639 sig_type->per_cu.is_debug_types = 1;
2640 sig_type->per_cu.section = section;
2641 sig_type->per_cu.sect_off = sect_off;
2642 sig_type->per_cu.v.quick
2643 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2644 struct dwarf2_per_cu_quick_data);
2645
2646 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2647 *slot = sig_type;
2648
2649 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2650 }
2651
2652 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2653 }
2654
2655 /* Read the address map data from the mapped index, and use it to
2656 populate the objfile's psymtabs_addrmap. */
2657
2658 static void
2659 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2660 struct mapped_index *index)
2661 {
2662 struct objfile *objfile = dwarf2_per_objfile->objfile;
2663 struct gdbarch *gdbarch = objfile->arch ();
2664 const gdb_byte *iter, *end;
2665 struct addrmap *mutable_map;
2666 CORE_ADDR baseaddr;
2667
2668 auto_obstack temp_obstack;
2669
2670 mutable_map = addrmap_create_mutable (&temp_obstack);
2671
2672 iter = index->address_table.data ();
2673 end = iter + index->address_table.size ();
2674
2675 baseaddr = objfile->text_section_offset ();
2676
2677 while (iter < end)
2678 {
2679 ULONGEST hi, lo, cu_index;
2680 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2681 iter += 8;
2682 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2683 iter += 8;
2684 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2685 iter += 4;
2686
2687 if (lo > hi)
2688 {
2689 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2690 hex_string (lo), hex_string (hi));
2691 continue;
2692 }
2693
2694 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2695 {
2696 complaint (_(".gdb_index address table has invalid CU number %u"),
2697 (unsigned) cu_index);
2698 continue;
2699 }
2700
2701 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2702 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2703 addrmap_set_empty (mutable_map, lo, hi - 1,
2704 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2705 }
2706
2707 objfile->partial_symtabs->psymtabs_addrmap
2708 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2709 }
2710
2711 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2712 populate the objfile's psymtabs_addrmap. */
2713
2714 static void
2715 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2716 struct dwarf2_section_info *section)
2717 {
2718 struct objfile *objfile = dwarf2_per_objfile->objfile;
2719 bfd *abfd = objfile->obfd;
2720 struct gdbarch *gdbarch = objfile->arch ();
2721 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2722
2723 auto_obstack temp_obstack;
2724 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2725
2726 std::unordered_map<sect_offset,
2727 dwarf2_per_cu_data *,
2728 gdb::hash_enum<sect_offset>>
2729 debug_info_offset_to_per_cu;
2730 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2731 {
2732 const auto insertpair
2733 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2734 if (!insertpair.second)
2735 {
2736 warning (_("Section .debug_aranges in %s has duplicate "
2737 "debug_info_offset %s, ignoring .debug_aranges."),
2738 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2739 return;
2740 }
2741 }
2742
2743 section->read (objfile);
2744
2745 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2746
2747 const gdb_byte *addr = section->buffer;
2748
2749 while (addr < section->buffer + section->size)
2750 {
2751 const gdb_byte *const entry_addr = addr;
2752 unsigned int bytes_read;
2753
2754 const LONGEST entry_length = read_initial_length (abfd, addr,
2755 &bytes_read);
2756 addr += bytes_read;
2757
2758 const gdb_byte *const entry_end = addr + entry_length;
2759 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2760 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2761 if (addr + entry_length > section->buffer + section->size)
2762 {
2763 warning (_("Section .debug_aranges in %s entry at offset %s "
2764 "length %s exceeds section length %s, "
2765 "ignoring .debug_aranges."),
2766 objfile_name (objfile),
2767 plongest (entry_addr - section->buffer),
2768 plongest (bytes_read + entry_length),
2769 pulongest (section->size));
2770 return;
2771 }
2772
2773 /* The version number. */
2774 const uint16_t version = read_2_bytes (abfd, addr);
2775 addr += 2;
2776 if (version != 2)
2777 {
2778 warning (_("Section .debug_aranges in %s entry at offset %s "
2779 "has unsupported version %d, ignoring .debug_aranges."),
2780 objfile_name (objfile),
2781 plongest (entry_addr - section->buffer), version);
2782 return;
2783 }
2784
2785 const uint64_t debug_info_offset
2786 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2787 addr += offset_size;
2788 const auto per_cu_it
2789 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2790 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2791 {
2792 warning (_("Section .debug_aranges in %s entry at offset %s "
2793 "debug_info_offset %s does not exists, "
2794 "ignoring .debug_aranges."),
2795 objfile_name (objfile),
2796 plongest (entry_addr - section->buffer),
2797 pulongest (debug_info_offset));
2798 return;
2799 }
2800 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2801
2802 const uint8_t address_size = *addr++;
2803 if (address_size < 1 || address_size > 8)
2804 {
2805 warning (_("Section .debug_aranges in %s entry at offset %s "
2806 "address_size %u is invalid, ignoring .debug_aranges."),
2807 objfile_name (objfile),
2808 plongest (entry_addr - section->buffer), address_size);
2809 return;
2810 }
2811
2812 const uint8_t segment_selector_size = *addr++;
2813 if (segment_selector_size != 0)
2814 {
2815 warning (_("Section .debug_aranges in %s entry at offset %s "
2816 "segment_selector_size %u is not supported, "
2817 "ignoring .debug_aranges."),
2818 objfile_name (objfile),
2819 plongest (entry_addr - section->buffer),
2820 segment_selector_size);
2821 return;
2822 }
2823
2824 /* Must pad to an alignment boundary that is twice the address
2825 size. It is undocumented by the DWARF standard but GCC does
2826 use it. */
2827 for (size_t padding = ((-(addr - section->buffer))
2828 & (2 * address_size - 1));
2829 padding > 0; padding--)
2830 if (*addr++ != 0)
2831 {
2832 warning (_("Section .debug_aranges in %s entry at offset %s "
2833 "padding is not zero, ignoring .debug_aranges."),
2834 objfile_name (objfile),
2835 plongest (entry_addr - section->buffer));
2836 return;
2837 }
2838
2839 for (;;)
2840 {
2841 if (addr + 2 * address_size > entry_end)
2842 {
2843 warning (_("Section .debug_aranges in %s entry at offset %s "
2844 "address list is not properly terminated, "
2845 "ignoring .debug_aranges."),
2846 objfile_name (objfile),
2847 plongest (entry_addr - section->buffer));
2848 return;
2849 }
2850 ULONGEST start = extract_unsigned_integer (addr, address_size,
2851 dwarf5_byte_order);
2852 addr += address_size;
2853 ULONGEST length = extract_unsigned_integer (addr, address_size,
2854 dwarf5_byte_order);
2855 addr += address_size;
2856 if (start == 0 && length == 0)
2857 break;
2858 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2859 {
2860 /* Symbol was eliminated due to a COMDAT group. */
2861 continue;
2862 }
2863 ULONGEST end = start + length;
2864 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2865 - baseaddr);
2866 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2867 - baseaddr);
2868 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2869 }
2870 }
2871
2872 objfile->partial_symtabs->psymtabs_addrmap
2873 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2874 }
2875
2876 /* Find a slot in the mapped index INDEX for the object named NAME.
2877 If NAME is found, set *VEC_OUT to point to the CU vector in the
2878 constant pool and return true. If NAME cannot be found, return
2879 false. */
2880
2881 static bool
2882 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2883 offset_type **vec_out)
2884 {
2885 offset_type hash;
2886 offset_type slot, step;
2887 int (*cmp) (const char *, const char *);
2888
2889 gdb::unique_xmalloc_ptr<char> without_params;
2890 if (current_language->la_language == language_cplus
2891 || current_language->la_language == language_fortran
2892 || current_language->la_language == language_d)
2893 {
2894 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2895 not contain any. */
2896
2897 if (strchr (name, '(') != NULL)
2898 {
2899 without_params = cp_remove_params (name);
2900
2901 if (without_params != NULL)
2902 name = without_params.get ();
2903 }
2904 }
2905
2906 /* Index version 4 did not support case insensitive searches. But the
2907 indices for case insensitive languages are built in lowercase, therefore
2908 simulate our NAME being searched is also lowercased. */
2909 hash = mapped_index_string_hash ((index->version == 4
2910 && case_sensitivity == case_sensitive_off
2911 ? 5 : index->version),
2912 name);
2913
2914 slot = hash & (index->symbol_table.size () - 1);
2915 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2916 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2917
2918 for (;;)
2919 {
2920 const char *str;
2921
2922 const auto &bucket = index->symbol_table[slot];
2923 if (bucket.name == 0 && bucket.vec == 0)
2924 return false;
2925
2926 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2927 if (!cmp (name, str))
2928 {
2929 *vec_out = (offset_type *) (index->constant_pool
2930 + MAYBE_SWAP (bucket.vec));
2931 return true;
2932 }
2933
2934 slot = (slot + step) & (index->symbol_table.size () - 1);
2935 }
2936 }
2937
2938 /* A helper function that reads the .gdb_index from BUFFER and fills
2939 in MAP. FILENAME is the name of the file containing the data;
2940 it is used for error reporting. DEPRECATED_OK is true if it is
2941 ok to use deprecated sections.
2942
2943 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2944 out parameters that are filled in with information about the CU and
2945 TU lists in the section.
2946
2947 Returns true if all went well, false otherwise. */
2948
2949 static bool
2950 read_gdb_index_from_buffer (const char *filename,
2951 bool deprecated_ok,
2952 gdb::array_view<const gdb_byte> buffer,
2953 struct mapped_index *map,
2954 const gdb_byte **cu_list,
2955 offset_type *cu_list_elements,
2956 const gdb_byte **types_list,
2957 offset_type *types_list_elements)
2958 {
2959 const gdb_byte *addr = &buffer[0];
2960
2961 /* Version check. */
2962 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2963 /* Versions earlier than 3 emitted every copy of a psymbol. This
2964 causes the index to behave very poorly for certain requests. Version 3
2965 contained incomplete addrmap. So, it seems better to just ignore such
2966 indices. */
2967 if (version < 4)
2968 {
2969 static int warning_printed = 0;
2970 if (!warning_printed)
2971 {
2972 warning (_("Skipping obsolete .gdb_index section in %s."),
2973 filename);
2974 warning_printed = 1;
2975 }
2976 return 0;
2977 }
2978 /* Index version 4 uses a different hash function than index version
2979 5 and later.
2980
2981 Versions earlier than 6 did not emit psymbols for inlined
2982 functions. Using these files will cause GDB not to be able to
2983 set breakpoints on inlined functions by name, so we ignore these
2984 indices unless the user has done
2985 "set use-deprecated-index-sections on". */
2986 if (version < 6 && !deprecated_ok)
2987 {
2988 static int warning_printed = 0;
2989 if (!warning_printed)
2990 {
2991 warning (_("\
2992 Skipping deprecated .gdb_index section in %s.\n\
2993 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2994 to use the section anyway."),
2995 filename);
2996 warning_printed = 1;
2997 }
2998 return 0;
2999 }
3000 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3001 of the TU (for symbols coming from TUs),
3002 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3003 Plus gold-generated indices can have duplicate entries for global symbols,
3004 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3005 These are just performance bugs, and we can't distinguish gdb-generated
3006 indices from gold-generated ones, so issue no warning here. */
3007
3008 /* Indexes with higher version than the one supported by GDB may be no
3009 longer backward compatible. */
3010 if (version > 8)
3011 return 0;
3012
3013 map->version = version;
3014
3015 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3016
3017 int i = 0;
3018 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3019 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3020 / 8);
3021 ++i;
3022
3023 *types_list = addr + MAYBE_SWAP (metadata[i]);
3024 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3025 - MAYBE_SWAP (metadata[i]))
3026 / 8);
3027 ++i;
3028
3029 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3030 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3031 map->address_table
3032 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3033 ++i;
3034
3035 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3036 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3037 map->symbol_table
3038 = gdb::array_view<mapped_index::symbol_table_slot>
3039 ((mapped_index::symbol_table_slot *) symbol_table,
3040 (mapped_index::symbol_table_slot *) symbol_table_end);
3041
3042 ++i;
3043 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3044
3045 return 1;
3046 }
3047
3048 /* Callback types for dwarf2_read_gdb_index. */
3049
3050 typedef gdb::function_view
3051 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3052 get_gdb_index_contents_ftype;
3053 typedef gdb::function_view
3054 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3055 get_gdb_index_contents_dwz_ftype;
3056
3057 /* Read .gdb_index. If everything went ok, initialize the "quick"
3058 elements of all the CUs and return 1. Otherwise, return 0. */
3059
3060 static int
3061 dwarf2_read_gdb_index
3062 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3063 get_gdb_index_contents_ftype get_gdb_index_contents,
3064 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3065 {
3066 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3067 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3068 struct dwz_file *dwz;
3069 struct objfile *objfile = dwarf2_per_objfile->objfile;
3070
3071 gdb::array_view<const gdb_byte> main_index_contents
3072 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3073
3074 if (main_index_contents.empty ())
3075 return 0;
3076
3077 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3078 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3079 use_deprecated_index_sections,
3080 main_index_contents, map.get (), &cu_list,
3081 &cu_list_elements, &types_list,
3082 &types_list_elements))
3083 return 0;
3084
3085 /* Don't use the index if it's empty. */
3086 if (map->symbol_table.empty ())
3087 return 0;
3088
3089 /* If there is a .dwz file, read it so we can get its CU list as
3090 well. */
3091 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3092 if (dwz != NULL)
3093 {
3094 struct mapped_index dwz_map;
3095 const gdb_byte *dwz_types_ignore;
3096 offset_type dwz_types_elements_ignore;
3097
3098 gdb::array_view<const gdb_byte> dwz_index_content
3099 = get_gdb_index_contents_dwz (objfile, dwz);
3100
3101 if (dwz_index_content.empty ())
3102 return 0;
3103
3104 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3105 1, dwz_index_content, &dwz_map,
3106 &dwz_list, &dwz_list_elements,
3107 &dwz_types_ignore,
3108 &dwz_types_elements_ignore))
3109 {
3110 warning (_("could not read '.gdb_index' section from %s; skipping"),
3111 bfd_get_filename (dwz->dwz_bfd.get ()));
3112 return 0;
3113 }
3114 }
3115
3116 create_cus_from_index (dwarf2_per_objfile->per_bfd, cu_list, cu_list_elements,
3117 dwz_list, dwz_list_elements);
3118
3119 if (types_list_elements)
3120 {
3121 /* We can only handle a single .debug_types when we have an
3122 index. */
3123 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3124 return 0;
3125
3126 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3127
3128 create_signatured_type_table_from_index (dwarf2_per_objfile->per_bfd,
3129 section, types_list,
3130 types_list_elements);
3131 }
3132
3133 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3134
3135 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3136 dwarf2_per_objfile->per_bfd->using_index = 1;
3137 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3138 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3139
3140 return 1;
3141 }
3142
3143 /* die_reader_func for dw2_get_file_names. */
3144
3145 static void
3146 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3147 const gdb_byte *info_ptr,
3148 struct die_info *comp_unit_die)
3149 {
3150 struct dwarf2_cu *cu = reader->cu;
3151 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3152 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3153 struct dwarf2_per_cu_data *lh_cu;
3154 struct attribute *attr;
3155 void **slot;
3156 struct quick_file_names *qfn;
3157
3158 gdb_assert (! this_cu->is_debug_types);
3159
3160 /* Our callers never want to match partial units -- instead they
3161 will match the enclosing full CU. */
3162 if (comp_unit_die->tag == DW_TAG_partial_unit)
3163 {
3164 this_cu->v.quick->no_file_data = 1;
3165 return;
3166 }
3167
3168 lh_cu = this_cu;
3169 slot = NULL;
3170
3171 line_header_up lh;
3172 sect_offset line_offset {};
3173
3174 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3175 if (attr != nullptr)
3176 {
3177 struct quick_file_names find_entry;
3178
3179 line_offset = (sect_offset) DW_UNSND (attr);
3180
3181 /* We may have already read in this line header (TU line header sharing).
3182 If we have we're done. */
3183 find_entry.hash.dwo_unit = cu->dwo_unit;
3184 find_entry.hash.line_sect_off = line_offset;
3185 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3186 &find_entry, INSERT);
3187 if (*slot != NULL)
3188 {
3189 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3190 return;
3191 }
3192
3193 lh = dwarf_decode_line_header (line_offset, cu);
3194 }
3195 if (lh == NULL)
3196 {
3197 lh_cu->v.quick->no_file_data = 1;
3198 return;
3199 }
3200
3201 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3202 qfn->hash.dwo_unit = cu->dwo_unit;
3203 qfn->hash.line_sect_off = line_offset;
3204 gdb_assert (slot != NULL);
3205 *slot = qfn;
3206
3207 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3208
3209 int offset = 0;
3210 if (strcmp (fnd.name, "<unknown>") != 0)
3211 ++offset;
3212
3213 qfn->num_file_names = offset + lh->file_names_size ();
3214 qfn->file_names =
3215 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3216 qfn->num_file_names);
3217 if (offset != 0)
3218 qfn->file_names[0] = xstrdup (fnd.name);
3219 for (int i = 0; i < lh->file_names_size (); ++i)
3220 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3221 fnd.comp_dir).release ();
3222 qfn->real_names = NULL;
3223
3224 lh_cu->v.quick->file_names = qfn;
3225 }
3226
3227 /* A helper for the "quick" functions which attempts to read the line
3228 table for THIS_CU. */
3229
3230 static struct quick_file_names *
3231 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3232 dwarf2_per_objfile *per_objfile)
3233 {
3234 /* This should never be called for TUs. */
3235 gdb_assert (! this_cu->is_debug_types);
3236 /* Nor type unit groups. */
3237 gdb_assert (! this_cu->type_unit_group_p ());
3238
3239 if (this_cu->v.quick->file_names != NULL)
3240 return this_cu->v.quick->file_names;
3241 /* If we know there is no line data, no point in looking again. */
3242 if (this_cu->v.quick->no_file_data)
3243 return NULL;
3244
3245 cutu_reader reader (this_cu, per_objfile);
3246 if (!reader.dummy_p)
3247 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3248
3249 if (this_cu->v.quick->no_file_data)
3250 return NULL;
3251 return this_cu->v.quick->file_names;
3252 }
3253
3254 /* A helper for the "quick" functions which computes and caches the
3255 real path for a given file name from the line table. */
3256
3257 static const char *
3258 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3259 struct quick_file_names *qfn, int index)
3260 {
3261 if (qfn->real_names == NULL)
3262 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3263 qfn->num_file_names, const char *);
3264
3265 if (qfn->real_names[index] == NULL)
3266 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3267
3268 return qfn->real_names[index];
3269 }
3270
3271 static struct symtab *
3272 dw2_find_last_source_symtab (struct objfile *objfile)
3273 {
3274 struct dwarf2_per_objfile *dwarf2_per_objfile
3275 = get_dwarf2_per_objfile (objfile);
3276 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3277 compunit_symtab *cust
3278 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3279
3280 if (cust == NULL)
3281 return NULL;
3282
3283 return compunit_primary_filetab (cust);
3284 }
3285
3286 /* Traversal function for dw2_forget_cached_source_info. */
3287
3288 static int
3289 dw2_free_cached_file_names (void **slot, void *info)
3290 {
3291 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3292
3293 if (file_data->real_names)
3294 {
3295 int i;
3296
3297 for (i = 0; i < file_data->num_file_names; ++i)
3298 {
3299 xfree ((void*) file_data->real_names[i]);
3300 file_data->real_names[i] = NULL;
3301 }
3302 }
3303
3304 return 1;
3305 }
3306
3307 static void
3308 dw2_forget_cached_source_info (struct objfile *objfile)
3309 {
3310 struct dwarf2_per_objfile *dwarf2_per_objfile
3311 = get_dwarf2_per_objfile (objfile);
3312
3313 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3314 dw2_free_cached_file_names, NULL);
3315 }
3316
3317 /* Helper function for dw2_map_symtabs_matching_filename that expands
3318 the symtabs and calls the iterator. */
3319
3320 static int
3321 dw2_map_expand_apply (struct objfile *objfile,
3322 struct dwarf2_per_cu_data *per_cu,
3323 const char *name, const char *real_path,
3324 gdb::function_view<bool (symtab *)> callback)
3325 {
3326 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3327
3328 /* Don't visit already-expanded CUs. */
3329 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3330 if (per_objfile->symtab_set_p (per_cu))
3331 return 0;
3332
3333 /* This may expand more than one symtab, and we want to iterate over
3334 all of them. */
3335 dw2_instantiate_symtab (per_cu, per_objfile, false);
3336
3337 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3338 last_made, callback);
3339 }
3340
3341 /* Implementation of the map_symtabs_matching_filename method. */
3342
3343 static bool
3344 dw2_map_symtabs_matching_filename
3345 (struct objfile *objfile, const char *name, const char *real_path,
3346 gdb::function_view<bool (symtab *)> callback)
3347 {
3348 const char *name_basename = lbasename (name);
3349 struct dwarf2_per_objfile *dwarf2_per_objfile
3350 = get_dwarf2_per_objfile (objfile);
3351
3352 /* The rule is CUs specify all the files, including those used by
3353 any TU, so there's no need to scan TUs here. */
3354
3355 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3356 {
3357 /* We only need to look at symtabs not already expanded. */
3358 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3359 continue;
3360
3361 quick_file_names *file_data
3362 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3363 if (file_data == NULL)
3364 continue;
3365
3366 for (int j = 0; j < file_data->num_file_names; ++j)
3367 {
3368 const char *this_name = file_data->file_names[j];
3369 const char *this_real_name;
3370
3371 if (compare_filenames_for_search (this_name, name))
3372 {
3373 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3374 callback))
3375 return true;
3376 continue;
3377 }
3378
3379 /* Before we invoke realpath, which can get expensive when many
3380 files are involved, do a quick comparison of the basenames. */
3381 if (! basenames_may_differ
3382 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3383 continue;
3384
3385 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3386 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 struct dwarf2_per_objfile *dwarf2_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. */
3440
3441 static void
3442 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3443 struct dwarf2_per_objfile *dwarf2_per_objfile,
3444 gdb::optional<block_enum> block_index,
3445 domain_enum domain,
3446 const char *name)
3447 {
3448 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3449 iter->block_index = block_index;
3450 iter->domain = domain;
3451 iter->next = 0;
3452 iter->global_seen = 0;
3453
3454 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3455
3456 /* index is NULL if OBJF_READNOW. */
3457 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3458 iter->length = MAYBE_SWAP (*iter->vec);
3459 else
3460 {
3461 iter->vec = NULL;
3462 iter->length = 0;
3463 }
3464 }
3465
3466 /* Return the next matching CU or NULL if there are no more. */
3467
3468 static struct dwarf2_per_cu_data *
3469 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3470 {
3471 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3472
3473 for ( ; iter->next < iter->length; ++iter->next)
3474 {
3475 offset_type cu_index_and_attrs =
3476 MAYBE_SWAP (iter->vec[iter->next + 1]);
3477 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3478 gdb_index_symbol_kind symbol_kind =
3479 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3480 /* Only check the symbol attributes if they're present.
3481 Indices prior to version 7 don't record them,
3482 and indices >= 7 may elide them for certain symbols
3483 (gold does this). */
3484 int attrs_valid =
3485 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3486 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3487
3488 /* Don't crash on bad data. */
3489 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3490 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3491 {
3492 complaint (_(".gdb_index entry has bad CU index"
3493 " [in module %s]"),
3494 objfile_name (dwarf2_per_objfile->objfile));
3495 continue;
3496 }
3497
3498 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3499
3500 /* Skip if already read in. */
3501 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3502 continue;
3503
3504 /* Check static vs global. */
3505 if (attrs_valid)
3506 {
3507 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3508
3509 if (iter->block_index.has_value ())
3510 {
3511 bool want_static = *iter->block_index == STATIC_BLOCK;
3512
3513 if (is_static != want_static)
3514 continue;
3515 }
3516
3517 /* Work around gold/15646. */
3518 if (!is_static && iter->global_seen)
3519 continue;
3520 if (!is_static)
3521 iter->global_seen = 1;
3522 }
3523
3524 /* Only check the symbol's kind if it has one. */
3525 if (attrs_valid)
3526 {
3527 switch (iter->domain)
3528 {
3529 case VAR_DOMAIN:
3530 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3531 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3532 /* Some types are also in VAR_DOMAIN. */
3533 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3534 continue;
3535 break;
3536 case STRUCT_DOMAIN:
3537 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3538 continue;
3539 break;
3540 case LABEL_DOMAIN:
3541 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3542 continue;
3543 break;
3544 case MODULE_DOMAIN:
3545 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3546 continue;
3547 break;
3548 default:
3549 break;
3550 }
3551 }
3552
3553 ++iter->next;
3554 return per_cu;
3555 }
3556
3557 return NULL;
3558 }
3559
3560 static struct compunit_symtab *
3561 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3562 const char *name, domain_enum domain)
3563 {
3564 struct compunit_symtab *stab_best = NULL;
3565 struct dwarf2_per_objfile *dwarf2_per_objfile
3566 = get_dwarf2_per_objfile (objfile);
3567
3568 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3569
3570 struct dw2_symtab_iterator iter;
3571 struct dwarf2_per_cu_data *per_cu;
3572
3573 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3574
3575 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3576 {
3577 struct symbol *sym, *with_opaque = NULL;
3578 struct compunit_symtab *stab
3579 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3580 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3581 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3582
3583 sym = block_find_symbol (block, name, domain,
3584 block_find_non_opaque_type_preferred,
3585 &with_opaque);
3586
3587 /* Some caution must be observed with overloaded functions
3588 and methods, since the index will not contain any overload
3589 information (but NAME might contain it). */
3590
3591 if (sym != NULL
3592 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3593 return stab;
3594 if (with_opaque != NULL
3595 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3596 stab_best = stab;
3597
3598 /* Keep looking through other CUs. */
3599 }
3600
3601 return stab_best;
3602 }
3603
3604 static void
3605 dw2_print_stats (struct objfile *objfile)
3606 {
3607 struct dwarf2_per_objfile *dwarf2_per_objfile
3608 = get_dwarf2_per_objfile (objfile);
3609 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3610 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3611 int count = 0;
3612
3613 for (int i = 0; i < total; ++i)
3614 {
3615 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3616
3617 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3618 ++count;
3619 }
3620 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3621 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3622 }
3623
3624 /* This dumps minimal information about the index.
3625 It is called via "mt print objfiles".
3626 One use is to verify .gdb_index has been loaded by the
3627 gdb.dwarf2/gdb-index.exp testcase. */
3628
3629 static void
3630 dw2_dump (struct objfile *objfile)
3631 {
3632 struct dwarf2_per_objfile *dwarf2_per_objfile
3633 = get_dwarf2_per_objfile (objfile);
3634
3635 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3636 printf_filtered (".gdb_index:");
3637 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3638 {
3639 printf_filtered (" version %d\n",
3640 dwarf2_per_objfile->per_bfd->index_table->version);
3641 }
3642 else
3643 printf_filtered (" faked for \"readnow\"\n");
3644 printf_filtered ("\n");
3645 }
3646
3647 static void
3648 dw2_expand_symtabs_for_function (struct objfile *objfile,
3649 const char *func_name)
3650 {
3651 struct dwarf2_per_objfile *dwarf2_per_objfile
3652 = get_dwarf2_per_objfile (objfile);
3653
3654 struct dw2_symtab_iterator iter;
3655 struct dwarf2_per_cu_data *per_cu;
3656
3657 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3658
3659 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3660 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3661
3662 }
3663
3664 static void
3665 dw2_expand_all_symtabs (struct objfile *objfile)
3666 {
3667 struct dwarf2_per_objfile *dwarf2_per_objfile
3668 = get_dwarf2_per_objfile (objfile);
3669 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3670 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3671
3672 for (int i = 0; i < total_units; ++i)
3673 {
3674 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3675
3676 /* We don't want to directly expand a partial CU, because if we
3677 read it with the wrong language, then assertion failures can
3678 be triggered later on. See PR symtab/23010. So, tell
3679 dw2_instantiate_symtab to skip partial CUs -- any important
3680 partial CU will be read via DW_TAG_imported_unit anyway. */
3681 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3682 }
3683 }
3684
3685 static void
3686 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3687 const char *fullname)
3688 {
3689 struct dwarf2_per_objfile *dwarf2_per_objfile
3690 = get_dwarf2_per_objfile (objfile);
3691
3692 /* We don't need to consider type units here.
3693 This is only called for examining code, e.g. expand_line_sal.
3694 There can be an order of magnitude (or more) more type units
3695 than comp units, and we avoid them if we can. */
3696
3697 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3698 {
3699 /* We only need to look at symtabs not already expanded. */
3700 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3701 continue;
3702
3703 quick_file_names *file_data
3704 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3705 if (file_data == NULL)
3706 continue;
3707
3708 for (int j = 0; j < file_data->num_file_names; ++j)
3709 {
3710 const char *this_fullname = file_data->file_names[j];
3711
3712 if (filename_cmp (this_fullname, fullname) == 0)
3713 {
3714 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3715 break;
3716 }
3717 }
3718 }
3719 }
3720
3721 static void
3722 dw2_expand_symtabs_matching_symbol
3723 (mapped_index_base &index,
3724 const lookup_name_info &lookup_name_in,
3725 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3726 enum search_domain kind,
3727 gdb::function_view<bool (offset_type)> match_callback,
3728 dwarf2_per_objfile *per_objfile);
3729
3730 static void
3731 dw2_expand_symtabs_matching_one
3732 (dwarf2_per_cu_data *per_cu,
3733 dwarf2_per_objfile *per_objfile,
3734 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3735 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3736
3737 static void
3738 dw2_map_matching_symbols
3739 (struct objfile *objfile,
3740 const lookup_name_info &name, domain_enum domain,
3741 int global,
3742 gdb::function_view<symbol_found_callback_ftype> callback,
3743 symbol_compare_ftype *ordered_compare)
3744 {
3745 /* Used for Ada. */
3746 struct dwarf2_per_objfile *dwarf2_per_objfile
3747 = get_dwarf2_per_objfile (objfile);
3748
3749 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3750
3751 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3752 {
3753 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3754 here though if the current language is Ada for a non-Ada objfile
3755 using GNU index. */
3756 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3757
3758 const char *match_name = name.ada ().lookup_name ().c_str ();
3759 auto matcher = [&] (const char *symname)
3760 {
3761 if (ordered_compare == nullptr)
3762 return true;
3763 return ordered_compare (symname, match_name) == 0;
3764 };
3765
3766 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3767 [&] (offset_type namei)
3768 {
3769 struct dw2_symtab_iterator iter;
3770 struct dwarf2_per_cu_data *per_cu;
3771
3772 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3773 match_name);
3774 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3775 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3776 nullptr);
3777 return true;
3778 }, dwarf2_per_objfile);
3779 }
3780 else
3781 {
3782 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3783 proceed assuming all symtabs have been read in. */
3784 }
3785
3786 for (compunit_symtab *cust : objfile->compunits ())
3787 {
3788 const struct block *block;
3789
3790 if (cust == NULL)
3791 continue;
3792 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3793 if (!iterate_over_symbols_terminated (block, name,
3794 domain, callback))
3795 return;
3796 }
3797 }
3798
3799 /* Starting from a search name, return the string that finds the upper
3800 bound of all strings that start with SEARCH_NAME in a sorted name
3801 list. Returns the empty string to indicate that the upper bound is
3802 the end of the list. */
3803
3804 static std::string
3805 make_sort_after_prefix_name (const char *search_name)
3806 {
3807 /* When looking to complete "func", we find the upper bound of all
3808 symbols that start with "func" by looking for where we'd insert
3809 the closest string that would follow "func" in lexicographical
3810 order. Usually, that's "func"-with-last-character-incremented,
3811 i.e. "fund". Mind non-ASCII characters, though. Usually those
3812 will be UTF-8 multi-byte sequences, but we can't be certain.
3813 Especially mind the 0xff character, which is a valid character in
3814 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3815 rule out compilers allowing it in identifiers. Note that
3816 conveniently, strcmp/strcasecmp are specified to compare
3817 characters interpreted as unsigned char. So what we do is treat
3818 the whole string as a base 256 number composed of a sequence of
3819 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3820 to 0, and carries 1 to the following more-significant position.
3821 If the very first character in SEARCH_NAME ends up incremented
3822 and carries/overflows, then the upper bound is the end of the
3823 list. The string after the empty string is also the empty
3824 string.
3825
3826 Some examples of this operation:
3827
3828 SEARCH_NAME => "+1" RESULT
3829
3830 "abc" => "abd"
3831 "ab\xff" => "ac"
3832 "\xff" "a" "\xff" => "\xff" "b"
3833 "\xff" => ""
3834 "\xff\xff" => ""
3835 "" => ""
3836
3837 Then, with these symbols for example:
3838
3839 func
3840 func1
3841 fund
3842
3843 completing "func" looks for symbols between "func" and
3844 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3845 which finds "func" and "func1", but not "fund".
3846
3847 And with:
3848
3849 funcÿ (Latin1 'ÿ' [0xff])
3850 funcÿ1
3851 fund
3852
3853 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3854 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3855
3856 And with:
3857
3858 ÿÿ (Latin1 'ÿ' [0xff])
3859 ÿÿ1
3860
3861 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3862 the end of the list.
3863 */
3864 std::string after = search_name;
3865 while (!after.empty () && (unsigned char) after.back () == 0xff)
3866 after.pop_back ();
3867 if (!after.empty ())
3868 after.back () = (unsigned char) after.back () + 1;
3869 return after;
3870 }
3871
3872 /* See declaration. */
3873
3874 std::pair<std::vector<name_component>::const_iterator,
3875 std::vector<name_component>::const_iterator>
3876 mapped_index_base::find_name_components_bounds
3877 (const lookup_name_info &lookup_name_without_params, language lang,
3878 dwarf2_per_objfile *per_objfile) const
3879 {
3880 auto *name_cmp
3881 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3882
3883 const char *lang_name
3884 = lookup_name_without_params.language_lookup_name (lang);
3885
3886 /* Comparison function object for lower_bound that matches against a
3887 given symbol name. */
3888 auto lookup_compare_lower = [&] (const name_component &elem,
3889 const char *name)
3890 {
3891 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3892 const char *elem_name = elem_qualified + elem.name_offset;
3893 return name_cmp (elem_name, name) < 0;
3894 };
3895
3896 /* Comparison function object for upper_bound that matches against a
3897 given symbol name. */
3898 auto lookup_compare_upper = [&] (const char *name,
3899 const name_component &elem)
3900 {
3901 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3902 const char *elem_name = elem_qualified + elem.name_offset;
3903 return name_cmp (name, elem_name) < 0;
3904 };
3905
3906 auto begin = this->name_components.begin ();
3907 auto end = this->name_components.end ();
3908
3909 /* Find the lower bound. */
3910 auto lower = [&] ()
3911 {
3912 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3913 return begin;
3914 else
3915 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3916 } ();
3917
3918 /* Find the upper bound. */
3919 auto upper = [&] ()
3920 {
3921 if (lookup_name_without_params.completion_mode ())
3922 {
3923 /* In completion mode, we want UPPER to point past all
3924 symbols names that have the same prefix. I.e., with
3925 these symbols, and completing "func":
3926
3927 function << lower bound
3928 function1
3929 other_function << upper bound
3930
3931 We find the upper bound by looking for the insertion
3932 point of "func"-with-last-character-incremented,
3933 i.e. "fund". */
3934 std::string after = make_sort_after_prefix_name (lang_name);
3935 if (after.empty ())
3936 return end;
3937 return std::lower_bound (lower, end, after.c_str (),
3938 lookup_compare_lower);
3939 }
3940 else
3941 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3942 } ();
3943
3944 return {lower, upper};
3945 }
3946
3947 /* See declaration. */
3948
3949 void
3950 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3951 {
3952 if (!this->name_components.empty ())
3953 return;
3954
3955 this->name_components_casing = case_sensitivity;
3956 auto *name_cmp
3957 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3958
3959 /* The code below only knows how to break apart components of C++
3960 symbol names (and other languages that use '::' as
3961 namespace/module separator) and Ada symbol names. */
3962 auto count = this->symbol_name_count ();
3963 for (offset_type idx = 0; idx < count; idx++)
3964 {
3965 if (this->symbol_name_slot_invalid (idx))
3966 continue;
3967
3968 const char *name = this->symbol_name_at (idx, per_objfile);
3969
3970 /* Add each name component to the name component table. */
3971 unsigned int previous_len = 0;
3972
3973 if (strstr (name, "::") != nullptr)
3974 {
3975 for (unsigned int current_len = cp_find_first_component (name);
3976 name[current_len] != '\0';
3977 current_len += cp_find_first_component (name + current_len))
3978 {
3979 gdb_assert (name[current_len] == ':');
3980 this->name_components.push_back ({previous_len, idx});
3981 /* Skip the '::'. */
3982 current_len += 2;
3983 previous_len = current_len;
3984 }
3985 }
3986 else
3987 {
3988 /* Handle the Ada encoded (aka mangled) form here. */
3989 for (const char *iter = strstr (name, "__");
3990 iter != nullptr;
3991 iter = strstr (iter, "__"))
3992 {
3993 this->name_components.push_back ({previous_len, idx});
3994 iter += 2;
3995 previous_len = iter - name;
3996 }
3997 }
3998
3999 this->name_components.push_back ({previous_len, idx});
4000 }
4001
4002 /* Sort name_components elements by name. */
4003 auto name_comp_compare = [&] (const name_component &left,
4004 const name_component &right)
4005 {
4006 const char *left_qualified
4007 = this->symbol_name_at (left.idx, per_objfile);
4008 const char *right_qualified
4009 = this->symbol_name_at (right.idx, per_objfile);
4010
4011 const char *left_name = left_qualified + left.name_offset;
4012 const char *right_name = right_qualified + right.name_offset;
4013
4014 return name_cmp (left_name, right_name) < 0;
4015 };
4016
4017 std::sort (this->name_components.begin (),
4018 this->name_components.end (),
4019 name_comp_compare);
4020 }
4021
4022 /* Helper for dw2_expand_symtabs_matching that works with a
4023 mapped_index_base instead of the containing objfile. This is split
4024 to a separate function in order to be able to unit test the
4025 name_components matching using a mock mapped_index_base. For each
4026 symbol name that matches, calls MATCH_CALLBACK, passing it the
4027 symbol's index in the mapped_index_base symbol table. */
4028
4029 static void
4030 dw2_expand_symtabs_matching_symbol
4031 (mapped_index_base &index,
4032 const lookup_name_info &lookup_name_in,
4033 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4034 enum search_domain kind,
4035 gdb::function_view<bool (offset_type)> match_callback,
4036 dwarf2_per_objfile *per_objfile)
4037 {
4038 lookup_name_info lookup_name_without_params
4039 = lookup_name_in.make_ignore_params ();
4040
4041 /* Build the symbol name component sorted vector, if we haven't
4042 yet. */
4043 index.build_name_components (per_objfile);
4044
4045 /* The same symbol may appear more than once in the range though.
4046 E.g., if we're looking for symbols that complete "w", and we have
4047 a symbol named "w1::w2", we'll find the two name components for
4048 that same symbol in the range. To be sure we only call the
4049 callback once per symbol, we first collect the symbol name
4050 indexes that matched in a temporary vector and ignore
4051 duplicates. */
4052 std::vector<offset_type> matches;
4053
4054 struct name_and_matcher
4055 {
4056 symbol_name_matcher_ftype *matcher;
4057 const char *name;
4058
4059 bool operator== (const name_and_matcher &other) const
4060 {
4061 return matcher == other.matcher && strcmp (name, other.name) == 0;
4062 }
4063 };
4064
4065 /* A vector holding all the different symbol name matchers, for all
4066 languages. */
4067 std::vector<name_and_matcher> matchers;
4068
4069 for (int i = 0; i < nr_languages; i++)
4070 {
4071 enum language lang_e = (enum language) i;
4072
4073 const language_defn *lang = language_def (lang_e);
4074 symbol_name_matcher_ftype *name_matcher
4075 = get_symbol_name_matcher (lang, lookup_name_without_params);
4076
4077 name_and_matcher key {
4078 name_matcher,
4079 lookup_name_without_params.language_lookup_name (lang_e)
4080 };
4081
4082 /* Don't insert the same comparison routine more than once.
4083 Note that we do this linear walk. This is not a problem in
4084 practice because the number of supported languages is
4085 low. */
4086 if (std::find (matchers.begin (), matchers.end (), key)
4087 != matchers.end ())
4088 continue;
4089 matchers.push_back (std::move (key));
4090
4091 auto bounds
4092 = index.find_name_components_bounds (lookup_name_without_params,
4093 lang_e, per_objfile);
4094
4095 /* Now for each symbol name in range, check to see if we have a name
4096 match, and if so, call the MATCH_CALLBACK callback. */
4097
4098 for (; bounds.first != bounds.second; ++bounds.first)
4099 {
4100 const char *qualified
4101 = index.symbol_name_at (bounds.first->idx, per_objfile);
4102
4103 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4104 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4105 continue;
4106
4107 matches.push_back (bounds.first->idx);
4108 }
4109 }
4110
4111 std::sort (matches.begin (), matches.end ());
4112
4113 /* Finally call the callback, once per match. */
4114 ULONGEST prev = -1;
4115 for (offset_type idx : matches)
4116 {
4117 if (prev != idx)
4118 {
4119 if (!match_callback (idx))
4120 break;
4121 prev = idx;
4122 }
4123 }
4124
4125 /* Above we use a type wider than idx's for 'prev', since 0 and
4126 (offset_type)-1 are both possible values. */
4127 static_assert (sizeof (prev) > sizeof (offset_type), "");
4128 }
4129
4130 #if GDB_SELF_TEST
4131
4132 namespace selftests { namespace dw2_expand_symtabs_matching {
4133
4134 /* A mock .gdb_index/.debug_names-like name index table, enough to
4135 exercise dw2_expand_symtabs_matching_symbol, which works with the
4136 mapped_index_base interface. Builds an index from the symbol list
4137 passed as parameter to the constructor. */
4138 class mock_mapped_index : public mapped_index_base
4139 {
4140 public:
4141 mock_mapped_index (gdb::array_view<const char *> symbols)
4142 : m_symbol_table (symbols)
4143 {}
4144
4145 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4146
4147 /* Return the number of names in the symbol table. */
4148 size_t symbol_name_count () const override
4149 {
4150 return m_symbol_table.size ();
4151 }
4152
4153 /* Get the name of the symbol at IDX in the symbol table. */
4154 const char *symbol_name_at
4155 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4156 {
4157 return m_symbol_table[idx];
4158 }
4159
4160 private:
4161 gdb::array_view<const char *> m_symbol_table;
4162 };
4163
4164 /* Convenience function that converts a NULL pointer to a "<null>"
4165 string, to pass to print routines. */
4166
4167 static const char *
4168 string_or_null (const char *str)
4169 {
4170 return str != NULL ? str : "<null>";
4171 }
4172
4173 /* Check if a lookup_name_info built from
4174 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4175 index. EXPECTED_LIST is the list of expected matches, in expected
4176 matching order. If no match expected, then an empty list is
4177 specified. Returns true on success. On failure prints a warning
4178 indicating the file:line that failed, and returns false. */
4179
4180 static bool
4181 check_match (const char *file, int line,
4182 mock_mapped_index &mock_index,
4183 const char *name, symbol_name_match_type match_type,
4184 bool completion_mode,
4185 std::initializer_list<const char *> expected_list,
4186 dwarf2_per_objfile *per_objfile)
4187 {
4188 lookup_name_info lookup_name (name, match_type, completion_mode);
4189
4190 bool matched = true;
4191
4192 auto mismatch = [&] (const char *expected_str,
4193 const char *got)
4194 {
4195 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4196 "expected=\"%s\", got=\"%s\"\n"),
4197 file, line,
4198 (match_type == symbol_name_match_type::FULL
4199 ? "FULL" : "WILD"),
4200 name, string_or_null (expected_str), string_or_null (got));
4201 matched = false;
4202 };
4203
4204 auto expected_it = expected_list.begin ();
4205 auto expected_end = expected_list.end ();
4206
4207 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4208 NULL, ALL_DOMAIN,
4209 [&] (offset_type idx)
4210 {
4211 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4212 const char *expected_str
4213 = expected_it == expected_end ? NULL : *expected_it++;
4214
4215 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4216 mismatch (expected_str, matched_name);
4217 return true;
4218 }, per_objfile);
4219
4220 const char *expected_str
4221 = expected_it == expected_end ? NULL : *expected_it++;
4222 if (expected_str != NULL)
4223 mismatch (expected_str, NULL);
4224
4225 return matched;
4226 }
4227
4228 /* The symbols added to the mock mapped_index for testing (in
4229 canonical form). */
4230 static const char *test_symbols[] = {
4231 "function",
4232 "std::bar",
4233 "std::zfunction",
4234 "std::zfunction2",
4235 "w1::w2",
4236 "ns::foo<char*>",
4237 "ns::foo<int>",
4238 "ns::foo<long>",
4239 "ns2::tmpl<int>::foo2",
4240 "(anonymous namespace)::A::B::C",
4241
4242 /* These are used to check that the increment-last-char in the
4243 matching algorithm for completion doesn't match "t1_fund" when
4244 completing "t1_func". */
4245 "t1_func",
4246 "t1_func1",
4247 "t1_fund",
4248 "t1_fund1",
4249
4250 /* A UTF-8 name with multi-byte sequences to make sure that
4251 cp-name-parser understands this as a single identifier ("função"
4252 is "function" in PT). */
4253 u8"u8função",
4254
4255 /* \377 (0xff) is Latin1 'ÿ'. */
4256 "yfunc\377",
4257
4258 /* \377 (0xff) is Latin1 'ÿ'. */
4259 "\377",
4260 "\377\377123",
4261
4262 /* A name with all sorts of complications. Starts with "z" to make
4263 it easier for the completion tests below. */
4264 #define Z_SYM_NAME \
4265 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4266 "::tuple<(anonymous namespace)::ui*, " \
4267 "std::default_delete<(anonymous namespace)::ui>, void>"
4268
4269 Z_SYM_NAME
4270 };
4271
4272 /* Returns true if the mapped_index_base::find_name_component_bounds
4273 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4274 in completion mode. */
4275
4276 static bool
4277 check_find_bounds_finds (mapped_index_base &index,
4278 const char *search_name,
4279 gdb::array_view<const char *> expected_syms,
4280 dwarf2_per_objfile *per_objfile)
4281 {
4282 lookup_name_info lookup_name (search_name,
4283 symbol_name_match_type::FULL, true);
4284
4285 auto bounds = index.find_name_components_bounds (lookup_name,
4286 language_cplus,
4287 per_objfile);
4288
4289 size_t distance = std::distance (bounds.first, bounds.second);
4290 if (distance != expected_syms.size ())
4291 return false;
4292
4293 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4294 {
4295 auto nc_elem = bounds.first + exp_elem;
4296 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4297 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4298 return false;
4299 }
4300
4301 return true;
4302 }
4303
4304 /* Test the lower-level mapped_index::find_name_component_bounds
4305 method. */
4306
4307 static void
4308 test_mapped_index_find_name_component_bounds ()
4309 {
4310 mock_mapped_index mock_index (test_symbols);
4311
4312 mock_index.build_name_components (NULL /* per_objfile */);
4313
4314 /* Test the lower-level mapped_index::find_name_component_bounds
4315 method in completion mode. */
4316 {
4317 static const char *expected_syms[] = {
4318 "t1_func",
4319 "t1_func1",
4320 };
4321
4322 SELF_CHECK (check_find_bounds_finds
4323 (mock_index, "t1_func", expected_syms,
4324 NULL /* per_objfile */));
4325 }
4326
4327 /* Check that the increment-last-char in the name matching algorithm
4328 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4329 {
4330 static const char *expected_syms1[] = {
4331 "\377",
4332 "\377\377123",
4333 };
4334 SELF_CHECK (check_find_bounds_finds
4335 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4336
4337 static const char *expected_syms2[] = {
4338 "\377\377123",
4339 };
4340 SELF_CHECK (check_find_bounds_finds
4341 (mock_index, "\377\377", expected_syms2,
4342 NULL /* per_objfile */));
4343 }
4344 }
4345
4346 /* Test dw2_expand_symtabs_matching_symbol. */
4347
4348 static void
4349 test_dw2_expand_symtabs_matching_symbol ()
4350 {
4351 mock_mapped_index mock_index (test_symbols);
4352
4353 /* We let all tests run until the end even if some fails, for debug
4354 convenience. */
4355 bool any_mismatch = false;
4356
4357 /* Create the expected symbols list (an initializer_list). Needed
4358 because lists have commas, and we need to pass them to CHECK,
4359 which is a macro. */
4360 #define EXPECT(...) { __VA_ARGS__ }
4361
4362 /* Wrapper for check_match that passes down the current
4363 __FILE__/__LINE__. */
4364 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4365 any_mismatch |= !check_match (__FILE__, __LINE__, \
4366 mock_index, \
4367 NAME, MATCH_TYPE, COMPLETION_MODE, \
4368 EXPECTED_LIST, NULL)
4369
4370 /* Identity checks. */
4371 for (const char *sym : test_symbols)
4372 {
4373 /* Should be able to match all existing symbols. */
4374 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4375 EXPECT (sym));
4376
4377 /* Should be able to match all existing symbols with
4378 parameters. */
4379 std::string with_params = std::string (sym) + "(int)";
4380 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4381 EXPECT (sym));
4382
4383 /* Should be able to match all existing symbols with
4384 parameters and qualifiers. */
4385 with_params = std::string (sym) + " ( int ) const";
4386 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4387 EXPECT (sym));
4388
4389 /* This should really find sym, but cp-name-parser.y doesn't
4390 know about lvalue/rvalue qualifiers yet. */
4391 with_params = std::string (sym) + " ( int ) &&";
4392 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4393 {});
4394 }
4395
4396 /* Check that the name matching algorithm for completion doesn't get
4397 confused with Latin1 'ÿ' / 0xff. */
4398 {
4399 static const char str[] = "\377";
4400 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4401 EXPECT ("\377", "\377\377123"));
4402 }
4403
4404 /* Check that the increment-last-char in the matching algorithm for
4405 completion doesn't match "t1_fund" when completing "t1_func". */
4406 {
4407 static const char str[] = "t1_func";
4408 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4409 EXPECT ("t1_func", "t1_func1"));
4410 }
4411
4412 /* Check that completion mode works at each prefix of the expected
4413 symbol name. */
4414 {
4415 static const char str[] = "function(int)";
4416 size_t len = strlen (str);
4417 std::string lookup;
4418
4419 for (size_t i = 1; i < len; i++)
4420 {
4421 lookup.assign (str, i);
4422 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4423 EXPECT ("function"));
4424 }
4425 }
4426
4427 /* While "w" is a prefix of both components, the match function
4428 should still only be called once. */
4429 {
4430 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4431 EXPECT ("w1::w2"));
4432 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4433 EXPECT ("w1::w2"));
4434 }
4435
4436 /* Same, with a "complicated" symbol. */
4437 {
4438 static const char str[] = Z_SYM_NAME;
4439 size_t len = strlen (str);
4440 std::string lookup;
4441
4442 for (size_t i = 1; i < len; i++)
4443 {
4444 lookup.assign (str, i);
4445 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4446 EXPECT (Z_SYM_NAME));
4447 }
4448 }
4449
4450 /* In FULL mode, an incomplete symbol doesn't match. */
4451 {
4452 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4453 {});
4454 }
4455
4456 /* A complete symbol with parameters matches any overload, since the
4457 index has no overload info. */
4458 {
4459 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4460 EXPECT ("std::zfunction", "std::zfunction2"));
4461 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4462 EXPECT ("std::zfunction", "std::zfunction2"));
4463 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4464 EXPECT ("std::zfunction", "std::zfunction2"));
4465 }
4466
4467 /* Check that whitespace is ignored appropriately. A symbol with a
4468 template argument list. */
4469 {
4470 static const char expected[] = "ns::foo<int>";
4471 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4472 EXPECT (expected));
4473 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4474 EXPECT (expected));
4475 }
4476
4477 /* Check that whitespace is ignored appropriately. A symbol with a
4478 template argument list that includes a pointer. */
4479 {
4480 static const char expected[] = "ns::foo<char*>";
4481 /* Try both completion and non-completion modes. */
4482 static const bool completion_mode[2] = {false, true};
4483 for (size_t i = 0; i < 2; i++)
4484 {
4485 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4486 completion_mode[i], EXPECT (expected));
4487 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4488 completion_mode[i], EXPECT (expected));
4489
4490 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4491 completion_mode[i], EXPECT (expected));
4492 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4493 completion_mode[i], EXPECT (expected));
4494 }
4495 }
4496
4497 {
4498 /* Check method qualifiers are ignored. */
4499 static const char expected[] = "ns::foo<char*>";
4500 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4501 symbol_name_match_type::FULL, true, EXPECT (expected));
4502 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4503 symbol_name_match_type::FULL, true, EXPECT (expected));
4504 CHECK_MATCH ("foo < char * > ( int ) const",
4505 symbol_name_match_type::WILD, true, EXPECT (expected));
4506 CHECK_MATCH ("foo < char * > ( int ) &&",
4507 symbol_name_match_type::WILD, true, EXPECT (expected));
4508 }
4509
4510 /* Test lookup names that don't match anything. */
4511 {
4512 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4513 {});
4514
4515 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4516 {});
4517 }
4518
4519 /* Some wild matching tests, exercising "(anonymous namespace)",
4520 which should not be confused with a parameter list. */
4521 {
4522 static const char *syms[] = {
4523 "A::B::C",
4524 "B::C",
4525 "C",
4526 "A :: B :: C ( int )",
4527 "B :: C ( int )",
4528 "C ( int )",
4529 };
4530
4531 for (const char *s : syms)
4532 {
4533 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4534 EXPECT ("(anonymous namespace)::A::B::C"));
4535 }
4536 }
4537
4538 {
4539 static const char expected[] = "ns2::tmpl<int>::foo2";
4540 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4541 EXPECT (expected));
4542 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4543 EXPECT (expected));
4544 }
4545
4546 SELF_CHECK (!any_mismatch);
4547
4548 #undef EXPECT
4549 #undef CHECK_MATCH
4550 }
4551
4552 static void
4553 run_test ()
4554 {
4555 test_mapped_index_find_name_component_bounds ();
4556 test_dw2_expand_symtabs_matching_symbol ();
4557 }
4558
4559 }} // namespace selftests::dw2_expand_symtabs_matching
4560
4561 #endif /* GDB_SELF_TEST */
4562
4563 /* If FILE_MATCHER is NULL or if PER_CU has
4564 dwarf2_per_cu_quick_data::MARK set (see
4565 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4566 EXPANSION_NOTIFY on it. */
4567
4568 static void
4569 dw2_expand_symtabs_matching_one
4570 (dwarf2_per_cu_data *per_cu,
4571 dwarf2_per_objfile *per_objfile,
4572 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4573 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4574 {
4575 if (file_matcher == NULL || per_cu->v.quick->mark)
4576 {
4577 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4578
4579 compunit_symtab *symtab
4580 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4581 gdb_assert (symtab != nullptr);
4582
4583 if (expansion_notify != NULL && symtab_was_null)
4584 expansion_notify (symtab);
4585 }
4586 }
4587
4588 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4589 matched, to expand corresponding CUs that were marked. IDX is the
4590 index of the symbol name that matched. */
4591
4592 static void
4593 dw2_expand_marked_cus
4594 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4595 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4596 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4597 search_domain kind)
4598 {
4599 offset_type *vec, vec_len, vec_idx;
4600 bool global_seen = false;
4601 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4602
4603 vec = (offset_type *) (index.constant_pool
4604 + MAYBE_SWAP (index.symbol_table[idx].vec));
4605 vec_len = MAYBE_SWAP (vec[0]);
4606 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4607 {
4608 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4609 /* This value is only valid for index versions >= 7. */
4610 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4611 gdb_index_symbol_kind symbol_kind =
4612 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4613 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4614 /* Only check the symbol attributes if they're present.
4615 Indices prior to version 7 don't record them,
4616 and indices >= 7 may elide them for certain symbols
4617 (gold does this). */
4618 int attrs_valid =
4619 (index.version >= 7
4620 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4621
4622 /* Work around gold/15646. */
4623 if (attrs_valid)
4624 {
4625 if (!is_static && global_seen)
4626 continue;
4627 if (!is_static)
4628 global_seen = true;
4629 }
4630
4631 /* Only check the symbol's kind if it has one. */
4632 if (attrs_valid)
4633 {
4634 switch (kind)
4635 {
4636 case VARIABLES_DOMAIN:
4637 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4638 continue;
4639 break;
4640 case FUNCTIONS_DOMAIN:
4641 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4642 continue;
4643 break;
4644 case TYPES_DOMAIN:
4645 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4646 continue;
4647 break;
4648 case MODULES_DOMAIN:
4649 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4650 continue;
4651 break;
4652 default:
4653 break;
4654 }
4655 }
4656
4657 /* Don't crash on bad data. */
4658 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4659 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4660 {
4661 complaint (_(".gdb_index entry has bad CU index"
4662 " [in module %s]"),
4663 objfile_name (dwarf2_per_objfile->objfile));
4664 continue;
4665 }
4666
4667 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4668 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4669 expansion_notify);
4670 }
4671 }
4672
4673 /* If FILE_MATCHER is non-NULL, set all the
4674 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4675 that match FILE_MATCHER. */
4676
4677 static void
4678 dw_expand_symtabs_matching_file_matcher
4679 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4680 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4681 {
4682 if (file_matcher == NULL)
4683 return;
4684
4685 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4686 htab_eq_pointer,
4687 NULL, xcalloc, xfree));
4688 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4689 htab_eq_pointer,
4690 NULL, xcalloc, xfree));
4691
4692 /* The rule is CUs specify all the files, including those used by
4693 any TU, so there's no need to scan TUs here. */
4694
4695 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4696 {
4697 QUIT;
4698
4699 per_cu->v.quick->mark = 0;
4700
4701 /* We only need to look at symtabs not already expanded. */
4702 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4703 continue;
4704
4705 quick_file_names *file_data
4706 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4707 if (file_data == NULL)
4708 continue;
4709
4710 if (htab_find (visited_not_found.get (), file_data) != NULL)
4711 continue;
4712 else if (htab_find (visited_found.get (), file_data) != NULL)
4713 {
4714 per_cu->v.quick->mark = 1;
4715 continue;
4716 }
4717
4718 for (int j = 0; j < file_data->num_file_names; ++j)
4719 {
4720 const char *this_real_name;
4721
4722 if (file_matcher (file_data->file_names[j], false))
4723 {
4724 per_cu->v.quick->mark = 1;
4725 break;
4726 }
4727
4728 /* Before we invoke realpath, which can get expensive when many
4729 files are involved, do a quick comparison of the basenames. */
4730 if (!basenames_may_differ
4731 && !file_matcher (lbasename (file_data->file_names[j]),
4732 true))
4733 continue;
4734
4735 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4736 file_data, j);
4737 if (file_matcher (this_real_name, false))
4738 {
4739 per_cu->v.quick->mark = 1;
4740 break;
4741 }
4742 }
4743
4744 void **slot = htab_find_slot (per_cu->v.quick->mark
4745 ? visited_found.get ()
4746 : visited_not_found.get (),
4747 file_data, INSERT);
4748 *slot = file_data;
4749 }
4750 }
4751
4752 static void
4753 dw2_expand_symtabs_matching
4754 (struct objfile *objfile,
4755 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4756 const lookup_name_info *lookup_name,
4757 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4758 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4759 enum search_domain kind)
4760 {
4761 struct dwarf2_per_objfile *dwarf2_per_objfile
4762 = get_dwarf2_per_objfile (objfile);
4763
4764 /* index_table is NULL if OBJF_READNOW. */
4765 if (!dwarf2_per_objfile->per_bfd->index_table)
4766 return;
4767
4768 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4769
4770 if (symbol_matcher == NULL && lookup_name == NULL)
4771 {
4772 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4773 {
4774 QUIT;
4775
4776 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4777 file_matcher, expansion_notify);
4778 }
4779 return;
4780 }
4781
4782 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4783
4784 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4785 symbol_matcher,
4786 kind, [&] (offset_type idx)
4787 {
4788 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4789 expansion_notify, kind);
4790 return true;
4791 }, dwarf2_per_objfile);
4792 }
4793
4794 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4795 symtab. */
4796
4797 static struct compunit_symtab *
4798 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4799 CORE_ADDR pc)
4800 {
4801 int i;
4802
4803 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4804 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4805 return cust;
4806
4807 if (cust->includes == NULL)
4808 return NULL;
4809
4810 for (i = 0; cust->includes[i]; ++i)
4811 {
4812 struct compunit_symtab *s = cust->includes[i];
4813
4814 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4815 if (s != NULL)
4816 return s;
4817 }
4818
4819 return NULL;
4820 }
4821
4822 static struct compunit_symtab *
4823 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4824 struct bound_minimal_symbol msymbol,
4825 CORE_ADDR pc,
4826 struct obj_section *section,
4827 int warn_if_readin)
4828 {
4829 struct dwarf2_per_cu_data *data;
4830 struct compunit_symtab *result;
4831
4832 if (!objfile->partial_symtabs->psymtabs_addrmap)
4833 return NULL;
4834
4835 CORE_ADDR baseaddr = objfile->text_section_offset ();
4836 data = (struct dwarf2_per_cu_data *) addrmap_find
4837 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4838 if (!data)
4839 return NULL;
4840
4841 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4842 if (warn_if_readin && per_objfile->symtab_set_p (data))
4843 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4844 paddress (objfile->arch (), pc));
4845
4846 result = recursively_find_pc_sect_compunit_symtab
4847 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4848
4849 gdb_assert (result != NULL);
4850 return result;
4851 }
4852
4853 static void
4854 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4855 void *data, int need_fullname)
4856 {
4857 struct dwarf2_per_objfile *dwarf2_per_objfile
4858 = get_dwarf2_per_objfile (objfile);
4859
4860 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4861 {
4862 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4863
4864 htab_up visited (htab_create_alloc (10,
4865 htab_hash_pointer, htab_eq_pointer,
4866 NULL, xcalloc, xfree));
4867
4868 /* The rule is CUs specify all the files, including those used
4869 by any TU, so there's no need to scan TUs here. We can
4870 ignore file names coming from already-expanded CUs. */
4871
4872 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4873 {
4874 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4875 {
4876 void **slot = htab_find_slot (visited.get (),
4877 per_cu->v.quick->file_names,
4878 INSERT);
4879
4880 *slot = per_cu->v.quick->file_names;
4881 }
4882 }
4883
4884 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4885 {
4886 /* We only need to look at symtabs not already expanded. */
4887 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4888 continue;
4889
4890 quick_file_names *file_data
4891 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4892 if (file_data == NULL)
4893 continue;
4894
4895 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4896 if (*slot)
4897 {
4898 /* Already visited. */
4899 continue;
4900 }
4901 *slot = file_data;
4902
4903 for (int j = 0; j < file_data->num_file_names; ++j)
4904 {
4905 const char *filename = file_data->file_names[j];
4906 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4907 }
4908 }
4909 }
4910
4911 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4912 {
4913 gdb::unique_xmalloc_ptr<char> this_real_name;
4914
4915 if (need_fullname)
4916 this_real_name = gdb_realpath (filename);
4917 (*fun) (filename, this_real_name.get (), data);
4918 });
4919 }
4920
4921 static int
4922 dw2_has_symbols (struct objfile *objfile)
4923 {
4924 return 1;
4925 }
4926
4927 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4928 {
4929 dw2_has_symbols,
4930 dw2_find_last_source_symtab,
4931 dw2_forget_cached_source_info,
4932 dw2_map_symtabs_matching_filename,
4933 dw2_lookup_symbol,
4934 NULL,
4935 dw2_print_stats,
4936 dw2_dump,
4937 dw2_expand_symtabs_for_function,
4938 dw2_expand_all_symtabs,
4939 dw2_expand_symtabs_with_fullname,
4940 dw2_map_matching_symbols,
4941 dw2_expand_symtabs_matching,
4942 dw2_find_pc_sect_compunit_symtab,
4943 NULL,
4944 dw2_map_symbol_filenames
4945 };
4946
4947 /* DWARF-5 debug_names reader. */
4948
4949 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4950 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4951
4952 /* A helper function that reads the .debug_names section in SECTION
4953 and fills in MAP. FILENAME is the name of the file containing the
4954 section; it is used for error reporting.
4955
4956 Returns true if all went well, false otherwise. */
4957
4958 static bool
4959 read_debug_names_from_section (struct objfile *objfile,
4960 const char *filename,
4961 struct dwarf2_section_info *section,
4962 mapped_debug_names &map)
4963 {
4964 if (section->empty ())
4965 return false;
4966
4967 /* Older elfutils strip versions could keep the section in the main
4968 executable while splitting it for the separate debug info file. */
4969 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4970 return false;
4971
4972 section->read (objfile);
4973
4974 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4975
4976 const gdb_byte *addr = section->buffer;
4977
4978 bfd *const abfd = section->get_bfd_owner ();
4979
4980 unsigned int bytes_read;
4981 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4982 addr += bytes_read;
4983
4984 map.dwarf5_is_dwarf64 = bytes_read != 4;
4985 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4986 if (bytes_read + length != section->size)
4987 {
4988 /* There may be multiple per-CU indices. */
4989 warning (_("Section .debug_names in %s length %s does not match "
4990 "section length %s, ignoring .debug_names."),
4991 filename, plongest (bytes_read + length),
4992 pulongest (section->size));
4993 return false;
4994 }
4995
4996 /* The version number. */
4997 uint16_t version = read_2_bytes (abfd, addr);
4998 addr += 2;
4999 if (version != 5)
5000 {
5001 warning (_("Section .debug_names in %s has unsupported version %d, "
5002 "ignoring .debug_names."),
5003 filename, version);
5004 return false;
5005 }
5006
5007 /* Padding. */
5008 uint16_t padding = read_2_bytes (abfd, addr);
5009 addr += 2;
5010 if (padding != 0)
5011 {
5012 warning (_("Section .debug_names in %s has unsupported padding %d, "
5013 "ignoring .debug_names."),
5014 filename, padding);
5015 return false;
5016 }
5017
5018 /* comp_unit_count - The number of CUs in the CU list. */
5019 map.cu_count = read_4_bytes (abfd, addr);
5020 addr += 4;
5021
5022 /* local_type_unit_count - The number of TUs in the local TU
5023 list. */
5024 map.tu_count = read_4_bytes (abfd, addr);
5025 addr += 4;
5026
5027 /* foreign_type_unit_count - The number of TUs in the foreign TU
5028 list. */
5029 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5030 addr += 4;
5031 if (foreign_tu_count != 0)
5032 {
5033 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5034 "ignoring .debug_names."),
5035 filename, static_cast<unsigned long> (foreign_tu_count));
5036 return false;
5037 }
5038
5039 /* bucket_count - The number of hash buckets in the hash lookup
5040 table. */
5041 map.bucket_count = read_4_bytes (abfd, addr);
5042 addr += 4;
5043
5044 /* name_count - The number of unique names in the index. */
5045 map.name_count = read_4_bytes (abfd, addr);
5046 addr += 4;
5047
5048 /* abbrev_table_size - The size in bytes of the abbreviations
5049 table. */
5050 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5051 addr += 4;
5052
5053 /* augmentation_string_size - The size in bytes of the augmentation
5054 string. This value is rounded up to a multiple of 4. */
5055 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5056 addr += 4;
5057 map.augmentation_is_gdb = ((augmentation_string_size
5058 == sizeof (dwarf5_augmentation))
5059 && memcmp (addr, dwarf5_augmentation,
5060 sizeof (dwarf5_augmentation)) == 0);
5061 augmentation_string_size += (-augmentation_string_size) & 3;
5062 addr += augmentation_string_size;
5063
5064 /* List of CUs */
5065 map.cu_table_reordered = addr;
5066 addr += map.cu_count * map.offset_size;
5067
5068 /* List of Local TUs */
5069 map.tu_table_reordered = addr;
5070 addr += map.tu_count * map.offset_size;
5071
5072 /* Hash Lookup Table */
5073 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5074 addr += map.bucket_count * 4;
5075 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5076 addr += map.name_count * 4;
5077
5078 /* Name Table */
5079 map.name_table_string_offs_reordered = addr;
5080 addr += map.name_count * map.offset_size;
5081 map.name_table_entry_offs_reordered = addr;
5082 addr += map.name_count * map.offset_size;
5083
5084 const gdb_byte *abbrev_table_start = addr;
5085 for (;;)
5086 {
5087 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5088 addr += bytes_read;
5089 if (index_num == 0)
5090 break;
5091
5092 const auto insertpair
5093 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5094 if (!insertpair.second)
5095 {
5096 warning (_("Section .debug_names in %s has duplicate index %s, "
5097 "ignoring .debug_names."),
5098 filename, pulongest (index_num));
5099 return false;
5100 }
5101 mapped_debug_names::index_val &indexval = insertpair.first->second;
5102 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5103 addr += bytes_read;
5104
5105 for (;;)
5106 {
5107 mapped_debug_names::index_val::attr attr;
5108 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5109 addr += bytes_read;
5110 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5111 addr += bytes_read;
5112 if (attr.form == DW_FORM_implicit_const)
5113 {
5114 attr.implicit_const = read_signed_leb128 (abfd, addr,
5115 &bytes_read);
5116 addr += bytes_read;
5117 }
5118 if (attr.dw_idx == 0 && attr.form == 0)
5119 break;
5120 indexval.attr_vec.push_back (std::move (attr));
5121 }
5122 }
5123 if (addr != abbrev_table_start + abbrev_table_size)
5124 {
5125 warning (_("Section .debug_names in %s has abbreviation_table "
5126 "of size %s vs. written as %u, ignoring .debug_names."),
5127 filename, plongest (addr - abbrev_table_start),
5128 abbrev_table_size);
5129 return false;
5130 }
5131 map.entry_pool = addr;
5132
5133 return true;
5134 }
5135
5136 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5137 list. */
5138
5139 static void
5140 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5141 const mapped_debug_names &map,
5142 dwarf2_section_info &section,
5143 bool is_dwz)
5144 {
5145 if (!map.augmentation_is_gdb)
5146 {
5147 for (uint32_t i = 0; i < map.cu_count; ++i)
5148 {
5149 sect_offset sect_off
5150 = (sect_offset) (extract_unsigned_integer
5151 (map.cu_table_reordered + i * map.offset_size,
5152 map.offset_size,
5153 map.dwarf5_byte_order));
5154 /* We don't know the length of the CU, because the CU list in a
5155 .debug_names index can be incomplete, so we can't use the start of
5156 the next CU as end of this CU. We create the CUs here with length 0,
5157 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5158 dwarf2_per_cu_data *per_cu
5159 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5160 per_bfd->all_comp_units.push_back (per_cu);
5161 }
5162 }
5163
5164 sect_offset sect_off_prev;
5165 for (uint32_t i = 0; i <= map.cu_count; ++i)
5166 {
5167 sect_offset sect_off_next;
5168 if (i < map.cu_count)
5169 {
5170 sect_off_next
5171 = (sect_offset) (extract_unsigned_integer
5172 (map.cu_table_reordered + i * map.offset_size,
5173 map.offset_size,
5174 map.dwarf5_byte_order));
5175 }
5176 else
5177 sect_off_next = (sect_offset) section.size;
5178 if (i >= 1)
5179 {
5180 const ULONGEST length = sect_off_next - sect_off_prev;
5181 dwarf2_per_cu_data *per_cu
5182 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5183 sect_off_prev, length);
5184 per_bfd->all_comp_units.push_back (per_cu);
5185 }
5186 sect_off_prev = sect_off_next;
5187 }
5188 }
5189
5190 /* Read the CU list from the mapped index, and use it to create all
5191 the CU objects for this dwarf2_per_objfile. */
5192
5193 static void
5194 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5195 const mapped_debug_names &map,
5196 const mapped_debug_names &dwz_map)
5197 {
5198 gdb_assert (per_bfd->all_comp_units.empty ());
5199 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5200
5201 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5202 false /* is_dwz */);
5203
5204 if (dwz_map.cu_count == 0)
5205 return;
5206
5207 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5208 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5209 true /* is_dwz */);
5210 }
5211
5212 /* Read .debug_names. If everything went ok, initialize the "quick"
5213 elements of all the CUs and return true. Otherwise, return false. */
5214
5215 static bool
5216 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5217 {
5218 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5219 mapped_debug_names dwz_map;
5220 struct objfile *objfile = dwarf2_per_objfile->objfile;
5221
5222 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5223 &dwarf2_per_objfile->per_bfd->debug_names,
5224 *map))
5225 return false;
5226
5227 /* Don't use the index if it's empty. */
5228 if (map->name_count == 0)
5229 return false;
5230
5231 /* If there is a .dwz file, read it so we can get its CU list as
5232 well. */
5233 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5234 if (dwz != NULL)
5235 {
5236 if (!read_debug_names_from_section (objfile,
5237 bfd_get_filename (dwz->dwz_bfd.get ()),
5238 &dwz->debug_names, dwz_map))
5239 {
5240 warning (_("could not read '.debug_names' section from %s; skipping"),
5241 bfd_get_filename (dwz->dwz_bfd.get ()));
5242 return false;
5243 }
5244 }
5245
5246 create_cus_from_debug_names (dwarf2_per_objfile->per_bfd, *map, dwz_map);
5247
5248 if (map->tu_count != 0)
5249 {
5250 /* We can only handle a single .debug_types when we have an
5251 index. */
5252 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5253 return false;
5254
5255 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5256
5257 create_signatured_type_table_from_debug_names
5258 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5259 }
5260
5261 create_addrmap_from_aranges (dwarf2_per_objfile,
5262 &dwarf2_per_objfile->per_bfd->debug_aranges);
5263
5264 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5265 dwarf2_per_objfile->per_bfd->using_index = 1;
5266 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5267 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5268
5269 return true;
5270 }
5271
5272 /* Type used to manage iterating over all CUs looking for a symbol for
5273 .debug_names. */
5274
5275 class dw2_debug_names_iterator
5276 {
5277 public:
5278 dw2_debug_names_iterator (const mapped_debug_names &map,
5279 gdb::optional<block_enum> block_index,
5280 domain_enum domain,
5281 const char *name, dwarf2_per_objfile *per_objfile)
5282 : m_map (map), m_block_index (block_index), m_domain (domain),
5283 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5284 m_per_objfile (per_objfile)
5285 {}
5286
5287 dw2_debug_names_iterator (const mapped_debug_names &map,
5288 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5289 : m_map (map),
5290 m_search (search),
5291 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5292 m_per_objfile (per_objfile)
5293 {}
5294
5295 dw2_debug_names_iterator (const mapped_debug_names &map,
5296 block_enum block_index, domain_enum domain,
5297 uint32_t namei, dwarf2_per_objfile *per_objfile)
5298 : m_map (map), m_block_index (block_index), m_domain (domain),
5299 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5300 m_per_objfile (per_objfile)
5301 {}
5302
5303 /* Return the next matching CU or NULL if there are no more. */
5304 dwarf2_per_cu_data *next ();
5305
5306 private:
5307 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5308 const char *name, dwarf2_per_objfile *per_objfile);
5309 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5310 uint32_t namei, dwarf2_per_objfile *per_objfile);
5311
5312 /* The internalized form of .debug_names. */
5313 const mapped_debug_names &m_map;
5314
5315 /* If set, only look for symbols that match that block. Valid values are
5316 GLOBAL_BLOCK and STATIC_BLOCK. */
5317 const gdb::optional<block_enum> m_block_index;
5318
5319 /* The kind of symbol we're looking for. */
5320 const domain_enum m_domain = UNDEF_DOMAIN;
5321 const search_domain m_search = ALL_DOMAIN;
5322
5323 /* The list of CUs from the index entry of the symbol, or NULL if
5324 not found. */
5325 const gdb_byte *m_addr;
5326
5327 dwarf2_per_objfile *m_per_objfile;
5328 };
5329
5330 const char *
5331 mapped_debug_names::namei_to_name
5332 (uint32_t namei, dwarf2_per_objfile *dwarf2_per_objfile) const
5333 {
5334 const ULONGEST namei_string_offs
5335 = extract_unsigned_integer ((name_table_string_offs_reordered
5336 + namei * offset_size),
5337 offset_size,
5338 dwarf5_byte_order);
5339 return read_indirect_string_at_offset (dwarf2_per_objfile,
5340 namei_string_offs);
5341 }
5342
5343 /* Find a slot in .debug_names for the object named NAME. If NAME is
5344 found, return pointer to its pool data. If NAME cannot be found,
5345 return NULL. */
5346
5347 const gdb_byte *
5348 dw2_debug_names_iterator::find_vec_in_debug_names
5349 (const mapped_debug_names &map, const char *name, dwarf2_per_objfile *per_objfile)
5350 {
5351 int (*cmp) (const char *, const char *);
5352
5353 gdb::unique_xmalloc_ptr<char> without_params;
5354 if (current_language->la_language == language_cplus
5355 || current_language->la_language == language_fortran
5356 || current_language->la_language == language_d)
5357 {
5358 /* NAME is already canonical. Drop any qualifiers as
5359 .debug_names does not contain any. */
5360
5361 if (strchr (name, '(') != NULL)
5362 {
5363 without_params = cp_remove_params (name);
5364 if (without_params != NULL)
5365 name = without_params.get ();
5366 }
5367 }
5368
5369 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5370
5371 const uint32_t full_hash = dwarf5_djb_hash (name);
5372 uint32_t namei
5373 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5374 (map.bucket_table_reordered
5375 + (full_hash % map.bucket_count)), 4,
5376 map.dwarf5_byte_order);
5377 if (namei == 0)
5378 return NULL;
5379 --namei;
5380 if (namei >= map.name_count)
5381 {
5382 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5383 "[in module %s]"),
5384 namei, map.name_count,
5385 objfile_name (per_objfile->objfile));
5386 return NULL;
5387 }
5388
5389 for (;;)
5390 {
5391 const uint32_t namei_full_hash
5392 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5393 (map.hash_table_reordered + namei), 4,
5394 map.dwarf5_byte_order);
5395 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5396 return NULL;
5397
5398 if (full_hash == namei_full_hash)
5399 {
5400 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5401
5402 #if 0 /* An expensive sanity check. */
5403 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5404 {
5405 complaint (_("Wrong .debug_names hash for string at index %u "
5406 "[in module %s]"),
5407 namei, objfile_name (dwarf2_per_objfile->objfile));
5408 return NULL;
5409 }
5410 #endif
5411
5412 if (cmp (namei_string, name) == 0)
5413 {
5414 const ULONGEST namei_entry_offs
5415 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5416 + namei * map.offset_size),
5417 map.offset_size, map.dwarf5_byte_order);
5418 return map.entry_pool + namei_entry_offs;
5419 }
5420 }
5421
5422 ++namei;
5423 if (namei >= map.name_count)
5424 return NULL;
5425 }
5426 }
5427
5428 const gdb_byte *
5429 dw2_debug_names_iterator::find_vec_in_debug_names
5430 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5431 {
5432 if (namei >= map.name_count)
5433 {
5434 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5435 "[in module %s]"),
5436 namei, map.name_count,
5437 objfile_name (per_objfile->objfile));
5438 return NULL;
5439 }
5440
5441 const ULONGEST namei_entry_offs
5442 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5443 + namei * map.offset_size),
5444 map.offset_size, map.dwarf5_byte_order);
5445 return map.entry_pool + namei_entry_offs;
5446 }
5447
5448 /* See dw2_debug_names_iterator. */
5449
5450 dwarf2_per_cu_data *
5451 dw2_debug_names_iterator::next ()
5452 {
5453 if (m_addr == NULL)
5454 return NULL;
5455
5456 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5457 struct objfile *objfile = m_per_objfile->objfile;
5458 bfd *const abfd = objfile->obfd;
5459
5460 again:
5461
5462 unsigned int bytes_read;
5463 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5464 m_addr += bytes_read;
5465 if (abbrev == 0)
5466 return NULL;
5467
5468 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5469 if (indexval_it == m_map.abbrev_map.cend ())
5470 {
5471 complaint (_("Wrong .debug_names undefined abbrev code %s "
5472 "[in module %s]"),
5473 pulongest (abbrev), objfile_name (objfile));
5474 return NULL;
5475 }
5476 const mapped_debug_names::index_val &indexval = indexval_it->second;
5477 enum class symbol_linkage {
5478 unknown,
5479 static_,
5480 extern_,
5481 } symbol_linkage_ = symbol_linkage::unknown;
5482 dwarf2_per_cu_data *per_cu = NULL;
5483 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5484 {
5485 ULONGEST ull;
5486 switch (attr.form)
5487 {
5488 case DW_FORM_implicit_const:
5489 ull = attr.implicit_const;
5490 break;
5491 case DW_FORM_flag_present:
5492 ull = 1;
5493 break;
5494 case DW_FORM_udata:
5495 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5496 m_addr += bytes_read;
5497 break;
5498 case DW_FORM_ref4:
5499 ull = read_4_bytes (abfd, m_addr);
5500 m_addr += 4;
5501 break;
5502 case DW_FORM_ref8:
5503 ull = read_8_bytes (abfd, m_addr);
5504 m_addr += 8;
5505 break;
5506 case DW_FORM_ref_sig8:
5507 ull = read_8_bytes (abfd, m_addr);
5508 m_addr += 8;
5509 break;
5510 default:
5511 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5512 dwarf_form_name (attr.form),
5513 objfile_name (objfile));
5514 return NULL;
5515 }
5516 switch (attr.dw_idx)
5517 {
5518 case DW_IDX_compile_unit:
5519 /* Don't crash on bad data. */
5520 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5521 {
5522 complaint (_(".debug_names entry has bad CU index %s"
5523 " [in module %s]"),
5524 pulongest (ull),
5525 objfile_name (objfile));
5526 continue;
5527 }
5528 per_cu = per_bfd->get_cutu (ull);
5529 break;
5530 case DW_IDX_type_unit:
5531 /* Don't crash on bad data. */
5532 if (ull >= per_bfd->all_type_units.size ())
5533 {
5534 complaint (_(".debug_names entry has bad TU index %s"
5535 " [in module %s]"),
5536 pulongest (ull),
5537 objfile_name (objfile));
5538 continue;
5539 }
5540 per_cu = &per_bfd->get_tu (ull)->per_cu;
5541 break;
5542 case DW_IDX_die_offset:
5543 /* In a per-CU index (as opposed to a per-module index), index
5544 entries without CU attribute implicitly refer to the single CU. */
5545 if (per_cu == NULL)
5546 per_cu = per_bfd->get_cu (0);
5547 break;
5548 case DW_IDX_GNU_internal:
5549 if (!m_map.augmentation_is_gdb)
5550 break;
5551 symbol_linkage_ = symbol_linkage::static_;
5552 break;
5553 case DW_IDX_GNU_external:
5554 if (!m_map.augmentation_is_gdb)
5555 break;
5556 symbol_linkage_ = symbol_linkage::extern_;
5557 break;
5558 }
5559 }
5560
5561 /* Skip if already read in. */
5562 if (m_per_objfile->symtab_set_p (per_cu))
5563 goto again;
5564
5565 /* Check static vs global. */
5566 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5567 {
5568 const bool want_static = *m_block_index == STATIC_BLOCK;
5569 const bool symbol_is_static =
5570 symbol_linkage_ == symbol_linkage::static_;
5571 if (want_static != symbol_is_static)
5572 goto again;
5573 }
5574
5575 /* Match dw2_symtab_iter_next, symbol_kind
5576 and debug_names::psymbol_tag. */
5577 switch (m_domain)
5578 {
5579 case VAR_DOMAIN:
5580 switch (indexval.dwarf_tag)
5581 {
5582 case DW_TAG_variable:
5583 case DW_TAG_subprogram:
5584 /* Some types are also in VAR_DOMAIN. */
5585 case DW_TAG_typedef:
5586 case DW_TAG_structure_type:
5587 break;
5588 default:
5589 goto again;
5590 }
5591 break;
5592 case STRUCT_DOMAIN:
5593 switch (indexval.dwarf_tag)
5594 {
5595 case DW_TAG_typedef:
5596 case DW_TAG_structure_type:
5597 break;
5598 default:
5599 goto again;
5600 }
5601 break;
5602 case LABEL_DOMAIN:
5603 switch (indexval.dwarf_tag)
5604 {
5605 case 0:
5606 case DW_TAG_variable:
5607 break;
5608 default:
5609 goto again;
5610 }
5611 break;
5612 case MODULE_DOMAIN:
5613 switch (indexval.dwarf_tag)
5614 {
5615 case DW_TAG_module:
5616 break;
5617 default:
5618 goto again;
5619 }
5620 break;
5621 default:
5622 break;
5623 }
5624
5625 /* Match dw2_expand_symtabs_matching, symbol_kind and
5626 debug_names::psymbol_tag. */
5627 switch (m_search)
5628 {
5629 case VARIABLES_DOMAIN:
5630 switch (indexval.dwarf_tag)
5631 {
5632 case DW_TAG_variable:
5633 break;
5634 default:
5635 goto again;
5636 }
5637 break;
5638 case FUNCTIONS_DOMAIN:
5639 switch (indexval.dwarf_tag)
5640 {
5641 case DW_TAG_subprogram:
5642 break;
5643 default:
5644 goto again;
5645 }
5646 break;
5647 case TYPES_DOMAIN:
5648 switch (indexval.dwarf_tag)
5649 {
5650 case DW_TAG_typedef:
5651 case DW_TAG_structure_type:
5652 break;
5653 default:
5654 goto again;
5655 }
5656 break;
5657 case MODULES_DOMAIN:
5658 switch (indexval.dwarf_tag)
5659 {
5660 case DW_TAG_module:
5661 break;
5662 default:
5663 goto again;
5664 }
5665 default:
5666 break;
5667 }
5668
5669 return per_cu;
5670 }
5671
5672 static struct compunit_symtab *
5673 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5674 const char *name, domain_enum domain)
5675 {
5676 struct dwarf2_per_objfile *dwarf2_per_objfile
5677 = get_dwarf2_per_objfile (objfile);
5678
5679 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5680 if (!mapp)
5681 {
5682 /* index is NULL if OBJF_READNOW. */
5683 return NULL;
5684 }
5685 const auto &map = *mapp;
5686
5687 dw2_debug_names_iterator iter (map, block_index, domain, name,
5688 dwarf2_per_objfile);
5689
5690 struct compunit_symtab *stab_best = NULL;
5691 struct dwarf2_per_cu_data *per_cu;
5692 while ((per_cu = iter.next ()) != NULL)
5693 {
5694 struct symbol *sym, *with_opaque = NULL;
5695 compunit_symtab *stab
5696 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5697 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5698 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5699
5700 sym = block_find_symbol (block, name, domain,
5701 block_find_non_opaque_type_preferred,
5702 &with_opaque);
5703
5704 /* Some caution must be observed with overloaded functions and
5705 methods, since the index will not contain any overload
5706 information (but NAME might contain it). */
5707
5708 if (sym != NULL
5709 && strcmp_iw (sym->search_name (), name) == 0)
5710 return stab;
5711 if (with_opaque != NULL
5712 && strcmp_iw (with_opaque->search_name (), name) == 0)
5713 stab_best = stab;
5714
5715 /* Keep looking through other CUs. */
5716 }
5717
5718 return stab_best;
5719 }
5720
5721 /* This dumps minimal information about .debug_names. It is called
5722 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5723 uses this to verify that .debug_names has been loaded. */
5724
5725 static void
5726 dw2_debug_names_dump (struct objfile *objfile)
5727 {
5728 struct dwarf2_per_objfile *dwarf2_per_objfile
5729 = get_dwarf2_per_objfile (objfile);
5730
5731 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5732 printf_filtered (".debug_names:");
5733 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5734 printf_filtered (" exists\n");
5735 else
5736 printf_filtered (" faked for \"readnow\"\n");
5737 printf_filtered ("\n");
5738 }
5739
5740 static void
5741 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5742 const char *func_name)
5743 {
5744 struct dwarf2_per_objfile *dwarf2_per_objfile
5745 = get_dwarf2_per_objfile (objfile);
5746
5747 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5748 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5749 {
5750 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5751
5752 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5753 dwarf2_per_objfile);
5754
5755 struct dwarf2_per_cu_data *per_cu;
5756 while ((per_cu = iter.next ()) != NULL)
5757 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5758 }
5759 }
5760
5761 static void
5762 dw2_debug_names_map_matching_symbols
5763 (struct objfile *objfile,
5764 const lookup_name_info &name, domain_enum domain,
5765 int global,
5766 gdb::function_view<symbol_found_callback_ftype> callback,
5767 symbol_compare_ftype *ordered_compare)
5768 {
5769 struct dwarf2_per_objfile *dwarf2_per_objfile
5770 = get_dwarf2_per_objfile (objfile);
5771
5772 /* debug_names_table is NULL if OBJF_READNOW. */
5773 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5774 return;
5775
5776 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5777 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5778
5779 const char *match_name = name.ada ().lookup_name ().c_str ();
5780 auto matcher = [&] (const char *symname)
5781 {
5782 if (ordered_compare == nullptr)
5783 return true;
5784 return ordered_compare (symname, match_name) == 0;
5785 };
5786
5787 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5788 [&] (offset_type namei)
5789 {
5790 /* The name was matched, now expand corresponding CUs that were
5791 marked. */
5792 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5793 dwarf2_per_objfile);
5794
5795 struct dwarf2_per_cu_data *per_cu;
5796 while ((per_cu = iter.next ()) != NULL)
5797 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5798 nullptr);
5799 return true;
5800 }, dwarf2_per_objfile);
5801
5802 /* It's a shame we couldn't do this inside the
5803 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5804 that have already been expanded. Instead, this loop matches what
5805 the psymtab code does. */
5806 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5807 {
5808 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5809 if (symtab != nullptr)
5810 {
5811 const struct block *block
5812 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5813 if (!iterate_over_symbols_terminated (block, name,
5814 domain, callback))
5815 break;
5816 }
5817 }
5818 }
5819
5820 static void
5821 dw2_debug_names_expand_symtabs_matching
5822 (struct objfile *objfile,
5823 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5824 const lookup_name_info *lookup_name,
5825 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5826 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5827 enum search_domain kind)
5828 {
5829 struct dwarf2_per_objfile *dwarf2_per_objfile
5830 = get_dwarf2_per_objfile (objfile);
5831
5832 /* debug_names_table is NULL if OBJF_READNOW. */
5833 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5834 return;
5835
5836 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5837
5838 if (symbol_matcher == NULL && lookup_name == NULL)
5839 {
5840 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5841 {
5842 QUIT;
5843
5844 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5845 file_matcher, expansion_notify);
5846 }
5847 return;
5848 }
5849
5850 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5851
5852 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5853 symbol_matcher,
5854 kind, [&] (offset_type namei)
5855 {
5856 /* The name was matched, now expand corresponding CUs that were
5857 marked. */
5858 dw2_debug_names_iterator iter (map, kind, namei, dwarf2_per_objfile);
5859
5860 struct dwarf2_per_cu_data *per_cu;
5861 while ((per_cu = iter.next ()) != NULL)
5862 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5863 file_matcher, expansion_notify);
5864 return true;
5865 }, dwarf2_per_objfile);
5866 }
5867
5868 const struct quick_symbol_functions dwarf2_debug_names_functions =
5869 {
5870 dw2_has_symbols,
5871 dw2_find_last_source_symtab,
5872 dw2_forget_cached_source_info,
5873 dw2_map_symtabs_matching_filename,
5874 dw2_debug_names_lookup_symbol,
5875 NULL,
5876 dw2_print_stats,
5877 dw2_debug_names_dump,
5878 dw2_debug_names_expand_symtabs_for_function,
5879 dw2_expand_all_symtabs,
5880 dw2_expand_symtabs_with_fullname,
5881 dw2_debug_names_map_matching_symbols,
5882 dw2_debug_names_expand_symtabs_matching,
5883 dw2_find_pc_sect_compunit_symtab,
5884 NULL,
5885 dw2_map_symbol_filenames
5886 };
5887
5888 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5889 to either a dwarf2_per_bfd or dwz_file object. */
5890
5891 template <typename T>
5892 static gdb::array_view<const gdb_byte>
5893 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5894 {
5895 dwarf2_section_info *section = &section_owner->gdb_index;
5896
5897 if (section->empty ())
5898 return {};
5899
5900 /* Older elfutils strip versions could keep the section in the main
5901 executable while splitting it for the separate debug info file. */
5902 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5903 return {};
5904
5905 section->read (obj);
5906
5907 /* dwarf2_section_info::size is a bfd_size_type, while
5908 gdb::array_view works with size_t. On 32-bit hosts, with
5909 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5910 is 32-bit. So we need an explicit narrowing conversion here.
5911 This is fine, because it's impossible to allocate or mmap an
5912 array/buffer larger than what size_t can represent. */
5913 return gdb::make_array_view (section->buffer, section->size);
5914 }
5915
5916 /* Lookup the index cache for the contents of the index associated to
5917 DWARF2_OBJ. */
5918
5919 static gdb::array_view<const gdb_byte>
5920 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5921 {
5922 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5923 if (build_id == nullptr)
5924 return {};
5925
5926 return global_index_cache.lookup_gdb_index (build_id,
5927 &dwarf2_per_bfd->index_cache_res);
5928 }
5929
5930 /* Same as the above, but for DWZ. */
5931
5932 static gdb::array_view<const gdb_byte>
5933 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5934 {
5935 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5936 if (build_id == nullptr)
5937 return {};
5938
5939 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5940 }
5941
5942 /* See symfile.h. */
5943
5944 bool
5945 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5946 {
5947 struct dwarf2_per_objfile *dwarf2_per_objfile
5948 = get_dwarf2_per_objfile (objfile);
5949 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
5950
5951 /* If we're about to read full symbols, don't bother with the
5952 indices. In this case we also don't care if some other debug
5953 format is making psymtabs, because they are all about to be
5954 expanded anyway. */
5955 if ((objfile->flags & OBJF_READNOW))
5956 {
5957 /* When using READNOW, the using_index flag (set below) indicates that
5958 PER_BFD was already initialized, when we loaded some other objfile. */
5959 if (per_bfd->using_index)
5960 {
5961 *index_kind = dw_index_kind::GDB_INDEX;
5962 dwarf2_per_objfile->resize_symtabs ();
5963 return true;
5964 }
5965
5966 per_bfd->using_index = 1;
5967 create_all_comp_units (dwarf2_per_objfile);
5968 create_all_type_units (dwarf2_per_objfile);
5969 per_bfd->quick_file_names_table
5970 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5971 dwarf2_per_objfile->resize_symtabs ();
5972
5973 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5974 + per_bfd->all_type_units.size ()); ++i)
5975 {
5976 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
5977
5978 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5979 struct dwarf2_per_cu_quick_data);
5980 }
5981
5982 /* Return 1 so that gdb sees the "quick" functions. However,
5983 these functions will be no-ops because we will have expanded
5984 all symtabs. */
5985 *index_kind = dw_index_kind::GDB_INDEX;
5986 return true;
5987 }
5988
5989 /* Was a debug names index already read when we processed an objfile sharing
5990 PER_BFD? */
5991 if (per_bfd->debug_names_table != nullptr)
5992 {
5993 *index_kind = dw_index_kind::DEBUG_NAMES;
5994 dwarf2_per_objfile->resize_symtabs ();
5995 return true;
5996 }
5997
5998 /* Was a GDB index already read when we processed an objfile sharing
5999 PER_BFD? */
6000 if (per_bfd->index_table != nullptr)
6001 {
6002 *index_kind = dw_index_kind::GDB_INDEX;
6003 dwarf2_per_objfile->resize_symtabs ();
6004 return true;
6005 }
6006
6007 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6008 {
6009 *index_kind = dw_index_kind::DEBUG_NAMES;
6010 dwarf2_per_objfile->resize_symtabs ();
6011 return true;
6012 }
6013
6014 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6015 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6016 get_gdb_index_contents_from_section<dwz_file>))
6017 {
6018 *index_kind = dw_index_kind::GDB_INDEX;
6019 dwarf2_per_objfile->resize_symtabs ();
6020 return true;
6021 }
6022
6023 /* ... otherwise, try to find the index in the index cache. */
6024 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6025 get_gdb_index_contents_from_cache,
6026 get_gdb_index_contents_from_cache_dwz))
6027 {
6028 global_index_cache.hit ();
6029 *index_kind = dw_index_kind::GDB_INDEX;
6030 dwarf2_per_objfile->resize_symtabs ();
6031 return true;
6032 }
6033
6034 global_index_cache.miss ();
6035 return false;
6036 }
6037
6038 \f
6039
6040 /* Build a partial symbol table. */
6041
6042 void
6043 dwarf2_build_psymtabs (struct objfile *objfile)
6044 {
6045 struct dwarf2_per_objfile *dwarf2_per_objfile
6046 = get_dwarf2_per_objfile (objfile);
6047 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6048
6049 if (per_bfd->partial_symtabs != nullptr)
6050 {
6051 /* Partial symbols were already read, so now we can simply
6052 attach them. */
6053 objfile->partial_symtabs = per_bfd->partial_symtabs;
6054 dwarf2_per_objfile->resize_symtabs ();
6055 return;
6056 }
6057
6058 init_psymbol_list (objfile, 1024);
6059
6060 try
6061 {
6062 /* This isn't really ideal: all the data we allocate on the
6063 objfile's obstack is still uselessly kept around. However,
6064 freeing it seems unsafe. */
6065 psymtab_discarder psymtabs (objfile);
6066 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6067 psymtabs.keep ();
6068
6069 dwarf2_per_objfile->resize_symtabs ();
6070
6071 /* (maybe) store an index in the cache. */
6072 global_index_cache.store (dwarf2_per_objfile);
6073 }
6074 catch (const gdb_exception_error &except)
6075 {
6076 exception_print (gdb_stderr, except);
6077 }
6078
6079 /* Finish by setting the local reference to partial symtabs, so that
6080 we don't try to read them again if reading another objfile with the same
6081 BFD. If we can't in fact share, this won't make a difference anyway as
6082 the dwarf2_per_bfd object won't be shared. */
6083 per_bfd->partial_symtabs = objfile->partial_symtabs;
6084 }
6085
6086 /* Find the base address of the compilation unit for range lists and
6087 location lists. It will normally be specified by DW_AT_low_pc.
6088 In DWARF-3 draft 4, the base address could be overridden by
6089 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6090 compilation units with discontinuous ranges. */
6091
6092 static void
6093 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6094 {
6095 struct attribute *attr;
6096
6097 cu->base_address.reset ();
6098
6099 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6100 if (attr != nullptr)
6101 cu->base_address = attr->value_as_address ();
6102 else
6103 {
6104 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6105 if (attr != nullptr)
6106 cu->base_address = attr->value_as_address ();
6107 }
6108 }
6109
6110 /* Helper function that returns the proper abbrev section for
6111 THIS_CU. */
6112
6113 static struct dwarf2_section_info *
6114 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6115 {
6116 struct dwarf2_section_info *abbrev;
6117 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6118
6119 if (this_cu->is_dwz)
6120 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6121 else
6122 abbrev = &per_bfd->abbrev;
6123
6124 return abbrev;
6125 }
6126
6127 /* Fetch the abbreviation table offset from a comp or type unit header. */
6128
6129 static sect_offset
6130 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6131 struct dwarf2_section_info *section,
6132 sect_offset sect_off)
6133 {
6134 bfd *abfd = section->get_bfd_owner ();
6135 const gdb_byte *info_ptr;
6136 unsigned int initial_length_size, offset_size;
6137 uint16_t version;
6138
6139 section->read (dwarf2_per_objfile->objfile);
6140 info_ptr = section->buffer + to_underlying (sect_off);
6141 read_initial_length (abfd, info_ptr, &initial_length_size);
6142 offset_size = initial_length_size == 4 ? 4 : 8;
6143 info_ptr += initial_length_size;
6144
6145 version = read_2_bytes (abfd, info_ptr);
6146 info_ptr += 2;
6147 if (version >= 5)
6148 {
6149 /* Skip unit type and address size. */
6150 info_ptr += 2;
6151 }
6152
6153 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6154 }
6155
6156 /* A partial symtab that is used only for include files. */
6157 struct dwarf2_include_psymtab : public partial_symtab
6158 {
6159 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6160 : partial_symtab (filename, objfile)
6161 {
6162 }
6163
6164 void read_symtab (struct objfile *objfile) override
6165 {
6166 /* It's an include file, no symbols to read for it.
6167 Everything is in the includer symtab. */
6168
6169 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6170 expansion of the includer psymtab. We use the dependencies[0] field to
6171 model the includer. But if we go the regular route of calling
6172 expand_psymtab here, and having expand_psymtab call expand_dependencies
6173 to expand the includer, we'll only use expand_psymtab on the includer
6174 (making it a non-toplevel psymtab), while if we expand the includer via
6175 another path, we'll use read_symtab (making it a toplevel psymtab).
6176 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6177 psymtab, and trigger read_symtab on the includer here directly. */
6178 includer ()->read_symtab (objfile);
6179 }
6180
6181 void expand_psymtab (struct objfile *objfile) override
6182 {
6183 /* This is not called by read_symtab, and should not be called by any
6184 expand_dependencies. */
6185 gdb_assert (false);
6186 }
6187
6188 bool readin_p (struct objfile *objfile) const override
6189 {
6190 return includer ()->readin_p (objfile);
6191 }
6192
6193 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6194 {
6195 return nullptr;
6196 }
6197
6198 private:
6199 partial_symtab *includer () const
6200 {
6201 /* An include psymtab has exactly one dependency: the psymtab that
6202 includes it. */
6203 gdb_assert (this->number_of_dependencies == 1);
6204 return this->dependencies[0];
6205 }
6206 };
6207
6208 /* Allocate a new partial symtab for file named NAME and mark this new
6209 partial symtab as being an include of PST. */
6210
6211 static void
6212 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6213 struct objfile *objfile)
6214 {
6215 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6216
6217 if (!IS_ABSOLUTE_PATH (subpst->filename))
6218 subpst->dirname = pst->dirname;
6219
6220 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6221 subpst->dependencies[0] = pst;
6222 subpst->number_of_dependencies = 1;
6223 }
6224
6225 /* Read the Line Number Program data and extract the list of files
6226 included by the source file represented by PST. Build an include
6227 partial symtab for each of these included files. */
6228
6229 static void
6230 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6231 struct die_info *die,
6232 dwarf2_psymtab *pst)
6233 {
6234 line_header_up lh;
6235 struct attribute *attr;
6236
6237 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6238 if (attr != nullptr)
6239 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6240 if (lh == NULL)
6241 return; /* No linetable, so no includes. */
6242
6243 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6244 that we pass in the raw text_low here; that is ok because we're
6245 only decoding the line table to make include partial symtabs, and
6246 so the addresses aren't really used. */
6247 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6248 pst->raw_text_low (), 1);
6249 }
6250
6251 static hashval_t
6252 hash_signatured_type (const void *item)
6253 {
6254 const struct signatured_type *sig_type
6255 = (const struct signatured_type *) item;
6256
6257 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6258 return sig_type->signature;
6259 }
6260
6261 static int
6262 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6263 {
6264 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6265 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6266
6267 return lhs->signature == rhs->signature;
6268 }
6269
6270 /* Allocate a hash table for signatured types. */
6271
6272 static htab_up
6273 allocate_signatured_type_table ()
6274 {
6275 return htab_up (htab_create_alloc (41,
6276 hash_signatured_type,
6277 eq_signatured_type,
6278 NULL, xcalloc, xfree));
6279 }
6280
6281 /* A helper function to add a signatured type CU to a table. */
6282
6283 static int
6284 add_signatured_type_cu_to_table (void **slot, void *datum)
6285 {
6286 struct signatured_type *sigt = (struct signatured_type *) *slot;
6287 std::vector<signatured_type *> *all_type_units
6288 = (std::vector<signatured_type *> *) datum;
6289
6290 all_type_units->push_back (sigt);
6291
6292 return 1;
6293 }
6294
6295 /* A helper for create_debug_types_hash_table. Read types from SECTION
6296 and fill them into TYPES_HTAB. It will process only type units,
6297 therefore DW_UT_type. */
6298
6299 static void
6300 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6301 struct dwo_file *dwo_file,
6302 dwarf2_section_info *section, htab_up &types_htab,
6303 rcuh_kind section_kind)
6304 {
6305 struct objfile *objfile = dwarf2_per_objfile->objfile;
6306 struct dwarf2_section_info *abbrev_section;
6307 bfd *abfd;
6308 const gdb_byte *info_ptr, *end_ptr;
6309
6310 abbrev_section = (dwo_file != NULL
6311 ? &dwo_file->sections.abbrev
6312 : &dwarf2_per_objfile->per_bfd->abbrev);
6313
6314 if (dwarf_read_debug)
6315 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6316 section->get_name (),
6317 abbrev_section->get_file_name ());
6318
6319 section->read (objfile);
6320 info_ptr = section->buffer;
6321
6322 if (info_ptr == NULL)
6323 return;
6324
6325 /* We can't set abfd until now because the section may be empty or
6326 not present, in which case the bfd is unknown. */
6327 abfd = section->get_bfd_owner ();
6328
6329 /* We don't use cutu_reader here because we don't need to read
6330 any dies: the signature is in the header. */
6331
6332 end_ptr = info_ptr + section->size;
6333 while (info_ptr < end_ptr)
6334 {
6335 struct signatured_type *sig_type;
6336 struct dwo_unit *dwo_tu;
6337 void **slot;
6338 const gdb_byte *ptr = info_ptr;
6339 struct comp_unit_head header;
6340 unsigned int length;
6341
6342 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6343
6344 /* Initialize it due to a false compiler warning. */
6345 header.signature = -1;
6346 header.type_cu_offset_in_tu = (cu_offset) -1;
6347
6348 /* We need to read the type's signature in order to build the hash
6349 table, but we don't need anything else just yet. */
6350
6351 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6352 abbrev_section, ptr, section_kind);
6353
6354 length = header.get_length ();
6355
6356 /* Skip dummy type units. */
6357 if (ptr >= info_ptr + length
6358 || peek_abbrev_code (abfd, ptr) == 0
6359 || header.unit_type != DW_UT_type)
6360 {
6361 info_ptr += length;
6362 continue;
6363 }
6364
6365 if (types_htab == NULL)
6366 {
6367 if (dwo_file)
6368 types_htab = allocate_dwo_unit_table ();
6369 else
6370 types_htab = allocate_signatured_type_table ();
6371 }
6372
6373 if (dwo_file)
6374 {
6375 sig_type = NULL;
6376 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6377 struct dwo_unit);
6378 dwo_tu->dwo_file = dwo_file;
6379 dwo_tu->signature = header.signature;
6380 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6381 dwo_tu->section = section;
6382 dwo_tu->sect_off = sect_off;
6383 dwo_tu->length = length;
6384 }
6385 else
6386 {
6387 /* N.B.: type_offset is not usable if this type uses a DWO file.
6388 The real type_offset is in the DWO file. */
6389 dwo_tu = NULL;
6390 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6391 sig_type->signature = header.signature;
6392 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6393 sig_type->per_cu.is_debug_types = 1;
6394 sig_type->per_cu.section = section;
6395 sig_type->per_cu.sect_off = sect_off;
6396 sig_type->per_cu.length = length;
6397 }
6398
6399 slot = htab_find_slot (types_htab.get (),
6400 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6401 INSERT);
6402 gdb_assert (slot != NULL);
6403 if (*slot != NULL)
6404 {
6405 sect_offset dup_sect_off;
6406
6407 if (dwo_file)
6408 {
6409 const struct dwo_unit *dup_tu
6410 = (const struct dwo_unit *) *slot;
6411
6412 dup_sect_off = dup_tu->sect_off;
6413 }
6414 else
6415 {
6416 const struct signatured_type *dup_tu
6417 = (const struct signatured_type *) *slot;
6418
6419 dup_sect_off = dup_tu->per_cu.sect_off;
6420 }
6421
6422 complaint (_("debug type entry at offset %s is duplicate to"
6423 " the entry at offset %s, signature %s"),
6424 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6425 hex_string (header.signature));
6426 }
6427 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6428
6429 if (dwarf_read_debug > 1)
6430 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6431 sect_offset_str (sect_off),
6432 hex_string (header.signature));
6433
6434 info_ptr += length;
6435 }
6436 }
6437
6438 /* Create the hash table of all entries in the .debug_types
6439 (or .debug_types.dwo) section(s).
6440 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6441 otherwise it is NULL.
6442
6443 The result is a pointer to the hash table or NULL if there are no types.
6444
6445 Note: This function processes DWO files only, not DWP files. */
6446
6447 static void
6448 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6449 struct dwo_file *dwo_file,
6450 gdb::array_view<dwarf2_section_info> type_sections,
6451 htab_up &types_htab)
6452 {
6453 for (dwarf2_section_info &section : type_sections)
6454 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6455 types_htab, rcuh_kind::TYPE);
6456 }
6457
6458 /* Create the hash table of all entries in the .debug_types section,
6459 and initialize all_type_units.
6460 The result is zero if there is an error (e.g. missing .debug_types section),
6461 otherwise non-zero. */
6462
6463 static int
6464 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6465 {
6466 htab_up types_htab;
6467
6468 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6469 &dwarf2_per_objfile->per_bfd->info, types_htab,
6470 rcuh_kind::COMPILE);
6471 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6472 dwarf2_per_objfile->per_bfd->types, types_htab);
6473 if (types_htab == NULL)
6474 {
6475 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6476 return 0;
6477 }
6478
6479 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6480
6481 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6482 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6483 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6484
6485 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6486 add_signatured_type_cu_to_table,
6487 &dwarf2_per_objfile->per_bfd->all_type_units);
6488
6489 return 1;
6490 }
6491
6492 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6493 If SLOT is non-NULL, it is the entry to use in the hash table.
6494 Otherwise we find one. */
6495
6496 static struct signatured_type *
6497 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6498 void **slot)
6499 {
6500 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6501 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6502 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6503
6504 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6505
6506 dwarf2_per_objfile->resize_symtabs ();
6507
6508 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6509 sig_type->signature = sig;
6510 sig_type->per_cu.is_debug_types = 1;
6511 if (dwarf2_per_objfile->per_bfd->using_index)
6512 {
6513 sig_type->per_cu.v.quick =
6514 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6515 struct dwarf2_per_cu_quick_data);
6516 }
6517
6518 if (slot == NULL)
6519 {
6520 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6521 sig_type, INSERT);
6522 }
6523 gdb_assert (*slot == NULL);
6524 *slot = sig_type;
6525 /* The rest of sig_type must be filled in by the caller. */
6526 return sig_type;
6527 }
6528
6529 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6530 Fill in SIG_ENTRY with DWO_ENTRY. */
6531
6532 static void
6533 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6534 struct signatured_type *sig_entry,
6535 struct dwo_unit *dwo_entry)
6536 {
6537 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6538
6539 /* Make sure we're not clobbering something we don't expect to. */
6540 gdb_assert (! sig_entry->per_cu.queued);
6541 gdb_assert (dwarf2_per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6542 if (per_bfd->using_index)
6543 {
6544 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6545 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6546 }
6547 else
6548 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6549 gdb_assert (sig_entry->signature == dwo_entry->signature);
6550 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6551 gdb_assert (sig_entry->type_unit_group == NULL);
6552 gdb_assert (sig_entry->dwo_unit == NULL);
6553
6554 sig_entry->per_cu.section = dwo_entry->section;
6555 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6556 sig_entry->per_cu.length = dwo_entry->length;
6557 sig_entry->per_cu.reading_dwo_directly = 1;
6558 sig_entry->per_cu.per_bfd = per_bfd;
6559 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6560 sig_entry->dwo_unit = dwo_entry;
6561 }
6562
6563 /* Subroutine of lookup_signatured_type.
6564 If we haven't read the TU yet, create the signatured_type data structure
6565 for a TU to be read in directly from a DWO file, bypassing the stub.
6566 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6567 using .gdb_index, then when reading a CU we want to stay in the DWO file
6568 containing that CU. Otherwise we could end up reading several other DWO
6569 files (due to comdat folding) to process the transitive closure of all the
6570 mentioned TUs, and that can be slow. The current DWO file will have every
6571 type signature that it needs.
6572 We only do this for .gdb_index because in the psymtab case we already have
6573 to read all the DWOs to build the type unit groups. */
6574
6575 static struct signatured_type *
6576 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6577 {
6578 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6579 struct dwo_file *dwo_file;
6580 struct dwo_unit find_dwo_entry, *dwo_entry;
6581 struct signatured_type find_sig_entry, *sig_entry;
6582 void **slot;
6583
6584 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6585
6586 /* If TU skeletons have been removed then we may not have read in any
6587 TUs yet. */
6588 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6589 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6590
6591 /* We only ever need to read in one copy of a signatured type.
6592 Use the global signatured_types array to do our own comdat-folding
6593 of types. If this is the first time we're reading this TU, and
6594 the TU has an entry in .gdb_index, replace the recorded data from
6595 .gdb_index with this TU. */
6596
6597 find_sig_entry.signature = sig;
6598 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6599 &find_sig_entry, INSERT);
6600 sig_entry = (struct signatured_type *) *slot;
6601
6602 /* We can get here with the TU already read, *or* in the process of being
6603 read. Don't reassign the global entry to point to this DWO if that's
6604 the case. Also note that if the TU is already being read, it may not
6605 have come from a DWO, the program may be a mix of Fission-compiled
6606 code and non-Fission-compiled code. */
6607
6608 /* Have we already tried to read this TU?
6609 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6610 needn't exist in the global table yet). */
6611 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6612 return sig_entry;
6613
6614 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6615 dwo_unit of the TU itself. */
6616 dwo_file = cu->dwo_unit->dwo_file;
6617
6618 /* Ok, this is the first time we're reading this TU. */
6619 if (dwo_file->tus == NULL)
6620 return NULL;
6621 find_dwo_entry.signature = sig;
6622 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6623 &find_dwo_entry);
6624 if (dwo_entry == NULL)
6625 return NULL;
6626
6627 /* If the global table doesn't have an entry for this TU, add one. */
6628 if (sig_entry == NULL)
6629 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6630
6631 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6632 sig_entry->per_cu.tu_read = 1;
6633 return sig_entry;
6634 }
6635
6636 /* Subroutine of lookup_signatured_type.
6637 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6638 then try the DWP file. If the TU stub (skeleton) has been removed then
6639 it won't be in .gdb_index. */
6640
6641 static struct signatured_type *
6642 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6643 {
6644 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6645 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6646 struct dwo_unit *dwo_entry;
6647 struct signatured_type find_sig_entry, *sig_entry;
6648 void **slot;
6649
6650 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6651 gdb_assert (dwp_file != NULL);
6652
6653 /* If TU skeletons have been removed then we may not have read in any
6654 TUs yet. */
6655 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6656 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6657
6658 find_sig_entry.signature = sig;
6659 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6660 &find_sig_entry, INSERT);
6661 sig_entry = (struct signatured_type *) *slot;
6662
6663 /* Have we already tried to read this TU?
6664 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6665 needn't exist in the global table yet). */
6666 if (sig_entry != NULL)
6667 return sig_entry;
6668
6669 if (dwp_file->tus == NULL)
6670 return NULL;
6671 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6672 sig, 1 /* is_debug_types */);
6673 if (dwo_entry == NULL)
6674 return NULL;
6675
6676 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6677 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6678
6679 return sig_entry;
6680 }
6681
6682 /* Lookup a signature based type for DW_FORM_ref_sig8.
6683 Returns NULL if signature SIG is not present in the table.
6684 It is up to the caller to complain about this. */
6685
6686 static struct signatured_type *
6687 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6688 {
6689 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6690
6691 if (cu->dwo_unit
6692 && dwarf2_per_objfile->per_bfd->using_index)
6693 {
6694 /* We're in a DWO/DWP file, and we're using .gdb_index.
6695 These cases require special processing. */
6696 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6697 return lookup_dwo_signatured_type (cu, sig);
6698 else
6699 return lookup_dwp_signatured_type (cu, sig);
6700 }
6701 else
6702 {
6703 struct signatured_type find_entry, *entry;
6704
6705 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6706 return NULL;
6707 find_entry.signature = sig;
6708 entry = ((struct signatured_type *)
6709 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6710 &find_entry));
6711 return entry;
6712 }
6713 }
6714
6715 /* Low level DIE reading support. */
6716
6717 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6718
6719 static void
6720 init_cu_die_reader (struct die_reader_specs *reader,
6721 struct dwarf2_cu *cu,
6722 struct dwarf2_section_info *section,
6723 struct dwo_file *dwo_file,
6724 struct abbrev_table *abbrev_table)
6725 {
6726 gdb_assert (section->readin && section->buffer != NULL);
6727 reader->abfd = section->get_bfd_owner ();
6728 reader->cu = cu;
6729 reader->dwo_file = dwo_file;
6730 reader->die_section = section;
6731 reader->buffer = section->buffer;
6732 reader->buffer_end = section->buffer + section->size;
6733 reader->abbrev_table = abbrev_table;
6734 }
6735
6736 /* Subroutine of cutu_reader to simplify it.
6737 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6738 There's just a lot of work to do, and cutu_reader is big enough
6739 already.
6740
6741 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6742 from it to the DIE in the DWO. If NULL we are skipping the stub.
6743 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6744 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6745 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6746 STUB_COMP_DIR may be non-NULL.
6747 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6748 are filled in with the info of the DIE from the DWO file.
6749 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6750 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6751 kept around for at least as long as *RESULT_READER.
6752
6753 The result is non-zero if a valid (non-dummy) DIE was found. */
6754
6755 static int
6756 read_cutu_die_from_dwo (dwarf2_cu *cu,
6757 struct dwo_unit *dwo_unit,
6758 struct die_info *stub_comp_unit_die,
6759 const char *stub_comp_dir,
6760 struct die_reader_specs *result_reader,
6761 const gdb_byte **result_info_ptr,
6762 struct die_info **result_comp_unit_die,
6763 abbrev_table_up *result_dwo_abbrev_table)
6764 {
6765 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6766 dwarf2_per_cu_data *per_cu = cu->per_cu;
6767 struct objfile *objfile = dwarf2_per_objfile->objfile;
6768 bfd *abfd;
6769 const gdb_byte *begin_info_ptr, *info_ptr;
6770 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6771 int i,num_extra_attrs;
6772 struct dwarf2_section_info *dwo_abbrev_section;
6773 struct die_info *comp_unit_die;
6774
6775 /* At most one of these may be provided. */
6776 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6777
6778 /* These attributes aren't processed until later:
6779 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6780 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6781 referenced later. However, these attributes are found in the stub
6782 which we won't have later. In order to not impose this complication
6783 on the rest of the code, we read them here and copy them to the
6784 DWO CU/TU die. */
6785
6786 stmt_list = NULL;
6787 low_pc = NULL;
6788 high_pc = NULL;
6789 ranges = NULL;
6790 comp_dir = NULL;
6791
6792 if (stub_comp_unit_die != NULL)
6793 {
6794 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6795 DWO file. */
6796 if (!per_cu->is_debug_types)
6797 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6798 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6799 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6800 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6801 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6802
6803 cu->addr_base = stub_comp_unit_die->addr_base ();
6804
6805 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6806 here (if needed). We need the value before we can process
6807 DW_AT_ranges. */
6808 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6809 }
6810 else if (stub_comp_dir != NULL)
6811 {
6812 /* Reconstruct the comp_dir attribute to simplify the code below. */
6813 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6814 comp_dir->name = DW_AT_comp_dir;
6815 comp_dir->form = DW_FORM_string;
6816 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6817 DW_STRING (comp_dir) = stub_comp_dir;
6818 }
6819
6820 /* Set up for reading the DWO CU/TU. */
6821 cu->dwo_unit = dwo_unit;
6822 dwarf2_section_info *section = dwo_unit->section;
6823 section->read (objfile);
6824 abfd = section->get_bfd_owner ();
6825 begin_info_ptr = info_ptr = (section->buffer
6826 + to_underlying (dwo_unit->sect_off));
6827 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6828
6829 if (per_cu->is_debug_types)
6830 {
6831 signatured_type *sig_type = (struct signatured_type *) per_cu;
6832
6833 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6834 &cu->header, section,
6835 dwo_abbrev_section,
6836 info_ptr, rcuh_kind::TYPE);
6837 /* This is not an assert because it can be caused by bad debug info. */
6838 if (sig_type->signature != cu->header.signature)
6839 {
6840 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6841 " TU at offset %s [in module %s]"),
6842 hex_string (sig_type->signature),
6843 hex_string (cu->header.signature),
6844 sect_offset_str (dwo_unit->sect_off),
6845 bfd_get_filename (abfd));
6846 }
6847 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6848 /* For DWOs coming from DWP files, we don't know the CU length
6849 nor the type's offset in the TU until now. */
6850 dwo_unit->length = cu->header.get_length ();
6851 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6852
6853 /* Establish the type offset that can be used to lookup the type.
6854 For DWO files, we don't know it until now. */
6855 sig_type->type_offset_in_section
6856 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6857 }
6858 else
6859 {
6860 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6861 &cu->header, section,
6862 dwo_abbrev_section,
6863 info_ptr, rcuh_kind::COMPILE);
6864 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6865 /* For DWOs coming from DWP files, we don't know the CU length
6866 until now. */
6867 dwo_unit->length = cu->header.get_length ();
6868 }
6869
6870 *result_dwo_abbrev_table
6871 = abbrev_table::read (objfile, dwo_abbrev_section,
6872 cu->header.abbrev_sect_off);
6873 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6874 result_dwo_abbrev_table->get ());
6875
6876 /* Read in the die, but leave space to copy over the attributes
6877 from the stub. This has the benefit of simplifying the rest of
6878 the code - all the work to maintain the illusion of a single
6879 DW_TAG_{compile,type}_unit DIE is done here. */
6880 num_extra_attrs = ((stmt_list != NULL)
6881 + (low_pc != NULL)
6882 + (high_pc != NULL)
6883 + (ranges != NULL)
6884 + (comp_dir != NULL));
6885 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6886 num_extra_attrs);
6887
6888 /* Copy over the attributes from the stub to the DIE we just read in. */
6889 comp_unit_die = *result_comp_unit_die;
6890 i = comp_unit_die->num_attrs;
6891 if (stmt_list != NULL)
6892 comp_unit_die->attrs[i++] = *stmt_list;
6893 if (low_pc != NULL)
6894 comp_unit_die->attrs[i++] = *low_pc;
6895 if (high_pc != NULL)
6896 comp_unit_die->attrs[i++] = *high_pc;
6897 if (ranges != NULL)
6898 comp_unit_die->attrs[i++] = *ranges;
6899 if (comp_dir != NULL)
6900 comp_unit_die->attrs[i++] = *comp_dir;
6901 comp_unit_die->num_attrs += num_extra_attrs;
6902
6903 if (dwarf_die_debug)
6904 {
6905 fprintf_unfiltered (gdb_stdlog,
6906 "Read die from %s@0x%x of %s:\n",
6907 section->get_name (),
6908 (unsigned) (begin_info_ptr - section->buffer),
6909 bfd_get_filename (abfd));
6910 dump_die (comp_unit_die, dwarf_die_debug);
6911 }
6912
6913 /* Skip dummy compilation units. */
6914 if (info_ptr >= begin_info_ptr + dwo_unit->length
6915 || peek_abbrev_code (abfd, info_ptr) == 0)
6916 return 0;
6917
6918 *result_info_ptr = info_ptr;
6919 return 1;
6920 }
6921
6922 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6923 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6924 signature is part of the header. */
6925 static gdb::optional<ULONGEST>
6926 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6927 {
6928 if (cu->header.version >= 5)
6929 return cu->header.signature;
6930 struct attribute *attr;
6931 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6932 if (attr == nullptr)
6933 return gdb::optional<ULONGEST> ();
6934 return DW_UNSND (attr);
6935 }
6936
6937 /* Subroutine of cutu_reader to simplify it.
6938 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6939 Returns NULL if the specified DWO unit cannot be found. */
6940
6941 static struct dwo_unit *
6942 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6943 {
6944 dwarf2_per_cu_data *per_cu = cu->per_cu;
6945 struct dwo_unit *dwo_unit;
6946 const char *comp_dir;
6947
6948 gdb_assert (cu != NULL);
6949
6950 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6951 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6952 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6953
6954 if (per_cu->is_debug_types)
6955 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6956 else
6957 {
6958 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6959
6960 if (!signature.has_value ())
6961 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6962 " [in module %s]"),
6963 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6964
6965 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6966 }
6967
6968 return dwo_unit;
6969 }
6970
6971 /* Subroutine of cutu_reader to simplify it.
6972 See it for a description of the parameters.
6973 Read a TU directly from a DWO file, bypassing the stub. */
6974
6975 void
6976 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6977 dwarf2_per_objfile *per_objfile,
6978 dwarf2_cu *existing_cu)
6979 {
6980 struct signatured_type *sig_type;
6981
6982 /* Verify we can do the following downcast, and that we have the
6983 data we need. */
6984 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6985 sig_type = (struct signatured_type *) this_cu;
6986 gdb_assert (sig_type->dwo_unit != NULL);
6987
6988 dwarf2_cu *cu;
6989
6990 if (existing_cu != nullptr)
6991 {
6992 cu = existing_cu;
6993 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6994 /* There's no need to do the rereading_dwo_cu handling that
6995 cutu_reader does since we don't read the stub. */
6996 }
6997 else
6998 {
6999 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7000 in per_objfile yet. */
7001 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7002 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7003 cu = m_new_cu.get ();
7004 }
7005
7006 /* A future optimization, if needed, would be to use an existing
7007 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7008 could share abbrev tables. */
7009
7010 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7011 NULL /* stub_comp_unit_die */,
7012 sig_type->dwo_unit->dwo_file->comp_dir,
7013 this, &info_ptr,
7014 &comp_unit_die,
7015 &m_dwo_abbrev_table) == 0)
7016 {
7017 /* Dummy die. */
7018 dummy_p = true;
7019 }
7020 }
7021
7022 /* Initialize a CU (or TU) and read its DIEs.
7023 If the CU defers to a DWO file, read the DWO file as well.
7024
7025 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7026 Otherwise the table specified in the comp unit header is read in and used.
7027 This is an optimization for when we already have the abbrev table.
7028
7029 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7030 allocated. */
7031
7032 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7033 dwarf2_per_objfile *dwarf2_per_objfile,
7034 struct abbrev_table *abbrev_table,
7035 dwarf2_cu *existing_cu,
7036 bool skip_partial)
7037 : die_reader_specs {},
7038 m_this_cu (this_cu)
7039 {
7040 struct objfile *objfile = dwarf2_per_objfile->objfile;
7041 struct dwarf2_section_info *section = this_cu->section;
7042 bfd *abfd = section->get_bfd_owner ();
7043 const gdb_byte *begin_info_ptr;
7044 struct signatured_type *sig_type = NULL;
7045 struct dwarf2_section_info *abbrev_section;
7046 /* Non-zero if CU currently points to a DWO file and we need to
7047 reread it. When this happens we need to reread the skeleton die
7048 before we can reread the DWO file (this only applies to CUs, not TUs). */
7049 int rereading_dwo_cu = 0;
7050
7051 if (dwarf_die_debug)
7052 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7053 this_cu->is_debug_types ? "type" : "comp",
7054 sect_offset_str (this_cu->sect_off));
7055
7056 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7057 file (instead of going through the stub), short-circuit all of this. */
7058 if (this_cu->reading_dwo_directly)
7059 {
7060 /* Narrow down the scope of possibilities to have to understand. */
7061 gdb_assert (this_cu->is_debug_types);
7062 gdb_assert (abbrev_table == NULL);
7063 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, existing_cu);
7064 return;
7065 }
7066
7067 /* This is cheap if the section is already read in. */
7068 section->read (objfile);
7069
7070 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7071
7072 abbrev_section = get_abbrev_section_for_cu (this_cu);
7073
7074 dwarf2_cu *cu;
7075
7076 if (existing_cu != nullptr)
7077 {
7078 cu = existing_cu;
7079 /* If this CU is from a DWO file we need to start over, we need to
7080 refetch the attributes from the skeleton CU.
7081 This could be optimized by retrieving those attributes from when we
7082 were here the first time: the previous comp_unit_die was stored in
7083 comp_unit_obstack. But there's no data yet that we need this
7084 optimization. */
7085 if (cu->dwo_unit != NULL)
7086 rereading_dwo_cu = 1;
7087 }
7088 else
7089 {
7090 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7091 in per_objfile yet. */
7092 gdb_assert (dwarf2_per_objfile->get_cu (this_cu) == nullptr);
7093 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7094 cu = m_new_cu.get ();
7095 }
7096
7097 /* Get the header. */
7098 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7099 {
7100 /* We already have the header, there's no need to read it in again. */
7101 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7102 }
7103 else
7104 {
7105 if (this_cu->is_debug_types)
7106 {
7107 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7108 &cu->header, section,
7109 abbrev_section, info_ptr,
7110 rcuh_kind::TYPE);
7111
7112 /* Since per_cu is the first member of struct signatured_type,
7113 we can go from a pointer to one to a pointer to the other. */
7114 sig_type = (struct signatured_type *) this_cu;
7115 gdb_assert (sig_type->signature == cu->header.signature);
7116 gdb_assert (sig_type->type_offset_in_tu
7117 == cu->header.type_cu_offset_in_tu);
7118 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7119
7120 /* LENGTH has not been set yet for type units if we're
7121 using .gdb_index. */
7122 this_cu->length = cu->header.get_length ();
7123
7124 /* Establish the type offset that can be used to lookup the type. */
7125 sig_type->type_offset_in_section =
7126 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7127
7128 this_cu->dwarf_version = cu->header.version;
7129 }
7130 else
7131 {
7132 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7133 &cu->header, section,
7134 abbrev_section,
7135 info_ptr,
7136 rcuh_kind::COMPILE);
7137
7138 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7139 if (this_cu->length == 0)
7140 this_cu->length = cu->header.get_length ();
7141 else
7142 gdb_assert (this_cu->length == cu->header.get_length ());
7143 this_cu->dwarf_version = cu->header.version;
7144 }
7145 }
7146
7147 /* Skip dummy compilation units. */
7148 if (info_ptr >= begin_info_ptr + this_cu->length
7149 || peek_abbrev_code (abfd, info_ptr) == 0)
7150 {
7151 dummy_p = true;
7152 return;
7153 }
7154
7155 /* If we don't have them yet, read the abbrevs for this compilation unit.
7156 And if we need to read them now, make sure they're freed when we're
7157 done. */
7158 if (abbrev_table != NULL)
7159 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7160 else
7161 {
7162 m_abbrev_table_holder
7163 = abbrev_table::read (objfile, abbrev_section,
7164 cu->header.abbrev_sect_off);
7165 abbrev_table = m_abbrev_table_holder.get ();
7166 }
7167
7168 /* Read the top level CU/TU die. */
7169 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7170 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7171
7172 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7173 {
7174 dummy_p = true;
7175 return;
7176 }
7177
7178 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7179 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7180 table from the DWO file and pass the ownership over to us. It will be
7181 referenced from READER, so we must make sure to free it after we're done
7182 with READER.
7183
7184 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7185 DWO CU, that this test will fail (the attribute will not be present). */
7186 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7187 if (dwo_name != nullptr)
7188 {
7189 struct dwo_unit *dwo_unit;
7190 struct die_info *dwo_comp_unit_die;
7191
7192 if (comp_unit_die->has_children)
7193 {
7194 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7195 " has children (offset %s) [in module %s]"),
7196 sect_offset_str (this_cu->sect_off),
7197 bfd_get_filename (abfd));
7198 }
7199 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7200 if (dwo_unit != NULL)
7201 {
7202 if (read_cutu_die_from_dwo (cu, dwo_unit,
7203 comp_unit_die, NULL,
7204 this, &info_ptr,
7205 &dwo_comp_unit_die,
7206 &m_dwo_abbrev_table) == 0)
7207 {
7208 /* Dummy die. */
7209 dummy_p = true;
7210 return;
7211 }
7212 comp_unit_die = dwo_comp_unit_die;
7213 }
7214 else
7215 {
7216 /* Yikes, we couldn't find the rest of the DIE, we only have
7217 the stub. A complaint has already been logged. There's
7218 not much more we can do except pass on the stub DIE to
7219 die_reader_func. We don't want to throw an error on bad
7220 debug info. */
7221 }
7222 }
7223 }
7224
7225 void
7226 cutu_reader::keep ()
7227 {
7228 /* Done, clean up. */
7229 gdb_assert (!dummy_p);
7230 if (m_new_cu != NULL)
7231 {
7232 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7233 now. */
7234 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7235 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7236 }
7237 }
7238
7239 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7240 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7241 assumed to have already done the lookup to find the DWO file).
7242
7243 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7244 THIS_CU->is_debug_types, but nothing else.
7245
7246 We fill in THIS_CU->length.
7247
7248 THIS_CU->cu is always freed when done.
7249 This is done in order to not leave THIS_CU->cu in a state where we have
7250 to care whether it refers to the "main" CU or the DWO CU.
7251
7252 When parent_cu is passed, it is used to provide a default value for
7253 str_offsets_base and addr_base from the parent. */
7254
7255 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7256 dwarf2_per_objfile *dwarf2_per_objfile,
7257 struct dwarf2_cu *parent_cu,
7258 struct dwo_file *dwo_file)
7259 : die_reader_specs {},
7260 m_this_cu (this_cu)
7261 {
7262 struct objfile *objfile = dwarf2_per_objfile->objfile;
7263 struct dwarf2_section_info *section = this_cu->section;
7264 bfd *abfd = section->get_bfd_owner ();
7265 struct dwarf2_section_info *abbrev_section;
7266 const gdb_byte *begin_info_ptr, *info_ptr;
7267
7268 if (dwarf_die_debug)
7269 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7270 this_cu->is_debug_types ? "type" : "comp",
7271 sect_offset_str (this_cu->sect_off));
7272
7273 gdb_assert (dwarf2_per_objfile->get_cu (this_cu) == nullptr);
7274
7275 abbrev_section = (dwo_file != NULL
7276 ? &dwo_file->sections.abbrev
7277 : get_abbrev_section_for_cu (this_cu));
7278
7279 /* This is cheap if the section is already read in. */
7280 section->read (objfile);
7281
7282 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7283
7284 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7285 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7286 &m_new_cu->header, section,
7287 abbrev_section, info_ptr,
7288 (this_cu->is_debug_types
7289 ? rcuh_kind::TYPE
7290 : rcuh_kind::COMPILE));
7291
7292 if (parent_cu != nullptr)
7293 {
7294 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7295 m_new_cu->addr_base = parent_cu->addr_base;
7296 }
7297 this_cu->length = m_new_cu->header.get_length ();
7298
7299 /* Skip dummy compilation units. */
7300 if (info_ptr >= begin_info_ptr + this_cu->length
7301 || peek_abbrev_code (abfd, info_ptr) == 0)
7302 {
7303 dummy_p = true;
7304 return;
7305 }
7306
7307 m_abbrev_table_holder
7308 = abbrev_table::read (objfile, abbrev_section,
7309 m_new_cu->header.abbrev_sect_off);
7310
7311 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7312 m_abbrev_table_holder.get ());
7313 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7314 }
7315
7316 \f
7317 /* Type Unit Groups.
7318
7319 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7320 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7321 so that all types coming from the same compilation (.o file) are grouped
7322 together. A future step could be to put the types in the same symtab as
7323 the CU the types ultimately came from. */
7324
7325 static hashval_t
7326 hash_type_unit_group (const void *item)
7327 {
7328 const struct type_unit_group *tu_group
7329 = (const struct type_unit_group *) item;
7330
7331 return hash_stmt_list_entry (&tu_group->hash);
7332 }
7333
7334 static int
7335 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7336 {
7337 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7338 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7339
7340 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7341 }
7342
7343 /* Allocate a hash table for type unit groups. */
7344
7345 static htab_up
7346 allocate_type_unit_groups_table ()
7347 {
7348 return htab_up (htab_create_alloc (3,
7349 hash_type_unit_group,
7350 eq_type_unit_group,
7351 NULL, xcalloc, xfree));
7352 }
7353
7354 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7355 partial symtabs. We combine several TUs per psymtab to not let the size
7356 of any one psymtab grow too big. */
7357 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7358 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7359
7360 /* Helper routine for get_type_unit_group.
7361 Create the type_unit_group object used to hold one or more TUs. */
7362
7363 static struct type_unit_group *
7364 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7365 {
7366 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7367 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7368 struct dwarf2_per_cu_data *per_cu;
7369 struct type_unit_group *tu_group;
7370
7371 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7372 struct type_unit_group);
7373 per_cu = &tu_group->per_cu;
7374 per_cu->per_bfd = per_bfd;
7375
7376 if (per_bfd->using_index)
7377 {
7378 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7379 struct dwarf2_per_cu_quick_data);
7380 }
7381 else
7382 {
7383 unsigned int line_offset = to_underlying (line_offset_struct);
7384 dwarf2_psymtab *pst;
7385 std::string name;
7386
7387 /* Give the symtab a useful name for debug purposes. */
7388 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7389 name = string_printf ("<type_units_%d>",
7390 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7391 else
7392 name = string_printf ("<type_units_at_0x%x>", line_offset);
7393
7394 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7395 pst->anonymous = true;
7396 }
7397
7398 tu_group->hash.dwo_unit = cu->dwo_unit;
7399 tu_group->hash.line_sect_off = line_offset_struct;
7400
7401 return tu_group;
7402 }
7403
7404 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7405 STMT_LIST is a DW_AT_stmt_list attribute. */
7406
7407 static struct type_unit_group *
7408 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7409 {
7410 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7411 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7412 struct type_unit_group *tu_group;
7413 void **slot;
7414 unsigned int line_offset;
7415 struct type_unit_group type_unit_group_for_lookup;
7416
7417 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7418 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7419
7420 /* Do we need to create a new group, or can we use an existing one? */
7421
7422 if (stmt_list)
7423 {
7424 line_offset = DW_UNSND (stmt_list);
7425 ++tu_stats->nr_symtab_sharers;
7426 }
7427 else
7428 {
7429 /* Ugh, no stmt_list. Rare, but we have to handle it.
7430 We can do various things here like create one group per TU or
7431 spread them over multiple groups to split up the expansion work.
7432 To avoid worst case scenarios (too many groups or too large groups)
7433 we, umm, group them in bunches. */
7434 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7435 | (tu_stats->nr_stmt_less_type_units
7436 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7437 ++tu_stats->nr_stmt_less_type_units;
7438 }
7439
7440 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7441 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7442 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7443 &type_unit_group_for_lookup, INSERT);
7444 if (*slot != NULL)
7445 {
7446 tu_group = (struct type_unit_group *) *slot;
7447 gdb_assert (tu_group != NULL);
7448 }
7449 else
7450 {
7451 sect_offset line_offset_struct = (sect_offset) line_offset;
7452 tu_group = create_type_unit_group (cu, line_offset_struct);
7453 *slot = tu_group;
7454 ++tu_stats->nr_symtabs;
7455 }
7456
7457 return tu_group;
7458 }
7459 \f
7460 /* Partial symbol tables. */
7461
7462 /* Create a psymtab named NAME and assign it to PER_CU.
7463
7464 The caller must fill in the following details:
7465 dirname, textlow, texthigh. */
7466
7467 static dwarf2_psymtab *
7468 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7469 dwarf2_per_objfile *per_objfile,
7470 const char *name)
7471 {
7472 struct objfile *objfile = per_objfile->objfile;
7473 dwarf2_psymtab *pst;
7474
7475 pst = new dwarf2_psymtab (name, objfile, per_cu);
7476
7477 pst->psymtabs_addrmap_supported = true;
7478
7479 /* This is the glue that links PST into GDB's symbol API. */
7480 per_cu->v.psymtab = pst;
7481
7482 return pst;
7483 }
7484
7485 /* DIE reader function for process_psymtab_comp_unit. */
7486
7487 static void
7488 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7489 const gdb_byte *info_ptr,
7490 struct die_info *comp_unit_die,
7491 enum language pretend_language)
7492 {
7493 struct dwarf2_cu *cu = reader->cu;
7494 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7495 struct objfile *objfile = per_objfile->objfile;
7496 struct gdbarch *gdbarch = objfile->arch ();
7497 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7498 CORE_ADDR baseaddr;
7499 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7500 dwarf2_psymtab *pst;
7501 enum pc_bounds_kind cu_bounds_kind;
7502 const char *filename;
7503
7504 gdb_assert (! per_cu->is_debug_types);
7505
7506 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7507
7508 /* Allocate a new partial symbol table structure. */
7509 gdb::unique_xmalloc_ptr<char> debug_filename;
7510 static const char artificial[] = "<artificial>";
7511 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7512 if (filename == NULL)
7513 filename = "";
7514 else if (strcmp (filename, artificial) == 0)
7515 {
7516 debug_filename.reset (concat (artificial, "@",
7517 sect_offset_str (per_cu->sect_off),
7518 (char *) NULL));
7519 filename = debug_filename.get ();
7520 }
7521
7522 pst = create_partial_symtab (per_cu, per_objfile, filename);
7523
7524 /* This must be done before calling dwarf2_build_include_psymtabs. */
7525 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7526
7527 baseaddr = objfile->text_section_offset ();
7528
7529 dwarf2_find_base_address (comp_unit_die, cu);
7530
7531 /* Possibly set the default values of LOWPC and HIGHPC from
7532 `DW_AT_ranges'. */
7533 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7534 &best_highpc, cu, pst);
7535 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7536 {
7537 CORE_ADDR low
7538 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7539 - baseaddr);
7540 CORE_ADDR high
7541 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7542 - baseaddr - 1);
7543 /* Store the contiguous range if it is not empty; it can be
7544 empty for CUs with no code. */
7545 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7546 low, high, pst);
7547 }
7548
7549 /* Check if comp unit has_children.
7550 If so, read the rest of the partial symbols from this comp unit.
7551 If not, there's no more debug_info for this comp unit. */
7552 if (comp_unit_die->has_children)
7553 {
7554 struct partial_die_info *first_die;
7555 CORE_ADDR lowpc, highpc;
7556
7557 lowpc = ((CORE_ADDR) -1);
7558 highpc = ((CORE_ADDR) 0);
7559
7560 first_die = load_partial_dies (reader, info_ptr, 1);
7561
7562 scan_partial_symbols (first_die, &lowpc, &highpc,
7563 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7564
7565 /* If we didn't find a lowpc, set it to highpc to avoid
7566 complaints from `maint check'. */
7567 if (lowpc == ((CORE_ADDR) -1))
7568 lowpc = highpc;
7569
7570 /* If the compilation unit didn't have an explicit address range,
7571 then use the information extracted from its child dies. */
7572 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7573 {
7574 best_lowpc = lowpc;
7575 best_highpc = highpc;
7576 }
7577 }
7578 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7579 best_lowpc + baseaddr)
7580 - baseaddr);
7581 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7582 best_highpc + baseaddr)
7583 - baseaddr);
7584
7585 end_psymtab_common (objfile, pst);
7586
7587 if (!cu->per_cu->imported_symtabs_empty ())
7588 {
7589 int i;
7590 int len = cu->per_cu->imported_symtabs_size ();
7591
7592 /* Fill in 'dependencies' here; we fill in 'users' in a
7593 post-pass. */
7594 pst->number_of_dependencies = len;
7595 pst->dependencies
7596 = objfile->partial_symtabs->allocate_dependencies (len);
7597 for (i = 0; i < len; ++i)
7598 {
7599 pst->dependencies[i]
7600 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7601 }
7602
7603 cu->per_cu->imported_symtabs_free ();
7604 }
7605
7606 /* Get the list of files included in the current compilation unit,
7607 and build a psymtab for each of them. */
7608 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7609
7610 if (dwarf_read_debug)
7611 fprintf_unfiltered (gdb_stdlog,
7612 "Psymtab for %s unit @%s: %s - %s"
7613 ", %d global, %d static syms\n",
7614 per_cu->is_debug_types ? "type" : "comp",
7615 sect_offset_str (per_cu->sect_off),
7616 paddress (gdbarch, pst->text_low (objfile)),
7617 paddress (gdbarch, pst->text_high (objfile)),
7618 pst->n_global_syms, pst->n_static_syms);
7619 }
7620
7621 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7622 Process compilation unit THIS_CU for a psymtab. */
7623
7624 static void
7625 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7626 dwarf2_per_objfile *per_objfile,
7627 bool want_partial_unit,
7628 enum language pretend_language)
7629 {
7630 /* If this compilation unit was already read in, free the
7631 cached copy in order to read it in again. This is
7632 necessary because we skipped some symbols when we first
7633 read in the compilation unit (see load_partial_dies).
7634 This problem could be avoided, but the benefit is unclear. */
7635 per_objfile->remove_cu (this_cu);
7636
7637 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7638
7639 switch (reader.comp_unit_die->tag)
7640 {
7641 case DW_TAG_compile_unit:
7642 this_cu->unit_type = DW_UT_compile;
7643 break;
7644 case DW_TAG_partial_unit:
7645 this_cu->unit_type = DW_UT_partial;
7646 break;
7647 default:
7648 abort ();
7649 }
7650
7651 if (reader.dummy_p)
7652 {
7653 /* Nothing. */
7654 }
7655 else if (this_cu->is_debug_types)
7656 build_type_psymtabs_reader (&reader, reader.info_ptr,
7657 reader.comp_unit_die);
7658 else if (want_partial_unit
7659 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7660 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7661 reader.comp_unit_die,
7662 pretend_language);
7663
7664 this_cu->lang = reader.cu->language;
7665
7666 /* Age out any secondary CUs. */
7667 per_objfile->age_comp_units ();
7668 }
7669
7670 /* Reader function for build_type_psymtabs. */
7671
7672 static void
7673 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7674 const gdb_byte *info_ptr,
7675 struct die_info *type_unit_die)
7676 {
7677 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7678 struct objfile *objfile = dwarf2_per_objfile->objfile;
7679 struct dwarf2_cu *cu = reader->cu;
7680 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7681 struct signatured_type *sig_type;
7682 struct type_unit_group *tu_group;
7683 struct attribute *attr;
7684 struct partial_die_info *first_die;
7685 CORE_ADDR lowpc, highpc;
7686 dwarf2_psymtab *pst;
7687
7688 gdb_assert (per_cu->is_debug_types);
7689 sig_type = (struct signatured_type *) per_cu;
7690
7691 if (! type_unit_die->has_children)
7692 return;
7693
7694 attr = type_unit_die->attr (DW_AT_stmt_list);
7695 tu_group = get_type_unit_group (cu, attr);
7696
7697 if (tu_group->tus == nullptr)
7698 tu_group->tus = new std::vector<signatured_type *>;
7699 tu_group->tus->push_back (sig_type);
7700
7701 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7702 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7703 pst->anonymous = true;
7704
7705 first_die = load_partial_dies (reader, info_ptr, 1);
7706
7707 lowpc = (CORE_ADDR) -1;
7708 highpc = (CORE_ADDR) 0;
7709 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7710
7711 end_psymtab_common (objfile, pst);
7712 }
7713
7714 /* Struct used to sort TUs by their abbreviation table offset. */
7715
7716 struct tu_abbrev_offset
7717 {
7718 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7719 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7720 {}
7721
7722 signatured_type *sig_type;
7723 sect_offset abbrev_offset;
7724 };
7725
7726 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7727
7728 static bool
7729 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7730 const struct tu_abbrev_offset &b)
7731 {
7732 return a.abbrev_offset < b.abbrev_offset;
7733 }
7734
7735 /* Efficiently read all the type units.
7736 This does the bulk of the work for build_type_psymtabs.
7737
7738 The efficiency is because we sort TUs by the abbrev table they use and
7739 only read each abbrev table once. In one program there are 200K TUs
7740 sharing 8K abbrev tables.
7741
7742 The main purpose of this function is to support building the
7743 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7744 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7745 can collapse the search space by grouping them by stmt_list.
7746 The savings can be significant, in the same program from above the 200K TUs
7747 share 8K stmt_list tables.
7748
7749 FUNC is expected to call get_type_unit_group, which will create the
7750 struct type_unit_group if necessary and add it to
7751 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7752
7753 static void
7754 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7755 {
7756 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7757 abbrev_table_up abbrev_table;
7758 sect_offset abbrev_offset;
7759
7760 /* It's up to the caller to not call us multiple times. */
7761 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7762
7763 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7764 return;
7765
7766 /* TUs typically share abbrev tables, and there can be way more TUs than
7767 abbrev tables. Sort by abbrev table to reduce the number of times we
7768 read each abbrev table in.
7769 Alternatives are to punt or to maintain a cache of abbrev tables.
7770 This is simpler and efficient enough for now.
7771
7772 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7773 symtab to use). Typically TUs with the same abbrev offset have the same
7774 stmt_list value too so in practice this should work well.
7775
7776 The basic algorithm here is:
7777
7778 sort TUs by abbrev table
7779 for each TU with same abbrev table:
7780 read abbrev table if first user
7781 read TU top level DIE
7782 [IWBN if DWO skeletons had DW_AT_stmt_list]
7783 call FUNC */
7784
7785 if (dwarf_read_debug)
7786 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7787
7788 /* Sort in a separate table to maintain the order of all_type_units
7789 for .gdb_index: TU indices directly index all_type_units. */
7790 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7791 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7792
7793 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7794 sorted_by_abbrev.emplace_back
7795 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7796 sig_type->per_cu.section,
7797 sig_type->per_cu.sect_off));
7798
7799 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7800 sort_tu_by_abbrev_offset);
7801
7802 abbrev_offset = (sect_offset) ~(unsigned) 0;
7803
7804 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7805 {
7806 /* Switch to the next abbrev table if necessary. */
7807 if (abbrev_table == NULL
7808 || tu.abbrev_offset != abbrev_offset)
7809 {
7810 abbrev_offset = tu.abbrev_offset;
7811 abbrev_table =
7812 abbrev_table::read (dwarf2_per_objfile->objfile,
7813 &dwarf2_per_objfile->per_bfd->abbrev,
7814 abbrev_offset);
7815 ++tu_stats->nr_uniq_abbrev_tables;
7816 }
7817
7818 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7819 abbrev_table.get (), nullptr, false);
7820 if (!reader.dummy_p)
7821 build_type_psymtabs_reader (&reader, reader.info_ptr,
7822 reader.comp_unit_die);
7823 }
7824 }
7825
7826 /* Print collected type unit statistics. */
7827
7828 static void
7829 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7830 {
7831 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7832
7833 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7834 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7835 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7836 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7837 tu_stats->nr_uniq_abbrev_tables);
7838 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7839 tu_stats->nr_symtabs);
7840 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7841 tu_stats->nr_symtab_sharers);
7842 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7843 tu_stats->nr_stmt_less_type_units);
7844 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7845 tu_stats->nr_all_type_units_reallocs);
7846 }
7847
7848 /* Traversal function for build_type_psymtabs. */
7849
7850 static int
7851 build_type_psymtab_dependencies (void **slot, void *info)
7852 {
7853 struct dwarf2_per_objfile *dwarf2_per_objfile
7854 = (struct dwarf2_per_objfile *) info;
7855 struct objfile *objfile = dwarf2_per_objfile->objfile;
7856 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7857 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7858 dwarf2_psymtab *pst = per_cu->v.psymtab;
7859 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7860 int i;
7861
7862 gdb_assert (len > 0);
7863 gdb_assert (per_cu->type_unit_group_p ());
7864
7865 pst->number_of_dependencies = len;
7866 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7867 for (i = 0; i < len; ++i)
7868 {
7869 struct signatured_type *iter = tu_group->tus->at (i);
7870 gdb_assert (iter->per_cu.is_debug_types);
7871 pst->dependencies[i] = iter->per_cu.v.psymtab;
7872 iter->type_unit_group = tu_group;
7873 }
7874
7875 delete tu_group->tus;
7876 tu_group->tus = nullptr;
7877
7878 return 1;
7879 }
7880
7881 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7882 Build partial symbol tables for the .debug_types comp-units. */
7883
7884 static void
7885 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7886 {
7887 if (! create_all_type_units (dwarf2_per_objfile))
7888 return;
7889
7890 build_type_psymtabs_1 (dwarf2_per_objfile);
7891 }
7892
7893 /* Traversal function for process_skeletonless_type_unit.
7894 Read a TU in a DWO file and build partial symbols for it. */
7895
7896 static int
7897 process_skeletonless_type_unit (void **slot, void *info)
7898 {
7899 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7900 struct dwarf2_per_objfile *dwarf2_per_objfile
7901 = (struct dwarf2_per_objfile *) info;
7902 struct signatured_type find_entry, *entry;
7903
7904 /* If this TU doesn't exist in the global table, add it and read it in. */
7905
7906 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7907 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7908
7909 find_entry.signature = dwo_unit->signature;
7910 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7911 &find_entry, INSERT);
7912 /* If we've already seen this type there's nothing to do. What's happening
7913 is we're doing our own version of comdat-folding here. */
7914 if (*slot != NULL)
7915 return 1;
7916
7917 /* This does the job that create_all_type_units would have done for
7918 this TU. */
7919 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7920 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7921 *slot = entry;
7922
7923 /* This does the job that build_type_psymtabs_1 would have done. */
7924 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, nullptr, nullptr,
7925 false);
7926 if (!reader.dummy_p)
7927 build_type_psymtabs_reader (&reader, reader.info_ptr,
7928 reader.comp_unit_die);
7929
7930 return 1;
7931 }
7932
7933 /* Traversal function for process_skeletonless_type_units. */
7934
7935 static int
7936 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7937 {
7938 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7939
7940 if (dwo_file->tus != NULL)
7941 htab_traverse_noresize (dwo_file->tus.get (),
7942 process_skeletonless_type_unit, info);
7943
7944 return 1;
7945 }
7946
7947 /* Scan all TUs of DWO files, verifying we've processed them.
7948 This is needed in case a TU was emitted without its skeleton.
7949 Note: This can't be done until we know what all the DWO files are. */
7950
7951 static void
7952 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7953 {
7954 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7955 if (get_dwp_file (dwarf2_per_objfile) == NULL
7956 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7957 {
7958 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7959 process_dwo_file_for_skeletonless_type_units,
7960 dwarf2_per_objfile);
7961 }
7962 }
7963
7964 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7965
7966 static void
7967 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7968 {
7969 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7970 {
7971 dwarf2_psymtab *pst = per_cu->v.psymtab;
7972
7973 if (pst == NULL)
7974 continue;
7975
7976 for (int j = 0; j < pst->number_of_dependencies; ++j)
7977 {
7978 /* Set the 'user' field only if it is not already set. */
7979 if (pst->dependencies[j]->user == NULL)
7980 pst->dependencies[j]->user = pst;
7981 }
7982 }
7983 }
7984
7985 /* Build the partial symbol table by doing a quick pass through the
7986 .debug_info and .debug_abbrev sections. */
7987
7988 static void
7989 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7990 {
7991 struct objfile *objfile = dwarf2_per_objfile->objfile;
7992
7993 if (dwarf_read_debug)
7994 {
7995 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7996 objfile_name (objfile));
7997 }
7998
7999 scoped_restore restore_reading_psyms
8000 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
8001 true);
8002
8003 dwarf2_per_objfile->per_bfd->info.read (objfile);
8004
8005 /* Any cached compilation units will be linked by the per-objfile
8006 read_in_chain. Make sure to free them when we're done. */
8007 free_cached_comp_units freer (dwarf2_per_objfile);
8008
8009 build_type_psymtabs (dwarf2_per_objfile);
8010
8011 create_all_comp_units (dwarf2_per_objfile);
8012
8013 /* Create a temporary address map on a temporary obstack. We later
8014 copy this to the final obstack. */
8015 auto_obstack temp_obstack;
8016
8017 scoped_restore save_psymtabs_addrmap
8018 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8019 addrmap_create_mutable (&temp_obstack));
8020
8021 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
8022 {
8023 if (per_cu->v.psymtab != NULL)
8024 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8025 continue;
8026 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
8027 language_minimal);
8028 }
8029
8030 /* This has to wait until we read the CUs, we need the list of DWOs. */
8031 process_skeletonless_type_units (dwarf2_per_objfile);
8032
8033 /* Now that all TUs have been processed we can fill in the dependencies. */
8034 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
8035 {
8036 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
8037 build_type_psymtab_dependencies, dwarf2_per_objfile);
8038 }
8039
8040 if (dwarf_read_debug)
8041 print_tu_stats (dwarf2_per_objfile);
8042
8043 set_partial_user (dwarf2_per_objfile);
8044
8045 objfile->partial_symtabs->psymtabs_addrmap
8046 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8047 objfile->partial_symtabs->obstack ());
8048 /* At this point we want to keep the address map. */
8049 save_psymtabs_addrmap.release ();
8050
8051 if (dwarf_read_debug)
8052 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8053 objfile_name (objfile));
8054 }
8055
8056 /* Load the partial DIEs for a secondary CU into memory.
8057 This is also used when rereading a primary CU with load_all_dies. */
8058
8059 static void
8060 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8061 dwarf2_per_objfile *per_objfile,
8062 dwarf2_cu *existing_cu)
8063 {
8064 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8065
8066 if (!reader.dummy_p)
8067 {
8068 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8069 language_minimal);
8070
8071 /* Check if comp unit has_children.
8072 If so, read the rest of the partial symbols from this comp unit.
8073 If not, there's no more debug_info for this comp unit. */
8074 if (reader.comp_unit_die->has_children)
8075 load_partial_dies (&reader, reader.info_ptr, 0);
8076
8077 reader.keep ();
8078 }
8079 }
8080
8081 static void
8082 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8083 struct dwarf2_section_info *section,
8084 struct dwarf2_section_info *abbrev_section,
8085 unsigned int is_dwz)
8086 {
8087 const gdb_byte *info_ptr;
8088 struct objfile *objfile = dwarf2_per_objfile->objfile;
8089
8090 if (dwarf_read_debug)
8091 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8092 section->get_name (),
8093 section->get_file_name ());
8094
8095 section->read (objfile);
8096
8097 info_ptr = section->buffer;
8098
8099 while (info_ptr < section->buffer + section->size)
8100 {
8101 struct dwarf2_per_cu_data *this_cu;
8102
8103 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8104
8105 comp_unit_head cu_header;
8106 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8107 abbrev_section, info_ptr,
8108 rcuh_kind::COMPILE);
8109
8110 /* Save the compilation unit for later lookup. */
8111 if (cu_header.unit_type != DW_UT_type)
8112 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8113 else
8114 {
8115 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8116 sig_type->signature = cu_header.signature;
8117 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8118 this_cu = &sig_type->per_cu;
8119 }
8120 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8121 this_cu->sect_off = sect_off;
8122 this_cu->length = cu_header.length + cu_header.initial_length_size;
8123 this_cu->is_dwz = is_dwz;
8124 this_cu->section = section;
8125
8126 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8127
8128 info_ptr = info_ptr + this_cu->length;
8129 }
8130 }
8131
8132 /* Create a list of all compilation units in OBJFILE.
8133 This is only done for -readnow and building partial symtabs. */
8134
8135 static void
8136 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8137 {
8138 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8139 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8140 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8141
8142 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8143 if (dwz != NULL)
8144 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8145 1);
8146 }
8147
8148 /* Process all loaded DIEs for compilation unit CU, starting at
8149 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8150 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8151 DW_AT_ranges). See the comments of add_partial_subprogram on how
8152 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8153
8154 static void
8155 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8156 CORE_ADDR *highpc, int set_addrmap,
8157 struct dwarf2_cu *cu)
8158 {
8159 struct partial_die_info *pdi;
8160
8161 /* Now, march along the PDI's, descending into ones which have
8162 interesting children but skipping the children of the other ones,
8163 until we reach the end of the compilation unit. */
8164
8165 pdi = first_die;
8166
8167 while (pdi != NULL)
8168 {
8169 pdi->fixup (cu);
8170
8171 /* Anonymous namespaces or modules have no name but have interesting
8172 children, so we need to look at them. Ditto for anonymous
8173 enums. */
8174
8175 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8176 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8177 || pdi->tag == DW_TAG_imported_unit
8178 || pdi->tag == DW_TAG_inlined_subroutine)
8179 {
8180 switch (pdi->tag)
8181 {
8182 case DW_TAG_subprogram:
8183 case DW_TAG_inlined_subroutine:
8184 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8185 break;
8186 case DW_TAG_constant:
8187 case DW_TAG_variable:
8188 case DW_TAG_typedef:
8189 case DW_TAG_union_type:
8190 if (!pdi->is_declaration
8191 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8192 {
8193 add_partial_symbol (pdi, cu);
8194 }
8195 break;
8196 case DW_TAG_class_type:
8197 case DW_TAG_interface_type:
8198 case DW_TAG_structure_type:
8199 if (!pdi->is_declaration)
8200 {
8201 add_partial_symbol (pdi, cu);
8202 }
8203 if ((cu->language == language_rust
8204 || cu->language == language_cplus) && pdi->has_children)
8205 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8206 set_addrmap, cu);
8207 break;
8208 case DW_TAG_enumeration_type:
8209 if (!pdi->is_declaration)
8210 add_partial_enumeration (pdi, cu);
8211 break;
8212 case DW_TAG_base_type:
8213 case DW_TAG_subrange_type:
8214 /* File scope base type definitions are added to the partial
8215 symbol table. */
8216 add_partial_symbol (pdi, cu);
8217 break;
8218 case DW_TAG_namespace:
8219 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8220 break;
8221 case DW_TAG_module:
8222 if (!pdi->is_declaration)
8223 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8224 break;
8225 case DW_TAG_imported_unit:
8226 {
8227 struct dwarf2_per_cu_data *per_cu;
8228
8229 /* For now we don't handle imported units in type units. */
8230 if (cu->per_cu->is_debug_types)
8231 {
8232 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8233 " supported in type units [in module %s]"),
8234 objfile_name (cu->per_objfile->objfile));
8235 }
8236
8237 per_cu = dwarf2_find_containing_comp_unit
8238 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8239
8240 /* Go read the partial unit, if needed. */
8241 if (per_cu->v.psymtab == NULL)
8242 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8243 cu->language);
8244
8245 cu->per_cu->imported_symtabs_push (per_cu);
8246 }
8247 break;
8248 case DW_TAG_imported_declaration:
8249 add_partial_symbol (pdi, cu);
8250 break;
8251 default:
8252 break;
8253 }
8254 }
8255
8256 /* If the die has a sibling, skip to the sibling. */
8257
8258 pdi = pdi->die_sibling;
8259 }
8260 }
8261
8262 /* Functions used to compute the fully scoped name of a partial DIE.
8263
8264 Normally, this is simple. For C++, the parent DIE's fully scoped
8265 name is concatenated with "::" and the partial DIE's name.
8266 Enumerators are an exception; they use the scope of their parent
8267 enumeration type, i.e. the name of the enumeration type is not
8268 prepended to the enumerator.
8269
8270 There are two complexities. One is DW_AT_specification; in this
8271 case "parent" means the parent of the target of the specification,
8272 instead of the direct parent of the DIE. The other is compilers
8273 which do not emit DW_TAG_namespace; in this case we try to guess
8274 the fully qualified name of structure types from their members'
8275 linkage names. This must be done using the DIE's children rather
8276 than the children of any DW_AT_specification target. We only need
8277 to do this for structures at the top level, i.e. if the target of
8278 any DW_AT_specification (if any; otherwise the DIE itself) does not
8279 have a parent. */
8280
8281 /* Compute the scope prefix associated with PDI's parent, in
8282 compilation unit CU. The result will be allocated on CU's
8283 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8284 field. NULL is returned if no prefix is necessary. */
8285 static const char *
8286 partial_die_parent_scope (struct partial_die_info *pdi,
8287 struct dwarf2_cu *cu)
8288 {
8289 const char *grandparent_scope;
8290 struct partial_die_info *parent, *real_pdi;
8291
8292 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8293 then this means the parent of the specification DIE. */
8294
8295 real_pdi = pdi;
8296 while (real_pdi->has_specification)
8297 {
8298 auto res = find_partial_die (real_pdi->spec_offset,
8299 real_pdi->spec_is_dwz, cu);
8300 real_pdi = res.pdi;
8301 cu = res.cu;
8302 }
8303
8304 parent = real_pdi->die_parent;
8305 if (parent == NULL)
8306 return NULL;
8307
8308 if (parent->scope_set)
8309 return parent->scope;
8310
8311 parent->fixup (cu);
8312
8313 grandparent_scope = partial_die_parent_scope (parent, cu);
8314
8315 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8316 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8317 Work around this problem here. */
8318 if (cu->language == language_cplus
8319 && parent->tag == DW_TAG_namespace
8320 && strcmp (parent->name, "::") == 0
8321 && grandparent_scope == NULL)
8322 {
8323 parent->scope = NULL;
8324 parent->scope_set = 1;
8325 return NULL;
8326 }
8327
8328 /* Nested subroutines in Fortran get a prefix. */
8329 if (pdi->tag == DW_TAG_enumerator)
8330 /* Enumerators should not get the name of the enumeration as a prefix. */
8331 parent->scope = grandparent_scope;
8332 else if (parent->tag == DW_TAG_namespace
8333 || parent->tag == DW_TAG_module
8334 || parent->tag == DW_TAG_structure_type
8335 || parent->tag == DW_TAG_class_type
8336 || parent->tag == DW_TAG_interface_type
8337 || parent->tag == DW_TAG_union_type
8338 || parent->tag == DW_TAG_enumeration_type
8339 || (cu->language == language_fortran
8340 && parent->tag == DW_TAG_subprogram
8341 && pdi->tag == DW_TAG_subprogram))
8342 {
8343 if (grandparent_scope == NULL)
8344 parent->scope = parent->name;
8345 else
8346 parent->scope = typename_concat (&cu->comp_unit_obstack,
8347 grandparent_scope,
8348 parent->name, 0, cu);
8349 }
8350 else
8351 {
8352 /* FIXME drow/2004-04-01: What should we be doing with
8353 function-local names? For partial symbols, we should probably be
8354 ignoring them. */
8355 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8356 dwarf_tag_name (parent->tag),
8357 sect_offset_str (pdi->sect_off));
8358 parent->scope = grandparent_scope;
8359 }
8360
8361 parent->scope_set = 1;
8362 return parent->scope;
8363 }
8364
8365 /* Return the fully scoped name associated with PDI, from compilation unit
8366 CU. The result will be allocated with malloc. */
8367
8368 static gdb::unique_xmalloc_ptr<char>
8369 partial_die_full_name (struct partial_die_info *pdi,
8370 struct dwarf2_cu *cu)
8371 {
8372 const char *parent_scope;
8373
8374 /* If this is a template instantiation, we can not work out the
8375 template arguments from partial DIEs. So, unfortunately, we have
8376 to go through the full DIEs. At least any work we do building
8377 types here will be reused if full symbols are loaded later. */
8378 if (pdi->has_template_arguments)
8379 {
8380 pdi->fixup (cu);
8381
8382 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8383 {
8384 struct die_info *die;
8385 struct attribute attr;
8386 struct dwarf2_cu *ref_cu = cu;
8387
8388 /* DW_FORM_ref_addr is using section offset. */
8389 attr.name = (enum dwarf_attribute) 0;
8390 attr.form = DW_FORM_ref_addr;
8391 attr.u.unsnd = to_underlying (pdi->sect_off);
8392 die = follow_die_ref (NULL, &attr, &ref_cu);
8393
8394 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8395 }
8396 }
8397
8398 parent_scope = partial_die_parent_scope (pdi, cu);
8399 if (parent_scope == NULL)
8400 return NULL;
8401 else
8402 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8403 pdi->name, 0, cu));
8404 }
8405
8406 static void
8407 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8408 {
8409 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8410 struct objfile *objfile = dwarf2_per_objfile->objfile;
8411 struct gdbarch *gdbarch = objfile->arch ();
8412 CORE_ADDR addr = 0;
8413 const char *actual_name = NULL;
8414 CORE_ADDR baseaddr;
8415
8416 baseaddr = objfile->text_section_offset ();
8417
8418 gdb::unique_xmalloc_ptr<char> built_actual_name
8419 = partial_die_full_name (pdi, cu);
8420 if (built_actual_name != NULL)
8421 actual_name = built_actual_name.get ();
8422
8423 if (actual_name == NULL)
8424 actual_name = pdi->name;
8425
8426 partial_symbol psymbol;
8427 memset (&psymbol, 0, sizeof (psymbol));
8428 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8429 psymbol.ginfo.section = -1;
8430
8431 /* The code below indicates that the psymbol should be installed by
8432 setting this. */
8433 gdb::optional<psymbol_placement> where;
8434
8435 switch (pdi->tag)
8436 {
8437 case DW_TAG_inlined_subroutine:
8438 case DW_TAG_subprogram:
8439 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8440 - baseaddr);
8441 if (pdi->is_external
8442 || cu->language == language_ada
8443 || (cu->language == language_fortran
8444 && pdi->die_parent != NULL
8445 && pdi->die_parent->tag == DW_TAG_subprogram))
8446 {
8447 /* Normally, only "external" DIEs are part of the global scope.
8448 But in Ada and Fortran, we want to be able to access nested
8449 procedures globally. So all Ada and Fortran subprograms are
8450 stored in the global scope. */
8451 where = psymbol_placement::GLOBAL;
8452 }
8453 else
8454 where = psymbol_placement::STATIC;
8455
8456 psymbol.domain = VAR_DOMAIN;
8457 psymbol.aclass = LOC_BLOCK;
8458 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8459 psymbol.ginfo.value.address = addr;
8460
8461 if (pdi->main_subprogram && actual_name != NULL)
8462 set_objfile_main_name (objfile, actual_name, cu->language);
8463 break;
8464 case DW_TAG_constant:
8465 psymbol.domain = VAR_DOMAIN;
8466 psymbol.aclass = LOC_STATIC;
8467 where = (pdi->is_external
8468 ? psymbol_placement::GLOBAL
8469 : psymbol_placement::STATIC);
8470 break;
8471 case DW_TAG_variable:
8472 if (pdi->d.locdesc)
8473 addr = decode_locdesc (pdi->d.locdesc, cu);
8474
8475 if (pdi->d.locdesc
8476 && addr == 0
8477 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8478 {
8479 /* A global or static variable may also have been stripped
8480 out by the linker if unused, in which case its address
8481 will be nullified; do not add such variables into partial
8482 symbol table then. */
8483 }
8484 else if (pdi->is_external)
8485 {
8486 /* Global Variable.
8487 Don't enter into the minimal symbol tables as there is
8488 a minimal symbol table entry from the ELF symbols already.
8489 Enter into partial symbol table if it has a location
8490 descriptor or a type.
8491 If the location descriptor is missing, new_symbol will create
8492 a LOC_UNRESOLVED symbol, the address of the variable will then
8493 be determined from the minimal symbol table whenever the variable
8494 is referenced.
8495 The address for the partial symbol table entry is not
8496 used by GDB, but it comes in handy for debugging partial symbol
8497 table building. */
8498
8499 if (pdi->d.locdesc || pdi->has_type)
8500 {
8501 psymbol.domain = VAR_DOMAIN;
8502 psymbol.aclass = LOC_STATIC;
8503 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8504 psymbol.ginfo.value.address = addr;
8505 where = psymbol_placement::GLOBAL;
8506 }
8507 }
8508 else
8509 {
8510 int has_loc = pdi->d.locdesc != NULL;
8511
8512 /* Static Variable. Skip symbols whose value we cannot know (those
8513 without location descriptors or constant values). */
8514 if (!has_loc && !pdi->has_const_value)
8515 return;
8516
8517 psymbol.domain = VAR_DOMAIN;
8518 psymbol.aclass = LOC_STATIC;
8519 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8520 if (has_loc)
8521 psymbol.ginfo.value.address = addr;
8522 where = psymbol_placement::STATIC;
8523 }
8524 break;
8525 case DW_TAG_typedef:
8526 case DW_TAG_base_type:
8527 case DW_TAG_subrange_type:
8528 psymbol.domain = VAR_DOMAIN;
8529 psymbol.aclass = LOC_TYPEDEF;
8530 where = psymbol_placement::STATIC;
8531 break;
8532 case DW_TAG_imported_declaration:
8533 case DW_TAG_namespace:
8534 psymbol.domain = VAR_DOMAIN;
8535 psymbol.aclass = LOC_TYPEDEF;
8536 where = psymbol_placement::GLOBAL;
8537 break;
8538 case DW_TAG_module:
8539 /* With Fortran 77 there might be a "BLOCK DATA" module
8540 available without any name. If so, we skip the module as it
8541 doesn't bring any value. */
8542 if (actual_name != nullptr)
8543 {
8544 psymbol.domain = MODULE_DOMAIN;
8545 psymbol.aclass = LOC_TYPEDEF;
8546 where = psymbol_placement::GLOBAL;
8547 }
8548 break;
8549 case DW_TAG_class_type:
8550 case DW_TAG_interface_type:
8551 case DW_TAG_structure_type:
8552 case DW_TAG_union_type:
8553 case DW_TAG_enumeration_type:
8554 /* Skip external references. The DWARF standard says in the section
8555 about "Structure, Union, and Class Type Entries": "An incomplete
8556 structure, union or class type is represented by a structure,
8557 union or class entry that does not have a byte size attribute
8558 and that has a DW_AT_declaration attribute." */
8559 if (!pdi->has_byte_size && pdi->is_declaration)
8560 return;
8561
8562 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8563 static vs. global. */
8564 psymbol.domain = STRUCT_DOMAIN;
8565 psymbol.aclass = LOC_TYPEDEF;
8566 where = (cu->language == language_cplus
8567 ? psymbol_placement::GLOBAL
8568 : psymbol_placement::STATIC);
8569 break;
8570 case DW_TAG_enumerator:
8571 psymbol.domain = VAR_DOMAIN;
8572 psymbol.aclass = LOC_CONST;
8573 where = (cu->language == language_cplus
8574 ? psymbol_placement::GLOBAL
8575 : psymbol_placement::STATIC);
8576 break;
8577 default:
8578 break;
8579 }
8580
8581 if (where.has_value ())
8582 {
8583 if (built_actual_name != nullptr)
8584 actual_name = objfile->intern (actual_name);
8585 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8586 psymbol.ginfo.set_linkage_name (actual_name);
8587 else
8588 {
8589 psymbol.ginfo.set_demangled_name (actual_name,
8590 &objfile->objfile_obstack);
8591 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8592 }
8593 add_psymbol_to_list (psymbol, *where, objfile);
8594 }
8595 }
8596
8597 /* Read a partial die corresponding to a namespace; also, add a symbol
8598 corresponding to that namespace to the symbol table. NAMESPACE is
8599 the name of the enclosing namespace. */
8600
8601 static void
8602 add_partial_namespace (struct partial_die_info *pdi,
8603 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8604 int set_addrmap, struct dwarf2_cu *cu)
8605 {
8606 /* Add a symbol for the namespace. */
8607
8608 add_partial_symbol (pdi, cu);
8609
8610 /* Now scan partial symbols in that namespace. */
8611
8612 if (pdi->has_children)
8613 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8614 }
8615
8616 /* Read a partial die corresponding to a Fortran module. */
8617
8618 static void
8619 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8620 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8621 {
8622 /* Add a symbol for the namespace. */
8623
8624 add_partial_symbol (pdi, cu);
8625
8626 /* Now scan partial symbols in that module. */
8627
8628 if (pdi->has_children)
8629 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8630 }
8631
8632 /* Read a partial die corresponding to a subprogram or an inlined
8633 subprogram and create a partial symbol for that subprogram.
8634 When the CU language allows it, this routine also defines a partial
8635 symbol for each nested subprogram that this subprogram contains.
8636 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8637 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8638
8639 PDI may also be a lexical block, in which case we simply search
8640 recursively for subprograms defined inside that lexical block.
8641 Again, this is only performed when the CU language allows this
8642 type of definitions. */
8643
8644 static void
8645 add_partial_subprogram (struct partial_die_info *pdi,
8646 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8647 int set_addrmap, struct dwarf2_cu *cu)
8648 {
8649 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8650 {
8651 if (pdi->has_pc_info)
8652 {
8653 if (pdi->lowpc < *lowpc)
8654 *lowpc = pdi->lowpc;
8655 if (pdi->highpc > *highpc)
8656 *highpc = pdi->highpc;
8657 if (set_addrmap)
8658 {
8659 struct objfile *objfile = cu->per_objfile->objfile;
8660 struct gdbarch *gdbarch = objfile->arch ();
8661 CORE_ADDR baseaddr;
8662 CORE_ADDR this_highpc;
8663 CORE_ADDR this_lowpc;
8664
8665 baseaddr = objfile->text_section_offset ();
8666 this_lowpc
8667 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8668 pdi->lowpc + baseaddr)
8669 - baseaddr);
8670 this_highpc
8671 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8672 pdi->highpc + baseaddr)
8673 - baseaddr);
8674 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8675 this_lowpc, this_highpc - 1,
8676 cu->per_cu->v.psymtab);
8677 }
8678 }
8679
8680 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8681 {
8682 if (!pdi->is_declaration)
8683 /* Ignore subprogram DIEs that do not have a name, they are
8684 illegal. Do not emit a complaint at this point, we will
8685 do so when we convert this psymtab into a symtab. */
8686 if (pdi->name)
8687 add_partial_symbol (pdi, cu);
8688 }
8689 }
8690
8691 if (! pdi->has_children)
8692 return;
8693
8694 if (cu->language == language_ada || cu->language == language_fortran)
8695 {
8696 pdi = pdi->die_child;
8697 while (pdi != NULL)
8698 {
8699 pdi->fixup (cu);
8700 if (pdi->tag == DW_TAG_subprogram
8701 || pdi->tag == DW_TAG_inlined_subroutine
8702 || pdi->tag == DW_TAG_lexical_block)
8703 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8704 pdi = pdi->die_sibling;
8705 }
8706 }
8707 }
8708
8709 /* Read a partial die corresponding to an enumeration type. */
8710
8711 static void
8712 add_partial_enumeration (struct partial_die_info *enum_pdi,
8713 struct dwarf2_cu *cu)
8714 {
8715 struct partial_die_info *pdi;
8716
8717 if (enum_pdi->name != NULL)
8718 add_partial_symbol (enum_pdi, cu);
8719
8720 pdi = enum_pdi->die_child;
8721 while (pdi)
8722 {
8723 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8724 complaint (_("malformed enumerator DIE ignored"));
8725 else
8726 add_partial_symbol (pdi, cu);
8727 pdi = pdi->die_sibling;
8728 }
8729 }
8730
8731 /* Return the initial uleb128 in the die at INFO_PTR. */
8732
8733 static unsigned int
8734 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8735 {
8736 unsigned int bytes_read;
8737
8738 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8739 }
8740
8741 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8742 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8743
8744 Return the corresponding abbrev, or NULL if the number is zero (indicating
8745 an empty DIE). In either case *BYTES_READ will be set to the length of
8746 the initial number. */
8747
8748 static struct abbrev_info *
8749 peek_die_abbrev (const die_reader_specs &reader,
8750 const gdb_byte *info_ptr, unsigned int *bytes_read)
8751 {
8752 dwarf2_cu *cu = reader.cu;
8753 bfd *abfd = cu->per_objfile->objfile->obfd;
8754 unsigned int abbrev_number
8755 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8756
8757 if (abbrev_number == 0)
8758 return NULL;
8759
8760 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8761 if (!abbrev)
8762 {
8763 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8764 " at offset %s [in module %s]"),
8765 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8766 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8767 }
8768
8769 return abbrev;
8770 }
8771
8772 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8773 Returns a pointer to the end of a series of DIEs, terminated by an empty
8774 DIE. Any children of the skipped DIEs will also be skipped. */
8775
8776 static const gdb_byte *
8777 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8778 {
8779 while (1)
8780 {
8781 unsigned int bytes_read;
8782 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8783
8784 if (abbrev == NULL)
8785 return info_ptr + bytes_read;
8786 else
8787 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8788 }
8789 }
8790
8791 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8792 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8793 abbrev corresponding to that skipped uleb128 should be passed in
8794 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8795 children. */
8796
8797 static const gdb_byte *
8798 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8799 struct abbrev_info *abbrev)
8800 {
8801 unsigned int bytes_read;
8802 struct attribute attr;
8803 bfd *abfd = reader->abfd;
8804 struct dwarf2_cu *cu = reader->cu;
8805 const gdb_byte *buffer = reader->buffer;
8806 const gdb_byte *buffer_end = reader->buffer_end;
8807 unsigned int form, i;
8808
8809 for (i = 0; i < abbrev->num_attrs; i++)
8810 {
8811 /* The only abbrev we care about is DW_AT_sibling. */
8812 if (abbrev->attrs[i].name == DW_AT_sibling)
8813 {
8814 bool ignored;
8815 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8816 &ignored);
8817 if (attr.form == DW_FORM_ref_addr)
8818 complaint (_("ignoring absolute DW_AT_sibling"));
8819 else
8820 {
8821 sect_offset off = attr.get_ref_die_offset ();
8822 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8823
8824 if (sibling_ptr < info_ptr)
8825 complaint (_("DW_AT_sibling points backwards"));
8826 else if (sibling_ptr > reader->buffer_end)
8827 reader->die_section->overflow_complaint ();
8828 else
8829 return sibling_ptr;
8830 }
8831 }
8832
8833 /* If it isn't DW_AT_sibling, skip this attribute. */
8834 form = abbrev->attrs[i].form;
8835 skip_attribute:
8836 switch (form)
8837 {
8838 case DW_FORM_ref_addr:
8839 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8840 and later it is offset sized. */
8841 if (cu->header.version == 2)
8842 info_ptr += cu->header.addr_size;
8843 else
8844 info_ptr += cu->header.offset_size;
8845 break;
8846 case DW_FORM_GNU_ref_alt:
8847 info_ptr += cu->header.offset_size;
8848 break;
8849 case DW_FORM_addr:
8850 info_ptr += cu->header.addr_size;
8851 break;
8852 case DW_FORM_data1:
8853 case DW_FORM_ref1:
8854 case DW_FORM_flag:
8855 case DW_FORM_strx1:
8856 info_ptr += 1;
8857 break;
8858 case DW_FORM_flag_present:
8859 case DW_FORM_implicit_const:
8860 break;
8861 case DW_FORM_data2:
8862 case DW_FORM_ref2:
8863 case DW_FORM_strx2:
8864 info_ptr += 2;
8865 break;
8866 case DW_FORM_strx3:
8867 info_ptr += 3;
8868 break;
8869 case DW_FORM_data4:
8870 case DW_FORM_ref4:
8871 case DW_FORM_strx4:
8872 info_ptr += 4;
8873 break;
8874 case DW_FORM_data8:
8875 case DW_FORM_ref8:
8876 case DW_FORM_ref_sig8:
8877 info_ptr += 8;
8878 break;
8879 case DW_FORM_data16:
8880 info_ptr += 16;
8881 break;
8882 case DW_FORM_string:
8883 read_direct_string (abfd, info_ptr, &bytes_read);
8884 info_ptr += bytes_read;
8885 break;
8886 case DW_FORM_sec_offset:
8887 case DW_FORM_strp:
8888 case DW_FORM_GNU_strp_alt:
8889 info_ptr += cu->header.offset_size;
8890 break;
8891 case DW_FORM_exprloc:
8892 case DW_FORM_block:
8893 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8894 info_ptr += bytes_read;
8895 break;
8896 case DW_FORM_block1:
8897 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8898 break;
8899 case DW_FORM_block2:
8900 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8901 break;
8902 case DW_FORM_block4:
8903 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8904 break;
8905 case DW_FORM_addrx:
8906 case DW_FORM_strx:
8907 case DW_FORM_sdata:
8908 case DW_FORM_udata:
8909 case DW_FORM_ref_udata:
8910 case DW_FORM_GNU_addr_index:
8911 case DW_FORM_GNU_str_index:
8912 case DW_FORM_rnglistx:
8913 case DW_FORM_loclistx:
8914 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8915 break;
8916 case DW_FORM_indirect:
8917 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8918 info_ptr += bytes_read;
8919 /* We need to continue parsing from here, so just go back to
8920 the top. */
8921 goto skip_attribute;
8922
8923 default:
8924 error (_("Dwarf Error: Cannot handle %s "
8925 "in DWARF reader [in module %s]"),
8926 dwarf_form_name (form),
8927 bfd_get_filename (abfd));
8928 }
8929 }
8930
8931 if (abbrev->has_children)
8932 return skip_children (reader, info_ptr);
8933 else
8934 return info_ptr;
8935 }
8936
8937 /* Locate ORIG_PDI's sibling.
8938 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8939
8940 static const gdb_byte *
8941 locate_pdi_sibling (const struct die_reader_specs *reader,
8942 struct partial_die_info *orig_pdi,
8943 const gdb_byte *info_ptr)
8944 {
8945 /* Do we know the sibling already? */
8946
8947 if (orig_pdi->sibling)
8948 return orig_pdi->sibling;
8949
8950 /* Are there any children to deal with? */
8951
8952 if (!orig_pdi->has_children)
8953 return info_ptr;
8954
8955 /* Skip the children the long way. */
8956
8957 return skip_children (reader, info_ptr);
8958 }
8959
8960 /* Expand this partial symbol table into a full symbol table. SELF is
8961 not NULL. */
8962
8963 void
8964 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8965 {
8966 struct dwarf2_per_objfile *dwarf2_per_objfile
8967 = get_dwarf2_per_objfile (objfile);
8968
8969 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8970
8971 /* If this psymtab is constructed from a debug-only objfile, the
8972 has_section_at_zero flag will not necessarily be correct. We
8973 can get the correct value for this flag by looking at the data
8974 associated with the (presumably stripped) associated objfile. */
8975 if (objfile->separate_debug_objfile_backlink)
8976 {
8977 struct dwarf2_per_objfile *dpo_backlink
8978 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8979
8980 dwarf2_per_objfile->per_bfd->has_section_at_zero
8981 = dpo_backlink->per_bfd->has_section_at_zero;
8982 }
8983
8984 expand_psymtab (objfile);
8985
8986 process_cu_includes (dwarf2_per_objfile);
8987 }
8988 \f
8989 /* Reading in full CUs. */
8990
8991 /* Add PER_CU to the queue. */
8992
8993 static void
8994 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8995 dwarf2_per_objfile *per_objfile,
8996 enum language pretend_language)
8997 {
8998 per_cu->queued = 1;
8999 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
9000 }
9001
9002 /* If PER_CU is not yet queued, add it to the queue.
9003 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9004 dependency.
9005 The result is non-zero if PER_CU was queued, otherwise the result is zero
9006 meaning either PER_CU is already queued or it is already loaded.
9007
9008 N.B. There is an invariant here that if a CU is queued then it is loaded.
9009 The caller is required to load PER_CU if we return non-zero. */
9010
9011 static int
9012 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9013 dwarf2_per_cu_data *per_cu,
9014 dwarf2_per_objfile *per_objfile,
9015 enum language pretend_language)
9016 {
9017 /* We may arrive here during partial symbol reading, if we need full
9018 DIEs to process an unusual case (e.g. template arguments). Do
9019 not queue PER_CU, just tell our caller to load its DIEs. */
9020 if (per_cu->per_bfd->reading_partial_symbols)
9021 {
9022 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9023
9024 if (cu == NULL || cu->dies == NULL)
9025 return 1;
9026 return 0;
9027 }
9028
9029 /* Mark the dependence relation so that we don't flush PER_CU
9030 too early. */
9031 if (dependent_cu != NULL)
9032 dwarf2_add_dependence (dependent_cu, per_cu);
9033
9034 /* If it's already on the queue, we have nothing to do. */
9035 if (per_cu->queued)
9036 return 0;
9037
9038 /* If the compilation unit is already loaded, just mark it as
9039 used. */
9040 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9041 if (cu != nullptr)
9042 {
9043 cu->last_used = 0;
9044 return 0;
9045 }
9046
9047 /* Add it to the queue. */
9048 queue_comp_unit (per_cu, per_objfile, pretend_language);
9049
9050 return 1;
9051 }
9052
9053 /* Process the queue. */
9054
9055 static void
9056 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9057 {
9058 if (dwarf_read_debug)
9059 {
9060 fprintf_unfiltered (gdb_stdlog,
9061 "Expanding one or more symtabs of objfile %s ...\n",
9062 objfile_name (dwarf2_per_objfile->objfile));
9063 }
9064
9065 /* The queue starts out with one item, but following a DIE reference
9066 may load a new CU, adding it to the end of the queue. */
9067 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
9068 {
9069 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
9070 dwarf2_per_cu_data *per_cu = item.per_cu;
9071
9072 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9073 {
9074 dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
9075
9076 /* Skip dummy CUs. */
9077 if (cu != nullptr)
9078 {
9079 unsigned int debug_print_threshold;
9080 char buf[100];
9081
9082 if (per_cu->is_debug_types)
9083 {
9084 struct signatured_type *sig_type =
9085 (struct signatured_type *) per_cu;
9086
9087 sprintf (buf, "TU %s at offset %s",
9088 hex_string (sig_type->signature),
9089 sect_offset_str (per_cu->sect_off));
9090 /* There can be 100s of TUs.
9091 Only print them in verbose mode. */
9092 debug_print_threshold = 2;
9093 }
9094 else
9095 {
9096 sprintf (buf, "CU at offset %s",
9097 sect_offset_str (per_cu->sect_off));
9098 debug_print_threshold = 1;
9099 }
9100
9101 if (dwarf_read_debug >= debug_print_threshold)
9102 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9103
9104 if (per_cu->is_debug_types)
9105 process_full_type_unit (cu, item.pretend_language);
9106 else
9107 process_full_comp_unit (cu, item.pretend_language);
9108
9109 if (dwarf_read_debug >= debug_print_threshold)
9110 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9111 }
9112 }
9113
9114 per_cu->queued = 0;
9115 dwarf2_per_objfile->per_bfd->queue.pop ();
9116 }
9117
9118 if (dwarf_read_debug)
9119 {
9120 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9121 objfile_name (dwarf2_per_objfile->objfile));
9122 }
9123 }
9124
9125 /* Read in full symbols for PST, and anything it depends on. */
9126
9127 void
9128 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9129 {
9130 gdb_assert (!readin_p (objfile));
9131
9132 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9133 free_cached_comp_units freer (per_objfile);
9134 expand_dependencies (objfile);
9135
9136 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9137 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9138 }
9139
9140 /* See psympriv.h. */
9141
9142 bool
9143 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9144 {
9145 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9146 return per_objfile->symtab_set_p (per_cu_data);
9147 }
9148
9149 /* See psympriv.h. */
9150
9151 compunit_symtab *
9152 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9153 {
9154 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9155 return per_objfile->get_symtab (per_cu_data);
9156 }
9157
9158 /* Trivial hash function for die_info: the hash value of a DIE
9159 is its offset in .debug_info for this objfile. */
9160
9161 static hashval_t
9162 die_hash (const void *item)
9163 {
9164 const struct die_info *die = (const struct die_info *) item;
9165
9166 return to_underlying (die->sect_off);
9167 }
9168
9169 /* Trivial comparison function for die_info structures: two DIEs
9170 are equal if they have the same offset. */
9171
9172 static int
9173 die_eq (const void *item_lhs, const void *item_rhs)
9174 {
9175 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9176 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9177
9178 return die_lhs->sect_off == die_rhs->sect_off;
9179 }
9180
9181 /* Load the DIEs associated with PER_CU into memory. */
9182
9183 static void
9184 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9185 dwarf2_per_objfile *per_objfile,
9186 bool skip_partial,
9187 enum language pretend_language)
9188 {
9189 gdb_assert (! this_cu->is_debug_types);
9190
9191 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9192 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9193 if (reader.dummy_p)
9194 return;
9195
9196 struct dwarf2_cu *cu = reader.cu;
9197 const gdb_byte *info_ptr = reader.info_ptr;
9198
9199 gdb_assert (cu->die_hash == NULL);
9200 cu->die_hash =
9201 htab_create_alloc_ex (cu->header.length / 12,
9202 die_hash,
9203 die_eq,
9204 NULL,
9205 &cu->comp_unit_obstack,
9206 hashtab_obstack_allocate,
9207 dummy_obstack_deallocate);
9208
9209 if (reader.comp_unit_die->has_children)
9210 reader.comp_unit_die->child
9211 = read_die_and_siblings (&reader, reader.info_ptr,
9212 &info_ptr, reader.comp_unit_die);
9213 cu->dies = reader.comp_unit_die;
9214 /* comp_unit_die is not stored in die_hash, no need. */
9215
9216 /* We try not to read any attributes in this function, because not
9217 all CUs needed for references have been loaded yet, and symbol
9218 table processing isn't initialized. But we have to set the CU language,
9219 or we won't be able to build types correctly.
9220 Similarly, if we do not read the producer, we can not apply
9221 producer-specific interpretation. */
9222 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9223
9224 reader.keep ();
9225 }
9226
9227 /* Add a DIE to the delayed physname list. */
9228
9229 static void
9230 add_to_method_list (struct type *type, int fnfield_index, int index,
9231 const char *name, struct die_info *die,
9232 struct dwarf2_cu *cu)
9233 {
9234 struct delayed_method_info mi;
9235 mi.type = type;
9236 mi.fnfield_index = fnfield_index;
9237 mi.index = index;
9238 mi.name = name;
9239 mi.die = die;
9240 cu->method_list.push_back (mi);
9241 }
9242
9243 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9244 "const" / "volatile". If so, decrements LEN by the length of the
9245 modifier and return true. Otherwise return false. */
9246
9247 template<size_t N>
9248 static bool
9249 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9250 {
9251 size_t mod_len = sizeof (mod) - 1;
9252 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9253 {
9254 len -= mod_len;
9255 return true;
9256 }
9257 return false;
9258 }
9259
9260 /* Compute the physnames of any methods on the CU's method list.
9261
9262 The computation of method physnames is delayed in order to avoid the
9263 (bad) condition that one of the method's formal parameters is of an as yet
9264 incomplete type. */
9265
9266 static void
9267 compute_delayed_physnames (struct dwarf2_cu *cu)
9268 {
9269 /* Only C++ delays computing physnames. */
9270 if (cu->method_list.empty ())
9271 return;
9272 gdb_assert (cu->language == language_cplus);
9273
9274 for (const delayed_method_info &mi : cu->method_list)
9275 {
9276 const char *physname;
9277 struct fn_fieldlist *fn_flp
9278 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9279 physname = dwarf2_physname (mi.name, mi.die, cu);
9280 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9281 = physname ? physname : "";
9282
9283 /* Since there's no tag to indicate whether a method is a
9284 const/volatile overload, extract that information out of the
9285 demangled name. */
9286 if (physname != NULL)
9287 {
9288 size_t len = strlen (physname);
9289
9290 while (1)
9291 {
9292 if (physname[len] == ')') /* shortcut */
9293 break;
9294 else if (check_modifier (physname, len, " const"))
9295 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9296 else if (check_modifier (physname, len, " volatile"))
9297 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9298 else
9299 break;
9300 }
9301 }
9302 }
9303
9304 /* The list is no longer needed. */
9305 cu->method_list.clear ();
9306 }
9307
9308 /* Go objects should be embedded in a DW_TAG_module DIE,
9309 and it's not clear if/how imported objects will appear.
9310 To keep Go support simple until that's worked out,
9311 go back through what we've read and create something usable.
9312 We could do this while processing each DIE, and feels kinda cleaner,
9313 but that way is more invasive.
9314 This is to, for example, allow the user to type "p var" or "b main"
9315 without having to specify the package name, and allow lookups
9316 of module.object to work in contexts that use the expression
9317 parser. */
9318
9319 static void
9320 fixup_go_packaging (struct dwarf2_cu *cu)
9321 {
9322 gdb::unique_xmalloc_ptr<char> package_name;
9323 struct pending *list;
9324 int i;
9325
9326 for (list = *cu->get_builder ()->get_global_symbols ();
9327 list != NULL;
9328 list = list->next)
9329 {
9330 for (i = 0; i < list->nsyms; ++i)
9331 {
9332 struct symbol *sym = list->symbol[i];
9333
9334 if (sym->language () == language_go
9335 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9336 {
9337 gdb::unique_xmalloc_ptr<char> this_package_name
9338 (go_symbol_package_name (sym));
9339
9340 if (this_package_name == NULL)
9341 continue;
9342 if (package_name == NULL)
9343 package_name = std::move (this_package_name);
9344 else
9345 {
9346 struct objfile *objfile = cu->per_objfile->objfile;
9347 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9348 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9349 (symbol_symtab (sym) != NULL
9350 ? symtab_to_filename_for_display
9351 (symbol_symtab (sym))
9352 : objfile_name (objfile)),
9353 this_package_name.get (), package_name.get ());
9354 }
9355 }
9356 }
9357 }
9358
9359 if (package_name != NULL)
9360 {
9361 struct objfile *objfile = cu->per_objfile->objfile;
9362 const char *saved_package_name = objfile->intern (package_name.get ());
9363 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9364 saved_package_name);
9365 struct symbol *sym;
9366
9367 sym = new (&objfile->objfile_obstack) symbol;
9368 sym->set_language (language_go, &objfile->objfile_obstack);
9369 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9370 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9371 e.g., "main" finds the "main" module and not C's main(). */
9372 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9373 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9374 SYMBOL_TYPE (sym) = type;
9375
9376 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9377 }
9378 }
9379
9380 /* Allocate a fully-qualified name consisting of the two parts on the
9381 obstack. */
9382
9383 static const char *
9384 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9385 {
9386 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9387 }
9388
9389 /* A helper that allocates a variant part to attach to a Rust enum
9390 type. OBSTACK is where the results should be allocated. TYPE is
9391 the type we're processing. DISCRIMINANT_INDEX is the index of the
9392 discriminant. It must be the index of one of the fields of TYPE.
9393 DEFAULT_INDEX is the index of the default field; or -1 if there is
9394 no default. RANGES is indexed by "effective" field number (the
9395 field index, but omitting the discriminant and default fields) and
9396 must hold the discriminant values used by the variants. Note that
9397 RANGES must have a lifetime at least as long as OBSTACK -- either
9398 already allocated on it, or static. */
9399
9400 static void
9401 alloc_rust_variant (struct obstack *obstack, struct type *type,
9402 int discriminant_index, int default_index,
9403 gdb::array_view<discriminant_range> ranges)
9404 {
9405 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9406 must be handled by the caller. */
9407 gdb_assert (discriminant_index >= 0
9408 && discriminant_index < type->num_fields ());
9409 gdb_assert (default_index == -1
9410 || (default_index >= 0 && default_index < type->num_fields ()));
9411
9412 /* We have one variant for each non-discriminant field. */
9413 int n_variants = type->num_fields () - 1;
9414
9415 variant *variants = new (obstack) variant[n_variants];
9416 int var_idx = 0;
9417 int range_idx = 0;
9418 for (int i = 0; i < type->num_fields (); ++i)
9419 {
9420 if (i == discriminant_index)
9421 continue;
9422
9423 variants[var_idx].first_field = i;
9424 variants[var_idx].last_field = i + 1;
9425
9426 /* The default field does not need a range, but other fields do.
9427 We skipped the discriminant above. */
9428 if (i != default_index)
9429 {
9430 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9431 ++range_idx;
9432 }
9433
9434 ++var_idx;
9435 }
9436
9437 gdb_assert (range_idx == ranges.size ());
9438 gdb_assert (var_idx == n_variants);
9439
9440 variant_part *part = new (obstack) variant_part;
9441 part->discriminant_index = discriminant_index;
9442 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9443 discriminant_index));
9444 part->variants = gdb::array_view<variant> (variants, n_variants);
9445
9446 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9447 gdb::array_view<variant_part> *prop_value
9448 = new (storage) gdb::array_view<variant_part> (part, 1);
9449
9450 struct dynamic_prop prop;
9451 prop.kind = PROP_VARIANT_PARTS;
9452 prop.data.variant_parts = prop_value;
9453
9454 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9455 }
9456
9457 /* Some versions of rustc emitted enums in an unusual way.
9458
9459 Ordinary enums were emitted as unions. The first element of each
9460 structure in the union was named "RUST$ENUM$DISR". This element
9461 held the discriminant.
9462
9463 These versions of Rust also implemented the "non-zero"
9464 optimization. When the enum had two values, and one is empty and
9465 the other holds a pointer that cannot be zero, the pointer is used
9466 as the discriminant, with a zero value meaning the empty variant.
9467 Here, the union's first member is of the form
9468 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9469 where the fieldnos are the indices of the fields that should be
9470 traversed in order to find the field (which may be several fields deep)
9471 and the variantname is the name of the variant of the case when the
9472 field is zero.
9473
9474 This function recognizes whether TYPE is of one of these forms,
9475 and, if so, smashes it to be a variant type. */
9476
9477 static void
9478 quirk_rust_enum (struct type *type, struct objfile *objfile)
9479 {
9480 gdb_assert (type->code () == TYPE_CODE_UNION);
9481
9482 /* We don't need to deal with empty enums. */
9483 if (type->num_fields () == 0)
9484 return;
9485
9486 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9487 if (type->num_fields () == 1
9488 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9489 {
9490 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9491
9492 /* Decode the field name to find the offset of the
9493 discriminant. */
9494 ULONGEST bit_offset = 0;
9495 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9496 while (name[0] >= '0' && name[0] <= '9')
9497 {
9498 char *tail;
9499 unsigned long index = strtoul (name, &tail, 10);
9500 name = tail;
9501 if (*name != '$'
9502 || index >= field_type->num_fields ()
9503 || (TYPE_FIELD_LOC_KIND (field_type, index)
9504 != FIELD_LOC_KIND_BITPOS))
9505 {
9506 complaint (_("Could not parse Rust enum encoding string \"%s\""
9507 "[in module %s]"),
9508 TYPE_FIELD_NAME (type, 0),
9509 objfile_name (objfile));
9510 return;
9511 }
9512 ++name;
9513
9514 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9515 field_type = TYPE_FIELD_TYPE (field_type, index);
9516 }
9517
9518 /* Smash this type to be a structure type. We have to do this
9519 because the type has already been recorded. */
9520 type->set_code (TYPE_CODE_STRUCT);
9521 type->set_num_fields (3);
9522 /* Save the field we care about. */
9523 struct field saved_field = type->field (0);
9524 type->set_fields
9525 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9526
9527 /* Put the discriminant at index 0. */
9528 TYPE_FIELD_TYPE (type, 0) = field_type;
9529 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9530 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9531 SET_FIELD_BITPOS (type->field (0), bit_offset);
9532
9533 /* The order of fields doesn't really matter, so put the real
9534 field at index 1 and the data-less field at index 2. */
9535 type->field (1) = saved_field;
9536 TYPE_FIELD_NAME (type, 1)
9537 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9538 TYPE_FIELD_TYPE (type, 1)->set_name
9539 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9540 TYPE_FIELD_NAME (type, 1)));
9541
9542 const char *dataless_name
9543 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9544 name);
9545 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9546 dataless_name);
9547 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9548 /* NAME points into the original discriminant name, which
9549 already has the correct lifetime. */
9550 TYPE_FIELD_NAME (type, 2) = name;
9551 SET_FIELD_BITPOS (type->field (2), 0);
9552
9553 /* Indicate that this is a variant type. */
9554 static discriminant_range ranges[1] = { { 0, 0 } };
9555 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9556 }
9557 /* A union with a single anonymous field is probably an old-style
9558 univariant enum. */
9559 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9560 {
9561 /* Smash this type to be a structure type. We have to do this
9562 because the type has already been recorded. */
9563 type->set_code (TYPE_CODE_STRUCT);
9564
9565 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9566 const char *variant_name
9567 = rust_last_path_segment (field_type->name ());
9568 TYPE_FIELD_NAME (type, 0) = variant_name;
9569 field_type->set_name
9570 (rust_fully_qualify (&objfile->objfile_obstack,
9571 type->name (), variant_name));
9572 }
9573 else
9574 {
9575 struct type *disr_type = nullptr;
9576 for (int i = 0; i < type->num_fields (); ++i)
9577 {
9578 disr_type = TYPE_FIELD_TYPE (type, i);
9579
9580 if (disr_type->code () != TYPE_CODE_STRUCT)
9581 {
9582 /* All fields of a true enum will be structs. */
9583 return;
9584 }
9585 else if (disr_type->num_fields () == 0)
9586 {
9587 /* Could be data-less variant, so keep going. */
9588 disr_type = nullptr;
9589 }
9590 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9591 "RUST$ENUM$DISR") != 0)
9592 {
9593 /* Not a Rust enum. */
9594 return;
9595 }
9596 else
9597 {
9598 /* Found one. */
9599 break;
9600 }
9601 }
9602
9603 /* If we got here without a discriminant, then it's probably
9604 just a union. */
9605 if (disr_type == nullptr)
9606 return;
9607
9608 /* Smash this type to be a structure type. We have to do this
9609 because the type has already been recorded. */
9610 type->set_code (TYPE_CODE_STRUCT);
9611
9612 /* Make space for the discriminant field. */
9613 struct field *disr_field = &disr_type->field (0);
9614 field *new_fields
9615 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9616 * sizeof (struct field)));
9617 memcpy (new_fields + 1, type->fields (),
9618 type->num_fields () * sizeof (struct field));
9619 type->set_fields (new_fields);
9620 type->set_num_fields (type->num_fields () + 1);
9621
9622 /* Install the discriminant at index 0 in the union. */
9623 type->field (0) = *disr_field;
9624 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9625 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9626
9627 /* We need a way to find the correct discriminant given a
9628 variant name. For convenience we build a map here. */
9629 struct type *enum_type = FIELD_TYPE (*disr_field);
9630 std::unordered_map<std::string, ULONGEST> discriminant_map;
9631 for (int i = 0; i < enum_type->num_fields (); ++i)
9632 {
9633 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9634 {
9635 const char *name
9636 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9637 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9638 }
9639 }
9640
9641 int n_fields = type->num_fields ();
9642 /* We don't need a range entry for the discriminant, but we do
9643 need one for every other field, as there is no default
9644 variant. */
9645 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9646 discriminant_range,
9647 n_fields - 1);
9648 /* Skip the discriminant here. */
9649 for (int i = 1; i < n_fields; ++i)
9650 {
9651 /* Find the final word in the name of this variant's type.
9652 That name can be used to look up the correct
9653 discriminant. */
9654 const char *variant_name
9655 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9656
9657 auto iter = discriminant_map.find (variant_name);
9658 if (iter != discriminant_map.end ())
9659 {
9660 ranges[i].low = iter->second;
9661 ranges[i].high = iter->second;
9662 }
9663
9664 /* Remove the discriminant field, if it exists. */
9665 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9666 if (sub_type->num_fields () > 0)
9667 {
9668 sub_type->set_num_fields (sub_type->num_fields () - 1);
9669 sub_type->set_fields (sub_type->fields () + 1);
9670 }
9671 TYPE_FIELD_NAME (type, i) = variant_name;
9672 sub_type->set_name
9673 (rust_fully_qualify (&objfile->objfile_obstack,
9674 type->name (), variant_name));
9675 }
9676
9677 /* Indicate that this is a variant type. */
9678 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9679 gdb::array_view<discriminant_range> (ranges,
9680 n_fields - 1));
9681 }
9682 }
9683
9684 /* Rewrite some Rust unions to be structures with variants parts. */
9685
9686 static void
9687 rust_union_quirks (struct dwarf2_cu *cu)
9688 {
9689 gdb_assert (cu->language == language_rust);
9690 for (type *type_ : cu->rust_unions)
9691 quirk_rust_enum (type_, cu->per_objfile->objfile);
9692 /* We don't need this any more. */
9693 cu->rust_unions.clear ();
9694 }
9695
9696 /* See read.h. */
9697
9698 type_unit_group_unshareable *
9699 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9700 {
9701 auto iter = this->m_type_units.find (tu_group);
9702 if (iter != this->m_type_units.end ())
9703 return iter->second.get ();
9704
9705 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9706 type_unit_group_unshareable *result = uniq.get ();
9707 this->m_type_units[tu_group] = std::move (uniq);
9708 return result;
9709 }
9710
9711 struct type *
9712 dwarf2_per_objfile::get_type_for_signatured_type
9713 (signatured_type *sig_type) const
9714 {
9715 auto iter = this->m_type_map.find (sig_type);
9716 if (iter == this->m_type_map.end ())
9717 return nullptr;
9718
9719 return iter->second;
9720 }
9721
9722 void dwarf2_per_objfile::set_type_for_signatured_type
9723 (signatured_type *sig_type, struct type *type)
9724 {
9725 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9726
9727 this->m_type_map[sig_type] = type;
9728 }
9729
9730 /* A helper function for computing the list of all symbol tables
9731 included by PER_CU. */
9732
9733 static void
9734 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9735 htab_t all_children, htab_t all_type_symtabs,
9736 dwarf2_per_cu_data *per_cu,
9737 dwarf2_per_objfile *per_objfile,
9738 struct compunit_symtab *immediate_parent)
9739 {
9740 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9741 if (*slot != NULL)
9742 {
9743 /* This inclusion and its children have been processed. */
9744 return;
9745 }
9746
9747 *slot = per_cu;
9748
9749 /* Only add a CU if it has a symbol table. */
9750 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9751 if (cust != NULL)
9752 {
9753 /* If this is a type unit only add its symbol table if we haven't
9754 seen it yet (type unit per_cu's can share symtabs). */
9755 if (per_cu->is_debug_types)
9756 {
9757 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9758 if (*slot == NULL)
9759 {
9760 *slot = cust;
9761 result->push_back (cust);
9762 if (cust->user == NULL)
9763 cust->user = immediate_parent;
9764 }
9765 }
9766 else
9767 {
9768 result->push_back (cust);
9769 if (cust->user == NULL)
9770 cust->user = immediate_parent;
9771 }
9772 }
9773
9774 if (!per_cu->imported_symtabs_empty ())
9775 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9776 {
9777 recursively_compute_inclusions (result, all_children,
9778 all_type_symtabs, ptr, per_objfile,
9779 cust);
9780 }
9781 }
9782
9783 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9784 PER_CU. */
9785
9786 static void
9787 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9788 dwarf2_per_objfile *per_objfile)
9789 {
9790 gdb_assert (! per_cu->is_debug_types);
9791
9792 if (!per_cu->imported_symtabs_empty ())
9793 {
9794 int len;
9795 std::vector<compunit_symtab *> result_symtabs;
9796 htab_t all_children, all_type_symtabs;
9797 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9798
9799 /* If we don't have a symtab, we can just skip this case. */
9800 if (cust == NULL)
9801 return;
9802
9803 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9804 NULL, xcalloc, xfree);
9805 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9806 NULL, xcalloc, xfree);
9807
9808 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9809 {
9810 recursively_compute_inclusions (&result_symtabs, all_children,
9811 all_type_symtabs, ptr, per_objfile,
9812 cust);
9813 }
9814
9815 /* Now we have a transitive closure of all the included symtabs. */
9816 len = result_symtabs.size ();
9817 cust->includes
9818 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9819 struct compunit_symtab *, len + 1);
9820 memcpy (cust->includes, result_symtabs.data (),
9821 len * sizeof (compunit_symtab *));
9822 cust->includes[len] = NULL;
9823
9824 htab_delete (all_children);
9825 htab_delete (all_type_symtabs);
9826 }
9827 }
9828
9829 /* Compute the 'includes' field for the symtabs of all the CUs we just
9830 read. */
9831
9832 static void
9833 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9834 {
9835 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9836 {
9837 if (! iter->is_debug_types)
9838 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
9839 }
9840
9841 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9842 }
9843
9844 /* Generate full symbol information for CU, whose DIEs have
9845 already been loaded into memory. */
9846
9847 static void
9848 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9849 {
9850 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9851 struct objfile *objfile = dwarf2_per_objfile->objfile;
9852 struct gdbarch *gdbarch = objfile->arch ();
9853 CORE_ADDR lowpc, highpc;
9854 struct compunit_symtab *cust;
9855 CORE_ADDR baseaddr;
9856 struct block *static_block;
9857 CORE_ADDR addr;
9858
9859 baseaddr = objfile->text_section_offset ();
9860
9861 /* Clear the list here in case something was left over. */
9862 cu->method_list.clear ();
9863
9864 cu->language = pretend_language;
9865 cu->language_defn = language_def (cu->language);
9866
9867 /* Do line number decoding in read_file_scope () */
9868 process_die (cu->dies, cu);
9869
9870 /* For now fudge the Go package. */
9871 if (cu->language == language_go)
9872 fixup_go_packaging (cu);
9873
9874 /* Now that we have processed all the DIEs in the CU, all the types
9875 should be complete, and it should now be safe to compute all of the
9876 physnames. */
9877 compute_delayed_physnames (cu);
9878
9879 if (cu->language == language_rust)
9880 rust_union_quirks (cu);
9881
9882 /* Some compilers don't define a DW_AT_high_pc attribute for the
9883 compilation unit. If the DW_AT_high_pc is missing, synthesize
9884 it, by scanning the DIE's below the compilation unit. */
9885 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9886
9887 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9888 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9889
9890 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9891 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9892 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9893 addrmap to help ensure it has an accurate map of pc values belonging to
9894 this comp unit. */
9895 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9896
9897 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9898 SECT_OFF_TEXT (objfile),
9899 0);
9900
9901 if (cust != NULL)
9902 {
9903 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9904
9905 /* Set symtab language to language from DW_AT_language. If the
9906 compilation is from a C file generated by language preprocessors, do
9907 not set the language if it was already deduced by start_subfile. */
9908 if (!(cu->language == language_c
9909 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9910 COMPUNIT_FILETABS (cust)->language = cu->language;
9911
9912 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9913 produce DW_AT_location with location lists but it can be possibly
9914 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9915 there were bugs in prologue debug info, fixed later in GCC-4.5
9916 by "unwind info for epilogues" patch (which is not directly related).
9917
9918 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9919 needed, it would be wrong due to missing DW_AT_producer there.
9920
9921 Still one can confuse GDB by using non-standard GCC compilation
9922 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9923 */
9924 if (cu->has_loclist && gcc_4_minor >= 5)
9925 cust->locations_valid = 1;
9926
9927 if (gcc_4_minor >= 5)
9928 cust->epilogue_unwind_valid = 1;
9929
9930 cust->call_site_htab = cu->call_site_htab;
9931 }
9932
9933 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
9934
9935 /* Push it for inclusion processing later. */
9936 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9937
9938 /* Not needed any more. */
9939 cu->reset_builder ();
9940 }
9941
9942 /* Generate full symbol information for type unit CU, whose DIEs have
9943 already been loaded into memory. */
9944
9945 static void
9946 process_full_type_unit (dwarf2_cu *cu,
9947 enum language pretend_language)
9948 {
9949 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9950 struct objfile *objfile = dwarf2_per_objfile->objfile;
9951 struct compunit_symtab *cust;
9952 struct signatured_type *sig_type;
9953
9954 gdb_assert (cu->per_cu->is_debug_types);
9955 sig_type = (struct signatured_type *) cu->per_cu;
9956
9957 /* Clear the list here in case something was left over. */
9958 cu->method_list.clear ();
9959
9960 cu->language = pretend_language;
9961 cu->language_defn = language_def (cu->language);
9962
9963 /* The symbol tables are set up in read_type_unit_scope. */
9964 process_die (cu->dies, cu);
9965
9966 /* For now fudge the Go package. */
9967 if (cu->language == language_go)
9968 fixup_go_packaging (cu);
9969
9970 /* Now that we have processed all the DIEs in the CU, all the types
9971 should be complete, and it should now be safe to compute all of the
9972 physnames. */
9973 compute_delayed_physnames (cu);
9974
9975 if (cu->language == language_rust)
9976 rust_union_quirks (cu);
9977
9978 /* TUs share symbol tables.
9979 If this is the first TU to use this symtab, complete the construction
9980 of it with end_expandable_symtab. Otherwise, complete the addition of
9981 this TU's symbols to the existing symtab. */
9982 type_unit_group_unshareable *tug_unshare =
9983 dwarf2_per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9984 if (tug_unshare->compunit_symtab == NULL)
9985 {
9986 buildsym_compunit *builder = cu->get_builder ();
9987 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9988 tug_unshare->compunit_symtab = cust;
9989
9990 if (cust != NULL)
9991 {
9992 /* Set symtab language to language from DW_AT_language. If the
9993 compilation is from a C file generated by language preprocessors,
9994 do not set the language if it was already deduced by
9995 start_subfile. */
9996 if (!(cu->language == language_c
9997 && COMPUNIT_FILETABS (cust)->language != language_c))
9998 COMPUNIT_FILETABS (cust)->language = cu->language;
9999 }
10000 }
10001 else
10002 {
10003 cu->get_builder ()->augment_type_symtab ();
10004 cust = tug_unshare->compunit_symtab;
10005 }
10006
10007 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
10008
10009 /* Not needed any more. */
10010 cu->reset_builder ();
10011 }
10012
10013 /* Process an imported unit DIE. */
10014
10015 static void
10016 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10017 {
10018 struct attribute *attr;
10019
10020 /* For now we don't handle imported units in type units. */
10021 if (cu->per_cu->is_debug_types)
10022 {
10023 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10024 " supported in type units [in module %s]"),
10025 objfile_name (cu->per_objfile->objfile));
10026 }
10027
10028 attr = dwarf2_attr (die, DW_AT_import, cu);
10029 if (attr != NULL)
10030 {
10031 sect_offset sect_off = attr->get_ref_die_offset ();
10032 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10033 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10034 dwarf2_per_cu_data *per_cu
10035 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10036
10037 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10038 into another compilation unit, at root level. Regard this as a hint,
10039 and ignore it. */
10040 if (die->parent && die->parent->parent == NULL
10041 && per_cu->unit_type == DW_UT_compile
10042 && per_cu->lang == language_cplus)
10043 return;
10044
10045 /* If necessary, add it to the queue and load its DIEs. */
10046 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10047 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
10048
10049 cu->per_cu->imported_symtabs_push (per_cu);
10050 }
10051 }
10052
10053 /* RAII object that represents a process_die scope: i.e.,
10054 starts/finishes processing a DIE. */
10055 class process_die_scope
10056 {
10057 public:
10058 process_die_scope (die_info *die, dwarf2_cu *cu)
10059 : m_die (die), m_cu (cu)
10060 {
10061 /* We should only be processing DIEs not already in process. */
10062 gdb_assert (!m_die->in_process);
10063 m_die->in_process = true;
10064 }
10065
10066 ~process_die_scope ()
10067 {
10068 m_die->in_process = false;
10069
10070 /* If we're done processing the DIE for the CU that owns the line
10071 header, we don't need the line header anymore. */
10072 if (m_cu->line_header_die_owner == m_die)
10073 {
10074 delete m_cu->line_header;
10075 m_cu->line_header = NULL;
10076 m_cu->line_header_die_owner = NULL;
10077 }
10078 }
10079
10080 private:
10081 die_info *m_die;
10082 dwarf2_cu *m_cu;
10083 };
10084
10085 /* Process a die and its children. */
10086
10087 static void
10088 process_die (struct die_info *die, struct dwarf2_cu *cu)
10089 {
10090 process_die_scope scope (die, cu);
10091
10092 switch (die->tag)
10093 {
10094 case DW_TAG_padding:
10095 break;
10096 case DW_TAG_compile_unit:
10097 case DW_TAG_partial_unit:
10098 read_file_scope (die, cu);
10099 break;
10100 case DW_TAG_type_unit:
10101 read_type_unit_scope (die, cu);
10102 break;
10103 case DW_TAG_subprogram:
10104 /* Nested subprograms in Fortran get a prefix. */
10105 if (cu->language == language_fortran
10106 && die->parent != NULL
10107 && die->parent->tag == DW_TAG_subprogram)
10108 cu->processing_has_namespace_info = true;
10109 /* Fall through. */
10110 case DW_TAG_inlined_subroutine:
10111 read_func_scope (die, cu);
10112 break;
10113 case DW_TAG_lexical_block:
10114 case DW_TAG_try_block:
10115 case DW_TAG_catch_block:
10116 read_lexical_block_scope (die, cu);
10117 break;
10118 case DW_TAG_call_site:
10119 case DW_TAG_GNU_call_site:
10120 read_call_site_scope (die, cu);
10121 break;
10122 case DW_TAG_class_type:
10123 case DW_TAG_interface_type:
10124 case DW_TAG_structure_type:
10125 case DW_TAG_union_type:
10126 process_structure_scope (die, cu);
10127 break;
10128 case DW_TAG_enumeration_type:
10129 process_enumeration_scope (die, cu);
10130 break;
10131
10132 /* These dies have a type, but processing them does not create
10133 a symbol or recurse to process the children. Therefore we can
10134 read them on-demand through read_type_die. */
10135 case DW_TAG_subroutine_type:
10136 case DW_TAG_set_type:
10137 case DW_TAG_array_type:
10138 case DW_TAG_pointer_type:
10139 case DW_TAG_ptr_to_member_type:
10140 case DW_TAG_reference_type:
10141 case DW_TAG_rvalue_reference_type:
10142 case DW_TAG_string_type:
10143 break;
10144
10145 case DW_TAG_base_type:
10146 case DW_TAG_subrange_type:
10147 case DW_TAG_typedef:
10148 /* Add a typedef symbol for the type definition, if it has a
10149 DW_AT_name. */
10150 new_symbol (die, read_type_die (die, cu), cu);
10151 break;
10152 case DW_TAG_common_block:
10153 read_common_block (die, cu);
10154 break;
10155 case DW_TAG_common_inclusion:
10156 break;
10157 case DW_TAG_namespace:
10158 cu->processing_has_namespace_info = true;
10159 read_namespace (die, cu);
10160 break;
10161 case DW_TAG_module:
10162 cu->processing_has_namespace_info = true;
10163 read_module (die, cu);
10164 break;
10165 case DW_TAG_imported_declaration:
10166 cu->processing_has_namespace_info = true;
10167 if (read_namespace_alias (die, cu))
10168 break;
10169 /* The declaration is not a global namespace alias. */
10170 /* Fall through. */
10171 case DW_TAG_imported_module:
10172 cu->processing_has_namespace_info = true;
10173 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10174 || cu->language != language_fortran))
10175 complaint (_("Tag '%s' has unexpected children"),
10176 dwarf_tag_name (die->tag));
10177 read_import_statement (die, cu);
10178 break;
10179
10180 case DW_TAG_imported_unit:
10181 process_imported_unit_die (die, cu);
10182 break;
10183
10184 case DW_TAG_variable:
10185 read_variable (die, cu);
10186 break;
10187
10188 default:
10189 new_symbol (die, NULL, cu);
10190 break;
10191 }
10192 }
10193 \f
10194 /* DWARF name computation. */
10195
10196 /* A helper function for dwarf2_compute_name which determines whether DIE
10197 needs to have the name of the scope prepended to the name listed in the
10198 die. */
10199
10200 static int
10201 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10202 {
10203 struct attribute *attr;
10204
10205 switch (die->tag)
10206 {
10207 case DW_TAG_namespace:
10208 case DW_TAG_typedef:
10209 case DW_TAG_class_type:
10210 case DW_TAG_interface_type:
10211 case DW_TAG_structure_type:
10212 case DW_TAG_union_type:
10213 case DW_TAG_enumeration_type:
10214 case DW_TAG_enumerator:
10215 case DW_TAG_subprogram:
10216 case DW_TAG_inlined_subroutine:
10217 case DW_TAG_member:
10218 case DW_TAG_imported_declaration:
10219 return 1;
10220
10221 case DW_TAG_variable:
10222 case DW_TAG_constant:
10223 /* We only need to prefix "globally" visible variables. These include
10224 any variable marked with DW_AT_external or any variable that
10225 lives in a namespace. [Variables in anonymous namespaces
10226 require prefixing, but they are not DW_AT_external.] */
10227
10228 if (dwarf2_attr (die, DW_AT_specification, cu))
10229 {
10230 struct dwarf2_cu *spec_cu = cu;
10231
10232 return die_needs_namespace (die_specification (die, &spec_cu),
10233 spec_cu);
10234 }
10235
10236 attr = dwarf2_attr (die, DW_AT_external, cu);
10237 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10238 && die->parent->tag != DW_TAG_module)
10239 return 0;
10240 /* A variable in a lexical block of some kind does not need a
10241 namespace, even though in C++ such variables may be external
10242 and have a mangled name. */
10243 if (die->parent->tag == DW_TAG_lexical_block
10244 || die->parent->tag == DW_TAG_try_block
10245 || die->parent->tag == DW_TAG_catch_block
10246 || die->parent->tag == DW_TAG_subprogram)
10247 return 0;
10248 return 1;
10249
10250 default:
10251 return 0;
10252 }
10253 }
10254
10255 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10256 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10257 defined for the given DIE. */
10258
10259 static struct attribute *
10260 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10261 {
10262 struct attribute *attr;
10263
10264 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10265 if (attr == NULL)
10266 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10267
10268 return attr;
10269 }
10270
10271 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10272 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10273 defined for the given DIE. */
10274
10275 static const char *
10276 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10277 {
10278 const char *linkage_name;
10279
10280 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10281 if (linkage_name == NULL)
10282 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10283
10284 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10285 See https://github.com/rust-lang/rust/issues/32925. */
10286 if (cu->language == language_rust && linkage_name != NULL
10287 && strchr (linkage_name, '{') != NULL)
10288 linkage_name = NULL;
10289
10290 return linkage_name;
10291 }
10292
10293 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10294 compute the physname for the object, which include a method's:
10295 - formal parameters (C++),
10296 - receiver type (Go),
10297
10298 The term "physname" is a bit confusing.
10299 For C++, for example, it is the demangled name.
10300 For Go, for example, it's the mangled name.
10301
10302 For Ada, return the DIE's linkage name rather than the fully qualified
10303 name. PHYSNAME is ignored..
10304
10305 The result is allocated on the objfile->per_bfd's obstack and
10306 canonicalized. */
10307
10308 static const char *
10309 dwarf2_compute_name (const char *name,
10310 struct die_info *die, struct dwarf2_cu *cu,
10311 int physname)
10312 {
10313 struct objfile *objfile = cu->per_objfile->objfile;
10314
10315 if (name == NULL)
10316 name = dwarf2_name (die, cu);
10317
10318 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10319 but otherwise compute it by typename_concat inside GDB.
10320 FIXME: Actually this is not really true, or at least not always true.
10321 It's all very confusing. compute_and_set_names doesn't try to demangle
10322 Fortran names because there is no mangling standard. So new_symbol
10323 will set the demangled name to the result of dwarf2_full_name, and it is
10324 the demangled name that GDB uses if it exists. */
10325 if (cu->language == language_ada
10326 || (cu->language == language_fortran && physname))
10327 {
10328 /* For Ada unit, we prefer the linkage name over the name, as
10329 the former contains the exported name, which the user expects
10330 to be able to reference. Ideally, we want the user to be able
10331 to reference this entity using either natural or linkage name,
10332 but we haven't started looking at this enhancement yet. */
10333 const char *linkage_name = dw2_linkage_name (die, cu);
10334
10335 if (linkage_name != NULL)
10336 return linkage_name;
10337 }
10338
10339 /* These are the only languages we know how to qualify names in. */
10340 if (name != NULL
10341 && (cu->language == language_cplus
10342 || cu->language == language_fortran || cu->language == language_d
10343 || cu->language == language_rust))
10344 {
10345 if (die_needs_namespace (die, cu))
10346 {
10347 const char *prefix;
10348 const char *canonical_name = NULL;
10349
10350 string_file buf;
10351
10352 prefix = determine_prefix (die, cu);
10353 if (*prefix != '\0')
10354 {
10355 gdb::unique_xmalloc_ptr<char> prefixed_name
10356 (typename_concat (NULL, prefix, name, physname, cu));
10357
10358 buf.puts (prefixed_name.get ());
10359 }
10360 else
10361 buf.puts (name);
10362
10363 /* Template parameters may be specified in the DIE's DW_AT_name, or
10364 as children with DW_TAG_template_type_param or
10365 DW_TAG_value_type_param. If the latter, add them to the name
10366 here. If the name already has template parameters, then
10367 skip this step; some versions of GCC emit both, and
10368 it is more efficient to use the pre-computed name.
10369
10370 Something to keep in mind about this process: it is very
10371 unlikely, or in some cases downright impossible, to produce
10372 something that will match the mangled name of a function.
10373 If the definition of the function has the same debug info,
10374 we should be able to match up with it anyway. But fallbacks
10375 using the minimal symbol, for instance to find a method
10376 implemented in a stripped copy of libstdc++, will not work.
10377 If we do not have debug info for the definition, we will have to
10378 match them up some other way.
10379
10380 When we do name matching there is a related problem with function
10381 templates; two instantiated function templates are allowed to
10382 differ only by their return types, which we do not add here. */
10383
10384 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10385 {
10386 struct attribute *attr;
10387 struct die_info *child;
10388 int first = 1;
10389
10390 die->building_fullname = 1;
10391
10392 for (child = die->child; child != NULL; child = child->sibling)
10393 {
10394 struct type *type;
10395 LONGEST value;
10396 const gdb_byte *bytes;
10397 struct dwarf2_locexpr_baton *baton;
10398 struct value *v;
10399
10400 if (child->tag != DW_TAG_template_type_param
10401 && child->tag != DW_TAG_template_value_param)
10402 continue;
10403
10404 if (first)
10405 {
10406 buf.puts ("<");
10407 first = 0;
10408 }
10409 else
10410 buf.puts (", ");
10411
10412 attr = dwarf2_attr (child, DW_AT_type, cu);
10413 if (attr == NULL)
10414 {
10415 complaint (_("template parameter missing DW_AT_type"));
10416 buf.puts ("UNKNOWN_TYPE");
10417 continue;
10418 }
10419 type = die_type (child, cu);
10420
10421 if (child->tag == DW_TAG_template_type_param)
10422 {
10423 c_print_type (type, "", &buf, -1, 0, cu->language,
10424 &type_print_raw_options);
10425 continue;
10426 }
10427
10428 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10429 if (attr == NULL)
10430 {
10431 complaint (_("template parameter missing "
10432 "DW_AT_const_value"));
10433 buf.puts ("UNKNOWN_VALUE");
10434 continue;
10435 }
10436
10437 dwarf2_const_value_attr (attr, type, name,
10438 &cu->comp_unit_obstack, cu,
10439 &value, &bytes, &baton);
10440
10441 if (TYPE_NOSIGN (type))
10442 /* GDB prints characters as NUMBER 'CHAR'. If that's
10443 changed, this can use value_print instead. */
10444 c_printchar (value, type, &buf);
10445 else
10446 {
10447 struct value_print_options opts;
10448
10449 if (baton != NULL)
10450 v = dwarf2_evaluate_loc_desc (type, NULL,
10451 baton->data,
10452 baton->size,
10453 baton->per_cu,
10454 baton->per_objfile);
10455 else if (bytes != NULL)
10456 {
10457 v = allocate_value (type);
10458 memcpy (value_contents_writeable (v), bytes,
10459 TYPE_LENGTH (type));
10460 }
10461 else
10462 v = value_from_longest (type, value);
10463
10464 /* Specify decimal so that we do not depend on
10465 the radix. */
10466 get_formatted_print_options (&opts, 'd');
10467 opts.raw = 1;
10468 value_print (v, &buf, &opts);
10469 release_value (v);
10470 }
10471 }
10472
10473 die->building_fullname = 0;
10474
10475 if (!first)
10476 {
10477 /* Close the argument list, with a space if necessary
10478 (nested templates). */
10479 if (!buf.empty () && buf.string ().back () == '>')
10480 buf.puts (" >");
10481 else
10482 buf.puts (">");
10483 }
10484 }
10485
10486 /* For C++ methods, append formal parameter type
10487 information, if PHYSNAME. */
10488
10489 if (physname && die->tag == DW_TAG_subprogram
10490 && cu->language == language_cplus)
10491 {
10492 struct type *type = read_type_die (die, cu);
10493
10494 c_type_print_args (type, &buf, 1, cu->language,
10495 &type_print_raw_options);
10496
10497 if (cu->language == language_cplus)
10498 {
10499 /* Assume that an artificial first parameter is
10500 "this", but do not crash if it is not. RealView
10501 marks unnamed (and thus unused) parameters as
10502 artificial; there is no way to differentiate
10503 the two cases. */
10504 if (type->num_fields () > 0
10505 && TYPE_FIELD_ARTIFICIAL (type, 0)
10506 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10507 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10508 0))))
10509 buf.puts (" const");
10510 }
10511 }
10512
10513 const std::string &intermediate_name = buf.string ();
10514
10515 if (cu->language == language_cplus)
10516 canonical_name
10517 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10518 objfile);
10519
10520 /* If we only computed INTERMEDIATE_NAME, or if
10521 INTERMEDIATE_NAME is already canonical, then we need to
10522 intern it. */
10523 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10524 name = objfile->intern (intermediate_name);
10525 else
10526 name = canonical_name;
10527 }
10528 }
10529
10530 return name;
10531 }
10532
10533 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10534 If scope qualifiers are appropriate they will be added. The result
10535 will be allocated on the storage_obstack, or NULL if the DIE does
10536 not have a name. NAME may either be from a previous call to
10537 dwarf2_name or NULL.
10538
10539 The output string will be canonicalized (if C++). */
10540
10541 static const char *
10542 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10543 {
10544 return dwarf2_compute_name (name, die, cu, 0);
10545 }
10546
10547 /* Construct a physname for the given DIE in CU. NAME may either be
10548 from a previous call to dwarf2_name or NULL. The result will be
10549 allocated on the objfile_objstack or NULL if the DIE does not have a
10550 name.
10551
10552 The output string will be canonicalized (if C++). */
10553
10554 static const char *
10555 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10556 {
10557 struct objfile *objfile = cu->per_objfile->objfile;
10558 const char *retval, *mangled = NULL, *canon = NULL;
10559 int need_copy = 1;
10560
10561 /* In this case dwarf2_compute_name is just a shortcut not building anything
10562 on its own. */
10563 if (!die_needs_namespace (die, cu))
10564 return dwarf2_compute_name (name, die, cu, 1);
10565
10566 if (cu->language != language_rust)
10567 mangled = dw2_linkage_name (die, cu);
10568
10569 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10570 has computed. */
10571 gdb::unique_xmalloc_ptr<char> demangled;
10572 if (mangled != NULL)
10573 {
10574
10575 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10576 {
10577 /* Do nothing (do not demangle the symbol name). */
10578 }
10579 else if (cu->language == language_go)
10580 {
10581 /* This is a lie, but we already lie to the caller new_symbol.
10582 new_symbol assumes we return the mangled name.
10583 This just undoes that lie until things are cleaned up. */
10584 }
10585 else
10586 {
10587 /* Use DMGL_RET_DROP for C++ template functions to suppress
10588 their return type. It is easier for GDB users to search
10589 for such functions as `name(params)' than `long name(params)'.
10590 In such case the minimal symbol names do not match the full
10591 symbol names but for template functions there is never a need
10592 to look up their definition from their declaration so
10593 the only disadvantage remains the minimal symbol variant
10594 `long name(params)' does not have the proper inferior type. */
10595 demangled.reset (gdb_demangle (mangled,
10596 (DMGL_PARAMS | DMGL_ANSI
10597 | DMGL_RET_DROP)));
10598 }
10599 if (demangled)
10600 canon = demangled.get ();
10601 else
10602 {
10603 canon = mangled;
10604 need_copy = 0;
10605 }
10606 }
10607
10608 if (canon == NULL || check_physname)
10609 {
10610 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10611
10612 if (canon != NULL && strcmp (physname, canon) != 0)
10613 {
10614 /* It may not mean a bug in GDB. The compiler could also
10615 compute DW_AT_linkage_name incorrectly. But in such case
10616 GDB would need to be bug-to-bug compatible. */
10617
10618 complaint (_("Computed physname <%s> does not match demangled <%s> "
10619 "(from linkage <%s>) - DIE at %s [in module %s]"),
10620 physname, canon, mangled, sect_offset_str (die->sect_off),
10621 objfile_name (objfile));
10622
10623 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10624 is available here - over computed PHYSNAME. It is safer
10625 against both buggy GDB and buggy compilers. */
10626
10627 retval = canon;
10628 }
10629 else
10630 {
10631 retval = physname;
10632 need_copy = 0;
10633 }
10634 }
10635 else
10636 retval = canon;
10637
10638 if (need_copy)
10639 retval = objfile->intern (retval);
10640
10641 return retval;
10642 }
10643
10644 /* Inspect DIE in CU for a namespace alias. If one exists, record
10645 a new symbol for it.
10646
10647 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10648
10649 static int
10650 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10651 {
10652 struct attribute *attr;
10653
10654 /* If the die does not have a name, this is not a namespace
10655 alias. */
10656 attr = dwarf2_attr (die, DW_AT_name, cu);
10657 if (attr != NULL)
10658 {
10659 int num;
10660 struct die_info *d = die;
10661 struct dwarf2_cu *imported_cu = cu;
10662
10663 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10664 keep inspecting DIEs until we hit the underlying import. */
10665 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10666 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10667 {
10668 attr = dwarf2_attr (d, DW_AT_import, cu);
10669 if (attr == NULL)
10670 break;
10671
10672 d = follow_die_ref (d, attr, &imported_cu);
10673 if (d->tag != DW_TAG_imported_declaration)
10674 break;
10675 }
10676
10677 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10678 {
10679 complaint (_("DIE at %s has too many recursively imported "
10680 "declarations"), sect_offset_str (d->sect_off));
10681 return 0;
10682 }
10683
10684 if (attr != NULL)
10685 {
10686 struct type *type;
10687 sect_offset sect_off = attr->get_ref_die_offset ();
10688
10689 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10690 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10691 {
10692 /* This declaration is a global namespace alias. Add
10693 a symbol for it whose type is the aliased namespace. */
10694 new_symbol (die, type, cu);
10695 return 1;
10696 }
10697 }
10698 }
10699
10700 return 0;
10701 }
10702
10703 /* Return the using directives repository (global or local?) to use in the
10704 current context for CU.
10705
10706 For Ada, imported declarations can materialize renamings, which *may* be
10707 global. However it is impossible (for now?) in DWARF to distinguish
10708 "external" imported declarations and "static" ones. As all imported
10709 declarations seem to be static in all other languages, make them all CU-wide
10710 global only in Ada. */
10711
10712 static struct using_direct **
10713 using_directives (struct dwarf2_cu *cu)
10714 {
10715 if (cu->language == language_ada
10716 && cu->get_builder ()->outermost_context_p ())
10717 return cu->get_builder ()->get_global_using_directives ();
10718 else
10719 return cu->get_builder ()->get_local_using_directives ();
10720 }
10721
10722 /* Read the import statement specified by the given die and record it. */
10723
10724 static void
10725 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10726 {
10727 struct objfile *objfile = cu->per_objfile->objfile;
10728 struct attribute *import_attr;
10729 struct die_info *imported_die, *child_die;
10730 struct dwarf2_cu *imported_cu;
10731 const char *imported_name;
10732 const char *imported_name_prefix;
10733 const char *canonical_name;
10734 const char *import_alias;
10735 const char *imported_declaration = NULL;
10736 const char *import_prefix;
10737 std::vector<const char *> excludes;
10738
10739 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10740 if (import_attr == NULL)
10741 {
10742 complaint (_("Tag '%s' has no DW_AT_import"),
10743 dwarf_tag_name (die->tag));
10744 return;
10745 }
10746
10747 imported_cu = cu;
10748 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10749 imported_name = dwarf2_name (imported_die, imported_cu);
10750 if (imported_name == NULL)
10751 {
10752 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10753
10754 The import in the following code:
10755 namespace A
10756 {
10757 typedef int B;
10758 }
10759
10760 int main ()
10761 {
10762 using A::B;
10763 B b;
10764 return b;
10765 }
10766
10767 ...
10768 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10769 <52> DW_AT_decl_file : 1
10770 <53> DW_AT_decl_line : 6
10771 <54> DW_AT_import : <0x75>
10772 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10773 <59> DW_AT_name : B
10774 <5b> DW_AT_decl_file : 1
10775 <5c> DW_AT_decl_line : 2
10776 <5d> DW_AT_type : <0x6e>
10777 ...
10778 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10779 <76> DW_AT_byte_size : 4
10780 <77> DW_AT_encoding : 5 (signed)
10781
10782 imports the wrong die ( 0x75 instead of 0x58 ).
10783 This case will be ignored until the gcc bug is fixed. */
10784 return;
10785 }
10786
10787 /* Figure out the local name after import. */
10788 import_alias = dwarf2_name (die, cu);
10789
10790 /* Figure out where the statement is being imported to. */
10791 import_prefix = determine_prefix (die, cu);
10792
10793 /* Figure out what the scope of the imported die is and prepend it
10794 to the name of the imported die. */
10795 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10796
10797 if (imported_die->tag != DW_TAG_namespace
10798 && imported_die->tag != DW_TAG_module)
10799 {
10800 imported_declaration = imported_name;
10801 canonical_name = imported_name_prefix;
10802 }
10803 else if (strlen (imported_name_prefix) > 0)
10804 canonical_name = obconcat (&objfile->objfile_obstack,
10805 imported_name_prefix,
10806 (cu->language == language_d ? "." : "::"),
10807 imported_name, (char *) NULL);
10808 else
10809 canonical_name = imported_name;
10810
10811 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10812 for (child_die = die->child; child_die && child_die->tag;
10813 child_die = child_die->sibling)
10814 {
10815 /* DWARF-4: A Fortran use statement with a “rename list” may be
10816 represented by an imported module entry with an import attribute
10817 referring to the module and owned entries corresponding to those
10818 entities that are renamed as part of being imported. */
10819
10820 if (child_die->tag != DW_TAG_imported_declaration)
10821 {
10822 complaint (_("child DW_TAG_imported_declaration expected "
10823 "- DIE at %s [in module %s]"),
10824 sect_offset_str (child_die->sect_off),
10825 objfile_name (objfile));
10826 continue;
10827 }
10828
10829 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10830 if (import_attr == NULL)
10831 {
10832 complaint (_("Tag '%s' has no DW_AT_import"),
10833 dwarf_tag_name (child_die->tag));
10834 continue;
10835 }
10836
10837 imported_cu = cu;
10838 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10839 &imported_cu);
10840 imported_name = dwarf2_name (imported_die, imported_cu);
10841 if (imported_name == NULL)
10842 {
10843 complaint (_("child DW_TAG_imported_declaration has unknown "
10844 "imported name - DIE at %s [in module %s]"),
10845 sect_offset_str (child_die->sect_off),
10846 objfile_name (objfile));
10847 continue;
10848 }
10849
10850 excludes.push_back (imported_name);
10851
10852 process_die (child_die, cu);
10853 }
10854
10855 add_using_directive (using_directives (cu),
10856 import_prefix,
10857 canonical_name,
10858 import_alias,
10859 imported_declaration,
10860 excludes,
10861 0,
10862 &objfile->objfile_obstack);
10863 }
10864
10865 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10866 types, but gives them a size of zero. Starting with version 14,
10867 ICC is compatible with GCC. */
10868
10869 static bool
10870 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10871 {
10872 if (!cu->checked_producer)
10873 check_producer (cu);
10874
10875 return cu->producer_is_icc_lt_14;
10876 }
10877
10878 /* ICC generates a DW_AT_type for C void functions. This was observed on
10879 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10880 which says that void functions should not have a DW_AT_type. */
10881
10882 static bool
10883 producer_is_icc (struct dwarf2_cu *cu)
10884 {
10885 if (!cu->checked_producer)
10886 check_producer (cu);
10887
10888 return cu->producer_is_icc;
10889 }
10890
10891 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10892 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10893 this, it was first present in GCC release 4.3.0. */
10894
10895 static bool
10896 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10897 {
10898 if (!cu->checked_producer)
10899 check_producer (cu);
10900
10901 return cu->producer_is_gcc_lt_4_3;
10902 }
10903
10904 static file_and_directory
10905 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10906 {
10907 file_and_directory res;
10908
10909 /* Find the filename. Do not use dwarf2_name here, since the filename
10910 is not a source language identifier. */
10911 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10912 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10913
10914 if (res.comp_dir == NULL
10915 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10916 && IS_ABSOLUTE_PATH (res.name))
10917 {
10918 res.comp_dir_storage = ldirname (res.name);
10919 if (!res.comp_dir_storage.empty ())
10920 res.comp_dir = res.comp_dir_storage.c_str ();
10921 }
10922 if (res.comp_dir != NULL)
10923 {
10924 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10925 directory, get rid of it. */
10926 const char *cp = strchr (res.comp_dir, ':');
10927
10928 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10929 res.comp_dir = cp + 1;
10930 }
10931
10932 if (res.name == NULL)
10933 res.name = "<unknown>";
10934
10935 return res;
10936 }
10937
10938 /* Handle DW_AT_stmt_list for a compilation unit.
10939 DIE is the DW_TAG_compile_unit die for CU.
10940 COMP_DIR is the compilation directory. LOWPC is passed to
10941 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10942
10943 static void
10944 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10945 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10946 {
10947 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10948 struct attribute *attr;
10949 struct line_header line_header_local;
10950 hashval_t line_header_local_hash;
10951 void **slot;
10952 int decode_mapping;
10953
10954 gdb_assert (! cu->per_cu->is_debug_types);
10955
10956 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10957 if (attr == NULL)
10958 return;
10959
10960 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10961
10962 /* The line header hash table is only created if needed (it exists to
10963 prevent redundant reading of the line table for partial_units).
10964 If we're given a partial_unit, we'll need it. If we're given a
10965 compile_unit, then use the line header hash table if it's already
10966 created, but don't create one just yet. */
10967
10968 if (dwarf2_per_objfile->line_header_hash == NULL
10969 && die->tag == DW_TAG_partial_unit)
10970 {
10971 dwarf2_per_objfile->line_header_hash
10972 .reset (htab_create_alloc (127, line_header_hash_voidp,
10973 line_header_eq_voidp,
10974 free_line_header_voidp,
10975 xcalloc, xfree));
10976 }
10977
10978 line_header_local.sect_off = line_offset;
10979 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10980 line_header_local_hash = line_header_hash (&line_header_local);
10981 if (dwarf2_per_objfile->line_header_hash != NULL)
10982 {
10983 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10984 &line_header_local,
10985 line_header_local_hash, NO_INSERT);
10986
10987 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10988 is not present in *SLOT (since if there is something in *SLOT then
10989 it will be for a partial_unit). */
10990 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10991 {
10992 gdb_assert (*slot != NULL);
10993 cu->line_header = (struct line_header *) *slot;
10994 return;
10995 }
10996 }
10997
10998 /* dwarf_decode_line_header does not yet provide sufficient information.
10999 We always have to call also dwarf_decode_lines for it. */
11000 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11001 if (lh == NULL)
11002 return;
11003
11004 cu->line_header = lh.release ();
11005 cu->line_header_die_owner = die;
11006
11007 if (dwarf2_per_objfile->line_header_hash == NULL)
11008 slot = NULL;
11009 else
11010 {
11011 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
11012 &line_header_local,
11013 line_header_local_hash, INSERT);
11014 gdb_assert (slot != NULL);
11015 }
11016 if (slot != NULL && *slot == NULL)
11017 {
11018 /* This newly decoded line number information unit will be owned
11019 by line_header_hash hash table. */
11020 *slot = cu->line_header;
11021 cu->line_header_die_owner = NULL;
11022 }
11023 else
11024 {
11025 /* We cannot free any current entry in (*slot) as that struct line_header
11026 may be already used by multiple CUs. Create only temporary decoded
11027 line_header for this CU - it may happen at most once for each line
11028 number information unit. And if we're not using line_header_hash
11029 then this is what we want as well. */
11030 gdb_assert (die->tag != DW_TAG_partial_unit);
11031 }
11032 decode_mapping = (die->tag != DW_TAG_partial_unit);
11033 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11034 decode_mapping);
11035
11036 }
11037
11038 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11039
11040 static void
11041 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11042 {
11043 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
11044 struct objfile *objfile = dwarf2_per_objfile->objfile;
11045 struct gdbarch *gdbarch = objfile->arch ();
11046 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11047 CORE_ADDR highpc = ((CORE_ADDR) 0);
11048 struct attribute *attr;
11049 struct die_info *child_die;
11050 CORE_ADDR baseaddr;
11051
11052 prepare_one_comp_unit (cu, die, cu->language);
11053 baseaddr = objfile->text_section_offset ();
11054
11055 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11056
11057 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11058 from finish_block. */
11059 if (lowpc == ((CORE_ADDR) -1))
11060 lowpc = highpc;
11061 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11062
11063 file_and_directory fnd = find_file_and_directory (die, cu);
11064
11065 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11066 standardised yet. As a workaround for the language detection we fall
11067 back to the DW_AT_producer string. */
11068 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11069 cu->language = language_opencl;
11070
11071 /* Similar hack for Go. */
11072 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11073 set_cu_language (DW_LANG_Go, cu);
11074
11075 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11076
11077 /* Decode line number information if present. We do this before
11078 processing child DIEs, so that the line header table is available
11079 for DW_AT_decl_file. */
11080 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11081
11082 /* Process all dies in compilation unit. */
11083 if (die->child != NULL)
11084 {
11085 child_die = die->child;
11086 while (child_die && child_die->tag)
11087 {
11088 process_die (child_die, cu);
11089 child_die = child_die->sibling;
11090 }
11091 }
11092
11093 /* Decode macro information, if present. Dwarf 2 macro information
11094 refers to information in the line number info statement program
11095 header, so we can only read it if we've read the header
11096 successfully. */
11097 attr = dwarf2_attr (die, DW_AT_macros, cu);
11098 if (attr == NULL)
11099 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11100 if (attr && cu->line_header)
11101 {
11102 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11103 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11104
11105 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11106 }
11107 else
11108 {
11109 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11110 if (attr && cu->line_header)
11111 {
11112 unsigned int macro_offset = DW_UNSND (attr);
11113
11114 dwarf_decode_macros (cu, macro_offset, 0);
11115 }
11116 }
11117 }
11118
11119 void
11120 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11121 {
11122 struct type_unit_group *tu_group;
11123 int first_time;
11124 struct attribute *attr;
11125 unsigned int i;
11126 struct signatured_type *sig_type;
11127
11128 gdb_assert (per_cu->is_debug_types);
11129 sig_type = (struct signatured_type *) per_cu;
11130
11131 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11132
11133 /* If we're using .gdb_index (includes -readnow) then
11134 per_cu->type_unit_group may not have been set up yet. */
11135 if (sig_type->type_unit_group == NULL)
11136 sig_type->type_unit_group = get_type_unit_group (this, attr);
11137 tu_group = sig_type->type_unit_group;
11138
11139 /* If we've already processed this stmt_list there's no real need to
11140 do it again, we could fake it and just recreate the part we need
11141 (file name,index -> symtab mapping). If data shows this optimization
11142 is useful we can do it then. */
11143 type_unit_group_unshareable *tug_unshare
11144 = per_objfile->get_type_unit_group_unshareable (tu_group);
11145 first_time = tug_unshare->compunit_symtab == NULL;
11146
11147 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11148 debug info. */
11149 line_header_up lh;
11150 if (attr != NULL)
11151 {
11152 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11153 lh = dwarf_decode_line_header (line_offset, this);
11154 }
11155 if (lh == NULL)
11156 {
11157 if (first_time)
11158 start_symtab ("", NULL, 0);
11159 else
11160 {
11161 gdb_assert (tug_unshare->symtabs == NULL);
11162 gdb_assert (m_builder == nullptr);
11163 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11164 m_builder.reset (new struct buildsym_compunit
11165 (COMPUNIT_OBJFILE (cust), "",
11166 COMPUNIT_DIRNAME (cust),
11167 compunit_language (cust),
11168 0, cust));
11169 list_in_scope = get_builder ()->get_file_symbols ();
11170 }
11171 return;
11172 }
11173
11174 line_header = lh.release ();
11175 line_header_die_owner = die;
11176
11177 if (first_time)
11178 {
11179 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11180
11181 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11182 still initializing it, and our caller (a few levels up)
11183 process_full_type_unit still needs to know if this is the first
11184 time. */
11185
11186 tug_unshare->symtabs
11187 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11188 struct symtab *, line_header->file_names_size ());
11189
11190 auto &file_names = line_header->file_names ();
11191 for (i = 0; i < file_names.size (); ++i)
11192 {
11193 file_entry &fe = file_names[i];
11194 dwarf2_start_subfile (this, fe.name,
11195 fe.include_dir (line_header));
11196 buildsym_compunit *b = get_builder ();
11197 if (b->get_current_subfile ()->symtab == NULL)
11198 {
11199 /* NOTE: start_subfile will recognize when it's been
11200 passed a file it has already seen. So we can't
11201 assume there's a simple mapping from
11202 cu->line_header->file_names to subfiles, plus
11203 cu->line_header->file_names may contain dups. */
11204 b->get_current_subfile ()->symtab
11205 = allocate_symtab (cust, b->get_current_subfile ()->name);
11206 }
11207
11208 fe.symtab = b->get_current_subfile ()->symtab;
11209 tug_unshare->symtabs[i] = fe.symtab;
11210 }
11211 }
11212 else
11213 {
11214 gdb_assert (m_builder == nullptr);
11215 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11216 m_builder.reset (new struct buildsym_compunit
11217 (COMPUNIT_OBJFILE (cust), "",
11218 COMPUNIT_DIRNAME (cust),
11219 compunit_language (cust),
11220 0, cust));
11221 list_in_scope = get_builder ()->get_file_symbols ();
11222
11223 auto &file_names = line_header->file_names ();
11224 for (i = 0; i < file_names.size (); ++i)
11225 {
11226 file_entry &fe = file_names[i];
11227 fe.symtab = tug_unshare->symtabs[i];
11228 }
11229 }
11230
11231 /* The main symtab is allocated last. Type units don't have DW_AT_name
11232 so they don't have a "real" (so to speak) symtab anyway.
11233 There is later code that will assign the main symtab to all symbols
11234 that don't have one. We need to handle the case of a symbol with a
11235 missing symtab (DW_AT_decl_file) anyway. */
11236 }
11237
11238 /* Process DW_TAG_type_unit.
11239 For TUs we want to skip the first top level sibling if it's not the
11240 actual type being defined by this TU. In this case the first top
11241 level sibling is there to provide context only. */
11242
11243 static void
11244 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11245 {
11246 struct die_info *child_die;
11247
11248 prepare_one_comp_unit (cu, die, language_minimal);
11249
11250 /* Initialize (or reinitialize) the machinery for building symtabs.
11251 We do this before processing child DIEs, so that the line header table
11252 is available for DW_AT_decl_file. */
11253 cu->setup_type_unit_groups (die);
11254
11255 if (die->child != NULL)
11256 {
11257 child_die = die->child;
11258 while (child_die && child_die->tag)
11259 {
11260 process_die (child_die, cu);
11261 child_die = child_die->sibling;
11262 }
11263 }
11264 }
11265 \f
11266 /* DWO/DWP files.
11267
11268 http://gcc.gnu.org/wiki/DebugFission
11269 http://gcc.gnu.org/wiki/DebugFissionDWP
11270
11271 To simplify handling of both DWO files ("object" files with the DWARF info)
11272 and DWP files (a file with the DWOs packaged up into one file), we treat
11273 DWP files as having a collection of virtual DWO files. */
11274
11275 static hashval_t
11276 hash_dwo_file (const void *item)
11277 {
11278 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11279 hashval_t hash;
11280
11281 hash = htab_hash_string (dwo_file->dwo_name);
11282 if (dwo_file->comp_dir != NULL)
11283 hash += htab_hash_string (dwo_file->comp_dir);
11284 return hash;
11285 }
11286
11287 static int
11288 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11289 {
11290 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11291 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11292
11293 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11294 return 0;
11295 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11296 return lhs->comp_dir == rhs->comp_dir;
11297 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11298 }
11299
11300 /* Allocate a hash table for DWO files. */
11301
11302 static htab_up
11303 allocate_dwo_file_hash_table ()
11304 {
11305 auto delete_dwo_file = [] (void *item)
11306 {
11307 struct dwo_file *dwo_file = (struct dwo_file *) item;
11308
11309 delete dwo_file;
11310 };
11311
11312 return htab_up (htab_create_alloc (41,
11313 hash_dwo_file,
11314 eq_dwo_file,
11315 delete_dwo_file,
11316 xcalloc, xfree));
11317 }
11318
11319 /* Lookup DWO file DWO_NAME. */
11320
11321 static void **
11322 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11323 const char *dwo_name,
11324 const char *comp_dir)
11325 {
11326 struct dwo_file find_entry;
11327 void **slot;
11328
11329 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11330 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11331
11332 find_entry.dwo_name = dwo_name;
11333 find_entry.comp_dir = comp_dir;
11334 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11335 INSERT);
11336
11337 return slot;
11338 }
11339
11340 static hashval_t
11341 hash_dwo_unit (const void *item)
11342 {
11343 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11344
11345 /* This drops the top 32 bits of the id, but is ok for a hash. */
11346 return dwo_unit->signature;
11347 }
11348
11349 static int
11350 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11351 {
11352 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11353 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11354
11355 /* The signature is assumed to be unique within the DWO file.
11356 So while object file CU dwo_id's always have the value zero,
11357 that's OK, assuming each object file DWO file has only one CU,
11358 and that's the rule for now. */
11359 return lhs->signature == rhs->signature;
11360 }
11361
11362 /* Allocate a hash table for DWO CUs,TUs.
11363 There is one of these tables for each of CUs,TUs for each DWO file. */
11364
11365 static htab_up
11366 allocate_dwo_unit_table ()
11367 {
11368 /* Start out with a pretty small number.
11369 Generally DWO files contain only one CU and maybe some TUs. */
11370 return htab_up (htab_create_alloc (3,
11371 hash_dwo_unit,
11372 eq_dwo_unit,
11373 NULL, xcalloc, xfree));
11374 }
11375
11376 /* die_reader_func for create_dwo_cu. */
11377
11378 static void
11379 create_dwo_cu_reader (const struct die_reader_specs *reader,
11380 const gdb_byte *info_ptr,
11381 struct die_info *comp_unit_die,
11382 struct dwo_file *dwo_file,
11383 struct dwo_unit *dwo_unit)
11384 {
11385 struct dwarf2_cu *cu = reader->cu;
11386 sect_offset sect_off = cu->per_cu->sect_off;
11387 struct dwarf2_section_info *section = cu->per_cu->section;
11388
11389 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11390 if (!signature.has_value ())
11391 {
11392 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11393 " its dwo_id [in module %s]"),
11394 sect_offset_str (sect_off), dwo_file->dwo_name);
11395 return;
11396 }
11397
11398 dwo_unit->dwo_file = dwo_file;
11399 dwo_unit->signature = *signature;
11400 dwo_unit->section = section;
11401 dwo_unit->sect_off = sect_off;
11402 dwo_unit->length = cu->per_cu->length;
11403
11404 if (dwarf_read_debug)
11405 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11406 sect_offset_str (sect_off),
11407 hex_string (dwo_unit->signature));
11408 }
11409
11410 /* Create the dwo_units for the CUs in a DWO_FILE.
11411 Note: This function processes DWO files only, not DWP files. */
11412
11413 static void
11414 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11415 dwarf2_cu *cu, struct dwo_file &dwo_file,
11416 dwarf2_section_info &section, htab_up &cus_htab)
11417 {
11418 struct objfile *objfile = dwarf2_per_objfile->objfile;
11419 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11420 const gdb_byte *info_ptr, *end_ptr;
11421
11422 section.read (objfile);
11423 info_ptr = section.buffer;
11424
11425 if (info_ptr == NULL)
11426 return;
11427
11428 if (dwarf_read_debug)
11429 {
11430 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11431 section.get_name (),
11432 section.get_file_name ());
11433 }
11434
11435 end_ptr = info_ptr + section.size;
11436 while (info_ptr < end_ptr)
11437 {
11438 struct dwarf2_per_cu_data per_cu;
11439 struct dwo_unit read_unit {};
11440 struct dwo_unit *dwo_unit;
11441 void **slot;
11442 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11443
11444 memset (&per_cu, 0, sizeof (per_cu));
11445 per_cu.per_bfd = per_bfd;
11446 per_cu.is_debug_types = 0;
11447 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11448 per_cu.section = &section;
11449
11450 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11451 if (!reader.dummy_p)
11452 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11453 &dwo_file, &read_unit);
11454 info_ptr += per_cu.length;
11455
11456 // If the unit could not be parsed, skip it.
11457 if (read_unit.dwo_file == NULL)
11458 continue;
11459
11460 if (cus_htab == NULL)
11461 cus_htab = allocate_dwo_unit_table ();
11462
11463 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11464 struct dwo_unit);
11465 *dwo_unit = read_unit;
11466 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11467 gdb_assert (slot != NULL);
11468 if (*slot != NULL)
11469 {
11470 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11471 sect_offset dup_sect_off = dup_cu->sect_off;
11472
11473 complaint (_("debug cu entry at offset %s is duplicate to"
11474 " the entry at offset %s, signature %s"),
11475 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11476 hex_string (dwo_unit->signature));
11477 }
11478 *slot = (void *)dwo_unit;
11479 }
11480 }
11481
11482 /* DWP file .debug_{cu,tu}_index section format:
11483 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11484
11485 DWP Version 1:
11486
11487 Both index sections have the same format, and serve to map a 64-bit
11488 signature to a set of section numbers. Each section begins with a header,
11489 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11490 indexes, and a pool of 32-bit section numbers. The index sections will be
11491 aligned at 8-byte boundaries in the file.
11492
11493 The index section header consists of:
11494
11495 V, 32 bit version number
11496 -, 32 bits unused
11497 N, 32 bit number of compilation units or type units in the index
11498 M, 32 bit number of slots in the hash table
11499
11500 Numbers are recorded using the byte order of the application binary.
11501
11502 The hash table begins at offset 16 in the section, and consists of an array
11503 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11504 order of the application binary). Unused slots in the hash table are 0.
11505 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11506
11507 The parallel table begins immediately after the hash table
11508 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11509 array of 32-bit indexes (using the byte order of the application binary),
11510 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11511 table contains a 32-bit index into the pool of section numbers. For unused
11512 hash table slots, the corresponding entry in the parallel table will be 0.
11513
11514 The pool of section numbers begins immediately following the hash table
11515 (at offset 16 + 12 * M from the beginning of the section). The pool of
11516 section numbers consists of an array of 32-bit words (using the byte order
11517 of the application binary). Each item in the array is indexed starting
11518 from 0. The hash table entry provides the index of the first section
11519 number in the set. Additional section numbers in the set follow, and the
11520 set is terminated by a 0 entry (section number 0 is not used in ELF).
11521
11522 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11523 section must be the first entry in the set, and the .debug_abbrev.dwo must
11524 be the second entry. Other members of the set may follow in any order.
11525
11526 ---
11527
11528 DWP Version 2:
11529
11530 DWP Version 2 combines all the .debug_info, etc. sections into one,
11531 and the entries in the index tables are now offsets into these sections.
11532 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11533 section.
11534
11535 Index Section Contents:
11536 Header
11537 Hash Table of Signatures dwp_hash_table.hash_table
11538 Parallel Table of Indices dwp_hash_table.unit_table
11539 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11540 Table of Section Sizes dwp_hash_table.v2.sizes
11541
11542 The index section header consists of:
11543
11544 V, 32 bit version number
11545 L, 32 bit number of columns in the table of section offsets
11546 N, 32 bit number of compilation units or type units in the index
11547 M, 32 bit number of slots in the hash table
11548
11549 Numbers are recorded using the byte order of the application binary.
11550
11551 The hash table has the same format as version 1.
11552 The parallel table of indices has the same format as version 1,
11553 except that the entries are origin-1 indices into the table of sections
11554 offsets and the table of section sizes.
11555
11556 The table of offsets begins immediately following the parallel table
11557 (at offset 16 + 12 * M from the beginning of the section). The table is
11558 a two-dimensional array of 32-bit words (using the byte order of the
11559 application binary), with L columns and N+1 rows, in row-major order.
11560 Each row in the array is indexed starting from 0. The first row provides
11561 a key to the remaining rows: each column in this row provides an identifier
11562 for a debug section, and the offsets in the same column of subsequent rows
11563 refer to that section. The section identifiers are:
11564
11565 DW_SECT_INFO 1 .debug_info.dwo
11566 DW_SECT_TYPES 2 .debug_types.dwo
11567 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11568 DW_SECT_LINE 4 .debug_line.dwo
11569 DW_SECT_LOC 5 .debug_loc.dwo
11570 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11571 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11572 DW_SECT_MACRO 8 .debug_macro.dwo
11573
11574 The offsets provided by the CU and TU index sections are the base offsets
11575 for the contributions made by each CU or TU to the corresponding section
11576 in the package file. Each CU and TU header contains an abbrev_offset
11577 field, used to find the abbreviations table for that CU or TU within the
11578 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11579 be interpreted as relative to the base offset given in the index section.
11580 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11581 should be interpreted as relative to the base offset for .debug_line.dwo,
11582 and offsets into other debug sections obtained from DWARF attributes should
11583 also be interpreted as relative to the corresponding base offset.
11584
11585 The table of sizes begins immediately following the table of offsets.
11586 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11587 with L columns and N rows, in row-major order. Each row in the array is
11588 indexed starting from 1 (row 0 is shared by the two tables).
11589
11590 ---
11591
11592 Hash table lookup is handled the same in version 1 and 2:
11593
11594 We assume that N and M will not exceed 2^32 - 1.
11595 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11596
11597 Given a 64-bit compilation unit signature or a type signature S, an entry
11598 in the hash table is located as follows:
11599
11600 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11601 the low-order k bits all set to 1.
11602
11603 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11604
11605 3) If the hash table entry at index H matches the signature, use that
11606 entry. If the hash table entry at index H is unused (all zeroes),
11607 terminate the search: the signature is not present in the table.
11608
11609 4) Let H = (H + H') modulo M. Repeat at Step 3.
11610
11611 Because M > N and H' and M are relatively prime, the search is guaranteed
11612 to stop at an unused slot or find the match. */
11613
11614 /* Create a hash table to map DWO IDs to their CU/TU entry in
11615 .debug_{info,types}.dwo in DWP_FILE.
11616 Returns NULL if there isn't one.
11617 Note: This function processes DWP files only, not DWO files. */
11618
11619 static struct dwp_hash_table *
11620 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11621 struct dwp_file *dwp_file, int is_debug_types)
11622 {
11623 struct objfile *objfile = dwarf2_per_objfile->objfile;
11624 bfd *dbfd = dwp_file->dbfd.get ();
11625 const gdb_byte *index_ptr, *index_end;
11626 struct dwarf2_section_info *index;
11627 uint32_t version, nr_columns, nr_units, nr_slots;
11628 struct dwp_hash_table *htab;
11629
11630 if (is_debug_types)
11631 index = &dwp_file->sections.tu_index;
11632 else
11633 index = &dwp_file->sections.cu_index;
11634
11635 if (index->empty ())
11636 return NULL;
11637 index->read (objfile);
11638
11639 index_ptr = index->buffer;
11640 index_end = index_ptr + index->size;
11641
11642 version = read_4_bytes (dbfd, index_ptr);
11643 index_ptr += 4;
11644 if (version == 2)
11645 nr_columns = read_4_bytes (dbfd, index_ptr);
11646 else
11647 nr_columns = 0;
11648 index_ptr += 4;
11649 nr_units = read_4_bytes (dbfd, index_ptr);
11650 index_ptr += 4;
11651 nr_slots = read_4_bytes (dbfd, index_ptr);
11652 index_ptr += 4;
11653
11654 if (version != 1 && version != 2)
11655 {
11656 error (_("Dwarf Error: unsupported DWP file version (%s)"
11657 " [in module %s]"),
11658 pulongest (version), dwp_file->name);
11659 }
11660 if (nr_slots != (nr_slots & -nr_slots))
11661 {
11662 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11663 " is not power of 2 [in module %s]"),
11664 pulongest (nr_slots), dwp_file->name);
11665 }
11666
11667 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11668 htab->version = version;
11669 htab->nr_columns = nr_columns;
11670 htab->nr_units = nr_units;
11671 htab->nr_slots = nr_slots;
11672 htab->hash_table = index_ptr;
11673 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11674
11675 /* Exit early if the table is empty. */
11676 if (nr_slots == 0 || nr_units == 0
11677 || (version == 2 && nr_columns == 0))
11678 {
11679 /* All must be zero. */
11680 if (nr_slots != 0 || nr_units != 0
11681 || (version == 2 && nr_columns != 0))
11682 {
11683 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11684 " all zero [in modules %s]"),
11685 dwp_file->name);
11686 }
11687 return htab;
11688 }
11689
11690 if (version == 1)
11691 {
11692 htab->section_pool.v1.indices =
11693 htab->unit_table + sizeof (uint32_t) * nr_slots;
11694 /* It's harder to decide whether the section is too small in v1.
11695 V1 is deprecated anyway so we punt. */
11696 }
11697 else
11698 {
11699 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11700 int *ids = htab->section_pool.v2.section_ids;
11701 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11702 /* Reverse map for error checking. */
11703 int ids_seen[DW_SECT_MAX + 1];
11704 int i;
11705
11706 if (nr_columns < 2)
11707 {
11708 error (_("Dwarf Error: bad DWP hash table, too few columns"
11709 " in section table [in module %s]"),
11710 dwp_file->name);
11711 }
11712 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11713 {
11714 error (_("Dwarf Error: bad DWP hash table, too many columns"
11715 " in section table [in module %s]"),
11716 dwp_file->name);
11717 }
11718 memset (ids, 255, sizeof_ids);
11719 memset (ids_seen, 255, sizeof (ids_seen));
11720 for (i = 0; i < nr_columns; ++i)
11721 {
11722 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11723
11724 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11725 {
11726 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11727 " in section table [in module %s]"),
11728 id, dwp_file->name);
11729 }
11730 if (ids_seen[id] != -1)
11731 {
11732 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11733 " id %d in section table [in module %s]"),
11734 id, dwp_file->name);
11735 }
11736 ids_seen[id] = i;
11737 ids[i] = id;
11738 }
11739 /* Must have exactly one info or types section. */
11740 if (((ids_seen[DW_SECT_INFO] != -1)
11741 + (ids_seen[DW_SECT_TYPES] != -1))
11742 != 1)
11743 {
11744 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11745 " DWO info/types section [in module %s]"),
11746 dwp_file->name);
11747 }
11748 /* Must have an abbrev section. */
11749 if (ids_seen[DW_SECT_ABBREV] == -1)
11750 {
11751 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11752 " section [in module %s]"),
11753 dwp_file->name);
11754 }
11755 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11756 htab->section_pool.v2.sizes =
11757 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11758 * nr_units * nr_columns);
11759 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11760 * nr_units * nr_columns))
11761 > index_end)
11762 {
11763 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11764 " [in module %s]"),
11765 dwp_file->name);
11766 }
11767 }
11768
11769 return htab;
11770 }
11771
11772 /* Update SECTIONS with the data from SECTP.
11773
11774 This function is like the other "locate" section routines that are
11775 passed to bfd_map_over_sections, but in this context the sections to
11776 read comes from the DWP V1 hash table, not the full ELF section table.
11777
11778 The result is non-zero for success, or zero if an error was found. */
11779
11780 static int
11781 locate_v1_virtual_dwo_sections (asection *sectp,
11782 struct virtual_v1_dwo_sections *sections)
11783 {
11784 const struct dwop_section_names *names = &dwop_section_names;
11785
11786 if (section_is_p (sectp->name, &names->abbrev_dwo))
11787 {
11788 /* There can be only one. */
11789 if (sections->abbrev.s.section != NULL)
11790 return 0;
11791 sections->abbrev.s.section = sectp;
11792 sections->abbrev.size = bfd_section_size (sectp);
11793 }
11794 else if (section_is_p (sectp->name, &names->info_dwo)
11795 || section_is_p (sectp->name, &names->types_dwo))
11796 {
11797 /* There can be only one. */
11798 if (sections->info_or_types.s.section != NULL)
11799 return 0;
11800 sections->info_or_types.s.section = sectp;
11801 sections->info_or_types.size = bfd_section_size (sectp);
11802 }
11803 else if (section_is_p (sectp->name, &names->line_dwo))
11804 {
11805 /* There can be only one. */
11806 if (sections->line.s.section != NULL)
11807 return 0;
11808 sections->line.s.section = sectp;
11809 sections->line.size = bfd_section_size (sectp);
11810 }
11811 else if (section_is_p (sectp->name, &names->loc_dwo))
11812 {
11813 /* There can be only one. */
11814 if (sections->loc.s.section != NULL)
11815 return 0;
11816 sections->loc.s.section = sectp;
11817 sections->loc.size = bfd_section_size (sectp);
11818 }
11819 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11820 {
11821 /* There can be only one. */
11822 if (sections->macinfo.s.section != NULL)
11823 return 0;
11824 sections->macinfo.s.section = sectp;
11825 sections->macinfo.size = bfd_section_size (sectp);
11826 }
11827 else if (section_is_p (sectp->name, &names->macro_dwo))
11828 {
11829 /* There can be only one. */
11830 if (sections->macro.s.section != NULL)
11831 return 0;
11832 sections->macro.s.section = sectp;
11833 sections->macro.size = bfd_section_size (sectp);
11834 }
11835 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11836 {
11837 /* There can be only one. */
11838 if (sections->str_offsets.s.section != NULL)
11839 return 0;
11840 sections->str_offsets.s.section = sectp;
11841 sections->str_offsets.size = bfd_section_size (sectp);
11842 }
11843 else
11844 {
11845 /* No other kind of section is valid. */
11846 return 0;
11847 }
11848
11849 return 1;
11850 }
11851
11852 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11853 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11854 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11855 This is for DWP version 1 files. */
11856
11857 static struct dwo_unit *
11858 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11859 struct dwp_file *dwp_file,
11860 uint32_t unit_index,
11861 const char *comp_dir,
11862 ULONGEST signature, int is_debug_types)
11863 {
11864 const struct dwp_hash_table *dwp_htab =
11865 is_debug_types ? dwp_file->tus : dwp_file->cus;
11866 bfd *dbfd = dwp_file->dbfd.get ();
11867 const char *kind = is_debug_types ? "TU" : "CU";
11868 struct dwo_file *dwo_file;
11869 struct dwo_unit *dwo_unit;
11870 struct virtual_v1_dwo_sections sections;
11871 void **dwo_file_slot;
11872 int i;
11873
11874 gdb_assert (dwp_file->version == 1);
11875
11876 if (dwarf_read_debug)
11877 {
11878 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11879 kind,
11880 pulongest (unit_index), hex_string (signature),
11881 dwp_file->name);
11882 }
11883
11884 /* Fetch the sections of this DWO unit.
11885 Put a limit on the number of sections we look for so that bad data
11886 doesn't cause us to loop forever. */
11887
11888 #define MAX_NR_V1_DWO_SECTIONS \
11889 (1 /* .debug_info or .debug_types */ \
11890 + 1 /* .debug_abbrev */ \
11891 + 1 /* .debug_line */ \
11892 + 1 /* .debug_loc */ \
11893 + 1 /* .debug_str_offsets */ \
11894 + 1 /* .debug_macro or .debug_macinfo */ \
11895 + 1 /* trailing zero */)
11896
11897 memset (&sections, 0, sizeof (sections));
11898
11899 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11900 {
11901 asection *sectp;
11902 uint32_t section_nr =
11903 read_4_bytes (dbfd,
11904 dwp_htab->section_pool.v1.indices
11905 + (unit_index + i) * sizeof (uint32_t));
11906
11907 if (section_nr == 0)
11908 break;
11909 if (section_nr >= dwp_file->num_sections)
11910 {
11911 error (_("Dwarf Error: bad DWP hash table, section number too large"
11912 " [in module %s]"),
11913 dwp_file->name);
11914 }
11915
11916 sectp = dwp_file->elf_sections[section_nr];
11917 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11918 {
11919 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11920 " [in module %s]"),
11921 dwp_file->name);
11922 }
11923 }
11924
11925 if (i < 2
11926 || sections.info_or_types.empty ()
11927 || sections.abbrev.empty ())
11928 {
11929 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11930 " [in module %s]"),
11931 dwp_file->name);
11932 }
11933 if (i == MAX_NR_V1_DWO_SECTIONS)
11934 {
11935 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11936 " [in module %s]"),
11937 dwp_file->name);
11938 }
11939
11940 /* It's easier for the rest of the code if we fake a struct dwo_file and
11941 have dwo_unit "live" in that. At least for now.
11942
11943 The DWP file can be made up of a random collection of CUs and TUs.
11944 However, for each CU + set of TUs that came from the same original DWO
11945 file, we can combine them back into a virtual DWO file to save space
11946 (fewer struct dwo_file objects to allocate). Remember that for really
11947 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11948
11949 std::string virtual_dwo_name =
11950 string_printf ("virtual-dwo/%d-%d-%d-%d",
11951 sections.abbrev.get_id (),
11952 sections.line.get_id (),
11953 sections.loc.get_id (),
11954 sections.str_offsets.get_id ());
11955 /* Can we use an existing virtual DWO file? */
11956 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11957 virtual_dwo_name.c_str (),
11958 comp_dir);
11959 /* Create one if necessary. */
11960 if (*dwo_file_slot == NULL)
11961 {
11962 if (dwarf_read_debug)
11963 {
11964 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11965 virtual_dwo_name.c_str ());
11966 }
11967 dwo_file = new struct dwo_file;
11968 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11969 dwo_file->comp_dir = comp_dir;
11970 dwo_file->sections.abbrev = sections.abbrev;
11971 dwo_file->sections.line = sections.line;
11972 dwo_file->sections.loc = sections.loc;
11973 dwo_file->sections.macinfo = sections.macinfo;
11974 dwo_file->sections.macro = sections.macro;
11975 dwo_file->sections.str_offsets = sections.str_offsets;
11976 /* The "str" section is global to the entire DWP file. */
11977 dwo_file->sections.str = dwp_file->sections.str;
11978 /* The info or types section is assigned below to dwo_unit,
11979 there's no need to record it in dwo_file.
11980 Also, we can't simply record type sections in dwo_file because
11981 we record a pointer into the vector in dwo_unit. As we collect more
11982 types we'll grow the vector and eventually have to reallocate space
11983 for it, invalidating all copies of pointers into the previous
11984 contents. */
11985 *dwo_file_slot = dwo_file;
11986 }
11987 else
11988 {
11989 if (dwarf_read_debug)
11990 {
11991 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11992 virtual_dwo_name.c_str ());
11993 }
11994 dwo_file = (struct dwo_file *) *dwo_file_slot;
11995 }
11996
11997 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11998 dwo_unit->dwo_file = dwo_file;
11999 dwo_unit->signature = signature;
12000 dwo_unit->section =
12001 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12002 *dwo_unit->section = sections.info_or_types;
12003 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12004
12005 return dwo_unit;
12006 }
12007
12008 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12009 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12010 piece within that section used by a TU/CU, return a virtual section
12011 of just that piece. */
12012
12013 static struct dwarf2_section_info
12014 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12015 struct dwarf2_section_info *section,
12016 bfd_size_type offset, bfd_size_type size)
12017 {
12018 struct dwarf2_section_info result;
12019 asection *sectp;
12020
12021 gdb_assert (section != NULL);
12022 gdb_assert (!section->is_virtual);
12023
12024 memset (&result, 0, sizeof (result));
12025 result.s.containing_section = section;
12026 result.is_virtual = true;
12027
12028 if (size == 0)
12029 return result;
12030
12031 sectp = section->get_bfd_section ();
12032
12033 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12034 bounds of the real section. This is a pretty-rare event, so just
12035 flag an error (easier) instead of a warning and trying to cope. */
12036 if (sectp == NULL
12037 || offset + size > bfd_section_size (sectp))
12038 {
12039 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12040 " in section %s [in module %s]"),
12041 sectp ? bfd_section_name (sectp) : "<unknown>",
12042 objfile_name (dwarf2_per_objfile->objfile));
12043 }
12044
12045 result.virtual_offset = offset;
12046 result.size = size;
12047 return result;
12048 }
12049
12050 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12051 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12052 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12053 This is for DWP version 2 files. */
12054
12055 static struct dwo_unit *
12056 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12057 struct dwp_file *dwp_file,
12058 uint32_t unit_index,
12059 const char *comp_dir,
12060 ULONGEST signature, int is_debug_types)
12061 {
12062 const struct dwp_hash_table *dwp_htab =
12063 is_debug_types ? dwp_file->tus : dwp_file->cus;
12064 bfd *dbfd = dwp_file->dbfd.get ();
12065 const char *kind = is_debug_types ? "TU" : "CU";
12066 struct dwo_file *dwo_file;
12067 struct dwo_unit *dwo_unit;
12068 struct virtual_v2_dwo_sections sections;
12069 void **dwo_file_slot;
12070 int i;
12071
12072 gdb_assert (dwp_file->version == 2);
12073
12074 if (dwarf_read_debug)
12075 {
12076 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12077 kind,
12078 pulongest (unit_index), hex_string (signature),
12079 dwp_file->name);
12080 }
12081
12082 /* Fetch the section offsets of this DWO unit. */
12083
12084 memset (&sections, 0, sizeof (sections));
12085
12086 for (i = 0; i < dwp_htab->nr_columns; ++i)
12087 {
12088 uint32_t offset = read_4_bytes (dbfd,
12089 dwp_htab->section_pool.v2.offsets
12090 + (((unit_index - 1) * dwp_htab->nr_columns
12091 + i)
12092 * sizeof (uint32_t)));
12093 uint32_t size = read_4_bytes (dbfd,
12094 dwp_htab->section_pool.v2.sizes
12095 + (((unit_index - 1) * dwp_htab->nr_columns
12096 + i)
12097 * sizeof (uint32_t)));
12098
12099 switch (dwp_htab->section_pool.v2.section_ids[i])
12100 {
12101 case DW_SECT_INFO:
12102 case DW_SECT_TYPES:
12103 sections.info_or_types_offset = offset;
12104 sections.info_or_types_size = size;
12105 break;
12106 case DW_SECT_ABBREV:
12107 sections.abbrev_offset = offset;
12108 sections.abbrev_size = size;
12109 break;
12110 case DW_SECT_LINE:
12111 sections.line_offset = offset;
12112 sections.line_size = size;
12113 break;
12114 case DW_SECT_LOC:
12115 sections.loc_offset = offset;
12116 sections.loc_size = size;
12117 break;
12118 case DW_SECT_STR_OFFSETS:
12119 sections.str_offsets_offset = offset;
12120 sections.str_offsets_size = size;
12121 break;
12122 case DW_SECT_MACINFO:
12123 sections.macinfo_offset = offset;
12124 sections.macinfo_size = size;
12125 break;
12126 case DW_SECT_MACRO:
12127 sections.macro_offset = offset;
12128 sections.macro_size = size;
12129 break;
12130 }
12131 }
12132
12133 /* It's easier for the rest of the code if we fake a struct dwo_file and
12134 have dwo_unit "live" in that. At least for now.
12135
12136 The DWP file can be made up of a random collection of CUs and TUs.
12137 However, for each CU + set of TUs that came from the same original DWO
12138 file, we can combine them back into a virtual DWO file to save space
12139 (fewer struct dwo_file objects to allocate). Remember that for really
12140 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12141
12142 std::string virtual_dwo_name =
12143 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12144 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12145 (long) (sections.line_size ? sections.line_offset : 0),
12146 (long) (sections.loc_size ? sections.loc_offset : 0),
12147 (long) (sections.str_offsets_size
12148 ? sections.str_offsets_offset : 0));
12149 /* Can we use an existing virtual DWO file? */
12150 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12151 virtual_dwo_name.c_str (),
12152 comp_dir);
12153 /* Create one if necessary. */
12154 if (*dwo_file_slot == NULL)
12155 {
12156 if (dwarf_read_debug)
12157 {
12158 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12159 virtual_dwo_name.c_str ());
12160 }
12161 dwo_file = new struct dwo_file;
12162 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12163 dwo_file->comp_dir = comp_dir;
12164 dwo_file->sections.abbrev =
12165 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12166 sections.abbrev_offset, sections.abbrev_size);
12167 dwo_file->sections.line =
12168 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12169 sections.line_offset, sections.line_size);
12170 dwo_file->sections.loc =
12171 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12172 sections.loc_offset, sections.loc_size);
12173 dwo_file->sections.macinfo =
12174 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12175 sections.macinfo_offset, sections.macinfo_size);
12176 dwo_file->sections.macro =
12177 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12178 sections.macro_offset, sections.macro_size);
12179 dwo_file->sections.str_offsets =
12180 create_dwp_v2_section (dwarf2_per_objfile,
12181 &dwp_file->sections.str_offsets,
12182 sections.str_offsets_offset,
12183 sections.str_offsets_size);
12184 /* The "str" section is global to the entire DWP file. */
12185 dwo_file->sections.str = dwp_file->sections.str;
12186 /* The info or types section is assigned below to dwo_unit,
12187 there's no need to record it in dwo_file.
12188 Also, we can't simply record type sections in dwo_file because
12189 we record a pointer into the vector in dwo_unit. As we collect more
12190 types we'll grow the vector and eventually have to reallocate space
12191 for it, invalidating all copies of pointers into the previous
12192 contents. */
12193 *dwo_file_slot = dwo_file;
12194 }
12195 else
12196 {
12197 if (dwarf_read_debug)
12198 {
12199 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12200 virtual_dwo_name.c_str ());
12201 }
12202 dwo_file = (struct dwo_file *) *dwo_file_slot;
12203 }
12204
12205 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12206 dwo_unit->dwo_file = dwo_file;
12207 dwo_unit->signature = signature;
12208 dwo_unit->section =
12209 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12210 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12211 is_debug_types
12212 ? &dwp_file->sections.types
12213 : &dwp_file->sections.info,
12214 sections.info_or_types_offset,
12215 sections.info_or_types_size);
12216 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12217
12218 return dwo_unit;
12219 }
12220
12221 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12222 Returns NULL if the signature isn't found. */
12223
12224 static struct dwo_unit *
12225 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12226 struct dwp_file *dwp_file, const char *comp_dir,
12227 ULONGEST signature, int is_debug_types)
12228 {
12229 const struct dwp_hash_table *dwp_htab =
12230 is_debug_types ? dwp_file->tus : dwp_file->cus;
12231 bfd *dbfd = dwp_file->dbfd.get ();
12232 uint32_t mask = dwp_htab->nr_slots - 1;
12233 uint32_t hash = signature & mask;
12234 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12235 unsigned int i;
12236 void **slot;
12237 struct dwo_unit find_dwo_cu;
12238
12239 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12240 find_dwo_cu.signature = signature;
12241 slot = htab_find_slot (is_debug_types
12242 ? dwp_file->loaded_tus.get ()
12243 : dwp_file->loaded_cus.get (),
12244 &find_dwo_cu, INSERT);
12245
12246 if (*slot != NULL)
12247 return (struct dwo_unit *) *slot;
12248
12249 /* Use a for loop so that we don't loop forever on bad debug info. */
12250 for (i = 0; i < dwp_htab->nr_slots; ++i)
12251 {
12252 ULONGEST signature_in_table;
12253
12254 signature_in_table =
12255 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12256 if (signature_in_table == signature)
12257 {
12258 uint32_t unit_index =
12259 read_4_bytes (dbfd,
12260 dwp_htab->unit_table + hash * sizeof (uint32_t));
12261
12262 if (dwp_file->version == 1)
12263 {
12264 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12265 dwp_file, unit_index,
12266 comp_dir, signature,
12267 is_debug_types);
12268 }
12269 else
12270 {
12271 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12272 dwp_file, unit_index,
12273 comp_dir, signature,
12274 is_debug_types);
12275 }
12276 return (struct dwo_unit *) *slot;
12277 }
12278 if (signature_in_table == 0)
12279 return NULL;
12280 hash = (hash + hash2) & mask;
12281 }
12282
12283 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12284 " [in module %s]"),
12285 dwp_file->name);
12286 }
12287
12288 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12289 Open the file specified by FILE_NAME and hand it off to BFD for
12290 preliminary analysis. Return a newly initialized bfd *, which
12291 includes a canonicalized copy of FILE_NAME.
12292 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12293 SEARCH_CWD is true if the current directory is to be searched.
12294 It will be searched before debug-file-directory.
12295 If successful, the file is added to the bfd include table of the
12296 objfile's bfd (see gdb_bfd_record_inclusion).
12297 If unable to find/open the file, return NULL.
12298 NOTE: This function is derived from symfile_bfd_open. */
12299
12300 static gdb_bfd_ref_ptr
12301 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12302 const char *file_name, int is_dwp, int search_cwd)
12303 {
12304 int desc;
12305 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12306 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12307 to debug_file_directory. */
12308 const char *search_path;
12309 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12310
12311 gdb::unique_xmalloc_ptr<char> search_path_holder;
12312 if (search_cwd)
12313 {
12314 if (*debug_file_directory != '\0')
12315 {
12316 search_path_holder.reset (concat (".", dirname_separator_string,
12317 debug_file_directory,
12318 (char *) NULL));
12319 search_path = search_path_holder.get ();
12320 }
12321 else
12322 search_path = ".";
12323 }
12324 else
12325 search_path = debug_file_directory;
12326
12327 openp_flags flags = OPF_RETURN_REALPATH;
12328 if (is_dwp)
12329 flags |= OPF_SEARCH_IN_PATH;
12330
12331 gdb::unique_xmalloc_ptr<char> absolute_name;
12332 desc = openp (search_path, flags, file_name,
12333 O_RDONLY | O_BINARY, &absolute_name);
12334 if (desc < 0)
12335 return NULL;
12336
12337 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12338 gnutarget, desc));
12339 if (sym_bfd == NULL)
12340 return NULL;
12341 bfd_set_cacheable (sym_bfd.get (), 1);
12342
12343 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12344 return NULL;
12345
12346 /* Success. Record the bfd as having been included by the objfile's bfd.
12347 This is important because things like demangled_names_hash lives in the
12348 objfile's per_bfd space and may have references to things like symbol
12349 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12350 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12351
12352 return sym_bfd;
12353 }
12354
12355 /* Try to open DWO file FILE_NAME.
12356 COMP_DIR is the DW_AT_comp_dir attribute.
12357 The result is the bfd handle of the file.
12358 If there is a problem finding or opening the file, return NULL.
12359 Upon success, the canonicalized path of the file is stored in the bfd,
12360 same as symfile_bfd_open. */
12361
12362 static gdb_bfd_ref_ptr
12363 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12364 const char *file_name, const char *comp_dir)
12365 {
12366 if (IS_ABSOLUTE_PATH (file_name))
12367 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12368 0 /*is_dwp*/, 0 /*search_cwd*/);
12369
12370 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12371
12372 if (comp_dir != NULL)
12373 {
12374 gdb::unique_xmalloc_ptr<char> path_to_try
12375 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12376
12377 /* NOTE: If comp_dir is a relative path, this will also try the
12378 search path, which seems useful. */
12379 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12380 path_to_try.get (),
12381 0 /*is_dwp*/,
12382 1 /*search_cwd*/));
12383 if (abfd != NULL)
12384 return abfd;
12385 }
12386
12387 /* That didn't work, try debug-file-directory, which, despite its name,
12388 is a list of paths. */
12389
12390 if (*debug_file_directory == '\0')
12391 return NULL;
12392
12393 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12394 0 /*is_dwp*/, 1 /*search_cwd*/);
12395 }
12396
12397 /* This function is mapped across the sections and remembers the offset and
12398 size of each of the DWO debugging sections we are interested in. */
12399
12400 static void
12401 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12402 {
12403 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12404 const struct dwop_section_names *names = &dwop_section_names;
12405
12406 if (section_is_p (sectp->name, &names->abbrev_dwo))
12407 {
12408 dwo_sections->abbrev.s.section = sectp;
12409 dwo_sections->abbrev.size = bfd_section_size (sectp);
12410 }
12411 else if (section_is_p (sectp->name, &names->info_dwo))
12412 {
12413 dwo_sections->info.s.section = sectp;
12414 dwo_sections->info.size = bfd_section_size (sectp);
12415 }
12416 else if (section_is_p (sectp->name, &names->line_dwo))
12417 {
12418 dwo_sections->line.s.section = sectp;
12419 dwo_sections->line.size = bfd_section_size (sectp);
12420 }
12421 else if (section_is_p (sectp->name, &names->loc_dwo))
12422 {
12423 dwo_sections->loc.s.section = sectp;
12424 dwo_sections->loc.size = bfd_section_size (sectp);
12425 }
12426 else if (section_is_p (sectp->name, &names->loclists_dwo))
12427 {
12428 dwo_sections->loclists.s.section = sectp;
12429 dwo_sections->loclists.size = bfd_section_size (sectp);
12430 }
12431 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12432 {
12433 dwo_sections->macinfo.s.section = sectp;
12434 dwo_sections->macinfo.size = bfd_section_size (sectp);
12435 }
12436 else if (section_is_p (sectp->name, &names->macro_dwo))
12437 {
12438 dwo_sections->macro.s.section = sectp;
12439 dwo_sections->macro.size = bfd_section_size (sectp);
12440 }
12441 else if (section_is_p (sectp->name, &names->str_dwo))
12442 {
12443 dwo_sections->str.s.section = sectp;
12444 dwo_sections->str.size = bfd_section_size (sectp);
12445 }
12446 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12447 {
12448 dwo_sections->str_offsets.s.section = sectp;
12449 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12450 }
12451 else if (section_is_p (sectp->name, &names->types_dwo))
12452 {
12453 struct dwarf2_section_info type_section;
12454
12455 memset (&type_section, 0, sizeof (type_section));
12456 type_section.s.section = sectp;
12457 type_section.size = bfd_section_size (sectp);
12458 dwo_sections->types.push_back (type_section);
12459 }
12460 }
12461
12462 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12463 by PER_CU. This is for the non-DWP case.
12464 The result is NULL if DWO_NAME can't be found. */
12465
12466 static struct dwo_file *
12467 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12468 const char *comp_dir)
12469 {
12470 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12471
12472 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12473 if (dbfd == NULL)
12474 {
12475 if (dwarf_read_debug)
12476 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12477 return NULL;
12478 }
12479
12480 dwo_file_up dwo_file (new struct dwo_file);
12481 dwo_file->dwo_name = dwo_name;
12482 dwo_file->comp_dir = comp_dir;
12483 dwo_file->dbfd = std::move (dbfd);
12484
12485 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12486 &dwo_file->sections);
12487
12488 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
12489 dwo_file->sections.info, dwo_file->cus);
12490
12491 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12492 dwo_file->sections.types, dwo_file->tus);
12493
12494 if (dwarf_read_debug)
12495 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12496
12497 return dwo_file.release ();
12498 }
12499
12500 /* This function is mapped across the sections and remembers the offset and
12501 size of each of the DWP debugging sections common to version 1 and 2 that
12502 we are interested in. */
12503
12504 static void
12505 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12506 void *dwp_file_ptr)
12507 {
12508 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12509 const struct dwop_section_names *names = &dwop_section_names;
12510 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12511
12512 /* Record the ELF section number for later lookup: this is what the
12513 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12514 gdb_assert (elf_section_nr < dwp_file->num_sections);
12515 dwp_file->elf_sections[elf_section_nr] = sectp;
12516
12517 /* Look for specific sections that we need. */
12518 if (section_is_p (sectp->name, &names->str_dwo))
12519 {
12520 dwp_file->sections.str.s.section = sectp;
12521 dwp_file->sections.str.size = bfd_section_size (sectp);
12522 }
12523 else if (section_is_p (sectp->name, &names->cu_index))
12524 {
12525 dwp_file->sections.cu_index.s.section = sectp;
12526 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12527 }
12528 else if (section_is_p (sectp->name, &names->tu_index))
12529 {
12530 dwp_file->sections.tu_index.s.section = sectp;
12531 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12532 }
12533 }
12534
12535 /* This function is mapped across the sections and remembers the offset and
12536 size of each of the DWP version 2 debugging sections that we are interested
12537 in. This is split into a separate function because we don't know if we
12538 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12539
12540 static void
12541 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12542 {
12543 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12544 const struct dwop_section_names *names = &dwop_section_names;
12545 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12546
12547 /* Record the ELF section number for later lookup: this is what the
12548 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12549 gdb_assert (elf_section_nr < dwp_file->num_sections);
12550 dwp_file->elf_sections[elf_section_nr] = sectp;
12551
12552 /* Look for specific sections that we need. */
12553 if (section_is_p (sectp->name, &names->abbrev_dwo))
12554 {
12555 dwp_file->sections.abbrev.s.section = sectp;
12556 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12557 }
12558 else if (section_is_p (sectp->name, &names->info_dwo))
12559 {
12560 dwp_file->sections.info.s.section = sectp;
12561 dwp_file->sections.info.size = bfd_section_size (sectp);
12562 }
12563 else if (section_is_p (sectp->name, &names->line_dwo))
12564 {
12565 dwp_file->sections.line.s.section = sectp;
12566 dwp_file->sections.line.size = bfd_section_size (sectp);
12567 }
12568 else if (section_is_p (sectp->name, &names->loc_dwo))
12569 {
12570 dwp_file->sections.loc.s.section = sectp;
12571 dwp_file->sections.loc.size = bfd_section_size (sectp);
12572 }
12573 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12574 {
12575 dwp_file->sections.macinfo.s.section = sectp;
12576 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12577 }
12578 else if (section_is_p (sectp->name, &names->macro_dwo))
12579 {
12580 dwp_file->sections.macro.s.section = sectp;
12581 dwp_file->sections.macro.size = bfd_section_size (sectp);
12582 }
12583 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12584 {
12585 dwp_file->sections.str_offsets.s.section = sectp;
12586 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12587 }
12588 else if (section_is_p (sectp->name, &names->types_dwo))
12589 {
12590 dwp_file->sections.types.s.section = sectp;
12591 dwp_file->sections.types.size = bfd_section_size (sectp);
12592 }
12593 }
12594
12595 /* Hash function for dwp_file loaded CUs/TUs. */
12596
12597 static hashval_t
12598 hash_dwp_loaded_cutus (const void *item)
12599 {
12600 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12601
12602 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12603 return dwo_unit->signature;
12604 }
12605
12606 /* Equality function for dwp_file loaded CUs/TUs. */
12607
12608 static int
12609 eq_dwp_loaded_cutus (const void *a, const void *b)
12610 {
12611 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12612 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12613
12614 return dua->signature == dub->signature;
12615 }
12616
12617 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12618
12619 static htab_up
12620 allocate_dwp_loaded_cutus_table ()
12621 {
12622 return htab_up (htab_create_alloc (3,
12623 hash_dwp_loaded_cutus,
12624 eq_dwp_loaded_cutus,
12625 NULL, xcalloc, xfree));
12626 }
12627
12628 /* Try to open DWP file FILE_NAME.
12629 The result is the bfd handle of the file.
12630 If there is a problem finding or opening the file, return NULL.
12631 Upon success, the canonicalized path of the file is stored in the bfd,
12632 same as symfile_bfd_open. */
12633
12634 static gdb_bfd_ref_ptr
12635 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12636 const char *file_name)
12637 {
12638 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12639 1 /*is_dwp*/,
12640 1 /*search_cwd*/));
12641 if (abfd != NULL)
12642 return abfd;
12643
12644 /* Work around upstream bug 15652.
12645 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12646 [Whether that's a "bug" is debatable, but it is getting in our way.]
12647 We have no real idea where the dwp file is, because gdb's realpath-ing
12648 of the executable's path may have discarded the needed info.
12649 [IWBN if the dwp file name was recorded in the executable, akin to
12650 .gnu_debuglink, but that doesn't exist yet.]
12651 Strip the directory from FILE_NAME and search again. */
12652 if (*debug_file_directory != '\0')
12653 {
12654 /* Don't implicitly search the current directory here.
12655 If the user wants to search "." to handle this case,
12656 it must be added to debug-file-directory. */
12657 return try_open_dwop_file (dwarf2_per_objfile,
12658 lbasename (file_name), 1 /*is_dwp*/,
12659 0 /*search_cwd*/);
12660 }
12661
12662 return NULL;
12663 }
12664
12665 /* Initialize the use of the DWP file for the current objfile.
12666 By convention the name of the DWP file is ${objfile}.dwp.
12667 The result is NULL if it can't be found. */
12668
12669 static std::unique_ptr<struct dwp_file>
12670 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12671 {
12672 struct objfile *objfile = dwarf2_per_objfile->objfile;
12673
12674 /* Try to find first .dwp for the binary file before any symbolic links
12675 resolving. */
12676
12677 /* If the objfile is a debug file, find the name of the real binary
12678 file and get the name of dwp file from there. */
12679 std::string dwp_name;
12680 if (objfile->separate_debug_objfile_backlink != NULL)
12681 {
12682 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12683 const char *backlink_basename = lbasename (backlink->original_name);
12684
12685 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12686 }
12687 else
12688 dwp_name = objfile->original_name;
12689
12690 dwp_name += ".dwp";
12691
12692 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12693 if (dbfd == NULL
12694 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12695 {
12696 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12697 dwp_name = objfile_name (objfile);
12698 dwp_name += ".dwp";
12699 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12700 }
12701
12702 if (dbfd == NULL)
12703 {
12704 if (dwarf_read_debug)
12705 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12706 return std::unique_ptr<dwp_file> ();
12707 }
12708
12709 const char *name = bfd_get_filename (dbfd.get ());
12710 std::unique_ptr<struct dwp_file> dwp_file
12711 (new struct dwp_file (name, std::move (dbfd)));
12712
12713 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12714 dwp_file->elf_sections =
12715 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12716 dwp_file->num_sections, asection *);
12717
12718 bfd_map_over_sections (dwp_file->dbfd.get (),
12719 dwarf2_locate_common_dwp_sections,
12720 dwp_file.get ());
12721
12722 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12723 0);
12724
12725 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12726 1);
12727
12728 /* The DWP file version is stored in the hash table. Oh well. */
12729 if (dwp_file->cus && dwp_file->tus
12730 && dwp_file->cus->version != dwp_file->tus->version)
12731 {
12732 /* Technically speaking, we should try to limp along, but this is
12733 pretty bizarre. We use pulongest here because that's the established
12734 portability solution (e.g, we cannot use %u for uint32_t). */
12735 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12736 " TU version %s [in DWP file %s]"),
12737 pulongest (dwp_file->cus->version),
12738 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12739 }
12740
12741 if (dwp_file->cus)
12742 dwp_file->version = dwp_file->cus->version;
12743 else if (dwp_file->tus)
12744 dwp_file->version = dwp_file->tus->version;
12745 else
12746 dwp_file->version = 2;
12747
12748 if (dwp_file->version == 2)
12749 bfd_map_over_sections (dwp_file->dbfd.get (),
12750 dwarf2_locate_v2_dwp_sections,
12751 dwp_file.get ());
12752
12753 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12754 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12755
12756 if (dwarf_read_debug)
12757 {
12758 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12759 fprintf_unfiltered (gdb_stdlog,
12760 " %s CUs, %s TUs\n",
12761 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12762 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12763 }
12764
12765 return dwp_file;
12766 }
12767
12768 /* Wrapper around open_and_init_dwp_file, only open it once. */
12769
12770 static struct dwp_file *
12771 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12772 {
12773 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12774 {
12775 dwarf2_per_objfile->per_bfd->dwp_file
12776 = open_and_init_dwp_file (dwarf2_per_objfile);
12777 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12778 }
12779 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12780 }
12781
12782 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12783 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12784 or in the DWP file for the objfile, referenced by THIS_UNIT.
12785 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12786 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12787
12788 This is called, for example, when wanting to read a variable with a
12789 complex location. Therefore we don't want to do file i/o for every call.
12790 Therefore we don't want to look for a DWO file on every call.
12791 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12792 then we check if we've already seen DWO_NAME, and only THEN do we check
12793 for a DWO file.
12794
12795 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12796 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12797
12798 static struct dwo_unit *
12799 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12800 ULONGEST signature, int is_debug_types)
12801 {
12802 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12803 struct objfile *objfile = dwarf2_per_objfile->objfile;
12804 const char *kind = is_debug_types ? "TU" : "CU";
12805 void **dwo_file_slot;
12806 struct dwo_file *dwo_file;
12807 struct dwp_file *dwp_file;
12808
12809 /* First see if there's a DWP file.
12810 If we have a DWP file but didn't find the DWO inside it, don't
12811 look for the original DWO file. It makes gdb behave differently
12812 depending on whether one is debugging in the build tree. */
12813
12814 dwp_file = get_dwp_file (dwarf2_per_objfile);
12815 if (dwp_file != NULL)
12816 {
12817 const struct dwp_hash_table *dwp_htab =
12818 is_debug_types ? dwp_file->tus : dwp_file->cus;
12819
12820 if (dwp_htab != NULL)
12821 {
12822 struct dwo_unit *dwo_cutu =
12823 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12824 signature, is_debug_types);
12825
12826 if (dwo_cutu != NULL)
12827 {
12828 if (dwarf_read_debug)
12829 {
12830 fprintf_unfiltered (gdb_stdlog,
12831 "Virtual DWO %s %s found: @%s\n",
12832 kind, hex_string (signature),
12833 host_address_to_string (dwo_cutu));
12834 }
12835 return dwo_cutu;
12836 }
12837 }
12838 }
12839 else
12840 {
12841 /* No DWP file, look for the DWO file. */
12842
12843 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12844 dwo_name, comp_dir);
12845 if (*dwo_file_slot == NULL)
12846 {
12847 /* Read in the file and build a table of the CUs/TUs it contains. */
12848 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12849 }
12850 /* NOTE: This will be NULL if unable to open the file. */
12851 dwo_file = (struct dwo_file *) *dwo_file_slot;
12852
12853 if (dwo_file != NULL)
12854 {
12855 struct dwo_unit *dwo_cutu = NULL;
12856
12857 if (is_debug_types && dwo_file->tus)
12858 {
12859 struct dwo_unit find_dwo_cutu;
12860
12861 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12862 find_dwo_cutu.signature = signature;
12863 dwo_cutu
12864 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12865 &find_dwo_cutu);
12866 }
12867 else if (!is_debug_types && dwo_file->cus)
12868 {
12869 struct dwo_unit find_dwo_cutu;
12870
12871 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12872 find_dwo_cutu.signature = signature;
12873 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12874 &find_dwo_cutu);
12875 }
12876
12877 if (dwo_cutu != NULL)
12878 {
12879 if (dwarf_read_debug)
12880 {
12881 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12882 kind, dwo_name, hex_string (signature),
12883 host_address_to_string (dwo_cutu));
12884 }
12885 return dwo_cutu;
12886 }
12887 }
12888 }
12889
12890 /* We didn't find it. This could mean a dwo_id mismatch, or
12891 someone deleted the DWO/DWP file, or the search path isn't set up
12892 correctly to find the file. */
12893
12894 if (dwarf_read_debug)
12895 {
12896 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12897 kind, dwo_name, hex_string (signature));
12898 }
12899
12900 /* This is a warning and not a complaint because it can be caused by
12901 pilot error (e.g., user accidentally deleting the DWO). */
12902 {
12903 /* Print the name of the DWP file if we looked there, helps the user
12904 better diagnose the problem. */
12905 std::string dwp_text;
12906
12907 if (dwp_file != NULL)
12908 dwp_text = string_printf (" [in DWP file %s]",
12909 lbasename (dwp_file->name));
12910
12911 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12912 " [in module %s]"),
12913 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12914 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12915 }
12916 return NULL;
12917 }
12918
12919 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12920 See lookup_dwo_cutu_unit for details. */
12921
12922 static struct dwo_unit *
12923 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12924 ULONGEST signature)
12925 {
12926 gdb_assert (!cu->per_cu->is_debug_types);
12927
12928 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12929 }
12930
12931 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12932 See lookup_dwo_cutu_unit for details. */
12933
12934 static struct dwo_unit *
12935 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12936 {
12937 gdb_assert (cu->per_cu->is_debug_types);
12938
12939 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12940
12941 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12942 }
12943
12944 /* Traversal function for queue_and_load_all_dwo_tus. */
12945
12946 static int
12947 queue_and_load_dwo_tu (void **slot, void *info)
12948 {
12949 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12950 dwarf2_cu *cu = (dwarf2_cu *) info;
12951 ULONGEST signature = dwo_unit->signature;
12952 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12953
12954 if (sig_type != NULL)
12955 {
12956 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12957
12958 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12959 a real dependency of PER_CU on SIG_TYPE. That is detected later
12960 while processing PER_CU. */
12961 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12962 load_full_type_unit (sig_cu, cu->per_objfile);
12963 cu->per_cu->imported_symtabs_push (sig_cu);
12964 }
12965
12966 return 1;
12967 }
12968
12969 /* Queue all TUs contained in the DWO of CU to be read in.
12970 The DWO may have the only definition of the type, though it may not be
12971 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12972 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12973
12974 static void
12975 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12976 {
12977 struct dwo_unit *dwo_unit;
12978 struct dwo_file *dwo_file;
12979
12980 gdb_assert (cu != nullptr);
12981 gdb_assert (!cu->per_cu->is_debug_types);
12982 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12983
12984 dwo_unit = cu->dwo_unit;
12985 gdb_assert (dwo_unit != NULL);
12986
12987 dwo_file = dwo_unit->dwo_file;
12988 if (dwo_file->tus != NULL)
12989 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12990 }
12991
12992 /* Read in various DIEs. */
12993
12994 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12995 Inherit only the children of the DW_AT_abstract_origin DIE not being
12996 already referenced by DW_AT_abstract_origin from the children of the
12997 current DIE. */
12998
12999 static void
13000 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13001 {
13002 struct die_info *child_die;
13003 sect_offset *offsetp;
13004 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13005 struct die_info *origin_die;
13006 /* Iterator of the ORIGIN_DIE children. */
13007 struct die_info *origin_child_die;
13008 struct attribute *attr;
13009 struct dwarf2_cu *origin_cu;
13010 struct pending **origin_previous_list_in_scope;
13011
13012 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13013 if (!attr)
13014 return;
13015
13016 /* Note that following die references may follow to a die in a
13017 different cu. */
13018
13019 origin_cu = cu;
13020 origin_die = follow_die_ref (die, attr, &origin_cu);
13021
13022 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13023 symbols in. */
13024 origin_previous_list_in_scope = origin_cu->list_in_scope;
13025 origin_cu->list_in_scope = cu->list_in_scope;
13026
13027 if (die->tag != origin_die->tag
13028 && !(die->tag == DW_TAG_inlined_subroutine
13029 && origin_die->tag == DW_TAG_subprogram))
13030 complaint (_("DIE %s and its abstract origin %s have different tags"),
13031 sect_offset_str (die->sect_off),
13032 sect_offset_str (origin_die->sect_off));
13033
13034 std::vector<sect_offset> offsets;
13035
13036 for (child_die = die->child;
13037 child_die && child_die->tag;
13038 child_die = child_die->sibling)
13039 {
13040 struct die_info *child_origin_die;
13041 struct dwarf2_cu *child_origin_cu;
13042
13043 /* We are trying to process concrete instance entries:
13044 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13045 it's not relevant to our analysis here. i.e. detecting DIEs that are
13046 present in the abstract instance but not referenced in the concrete
13047 one. */
13048 if (child_die->tag == DW_TAG_call_site
13049 || child_die->tag == DW_TAG_GNU_call_site)
13050 continue;
13051
13052 /* For each CHILD_DIE, find the corresponding child of
13053 ORIGIN_DIE. If there is more than one layer of
13054 DW_AT_abstract_origin, follow them all; there shouldn't be,
13055 but GCC versions at least through 4.4 generate this (GCC PR
13056 40573). */
13057 child_origin_die = child_die;
13058 child_origin_cu = cu;
13059 while (1)
13060 {
13061 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13062 child_origin_cu);
13063 if (attr == NULL)
13064 break;
13065 child_origin_die = follow_die_ref (child_origin_die, attr,
13066 &child_origin_cu);
13067 }
13068
13069 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13070 counterpart may exist. */
13071 if (child_origin_die != child_die)
13072 {
13073 if (child_die->tag != child_origin_die->tag
13074 && !(child_die->tag == DW_TAG_inlined_subroutine
13075 && child_origin_die->tag == DW_TAG_subprogram))
13076 complaint (_("Child DIE %s and its abstract origin %s have "
13077 "different tags"),
13078 sect_offset_str (child_die->sect_off),
13079 sect_offset_str (child_origin_die->sect_off));
13080 if (child_origin_die->parent != origin_die)
13081 complaint (_("Child DIE %s and its abstract origin %s have "
13082 "different parents"),
13083 sect_offset_str (child_die->sect_off),
13084 sect_offset_str (child_origin_die->sect_off));
13085 else
13086 offsets.push_back (child_origin_die->sect_off);
13087 }
13088 }
13089 std::sort (offsets.begin (), offsets.end ());
13090 sect_offset *offsets_end = offsets.data () + offsets.size ();
13091 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13092 if (offsetp[-1] == *offsetp)
13093 complaint (_("Multiple children of DIE %s refer "
13094 "to DIE %s as their abstract origin"),
13095 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13096
13097 offsetp = offsets.data ();
13098 origin_child_die = origin_die->child;
13099 while (origin_child_die && origin_child_die->tag)
13100 {
13101 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13102 while (offsetp < offsets_end
13103 && *offsetp < origin_child_die->sect_off)
13104 offsetp++;
13105 if (offsetp >= offsets_end
13106 || *offsetp > origin_child_die->sect_off)
13107 {
13108 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13109 Check whether we're already processing ORIGIN_CHILD_DIE.
13110 This can happen with mutually referenced abstract_origins.
13111 PR 16581. */
13112 if (!origin_child_die->in_process)
13113 process_die (origin_child_die, origin_cu);
13114 }
13115 origin_child_die = origin_child_die->sibling;
13116 }
13117 origin_cu->list_in_scope = origin_previous_list_in_scope;
13118
13119 if (cu != origin_cu)
13120 compute_delayed_physnames (origin_cu);
13121 }
13122
13123 static void
13124 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13125 {
13126 struct objfile *objfile = cu->per_objfile->objfile;
13127 struct gdbarch *gdbarch = objfile->arch ();
13128 struct context_stack *newobj;
13129 CORE_ADDR lowpc;
13130 CORE_ADDR highpc;
13131 struct die_info *child_die;
13132 struct attribute *attr, *call_line, *call_file;
13133 const char *name;
13134 CORE_ADDR baseaddr;
13135 struct block *block;
13136 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13137 std::vector<struct symbol *> template_args;
13138 struct template_symbol *templ_func = NULL;
13139
13140 if (inlined_func)
13141 {
13142 /* If we do not have call site information, we can't show the
13143 caller of this inlined function. That's too confusing, so
13144 only use the scope for local variables. */
13145 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13146 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13147 if (call_line == NULL || call_file == NULL)
13148 {
13149 read_lexical_block_scope (die, cu);
13150 return;
13151 }
13152 }
13153
13154 baseaddr = objfile->text_section_offset ();
13155
13156 name = dwarf2_name (die, cu);
13157
13158 /* Ignore functions with missing or empty names. These are actually
13159 illegal according to the DWARF standard. */
13160 if (name == NULL)
13161 {
13162 complaint (_("missing name for subprogram DIE at %s"),
13163 sect_offset_str (die->sect_off));
13164 return;
13165 }
13166
13167 /* Ignore functions with missing or invalid low and high pc attributes. */
13168 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13169 <= PC_BOUNDS_INVALID)
13170 {
13171 attr = dwarf2_attr (die, DW_AT_external, cu);
13172 if (!attr || !DW_UNSND (attr))
13173 complaint (_("cannot get low and high bounds "
13174 "for subprogram DIE at %s"),
13175 sect_offset_str (die->sect_off));
13176 return;
13177 }
13178
13179 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13180 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13181
13182 /* If we have any template arguments, then we must allocate a
13183 different sort of symbol. */
13184 for (child_die = die->child; child_die; child_die = child_die->sibling)
13185 {
13186 if (child_die->tag == DW_TAG_template_type_param
13187 || child_die->tag == DW_TAG_template_value_param)
13188 {
13189 templ_func = new (&objfile->objfile_obstack) template_symbol;
13190 templ_func->subclass = SYMBOL_TEMPLATE;
13191 break;
13192 }
13193 }
13194
13195 newobj = cu->get_builder ()->push_context (0, lowpc);
13196 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13197 (struct symbol *) templ_func);
13198
13199 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13200 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13201 cu->language);
13202
13203 /* If there is a location expression for DW_AT_frame_base, record
13204 it. */
13205 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13206 if (attr != nullptr)
13207 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13208
13209 /* If there is a location for the static link, record it. */
13210 newobj->static_link = NULL;
13211 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13212 if (attr != nullptr)
13213 {
13214 newobj->static_link
13215 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13216 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13217 cu->addr_type ());
13218 }
13219
13220 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13221
13222 if (die->child != NULL)
13223 {
13224 child_die = die->child;
13225 while (child_die && child_die->tag)
13226 {
13227 if (child_die->tag == DW_TAG_template_type_param
13228 || child_die->tag == DW_TAG_template_value_param)
13229 {
13230 struct symbol *arg = new_symbol (child_die, NULL, cu);
13231
13232 if (arg != NULL)
13233 template_args.push_back (arg);
13234 }
13235 else
13236 process_die (child_die, cu);
13237 child_die = child_die->sibling;
13238 }
13239 }
13240
13241 inherit_abstract_dies (die, cu);
13242
13243 /* If we have a DW_AT_specification, we might need to import using
13244 directives from the context of the specification DIE. See the
13245 comment in determine_prefix. */
13246 if (cu->language == language_cplus
13247 && dwarf2_attr (die, DW_AT_specification, cu))
13248 {
13249 struct dwarf2_cu *spec_cu = cu;
13250 struct die_info *spec_die = die_specification (die, &spec_cu);
13251
13252 while (spec_die)
13253 {
13254 child_die = spec_die->child;
13255 while (child_die && child_die->tag)
13256 {
13257 if (child_die->tag == DW_TAG_imported_module)
13258 process_die (child_die, spec_cu);
13259 child_die = child_die->sibling;
13260 }
13261
13262 /* In some cases, GCC generates specification DIEs that
13263 themselves contain DW_AT_specification attributes. */
13264 spec_die = die_specification (spec_die, &spec_cu);
13265 }
13266 }
13267
13268 struct context_stack cstk = cu->get_builder ()->pop_context ();
13269 /* Make a block for the local symbols within. */
13270 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13271 cstk.static_link, lowpc, highpc);
13272
13273 /* For C++, set the block's scope. */
13274 if ((cu->language == language_cplus
13275 || cu->language == language_fortran
13276 || cu->language == language_d
13277 || cu->language == language_rust)
13278 && cu->processing_has_namespace_info)
13279 block_set_scope (block, determine_prefix (die, cu),
13280 &objfile->objfile_obstack);
13281
13282 /* If we have address ranges, record them. */
13283 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13284
13285 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13286
13287 /* Attach template arguments to function. */
13288 if (!template_args.empty ())
13289 {
13290 gdb_assert (templ_func != NULL);
13291
13292 templ_func->n_template_arguments = template_args.size ();
13293 templ_func->template_arguments
13294 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13295 templ_func->n_template_arguments);
13296 memcpy (templ_func->template_arguments,
13297 template_args.data (),
13298 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13299
13300 /* Make sure that the symtab is set on the new symbols. Even
13301 though they don't appear in this symtab directly, other parts
13302 of gdb assume that symbols do, and this is reasonably
13303 true. */
13304 for (symbol *sym : template_args)
13305 symbol_set_symtab (sym, symbol_symtab (templ_func));
13306 }
13307
13308 /* In C++, we can have functions nested inside functions (e.g., when
13309 a function declares a class that has methods). This means that
13310 when we finish processing a function scope, we may need to go
13311 back to building a containing block's symbol lists. */
13312 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13313 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13314
13315 /* If we've finished processing a top-level function, subsequent
13316 symbols go in the file symbol list. */
13317 if (cu->get_builder ()->outermost_context_p ())
13318 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13319 }
13320
13321 /* Process all the DIES contained within a lexical block scope. Start
13322 a new scope, process the dies, and then close the scope. */
13323
13324 static void
13325 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13326 {
13327 struct objfile *objfile = cu->per_objfile->objfile;
13328 struct gdbarch *gdbarch = objfile->arch ();
13329 CORE_ADDR lowpc, highpc;
13330 struct die_info *child_die;
13331 CORE_ADDR baseaddr;
13332
13333 baseaddr = objfile->text_section_offset ();
13334
13335 /* Ignore blocks with missing or invalid low and high pc attributes. */
13336 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13337 as multiple lexical blocks? Handling children in a sane way would
13338 be nasty. Might be easier to properly extend generic blocks to
13339 describe ranges. */
13340 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13341 {
13342 case PC_BOUNDS_NOT_PRESENT:
13343 /* DW_TAG_lexical_block has no attributes, process its children as if
13344 there was no wrapping by that DW_TAG_lexical_block.
13345 GCC does no longer produces such DWARF since GCC r224161. */
13346 for (child_die = die->child;
13347 child_die != NULL && child_die->tag;
13348 child_die = child_die->sibling)
13349 {
13350 /* We might already be processing this DIE. This can happen
13351 in an unusual circumstance -- where a subroutine A
13352 appears lexically in another subroutine B, but A actually
13353 inlines B. The recursion is broken here, rather than in
13354 inherit_abstract_dies, because it seems better to simply
13355 drop concrete children here. */
13356 if (!child_die->in_process)
13357 process_die (child_die, cu);
13358 }
13359 return;
13360 case PC_BOUNDS_INVALID:
13361 return;
13362 }
13363 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13364 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13365
13366 cu->get_builder ()->push_context (0, lowpc);
13367 if (die->child != NULL)
13368 {
13369 child_die = die->child;
13370 while (child_die && child_die->tag)
13371 {
13372 process_die (child_die, cu);
13373 child_die = child_die->sibling;
13374 }
13375 }
13376 inherit_abstract_dies (die, cu);
13377 struct context_stack cstk = cu->get_builder ()->pop_context ();
13378
13379 if (*cu->get_builder ()->get_local_symbols () != NULL
13380 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13381 {
13382 struct block *block
13383 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13384 cstk.start_addr, highpc);
13385
13386 /* Note that recording ranges after traversing children, as we
13387 do here, means that recording a parent's ranges entails
13388 walking across all its children's ranges as they appear in
13389 the address map, which is quadratic behavior.
13390
13391 It would be nicer to record the parent's ranges before
13392 traversing its children, simply overriding whatever you find
13393 there. But since we don't even decide whether to create a
13394 block until after we've traversed its children, that's hard
13395 to do. */
13396 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13397 }
13398 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13399 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13400 }
13401
13402 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13403
13404 static void
13405 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13406 {
13407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13408 struct objfile *objfile = per_objfile->objfile;
13409 struct gdbarch *gdbarch = objfile->arch ();
13410 CORE_ADDR pc, baseaddr;
13411 struct attribute *attr;
13412 struct call_site *call_site, call_site_local;
13413 void **slot;
13414 int nparams;
13415 struct die_info *child_die;
13416
13417 baseaddr = objfile->text_section_offset ();
13418
13419 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13420 if (attr == NULL)
13421 {
13422 /* This was a pre-DWARF-5 GNU extension alias
13423 for DW_AT_call_return_pc. */
13424 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13425 }
13426 if (!attr)
13427 {
13428 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13429 "DIE %s [in module %s]"),
13430 sect_offset_str (die->sect_off), objfile_name (objfile));
13431 return;
13432 }
13433 pc = attr->value_as_address () + baseaddr;
13434 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13435
13436 if (cu->call_site_htab == NULL)
13437 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13438 NULL, &objfile->objfile_obstack,
13439 hashtab_obstack_allocate, NULL);
13440 call_site_local.pc = pc;
13441 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13442 if (*slot != NULL)
13443 {
13444 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13445 "DIE %s [in module %s]"),
13446 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13447 objfile_name (objfile));
13448 return;
13449 }
13450
13451 /* Count parameters at the caller. */
13452
13453 nparams = 0;
13454 for (child_die = die->child; child_die && child_die->tag;
13455 child_die = child_die->sibling)
13456 {
13457 if (child_die->tag != DW_TAG_call_site_parameter
13458 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13459 {
13460 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13461 "DW_TAG_call_site child DIE %s [in module %s]"),
13462 child_die->tag, sect_offset_str (child_die->sect_off),
13463 objfile_name (objfile));
13464 continue;
13465 }
13466
13467 nparams++;
13468 }
13469
13470 call_site
13471 = ((struct call_site *)
13472 obstack_alloc (&objfile->objfile_obstack,
13473 sizeof (*call_site)
13474 + (sizeof (*call_site->parameter) * (nparams - 1))));
13475 *slot = call_site;
13476 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13477 call_site->pc = pc;
13478
13479 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13480 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13481 {
13482 struct die_info *func_die;
13483
13484 /* Skip also over DW_TAG_inlined_subroutine. */
13485 for (func_die = die->parent;
13486 func_die && func_die->tag != DW_TAG_subprogram
13487 && func_die->tag != DW_TAG_subroutine_type;
13488 func_die = func_die->parent);
13489
13490 /* DW_AT_call_all_calls is a superset
13491 of DW_AT_call_all_tail_calls. */
13492 if (func_die
13493 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13494 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13495 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13496 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13497 {
13498 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13499 not complete. But keep CALL_SITE for look ups via call_site_htab,
13500 both the initial caller containing the real return address PC and
13501 the final callee containing the current PC of a chain of tail
13502 calls do not need to have the tail call list complete. But any
13503 function candidate for a virtual tail call frame searched via
13504 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13505 determined unambiguously. */
13506 }
13507 else
13508 {
13509 struct type *func_type = NULL;
13510
13511 if (func_die)
13512 func_type = get_die_type (func_die, cu);
13513 if (func_type != NULL)
13514 {
13515 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13516
13517 /* Enlist this call site to the function. */
13518 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13519 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13520 }
13521 else
13522 complaint (_("Cannot find function owning DW_TAG_call_site "
13523 "DIE %s [in module %s]"),
13524 sect_offset_str (die->sect_off), objfile_name (objfile));
13525 }
13526 }
13527
13528 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13529 if (attr == NULL)
13530 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13531 if (attr == NULL)
13532 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13533 if (attr == NULL)
13534 {
13535 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13536 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13537 }
13538 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13539 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13540 /* Keep NULL DWARF_BLOCK. */;
13541 else if (attr->form_is_block ())
13542 {
13543 struct dwarf2_locexpr_baton *dlbaton;
13544
13545 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13546 dlbaton->data = DW_BLOCK (attr)->data;
13547 dlbaton->size = DW_BLOCK (attr)->size;
13548 dlbaton->per_objfile = per_objfile;
13549 dlbaton->per_cu = cu->per_cu;
13550
13551 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13552 }
13553 else if (attr->form_is_ref ())
13554 {
13555 struct dwarf2_cu *target_cu = cu;
13556 struct die_info *target_die;
13557
13558 target_die = follow_die_ref (die, attr, &target_cu);
13559 gdb_assert (target_cu->per_objfile->objfile == objfile);
13560 if (die_is_declaration (target_die, target_cu))
13561 {
13562 const char *target_physname;
13563
13564 /* Prefer the mangled name; otherwise compute the demangled one. */
13565 target_physname = dw2_linkage_name (target_die, target_cu);
13566 if (target_physname == NULL)
13567 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13568 if (target_physname == NULL)
13569 complaint (_("DW_AT_call_target target DIE has invalid "
13570 "physname, for referencing DIE %s [in module %s]"),
13571 sect_offset_str (die->sect_off), objfile_name (objfile));
13572 else
13573 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13574 }
13575 else
13576 {
13577 CORE_ADDR lowpc;
13578
13579 /* DW_AT_entry_pc should be preferred. */
13580 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13581 <= PC_BOUNDS_INVALID)
13582 complaint (_("DW_AT_call_target target DIE has invalid "
13583 "low pc, for referencing DIE %s [in module %s]"),
13584 sect_offset_str (die->sect_off), objfile_name (objfile));
13585 else
13586 {
13587 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13588 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13589 }
13590 }
13591 }
13592 else
13593 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13594 "block nor reference, for DIE %s [in module %s]"),
13595 sect_offset_str (die->sect_off), objfile_name (objfile));
13596
13597 call_site->per_cu = cu->per_cu;
13598 call_site->per_objfile = per_objfile;
13599
13600 for (child_die = die->child;
13601 child_die && child_die->tag;
13602 child_die = child_die->sibling)
13603 {
13604 struct call_site_parameter *parameter;
13605 struct attribute *loc, *origin;
13606
13607 if (child_die->tag != DW_TAG_call_site_parameter
13608 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13609 {
13610 /* Already printed the complaint above. */
13611 continue;
13612 }
13613
13614 gdb_assert (call_site->parameter_count < nparams);
13615 parameter = &call_site->parameter[call_site->parameter_count];
13616
13617 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13618 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13619 register is contained in DW_AT_call_value. */
13620
13621 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13622 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13623 if (origin == NULL)
13624 {
13625 /* This was a pre-DWARF-5 GNU extension alias
13626 for DW_AT_call_parameter. */
13627 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13628 }
13629 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13630 {
13631 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13632
13633 sect_offset sect_off = origin->get_ref_die_offset ();
13634 if (!cu->header.offset_in_cu_p (sect_off))
13635 {
13636 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13637 binding can be done only inside one CU. Such referenced DIE
13638 therefore cannot be even moved to DW_TAG_partial_unit. */
13639 complaint (_("DW_AT_call_parameter offset is not in CU for "
13640 "DW_TAG_call_site child DIE %s [in module %s]"),
13641 sect_offset_str (child_die->sect_off),
13642 objfile_name (objfile));
13643 continue;
13644 }
13645 parameter->u.param_cu_off
13646 = (cu_offset) (sect_off - cu->header.sect_off);
13647 }
13648 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13649 {
13650 complaint (_("No DW_FORM_block* DW_AT_location for "
13651 "DW_TAG_call_site child DIE %s [in module %s]"),
13652 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13653 continue;
13654 }
13655 else
13656 {
13657 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13658 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13659 if (parameter->u.dwarf_reg != -1)
13660 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13661 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13662 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13663 &parameter->u.fb_offset))
13664 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13665 else
13666 {
13667 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13668 "for DW_FORM_block* DW_AT_location is supported for "
13669 "DW_TAG_call_site child DIE %s "
13670 "[in module %s]"),
13671 sect_offset_str (child_die->sect_off),
13672 objfile_name (objfile));
13673 continue;
13674 }
13675 }
13676
13677 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13678 if (attr == NULL)
13679 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13680 if (attr == NULL || !attr->form_is_block ())
13681 {
13682 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13683 "DW_TAG_call_site child DIE %s [in module %s]"),
13684 sect_offset_str (child_die->sect_off),
13685 objfile_name (objfile));
13686 continue;
13687 }
13688 parameter->value = DW_BLOCK (attr)->data;
13689 parameter->value_size = DW_BLOCK (attr)->size;
13690
13691 /* Parameters are not pre-cleared by memset above. */
13692 parameter->data_value = NULL;
13693 parameter->data_value_size = 0;
13694 call_site->parameter_count++;
13695
13696 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13697 if (attr == NULL)
13698 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13699 if (attr != nullptr)
13700 {
13701 if (!attr->form_is_block ())
13702 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13703 "DW_TAG_call_site child DIE %s [in module %s]"),
13704 sect_offset_str (child_die->sect_off),
13705 objfile_name (objfile));
13706 else
13707 {
13708 parameter->data_value = DW_BLOCK (attr)->data;
13709 parameter->data_value_size = DW_BLOCK (attr)->size;
13710 }
13711 }
13712 }
13713 }
13714
13715 /* Helper function for read_variable. If DIE represents a virtual
13716 table, then return the type of the concrete object that is
13717 associated with the virtual table. Otherwise, return NULL. */
13718
13719 static struct type *
13720 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13721 {
13722 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13723 if (attr == NULL)
13724 return NULL;
13725
13726 /* Find the type DIE. */
13727 struct die_info *type_die = NULL;
13728 struct dwarf2_cu *type_cu = cu;
13729
13730 if (attr->form_is_ref ())
13731 type_die = follow_die_ref (die, attr, &type_cu);
13732 if (type_die == NULL)
13733 return NULL;
13734
13735 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13736 return NULL;
13737 return die_containing_type (type_die, type_cu);
13738 }
13739
13740 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13741
13742 static void
13743 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13744 {
13745 struct rust_vtable_symbol *storage = NULL;
13746
13747 if (cu->language == language_rust)
13748 {
13749 struct type *containing_type = rust_containing_type (die, cu);
13750
13751 if (containing_type != NULL)
13752 {
13753 struct objfile *objfile = cu->per_objfile->objfile;
13754
13755 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13756 storage->concrete_type = containing_type;
13757 storage->subclass = SYMBOL_RUST_VTABLE;
13758 }
13759 }
13760
13761 struct symbol *res = new_symbol (die, NULL, cu, storage);
13762 struct attribute *abstract_origin
13763 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13764 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13765 if (res == NULL && loc && abstract_origin)
13766 {
13767 /* We have a variable without a name, but with a location and an abstract
13768 origin. This may be a concrete instance of an abstract variable
13769 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13770 later. */
13771 struct dwarf2_cu *origin_cu = cu;
13772 struct die_info *origin_die
13773 = follow_die_ref (die, abstract_origin, &origin_cu);
13774 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13775 per_objfile->per_bfd->abstract_to_concrete
13776 [origin_die->sect_off].push_back (die->sect_off);
13777 }
13778 }
13779
13780 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13781 reading .debug_rnglists.
13782 Callback's type should be:
13783 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13784 Return true if the attributes are present and valid, otherwise,
13785 return false. */
13786
13787 template <typename Callback>
13788 static bool
13789 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13790 Callback &&callback)
13791 {
13792 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13793 struct objfile *objfile = dwarf2_per_objfile->objfile;
13794 bfd *obfd = objfile->obfd;
13795 /* Base address selection entry. */
13796 gdb::optional<CORE_ADDR> base;
13797 const gdb_byte *buffer;
13798 CORE_ADDR baseaddr;
13799 bool overflow = false;
13800
13801 base = cu->base_address;
13802
13803 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13804 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13805 {
13806 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13807 offset);
13808 return false;
13809 }
13810 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13811
13812 baseaddr = objfile->text_section_offset ();
13813
13814 while (1)
13815 {
13816 /* Initialize it due to a false compiler warning. */
13817 CORE_ADDR range_beginning = 0, range_end = 0;
13818 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13819 + dwarf2_per_objfile->per_bfd->rnglists.size);
13820 unsigned int bytes_read;
13821
13822 if (buffer == buf_end)
13823 {
13824 overflow = true;
13825 break;
13826 }
13827 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13828 switch (rlet)
13829 {
13830 case DW_RLE_end_of_list:
13831 break;
13832 case DW_RLE_base_address:
13833 if (buffer + cu->header.addr_size > buf_end)
13834 {
13835 overflow = true;
13836 break;
13837 }
13838 base = cu->header.read_address (obfd, buffer, &bytes_read);
13839 buffer += bytes_read;
13840 break;
13841 case DW_RLE_start_length:
13842 if (buffer + cu->header.addr_size > buf_end)
13843 {
13844 overflow = true;
13845 break;
13846 }
13847 range_beginning = cu->header.read_address (obfd, buffer,
13848 &bytes_read);
13849 buffer += bytes_read;
13850 range_end = (range_beginning
13851 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13852 buffer += bytes_read;
13853 if (buffer > buf_end)
13854 {
13855 overflow = true;
13856 break;
13857 }
13858 break;
13859 case DW_RLE_offset_pair:
13860 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13861 buffer += bytes_read;
13862 if (buffer > buf_end)
13863 {
13864 overflow = true;
13865 break;
13866 }
13867 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13868 buffer += bytes_read;
13869 if (buffer > buf_end)
13870 {
13871 overflow = true;
13872 break;
13873 }
13874 break;
13875 case DW_RLE_start_end:
13876 if (buffer + 2 * cu->header.addr_size > buf_end)
13877 {
13878 overflow = true;
13879 break;
13880 }
13881 range_beginning = cu->header.read_address (obfd, buffer,
13882 &bytes_read);
13883 buffer += bytes_read;
13884 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13885 buffer += bytes_read;
13886 break;
13887 default:
13888 complaint (_("Invalid .debug_rnglists data (no base address)"));
13889 return false;
13890 }
13891 if (rlet == DW_RLE_end_of_list || overflow)
13892 break;
13893 if (rlet == DW_RLE_base_address)
13894 continue;
13895
13896 if (!base.has_value ())
13897 {
13898 /* We have no valid base address for the ranges
13899 data. */
13900 complaint (_("Invalid .debug_rnglists data (no base address)"));
13901 return false;
13902 }
13903
13904 if (range_beginning > range_end)
13905 {
13906 /* Inverted range entries are invalid. */
13907 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13908 return false;
13909 }
13910
13911 /* Empty range entries have no effect. */
13912 if (range_beginning == range_end)
13913 continue;
13914
13915 range_beginning += *base;
13916 range_end += *base;
13917
13918 /* A not-uncommon case of bad debug info.
13919 Don't pollute the addrmap with bad data. */
13920 if (range_beginning + baseaddr == 0
13921 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13922 {
13923 complaint (_(".debug_rnglists entry has start address of zero"
13924 " [in module %s]"), objfile_name (objfile));
13925 continue;
13926 }
13927
13928 callback (range_beginning, range_end);
13929 }
13930
13931 if (overflow)
13932 {
13933 complaint (_("Offset %d is not terminated "
13934 "for DW_AT_ranges attribute"),
13935 offset);
13936 return false;
13937 }
13938
13939 return true;
13940 }
13941
13942 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13943 Callback's type should be:
13944 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13945 Return 1 if the attributes are present and valid, otherwise, return 0. */
13946
13947 template <typename Callback>
13948 static int
13949 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13950 Callback &&callback)
13951 {
13952 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13953 struct objfile *objfile = per_objfile->objfile;
13954 struct comp_unit_head *cu_header = &cu->header;
13955 bfd *obfd = objfile->obfd;
13956 unsigned int addr_size = cu_header->addr_size;
13957 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13958 /* Base address selection entry. */
13959 gdb::optional<CORE_ADDR> base;
13960 unsigned int dummy;
13961 const gdb_byte *buffer;
13962 CORE_ADDR baseaddr;
13963
13964 if (cu_header->version >= 5)
13965 return dwarf2_rnglists_process (offset, cu, callback);
13966
13967 base = cu->base_address;
13968
13969 per_objfile->per_bfd->ranges.read (objfile);
13970 if (offset >= per_objfile->per_bfd->ranges.size)
13971 {
13972 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13973 offset);
13974 return 0;
13975 }
13976 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13977
13978 baseaddr = objfile->text_section_offset ();
13979
13980 while (1)
13981 {
13982 CORE_ADDR range_beginning, range_end;
13983
13984 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13985 buffer += addr_size;
13986 range_end = cu->header.read_address (obfd, buffer, &dummy);
13987 buffer += addr_size;
13988 offset += 2 * addr_size;
13989
13990 /* An end of list marker is a pair of zero addresses. */
13991 if (range_beginning == 0 && range_end == 0)
13992 /* Found the end of list entry. */
13993 break;
13994
13995 /* Each base address selection entry is a pair of 2 values.
13996 The first is the largest possible address, the second is
13997 the base address. Check for a base address here. */
13998 if ((range_beginning & mask) == mask)
13999 {
14000 /* If we found the largest possible address, then we already
14001 have the base address in range_end. */
14002 base = range_end;
14003 continue;
14004 }
14005
14006 if (!base.has_value ())
14007 {
14008 /* We have no valid base address for the ranges
14009 data. */
14010 complaint (_("Invalid .debug_ranges data (no base address)"));
14011 return 0;
14012 }
14013
14014 if (range_beginning > range_end)
14015 {
14016 /* Inverted range entries are invalid. */
14017 complaint (_("Invalid .debug_ranges data (inverted range)"));
14018 return 0;
14019 }
14020
14021 /* Empty range entries have no effect. */
14022 if (range_beginning == range_end)
14023 continue;
14024
14025 range_beginning += *base;
14026 range_end += *base;
14027
14028 /* A not-uncommon case of bad debug info.
14029 Don't pollute the addrmap with bad data. */
14030 if (range_beginning + baseaddr == 0
14031 && !per_objfile->per_bfd->has_section_at_zero)
14032 {
14033 complaint (_(".debug_ranges entry has start address of zero"
14034 " [in module %s]"), objfile_name (objfile));
14035 continue;
14036 }
14037
14038 callback (range_beginning, range_end);
14039 }
14040
14041 return 1;
14042 }
14043
14044 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14045 Return 1 if the attributes are present and valid, otherwise, return 0.
14046 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14047
14048 static int
14049 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14050 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14051 dwarf2_psymtab *ranges_pst)
14052 {
14053 struct objfile *objfile = cu->per_objfile->objfile;
14054 struct gdbarch *gdbarch = objfile->arch ();
14055 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14056 int low_set = 0;
14057 CORE_ADDR low = 0;
14058 CORE_ADDR high = 0;
14059 int retval;
14060
14061 retval = dwarf2_ranges_process (offset, cu,
14062 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14063 {
14064 if (ranges_pst != NULL)
14065 {
14066 CORE_ADDR lowpc;
14067 CORE_ADDR highpc;
14068
14069 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14070 range_beginning + baseaddr)
14071 - baseaddr);
14072 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14073 range_end + baseaddr)
14074 - baseaddr);
14075 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14076 lowpc, highpc - 1, ranges_pst);
14077 }
14078
14079 /* FIXME: This is recording everything as a low-high
14080 segment of consecutive addresses. We should have a
14081 data structure for discontiguous block ranges
14082 instead. */
14083 if (! low_set)
14084 {
14085 low = range_beginning;
14086 high = range_end;
14087 low_set = 1;
14088 }
14089 else
14090 {
14091 if (range_beginning < low)
14092 low = range_beginning;
14093 if (range_end > high)
14094 high = range_end;
14095 }
14096 });
14097 if (!retval)
14098 return 0;
14099
14100 if (! low_set)
14101 /* If the first entry is an end-of-list marker, the range
14102 describes an empty scope, i.e. no instructions. */
14103 return 0;
14104
14105 if (low_return)
14106 *low_return = low;
14107 if (high_return)
14108 *high_return = high;
14109 return 1;
14110 }
14111
14112 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14113 definition for the return value. *LOWPC and *HIGHPC are set iff
14114 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14115
14116 static enum pc_bounds_kind
14117 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14118 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14119 dwarf2_psymtab *pst)
14120 {
14121 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14122 struct attribute *attr;
14123 struct attribute *attr_high;
14124 CORE_ADDR low = 0;
14125 CORE_ADDR high = 0;
14126 enum pc_bounds_kind ret;
14127
14128 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14129 if (attr_high)
14130 {
14131 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14132 if (attr != nullptr)
14133 {
14134 low = attr->value_as_address ();
14135 high = attr_high->value_as_address ();
14136 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14137 high += low;
14138 }
14139 else
14140 /* Found high w/o low attribute. */
14141 return PC_BOUNDS_INVALID;
14142
14143 /* Found consecutive range of addresses. */
14144 ret = PC_BOUNDS_HIGH_LOW;
14145 }
14146 else
14147 {
14148 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14149 if (attr != NULL)
14150 {
14151 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14152 We take advantage of the fact that DW_AT_ranges does not appear
14153 in DW_TAG_compile_unit of DWO files. */
14154 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14155 unsigned int ranges_offset = (DW_UNSND (attr)
14156 + (need_ranges_base
14157 ? cu->ranges_base
14158 : 0));
14159
14160 /* Value of the DW_AT_ranges attribute is the offset in the
14161 .debug_ranges section. */
14162 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14163 return PC_BOUNDS_INVALID;
14164 /* Found discontinuous range of addresses. */
14165 ret = PC_BOUNDS_RANGES;
14166 }
14167 else
14168 return PC_BOUNDS_NOT_PRESENT;
14169 }
14170
14171 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14172 if (high <= low)
14173 return PC_BOUNDS_INVALID;
14174
14175 /* When using the GNU linker, .gnu.linkonce. sections are used to
14176 eliminate duplicate copies of functions and vtables and such.
14177 The linker will arbitrarily choose one and discard the others.
14178 The AT_*_pc values for such functions refer to local labels in
14179 these sections. If the section from that file was discarded, the
14180 labels are not in the output, so the relocs get a value of 0.
14181 If this is a discarded function, mark the pc bounds as invalid,
14182 so that GDB will ignore it. */
14183 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14184 return PC_BOUNDS_INVALID;
14185
14186 *lowpc = low;
14187 if (highpc)
14188 *highpc = high;
14189 return ret;
14190 }
14191
14192 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14193 its low and high PC addresses. Do nothing if these addresses could not
14194 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14195 and HIGHPC to the high address if greater than HIGHPC. */
14196
14197 static void
14198 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14199 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14200 struct dwarf2_cu *cu)
14201 {
14202 CORE_ADDR low, high;
14203 struct die_info *child = die->child;
14204
14205 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14206 {
14207 *lowpc = std::min (*lowpc, low);
14208 *highpc = std::max (*highpc, high);
14209 }
14210
14211 /* If the language does not allow nested subprograms (either inside
14212 subprograms or lexical blocks), we're done. */
14213 if (cu->language != language_ada)
14214 return;
14215
14216 /* Check all the children of the given DIE. If it contains nested
14217 subprograms, then check their pc bounds. Likewise, we need to
14218 check lexical blocks as well, as they may also contain subprogram
14219 definitions. */
14220 while (child && child->tag)
14221 {
14222 if (child->tag == DW_TAG_subprogram
14223 || child->tag == DW_TAG_lexical_block)
14224 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14225 child = child->sibling;
14226 }
14227 }
14228
14229 /* Get the low and high pc's represented by the scope DIE, and store
14230 them in *LOWPC and *HIGHPC. If the correct values can't be
14231 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14232
14233 static void
14234 get_scope_pc_bounds (struct die_info *die,
14235 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14236 struct dwarf2_cu *cu)
14237 {
14238 CORE_ADDR best_low = (CORE_ADDR) -1;
14239 CORE_ADDR best_high = (CORE_ADDR) 0;
14240 CORE_ADDR current_low, current_high;
14241
14242 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14243 >= PC_BOUNDS_RANGES)
14244 {
14245 best_low = current_low;
14246 best_high = current_high;
14247 }
14248 else
14249 {
14250 struct die_info *child = die->child;
14251
14252 while (child && child->tag)
14253 {
14254 switch (child->tag) {
14255 case DW_TAG_subprogram:
14256 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14257 break;
14258 case DW_TAG_namespace:
14259 case DW_TAG_module:
14260 /* FIXME: carlton/2004-01-16: Should we do this for
14261 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14262 that current GCC's always emit the DIEs corresponding
14263 to definitions of methods of classes as children of a
14264 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14265 the DIEs giving the declarations, which could be
14266 anywhere). But I don't see any reason why the
14267 standards says that they have to be there. */
14268 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14269
14270 if (current_low != ((CORE_ADDR) -1))
14271 {
14272 best_low = std::min (best_low, current_low);
14273 best_high = std::max (best_high, current_high);
14274 }
14275 break;
14276 default:
14277 /* Ignore. */
14278 break;
14279 }
14280
14281 child = child->sibling;
14282 }
14283 }
14284
14285 *lowpc = best_low;
14286 *highpc = best_high;
14287 }
14288
14289 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14290 in DIE. */
14291
14292 static void
14293 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14294 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14295 {
14296 struct objfile *objfile = cu->per_objfile->objfile;
14297 struct gdbarch *gdbarch = objfile->arch ();
14298 struct attribute *attr;
14299 struct attribute *attr_high;
14300
14301 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14302 if (attr_high)
14303 {
14304 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14305 if (attr != nullptr)
14306 {
14307 CORE_ADDR low = attr->value_as_address ();
14308 CORE_ADDR high = attr_high->value_as_address ();
14309
14310 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14311 high += low;
14312
14313 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14314 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14315 cu->get_builder ()->record_block_range (block, low, high - 1);
14316 }
14317 }
14318
14319 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14320 if (attr != nullptr)
14321 {
14322 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14323 We take advantage of the fact that DW_AT_ranges does not appear
14324 in DW_TAG_compile_unit of DWO files. */
14325 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14326
14327 /* The value of the DW_AT_ranges attribute is the offset of the
14328 address range list in the .debug_ranges section. */
14329 unsigned long offset = (DW_UNSND (attr)
14330 + (need_ranges_base ? cu->ranges_base : 0));
14331
14332 std::vector<blockrange> blockvec;
14333 dwarf2_ranges_process (offset, cu,
14334 [&] (CORE_ADDR start, CORE_ADDR end)
14335 {
14336 start += baseaddr;
14337 end += baseaddr;
14338 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14339 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14340 cu->get_builder ()->record_block_range (block, start, end - 1);
14341 blockvec.emplace_back (start, end);
14342 });
14343
14344 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14345 }
14346 }
14347
14348 /* Check whether the producer field indicates either of GCC < 4.6, or the
14349 Intel C/C++ compiler, and cache the result in CU. */
14350
14351 static void
14352 check_producer (struct dwarf2_cu *cu)
14353 {
14354 int major, minor;
14355
14356 if (cu->producer == NULL)
14357 {
14358 /* For unknown compilers expect their behavior is DWARF version
14359 compliant.
14360
14361 GCC started to support .debug_types sections by -gdwarf-4 since
14362 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14363 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14364 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14365 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14366 }
14367 else if (producer_is_gcc (cu->producer, &major, &minor))
14368 {
14369 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14370 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14371 }
14372 else if (producer_is_icc (cu->producer, &major, &minor))
14373 {
14374 cu->producer_is_icc = true;
14375 cu->producer_is_icc_lt_14 = major < 14;
14376 }
14377 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14378 cu->producer_is_codewarrior = true;
14379 else
14380 {
14381 /* For other non-GCC compilers, expect their behavior is DWARF version
14382 compliant. */
14383 }
14384
14385 cu->checked_producer = true;
14386 }
14387
14388 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14389 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14390 during 4.6.0 experimental. */
14391
14392 static bool
14393 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14394 {
14395 if (!cu->checked_producer)
14396 check_producer (cu);
14397
14398 return cu->producer_is_gxx_lt_4_6;
14399 }
14400
14401
14402 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14403 with incorrect is_stmt attributes. */
14404
14405 static bool
14406 producer_is_codewarrior (struct dwarf2_cu *cu)
14407 {
14408 if (!cu->checked_producer)
14409 check_producer (cu);
14410
14411 return cu->producer_is_codewarrior;
14412 }
14413
14414 /* Return the default accessibility type if it is not overridden by
14415 DW_AT_accessibility. */
14416
14417 static enum dwarf_access_attribute
14418 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14419 {
14420 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14421 {
14422 /* The default DWARF 2 accessibility for members is public, the default
14423 accessibility for inheritance is private. */
14424
14425 if (die->tag != DW_TAG_inheritance)
14426 return DW_ACCESS_public;
14427 else
14428 return DW_ACCESS_private;
14429 }
14430 else
14431 {
14432 /* DWARF 3+ defines the default accessibility a different way. The same
14433 rules apply now for DW_TAG_inheritance as for the members and it only
14434 depends on the container kind. */
14435
14436 if (die->parent->tag == DW_TAG_class_type)
14437 return DW_ACCESS_private;
14438 else
14439 return DW_ACCESS_public;
14440 }
14441 }
14442
14443 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14444 offset. If the attribute was not found return 0, otherwise return
14445 1. If it was found but could not properly be handled, set *OFFSET
14446 to 0. */
14447
14448 static int
14449 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14450 LONGEST *offset)
14451 {
14452 struct attribute *attr;
14453
14454 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14455 if (attr != NULL)
14456 {
14457 *offset = 0;
14458
14459 /* Note that we do not check for a section offset first here.
14460 This is because DW_AT_data_member_location is new in DWARF 4,
14461 so if we see it, we can assume that a constant form is really
14462 a constant and not a section offset. */
14463 if (attr->form_is_constant ())
14464 *offset = attr->constant_value (0);
14465 else if (attr->form_is_section_offset ())
14466 dwarf2_complex_location_expr_complaint ();
14467 else if (attr->form_is_block ())
14468 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14469 else
14470 dwarf2_complex_location_expr_complaint ();
14471
14472 return 1;
14473 }
14474
14475 return 0;
14476 }
14477
14478 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14479
14480 static void
14481 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14482 struct field *field)
14483 {
14484 struct attribute *attr;
14485
14486 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14487 if (attr != NULL)
14488 {
14489 if (attr->form_is_constant ())
14490 {
14491 LONGEST offset = attr->constant_value (0);
14492 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14493 }
14494 else if (attr->form_is_section_offset ())
14495 dwarf2_complex_location_expr_complaint ();
14496 else if (attr->form_is_block ())
14497 {
14498 bool handled;
14499 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14500 if (handled)
14501 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14502 else
14503 {
14504 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14505 struct objfile *objfile = per_objfile->objfile;
14506 struct dwarf2_locexpr_baton *dlbaton
14507 = XOBNEW (&objfile->objfile_obstack,
14508 struct dwarf2_locexpr_baton);
14509 dlbaton->data = DW_BLOCK (attr)->data;
14510 dlbaton->size = DW_BLOCK (attr)->size;
14511 /* When using this baton, we want to compute the address
14512 of the field, not the value. This is why
14513 is_reference is set to false here. */
14514 dlbaton->is_reference = false;
14515 dlbaton->per_objfile = per_objfile;
14516 dlbaton->per_cu = cu->per_cu;
14517
14518 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14519 }
14520 }
14521 else
14522 dwarf2_complex_location_expr_complaint ();
14523 }
14524 }
14525
14526 /* Add an aggregate field to the field list. */
14527
14528 static void
14529 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14530 struct dwarf2_cu *cu)
14531 {
14532 struct objfile *objfile = cu->per_objfile->objfile;
14533 struct gdbarch *gdbarch = objfile->arch ();
14534 struct nextfield *new_field;
14535 struct attribute *attr;
14536 struct field *fp;
14537 const char *fieldname = "";
14538
14539 if (die->tag == DW_TAG_inheritance)
14540 {
14541 fip->baseclasses.emplace_back ();
14542 new_field = &fip->baseclasses.back ();
14543 }
14544 else
14545 {
14546 fip->fields.emplace_back ();
14547 new_field = &fip->fields.back ();
14548 }
14549
14550 new_field->offset = die->sect_off;
14551
14552 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14553 if (attr != nullptr)
14554 new_field->accessibility = DW_UNSND (attr);
14555 else
14556 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14557 if (new_field->accessibility != DW_ACCESS_public)
14558 fip->non_public_fields = 1;
14559
14560 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14561 if (attr != nullptr)
14562 new_field->virtuality = DW_UNSND (attr);
14563 else
14564 new_field->virtuality = DW_VIRTUALITY_none;
14565
14566 fp = &new_field->field;
14567
14568 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14569 {
14570 /* Data member other than a C++ static data member. */
14571
14572 /* Get type of field. */
14573 fp->type = die_type (die, cu);
14574
14575 SET_FIELD_BITPOS (*fp, 0);
14576
14577 /* Get bit size of field (zero if none). */
14578 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14579 if (attr != nullptr)
14580 {
14581 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14582 }
14583 else
14584 {
14585 FIELD_BITSIZE (*fp) = 0;
14586 }
14587
14588 /* Get bit offset of field. */
14589 handle_data_member_location (die, cu, fp);
14590 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14591 if (attr != nullptr)
14592 {
14593 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14594 {
14595 /* For big endian bits, the DW_AT_bit_offset gives the
14596 additional bit offset from the MSB of the containing
14597 anonymous object to the MSB of the field. We don't
14598 have to do anything special since we don't need to
14599 know the size of the anonymous object. */
14600 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14601 }
14602 else
14603 {
14604 /* For little endian bits, compute the bit offset to the
14605 MSB of the anonymous object, subtract off the number of
14606 bits from the MSB of the field to the MSB of the
14607 object, and then subtract off the number of bits of
14608 the field itself. The result is the bit offset of
14609 the LSB of the field. */
14610 int anonymous_size;
14611 int bit_offset = DW_UNSND (attr);
14612
14613 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14614 if (attr != nullptr)
14615 {
14616 /* The size of the anonymous object containing
14617 the bit field is explicit, so use the
14618 indicated size (in bytes). */
14619 anonymous_size = DW_UNSND (attr);
14620 }
14621 else
14622 {
14623 /* The size of the anonymous object containing
14624 the bit field must be inferred from the type
14625 attribute of the data member containing the
14626 bit field. */
14627 anonymous_size = TYPE_LENGTH (fp->type);
14628 }
14629 SET_FIELD_BITPOS (*fp,
14630 (FIELD_BITPOS (*fp)
14631 + anonymous_size * bits_per_byte
14632 - bit_offset - FIELD_BITSIZE (*fp)));
14633 }
14634 }
14635 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14636 if (attr != NULL)
14637 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14638 + attr->constant_value (0)));
14639
14640 /* Get name of field. */
14641 fieldname = dwarf2_name (die, cu);
14642 if (fieldname == NULL)
14643 fieldname = "";
14644
14645 /* The name is already allocated along with this objfile, so we don't
14646 need to duplicate it for the type. */
14647 fp->name = fieldname;
14648
14649 /* Change accessibility for artificial fields (e.g. virtual table
14650 pointer or virtual base class pointer) to private. */
14651 if (dwarf2_attr (die, DW_AT_artificial, cu))
14652 {
14653 FIELD_ARTIFICIAL (*fp) = 1;
14654 new_field->accessibility = DW_ACCESS_private;
14655 fip->non_public_fields = 1;
14656 }
14657 }
14658 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14659 {
14660 /* C++ static member. */
14661
14662 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14663 is a declaration, but all versions of G++ as of this writing
14664 (so through at least 3.2.1) incorrectly generate
14665 DW_TAG_variable tags. */
14666
14667 const char *physname;
14668
14669 /* Get name of field. */
14670 fieldname = dwarf2_name (die, cu);
14671 if (fieldname == NULL)
14672 return;
14673
14674 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14675 if (attr
14676 /* Only create a symbol if this is an external value.
14677 new_symbol checks this and puts the value in the global symbol
14678 table, which we want. If it is not external, new_symbol
14679 will try to put the value in cu->list_in_scope which is wrong. */
14680 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14681 {
14682 /* A static const member, not much different than an enum as far as
14683 we're concerned, except that we can support more types. */
14684 new_symbol (die, NULL, cu);
14685 }
14686
14687 /* Get physical name. */
14688 physname = dwarf2_physname (fieldname, die, cu);
14689
14690 /* The name is already allocated along with this objfile, so we don't
14691 need to duplicate it for the type. */
14692 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14693 FIELD_TYPE (*fp) = die_type (die, cu);
14694 FIELD_NAME (*fp) = fieldname;
14695 }
14696 else if (die->tag == DW_TAG_inheritance)
14697 {
14698 /* C++ base class field. */
14699 handle_data_member_location (die, cu, fp);
14700 FIELD_BITSIZE (*fp) = 0;
14701 FIELD_TYPE (*fp) = die_type (die, cu);
14702 FIELD_NAME (*fp) = fp->type->name ();
14703 }
14704 else
14705 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14706 }
14707
14708 /* Can the type given by DIE define another type? */
14709
14710 static bool
14711 type_can_define_types (const struct die_info *die)
14712 {
14713 switch (die->tag)
14714 {
14715 case DW_TAG_typedef:
14716 case DW_TAG_class_type:
14717 case DW_TAG_structure_type:
14718 case DW_TAG_union_type:
14719 case DW_TAG_enumeration_type:
14720 return true;
14721
14722 default:
14723 return false;
14724 }
14725 }
14726
14727 /* Add a type definition defined in the scope of the FIP's class. */
14728
14729 static void
14730 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14731 struct dwarf2_cu *cu)
14732 {
14733 struct decl_field fp;
14734 memset (&fp, 0, sizeof (fp));
14735
14736 gdb_assert (type_can_define_types (die));
14737
14738 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14739 fp.name = dwarf2_name (die, cu);
14740 fp.type = read_type_die (die, cu);
14741
14742 /* Save accessibility. */
14743 enum dwarf_access_attribute accessibility;
14744 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14745 if (attr != NULL)
14746 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14747 else
14748 accessibility = dwarf2_default_access_attribute (die, cu);
14749 switch (accessibility)
14750 {
14751 case DW_ACCESS_public:
14752 /* The assumed value if neither private nor protected. */
14753 break;
14754 case DW_ACCESS_private:
14755 fp.is_private = 1;
14756 break;
14757 case DW_ACCESS_protected:
14758 fp.is_protected = 1;
14759 break;
14760 default:
14761 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14762 }
14763
14764 if (die->tag == DW_TAG_typedef)
14765 fip->typedef_field_list.push_back (fp);
14766 else
14767 fip->nested_types_list.push_back (fp);
14768 }
14769
14770 /* A convenience typedef that's used when finding the discriminant
14771 field for a variant part. */
14772 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14773 offset_map_type;
14774
14775 /* Compute the discriminant range for a given variant. OBSTACK is
14776 where the results will be stored. VARIANT is the variant to
14777 process. IS_UNSIGNED indicates whether the discriminant is signed
14778 or unsigned. */
14779
14780 static const gdb::array_view<discriminant_range>
14781 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14782 bool is_unsigned)
14783 {
14784 std::vector<discriminant_range> ranges;
14785
14786 if (variant.default_branch)
14787 return {};
14788
14789 if (variant.discr_list_data == nullptr)
14790 {
14791 discriminant_range r
14792 = {variant.discriminant_value, variant.discriminant_value};
14793 ranges.push_back (r);
14794 }
14795 else
14796 {
14797 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14798 variant.discr_list_data->size);
14799 while (!data.empty ())
14800 {
14801 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14802 {
14803 complaint (_("invalid discriminant marker: %d"), data[0]);
14804 break;
14805 }
14806 bool is_range = data[0] == DW_DSC_range;
14807 data = data.slice (1);
14808
14809 ULONGEST low, high;
14810 unsigned int bytes_read;
14811
14812 if (data.empty ())
14813 {
14814 complaint (_("DW_AT_discr_list missing low value"));
14815 break;
14816 }
14817 if (is_unsigned)
14818 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14819 else
14820 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14821 &bytes_read);
14822 data = data.slice (bytes_read);
14823
14824 if (is_range)
14825 {
14826 if (data.empty ())
14827 {
14828 complaint (_("DW_AT_discr_list missing high value"));
14829 break;
14830 }
14831 if (is_unsigned)
14832 high = read_unsigned_leb128 (nullptr, data.data (),
14833 &bytes_read);
14834 else
14835 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14836 &bytes_read);
14837 data = data.slice (bytes_read);
14838 }
14839 else
14840 high = low;
14841
14842 ranges.push_back ({ low, high });
14843 }
14844 }
14845
14846 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14847 ranges.size ());
14848 std::copy (ranges.begin (), ranges.end (), result);
14849 return gdb::array_view<discriminant_range> (result, ranges.size ());
14850 }
14851
14852 static const gdb::array_view<variant_part> create_variant_parts
14853 (struct obstack *obstack,
14854 const offset_map_type &offset_map,
14855 struct field_info *fi,
14856 const std::vector<variant_part_builder> &variant_parts);
14857
14858 /* Fill in a "struct variant" for a given variant field. RESULT is
14859 the variant to fill in. OBSTACK is where any needed allocations
14860 will be done. OFFSET_MAP holds the mapping from section offsets to
14861 fields for the type. FI describes the fields of the type we're
14862 processing. FIELD is the variant field we're converting. */
14863
14864 static void
14865 create_one_variant (variant &result, struct obstack *obstack,
14866 const offset_map_type &offset_map,
14867 struct field_info *fi, const variant_field &field)
14868 {
14869 result.discriminants = convert_variant_range (obstack, field, false);
14870 result.first_field = field.first_field + fi->baseclasses.size ();
14871 result.last_field = field.last_field + fi->baseclasses.size ();
14872 result.parts = create_variant_parts (obstack, offset_map, fi,
14873 field.variant_parts);
14874 }
14875
14876 /* Fill in a "struct variant_part" for a given variant part. RESULT
14877 is the variant part to fill in. OBSTACK is where any needed
14878 allocations will be done. OFFSET_MAP holds the mapping from
14879 section offsets to fields for the type. FI describes the fields of
14880 the type we're processing. BUILDER is the variant part to be
14881 converted. */
14882
14883 static void
14884 create_one_variant_part (variant_part &result,
14885 struct obstack *obstack,
14886 const offset_map_type &offset_map,
14887 struct field_info *fi,
14888 const variant_part_builder &builder)
14889 {
14890 auto iter = offset_map.find (builder.discriminant_offset);
14891 if (iter == offset_map.end ())
14892 {
14893 result.discriminant_index = -1;
14894 /* Doesn't matter. */
14895 result.is_unsigned = false;
14896 }
14897 else
14898 {
14899 result.discriminant_index = iter->second;
14900 result.is_unsigned
14901 = TYPE_UNSIGNED (FIELD_TYPE
14902 (fi->fields[result.discriminant_index].field));
14903 }
14904
14905 size_t n = builder.variants.size ();
14906 variant *output = new (obstack) variant[n];
14907 for (size_t i = 0; i < n; ++i)
14908 create_one_variant (output[i], obstack, offset_map, fi,
14909 builder.variants[i]);
14910
14911 result.variants = gdb::array_view<variant> (output, n);
14912 }
14913
14914 /* Create a vector of variant parts that can be attached to a type.
14915 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14916 holds the mapping from section offsets to fields for the type. FI
14917 describes the fields of the type we're processing. VARIANT_PARTS
14918 is the vector to convert. */
14919
14920 static const gdb::array_view<variant_part>
14921 create_variant_parts (struct obstack *obstack,
14922 const offset_map_type &offset_map,
14923 struct field_info *fi,
14924 const std::vector<variant_part_builder> &variant_parts)
14925 {
14926 if (variant_parts.empty ())
14927 return {};
14928
14929 size_t n = variant_parts.size ();
14930 variant_part *result = new (obstack) variant_part[n];
14931 for (size_t i = 0; i < n; ++i)
14932 create_one_variant_part (result[i], obstack, offset_map, fi,
14933 variant_parts[i]);
14934
14935 return gdb::array_view<variant_part> (result, n);
14936 }
14937
14938 /* Compute the variant part vector for FIP, attaching it to TYPE when
14939 done. */
14940
14941 static void
14942 add_variant_property (struct field_info *fip, struct type *type,
14943 struct dwarf2_cu *cu)
14944 {
14945 /* Map section offsets of fields to their field index. Note the
14946 field index here does not take the number of baseclasses into
14947 account. */
14948 offset_map_type offset_map;
14949 for (int i = 0; i < fip->fields.size (); ++i)
14950 offset_map[fip->fields[i].offset] = i;
14951
14952 struct objfile *objfile = cu->per_objfile->objfile;
14953 gdb::array_view<variant_part> parts
14954 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14955 fip->variant_parts);
14956
14957 struct dynamic_prop prop;
14958 prop.kind = PROP_VARIANT_PARTS;
14959 prop.data.variant_parts
14960 = ((gdb::array_view<variant_part> *)
14961 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14962
14963 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14964 }
14965
14966 /* Create the vector of fields, and attach it to the type. */
14967
14968 static void
14969 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14970 struct dwarf2_cu *cu)
14971 {
14972 int nfields = fip->nfields ();
14973
14974 /* Record the field count, allocate space for the array of fields,
14975 and create blank accessibility bitfields if necessary. */
14976 type->set_num_fields (nfields);
14977 type->set_fields
14978 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14979
14980 if (fip->non_public_fields && cu->language != language_ada)
14981 {
14982 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14983
14984 TYPE_FIELD_PRIVATE_BITS (type) =
14985 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14986 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14987
14988 TYPE_FIELD_PROTECTED_BITS (type) =
14989 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14990 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14991
14992 TYPE_FIELD_IGNORE_BITS (type) =
14993 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14994 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14995 }
14996
14997 /* If the type has baseclasses, allocate and clear a bit vector for
14998 TYPE_FIELD_VIRTUAL_BITS. */
14999 if (!fip->baseclasses.empty () && cu->language != language_ada)
15000 {
15001 int num_bytes = B_BYTES (fip->baseclasses.size ());
15002 unsigned char *pointer;
15003
15004 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15005 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15006 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15007 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15008 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15009 }
15010
15011 if (!fip->variant_parts.empty ())
15012 add_variant_property (fip, type, cu);
15013
15014 /* Copy the saved-up fields into the field vector. */
15015 for (int i = 0; i < nfields; ++i)
15016 {
15017 struct nextfield &field
15018 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15019 : fip->fields[i - fip->baseclasses.size ()]);
15020
15021 type->field (i) = field.field;
15022 switch (field.accessibility)
15023 {
15024 case DW_ACCESS_private:
15025 if (cu->language != language_ada)
15026 SET_TYPE_FIELD_PRIVATE (type, i);
15027 break;
15028
15029 case DW_ACCESS_protected:
15030 if (cu->language != language_ada)
15031 SET_TYPE_FIELD_PROTECTED (type, i);
15032 break;
15033
15034 case DW_ACCESS_public:
15035 break;
15036
15037 default:
15038 /* Unknown accessibility. Complain and treat it as public. */
15039 {
15040 complaint (_("unsupported accessibility %d"),
15041 field.accessibility);
15042 }
15043 break;
15044 }
15045 if (i < fip->baseclasses.size ())
15046 {
15047 switch (field.virtuality)
15048 {
15049 case DW_VIRTUALITY_virtual:
15050 case DW_VIRTUALITY_pure_virtual:
15051 if (cu->language == language_ada)
15052 error (_("unexpected virtuality in component of Ada type"));
15053 SET_TYPE_FIELD_VIRTUAL (type, i);
15054 break;
15055 }
15056 }
15057 }
15058 }
15059
15060 /* Return true if this member function is a constructor, false
15061 otherwise. */
15062
15063 static int
15064 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15065 {
15066 const char *fieldname;
15067 const char *type_name;
15068 int len;
15069
15070 if (die->parent == NULL)
15071 return 0;
15072
15073 if (die->parent->tag != DW_TAG_structure_type
15074 && die->parent->tag != DW_TAG_union_type
15075 && die->parent->tag != DW_TAG_class_type)
15076 return 0;
15077
15078 fieldname = dwarf2_name (die, cu);
15079 type_name = dwarf2_name (die->parent, cu);
15080 if (fieldname == NULL || type_name == NULL)
15081 return 0;
15082
15083 len = strlen (fieldname);
15084 return (strncmp (fieldname, type_name, len) == 0
15085 && (type_name[len] == '\0' || type_name[len] == '<'));
15086 }
15087
15088 /* Check if the given VALUE is a recognized enum
15089 dwarf_defaulted_attribute constant according to DWARF5 spec,
15090 Table 7.24. */
15091
15092 static bool
15093 is_valid_DW_AT_defaulted (ULONGEST value)
15094 {
15095 switch (value)
15096 {
15097 case DW_DEFAULTED_no:
15098 case DW_DEFAULTED_in_class:
15099 case DW_DEFAULTED_out_of_class:
15100 return true;
15101 }
15102
15103 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15104 return false;
15105 }
15106
15107 /* Add a member function to the proper fieldlist. */
15108
15109 static void
15110 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15111 struct type *type, struct dwarf2_cu *cu)
15112 {
15113 struct objfile *objfile = cu->per_objfile->objfile;
15114 struct attribute *attr;
15115 int i;
15116 struct fnfieldlist *flp = nullptr;
15117 struct fn_field *fnp;
15118 const char *fieldname;
15119 struct type *this_type;
15120 enum dwarf_access_attribute accessibility;
15121
15122 if (cu->language == language_ada)
15123 error (_("unexpected member function in Ada type"));
15124
15125 /* Get name of member function. */
15126 fieldname = dwarf2_name (die, cu);
15127 if (fieldname == NULL)
15128 return;
15129
15130 /* Look up member function name in fieldlist. */
15131 for (i = 0; i < fip->fnfieldlists.size (); i++)
15132 {
15133 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15134 {
15135 flp = &fip->fnfieldlists[i];
15136 break;
15137 }
15138 }
15139
15140 /* Create a new fnfieldlist if necessary. */
15141 if (flp == nullptr)
15142 {
15143 fip->fnfieldlists.emplace_back ();
15144 flp = &fip->fnfieldlists.back ();
15145 flp->name = fieldname;
15146 i = fip->fnfieldlists.size () - 1;
15147 }
15148
15149 /* Create a new member function field and add it to the vector of
15150 fnfieldlists. */
15151 flp->fnfields.emplace_back ();
15152 fnp = &flp->fnfields.back ();
15153
15154 /* Delay processing of the physname until later. */
15155 if (cu->language == language_cplus)
15156 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15157 die, cu);
15158 else
15159 {
15160 const char *physname = dwarf2_physname (fieldname, die, cu);
15161 fnp->physname = physname ? physname : "";
15162 }
15163
15164 fnp->type = alloc_type (objfile);
15165 this_type = read_type_die (die, cu);
15166 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15167 {
15168 int nparams = this_type->num_fields ();
15169
15170 /* TYPE is the domain of this method, and THIS_TYPE is the type
15171 of the method itself (TYPE_CODE_METHOD). */
15172 smash_to_method_type (fnp->type, type,
15173 TYPE_TARGET_TYPE (this_type),
15174 this_type->fields (),
15175 this_type->num_fields (),
15176 TYPE_VARARGS (this_type));
15177
15178 /* Handle static member functions.
15179 Dwarf2 has no clean way to discern C++ static and non-static
15180 member functions. G++ helps GDB by marking the first
15181 parameter for non-static member functions (which is the this
15182 pointer) as artificial. We obtain this information from
15183 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15184 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15185 fnp->voffset = VOFFSET_STATIC;
15186 }
15187 else
15188 complaint (_("member function type missing for '%s'"),
15189 dwarf2_full_name (fieldname, die, cu));
15190
15191 /* Get fcontext from DW_AT_containing_type if present. */
15192 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15193 fnp->fcontext = die_containing_type (die, cu);
15194
15195 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15196 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15197
15198 /* Get accessibility. */
15199 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15200 if (attr != nullptr)
15201 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15202 else
15203 accessibility = dwarf2_default_access_attribute (die, cu);
15204 switch (accessibility)
15205 {
15206 case DW_ACCESS_private:
15207 fnp->is_private = 1;
15208 break;
15209 case DW_ACCESS_protected:
15210 fnp->is_protected = 1;
15211 break;
15212 }
15213
15214 /* Check for artificial methods. */
15215 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15216 if (attr && DW_UNSND (attr) != 0)
15217 fnp->is_artificial = 1;
15218
15219 /* Check for defaulted methods. */
15220 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15221 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15222 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15223
15224 /* Check for deleted methods. */
15225 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15226 if (attr != nullptr && DW_UNSND (attr) != 0)
15227 fnp->is_deleted = 1;
15228
15229 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15230
15231 /* Get index in virtual function table if it is a virtual member
15232 function. For older versions of GCC, this is an offset in the
15233 appropriate virtual table, as specified by DW_AT_containing_type.
15234 For everyone else, it is an expression to be evaluated relative
15235 to the object address. */
15236
15237 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15238 if (attr != nullptr)
15239 {
15240 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15241 {
15242 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15243 {
15244 /* Old-style GCC. */
15245 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15246 }
15247 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15248 || (DW_BLOCK (attr)->size > 1
15249 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15250 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15251 {
15252 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15253 if ((fnp->voffset % cu->header.addr_size) != 0)
15254 dwarf2_complex_location_expr_complaint ();
15255 else
15256 fnp->voffset /= cu->header.addr_size;
15257 fnp->voffset += 2;
15258 }
15259 else
15260 dwarf2_complex_location_expr_complaint ();
15261
15262 if (!fnp->fcontext)
15263 {
15264 /* If there is no `this' field and no DW_AT_containing_type,
15265 we cannot actually find a base class context for the
15266 vtable! */
15267 if (this_type->num_fields () == 0
15268 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15269 {
15270 complaint (_("cannot determine context for virtual member "
15271 "function \"%s\" (offset %s)"),
15272 fieldname, sect_offset_str (die->sect_off));
15273 }
15274 else
15275 {
15276 fnp->fcontext
15277 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15278 }
15279 }
15280 }
15281 else if (attr->form_is_section_offset ())
15282 {
15283 dwarf2_complex_location_expr_complaint ();
15284 }
15285 else
15286 {
15287 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15288 fieldname);
15289 }
15290 }
15291 else
15292 {
15293 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15294 if (attr && DW_UNSND (attr))
15295 {
15296 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15297 complaint (_("Member function \"%s\" (offset %s) is virtual "
15298 "but the vtable offset is not specified"),
15299 fieldname, sect_offset_str (die->sect_off));
15300 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15301 TYPE_CPLUS_DYNAMIC (type) = 1;
15302 }
15303 }
15304 }
15305
15306 /* Create the vector of member function fields, and attach it to the type. */
15307
15308 static void
15309 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15310 struct dwarf2_cu *cu)
15311 {
15312 if (cu->language == language_ada)
15313 error (_("unexpected member functions in Ada type"));
15314
15315 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15316 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15317 TYPE_ALLOC (type,
15318 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15319
15320 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15321 {
15322 struct fnfieldlist &nf = fip->fnfieldlists[i];
15323 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15324
15325 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15326 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15327 fn_flp->fn_fields = (struct fn_field *)
15328 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15329
15330 for (int k = 0; k < nf.fnfields.size (); ++k)
15331 fn_flp->fn_fields[k] = nf.fnfields[k];
15332 }
15333
15334 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15335 }
15336
15337 /* Returns non-zero if NAME is the name of a vtable member in CU's
15338 language, zero otherwise. */
15339 static int
15340 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15341 {
15342 static const char vptr[] = "_vptr";
15343
15344 /* Look for the C++ form of the vtable. */
15345 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15346 return 1;
15347
15348 return 0;
15349 }
15350
15351 /* GCC outputs unnamed structures that are really pointers to member
15352 functions, with the ABI-specified layout. If TYPE describes
15353 such a structure, smash it into a member function type.
15354
15355 GCC shouldn't do this; it should just output pointer to member DIEs.
15356 This is GCC PR debug/28767. */
15357
15358 static void
15359 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15360 {
15361 struct type *pfn_type, *self_type, *new_type;
15362
15363 /* Check for a structure with no name and two children. */
15364 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15365 return;
15366
15367 /* Check for __pfn and __delta members. */
15368 if (TYPE_FIELD_NAME (type, 0) == NULL
15369 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15370 || TYPE_FIELD_NAME (type, 1) == NULL
15371 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15372 return;
15373
15374 /* Find the type of the method. */
15375 pfn_type = TYPE_FIELD_TYPE (type, 0);
15376 if (pfn_type == NULL
15377 || pfn_type->code () != TYPE_CODE_PTR
15378 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15379 return;
15380
15381 /* Look for the "this" argument. */
15382 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15383 if (pfn_type->num_fields () == 0
15384 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15385 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15386 return;
15387
15388 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15389 new_type = alloc_type (objfile);
15390 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15391 pfn_type->fields (), pfn_type->num_fields (),
15392 TYPE_VARARGS (pfn_type));
15393 smash_to_methodptr_type (type, new_type);
15394 }
15395
15396 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15397 appropriate error checking and issuing complaints if there is a
15398 problem. */
15399
15400 static ULONGEST
15401 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15402 {
15403 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15404
15405 if (attr == nullptr)
15406 return 0;
15407
15408 if (!attr->form_is_constant ())
15409 {
15410 complaint (_("DW_AT_alignment must have constant form"
15411 " - DIE at %s [in module %s]"),
15412 sect_offset_str (die->sect_off),
15413 objfile_name (cu->per_objfile->objfile));
15414 return 0;
15415 }
15416
15417 ULONGEST align;
15418 if (attr->form == DW_FORM_sdata)
15419 {
15420 LONGEST val = DW_SND (attr);
15421 if (val < 0)
15422 {
15423 complaint (_("DW_AT_alignment value must not be negative"
15424 " - DIE at %s [in module %s]"),
15425 sect_offset_str (die->sect_off),
15426 objfile_name (cu->per_objfile->objfile));
15427 return 0;
15428 }
15429 align = val;
15430 }
15431 else
15432 align = DW_UNSND (attr);
15433
15434 if (align == 0)
15435 {
15436 complaint (_("DW_AT_alignment value must not be zero"
15437 " - DIE at %s [in module %s]"),
15438 sect_offset_str (die->sect_off),
15439 objfile_name (cu->per_objfile->objfile));
15440 return 0;
15441 }
15442 if ((align & (align - 1)) != 0)
15443 {
15444 complaint (_("DW_AT_alignment value must be a power of 2"
15445 " - DIE at %s [in module %s]"),
15446 sect_offset_str (die->sect_off),
15447 objfile_name (cu->per_objfile->objfile));
15448 return 0;
15449 }
15450
15451 return align;
15452 }
15453
15454 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15455 the alignment for TYPE. */
15456
15457 static void
15458 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15459 struct type *type)
15460 {
15461 if (!set_type_align (type, get_alignment (cu, die)))
15462 complaint (_("DW_AT_alignment value too large"
15463 " - DIE at %s [in module %s]"),
15464 sect_offset_str (die->sect_off),
15465 objfile_name (cu->per_objfile->objfile));
15466 }
15467
15468 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15469 constant for a type, according to DWARF5 spec, Table 5.5. */
15470
15471 static bool
15472 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15473 {
15474 switch (value)
15475 {
15476 case DW_CC_normal:
15477 case DW_CC_pass_by_reference:
15478 case DW_CC_pass_by_value:
15479 return true;
15480
15481 default:
15482 complaint (_("unrecognized DW_AT_calling_convention value "
15483 "(%s) for a type"), pulongest (value));
15484 return false;
15485 }
15486 }
15487
15488 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15489 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15490 also according to GNU-specific values (see include/dwarf2.h). */
15491
15492 static bool
15493 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15494 {
15495 switch (value)
15496 {
15497 case DW_CC_normal:
15498 case DW_CC_program:
15499 case DW_CC_nocall:
15500 return true;
15501
15502 case DW_CC_GNU_renesas_sh:
15503 case DW_CC_GNU_borland_fastcall_i386:
15504 case DW_CC_GDB_IBM_OpenCL:
15505 return true;
15506
15507 default:
15508 complaint (_("unrecognized DW_AT_calling_convention value "
15509 "(%s) for a subroutine"), pulongest (value));
15510 return false;
15511 }
15512 }
15513
15514 /* Called when we find the DIE that starts a structure or union scope
15515 (definition) to create a type for the structure or union. Fill in
15516 the type's name and general properties; the members will not be
15517 processed until process_structure_scope. A symbol table entry for
15518 the type will also not be done until process_structure_scope (assuming
15519 the type has a name).
15520
15521 NOTE: we need to call these functions regardless of whether or not the
15522 DIE has a DW_AT_name attribute, since it might be an anonymous
15523 structure or union. This gets the type entered into our set of
15524 user defined types. */
15525
15526 static struct type *
15527 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15528 {
15529 struct objfile *objfile = cu->per_objfile->objfile;
15530 struct type *type;
15531 struct attribute *attr;
15532 const char *name;
15533
15534 /* If the definition of this type lives in .debug_types, read that type.
15535 Don't follow DW_AT_specification though, that will take us back up
15536 the chain and we want to go down. */
15537 attr = die->attr (DW_AT_signature);
15538 if (attr != nullptr)
15539 {
15540 type = get_DW_AT_signature_type (die, attr, cu);
15541
15542 /* The type's CU may not be the same as CU.
15543 Ensure TYPE is recorded with CU in die_type_hash. */
15544 return set_die_type (die, type, cu);
15545 }
15546
15547 type = alloc_type (objfile);
15548 INIT_CPLUS_SPECIFIC (type);
15549
15550 name = dwarf2_name (die, cu);
15551 if (name != NULL)
15552 {
15553 if (cu->language == language_cplus
15554 || cu->language == language_d
15555 || cu->language == language_rust)
15556 {
15557 const char *full_name = dwarf2_full_name (name, die, cu);
15558
15559 /* dwarf2_full_name might have already finished building the DIE's
15560 type. If so, there is no need to continue. */
15561 if (get_die_type (die, cu) != NULL)
15562 return get_die_type (die, cu);
15563
15564 type->set_name (full_name);
15565 }
15566 else
15567 {
15568 /* The name is already allocated along with this objfile, so
15569 we don't need to duplicate it for the type. */
15570 type->set_name (name);
15571 }
15572 }
15573
15574 if (die->tag == DW_TAG_structure_type)
15575 {
15576 type->set_code (TYPE_CODE_STRUCT);
15577 }
15578 else if (die->tag == DW_TAG_union_type)
15579 {
15580 type->set_code (TYPE_CODE_UNION);
15581 }
15582 else
15583 {
15584 type->set_code (TYPE_CODE_STRUCT);
15585 }
15586
15587 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15588 TYPE_DECLARED_CLASS (type) = 1;
15589
15590 /* Store the calling convention in the type if it's available in
15591 the die. Otherwise the calling convention remains set to
15592 the default value DW_CC_normal. */
15593 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15594 if (attr != nullptr
15595 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15596 {
15597 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15598 TYPE_CPLUS_CALLING_CONVENTION (type)
15599 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15600 }
15601
15602 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15603 if (attr != nullptr)
15604 {
15605 if (attr->form_is_constant ())
15606 TYPE_LENGTH (type) = DW_UNSND (attr);
15607 else
15608 {
15609 struct dynamic_prop prop;
15610 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15611 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15612 TYPE_LENGTH (type) = 0;
15613 }
15614 }
15615 else
15616 {
15617 TYPE_LENGTH (type) = 0;
15618 }
15619
15620 maybe_set_alignment (cu, die, type);
15621
15622 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15623 {
15624 /* ICC<14 does not output the required DW_AT_declaration on
15625 incomplete types, but gives them a size of zero. */
15626 TYPE_STUB (type) = 1;
15627 }
15628 else
15629 TYPE_STUB_SUPPORTED (type) = 1;
15630
15631 if (die_is_declaration (die, cu))
15632 TYPE_STUB (type) = 1;
15633 else if (attr == NULL && die->child == NULL
15634 && producer_is_realview (cu->producer))
15635 /* RealView does not output the required DW_AT_declaration
15636 on incomplete types. */
15637 TYPE_STUB (type) = 1;
15638
15639 /* We need to add the type field to the die immediately so we don't
15640 infinitely recurse when dealing with pointers to the structure
15641 type within the structure itself. */
15642 set_die_type (die, type, cu);
15643
15644 /* set_die_type should be already done. */
15645 set_descriptive_type (type, die, cu);
15646
15647 return type;
15648 }
15649
15650 static void handle_struct_member_die
15651 (struct die_info *child_die,
15652 struct type *type,
15653 struct field_info *fi,
15654 std::vector<struct symbol *> *template_args,
15655 struct dwarf2_cu *cu);
15656
15657 /* A helper for handle_struct_member_die that handles
15658 DW_TAG_variant_part. */
15659
15660 static void
15661 handle_variant_part (struct die_info *die, struct type *type,
15662 struct field_info *fi,
15663 std::vector<struct symbol *> *template_args,
15664 struct dwarf2_cu *cu)
15665 {
15666 variant_part_builder *new_part;
15667 if (fi->current_variant_part == nullptr)
15668 {
15669 fi->variant_parts.emplace_back ();
15670 new_part = &fi->variant_parts.back ();
15671 }
15672 else if (!fi->current_variant_part->processing_variant)
15673 {
15674 complaint (_("nested DW_TAG_variant_part seen "
15675 "- DIE at %s [in module %s]"),
15676 sect_offset_str (die->sect_off),
15677 objfile_name (cu->per_objfile->objfile));
15678 return;
15679 }
15680 else
15681 {
15682 variant_field &current = fi->current_variant_part->variants.back ();
15683 current.variant_parts.emplace_back ();
15684 new_part = &current.variant_parts.back ();
15685 }
15686
15687 /* When we recurse, we want callees to add to this new variant
15688 part. */
15689 scoped_restore save_current_variant_part
15690 = make_scoped_restore (&fi->current_variant_part, new_part);
15691
15692 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15693 if (discr == NULL)
15694 {
15695 /* It's a univariant form, an extension we support. */
15696 }
15697 else if (discr->form_is_ref ())
15698 {
15699 struct dwarf2_cu *target_cu = cu;
15700 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15701
15702 new_part->discriminant_offset = target_die->sect_off;
15703 }
15704 else
15705 {
15706 complaint (_("DW_AT_discr does not have DIE reference form"
15707 " - DIE at %s [in module %s]"),
15708 sect_offset_str (die->sect_off),
15709 objfile_name (cu->per_objfile->objfile));
15710 }
15711
15712 for (die_info *child_die = die->child;
15713 child_die != NULL;
15714 child_die = child_die->sibling)
15715 handle_struct_member_die (child_die, type, fi, template_args, cu);
15716 }
15717
15718 /* A helper for handle_struct_member_die that handles
15719 DW_TAG_variant. */
15720
15721 static void
15722 handle_variant (struct die_info *die, struct type *type,
15723 struct field_info *fi,
15724 std::vector<struct symbol *> *template_args,
15725 struct dwarf2_cu *cu)
15726 {
15727 if (fi->current_variant_part == nullptr)
15728 {
15729 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15730 "- DIE at %s [in module %s]"),
15731 sect_offset_str (die->sect_off),
15732 objfile_name (cu->per_objfile->objfile));
15733 return;
15734 }
15735 if (fi->current_variant_part->processing_variant)
15736 {
15737 complaint (_("nested DW_TAG_variant seen "
15738 "- DIE at %s [in module %s]"),
15739 sect_offset_str (die->sect_off),
15740 objfile_name (cu->per_objfile->objfile));
15741 return;
15742 }
15743
15744 scoped_restore save_processing_variant
15745 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15746 true);
15747
15748 fi->current_variant_part->variants.emplace_back ();
15749 variant_field &variant = fi->current_variant_part->variants.back ();
15750 variant.first_field = fi->fields.size ();
15751
15752 /* In a variant we want to get the discriminant and also add a
15753 field for our sole member child. */
15754 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15755 if (discr == nullptr)
15756 {
15757 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15758 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15759 variant.default_branch = true;
15760 else
15761 variant.discr_list_data = DW_BLOCK (discr);
15762 }
15763 else
15764 variant.discriminant_value = DW_UNSND (discr);
15765
15766 for (die_info *variant_child = die->child;
15767 variant_child != NULL;
15768 variant_child = variant_child->sibling)
15769 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15770
15771 variant.last_field = fi->fields.size ();
15772 }
15773
15774 /* A helper for process_structure_scope that handles a single member
15775 DIE. */
15776
15777 static void
15778 handle_struct_member_die (struct die_info *child_die, struct type *type,
15779 struct field_info *fi,
15780 std::vector<struct symbol *> *template_args,
15781 struct dwarf2_cu *cu)
15782 {
15783 if (child_die->tag == DW_TAG_member
15784 || child_die->tag == DW_TAG_variable)
15785 {
15786 /* NOTE: carlton/2002-11-05: A C++ static data member
15787 should be a DW_TAG_member that is a declaration, but
15788 all versions of G++ as of this writing (so through at
15789 least 3.2.1) incorrectly generate DW_TAG_variable
15790 tags for them instead. */
15791 dwarf2_add_field (fi, child_die, cu);
15792 }
15793 else if (child_die->tag == DW_TAG_subprogram)
15794 {
15795 /* Rust doesn't have member functions in the C++ sense.
15796 However, it does emit ordinary functions as children
15797 of a struct DIE. */
15798 if (cu->language == language_rust)
15799 read_func_scope (child_die, cu);
15800 else
15801 {
15802 /* C++ member function. */
15803 dwarf2_add_member_fn (fi, child_die, type, cu);
15804 }
15805 }
15806 else if (child_die->tag == DW_TAG_inheritance)
15807 {
15808 /* C++ base class field. */
15809 dwarf2_add_field (fi, child_die, cu);
15810 }
15811 else if (type_can_define_types (child_die))
15812 dwarf2_add_type_defn (fi, child_die, cu);
15813 else if (child_die->tag == DW_TAG_template_type_param
15814 || child_die->tag == DW_TAG_template_value_param)
15815 {
15816 struct symbol *arg = new_symbol (child_die, NULL, cu);
15817
15818 if (arg != NULL)
15819 template_args->push_back (arg);
15820 }
15821 else if (child_die->tag == DW_TAG_variant_part)
15822 handle_variant_part (child_die, type, fi, template_args, cu);
15823 else if (child_die->tag == DW_TAG_variant)
15824 handle_variant (child_die, type, fi, template_args, cu);
15825 }
15826
15827 /* Finish creating a structure or union type, including filling in
15828 its members and creating a symbol for it. */
15829
15830 static void
15831 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15832 {
15833 struct objfile *objfile = cu->per_objfile->objfile;
15834 struct die_info *child_die;
15835 struct type *type;
15836
15837 type = get_die_type (die, cu);
15838 if (type == NULL)
15839 type = read_structure_type (die, cu);
15840
15841 bool has_template_parameters = false;
15842 if (die->child != NULL && ! die_is_declaration (die, cu))
15843 {
15844 struct field_info fi;
15845 std::vector<struct symbol *> template_args;
15846
15847 child_die = die->child;
15848
15849 while (child_die && child_die->tag)
15850 {
15851 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15852 child_die = child_die->sibling;
15853 }
15854
15855 /* Attach template arguments to type. */
15856 if (!template_args.empty ())
15857 {
15858 has_template_parameters = true;
15859 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15860 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15861 TYPE_TEMPLATE_ARGUMENTS (type)
15862 = XOBNEWVEC (&objfile->objfile_obstack,
15863 struct symbol *,
15864 TYPE_N_TEMPLATE_ARGUMENTS (type));
15865 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15866 template_args.data (),
15867 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15868 * sizeof (struct symbol *)));
15869 }
15870
15871 /* Attach fields and member functions to the type. */
15872 if (fi.nfields () > 0)
15873 dwarf2_attach_fields_to_type (&fi, type, cu);
15874 if (!fi.fnfieldlists.empty ())
15875 {
15876 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15877
15878 /* Get the type which refers to the base class (possibly this
15879 class itself) which contains the vtable pointer for the current
15880 class from the DW_AT_containing_type attribute. This use of
15881 DW_AT_containing_type is a GNU extension. */
15882
15883 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15884 {
15885 struct type *t = die_containing_type (die, cu);
15886
15887 set_type_vptr_basetype (type, t);
15888 if (type == t)
15889 {
15890 int i;
15891
15892 /* Our own class provides vtbl ptr. */
15893 for (i = t->num_fields () - 1;
15894 i >= TYPE_N_BASECLASSES (t);
15895 --i)
15896 {
15897 const char *fieldname = TYPE_FIELD_NAME (t, i);
15898
15899 if (is_vtable_name (fieldname, cu))
15900 {
15901 set_type_vptr_fieldno (type, i);
15902 break;
15903 }
15904 }
15905
15906 /* Complain if virtual function table field not found. */
15907 if (i < TYPE_N_BASECLASSES (t))
15908 complaint (_("virtual function table pointer "
15909 "not found when defining class '%s'"),
15910 type->name () ? type->name () : "");
15911 }
15912 else
15913 {
15914 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15915 }
15916 }
15917 else if (cu->producer
15918 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15919 {
15920 /* The IBM XLC compiler does not provide direct indication
15921 of the containing type, but the vtable pointer is
15922 always named __vfp. */
15923
15924 int i;
15925
15926 for (i = type->num_fields () - 1;
15927 i >= TYPE_N_BASECLASSES (type);
15928 --i)
15929 {
15930 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15931 {
15932 set_type_vptr_fieldno (type, i);
15933 set_type_vptr_basetype (type, type);
15934 break;
15935 }
15936 }
15937 }
15938 }
15939
15940 /* Copy fi.typedef_field_list linked list elements content into the
15941 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15942 if (!fi.typedef_field_list.empty ())
15943 {
15944 int count = fi.typedef_field_list.size ();
15945
15946 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15947 TYPE_TYPEDEF_FIELD_ARRAY (type)
15948 = ((struct decl_field *)
15949 TYPE_ALLOC (type,
15950 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15951 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15952
15953 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15954 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15955 }
15956
15957 /* Copy fi.nested_types_list linked list elements content into the
15958 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15959 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15960 {
15961 int count = fi.nested_types_list.size ();
15962
15963 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15964 TYPE_NESTED_TYPES_ARRAY (type)
15965 = ((struct decl_field *)
15966 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15967 TYPE_NESTED_TYPES_COUNT (type) = count;
15968
15969 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15970 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15971 }
15972 }
15973
15974 quirk_gcc_member_function_pointer (type, objfile);
15975 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15976 cu->rust_unions.push_back (type);
15977
15978 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15979 snapshots) has been known to create a die giving a declaration
15980 for a class that has, as a child, a die giving a definition for a
15981 nested class. So we have to process our children even if the
15982 current die is a declaration. Normally, of course, a declaration
15983 won't have any children at all. */
15984
15985 child_die = die->child;
15986
15987 while (child_die != NULL && child_die->tag)
15988 {
15989 if (child_die->tag == DW_TAG_member
15990 || child_die->tag == DW_TAG_variable
15991 || child_die->tag == DW_TAG_inheritance
15992 || child_die->tag == DW_TAG_template_value_param
15993 || child_die->tag == DW_TAG_template_type_param)
15994 {
15995 /* Do nothing. */
15996 }
15997 else
15998 process_die (child_die, cu);
15999
16000 child_die = child_die->sibling;
16001 }
16002
16003 /* Do not consider external references. According to the DWARF standard,
16004 these DIEs are identified by the fact that they have no byte_size
16005 attribute, and a declaration attribute. */
16006 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16007 || !die_is_declaration (die, cu)
16008 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16009 {
16010 struct symbol *sym = new_symbol (die, type, cu);
16011
16012 if (has_template_parameters)
16013 {
16014 struct symtab *symtab;
16015 if (sym != nullptr)
16016 symtab = symbol_symtab (sym);
16017 else if (cu->line_header != nullptr)
16018 {
16019 /* Any related symtab will do. */
16020 symtab
16021 = cu->line_header->file_names ()[0].symtab;
16022 }
16023 else
16024 {
16025 symtab = nullptr;
16026 complaint (_("could not find suitable "
16027 "symtab for template parameter"
16028 " - DIE at %s [in module %s]"),
16029 sect_offset_str (die->sect_off),
16030 objfile_name (objfile));
16031 }
16032
16033 if (symtab != nullptr)
16034 {
16035 /* Make sure that the symtab is set on the new symbols.
16036 Even though they don't appear in this symtab directly,
16037 other parts of gdb assume that symbols do, and this is
16038 reasonably true. */
16039 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16040 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16041 }
16042 }
16043 }
16044 }
16045
16046 /* Assuming DIE is an enumeration type, and TYPE is its associated
16047 type, update TYPE using some information only available in DIE's
16048 children. In particular, the fields are computed. */
16049
16050 static void
16051 update_enumeration_type_from_children (struct die_info *die,
16052 struct type *type,
16053 struct dwarf2_cu *cu)
16054 {
16055 struct die_info *child_die;
16056 int unsigned_enum = 1;
16057 int flag_enum = 1;
16058
16059 auto_obstack obstack;
16060 std::vector<struct field> fields;
16061
16062 for (child_die = die->child;
16063 child_die != NULL && child_die->tag;
16064 child_die = child_die->sibling)
16065 {
16066 struct attribute *attr;
16067 LONGEST value;
16068 const gdb_byte *bytes;
16069 struct dwarf2_locexpr_baton *baton;
16070 const char *name;
16071
16072 if (child_die->tag != DW_TAG_enumerator)
16073 continue;
16074
16075 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16076 if (attr == NULL)
16077 continue;
16078
16079 name = dwarf2_name (child_die, cu);
16080 if (name == NULL)
16081 name = "<anonymous enumerator>";
16082
16083 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16084 &value, &bytes, &baton);
16085 if (value < 0)
16086 {
16087 unsigned_enum = 0;
16088 flag_enum = 0;
16089 }
16090 else
16091 {
16092 if (count_one_bits_ll (value) >= 2)
16093 flag_enum = 0;
16094 }
16095
16096 fields.emplace_back ();
16097 struct field &field = fields.back ();
16098 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16099 SET_FIELD_ENUMVAL (field, value);
16100 }
16101
16102 if (!fields.empty ())
16103 {
16104 type->set_num_fields (fields.size ());
16105 type->set_fields
16106 ((struct field *)
16107 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16108 memcpy (type->fields (), fields.data (),
16109 sizeof (struct field) * fields.size ());
16110 }
16111
16112 if (unsigned_enum)
16113 TYPE_UNSIGNED (type) = 1;
16114 if (flag_enum)
16115 TYPE_FLAG_ENUM (type) = 1;
16116 }
16117
16118 /* Given a DW_AT_enumeration_type die, set its type. We do not
16119 complete the type's fields yet, or create any symbols. */
16120
16121 static struct type *
16122 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16123 {
16124 struct objfile *objfile = cu->per_objfile->objfile;
16125 struct type *type;
16126 struct attribute *attr;
16127 const char *name;
16128
16129 /* If the definition of this type lives in .debug_types, read that type.
16130 Don't follow DW_AT_specification though, that will take us back up
16131 the chain and we want to go down. */
16132 attr = die->attr (DW_AT_signature);
16133 if (attr != nullptr)
16134 {
16135 type = get_DW_AT_signature_type (die, attr, cu);
16136
16137 /* The type's CU may not be the same as CU.
16138 Ensure TYPE is recorded with CU in die_type_hash. */
16139 return set_die_type (die, type, cu);
16140 }
16141
16142 type = alloc_type (objfile);
16143
16144 type->set_code (TYPE_CODE_ENUM);
16145 name = dwarf2_full_name (NULL, die, cu);
16146 if (name != NULL)
16147 type->set_name (name);
16148
16149 attr = dwarf2_attr (die, DW_AT_type, cu);
16150 if (attr != NULL)
16151 {
16152 struct type *underlying_type = die_type (die, cu);
16153
16154 TYPE_TARGET_TYPE (type) = underlying_type;
16155 }
16156
16157 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16158 if (attr != nullptr)
16159 {
16160 TYPE_LENGTH (type) = DW_UNSND (attr);
16161 }
16162 else
16163 {
16164 TYPE_LENGTH (type) = 0;
16165 }
16166
16167 maybe_set_alignment (cu, die, type);
16168
16169 /* The enumeration DIE can be incomplete. In Ada, any type can be
16170 declared as private in the package spec, and then defined only
16171 inside the package body. Such types are known as Taft Amendment
16172 Types. When another package uses such a type, an incomplete DIE
16173 may be generated by the compiler. */
16174 if (die_is_declaration (die, cu))
16175 TYPE_STUB (type) = 1;
16176
16177 /* If this type has an underlying type that is not a stub, then we
16178 may use its attributes. We always use the "unsigned" attribute
16179 in this situation, because ordinarily we guess whether the type
16180 is unsigned -- but the guess can be wrong and the underlying type
16181 can tell us the reality. However, we defer to a local size
16182 attribute if one exists, because this lets the compiler override
16183 the underlying type if needed. */
16184 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16185 {
16186 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16187 underlying_type = check_typedef (underlying_type);
16188 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16189 if (TYPE_LENGTH (type) == 0)
16190 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16191 if (TYPE_RAW_ALIGN (type) == 0
16192 && TYPE_RAW_ALIGN (underlying_type) != 0)
16193 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16194 }
16195
16196 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16197
16198 set_die_type (die, type, cu);
16199
16200 /* Finish the creation of this type by using the enum's children.
16201 Note that, as usual, this must come after set_die_type to avoid
16202 infinite recursion when trying to compute the names of the
16203 enumerators. */
16204 update_enumeration_type_from_children (die, type, cu);
16205
16206 return type;
16207 }
16208
16209 /* Given a pointer to a die which begins an enumeration, process all
16210 the dies that define the members of the enumeration, and create the
16211 symbol for the enumeration type.
16212
16213 NOTE: We reverse the order of the element list. */
16214
16215 static void
16216 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16217 {
16218 struct type *this_type;
16219
16220 this_type = get_die_type (die, cu);
16221 if (this_type == NULL)
16222 this_type = read_enumeration_type (die, cu);
16223
16224 if (die->child != NULL)
16225 {
16226 struct die_info *child_die;
16227 const char *name;
16228
16229 child_die = die->child;
16230 while (child_die && child_die->tag)
16231 {
16232 if (child_die->tag != DW_TAG_enumerator)
16233 {
16234 process_die (child_die, cu);
16235 }
16236 else
16237 {
16238 name = dwarf2_name (child_die, cu);
16239 if (name)
16240 new_symbol (child_die, this_type, cu);
16241 }
16242
16243 child_die = child_die->sibling;
16244 }
16245 }
16246
16247 /* If we are reading an enum from a .debug_types unit, and the enum
16248 is a declaration, and the enum is not the signatured type in the
16249 unit, then we do not want to add a symbol for it. Adding a
16250 symbol would in some cases obscure the true definition of the
16251 enum, giving users an incomplete type when the definition is
16252 actually available. Note that we do not want to do this for all
16253 enums which are just declarations, because C++0x allows forward
16254 enum declarations. */
16255 if (cu->per_cu->is_debug_types
16256 && die_is_declaration (die, cu))
16257 {
16258 struct signatured_type *sig_type;
16259
16260 sig_type = (struct signatured_type *) cu->per_cu;
16261 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16262 if (sig_type->type_offset_in_section != die->sect_off)
16263 return;
16264 }
16265
16266 new_symbol (die, this_type, cu);
16267 }
16268
16269 /* Extract all information from a DW_TAG_array_type DIE and put it in
16270 the DIE's type field. For now, this only handles one dimensional
16271 arrays. */
16272
16273 static struct type *
16274 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16275 {
16276 struct objfile *objfile = cu->per_objfile->objfile;
16277 struct die_info *child_die;
16278 struct type *type;
16279 struct type *element_type, *range_type, *index_type;
16280 struct attribute *attr;
16281 const char *name;
16282 struct dynamic_prop *byte_stride_prop = NULL;
16283 unsigned int bit_stride = 0;
16284
16285 element_type = die_type (die, cu);
16286
16287 /* The die_type call above may have already set the type for this DIE. */
16288 type = get_die_type (die, cu);
16289 if (type)
16290 return type;
16291
16292 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16293 if (attr != NULL)
16294 {
16295 int stride_ok;
16296 struct type *prop_type = cu->addr_sized_int_type (false);
16297
16298 byte_stride_prop
16299 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16300 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16301 prop_type);
16302 if (!stride_ok)
16303 {
16304 complaint (_("unable to read array DW_AT_byte_stride "
16305 " - DIE at %s [in module %s]"),
16306 sect_offset_str (die->sect_off),
16307 objfile_name (cu->per_objfile->objfile));
16308 /* Ignore this attribute. We will likely not be able to print
16309 arrays of this type correctly, but there is little we can do
16310 to help if we cannot read the attribute's value. */
16311 byte_stride_prop = NULL;
16312 }
16313 }
16314
16315 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16316 if (attr != NULL)
16317 bit_stride = DW_UNSND (attr);
16318
16319 /* Irix 6.2 native cc creates array types without children for
16320 arrays with unspecified length. */
16321 if (die->child == NULL)
16322 {
16323 index_type = objfile_type (objfile)->builtin_int;
16324 range_type = create_static_range_type (NULL, index_type, 0, -1);
16325 type = create_array_type_with_stride (NULL, element_type, range_type,
16326 byte_stride_prop, bit_stride);
16327 return set_die_type (die, type, cu);
16328 }
16329
16330 std::vector<struct type *> range_types;
16331 child_die = die->child;
16332 while (child_die && child_die->tag)
16333 {
16334 if (child_die->tag == DW_TAG_subrange_type)
16335 {
16336 struct type *child_type = read_type_die (child_die, cu);
16337
16338 if (child_type != NULL)
16339 {
16340 /* The range type was succesfully read. Save it for the
16341 array type creation. */
16342 range_types.push_back (child_type);
16343 }
16344 }
16345 child_die = child_die->sibling;
16346 }
16347
16348 /* Dwarf2 dimensions are output from left to right, create the
16349 necessary array types in backwards order. */
16350
16351 type = element_type;
16352
16353 if (read_array_order (die, cu) == DW_ORD_col_major)
16354 {
16355 int i = 0;
16356
16357 while (i < range_types.size ())
16358 type = create_array_type_with_stride (NULL, type, range_types[i++],
16359 byte_stride_prop, bit_stride);
16360 }
16361 else
16362 {
16363 size_t ndim = range_types.size ();
16364 while (ndim-- > 0)
16365 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16366 byte_stride_prop, bit_stride);
16367 }
16368
16369 /* Understand Dwarf2 support for vector types (like they occur on
16370 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16371 array type. This is not part of the Dwarf2/3 standard yet, but a
16372 custom vendor extension. The main difference between a regular
16373 array and the vector variant is that vectors are passed by value
16374 to functions. */
16375 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16376 if (attr != nullptr)
16377 make_vector_type (type);
16378
16379 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16380 implementation may choose to implement triple vectors using this
16381 attribute. */
16382 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16383 if (attr != nullptr)
16384 {
16385 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16386 TYPE_LENGTH (type) = DW_UNSND (attr);
16387 else
16388 complaint (_("DW_AT_byte_size for array type smaller "
16389 "than the total size of elements"));
16390 }
16391
16392 name = dwarf2_name (die, cu);
16393 if (name)
16394 type->set_name (name);
16395
16396 maybe_set_alignment (cu, die, type);
16397
16398 /* Install the type in the die. */
16399 set_die_type (die, type, cu);
16400
16401 /* set_die_type should be already done. */
16402 set_descriptive_type (type, die, cu);
16403
16404 return type;
16405 }
16406
16407 static enum dwarf_array_dim_ordering
16408 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16409 {
16410 struct attribute *attr;
16411
16412 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16413
16414 if (attr != nullptr)
16415 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16416
16417 /* GNU F77 is a special case, as at 08/2004 array type info is the
16418 opposite order to the dwarf2 specification, but data is still
16419 laid out as per normal fortran.
16420
16421 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16422 version checking. */
16423
16424 if (cu->language == language_fortran
16425 && cu->producer && strstr (cu->producer, "GNU F77"))
16426 {
16427 return DW_ORD_row_major;
16428 }
16429
16430 switch (cu->language_defn->la_array_ordering)
16431 {
16432 case array_column_major:
16433 return DW_ORD_col_major;
16434 case array_row_major:
16435 default:
16436 return DW_ORD_row_major;
16437 };
16438 }
16439
16440 /* Extract all information from a DW_TAG_set_type DIE and put it in
16441 the DIE's type field. */
16442
16443 static struct type *
16444 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16445 {
16446 struct type *domain_type, *set_type;
16447 struct attribute *attr;
16448
16449 domain_type = die_type (die, cu);
16450
16451 /* The die_type call above may have already set the type for this DIE. */
16452 set_type = get_die_type (die, cu);
16453 if (set_type)
16454 return set_type;
16455
16456 set_type = create_set_type (NULL, domain_type);
16457
16458 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16459 if (attr != nullptr)
16460 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16461
16462 maybe_set_alignment (cu, die, set_type);
16463
16464 return set_die_type (die, set_type, cu);
16465 }
16466
16467 /* A helper for read_common_block that creates a locexpr baton.
16468 SYM is the symbol which we are marking as computed.
16469 COMMON_DIE is the DIE for the common block.
16470 COMMON_LOC is the location expression attribute for the common
16471 block itself.
16472 MEMBER_LOC is the location expression attribute for the particular
16473 member of the common block that we are processing.
16474 CU is the CU from which the above come. */
16475
16476 static void
16477 mark_common_block_symbol_computed (struct symbol *sym,
16478 struct die_info *common_die,
16479 struct attribute *common_loc,
16480 struct attribute *member_loc,
16481 struct dwarf2_cu *cu)
16482 {
16483 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16484 struct objfile *objfile = per_objfile->objfile;
16485 struct dwarf2_locexpr_baton *baton;
16486 gdb_byte *ptr;
16487 unsigned int cu_off;
16488 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16489 LONGEST offset = 0;
16490
16491 gdb_assert (common_loc && member_loc);
16492 gdb_assert (common_loc->form_is_block ());
16493 gdb_assert (member_loc->form_is_block ()
16494 || member_loc->form_is_constant ());
16495
16496 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16497 baton->per_objfile = per_objfile;
16498 baton->per_cu = cu->per_cu;
16499 gdb_assert (baton->per_cu);
16500
16501 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16502
16503 if (member_loc->form_is_constant ())
16504 {
16505 offset = member_loc->constant_value (0);
16506 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16507 }
16508 else
16509 baton->size += DW_BLOCK (member_loc)->size;
16510
16511 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16512 baton->data = ptr;
16513
16514 *ptr++ = DW_OP_call4;
16515 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16516 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16517 ptr += 4;
16518
16519 if (member_loc->form_is_constant ())
16520 {
16521 *ptr++ = DW_OP_addr;
16522 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16523 ptr += cu->header.addr_size;
16524 }
16525 else
16526 {
16527 /* We have to copy the data here, because DW_OP_call4 will only
16528 use a DW_AT_location attribute. */
16529 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16530 ptr += DW_BLOCK (member_loc)->size;
16531 }
16532
16533 *ptr++ = DW_OP_plus;
16534 gdb_assert (ptr - baton->data == baton->size);
16535
16536 SYMBOL_LOCATION_BATON (sym) = baton;
16537 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16538 }
16539
16540 /* Create appropriate locally-scoped variables for all the
16541 DW_TAG_common_block entries. Also create a struct common_block
16542 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16543 is used to separate the common blocks name namespace from regular
16544 variable names. */
16545
16546 static void
16547 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16548 {
16549 struct attribute *attr;
16550
16551 attr = dwarf2_attr (die, DW_AT_location, cu);
16552 if (attr != nullptr)
16553 {
16554 /* Support the .debug_loc offsets. */
16555 if (attr->form_is_block ())
16556 {
16557 /* Ok. */
16558 }
16559 else if (attr->form_is_section_offset ())
16560 {
16561 dwarf2_complex_location_expr_complaint ();
16562 attr = NULL;
16563 }
16564 else
16565 {
16566 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16567 "common block member");
16568 attr = NULL;
16569 }
16570 }
16571
16572 if (die->child != NULL)
16573 {
16574 struct objfile *objfile = cu->per_objfile->objfile;
16575 struct die_info *child_die;
16576 size_t n_entries = 0, size;
16577 struct common_block *common_block;
16578 struct symbol *sym;
16579
16580 for (child_die = die->child;
16581 child_die && child_die->tag;
16582 child_die = child_die->sibling)
16583 ++n_entries;
16584
16585 size = (sizeof (struct common_block)
16586 + (n_entries - 1) * sizeof (struct symbol *));
16587 common_block
16588 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16589 size);
16590 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16591 common_block->n_entries = 0;
16592
16593 for (child_die = die->child;
16594 child_die && child_die->tag;
16595 child_die = child_die->sibling)
16596 {
16597 /* Create the symbol in the DW_TAG_common_block block in the current
16598 symbol scope. */
16599 sym = new_symbol (child_die, NULL, cu);
16600 if (sym != NULL)
16601 {
16602 struct attribute *member_loc;
16603
16604 common_block->contents[common_block->n_entries++] = sym;
16605
16606 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16607 cu);
16608 if (member_loc)
16609 {
16610 /* GDB has handled this for a long time, but it is
16611 not specified by DWARF. It seems to have been
16612 emitted by gfortran at least as recently as:
16613 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16614 complaint (_("Variable in common block has "
16615 "DW_AT_data_member_location "
16616 "- DIE at %s [in module %s]"),
16617 sect_offset_str (child_die->sect_off),
16618 objfile_name (objfile));
16619
16620 if (member_loc->form_is_section_offset ())
16621 dwarf2_complex_location_expr_complaint ();
16622 else if (member_loc->form_is_constant ()
16623 || member_loc->form_is_block ())
16624 {
16625 if (attr != nullptr)
16626 mark_common_block_symbol_computed (sym, die, attr,
16627 member_loc, cu);
16628 }
16629 else
16630 dwarf2_complex_location_expr_complaint ();
16631 }
16632 }
16633 }
16634
16635 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16636 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16637 }
16638 }
16639
16640 /* Create a type for a C++ namespace. */
16641
16642 static struct type *
16643 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16644 {
16645 struct objfile *objfile = cu->per_objfile->objfile;
16646 const char *previous_prefix, *name;
16647 int is_anonymous;
16648 struct type *type;
16649
16650 /* For extensions, reuse the type of the original namespace. */
16651 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16652 {
16653 struct die_info *ext_die;
16654 struct dwarf2_cu *ext_cu = cu;
16655
16656 ext_die = dwarf2_extension (die, &ext_cu);
16657 type = read_type_die (ext_die, ext_cu);
16658
16659 /* EXT_CU may not be the same as CU.
16660 Ensure TYPE is recorded with CU in die_type_hash. */
16661 return set_die_type (die, type, cu);
16662 }
16663
16664 name = namespace_name (die, &is_anonymous, cu);
16665
16666 /* Now build the name of the current namespace. */
16667
16668 previous_prefix = determine_prefix (die, cu);
16669 if (previous_prefix[0] != '\0')
16670 name = typename_concat (&objfile->objfile_obstack,
16671 previous_prefix, name, 0, cu);
16672
16673 /* Create the type. */
16674 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16675
16676 return set_die_type (die, type, cu);
16677 }
16678
16679 /* Read a namespace scope. */
16680
16681 static void
16682 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16683 {
16684 struct objfile *objfile = cu->per_objfile->objfile;
16685 int is_anonymous;
16686
16687 /* Add a symbol associated to this if we haven't seen the namespace
16688 before. Also, add a using directive if it's an anonymous
16689 namespace. */
16690
16691 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16692 {
16693 struct type *type;
16694
16695 type = read_type_die (die, cu);
16696 new_symbol (die, type, cu);
16697
16698 namespace_name (die, &is_anonymous, cu);
16699 if (is_anonymous)
16700 {
16701 const char *previous_prefix = determine_prefix (die, cu);
16702
16703 std::vector<const char *> excludes;
16704 add_using_directive (using_directives (cu),
16705 previous_prefix, type->name (), NULL,
16706 NULL, excludes, 0, &objfile->objfile_obstack);
16707 }
16708 }
16709
16710 if (die->child != NULL)
16711 {
16712 struct die_info *child_die = die->child;
16713
16714 while (child_die && child_die->tag)
16715 {
16716 process_die (child_die, cu);
16717 child_die = child_die->sibling;
16718 }
16719 }
16720 }
16721
16722 /* Read a Fortran module as type. This DIE can be only a declaration used for
16723 imported module. Still we need that type as local Fortran "use ... only"
16724 declaration imports depend on the created type in determine_prefix. */
16725
16726 static struct type *
16727 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16728 {
16729 struct objfile *objfile = cu->per_objfile->objfile;
16730 const char *module_name;
16731 struct type *type;
16732
16733 module_name = dwarf2_name (die, cu);
16734 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16735
16736 return set_die_type (die, type, cu);
16737 }
16738
16739 /* Read a Fortran module. */
16740
16741 static void
16742 read_module (struct die_info *die, struct dwarf2_cu *cu)
16743 {
16744 struct die_info *child_die = die->child;
16745 struct type *type;
16746
16747 type = read_type_die (die, cu);
16748 new_symbol (die, type, cu);
16749
16750 while (child_die && child_die->tag)
16751 {
16752 process_die (child_die, cu);
16753 child_die = child_die->sibling;
16754 }
16755 }
16756
16757 /* Return the name of the namespace represented by DIE. Set
16758 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16759 namespace. */
16760
16761 static const char *
16762 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16763 {
16764 struct die_info *current_die;
16765 const char *name = NULL;
16766
16767 /* Loop through the extensions until we find a name. */
16768
16769 for (current_die = die;
16770 current_die != NULL;
16771 current_die = dwarf2_extension (die, &cu))
16772 {
16773 /* We don't use dwarf2_name here so that we can detect the absence
16774 of a name -> anonymous namespace. */
16775 name = dwarf2_string_attr (die, DW_AT_name, cu);
16776
16777 if (name != NULL)
16778 break;
16779 }
16780
16781 /* Is it an anonymous namespace? */
16782
16783 *is_anonymous = (name == NULL);
16784 if (*is_anonymous)
16785 name = CP_ANONYMOUS_NAMESPACE_STR;
16786
16787 return name;
16788 }
16789
16790 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16791 the user defined type vector. */
16792
16793 static struct type *
16794 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16795 {
16796 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16797 struct comp_unit_head *cu_header = &cu->header;
16798 struct type *type;
16799 struct attribute *attr_byte_size;
16800 struct attribute *attr_address_class;
16801 int byte_size, addr_class;
16802 struct type *target_type;
16803
16804 target_type = die_type (die, cu);
16805
16806 /* The die_type call above may have already set the type for this DIE. */
16807 type = get_die_type (die, cu);
16808 if (type)
16809 return type;
16810
16811 type = lookup_pointer_type (target_type);
16812
16813 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16814 if (attr_byte_size)
16815 byte_size = DW_UNSND (attr_byte_size);
16816 else
16817 byte_size = cu_header->addr_size;
16818
16819 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16820 if (attr_address_class)
16821 addr_class = DW_UNSND (attr_address_class);
16822 else
16823 addr_class = DW_ADDR_none;
16824
16825 ULONGEST alignment = get_alignment (cu, die);
16826
16827 /* If the pointer size, alignment, or address class is different
16828 than the default, create a type variant marked as such and set
16829 the length accordingly. */
16830 if (TYPE_LENGTH (type) != byte_size
16831 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16832 && alignment != TYPE_RAW_ALIGN (type))
16833 || addr_class != DW_ADDR_none)
16834 {
16835 if (gdbarch_address_class_type_flags_p (gdbarch))
16836 {
16837 int type_flags;
16838
16839 type_flags = gdbarch_address_class_type_flags
16840 (gdbarch, byte_size, addr_class);
16841 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16842 == 0);
16843 type = make_type_with_address_space (type, type_flags);
16844 }
16845 else if (TYPE_LENGTH (type) != byte_size)
16846 {
16847 complaint (_("invalid pointer size %d"), byte_size);
16848 }
16849 else if (TYPE_RAW_ALIGN (type) != alignment)
16850 {
16851 complaint (_("Invalid DW_AT_alignment"
16852 " - DIE at %s [in module %s]"),
16853 sect_offset_str (die->sect_off),
16854 objfile_name (cu->per_objfile->objfile));
16855 }
16856 else
16857 {
16858 /* Should we also complain about unhandled address classes? */
16859 }
16860 }
16861
16862 TYPE_LENGTH (type) = byte_size;
16863 set_type_align (type, alignment);
16864 return set_die_type (die, type, cu);
16865 }
16866
16867 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16868 the user defined type vector. */
16869
16870 static struct type *
16871 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16872 {
16873 struct type *type;
16874 struct type *to_type;
16875 struct type *domain;
16876
16877 to_type = die_type (die, cu);
16878 domain = die_containing_type (die, cu);
16879
16880 /* The calls above may have already set the type for this DIE. */
16881 type = get_die_type (die, cu);
16882 if (type)
16883 return type;
16884
16885 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16886 type = lookup_methodptr_type (to_type);
16887 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16888 {
16889 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16890
16891 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16892 to_type->fields (), to_type->num_fields (),
16893 TYPE_VARARGS (to_type));
16894 type = lookup_methodptr_type (new_type);
16895 }
16896 else
16897 type = lookup_memberptr_type (to_type, domain);
16898
16899 return set_die_type (die, type, cu);
16900 }
16901
16902 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16903 the user defined type vector. */
16904
16905 static struct type *
16906 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16907 enum type_code refcode)
16908 {
16909 struct comp_unit_head *cu_header = &cu->header;
16910 struct type *type, *target_type;
16911 struct attribute *attr;
16912
16913 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16914
16915 target_type = die_type (die, cu);
16916
16917 /* The die_type call above may have already set the type for this DIE. */
16918 type = get_die_type (die, cu);
16919 if (type)
16920 return type;
16921
16922 type = lookup_reference_type (target_type, refcode);
16923 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16924 if (attr != nullptr)
16925 {
16926 TYPE_LENGTH (type) = DW_UNSND (attr);
16927 }
16928 else
16929 {
16930 TYPE_LENGTH (type) = cu_header->addr_size;
16931 }
16932 maybe_set_alignment (cu, die, type);
16933 return set_die_type (die, type, cu);
16934 }
16935
16936 /* Add the given cv-qualifiers to the element type of the array. GCC
16937 outputs DWARF type qualifiers that apply to an array, not the
16938 element type. But GDB relies on the array element type to carry
16939 the cv-qualifiers. This mimics section 6.7.3 of the C99
16940 specification. */
16941
16942 static struct type *
16943 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16944 struct type *base_type, int cnst, int voltl)
16945 {
16946 struct type *el_type, *inner_array;
16947
16948 base_type = copy_type (base_type);
16949 inner_array = base_type;
16950
16951 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16952 {
16953 TYPE_TARGET_TYPE (inner_array) =
16954 copy_type (TYPE_TARGET_TYPE (inner_array));
16955 inner_array = TYPE_TARGET_TYPE (inner_array);
16956 }
16957
16958 el_type = TYPE_TARGET_TYPE (inner_array);
16959 cnst |= TYPE_CONST (el_type);
16960 voltl |= TYPE_VOLATILE (el_type);
16961 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16962
16963 return set_die_type (die, base_type, cu);
16964 }
16965
16966 static struct type *
16967 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16968 {
16969 struct type *base_type, *cv_type;
16970
16971 base_type = die_type (die, cu);
16972
16973 /* The die_type call above may have already set the type for this DIE. */
16974 cv_type = get_die_type (die, cu);
16975 if (cv_type)
16976 return cv_type;
16977
16978 /* In case the const qualifier is applied to an array type, the element type
16979 is so qualified, not the array type (section 6.7.3 of C99). */
16980 if (base_type->code () == TYPE_CODE_ARRAY)
16981 return add_array_cv_type (die, cu, base_type, 1, 0);
16982
16983 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16984 return set_die_type (die, cv_type, cu);
16985 }
16986
16987 static struct type *
16988 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16989 {
16990 struct type *base_type, *cv_type;
16991
16992 base_type = die_type (die, cu);
16993
16994 /* The die_type call above may have already set the type for this DIE. */
16995 cv_type = get_die_type (die, cu);
16996 if (cv_type)
16997 return cv_type;
16998
16999 /* In case the volatile qualifier is applied to an array type, the
17000 element type is so qualified, not the array type (section 6.7.3
17001 of C99). */
17002 if (base_type->code () == TYPE_CODE_ARRAY)
17003 return add_array_cv_type (die, cu, base_type, 0, 1);
17004
17005 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17006 return set_die_type (die, cv_type, cu);
17007 }
17008
17009 /* Handle DW_TAG_restrict_type. */
17010
17011 static struct type *
17012 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17013 {
17014 struct type *base_type, *cv_type;
17015
17016 base_type = die_type (die, cu);
17017
17018 /* The die_type call above may have already set the type for this DIE. */
17019 cv_type = get_die_type (die, cu);
17020 if (cv_type)
17021 return cv_type;
17022
17023 cv_type = make_restrict_type (base_type);
17024 return set_die_type (die, cv_type, cu);
17025 }
17026
17027 /* Handle DW_TAG_atomic_type. */
17028
17029 static struct type *
17030 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17031 {
17032 struct type *base_type, *cv_type;
17033
17034 base_type = die_type (die, cu);
17035
17036 /* The die_type call above may have already set the type for this DIE. */
17037 cv_type = get_die_type (die, cu);
17038 if (cv_type)
17039 return cv_type;
17040
17041 cv_type = make_atomic_type (base_type);
17042 return set_die_type (die, cv_type, cu);
17043 }
17044
17045 /* Extract all information from a DW_TAG_string_type DIE and add to
17046 the user defined type vector. It isn't really a user defined type,
17047 but it behaves like one, with other DIE's using an AT_user_def_type
17048 attribute to reference it. */
17049
17050 static struct type *
17051 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17052 {
17053 struct objfile *objfile = cu->per_objfile->objfile;
17054 struct gdbarch *gdbarch = objfile->arch ();
17055 struct type *type, *range_type, *index_type, *char_type;
17056 struct attribute *attr;
17057 struct dynamic_prop prop;
17058 bool length_is_constant = true;
17059 LONGEST length;
17060
17061 /* There are a couple of places where bit sizes might be made use of
17062 when parsing a DW_TAG_string_type, however, no producer that we know
17063 of make use of these. Handling bit sizes that are a multiple of the
17064 byte size is easy enough, but what about other bit sizes? Lets deal
17065 with that problem when we have to. Warn about these attributes being
17066 unsupported, then parse the type and ignore them like we always
17067 have. */
17068 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17069 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17070 {
17071 static bool warning_printed = false;
17072 if (!warning_printed)
17073 {
17074 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17075 "currently supported on DW_TAG_string_type."));
17076 warning_printed = true;
17077 }
17078 }
17079
17080 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17081 if (attr != nullptr && !attr->form_is_constant ())
17082 {
17083 /* The string length describes the location at which the length of
17084 the string can be found. The size of the length field can be
17085 specified with one of the attributes below. */
17086 struct type *prop_type;
17087 struct attribute *len
17088 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17089 if (len == nullptr)
17090 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17091 if (len != nullptr && len->form_is_constant ())
17092 {
17093 /* Pass 0 as the default as we know this attribute is constant
17094 and the default value will not be returned. */
17095 LONGEST sz = len->constant_value (0);
17096 prop_type = cu->per_objfile->int_type (sz, true);
17097 }
17098 else
17099 {
17100 /* If the size is not specified then we assume it is the size of
17101 an address on this target. */
17102 prop_type = cu->addr_sized_int_type (true);
17103 }
17104
17105 /* Convert the attribute into a dynamic property. */
17106 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17107 length = 1;
17108 else
17109 length_is_constant = false;
17110 }
17111 else if (attr != nullptr)
17112 {
17113 /* This DW_AT_string_length just contains the length with no
17114 indirection. There's no need to create a dynamic property in this
17115 case. Pass 0 for the default value as we know it will not be
17116 returned in this case. */
17117 length = attr->constant_value (0);
17118 }
17119 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17120 {
17121 /* We don't currently support non-constant byte sizes for strings. */
17122 length = attr->constant_value (1);
17123 }
17124 else
17125 {
17126 /* Use 1 as a fallback length if we have nothing else. */
17127 length = 1;
17128 }
17129
17130 index_type = objfile_type (objfile)->builtin_int;
17131 if (length_is_constant)
17132 range_type = create_static_range_type (NULL, index_type, 1, length);
17133 else
17134 {
17135 struct dynamic_prop low_bound;
17136
17137 low_bound.kind = PROP_CONST;
17138 low_bound.data.const_val = 1;
17139 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17140 }
17141 char_type = language_string_char_type (cu->language_defn, gdbarch);
17142 type = create_string_type (NULL, char_type, range_type);
17143
17144 return set_die_type (die, type, cu);
17145 }
17146
17147 /* Assuming that DIE corresponds to a function, returns nonzero
17148 if the function is prototyped. */
17149
17150 static int
17151 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17152 {
17153 struct attribute *attr;
17154
17155 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17156 if (attr && (DW_UNSND (attr) != 0))
17157 return 1;
17158
17159 /* The DWARF standard implies that the DW_AT_prototyped attribute
17160 is only meaningful for C, but the concept also extends to other
17161 languages that allow unprototyped functions (Eg: Objective C).
17162 For all other languages, assume that functions are always
17163 prototyped. */
17164 if (cu->language != language_c
17165 && cu->language != language_objc
17166 && cu->language != language_opencl)
17167 return 1;
17168
17169 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17170 prototyped and unprototyped functions; default to prototyped,
17171 since that is more common in modern code (and RealView warns
17172 about unprototyped functions). */
17173 if (producer_is_realview (cu->producer))
17174 return 1;
17175
17176 return 0;
17177 }
17178
17179 /* Handle DIES due to C code like:
17180
17181 struct foo
17182 {
17183 int (*funcp)(int a, long l);
17184 int b;
17185 };
17186
17187 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17188
17189 static struct type *
17190 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17191 {
17192 struct objfile *objfile = cu->per_objfile->objfile;
17193 struct type *type; /* Type that this function returns. */
17194 struct type *ftype; /* Function that returns above type. */
17195 struct attribute *attr;
17196
17197 type = die_type (die, cu);
17198
17199 /* The die_type call above may have already set the type for this DIE. */
17200 ftype = get_die_type (die, cu);
17201 if (ftype)
17202 return ftype;
17203
17204 ftype = lookup_function_type (type);
17205
17206 if (prototyped_function_p (die, cu))
17207 TYPE_PROTOTYPED (ftype) = 1;
17208
17209 /* Store the calling convention in the type if it's available in
17210 the subroutine die. Otherwise set the calling convention to
17211 the default value DW_CC_normal. */
17212 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17213 if (attr != nullptr
17214 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17215 TYPE_CALLING_CONVENTION (ftype)
17216 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17217 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17218 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17219 else
17220 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17221
17222 /* Record whether the function returns normally to its caller or not
17223 if the DWARF producer set that information. */
17224 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17225 if (attr && (DW_UNSND (attr) != 0))
17226 TYPE_NO_RETURN (ftype) = 1;
17227
17228 /* We need to add the subroutine type to the die immediately so
17229 we don't infinitely recurse when dealing with parameters
17230 declared as the same subroutine type. */
17231 set_die_type (die, ftype, cu);
17232
17233 if (die->child != NULL)
17234 {
17235 struct type *void_type = objfile_type (objfile)->builtin_void;
17236 struct die_info *child_die;
17237 int nparams, iparams;
17238
17239 /* Count the number of parameters.
17240 FIXME: GDB currently ignores vararg functions, but knows about
17241 vararg member functions. */
17242 nparams = 0;
17243 child_die = die->child;
17244 while (child_die && child_die->tag)
17245 {
17246 if (child_die->tag == DW_TAG_formal_parameter)
17247 nparams++;
17248 else if (child_die->tag == DW_TAG_unspecified_parameters)
17249 TYPE_VARARGS (ftype) = 1;
17250 child_die = child_die->sibling;
17251 }
17252
17253 /* Allocate storage for parameters and fill them in. */
17254 ftype->set_num_fields (nparams);
17255 ftype->set_fields
17256 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17257
17258 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17259 even if we error out during the parameters reading below. */
17260 for (iparams = 0; iparams < nparams; iparams++)
17261 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17262
17263 iparams = 0;
17264 child_die = die->child;
17265 while (child_die && child_die->tag)
17266 {
17267 if (child_die->tag == DW_TAG_formal_parameter)
17268 {
17269 struct type *arg_type;
17270
17271 /* DWARF version 2 has no clean way to discern C++
17272 static and non-static member functions. G++ helps
17273 GDB by marking the first parameter for non-static
17274 member functions (which is the this pointer) as
17275 artificial. We pass this information to
17276 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17277
17278 DWARF version 3 added DW_AT_object_pointer, which GCC
17279 4.5 does not yet generate. */
17280 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17281 if (attr != nullptr)
17282 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17283 else
17284 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17285 arg_type = die_type (child_die, cu);
17286
17287 /* RealView does not mark THIS as const, which the testsuite
17288 expects. GCC marks THIS as const in method definitions,
17289 but not in the class specifications (GCC PR 43053). */
17290 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17291 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17292 {
17293 int is_this = 0;
17294 struct dwarf2_cu *arg_cu = cu;
17295 const char *name = dwarf2_name (child_die, cu);
17296
17297 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17298 if (attr != nullptr)
17299 {
17300 /* If the compiler emits this, use it. */
17301 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17302 is_this = 1;
17303 }
17304 else if (name && strcmp (name, "this") == 0)
17305 /* Function definitions will have the argument names. */
17306 is_this = 1;
17307 else if (name == NULL && iparams == 0)
17308 /* Declarations may not have the names, so like
17309 elsewhere in GDB, assume an artificial first
17310 argument is "this". */
17311 is_this = 1;
17312
17313 if (is_this)
17314 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17315 arg_type, 0);
17316 }
17317
17318 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17319 iparams++;
17320 }
17321 child_die = child_die->sibling;
17322 }
17323 }
17324
17325 return ftype;
17326 }
17327
17328 static struct type *
17329 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17330 {
17331 struct objfile *objfile = cu->per_objfile->objfile;
17332 const char *name = NULL;
17333 struct type *this_type, *target_type;
17334
17335 name = dwarf2_full_name (NULL, die, cu);
17336 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17337 TYPE_TARGET_STUB (this_type) = 1;
17338 set_die_type (die, this_type, cu);
17339 target_type = die_type (die, cu);
17340 if (target_type != this_type)
17341 TYPE_TARGET_TYPE (this_type) = target_type;
17342 else
17343 {
17344 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17345 spec and cause infinite loops in GDB. */
17346 complaint (_("Self-referential DW_TAG_typedef "
17347 "- DIE at %s [in module %s]"),
17348 sect_offset_str (die->sect_off), objfile_name (objfile));
17349 TYPE_TARGET_TYPE (this_type) = NULL;
17350 }
17351 if (name == NULL)
17352 {
17353 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17354 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17355 Handle these by just returning the target type, rather than
17356 constructing an anonymous typedef type and trying to handle this
17357 elsewhere. */
17358 set_die_type (die, target_type, cu);
17359 return target_type;
17360 }
17361 return this_type;
17362 }
17363
17364 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17365 (which may be different from NAME) to the architecture back-end to allow
17366 it to guess the correct format if necessary. */
17367
17368 static struct type *
17369 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17370 const char *name_hint, enum bfd_endian byte_order)
17371 {
17372 struct gdbarch *gdbarch = objfile->arch ();
17373 const struct floatformat **format;
17374 struct type *type;
17375
17376 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17377 if (format)
17378 type = init_float_type (objfile, bits, name, format, byte_order);
17379 else
17380 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17381
17382 return type;
17383 }
17384
17385 /* Allocate an integer type of size BITS and name NAME. */
17386
17387 static struct type *
17388 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17389 int bits, int unsigned_p, const char *name)
17390 {
17391 struct type *type;
17392
17393 /* Versions of Intel's C Compiler generate an integer type called "void"
17394 instead of using DW_TAG_unspecified_type. This has been seen on
17395 at least versions 14, 17, and 18. */
17396 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17397 && strcmp (name, "void") == 0)
17398 type = objfile_type (objfile)->builtin_void;
17399 else
17400 type = init_integer_type (objfile, bits, unsigned_p, name);
17401
17402 return type;
17403 }
17404
17405 /* Initialise and return a floating point type of size BITS suitable for
17406 use as a component of a complex number. The NAME_HINT is passed through
17407 when initialising the floating point type and is the name of the complex
17408 type.
17409
17410 As DWARF doesn't currently provide an explicit name for the components
17411 of a complex number, but it can be helpful to have these components
17412 named, we try to select a suitable name based on the size of the
17413 component. */
17414 static struct type *
17415 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17416 struct objfile *objfile,
17417 int bits, const char *name_hint,
17418 enum bfd_endian byte_order)
17419 {
17420 gdbarch *gdbarch = objfile->arch ();
17421 struct type *tt = nullptr;
17422
17423 /* Try to find a suitable floating point builtin type of size BITS.
17424 We're going to use the name of this type as the name for the complex
17425 target type that we are about to create. */
17426 switch (cu->language)
17427 {
17428 case language_fortran:
17429 switch (bits)
17430 {
17431 case 32:
17432 tt = builtin_f_type (gdbarch)->builtin_real;
17433 break;
17434 case 64:
17435 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17436 break;
17437 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17438 case 128:
17439 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17440 break;
17441 }
17442 break;
17443 default:
17444 switch (bits)
17445 {
17446 case 32:
17447 tt = builtin_type (gdbarch)->builtin_float;
17448 break;
17449 case 64:
17450 tt = builtin_type (gdbarch)->builtin_double;
17451 break;
17452 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17453 case 128:
17454 tt = builtin_type (gdbarch)->builtin_long_double;
17455 break;
17456 }
17457 break;
17458 }
17459
17460 /* If the type we found doesn't match the size we were looking for, then
17461 pretend we didn't find a type at all, the complex target type we
17462 create will then be nameless. */
17463 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17464 tt = nullptr;
17465
17466 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17467 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17468 }
17469
17470 /* Find a representation of a given base type and install
17471 it in the TYPE field of the die. */
17472
17473 static struct type *
17474 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17475 {
17476 struct objfile *objfile = cu->per_objfile->objfile;
17477 struct type *type;
17478 struct attribute *attr;
17479 int encoding = 0, bits = 0;
17480 const char *name;
17481 gdbarch *arch;
17482
17483 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17484 if (attr != nullptr)
17485 encoding = DW_UNSND (attr);
17486 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17487 if (attr != nullptr)
17488 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17489 name = dwarf2_name (die, cu);
17490 if (!name)
17491 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17492
17493 arch = objfile->arch ();
17494 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17495
17496 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17497 if (attr)
17498 {
17499 int endianity = DW_UNSND (attr);
17500
17501 switch (endianity)
17502 {
17503 case DW_END_big:
17504 byte_order = BFD_ENDIAN_BIG;
17505 break;
17506 case DW_END_little:
17507 byte_order = BFD_ENDIAN_LITTLE;
17508 break;
17509 default:
17510 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17511 break;
17512 }
17513 }
17514
17515 switch (encoding)
17516 {
17517 case DW_ATE_address:
17518 /* Turn DW_ATE_address into a void * pointer. */
17519 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17520 type = init_pointer_type (objfile, bits, name, type);
17521 break;
17522 case DW_ATE_boolean:
17523 type = init_boolean_type (objfile, bits, 1, name);
17524 break;
17525 case DW_ATE_complex_float:
17526 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17527 byte_order);
17528 if (type->code () == TYPE_CODE_ERROR)
17529 {
17530 if (name == nullptr)
17531 {
17532 struct obstack *obstack
17533 = &cu->per_objfile->objfile->objfile_obstack;
17534 name = obconcat (obstack, "_Complex ", type->name (),
17535 nullptr);
17536 }
17537 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17538 }
17539 else
17540 type = init_complex_type (name, type);
17541 break;
17542 case DW_ATE_decimal_float:
17543 type = init_decfloat_type (objfile, bits, name);
17544 break;
17545 case DW_ATE_float:
17546 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17547 break;
17548 case DW_ATE_signed:
17549 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17550 break;
17551 case DW_ATE_unsigned:
17552 if (cu->language == language_fortran
17553 && name
17554 && startswith (name, "character("))
17555 type = init_character_type (objfile, bits, 1, name);
17556 else
17557 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17558 break;
17559 case DW_ATE_signed_char:
17560 if (cu->language == language_ada || cu->language == language_m2
17561 || cu->language == language_pascal
17562 || cu->language == language_fortran)
17563 type = init_character_type (objfile, bits, 0, name);
17564 else
17565 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17566 break;
17567 case DW_ATE_unsigned_char:
17568 if (cu->language == language_ada || cu->language == language_m2
17569 || cu->language == language_pascal
17570 || cu->language == language_fortran
17571 || cu->language == language_rust)
17572 type = init_character_type (objfile, bits, 1, name);
17573 else
17574 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17575 break;
17576 case DW_ATE_UTF:
17577 {
17578 if (bits == 16)
17579 type = builtin_type (arch)->builtin_char16;
17580 else if (bits == 32)
17581 type = builtin_type (arch)->builtin_char32;
17582 else
17583 {
17584 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17585 bits);
17586 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17587 }
17588 return set_die_type (die, type, cu);
17589 }
17590 break;
17591
17592 default:
17593 complaint (_("unsupported DW_AT_encoding: '%s'"),
17594 dwarf_type_encoding_name (encoding));
17595 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17596 break;
17597 }
17598
17599 if (name && strcmp (name, "char") == 0)
17600 TYPE_NOSIGN (type) = 1;
17601
17602 maybe_set_alignment (cu, die, type);
17603
17604 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17605
17606 return set_die_type (die, type, cu);
17607 }
17608
17609 /* Parse dwarf attribute if it's a block, reference or constant and put the
17610 resulting value of the attribute into struct bound_prop.
17611 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17612
17613 static int
17614 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17615 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17616 struct type *default_type)
17617 {
17618 struct dwarf2_property_baton *baton;
17619 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17620 struct objfile *objfile = per_objfile->objfile;
17621 struct obstack *obstack = &objfile->objfile_obstack;
17622
17623 gdb_assert (default_type != NULL);
17624
17625 if (attr == NULL || prop == NULL)
17626 return 0;
17627
17628 if (attr->form_is_block ())
17629 {
17630 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17631 baton->property_type = default_type;
17632 baton->locexpr.per_cu = cu->per_cu;
17633 baton->locexpr.per_objfile = per_objfile;
17634 baton->locexpr.size = DW_BLOCK (attr)->size;
17635 baton->locexpr.data = DW_BLOCK (attr)->data;
17636 switch (attr->name)
17637 {
17638 case DW_AT_string_length:
17639 baton->locexpr.is_reference = true;
17640 break;
17641 default:
17642 baton->locexpr.is_reference = false;
17643 break;
17644 }
17645 prop->data.baton = baton;
17646 prop->kind = PROP_LOCEXPR;
17647 gdb_assert (prop->data.baton != NULL);
17648 }
17649 else if (attr->form_is_ref ())
17650 {
17651 struct dwarf2_cu *target_cu = cu;
17652 struct die_info *target_die;
17653 struct attribute *target_attr;
17654
17655 target_die = follow_die_ref (die, attr, &target_cu);
17656 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17657 if (target_attr == NULL)
17658 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17659 target_cu);
17660 if (target_attr == NULL)
17661 return 0;
17662
17663 switch (target_attr->name)
17664 {
17665 case DW_AT_location:
17666 if (target_attr->form_is_section_offset ())
17667 {
17668 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17669 baton->property_type = die_type (target_die, target_cu);
17670 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17671 prop->data.baton = baton;
17672 prop->kind = PROP_LOCLIST;
17673 gdb_assert (prop->data.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->data.baton = baton;
17685 prop->kind = PROP_LOCEXPR;
17686 gdb_assert (prop->data.baton != NULL);
17687 }
17688 else
17689 {
17690 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17691 "dynamic property");
17692 return 0;
17693 }
17694 break;
17695 case DW_AT_data_member_location:
17696 {
17697 LONGEST offset;
17698
17699 if (!handle_data_member_location (target_die, target_cu,
17700 &offset))
17701 return 0;
17702
17703 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17704 baton->property_type = read_type_die (target_die->parent,
17705 target_cu);
17706 baton->offset_info.offset = offset;
17707 baton->offset_info.type = die_type (target_die, target_cu);
17708 prop->data.baton = baton;
17709 prop->kind = PROP_ADDR_OFFSET;
17710 break;
17711 }
17712 }
17713 }
17714 else if (attr->form_is_constant ())
17715 {
17716 prop->data.const_val = attr->constant_value (0);
17717 prop->kind = PROP_CONST;
17718 }
17719 else
17720 {
17721 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17722 dwarf2_name (die, cu));
17723 return 0;
17724 }
17725
17726 return 1;
17727 }
17728
17729 /* See read.h. */
17730
17731 struct type *
17732 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17733 {
17734 struct type *int_type;
17735
17736 /* Helper macro to examine the various builtin types. */
17737 #define TRY_TYPE(F) \
17738 int_type = (unsigned_p \
17739 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17740 : objfile_type (objfile)->builtin_ ## F); \
17741 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17742 return int_type
17743
17744 TRY_TYPE (char);
17745 TRY_TYPE (short);
17746 TRY_TYPE (int);
17747 TRY_TYPE (long);
17748 TRY_TYPE (long_long);
17749
17750 #undef TRY_TYPE
17751
17752 gdb_assert_not_reached ("unable to find suitable integer type");
17753 }
17754
17755 /* See read.h. */
17756
17757 struct type *
17758 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17759 {
17760 int addr_size = this->per_cu->addr_size ();
17761 return this->per_objfile->int_type (addr_size, unsigned_p);
17762 }
17763
17764 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17765 present (which is valid) then compute the default type based on the
17766 compilation units address size. */
17767
17768 static struct type *
17769 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17770 {
17771 struct type *index_type = die_type (die, cu);
17772
17773 /* Dwarf-2 specifications explicitly allows to create subrange types
17774 without specifying a base type.
17775 In that case, the base type must be set to the type of
17776 the lower bound, upper bound or count, in that order, if any of these
17777 three attributes references an object that has a type.
17778 If no base type is found, the Dwarf-2 specifications say that
17779 a signed integer type of size equal to the size of an address should
17780 be used.
17781 For the following C code: `extern char gdb_int [];'
17782 GCC produces an empty range DIE.
17783 FIXME: muller/2010-05-28: Possible references to object for low bound,
17784 high bound or count are not yet handled by this code. */
17785 if (index_type->code () == TYPE_CODE_VOID)
17786 index_type = cu->addr_sized_int_type (false);
17787
17788 return index_type;
17789 }
17790
17791 /* Read the given DW_AT_subrange DIE. */
17792
17793 static struct type *
17794 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17795 {
17796 struct type *base_type, *orig_base_type;
17797 struct type *range_type;
17798 struct attribute *attr;
17799 struct dynamic_prop low, high;
17800 int low_default_is_valid;
17801 int high_bound_is_count = 0;
17802 const char *name;
17803 ULONGEST negative_mask;
17804
17805 orig_base_type = read_subrange_index_type (die, cu);
17806
17807 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17808 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17809 creating the range type, but we use the result of check_typedef
17810 when examining properties of the type. */
17811 base_type = check_typedef (orig_base_type);
17812
17813 /* The die_type call above may have already set the type for this DIE. */
17814 range_type = get_die_type (die, cu);
17815 if (range_type)
17816 return range_type;
17817
17818 low.kind = PROP_CONST;
17819 high.kind = PROP_CONST;
17820 high.data.const_val = 0;
17821
17822 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17823 omitting DW_AT_lower_bound. */
17824 switch (cu->language)
17825 {
17826 case language_c:
17827 case language_cplus:
17828 low.data.const_val = 0;
17829 low_default_is_valid = 1;
17830 break;
17831 case language_fortran:
17832 low.data.const_val = 1;
17833 low_default_is_valid = 1;
17834 break;
17835 case language_d:
17836 case language_objc:
17837 case language_rust:
17838 low.data.const_val = 0;
17839 low_default_is_valid = (cu->header.version >= 4);
17840 break;
17841 case language_ada:
17842 case language_m2:
17843 case language_pascal:
17844 low.data.const_val = 1;
17845 low_default_is_valid = (cu->header.version >= 4);
17846 break;
17847 default:
17848 low.data.const_val = 0;
17849 low_default_is_valid = 0;
17850 break;
17851 }
17852
17853 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17854 if (attr != nullptr)
17855 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17856 else if (!low_default_is_valid)
17857 complaint (_("Missing DW_AT_lower_bound "
17858 "- DIE at %s [in module %s]"),
17859 sect_offset_str (die->sect_off),
17860 objfile_name (cu->per_objfile->objfile));
17861
17862 struct attribute *attr_ub, *attr_count;
17863 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17864 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17865 {
17866 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17867 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17868 {
17869 /* If bounds are constant do the final calculation here. */
17870 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17871 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17872 else
17873 high_bound_is_count = 1;
17874 }
17875 else
17876 {
17877 if (attr_ub != NULL)
17878 complaint (_("Unresolved DW_AT_upper_bound "
17879 "- DIE at %s [in module %s]"),
17880 sect_offset_str (die->sect_off),
17881 objfile_name (cu->per_objfile->objfile));
17882 if (attr_count != NULL)
17883 complaint (_("Unresolved DW_AT_count "
17884 "- DIE at %s [in module %s]"),
17885 sect_offset_str (die->sect_off),
17886 objfile_name (cu->per_objfile->objfile));
17887 }
17888 }
17889
17890 LONGEST bias = 0;
17891 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17892 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17893 bias = bias_attr->constant_value (0);
17894
17895 /* Normally, the DWARF producers are expected to use a signed
17896 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17897 But this is unfortunately not always the case, as witnessed
17898 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17899 is used instead. To work around that ambiguity, we treat
17900 the bounds as signed, and thus sign-extend their values, when
17901 the base type is signed. */
17902 negative_mask =
17903 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17904 if (low.kind == PROP_CONST
17905 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17906 low.data.const_val |= negative_mask;
17907 if (high.kind == PROP_CONST
17908 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17909 high.data.const_val |= negative_mask;
17910
17911 /* Check for bit and byte strides. */
17912 struct dynamic_prop byte_stride_prop;
17913 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17914 if (attr_byte_stride != nullptr)
17915 {
17916 struct type *prop_type = cu->addr_sized_int_type (false);
17917 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17918 prop_type);
17919 }
17920
17921 struct dynamic_prop bit_stride_prop;
17922 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17923 if (attr_bit_stride != nullptr)
17924 {
17925 /* It only makes sense to have either a bit or byte stride. */
17926 if (attr_byte_stride != nullptr)
17927 {
17928 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17929 "- DIE at %s [in module %s]"),
17930 sect_offset_str (die->sect_off),
17931 objfile_name (cu->per_objfile->objfile));
17932 attr_bit_stride = nullptr;
17933 }
17934 else
17935 {
17936 struct type *prop_type = cu->addr_sized_int_type (false);
17937 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17938 prop_type);
17939 }
17940 }
17941
17942 if (attr_byte_stride != nullptr
17943 || attr_bit_stride != nullptr)
17944 {
17945 bool byte_stride_p = (attr_byte_stride != nullptr);
17946 struct dynamic_prop *stride
17947 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17948
17949 range_type
17950 = create_range_type_with_stride (NULL, orig_base_type, &low,
17951 &high, bias, stride, byte_stride_p);
17952 }
17953 else
17954 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17955
17956 if (high_bound_is_count)
17957 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17958
17959 /* Ada expects an empty array on no boundary attributes. */
17960 if (attr == NULL && cu->language != language_ada)
17961 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17962
17963 name = dwarf2_name (die, cu);
17964 if (name)
17965 range_type->set_name (name);
17966
17967 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17968 if (attr != nullptr)
17969 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17970
17971 maybe_set_alignment (cu, die, range_type);
17972
17973 set_die_type (die, range_type, cu);
17974
17975 /* set_die_type should be already done. */
17976 set_descriptive_type (range_type, die, cu);
17977
17978 return range_type;
17979 }
17980
17981 static struct type *
17982 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17983 {
17984 struct type *type;
17985
17986 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17987 type->set_name (dwarf2_name (die, cu));
17988
17989 /* In Ada, an unspecified type is typically used when the description
17990 of the type is deferred to a different unit. When encountering
17991 such a type, we treat it as a stub, and try to resolve it later on,
17992 when needed. */
17993 if (cu->language == language_ada)
17994 TYPE_STUB (type) = 1;
17995
17996 return set_die_type (die, type, cu);
17997 }
17998
17999 /* Read a single die and all its descendents. Set the die's sibling
18000 field to NULL; set other fields in the die correctly, and set all
18001 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18002 location of the info_ptr after reading all of those dies. PARENT
18003 is the parent of the die in question. */
18004
18005 static struct die_info *
18006 read_die_and_children (const struct die_reader_specs *reader,
18007 const gdb_byte *info_ptr,
18008 const gdb_byte **new_info_ptr,
18009 struct die_info *parent)
18010 {
18011 struct die_info *die;
18012 const gdb_byte *cur_ptr;
18013
18014 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18015 if (die == NULL)
18016 {
18017 *new_info_ptr = cur_ptr;
18018 return NULL;
18019 }
18020 store_in_ref_table (die, reader->cu);
18021
18022 if (die->has_children)
18023 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18024 else
18025 {
18026 die->child = NULL;
18027 *new_info_ptr = cur_ptr;
18028 }
18029
18030 die->sibling = NULL;
18031 die->parent = parent;
18032 return die;
18033 }
18034
18035 /* Read a die, all of its descendents, and all of its siblings; set
18036 all of the fields of all of the dies correctly. Arguments are as
18037 in read_die_and_children. */
18038
18039 static struct die_info *
18040 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18041 const gdb_byte *info_ptr,
18042 const gdb_byte **new_info_ptr,
18043 struct die_info *parent)
18044 {
18045 struct die_info *first_die, *last_sibling;
18046 const gdb_byte *cur_ptr;
18047
18048 cur_ptr = info_ptr;
18049 first_die = last_sibling = NULL;
18050
18051 while (1)
18052 {
18053 struct die_info *die
18054 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18055
18056 if (die == NULL)
18057 {
18058 *new_info_ptr = cur_ptr;
18059 return first_die;
18060 }
18061
18062 if (!first_die)
18063 first_die = die;
18064 else
18065 last_sibling->sibling = die;
18066
18067 last_sibling = die;
18068 }
18069 }
18070
18071 /* Read a die, all of its descendents, and all of its siblings; set
18072 all of the fields of all of the dies correctly. Arguments are as
18073 in read_die_and_children.
18074 This the main entry point for reading a DIE and all its children. */
18075
18076 static struct die_info *
18077 read_die_and_siblings (const struct die_reader_specs *reader,
18078 const gdb_byte *info_ptr,
18079 const gdb_byte **new_info_ptr,
18080 struct die_info *parent)
18081 {
18082 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18083 new_info_ptr, parent);
18084
18085 if (dwarf_die_debug)
18086 {
18087 fprintf_unfiltered (gdb_stdlog,
18088 "Read die from %s@0x%x of %s:\n",
18089 reader->die_section->get_name (),
18090 (unsigned) (info_ptr - reader->die_section->buffer),
18091 bfd_get_filename (reader->abfd));
18092 dump_die (die, dwarf_die_debug);
18093 }
18094
18095 return die;
18096 }
18097
18098 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18099 attributes.
18100 The caller is responsible for filling in the extra attributes
18101 and updating (*DIEP)->num_attrs.
18102 Set DIEP to point to a newly allocated die with its information,
18103 except for its child, sibling, and parent fields. */
18104
18105 static const gdb_byte *
18106 read_full_die_1 (const struct die_reader_specs *reader,
18107 struct die_info **diep, const gdb_byte *info_ptr,
18108 int num_extra_attrs)
18109 {
18110 unsigned int abbrev_number, bytes_read, i;
18111 struct abbrev_info *abbrev;
18112 struct die_info *die;
18113 struct dwarf2_cu *cu = reader->cu;
18114 bfd *abfd = reader->abfd;
18115
18116 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18117 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18118 info_ptr += bytes_read;
18119 if (!abbrev_number)
18120 {
18121 *diep = NULL;
18122 return info_ptr;
18123 }
18124
18125 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18126 if (!abbrev)
18127 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18128 abbrev_number,
18129 bfd_get_filename (abfd));
18130
18131 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18132 die->sect_off = sect_off;
18133 die->tag = abbrev->tag;
18134 die->abbrev = abbrev_number;
18135 die->has_children = abbrev->has_children;
18136
18137 /* Make the result usable.
18138 The caller needs to update num_attrs after adding the extra
18139 attributes. */
18140 die->num_attrs = abbrev->num_attrs;
18141
18142 std::vector<int> indexes_that_need_reprocess;
18143 for (i = 0; i < abbrev->num_attrs; ++i)
18144 {
18145 bool need_reprocess;
18146 info_ptr =
18147 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18148 info_ptr, &need_reprocess);
18149 if (need_reprocess)
18150 indexes_that_need_reprocess.push_back (i);
18151 }
18152
18153 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18154 if (attr != nullptr)
18155 cu->str_offsets_base = DW_UNSND (attr);
18156
18157 attr = die->attr (DW_AT_loclists_base);
18158 if (attr != nullptr)
18159 cu->loclist_base = DW_UNSND (attr);
18160
18161 auto maybe_addr_base = die->addr_base ();
18162 if (maybe_addr_base.has_value ())
18163 cu->addr_base = *maybe_addr_base;
18164 for (int index : indexes_that_need_reprocess)
18165 read_attribute_reprocess (reader, &die->attrs[index]);
18166 *diep = die;
18167 return info_ptr;
18168 }
18169
18170 /* Read a die and all its attributes.
18171 Set DIEP to point to a newly allocated die with its information,
18172 except for its child, sibling, and parent fields. */
18173
18174 static const gdb_byte *
18175 read_full_die (const struct die_reader_specs *reader,
18176 struct die_info **diep, const gdb_byte *info_ptr)
18177 {
18178 const gdb_byte *result;
18179
18180 result = read_full_die_1 (reader, diep, info_ptr, 0);
18181
18182 if (dwarf_die_debug)
18183 {
18184 fprintf_unfiltered (gdb_stdlog,
18185 "Read die from %s@0x%x of %s:\n",
18186 reader->die_section->get_name (),
18187 (unsigned) (info_ptr - reader->die_section->buffer),
18188 bfd_get_filename (reader->abfd));
18189 dump_die (*diep, dwarf_die_debug);
18190 }
18191
18192 return result;
18193 }
18194 \f
18195
18196 /* Returns nonzero if TAG represents a type that we might generate a partial
18197 symbol for. */
18198
18199 static int
18200 is_type_tag_for_partial (int tag)
18201 {
18202 switch (tag)
18203 {
18204 #if 0
18205 /* Some types that would be reasonable to generate partial symbols for,
18206 that we don't at present. */
18207 case DW_TAG_array_type:
18208 case DW_TAG_file_type:
18209 case DW_TAG_ptr_to_member_type:
18210 case DW_TAG_set_type:
18211 case DW_TAG_string_type:
18212 case DW_TAG_subroutine_type:
18213 #endif
18214 case DW_TAG_base_type:
18215 case DW_TAG_class_type:
18216 case DW_TAG_interface_type:
18217 case DW_TAG_enumeration_type:
18218 case DW_TAG_structure_type:
18219 case DW_TAG_subrange_type:
18220 case DW_TAG_typedef:
18221 case DW_TAG_union_type:
18222 return 1;
18223 default:
18224 return 0;
18225 }
18226 }
18227
18228 /* Load all DIEs that are interesting for partial symbols into memory. */
18229
18230 static struct partial_die_info *
18231 load_partial_dies (const struct die_reader_specs *reader,
18232 const gdb_byte *info_ptr, int building_psymtab)
18233 {
18234 struct dwarf2_cu *cu = reader->cu;
18235 struct objfile *objfile = cu->per_objfile->objfile;
18236 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18237 unsigned int bytes_read;
18238 unsigned int load_all = 0;
18239 int nesting_level = 1;
18240
18241 parent_die = NULL;
18242 last_die = NULL;
18243
18244 gdb_assert (cu->per_cu != NULL);
18245 if (cu->per_cu->load_all_dies)
18246 load_all = 1;
18247
18248 cu->partial_dies
18249 = htab_create_alloc_ex (cu->header.length / 12,
18250 partial_die_hash,
18251 partial_die_eq,
18252 NULL,
18253 &cu->comp_unit_obstack,
18254 hashtab_obstack_allocate,
18255 dummy_obstack_deallocate);
18256
18257 while (1)
18258 {
18259 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18260
18261 /* A NULL abbrev means the end of a series of children. */
18262 if (abbrev == NULL)
18263 {
18264 if (--nesting_level == 0)
18265 return first_die;
18266
18267 info_ptr += bytes_read;
18268 last_die = parent_die;
18269 parent_die = parent_die->die_parent;
18270 continue;
18271 }
18272
18273 /* Check for template arguments. We never save these; if
18274 they're seen, we just mark the parent, and go on our way. */
18275 if (parent_die != NULL
18276 && cu->language == language_cplus
18277 && (abbrev->tag == DW_TAG_template_type_param
18278 || abbrev->tag == DW_TAG_template_value_param))
18279 {
18280 parent_die->has_template_arguments = 1;
18281
18282 if (!load_all)
18283 {
18284 /* We don't need a partial DIE for the template argument. */
18285 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18286 continue;
18287 }
18288 }
18289
18290 /* We only recurse into c++ subprograms looking for template arguments.
18291 Skip their other children. */
18292 if (!load_all
18293 && cu->language == language_cplus
18294 && parent_die != NULL
18295 && parent_die->tag == DW_TAG_subprogram)
18296 {
18297 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18298 continue;
18299 }
18300
18301 /* Check whether this DIE is interesting enough to save. Normally
18302 we would not be interested in members here, but there may be
18303 later variables referencing them via DW_AT_specification (for
18304 static members). */
18305 if (!load_all
18306 && !is_type_tag_for_partial (abbrev->tag)
18307 && abbrev->tag != DW_TAG_constant
18308 && abbrev->tag != DW_TAG_enumerator
18309 && abbrev->tag != DW_TAG_subprogram
18310 && abbrev->tag != DW_TAG_inlined_subroutine
18311 && abbrev->tag != DW_TAG_lexical_block
18312 && abbrev->tag != DW_TAG_variable
18313 && abbrev->tag != DW_TAG_namespace
18314 && abbrev->tag != DW_TAG_module
18315 && abbrev->tag != DW_TAG_member
18316 && abbrev->tag != DW_TAG_imported_unit
18317 && abbrev->tag != DW_TAG_imported_declaration)
18318 {
18319 /* Otherwise we skip to the next sibling, if any. */
18320 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18321 continue;
18322 }
18323
18324 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18325 abbrev);
18326
18327 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18328
18329 /* This two-pass algorithm for processing partial symbols has a
18330 high cost in cache pressure. Thus, handle some simple cases
18331 here which cover the majority of C partial symbols. DIEs
18332 which neither have specification tags in them, nor could have
18333 specification tags elsewhere pointing at them, can simply be
18334 processed and discarded.
18335
18336 This segment is also optional; scan_partial_symbols and
18337 add_partial_symbol will handle these DIEs if we chain
18338 them in normally. When compilers which do not emit large
18339 quantities of duplicate debug information are more common,
18340 this code can probably be removed. */
18341
18342 /* Any complete simple types at the top level (pretty much all
18343 of them, for a language without namespaces), can be processed
18344 directly. */
18345 if (parent_die == NULL
18346 && pdi.has_specification == 0
18347 && pdi.is_declaration == 0
18348 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18349 || pdi.tag == DW_TAG_base_type
18350 || pdi.tag == DW_TAG_subrange_type))
18351 {
18352 if (building_psymtab && pdi.name != NULL)
18353 add_psymbol_to_list (pdi.name, false,
18354 VAR_DOMAIN, LOC_TYPEDEF, -1,
18355 psymbol_placement::STATIC,
18356 0, cu->language, objfile);
18357 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18358 continue;
18359 }
18360
18361 /* The exception for DW_TAG_typedef with has_children above is
18362 a workaround of GCC PR debug/47510. In the case of this complaint
18363 type_name_or_error will error on such types later.
18364
18365 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18366 it could not find the child DIEs referenced later, this is checked
18367 above. In correct DWARF DW_TAG_typedef should have no children. */
18368
18369 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18370 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18371 "- DIE at %s [in module %s]"),
18372 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18373
18374 /* If we're at the second level, and we're an enumerator, and
18375 our parent has no specification (meaning possibly lives in a
18376 namespace elsewhere), then we can add the partial symbol now
18377 instead of queueing it. */
18378 if (pdi.tag == DW_TAG_enumerator
18379 && parent_die != NULL
18380 && parent_die->die_parent == NULL
18381 && parent_die->tag == DW_TAG_enumeration_type
18382 && parent_die->has_specification == 0)
18383 {
18384 if (pdi.name == NULL)
18385 complaint (_("malformed enumerator DIE ignored"));
18386 else if (building_psymtab)
18387 add_psymbol_to_list (pdi.name, false,
18388 VAR_DOMAIN, LOC_CONST, -1,
18389 cu->language == language_cplus
18390 ? psymbol_placement::GLOBAL
18391 : psymbol_placement::STATIC,
18392 0, cu->language, objfile);
18393
18394 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18395 continue;
18396 }
18397
18398 struct partial_die_info *part_die
18399 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18400
18401 /* We'll save this DIE so link it in. */
18402 part_die->die_parent = parent_die;
18403 part_die->die_sibling = NULL;
18404 part_die->die_child = NULL;
18405
18406 if (last_die && last_die == parent_die)
18407 last_die->die_child = part_die;
18408 else if (last_die)
18409 last_die->die_sibling = part_die;
18410
18411 last_die = part_die;
18412
18413 if (first_die == NULL)
18414 first_die = part_die;
18415
18416 /* Maybe add the DIE to the hash table. Not all DIEs that we
18417 find interesting need to be in the hash table, because we
18418 also have the parent/sibling/child chains; only those that we
18419 might refer to by offset later during partial symbol reading.
18420
18421 For now this means things that might have be the target of a
18422 DW_AT_specification, DW_AT_abstract_origin, or
18423 DW_AT_extension. DW_AT_extension will refer only to
18424 namespaces; DW_AT_abstract_origin refers to functions (and
18425 many things under the function DIE, but we do not recurse
18426 into function DIEs during partial symbol reading) and
18427 possibly variables as well; DW_AT_specification refers to
18428 declarations. Declarations ought to have the DW_AT_declaration
18429 flag. It happens that GCC forgets to put it in sometimes, but
18430 only for functions, not for types.
18431
18432 Adding more things than necessary to the hash table is harmless
18433 except for the performance cost. Adding too few will result in
18434 wasted time in find_partial_die, when we reread the compilation
18435 unit with load_all_dies set. */
18436
18437 if (load_all
18438 || abbrev->tag == DW_TAG_constant
18439 || abbrev->tag == DW_TAG_subprogram
18440 || abbrev->tag == DW_TAG_variable
18441 || abbrev->tag == DW_TAG_namespace
18442 || part_die->is_declaration)
18443 {
18444 void **slot;
18445
18446 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18447 to_underlying (part_die->sect_off),
18448 INSERT);
18449 *slot = part_die;
18450 }
18451
18452 /* For some DIEs we want to follow their children (if any). For C
18453 we have no reason to follow the children of structures; for other
18454 languages we have to, so that we can get at method physnames
18455 to infer fully qualified class names, for DW_AT_specification,
18456 and for C++ template arguments. For C++, we also look one level
18457 inside functions to find template arguments (if the name of the
18458 function does not already contain the template arguments).
18459
18460 For Ada and Fortran, we need to scan the children of subprograms
18461 and lexical blocks as well because these languages allow the
18462 definition of nested entities that could be interesting for the
18463 debugger, such as nested subprograms for instance. */
18464 if (last_die->has_children
18465 && (load_all
18466 || last_die->tag == DW_TAG_namespace
18467 || last_die->tag == DW_TAG_module
18468 || last_die->tag == DW_TAG_enumeration_type
18469 || (cu->language == language_cplus
18470 && last_die->tag == DW_TAG_subprogram
18471 && (last_die->name == NULL
18472 || strchr (last_die->name, '<') == NULL))
18473 || (cu->language != language_c
18474 && (last_die->tag == DW_TAG_class_type
18475 || last_die->tag == DW_TAG_interface_type
18476 || last_die->tag == DW_TAG_structure_type
18477 || last_die->tag == DW_TAG_union_type))
18478 || ((cu->language == language_ada
18479 || cu->language == language_fortran)
18480 && (last_die->tag == DW_TAG_subprogram
18481 || last_die->tag == DW_TAG_lexical_block))))
18482 {
18483 nesting_level++;
18484 parent_die = last_die;
18485 continue;
18486 }
18487
18488 /* Otherwise we skip to the next sibling, if any. */
18489 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18490
18491 /* Back to the top, do it again. */
18492 }
18493 }
18494
18495 partial_die_info::partial_die_info (sect_offset sect_off_,
18496 struct abbrev_info *abbrev)
18497 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18498 {
18499 }
18500
18501 /* Read a minimal amount of information into the minimal die structure.
18502 INFO_PTR should point just after the initial uleb128 of a DIE. */
18503
18504 const gdb_byte *
18505 partial_die_info::read (const struct die_reader_specs *reader,
18506 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18507 {
18508 struct dwarf2_cu *cu = reader->cu;
18509 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18510 unsigned int i;
18511 int has_low_pc_attr = 0;
18512 int has_high_pc_attr = 0;
18513 int high_pc_relative = 0;
18514
18515 for (i = 0; i < abbrev.num_attrs; ++i)
18516 {
18517 attribute attr;
18518 bool need_reprocess;
18519 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18520 info_ptr, &need_reprocess);
18521 /* String and address offsets that need to do the reprocessing have
18522 already been read at this point, so there is no need to wait until
18523 the loop terminates to do the reprocessing. */
18524 if (need_reprocess)
18525 read_attribute_reprocess (reader, &attr);
18526 /* Store the data if it is of an attribute we want to keep in a
18527 partial symbol table. */
18528 switch (attr.name)
18529 {
18530 case DW_AT_name:
18531 switch (tag)
18532 {
18533 case DW_TAG_compile_unit:
18534 case DW_TAG_partial_unit:
18535 case DW_TAG_type_unit:
18536 /* Compilation units have a DW_AT_name that is a filename, not
18537 a source language identifier. */
18538 case DW_TAG_enumeration_type:
18539 case DW_TAG_enumerator:
18540 /* These tags always have simple identifiers already; no need
18541 to canonicalize them. */
18542 name = DW_STRING (&attr);
18543 break;
18544 default:
18545 {
18546 struct objfile *objfile = dwarf2_per_objfile->objfile;
18547
18548 name
18549 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18550 }
18551 break;
18552 }
18553 break;
18554 case DW_AT_linkage_name:
18555 case DW_AT_MIPS_linkage_name:
18556 /* Note that both forms of linkage name might appear. We
18557 assume they will be the same, and we only store the last
18558 one we see. */
18559 linkage_name = attr.value_as_string ();
18560 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18561 See https://github.com/rust-lang/rust/issues/32925. */
18562 if (cu->language == language_rust && linkage_name != NULL
18563 && strchr (linkage_name, '{') != NULL)
18564 linkage_name = NULL;
18565 break;
18566 case DW_AT_low_pc:
18567 has_low_pc_attr = 1;
18568 lowpc = attr.value_as_address ();
18569 break;
18570 case DW_AT_high_pc:
18571 has_high_pc_attr = 1;
18572 highpc = attr.value_as_address ();
18573 if (cu->header.version >= 4 && attr.form_is_constant ())
18574 high_pc_relative = 1;
18575 break;
18576 case DW_AT_location:
18577 /* Support the .debug_loc offsets. */
18578 if (attr.form_is_block ())
18579 {
18580 d.locdesc = DW_BLOCK (&attr);
18581 }
18582 else if (attr.form_is_section_offset ())
18583 {
18584 dwarf2_complex_location_expr_complaint ();
18585 }
18586 else
18587 {
18588 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18589 "partial symbol information");
18590 }
18591 break;
18592 case DW_AT_external:
18593 is_external = DW_UNSND (&attr);
18594 break;
18595 case DW_AT_declaration:
18596 is_declaration = DW_UNSND (&attr);
18597 break;
18598 case DW_AT_type:
18599 has_type = 1;
18600 break;
18601 case DW_AT_abstract_origin:
18602 case DW_AT_specification:
18603 case DW_AT_extension:
18604 has_specification = 1;
18605 spec_offset = attr.get_ref_die_offset ();
18606 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18607 || cu->per_cu->is_dwz);
18608 break;
18609 case DW_AT_sibling:
18610 /* Ignore absolute siblings, they might point outside of
18611 the current compile unit. */
18612 if (attr.form == DW_FORM_ref_addr)
18613 complaint (_("ignoring absolute DW_AT_sibling"));
18614 else
18615 {
18616 const gdb_byte *buffer = reader->buffer;
18617 sect_offset off = attr.get_ref_die_offset ();
18618 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18619
18620 if (sibling_ptr < info_ptr)
18621 complaint (_("DW_AT_sibling points backwards"));
18622 else if (sibling_ptr > reader->buffer_end)
18623 reader->die_section->overflow_complaint ();
18624 else
18625 sibling = sibling_ptr;
18626 }
18627 break;
18628 case DW_AT_byte_size:
18629 has_byte_size = 1;
18630 break;
18631 case DW_AT_const_value:
18632 has_const_value = 1;
18633 break;
18634 case DW_AT_calling_convention:
18635 /* DWARF doesn't provide a way to identify a program's source-level
18636 entry point. DW_AT_calling_convention attributes are only meant
18637 to describe functions' calling conventions.
18638
18639 However, because it's a necessary piece of information in
18640 Fortran, and before DWARF 4 DW_CC_program was the only
18641 piece of debugging information whose definition refers to
18642 a 'main program' at all, several compilers marked Fortran
18643 main programs with DW_CC_program --- even when those
18644 functions use the standard calling conventions.
18645
18646 Although DWARF now specifies a way to provide this
18647 information, we support this practice for backward
18648 compatibility. */
18649 if (DW_UNSND (&attr) == DW_CC_program
18650 && cu->language == language_fortran)
18651 main_subprogram = 1;
18652 break;
18653 case DW_AT_inline:
18654 if (DW_UNSND (&attr) == DW_INL_inlined
18655 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18656 may_be_inlined = 1;
18657 break;
18658
18659 case DW_AT_import:
18660 if (tag == DW_TAG_imported_unit)
18661 {
18662 d.sect_off = attr.get_ref_die_offset ();
18663 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18664 || cu->per_cu->is_dwz);
18665 }
18666 break;
18667
18668 case DW_AT_main_subprogram:
18669 main_subprogram = DW_UNSND (&attr);
18670 break;
18671
18672 case DW_AT_ranges:
18673 {
18674 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18675 but that requires a full DIE, so instead we just
18676 reimplement it. */
18677 int need_ranges_base = tag != DW_TAG_compile_unit;
18678 unsigned int ranges_offset = (DW_UNSND (&attr)
18679 + (need_ranges_base
18680 ? cu->ranges_base
18681 : 0));
18682
18683 /* Value of the DW_AT_ranges attribute is the offset in the
18684 .debug_ranges section. */
18685 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18686 nullptr))
18687 has_pc_info = 1;
18688 }
18689 break;
18690
18691 default:
18692 break;
18693 }
18694 }
18695
18696 /* For Ada, if both the name and the linkage name appear, we prefer
18697 the latter. This lets "catch exception" work better, regardless
18698 of the order in which the name and linkage name were emitted.
18699 Really, though, this is just a workaround for the fact that gdb
18700 doesn't store both the name and the linkage name. */
18701 if (cu->language == language_ada && linkage_name != nullptr)
18702 name = linkage_name;
18703
18704 if (high_pc_relative)
18705 highpc += lowpc;
18706
18707 if (has_low_pc_attr && has_high_pc_attr)
18708 {
18709 /* When using the GNU linker, .gnu.linkonce. sections are used to
18710 eliminate duplicate copies of functions and vtables and such.
18711 The linker will arbitrarily choose one and discard the others.
18712 The AT_*_pc values for such functions refer to local labels in
18713 these sections. If the section from that file was discarded, the
18714 labels are not in the output, so the relocs get a value of 0.
18715 If this is a discarded function, mark the pc bounds as invalid,
18716 so that GDB will ignore it. */
18717 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18718 {
18719 struct objfile *objfile = dwarf2_per_objfile->objfile;
18720 struct gdbarch *gdbarch = objfile->arch ();
18721
18722 complaint (_("DW_AT_low_pc %s is zero "
18723 "for DIE at %s [in module %s]"),
18724 paddress (gdbarch, lowpc),
18725 sect_offset_str (sect_off),
18726 objfile_name (objfile));
18727 }
18728 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18729 else if (lowpc >= highpc)
18730 {
18731 struct objfile *objfile = dwarf2_per_objfile->objfile;
18732 struct gdbarch *gdbarch = objfile->arch ();
18733
18734 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18735 "for DIE at %s [in module %s]"),
18736 paddress (gdbarch, lowpc),
18737 paddress (gdbarch, highpc),
18738 sect_offset_str (sect_off),
18739 objfile_name (objfile));
18740 }
18741 else
18742 has_pc_info = 1;
18743 }
18744
18745 return info_ptr;
18746 }
18747
18748 /* Find a cached partial DIE at OFFSET in CU. */
18749
18750 struct partial_die_info *
18751 dwarf2_cu::find_partial_die (sect_offset sect_off)
18752 {
18753 struct partial_die_info *lookup_die = NULL;
18754 struct partial_die_info part_die (sect_off);
18755
18756 lookup_die = ((struct partial_die_info *)
18757 htab_find_with_hash (partial_dies, &part_die,
18758 to_underlying (sect_off)));
18759
18760 return lookup_die;
18761 }
18762
18763 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18764 except in the case of .debug_types DIEs which do not reference
18765 outside their CU (they do however referencing other types via
18766 DW_FORM_ref_sig8). */
18767
18768 static const struct cu_partial_die_info
18769 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18770 {
18771 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18772 struct objfile *objfile = dwarf2_per_objfile->objfile;
18773 struct partial_die_info *pd = NULL;
18774
18775 if (offset_in_dwz == cu->per_cu->is_dwz
18776 && cu->header.offset_in_cu_p (sect_off))
18777 {
18778 pd = cu->find_partial_die (sect_off);
18779 if (pd != NULL)
18780 return { cu, pd };
18781 /* We missed recording what we needed.
18782 Load all dies and try again. */
18783 }
18784 else
18785 {
18786 /* TUs don't reference other CUs/TUs (except via type signatures). */
18787 if (cu->per_cu->is_debug_types)
18788 {
18789 error (_("Dwarf Error: Type Unit at offset %s contains"
18790 " external reference to offset %s [in module %s].\n"),
18791 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18792 bfd_get_filename (objfile->obfd));
18793 }
18794 dwarf2_per_cu_data *per_cu
18795 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18796 dwarf2_per_objfile);
18797
18798 cu = dwarf2_per_objfile->get_cu (per_cu);
18799 if (cu == NULL || cu->partial_dies == NULL)
18800 load_partial_comp_unit (per_cu, dwarf2_per_objfile, nullptr);
18801
18802 cu = dwarf2_per_objfile->get_cu (per_cu);
18803
18804 cu->last_used = 0;
18805 pd = cu->find_partial_die (sect_off);
18806 }
18807
18808 /* If we didn't find it, and not all dies have been loaded,
18809 load them all and try again. */
18810
18811 if (pd == NULL && cu->per_cu->load_all_dies == 0)
18812 {
18813 cu->per_cu->load_all_dies = 1;
18814
18815 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18816 THIS_CU->cu may already be in use. So we can't just free it and
18817 replace its DIEs with the ones we read in. Instead, we leave those
18818 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18819 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18820 set. */
18821 load_partial_comp_unit (cu->per_cu, dwarf2_per_objfile, cu);
18822
18823 pd = cu->find_partial_die (sect_off);
18824 }
18825
18826 if (pd == NULL)
18827 internal_error (__FILE__, __LINE__,
18828 _("could not find partial DIE %s "
18829 "in cache [from module %s]\n"),
18830 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18831 return { cu, pd };
18832 }
18833
18834 /* See if we can figure out if the class lives in a namespace. We do
18835 this by looking for a member function; its demangled name will
18836 contain namespace info, if there is any. */
18837
18838 static void
18839 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18840 struct dwarf2_cu *cu)
18841 {
18842 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18843 what template types look like, because the demangler
18844 frequently doesn't give the same name as the debug info. We
18845 could fix this by only using the demangled name to get the
18846 prefix (but see comment in read_structure_type). */
18847
18848 struct partial_die_info *real_pdi;
18849 struct partial_die_info *child_pdi;
18850
18851 /* If this DIE (this DIE's specification, if any) has a parent, then
18852 we should not do this. We'll prepend the parent's fully qualified
18853 name when we create the partial symbol. */
18854
18855 real_pdi = struct_pdi;
18856 while (real_pdi->has_specification)
18857 {
18858 auto res = find_partial_die (real_pdi->spec_offset,
18859 real_pdi->spec_is_dwz, cu);
18860 real_pdi = res.pdi;
18861 cu = res.cu;
18862 }
18863
18864 if (real_pdi->die_parent != NULL)
18865 return;
18866
18867 for (child_pdi = struct_pdi->die_child;
18868 child_pdi != NULL;
18869 child_pdi = child_pdi->die_sibling)
18870 {
18871 if (child_pdi->tag == DW_TAG_subprogram
18872 && child_pdi->linkage_name != NULL)
18873 {
18874 gdb::unique_xmalloc_ptr<char> actual_class_name
18875 (language_class_name_from_physname (cu->language_defn,
18876 child_pdi->linkage_name));
18877 if (actual_class_name != NULL)
18878 {
18879 struct objfile *objfile = cu->per_objfile->objfile;
18880 struct_pdi->name = objfile->intern (actual_class_name.get ());
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 (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->name)
18929 {
18930 name = spec_die->name;
18931
18932 /* Copy DW_AT_external attribute if it is set. */
18933 if (spec_die->is_external)
18934 is_external = spec_die->is_external;
18935 }
18936 }
18937
18938 if (!has_const_value && has_specification
18939 && can_have_DW_AT_const_value_p (tag))
18940 {
18941 struct partial_die_info *spec_die;
18942
18943 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18944 spec_die = res.pdi;
18945 cu = res.cu;
18946
18947 spec_die->fixup (cu);
18948
18949 if (spec_die->has_const_value)
18950 {
18951 /* Copy DW_AT_const_value attribute if it is set. */
18952 has_const_value = spec_die->has_const_value;
18953 }
18954 }
18955
18956 /* Set default names for some unnamed DIEs. */
18957
18958 if (name == NULL && tag == DW_TAG_namespace)
18959 name = CP_ANONYMOUS_NAMESPACE_STR;
18960
18961 /* If there is no parent die to provide a namespace, and there are
18962 children, see if we can determine the namespace from their linkage
18963 name. */
18964 if (cu->language == language_cplus
18965 && !cu->per_objfile->per_bfd->types.empty ()
18966 && die_parent == NULL
18967 && has_children
18968 && (tag == DW_TAG_class_type
18969 || tag == DW_TAG_structure_type
18970 || tag == DW_TAG_union_type))
18971 guess_partial_die_structure_name (this, cu);
18972
18973 /* GCC might emit a nameless struct or union that has a linkage
18974 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18975 if (name == NULL
18976 && (tag == DW_TAG_class_type
18977 || tag == DW_TAG_interface_type
18978 || tag == DW_TAG_structure_type
18979 || tag == DW_TAG_union_type)
18980 && linkage_name != NULL)
18981 {
18982 gdb::unique_xmalloc_ptr<char> demangled
18983 (gdb_demangle (linkage_name, DMGL_TYPES));
18984 if (demangled != nullptr)
18985 {
18986 const char *base;
18987
18988 /* Strip any leading namespaces/classes, keep only the base name.
18989 DW_AT_name for named DIEs does not contain the prefixes. */
18990 base = strrchr (demangled.get (), ':');
18991 if (base && base > demangled.get () && base[-1] == ':')
18992 base++;
18993 else
18994 base = demangled.get ();
18995
18996 struct objfile *objfile = cu->per_objfile->objfile;
18997 name = objfile->intern (base);
18998 }
18999 }
19000
19001 fixup_called = 1;
19002 }
19003
19004 /* Read the .debug_loclists header contents from the given SECTION in the
19005 HEADER. */
19006 static void
19007 read_loclist_header (struct loclist_header *header,
19008 struct dwarf2_section_info *section)
19009 {
19010 unsigned int bytes_read;
19011 bfd *abfd = section->get_bfd_owner ();
19012 const gdb_byte *info_ptr = section->buffer;
19013 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19014 info_ptr += bytes_read;
19015 header->version = read_2_bytes (abfd, info_ptr);
19016 info_ptr += 2;
19017 header->addr_size = read_1_byte (abfd, info_ptr);
19018 info_ptr += 1;
19019 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19020 info_ptr += 1;
19021 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19022 }
19023
19024 /* Return the DW_AT_loclists_base value for the CU. */
19025 static ULONGEST
19026 lookup_loclist_base (struct dwarf2_cu *cu)
19027 {
19028 /* For the .dwo unit, the loclist_base points to the first offset following
19029 the header. The header consists of the following entities-
19030 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19031 bit format)
19032 2. version (2 bytes)
19033 3. address size (1 byte)
19034 4. segment selector size (1 byte)
19035 5. offset entry count (4 bytes)
19036 These sizes are derived as per the DWARFv5 standard. */
19037 if (cu->dwo_unit != nullptr)
19038 {
19039 if (cu->header.initial_length_size == 4)
19040 return LOCLIST_HEADER_SIZE32;
19041 return LOCLIST_HEADER_SIZE64;
19042 }
19043 return cu->loclist_base;
19044 }
19045
19046 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19047 array of offsets in the .debug_loclists section. */
19048 static CORE_ADDR
19049 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19050 {
19051 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19052 struct objfile *objfile = dwarf2_per_objfile->objfile;
19053 bfd *abfd = objfile->obfd;
19054 ULONGEST loclist_base = lookup_loclist_base (cu);
19055 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19056
19057 section->read (objfile);
19058 if (section->buffer == NULL)
19059 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19060 "section [in module %s]"), objfile_name (objfile));
19061 struct loclist_header header;
19062 read_loclist_header (&header, section);
19063 if (loclist_index >= header.offset_entry_count)
19064 complaint (_("DW_FORM_loclistx pointing outside of "
19065 ".debug_loclists offset array [in module %s]"),
19066 objfile_name (objfile));
19067 if (loclist_base + loclist_index * cu->header.offset_size
19068 >= section->size)
19069 complaint (_("DW_FORM_loclistx pointing outside of "
19070 ".debug_loclists section [in module %s]"),
19071 objfile_name (objfile));
19072 const gdb_byte *info_ptr
19073 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19074
19075 if (cu->header.offset_size == 4)
19076 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19077 else
19078 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19079 }
19080
19081 /* Process the attributes that had to be skipped in the first round. These
19082 attributes are the ones that need str_offsets_base or addr_base attributes.
19083 They could not have been processed in the first round, because at the time
19084 the values of str_offsets_base or addr_base may not have been known. */
19085 static void
19086 read_attribute_reprocess (const struct die_reader_specs *reader,
19087 struct attribute *attr)
19088 {
19089 struct dwarf2_cu *cu = reader->cu;
19090 switch (attr->form)
19091 {
19092 case DW_FORM_addrx:
19093 case DW_FORM_GNU_addr_index:
19094 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19095 break;
19096 case DW_FORM_loclistx:
19097 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19098 break;
19099 case DW_FORM_strx:
19100 case DW_FORM_strx1:
19101 case DW_FORM_strx2:
19102 case DW_FORM_strx3:
19103 case DW_FORM_strx4:
19104 case DW_FORM_GNU_str_index:
19105 {
19106 unsigned int str_index = DW_UNSND (attr);
19107 if (reader->dwo_file != NULL)
19108 {
19109 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19110 DW_STRING_IS_CANONICAL (attr) = 0;
19111 }
19112 else
19113 {
19114 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19115 DW_STRING_IS_CANONICAL (attr) = 0;
19116 }
19117 break;
19118 }
19119 default:
19120 gdb_assert_not_reached (_("Unexpected DWARF form."));
19121 }
19122 }
19123
19124 /* Read an attribute value described by an attribute form. */
19125
19126 static const gdb_byte *
19127 read_attribute_value (const struct die_reader_specs *reader,
19128 struct attribute *attr, unsigned form,
19129 LONGEST implicit_const, const gdb_byte *info_ptr,
19130 bool *need_reprocess)
19131 {
19132 struct dwarf2_cu *cu = reader->cu;
19133 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19134 struct objfile *objfile = dwarf2_per_objfile->objfile;
19135 bfd *abfd = reader->abfd;
19136 struct comp_unit_head *cu_header = &cu->header;
19137 unsigned int bytes_read;
19138 struct dwarf_block *blk;
19139 *need_reprocess = false;
19140
19141 attr->form = (enum dwarf_form) form;
19142 switch (form)
19143 {
19144 case DW_FORM_ref_addr:
19145 if (cu->header.version == 2)
19146 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19147 &bytes_read);
19148 else
19149 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19150 &bytes_read);
19151 info_ptr += bytes_read;
19152 break;
19153 case DW_FORM_GNU_ref_alt:
19154 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19155 info_ptr += bytes_read;
19156 break;
19157 case DW_FORM_addr:
19158 {
19159 struct gdbarch *gdbarch = objfile->arch ();
19160 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19161 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19162 info_ptr += bytes_read;
19163 }
19164 break;
19165 case DW_FORM_block2:
19166 blk = dwarf_alloc_block (cu);
19167 blk->size = read_2_bytes (abfd, info_ptr);
19168 info_ptr += 2;
19169 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19170 info_ptr += blk->size;
19171 DW_BLOCK (attr) = blk;
19172 break;
19173 case DW_FORM_block4:
19174 blk = dwarf_alloc_block (cu);
19175 blk->size = read_4_bytes (abfd, info_ptr);
19176 info_ptr += 4;
19177 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19178 info_ptr += blk->size;
19179 DW_BLOCK (attr) = blk;
19180 break;
19181 case DW_FORM_data2:
19182 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19183 info_ptr += 2;
19184 break;
19185 case DW_FORM_data4:
19186 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19187 info_ptr += 4;
19188 break;
19189 case DW_FORM_data8:
19190 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19191 info_ptr += 8;
19192 break;
19193 case DW_FORM_data16:
19194 blk = dwarf_alloc_block (cu);
19195 blk->size = 16;
19196 blk->data = read_n_bytes (abfd, info_ptr, 16);
19197 info_ptr += 16;
19198 DW_BLOCK (attr) = blk;
19199 break;
19200 case DW_FORM_sec_offset:
19201 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19202 info_ptr += bytes_read;
19203 break;
19204 case DW_FORM_loclistx:
19205 {
19206 *need_reprocess = true;
19207 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19208 info_ptr += bytes_read;
19209 }
19210 break;
19211 case DW_FORM_string:
19212 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19213 DW_STRING_IS_CANONICAL (attr) = 0;
19214 info_ptr += bytes_read;
19215 break;
19216 case DW_FORM_strp:
19217 if (!cu->per_cu->is_dwz)
19218 {
19219 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19220 abfd, info_ptr, cu_header,
19221 &bytes_read);
19222 DW_STRING_IS_CANONICAL (attr) = 0;
19223 info_ptr += bytes_read;
19224 break;
19225 }
19226 /* FALLTHROUGH */
19227 case DW_FORM_line_strp:
19228 if (!cu->per_cu->is_dwz)
19229 {
19230 DW_STRING (attr)
19231 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19232 &bytes_read);
19233 DW_STRING_IS_CANONICAL (attr) = 0;
19234 info_ptr += bytes_read;
19235 break;
19236 }
19237 /* FALLTHROUGH */
19238 case DW_FORM_GNU_strp_alt:
19239 {
19240 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19241 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19242 &bytes_read);
19243
19244 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19245 DW_STRING_IS_CANONICAL (attr) = 0;
19246 info_ptr += bytes_read;
19247 }
19248 break;
19249 case DW_FORM_exprloc:
19250 case DW_FORM_block:
19251 blk = dwarf_alloc_block (cu);
19252 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19253 info_ptr += bytes_read;
19254 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19255 info_ptr += blk->size;
19256 DW_BLOCK (attr) = blk;
19257 break;
19258 case DW_FORM_block1:
19259 blk = dwarf_alloc_block (cu);
19260 blk->size = read_1_byte (abfd, info_ptr);
19261 info_ptr += 1;
19262 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19263 info_ptr += blk->size;
19264 DW_BLOCK (attr) = blk;
19265 break;
19266 case DW_FORM_data1:
19267 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19268 info_ptr += 1;
19269 break;
19270 case DW_FORM_flag:
19271 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19272 info_ptr += 1;
19273 break;
19274 case DW_FORM_flag_present:
19275 DW_UNSND (attr) = 1;
19276 break;
19277 case DW_FORM_sdata:
19278 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19279 info_ptr += bytes_read;
19280 break;
19281 case DW_FORM_udata:
19282 case DW_FORM_rnglistx:
19283 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19284 info_ptr += bytes_read;
19285 break;
19286 case DW_FORM_ref1:
19287 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19288 + read_1_byte (abfd, info_ptr));
19289 info_ptr += 1;
19290 break;
19291 case DW_FORM_ref2:
19292 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19293 + read_2_bytes (abfd, info_ptr));
19294 info_ptr += 2;
19295 break;
19296 case DW_FORM_ref4:
19297 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19298 + read_4_bytes (abfd, info_ptr));
19299 info_ptr += 4;
19300 break;
19301 case DW_FORM_ref8:
19302 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19303 + read_8_bytes (abfd, info_ptr));
19304 info_ptr += 8;
19305 break;
19306 case DW_FORM_ref_sig8:
19307 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19308 info_ptr += 8;
19309 break;
19310 case DW_FORM_ref_udata:
19311 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19312 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19313 info_ptr += bytes_read;
19314 break;
19315 case DW_FORM_indirect:
19316 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19317 info_ptr += bytes_read;
19318 if (form == DW_FORM_implicit_const)
19319 {
19320 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19321 info_ptr += bytes_read;
19322 }
19323 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19324 info_ptr, need_reprocess);
19325 break;
19326 case DW_FORM_implicit_const:
19327 DW_SND (attr) = implicit_const;
19328 break;
19329 case DW_FORM_addrx:
19330 case DW_FORM_GNU_addr_index:
19331 *need_reprocess = true;
19332 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19333 info_ptr += bytes_read;
19334 break;
19335 case DW_FORM_strx:
19336 case DW_FORM_strx1:
19337 case DW_FORM_strx2:
19338 case DW_FORM_strx3:
19339 case DW_FORM_strx4:
19340 case DW_FORM_GNU_str_index:
19341 {
19342 ULONGEST str_index;
19343 if (form == DW_FORM_strx1)
19344 {
19345 str_index = read_1_byte (abfd, info_ptr);
19346 info_ptr += 1;
19347 }
19348 else if (form == DW_FORM_strx2)
19349 {
19350 str_index = read_2_bytes (abfd, info_ptr);
19351 info_ptr += 2;
19352 }
19353 else if (form == DW_FORM_strx3)
19354 {
19355 str_index = read_3_bytes (abfd, info_ptr);
19356 info_ptr += 3;
19357 }
19358 else if (form == DW_FORM_strx4)
19359 {
19360 str_index = read_4_bytes (abfd, info_ptr);
19361 info_ptr += 4;
19362 }
19363 else
19364 {
19365 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19366 info_ptr += bytes_read;
19367 }
19368 *need_reprocess = true;
19369 DW_UNSND (attr) = str_index;
19370 }
19371 break;
19372 default:
19373 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19374 dwarf_form_name (form),
19375 bfd_get_filename (abfd));
19376 }
19377
19378 /* Super hack. */
19379 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19380 attr->form = DW_FORM_GNU_ref_alt;
19381
19382 /* We have seen instances where the compiler tried to emit a byte
19383 size attribute of -1 which ended up being encoded as an unsigned
19384 0xffffffff. Although 0xffffffff is technically a valid size value,
19385 an object of this size seems pretty unlikely so we can relatively
19386 safely treat these cases as if the size attribute was invalid and
19387 treat them as zero by default. */
19388 if (attr->name == DW_AT_byte_size
19389 && form == DW_FORM_data4
19390 && DW_UNSND (attr) >= 0xffffffff)
19391 {
19392 complaint
19393 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19394 hex_string (DW_UNSND (attr)));
19395 DW_UNSND (attr) = 0;
19396 }
19397
19398 return info_ptr;
19399 }
19400
19401 /* Read an attribute described by an abbreviated attribute. */
19402
19403 static const gdb_byte *
19404 read_attribute (const struct die_reader_specs *reader,
19405 struct attribute *attr, struct attr_abbrev *abbrev,
19406 const gdb_byte *info_ptr, bool *need_reprocess)
19407 {
19408 attr->name = abbrev->name;
19409 return read_attribute_value (reader, attr, abbrev->form,
19410 abbrev->implicit_const, info_ptr,
19411 need_reprocess);
19412 }
19413
19414 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19415
19416 static const char *
19417 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19418 LONGEST str_offset)
19419 {
19420 return dwarf2_per_objfile->per_bfd->str.read_string
19421 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19422 }
19423
19424 /* Return pointer to string at .debug_str offset as read from BUF.
19425 BUF is assumed to be in a compilation unit described by CU_HEADER.
19426 Return *BYTES_READ_PTR count of bytes read from BUF. */
19427
19428 static const char *
19429 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19430 const gdb_byte *buf,
19431 const struct comp_unit_head *cu_header,
19432 unsigned int *bytes_read_ptr)
19433 {
19434 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19435
19436 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19437 }
19438
19439 /* See read.h. */
19440
19441 const char *
19442 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19443 const struct comp_unit_head *cu_header,
19444 unsigned int *bytes_read_ptr)
19445 {
19446 bfd *abfd = objfile->obfd;
19447 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19448
19449 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19450 }
19451
19452 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19453 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19454 ADDR_SIZE is the size of addresses from the CU header. */
19455
19456 static CORE_ADDR
19457 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19458 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19459 int addr_size)
19460 {
19461 struct objfile *objfile = dwarf2_per_objfile->objfile;
19462 bfd *abfd = objfile->obfd;
19463 const gdb_byte *info_ptr;
19464 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19465
19466 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19467 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19468 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19469 objfile_name (objfile));
19470 if (addr_base_or_zero + addr_index * addr_size
19471 >= dwarf2_per_objfile->per_bfd->addr.size)
19472 error (_("DW_FORM_addr_index pointing outside of "
19473 ".debug_addr section [in module %s]"),
19474 objfile_name (objfile));
19475 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19476 + addr_base_or_zero + addr_index * addr_size);
19477 if (addr_size == 4)
19478 return bfd_get_32 (abfd, info_ptr);
19479 else
19480 return bfd_get_64 (abfd, info_ptr);
19481 }
19482
19483 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19484
19485 static CORE_ADDR
19486 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19487 {
19488 return read_addr_index_1 (cu->per_objfile, addr_index,
19489 cu->addr_base, cu->header.addr_size);
19490 }
19491
19492 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19493
19494 static CORE_ADDR
19495 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19496 unsigned int *bytes_read)
19497 {
19498 bfd *abfd = cu->per_objfile->objfile->obfd;
19499 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19500
19501 return read_addr_index (cu, addr_index);
19502 }
19503
19504 /* See read.h. */
19505
19506 CORE_ADDR
19507 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19508 dwarf2_per_objfile *dwarf2_per_objfile,
19509 unsigned int addr_index)
19510 {
19511 struct dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
19512 gdb::optional<ULONGEST> addr_base;
19513 int addr_size;
19514
19515 /* We need addr_base and addr_size.
19516 If we don't have PER_CU->cu, we have to get it.
19517 Nasty, but the alternative is storing the needed info in PER_CU,
19518 which at this point doesn't seem justified: it's not clear how frequently
19519 it would get used and it would increase the size of every PER_CU.
19520 Entry points like dwarf2_per_cu_addr_size do a similar thing
19521 so we're not in uncharted territory here.
19522 Alas we need to be a bit more complicated as addr_base is contained
19523 in the DIE.
19524
19525 We don't need to read the entire CU(/TU).
19526 We just need the header and top level die.
19527
19528 IWBN to use the aging mechanism to let us lazily later discard the CU.
19529 For now we skip this optimization. */
19530
19531 if (cu != NULL)
19532 {
19533 addr_base = cu->addr_base;
19534 addr_size = cu->header.addr_size;
19535 }
19536 else
19537 {
19538 cutu_reader reader (per_cu, dwarf2_per_objfile, nullptr, nullptr, false);
19539 addr_base = reader.cu->addr_base;
19540 addr_size = reader.cu->header.addr_size;
19541 }
19542
19543 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19544 addr_size);
19545 }
19546
19547 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19548 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19549 DWO file. */
19550
19551 static const char *
19552 read_str_index (struct dwarf2_cu *cu,
19553 struct dwarf2_section_info *str_section,
19554 struct dwarf2_section_info *str_offsets_section,
19555 ULONGEST str_offsets_base, ULONGEST str_index)
19556 {
19557 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19558 struct objfile *objfile = dwarf2_per_objfile->objfile;
19559 const char *objf_name = objfile_name (objfile);
19560 bfd *abfd = objfile->obfd;
19561 const gdb_byte *info_ptr;
19562 ULONGEST str_offset;
19563 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19564
19565 str_section->read (objfile);
19566 str_offsets_section->read (objfile);
19567 if (str_section->buffer == NULL)
19568 error (_("%s used without %s section"
19569 " in CU at offset %s [in module %s]"),
19570 form_name, str_section->get_name (),
19571 sect_offset_str (cu->header.sect_off), objf_name);
19572 if (str_offsets_section->buffer == NULL)
19573 error (_("%s used without %s section"
19574 " in CU at offset %s [in module %s]"),
19575 form_name, str_section->get_name (),
19576 sect_offset_str (cu->header.sect_off), objf_name);
19577 info_ptr = (str_offsets_section->buffer
19578 + str_offsets_base
19579 + str_index * cu->header.offset_size);
19580 if (cu->header.offset_size == 4)
19581 str_offset = bfd_get_32 (abfd, info_ptr);
19582 else
19583 str_offset = bfd_get_64 (abfd, info_ptr);
19584 if (str_offset >= str_section->size)
19585 error (_("Offset from %s pointing outside of"
19586 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19587 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19588 return (const char *) (str_section->buffer + str_offset);
19589 }
19590
19591 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19592
19593 static const char *
19594 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19595 {
19596 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19597 ? reader->cu->header.addr_size : 0;
19598 return read_str_index (reader->cu,
19599 &reader->dwo_file->sections.str,
19600 &reader->dwo_file->sections.str_offsets,
19601 str_offsets_base, str_index);
19602 }
19603
19604 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19605
19606 static const char *
19607 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19608 {
19609 struct objfile *objfile = cu->per_objfile->objfile;
19610 const char *objf_name = objfile_name (objfile);
19611 static const char form_name[] = "DW_FORM_GNU_str_index";
19612 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19613
19614 if (!cu->str_offsets_base.has_value ())
19615 error (_("%s used in Fission stub without %s"
19616 " in CU at offset 0x%lx [in module %s]"),
19617 form_name, str_offsets_attr_name,
19618 (long) cu->header.offset_size, objf_name);
19619
19620 return read_str_index (cu,
19621 &cu->per_objfile->per_bfd->str,
19622 &cu->per_objfile->per_bfd->str_offsets,
19623 *cu->str_offsets_base, str_index);
19624 }
19625
19626 /* Return the length of an LEB128 number in BUF. */
19627
19628 static int
19629 leb128_size (const gdb_byte *buf)
19630 {
19631 const gdb_byte *begin = buf;
19632 gdb_byte byte;
19633
19634 while (1)
19635 {
19636 byte = *buf++;
19637 if ((byte & 128) == 0)
19638 return buf - begin;
19639 }
19640 }
19641
19642 static void
19643 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19644 {
19645 switch (lang)
19646 {
19647 case DW_LANG_C89:
19648 case DW_LANG_C99:
19649 case DW_LANG_C11:
19650 case DW_LANG_C:
19651 case DW_LANG_UPC:
19652 cu->language = language_c;
19653 break;
19654 case DW_LANG_Java:
19655 case DW_LANG_C_plus_plus:
19656 case DW_LANG_C_plus_plus_11:
19657 case DW_LANG_C_plus_plus_14:
19658 cu->language = language_cplus;
19659 break;
19660 case DW_LANG_D:
19661 cu->language = language_d;
19662 break;
19663 case DW_LANG_Fortran77:
19664 case DW_LANG_Fortran90:
19665 case DW_LANG_Fortran95:
19666 case DW_LANG_Fortran03:
19667 case DW_LANG_Fortran08:
19668 cu->language = language_fortran;
19669 break;
19670 case DW_LANG_Go:
19671 cu->language = language_go;
19672 break;
19673 case DW_LANG_Mips_Assembler:
19674 cu->language = language_asm;
19675 break;
19676 case DW_LANG_Ada83:
19677 case DW_LANG_Ada95:
19678 cu->language = language_ada;
19679 break;
19680 case DW_LANG_Modula2:
19681 cu->language = language_m2;
19682 break;
19683 case DW_LANG_Pascal83:
19684 cu->language = language_pascal;
19685 break;
19686 case DW_LANG_ObjC:
19687 cu->language = language_objc;
19688 break;
19689 case DW_LANG_Rust:
19690 case DW_LANG_Rust_old:
19691 cu->language = language_rust;
19692 break;
19693 case DW_LANG_Cobol74:
19694 case DW_LANG_Cobol85:
19695 default:
19696 cu->language = language_minimal;
19697 break;
19698 }
19699 cu->language_defn = language_def (cu->language);
19700 }
19701
19702 /* Return the named attribute or NULL if not there. */
19703
19704 static struct attribute *
19705 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19706 {
19707 for (;;)
19708 {
19709 unsigned int i;
19710 struct attribute *spec = NULL;
19711
19712 for (i = 0; i < die->num_attrs; ++i)
19713 {
19714 if (die->attrs[i].name == name)
19715 return &die->attrs[i];
19716 if (die->attrs[i].name == DW_AT_specification
19717 || die->attrs[i].name == DW_AT_abstract_origin)
19718 spec = &die->attrs[i];
19719 }
19720
19721 if (!spec)
19722 break;
19723
19724 die = follow_die_ref (die, spec, &cu);
19725 }
19726
19727 return NULL;
19728 }
19729
19730 /* Return the string associated with a string-typed attribute, or NULL if it
19731 is either not found or is of an incorrect type. */
19732
19733 static const char *
19734 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19735 {
19736 struct attribute *attr;
19737 const char *str = NULL;
19738
19739 attr = dwarf2_attr (die, name, cu);
19740
19741 if (attr != NULL)
19742 {
19743 str = attr->value_as_string ();
19744 if (str == nullptr)
19745 complaint (_("string type expected for attribute %s for "
19746 "DIE at %s in module %s"),
19747 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19748 objfile_name (cu->per_objfile->objfile));
19749 }
19750
19751 return str;
19752 }
19753
19754 /* Return the dwo name or NULL if not present. If present, it is in either
19755 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19756 static const char *
19757 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19758 {
19759 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19760 if (dwo_name == nullptr)
19761 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19762 return dwo_name;
19763 }
19764
19765 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19766 and holds a non-zero value. This function should only be used for
19767 DW_FORM_flag or DW_FORM_flag_present attributes. */
19768
19769 static int
19770 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19771 {
19772 struct attribute *attr = dwarf2_attr (die, name, cu);
19773
19774 return (attr && DW_UNSND (attr));
19775 }
19776
19777 static int
19778 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19779 {
19780 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19781 which value is non-zero. However, we have to be careful with
19782 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19783 (via dwarf2_flag_true_p) follows this attribute. So we may
19784 end up accidently finding a declaration attribute that belongs
19785 to a different DIE referenced by the specification attribute,
19786 even though the given DIE does not have a declaration attribute. */
19787 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19788 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19789 }
19790
19791 /* Return the die giving the specification for DIE, if there is
19792 one. *SPEC_CU is the CU containing DIE on input, and the CU
19793 containing the return value on output. If there is no
19794 specification, but there is an abstract origin, that is
19795 returned. */
19796
19797 static struct die_info *
19798 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19799 {
19800 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19801 *spec_cu);
19802
19803 if (spec_attr == NULL)
19804 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19805
19806 if (spec_attr == NULL)
19807 return NULL;
19808 else
19809 return follow_die_ref (die, spec_attr, spec_cu);
19810 }
19811
19812 /* Stub for free_line_header to match void * callback types. */
19813
19814 static void
19815 free_line_header_voidp (void *arg)
19816 {
19817 struct line_header *lh = (struct line_header *) arg;
19818
19819 delete lh;
19820 }
19821
19822 /* A convenience function to find the proper .debug_line section for a CU. */
19823
19824 static struct dwarf2_section_info *
19825 get_debug_line_section (struct dwarf2_cu *cu)
19826 {
19827 struct dwarf2_section_info *section;
19828 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19829
19830 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19831 DWO file. */
19832 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19833 section = &cu->dwo_unit->dwo_file->sections.line;
19834 else if (cu->per_cu->is_dwz)
19835 {
19836 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19837
19838 section = &dwz->line;
19839 }
19840 else
19841 section = &dwarf2_per_objfile->per_bfd->line;
19842
19843 return section;
19844 }
19845
19846 /* Read the statement program header starting at OFFSET in
19847 .debug_line, or .debug_line.dwo. Return a pointer
19848 to a struct line_header, allocated using xmalloc.
19849 Returns NULL if there is a problem reading the header, e.g., if it
19850 has a version we don't understand.
19851
19852 NOTE: the strings in the include directory and file name tables of
19853 the returned object point into the dwarf line section buffer,
19854 and must not be freed. */
19855
19856 static line_header_up
19857 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19858 {
19859 struct dwarf2_section_info *section;
19860 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19861
19862 section = get_debug_line_section (cu);
19863 section->read (dwarf2_per_objfile->objfile);
19864 if (section->buffer == NULL)
19865 {
19866 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19867 complaint (_("missing .debug_line.dwo section"));
19868 else
19869 complaint (_("missing .debug_line section"));
19870 return 0;
19871 }
19872
19873 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19874 dwarf2_per_objfile, section,
19875 &cu->header);
19876 }
19877
19878 /* Subroutine of dwarf_decode_lines to simplify it.
19879 Return the file name of the psymtab for the given file_entry.
19880 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19881 If space for the result is malloc'd, *NAME_HOLDER will be set.
19882 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19883
19884 static const char *
19885 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19886 const dwarf2_psymtab *pst,
19887 const char *comp_dir,
19888 gdb::unique_xmalloc_ptr<char> *name_holder)
19889 {
19890 const char *include_name = fe.name;
19891 const char *include_name_to_compare = include_name;
19892 const char *pst_filename;
19893 int file_is_pst;
19894
19895 const char *dir_name = fe.include_dir (lh);
19896
19897 gdb::unique_xmalloc_ptr<char> hold_compare;
19898 if (!IS_ABSOLUTE_PATH (include_name)
19899 && (dir_name != NULL || comp_dir != NULL))
19900 {
19901 /* Avoid creating a duplicate psymtab for PST.
19902 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19903 Before we do the comparison, however, we need to account
19904 for DIR_NAME and COMP_DIR.
19905 First prepend dir_name (if non-NULL). If we still don't
19906 have an absolute path prepend comp_dir (if non-NULL).
19907 However, the directory we record in the include-file's
19908 psymtab does not contain COMP_DIR (to match the
19909 corresponding symtab(s)).
19910
19911 Example:
19912
19913 bash$ cd /tmp
19914 bash$ gcc -g ./hello.c
19915 include_name = "hello.c"
19916 dir_name = "."
19917 DW_AT_comp_dir = comp_dir = "/tmp"
19918 DW_AT_name = "./hello.c"
19919
19920 */
19921
19922 if (dir_name != NULL)
19923 {
19924 name_holder->reset (concat (dir_name, SLASH_STRING,
19925 include_name, (char *) NULL));
19926 include_name = name_holder->get ();
19927 include_name_to_compare = include_name;
19928 }
19929 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19930 {
19931 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19932 include_name, (char *) NULL));
19933 include_name_to_compare = hold_compare.get ();
19934 }
19935 }
19936
19937 pst_filename = pst->filename;
19938 gdb::unique_xmalloc_ptr<char> copied_name;
19939 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19940 {
19941 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19942 pst_filename, (char *) NULL));
19943 pst_filename = copied_name.get ();
19944 }
19945
19946 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19947
19948 if (file_is_pst)
19949 return NULL;
19950 return include_name;
19951 }
19952
19953 /* State machine to track the state of the line number program. */
19954
19955 class lnp_state_machine
19956 {
19957 public:
19958 /* Initialize a machine state for the start of a line number
19959 program. */
19960 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19961 bool record_lines_p);
19962
19963 file_entry *current_file ()
19964 {
19965 /* lh->file_names is 0-based, but the file name numbers in the
19966 statement program are 1-based. */
19967 return m_line_header->file_name_at (m_file);
19968 }
19969
19970 /* Record the line in the state machine. END_SEQUENCE is true if
19971 we're processing the end of a sequence. */
19972 void record_line (bool end_sequence);
19973
19974 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19975 nop-out rest of the lines in this sequence. */
19976 void check_line_address (struct dwarf2_cu *cu,
19977 const gdb_byte *line_ptr,
19978 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19979
19980 void handle_set_discriminator (unsigned int discriminator)
19981 {
19982 m_discriminator = discriminator;
19983 m_line_has_non_zero_discriminator |= discriminator != 0;
19984 }
19985
19986 /* Handle DW_LNE_set_address. */
19987 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19988 {
19989 m_op_index = 0;
19990 address += baseaddr;
19991 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19992 }
19993
19994 /* Handle DW_LNS_advance_pc. */
19995 void handle_advance_pc (CORE_ADDR adjust);
19996
19997 /* Handle a special opcode. */
19998 void handle_special_opcode (unsigned char op_code);
19999
20000 /* Handle DW_LNS_advance_line. */
20001 void handle_advance_line (int line_delta)
20002 {
20003 advance_line (line_delta);
20004 }
20005
20006 /* Handle DW_LNS_set_file. */
20007 void handle_set_file (file_name_index file);
20008
20009 /* Handle DW_LNS_negate_stmt. */
20010 void handle_negate_stmt ()
20011 {
20012 m_is_stmt = !m_is_stmt;
20013 }
20014
20015 /* Handle DW_LNS_const_add_pc. */
20016 void handle_const_add_pc ();
20017
20018 /* Handle DW_LNS_fixed_advance_pc. */
20019 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20020 {
20021 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20022 m_op_index = 0;
20023 }
20024
20025 /* Handle DW_LNS_copy. */
20026 void handle_copy ()
20027 {
20028 record_line (false);
20029 m_discriminator = 0;
20030 }
20031
20032 /* Handle DW_LNE_end_sequence. */
20033 void handle_end_sequence ()
20034 {
20035 m_currently_recording_lines = true;
20036 }
20037
20038 private:
20039 /* Advance the line by LINE_DELTA. */
20040 void advance_line (int line_delta)
20041 {
20042 m_line += line_delta;
20043
20044 if (line_delta != 0)
20045 m_line_has_non_zero_discriminator = m_discriminator != 0;
20046 }
20047
20048 struct dwarf2_cu *m_cu;
20049
20050 gdbarch *m_gdbarch;
20051
20052 /* True if we're recording lines.
20053 Otherwise we're building partial symtabs and are just interested in
20054 finding include files mentioned by the line number program. */
20055 bool m_record_lines_p;
20056
20057 /* The line number header. */
20058 line_header *m_line_header;
20059
20060 /* These are part of the standard DWARF line number state machine,
20061 and initialized according to the DWARF spec. */
20062
20063 unsigned char m_op_index = 0;
20064 /* The line table index of the current file. */
20065 file_name_index m_file = 1;
20066 unsigned int m_line = 1;
20067
20068 /* These are initialized in the constructor. */
20069
20070 CORE_ADDR m_address;
20071 bool m_is_stmt;
20072 unsigned int m_discriminator;
20073
20074 /* Additional bits of state we need to track. */
20075
20076 /* The last file that we called dwarf2_start_subfile for.
20077 This is only used for TLLs. */
20078 unsigned int m_last_file = 0;
20079 /* The last file a line number was recorded for. */
20080 struct subfile *m_last_subfile = NULL;
20081
20082 /* When true, record the lines we decode. */
20083 bool m_currently_recording_lines = false;
20084
20085 /* The last line number that was recorded, used to coalesce
20086 consecutive entries for the same line. This can happen, for
20087 example, when discriminators are present. PR 17276. */
20088 unsigned int m_last_line = 0;
20089 bool m_line_has_non_zero_discriminator = false;
20090 };
20091
20092 void
20093 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20094 {
20095 CORE_ADDR addr_adj = (((m_op_index + adjust)
20096 / m_line_header->maximum_ops_per_instruction)
20097 * m_line_header->minimum_instruction_length);
20098 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20099 m_op_index = ((m_op_index + adjust)
20100 % m_line_header->maximum_ops_per_instruction);
20101 }
20102
20103 void
20104 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20105 {
20106 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20107 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20108 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20109 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20110 / m_line_header->maximum_ops_per_instruction)
20111 * m_line_header->minimum_instruction_length);
20112 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20113 m_op_index = ((m_op_index + adj_opcode_d)
20114 % m_line_header->maximum_ops_per_instruction);
20115
20116 int line_delta = m_line_header->line_base + adj_opcode_r;
20117 advance_line (line_delta);
20118 record_line (false);
20119 m_discriminator = 0;
20120 }
20121
20122 void
20123 lnp_state_machine::handle_set_file (file_name_index file)
20124 {
20125 m_file = file;
20126
20127 const file_entry *fe = current_file ();
20128 if (fe == NULL)
20129 dwarf2_debug_line_missing_file_complaint ();
20130 else if (m_record_lines_p)
20131 {
20132 const char *dir = fe->include_dir (m_line_header);
20133
20134 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20135 m_line_has_non_zero_discriminator = m_discriminator != 0;
20136 dwarf2_start_subfile (m_cu, fe->name, dir);
20137 }
20138 }
20139
20140 void
20141 lnp_state_machine::handle_const_add_pc ()
20142 {
20143 CORE_ADDR adjust
20144 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20145
20146 CORE_ADDR addr_adj
20147 = (((m_op_index + adjust)
20148 / m_line_header->maximum_ops_per_instruction)
20149 * m_line_header->minimum_instruction_length);
20150
20151 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20152 m_op_index = ((m_op_index + adjust)
20153 % m_line_header->maximum_ops_per_instruction);
20154 }
20155
20156 /* Return non-zero if we should add LINE to the line number table.
20157 LINE is the line to add, LAST_LINE is the last line that was added,
20158 LAST_SUBFILE is the subfile for LAST_LINE.
20159 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20160 had a non-zero discriminator.
20161
20162 We have to be careful in the presence of discriminators.
20163 E.g., for this line:
20164
20165 for (i = 0; i < 100000; i++);
20166
20167 clang can emit four line number entries for that one line,
20168 each with a different discriminator.
20169 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20170
20171 However, we want gdb to coalesce all four entries into one.
20172 Otherwise the user could stepi into the middle of the line and
20173 gdb would get confused about whether the pc really was in the
20174 middle of the line.
20175
20176 Things are further complicated by the fact that two consecutive
20177 line number entries for the same line is a heuristic used by gcc
20178 to denote the end of the prologue. So we can't just discard duplicate
20179 entries, we have to be selective about it. The heuristic we use is
20180 that we only collapse consecutive entries for the same line if at least
20181 one of those entries has a non-zero discriminator. PR 17276.
20182
20183 Note: Addresses in the line number state machine can never go backwards
20184 within one sequence, thus this coalescing is ok. */
20185
20186 static int
20187 dwarf_record_line_p (struct dwarf2_cu *cu,
20188 unsigned int line, unsigned int last_line,
20189 int line_has_non_zero_discriminator,
20190 struct subfile *last_subfile)
20191 {
20192 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20193 return 1;
20194 if (line != last_line)
20195 return 1;
20196 /* Same line for the same file that we've seen already.
20197 As a last check, for pr 17276, only record the line if the line
20198 has never had a non-zero discriminator. */
20199 if (!line_has_non_zero_discriminator)
20200 return 1;
20201 return 0;
20202 }
20203
20204 /* Use the CU's builder to record line number LINE beginning at
20205 address ADDRESS in the line table of subfile SUBFILE. */
20206
20207 static void
20208 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20209 unsigned int line, CORE_ADDR address, bool is_stmt,
20210 struct dwarf2_cu *cu)
20211 {
20212 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20213
20214 if (dwarf_line_debug)
20215 {
20216 fprintf_unfiltered (gdb_stdlog,
20217 "Recording line %u, file %s, address %s\n",
20218 line, lbasename (subfile->name),
20219 paddress (gdbarch, address));
20220 }
20221
20222 if (cu != nullptr)
20223 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20224 }
20225
20226 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20227 Mark the end of a set of line number records.
20228 The arguments are the same as for dwarf_record_line_1.
20229 If SUBFILE is NULL the request is ignored. */
20230
20231 static void
20232 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20233 CORE_ADDR address, struct dwarf2_cu *cu)
20234 {
20235 if (subfile == NULL)
20236 return;
20237
20238 if (dwarf_line_debug)
20239 {
20240 fprintf_unfiltered (gdb_stdlog,
20241 "Finishing current line, file %s, address %s\n",
20242 lbasename (subfile->name),
20243 paddress (gdbarch, address));
20244 }
20245
20246 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20247 }
20248
20249 void
20250 lnp_state_machine::record_line (bool end_sequence)
20251 {
20252 if (dwarf_line_debug)
20253 {
20254 fprintf_unfiltered (gdb_stdlog,
20255 "Processing actual line %u: file %u,"
20256 " address %s, is_stmt %u, discrim %u%s\n",
20257 m_line, m_file,
20258 paddress (m_gdbarch, m_address),
20259 m_is_stmt, m_discriminator,
20260 (end_sequence ? "\t(end sequence)" : ""));
20261 }
20262
20263 file_entry *fe = current_file ();
20264
20265 if (fe == NULL)
20266 dwarf2_debug_line_missing_file_complaint ();
20267 /* For now we ignore lines not starting on an instruction boundary.
20268 But not when processing end_sequence for compatibility with the
20269 previous version of the code. */
20270 else if (m_op_index == 0 || end_sequence)
20271 {
20272 fe->included_p = 1;
20273 if (m_record_lines_p)
20274 {
20275 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20276 || end_sequence)
20277 {
20278 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20279 m_currently_recording_lines ? m_cu : nullptr);
20280 }
20281
20282 if (!end_sequence)
20283 {
20284 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20285
20286 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20287 m_line_has_non_zero_discriminator,
20288 m_last_subfile))
20289 {
20290 buildsym_compunit *builder = m_cu->get_builder ();
20291 dwarf_record_line_1 (m_gdbarch,
20292 builder->get_current_subfile (),
20293 m_line, m_address, is_stmt,
20294 m_currently_recording_lines ? m_cu : nullptr);
20295 }
20296 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20297 m_last_line = m_line;
20298 }
20299 }
20300 }
20301 }
20302
20303 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20304 line_header *lh, bool record_lines_p)
20305 {
20306 m_cu = cu;
20307 m_gdbarch = arch;
20308 m_record_lines_p = record_lines_p;
20309 m_line_header = lh;
20310
20311 m_currently_recording_lines = true;
20312
20313 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20314 was a line entry for it so that the backend has a chance to adjust it
20315 and also record it in case it needs it. This is currently used by MIPS
20316 code, cf. `mips_adjust_dwarf2_line'. */
20317 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20318 m_is_stmt = lh->default_is_stmt;
20319 m_discriminator = 0;
20320 }
20321
20322 void
20323 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20324 const gdb_byte *line_ptr,
20325 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20326 {
20327 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20328 the pc range of the CU. However, we restrict the test to only ADDRESS
20329 values of zero to preserve GDB's previous behaviour which is to handle
20330 the specific case of a function being GC'd by the linker. */
20331
20332 if (address == 0 && address < unrelocated_lowpc)
20333 {
20334 /* This line table is for a function which has been
20335 GCd by the linker. Ignore it. PR gdb/12528 */
20336
20337 struct objfile *objfile = cu->per_objfile->objfile;
20338 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20339
20340 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20341 line_offset, objfile_name (objfile));
20342 m_currently_recording_lines = false;
20343 /* Note: m_currently_recording_lines is left as false until we see
20344 DW_LNE_end_sequence. */
20345 }
20346 }
20347
20348 /* Subroutine of dwarf_decode_lines to simplify it.
20349 Process the line number information in LH.
20350 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20351 program in order to set included_p for every referenced header. */
20352
20353 static void
20354 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20355 const int decode_for_pst_p, CORE_ADDR lowpc)
20356 {
20357 const gdb_byte *line_ptr, *extended_end;
20358 const gdb_byte *line_end;
20359 unsigned int bytes_read, extended_len;
20360 unsigned char op_code, extended_op;
20361 CORE_ADDR baseaddr;
20362 struct objfile *objfile = cu->per_objfile->objfile;
20363 bfd *abfd = objfile->obfd;
20364 struct gdbarch *gdbarch = objfile->arch ();
20365 /* True if we're recording line info (as opposed to building partial
20366 symtabs and just interested in finding include files mentioned by
20367 the line number program). */
20368 bool record_lines_p = !decode_for_pst_p;
20369
20370 baseaddr = objfile->text_section_offset ();
20371
20372 line_ptr = lh->statement_program_start;
20373 line_end = lh->statement_program_end;
20374
20375 /* Read the statement sequences until there's nothing left. */
20376 while (line_ptr < line_end)
20377 {
20378 /* The DWARF line number program state machine. Reset the state
20379 machine at the start of each sequence. */
20380 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20381 bool end_sequence = false;
20382
20383 if (record_lines_p)
20384 {
20385 /* Start a subfile for the current file of the state
20386 machine. */
20387 const file_entry *fe = state_machine.current_file ();
20388
20389 if (fe != NULL)
20390 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20391 }
20392
20393 /* Decode the table. */
20394 while (line_ptr < line_end && !end_sequence)
20395 {
20396 op_code = read_1_byte (abfd, line_ptr);
20397 line_ptr += 1;
20398
20399 if (op_code >= lh->opcode_base)
20400 {
20401 /* Special opcode. */
20402 state_machine.handle_special_opcode (op_code);
20403 }
20404 else switch (op_code)
20405 {
20406 case DW_LNS_extended_op:
20407 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20408 &bytes_read);
20409 line_ptr += bytes_read;
20410 extended_end = line_ptr + extended_len;
20411 extended_op = read_1_byte (abfd, line_ptr);
20412 line_ptr += 1;
20413 switch (extended_op)
20414 {
20415 case DW_LNE_end_sequence:
20416 state_machine.handle_end_sequence ();
20417 end_sequence = true;
20418 break;
20419 case DW_LNE_set_address:
20420 {
20421 CORE_ADDR address
20422 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20423 line_ptr += bytes_read;
20424
20425 state_machine.check_line_address (cu, line_ptr,
20426 lowpc - baseaddr, address);
20427 state_machine.handle_set_address (baseaddr, address);
20428 }
20429 break;
20430 case DW_LNE_define_file:
20431 {
20432 const char *cur_file;
20433 unsigned int mod_time, length;
20434 dir_index dindex;
20435
20436 cur_file = read_direct_string (abfd, line_ptr,
20437 &bytes_read);
20438 line_ptr += bytes_read;
20439 dindex = (dir_index)
20440 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20441 line_ptr += bytes_read;
20442 mod_time =
20443 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20444 line_ptr += bytes_read;
20445 length =
20446 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20447 line_ptr += bytes_read;
20448 lh->add_file_name (cur_file, dindex, mod_time, length);
20449 }
20450 break;
20451 case DW_LNE_set_discriminator:
20452 {
20453 /* The discriminator is not interesting to the
20454 debugger; just ignore it. We still need to
20455 check its value though:
20456 if there are consecutive entries for the same
20457 (non-prologue) line we want to coalesce them.
20458 PR 17276. */
20459 unsigned int discr
20460 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20461 line_ptr += bytes_read;
20462
20463 state_machine.handle_set_discriminator (discr);
20464 }
20465 break;
20466 default:
20467 complaint (_("mangled .debug_line section"));
20468 return;
20469 }
20470 /* Make sure that we parsed the extended op correctly. If e.g.
20471 we expected a different address size than the producer used,
20472 we may have read the wrong number of bytes. */
20473 if (line_ptr != extended_end)
20474 {
20475 complaint (_("mangled .debug_line section"));
20476 return;
20477 }
20478 break;
20479 case DW_LNS_copy:
20480 state_machine.handle_copy ();
20481 break;
20482 case DW_LNS_advance_pc:
20483 {
20484 CORE_ADDR adjust
20485 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20486 line_ptr += bytes_read;
20487
20488 state_machine.handle_advance_pc (adjust);
20489 }
20490 break;
20491 case DW_LNS_advance_line:
20492 {
20493 int line_delta
20494 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20495 line_ptr += bytes_read;
20496
20497 state_machine.handle_advance_line (line_delta);
20498 }
20499 break;
20500 case DW_LNS_set_file:
20501 {
20502 file_name_index file
20503 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20504 &bytes_read);
20505 line_ptr += bytes_read;
20506
20507 state_machine.handle_set_file (file);
20508 }
20509 break;
20510 case DW_LNS_set_column:
20511 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20512 line_ptr += bytes_read;
20513 break;
20514 case DW_LNS_negate_stmt:
20515 state_machine.handle_negate_stmt ();
20516 break;
20517 case DW_LNS_set_basic_block:
20518 break;
20519 /* Add to the address register of the state machine the
20520 address increment value corresponding to special opcode
20521 255. I.e., this value is scaled by the minimum
20522 instruction length since special opcode 255 would have
20523 scaled the increment. */
20524 case DW_LNS_const_add_pc:
20525 state_machine.handle_const_add_pc ();
20526 break;
20527 case DW_LNS_fixed_advance_pc:
20528 {
20529 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20530 line_ptr += 2;
20531
20532 state_machine.handle_fixed_advance_pc (addr_adj);
20533 }
20534 break;
20535 default:
20536 {
20537 /* Unknown standard opcode, ignore it. */
20538 int i;
20539
20540 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20541 {
20542 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20543 line_ptr += bytes_read;
20544 }
20545 }
20546 }
20547 }
20548
20549 if (!end_sequence)
20550 dwarf2_debug_line_missing_end_sequence_complaint ();
20551
20552 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20553 in which case we still finish recording the last line). */
20554 state_machine.record_line (true);
20555 }
20556 }
20557
20558 /* Decode the Line Number Program (LNP) for the given line_header
20559 structure and CU. The actual information extracted and the type
20560 of structures created from the LNP depends on the value of PST.
20561
20562 1. If PST is NULL, then this procedure uses the data from the program
20563 to create all necessary symbol tables, and their linetables.
20564
20565 2. If PST is not NULL, this procedure reads the program to determine
20566 the list of files included by the unit represented by PST, and
20567 builds all the associated partial symbol tables.
20568
20569 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20570 It is used for relative paths in the line table.
20571 NOTE: When processing partial symtabs (pst != NULL),
20572 comp_dir == pst->dirname.
20573
20574 NOTE: It is important that psymtabs have the same file name (via strcmp)
20575 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20576 symtab we don't use it in the name of the psymtabs we create.
20577 E.g. expand_line_sal requires this when finding psymtabs to expand.
20578 A good testcase for this is mb-inline.exp.
20579
20580 LOWPC is the lowest address in CU (or 0 if not known).
20581
20582 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20583 for its PC<->lines mapping information. Otherwise only the filename
20584 table is read in. */
20585
20586 static void
20587 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20588 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20589 CORE_ADDR lowpc, int decode_mapping)
20590 {
20591 struct objfile *objfile = cu->per_objfile->objfile;
20592 const int decode_for_pst_p = (pst != NULL);
20593
20594 if (decode_mapping)
20595 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20596
20597 if (decode_for_pst_p)
20598 {
20599 /* Now that we're done scanning the Line Header Program, we can
20600 create the psymtab of each included file. */
20601 for (auto &file_entry : lh->file_names ())
20602 if (file_entry.included_p == 1)
20603 {
20604 gdb::unique_xmalloc_ptr<char> name_holder;
20605 const char *include_name =
20606 psymtab_include_file_name (lh, file_entry, pst,
20607 comp_dir, &name_holder);
20608 if (include_name != NULL)
20609 dwarf2_create_include_psymtab (include_name, pst, objfile);
20610 }
20611 }
20612 else
20613 {
20614 /* Make sure a symtab is created for every file, even files
20615 which contain only variables (i.e. no code with associated
20616 line numbers). */
20617 buildsym_compunit *builder = cu->get_builder ();
20618 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20619
20620 for (auto &fe : lh->file_names ())
20621 {
20622 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20623 if (builder->get_current_subfile ()->symtab == NULL)
20624 {
20625 builder->get_current_subfile ()->symtab
20626 = allocate_symtab (cust,
20627 builder->get_current_subfile ()->name);
20628 }
20629 fe.symtab = builder->get_current_subfile ()->symtab;
20630 }
20631 }
20632 }
20633
20634 /* Start a subfile for DWARF. FILENAME is the name of the file and
20635 DIRNAME the name of the source directory which contains FILENAME
20636 or NULL if not known.
20637 This routine tries to keep line numbers from identical absolute and
20638 relative file names in a common subfile.
20639
20640 Using the `list' example from the GDB testsuite, which resides in
20641 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20642 of /srcdir/list0.c yields the following debugging information for list0.c:
20643
20644 DW_AT_name: /srcdir/list0.c
20645 DW_AT_comp_dir: /compdir
20646 files.files[0].name: list0.h
20647 files.files[0].dir: /srcdir
20648 files.files[1].name: list0.c
20649 files.files[1].dir: /srcdir
20650
20651 The line number information for list0.c has to end up in a single
20652 subfile, so that `break /srcdir/list0.c:1' works as expected.
20653 start_subfile will ensure that this happens provided that we pass the
20654 concatenation of files.files[1].dir and files.files[1].name as the
20655 subfile's name. */
20656
20657 static void
20658 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20659 const char *dirname)
20660 {
20661 gdb::unique_xmalloc_ptr<char> copy;
20662
20663 /* In order not to lose the line information directory,
20664 we concatenate it to the filename when it makes sense.
20665 Note that the Dwarf3 standard says (speaking of filenames in line
20666 information): ``The directory index is ignored for file names
20667 that represent full path names''. Thus ignoring dirname in the
20668 `else' branch below isn't an issue. */
20669
20670 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20671 {
20672 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20673 filename = copy.get ();
20674 }
20675
20676 cu->get_builder ()->start_subfile (filename);
20677 }
20678
20679 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20680 buildsym_compunit constructor. */
20681
20682 struct compunit_symtab *
20683 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20684 CORE_ADDR low_pc)
20685 {
20686 gdb_assert (m_builder == nullptr);
20687
20688 m_builder.reset (new struct buildsym_compunit
20689 (this->per_objfile->objfile,
20690 name, comp_dir, language, low_pc));
20691
20692 list_in_scope = get_builder ()->get_file_symbols ();
20693
20694 get_builder ()->record_debugformat ("DWARF 2");
20695 get_builder ()->record_producer (producer);
20696
20697 processing_has_namespace_info = false;
20698
20699 return get_builder ()->get_compunit_symtab ();
20700 }
20701
20702 static void
20703 var_decode_location (struct attribute *attr, struct symbol *sym,
20704 struct dwarf2_cu *cu)
20705 {
20706 struct objfile *objfile = cu->per_objfile->objfile;
20707 struct comp_unit_head *cu_header = &cu->header;
20708
20709 /* NOTE drow/2003-01-30: There used to be a comment and some special
20710 code here to turn a symbol with DW_AT_external and a
20711 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20712 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20713 with some versions of binutils) where shared libraries could have
20714 relocations against symbols in their debug information - the
20715 minimal symbol would have the right address, but the debug info
20716 would not. It's no longer necessary, because we will explicitly
20717 apply relocations when we read in the debug information now. */
20718
20719 /* A DW_AT_location attribute with no contents indicates that a
20720 variable has been optimized away. */
20721 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20722 {
20723 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20724 return;
20725 }
20726
20727 /* Handle one degenerate form of location expression specially, to
20728 preserve GDB's previous behavior when section offsets are
20729 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20730 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20731
20732 if (attr->form_is_block ()
20733 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20734 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20735 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20736 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20737 && (DW_BLOCK (attr)->size
20738 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20739 {
20740 unsigned int dummy;
20741
20742 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20743 SET_SYMBOL_VALUE_ADDRESS
20744 (sym, cu->header.read_address (objfile->obfd,
20745 DW_BLOCK (attr)->data + 1,
20746 &dummy));
20747 else
20748 SET_SYMBOL_VALUE_ADDRESS
20749 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20750 &dummy));
20751 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20752 fixup_symbol_section (sym, objfile);
20753 SET_SYMBOL_VALUE_ADDRESS
20754 (sym,
20755 SYMBOL_VALUE_ADDRESS (sym)
20756 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20757 return;
20758 }
20759
20760 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20761 expression evaluator, and use LOC_COMPUTED only when necessary
20762 (i.e. when the value of a register or memory location is
20763 referenced, or a thread-local block, etc.). Then again, it might
20764 not be worthwhile. I'm assuming that it isn't unless performance
20765 or memory numbers show me otherwise. */
20766
20767 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20768
20769 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20770 cu->has_loclist = true;
20771 }
20772
20773 /* Given a pointer to a DWARF information entry, figure out if we need
20774 to make a symbol table entry for it, and if so, create a new entry
20775 and return a pointer to it.
20776 If TYPE is NULL, determine symbol type from the die, otherwise
20777 used the passed type.
20778 If SPACE is not NULL, use it to hold the new symbol. If it is
20779 NULL, allocate a new symbol on the objfile's obstack. */
20780
20781 static struct symbol *
20782 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20783 struct symbol *space)
20784 {
20785 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20786 struct objfile *objfile = dwarf2_per_objfile->objfile;
20787 struct gdbarch *gdbarch = objfile->arch ();
20788 struct symbol *sym = NULL;
20789 const char *name;
20790 struct attribute *attr = NULL;
20791 struct attribute *attr2 = NULL;
20792 CORE_ADDR baseaddr;
20793 struct pending **list_to_add = NULL;
20794
20795 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20796
20797 baseaddr = objfile->text_section_offset ();
20798
20799 name = dwarf2_name (die, cu);
20800 if (name)
20801 {
20802 int suppress_add = 0;
20803
20804 if (space)
20805 sym = space;
20806 else
20807 sym = new (&objfile->objfile_obstack) symbol;
20808 OBJSTAT (objfile, n_syms++);
20809
20810 /* Cache this symbol's name and the name's demangled form (if any). */
20811 sym->set_language (cu->language, &objfile->objfile_obstack);
20812 /* Fortran does not have mangling standard and the mangling does differ
20813 between gfortran, iFort etc. */
20814 const char *physname
20815 = (cu->language == language_fortran
20816 ? dwarf2_full_name (name, die, cu)
20817 : dwarf2_physname (name, die, cu));
20818 const char *linkagename = dw2_linkage_name (die, cu);
20819
20820 if (linkagename == nullptr || cu->language == language_ada)
20821 sym->set_linkage_name (physname);
20822 else
20823 {
20824 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20825 sym->set_linkage_name (linkagename);
20826 }
20827
20828 /* Default assumptions.
20829 Use the passed type or decode it from the die. */
20830 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20831 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20832 if (type != NULL)
20833 SYMBOL_TYPE (sym) = type;
20834 else
20835 SYMBOL_TYPE (sym) = die_type (die, cu);
20836 attr = dwarf2_attr (die,
20837 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20838 cu);
20839 if (attr != nullptr)
20840 {
20841 SYMBOL_LINE (sym) = DW_UNSND (attr);
20842 }
20843
20844 attr = dwarf2_attr (die,
20845 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20846 cu);
20847 if (attr != nullptr)
20848 {
20849 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20850 struct file_entry *fe;
20851
20852 if (cu->line_header != NULL)
20853 fe = cu->line_header->file_name_at (file_index);
20854 else
20855 fe = NULL;
20856
20857 if (fe == NULL)
20858 complaint (_("file index out of range"));
20859 else
20860 symbol_set_symtab (sym, fe->symtab);
20861 }
20862
20863 switch (die->tag)
20864 {
20865 case DW_TAG_label:
20866 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20867 if (attr != nullptr)
20868 {
20869 CORE_ADDR addr;
20870
20871 addr = attr->value_as_address ();
20872 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20873 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20874 }
20875 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20876 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20877 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20878 add_symbol_to_list (sym, cu->list_in_scope);
20879 break;
20880 case DW_TAG_subprogram:
20881 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20882 finish_block. */
20883 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20884 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20885 if ((attr2 && (DW_UNSND (attr2) != 0))
20886 || cu->language == language_ada
20887 || cu->language == language_fortran)
20888 {
20889 /* Subprograms marked external are stored as a global symbol.
20890 Ada and Fortran subprograms, whether marked external or
20891 not, are always stored as a global symbol, because we want
20892 to be able to access them globally. For instance, we want
20893 to be able to break on a nested subprogram without having
20894 to specify the context. */
20895 list_to_add = cu->get_builder ()->get_global_symbols ();
20896 }
20897 else
20898 {
20899 list_to_add = cu->list_in_scope;
20900 }
20901 break;
20902 case DW_TAG_inlined_subroutine:
20903 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20904 finish_block. */
20905 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20906 SYMBOL_INLINED (sym) = 1;
20907 list_to_add = cu->list_in_scope;
20908 break;
20909 case DW_TAG_template_value_param:
20910 suppress_add = 1;
20911 /* Fall through. */
20912 case DW_TAG_constant:
20913 case DW_TAG_variable:
20914 case DW_TAG_member:
20915 /* Compilation with minimal debug info may result in
20916 variables with missing type entries. Change the
20917 misleading `void' type to something sensible. */
20918 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20919 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20920
20921 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20922 /* In the case of DW_TAG_member, we should only be called for
20923 static const members. */
20924 if (die->tag == DW_TAG_member)
20925 {
20926 /* dwarf2_add_field uses die_is_declaration,
20927 so we do the same. */
20928 gdb_assert (die_is_declaration (die, cu));
20929 gdb_assert (attr);
20930 }
20931 if (attr != nullptr)
20932 {
20933 dwarf2_const_value (attr, sym, cu);
20934 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20935 if (!suppress_add)
20936 {
20937 if (attr2 && (DW_UNSND (attr2) != 0))
20938 list_to_add = cu->get_builder ()->get_global_symbols ();
20939 else
20940 list_to_add = cu->list_in_scope;
20941 }
20942 break;
20943 }
20944 attr = dwarf2_attr (die, DW_AT_location, cu);
20945 if (attr != nullptr)
20946 {
20947 var_decode_location (attr, sym, cu);
20948 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20949
20950 /* Fortran explicitly imports any global symbols to the local
20951 scope by DW_TAG_common_block. */
20952 if (cu->language == language_fortran && die->parent
20953 && die->parent->tag == DW_TAG_common_block)
20954 attr2 = NULL;
20955
20956 if (SYMBOL_CLASS (sym) == LOC_STATIC
20957 && SYMBOL_VALUE_ADDRESS (sym) == 0
20958 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20959 {
20960 /* When a static variable is eliminated by the linker,
20961 the corresponding debug information is not stripped
20962 out, but the variable address is set to null;
20963 do not add such variables into symbol table. */
20964 }
20965 else if (attr2 && (DW_UNSND (attr2) != 0))
20966 {
20967 if (SYMBOL_CLASS (sym) == LOC_STATIC
20968 && (objfile->flags & OBJF_MAINLINE) == 0
20969 && dwarf2_per_objfile->per_bfd->can_copy)
20970 {
20971 /* A global static variable might be subject to
20972 copy relocation. We first check for a local
20973 minsym, though, because maybe the symbol was
20974 marked hidden, in which case this would not
20975 apply. */
20976 bound_minimal_symbol found
20977 = (lookup_minimal_symbol_linkage
20978 (sym->linkage_name (), objfile));
20979 if (found.minsym != nullptr)
20980 sym->maybe_copied = 1;
20981 }
20982
20983 /* A variable with DW_AT_external is never static,
20984 but it may be block-scoped. */
20985 list_to_add
20986 = ((cu->list_in_scope
20987 == cu->get_builder ()->get_file_symbols ())
20988 ? cu->get_builder ()->get_global_symbols ()
20989 : cu->list_in_scope);
20990 }
20991 else
20992 list_to_add = cu->list_in_scope;
20993 }
20994 else
20995 {
20996 /* We do not know the address of this symbol.
20997 If it is an external symbol and we have type information
20998 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20999 The address of the variable will then be determined from
21000 the minimal symbol table whenever the variable is
21001 referenced. */
21002 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21003
21004 /* Fortran explicitly imports any global symbols to the local
21005 scope by DW_TAG_common_block. */
21006 if (cu->language == language_fortran && die->parent
21007 && die->parent->tag == DW_TAG_common_block)
21008 {
21009 /* SYMBOL_CLASS doesn't matter here because
21010 read_common_block is going to reset it. */
21011 if (!suppress_add)
21012 list_to_add = cu->list_in_scope;
21013 }
21014 else if (attr2 && (DW_UNSND (attr2) != 0)
21015 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21016 {
21017 /* A variable with DW_AT_external is never static, but it
21018 may be block-scoped. */
21019 list_to_add
21020 = ((cu->list_in_scope
21021 == cu->get_builder ()->get_file_symbols ())
21022 ? cu->get_builder ()->get_global_symbols ()
21023 : cu->list_in_scope);
21024
21025 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21026 }
21027 else if (!die_is_declaration (die, cu))
21028 {
21029 /* Use the default LOC_OPTIMIZED_OUT class. */
21030 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21031 if (!suppress_add)
21032 list_to_add = cu->list_in_scope;
21033 }
21034 }
21035 break;
21036 case DW_TAG_formal_parameter:
21037 {
21038 /* If we are inside a function, mark this as an argument. If
21039 not, we might be looking at an argument to an inlined function
21040 when we do not have enough information to show inlined frames;
21041 pretend it's a local variable in that case so that the user can
21042 still see it. */
21043 struct context_stack *curr
21044 = cu->get_builder ()->get_current_context_stack ();
21045 if (curr != nullptr && curr->name != nullptr)
21046 SYMBOL_IS_ARGUMENT (sym) = 1;
21047 attr = dwarf2_attr (die, DW_AT_location, cu);
21048 if (attr != nullptr)
21049 {
21050 var_decode_location (attr, sym, cu);
21051 }
21052 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21053 if (attr != nullptr)
21054 {
21055 dwarf2_const_value (attr, sym, cu);
21056 }
21057
21058 list_to_add = cu->list_in_scope;
21059 }
21060 break;
21061 case DW_TAG_unspecified_parameters:
21062 /* From varargs functions; gdb doesn't seem to have any
21063 interest in this information, so just ignore it for now.
21064 (FIXME?) */
21065 break;
21066 case DW_TAG_template_type_param:
21067 suppress_add = 1;
21068 /* Fall through. */
21069 case DW_TAG_class_type:
21070 case DW_TAG_interface_type:
21071 case DW_TAG_structure_type:
21072 case DW_TAG_union_type:
21073 case DW_TAG_set_type:
21074 case DW_TAG_enumeration_type:
21075 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21076 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21077
21078 {
21079 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21080 really ever be static objects: otherwise, if you try
21081 to, say, break of a class's method and you're in a file
21082 which doesn't mention that class, it won't work unless
21083 the check for all static symbols in lookup_symbol_aux
21084 saves you. See the OtherFileClass tests in
21085 gdb.c++/namespace.exp. */
21086
21087 if (!suppress_add)
21088 {
21089 buildsym_compunit *builder = cu->get_builder ();
21090 list_to_add
21091 = (cu->list_in_scope == builder->get_file_symbols ()
21092 && cu->language == language_cplus
21093 ? builder->get_global_symbols ()
21094 : cu->list_in_scope);
21095
21096 /* The semantics of C++ state that "struct foo {
21097 ... }" also defines a typedef for "foo". */
21098 if (cu->language == language_cplus
21099 || cu->language == language_ada
21100 || cu->language == language_d
21101 || cu->language == language_rust)
21102 {
21103 /* The symbol's name is already allocated along
21104 with this objfile, so we don't need to
21105 duplicate it for the type. */
21106 if (SYMBOL_TYPE (sym)->name () == 0)
21107 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21108 }
21109 }
21110 }
21111 break;
21112 case DW_TAG_typedef:
21113 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21114 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21115 list_to_add = cu->list_in_scope;
21116 break;
21117 case DW_TAG_base_type:
21118 case DW_TAG_subrange_type:
21119 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21120 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21121 list_to_add = cu->list_in_scope;
21122 break;
21123 case DW_TAG_enumerator:
21124 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21125 if (attr != nullptr)
21126 {
21127 dwarf2_const_value (attr, sym, cu);
21128 }
21129 {
21130 /* NOTE: carlton/2003-11-10: See comment above in the
21131 DW_TAG_class_type, etc. block. */
21132
21133 list_to_add
21134 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21135 && cu->language == language_cplus
21136 ? cu->get_builder ()->get_global_symbols ()
21137 : cu->list_in_scope);
21138 }
21139 break;
21140 case DW_TAG_imported_declaration:
21141 case DW_TAG_namespace:
21142 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21143 list_to_add = cu->get_builder ()->get_global_symbols ();
21144 break;
21145 case DW_TAG_module:
21146 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21147 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21148 list_to_add = cu->get_builder ()->get_global_symbols ();
21149 break;
21150 case DW_TAG_common_block:
21151 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21152 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21153 add_symbol_to_list (sym, cu->list_in_scope);
21154 break;
21155 default:
21156 /* Not a tag we recognize. Hopefully we aren't processing
21157 trash data, but since we must specifically ignore things
21158 we don't recognize, there is nothing else we should do at
21159 this point. */
21160 complaint (_("unsupported tag: '%s'"),
21161 dwarf_tag_name (die->tag));
21162 break;
21163 }
21164
21165 if (suppress_add)
21166 {
21167 sym->hash_next = objfile->template_symbols;
21168 objfile->template_symbols = sym;
21169 list_to_add = NULL;
21170 }
21171
21172 if (list_to_add != NULL)
21173 add_symbol_to_list (sym, list_to_add);
21174
21175 /* For the benefit of old versions of GCC, check for anonymous
21176 namespaces based on the demangled name. */
21177 if (!cu->processing_has_namespace_info
21178 && cu->language == language_cplus)
21179 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21180 }
21181 return (sym);
21182 }
21183
21184 /* Given an attr with a DW_FORM_dataN value in host byte order,
21185 zero-extend it as appropriate for the symbol's type. The DWARF
21186 standard (v4) is not entirely clear about the meaning of using
21187 DW_FORM_dataN for a constant with a signed type, where the type is
21188 wider than the data. The conclusion of a discussion on the DWARF
21189 list was that this is unspecified. We choose to always zero-extend
21190 because that is the interpretation long in use by GCC. */
21191
21192 static gdb_byte *
21193 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21194 struct dwarf2_cu *cu, LONGEST *value, int bits)
21195 {
21196 struct objfile *objfile = cu->per_objfile->objfile;
21197 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21198 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21199 LONGEST l = DW_UNSND (attr);
21200
21201 if (bits < sizeof (*value) * 8)
21202 {
21203 l &= ((LONGEST) 1 << bits) - 1;
21204 *value = l;
21205 }
21206 else if (bits == sizeof (*value) * 8)
21207 *value = l;
21208 else
21209 {
21210 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21211 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21212 return bytes;
21213 }
21214
21215 return NULL;
21216 }
21217
21218 /* Read a constant value from an attribute. Either set *VALUE, or if
21219 the value does not fit in *VALUE, set *BYTES - either already
21220 allocated on the objfile obstack, or newly allocated on OBSTACK,
21221 or, set *BATON, if we translated the constant to a location
21222 expression. */
21223
21224 static void
21225 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21226 const char *name, struct obstack *obstack,
21227 struct dwarf2_cu *cu,
21228 LONGEST *value, const gdb_byte **bytes,
21229 struct dwarf2_locexpr_baton **baton)
21230 {
21231 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21232 struct objfile *objfile = per_objfile->objfile;
21233 struct comp_unit_head *cu_header = &cu->header;
21234 struct dwarf_block *blk;
21235 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21236 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21237
21238 *value = 0;
21239 *bytes = NULL;
21240 *baton = NULL;
21241
21242 switch (attr->form)
21243 {
21244 case DW_FORM_addr:
21245 case DW_FORM_addrx:
21246 case DW_FORM_GNU_addr_index:
21247 {
21248 gdb_byte *data;
21249
21250 if (TYPE_LENGTH (type) != cu_header->addr_size)
21251 dwarf2_const_value_length_mismatch_complaint (name,
21252 cu_header->addr_size,
21253 TYPE_LENGTH (type));
21254 /* Symbols of this form are reasonably rare, so we just
21255 piggyback on the existing location code rather than writing
21256 a new implementation of symbol_computed_ops. */
21257 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21258 (*baton)->per_objfile = per_objfile;
21259 (*baton)->per_cu = cu->per_cu;
21260 gdb_assert ((*baton)->per_cu);
21261
21262 (*baton)->size = 2 + cu_header->addr_size;
21263 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21264 (*baton)->data = data;
21265
21266 data[0] = DW_OP_addr;
21267 store_unsigned_integer (&data[1], cu_header->addr_size,
21268 byte_order, DW_ADDR (attr));
21269 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21270 }
21271 break;
21272 case DW_FORM_string:
21273 case DW_FORM_strp:
21274 case DW_FORM_strx:
21275 case DW_FORM_GNU_str_index:
21276 case DW_FORM_GNU_strp_alt:
21277 /* DW_STRING is already allocated on the objfile obstack, point
21278 directly to it. */
21279 *bytes = (const gdb_byte *) DW_STRING (attr);
21280 break;
21281 case DW_FORM_block1:
21282 case DW_FORM_block2:
21283 case DW_FORM_block4:
21284 case DW_FORM_block:
21285 case DW_FORM_exprloc:
21286 case DW_FORM_data16:
21287 blk = DW_BLOCK (attr);
21288 if (TYPE_LENGTH (type) != blk->size)
21289 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21290 TYPE_LENGTH (type));
21291 *bytes = blk->data;
21292 break;
21293
21294 /* The DW_AT_const_value attributes are supposed to carry the
21295 symbol's value "represented as it would be on the target
21296 architecture." By the time we get here, it's already been
21297 converted to host endianness, so we just need to sign- or
21298 zero-extend it as appropriate. */
21299 case DW_FORM_data1:
21300 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21301 break;
21302 case DW_FORM_data2:
21303 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21304 break;
21305 case DW_FORM_data4:
21306 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21307 break;
21308 case DW_FORM_data8:
21309 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21310 break;
21311
21312 case DW_FORM_sdata:
21313 case DW_FORM_implicit_const:
21314 *value = DW_SND (attr);
21315 break;
21316
21317 case DW_FORM_udata:
21318 *value = DW_UNSND (attr);
21319 break;
21320
21321 default:
21322 complaint (_("unsupported const value attribute form: '%s'"),
21323 dwarf_form_name (attr->form));
21324 *value = 0;
21325 break;
21326 }
21327 }
21328
21329
21330 /* Copy constant value from an attribute to a symbol. */
21331
21332 static void
21333 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21334 struct dwarf2_cu *cu)
21335 {
21336 struct objfile *objfile = cu->per_objfile->objfile;
21337 LONGEST value;
21338 const gdb_byte *bytes;
21339 struct dwarf2_locexpr_baton *baton;
21340
21341 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21342 sym->print_name (),
21343 &objfile->objfile_obstack, cu,
21344 &value, &bytes, &baton);
21345
21346 if (baton != NULL)
21347 {
21348 SYMBOL_LOCATION_BATON (sym) = baton;
21349 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21350 }
21351 else if (bytes != NULL)
21352 {
21353 SYMBOL_VALUE_BYTES (sym) = bytes;
21354 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21355 }
21356 else
21357 {
21358 SYMBOL_VALUE (sym) = value;
21359 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21360 }
21361 }
21362
21363 /* Return the type of the die in question using its DW_AT_type attribute. */
21364
21365 static struct type *
21366 die_type (struct die_info *die, struct dwarf2_cu *cu)
21367 {
21368 struct attribute *type_attr;
21369
21370 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21371 if (!type_attr)
21372 {
21373 struct objfile *objfile = cu->per_objfile->objfile;
21374 /* A missing DW_AT_type represents a void type. */
21375 return objfile_type (objfile)->builtin_void;
21376 }
21377
21378 return lookup_die_type (die, type_attr, cu);
21379 }
21380
21381 /* True iff CU's producer generates GNAT Ada auxiliary information
21382 that allows to find parallel types through that information instead
21383 of having to do expensive parallel lookups by type name. */
21384
21385 static int
21386 need_gnat_info (struct dwarf2_cu *cu)
21387 {
21388 /* Assume that the Ada compiler was GNAT, which always produces
21389 the auxiliary information. */
21390 return (cu->language == language_ada);
21391 }
21392
21393 /* Return the auxiliary type of the die in question using its
21394 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21395 attribute is not present. */
21396
21397 static struct type *
21398 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21399 {
21400 struct attribute *type_attr;
21401
21402 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21403 if (!type_attr)
21404 return NULL;
21405
21406 return lookup_die_type (die, type_attr, cu);
21407 }
21408
21409 /* If DIE has a descriptive_type attribute, then set the TYPE's
21410 descriptive type accordingly. */
21411
21412 static void
21413 set_descriptive_type (struct type *type, struct die_info *die,
21414 struct dwarf2_cu *cu)
21415 {
21416 struct type *descriptive_type = die_descriptive_type (die, cu);
21417
21418 if (descriptive_type)
21419 {
21420 ALLOCATE_GNAT_AUX_TYPE (type);
21421 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21422 }
21423 }
21424
21425 /* Return the containing type of the die in question using its
21426 DW_AT_containing_type attribute. */
21427
21428 static struct type *
21429 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21430 {
21431 struct attribute *type_attr;
21432 struct objfile *objfile = cu->per_objfile->objfile;
21433
21434 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21435 if (!type_attr)
21436 error (_("Dwarf Error: Problem turning containing type into gdb type "
21437 "[in module %s]"), objfile_name (objfile));
21438
21439 return lookup_die_type (die, type_attr, cu);
21440 }
21441
21442 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21443
21444 static struct type *
21445 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21446 {
21447 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21448 struct objfile *objfile = dwarf2_per_objfile->objfile;
21449 char *saved;
21450
21451 std::string message
21452 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21453 objfile_name (objfile),
21454 sect_offset_str (cu->header.sect_off),
21455 sect_offset_str (die->sect_off));
21456 saved = obstack_strdup (&objfile->objfile_obstack, message);
21457
21458 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21459 }
21460
21461 /* Look up the type of DIE in CU using its type attribute ATTR.
21462 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21463 DW_AT_containing_type.
21464 If there is no type substitute an error marker. */
21465
21466 static struct type *
21467 lookup_die_type (struct die_info *die, const struct attribute *attr,
21468 struct dwarf2_cu *cu)
21469 {
21470 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21471 struct objfile *objfile = dwarf2_per_objfile->objfile;
21472 struct type *this_type;
21473
21474 gdb_assert (attr->name == DW_AT_type
21475 || attr->name == DW_AT_GNAT_descriptive_type
21476 || attr->name == DW_AT_containing_type);
21477
21478 /* First see if we have it cached. */
21479
21480 if (attr->form == DW_FORM_GNU_ref_alt)
21481 {
21482 struct dwarf2_per_cu_data *per_cu;
21483 sect_offset sect_off = attr->get_ref_die_offset ();
21484
21485 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21486 dwarf2_per_objfile);
21487 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
21488 }
21489 else if (attr->form_is_ref ())
21490 {
21491 sect_offset sect_off = attr->get_ref_die_offset ();
21492
21493 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21494 dwarf2_per_objfile);
21495 }
21496 else if (attr->form == DW_FORM_ref_sig8)
21497 {
21498 ULONGEST signature = DW_SIGNATURE (attr);
21499
21500 return get_signatured_type (die, signature, cu);
21501 }
21502 else
21503 {
21504 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21505 " at %s [in module %s]"),
21506 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21507 objfile_name (objfile));
21508 return build_error_marker_type (cu, die);
21509 }
21510
21511 /* If not cached we need to read it in. */
21512
21513 if (this_type == NULL)
21514 {
21515 struct die_info *type_die = NULL;
21516 struct dwarf2_cu *type_cu = cu;
21517
21518 if (attr->form_is_ref ())
21519 type_die = follow_die_ref (die, attr, &type_cu);
21520 if (type_die == NULL)
21521 return build_error_marker_type (cu, die);
21522 /* If we find the type now, it's probably because the type came
21523 from an inter-CU reference and the type's CU got expanded before
21524 ours. */
21525 this_type = read_type_die (type_die, type_cu);
21526 }
21527
21528 /* If we still don't have a type use an error marker. */
21529
21530 if (this_type == NULL)
21531 return build_error_marker_type (cu, die);
21532
21533 return this_type;
21534 }
21535
21536 /* Return the type in DIE, CU.
21537 Returns NULL for invalid types.
21538
21539 This first does a lookup in die_type_hash,
21540 and only reads the die in if necessary.
21541
21542 NOTE: This can be called when reading in partial or full symbols. */
21543
21544 static struct type *
21545 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21546 {
21547 struct type *this_type;
21548
21549 this_type = get_die_type (die, cu);
21550 if (this_type)
21551 return this_type;
21552
21553 return read_type_die_1 (die, cu);
21554 }
21555
21556 /* Read the type in DIE, CU.
21557 Returns NULL for invalid types. */
21558
21559 static struct type *
21560 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21561 {
21562 struct type *this_type = NULL;
21563
21564 switch (die->tag)
21565 {
21566 case DW_TAG_class_type:
21567 case DW_TAG_interface_type:
21568 case DW_TAG_structure_type:
21569 case DW_TAG_union_type:
21570 this_type = read_structure_type (die, cu);
21571 break;
21572 case DW_TAG_enumeration_type:
21573 this_type = read_enumeration_type (die, cu);
21574 break;
21575 case DW_TAG_subprogram:
21576 case DW_TAG_subroutine_type:
21577 case DW_TAG_inlined_subroutine:
21578 this_type = read_subroutine_type (die, cu);
21579 break;
21580 case DW_TAG_array_type:
21581 this_type = read_array_type (die, cu);
21582 break;
21583 case DW_TAG_set_type:
21584 this_type = read_set_type (die, cu);
21585 break;
21586 case DW_TAG_pointer_type:
21587 this_type = read_tag_pointer_type (die, cu);
21588 break;
21589 case DW_TAG_ptr_to_member_type:
21590 this_type = read_tag_ptr_to_member_type (die, cu);
21591 break;
21592 case DW_TAG_reference_type:
21593 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21594 break;
21595 case DW_TAG_rvalue_reference_type:
21596 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21597 break;
21598 case DW_TAG_const_type:
21599 this_type = read_tag_const_type (die, cu);
21600 break;
21601 case DW_TAG_volatile_type:
21602 this_type = read_tag_volatile_type (die, cu);
21603 break;
21604 case DW_TAG_restrict_type:
21605 this_type = read_tag_restrict_type (die, cu);
21606 break;
21607 case DW_TAG_string_type:
21608 this_type = read_tag_string_type (die, cu);
21609 break;
21610 case DW_TAG_typedef:
21611 this_type = read_typedef (die, cu);
21612 break;
21613 case DW_TAG_subrange_type:
21614 this_type = read_subrange_type (die, cu);
21615 break;
21616 case DW_TAG_base_type:
21617 this_type = read_base_type (die, cu);
21618 break;
21619 case DW_TAG_unspecified_type:
21620 this_type = read_unspecified_type (die, cu);
21621 break;
21622 case DW_TAG_namespace:
21623 this_type = read_namespace_type (die, cu);
21624 break;
21625 case DW_TAG_module:
21626 this_type = read_module_type (die, cu);
21627 break;
21628 case DW_TAG_atomic_type:
21629 this_type = read_tag_atomic_type (die, cu);
21630 break;
21631 default:
21632 complaint (_("unexpected tag in read_type_die: '%s'"),
21633 dwarf_tag_name (die->tag));
21634 break;
21635 }
21636
21637 return this_type;
21638 }
21639
21640 /* See if we can figure out if the class lives in a namespace. We do
21641 this by looking for a member function; its demangled name will
21642 contain namespace info, if there is any.
21643 Return the computed name or NULL.
21644 Space for the result is allocated on the objfile's obstack.
21645 This is the full-die version of guess_partial_die_structure_name.
21646 In this case we know DIE has no useful parent. */
21647
21648 static const char *
21649 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21650 {
21651 struct die_info *spec_die;
21652 struct dwarf2_cu *spec_cu;
21653 struct die_info *child;
21654 struct objfile *objfile = cu->per_objfile->objfile;
21655
21656 spec_cu = cu;
21657 spec_die = die_specification (die, &spec_cu);
21658 if (spec_die != NULL)
21659 {
21660 die = spec_die;
21661 cu = spec_cu;
21662 }
21663
21664 for (child = die->child;
21665 child != NULL;
21666 child = child->sibling)
21667 {
21668 if (child->tag == DW_TAG_subprogram)
21669 {
21670 const char *linkage_name = dw2_linkage_name (child, cu);
21671
21672 if (linkage_name != NULL)
21673 {
21674 gdb::unique_xmalloc_ptr<char> actual_name
21675 (language_class_name_from_physname (cu->language_defn,
21676 linkage_name));
21677 const char *name = NULL;
21678
21679 if (actual_name != NULL)
21680 {
21681 const char *die_name = dwarf2_name (die, cu);
21682
21683 if (die_name != NULL
21684 && strcmp (die_name, actual_name.get ()) != 0)
21685 {
21686 /* Strip off the class name from the full name.
21687 We want the prefix. */
21688 int die_name_len = strlen (die_name);
21689 int actual_name_len = strlen (actual_name.get ());
21690 const char *ptr = actual_name.get ();
21691
21692 /* Test for '::' as a sanity check. */
21693 if (actual_name_len > die_name_len + 2
21694 && ptr[actual_name_len - die_name_len - 1] == ':')
21695 name = obstack_strndup (
21696 &objfile->per_bfd->storage_obstack,
21697 ptr, actual_name_len - die_name_len - 2);
21698 }
21699 }
21700 return name;
21701 }
21702 }
21703 }
21704
21705 return NULL;
21706 }
21707
21708 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21709 prefix part in such case. See
21710 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21711
21712 static const char *
21713 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21714 {
21715 struct attribute *attr;
21716 const char *base;
21717
21718 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21719 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21720 return NULL;
21721
21722 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21723 return NULL;
21724
21725 attr = dw2_linkage_name_attr (die, cu);
21726 if (attr == NULL || DW_STRING (attr) == NULL)
21727 return NULL;
21728
21729 /* dwarf2_name had to be already called. */
21730 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21731
21732 /* Strip the base name, keep any leading namespaces/classes. */
21733 base = strrchr (DW_STRING (attr), ':');
21734 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21735 return "";
21736
21737 struct objfile *objfile = cu->per_objfile->objfile;
21738 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21739 DW_STRING (attr),
21740 &base[-1] - DW_STRING (attr));
21741 }
21742
21743 /* Return the name of the namespace/class that DIE is defined within,
21744 or "" if we can't tell. The caller should not xfree the result.
21745
21746 For example, if we're within the method foo() in the following
21747 code:
21748
21749 namespace N {
21750 class C {
21751 void foo () {
21752 }
21753 };
21754 }
21755
21756 then determine_prefix on foo's die will return "N::C". */
21757
21758 static const char *
21759 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21760 {
21761 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21762 struct die_info *parent, *spec_die;
21763 struct dwarf2_cu *spec_cu;
21764 struct type *parent_type;
21765 const char *retval;
21766
21767 if (cu->language != language_cplus
21768 && cu->language != language_fortran && cu->language != language_d
21769 && cu->language != language_rust)
21770 return "";
21771
21772 retval = anonymous_struct_prefix (die, cu);
21773 if (retval)
21774 return retval;
21775
21776 /* We have to be careful in the presence of DW_AT_specification.
21777 For example, with GCC 3.4, given the code
21778
21779 namespace N {
21780 void foo() {
21781 // Definition of N::foo.
21782 }
21783 }
21784
21785 then we'll have a tree of DIEs like this:
21786
21787 1: DW_TAG_compile_unit
21788 2: DW_TAG_namespace // N
21789 3: DW_TAG_subprogram // declaration of N::foo
21790 4: DW_TAG_subprogram // definition of N::foo
21791 DW_AT_specification // refers to die #3
21792
21793 Thus, when processing die #4, we have to pretend that we're in
21794 the context of its DW_AT_specification, namely the contex of die
21795 #3. */
21796 spec_cu = cu;
21797 spec_die = die_specification (die, &spec_cu);
21798 if (spec_die == NULL)
21799 parent = die->parent;
21800 else
21801 {
21802 parent = spec_die->parent;
21803 cu = spec_cu;
21804 }
21805
21806 if (parent == NULL)
21807 return "";
21808 else if (parent->building_fullname)
21809 {
21810 const char *name;
21811 const char *parent_name;
21812
21813 /* It has been seen on RealView 2.2 built binaries,
21814 DW_TAG_template_type_param types actually _defined_ as
21815 children of the parent class:
21816
21817 enum E {};
21818 template class <class Enum> Class{};
21819 Class<enum E> class_e;
21820
21821 1: DW_TAG_class_type (Class)
21822 2: DW_TAG_enumeration_type (E)
21823 3: DW_TAG_enumerator (enum1:0)
21824 3: DW_TAG_enumerator (enum2:1)
21825 ...
21826 2: DW_TAG_template_type_param
21827 DW_AT_type DW_FORM_ref_udata (E)
21828
21829 Besides being broken debug info, it can put GDB into an
21830 infinite loop. Consider:
21831
21832 When we're building the full name for Class<E>, we'll start
21833 at Class, and go look over its template type parameters,
21834 finding E. We'll then try to build the full name of E, and
21835 reach here. We're now trying to build the full name of E,
21836 and look over the parent DIE for containing scope. In the
21837 broken case, if we followed the parent DIE of E, we'd again
21838 find Class, and once again go look at its template type
21839 arguments, etc., etc. Simply don't consider such parent die
21840 as source-level parent of this die (it can't be, the language
21841 doesn't allow it), and break the loop here. */
21842 name = dwarf2_name (die, cu);
21843 parent_name = dwarf2_name (parent, cu);
21844 complaint (_("template param type '%s' defined within parent '%s'"),
21845 name ? name : "<unknown>",
21846 parent_name ? parent_name : "<unknown>");
21847 return "";
21848 }
21849 else
21850 switch (parent->tag)
21851 {
21852 case DW_TAG_namespace:
21853 parent_type = read_type_die (parent, cu);
21854 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21855 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21856 Work around this problem here. */
21857 if (cu->language == language_cplus
21858 && strcmp (parent_type->name (), "::") == 0)
21859 return "";
21860 /* We give a name to even anonymous namespaces. */
21861 return parent_type->name ();
21862 case DW_TAG_class_type:
21863 case DW_TAG_interface_type:
21864 case DW_TAG_structure_type:
21865 case DW_TAG_union_type:
21866 case DW_TAG_module:
21867 parent_type = read_type_die (parent, cu);
21868 if (parent_type->name () != NULL)
21869 return parent_type->name ();
21870 else
21871 /* An anonymous structure is only allowed non-static data
21872 members; no typedefs, no member functions, et cetera.
21873 So it does not need a prefix. */
21874 return "";
21875 case DW_TAG_compile_unit:
21876 case DW_TAG_partial_unit:
21877 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21878 if (cu->language == language_cplus
21879 && !dwarf2_per_objfile->per_bfd->types.empty ()
21880 && die->child != NULL
21881 && (die->tag == DW_TAG_class_type
21882 || die->tag == DW_TAG_structure_type
21883 || die->tag == DW_TAG_union_type))
21884 {
21885 const char *name = guess_full_die_structure_name (die, cu);
21886 if (name != NULL)
21887 return name;
21888 }
21889 return "";
21890 case DW_TAG_subprogram:
21891 /* Nested subroutines in Fortran get a prefix with the name
21892 of the parent's subroutine. */
21893 if (cu->language == language_fortran)
21894 {
21895 if ((die->tag == DW_TAG_subprogram)
21896 && (dwarf2_name (parent, cu) != NULL))
21897 return dwarf2_name (parent, cu);
21898 }
21899 return determine_prefix (parent, cu);
21900 case DW_TAG_enumeration_type:
21901 parent_type = read_type_die (parent, cu);
21902 if (TYPE_DECLARED_CLASS (parent_type))
21903 {
21904 if (parent_type->name () != NULL)
21905 return parent_type->name ();
21906 return "";
21907 }
21908 /* Fall through. */
21909 default:
21910 return determine_prefix (parent, cu);
21911 }
21912 }
21913
21914 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21915 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21916 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21917 an obconcat, otherwise allocate storage for the result. The CU argument is
21918 used to determine the language and hence, the appropriate separator. */
21919
21920 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21921
21922 static char *
21923 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21924 int physname, struct dwarf2_cu *cu)
21925 {
21926 const char *lead = "";
21927 const char *sep;
21928
21929 if (suffix == NULL || suffix[0] == '\0'
21930 || prefix == NULL || prefix[0] == '\0')
21931 sep = "";
21932 else if (cu->language == language_d)
21933 {
21934 /* For D, the 'main' function could be defined in any module, but it
21935 should never be prefixed. */
21936 if (strcmp (suffix, "D main") == 0)
21937 {
21938 prefix = "";
21939 sep = "";
21940 }
21941 else
21942 sep = ".";
21943 }
21944 else if (cu->language == language_fortran && physname)
21945 {
21946 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21947 DW_AT_MIPS_linkage_name is preferred and used instead. */
21948
21949 lead = "__";
21950 sep = "_MOD_";
21951 }
21952 else
21953 sep = "::";
21954
21955 if (prefix == NULL)
21956 prefix = "";
21957 if (suffix == NULL)
21958 suffix = "";
21959
21960 if (obs == NULL)
21961 {
21962 char *retval
21963 = ((char *)
21964 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21965
21966 strcpy (retval, lead);
21967 strcat (retval, prefix);
21968 strcat (retval, sep);
21969 strcat (retval, suffix);
21970 return retval;
21971 }
21972 else
21973 {
21974 /* We have an obstack. */
21975 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21976 }
21977 }
21978
21979 /* Get name of a die, return NULL if not found. */
21980
21981 static const char *
21982 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21983 struct objfile *objfile)
21984 {
21985 if (name && cu->language == language_cplus)
21986 {
21987 gdb::unique_xmalloc_ptr<char> canon_name
21988 = cp_canonicalize_string (name);
21989
21990 if (canon_name != nullptr)
21991 name = objfile->intern (canon_name.get ());
21992 }
21993
21994 return name;
21995 }
21996
21997 /* Get name of a die, return NULL if not found.
21998 Anonymous namespaces are converted to their magic string. */
21999
22000 static const char *
22001 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22002 {
22003 struct attribute *attr;
22004 struct objfile *objfile = cu->per_objfile->objfile;
22005
22006 attr = dwarf2_attr (die, DW_AT_name, cu);
22007 if ((!attr || !DW_STRING (attr))
22008 && die->tag != DW_TAG_namespace
22009 && die->tag != DW_TAG_class_type
22010 && die->tag != DW_TAG_interface_type
22011 && die->tag != DW_TAG_structure_type
22012 && die->tag != DW_TAG_union_type)
22013 return NULL;
22014
22015 switch (die->tag)
22016 {
22017 case DW_TAG_compile_unit:
22018 case DW_TAG_partial_unit:
22019 /* Compilation units have a DW_AT_name that is a filename, not
22020 a source language identifier. */
22021 case DW_TAG_enumeration_type:
22022 case DW_TAG_enumerator:
22023 /* These tags always have simple identifiers already; no need
22024 to canonicalize them. */
22025 return DW_STRING (attr);
22026
22027 case DW_TAG_namespace:
22028 if (attr != NULL && DW_STRING (attr) != NULL)
22029 return DW_STRING (attr);
22030 return CP_ANONYMOUS_NAMESPACE_STR;
22031
22032 case DW_TAG_class_type:
22033 case DW_TAG_interface_type:
22034 case DW_TAG_structure_type:
22035 case DW_TAG_union_type:
22036 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22037 structures or unions. These were of the form "._%d" in GCC 4.1,
22038 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22039 and GCC 4.4. We work around this problem by ignoring these. */
22040 if (attr && DW_STRING (attr)
22041 && (startswith (DW_STRING (attr), "._")
22042 || startswith (DW_STRING (attr), "<anonymous")))
22043 return NULL;
22044
22045 /* GCC might emit a nameless typedef that has a linkage name. See
22046 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22047 if (!attr || DW_STRING (attr) == NULL)
22048 {
22049 attr = dw2_linkage_name_attr (die, cu);
22050 if (attr == NULL || DW_STRING (attr) == NULL)
22051 return NULL;
22052
22053 /* Avoid demangling DW_STRING (attr) the second time on a second
22054 call for the same DIE. */
22055 if (!DW_STRING_IS_CANONICAL (attr))
22056 {
22057 gdb::unique_xmalloc_ptr<char> demangled
22058 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22059 if (demangled == nullptr)
22060 return nullptr;
22061
22062 DW_STRING (attr) = objfile->intern (demangled.get ());
22063 DW_STRING_IS_CANONICAL (attr) = 1;
22064 }
22065
22066 /* Strip any leading namespaces/classes, keep only the base name.
22067 DW_AT_name for named DIEs does not contain the prefixes. */
22068 const char *base = strrchr (DW_STRING (attr), ':');
22069 if (base && base > DW_STRING (attr) && base[-1] == ':')
22070 return &base[1];
22071 else
22072 return DW_STRING (attr);
22073 }
22074 break;
22075
22076 default:
22077 break;
22078 }
22079
22080 if (!DW_STRING_IS_CANONICAL (attr))
22081 {
22082 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22083 objfile);
22084 DW_STRING_IS_CANONICAL (attr) = 1;
22085 }
22086 return DW_STRING (attr);
22087 }
22088
22089 /* Return the die that this die in an extension of, or NULL if there
22090 is none. *EXT_CU is the CU containing DIE on input, and the CU
22091 containing the return value on output. */
22092
22093 static struct die_info *
22094 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22095 {
22096 struct attribute *attr;
22097
22098 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22099 if (attr == NULL)
22100 return NULL;
22101
22102 return follow_die_ref (die, attr, ext_cu);
22103 }
22104
22105 static void
22106 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22107 {
22108 unsigned int i;
22109
22110 print_spaces (indent, f);
22111 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22112 dwarf_tag_name (die->tag), die->abbrev,
22113 sect_offset_str (die->sect_off));
22114
22115 if (die->parent != NULL)
22116 {
22117 print_spaces (indent, f);
22118 fprintf_unfiltered (f, " parent at offset: %s\n",
22119 sect_offset_str (die->parent->sect_off));
22120 }
22121
22122 print_spaces (indent, f);
22123 fprintf_unfiltered (f, " has children: %s\n",
22124 dwarf_bool_name (die->child != NULL));
22125
22126 print_spaces (indent, f);
22127 fprintf_unfiltered (f, " attributes:\n");
22128
22129 for (i = 0; i < die->num_attrs; ++i)
22130 {
22131 print_spaces (indent, f);
22132 fprintf_unfiltered (f, " %s (%s) ",
22133 dwarf_attr_name (die->attrs[i].name),
22134 dwarf_form_name (die->attrs[i].form));
22135
22136 switch (die->attrs[i].form)
22137 {
22138 case DW_FORM_addr:
22139 case DW_FORM_addrx:
22140 case DW_FORM_GNU_addr_index:
22141 fprintf_unfiltered (f, "address: ");
22142 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22143 break;
22144 case DW_FORM_block2:
22145 case DW_FORM_block4:
22146 case DW_FORM_block:
22147 case DW_FORM_block1:
22148 fprintf_unfiltered (f, "block: size %s",
22149 pulongest (DW_BLOCK (&die->attrs[i])->size));
22150 break;
22151 case DW_FORM_exprloc:
22152 fprintf_unfiltered (f, "expression: size %s",
22153 pulongest (DW_BLOCK (&die->attrs[i])->size));
22154 break;
22155 case DW_FORM_data16:
22156 fprintf_unfiltered (f, "constant of 16 bytes");
22157 break;
22158 case DW_FORM_ref_addr:
22159 fprintf_unfiltered (f, "ref address: ");
22160 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22161 break;
22162 case DW_FORM_GNU_ref_alt:
22163 fprintf_unfiltered (f, "alt ref address: ");
22164 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22165 break;
22166 case DW_FORM_ref1:
22167 case DW_FORM_ref2:
22168 case DW_FORM_ref4:
22169 case DW_FORM_ref8:
22170 case DW_FORM_ref_udata:
22171 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22172 (long) (DW_UNSND (&die->attrs[i])));
22173 break;
22174 case DW_FORM_data1:
22175 case DW_FORM_data2:
22176 case DW_FORM_data4:
22177 case DW_FORM_data8:
22178 case DW_FORM_udata:
22179 case DW_FORM_sdata:
22180 fprintf_unfiltered (f, "constant: %s",
22181 pulongest (DW_UNSND (&die->attrs[i])));
22182 break;
22183 case DW_FORM_sec_offset:
22184 fprintf_unfiltered (f, "section offset: %s",
22185 pulongest (DW_UNSND (&die->attrs[i])));
22186 break;
22187 case DW_FORM_ref_sig8:
22188 fprintf_unfiltered (f, "signature: %s",
22189 hex_string (DW_SIGNATURE (&die->attrs[i])));
22190 break;
22191 case DW_FORM_string:
22192 case DW_FORM_strp:
22193 case DW_FORM_line_strp:
22194 case DW_FORM_strx:
22195 case DW_FORM_GNU_str_index:
22196 case DW_FORM_GNU_strp_alt:
22197 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22198 DW_STRING (&die->attrs[i])
22199 ? DW_STRING (&die->attrs[i]) : "",
22200 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22201 break;
22202 case DW_FORM_flag:
22203 if (DW_UNSND (&die->attrs[i]))
22204 fprintf_unfiltered (f, "flag: TRUE");
22205 else
22206 fprintf_unfiltered (f, "flag: FALSE");
22207 break;
22208 case DW_FORM_flag_present:
22209 fprintf_unfiltered (f, "flag: TRUE");
22210 break;
22211 case DW_FORM_indirect:
22212 /* The reader will have reduced the indirect form to
22213 the "base form" so this form should not occur. */
22214 fprintf_unfiltered (f,
22215 "unexpected attribute form: DW_FORM_indirect");
22216 break;
22217 case DW_FORM_implicit_const:
22218 fprintf_unfiltered (f, "constant: %s",
22219 plongest (DW_SND (&die->attrs[i])));
22220 break;
22221 default:
22222 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22223 die->attrs[i].form);
22224 break;
22225 }
22226 fprintf_unfiltered (f, "\n");
22227 }
22228 }
22229
22230 static void
22231 dump_die_for_error (struct die_info *die)
22232 {
22233 dump_die_shallow (gdb_stderr, 0, die);
22234 }
22235
22236 static void
22237 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22238 {
22239 int indent = level * 4;
22240
22241 gdb_assert (die != NULL);
22242
22243 if (level >= max_level)
22244 return;
22245
22246 dump_die_shallow (f, indent, die);
22247
22248 if (die->child != NULL)
22249 {
22250 print_spaces (indent, f);
22251 fprintf_unfiltered (f, " Children:");
22252 if (level + 1 < max_level)
22253 {
22254 fprintf_unfiltered (f, "\n");
22255 dump_die_1 (f, level + 1, max_level, die->child);
22256 }
22257 else
22258 {
22259 fprintf_unfiltered (f,
22260 " [not printed, max nesting level reached]\n");
22261 }
22262 }
22263
22264 if (die->sibling != NULL && level > 0)
22265 {
22266 dump_die_1 (f, level, max_level, die->sibling);
22267 }
22268 }
22269
22270 /* This is called from the pdie macro in gdbinit.in.
22271 It's not static so gcc will keep a copy callable from gdb. */
22272
22273 void
22274 dump_die (struct die_info *die, int max_level)
22275 {
22276 dump_die_1 (gdb_stdlog, 0, max_level, die);
22277 }
22278
22279 static void
22280 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22281 {
22282 void **slot;
22283
22284 slot = htab_find_slot_with_hash (cu->die_hash, die,
22285 to_underlying (die->sect_off),
22286 INSERT);
22287
22288 *slot = die;
22289 }
22290
22291 /* Follow reference or signature attribute ATTR of SRC_DIE.
22292 On entry *REF_CU is the CU of SRC_DIE.
22293 On exit *REF_CU is the CU of the result. */
22294
22295 static struct die_info *
22296 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22297 struct dwarf2_cu **ref_cu)
22298 {
22299 struct die_info *die;
22300
22301 if (attr->form_is_ref ())
22302 die = follow_die_ref (src_die, attr, ref_cu);
22303 else if (attr->form == DW_FORM_ref_sig8)
22304 die = follow_die_sig (src_die, attr, ref_cu);
22305 else
22306 {
22307 dump_die_for_error (src_die);
22308 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22309 objfile_name ((*ref_cu)->per_objfile->objfile));
22310 }
22311
22312 return die;
22313 }
22314
22315 /* Follow reference OFFSET.
22316 On entry *REF_CU is the CU of the source die referencing OFFSET.
22317 On exit *REF_CU is the CU of the result.
22318 Returns NULL if OFFSET is invalid. */
22319
22320 static struct die_info *
22321 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22322 struct dwarf2_cu **ref_cu)
22323 {
22324 struct die_info temp_die;
22325 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22326 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22327
22328 gdb_assert (cu->per_cu != NULL);
22329
22330 target_cu = cu;
22331
22332 if (cu->per_cu->is_debug_types)
22333 {
22334 /* .debug_types CUs cannot reference anything outside their CU.
22335 If they need to, they have to reference a signatured type via
22336 DW_FORM_ref_sig8. */
22337 if (!cu->header.offset_in_cu_p (sect_off))
22338 return NULL;
22339 }
22340 else if (offset_in_dwz != cu->per_cu->is_dwz
22341 || !cu->header.offset_in_cu_p (sect_off))
22342 {
22343 struct dwarf2_per_cu_data *per_cu;
22344
22345 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22346 dwarf2_per_objfile);
22347
22348 /* If necessary, add it to the queue and load its DIEs. */
22349 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
22350 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22351
22352 target_cu = dwarf2_per_objfile->get_cu (per_cu);
22353 }
22354 else if (cu->dies == NULL)
22355 {
22356 /* We're loading full DIEs during partial symbol reading. */
22357 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22358 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22359 language_minimal);
22360 }
22361
22362 *ref_cu = target_cu;
22363 temp_die.sect_off = sect_off;
22364
22365 if (target_cu != cu)
22366 target_cu->ancestor = cu;
22367
22368 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22369 &temp_die,
22370 to_underlying (sect_off));
22371 }
22372
22373 /* Follow reference attribute ATTR of SRC_DIE.
22374 On entry *REF_CU is the CU of SRC_DIE.
22375 On exit *REF_CU is the CU of the result. */
22376
22377 static struct die_info *
22378 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22379 struct dwarf2_cu **ref_cu)
22380 {
22381 sect_offset sect_off = attr->get_ref_die_offset ();
22382 struct dwarf2_cu *cu = *ref_cu;
22383 struct die_info *die;
22384
22385 die = follow_die_offset (sect_off,
22386 (attr->form == DW_FORM_GNU_ref_alt
22387 || cu->per_cu->is_dwz),
22388 ref_cu);
22389 if (!die)
22390 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22391 "at %s [in module %s]"),
22392 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22393 objfile_name (cu->per_objfile->objfile));
22394
22395 return die;
22396 }
22397
22398 /* See read.h. */
22399
22400 struct dwarf2_locexpr_baton
22401 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22402 dwarf2_per_cu_data *per_cu,
22403 dwarf2_per_objfile *dwarf2_per_objfile,
22404 CORE_ADDR (*get_frame_pc) (void *baton),
22405 void *baton, bool resolve_abstract_p)
22406 {
22407 struct die_info *die;
22408 struct attribute *attr;
22409 struct dwarf2_locexpr_baton retval;
22410 struct objfile *objfile = dwarf2_per_objfile->objfile;
22411
22412 dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
22413 if (cu == nullptr)
22414 cu = load_cu (per_cu, dwarf2_per_objfile, false);
22415
22416 if (cu == nullptr)
22417 {
22418 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22419 Instead just throw an error, not much else we can do. */
22420 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22421 sect_offset_str (sect_off), objfile_name (objfile));
22422 }
22423
22424 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22425 if (!die)
22426 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22427 sect_offset_str (sect_off), objfile_name (objfile));
22428
22429 attr = dwarf2_attr (die, DW_AT_location, cu);
22430 if (!attr && resolve_abstract_p
22431 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22432 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22433 {
22434 CORE_ADDR pc = (*get_frame_pc) (baton);
22435 CORE_ADDR baseaddr = objfile->text_section_offset ();
22436 struct gdbarch *gdbarch = objfile->arch ();
22437
22438 for (const auto &cand_off
22439 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22440 {
22441 struct dwarf2_cu *cand_cu = cu;
22442 struct die_info *cand
22443 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22444 if (!cand
22445 || !cand->parent
22446 || cand->parent->tag != DW_TAG_subprogram)
22447 continue;
22448
22449 CORE_ADDR pc_low, pc_high;
22450 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22451 if (pc_low == ((CORE_ADDR) -1))
22452 continue;
22453 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22454 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22455 if (!(pc_low <= pc && pc < pc_high))
22456 continue;
22457
22458 die = cand;
22459 attr = dwarf2_attr (die, DW_AT_location, cu);
22460 break;
22461 }
22462 }
22463
22464 if (!attr)
22465 {
22466 /* DWARF: "If there is no such attribute, then there is no effect.".
22467 DATA is ignored if SIZE is 0. */
22468
22469 retval.data = NULL;
22470 retval.size = 0;
22471 }
22472 else if (attr->form_is_section_offset ())
22473 {
22474 struct dwarf2_loclist_baton loclist_baton;
22475 CORE_ADDR pc = (*get_frame_pc) (baton);
22476 size_t size;
22477
22478 fill_in_loclist_baton (cu, &loclist_baton, attr);
22479
22480 retval.data = dwarf2_find_location_expression (&loclist_baton,
22481 &size, pc);
22482 retval.size = size;
22483 }
22484 else
22485 {
22486 if (!attr->form_is_block ())
22487 error (_("Dwarf Error: DIE at %s referenced in module %s "
22488 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22489 sect_offset_str (sect_off), objfile_name (objfile));
22490
22491 retval.data = DW_BLOCK (attr)->data;
22492 retval.size = DW_BLOCK (attr)->size;
22493 }
22494 retval.per_objfile = dwarf2_per_objfile;
22495 retval.per_cu = cu->per_cu;
22496
22497 dwarf2_per_objfile->age_comp_units ();
22498
22499 return retval;
22500 }
22501
22502 /* See read.h. */
22503
22504 struct dwarf2_locexpr_baton
22505 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22506 dwarf2_per_cu_data *per_cu,
22507 dwarf2_per_objfile *per_objfile,
22508 CORE_ADDR (*get_frame_pc) (void *baton),
22509 void *baton)
22510 {
22511 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22512
22513 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22514 get_frame_pc, baton);
22515 }
22516
22517 /* Write a constant of a given type as target-ordered bytes into
22518 OBSTACK. */
22519
22520 static const gdb_byte *
22521 write_constant_as_bytes (struct obstack *obstack,
22522 enum bfd_endian byte_order,
22523 struct type *type,
22524 ULONGEST value,
22525 LONGEST *len)
22526 {
22527 gdb_byte *result;
22528
22529 *len = TYPE_LENGTH (type);
22530 result = (gdb_byte *) obstack_alloc (obstack, *len);
22531 store_unsigned_integer (result, *len, byte_order, value);
22532
22533 return result;
22534 }
22535
22536 /* See read.h. */
22537
22538 const gdb_byte *
22539 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22540 dwarf2_per_cu_data *per_cu,
22541 dwarf2_per_objfile *per_objfile,
22542 obstack *obstack,
22543 LONGEST *len)
22544 {
22545 struct die_info *die;
22546 struct attribute *attr;
22547 const gdb_byte *result = NULL;
22548 struct type *type;
22549 LONGEST value;
22550 enum bfd_endian byte_order;
22551 struct objfile *objfile = per_objfile->objfile;
22552
22553 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22554 if (cu == nullptr)
22555 cu = load_cu (per_cu, per_objfile, false);
22556
22557 if (cu == nullptr)
22558 {
22559 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22560 Instead just throw an error, not much else we can do. */
22561 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22562 sect_offset_str (sect_off), objfile_name (objfile));
22563 }
22564
22565 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22566 if (!die)
22567 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22568 sect_offset_str (sect_off), objfile_name (objfile));
22569
22570 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22571 if (attr == NULL)
22572 return NULL;
22573
22574 byte_order = (bfd_big_endian (objfile->obfd)
22575 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22576
22577 switch (attr->form)
22578 {
22579 case DW_FORM_addr:
22580 case DW_FORM_addrx:
22581 case DW_FORM_GNU_addr_index:
22582 {
22583 gdb_byte *tem;
22584
22585 *len = cu->header.addr_size;
22586 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22587 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22588 result = tem;
22589 }
22590 break;
22591 case DW_FORM_string:
22592 case DW_FORM_strp:
22593 case DW_FORM_strx:
22594 case DW_FORM_GNU_str_index:
22595 case DW_FORM_GNU_strp_alt:
22596 /* DW_STRING is already allocated on the objfile obstack, point
22597 directly to it. */
22598 result = (const gdb_byte *) DW_STRING (attr);
22599 *len = strlen (DW_STRING (attr));
22600 break;
22601 case DW_FORM_block1:
22602 case DW_FORM_block2:
22603 case DW_FORM_block4:
22604 case DW_FORM_block:
22605 case DW_FORM_exprloc:
22606 case DW_FORM_data16:
22607 result = DW_BLOCK (attr)->data;
22608 *len = DW_BLOCK (attr)->size;
22609 break;
22610
22611 /* The DW_AT_const_value attributes are supposed to carry the
22612 symbol's value "represented as it would be on the target
22613 architecture." By the time we get here, it's already been
22614 converted to host endianness, so we just need to sign- or
22615 zero-extend it as appropriate. */
22616 case DW_FORM_data1:
22617 type = die_type (die, cu);
22618 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22619 if (result == NULL)
22620 result = write_constant_as_bytes (obstack, byte_order,
22621 type, value, len);
22622 break;
22623 case DW_FORM_data2:
22624 type = die_type (die, cu);
22625 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22626 if (result == NULL)
22627 result = write_constant_as_bytes (obstack, byte_order,
22628 type, value, len);
22629 break;
22630 case DW_FORM_data4:
22631 type = die_type (die, cu);
22632 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22633 if (result == NULL)
22634 result = write_constant_as_bytes (obstack, byte_order,
22635 type, value, len);
22636 break;
22637 case DW_FORM_data8:
22638 type = die_type (die, cu);
22639 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22640 if (result == NULL)
22641 result = write_constant_as_bytes (obstack, byte_order,
22642 type, value, len);
22643 break;
22644
22645 case DW_FORM_sdata:
22646 case DW_FORM_implicit_const:
22647 type = die_type (die, cu);
22648 result = write_constant_as_bytes (obstack, byte_order,
22649 type, DW_SND (attr), len);
22650 break;
22651
22652 case DW_FORM_udata:
22653 type = die_type (die, cu);
22654 result = write_constant_as_bytes (obstack, byte_order,
22655 type, DW_UNSND (attr), len);
22656 break;
22657
22658 default:
22659 complaint (_("unsupported const value attribute form: '%s'"),
22660 dwarf_form_name (attr->form));
22661 break;
22662 }
22663
22664 return result;
22665 }
22666
22667 /* See read.h. */
22668
22669 struct type *
22670 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22671 dwarf2_per_cu_data *per_cu,
22672 dwarf2_per_objfile *per_objfile)
22673 {
22674 struct die_info *die;
22675
22676 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22677 if (cu == nullptr)
22678 cu = load_cu (per_cu, per_objfile, false);
22679
22680 if (cu == nullptr)
22681 return nullptr;
22682
22683 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22684 if (!die)
22685 return NULL;
22686
22687 return die_type (die, cu);
22688 }
22689
22690 /* See read.h. */
22691
22692 struct type *
22693 dwarf2_get_die_type (cu_offset die_offset,
22694 dwarf2_per_cu_data *per_cu,
22695 dwarf2_per_objfile *per_objfile)
22696 {
22697 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22698 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22699 }
22700
22701 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22702 On entry *REF_CU is the CU of SRC_DIE.
22703 On exit *REF_CU is the CU of the result.
22704 Returns NULL if the referenced DIE isn't found. */
22705
22706 static struct die_info *
22707 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22708 struct dwarf2_cu **ref_cu)
22709 {
22710 struct die_info temp_die;
22711 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22712 struct die_info *die;
22713 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22714
22715
22716 /* While it might be nice to assert sig_type->type == NULL here,
22717 we can get here for DW_AT_imported_declaration where we need
22718 the DIE not the type. */
22719
22720 /* If necessary, add it to the queue and load its DIEs. */
22721
22722 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22723 language_minimal))
22724 read_signatured_type (sig_type, dwarf2_per_objfile);
22725
22726 sig_cu = dwarf2_per_objfile->get_cu (&sig_type->per_cu);
22727 gdb_assert (sig_cu != NULL);
22728 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22729 temp_die.sect_off = sig_type->type_offset_in_section;
22730 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22731 to_underlying (temp_die.sect_off));
22732 if (die)
22733 {
22734 /* For .gdb_index version 7 keep track of included TUs.
22735 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22736 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22737 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22738 {
22739 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22740 }
22741
22742 *ref_cu = sig_cu;
22743 if (sig_cu != cu)
22744 sig_cu->ancestor = cu;
22745
22746 return die;
22747 }
22748
22749 return NULL;
22750 }
22751
22752 /* Follow signatured type referenced by ATTR in SRC_DIE.
22753 On entry *REF_CU is the CU of SRC_DIE.
22754 On exit *REF_CU is the CU of the result.
22755 The result is the DIE of the type.
22756 If the referenced type cannot be found an error is thrown. */
22757
22758 static struct die_info *
22759 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22760 struct dwarf2_cu **ref_cu)
22761 {
22762 ULONGEST signature = DW_SIGNATURE (attr);
22763 struct signatured_type *sig_type;
22764 struct die_info *die;
22765
22766 gdb_assert (attr->form == DW_FORM_ref_sig8);
22767
22768 sig_type = lookup_signatured_type (*ref_cu, signature);
22769 /* sig_type will be NULL if the signatured type is missing from
22770 the debug info. */
22771 if (sig_type == NULL)
22772 {
22773 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22774 " from DIE at %s [in module %s]"),
22775 hex_string (signature), sect_offset_str (src_die->sect_off),
22776 objfile_name ((*ref_cu)->per_objfile->objfile));
22777 }
22778
22779 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22780 if (die == NULL)
22781 {
22782 dump_die_for_error (src_die);
22783 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22784 " from DIE at %s [in module %s]"),
22785 hex_string (signature), sect_offset_str (src_die->sect_off),
22786 objfile_name ((*ref_cu)->per_objfile->objfile));
22787 }
22788
22789 return die;
22790 }
22791
22792 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22793 reading in and processing the type unit if necessary. */
22794
22795 static struct type *
22796 get_signatured_type (struct die_info *die, ULONGEST signature,
22797 struct dwarf2_cu *cu)
22798 {
22799 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22800 struct signatured_type *sig_type;
22801 struct dwarf2_cu *type_cu;
22802 struct die_info *type_die;
22803 struct type *type;
22804
22805 sig_type = lookup_signatured_type (cu, signature);
22806 /* sig_type will be NULL if the signatured type is missing from
22807 the debug info. */
22808 if (sig_type == NULL)
22809 {
22810 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22811 " from DIE at %s [in module %s]"),
22812 hex_string (signature), sect_offset_str (die->sect_off),
22813 objfile_name (dwarf2_per_objfile->objfile));
22814 return build_error_marker_type (cu, die);
22815 }
22816
22817 /* If we already know the type we're done. */
22818 type = dwarf2_per_objfile->get_type_for_signatured_type (sig_type);
22819 if (type != nullptr)
22820 return type;
22821
22822 type_cu = cu;
22823 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22824 if (type_die != NULL)
22825 {
22826 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22827 is created. This is important, for example, because for c++ classes
22828 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22829 type = read_type_die (type_die, type_cu);
22830 if (type == NULL)
22831 {
22832 complaint (_("Dwarf Error: Cannot build signatured type %s"
22833 " referenced from DIE at %s [in module %s]"),
22834 hex_string (signature), sect_offset_str (die->sect_off),
22835 objfile_name (dwarf2_per_objfile->objfile));
22836 type = build_error_marker_type (cu, die);
22837 }
22838 }
22839 else
22840 {
22841 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22842 " from DIE at %s [in module %s]"),
22843 hex_string (signature), sect_offset_str (die->sect_off),
22844 objfile_name (dwarf2_per_objfile->objfile));
22845 type = build_error_marker_type (cu, die);
22846 }
22847
22848 dwarf2_per_objfile->set_type_for_signatured_type (sig_type, type);
22849
22850 return type;
22851 }
22852
22853 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22854 reading in and processing the type unit if necessary. */
22855
22856 static struct type *
22857 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22858 struct dwarf2_cu *cu) /* ARI: editCase function */
22859 {
22860 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22861 if (attr->form_is_ref ())
22862 {
22863 struct dwarf2_cu *type_cu = cu;
22864 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22865
22866 return read_type_die (type_die, type_cu);
22867 }
22868 else if (attr->form == DW_FORM_ref_sig8)
22869 {
22870 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22871 }
22872 else
22873 {
22874 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22875
22876 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22877 " at %s [in module %s]"),
22878 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22879 objfile_name (dwarf2_per_objfile->objfile));
22880 return build_error_marker_type (cu, die);
22881 }
22882 }
22883
22884 /* Load the DIEs associated with type unit PER_CU into memory. */
22885
22886 static void
22887 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22888 dwarf2_per_objfile *per_objfile)
22889 {
22890 struct signatured_type *sig_type;
22891
22892 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22893 gdb_assert (! per_cu->type_unit_group_p ());
22894
22895 /* We have the per_cu, but we need the signatured_type.
22896 Fortunately this is an easy translation. */
22897 gdb_assert (per_cu->is_debug_types);
22898 sig_type = (struct signatured_type *) per_cu;
22899
22900 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22901
22902 read_signatured_type (sig_type, per_objfile);
22903
22904 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22905 }
22906
22907 /* Read in a signatured type and build its CU and DIEs.
22908 If the type is a stub for the real type in a DWO file,
22909 read in the real type from the DWO file as well. */
22910
22911 static void
22912 read_signatured_type (signatured_type *sig_type,
22913 dwarf2_per_objfile *per_objfile)
22914 {
22915 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22916
22917 gdb_assert (per_cu->is_debug_types);
22918 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22919
22920 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
22921
22922 if (!reader.dummy_p)
22923 {
22924 struct dwarf2_cu *cu = reader.cu;
22925 const gdb_byte *info_ptr = reader.info_ptr;
22926
22927 gdb_assert (cu->die_hash == NULL);
22928 cu->die_hash =
22929 htab_create_alloc_ex (cu->header.length / 12,
22930 die_hash,
22931 die_eq,
22932 NULL,
22933 &cu->comp_unit_obstack,
22934 hashtab_obstack_allocate,
22935 dummy_obstack_deallocate);
22936
22937 if (reader.comp_unit_die->has_children)
22938 reader.comp_unit_die->child
22939 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22940 reader.comp_unit_die);
22941 cu->dies = reader.comp_unit_die;
22942 /* comp_unit_die is not stored in die_hash, no need. */
22943
22944 /* We try not to read any attributes in this function, because
22945 not all CUs needed for references have been loaded yet, and
22946 symbol table processing isn't initialized. But we have to
22947 set the CU language, or we won't be able to build types
22948 correctly. Similarly, if we do not read the producer, we can
22949 not apply producer-specific interpretation. */
22950 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22951
22952 reader.keep ();
22953 }
22954
22955 sig_type->per_cu.tu_read = 1;
22956 }
22957
22958 /* Decode simple location descriptions.
22959 Given a pointer to a dwarf block that defines a location, compute
22960 the location and return the value. If COMPUTED is non-null, it is
22961 set to true to indicate that decoding was successful, and false
22962 otherwise. If COMPUTED is null, then this function may emit a
22963 complaint. */
22964
22965 static CORE_ADDR
22966 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22967 {
22968 struct objfile *objfile = cu->per_objfile->objfile;
22969 size_t i;
22970 size_t size = blk->size;
22971 const gdb_byte *data = blk->data;
22972 CORE_ADDR stack[64];
22973 int stacki;
22974 unsigned int bytes_read, unsnd;
22975 gdb_byte op;
22976
22977 if (computed != nullptr)
22978 *computed = false;
22979
22980 i = 0;
22981 stacki = 0;
22982 stack[stacki] = 0;
22983 stack[++stacki] = 0;
22984
22985 while (i < size)
22986 {
22987 op = data[i++];
22988 switch (op)
22989 {
22990 case DW_OP_lit0:
22991 case DW_OP_lit1:
22992 case DW_OP_lit2:
22993 case DW_OP_lit3:
22994 case DW_OP_lit4:
22995 case DW_OP_lit5:
22996 case DW_OP_lit6:
22997 case DW_OP_lit7:
22998 case DW_OP_lit8:
22999 case DW_OP_lit9:
23000 case DW_OP_lit10:
23001 case DW_OP_lit11:
23002 case DW_OP_lit12:
23003 case DW_OP_lit13:
23004 case DW_OP_lit14:
23005 case DW_OP_lit15:
23006 case DW_OP_lit16:
23007 case DW_OP_lit17:
23008 case DW_OP_lit18:
23009 case DW_OP_lit19:
23010 case DW_OP_lit20:
23011 case DW_OP_lit21:
23012 case DW_OP_lit22:
23013 case DW_OP_lit23:
23014 case DW_OP_lit24:
23015 case DW_OP_lit25:
23016 case DW_OP_lit26:
23017 case DW_OP_lit27:
23018 case DW_OP_lit28:
23019 case DW_OP_lit29:
23020 case DW_OP_lit30:
23021 case DW_OP_lit31:
23022 stack[++stacki] = op - DW_OP_lit0;
23023 break;
23024
23025 case DW_OP_reg0:
23026 case DW_OP_reg1:
23027 case DW_OP_reg2:
23028 case DW_OP_reg3:
23029 case DW_OP_reg4:
23030 case DW_OP_reg5:
23031 case DW_OP_reg6:
23032 case DW_OP_reg7:
23033 case DW_OP_reg8:
23034 case DW_OP_reg9:
23035 case DW_OP_reg10:
23036 case DW_OP_reg11:
23037 case DW_OP_reg12:
23038 case DW_OP_reg13:
23039 case DW_OP_reg14:
23040 case DW_OP_reg15:
23041 case DW_OP_reg16:
23042 case DW_OP_reg17:
23043 case DW_OP_reg18:
23044 case DW_OP_reg19:
23045 case DW_OP_reg20:
23046 case DW_OP_reg21:
23047 case DW_OP_reg22:
23048 case DW_OP_reg23:
23049 case DW_OP_reg24:
23050 case DW_OP_reg25:
23051 case DW_OP_reg26:
23052 case DW_OP_reg27:
23053 case DW_OP_reg28:
23054 case DW_OP_reg29:
23055 case DW_OP_reg30:
23056 case DW_OP_reg31:
23057 stack[++stacki] = op - DW_OP_reg0;
23058 if (i < size)
23059 {
23060 if (computed == nullptr)
23061 dwarf2_complex_location_expr_complaint ();
23062 else
23063 return 0;
23064 }
23065 break;
23066
23067 case DW_OP_regx:
23068 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23069 i += bytes_read;
23070 stack[++stacki] = unsnd;
23071 if (i < size)
23072 {
23073 if (computed == nullptr)
23074 dwarf2_complex_location_expr_complaint ();
23075 else
23076 return 0;
23077 }
23078 break;
23079
23080 case DW_OP_addr:
23081 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23082 &bytes_read);
23083 i += bytes_read;
23084 break;
23085
23086 case DW_OP_const1u:
23087 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23088 i += 1;
23089 break;
23090
23091 case DW_OP_const1s:
23092 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23093 i += 1;
23094 break;
23095
23096 case DW_OP_const2u:
23097 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23098 i += 2;
23099 break;
23100
23101 case DW_OP_const2s:
23102 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23103 i += 2;
23104 break;
23105
23106 case DW_OP_const4u:
23107 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23108 i += 4;
23109 break;
23110
23111 case DW_OP_const4s:
23112 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23113 i += 4;
23114 break;
23115
23116 case DW_OP_const8u:
23117 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23118 i += 8;
23119 break;
23120
23121 case DW_OP_constu:
23122 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23123 &bytes_read);
23124 i += bytes_read;
23125 break;
23126
23127 case DW_OP_consts:
23128 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23129 i += bytes_read;
23130 break;
23131
23132 case DW_OP_dup:
23133 stack[stacki + 1] = stack[stacki];
23134 stacki++;
23135 break;
23136
23137 case DW_OP_plus:
23138 stack[stacki - 1] += stack[stacki];
23139 stacki--;
23140 break;
23141
23142 case DW_OP_plus_uconst:
23143 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23144 &bytes_read);
23145 i += bytes_read;
23146 break;
23147
23148 case DW_OP_minus:
23149 stack[stacki - 1] -= stack[stacki];
23150 stacki--;
23151 break;
23152
23153 case DW_OP_deref:
23154 /* If we're not the last op, then we definitely can't encode
23155 this using GDB's address_class enum. This is valid for partial
23156 global symbols, although the variable's address will be bogus
23157 in the psymtab. */
23158 if (i < size)
23159 {
23160 if (computed == nullptr)
23161 dwarf2_complex_location_expr_complaint ();
23162 else
23163 return 0;
23164 }
23165 break;
23166
23167 case DW_OP_GNU_push_tls_address:
23168 case DW_OP_form_tls_address:
23169 /* The top of the stack has the offset from the beginning
23170 of the thread control block at which the variable is located. */
23171 /* Nothing should follow this operator, so the top of stack would
23172 be returned. */
23173 /* This is valid for partial global symbols, but the variable's
23174 address will be bogus in the psymtab. Make it always at least
23175 non-zero to not look as a variable garbage collected by linker
23176 which have DW_OP_addr 0. */
23177 if (i < size)
23178 {
23179 if (computed == nullptr)
23180 dwarf2_complex_location_expr_complaint ();
23181 else
23182 return 0;
23183 }
23184 stack[stacki]++;
23185 break;
23186
23187 case DW_OP_GNU_uninit:
23188 if (computed != nullptr)
23189 return 0;
23190 break;
23191
23192 case DW_OP_addrx:
23193 case DW_OP_GNU_addr_index:
23194 case DW_OP_GNU_const_index:
23195 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23196 &bytes_read);
23197 i += bytes_read;
23198 break;
23199
23200 default:
23201 if (computed == nullptr)
23202 {
23203 const char *name = get_DW_OP_name (op);
23204
23205 if (name)
23206 complaint (_("unsupported stack op: '%s'"),
23207 name);
23208 else
23209 complaint (_("unsupported stack op: '%02x'"),
23210 op);
23211 }
23212
23213 return (stack[stacki]);
23214 }
23215
23216 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23217 outside of the allocated space. Also enforce minimum>0. */
23218 if (stacki >= ARRAY_SIZE (stack) - 1)
23219 {
23220 if (computed == nullptr)
23221 complaint (_("location description stack overflow"));
23222 return 0;
23223 }
23224
23225 if (stacki <= 0)
23226 {
23227 if (computed == nullptr)
23228 complaint (_("location description stack underflow"));
23229 return 0;
23230 }
23231 }
23232
23233 if (computed != nullptr)
23234 *computed = true;
23235 return (stack[stacki]);
23236 }
23237
23238 /* memory allocation interface */
23239
23240 static struct dwarf_block *
23241 dwarf_alloc_block (struct dwarf2_cu *cu)
23242 {
23243 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23244 }
23245
23246 static struct die_info *
23247 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23248 {
23249 struct die_info *die;
23250 size_t size = sizeof (struct die_info);
23251
23252 if (num_attrs > 1)
23253 size += (num_attrs - 1) * sizeof (struct attribute);
23254
23255 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23256 memset (die, 0, sizeof (struct die_info));
23257 return (die);
23258 }
23259
23260 \f
23261
23262 /* Macro support. */
23263
23264 /* An overload of dwarf_decode_macros that finds the correct section
23265 and ensures it is read in before calling the other overload. */
23266
23267 static void
23268 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23269 int section_is_gnu)
23270 {
23271 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23272 struct objfile *objfile = dwarf2_per_objfile->objfile;
23273 const struct line_header *lh = cu->line_header;
23274 unsigned int offset_size = cu->header.offset_size;
23275 struct dwarf2_section_info *section;
23276 const char *section_name;
23277
23278 if (cu->dwo_unit != nullptr)
23279 {
23280 if (section_is_gnu)
23281 {
23282 section = &cu->dwo_unit->dwo_file->sections.macro;
23283 section_name = ".debug_macro.dwo";
23284 }
23285 else
23286 {
23287 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23288 section_name = ".debug_macinfo.dwo";
23289 }
23290 }
23291 else
23292 {
23293 if (section_is_gnu)
23294 {
23295 section = &dwarf2_per_objfile->per_bfd->macro;
23296 section_name = ".debug_macro";
23297 }
23298 else
23299 {
23300 section = &dwarf2_per_objfile->per_bfd->macinfo;
23301 section_name = ".debug_macinfo";
23302 }
23303 }
23304
23305 section->read (objfile);
23306 if (section->buffer == nullptr)
23307 {
23308 complaint (_("missing %s section"), section_name);
23309 return;
23310 }
23311
23312 buildsym_compunit *builder = cu->get_builder ();
23313
23314 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23315 offset_size, offset, section_is_gnu);
23316 }
23317
23318 /* Return the .debug_loc section to use for CU.
23319 For DWO files use .debug_loc.dwo. */
23320
23321 static struct dwarf2_section_info *
23322 cu_debug_loc_section (struct dwarf2_cu *cu)
23323 {
23324 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23325
23326 if (cu->dwo_unit)
23327 {
23328 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23329
23330 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23331 }
23332 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23333 : &dwarf2_per_objfile->per_bfd->loc);
23334 }
23335
23336 /* A helper function that fills in a dwarf2_loclist_baton. */
23337
23338 static void
23339 fill_in_loclist_baton (struct dwarf2_cu *cu,
23340 struct dwarf2_loclist_baton *baton,
23341 const struct attribute *attr)
23342 {
23343 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23344 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23345
23346 section->read (dwarf2_per_objfile->objfile);
23347
23348 baton->per_objfile = dwarf2_per_objfile;
23349 baton->per_cu = cu->per_cu;
23350 gdb_assert (baton->per_cu);
23351 /* We don't know how long the location list is, but make sure we
23352 don't run off the edge of the section. */
23353 baton->size = section->size - DW_UNSND (attr);
23354 baton->data = section->buffer + DW_UNSND (attr);
23355 if (cu->base_address.has_value ())
23356 baton->base_address = *cu->base_address;
23357 else
23358 baton->base_address = 0;
23359 baton->from_dwo = cu->dwo_unit != NULL;
23360 }
23361
23362 static void
23363 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23364 struct dwarf2_cu *cu, int is_block)
23365 {
23366 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23367 struct objfile *objfile = dwarf2_per_objfile->objfile;
23368 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23369
23370 if (attr->form_is_section_offset ()
23371 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23372 the section. If so, fall through to the complaint in the
23373 other branch. */
23374 && DW_UNSND (attr) < section->get_size (objfile))
23375 {
23376 struct dwarf2_loclist_baton *baton;
23377
23378 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23379
23380 fill_in_loclist_baton (cu, baton, attr);
23381
23382 if (!cu->base_address.has_value ())
23383 complaint (_("Location list used without "
23384 "specifying the CU base address."));
23385
23386 SYMBOL_ACLASS_INDEX (sym) = (is_block
23387 ? dwarf2_loclist_block_index
23388 : dwarf2_loclist_index);
23389 SYMBOL_LOCATION_BATON (sym) = baton;
23390 }
23391 else
23392 {
23393 struct dwarf2_locexpr_baton *baton;
23394
23395 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23396 baton->per_objfile = dwarf2_per_objfile;
23397 baton->per_cu = cu->per_cu;
23398 gdb_assert (baton->per_cu);
23399
23400 if (attr->form_is_block ())
23401 {
23402 /* Note that we're just copying the block's data pointer
23403 here, not the actual data. We're still pointing into the
23404 info_buffer for SYM's objfile; right now we never release
23405 that buffer, but when we do clean up properly this may
23406 need to change. */
23407 baton->size = DW_BLOCK (attr)->size;
23408 baton->data = DW_BLOCK (attr)->data;
23409 }
23410 else
23411 {
23412 dwarf2_invalid_attrib_class_complaint ("location description",
23413 sym->natural_name ());
23414 baton->size = 0;
23415 }
23416
23417 SYMBOL_ACLASS_INDEX (sym) = (is_block
23418 ? dwarf2_locexpr_block_index
23419 : dwarf2_locexpr_index);
23420 SYMBOL_LOCATION_BATON (sym) = baton;
23421 }
23422 }
23423
23424 /* See read.h. */
23425
23426 const comp_unit_head *
23427 dwarf2_per_cu_data::get_header () const
23428 {
23429 if (!m_header_read_in)
23430 {
23431 const gdb_byte *info_ptr
23432 = this->section->buffer + to_underlying (this->sect_off);
23433
23434 memset (&m_header, 0, sizeof (m_header));
23435
23436 read_comp_unit_head (&m_header, info_ptr, this->section,
23437 rcuh_kind::COMPILE);
23438 }
23439
23440 return &m_header;
23441 }
23442
23443 /* See read.h. */
23444
23445 int
23446 dwarf2_per_cu_data::addr_size () const
23447 {
23448 return this->get_header ()->addr_size;
23449 }
23450
23451 /* See read.h. */
23452
23453 int
23454 dwarf2_per_cu_data::offset_size () const
23455 {
23456 return this->get_header ()->offset_size;
23457 }
23458
23459 /* See read.h. */
23460
23461 int
23462 dwarf2_per_cu_data::ref_addr_size () const
23463 {
23464 const comp_unit_head *header = this->get_header ();
23465
23466 if (header->version == 2)
23467 return header->addr_size;
23468 else
23469 return header->offset_size;
23470 }
23471
23472 /* See read.h. */
23473
23474 struct type *
23475 dwarf2_cu::addr_type () const
23476 {
23477 struct objfile *objfile = this->per_objfile->objfile;
23478 struct type *void_type = objfile_type (objfile)->builtin_void;
23479 struct type *addr_type = lookup_pointer_type (void_type);
23480 int addr_size = this->per_cu->addr_size ();
23481
23482 if (TYPE_LENGTH (addr_type) == addr_size)
23483 return addr_type;
23484
23485 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23486 return addr_type;
23487 }
23488
23489 /* A helper function for dwarf2_find_containing_comp_unit that returns
23490 the index of the result, and that searches a vector. It will
23491 return a result even if the offset in question does not actually
23492 occur in any CU. This is separate so that it can be unit
23493 tested. */
23494
23495 static int
23496 dwarf2_find_containing_comp_unit
23497 (sect_offset sect_off,
23498 unsigned int offset_in_dwz,
23499 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23500 {
23501 int low, high;
23502
23503 low = 0;
23504 high = all_comp_units.size () - 1;
23505 while (high > low)
23506 {
23507 struct dwarf2_per_cu_data *mid_cu;
23508 int mid = low + (high - low) / 2;
23509
23510 mid_cu = all_comp_units[mid];
23511 if (mid_cu->is_dwz > offset_in_dwz
23512 || (mid_cu->is_dwz == offset_in_dwz
23513 && mid_cu->sect_off + mid_cu->length > sect_off))
23514 high = mid;
23515 else
23516 low = mid + 1;
23517 }
23518 gdb_assert (low == high);
23519 return low;
23520 }
23521
23522 /* Locate the .debug_info compilation unit from CU's objfile which contains
23523 the DIE at OFFSET. Raises an error on failure. */
23524
23525 static struct dwarf2_per_cu_data *
23526 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23527 unsigned int offset_in_dwz,
23528 struct dwarf2_per_objfile *dwarf2_per_objfile)
23529 {
23530 int low
23531 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23532 dwarf2_per_objfile->per_bfd->all_comp_units);
23533 struct dwarf2_per_cu_data *this_cu
23534 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23535
23536 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23537 {
23538 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23539 error (_("Dwarf Error: could not find partial DIE containing "
23540 "offset %s [in module %s]"),
23541 sect_offset_str (sect_off),
23542 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23543
23544 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23545 <= sect_off);
23546 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23547 }
23548 else
23549 {
23550 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23551 && sect_off >= this_cu->sect_off + this_cu->length)
23552 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23553 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23554 return this_cu;
23555 }
23556 }
23557
23558 #if GDB_SELF_TEST
23559
23560 namespace selftests {
23561 namespace find_containing_comp_unit {
23562
23563 static void
23564 run_test ()
23565 {
23566 struct dwarf2_per_cu_data one {};
23567 struct dwarf2_per_cu_data two {};
23568 struct dwarf2_per_cu_data three {};
23569 struct dwarf2_per_cu_data four {};
23570
23571 one.length = 5;
23572 two.sect_off = sect_offset (one.length);
23573 two.length = 7;
23574
23575 three.length = 5;
23576 three.is_dwz = 1;
23577 four.sect_off = sect_offset (three.length);
23578 four.length = 7;
23579 four.is_dwz = 1;
23580
23581 std::vector<dwarf2_per_cu_data *> units;
23582 units.push_back (&one);
23583 units.push_back (&two);
23584 units.push_back (&three);
23585 units.push_back (&four);
23586
23587 int result;
23588
23589 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23590 SELF_CHECK (units[result] == &one);
23591 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23592 SELF_CHECK (units[result] == &one);
23593 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23594 SELF_CHECK (units[result] == &two);
23595
23596 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23597 SELF_CHECK (units[result] == &three);
23598 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23599 SELF_CHECK (units[result] == &three);
23600 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23601 SELF_CHECK (units[result] == &four);
23602 }
23603
23604 }
23605 }
23606
23607 #endif /* GDB_SELF_TEST */
23608
23609 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23610
23611 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23612 dwarf2_per_objfile *per_objfile)
23613 : per_cu (per_cu),
23614 per_objfile (per_objfile),
23615 mark (false),
23616 has_loclist (false),
23617 checked_producer (false),
23618 producer_is_gxx_lt_4_6 (false),
23619 producer_is_gcc_lt_4_3 (false),
23620 producer_is_icc (false),
23621 producer_is_icc_lt_14 (false),
23622 producer_is_codewarrior (false),
23623 processing_has_namespace_info (false)
23624 {
23625 }
23626
23627 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23628
23629 static void
23630 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23631 enum language pretend_language)
23632 {
23633 struct attribute *attr;
23634
23635 /* Set the language we're debugging. */
23636 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23637 if (attr != nullptr)
23638 set_cu_language (DW_UNSND (attr), cu);
23639 else
23640 {
23641 cu->language = pretend_language;
23642 cu->language_defn = language_def (cu->language);
23643 }
23644
23645 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23646 }
23647
23648 /* See read.h. */
23649
23650 dwarf2_cu *
23651 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23652 {
23653 auto it = m_dwarf2_cus.find (per_cu);
23654 if (it == m_dwarf2_cus.end ())
23655 return nullptr;
23656
23657 return it->second;
23658 }
23659
23660 /* See read.h. */
23661
23662 void
23663 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23664 {
23665 gdb_assert (this->get_cu (per_cu) == nullptr);
23666
23667 m_dwarf2_cus[per_cu] = cu;
23668 }
23669
23670 /* See read.h. */
23671
23672 void
23673 dwarf2_per_objfile::age_comp_units ()
23674 {
23675 /* Start by clearing all marks. */
23676 for (auto pair : m_dwarf2_cus)
23677 pair.second->mark = false;
23678
23679 /* Traverse all CUs, mark them and their dependencies if used recently
23680 enough. */
23681 for (auto pair : m_dwarf2_cus)
23682 {
23683 dwarf2_cu *cu = pair.second;
23684
23685 cu->last_used++;
23686 if (cu->last_used <= dwarf_max_cache_age)
23687 dwarf2_mark (cu);
23688 }
23689
23690 /* Delete all CUs still not marked. */
23691 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23692 {
23693 dwarf2_cu *cu = it->second;
23694
23695 if (!cu->mark)
23696 {
23697 delete cu;
23698 it = m_dwarf2_cus.erase (it);
23699 }
23700 else
23701 it++;
23702 }
23703 }
23704
23705 /* See read.h. */
23706
23707 void
23708 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23709 {
23710 auto it = m_dwarf2_cus.find (per_cu);
23711 if (it == m_dwarf2_cus.end ())
23712 return;
23713
23714 delete it->second;
23715
23716 m_dwarf2_cus.erase (it);
23717 }
23718
23719 dwarf2_per_objfile::~dwarf2_per_objfile ()
23720 {
23721 remove_all_cus ();
23722 }
23723
23724 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23725 We store these in a hash table separate from the DIEs, and preserve them
23726 when the DIEs are flushed out of cache.
23727
23728 The CU "per_cu" pointer is needed because offset alone is not enough to
23729 uniquely identify the type. A file may have multiple .debug_types sections,
23730 or the type may come from a DWO file. Furthermore, while it's more logical
23731 to use per_cu->section+offset, with Fission the section with the data is in
23732 the DWO file but we don't know that section at the point we need it.
23733 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23734 because we can enter the lookup routine, get_die_type_at_offset, from
23735 outside this file, and thus won't necessarily have PER_CU->cu.
23736 Fortunately, PER_CU is stable for the life of the objfile. */
23737
23738 struct dwarf2_per_cu_offset_and_type
23739 {
23740 const struct dwarf2_per_cu_data *per_cu;
23741 sect_offset sect_off;
23742 struct type *type;
23743 };
23744
23745 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23746
23747 static hashval_t
23748 per_cu_offset_and_type_hash (const void *item)
23749 {
23750 const struct dwarf2_per_cu_offset_and_type *ofs
23751 = (const struct dwarf2_per_cu_offset_and_type *) item;
23752
23753 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23754 }
23755
23756 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23757
23758 static int
23759 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23760 {
23761 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23762 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23763 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23764 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23765
23766 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23767 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23768 }
23769
23770 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23771 table if necessary. For convenience, return TYPE.
23772
23773 The DIEs reading must have careful ordering to:
23774 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23775 reading current DIE.
23776 * Not trying to dereference contents of still incompletely read in types
23777 while reading in other DIEs.
23778 * Enable referencing still incompletely read in types just by a pointer to
23779 the type without accessing its fields.
23780
23781 Therefore caller should follow these rules:
23782 * Try to fetch any prerequisite types we may need to build this DIE type
23783 before building the type and calling set_die_type.
23784 * After building type call set_die_type for current DIE as soon as
23785 possible before fetching more types to complete the current type.
23786 * Make the type as complete as possible before fetching more types. */
23787
23788 static struct type *
23789 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23790 {
23791 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23792 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23793 struct objfile *objfile = dwarf2_per_objfile->objfile;
23794 struct attribute *attr;
23795 struct dynamic_prop prop;
23796
23797 /* For Ada types, make sure that the gnat-specific data is always
23798 initialized (if not already set). There are a few types where
23799 we should not be doing so, because the type-specific area is
23800 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23801 where the type-specific area is used to store the floatformat).
23802 But this is not a problem, because the gnat-specific information
23803 is actually not needed for these types. */
23804 if (need_gnat_info (cu)
23805 && type->code () != TYPE_CODE_FUNC
23806 && type->code () != TYPE_CODE_FLT
23807 && type->code () != TYPE_CODE_METHODPTR
23808 && type->code () != TYPE_CODE_MEMBERPTR
23809 && type->code () != TYPE_CODE_METHOD
23810 && !HAVE_GNAT_AUX_INFO (type))
23811 INIT_GNAT_SPECIFIC (type);
23812
23813 /* Read DW_AT_allocated and set in type. */
23814 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23815 if (attr != NULL && attr->form_is_block ())
23816 {
23817 struct type *prop_type = cu->addr_sized_int_type (false);
23818 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23819 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23820 }
23821 else if (attr != NULL)
23822 {
23823 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23824 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23825 sect_offset_str (die->sect_off));
23826 }
23827
23828 /* Read DW_AT_associated and set in type. */
23829 attr = dwarf2_attr (die, DW_AT_associated, cu);
23830 if (attr != NULL && attr->form_is_block ())
23831 {
23832 struct type *prop_type = cu->addr_sized_int_type (false);
23833 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23834 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23835 }
23836 else if (attr != NULL)
23837 {
23838 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23839 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23840 sect_offset_str (die->sect_off));
23841 }
23842
23843 /* Read DW_AT_data_location and set in type. */
23844 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23845 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23846 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23847
23848 if (dwarf2_per_objfile->die_type_hash == NULL)
23849 dwarf2_per_objfile->die_type_hash
23850 = htab_up (htab_create_alloc (127,
23851 per_cu_offset_and_type_hash,
23852 per_cu_offset_and_type_eq,
23853 NULL, xcalloc, xfree));
23854
23855 ofs.per_cu = cu->per_cu;
23856 ofs.sect_off = die->sect_off;
23857 ofs.type = type;
23858 slot = (struct dwarf2_per_cu_offset_and_type **)
23859 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23860 if (*slot)
23861 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23862 sect_offset_str (die->sect_off));
23863 *slot = XOBNEW (&objfile->objfile_obstack,
23864 struct dwarf2_per_cu_offset_and_type);
23865 **slot = ofs;
23866 return type;
23867 }
23868
23869 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23870 or return NULL if the die does not have a saved type. */
23871
23872 static struct type *
23873 get_die_type_at_offset (sect_offset sect_off,
23874 dwarf2_per_cu_data *per_cu,
23875 dwarf2_per_objfile *dwarf2_per_objfile)
23876 {
23877 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23878
23879 if (dwarf2_per_objfile->die_type_hash == NULL)
23880 return NULL;
23881
23882 ofs.per_cu = per_cu;
23883 ofs.sect_off = sect_off;
23884 slot = ((struct dwarf2_per_cu_offset_and_type *)
23885 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23886 if (slot)
23887 return slot->type;
23888 else
23889 return NULL;
23890 }
23891
23892 /* Look up the type for DIE in CU in die_type_hash,
23893 or return NULL if DIE does not have a saved type. */
23894
23895 static struct type *
23896 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23897 {
23898 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23899 }
23900
23901 /* Add a dependence relationship from CU to REF_PER_CU. */
23902
23903 static void
23904 dwarf2_add_dependence (struct dwarf2_cu *cu,
23905 struct dwarf2_per_cu_data *ref_per_cu)
23906 {
23907 void **slot;
23908
23909 if (cu->dependencies == NULL)
23910 cu->dependencies
23911 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23912 NULL, &cu->comp_unit_obstack,
23913 hashtab_obstack_allocate,
23914 dummy_obstack_deallocate);
23915
23916 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23917 if (*slot == NULL)
23918 *slot = ref_per_cu;
23919 }
23920
23921 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23922 Set the mark field in every compilation unit in the
23923 cache that we must keep because we are keeping CU.
23924
23925 DATA is the dwarf2_per_objfile object in which to look up CUs. */
23926
23927 static int
23928 dwarf2_mark_helper (void **slot, void *data)
23929 {
23930 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23931 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23932 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23933
23934 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23935 reading of the chain. As such dependencies remain valid it is not much
23936 useful to track and undo them during QUIT cleanups. */
23937 if (cu == nullptr)
23938 return 1;
23939
23940 if (cu->mark)
23941 return 1;
23942
23943 cu->mark = true;
23944
23945 if (cu->dependencies != nullptr)
23946 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
23947
23948 return 1;
23949 }
23950
23951 /* Set the mark field in CU and in every other compilation unit in the
23952 cache that we must keep because we are keeping CU. */
23953
23954 static void
23955 dwarf2_mark (struct dwarf2_cu *cu)
23956 {
23957 if (cu->mark)
23958 return;
23959
23960 cu->mark = true;
23961
23962 if (cu->dependencies != nullptr)
23963 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
23964 }
23965
23966 /* Trivial hash function for partial_die_info: the hash value of a DIE
23967 is its offset in .debug_info for this objfile. */
23968
23969 static hashval_t
23970 partial_die_hash (const void *item)
23971 {
23972 const struct partial_die_info *part_die
23973 = (const struct partial_die_info *) item;
23974
23975 return to_underlying (part_die->sect_off);
23976 }
23977
23978 /* Trivial comparison function for partial_die_info structures: two DIEs
23979 are equal if they have the same offset. */
23980
23981 static int
23982 partial_die_eq (const void *item_lhs, const void *item_rhs)
23983 {
23984 const struct partial_die_info *part_die_lhs
23985 = (const struct partial_die_info *) item_lhs;
23986 const struct partial_die_info *part_die_rhs
23987 = (const struct partial_die_info *) item_rhs;
23988
23989 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23990 }
23991
23992 struct cmd_list_element *set_dwarf_cmdlist;
23993 struct cmd_list_element *show_dwarf_cmdlist;
23994
23995 static void
23996 show_check_physname (struct ui_file *file, int from_tty,
23997 struct cmd_list_element *c, const char *value)
23998 {
23999 fprintf_filtered (file,
24000 _("Whether to check \"physname\" is %s.\n"),
24001 value);
24002 }
24003
24004 void _initialize_dwarf2_read ();
24005 void
24006 _initialize_dwarf2_read ()
24007 {
24008 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24009 Set DWARF specific variables.\n\
24010 Configure DWARF variables such as the cache size."),
24011 &set_dwarf_cmdlist, "maintenance set dwarf ",
24012 0/*allow-unknown*/, &maintenance_set_cmdlist);
24013
24014 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24015 Show DWARF specific variables.\n\
24016 Show DWARF variables such as the cache size."),
24017 &show_dwarf_cmdlist, "maintenance show dwarf ",
24018 0/*allow-unknown*/, &maintenance_show_cmdlist);
24019
24020 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24021 &dwarf_max_cache_age, _("\
24022 Set the upper bound on the age of cached DWARF compilation units."), _("\
24023 Show the upper bound on the age of cached DWARF compilation units."), _("\
24024 A higher limit means that cached compilation units will be stored\n\
24025 in memory longer, and more total memory will be used. Zero disables\n\
24026 caching, which can slow down startup."),
24027 NULL,
24028 show_dwarf_max_cache_age,
24029 &set_dwarf_cmdlist,
24030 &show_dwarf_cmdlist);
24031
24032 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24033 Set debugging of the DWARF reader."), _("\
24034 Show debugging of the DWARF reader."), _("\
24035 When enabled (non-zero), debugging messages are printed during DWARF\n\
24036 reading and symtab expansion. A value of 1 (one) provides basic\n\
24037 information. A value greater than 1 provides more verbose information."),
24038 NULL,
24039 NULL,
24040 &setdebuglist, &showdebuglist);
24041
24042 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24043 Set debugging of the DWARF DIE reader."), _("\
24044 Show debugging of the DWARF DIE reader."), _("\
24045 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24046 The value is the maximum depth to print."),
24047 NULL,
24048 NULL,
24049 &setdebuglist, &showdebuglist);
24050
24051 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24052 Set debugging of the dwarf line reader."), _("\
24053 Show debugging of the dwarf line reader."), _("\
24054 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24055 A value of 1 (one) provides basic information.\n\
24056 A value greater than 1 provides more verbose information."),
24057 NULL,
24058 NULL,
24059 &setdebuglist, &showdebuglist);
24060
24061 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24062 Set cross-checking of \"physname\" code against demangler."), _("\
24063 Show cross-checking of \"physname\" code against demangler."), _("\
24064 When enabled, GDB's internal \"physname\" code is checked against\n\
24065 the demangler."),
24066 NULL, show_check_physname,
24067 &setdebuglist, &showdebuglist);
24068
24069 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24070 no_class, &use_deprecated_index_sections, _("\
24071 Set whether to use deprecated gdb_index sections."), _("\
24072 Show whether to use deprecated gdb_index sections."), _("\
24073 When enabled, deprecated .gdb_index sections are used anyway.\n\
24074 Normally they are ignored either because of a missing feature or\n\
24075 performance issue.\n\
24076 Warning: This option must be enabled before gdb reads the file."),
24077 NULL,
24078 NULL,
24079 &setlist, &showlist);
24080
24081 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24082 &dwarf2_locexpr_funcs);
24083 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24084 &dwarf2_loclist_funcs);
24085
24086 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24087 &dwarf2_block_frame_base_locexpr_funcs);
24088 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24089 &dwarf2_block_frame_base_loclist_funcs);
24090
24091 #if GDB_SELF_TEST
24092 selftests::register_test ("dw2_expand_symtabs_matching",
24093 selftests::dw2_expand_symtabs_matching::run_test);
24094 selftests::register_test ("dwarf2_find_containing_comp_unit",
24095 selftests::find_containing_comp_unit::run_test);
24096 #endif
24097 }
This page took 0.559253 seconds and 4 git commands to generate.