Don't write to inferior_ptid in corelow.c
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 /* This is used to store the data that is always per objfile. */
109 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
110
111 /* These are used to store the dwarf2_per_bfd objects.
112
113 objfiles having the same BFD, which doesn't require relocations, are going to
114 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
115
116 Other objfiles are not going to share a dwarf2_per_bfd with any other
117 objfiles, so they'll have their own version kept in the _objfile_data_key
118 version. */
119 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
120 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
121
122 /* The "aclass" indices for various kinds of computed DWARF symbols. */
123
124 static int dwarf2_locexpr_index;
125 static int dwarf2_loclist_index;
126 static int dwarf2_locexpr_block_index;
127 static int dwarf2_loclist_block_index;
128
129 /* Size of .debug_loclists section header for 32-bit DWARF format. */
130 #define LOCLIST_HEADER_SIZE32 12
131
132 /* Size of .debug_loclists section header for 64-bit DWARF format. */
133 #define LOCLIST_HEADER_SIZE64 20
134
135 /* An index into a (C++) symbol name component in a symbol name as
136 recorded in the mapped_index's symbol table. For each C++ symbol
137 in the symbol table, we record one entry for the start of each
138 component in the symbol in a table of name components, and then
139 sort the table, in order to be able to binary search symbol names,
140 ignoring leading namespaces, both completion and regular look up.
141 For example, for symbol "A::B::C", we'll have an entry that points
142 to "A::B::C", another that points to "B::C", and another for "C".
143 Note that function symbols in GDB index have no parameter
144 information, just the function/method names. You can convert a
145 name_component to a "const char *" using the
146 'mapped_index::symbol_name_at(offset_type)' method. */
147
148 struct name_component
149 {
150 /* Offset in the symbol name where the component starts. Stored as
151 a (32-bit) offset instead of a pointer to save memory and improve
152 locality on 64-bit architectures. */
153 offset_type name_offset;
154
155 /* The symbol's index in the symbol and constant pool tables of a
156 mapped_index. */
157 offset_type idx;
158 };
159
160 /* Base class containing bits shared by both .gdb_index and
161 .debug_name indexes. */
162
163 struct mapped_index_base
164 {
165 mapped_index_base () = default;
166 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
167
168 /* The name_component table (a sorted vector). See name_component's
169 description above. */
170 std::vector<name_component> name_components;
171
172 /* How NAME_COMPONENTS is sorted. */
173 enum case_sensitivity name_components_casing;
174
175 /* Return the number of names in the symbol table. */
176 virtual size_t symbol_name_count () const = 0;
177
178 /* Get the name of the symbol at IDX in the symbol table. */
179 virtual const char *symbol_name_at
180 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
181
182 /* Return whether the name at IDX in the symbol table should be
183 ignored. */
184 virtual bool symbol_name_slot_invalid (offset_type idx) const
185 {
186 return false;
187 }
188
189 /* Build the symbol name component sorted vector, if we haven't
190 yet. */
191 void build_name_components (dwarf2_per_objfile *per_objfile);
192
193 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
194 possible matches for LN_NO_PARAMS in the name component
195 vector. */
196 std::pair<std::vector<name_component>::const_iterator,
197 std::vector<name_component>::const_iterator>
198 find_name_components_bounds (const lookup_name_info &ln_no_params,
199 enum language lang,
200 dwarf2_per_objfile *per_objfile) const;
201
202 /* Prevent deleting/destroying via a base class pointer. */
203 protected:
204 ~mapped_index_base() = default;
205 };
206
207 /* A description of the mapped index. The file format is described in
208 a comment by the code that writes the index. */
209 struct mapped_index final : public mapped_index_base
210 {
211 /* A slot/bucket in the symbol table hash. */
212 struct symbol_table_slot
213 {
214 const offset_type name;
215 const offset_type vec;
216 };
217
218 /* Index data format version. */
219 int version = 0;
220
221 /* The address table data. */
222 gdb::array_view<const gdb_byte> address_table;
223
224 /* The symbol table, implemented as a hash table. */
225 gdb::array_view<symbol_table_slot> symbol_table;
226
227 /* A pointer to the constant pool. */
228 const char *constant_pool = nullptr;
229
230 bool symbol_name_slot_invalid (offset_type idx) const override
231 {
232 const auto &bucket = this->symbol_table[idx];
233 return bucket.name == 0 && bucket.vec == 0;
234 }
235
236 /* Convenience method to get at the name of the symbol at IDX in the
237 symbol table. */
238 const char *symbol_name_at
239 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
240 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
241
242 size_t symbol_name_count () const override
243 { return this->symbol_table.size (); }
244 };
245
246 /* A description of the mapped .debug_names.
247 Uninitialized map has CU_COUNT 0. */
248 struct mapped_debug_names final : public mapped_index_base
249 {
250 bfd_endian dwarf5_byte_order;
251 bool dwarf5_is_dwarf64;
252 bool augmentation_is_gdb;
253 uint8_t offset_size;
254 uint32_t cu_count = 0;
255 uint32_t tu_count, bucket_count, name_count;
256 const gdb_byte *cu_table_reordered, *tu_table_reordered;
257 const uint32_t *bucket_table_reordered, *hash_table_reordered;
258 const gdb_byte *name_table_string_offs_reordered;
259 const gdb_byte *name_table_entry_offs_reordered;
260 const gdb_byte *entry_pool;
261
262 struct index_val
263 {
264 ULONGEST dwarf_tag;
265 struct attr
266 {
267 /* Attribute name DW_IDX_*. */
268 ULONGEST dw_idx;
269
270 /* Attribute form DW_FORM_*. */
271 ULONGEST form;
272
273 /* Value if FORM is DW_FORM_implicit_const. */
274 LONGEST implicit_const;
275 };
276 std::vector<attr> attr_vec;
277 };
278
279 std::unordered_map<ULONGEST, index_val> abbrev_map;
280
281 const char *namei_to_name
282 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
283
284 /* Implementation of the mapped_index_base virtual interface, for
285 the name_components cache. */
286
287 const char *symbol_name_at
288 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
289 { return namei_to_name (idx, per_objfile); }
290
291 size_t symbol_name_count () const override
292 { return this->name_count; }
293 };
294
295 /* See dwarf2read.h. */
296
297 dwarf2_per_objfile *
298 get_dwarf2_per_objfile (struct objfile *objfile)
299 {
300 return dwarf2_objfile_data_key.get (objfile);
301 }
302
303 /* Default names of the debugging sections. */
304
305 /* Note that if the debugging section has been compressed, it might
306 have a name like .zdebug_info. */
307
308 static const struct dwarf2_debug_sections dwarf2_elf_names =
309 {
310 { ".debug_info", ".zdebug_info" },
311 { ".debug_abbrev", ".zdebug_abbrev" },
312 { ".debug_line", ".zdebug_line" },
313 { ".debug_loc", ".zdebug_loc" },
314 { ".debug_loclists", ".zdebug_loclists" },
315 { ".debug_macinfo", ".zdebug_macinfo" },
316 { ".debug_macro", ".zdebug_macro" },
317 { ".debug_str", ".zdebug_str" },
318 { ".debug_str_offsets", ".zdebug_str_offsets" },
319 { ".debug_line_str", ".zdebug_line_str" },
320 { ".debug_ranges", ".zdebug_ranges" },
321 { ".debug_rnglists", ".zdebug_rnglists" },
322 { ".debug_types", ".zdebug_types" },
323 { ".debug_addr", ".zdebug_addr" },
324 { ".debug_frame", ".zdebug_frame" },
325 { ".eh_frame", NULL },
326 { ".gdb_index", ".zgdb_index" },
327 { ".debug_names", ".zdebug_names" },
328 { ".debug_aranges", ".zdebug_aranges" },
329 23
330 };
331
332 /* List of DWO/DWP sections. */
333
334 static const struct dwop_section_names
335 {
336 struct dwarf2_section_names abbrev_dwo;
337 struct dwarf2_section_names info_dwo;
338 struct dwarf2_section_names line_dwo;
339 struct dwarf2_section_names loc_dwo;
340 struct dwarf2_section_names loclists_dwo;
341 struct dwarf2_section_names macinfo_dwo;
342 struct dwarf2_section_names macro_dwo;
343 struct dwarf2_section_names str_dwo;
344 struct dwarf2_section_names str_offsets_dwo;
345 struct dwarf2_section_names types_dwo;
346 struct dwarf2_section_names cu_index;
347 struct dwarf2_section_names tu_index;
348 }
349 dwop_section_names =
350 {
351 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
352 { ".debug_info.dwo", ".zdebug_info.dwo" },
353 { ".debug_line.dwo", ".zdebug_line.dwo" },
354 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
355 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
356 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
357 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
358 { ".debug_str.dwo", ".zdebug_str.dwo" },
359 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
360 { ".debug_types.dwo", ".zdebug_types.dwo" },
361 { ".debug_cu_index", ".zdebug_cu_index" },
362 { ".debug_tu_index", ".zdebug_tu_index" },
363 };
364
365 /* local data types */
366
367 /* The location list section (.debug_loclists) begins with a header,
368 which contains the following information. */
369 struct loclist_header
370 {
371 /* A 4-byte or 12-byte length containing the length of the
372 set of entries for this compilation unit, not including the
373 length field itself. */
374 unsigned int length;
375
376 /* A 2-byte version identifier. */
377 short version;
378
379 /* A 1-byte unsigned integer containing the size in bytes of an address on
380 the target system. */
381 unsigned char addr_size;
382
383 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
384 on the target system. */
385 unsigned char segment_collector_size;
386
387 /* A 4-byte count of the number of offsets that follow the header. */
388 unsigned int offset_entry_count;
389 };
390
391 /* Type used for delaying computation of method physnames.
392 See comments for compute_delayed_physnames. */
393 struct delayed_method_info
394 {
395 /* The type to which the method is attached, i.e., its parent class. */
396 struct type *type;
397
398 /* The index of the method in the type's function fieldlists. */
399 int fnfield_index;
400
401 /* The index of the method in the fieldlist. */
402 int index;
403
404 /* The name of the DIE. */
405 const char *name;
406
407 /* The DIE associated with this method. */
408 struct die_info *die;
409 };
410
411 /* Internal state when decoding a particular compilation unit. */
412 struct dwarf2_cu
413 {
414 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
415 dwarf2_per_objfile *per_objfile);
416
417 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
418
419 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
420 Create the set of symtabs used by this TU, or if this TU is sharing
421 symtabs with another TU and the symtabs have already been created
422 then restore those symtabs in the line header.
423 We don't need the pc/line-number mapping for type units. */
424 void setup_type_unit_groups (struct die_info *die);
425
426 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
427 buildsym_compunit constructor. */
428 struct compunit_symtab *start_symtab (const char *name,
429 const char *comp_dir,
430 CORE_ADDR low_pc);
431
432 /* Reset the builder. */
433 void reset_builder () { m_builder.reset (); }
434
435 /* Return a type that is a generic pointer type, the size of which
436 matches the address size given in the compilation unit header for
437 this CU. */
438 struct type *addr_type () const;
439
440 /* Find an integer type the same size as the address size given in
441 the compilation unit header for this CU. UNSIGNED_P controls if
442 the integer is unsigned or not. */
443 struct type *addr_sized_int_type (bool unsigned_p) const;
444
445 /* The header of the compilation unit. */
446 struct comp_unit_head header {};
447
448 /* Base address of this compilation unit. */
449 gdb::optional<CORE_ADDR> base_address;
450
451 /* The language we are debugging. */
452 enum language language = language_unknown;
453 const struct language_defn *language_defn = nullptr;
454
455 const char *producer = nullptr;
456
457 private:
458 /* The symtab builder for this CU. This is only non-NULL when full
459 symbols are being read. */
460 std::unique_ptr<buildsym_compunit> m_builder;
461
462 public:
463 /* The generic symbol table building routines have separate lists for
464 file scope symbols and all all other scopes (local scopes). So
465 we need to select the right one to pass to add_symbol_to_list().
466 We do it by keeping a pointer to the correct list in list_in_scope.
467
468 FIXME: The original dwarf code just treated the file scope as the
469 first local scope, and all other local scopes as nested local
470 scopes, and worked fine. Check to see if we really need to
471 distinguish these in buildsym.c. */
472 struct pending **list_in_scope = nullptr;
473
474 /* Hash table holding all the loaded partial DIEs
475 with partial_die->offset.SECT_OFF as hash. */
476 htab_t partial_dies = nullptr;
477
478 /* Storage for things with the same lifetime as this read-in compilation
479 unit, including partial DIEs. */
480 auto_obstack comp_unit_obstack;
481
482 /* Backlink to our per_cu entry. */
483 struct dwarf2_per_cu_data *per_cu;
484
485 /* The dwarf2_per_objfile that owns this. */
486 dwarf2_per_objfile *per_objfile;
487
488 /* How many compilation units ago was this CU last referenced? */
489 int last_used = 0;
490
491 /* A hash table of DIE cu_offset for following references with
492 die_info->offset.sect_off as hash. */
493 htab_t die_hash = nullptr;
494
495 /* Full DIEs if read in. */
496 struct die_info *dies = nullptr;
497
498 /* A set of pointers to dwarf2_per_cu_data objects for compilation
499 units referenced by this one. Only set during full symbol processing;
500 partial symbol tables do not have dependencies. */
501 htab_t dependencies = nullptr;
502
503 /* Header data from the line table, during full symbol processing. */
504 struct line_header *line_header = nullptr;
505 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
506 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
507 this is the DW_TAG_compile_unit die for this CU. We'll hold on
508 to the line header as long as this DIE is being processed. See
509 process_die_scope. */
510 die_info *line_header_die_owner = nullptr;
511
512 /* A list of methods which need to have physnames computed
513 after all type information has been read. */
514 std::vector<delayed_method_info> method_list;
515
516 /* To be copied to symtab->call_site_htab. */
517 htab_t call_site_htab = nullptr;
518
519 /* Non-NULL if this CU came from a DWO file.
520 There is an invariant here that is important to remember:
521 Except for attributes copied from the top level DIE in the "main"
522 (or "stub") file in preparation for reading the DWO file
523 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
524 Either there isn't a DWO file (in which case this is NULL and the point
525 is moot), or there is and either we're not going to read it (in which
526 case this is NULL) or there is and we are reading it (in which case this
527 is non-NULL). */
528 struct dwo_unit *dwo_unit = nullptr;
529
530 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
531 Note this value comes from the Fission stub CU/TU's DIE. */
532 gdb::optional<ULONGEST> addr_base;
533
534 /* The DW_AT_rnglists_base attribute if present.
535 Note this value comes from the Fission stub CU/TU's DIE.
536 Also note that the value is zero in the non-DWO case so this value can
537 be used without needing to know whether DWO files are in use or not.
538 N.B. This does not apply to DW_AT_ranges appearing in
539 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
540 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
541 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
542 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
543 ULONGEST ranges_base = 0;
544
545 /* The DW_AT_loclists_base attribute if present. */
546 ULONGEST loclist_base = 0;
547
548 /* When reading debug info generated by older versions of rustc, we
549 have to rewrite some union types to be struct types with a
550 variant part. This rewriting must be done after the CU is fully
551 read in, because otherwise at the point of rewriting some struct
552 type might not have been fully processed. So, we keep a list of
553 all such types here and process them after expansion. */
554 std::vector<struct type *> rust_unions;
555
556 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
557 files, the value is implicitly zero. For DWARF 5 version DWO files, the
558 value is often implicit and is the size of the header of
559 .debug_str_offsets section (8 or 4, depending on the address size). */
560 gdb::optional<ULONGEST> str_offsets_base;
561
562 /* Mark used when releasing cached dies. */
563 bool mark : 1;
564
565 /* This CU references .debug_loc. See the symtab->locations_valid field.
566 This test is imperfect as there may exist optimized debug code not using
567 any location list and still facing inlining issues if handled as
568 unoptimized code. For a future better test see GCC PR other/32998. */
569 bool has_loclist : 1;
570
571 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
572 if all the producer_is_* fields are valid. This information is cached
573 because profiling CU expansion showed excessive time spent in
574 producer_is_gxx_lt_4_6. */
575 bool checked_producer : 1;
576 bool producer_is_gxx_lt_4_6 : 1;
577 bool producer_is_gcc_lt_4_3 : 1;
578 bool producer_is_icc : 1;
579 bool producer_is_icc_lt_14 : 1;
580 bool producer_is_codewarrior : 1;
581
582 /* When true, the file that we're processing is known to have
583 debugging info for C++ namespaces. GCC 3.3.x did not produce
584 this information, but later versions do. */
585
586 bool processing_has_namespace_info : 1;
587
588 struct partial_die_info *find_partial_die (sect_offset sect_off);
589
590 /* If this CU was inherited by another CU (via specification,
591 abstract_origin, etc), this is the ancestor CU. */
592 dwarf2_cu *ancestor;
593
594 /* Get the buildsym_compunit for this CU. */
595 buildsym_compunit *get_builder ()
596 {
597 /* If this CU has a builder associated with it, use that. */
598 if (m_builder != nullptr)
599 return m_builder.get ();
600
601 /* Otherwise, search ancestors for a valid builder. */
602 if (ancestor != nullptr)
603 return ancestor->get_builder ();
604
605 return nullptr;
606 }
607 };
608
609 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
610 This includes type_unit_group and quick_file_names. */
611
612 struct stmt_list_hash
613 {
614 /* The DWO unit this table is from or NULL if there is none. */
615 struct dwo_unit *dwo_unit;
616
617 /* Offset in .debug_line or .debug_line.dwo. */
618 sect_offset line_sect_off;
619 };
620
621 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
622 an object of this type. This contains elements of type unit groups
623 that can be shared across objfiles. The non-shareable parts are in
624 type_unit_group_unshareable. */
625
626 struct type_unit_group
627 {
628 /* dwarf2read.c's main "handle" on a TU symtab.
629 To simplify things we create an artificial CU that "includes" all the
630 type units using this stmt_list so that the rest of the code still has
631 a "per_cu" handle on the symtab. */
632 struct dwarf2_per_cu_data per_cu;
633
634 /* The TUs that share this DW_AT_stmt_list entry.
635 This is added to while parsing type units to build partial symtabs,
636 and is deleted afterwards and not used again. */
637 std::vector<signatured_type *> *tus;
638
639 /* The data used to construct the hash key. */
640 struct stmt_list_hash hash;
641 };
642
643 /* These sections are what may appear in a (real or virtual) DWO file. */
644
645 struct dwo_sections
646 {
647 struct dwarf2_section_info abbrev;
648 struct dwarf2_section_info line;
649 struct dwarf2_section_info loc;
650 struct dwarf2_section_info loclists;
651 struct dwarf2_section_info macinfo;
652 struct dwarf2_section_info macro;
653 struct dwarf2_section_info str;
654 struct dwarf2_section_info str_offsets;
655 /* In the case of a virtual DWO file, these two are unused. */
656 struct dwarf2_section_info info;
657 std::vector<dwarf2_section_info> types;
658 };
659
660 /* CUs/TUs in DWP/DWO files. */
661
662 struct dwo_unit
663 {
664 /* Backlink to the containing struct dwo_file. */
665 struct dwo_file *dwo_file;
666
667 /* The "id" that distinguishes this CU/TU.
668 .debug_info calls this "dwo_id", .debug_types calls this "signature".
669 Since signatures came first, we stick with it for consistency. */
670 ULONGEST signature;
671
672 /* The section this CU/TU lives in, in the DWO file. */
673 struct dwarf2_section_info *section;
674
675 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
676 sect_offset sect_off;
677 unsigned int length;
678
679 /* For types, offset in the type's DIE of the type defined by this TU. */
680 cu_offset type_offset_in_tu;
681 };
682
683 /* include/dwarf2.h defines the DWP section codes.
684 It defines a max value but it doesn't define a min value, which we
685 use for error checking, so provide one. */
686
687 enum dwp_v2_section_ids
688 {
689 DW_SECT_MIN = 1
690 };
691
692 /* Data for one DWO file.
693
694 This includes virtual DWO files (a virtual DWO file is a DWO file as it
695 appears in a DWP file). DWP files don't really have DWO files per se -
696 comdat folding of types "loses" the DWO file they came from, and from
697 a high level view DWP files appear to contain a mass of random types.
698 However, to maintain consistency with the non-DWP case we pretend DWP
699 files contain virtual DWO files, and we assign each TU with one virtual
700 DWO file (generally based on the line and abbrev section offsets -
701 a heuristic that seems to work in practice). */
702
703 struct dwo_file
704 {
705 dwo_file () = default;
706 DISABLE_COPY_AND_ASSIGN (dwo_file);
707
708 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
709 For virtual DWO files the name is constructed from the section offsets
710 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
711 from related CU+TUs. */
712 const char *dwo_name = nullptr;
713
714 /* The DW_AT_comp_dir attribute. */
715 const char *comp_dir = nullptr;
716
717 /* The bfd, when the file is open. Otherwise this is NULL.
718 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
719 gdb_bfd_ref_ptr dbfd;
720
721 /* The sections that make up this DWO file.
722 Remember that for virtual DWO files in DWP V2, these are virtual
723 sections (for lack of a better name). */
724 struct dwo_sections sections {};
725
726 /* The CUs in the file.
727 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
728 an extension to handle LLVM's Link Time Optimization output (where
729 multiple source files may be compiled into a single object/dwo pair). */
730 htab_up cus;
731
732 /* Table of TUs in the file.
733 Each element is a struct dwo_unit. */
734 htab_up tus;
735 };
736
737 /* These sections are what may appear in a DWP file. */
738
739 struct dwp_sections
740 {
741 /* These are used by both DWP version 1 and 2. */
742 struct dwarf2_section_info str;
743 struct dwarf2_section_info cu_index;
744 struct dwarf2_section_info tu_index;
745
746 /* These are only used by DWP version 2 files.
747 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
748 sections are referenced by section number, and are not recorded here.
749 In DWP version 2 there is at most one copy of all these sections, each
750 section being (effectively) comprised of the concatenation of all of the
751 individual sections that exist in the version 1 format.
752 To keep the code simple we treat each of these concatenated pieces as a
753 section itself (a virtual section?). */
754 struct dwarf2_section_info abbrev;
755 struct dwarf2_section_info info;
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
760 struct dwarf2_section_info str_offsets;
761 struct dwarf2_section_info types;
762 };
763
764 /* These sections are what may appear in a virtual DWO file in DWP version 1.
765 A virtual DWO file is a DWO file as it appears in a DWP file. */
766
767 struct virtual_v1_dwo_sections
768 {
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
776 That is recorded here, and copied to dwo_unit.section. */
777 struct dwarf2_section_info info_or_types;
778 };
779
780 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
781 In version 2, the sections of the DWO files are concatenated together
782 and stored in one section of that name. Thus each ELF section contains
783 several "virtual" sections. */
784
785 struct virtual_v2_dwo_sections
786 {
787 bfd_size_type abbrev_offset;
788 bfd_size_type abbrev_size;
789
790 bfd_size_type line_offset;
791 bfd_size_type line_size;
792
793 bfd_size_type loc_offset;
794 bfd_size_type loc_size;
795
796 bfd_size_type macinfo_offset;
797 bfd_size_type macinfo_size;
798
799 bfd_size_type macro_offset;
800 bfd_size_type macro_size;
801
802 bfd_size_type str_offsets_offset;
803 bfd_size_type str_offsets_size;
804
805 /* Each DWP hash table entry records one CU or one TU.
806 That is recorded here, and copied to dwo_unit.section. */
807 bfd_size_type info_or_types_offset;
808 bfd_size_type info_or_types_size;
809 };
810
811 /* Contents of DWP hash tables. */
812
813 struct dwp_hash_table
814 {
815 uint32_t version, nr_columns;
816 uint32_t nr_units, nr_slots;
817 const gdb_byte *hash_table, *unit_table;
818 union
819 {
820 struct
821 {
822 const gdb_byte *indices;
823 } v1;
824 struct
825 {
826 /* This is indexed by column number and gives the id of the section
827 in that column. */
828 #define MAX_NR_V2_DWO_SECTIONS \
829 (1 /* .debug_info or .debug_types */ \
830 + 1 /* .debug_abbrev */ \
831 + 1 /* .debug_line */ \
832 + 1 /* .debug_loc */ \
833 + 1 /* .debug_str_offsets */ \
834 + 1 /* .debug_macro or .debug_macinfo */)
835 int section_ids[MAX_NR_V2_DWO_SECTIONS];
836 const gdb_byte *offsets;
837 const gdb_byte *sizes;
838 } v2;
839 } section_pool;
840 };
841
842 /* Data for one DWP file. */
843
844 struct dwp_file
845 {
846 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
847 : name (name_),
848 dbfd (std::move (abfd))
849 {
850 }
851
852 /* Name of the file. */
853 const char *name;
854
855 /* File format version. */
856 int version = 0;
857
858 /* The bfd. */
859 gdb_bfd_ref_ptr dbfd;
860
861 /* Section info for this file. */
862 struct dwp_sections sections {};
863
864 /* Table of CUs in the file. */
865 const struct dwp_hash_table *cus = nullptr;
866
867 /* Table of TUs in the file. */
868 const struct dwp_hash_table *tus = nullptr;
869
870 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
871 htab_up loaded_cus;
872 htab_up loaded_tus;
873
874 /* Table to map ELF section numbers to their sections.
875 This is only needed for the DWP V1 file format. */
876 unsigned int num_sections = 0;
877 asection **elf_sections = nullptr;
878 };
879
880 /* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
883 struct exists to abstract away the constant parameters of die reading. */
884
885 struct die_reader_specs
886 {
887 /* The bfd of die_section. */
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
894 struct dwo_file *dwo_file;
895
896 /* The section the die comes from.
897 This is either .debug_info or .debug_types, or the .dwo variants. */
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
901 const gdb_byte *buffer;
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
905
906 /* The abbreviation table to use when reading the DIEs. */
907 struct abbrev_table *abbrev_table;
908 };
909
910 /* A subclass of die_reader_specs that holds storage and has complex
911 constructor and destructor behavior. */
912
913 class cutu_reader : public die_reader_specs
914 {
915 public:
916
917 cutu_reader (dwarf2_per_cu_data *this_cu,
918 dwarf2_per_objfile *per_objfile,
919 struct abbrev_table *abbrev_table,
920 dwarf2_cu *existing_cu,
921 bool skip_partial);
922
923 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
924 dwarf2_per_objfile *per_objfile,
925 struct dwarf2_cu *parent_cu = nullptr,
926 struct dwo_file *dwo_file = nullptr);
927
928 DISABLE_COPY_AND_ASSIGN (cutu_reader);
929
930 const gdb_byte *info_ptr = nullptr;
931 struct die_info *comp_unit_die = nullptr;
932 bool dummy_p = false;
933
934 /* Release the new CU, putting it on the chain. This cannot be done
935 for dummy CUs. */
936 void keep ();
937
938 private:
939 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
940 dwarf2_per_objfile *per_objfile,
941 dwarf2_cu *existing_cu);
942
943 struct dwarf2_per_cu_data *m_this_cu;
944 std::unique_ptr<dwarf2_cu> m_new_cu;
945
946 /* The ordinary abbreviation table. */
947 abbrev_table_up m_abbrev_table_holder;
948
949 /* The DWO abbreviation table. */
950 abbrev_table_up m_dwo_abbrev_table;
951 };
952
953 /* When we construct a partial symbol table entry we only
954 need this much information. */
955 struct partial_die_info : public allocate_on_obstack
956 {
957 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
958
959 /* Disable assign but still keep copy ctor, which is needed
960 load_partial_dies. */
961 partial_die_info& operator=(const partial_die_info& rhs) = delete;
962
963 /* Adjust the partial die before generating a symbol for it. This
964 function may set the is_external flag or change the DIE's
965 name. */
966 void fixup (struct dwarf2_cu *cu);
967
968 /* Read a minimal amount of information into the minimal die
969 structure. */
970 const gdb_byte *read (const struct die_reader_specs *reader,
971 const struct abbrev_info &abbrev,
972 const gdb_byte *info_ptr);
973
974 /* Compute the name of this partial DIE. This memoizes the
975 result, so it is safe to call multiple times. */
976 const char *name (dwarf2_cu *cu);
977
978 /* Offset of this DIE. */
979 const sect_offset sect_off;
980
981 /* DWARF-2 tag for this DIE. */
982 const ENUM_BITFIELD(dwarf_tag) tag : 16;
983
984 /* Assorted flags describing the data found in this DIE. */
985 const unsigned int has_children : 1;
986
987 unsigned int is_external : 1;
988 unsigned int is_declaration : 1;
989 unsigned int has_type : 1;
990 unsigned int has_specification : 1;
991 unsigned int has_pc_info : 1;
992 unsigned int may_be_inlined : 1;
993
994 /* This DIE has been marked DW_AT_main_subprogram. */
995 unsigned int main_subprogram : 1;
996
997 /* Flag set if the SCOPE field of this structure has been
998 computed. */
999 unsigned int scope_set : 1;
1000
1001 /* Flag set if the DIE has a byte_size attribute. */
1002 unsigned int has_byte_size : 1;
1003
1004 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1005 unsigned int has_const_value : 1;
1006
1007 /* Flag set if any of the DIE's children are template arguments. */
1008 unsigned int has_template_arguments : 1;
1009
1010 /* Flag set if fixup has been called on this die. */
1011 unsigned int fixup_called : 1;
1012
1013 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1014 unsigned int is_dwz : 1;
1015
1016 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1017 unsigned int spec_is_dwz : 1;
1018
1019 unsigned int canonical_name : 1;
1020
1021 /* The name of this DIE. Normally the value of DW_AT_name, but
1022 sometimes a default name for unnamed DIEs. */
1023 const char *raw_name = nullptr;
1024
1025 /* The linkage name, if present. */
1026 const char *linkage_name = nullptr;
1027
1028 /* The scope to prepend to our children. This is generally
1029 allocated on the comp_unit_obstack, so will disappear
1030 when this compilation unit leaves the cache. */
1031 const char *scope = nullptr;
1032
1033 /* Some data associated with the partial DIE. The tag determines
1034 which field is live. */
1035 union
1036 {
1037 /* The location description associated with this DIE, if any. */
1038 struct dwarf_block *locdesc;
1039 /* The offset of an import, for DW_TAG_imported_unit. */
1040 sect_offset sect_off;
1041 } d {};
1042
1043 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1044 CORE_ADDR lowpc = 0;
1045 CORE_ADDR highpc = 0;
1046
1047 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1048 DW_AT_sibling, if any. */
1049 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1050 could return DW_AT_sibling values to its caller load_partial_dies. */
1051 const gdb_byte *sibling = nullptr;
1052
1053 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1054 DW_AT_specification (or DW_AT_abstract_origin or
1055 DW_AT_extension). */
1056 sect_offset spec_offset {};
1057
1058 /* Pointers to this DIE's parent, first child, and next sibling,
1059 if any. */
1060 struct partial_die_info *die_parent = nullptr;
1061 struct partial_die_info *die_child = nullptr;
1062 struct partial_die_info *die_sibling = nullptr;
1063
1064 friend struct partial_die_info *
1065 dwarf2_cu::find_partial_die (sect_offset sect_off);
1066
1067 private:
1068 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1069 partial_die_info (sect_offset sect_off)
1070 : partial_die_info (sect_off, DW_TAG_padding, 0)
1071 {
1072 }
1073
1074 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1075 int has_children_)
1076 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1077 {
1078 is_external = 0;
1079 is_declaration = 0;
1080 has_type = 0;
1081 has_specification = 0;
1082 has_pc_info = 0;
1083 may_be_inlined = 0;
1084 main_subprogram = 0;
1085 scope_set = 0;
1086 has_byte_size = 0;
1087 has_const_value = 0;
1088 has_template_arguments = 0;
1089 fixup_called = 0;
1090 is_dwz = 0;
1091 spec_is_dwz = 0;
1092 canonical_name = 0;
1093 }
1094 };
1095
1096 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1097 but this would require a corresponding change in unpack_field_as_long
1098 and friends. */
1099 static int bits_per_byte = 8;
1100
1101 struct variant_part_builder;
1102
1103 /* When reading a variant, we track a bit more information about the
1104 field, and store it in an object of this type. */
1105
1106 struct variant_field
1107 {
1108 int first_field = -1;
1109 int last_field = -1;
1110
1111 /* A variant can contain other variant parts. */
1112 std::vector<variant_part_builder> variant_parts;
1113
1114 /* If we see a DW_TAG_variant, then this will be set if this is the
1115 default branch. */
1116 bool default_branch = false;
1117 /* If we see a DW_AT_discr_value, then this will be the discriminant
1118 value. */
1119 ULONGEST discriminant_value = 0;
1120 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1121 data. */
1122 struct dwarf_block *discr_list_data = nullptr;
1123 };
1124
1125 /* This represents a DW_TAG_variant_part. */
1126
1127 struct variant_part_builder
1128 {
1129 /* The offset of the discriminant field. */
1130 sect_offset discriminant_offset {};
1131
1132 /* Variants that are direct children of this variant part. */
1133 std::vector<variant_field> variants;
1134
1135 /* True if we're currently reading a variant. */
1136 bool processing_variant = false;
1137 };
1138
1139 struct nextfield
1140 {
1141 int accessibility = 0;
1142 int virtuality = 0;
1143 /* Variant parts need to find the discriminant, which is a DIE
1144 reference. We track the section offset of each field to make
1145 this link. */
1146 sect_offset offset;
1147 struct field field {};
1148 };
1149
1150 struct fnfieldlist
1151 {
1152 const char *name = nullptr;
1153 std::vector<struct fn_field> fnfields;
1154 };
1155
1156 /* The routines that read and process dies for a C struct or C++ class
1157 pass lists of data member fields and lists of member function fields
1158 in an instance of a field_info structure, as defined below. */
1159 struct field_info
1160 {
1161 /* List of data member and baseclasses fields. */
1162 std::vector<struct nextfield> fields;
1163 std::vector<struct nextfield> baseclasses;
1164
1165 /* Set if the accessibility of one of the fields is not public. */
1166 int non_public_fields = 0;
1167
1168 /* Member function fieldlist array, contains name of possibly overloaded
1169 member function, number of overloaded member functions and a pointer
1170 to the head of the member function field chain. */
1171 std::vector<struct fnfieldlist> fnfieldlists;
1172
1173 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1174 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1175 std::vector<struct decl_field> typedef_field_list;
1176
1177 /* Nested types defined by this class and the number of elements in this
1178 list. */
1179 std::vector<struct decl_field> nested_types_list;
1180
1181 /* If non-null, this is the variant part we are currently
1182 reading. */
1183 variant_part_builder *current_variant_part = nullptr;
1184 /* This holds all the top-level variant parts attached to the type
1185 we're reading. */
1186 std::vector<variant_part_builder> variant_parts;
1187
1188 /* Return the total number of fields (including baseclasses). */
1189 int nfields () const
1190 {
1191 return fields.size () + baseclasses.size ();
1192 }
1193 };
1194
1195 /* Loaded secondary compilation units are kept in memory until they
1196 have not been referenced for the processing of this many
1197 compilation units. Set this to zero to disable caching. Cache
1198 sizes of up to at least twenty will improve startup time for
1199 typical inter-CU-reference binaries, at an obvious memory cost. */
1200 static int dwarf_max_cache_age = 5;
1201 static void
1202 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1203 struct cmd_list_element *c, const char *value)
1204 {
1205 fprintf_filtered (file, _("The upper bound on the age of cached "
1206 "DWARF compilation units is %s.\n"),
1207 value);
1208 }
1209 \f
1210 /* local function prototypes */
1211
1212 static void dwarf2_find_base_address (struct die_info *die,
1213 struct dwarf2_cu *cu);
1214
1215 static dwarf2_psymtab *create_partial_symtab
1216 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1217 const char *name);
1218
1219 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1220 const gdb_byte *info_ptr,
1221 struct die_info *type_unit_die);
1222
1223 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1224
1225 static void scan_partial_symbols (struct partial_die_info *,
1226 CORE_ADDR *, CORE_ADDR *,
1227 int, struct dwarf2_cu *);
1228
1229 static void add_partial_symbol (struct partial_die_info *,
1230 struct dwarf2_cu *);
1231
1232 static void add_partial_namespace (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1234 int set_addrmap, struct dwarf2_cu *cu);
1235
1236 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1237 CORE_ADDR *highpc, int set_addrmap,
1238 struct dwarf2_cu *cu);
1239
1240 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1241 struct dwarf2_cu *cu);
1242
1243 static void add_partial_subprogram (struct partial_die_info *pdi,
1244 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1245 int need_pc, struct dwarf2_cu *cu);
1246
1247 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1248
1249 static struct partial_die_info *load_partial_dies
1250 (const struct die_reader_specs *, const gdb_byte *, int);
1251
1252 /* A pair of partial_die_info and compilation unit. */
1253 struct cu_partial_die_info
1254 {
1255 /* The compilation unit of the partial_die_info. */
1256 struct dwarf2_cu *cu;
1257 /* A partial_die_info. */
1258 struct partial_die_info *pdi;
1259
1260 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1261 : cu (cu),
1262 pdi (pdi)
1263 { /* Nothing. */ }
1264
1265 private:
1266 cu_partial_die_info () = delete;
1267 };
1268
1269 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1270 struct dwarf2_cu *);
1271
1272 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1273 struct attribute *, struct attr_abbrev *,
1274 const gdb_byte *, bool *need_reprocess);
1275
1276 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1277 struct attribute *attr);
1278
1279 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1280
1281 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1282 dwarf2_section_info *, sect_offset);
1283
1284 static const char *read_indirect_string
1285 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1286 const struct comp_unit_head *, unsigned int *);
1287
1288 static const char *read_indirect_string_at_offset
1289 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1290
1291 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1292 const gdb_byte *,
1293 unsigned int *);
1294
1295 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1296 ULONGEST str_index);
1297
1298 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1299 ULONGEST str_index);
1300
1301 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1302
1303 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1304 struct dwarf2_cu *);
1305
1306 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1307 struct dwarf2_cu *cu);
1308
1309 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1310
1311 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1312 struct dwarf2_cu *cu);
1313
1314 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1315
1316 static struct die_info *die_specification (struct die_info *die,
1317 struct dwarf2_cu **);
1318
1319 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1320 struct dwarf2_cu *cu);
1321
1322 static void dwarf_decode_lines (struct line_header *, const char *,
1323 struct dwarf2_cu *, dwarf2_psymtab *,
1324 CORE_ADDR, int decode_mapping);
1325
1326 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1327 const char *);
1328
1329 static struct symbol *new_symbol (struct die_info *, struct type *,
1330 struct dwarf2_cu *, struct symbol * = NULL);
1331
1332 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1333 struct dwarf2_cu *);
1334
1335 static void dwarf2_const_value_attr (const struct attribute *attr,
1336 struct type *type,
1337 const char *name,
1338 struct obstack *obstack,
1339 struct dwarf2_cu *cu, LONGEST *value,
1340 const gdb_byte **bytes,
1341 struct dwarf2_locexpr_baton **baton);
1342
1343 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1344
1345 static int need_gnat_info (struct dwarf2_cu *);
1346
1347 static struct type *die_descriptive_type (struct die_info *,
1348 struct dwarf2_cu *);
1349
1350 static void set_descriptive_type (struct type *, struct die_info *,
1351 struct dwarf2_cu *);
1352
1353 static struct type *die_containing_type (struct die_info *,
1354 struct dwarf2_cu *);
1355
1356 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1357 struct dwarf2_cu *);
1358
1359 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1360
1361 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1362
1363 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1364
1365 static char *typename_concat (struct obstack *obs, const char *prefix,
1366 const char *suffix, int physname,
1367 struct dwarf2_cu *cu);
1368
1369 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1370
1371 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1372
1373 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1374
1375 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1376
1377 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1378
1379 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1380
1381 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1382 struct dwarf2_cu *, dwarf2_psymtab *);
1383
1384 /* Return the .debug_loclists section to use for cu. */
1385 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1386
1387 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1388 values. Keep the items ordered with increasing constraints compliance. */
1389 enum pc_bounds_kind
1390 {
1391 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1392 PC_BOUNDS_NOT_PRESENT,
1393
1394 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1395 were present but they do not form a valid range of PC addresses. */
1396 PC_BOUNDS_INVALID,
1397
1398 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1399 PC_BOUNDS_RANGES,
1400
1401 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1402 PC_BOUNDS_HIGH_LOW,
1403 };
1404
1405 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1406 CORE_ADDR *, CORE_ADDR *,
1407 struct dwarf2_cu *,
1408 dwarf2_psymtab *);
1409
1410 static void get_scope_pc_bounds (struct die_info *,
1411 CORE_ADDR *, CORE_ADDR *,
1412 struct dwarf2_cu *);
1413
1414 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1415 CORE_ADDR, struct dwarf2_cu *);
1416
1417 static void dwarf2_add_field (struct field_info *, struct die_info *,
1418 struct dwarf2_cu *);
1419
1420 static void dwarf2_attach_fields_to_type (struct field_info *,
1421 struct type *, struct dwarf2_cu *);
1422
1423 static void dwarf2_add_member_fn (struct field_info *,
1424 struct die_info *, struct type *,
1425 struct dwarf2_cu *);
1426
1427 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1428 struct type *,
1429 struct dwarf2_cu *);
1430
1431 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1432
1433 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1434
1435 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1436
1437 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1438
1439 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1440
1441 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1442
1443 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1444
1445 static struct type *read_module_type (struct die_info *die,
1446 struct dwarf2_cu *cu);
1447
1448 static const char *namespace_name (struct die_info *die,
1449 int *is_anonymous, struct dwarf2_cu *);
1450
1451 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1452
1453 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1454 bool * = nullptr);
1455
1456 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1457 struct dwarf2_cu *);
1458
1459 static struct die_info *read_die_and_siblings_1
1460 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1461 struct die_info *);
1462
1463 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1464 const gdb_byte *info_ptr,
1465 const gdb_byte **new_info_ptr,
1466 struct die_info *parent);
1467
1468 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1469 struct die_info **, const gdb_byte *,
1470 int);
1471
1472 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1473 struct die_info **, const gdb_byte *);
1474
1475 static void process_die (struct die_info *, struct dwarf2_cu *);
1476
1477 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1478 struct objfile *);
1479
1480 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1481
1482 static const char *dwarf2_full_name (const char *name,
1483 struct die_info *die,
1484 struct dwarf2_cu *cu);
1485
1486 static const char *dwarf2_physname (const char *name, struct die_info *die,
1487 struct dwarf2_cu *cu);
1488
1489 static struct die_info *dwarf2_extension (struct die_info *die,
1490 struct dwarf2_cu **);
1491
1492 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1493
1494 static void dump_die_for_error (struct die_info *);
1495
1496 static void dump_die_1 (struct ui_file *, int level, int max_level,
1497 struct die_info *);
1498
1499 /*static*/ void dump_die (struct die_info *, int max_level);
1500
1501 static void store_in_ref_table (struct die_info *,
1502 struct dwarf2_cu *);
1503
1504 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1505 const struct attribute *,
1506 struct dwarf2_cu **);
1507
1508 static struct die_info *follow_die_ref (struct die_info *,
1509 const struct attribute *,
1510 struct dwarf2_cu **);
1511
1512 static struct die_info *follow_die_sig (struct die_info *,
1513 const struct attribute *,
1514 struct dwarf2_cu **);
1515
1516 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1517 struct dwarf2_cu *);
1518
1519 static struct type *get_DW_AT_signature_type (struct die_info *,
1520 const struct attribute *,
1521 struct dwarf2_cu *);
1522
1523 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1524 dwarf2_per_objfile *per_objfile);
1525
1526 static void read_signatured_type (signatured_type *sig_type,
1527 dwarf2_per_objfile *per_objfile);
1528
1529 static int attr_to_dynamic_prop (const struct attribute *attr,
1530 struct die_info *die, struct dwarf2_cu *cu,
1531 struct dynamic_prop *prop, struct type *type);
1532
1533 /* memory allocation interface */
1534
1535 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1536
1537 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1538
1539 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1540
1541 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1542 struct dwarf2_loclist_baton *baton,
1543 const struct attribute *attr);
1544
1545 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1546 struct symbol *sym,
1547 struct dwarf2_cu *cu,
1548 int is_block);
1549
1550 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1551 const gdb_byte *info_ptr,
1552 struct abbrev_info *abbrev);
1553
1554 static hashval_t partial_die_hash (const void *item);
1555
1556 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1557
1558 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1559 (sect_offset sect_off, unsigned int offset_in_dwz,
1560 dwarf2_per_objfile *per_objfile);
1561
1562 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1563 struct die_info *comp_unit_die,
1564 enum language pretend_language);
1565
1566 static struct type *set_die_type (struct die_info *, struct type *,
1567 struct dwarf2_cu *);
1568
1569 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1570
1571 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1572
1573 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile,
1575 bool skip_partial,
1576 enum language pretend_language);
1577
1578 static void process_full_comp_unit (dwarf2_cu *cu,
1579 enum language pretend_language);
1580
1581 static void process_full_type_unit (dwarf2_cu *cu,
1582 enum language pretend_language);
1583
1584 static void dwarf2_add_dependence (struct dwarf2_cu *,
1585 struct dwarf2_per_cu_data *);
1586
1587 static void dwarf2_mark (struct dwarf2_cu *);
1588
1589 static struct type *get_die_type_at_offset (sect_offset,
1590 dwarf2_per_cu_data *per_cu,
1591 dwarf2_per_objfile *per_objfile);
1592
1593 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1594
1595 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1596 dwarf2_per_objfile *per_objfile,
1597 enum language pretend_language);
1598
1599 static void process_queue (dwarf2_per_objfile *per_objfile);
1600
1601 /* Class, the destructor of which frees all allocated queue entries. This
1602 will only have work to do if an error was thrown while processing the
1603 dwarf. If no error was thrown then the queue entries should have all
1604 been processed, and freed, as we went along. */
1605
1606 class dwarf2_queue_guard
1607 {
1608 public:
1609 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1610 : m_per_objfile (per_objfile)
1611 {
1612 }
1613
1614 /* Free any entries remaining on the queue. There should only be
1615 entries left if we hit an error while processing the dwarf. */
1616 ~dwarf2_queue_guard ()
1617 {
1618 /* Ensure that no memory is allocated by the queue. */
1619 std::queue<dwarf2_queue_item> empty;
1620 std::swap (m_per_objfile->per_bfd->queue, empty);
1621 }
1622
1623 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1624
1625 private:
1626 dwarf2_per_objfile *m_per_objfile;
1627 };
1628
1629 dwarf2_queue_item::~dwarf2_queue_item ()
1630 {
1631 /* Anything still marked queued is likely to be in an
1632 inconsistent state, so discard it. */
1633 if (per_cu->queued)
1634 {
1635 per_objfile->remove_cu (per_cu);
1636 per_cu->queued = 0;
1637 }
1638 }
1639
1640 /* The return type of find_file_and_directory. Note, the enclosed
1641 string pointers are only valid while this object is valid. */
1642
1643 struct file_and_directory
1644 {
1645 /* The filename. This is never NULL. */
1646 const char *name;
1647
1648 /* The compilation directory. NULL if not known. If we needed to
1649 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1650 points directly to the DW_AT_comp_dir string attribute owned by
1651 the obstack that owns the DIE. */
1652 const char *comp_dir;
1653
1654 /* If we needed to build a new string for comp_dir, this is what
1655 owns the storage. */
1656 std::string comp_dir_storage;
1657 };
1658
1659 static file_and_directory find_file_and_directory (struct die_info *die,
1660 struct dwarf2_cu *cu);
1661
1662 static htab_up allocate_signatured_type_table ();
1663
1664 static htab_up allocate_dwo_unit_table ();
1665
1666 static struct dwo_unit *lookup_dwo_unit_in_dwp
1667 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1668 const char *comp_dir, ULONGEST signature, int is_debug_types);
1669
1670 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1671
1672 static struct dwo_unit *lookup_dwo_comp_unit
1673 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1674 ULONGEST signature);
1675
1676 static struct dwo_unit *lookup_dwo_type_unit
1677 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1678
1679 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1680
1681 /* A unique pointer to a dwo_file. */
1682
1683 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1684
1685 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1686
1687 static void check_producer (struct dwarf2_cu *cu);
1688
1689 static void free_line_header_voidp (void *arg);
1690 \f
1691 /* Various complaints about symbol reading that don't abort the process. */
1692
1693 static void
1694 dwarf2_debug_line_missing_file_complaint (void)
1695 {
1696 complaint (_(".debug_line section has line data without a file"));
1697 }
1698
1699 static void
1700 dwarf2_debug_line_missing_end_sequence_complaint (void)
1701 {
1702 complaint (_(".debug_line section has line "
1703 "program sequence without an end"));
1704 }
1705
1706 static void
1707 dwarf2_complex_location_expr_complaint (void)
1708 {
1709 complaint (_("location expression too complex"));
1710 }
1711
1712 static void
1713 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1714 int arg3)
1715 {
1716 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1717 arg1, arg2, arg3);
1718 }
1719
1720 static void
1721 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1722 {
1723 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1724 arg1, arg2);
1725 }
1726
1727 /* Hash function for line_header_hash. */
1728
1729 static hashval_t
1730 line_header_hash (const struct line_header *ofs)
1731 {
1732 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1733 }
1734
1735 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1736
1737 static hashval_t
1738 line_header_hash_voidp (const void *item)
1739 {
1740 const struct line_header *ofs = (const struct line_header *) item;
1741
1742 return line_header_hash (ofs);
1743 }
1744
1745 /* Equality function for line_header_hash. */
1746
1747 static int
1748 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1749 {
1750 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1751 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1752
1753 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1754 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1755 }
1756
1757 \f
1758
1759 /* See declaration. */
1760
1761 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1762 bool can_copy_)
1763 : obfd (obfd),
1764 can_copy (can_copy_)
1765 {
1766 if (names == NULL)
1767 names = &dwarf2_elf_names;
1768
1769 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1770 locate_sections (obfd, sec, *names);
1771 }
1772
1773 dwarf2_per_bfd::~dwarf2_per_bfd ()
1774 {
1775 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1776 per_cu->imported_symtabs_free ();
1777
1778 for (signatured_type *sig_type : all_type_units)
1779 sig_type->per_cu.imported_symtabs_free ();
1780
1781 /* Everything else should be on this->obstack. */
1782 }
1783
1784 /* See read.h. */
1785
1786 void
1787 dwarf2_per_objfile::remove_all_cus ()
1788 {
1789 for (auto pair : m_dwarf2_cus)
1790 delete pair.second;
1791
1792 m_dwarf2_cus.clear ();
1793 }
1794
1795 /* A helper class that calls free_cached_comp_units on
1796 destruction. */
1797
1798 class free_cached_comp_units
1799 {
1800 public:
1801
1802 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1803 : m_per_objfile (per_objfile)
1804 {
1805 }
1806
1807 ~free_cached_comp_units ()
1808 {
1809 m_per_objfile->remove_all_cus ();
1810 }
1811
1812 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1813
1814 private:
1815
1816 dwarf2_per_objfile *m_per_objfile;
1817 };
1818
1819 /* See read.h. */
1820
1821 bool
1822 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1823 {
1824 gdb_assert (per_cu->index < this->m_symtabs.size ());
1825
1826 return this->m_symtabs[per_cu->index] != nullptr;
1827 }
1828
1829 /* See read.h. */
1830
1831 compunit_symtab *
1832 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1833 {
1834 gdb_assert (per_cu->index < this->m_symtabs.size ());
1835
1836 return this->m_symtabs[per_cu->index];
1837 }
1838
1839 /* See read.h. */
1840
1841 void
1842 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1843 compunit_symtab *symtab)
1844 {
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1847
1848 this->m_symtabs[per_cu->index] = symtab;
1849 }
1850
1851 /* Try to locate the sections we need for DWARF 2 debugging
1852 information and return true if we have enough to do something.
1853 NAMES points to the dwarf2 section names, or is NULL if the standard
1854 ELF names are used. CAN_COPY is true for formats where symbol
1855 interposition is possible and so symbol values must follow copy
1856 relocation rules. */
1857
1858 int
1859 dwarf2_has_info (struct objfile *objfile,
1860 const struct dwarf2_debug_sections *names,
1861 bool can_copy)
1862 {
1863 if (objfile->flags & OBJF_READNEVER)
1864 return 0;
1865
1866 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1867
1868 if (per_objfile == NULL)
1869 {
1870 dwarf2_per_bfd *per_bfd;
1871
1872 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1873 doesn't require relocations and if there aren't partial symbols
1874 from some other reader. */
1875 if (!objfile_has_partial_symbols (objfile)
1876 && !gdb_bfd_requires_relocations (objfile->obfd))
1877 {
1878 /* See if one has been created for this BFD yet. */
1879 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1880
1881 if (per_bfd == nullptr)
1882 {
1883 /* No, create it now. */
1884 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1885 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1886 }
1887 }
1888 else
1889 {
1890 /* No sharing possible, create one specifically for this objfile. */
1891 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1892 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1893 }
1894
1895 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1896 }
1897
1898 return (!per_objfile->per_bfd->info.is_virtual
1899 && per_objfile->per_bfd->info.s.section != NULL
1900 && !per_objfile->per_bfd->abbrev.is_virtual
1901 && per_objfile->per_bfd->abbrev.s.section != NULL);
1902 }
1903
1904 /* When loading sections, we look either for uncompressed section or for
1905 compressed section names. */
1906
1907 static int
1908 section_is_p (const char *section_name,
1909 const struct dwarf2_section_names *names)
1910 {
1911 if (names->normal != NULL
1912 && strcmp (section_name, names->normal) == 0)
1913 return 1;
1914 if (names->compressed != NULL
1915 && strcmp (section_name, names->compressed) == 0)
1916 return 1;
1917 return 0;
1918 }
1919
1920 /* See declaration. */
1921
1922 void
1923 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1924 const dwarf2_debug_sections &names)
1925 {
1926 flagword aflag = bfd_section_flags (sectp);
1927
1928 if ((aflag & SEC_HAS_CONTENTS) == 0)
1929 {
1930 }
1931 else if (elf_section_data (sectp)->this_hdr.sh_size
1932 > bfd_get_file_size (abfd))
1933 {
1934 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1935 warning (_("Discarding section %s which has a section size (%s"
1936 ") larger than the file size [in module %s]"),
1937 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1938 bfd_get_filename (abfd));
1939 }
1940 else if (section_is_p (sectp->name, &names.info))
1941 {
1942 this->info.s.section = sectp;
1943 this->info.size = bfd_section_size (sectp);
1944 }
1945 else if (section_is_p (sectp->name, &names.abbrev))
1946 {
1947 this->abbrev.s.section = sectp;
1948 this->abbrev.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.line))
1951 {
1952 this->line.s.section = sectp;
1953 this->line.size = bfd_section_size (sectp);
1954 }
1955 else if (section_is_p (sectp->name, &names.loc))
1956 {
1957 this->loc.s.section = sectp;
1958 this->loc.size = bfd_section_size (sectp);
1959 }
1960 else if (section_is_p (sectp->name, &names.loclists))
1961 {
1962 this->loclists.s.section = sectp;
1963 this->loclists.size = bfd_section_size (sectp);
1964 }
1965 else if (section_is_p (sectp->name, &names.macinfo))
1966 {
1967 this->macinfo.s.section = sectp;
1968 this->macinfo.size = bfd_section_size (sectp);
1969 }
1970 else if (section_is_p (sectp->name, &names.macro))
1971 {
1972 this->macro.s.section = sectp;
1973 this->macro.size = bfd_section_size (sectp);
1974 }
1975 else if (section_is_p (sectp->name, &names.str))
1976 {
1977 this->str.s.section = sectp;
1978 this->str.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.str_offsets))
1981 {
1982 this->str_offsets.s.section = sectp;
1983 this->str_offsets.size = bfd_section_size (sectp);
1984 }
1985 else if (section_is_p (sectp->name, &names.line_str))
1986 {
1987 this->line_str.s.section = sectp;
1988 this->line_str.size = bfd_section_size (sectp);
1989 }
1990 else if (section_is_p (sectp->name, &names.addr))
1991 {
1992 this->addr.s.section = sectp;
1993 this->addr.size = bfd_section_size (sectp);
1994 }
1995 else if (section_is_p (sectp->name, &names.frame))
1996 {
1997 this->frame.s.section = sectp;
1998 this->frame.size = bfd_section_size (sectp);
1999 }
2000 else if (section_is_p (sectp->name, &names.eh_frame))
2001 {
2002 this->eh_frame.s.section = sectp;
2003 this->eh_frame.size = bfd_section_size (sectp);
2004 }
2005 else if (section_is_p (sectp->name, &names.ranges))
2006 {
2007 this->ranges.s.section = sectp;
2008 this->ranges.size = bfd_section_size (sectp);
2009 }
2010 else if (section_is_p (sectp->name, &names.rnglists))
2011 {
2012 this->rnglists.s.section = sectp;
2013 this->rnglists.size = bfd_section_size (sectp);
2014 }
2015 else if (section_is_p (sectp->name, &names.types))
2016 {
2017 struct dwarf2_section_info type_section;
2018
2019 memset (&type_section, 0, sizeof (type_section));
2020 type_section.s.section = sectp;
2021 type_section.size = bfd_section_size (sectp);
2022
2023 this->types.push_back (type_section);
2024 }
2025 else if (section_is_p (sectp->name, &names.gdb_index))
2026 {
2027 this->gdb_index.s.section = sectp;
2028 this->gdb_index.size = bfd_section_size (sectp);
2029 }
2030 else if (section_is_p (sectp->name, &names.debug_names))
2031 {
2032 this->debug_names.s.section = sectp;
2033 this->debug_names.size = bfd_section_size (sectp);
2034 }
2035 else if (section_is_p (sectp->name, &names.debug_aranges))
2036 {
2037 this->debug_aranges.s.section = sectp;
2038 this->debug_aranges.size = bfd_section_size (sectp);
2039 }
2040
2041 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2042 && bfd_section_vma (sectp) == 0)
2043 this->has_section_at_zero = true;
2044 }
2045
2046 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2047 SECTION_NAME. */
2048
2049 void
2050 dwarf2_get_section_info (struct objfile *objfile,
2051 enum dwarf2_section_enum sect,
2052 asection **sectp, const gdb_byte **bufp,
2053 bfd_size_type *sizep)
2054 {
2055 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2056 struct dwarf2_section_info *info;
2057
2058 /* We may see an objfile without any DWARF, in which case we just
2059 return nothing. */
2060 if (per_objfile == NULL)
2061 {
2062 *sectp = NULL;
2063 *bufp = NULL;
2064 *sizep = 0;
2065 return;
2066 }
2067 switch (sect)
2068 {
2069 case DWARF2_DEBUG_FRAME:
2070 info = &per_objfile->per_bfd->frame;
2071 break;
2072 case DWARF2_EH_FRAME:
2073 info = &per_objfile->per_bfd->eh_frame;
2074 break;
2075 default:
2076 gdb_assert_not_reached ("unexpected section");
2077 }
2078
2079 info->read (objfile);
2080
2081 *sectp = info->get_bfd_section ();
2082 *bufp = info->buffer;
2083 *sizep = info->size;
2084 }
2085
2086 /* A helper function to find the sections for a .dwz file. */
2087
2088 static void
2089 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2090 {
2091 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2092
2093 /* Note that we only support the standard ELF names, because .dwz
2094 is ELF-only (at the time of writing). */
2095 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2096 {
2097 dwz_file->abbrev.s.section = sectp;
2098 dwz_file->abbrev.size = bfd_section_size (sectp);
2099 }
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2101 {
2102 dwz_file->info.s.section = sectp;
2103 dwz_file->info.size = bfd_section_size (sectp);
2104 }
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2106 {
2107 dwz_file->str.s.section = sectp;
2108 dwz_file->str.size = bfd_section_size (sectp);
2109 }
2110 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2111 {
2112 dwz_file->line.s.section = sectp;
2113 dwz_file->line.size = bfd_section_size (sectp);
2114 }
2115 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2116 {
2117 dwz_file->macro.s.section = sectp;
2118 dwz_file->macro.size = bfd_section_size (sectp);
2119 }
2120 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2121 {
2122 dwz_file->gdb_index.s.section = sectp;
2123 dwz_file->gdb_index.size = bfd_section_size (sectp);
2124 }
2125 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2126 {
2127 dwz_file->debug_names.s.section = sectp;
2128 dwz_file->debug_names.size = bfd_section_size (sectp);
2129 }
2130 }
2131
2132 /* See dwarf2read.h. */
2133
2134 struct dwz_file *
2135 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2136 {
2137 const char *filename;
2138 bfd_size_type buildid_len_arg;
2139 size_t buildid_len;
2140 bfd_byte *buildid;
2141
2142 if (per_bfd->dwz_file != NULL)
2143 return per_bfd->dwz_file.get ();
2144
2145 bfd_set_error (bfd_error_no_error);
2146 gdb::unique_xmalloc_ptr<char> data
2147 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2148 &buildid_len_arg, &buildid));
2149 if (data == NULL)
2150 {
2151 if (bfd_get_error () == bfd_error_no_error)
2152 return NULL;
2153 error (_("could not read '.gnu_debugaltlink' section: %s"),
2154 bfd_errmsg (bfd_get_error ()));
2155 }
2156
2157 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2158
2159 buildid_len = (size_t) buildid_len_arg;
2160
2161 filename = data.get ();
2162
2163 std::string abs_storage;
2164 if (!IS_ABSOLUTE_PATH (filename))
2165 {
2166 gdb::unique_xmalloc_ptr<char> abs
2167 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2168
2169 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2170 filename = abs_storage.c_str ();
2171 }
2172
2173 /* First try the file name given in the section. If that doesn't
2174 work, try to use the build-id instead. */
2175 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2176 if (dwz_bfd != NULL)
2177 {
2178 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2179 dwz_bfd.reset (nullptr);
2180 }
2181
2182 if (dwz_bfd == NULL)
2183 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2184
2185 if (dwz_bfd == nullptr)
2186 {
2187 gdb::unique_xmalloc_ptr<char> alt_filename;
2188 const char *origname = bfd_get_filename (per_bfd->obfd);
2189
2190 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2191 buildid_len,
2192 origname,
2193 &alt_filename));
2194
2195 if (fd.get () >= 0)
2196 {
2197 /* File successfully retrieved from server. */
2198 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2199
2200 if (dwz_bfd == nullptr)
2201 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2202 alt_filename.get ());
2203 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2204 dwz_bfd.reset (nullptr);
2205 }
2206 }
2207
2208 if (dwz_bfd == NULL)
2209 error (_("could not find '.gnu_debugaltlink' file for %s"),
2210 bfd_get_filename (per_bfd->obfd));
2211
2212 std::unique_ptr<struct dwz_file> result
2213 (new struct dwz_file (std::move (dwz_bfd)));
2214
2215 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2216 result.get ());
2217
2218 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2219 per_bfd->dwz_file = std::move (result);
2220 return per_bfd->dwz_file.get ();
2221 }
2222 \f
2223 /* DWARF quick_symbols_functions support. */
2224
2225 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2226 unique line tables, so we maintain a separate table of all .debug_line
2227 derived entries to support the sharing.
2228 All the quick functions need is the list of file names. We discard the
2229 line_header when we're done and don't need to record it here. */
2230 struct quick_file_names
2231 {
2232 /* The data used to construct the hash key. */
2233 struct stmt_list_hash hash;
2234
2235 /* The number of entries in file_names, real_names. */
2236 unsigned int num_file_names;
2237
2238 /* The file names from the line table, after being run through
2239 file_full_name. */
2240 const char **file_names;
2241
2242 /* The file names from the line table after being run through
2243 gdb_realpath. These are computed lazily. */
2244 const char **real_names;
2245 };
2246
2247 /* When using the index (and thus not using psymtabs), each CU has an
2248 object of this type. This is used to hold information needed by
2249 the various "quick" methods. */
2250 struct dwarf2_per_cu_quick_data
2251 {
2252 /* The file table. This can be NULL if there was no file table
2253 or it's currently not read in.
2254 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2255 struct quick_file_names *file_names;
2256
2257 /* A temporary mark bit used when iterating over all CUs in
2258 expand_symtabs_matching. */
2259 unsigned int mark : 1;
2260
2261 /* True if we've tried to read the file table and found there isn't one.
2262 There will be no point in trying to read it again next time. */
2263 unsigned int no_file_data : 1;
2264 };
2265
2266 /* Utility hash function for a stmt_list_hash. */
2267
2268 static hashval_t
2269 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2270 {
2271 hashval_t v = 0;
2272
2273 if (stmt_list_hash->dwo_unit != NULL)
2274 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2275 v += to_underlying (stmt_list_hash->line_sect_off);
2276 return v;
2277 }
2278
2279 /* Utility equality function for a stmt_list_hash. */
2280
2281 static int
2282 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2283 const struct stmt_list_hash *rhs)
2284 {
2285 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2286 return 0;
2287 if (lhs->dwo_unit != NULL
2288 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2289 return 0;
2290
2291 return lhs->line_sect_off == rhs->line_sect_off;
2292 }
2293
2294 /* Hash function for a quick_file_names. */
2295
2296 static hashval_t
2297 hash_file_name_entry (const void *e)
2298 {
2299 const struct quick_file_names *file_data
2300 = (const struct quick_file_names *) e;
2301
2302 return hash_stmt_list_entry (&file_data->hash);
2303 }
2304
2305 /* Equality function for a quick_file_names. */
2306
2307 static int
2308 eq_file_name_entry (const void *a, const void *b)
2309 {
2310 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2311 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2312
2313 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2314 }
2315
2316 /* Delete function for a quick_file_names. */
2317
2318 static void
2319 delete_file_name_entry (void *e)
2320 {
2321 struct quick_file_names *file_data = (struct quick_file_names *) e;
2322 int i;
2323
2324 for (i = 0; i < file_data->num_file_names; ++i)
2325 {
2326 xfree ((void*) file_data->file_names[i]);
2327 if (file_data->real_names)
2328 xfree ((void*) file_data->real_names[i]);
2329 }
2330
2331 /* The space for the struct itself lives on the obstack, so we don't
2332 free it here. */
2333 }
2334
2335 /* Create a quick_file_names hash table. */
2336
2337 static htab_up
2338 create_quick_file_names_table (unsigned int nr_initial_entries)
2339 {
2340 return htab_up (htab_create_alloc (nr_initial_entries,
2341 hash_file_name_entry, eq_file_name_entry,
2342 delete_file_name_entry, xcalloc, xfree));
2343 }
2344
2345 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2346 function is unrelated to symtabs, symtab would have to be created afterwards.
2347 You should call age_cached_comp_units after processing the CU. */
2348
2349 static dwarf2_cu *
2350 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2351 bool skip_partial)
2352 {
2353 if (per_cu->is_debug_types)
2354 load_full_type_unit (per_cu, per_objfile);
2355 else
2356 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2357
2358 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2359 if (cu == nullptr)
2360 return nullptr; /* Dummy CU. */
2361
2362 dwarf2_find_base_address (cu->dies, cu);
2363
2364 return cu;
2365 }
2366
2367 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2368
2369 static void
2370 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2371 dwarf2_per_objfile *per_objfile, bool skip_partial)
2372 {
2373 /* Skip type_unit_groups, reading the type units they contain
2374 is handled elsewhere. */
2375 if (per_cu->type_unit_group_p ())
2376 return;
2377
2378 /* The destructor of dwarf2_queue_guard frees any entries left on
2379 the queue. After this point we're guaranteed to leave this function
2380 with the dwarf queue empty. */
2381 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2382
2383 if (!per_objfile->symtab_set_p (per_cu))
2384 {
2385 queue_comp_unit (per_cu, per_objfile, language_minimal);
2386 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2387
2388 /* If we just loaded a CU from a DWO, and we're working with an index
2389 that may badly handle TUs, load all the TUs in that DWO as well.
2390 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2391 if (!per_cu->is_debug_types
2392 && cu != NULL
2393 && cu->dwo_unit != NULL
2394 && per_objfile->per_bfd->index_table != NULL
2395 && per_objfile->per_bfd->index_table->version <= 7
2396 /* DWP files aren't supported yet. */
2397 && get_dwp_file (per_objfile) == NULL)
2398 queue_and_load_all_dwo_tus (cu);
2399 }
2400
2401 process_queue (per_objfile);
2402
2403 /* Age the cache, releasing compilation units that have not
2404 been used recently. */
2405 per_objfile->age_comp_units ();
2406 }
2407
2408 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2409 the per-objfile for which this symtab is instantiated.
2410
2411 Returns the resulting symbol table. */
2412
2413 static struct compunit_symtab *
2414 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2415 dwarf2_per_objfile *per_objfile,
2416 bool skip_partial)
2417 {
2418 gdb_assert (per_objfile->per_bfd->using_index);
2419
2420 if (!per_objfile->symtab_set_p (per_cu))
2421 {
2422 free_cached_comp_units freer (per_objfile);
2423 scoped_restore decrementer = increment_reading_symtab ();
2424 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2425 process_cu_includes (per_objfile);
2426 }
2427
2428 return per_objfile->get_symtab (per_cu);
2429 }
2430
2431 /* See declaration. */
2432
2433 dwarf2_per_cu_data *
2434 dwarf2_per_bfd::get_cutu (int index)
2435 {
2436 if (index >= this->all_comp_units.size ())
2437 {
2438 index -= this->all_comp_units.size ();
2439 gdb_assert (index < this->all_type_units.size ());
2440 return &this->all_type_units[index]->per_cu;
2441 }
2442
2443 return this->all_comp_units[index];
2444 }
2445
2446 /* See declaration. */
2447
2448 dwarf2_per_cu_data *
2449 dwarf2_per_bfd::get_cu (int index)
2450 {
2451 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2452
2453 return this->all_comp_units[index];
2454 }
2455
2456 /* See declaration. */
2457
2458 signatured_type *
2459 dwarf2_per_bfd::get_tu (int index)
2460 {
2461 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2462
2463 return this->all_type_units[index];
2464 }
2465
2466 /* See read.h. */
2467
2468 dwarf2_per_cu_data *
2469 dwarf2_per_bfd::allocate_per_cu ()
2470 {
2471 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2472 result->per_bfd = this;
2473 result->index = m_num_psymtabs++;
2474 return result;
2475 }
2476
2477 /* See read.h. */
2478
2479 signatured_type *
2480 dwarf2_per_bfd::allocate_signatured_type ()
2481 {
2482 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2483 result->per_cu.per_bfd = this;
2484 result->per_cu.index = m_num_psymtabs++;
2485 return result;
2486 }
2487
2488 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2489 obstack, and constructed with the specified field values. */
2490
2491 static dwarf2_per_cu_data *
2492 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2493 struct dwarf2_section_info *section,
2494 int is_dwz,
2495 sect_offset sect_off, ULONGEST length)
2496 {
2497 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2498 the_cu->sect_off = sect_off;
2499 the_cu->length = length;
2500 the_cu->section = section;
2501 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2502 struct dwarf2_per_cu_quick_data);
2503 the_cu->is_dwz = is_dwz;
2504 return the_cu;
2505 }
2506
2507 /* A helper for create_cus_from_index that handles a given list of
2508 CUs. */
2509
2510 static void
2511 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2512 const gdb_byte *cu_list, offset_type n_elements,
2513 struct dwarf2_section_info *section,
2514 int is_dwz)
2515 {
2516 for (offset_type i = 0; i < n_elements; i += 2)
2517 {
2518 gdb_static_assert (sizeof (ULONGEST) >= 8);
2519
2520 sect_offset sect_off
2521 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2522 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2523 cu_list += 2 * 8;
2524
2525 dwarf2_per_cu_data *per_cu
2526 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2527 length);
2528 per_bfd->all_comp_units.push_back (per_cu);
2529 }
2530 }
2531
2532 /* Read the CU list from the mapped index, and use it to create all
2533 the CU objects for PER_BFD. */
2534
2535 static void
2536 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2537 const gdb_byte *cu_list, offset_type cu_list_elements,
2538 const gdb_byte *dwz_list, offset_type dwz_elements)
2539 {
2540 gdb_assert (per_bfd->all_comp_units.empty ());
2541 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2542
2543 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2544 &per_bfd->info, 0);
2545
2546 if (dwz_elements == 0)
2547 return;
2548
2549 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2550 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2551 &dwz->info, 1);
2552 }
2553
2554 /* Create the signatured type hash table from the index. */
2555
2556 static void
2557 create_signatured_type_table_from_index
2558 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2559 const gdb_byte *bytes, offset_type elements)
2560 {
2561 gdb_assert (per_bfd->all_type_units.empty ());
2562 per_bfd->all_type_units.reserve (elements / 3);
2563
2564 htab_up sig_types_hash = allocate_signatured_type_table ();
2565
2566 for (offset_type i = 0; i < elements; i += 3)
2567 {
2568 struct signatured_type *sig_type;
2569 ULONGEST signature;
2570 void **slot;
2571 cu_offset type_offset_in_tu;
2572
2573 gdb_static_assert (sizeof (ULONGEST) >= 8);
2574 sect_offset sect_off
2575 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2576 type_offset_in_tu
2577 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2578 BFD_ENDIAN_LITTLE);
2579 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2580 bytes += 3 * 8;
2581
2582 sig_type = per_bfd->allocate_signatured_type ();
2583 sig_type->signature = signature;
2584 sig_type->type_offset_in_tu = type_offset_in_tu;
2585 sig_type->per_cu.is_debug_types = 1;
2586 sig_type->per_cu.section = section;
2587 sig_type->per_cu.sect_off = sect_off;
2588 sig_type->per_cu.v.quick
2589 = OBSTACK_ZALLOC (&per_bfd->obstack,
2590 struct dwarf2_per_cu_quick_data);
2591
2592 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2593 *slot = sig_type;
2594
2595 per_bfd->all_type_units.push_back (sig_type);
2596 }
2597
2598 per_bfd->signatured_types = std::move (sig_types_hash);
2599 }
2600
2601 /* Create the signatured type hash table from .debug_names. */
2602
2603 static void
2604 create_signatured_type_table_from_debug_names
2605 (dwarf2_per_objfile *per_objfile,
2606 const mapped_debug_names &map,
2607 struct dwarf2_section_info *section,
2608 struct dwarf2_section_info *abbrev_section)
2609 {
2610 struct objfile *objfile = per_objfile->objfile;
2611
2612 section->read (objfile);
2613 abbrev_section->read (objfile);
2614
2615 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2616 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2617
2618 htab_up sig_types_hash = allocate_signatured_type_table ();
2619
2620 for (uint32_t i = 0; i < map.tu_count; ++i)
2621 {
2622 struct signatured_type *sig_type;
2623 void **slot;
2624
2625 sect_offset sect_off
2626 = (sect_offset) (extract_unsigned_integer
2627 (map.tu_table_reordered + i * map.offset_size,
2628 map.offset_size,
2629 map.dwarf5_byte_order));
2630
2631 comp_unit_head cu_header;
2632 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2633 abbrev_section,
2634 section->buffer + to_underlying (sect_off),
2635 rcuh_kind::TYPE);
2636
2637 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2638 sig_type->signature = cu_header.signature;
2639 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2640 sig_type->per_cu.is_debug_types = 1;
2641 sig_type->per_cu.section = section;
2642 sig_type->per_cu.sect_off = sect_off;
2643 sig_type->per_cu.v.quick
2644 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2645 struct dwarf2_per_cu_quick_data);
2646
2647 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2648 *slot = sig_type;
2649
2650 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2651 }
2652
2653 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2654 }
2655
2656 /* Read the address map data from the mapped index, and use it to
2657 populate the objfile's psymtabs_addrmap. */
2658
2659 static void
2660 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2661 struct mapped_index *index)
2662 {
2663 struct objfile *objfile = per_objfile->objfile;
2664 struct gdbarch *gdbarch = objfile->arch ();
2665 const gdb_byte *iter, *end;
2666 struct addrmap *mutable_map;
2667 CORE_ADDR baseaddr;
2668
2669 auto_obstack temp_obstack;
2670
2671 mutable_map = addrmap_create_mutable (&temp_obstack);
2672
2673 iter = index->address_table.data ();
2674 end = iter + index->address_table.size ();
2675
2676 baseaddr = objfile->text_section_offset ();
2677
2678 while (iter < end)
2679 {
2680 ULONGEST hi, lo, cu_index;
2681 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2682 iter += 8;
2683 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2684 iter += 8;
2685 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2686 iter += 4;
2687
2688 if (lo > hi)
2689 {
2690 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2691 hex_string (lo), hex_string (hi));
2692 continue;
2693 }
2694
2695 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2696 {
2697 complaint (_(".gdb_index address table has invalid CU number %u"),
2698 (unsigned) cu_index);
2699 continue;
2700 }
2701
2702 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2703 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2704 addrmap_set_empty (mutable_map, lo, hi - 1,
2705 per_objfile->per_bfd->get_cu (cu_index));
2706 }
2707
2708 objfile->partial_symtabs->psymtabs_addrmap
2709 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2710 }
2711
2712 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2713 populate the objfile's psymtabs_addrmap. */
2714
2715 static void
2716 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2717 struct dwarf2_section_info *section)
2718 {
2719 struct objfile *objfile = per_objfile->objfile;
2720 bfd *abfd = objfile->obfd;
2721 struct gdbarch *gdbarch = objfile->arch ();
2722 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2723
2724 auto_obstack temp_obstack;
2725 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2726
2727 std::unordered_map<sect_offset,
2728 dwarf2_per_cu_data *,
2729 gdb::hash_enum<sect_offset>>
2730 debug_info_offset_to_per_cu;
2731 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2732 {
2733 const auto insertpair
2734 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2735 if (!insertpair.second)
2736 {
2737 warning (_("Section .debug_aranges in %s has duplicate "
2738 "debug_info_offset %s, ignoring .debug_aranges."),
2739 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2740 return;
2741 }
2742 }
2743
2744 section->read (objfile);
2745
2746 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2747
2748 const gdb_byte *addr = section->buffer;
2749
2750 while (addr < section->buffer + section->size)
2751 {
2752 const gdb_byte *const entry_addr = addr;
2753 unsigned int bytes_read;
2754
2755 const LONGEST entry_length = read_initial_length (abfd, addr,
2756 &bytes_read);
2757 addr += bytes_read;
2758
2759 const gdb_byte *const entry_end = addr + entry_length;
2760 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2761 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2762 if (addr + entry_length > section->buffer + section->size)
2763 {
2764 warning (_("Section .debug_aranges in %s entry at offset %s "
2765 "length %s exceeds section length %s, "
2766 "ignoring .debug_aranges."),
2767 objfile_name (objfile),
2768 plongest (entry_addr - section->buffer),
2769 plongest (bytes_read + entry_length),
2770 pulongest (section->size));
2771 return;
2772 }
2773
2774 /* The version number. */
2775 const uint16_t version = read_2_bytes (abfd, addr);
2776 addr += 2;
2777 if (version != 2)
2778 {
2779 warning (_("Section .debug_aranges in %s entry at offset %s "
2780 "has unsupported version %d, ignoring .debug_aranges."),
2781 objfile_name (objfile),
2782 plongest (entry_addr - section->buffer), version);
2783 return;
2784 }
2785
2786 const uint64_t debug_info_offset
2787 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2788 addr += offset_size;
2789 const auto per_cu_it
2790 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2791 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2792 {
2793 warning (_("Section .debug_aranges in %s entry at offset %s "
2794 "debug_info_offset %s does not exists, "
2795 "ignoring .debug_aranges."),
2796 objfile_name (objfile),
2797 plongest (entry_addr - section->buffer),
2798 pulongest (debug_info_offset));
2799 return;
2800 }
2801 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2802
2803 const uint8_t address_size = *addr++;
2804 if (address_size < 1 || address_size > 8)
2805 {
2806 warning (_("Section .debug_aranges in %s entry at offset %s "
2807 "address_size %u is invalid, ignoring .debug_aranges."),
2808 objfile_name (objfile),
2809 plongest (entry_addr - section->buffer), address_size);
2810 return;
2811 }
2812
2813 const uint8_t segment_selector_size = *addr++;
2814 if (segment_selector_size != 0)
2815 {
2816 warning (_("Section .debug_aranges in %s entry at offset %s "
2817 "segment_selector_size %u is not supported, "
2818 "ignoring .debug_aranges."),
2819 objfile_name (objfile),
2820 plongest (entry_addr - section->buffer),
2821 segment_selector_size);
2822 return;
2823 }
2824
2825 /* Must pad to an alignment boundary that is twice the address
2826 size. It is undocumented by the DWARF standard but GCC does
2827 use it. */
2828 for (size_t padding = ((-(addr - section->buffer))
2829 & (2 * address_size - 1));
2830 padding > 0; padding--)
2831 if (*addr++ != 0)
2832 {
2833 warning (_("Section .debug_aranges in %s entry at offset %s "
2834 "padding is not zero, ignoring .debug_aranges."),
2835 objfile_name (objfile),
2836 plongest (entry_addr - section->buffer));
2837 return;
2838 }
2839
2840 for (;;)
2841 {
2842 if (addr + 2 * address_size > entry_end)
2843 {
2844 warning (_("Section .debug_aranges in %s entry at offset %s "
2845 "address list is not properly terminated, "
2846 "ignoring .debug_aranges."),
2847 objfile_name (objfile),
2848 plongest (entry_addr - section->buffer));
2849 return;
2850 }
2851 ULONGEST start = extract_unsigned_integer (addr, address_size,
2852 dwarf5_byte_order);
2853 addr += address_size;
2854 ULONGEST length = extract_unsigned_integer (addr, address_size,
2855 dwarf5_byte_order);
2856 addr += address_size;
2857 if (start == 0 && length == 0)
2858 break;
2859 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
2860 {
2861 /* Symbol was eliminated due to a COMDAT group. */
2862 continue;
2863 }
2864 ULONGEST end = start + length;
2865 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2866 - baseaddr);
2867 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2868 - baseaddr);
2869 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2870 }
2871 }
2872
2873 objfile->partial_symtabs->psymtabs_addrmap
2874 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2875 }
2876
2877 /* Find a slot in the mapped index INDEX for the object named NAME.
2878 If NAME is found, set *VEC_OUT to point to the CU vector in the
2879 constant pool and return true. If NAME cannot be found, return
2880 false. */
2881
2882 static bool
2883 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2884 offset_type **vec_out)
2885 {
2886 offset_type hash;
2887 offset_type slot, step;
2888 int (*cmp) (const char *, const char *);
2889
2890 gdb::unique_xmalloc_ptr<char> without_params;
2891 if (current_language->la_language == language_cplus
2892 || current_language->la_language == language_fortran
2893 || current_language->la_language == language_d)
2894 {
2895 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2896 not contain any. */
2897
2898 if (strchr (name, '(') != NULL)
2899 {
2900 without_params = cp_remove_params (name);
2901
2902 if (without_params != NULL)
2903 name = without_params.get ();
2904 }
2905 }
2906
2907 /* Index version 4 did not support case insensitive searches. But the
2908 indices for case insensitive languages are built in lowercase, therefore
2909 simulate our NAME being searched is also lowercased. */
2910 hash = mapped_index_string_hash ((index->version == 4
2911 && case_sensitivity == case_sensitive_off
2912 ? 5 : index->version),
2913 name);
2914
2915 slot = hash & (index->symbol_table.size () - 1);
2916 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2917 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2918
2919 for (;;)
2920 {
2921 const char *str;
2922
2923 const auto &bucket = index->symbol_table[slot];
2924 if (bucket.name == 0 && bucket.vec == 0)
2925 return false;
2926
2927 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2928 if (!cmp (name, str))
2929 {
2930 *vec_out = (offset_type *) (index->constant_pool
2931 + MAYBE_SWAP (bucket.vec));
2932 return true;
2933 }
2934
2935 slot = (slot + step) & (index->symbol_table.size () - 1);
2936 }
2937 }
2938
2939 /* A helper function that reads the .gdb_index from BUFFER and fills
2940 in MAP. FILENAME is the name of the file containing the data;
2941 it is used for error reporting. DEPRECATED_OK is true if it is
2942 ok to use deprecated sections.
2943
2944 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2945 out parameters that are filled in with information about the CU and
2946 TU lists in the section.
2947
2948 Returns true if all went well, false otherwise. */
2949
2950 static bool
2951 read_gdb_index_from_buffer (const char *filename,
2952 bool deprecated_ok,
2953 gdb::array_view<const gdb_byte> buffer,
2954 struct mapped_index *map,
2955 const gdb_byte **cu_list,
2956 offset_type *cu_list_elements,
2957 const gdb_byte **types_list,
2958 offset_type *types_list_elements)
2959 {
2960 const gdb_byte *addr = &buffer[0];
2961
2962 /* Version check. */
2963 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2964 /* Versions earlier than 3 emitted every copy of a psymbol. This
2965 causes the index to behave very poorly for certain requests. Version 3
2966 contained incomplete addrmap. So, it seems better to just ignore such
2967 indices. */
2968 if (version < 4)
2969 {
2970 static int warning_printed = 0;
2971 if (!warning_printed)
2972 {
2973 warning (_("Skipping obsolete .gdb_index section in %s."),
2974 filename);
2975 warning_printed = 1;
2976 }
2977 return 0;
2978 }
2979 /* Index version 4 uses a different hash function than index version
2980 5 and later.
2981
2982 Versions earlier than 6 did not emit psymbols for inlined
2983 functions. Using these files will cause GDB not to be able to
2984 set breakpoints on inlined functions by name, so we ignore these
2985 indices unless the user has done
2986 "set use-deprecated-index-sections on". */
2987 if (version < 6 && !deprecated_ok)
2988 {
2989 static int warning_printed = 0;
2990 if (!warning_printed)
2991 {
2992 warning (_("\
2993 Skipping deprecated .gdb_index section in %s.\n\
2994 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2995 to use the section anyway."),
2996 filename);
2997 warning_printed = 1;
2998 }
2999 return 0;
3000 }
3001 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3002 of the TU (for symbols coming from TUs),
3003 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3004 Plus gold-generated indices can have duplicate entries for global symbols,
3005 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3006 These are just performance bugs, and we can't distinguish gdb-generated
3007 indices from gold-generated ones, so issue no warning here. */
3008
3009 /* Indexes with higher version than the one supported by GDB may be no
3010 longer backward compatible. */
3011 if (version > 8)
3012 return 0;
3013
3014 map->version = version;
3015
3016 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3017
3018 int i = 0;
3019 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3020 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3021 / 8);
3022 ++i;
3023
3024 *types_list = addr + MAYBE_SWAP (metadata[i]);
3025 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3026 - MAYBE_SWAP (metadata[i]))
3027 / 8);
3028 ++i;
3029
3030 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3031 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3032 map->address_table
3033 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3034 ++i;
3035
3036 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3037 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3038 map->symbol_table
3039 = gdb::array_view<mapped_index::symbol_table_slot>
3040 ((mapped_index::symbol_table_slot *) symbol_table,
3041 (mapped_index::symbol_table_slot *) symbol_table_end);
3042
3043 ++i;
3044 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3045
3046 return 1;
3047 }
3048
3049 /* Callback types for dwarf2_read_gdb_index. */
3050
3051 typedef gdb::function_view
3052 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3053 get_gdb_index_contents_ftype;
3054 typedef gdb::function_view
3055 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3056 get_gdb_index_contents_dwz_ftype;
3057
3058 /* Read .gdb_index. If everything went ok, initialize the "quick"
3059 elements of all the CUs and return 1. Otherwise, return 0. */
3060
3061 static int
3062 dwarf2_read_gdb_index
3063 (dwarf2_per_objfile *per_objfile,
3064 get_gdb_index_contents_ftype get_gdb_index_contents,
3065 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3066 {
3067 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3068 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3069 struct dwz_file *dwz;
3070 struct objfile *objfile = per_objfile->objfile;
3071 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3072
3073 gdb::array_view<const gdb_byte> main_index_contents
3074 = get_gdb_index_contents (objfile, per_bfd);
3075
3076 if (main_index_contents.empty ())
3077 return 0;
3078
3079 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3080 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3081 use_deprecated_index_sections,
3082 main_index_contents, map.get (), &cu_list,
3083 &cu_list_elements, &types_list,
3084 &types_list_elements))
3085 return 0;
3086
3087 /* Don't use the index if it's empty. */
3088 if (map->symbol_table.empty ())
3089 return 0;
3090
3091 /* If there is a .dwz file, read it so we can get its CU list as
3092 well. */
3093 dwz = dwarf2_get_dwz_file (per_bfd);
3094 if (dwz != NULL)
3095 {
3096 struct mapped_index dwz_map;
3097 const gdb_byte *dwz_types_ignore;
3098 offset_type dwz_types_elements_ignore;
3099
3100 gdb::array_view<const gdb_byte> dwz_index_content
3101 = get_gdb_index_contents_dwz (objfile, dwz);
3102
3103 if (dwz_index_content.empty ())
3104 return 0;
3105
3106 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3107 1, dwz_index_content, &dwz_map,
3108 &dwz_list, &dwz_list_elements,
3109 &dwz_types_ignore,
3110 &dwz_types_elements_ignore))
3111 {
3112 warning (_("could not read '.gdb_index' section from %s; skipping"),
3113 bfd_get_filename (dwz->dwz_bfd.get ()));
3114 return 0;
3115 }
3116 }
3117
3118 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3119 dwz_list_elements);
3120
3121 if (types_list_elements)
3122 {
3123 /* We can only handle a single .debug_types when we have an
3124 index. */
3125 if (per_bfd->types.size () != 1)
3126 return 0;
3127
3128 dwarf2_section_info *section = &per_bfd->types[0];
3129
3130 create_signatured_type_table_from_index (per_bfd, section, types_list,
3131 types_list_elements);
3132 }
3133
3134 create_addrmap_from_index (per_objfile, map.get ());
3135
3136 per_bfd->index_table = std::move (map);
3137 per_bfd->using_index = 1;
3138 per_bfd->quick_file_names_table =
3139 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3140
3141 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3142 objfiles using the same BFD. */
3143 gdb_assert (per_bfd->partial_symtabs == nullptr);
3144 per_bfd->partial_symtabs = objfile->partial_symtabs;
3145
3146 return 1;
3147 }
3148
3149 /* die_reader_func for dw2_get_file_names. */
3150
3151 static void
3152 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3153 const gdb_byte *info_ptr,
3154 struct die_info *comp_unit_die)
3155 {
3156 struct dwarf2_cu *cu = reader->cu;
3157 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3158 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3159 struct dwarf2_per_cu_data *lh_cu;
3160 struct attribute *attr;
3161 void **slot;
3162 struct quick_file_names *qfn;
3163
3164 gdb_assert (! this_cu->is_debug_types);
3165
3166 /* Our callers never want to match partial units -- instead they
3167 will match the enclosing full CU. */
3168 if (comp_unit_die->tag == DW_TAG_partial_unit)
3169 {
3170 this_cu->v.quick->no_file_data = 1;
3171 return;
3172 }
3173
3174 lh_cu = this_cu;
3175 slot = NULL;
3176
3177 line_header_up lh;
3178 sect_offset line_offset {};
3179
3180 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3181 if (attr != nullptr)
3182 {
3183 struct quick_file_names find_entry;
3184
3185 line_offset = (sect_offset) DW_UNSND (attr);
3186
3187 /* We may have already read in this line header (TU line header sharing).
3188 If we have we're done. */
3189 find_entry.hash.dwo_unit = cu->dwo_unit;
3190 find_entry.hash.line_sect_off = line_offset;
3191 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3192 &find_entry, INSERT);
3193 if (*slot != NULL)
3194 {
3195 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3196 return;
3197 }
3198
3199 lh = dwarf_decode_line_header (line_offset, cu);
3200 }
3201 if (lh == NULL)
3202 {
3203 lh_cu->v.quick->no_file_data = 1;
3204 return;
3205 }
3206
3207 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3208 qfn->hash.dwo_unit = cu->dwo_unit;
3209 qfn->hash.line_sect_off = line_offset;
3210 gdb_assert (slot != NULL);
3211 *slot = qfn;
3212
3213 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3214
3215 int offset = 0;
3216 if (strcmp (fnd.name, "<unknown>") != 0)
3217 ++offset;
3218
3219 qfn->num_file_names = offset + lh->file_names_size ();
3220 qfn->file_names =
3221 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3222 qfn->num_file_names);
3223 if (offset != 0)
3224 qfn->file_names[0] = xstrdup (fnd.name);
3225 for (int i = 0; i < lh->file_names_size (); ++i)
3226 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3227 fnd.comp_dir).release ();
3228 qfn->real_names = NULL;
3229
3230 lh_cu->v.quick->file_names = qfn;
3231 }
3232
3233 /* A helper for the "quick" functions which attempts to read the line
3234 table for THIS_CU. */
3235
3236 static struct quick_file_names *
3237 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3238 dwarf2_per_objfile *per_objfile)
3239 {
3240 /* This should never be called for TUs. */
3241 gdb_assert (! this_cu->is_debug_types);
3242 /* Nor type unit groups. */
3243 gdb_assert (! this_cu->type_unit_group_p ());
3244
3245 if (this_cu->v.quick->file_names != NULL)
3246 return this_cu->v.quick->file_names;
3247 /* If we know there is no line data, no point in looking again. */
3248 if (this_cu->v.quick->no_file_data)
3249 return NULL;
3250
3251 cutu_reader reader (this_cu, per_objfile);
3252 if (!reader.dummy_p)
3253 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3254
3255 if (this_cu->v.quick->no_file_data)
3256 return NULL;
3257 return this_cu->v.quick->file_names;
3258 }
3259
3260 /* A helper for the "quick" functions which computes and caches the
3261 real path for a given file name from the line table. */
3262
3263 static const char *
3264 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3265 struct quick_file_names *qfn, int index)
3266 {
3267 if (qfn->real_names == NULL)
3268 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3269 qfn->num_file_names, const char *);
3270
3271 if (qfn->real_names[index] == NULL)
3272 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3273
3274 return qfn->real_names[index];
3275 }
3276
3277 static struct symtab *
3278 dw2_find_last_source_symtab (struct objfile *objfile)
3279 {
3280 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3281 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3282 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3283
3284 if (cust == NULL)
3285 return NULL;
3286
3287 return compunit_primary_filetab (cust);
3288 }
3289
3290 /* Traversal function for dw2_forget_cached_source_info. */
3291
3292 static int
3293 dw2_free_cached_file_names (void **slot, void *info)
3294 {
3295 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3296
3297 if (file_data->real_names)
3298 {
3299 int i;
3300
3301 for (i = 0; i < file_data->num_file_names; ++i)
3302 {
3303 xfree ((void*) file_data->real_names[i]);
3304 file_data->real_names[i] = NULL;
3305 }
3306 }
3307
3308 return 1;
3309 }
3310
3311 static void
3312 dw2_forget_cached_source_info (struct objfile *objfile)
3313 {
3314 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3315
3316 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3317 dw2_free_cached_file_names, NULL);
3318 }
3319
3320 /* Helper function for dw2_map_symtabs_matching_filename that expands
3321 the symtabs and calls the iterator. */
3322
3323 static int
3324 dw2_map_expand_apply (struct objfile *objfile,
3325 struct dwarf2_per_cu_data *per_cu,
3326 const char *name, const char *real_path,
3327 gdb::function_view<bool (symtab *)> callback)
3328 {
3329 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3330
3331 /* Don't visit already-expanded CUs. */
3332 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3333 if (per_objfile->symtab_set_p (per_cu))
3334 return 0;
3335
3336 /* This may expand more than one symtab, and we want to iterate over
3337 all of them. */
3338 dw2_instantiate_symtab (per_cu, per_objfile, false);
3339
3340 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3341 last_made, callback);
3342 }
3343
3344 /* Implementation of the map_symtabs_matching_filename method. */
3345
3346 static bool
3347 dw2_map_symtabs_matching_filename
3348 (struct objfile *objfile, const char *name, const char *real_path,
3349 gdb::function_view<bool (symtab *)> callback)
3350 {
3351 const char *name_basename = lbasename (name);
3352 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3353
3354 /* The rule is CUs specify all the files, including those used by
3355 any TU, so there's no need to scan TUs here. */
3356
3357 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3358 {
3359 /* We only need to look at symtabs not already expanded. */
3360 if (per_objfile->symtab_set_p (per_cu))
3361 continue;
3362
3363 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3364 if (file_data == NULL)
3365 continue;
3366
3367 for (int j = 0; j < file_data->num_file_names; ++j)
3368 {
3369 const char *this_name = file_data->file_names[j];
3370 const char *this_real_name;
3371
3372 if (compare_filenames_for_search (this_name, name))
3373 {
3374 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3375 callback))
3376 return true;
3377 continue;
3378 }
3379
3380 /* Before we invoke realpath, which can get expensive when many
3381 files are involved, do a quick comparison of the basenames. */
3382 if (! basenames_may_differ
3383 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3384 continue;
3385
3386 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3387 if (compare_filenames_for_search (this_real_name, name))
3388 {
3389 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3390 callback))
3391 return true;
3392 continue;
3393 }
3394
3395 if (real_path != NULL)
3396 {
3397 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3398 gdb_assert (IS_ABSOLUTE_PATH (name));
3399 if (this_real_name != NULL
3400 && FILENAME_CMP (real_path, this_real_name) == 0)
3401 {
3402 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3403 callback))
3404 return true;
3405 continue;
3406 }
3407 }
3408 }
3409 }
3410
3411 return false;
3412 }
3413
3414 /* Struct used to manage iterating over all CUs looking for a symbol. */
3415
3416 struct dw2_symtab_iterator
3417 {
3418 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3419 dwarf2_per_objfile *per_objfile;
3420 /* If set, only look for symbols that match that block. Valid values are
3421 GLOBAL_BLOCK and STATIC_BLOCK. */
3422 gdb::optional<block_enum> block_index;
3423 /* The kind of symbol we're looking for. */
3424 domain_enum domain;
3425 /* The list of CUs from the index entry of the symbol,
3426 or NULL if not found. */
3427 offset_type *vec;
3428 /* The next element in VEC to look at. */
3429 int next;
3430 /* The number of elements in VEC, or zero if there is no match. */
3431 int length;
3432 /* Have we seen a global version of the symbol?
3433 If so we can ignore all further global instances.
3434 This is to work around gold/15646, inefficient gold-generated
3435 indices. */
3436 int global_seen;
3437 };
3438
3439 /* Initialize the index symtab iterator ITER, common part. */
3440
3441 static void
3442 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3443 dwarf2_per_objfile *per_objfile,
3444 gdb::optional<block_enum> block_index,
3445 domain_enum domain)
3446 {
3447 iter->per_objfile = per_objfile;
3448 iter->block_index = block_index;
3449 iter->domain = domain;
3450 iter->next = 0;
3451 iter->global_seen = 0;
3452 iter->vec = NULL;
3453 iter->length = 0;
3454 }
3455
3456 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3457
3458 static void
3459 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3460 dwarf2_per_objfile *per_objfile,
3461 gdb::optional<block_enum> block_index,
3462 domain_enum domain,
3463 const char *name)
3464 {
3465 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3466
3467 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3468 /* index is NULL if OBJF_READNOW. */
3469 if (index == NULL)
3470 return;
3471
3472 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3473 iter->length = MAYBE_SWAP (*iter->vec);
3474 }
3475
3476 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3477
3478 static void
3479 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3480 dwarf2_per_objfile *per_objfile,
3481 gdb::optional<block_enum> block_index,
3482 domain_enum domain, offset_type namei)
3483 {
3484 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3485
3486 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3487 /* index is NULL if OBJF_READNOW. */
3488 if (index == NULL)
3489 return;
3490
3491 gdb_assert (!index->symbol_name_slot_invalid (namei));
3492 const auto &bucket = index->symbol_table[namei];
3493
3494 iter->vec = (offset_type *) (index->constant_pool
3495 + MAYBE_SWAP (bucket.vec));
3496 iter->length = MAYBE_SWAP (*iter->vec);
3497 }
3498
3499 /* Return the next matching CU or NULL if there are no more. */
3500
3501 static struct dwarf2_per_cu_data *
3502 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3503 {
3504 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3505
3506 for ( ; iter->next < iter->length; ++iter->next)
3507 {
3508 offset_type cu_index_and_attrs =
3509 MAYBE_SWAP (iter->vec[iter->next + 1]);
3510 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3511 gdb_index_symbol_kind symbol_kind =
3512 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3513 /* Only check the symbol attributes if they're present.
3514 Indices prior to version 7 don't record them,
3515 and indices >= 7 may elide them for certain symbols
3516 (gold does this). */
3517 int attrs_valid =
3518 (per_objfile->per_bfd->index_table->version >= 7
3519 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3520
3521 /* Don't crash on bad data. */
3522 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3523 + per_objfile->per_bfd->all_type_units.size ()))
3524 {
3525 complaint (_(".gdb_index entry has bad CU index"
3526 " [in module %s]"), objfile_name (per_objfile->objfile));
3527 continue;
3528 }
3529
3530 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3531
3532 /* Skip if already read in. */
3533 if (per_objfile->symtab_set_p (per_cu))
3534 continue;
3535
3536 /* Check static vs global. */
3537 if (attrs_valid)
3538 {
3539 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3540
3541 if (iter->block_index.has_value ())
3542 {
3543 bool want_static = *iter->block_index == STATIC_BLOCK;
3544
3545 if (is_static != want_static)
3546 continue;
3547 }
3548
3549 /* Work around gold/15646. */
3550 if (!is_static
3551 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3552 {
3553 if (iter->global_seen)
3554 continue;
3555
3556 iter->global_seen = 1;
3557 }
3558 }
3559
3560 /* Only check the symbol's kind if it has one. */
3561 if (attrs_valid)
3562 {
3563 switch (iter->domain)
3564 {
3565 case VAR_DOMAIN:
3566 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3567 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3568 /* Some types are also in VAR_DOMAIN. */
3569 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3570 continue;
3571 break;
3572 case STRUCT_DOMAIN:
3573 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3574 continue;
3575 break;
3576 case LABEL_DOMAIN:
3577 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3578 continue;
3579 break;
3580 case MODULE_DOMAIN:
3581 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3582 continue;
3583 break;
3584 default:
3585 break;
3586 }
3587 }
3588
3589 ++iter->next;
3590 return per_cu;
3591 }
3592
3593 return NULL;
3594 }
3595
3596 static struct compunit_symtab *
3597 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3598 const char *name, domain_enum domain)
3599 {
3600 struct compunit_symtab *stab_best = NULL;
3601 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3602
3603 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3604
3605 struct dw2_symtab_iterator iter;
3606 struct dwarf2_per_cu_data *per_cu;
3607
3608 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3609
3610 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3611 {
3612 struct symbol *sym, *with_opaque = NULL;
3613 struct compunit_symtab *stab
3614 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3615 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3616 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3617
3618 sym = block_find_symbol (block, name, domain,
3619 block_find_non_opaque_type_preferred,
3620 &with_opaque);
3621
3622 /* Some caution must be observed with overloaded functions
3623 and methods, since the index will not contain any overload
3624 information (but NAME might contain it). */
3625
3626 if (sym != NULL
3627 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3628 return stab;
3629 if (with_opaque != NULL
3630 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3631 stab_best = stab;
3632
3633 /* Keep looking through other CUs. */
3634 }
3635
3636 return stab_best;
3637 }
3638
3639 static void
3640 dw2_print_stats (struct objfile *objfile)
3641 {
3642 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3643 int total = (per_objfile->per_bfd->all_comp_units.size ()
3644 + per_objfile->per_bfd->all_type_units.size ());
3645 int count = 0;
3646
3647 for (int i = 0; i < total; ++i)
3648 {
3649 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3650
3651 if (!per_objfile->symtab_set_p (per_cu))
3652 ++count;
3653 }
3654 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3655 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3656 }
3657
3658 /* This dumps minimal information about the index.
3659 It is called via "mt print objfiles".
3660 One use is to verify .gdb_index has been loaded by the
3661 gdb.dwarf2/gdb-index.exp testcase. */
3662
3663 static void
3664 dw2_dump (struct objfile *objfile)
3665 {
3666 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3667
3668 gdb_assert (per_objfile->per_bfd->using_index);
3669 printf_filtered (".gdb_index:");
3670 if (per_objfile->per_bfd->index_table != NULL)
3671 {
3672 printf_filtered (" version %d\n",
3673 per_objfile->per_bfd->index_table->version);
3674 }
3675 else
3676 printf_filtered (" faked for \"readnow\"\n");
3677 printf_filtered ("\n");
3678 }
3679
3680 static void
3681 dw2_expand_symtabs_for_function (struct objfile *objfile,
3682 const char *func_name)
3683 {
3684 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3685
3686 struct dw2_symtab_iterator iter;
3687 struct dwarf2_per_cu_data *per_cu;
3688
3689 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3690
3691 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3692 dw2_instantiate_symtab (per_cu, per_objfile, false);
3693
3694 }
3695
3696 static void
3697 dw2_expand_all_symtabs (struct objfile *objfile)
3698 {
3699 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3700 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3701 + per_objfile->per_bfd->all_type_units.size ());
3702
3703 for (int i = 0; i < total_units; ++i)
3704 {
3705 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3706
3707 /* We don't want to directly expand a partial CU, because if we
3708 read it with the wrong language, then assertion failures can
3709 be triggered later on. See PR symtab/23010. So, tell
3710 dw2_instantiate_symtab to skip partial CUs -- any important
3711 partial CU will be read via DW_TAG_imported_unit anyway. */
3712 dw2_instantiate_symtab (per_cu, per_objfile, true);
3713 }
3714 }
3715
3716 static void
3717 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3718 const char *fullname)
3719 {
3720 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3721
3722 /* We don't need to consider type units here.
3723 This is only called for examining code, e.g. expand_line_sal.
3724 There can be an order of magnitude (or more) more type units
3725 than comp units, and we avoid them if we can. */
3726
3727 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3728 {
3729 /* We only need to look at symtabs not already expanded. */
3730 if (per_objfile->symtab_set_p (per_cu))
3731 continue;
3732
3733 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3734 if (file_data == NULL)
3735 continue;
3736
3737 for (int j = 0; j < file_data->num_file_names; ++j)
3738 {
3739 const char *this_fullname = file_data->file_names[j];
3740
3741 if (filename_cmp (this_fullname, fullname) == 0)
3742 {
3743 dw2_instantiate_symtab (per_cu, per_objfile, false);
3744 break;
3745 }
3746 }
3747 }
3748 }
3749
3750 static void
3751 dw2_expand_symtabs_matching_symbol
3752 (mapped_index_base &index,
3753 const lookup_name_info &lookup_name_in,
3754 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3755 enum search_domain kind,
3756 gdb::function_view<bool (offset_type)> match_callback,
3757 dwarf2_per_objfile *per_objfile);
3758
3759 static void
3760 dw2_expand_symtabs_matching_one
3761 (dwarf2_per_cu_data *per_cu,
3762 dwarf2_per_objfile *per_objfile,
3763 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3764 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3765
3766 static void
3767 dw2_map_matching_symbols
3768 (struct objfile *objfile,
3769 const lookup_name_info &name, domain_enum domain,
3770 int global,
3771 gdb::function_view<symbol_found_callback_ftype> callback,
3772 symbol_compare_ftype *ordered_compare)
3773 {
3774 /* Used for Ada. */
3775 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3776
3777 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3778
3779 if (per_objfile->per_bfd->index_table != nullptr)
3780 {
3781 mapped_index &index = *per_objfile->per_bfd->index_table;
3782
3783 const char *match_name = name.ada ().lookup_name ().c_str ();
3784 auto matcher = [&] (const char *symname)
3785 {
3786 if (ordered_compare == nullptr)
3787 return true;
3788 return ordered_compare (symname, match_name) == 0;
3789 };
3790
3791 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3792 [&] (offset_type namei)
3793 {
3794 struct dw2_symtab_iterator iter;
3795 struct dwarf2_per_cu_data *per_cu;
3796
3797 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3798 namei);
3799 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3800 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3801 nullptr);
3802 return true;
3803 }, per_objfile);
3804 }
3805 else
3806 {
3807 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3808 proceed assuming all symtabs have been read in. */
3809 }
3810
3811 for (compunit_symtab *cust : objfile->compunits ())
3812 {
3813 const struct block *block;
3814
3815 if (cust == NULL)
3816 continue;
3817 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3818 if (!iterate_over_symbols_terminated (block, name,
3819 domain, callback))
3820 return;
3821 }
3822 }
3823
3824 /* Starting from a search name, return the string that finds the upper
3825 bound of all strings that start with SEARCH_NAME in a sorted name
3826 list. Returns the empty string to indicate that the upper bound is
3827 the end of the list. */
3828
3829 static std::string
3830 make_sort_after_prefix_name (const char *search_name)
3831 {
3832 /* When looking to complete "func", we find the upper bound of all
3833 symbols that start with "func" by looking for where we'd insert
3834 the closest string that would follow "func" in lexicographical
3835 order. Usually, that's "func"-with-last-character-incremented,
3836 i.e. "fund". Mind non-ASCII characters, though. Usually those
3837 will be UTF-8 multi-byte sequences, but we can't be certain.
3838 Especially mind the 0xff character, which is a valid character in
3839 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3840 rule out compilers allowing it in identifiers. Note that
3841 conveniently, strcmp/strcasecmp are specified to compare
3842 characters interpreted as unsigned char. So what we do is treat
3843 the whole string as a base 256 number composed of a sequence of
3844 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3845 to 0, and carries 1 to the following more-significant position.
3846 If the very first character in SEARCH_NAME ends up incremented
3847 and carries/overflows, then the upper bound is the end of the
3848 list. The string after the empty string is also the empty
3849 string.
3850
3851 Some examples of this operation:
3852
3853 SEARCH_NAME => "+1" RESULT
3854
3855 "abc" => "abd"
3856 "ab\xff" => "ac"
3857 "\xff" "a" "\xff" => "\xff" "b"
3858 "\xff" => ""
3859 "\xff\xff" => ""
3860 "" => ""
3861
3862 Then, with these symbols for example:
3863
3864 func
3865 func1
3866 fund
3867
3868 completing "func" looks for symbols between "func" and
3869 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3870 which finds "func" and "func1", but not "fund".
3871
3872 And with:
3873
3874 funcÿ (Latin1 'ÿ' [0xff])
3875 funcÿ1
3876 fund
3877
3878 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3879 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3880
3881 And with:
3882
3883 ÿÿ (Latin1 'ÿ' [0xff])
3884 ÿÿ1
3885
3886 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3887 the end of the list.
3888 */
3889 std::string after = search_name;
3890 while (!after.empty () && (unsigned char) after.back () == 0xff)
3891 after.pop_back ();
3892 if (!after.empty ())
3893 after.back () = (unsigned char) after.back () + 1;
3894 return after;
3895 }
3896
3897 /* See declaration. */
3898
3899 std::pair<std::vector<name_component>::const_iterator,
3900 std::vector<name_component>::const_iterator>
3901 mapped_index_base::find_name_components_bounds
3902 (const lookup_name_info &lookup_name_without_params, language lang,
3903 dwarf2_per_objfile *per_objfile) const
3904 {
3905 auto *name_cmp
3906 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3907
3908 const char *lang_name
3909 = lookup_name_without_params.language_lookup_name (lang);
3910
3911 /* Comparison function object for lower_bound that matches against a
3912 given symbol name. */
3913 auto lookup_compare_lower = [&] (const name_component &elem,
3914 const char *name)
3915 {
3916 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3917 const char *elem_name = elem_qualified + elem.name_offset;
3918 return name_cmp (elem_name, name) < 0;
3919 };
3920
3921 /* Comparison function object for upper_bound that matches against a
3922 given symbol name. */
3923 auto lookup_compare_upper = [&] (const char *name,
3924 const name_component &elem)
3925 {
3926 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3927 const char *elem_name = elem_qualified + elem.name_offset;
3928 return name_cmp (name, elem_name) < 0;
3929 };
3930
3931 auto begin = this->name_components.begin ();
3932 auto end = this->name_components.end ();
3933
3934 /* Find the lower bound. */
3935 auto lower = [&] ()
3936 {
3937 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3938 return begin;
3939 else
3940 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3941 } ();
3942
3943 /* Find the upper bound. */
3944 auto upper = [&] ()
3945 {
3946 if (lookup_name_without_params.completion_mode ())
3947 {
3948 /* In completion mode, we want UPPER to point past all
3949 symbols names that have the same prefix. I.e., with
3950 these symbols, and completing "func":
3951
3952 function << lower bound
3953 function1
3954 other_function << upper bound
3955
3956 We find the upper bound by looking for the insertion
3957 point of "func"-with-last-character-incremented,
3958 i.e. "fund". */
3959 std::string after = make_sort_after_prefix_name (lang_name);
3960 if (after.empty ())
3961 return end;
3962 return std::lower_bound (lower, end, after.c_str (),
3963 lookup_compare_lower);
3964 }
3965 else
3966 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3967 } ();
3968
3969 return {lower, upper};
3970 }
3971
3972 /* See declaration. */
3973
3974 void
3975 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3976 {
3977 if (!this->name_components.empty ())
3978 return;
3979
3980 this->name_components_casing = case_sensitivity;
3981 auto *name_cmp
3982 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3983
3984 /* The code below only knows how to break apart components of C++
3985 symbol names (and other languages that use '::' as
3986 namespace/module separator) and Ada symbol names. */
3987 auto count = this->symbol_name_count ();
3988 for (offset_type idx = 0; idx < count; idx++)
3989 {
3990 if (this->symbol_name_slot_invalid (idx))
3991 continue;
3992
3993 const char *name = this->symbol_name_at (idx, per_objfile);
3994
3995 /* Add each name component to the name component table. */
3996 unsigned int previous_len = 0;
3997
3998 if (strstr (name, "::") != nullptr)
3999 {
4000 for (unsigned int current_len = cp_find_first_component (name);
4001 name[current_len] != '\0';
4002 current_len += cp_find_first_component (name + current_len))
4003 {
4004 gdb_assert (name[current_len] == ':');
4005 this->name_components.push_back ({previous_len, idx});
4006 /* Skip the '::'. */
4007 current_len += 2;
4008 previous_len = current_len;
4009 }
4010 }
4011 else
4012 {
4013 /* Handle the Ada encoded (aka mangled) form here. */
4014 for (const char *iter = strstr (name, "__");
4015 iter != nullptr;
4016 iter = strstr (iter, "__"))
4017 {
4018 this->name_components.push_back ({previous_len, idx});
4019 iter += 2;
4020 previous_len = iter - name;
4021 }
4022 }
4023
4024 this->name_components.push_back ({previous_len, idx});
4025 }
4026
4027 /* Sort name_components elements by name. */
4028 auto name_comp_compare = [&] (const name_component &left,
4029 const name_component &right)
4030 {
4031 const char *left_qualified
4032 = this->symbol_name_at (left.idx, per_objfile);
4033 const char *right_qualified
4034 = this->symbol_name_at (right.idx, per_objfile);
4035
4036 const char *left_name = left_qualified + left.name_offset;
4037 const char *right_name = right_qualified + right.name_offset;
4038
4039 return name_cmp (left_name, right_name) < 0;
4040 };
4041
4042 std::sort (this->name_components.begin (),
4043 this->name_components.end (),
4044 name_comp_compare);
4045 }
4046
4047 /* Helper for dw2_expand_symtabs_matching that works with a
4048 mapped_index_base instead of the containing objfile. This is split
4049 to a separate function in order to be able to unit test the
4050 name_components matching using a mock mapped_index_base. For each
4051 symbol name that matches, calls MATCH_CALLBACK, passing it the
4052 symbol's index in the mapped_index_base symbol table. */
4053
4054 static void
4055 dw2_expand_symtabs_matching_symbol
4056 (mapped_index_base &index,
4057 const lookup_name_info &lookup_name_in,
4058 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4059 enum search_domain kind,
4060 gdb::function_view<bool (offset_type)> match_callback,
4061 dwarf2_per_objfile *per_objfile)
4062 {
4063 lookup_name_info lookup_name_without_params
4064 = lookup_name_in.make_ignore_params ();
4065
4066 /* Build the symbol name component sorted vector, if we haven't
4067 yet. */
4068 index.build_name_components (per_objfile);
4069
4070 /* The same symbol may appear more than once in the range though.
4071 E.g., if we're looking for symbols that complete "w", and we have
4072 a symbol named "w1::w2", we'll find the two name components for
4073 that same symbol in the range. To be sure we only call the
4074 callback once per symbol, we first collect the symbol name
4075 indexes that matched in a temporary vector and ignore
4076 duplicates. */
4077 std::vector<offset_type> matches;
4078
4079 struct name_and_matcher
4080 {
4081 symbol_name_matcher_ftype *matcher;
4082 const char *name;
4083
4084 bool operator== (const name_and_matcher &other) const
4085 {
4086 return matcher == other.matcher && strcmp (name, other.name) == 0;
4087 }
4088 };
4089
4090 /* A vector holding all the different symbol name matchers, for all
4091 languages. */
4092 std::vector<name_and_matcher> matchers;
4093
4094 for (int i = 0; i < nr_languages; i++)
4095 {
4096 enum language lang_e = (enum language) i;
4097
4098 const language_defn *lang = language_def (lang_e);
4099 symbol_name_matcher_ftype *name_matcher
4100 = lang->get_symbol_name_matcher (lookup_name_without_params);
4101
4102 name_and_matcher key {
4103 name_matcher,
4104 lookup_name_without_params.language_lookup_name (lang_e)
4105 };
4106
4107 /* Don't insert the same comparison routine more than once.
4108 Note that we do this linear walk. This is not a problem in
4109 practice because the number of supported languages is
4110 low. */
4111 if (std::find (matchers.begin (), matchers.end (), key)
4112 != matchers.end ())
4113 continue;
4114 matchers.push_back (std::move (key));
4115
4116 auto bounds
4117 = index.find_name_components_bounds (lookup_name_without_params,
4118 lang_e, per_objfile);
4119
4120 /* Now for each symbol name in range, check to see if we have a name
4121 match, and if so, call the MATCH_CALLBACK callback. */
4122
4123 for (; bounds.first != bounds.second; ++bounds.first)
4124 {
4125 const char *qualified
4126 = index.symbol_name_at (bounds.first->idx, per_objfile);
4127
4128 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4129 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4130 continue;
4131
4132 matches.push_back (bounds.first->idx);
4133 }
4134 }
4135
4136 std::sort (matches.begin (), matches.end ());
4137
4138 /* Finally call the callback, once per match. */
4139 ULONGEST prev = -1;
4140 for (offset_type idx : matches)
4141 {
4142 if (prev != idx)
4143 {
4144 if (!match_callback (idx))
4145 break;
4146 prev = idx;
4147 }
4148 }
4149
4150 /* Above we use a type wider than idx's for 'prev', since 0 and
4151 (offset_type)-1 are both possible values. */
4152 static_assert (sizeof (prev) > sizeof (offset_type), "");
4153 }
4154
4155 #if GDB_SELF_TEST
4156
4157 namespace selftests { namespace dw2_expand_symtabs_matching {
4158
4159 /* A mock .gdb_index/.debug_names-like name index table, enough to
4160 exercise dw2_expand_symtabs_matching_symbol, which works with the
4161 mapped_index_base interface. Builds an index from the symbol list
4162 passed as parameter to the constructor. */
4163 class mock_mapped_index : public mapped_index_base
4164 {
4165 public:
4166 mock_mapped_index (gdb::array_view<const char *> symbols)
4167 : m_symbol_table (symbols)
4168 {}
4169
4170 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4171
4172 /* Return the number of names in the symbol table. */
4173 size_t symbol_name_count () const override
4174 {
4175 return m_symbol_table.size ();
4176 }
4177
4178 /* Get the name of the symbol at IDX in the symbol table. */
4179 const char *symbol_name_at
4180 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4181 {
4182 return m_symbol_table[idx];
4183 }
4184
4185 private:
4186 gdb::array_view<const char *> m_symbol_table;
4187 };
4188
4189 /* Convenience function that converts a NULL pointer to a "<null>"
4190 string, to pass to print routines. */
4191
4192 static const char *
4193 string_or_null (const char *str)
4194 {
4195 return str != NULL ? str : "<null>";
4196 }
4197
4198 /* Check if a lookup_name_info built from
4199 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4200 index. EXPECTED_LIST is the list of expected matches, in expected
4201 matching order. If no match expected, then an empty list is
4202 specified. Returns true on success. On failure prints a warning
4203 indicating the file:line that failed, and returns false. */
4204
4205 static bool
4206 check_match (const char *file, int line,
4207 mock_mapped_index &mock_index,
4208 const char *name, symbol_name_match_type match_type,
4209 bool completion_mode,
4210 std::initializer_list<const char *> expected_list,
4211 dwarf2_per_objfile *per_objfile)
4212 {
4213 lookup_name_info lookup_name (name, match_type, completion_mode);
4214
4215 bool matched = true;
4216
4217 auto mismatch = [&] (const char *expected_str,
4218 const char *got)
4219 {
4220 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4221 "expected=\"%s\", got=\"%s\"\n"),
4222 file, line,
4223 (match_type == symbol_name_match_type::FULL
4224 ? "FULL" : "WILD"),
4225 name, string_or_null (expected_str), string_or_null (got));
4226 matched = false;
4227 };
4228
4229 auto expected_it = expected_list.begin ();
4230 auto expected_end = expected_list.end ();
4231
4232 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4233 NULL, ALL_DOMAIN,
4234 [&] (offset_type idx)
4235 {
4236 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4237 const char *expected_str
4238 = expected_it == expected_end ? NULL : *expected_it++;
4239
4240 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4241 mismatch (expected_str, matched_name);
4242 return true;
4243 }, per_objfile);
4244
4245 const char *expected_str
4246 = expected_it == expected_end ? NULL : *expected_it++;
4247 if (expected_str != NULL)
4248 mismatch (expected_str, NULL);
4249
4250 return matched;
4251 }
4252
4253 /* The symbols added to the mock mapped_index for testing (in
4254 canonical form). */
4255 static const char *test_symbols[] = {
4256 "function",
4257 "std::bar",
4258 "std::zfunction",
4259 "std::zfunction2",
4260 "w1::w2",
4261 "ns::foo<char*>",
4262 "ns::foo<int>",
4263 "ns::foo<long>",
4264 "ns2::tmpl<int>::foo2",
4265 "(anonymous namespace)::A::B::C",
4266
4267 /* These are used to check that the increment-last-char in the
4268 matching algorithm for completion doesn't match "t1_fund" when
4269 completing "t1_func". */
4270 "t1_func",
4271 "t1_func1",
4272 "t1_fund",
4273 "t1_fund1",
4274
4275 /* A UTF-8 name with multi-byte sequences to make sure that
4276 cp-name-parser understands this as a single identifier ("função"
4277 is "function" in PT). */
4278 u8"u8função",
4279
4280 /* \377 (0xff) is Latin1 'ÿ'. */
4281 "yfunc\377",
4282
4283 /* \377 (0xff) is Latin1 'ÿ'. */
4284 "\377",
4285 "\377\377123",
4286
4287 /* A name with all sorts of complications. Starts with "z" to make
4288 it easier for the completion tests below. */
4289 #define Z_SYM_NAME \
4290 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4291 "::tuple<(anonymous namespace)::ui*, " \
4292 "std::default_delete<(anonymous namespace)::ui>, void>"
4293
4294 Z_SYM_NAME
4295 };
4296
4297 /* Returns true if the mapped_index_base::find_name_component_bounds
4298 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4299 in completion mode. */
4300
4301 static bool
4302 check_find_bounds_finds (mapped_index_base &index,
4303 const char *search_name,
4304 gdb::array_view<const char *> expected_syms,
4305 dwarf2_per_objfile *per_objfile)
4306 {
4307 lookup_name_info lookup_name (search_name,
4308 symbol_name_match_type::FULL, true);
4309
4310 auto bounds = index.find_name_components_bounds (lookup_name,
4311 language_cplus,
4312 per_objfile);
4313
4314 size_t distance = std::distance (bounds.first, bounds.second);
4315 if (distance != expected_syms.size ())
4316 return false;
4317
4318 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4319 {
4320 auto nc_elem = bounds.first + exp_elem;
4321 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4322 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4323 return false;
4324 }
4325
4326 return true;
4327 }
4328
4329 /* Test the lower-level mapped_index::find_name_component_bounds
4330 method. */
4331
4332 static void
4333 test_mapped_index_find_name_component_bounds ()
4334 {
4335 mock_mapped_index mock_index (test_symbols);
4336
4337 mock_index.build_name_components (NULL /* per_objfile */);
4338
4339 /* Test the lower-level mapped_index::find_name_component_bounds
4340 method in completion mode. */
4341 {
4342 static const char *expected_syms[] = {
4343 "t1_func",
4344 "t1_func1",
4345 };
4346
4347 SELF_CHECK (check_find_bounds_finds
4348 (mock_index, "t1_func", expected_syms,
4349 NULL /* per_objfile */));
4350 }
4351
4352 /* Check that the increment-last-char in the name matching algorithm
4353 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4354 {
4355 static const char *expected_syms1[] = {
4356 "\377",
4357 "\377\377123",
4358 };
4359 SELF_CHECK (check_find_bounds_finds
4360 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4361
4362 static const char *expected_syms2[] = {
4363 "\377\377123",
4364 };
4365 SELF_CHECK (check_find_bounds_finds
4366 (mock_index, "\377\377", expected_syms2,
4367 NULL /* per_objfile */));
4368 }
4369 }
4370
4371 /* Test dw2_expand_symtabs_matching_symbol. */
4372
4373 static void
4374 test_dw2_expand_symtabs_matching_symbol ()
4375 {
4376 mock_mapped_index mock_index (test_symbols);
4377
4378 /* We let all tests run until the end even if some fails, for debug
4379 convenience. */
4380 bool any_mismatch = false;
4381
4382 /* Create the expected symbols list (an initializer_list). Needed
4383 because lists have commas, and we need to pass them to CHECK,
4384 which is a macro. */
4385 #define EXPECT(...) { __VA_ARGS__ }
4386
4387 /* Wrapper for check_match that passes down the current
4388 __FILE__/__LINE__. */
4389 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4390 any_mismatch |= !check_match (__FILE__, __LINE__, \
4391 mock_index, \
4392 NAME, MATCH_TYPE, COMPLETION_MODE, \
4393 EXPECTED_LIST, NULL)
4394
4395 /* Identity checks. */
4396 for (const char *sym : test_symbols)
4397 {
4398 /* Should be able to match all existing symbols. */
4399 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4400 EXPECT (sym));
4401
4402 /* Should be able to match all existing symbols with
4403 parameters. */
4404 std::string with_params = std::string (sym) + "(int)";
4405 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4406 EXPECT (sym));
4407
4408 /* Should be able to match all existing symbols with
4409 parameters and qualifiers. */
4410 with_params = std::string (sym) + " ( int ) const";
4411 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4412 EXPECT (sym));
4413
4414 /* This should really find sym, but cp-name-parser.y doesn't
4415 know about lvalue/rvalue qualifiers yet. */
4416 with_params = std::string (sym) + " ( int ) &&";
4417 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4418 {});
4419 }
4420
4421 /* Check that the name matching algorithm for completion doesn't get
4422 confused with Latin1 'ÿ' / 0xff. */
4423 {
4424 static const char str[] = "\377";
4425 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4426 EXPECT ("\377", "\377\377123"));
4427 }
4428
4429 /* Check that the increment-last-char in the matching algorithm for
4430 completion doesn't match "t1_fund" when completing "t1_func". */
4431 {
4432 static const char str[] = "t1_func";
4433 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4434 EXPECT ("t1_func", "t1_func1"));
4435 }
4436
4437 /* Check that completion mode works at each prefix of the expected
4438 symbol name. */
4439 {
4440 static const char str[] = "function(int)";
4441 size_t len = strlen (str);
4442 std::string lookup;
4443
4444 for (size_t i = 1; i < len; i++)
4445 {
4446 lookup.assign (str, i);
4447 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4448 EXPECT ("function"));
4449 }
4450 }
4451
4452 /* While "w" is a prefix of both components, the match function
4453 should still only be called once. */
4454 {
4455 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4456 EXPECT ("w1::w2"));
4457 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4458 EXPECT ("w1::w2"));
4459 }
4460
4461 /* Same, with a "complicated" symbol. */
4462 {
4463 static const char str[] = Z_SYM_NAME;
4464 size_t len = strlen (str);
4465 std::string lookup;
4466
4467 for (size_t i = 1; i < len; i++)
4468 {
4469 lookup.assign (str, i);
4470 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4471 EXPECT (Z_SYM_NAME));
4472 }
4473 }
4474
4475 /* In FULL mode, an incomplete symbol doesn't match. */
4476 {
4477 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4478 {});
4479 }
4480
4481 /* A complete symbol with parameters matches any overload, since the
4482 index has no overload info. */
4483 {
4484 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4485 EXPECT ("std::zfunction", "std::zfunction2"));
4486 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4487 EXPECT ("std::zfunction", "std::zfunction2"));
4488 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4489 EXPECT ("std::zfunction", "std::zfunction2"));
4490 }
4491
4492 /* Check that whitespace is ignored appropriately. A symbol with a
4493 template argument list. */
4494 {
4495 static const char expected[] = "ns::foo<int>";
4496 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4497 EXPECT (expected));
4498 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4499 EXPECT (expected));
4500 }
4501
4502 /* Check that whitespace is ignored appropriately. A symbol with a
4503 template argument list that includes a pointer. */
4504 {
4505 static const char expected[] = "ns::foo<char*>";
4506 /* Try both completion and non-completion modes. */
4507 static const bool completion_mode[2] = {false, true};
4508 for (size_t i = 0; i < 2; i++)
4509 {
4510 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4511 completion_mode[i], EXPECT (expected));
4512 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4513 completion_mode[i], EXPECT (expected));
4514
4515 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4516 completion_mode[i], EXPECT (expected));
4517 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4518 completion_mode[i], EXPECT (expected));
4519 }
4520 }
4521
4522 {
4523 /* Check method qualifiers are ignored. */
4524 static const char expected[] = "ns::foo<char*>";
4525 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4526 symbol_name_match_type::FULL, true, EXPECT (expected));
4527 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4528 symbol_name_match_type::FULL, true, EXPECT (expected));
4529 CHECK_MATCH ("foo < char * > ( int ) const",
4530 symbol_name_match_type::WILD, true, EXPECT (expected));
4531 CHECK_MATCH ("foo < char * > ( int ) &&",
4532 symbol_name_match_type::WILD, true, EXPECT (expected));
4533 }
4534
4535 /* Test lookup names that don't match anything. */
4536 {
4537 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4538 {});
4539
4540 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4541 {});
4542 }
4543
4544 /* Some wild matching tests, exercising "(anonymous namespace)",
4545 which should not be confused with a parameter list. */
4546 {
4547 static const char *syms[] = {
4548 "A::B::C",
4549 "B::C",
4550 "C",
4551 "A :: B :: C ( int )",
4552 "B :: C ( int )",
4553 "C ( int )",
4554 };
4555
4556 for (const char *s : syms)
4557 {
4558 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4559 EXPECT ("(anonymous namespace)::A::B::C"));
4560 }
4561 }
4562
4563 {
4564 static const char expected[] = "ns2::tmpl<int>::foo2";
4565 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4566 EXPECT (expected));
4567 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4568 EXPECT (expected));
4569 }
4570
4571 SELF_CHECK (!any_mismatch);
4572
4573 #undef EXPECT
4574 #undef CHECK_MATCH
4575 }
4576
4577 static void
4578 run_test ()
4579 {
4580 test_mapped_index_find_name_component_bounds ();
4581 test_dw2_expand_symtabs_matching_symbol ();
4582 }
4583
4584 }} // namespace selftests::dw2_expand_symtabs_matching
4585
4586 #endif /* GDB_SELF_TEST */
4587
4588 /* If FILE_MATCHER is NULL or if PER_CU has
4589 dwarf2_per_cu_quick_data::MARK set (see
4590 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4591 EXPANSION_NOTIFY on it. */
4592
4593 static void
4594 dw2_expand_symtabs_matching_one
4595 (dwarf2_per_cu_data *per_cu,
4596 dwarf2_per_objfile *per_objfile,
4597 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4598 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4599 {
4600 if (file_matcher == NULL || per_cu->v.quick->mark)
4601 {
4602 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4603
4604 compunit_symtab *symtab
4605 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4606 gdb_assert (symtab != nullptr);
4607
4608 if (expansion_notify != NULL && symtab_was_null)
4609 expansion_notify (symtab);
4610 }
4611 }
4612
4613 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4614 matched, to expand corresponding CUs that were marked. IDX is the
4615 index of the symbol name that matched. */
4616
4617 static void
4618 dw2_expand_marked_cus
4619 (dwarf2_per_objfile *per_objfile, offset_type idx,
4620 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4621 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4622 search_domain kind)
4623 {
4624 offset_type *vec, vec_len, vec_idx;
4625 bool global_seen = false;
4626 mapped_index &index = *per_objfile->per_bfd->index_table;
4627
4628 vec = (offset_type *) (index.constant_pool
4629 + MAYBE_SWAP (index.symbol_table[idx].vec));
4630 vec_len = MAYBE_SWAP (vec[0]);
4631 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4632 {
4633 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4634 /* This value is only valid for index versions >= 7. */
4635 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4636 gdb_index_symbol_kind symbol_kind =
4637 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4638 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4639 /* Only check the symbol attributes if they're present.
4640 Indices prior to version 7 don't record them,
4641 and indices >= 7 may elide them for certain symbols
4642 (gold does this). */
4643 int attrs_valid =
4644 (index.version >= 7
4645 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4646
4647 /* Work around gold/15646. */
4648 if (attrs_valid
4649 && !is_static
4650 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4651 {
4652 if (global_seen)
4653 continue;
4654
4655 global_seen = true;
4656 }
4657
4658 /* Only check the symbol's kind if it has one. */
4659 if (attrs_valid)
4660 {
4661 switch (kind)
4662 {
4663 case VARIABLES_DOMAIN:
4664 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4665 continue;
4666 break;
4667 case FUNCTIONS_DOMAIN:
4668 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4669 continue;
4670 break;
4671 case TYPES_DOMAIN:
4672 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4673 continue;
4674 break;
4675 case MODULES_DOMAIN:
4676 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4677 continue;
4678 break;
4679 default:
4680 break;
4681 }
4682 }
4683
4684 /* Don't crash on bad data. */
4685 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4686 + per_objfile->per_bfd->all_type_units.size ()))
4687 {
4688 complaint (_(".gdb_index entry has bad CU index"
4689 " [in module %s]"), objfile_name (per_objfile->objfile));
4690 continue;
4691 }
4692
4693 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4694 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4695 expansion_notify);
4696 }
4697 }
4698
4699 /* If FILE_MATCHER is non-NULL, set all the
4700 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4701 that match FILE_MATCHER. */
4702
4703 static void
4704 dw_expand_symtabs_matching_file_matcher
4705 (dwarf2_per_objfile *per_objfile,
4706 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4707 {
4708 if (file_matcher == NULL)
4709 return;
4710
4711 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4712 htab_eq_pointer,
4713 NULL, xcalloc, xfree));
4714 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4715 htab_eq_pointer,
4716 NULL, xcalloc, xfree));
4717
4718 /* The rule is CUs specify all the files, including those used by
4719 any TU, so there's no need to scan TUs here. */
4720
4721 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4722 {
4723 QUIT;
4724
4725 per_cu->v.quick->mark = 0;
4726
4727 /* We only need to look at symtabs not already expanded. */
4728 if (per_objfile->symtab_set_p (per_cu))
4729 continue;
4730
4731 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4732 if (file_data == NULL)
4733 continue;
4734
4735 if (htab_find (visited_not_found.get (), file_data) != NULL)
4736 continue;
4737 else if (htab_find (visited_found.get (), file_data) != NULL)
4738 {
4739 per_cu->v.quick->mark = 1;
4740 continue;
4741 }
4742
4743 for (int j = 0; j < file_data->num_file_names; ++j)
4744 {
4745 const char *this_real_name;
4746
4747 if (file_matcher (file_data->file_names[j], false))
4748 {
4749 per_cu->v.quick->mark = 1;
4750 break;
4751 }
4752
4753 /* Before we invoke realpath, which can get expensive when many
4754 files are involved, do a quick comparison of the basenames. */
4755 if (!basenames_may_differ
4756 && !file_matcher (lbasename (file_data->file_names[j]),
4757 true))
4758 continue;
4759
4760 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4761 if (file_matcher (this_real_name, false))
4762 {
4763 per_cu->v.quick->mark = 1;
4764 break;
4765 }
4766 }
4767
4768 void **slot = htab_find_slot (per_cu->v.quick->mark
4769 ? visited_found.get ()
4770 : visited_not_found.get (),
4771 file_data, INSERT);
4772 *slot = file_data;
4773 }
4774 }
4775
4776 static void
4777 dw2_expand_symtabs_matching
4778 (struct objfile *objfile,
4779 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4780 const lookup_name_info *lookup_name,
4781 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4782 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4783 enum search_domain kind)
4784 {
4785 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4786
4787 /* index_table is NULL if OBJF_READNOW. */
4788 if (!per_objfile->per_bfd->index_table)
4789 return;
4790
4791 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4792
4793 if (symbol_matcher == NULL && lookup_name == NULL)
4794 {
4795 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4796 {
4797 QUIT;
4798
4799 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4800 file_matcher, expansion_notify);
4801 }
4802 return;
4803 }
4804
4805 mapped_index &index = *per_objfile->per_bfd->index_table;
4806
4807 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4808 symbol_matcher,
4809 kind, [&] (offset_type idx)
4810 {
4811 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4812 kind);
4813 return true;
4814 }, per_objfile);
4815 }
4816
4817 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4818 symtab. */
4819
4820 static struct compunit_symtab *
4821 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4822 CORE_ADDR pc)
4823 {
4824 int i;
4825
4826 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4827 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4828 return cust;
4829
4830 if (cust->includes == NULL)
4831 return NULL;
4832
4833 for (i = 0; cust->includes[i]; ++i)
4834 {
4835 struct compunit_symtab *s = cust->includes[i];
4836
4837 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4838 if (s != NULL)
4839 return s;
4840 }
4841
4842 return NULL;
4843 }
4844
4845 static struct compunit_symtab *
4846 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4847 struct bound_minimal_symbol msymbol,
4848 CORE_ADDR pc,
4849 struct obj_section *section,
4850 int warn_if_readin)
4851 {
4852 struct dwarf2_per_cu_data *data;
4853 struct compunit_symtab *result;
4854
4855 if (!objfile->partial_symtabs->psymtabs_addrmap)
4856 return NULL;
4857
4858 CORE_ADDR baseaddr = objfile->text_section_offset ();
4859 data = (struct dwarf2_per_cu_data *) addrmap_find
4860 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4861 if (!data)
4862 return NULL;
4863
4864 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4865 if (warn_if_readin && per_objfile->symtab_set_p (data))
4866 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4867 paddress (objfile->arch (), pc));
4868
4869 result = recursively_find_pc_sect_compunit_symtab
4870 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4871
4872 gdb_assert (result != NULL);
4873 return result;
4874 }
4875
4876 static void
4877 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4878 void *data, int need_fullname)
4879 {
4880 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4881
4882 if (!per_objfile->per_bfd->filenames_cache)
4883 {
4884 per_objfile->per_bfd->filenames_cache.emplace ();
4885
4886 htab_up visited (htab_create_alloc (10,
4887 htab_hash_pointer, htab_eq_pointer,
4888 NULL, xcalloc, xfree));
4889
4890 /* The rule is CUs specify all the files, including those used
4891 by any TU, so there's no need to scan TUs here. We can
4892 ignore file names coming from already-expanded CUs. */
4893
4894 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4895 {
4896 if (per_objfile->symtab_set_p (per_cu))
4897 {
4898 void **slot = htab_find_slot (visited.get (),
4899 per_cu->v.quick->file_names,
4900 INSERT);
4901
4902 *slot = per_cu->v.quick->file_names;
4903 }
4904 }
4905
4906 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4907 {
4908 /* We only need to look at symtabs not already expanded. */
4909 if (per_objfile->symtab_set_p (per_cu))
4910 continue;
4911
4912 quick_file_names *file_data
4913 = dw2_get_file_names (per_cu, per_objfile);
4914 if (file_data == NULL)
4915 continue;
4916
4917 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4918 if (*slot)
4919 {
4920 /* Already visited. */
4921 continue;
4922 }
4923 *slot = file_data;
4924
4925 for (int j = 0; j < file_data->num_file_names; ++j)
4926 {
4927 const char *filename = file_data->file_names[j];
4928 per_objfile->per_bfd->filenames_cache->seen (filename);
4929 }
4930 }
4931 }
4932
4933 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4934 {
4935 gdb::unique_xmalloc_ptr<char> this_real_name;
4936
4937 if (need_fullname)
4938 this_real_name = gdb_realpath (filename);
4939 (*fun) (filename, this_real_name.get (), data);
4940 });
4941 }
4942
4943 static int
4944 dw2_has_symbols (struct objfile *objfile)
4945 {
4946 return 1;
4947 }
4948
4949 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4950 {
4951 dw2_has_symbols,
4952 dw2_find_last_source_symtab,
4953 dw2_forget_cached_source_info,
4954 dw2_map_symtabs_matching_filename,
4955 dw2_lookup_symbol,
4956 NULL,
4957 dw2_print_stats,
4958 dw2_dump,
4959 dw2_expand_symtabs_for_function,
4960 dw2_expand_all_symtabs,
4961 dw2_expand_symtabs_with_fullname,
4962 dw2_map_matching_symbols,
4963 dw2_expand_symtabs_matching,
4964 dw2_find_pc_sect_compunit_symtab,
4965 NULL,
4966 dw2_map_symbol_filenames
4967 };
4968
4969 /* DWARF-5 debug_names reader. */
4970
4971 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4972 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4973
4974 /* A helper function that reads the .debug_names section in SECTION
4975 and fills in MAP. FILENAME is the name of the file containing the
4976 section; it is used for error reporting.
4977
4978 Returns true if all went well, false otherwise. */
4979
4980 static bool
4981 read_debug_names_from_section (struct objfile *objfile,
4982 const char *filename,
4983 struct dwarf2_section_info *section,
4984 mapped_debug_names &map)
4985 {
4986 if (section->empty ())
4987 return false;
4988
4989 /* Older elfutils strip versions could keep the section in the main
4990 executable while splitting it for the separate debug info file. */
4991 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4992 return false;
4993
4994 section->read (objfile);
4995
4996 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4997
4998 const gdb_byte *addr = section->buffer;
4999
5000 bfd *const abfd = section->get_bfd_owner ();
5001
5002 unsigned int bytes_read;
5003 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5004 addr += bytes_read;
5005
5006 map.dwarf5_is_dwarf64 = bytes_read != 4;
5007 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5008 if (bytes_read + length != section->size)
5009 {
5010 /* There may be multiple per-CU indices. */
5011 warning (_("Section .debug_names in %s length %s does not match "
5012 "section length %s, ignoring .debug_names."),
5013 filename, plongest (bytes_read + length),
5014 pulongest (section->size));
5015 return false;
5016 }
5017
5018 /* The version number. */
5019 uint16_t version = read_2_bytes (abfd, addr);
5020 addr += 2;
5021 if (version != 5)
5022 {
5023 warning (_("Section .debug_names in %s has unsupported version %d, "
5024 "ignoring .debug_names."),
5025 filename, version);
5026 return false;
5027 }
5028
5029 /* Padding. */
5030 uint16_t padding = read_2_bytes (abfd, addr);
5031 addr += 2;
5032 if (padding != 0)
5033 {
5034 warning (_("Section .debug_names in %s has unsupported padding %d, "
5035 "ignoring .debug_names."),
5036 filename, padding);
5037 return false;
5038 }
5039
5040 /* comp_unit_count - The number of CUs in the CU list. */
5041 map.cu_count = read_4_bytes (abfd, addr);
5042 addr += 4;
5043
5044 /* local_type_unit_count - The number of TUs in the local TU
5045 list. */
5046 map.tu_count = read_4_bytes (abfd, addr);
5047 addr += 4;
5048
5049 /* foreign_type_unit_count - The number of TUs in the foreign TU
5050 list. */
5051 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5052 addr += 4;
5053 if (foreign_tu_count != 0)
5054 {
5055 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5056 "ignoring .debug_names."),
5057 filename, static_cast<unsigned long> (foreign_tu_count));
5058 return false;
5059 }
5060
5061 /* bucket_count - The number of hash buckets in the hash lookup
5062 table. */
5063 map.bucket_count = read_4_bytes (abfd, addr);
5064 addr += 4;
5065
5066 /* name_count - The number of unique names in the index. */
5067 map.name_count = read_4_bytes (abfd, addr);
5068 addr += 4;
5069
5070 /* abbrev_table_size - The size in bytes of the abbreviations
5071 table. */
5072 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5073 addr += 4;
5074
5075 /* augmentation_string_size - The size in bytes of the augmentation
5076 string. This value is rounded up to a multiple of 4. */
5077 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5078 addr += 4;
5079 map.augmentation_is_gdb = ((augmentation_string_size
5080 == sizeof (dwarf5_augmentation))
5081 && memcmp (addr, dwarf5_augmentation,
5082 sizeof (dwarf5_augmentation)) == 0);
5083 augmentation_string_size += (-augmentation_string_size) & 3;
5084 addr += augmentation_string_size;
5085
5086 /* List of CUs */
5087 map.cu_table_reordered = addr;
5088 addr += map.cu_count * map.offset_size;
5089
5090 /* List of Local TUs */
5091 map.tu_table_reordered = addr;
5092 addr += map.tu_count * map.offset_size;
5093
5094 /* Hash Lookup Table */
5095 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5096 addr += map.bucket_count * 4;
5097 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5098 addr += map.name_count * 4;
5099
5100 /* Name Table */
5101 map.name_table_string_offs_reordered = addr;
5102 addr += map.name_count * map.offset_size;
5103 map.name_table_entry_offs_reordered = addr;
5104 addr += map.name_count * map.offset_size;
5105
5106 const gdb_byte *abbrev_table_start = addr;
5107 for (;;)
5108 {
5109 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5110 addr += bytes_read;
5111 if (index_num == 0)
5112 break;
5113
5114 const auto insertpair
5115 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5116 if (!insertpair.second)
5117 {
5118 warning (_("Section .debug_names in %s has duplicate index %s, "
5119 "ignoring .debug_names."),
5120 filename, pulongest (index_num));
5121 return false;
5122 }
5123 mapped_debug_names::index_val &indexval = insertpair.first->second;
5124 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5125 addr += bytes_read;
5126
5127 for (;;)
5128 {
5129 mapped_debug_names::index_val::attr attr;
5130 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5131 addr += bytes_read;
5132 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5133 addr += bytes_read;
5134 if (attr.form == DW_FORM_implicit_const)
5135 {
5136 attr.implicit_const = read_signed_leb128 (abfd, addr,
5137 &bytes_read);
5138 addr += bytes_read;
5139 }
5140 if (attr.dw_idx == 0 && attr.form == 0)
5141 break;
5142 indexval.attr_vec.push_back (std::move (attr));
5143 }
5144 }
5145 if (addr != abbrev_table_start + abbrev_table_size)
5146 {
5147 warning (_("Section .debug_names in %s has abbreviation_table "
5148 "of size %s vs. written as %u, ignoring .debug_names."),
5149 filename, plongest (addr - abbrev_table_start),
5150 abbrev_table_size);
5151 return false;
5152 }
5153 map.entry_pool = addr;
5154
5155 return true;
5156 }
5157
5158 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5159 list. */
5160
5161 static void
5162 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5163 const mapped_debug_names &map,
5164 dwarf2_section_info &section,
5165 bool is_dwz)
5166 {
5167 if (!map.augmentation_is_gdb)
5168 {
5169 for (uint32_t i = 0; i < map.cu_count; ++i)
5170 {
5171 sect_offset sect_off
5172 = (sect_offset) (extract_unsigned_integer
5173 (map.cu_table_reordered + i * map.offset_size,
5174 map.offset_size,
5175 map.dwarf5_byte_order));
5176 /* We don't know the length of the CU, because the CU list in a
5177 .debug_names index can be incomplete, so we can't use the start of
5178 the next CU as end of this CU. We create the CUs here with length 0,
5179 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5180 dwarf2_per_cu_data *per_cu
5181 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5182 per_bfd->all_comp_units.push_back (per_cu);
5183 }
5184 }
5185
5186 sect_offset sect_off_prev;
5187 for (uint32_t i = 0; i <= map.cu_count; ++i)
5188 {
5189 sect_offset sect_off_next;
5190 if (i < map.cu_count)
5191 {
5192 sect_off_next
5193 = (sect_offset) (extract_unsigned_integer
5194 (map.cu_table_reordered + i * map.offset_size,
5195 map.offset_size,
5196 map.dwarf5_byte_order));
5197 }
5198 else
5199 sect_off_next = (sect_offset) section.size;
5200 if (i >= 1)
5201 {
5202 const ULONGEST length = sect_off_next - sect_off_prev;
5203 dwarf2_per_cu_data *per_cu
5204 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5205 sect_off_prev, length);
5206 per_bfd->all_comp_units.push_back (per_cu);
5207 }
5208 sect_off_prev = sect_off_next;
5209 }
5210 }
5211
5212 /* Read the CU list from the mapped index, and use it to create all
5213 the CU objects for this dwarf2_per_objfile. */
5214
5215 static void
5216 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5217 const mapped_debug_names &map,
5218 const mapped_debug_names &dwz_map)
5219 {
5220 gdb_assert (per_bfd->all_comp_units.empty ());
5221 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5222
5223 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5224 false /* is_dwz */);
5225
5226 if (dwz_map.cu_count == 0)
5227 return;
5228
5229 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5230 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5231 true /* is_dwz */);
5232 }
5233
5234 /* Read .debug_names. If everything went ok, initialize the "quick"
5235 elements of all the CUs and return true. Otherwise, return false. */
5236
5237 static bool
5238 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5239 {
5240 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5241 mapped_debug_names dwz_map;
5242 struct objfile *objfile = per_objfile->objfile;
5243 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5244
5245 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5246 &per_objfile->per_bfd->debug_names, *map))
5247 return false;
5248
5249 /* Don't use the index if it's empty. */
5250 if (map->name_count == 0)
5251 return false;
5252
5253 /* If there is a .dwz file, read it so we can get its CU list as
5254 well. */
5255 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5256 if (dwz != NULL)
5257 {
5258 if (!read_debug_names_from_section (objfile,
5259 bfd_get_filename (dwz->dwz_bfd.get ()),
5260 &dwz->debug_names, dwz_map))
5261 {
5262 warning (_("could not read '.debug_names' section from %s; skipping"),
5263 bfd_get_filename (dwz->dwz_bfd.get ()));
5264 return false;
5265 }
5266 }
5267
5268 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5269
5270 if (map->tu_count != 0)
5271 {
5272 /* We can only handle a single .debug_types when we have an
5273 index. */
5274 if (per_bfd->types.size () != 1)
5275 return false;
5276
5277 dwarf2_section_info *section = &per_bfd->types[0];
5278
5279 create_signatured_type_table_from_debug_names
5280 (per_objfile, *map, section, &per_bfd->abbrev);
5281 }
5282
5283 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5284
5285 per_bfd->debug_names_table = std::move (map);
5286 per_bfd->using_index = 1;
5287 per_bfd->quick_file_names_table =
5288 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5289
5290 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5291 objfiles using the same BFD. */
5292 gdb_assert (per_bfd->partial_symtabs == nullptr);
5293 per_bfd->partial_symtabs = objfile->partial_symtabs;
5294
5295 return true;
5296 }
5297
5298 /* Type used to manage iterating over all CUs looking for a symbol for
5299 .debug_names. */
5300
5301 class dw2_debug_names_iterator
5302 {
5303 public:
5304 dw2_debug_names_iterator (const mapped_debug_names &map,
5305 gdb::optional<block_enum> block_index,
5306 domain_enum domain,
5307 const char *name, dwarf2_per_objfile *per_objfile)
5308 : m_map (map), m_block_index (block_index), m_domain (domain),
5309 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5310 m_per_objfile (per_objfile)
5311 {}
5312
5313 dw2_debug_names_iterator (const mapped_debug_names &map,
5314 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5315 : m_map (map),
5316 m_search (search),
5317 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5318 m_per_objfile (per_objfile)
5319 {}
5320
5321 dw2_debug_names_iterator (const mapped_debug_names &map,
5322 block_enum block_index, domain_enum domain,
5323 uint32_t namei, dwarf2_per_objfile *per_objfile)
5324 : m_map (map), m_block_index (block_index), m_domain (domain),
5325 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5326 m_per_objfile (per_objfile)
5327 {}
5328
5329 /* Return the next matching CU or NULL if there are no more. */
5330 dwarf2_per_cu_data *next ();
5331
5332 private:
5333 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5334 const char *name,
5335 dwarf2_per_objfile *per_objfile);
5336 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5337 uint32_t namei,
5338 dwarf2_per_objfile *per_objfile);
5339
5340 /* The internalized form of .debug_names. */
5341 const mapped_debug_names &m_map;
5342
5343 /* If set, only look for symbols that match that block. Valid values are
5344 GLOBAL_BLOCK and STATIC_BLOCK. */
5345 const gdb::optional<block_enum> m_block_index;
5346
5347 /* The kind of symbol we're looking for. */
5348 const domain_enum m_domain = UNDEF_DOMAIN;
5349 const search_domain m_search = ALL_DOMAIN;
5350
5351 /* The list of CUs from the index entry of the symbol, or NULL if
5352 not found. */
5353 const gdb_byte *m_addr;
5354
5355 dwarf2_per_objfile *m_per_objfile;
5356 };
5357
5358 const char *
5359 mapped_debug_names::namei_to_name
5360 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5361 {
5362 const ULONGEST namei_string_offs
5363 = extract_unsigned_integer ((name_table_string_offs_reordered
5364 + namei * offset_size),
5365 offset_size,
5366 dwarf5_byte_order);
5367 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5368 }
5369
5370 /* Find a slot in .debug_names for the object named NAME. If NAME is
5371 found, return pointer to its pool data. If NAME cannot be found,
5372 return NULL. */
5373
5374 const gdb_byte *
5375 dw2_debug_names_iterator::find_vec_in_debug_names
5376 (const mapped_debug_names &map, const char *name,
5377 dwarf2_per_objfile *per_objfile)
5378 {
5379 int (*cmp) (const char *, const char *);
5380
5381 gdb::unique_xmalloc_ptr<char> without_params;
5382 if (current_language->la_language == language_cplus
5383 || current_language->la_language == language_fortran
5384 || current_language->la_language == language_d)
5385 {
5386 /* NAME is already canonical. Drop any qualifiers as
5387 .debug_names does not contain any. */
5388
5389 if (strchr (name, '(') != NULL)
5390 {
5391 without_params = cp_remove_params (name);
5392 if (without_params != NULL)
5393 name = without_params.get ();
5394 }
5395 }
5396
5397 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5398
5399 const uint32_t full_hash = dwarf5_djb_hash (name);
5400 uint32_t namei
5401 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5402 (map.bucket_table_reordered
5403 + (full_hash % map.bucket_count)), 4,
5404 map.dwarf5_byte_order);
5405 if (namei == 0)
5406 return NULL;
5407 --namei;
5408 if (namei >= map.name_count)
5409 {
5410 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5411 "[in module %s]"),
5412 namei, map.name_count,
5413 objfile_name (per_objfile->objfile));
5414 return NULL;
5415 }
5416
5417 for (;;)
5418 {
5419 const uint32_t namei_full_hash
5420 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5421 (map.hash_table_reordered + namei), 4,
5422 map.dwarf5_byte_order);
5423 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5424 return NULL;
5425
5426 if (full_hash == namei_full_hash)
5427 {
5428 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5429
5430 #if 0 /* An expensive sanity check. */
5431 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5432 {
5433 complaint (_("Wrong .debug_names hash for string at index %u "
5434 "[in module %s]"),
5435 namei, objfile_name (dwarf2_per_objfile->objfile));
5436 return NULL;
5437 }
5438 #endif
5439
5440 if (cmp (namei_string, name) == 0)
5441 {
5442 const ULONGEST namei_entry_offs
5443 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5444 + namei * map.offset_size),
5445 map.offset_size, map.dwarf5_byte_order);
5446 return map.entry_pool + namei_entry_offs;
5447 }
5448 }
5449
5450 ++namei;
5451 if (namei >= map.name_count)
5452 return NULL;
5453 }
5454 }
5455
5456 const gdb_byte *
5457 dw2_debug_names_iterator::find_vec_in_debug_names
5458 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5459 {
5460 if (namei >= map.name_count)
5461 {
5462 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5463 "[in module %s]"),
5464 namei, map.name_count,
5465 objfile_name (per_objfile->objfile));
5466 return NULL;
5467 }
5468
5469 const ULONGEST namei_entry_offs
5470 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5471 + namei * map.offset_size),
5472 map.offset_size, map.dwarf5_byte_order);
5473 return map.entry_pool + namei_entry_offs;
5474 }
5475
5476 /* See dw2_debug_names_iterator. */
5477
5478 dwarf2_per_cu_data *
5479 dw2_debug_names_iterator::next ()
5480 {
5481 if (m_addr == NULL)
5482 return NULL;
5483
5484 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5485 struct objfile *objfile = m_per_objfile->objfile;
5486 bfd *const abfd = objfile->obfd;
5487
5488 again:
5489
5490 unsigned int bytes_read;
5491 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5492 m_addr += bytes_read;
5493 if (abbrev == 0)
5494 return NULL;
5495
5496 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5497 if (indexval_it == m_map.abbrev_map.cend ())
5498 {
5499 complaint (_("Wrong .debug_names undefined abbrev code %s "
5500 "[in module %s]"),
5501 pulongest (abbrev), objfile_name (objfile));
5502 return NULL;
5503 }
5504 const mapped_debug_names::index_val &indexval = indexval_it->second;
5505 enum class symbol_linkage {
5506 unknown,
5507 static_,
5508 extern_,
5509 } symbol_linkage_ = symbol_linkage::unknown;
5510 dwarf2_per_cu_data *per_cu = NULL;
5511 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5512 {
5513 ULONGEST ull;
5514 switch (attr.form)
5515 {
5516 case DW_FORM_implicit_const:
5517 ull = attr.implicit_const;
5518 break;
5519 case DW_FORM_flag_present:
5520 ull = 1;
5521 break;
5522 case DW_FORM_udata:
5523 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5524 m_addr += bytes_read;
5525 break;
5526 case DW_FORM_ref4:
5527 ull = read_4_bytes (abfd, m_addr);
5528 m_addr += 4;
5529 break;
5530 case DW_FORM_ref8:
5531 ull = read_8_bytes (abfd, m_addr);
5532 m_addr += 8;
5533 break;
5534 case DW_FORM_ref_sig8:
5535 ull = read_8_bytes (abfd, m_addr);
5536 m_addr += 8;
5537 break;
5538 default:
5539 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5540 dwarf_form_name (attr.form),
5541 objfile_name (objfile));
5542 return NULL;
5543 }
5544 switch (attr.dw_idx)
5545 {
5546 case DW_IDX_compile_unit:
5547 /* Don't crash on bad data. */
5548 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5549 {
5550 complaint (_(".debug_names entry has bad CU index %s"
5551 " [in module %s]"),
5552 pulongest (ull),
5553 objfile_name (objfile));
5554 continue;
5555 }
5556 per_cu = per_bfd->get_cutu (ull);
5557 break;
5558 case DW_IDX_type_unit:
5559 /* Don't crash on bad data. */
5560 if (ull >= per_bfd->all_type_units.size ())
5561 {
5562 complaint (_(".debug_names entry has bad TU index %s"
5563 " [in module %s]"),
5564 pulongest (ull),
5565 objfile_name (objfile));
5566 continue;
5567 }
5568 per_cu = &per_bfd->get_tu (ull)->per_cu;
5569 break;
5570 case DW_IDX_die_offset:
5571 /* In a per-CU index (as opposed to a per-module index), index
5572 entries without CU attribute implicitly refer to the single CU. */
5573 if (per_cu == NULL)
5574 per_cu = per_bfd->get_cu (0);
5575 break;
5576 case DW_IDX_GNU_internal:
5577 if (!m_map.augmentation_is_gdb)
5578 break;
5579 symbol_linkage_ = symbol_linkage::static_;
5580 break;
5581 case DW_IDX_GNU_external:
5582 if (!m_map.augmentation_is_gdb)
5583 break;
5584 symbol_linkage_ = symbol_linkage::extern_;
5585 break;
5586 }
5587 }
5588
5589 /* Skip if already read in. */
5590 if (m_per_objfile->symtab_set_p (per_cu))
5591 goto again;
5592
5593 /* Check static vs global. */
5594 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5595 {
5596 const bool want_static = *m_block_index == STATIC_BLOCK;
5597 const bool symbol_is_static =
5598 symbol_linkage_ == symbol_linkage::static_;
5599 if (want_static != symbol_is_static)
5600 goto again;
5601 }
5602
5603 /* Match dw2_symtab_iter_next, symbol_kind
5604 and debug_names::psymbol_tag. */
5605 switch (m_domain)
5606 {
5607 case VAR_DOMAIN:
5608 switch (indexval.dwarf_tag)
5609 {
5610 case DW_TAG_variable:
5611 case DW_TAG_subprogram:
5612 /* Some types are also in VAR_DOMAIN. */
5613 case DW_TAG_typedef:
5614 case DW_TAG_structure_type:
5615 break;
5616 default:
5617 goto again;
5618 }
5619 break;
5620 case STRUCT_DOMAIN:
5621 switch (indexval.dwarf_tag)
5622 {
5623 case DW_TAG_typedef:
5624 case DW_TAG_structure_type:
5625 break;
5626 default:
5627 goto again;
5628 }
5629 break;
5630 case LABEL_DOMAIN:
5631 switch (indexval.dwarf_tag)
5632 {
5633 case 0:
5634 case DW_TAG_variable:
5635 break;
5636 default:
5637 goto again;
5638 }
5639 break;
5640 case MODULE_DOMAIN:
5641 switch (indexval.dwarf_tag)
5642 {
5643 case DW_TAG_module:
5644 break;
5645 default:
5646 goto again;
5647 }
5648 break;
5649 default:
5650 break;
5651 }
5652
5653 /* Match dw2_expand_symtabs_matching, symbol_kind and
5654 debug_names::psymbol_tag. */
5655 switch (m_search)
5656 {
5657 case VARIABLES_DOMAIN:
5658 switch (indexval.dwarf_tag)
5659 {
5660 case DW_TAG_variable:
5661 break;
5662 default:
5663 goto again;
5664 }
5665 break;
5666 case FUNCTIONS_DOMAIN:
5667 switch (indexval.dwarf_tag)
5668 {
5669 case DW_TAG_subprogram:
5670 break;
5671 default:
5672 goto again;
5673 }
5674 break;
5675 case TYPES_DOMAIN:
5676 switch (indexval.dwarf_tag)
5677 {
5678 case DW_TAG_typedef:
5679 case DW_TAG_structure_type:
5680 break;
5681 default:
5682 goto again;
5683 }
5684 break;
5685 case MODULES_DOMAIN:
5686 switch (indexval.dwarf_tag)
5687 {
5688 case DW_TAG_module:
5689 break;
5690 default:
5691 goto again;
5692 }
5693 default:
5694 break;
5695 }
5696
5697 return per_cu;
5698 }
5699
5700 static struct compunit_symtab *
5701 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5702 const char *name, domain_enum domain)
5703 {
5704 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5705
5706 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5707 if (!mapp)
5708 {
5709 /* index is NULL if OBJF_READNOW. */
5710 return NULL;
5711 }
5712 const auto &map = *mapp;
5713
5714 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5715
5716 struct compunit_symtab *stab_best = NULL;
5717 struct dwarf2_per_cu_data *per_cu;
5718 while ((per_cu = iter.next ()) != NULL)
5719 {
5720 struct symbol *sym, *with_opaque = NULL;
5721 compunit_symtab *stab
5722 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5723 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5724 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5725
5726 sym = block_find_symbol (block, name, domain,
5727 block_find_non_opaque_type_preferred,
5728 &with_opaque);
5729
5730 /* Some caution must be observed with overloaded functions and
5731 methods, since the index will not contain any overload
5732 information (but NAME might contain it). */
5733
5734 if (sym != NULL
5735 && strcmp_iw (sym->search_name (), name) == 0)
5736 return stab;
5737 if (with_opaque != NULL
5738 && strcmp_iw (with_opaque->search_name (), name) == 0)
5739 stab_best = stab;
5740
5741 /* Keep looking through other CUs. */
5742 }
5743
5744 return stab_best;
5745 }
5746
5747 /* This dumps minimal information about .debug_names. It is called
5748 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5749 uses this to verify that .debug_names has been loaded. */
5750
5751 static void
5752 dw2_debug_names_dump (struct objfile *objfile)
5753 {
5754 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5755
5756 gdb_assert (per_objfile->per_bfd->using_index);
5757 printf_filtered (".debug_names:");
5758 if (per_objfile->per_bfd->debug_names_table)
5759 printf_filtered (" exists\n");
5760 else
5761 printf_filtered (" faked for \"readnow\"\n");
5762 printf_filtered ("\n");
5763 }
5764
5765 static void
5766 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5767 const char *func_name)
5768 {
5769 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5770
5771 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5772 if (per_objfile->per_bfd->debug_names_table)
5773 {
5774 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5775
5776 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5777 per_objfile);
5778
5779 struct dwarf2_per_cu_data *per_cu;
5780 while ((per_cu = iter.next ()) != NULL)
5781 dw2_instantiate_symtab (per_cu, per_objfile, false);
5782 }
5783 }
5784
5785 static void
5786 dw2_debug_names_map_matching_symbols
5787 (struct objfile *objfile,
5788 const lookup_name_info &name, domain_enum domain,
5789 int global,
5790 gdb::function_view<symbol_found_callback_ftype> callback,
5791 symbol_compare_ftype *ordered_compare)
5792 {
5793 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5794
5795 /* debug_names_table is NULL if OBJF_READNOW. */
5796 if (!per_objfile->per_bfd->debug_names_table)
5797 return;
5798
5799 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5800 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5801
5802 const char *match_name = name.ada ().lookup_name ().c_str ();
5803 auto matcher = [&] (const char *symname)
5804 {
5805 if (ordered_compare == nullptr)
5806 return true;
5807 return ordered_compare (symname, match_name) == 0;
5808 };
5809
5810 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5811 [&] (offset_type namei)
5812 {
5813 /* The name was matched, now expand corresponding CUs that were
5814 marked. */
5815 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5816 per_objfile);
5817
5818 struct dwarf2_per_cu_data *per_cu;
5819 while ((per_cu = iter.next ()) != NULL)
5820 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5821 nullptr);
5822 return true;
5823 }, per_objfile);
5824
5825 /* It's a shame we couldn't do this inside the
5826 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5827 that have already been expanded. Instead, this loop matches what
5828 the psymtab code does. */
5829 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5830 {
5831 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5832 if (symtab != nullptr)
5833 {
5834 const struct block *block
5835 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5836 if (!iterate_over_symbols_terminated (block, name,
5837 domain, callback))
5838 break;
5839 }
5840 }
5841 }
5842
5843 static void
5844 dw2_debug_names_expand_symtabs_matching
5845 (struct objfile *objfile,
5846 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5847 const lookup_name_info *lookup_name,
5848 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5849 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5850 enum search_domain kind)
5851 {
5852 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5853
5854 /* debug_names_table is NULL if OBJF_READNOW. */
5855 if (!per_objfile->per_bfd->debug_names_table)
5856 return;
5857
5858 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5859
5860 if (symbol_matcher == NULL && lookup_name == NULL)
5861 {
5862 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5863 {
5864 QUIT;
5865
5866 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5867 expansion_notify);
5868 }
5869 return;
5870 }
5871
5872 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5873
5874 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5875 symbol_matcher,
5876 kind, [&] (offset_type namei)
5877 {
5878 /* The name was matched, now expand corresponding CUs that were
5879 marked. */
5880 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5881
5882 struct dwarf2_per_cu_data *per_cu;
5883 while ((per_cu = iter.next ()) != NULL)
5884 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5885 expansion_notify);
5886 return true;
5887 }, per_objfile);
5888 }
5889
5890 const struct quick_symbol_functions dwarf2_debug_names_functions =
5891 {
5892 dw2_has_symbols,
5893 dw2_find_last_source_symtab,
5894 dw2_forget_cached_source_info,
5895 dw2_map_symtabs_matching_filename,
5896 dw2_debug_names_lookup_symbol,
5897 NULL,
5898 dw2_print_stats,
5899 dw2_debug_names_dump,
5900 dw2_debug_names_expand_symtabs_for_function,
5901 dw2_expand_all_symtabs,
5902 dw2_expand_symtabs_with_fullname,
5903 dw2_debug_names_map_matching_symbols,
5904 dw2_debug_names_expand_symtabs_matching,
5905 dw2_find_pc_sect_compunit_symtab,
5906 NULL,
5907 dw2_map_symbol_filenames
5908 };
5909
5910 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5911 to either a dwarf2_per_bfd or dwz_file object. */
5912
5913 template <typename T>
5914 static gdb::array_view<const gdb_byte>
5915 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5916 {
5917 dwarf2_section_info *section = &section_owner->gdb_index;
5918
5919 if (section->empty ())
5920 return {};
5921
5922 /* Older elfutils strip versions could keep the section in the main
5923 executable while splitting it for the separate debug info file. */
5924 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5925 return {};
5926
5927 section->read (obj);
5928
5929 /* dwarf2_section_info::size is a bfd_size_type, while
5930 gdb::array_view works with size_t. On 32-bit hosts, with
5931 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5932 is 32-bit. So we need an explicit narrowing conversion here.
5933 This is fine, because it's impossible to allocate or mmap an
5934 array/buffer larger than what size_t can represent. */
5935 return gdb::make_array_view (section->buffer, section->size);
5936 }
5937
5938 /* Lookup the index cache for the contents of the index associated to
5939 DWARF2_OBJ. */
5940
5941 static gdb::array_view<const gdb_byte>
5942 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5943 {
5944 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5945 if (build_id == nullptr)
5946 return {};
5947
5948 return global_index_cache.lookup_gdb_index (build_id,
5949 &dwarf2_per_bfd->index_cache_res);
5950 }
5951
5952 /* Same as the above, but for DWZ. */
5953
5954 static gdb::array_view<const gdb_byte>
5955 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5956 {
5957 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5958 if (build_id == nullptr)
5959 return {};
5960
5961 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5962 }
5963
5964 /* See symfile.h. */
5965
5966 bool
5967 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5968 {
5969 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5970 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5971
5972 /* If we're about to read full symbols, don't bother with the
5973 indices. In this case we also don't care if some other debug
5974 format is making psymtabs, because they are all about to be
5975 expanded anyway. */
5976 if ((objfile->flags & OBJF_READNOW))
5977 {
5978 /* When using READNOW, the using_index flag (set below) indicates that
5979 PER_BFD was already initialized, when we loaded some other objfile. */
5980 if (per_bfd->using_index)
5981 {
5982 *index_kind = dw_index_kind::GDB_INDEX;
5983 per_objfile->resize_symtabs ();
5984 return true;
5985 }
5986
5987 per_bfd->using_index = 1;
5988 create_all_comp_units (per_objfile);
5989 create_all_type_units (per_objfile);
5990 per_bfd->quick_file_names_table
5991 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5992 per_objfile->resize_symtabs ();
5993
5994 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5995 + per_bfd->all_type_units.size ()); ++i)
5996 {
5997 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
5998
5999 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6000 struct dwarf2_per_cu_quick_data);
6001 }
6002
6003 /* Return 1 so that gdb sees the "quick" functions. However,
6004 these functions will be no-ops because we will have expanded
6005 all symtabs. */
6006 *index_kind = dw_index_kind::GDB_INDEX;
6007 return true;
6008 }
6009
6010 /* Was a debug names index already read when we processed an objfile sharing
6011 PER_BFD? */
6012 if (per_bfd->debug_names_table != nullptr)
6013 {
6014 *index_kind = dw_index_kind::DEBUG_NAMES;
6015 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6016 per_objfile->resize_symtabs ();
6017 return true;
6018 }
6019
6020 /* Was a GDB index already read when we processed an objfile sharing
6021 PER_BFD? */
6022 if (per_bfd->index_table != nullptr)
6023 {
6024 *index_kind = dw_index_kind::GDB_INDEX;
6025 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6026 per_objfile->resize_symtabs ();
6027 return true;
6028 }
6029
6030 /* There might already be partial symtabs built for this BFD. This happens
6031 when loading the same binary twice with the index-cache enabled. If so,
6032 don't try to read an index. The objfile / per_objfile initialization will
6033 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6034 code path. */
6035 if (per_bfd->partial_symtabs != nullptr)
6036 return false;
6037
6038 if (dwarf2_read_debug_names (per_objfile))
6039 {
6040 *index_kind = dw_index_kind::DEBUG_NAMES;
6041 per_objfile->resize_symtabs ();
6042 return true;
6043 }
6044
6045 if (dwarf2_read_gdb_index (per_objfile,
6046 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6047 get_gdb_index_contents_from_section<dwz_file>))
6048 {
6049 *index_kind = dw_index_kind::GDB_INDEX;
6050 per_objfile->resize_symtabs ();
6051 return true;
6052 }
6053
6054 /* ... otherwise, try to find the index in the index cache. */
6055 if (dwarf2_read_gdb_index (per_objfile,
6056 get_gdb_index_contents_from_cache,
6057 get_gdb_index_contents_from_cache_dwz))
6058 {
6059 global_index_cache.hit ();
6060 *index_kind = dw_index_kind::GDB_INDEX;
6061 per_objfile->resize_symtabs ();
6062 return true;
6063 }
6064
6065 global_index_cache.miss ();
6066 return false;
6067 }
6068
6069 \f
6070
6071 /* Build a partial symbol table. */
6072
6073 void
6074 dwarf2_build_psymtabs (struct objfile *objfile)
6075 {
6076 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6077 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6078
6079 if (per_bfd->partial_symtabs != nullptr)
6080 {
6081 /* Partial symbols were already read, so now we can simply
6082 attach them. */
6083 objfile->partial_symtabs = per_bfd->partial_symtabs;
6084 per_objfile->resize_symtabs ();
6085 return;
6086 }
6087
6088 init_psymbol_list (objfile, 1024);
6089
6090 try
6091 {
6092 /* This isn't really ideal: all the data we allocate on the
6093 objfile's obstack is still uselessly kept around. However,
6094 freeing it seems unsafe. */
6095 psymtab_discarder psymtabs (objfile);
6096 dwarf2_build_psymtabs_hard (per_objfile);
6097 psymtabs.keep ();
6098
6099 per_objfile->resize_symtabs ();
6100
6101 /* (maybe) store an index in the cache. */
6102 global_index_cache.store (per_objfile);
6103 }
6104 catch (const gdb_exception_error &except)
6105 {
6106 exception_print (gdb_stderr, except);
6107 }
6108
6109 /* Finish by setting the local reference to partial symtabs, so that
6110 we don't try to read them again if reading another objfile with the same
6111 BFD. If we can't in fact share, this won't make a difference anyway as
6112 the dwarf2_per_bfd object won't be shared. */
6113 per_bfd->partial_symtabs = objfile->partial_symtabs;
6114 }
6115
6116 /* Find the base address of the compilation unit for range lists and
6117 location lists. It will normally be specified by DW_AT_low_pc.
6118 In DWARF-3 draft 4, the base address could be overridden by
6119 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6120 compilation units with discontinuous ranges. */
6121
6122 static void
6123 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6124 {
6125 struct attribute *attr;
6126
6127 cu->base_address.reset ();
6128
6129 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6130 if (attr != nullptr)
6131 cu->base_address = attr->value_as_address ();
6132 else
6133 {
6134 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6135 if (attr != nullptr)
6136 cu->base_address = attr->value_as_address ();
6137 }
6138 }
6139
6140 /* Helper function that returns the proper abbrev section for
6141 THIS_CU. */
6142
6143 static struct dwarf2_section_info *
6144 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6145 {
6146 struct dwarf2_section_info *abbrev;
6147 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6148
6149 if (this_cu->is_dwz)
6150 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6151 else
6152 abbrev = &per_bfd->abbrev;
6153
6154 return abbrev;
6155 }
6156
6157 /* Fetch the abbreviation table offset from a comp or type unit header. */
6158
6159 static sect_offset
6160 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6161 struct dwarf2_section_info *section,
6162 sect_offset sect_off)
6163 {
6164 bfd *abfd = section->get_bfd_owner ();
6165 const gdb_byte *info_ptr;
6166 unsigned int initial_length_size, offset_size;
6167 uint16_t version;
6168
6169 section->read (per_objfile->objfile);
6170 info_ptr = section->buffer + to_underlying (sect_off);
6171 read_initial_length (abfd, info_ptr, &initial_length_size);
6172 offset_size = initial_length_size == 4 ? 4 : 8;
6173 info_ptr += initial_length_size;
6174
6175 version = read_2_bytes (abfd, info_ptr);
6176 info_ptr += 2;
6177 if (version >= 5)
6178 {
6179 /* Skip unit type and address size. */
6180 info_ptr += 2;
6181 }
6182
6183 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6184 }
6185
6186 /* A partial symtab that is used only for include files. */
6187 struct dwarf2_include_psymtab : public partial_symtab
6188 {
6189 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6190 : partial_symtab (filename, objfile)
6191 {
6192 }
6193
6194 void read_symtab (struct objfile *objfile) override
6195 {
6196 /* It's an include file, no symbols to read for it.
6197 Everything is in the includer symtab. */
6198
6199 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6200 expansion of the includer psymtab. We use the dependencies[0] field to
6201 model the includer. But if we go the regular route of calling
6202 expand_psymtab here, and having expand_psymtab call expand_dependencies
6203 to expand the includer, we'll only use expand_psymtab on the includer
6204 (making it a non-toplevel psymtab), while if we expand the includer via
6205 another path, we'll use read_symtab (making it a toplevel psymtab).
6206 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6207 psymtab, and trigger read_symtab on the includer here directly. */
6208 includer ()->read_symtab (objfile);
6209 }
6210
6211 void expand_psymtab (struct objfile *objfile) override
6212 {
6213 /* This is not called by read_symtab, and should not be called by any
6214 expand_dependencies. */
6215 gdb_assert (false);
6216 }
6217
6218 bool readin_p (struct objfile *objfile) const override
6219 {
6220 return includer ()->readin_p (objfile);
6221 }
6222
6223 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6224 {
6225 return nullptr;
6226 }
6227
6228 private:
6229 partial_symtab *includer () const
6230 {
6231 /* An include psymtab has exactly one dependency: the psymtab that
6232 includes it. */
6233 gdb_assert (this->number_of_dependencies == 1);
6234 return this->dependencies[0];
6235 }
6236 };
6237
6238 /* Allocate a new partial symtab for file named NAME and mark this new
6239 partial symtab as being an include of PST. */
6240
6241 static void
6242 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6243 struct objfile *objfile)
6244 {
6245 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6246
6247 if (!IS_ABSOLUTE_PATH (subpst->filename))
6248 subpst->dirname = pst->dirname;
6249
6250 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6251 subpst->dependencies[0] = pst;
6252 subpst->number_of_dependencies = 1;
6253 }
6254
6255 /* Read the Line Number Program data and extract the list of files
6256 included by the source file represented by PST. Build an include
6257 partial symtab for each of these included files. */
6258
6259 static void
6260 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6261 struct die_info *die,
6262 dwarf2_psymtab *pst)
6263 {
6264 line_header_up lh;
6265 struct attribute *attr;
6266
6267 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6268 if (attr != nullptr)
6269 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6270 if (lh == NULL)
6271 return; /* No linetable, so no includes. */
6272
6273 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6274 that we pass in the raw text_low here; that is ok because we're
6275 only decoding the line table to make include partial symtabs, and
6276 so the addresses aren't really used. */
6277 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6278 pst->raw_text_low (), 1);
6279 }
6280
6281 static hashval_t
6282 hash_signatured_type (const void *item)
6283 {
6284 const struct signatured_type *sig_type
6285 = (const struct signatured_type *) item;
6286
6287 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6288 return sig_type->signature;
6289 }
6290
6291 static int
6292 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6293 {
6294 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6295 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6296
6297 return lhs->signature == rhs->signature;
6298 }
6299
6300 /* Allocate a hash table for signatured types. */
6301
6302 static htab_up
6303 allocate_signatured_type_table ()
6304 {
6305 return htab_up (htab_create_alloc (41,
6306 hash_signatured_type,
6307 eq_signatured_type,
6308 NULL, xcalloc, xfree));
6309 }
6310
6311 /* A helper function to add a signatured type CU to a table. */
6312
6313 static int
6314 add_signatured_type_cu_to_table (void **slot, void *datum)
6315 {
6316 struct signatured_type *sigt = (struct signatured_type *) *slot;
6317 std::vector<signatured_type *> *all_type_units
6318 = (std::vector<signatured_type *> *) datum;
6319
6320 all_type_units->push_back (sigt);
6321
6322 return 1;
6323 }
6324
6325 /* A helper for create_debug_types_hash_table. Read types from SECTION
6326 and fill them into TYPES_HTAB. It will process only type units,
6327 therefore DW_UT_type. */
6328
6329 static void
6330 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6331 struct dwo_file *dwo_file,
6332 dwarf2_section_info *section, htab_up &types_htab,
6333 rcuh_kind section_kind)
6334 {
6335 struct objfile *objfile = per_objfile->objfile;
6336 struct dwarf2_section_info *abbrev_section;
6337 bfd *abfd;
6338 const gdb_byte *info_ptr, *end_ptr;
6339
6340 abbrev_section = (dwo_file != NULL
6341 ? &dwo_file->sections.abbrev
6342 : &per_objfile->per_bfd->abbrev);
6343
6344 if (dwarf_read_debug)
6345 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6346 section->get_name (),
6347 abbrev_section->get_file_name ());
6348
6349 section->read (objfile);
6350 info_ptr = section->buffer;
6351
6352 if (info_ptr == NULL)
6353 return;
6354
6355 /* We can't set abfd until now because the section may be empty or
6356 not present, in which case the bfd is unknown. */
6357 abfd = section->get_bfd_owner ();
6358
6359 /* We don't use cutu_reader here because we don't need to read
6360 any dies: the signature is in the header. */
6361
6362 end_ptr = info_ptr + section->size;
6363 while (info_ptr < end_ptr)
6364 {
6365 struct signatured_type *sig_type;
6366 struct dwo_unit *dwo_tu;
6367 void **slot;
6368 const gdb_byte *ptr = info_ptr;
6369 struct comp_unit_head header;
6370 unsigned int length;
6371
6372 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6373
6374 /* Initialize it due to a false compiler warning. */
6375 header.signature = -1;
6376 header.type_cu_offset_in_tu = (cu_offset) -1;
6377
6378 /* We need to read the type's signature in order to build the hash
6379 table, but we don't need anything else just yet. */
6380
6381 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6382 abbrev_section, ptr, section_kind);
6383
6384 length = header.get_length ();
6385
6386 /* Skip dummy type units. */
6387 if (ptr >= info_ptr + length
6388 || peek_abbrev_code (abfd, ptr) == 0
6389 || header.unit_type != DW_UT_type)
6390 {
6391 info_ptr += length;
6392 continue;
6393 }
6394
6395 if (types_htab == NULL)
6396 {
6397 if (dwo_file)
6398 types_htab = allocate_dwo_unit_table ();
6399 else
6400 types_htab = allocate_signatured_type_table ();
6401 }
6402
6403 if (dwo_file)
6404 {
6405 sig_type = NULL;
6406 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6407 dwo_tu->dwo_file = dwo_file;
6408 dwo_tu->signature = header.signature;
6409 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6410 dwo_tu->section = section;
6411 dwo_tu->sect_off = sect_off;
6412 dwo_tu->length = length;
6413 }
6414 else
6415 {
6416 /* N.B.: type_offset is not usable if this type uses a DWO file.
6417 The real type_offset is in the DWO file. */
6418 dwo_tu = NULL;
6419 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6420 sig_type->signature = header.signature;
6421 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6422 sig_type->per_cu.is_debug_types = 1;
6423 sig_type->per_cu.section = section;
6424 sig_type->per_cu.sect_off = sect_off;
6425 sig_type->per_cu.length = length;
6426 }
6427
6428 slot = htab_find_slot (types_htab.get (),
6429 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6430 INSERT);
6431 gdb_assert (slot != NULL);
6432 if (*slot != NULL)
6433 {
6434 sect_offset dup_sect_off;
6435
6436 if (dwo_file)
6437 {
6438 const struct dwo_unit *dup_tu
6439 = (const struct dwo_unit *) *slot;
6440
6441 dup_sect_off = dup_tu->sect_off;
6442 }
6443 else
6444 {
6445 const struct signatured_type *dup_tu
6446 = (const struct signatured_type *) *slot;
6447
6448 dup_sect_off = dup_tu->per_cu.sect_off;
6449 }
6450
6451 complaint (_("debug type entry at offset %s is duplicate to"
6452 " the entry at offset %s, signature %s"),
6453 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6454 hex_string (header.signature));
6455 }
6456 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6457
6458 if (dwarf_read_debug > 1)
6459 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6460 sect_offset_str (sect_off),
6461 hex_string (header.signature));
6462
6463 info_ptr += length;
6464 }
6465 }
6466
6467 /* Create the hash table of all entries in the .debug_types
6468 (or .debug_types.dwo) section(s).
6469 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6470 otherwise it is NULL.
6471
6472 The result is a pointer to the hash table or NULL if there are no types.
6473
6474 Note: This function processes DWO files only, not DWP files. */
6475
6476 static void
6477 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6478 struct dwo_file *dwo_file,
6479 gdb::array_view<dwarf2_section_info> type_sections,
6480 htab_up &types_htab)
6481 {
6482 for (dwarf2_section_info &section : type_sections)
6483 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6484 rcuh_kind::TYPE);
6485 }
6486
6487 /* Create the hash table of all entries in the .debug_types section,
6488 and initialize all_type_units.
6489 The result is zero if there is an error (e.g. missing .debug_types section),
6490 otherwise non-zero. */
6491
6492 static int
6493 create_all_type_units (dwarf2_per_objfile *per_objfile)
6494 {
6495 htab_up types_htab;
6496
6497 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6498 types_htab, rcuh_kind::COMPILE);
6499 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6500 types_htab);
6501 if (types_htab == NULL)
6502 {
6503 per_objfile->per_bfd->signatured_types = NULL;
6504 return 0;
6505 }
6506
6507 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6508
6509 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6510 per_objfile->per_bfd->all_type_units.reserve
6511 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6512
6513 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6514 add_signatured_type_cu_to_table,
6515 &per_objfile->per_bfd->all_type_units);
6516
6517 return 1;
6518 }
6519
6520 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6521 If SLOT is non-NULL, it is the entry to use in the hash table.
6522 Otherwise we find one. */
6523
6524 static struct signatured_type *
6525 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6526 {
6527 if (per_objfile->per_bfd->all_type_units.size ()
6528 == per_objfile->per_bfd->all_type_units.capacity ())
6529 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6530
6531 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6532
6533 per_objfile->resize_symtabs ();
6534
6535 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6536 sig_type->signature = sig;
6537 sig_type->per_cu.is_debug_types = 1;
6538 if (per_objfile->per_bfd->using_index)
6539 {
6540 sig_type->per_cu.v.quick =
6541 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6542 struct dwarf2_per_cu_quick_data);
6543 }
6544
6545 if (slot == NULL)
6546 {
6547 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6548 sig_type, INSERT);
6549 }
6550 gdb_assert (*slot == NULL);
6551 *slot = sig_type;
6552 /* The rest of sig_type must be filled in by the caller. */
6553 return sig_type;
6554 }
6555
6556 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6557 Fill in SIG_ENTRY with DWO_ENTRY. */
6558
6559 static void
6560 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6561 struct signatured_type *sig_entry,
6562 struct dwo_unit *dwo_entry)
6563 {
6564 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6565
6566 /* Make sure we're not clobbering something we don't expect to. */
6567 gdb_assert (! sig_entry->per_cu.queued);
6568 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6569 if (per_bfd->using_index)
6570 {
6571 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6572 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6573 }
6574 else
6575 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6576 gdb_assert (sig_entry->signature == dwo_entry->signature);
6577 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6578 gdb_assert (sig_entry->type_unit_group == NULL);
6579 gdb_assert (sig_entry->dwo_unit == NULL);
6580
6581 sig_entry->per_cu.section = dwo_entry->section;
6582 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6583 sig_entry->per_cu.length = dwo_entry->length;
6584 sig_entry->per_cu.reading_dwo_directly = 1;
6585 sig_entry->per_cu.per_bfd = per_bfd;
6586 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6587 sig_entry->dwo_unit = dwo_entry;
6588 }
6589
6590 /* Subroutine of lookup_signatured_type.
6591 If we haven't read the TU yet, create the signatured_type data structure
6592 for a TU to be read in directly from a DWO file, bypassing the stub.
6593 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6594 using .gdb_index, then when reading a CU we want to stay in the DWO file
6595 containing that CU. Otherwise we could end up reading several other DWO
6596 files (due to comdat folding) to process the transitive closure of all the
6597 mentioned TUs, and that can be slow. The current DWO file will have every
6598 type signature that it needs.
6599 We only do this for .gdb_index because in the psymtab case we already have
6600 to read all the DWOs to build the type unit groups. */
6601
6602 static struct signatured_type *
6603 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6604 {
6605 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6606 struct dwo_file *dwo_file;
6607 struct dwo_unit find_dwo_entry, *dwo_entry;
6608 struct signatured_type find_sig_entry, *sig_entry;
6609 void **slot;
6610
6611 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6612
6613 /* If TU skeletons have been removed then we may not have read in any
6614 TUs yet. */
6615 if (per_objfile->per_bfd->signatured_types == NULL)
6616 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6617
6618 /* We only ever need to read in one copy of a signatured type.
6619 Use the global signatured_types array to do our own comdat-folding
6620 of types. If this is the first time we're reading this TU, and
6621 the TU has an entry in .gdb_index, replace the recorded data from
6622 .gdb_index with this TU. */
6623
6624 find_sig_entry.signature = sig;
6625 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6626 &find_sig_entry, INSERT);
6627 sig_entry = (struct signatured_type *) *slot;
6628
6629 /* We can get here with the TU already read, *or* in the process of being
6630 read. Don't reassign the global entry to point to this DWO if that's
6631 the case. Also note that if the TU is already being read, it may not
6632 have come from a DWO, the program may be a mix of Fission-compiled
6633 code and non-Fission-compiled code. */
6634
6635 /* Have we already tried to read this TU?
6636 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6637 needn't exist in the global table yet). */
6638 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6639 return sig_entry;
6640
6641 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6642 dwo_unit of the TU itself. */
6643 dwo_file = cu->dwo_unit->dwo_file;
6644
6645 /* Ok, this is the first time we're reading this TU. */
6646 if (dwo_file->tus == NULL)
6647 return NULL;
6648 find_dwo_entry.signature = sig;
6649 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6650 &find_dwo_entry);
6651 if (dwo_entry == NULL)
6652 return NULL;
6653
6654 /* If the global table doesn't have an entry for this TU, add one. */
6655 if (sig_entry == NULL)
6656 sig_entry = add_type_unit (per_objfile, sig, slot);
6657
6658 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6659 sig_entry->per_cu.tu_read = 1;
6660 return sig_entry;
6661 }
6662
6663 /* Subroutine of lookup_signatured_type.
6664 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6665 then try the DWP file. If the TU stub (skeleton) has been removed then
6666 it won't be in .gdb_index. */
6667
6668 static struct signatured_type *
6669 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6670 {
6671 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6672 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6673 struct dwo_unit *dwo_entry;
6674 struct signatured_type find_sig_entry, *sig_entry;
6675 void **slot;
6676
6677 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6678 gdb_assert (dwp_file != NULL);
6679
6680 /* If TU skeletons have been removed then we may not have read in any
6681 TUs yet. */
6682 if (per_objfile->per_bfd->signatured_types == NULL)
6683 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6684
6685 find_sig_entry.signature = sig;
6686 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6687 &find_sig_entry, INSERT);
6688 sig_entry = (struct signatured_type *) *slot;
6689
6690 /* Have we already tried to read this TU?
6691 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6692 needn't exist in the global table yet). */
6693 if (sig_entry != NULL)
6694 return sig_entry;
6695
6696 if (dwp_file->tus == NULL)
6697 return NULL;
6698 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6699 1 /* is_debug_types */);
6700 if (dwo_entry == NULL)
6701 return NULL;
6702
6703 sig_entry = add_type_unit (per_objfile, sig, slot);
6704 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6705
6706 return sig_entry;
6707 }
6708
6709 /* Lookup a signature based type for DW_FORM_ref_sig8.
6710 Returns NULL if signature SIG is not present in the table.
6711 It is up to the caller to complain about this. */
6712
6713 static struct signatured_type *
6714 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6715 {
6716 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6717
6718 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6719 {
6720 /* We're in a DWO/DWP file, and we're using .gdb_index.
6721 These cases require special processing. */
6722 if (get_dwp_file (per_objfile) == NULL)
6723 return lookup_dwo_signatured_type (cu, sig);
6724 else
6725 return lookup_dwp_signatured_type (cu, sig);
6726 }
6727 else
6728 {
6729 struct signatured_type find_entry, *entry;
6730
6731 if (per_objfile->per_bfd->signatured_types == NULL)
6732 return NULL;
6733 find_entry.signature = sig;
6734 entry = ((struct signatured_type *)
6735 htab_find (per_objfile->per_bfd->signatured_types.get (),
6736 &find_entry));
6737 return entry;
6738 }
6739 }
6740
6741 /* Low level DIE reading support. */
6742
6743 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6744
6745 static void
6746 init_cu_die_reader (struct die_reader_specs *reader,
6747 struct dwarf2_cu *cu,
6748 struct dwarf2_section_info *section,
6749 struct dwo_file *dwo_file,
6750 struct abbrev_table *abbrev_table)
6751 {
6752 gdb_assert (section->readin && section->buffer != NULL);
6753 reader->abfd = section->get_bfd_owner ();
6754 reader->cu = cu;
6755 reader->dwo_file = dwo_file;
6756 reader->die_section = section;
6757 reader->buffer = section->buffer;
6758 reader->buffer_end = section->buffer + section->size;
6759 reader->abbrev_table = abbrev_table;
6760 }
6761
6762 /* Subroutine of cutu_reader to simplify it.
6763 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6764 There's just a lot of work to do, and cutu_reader is big enough
6765 already.
6766
6767 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6768 from it to the DIE in the DWO. If NULL we are skipping the stub.
6769 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6770 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6771 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6772 STUB_COMP_DIR may be non-NULL.
6773 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6774 are filled in with the info of the DIE from the DWO file.
6775 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6776 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6777 kept around for at least as long as *RESULT_READER.
6778
6779 The result is non-zero if a valid (non-dummy) DIE was found. */
6780
6781 static int
6782 read_cutu_die_from_dwo (dwarf2_cu *cu,
6783 struct dwo_unit *dwo_unit,
6784 struct die_info *stub_comp_unit_die,
6785 const char *stub_comp_dir,
6786 struct die_reader_specs *result_reader,
6787 const gdb_byte **result_info_ptr,
6788 struct die_info **result_comp_unit_die,
6789 abbrev_table_up *result_dwo_abbrev_table)
6790 {
6791 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6792 dwarf2_per_cu_data *per_cu = cu->per_cu;
6793 struct objfile *objfile = per_objfile->objfile;
6794 bfd *abfd;
6795 const gdb_byte *begin_info_ptr, *info_ptr;
6796 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6797 int i,num_extra_attrs;
6798 struct dwarf2_section_info *dwo_abbrev_section;
6799 struct die_info *comp_unit_die;
6800
6801 /* At most one of these may be provided. */
6802 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6803
6804 /* These attributes aren't processed until later:
6805 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6806 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6807 referenced later. However, these attributes are found in the stub
6808 which we won't have later. In order to not impose this complication
6809 on the rest of the code, we read them here and copy them to the
6810 DWO CU/TU die. */
6811
6812 stmt_list = NULL;
6813 low_pc = NULL;
6814 high_pc = NULL;
6815 ranges = NULL;
6816 comp_dir = NULL;
6817
6818 if (stub_comp_unit_die != NULL)
6819 {
6820 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6821 DWO file. */
6822 if (!per_cu->is_debug_types)
6823 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6824 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6825 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6826 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6827 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6828
6829 cu->addr_base = stub_comp_unit_die->addr_base ();
6830
6831 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6832 here (if needed). We need the value before we can process
6833 DW_AT_ranges. */
6834 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6835 }
6836 else if (stub_comp_dir != NULL)
6837 {
6838 /* Reconstruct the comp_dir attribute to simplify the code below. */
6839 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6840 comp_dir->name = DW_AT_comp_dir;
6841 comp_dir->form = DW_FORM_string;
6842 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6843 DW_STRING (comp_dir) = stub_comp_dir;
6844 }
6845
6846 /* Set up for reading the DWO CU/TU. */
6847 cu->dwo_unit = dwo_unit;
6848 dwarf2_section_info *section = dwo_unit->section;
6849 section->read (objfile);
6850 abfd = section->get_bfd_owner ();
6851 begin_info_ptr = info_ptr = (section->buffer
6852 + to_underlying (dwo_unit->sect_off));
6853 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6854
6855 if (per_cu->is_debug_types)
6856 {
6857 signatured_type *sig_type = (struct signatured_type *) per_cu;
6858
6859 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6860 section, dwo_abbrev_section,
6861 info_ptr, rcuh_kind::TYPE);
6862 /* This is not an assert because it can be caused by bad debug info. */
6863 if (sig_type->signature != cu->header.signature)
6864 {
6865 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6866 " TU at offset %s [in module %s]"),
6867 hex_string (sig_type->signature),
6868 hex_string (cu->header.signature),
6869 sect_offset_str (dwo_unit->sect_off),
6870 bfd_get_filename (abfd));
6871 }
6872 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6873 /* For DWOs coming from DWP files, we don't know the CU length
6874 nor the type's offset in the TU until now. */
6875 dwo_unit->length = cu->header.get_length ();
6876 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6877
6878 /* Establish the type offset that can be used to lookup the type.
6879 For DWO files, we don't know it until now. */
6880 sig_type->type_offset_in_section
6881 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6882 }
6883 else
6884 {
6885 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6886 section, dwo_abbrev_section,
6887 info_ptr, rcuh_kind::COMPILE);
6888 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6889 /* For DWOs coming from DWP files, we don't know the CU length
6890 until now. */
6891 dwo_unit->length = cu->header.get_length ();
6892 }
6893
6894 *result_dwo_abbrev_table
6895 = abbrev_table::read (objfile, dwo_abbrev_section,
6896 cu->header.abbrev_sect_off);
6897 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6898 result_dwo_abbrev_table->get ());
6899
6900 /* Read in the die, but leave space to copy over the attributes
6901 from the stub. This has the benefit of simplifying the rest of
6902 the code - all the work to maintain the illusion of a single
6903 DW_TAG_{compile,type}_unit DIE is done here. */
6904 num_extra_attrs = ((stmt_list != NULL)
6905 + (low_pc != NULL)
6906 + (high_pc != NULL)
6907 + (ranges != NULL)
6908 + (comp_dir != NULL));
6909 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6910 num_extra_attrs);
6911
6912 /* Copy over the attributes from the stub to the DIE we just read in. */
6913 comp_unit_die = *result_comp_unit_die;
6914 i = comp_unit_die->num_attrs;
6915 if (stmt_list != NULL)
6916 comp_unit_die->attrs[i++] = *stmt_list;
6917 if (low_pc != NULL)
6918 comp_unit_die->attrs[i++] = *low_pc;
6919 if (high_pc != NULL)
6920 comp_unit_die->attrs[i++] = *high_pc;
6921 if (ranges != NULL)
6922 comp_unit_die->attrs[i++] = *ranges;
6923 if (comp_dir != NULL)
6924 comp_unit_die->attrs[i++] = *comp_dir;
6925 comp_unit_die->num_attrs += num_extra_attrs;
6926
6927 if (dwarf_die_debug)
6928 {
6929 fprintf_unfiltered (gdb_stdlog,
6930 "Read die from %s@0x%x of %s:\n",
6931 section->get_name (),
6932 (unsigned) (begin_info_ptr - section->buffer),
6933 bfd_get_filename (abfd));
6934 dump_die (comp_unit_die, dwarf_die_debug);
6935 }
6936
6937 /* Skip dummy compilation units. */
6938 if (info_ptr >= begin_info_ptr + dwo_unit->length
6939 || peek_abbrev_code (abfd, info_ptr) == 0)
6940 return 0;
6941
6942 *result_info_ptr = info_ptr;
6943 return 1;
6944 }
6945
6946 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6947 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6948 signature is part of the header. */
6949 static gdb::optional<ULONGEST>
6950 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6951 {
6952 if (cu->header.version >= 5)
6953 return cu->header.signature;
6954 struct attribute *attr;
6955 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6956 if (attr == nullptr)
6957 return gdb::optional<ULONGEST> ();
6958 return DW_UNSND (attr);
6959 }
6960
6961 /* Subroutine of cutu_reader to simplify it.
6962 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6963 Returns NULL if the specified DWO unit cannot be found. */
6964
6965 static struct dwo_unit *
6966 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6967 {
6968 dwarf2_per_cu_data *per_cu = cu->per_cu;
6969 struct dwo_unit *dwo_unit;
6970 const char *comp_dir;
6971
6972 gdb_assert (cu != NULL);
6973
6974 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6975 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6976 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6977
6978 if (per_cu->is_debug_types)
6979 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6980 else
6981 {
6982 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6983
6984 if (!signature.has_value ())
6985 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6986 " [in module %s]"),
6987 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6988
6989 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6990 }
6991
6992 return dwo_unit;
6993 }
6994
6995 /* Subroutine of cutu_reader to simplify it.
6996 See it for a description of the parameters.
6997 Read a TU directly from a DWO file, bypassing the stub. */
6998
6999 void
7000 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7001 dwarf2_per_objfile *per_objfile,
7002 dwarf2_cu *existing_cu)
7003 {
7004 struct signatured_type *sig_type;
7005
7006 /* Verify we can do the following downcast, and that we have the
7007 data we need. */
7008 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7009 sig_type = (struct signatured_type *) this_cu;
7010 gdb_assert (sig_type->dwo_unit != NULL);
7011
7012 dwarf2_cu *cu;
7013
7014 if (existing_cu != nullptr)
7015 {
7016 cu = existing_cu;
7017 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7018 /* There's no need to do the rereading_dwo_cu handling that
7019 cutu_reader does since we don't read the stub. */
7020 }
7021 else
7022 {
7023 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7024 in per_objfile yet. */
7025 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7026 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7027 cu = m_new_cu.get ();
7028 }
7029
7030 /* A future optimization, if needed, would be to use an existing
7031 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7032 could share abbrev tables. */
7033
7034 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7035 NULL /* stub_comp_unit_die */,
7036 sig_type->dwo_unit->dwo_file->comp_dir,
7037 this, &info_ptr,
7038 &comp_unit_die,
7039 &m_dwo_abbrev_table) == 0)
7040 {
7041 /* Dummy die. */
7042 dummy_p = true;
7043 }
7044 }
7045
7046 /* Initialize a CU (or TU) and read its DIEs.
7047 If the CU defers to a DWO file, read the DWO file as well.
7048
7049 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7050 Otherwise the table specified in the comp unit header is read in and used.
7051 This is an optimization for when we already have the abbrev table.
7052
7053 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7054 allocated. */
7055
7056 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7057 dwarf2_per_objfile *per_objfile,
7058 struct abbrev_table *abbrev_table,
7059 dwarf2_cu *existing_cu,
7060 bool skip_partial)
7061 : die_reader_specs {},
7062 m_this_cu (this_cu)
7063 {
7064 struct objfile *objfile = per_objfile->objfile;
7065 struct dwarf2_section_info *section = this_cu->section;
7066 bfd *abfd = section->get_bfd_owner ();
7067 const gdb_byte *begin_info_ptr;
7068 struct signatured_type *sig_type = NULL;
7069 struct dwarf2_section_info *abbrev_section;
7070 /* Non-zero if CU currently points to a DWO file and we need to
7071 reread it. When this happens we need to reread the skeleton die
7072 before we can reread the DWO file (this only applies to CUs, not TUs). */
7073 int rereading_dwo_cu = 0;
7074
7075 if (dwarf_die_debug)
7076 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7077 this_cu->is_debug_types ? "type" : "comp",
7078 sect_offset_str (this_cu->sect_off));
7079
7080 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7081 file (instead of going through the stub), short-circuit all of this. */
7082 if (this_cu->reading_dwo_directly)
7083 {
7084 /* Narrow down the scope of possibilities to have to understand. */
7085 gdb_assert (this_cu->is_debug_types);
7086 gdb_assert (abbrev_table == NULL);
7087 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7088 return;
7089 }
7090
7091 /* This is cheap if the section is already read in. */
7092 section->read (objfile);
7093
7094 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7095
7096 abbrev_section = get_abbrev_section_for_cu (this_cu);
7097
7098 dwarf2_cu *cu;
7099
7100 if (existing_cu != nullptr)
7101 {
7102 cu = existing_cu;
7103 /* If this CU is from a DWO file we need to start over, we need to
7104 refetch the attributes from the skeleton CU.
7105 This could be optimized by retrieving those attributes from when we
7106 were here the first time: the previous comp_unit_die was stored in
7107 comp_unit_obstack. But there's no data yet that we need this
7108 optimization. */
7109 if (cu->dwo_unit != NULL)
7110 rereading_dwo_cu = 1;
7111 }
7112 else
7113 {
7114 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7115 in per_objfile yet. */
7116 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7117 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7118 cu = m_new_cu.get ();
7119 }
7120
7121 /* Get the header. */
7122 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7123 {
7124 /* We already have the header, there's no need to read it in again. */
7125 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7126 }
7127 else
7128 {
7129 if (this_cu->is_debug_types)
7130 {
7131 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7132 section, abbrev_section,
7133 info_ptr, rcuh_kind::TYPE);
7134
7135 /* Since per_cu is the first member of struct signatured_type,
7136 we can go from a pointer to one to a pointer to the other. */
7137 sig_type = (struct signatured_type *) this_cu;
7138 gdb_assert (sig_type->signature == cu->header.signature);
7139 gdb_assert (sig_type->type_offset_in_tu
7140 == cu->header.type_cu_offset_in_tu);
7141 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7142
7143 /* LENGTH has not been set yet for type units if we're
7144 using .gdb_index. */
7145 this_cu->length = cu->header.get_length ();
7146
7147 /* Establish the type offset that can be used to lookup the type. */
7148 sig_type->type_offset_in_section =
7149 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7150
7151 this_cu->dwarf_version = cu->header.version;
7152 }
7153 else
7154 {
7155 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7156 section, abbrev_section,
7157 info_ptr,
7158 rcuh_kind::COMPILE);
7159
7160 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7161 if (this_cu->length == 0)
7162 this_cu->length = cu->header.get_length ();
7163 else
7164 gdb_assert (this_cu->length == cu->header.get_length ());
7165 this_cu->dwarf_version = cu->header.version;
7166 }
7167 }
7168
7169 /* Skip dummy compilation units. */
7170 if (info_ptr >= begin_info_ptr + this_cu->length
7171 || peek_abbrev_code (abfd, info_ptr) == 0)
7172 {
7173 dummy_p = true;
7174 return;
7175 }
7176
7177 /* If we don't have them yet, read the abbrevs for this compilation unit.
7178 And if we need to read them now, make sure they're freed when we're
7179 done. */
7180 if (abbrev_table != NULL)
7181 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7182 else
7183 {
7184 m_abbrev_table_holder
7185 = abbrev_table::read (objfile, abbrev_section,
7186 cu->header.abbrev_sect_off);
7187 abbrev_table = m_abbrev_table_holder.get ();
7188 }
7189
7190 /* Read the top level CU/TU die. */
7191 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7192 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7193
7194 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7195 {
7196 dummy_p = true;
7197 return;
7198 }
7199
7200 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7201 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7202 table from the DWO file and pass the ownership over to us. It will be
7203 referenced from READER, so we must make sure to free it after we're done
7204 with READER.
7205
7206 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7207 DWO CU, that this test will fail (the attribute will not be present). */
7208 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7209 if (dwo_name != nullptr)
7210 {
7211 struct dwo_unit *dwo_unit;
7212 struct die_info *dwo_comp_unit_die;
7213
7214 if (comp_unit_die->has_children)
7215 {
7216 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7217 " has children (offset %s) [in module %s]"),
7218 sect_offset_str (this_cu->sect_off),
7219 bfd_get_filename (abfd));
7220 }
7221 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7222 if (dwo_unit != NULL)
7223 {
7224 if (read_cutu_die_from_dwo (cu, dwo_unit,
7225 comp_unit_die, NULL,
7226 this, &info_ptr,
7227 &dwo_comp_unit_die,
7228 &m_dwo_abbrev_table) == 0)
7229 {
7230 /* Dummy die. */
7231 dummy_p = true;
7232 return;
7233 }
7234 comp_unit_die = dwo_comp_unit_die;
7235 }
7236 else
7237 {
7238 /* Yikes, we couldn't find the rest of the DIE, we only have
7239 the stub. A complaint has already been logged. There's
7240 not much more we can do except pass on the stub DIE to
7241 die_reader_func. We don't want to throw an error on bad
7242 debug info. */
7243 }
7244 }
7245 }
7246
7247 void
7248 cutu_reader::keep ()
7249 {
7250 /* Done, clean up. */
7251 gdb_assert (!dummy_p);
7252 if (m_new_cu != NULL)
7253 {
7254 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7255 now. */
7256 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7257 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7258 }
7259 }
7260
7261 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7262 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7263 assumed to have already done the lookup to find the DWO file).
7264
7265 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7266 THIS_CU->is_debug_types, but nothing else.
7267
7268 We fill in THIS_CU->length.
7269
7270 THIS_CU->cu is always freed when done.
7271 This is done in order to not leave THIS_CU->cu in a state where we have
7272 to care whether it refers to the "main" CU or the DWO CU.
7273
7274 When parent_cu is passed, it is used to provide a default value for
7275 str_offsets_base and addr_base from the parent. */
7276
7277 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7278 dwarf2_per_objfile *per_objfile,
7279 struct dwarf2_cu *parent_cu,
7280 struct dwo_file *dwo_file)
7281 : die_reader_specs {},
7282 m_this_cu (this_cu)
7283 {
7284 struct objfile *objfile = per_objfile->objfile;
7285 struct dwarf2_section_info *section = this_cu->section;
7286 bfd *abfd = section->get_bfd_owner ();
7287 struct dwarf2_section_info *abbrev_section;
7288 const gdb_byte *begin_info_ptr, *info_ptr;
7289
7290 if (dwarf_die_debug)
7291 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7292 this_cu->is_debug_types ? "type" : "comp",
7293 sect_offset_str (this_cu->sect_off));
7294
7295 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7296
7297 abbrev_section = (dwo_file != NULL
7298 ? &dwo_file->sections.abbrev
7299 : get_abbrev_section_for_cu (this_cu));
7300
7301 /* This is cheap if the section is already read in. */
7302 section->read (objfile);
7303
7304 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7305
7306 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7307 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7308 section, abbrev_section, info_ptr,
7309 (this_cu->is_debug_types
7310 ? rcuh_kind::TYPE
7311 : rcuh_kind::COMPILE));
7312
7313 if (parent_cu != nullptr)
7314 {
7315 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7316 m_new_cu->addr_base = parent_cu->addr_base;
7317 }
7318 this_cu->length = m_new_cu->header.get_length ();
7319
7320 /* Skip dummy compilation units. */
7321 if (info_ptr >= begin_info_ptr + this_cu->length
7322 || peek_abbrev_code (abfd, info_ptr) == 0)
7323 {
7324 dummy_p = true;
7325 return;
7326 }
7327
7328 m_abbrev_table_holder
7329 = abbrev_table::read (objfile, abbrev_section,
7330 m_new_cu->header.abbrev_sect_off);
7331
7332 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7333 m_abbrev_table_holder.get ());
7334 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7335 }
7336
7337 \f
7338 /* Type Unit Groups.
7339
7340 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7341 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7342 so that all types coming from the same compilation (.o file) are grouped
7343 together. A future step could be to put the types in the same symtab as
7344 the CU the types ultimately came from. */
7345
7346 static hashval_t
7347 hash_type_unit_group (const void *item)
7348 {
7349 const struct type_unit_group *tu_group
7350 = (const struct type_unit_group *) item;
7351
7352 return hash_stmt_list_entry (&tu_group->hash);
7353 }
7354
7355 static int
7356 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7357 {
7358 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7359 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7360
7361 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7362 }
7363
7364 /* Allocate a hash table for type unit groups. */
7365
7366 static htab_up
7367 allocate_type_unit_groups_table ()
7368 {
7369 return htab_up (htab_create_alloc (3,
7370 hash_type_unit_group,
7371 eq_type_unit_group,
7372 NULL, xcalloc, xfree));
7373 }
7374
7375 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7376 partial symtabs. We combine several TUs per psymtab to not let the size
7377 of any one psymtab grow too big. */
7378 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7379 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7380
7381 /* Helper routine for get_type_unit_group.
7382 Create the type_unit_group object used to hold one or more TUs. */
7383
7384 static struct type_unit_group *
7385 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7386 {
7387 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7388 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7389 struct dwarf2_per_cu_data *per_cu;
7390 struct type_unit_group *tu_group;
7391
7392 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7393 per_cu = &tu_group->per_cu;
7394 per_cu->per_bfd = per_bfd;
7395
7396 if (per_bfd->using_index)
7397 {
7398 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7399 struct dwarf2_per_cu_quick_data);
7400 }
7401 else
7402 {
7403 unsigned int line_offset = to_underlying (line_offset_struct);
7404 dwarf2_psymtab *pst;
7405 std::string name;
7406
7407 /* Give the symtab a useful name for debug purposes. */
7408 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7409 name = string_printf ("<type_units_%d>",
7410 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7411 else
7412 name = string_printf ("<type_units_at_0x%x>", line_offset);
7413
7414 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7415 pst->anonymous = true;
7416 }
7417
7418 tu_group->hash.dwo_unit = cu->dwo_unit;
7419 tu_group->hash.line_sect_off = line_offset_struct;
7420
7421 return tu_group;
7422 }
7423
7424 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7425 STMT_LIST is a DW_AT_stmt_list attribute. */
7426
7427 static struct type_unit_group *
7428 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7429 {
7430 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7431 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7432 struct type_unit_group *tu_group;
7433 void **slot;
7434 unsigned int line_offset;
7435 struct type_unit_group type_unit_group_for_lookup;
7436
7437 if (per_objfile->per_bfd->type_unit_groups == NULL)
7438 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7439
7440 /* Do we need to create a new group, or can we use an existing one? */
7441
7442 if (stmt_list)
7443 {
7444 line_offset = DW_UNSND (stmt_list);
7445 ++tu_stats->nr_symtab_sharers;
7446 }
7447 else
7448 {
7449 /* Ugh, no stmt_list. Rare, but we have to handle it.
7450 We can do various things here like create one group per TU or
7451 spread them over multiple groups to split up the expansion work.
7452 To avoid worst case scenarios (too many groups or too large groups)
7453 we, umm, group them in bunches. */
7454 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7455 | (tu_stats->nr_stmt_less_type_units
7456 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7457 ++tu_stats->nr_stmt_less_type_units;
7458 }
7459
7460 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7461 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7462 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7463 &type_unit_group_for_lookup, INSERT);
7464 if (*slot != NULL)
7465 {
7466 tu_group = (struct type_unit_group *) *slot;
7467 gdb_assert (tu_group != NULL);
7468 }
7469 else
7470 {
7471 sect_offset line_offset_struct = (sect_offset) line_offset;
7472 tu_group = create_type_unit_group (cu, line_offset_struct);
7473 *slot = tu_group;
7474 ++tu_stats->nr_symtabs;
7475 }
7476
7477 return tu_group;
7478 }
7479 \f
7480 /* Partial symbol tables. */
7481
7482 /* Create a psymtab named NAME and assign it to PER_CU.
7483
7484 The caller must fill in the following details:
7485 dirname, textlow, texthigh. */
7486
7487 static dwarf2_psymtab *
7488 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7489 dwarf2_per_objfile *per_objfile,
7490 const char *name)
7491 {
7492 struct objfile *objfile = per_objfile->objfile;
7493 dwarf2_psymtab *pst;
7494
7495 pst = new dwarf2_psymtab (name, objfile, per_cu);
7496
7497 pst->psymtabs_addrmap_supported = true;
7498
7499 /* This is the glue that links PST into GDB's symbol API. */
7500 per_cu->v.psymtab = pst;
7501
7502 return pst;
7503 }
7504
7505 /* DIE reader function for process_psymtab_comp_unit. */
7506
7507 static void
7508 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7509 const gdb_byte *info_ptr,
7510 struct die_info *comp_unit_die,
7511 enum language pretend_language)
7512 {
7513 struct dwarf2_cu *cu = reader->cu;
7514 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7515 struct objfile *objfile = per_objfile->objfile;
7516 struct gdbarch *gdbarch = objfile->arch ();
7517 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7518 CORE_ADDR baseaddr;
7519 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7520 dwarf2_psymtab *pst;
7521 enum pc_bounds_kind cu_bounds_kind;
7522 const char *filename;
7523
7524 gdb_assert (! per_cu->is_debug_types);
7525
7526 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7527
7528 /* Allocate a new partial symbol table structure. */
7529 gdb::unique_xmalloc_ptr<char> debug_filename;
7530 static const char artificial[] = "<artificial>";
7531 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7532 if (filename == NULL)
7533 filename = "";
7534 else if (strcmp (filename, artificial) == 0)
7535 {
7536 debug_filename.reset (concat (artificial, "@",
7537 sect_offset_str (per_cu->sect_off),
7538 (char *) NULL));
7539 filename = debug_filename.get ();
7540 }
7541
7542 pst = create_partial_symtab (per_cu, per_objfile, filename);
7543
7544 /* This must be done before calling dwarf2_build_include_psymtabs. */
7545 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7546
7547 baseaddr = objfile->text_section_offset ();
7548
7549 dwarf2_find_base_address (comp_unit_die, cu);
7550
7551 /* Possibly set the default values of LOWPC and HIGHPC from
7552 `DW_AT_ranges'. */
7553 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7554 &best_highpc, cu, pst);
7555 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7556 {
7557 CORE_ADDR low
7558 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7559 - baseaddr);
7560 CORE_ADDR high
7561 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7562 - baseaddr - 1);
7563 /* Store the contiguous range if it is not empty; it can be
7564 empty for CUs with no code. */
7565 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7566 low, high, pst);
7567 }
7568
7569 /* Check if comp unit has_children.
7570 If so, read the rest of the partial symbols from this comp unit.
7571 If not, there's no more debug_info for this comp unit. */
7572 if (comp_unit_die->has_children)
7573 {
7574 struct partial_die_info *first_die;
7575 CORE_ADDR lowpc, highpc;
7576
7577 lowpc = ((CORE_ADDR) -1);
7578 highpc = ((CORE_ADDR) 0);
7579
7580 first_die = load_partial_dies (reader, info_ptr, 1);
7581
7582 scan_partial_symbols (first_die, &lowpc, &highpc,
7583 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7584
7585 /* If we didn't find a lowpc, set it to highpc to avoid
7586 complaints from `maint check'. */
7587 if (lowpc == ((CORE_ADDR) -1))
7588 lowpc = highpc;
7589
7590 /* If the compilation unit didn't have an explicit address range,
7591 then use the information extracted from its child dies. */
7592 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7593 {
7594 best_lowpc = lowpc;
7595 best_highpc = highpc;
7596 }
7597 }
7598 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7599 best_lowpc + baseaddr)
7600 - baseaddr);
7601 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7602 best_highpc + baseaddr)
7603 - baseaddr);
7604
7605 end_psymtab_common (objfile, pst);
7606
7607 if (!cu->per_cu->imported_symtabs_empty ())
7608 {
7609 int i;
7610 int len = cu->per_cu->imported_symtabs_size ();
7611
7612 /* Fill in 'dependencies' here; we fill in 'users' in a
7613 post-pass. */
7614 pst->number_of_dependencies = len;
7615 pst->dependencies
7616 = objfile->partial_symtabs->allocate_dependencies (len);
7617 for (i = 0; i < len; ++i)
7618 {
7619 pst->dependencies[i]
7620 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7621 }
7622
7623 cu->per_cu->imported_symtabs_free ();
7624 }
7625
7626 /* Get the list of files included in the current compilation unit,
7627 and build a psymtab for each of them. */
7628 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7629
7630 if (dwarf_read_debug)
7631 fprintf_unfiltered (gdb_stdlog,
7632 "Psymtab for %s unit @%s: %s - %s"
7633 ", %d global, %d static syms\n",
7634 per_cu->is_debug_types ? "type" : "comp",
7635 sect_offset_str (per_cu->sect_off),
7636 paddress (gdbarch, pst->text_low (objfile)),
7637 paddress (gdbarch, pst->text_high (objfile)),
7638 pst->n_global_syms, pst->n_static_syms);
7639 }
7640
7641 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7642 Process compilation unit THIS_CU for a psymtab. */
7643
7644 static void
7645 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7646 dwarf2_per_objfile *per_objfile,
7647 bool want_partial_unit,
7648 enum language pretend_language)
7649 {
7650 /* If this compilation unit was already read in, free the
7651 cached copy in order to read it in again. This is
7652 necessary because we skipped some symbols when we first
7653 read in the compilation unit (see load_partial_dies).
7654 This problem could be avoided, but the benefit is unclear. */
7655 per_objfile->remove_cu (this_cu);
7656
7657 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7658
7659 switch (reader.comp_unit_die->tag)
7660 {
7661 case DW_TAG_compile_unit:
7662 this_cu->unit_type = DW_UT_compile;
7663 break;
7664 case DW_TAG_partial_unit:
7665 this_cu->unit_type = DW_UT_partial;
7666 break;
7667 default:
7668 abort ();
7669 }
7670
7671 if (reader.dummy_p)
7672 {
7673 /* Nothing. */
7674 }
7675 else if (this_cu->is_debug_types)
7676 build_type_psymtabs_reader (&reader, reader.info_ptr,
7677 reader.comp_unit_die);
7678 else if (want_partial_unit
7679 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7680 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7681 reader.comp_unit_die,
7682 pretend_language);
7683
7684 this_cu->lang = reader.cu->language;
7685
7686 /* Age out any secondary CUs. */
7687 per_objfile->age_comp_units ();
7688 }
7689
7690 /* Reader function for build_type_psymtabs. */
7691
7692 static void
7693 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7694 const gdb_byte *info_ptr,
7695 struct die_info *type_unit_die)
7696 {
7697 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7698 struct objfile *objfile = per_objfile->objfile;
7699 struct dwarf2_cu *cu = reader->cu;
7700 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7701 struct signatured_type *sig_type;
7702 struct type_unit_group *tu_group;
7703 struct attribute *attr;
7704 struct partial_die_info *first_die;
7705 CORE_ADDR lowpc, highpc;
7706 dwarf2_psymtab *pst;
7707
7708 gdb_assert (per_cu->is_debug_types);
7709 sig_type = (struct signatured_type *) per_cu;
7710
7711 if (! type_unit_die->has_children)
7712 return;
7713
7714 attr = type_unit_die->attr (DW_AT_stmt_list);
7715 tu_group = get_type_unit_group (cu, attr);
7716
7717 if (tu_group->tus == nullptr)
7718 tu_group->tus = new std::vector<signatured_type *>;
7719 tu_group->tus->push_back (sig_type);
7720
7721 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7722 pst = create_partial_symtab (per_cu, per_objfile, "");
7723 pst->anonymous = true;
7724
7725 first_die = load_partial_dies (reader, info_ptr, 1);
7726
7727 lowpc = (CORE_ADDR) -1;
7728 highpc = (CORE_ADDR) 0;
7729 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7730
7731 end_psymtab_common (objfile, pst);
7732 }
7733
7734 /* Struct used to sort TUs by their abbreviation table offset. */
7735
7736 struct tu_abbrev_offset
7737 {
7738 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7739 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7740 {}
7741
7742 signatured_type *sig_type;
7743 sect_offset abbrev_offset;
7744 };
7745
7746 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7747
7748 static bool
7749 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7750 const struct tu_abbrev_offset &b)
7751 {
7752 return a.abbrev_offset < b.abbrev_offset;
7753 }
7754
7755 /* Efficiently read all the type units.
7756 This does the bulk of the work for build_type_psymtabs.
7757
7758 The efficiency is because we sort TUs by the abbrev table they use and
7759 only read each abbrev table once. In one program there are 200K TUs
7760 sharing 8K abbrev tables.
7761
7762 The main purpose of this function is to support building the
7763 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7764 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7765 can collapse the search space by grouping them by stmt_list.
7766 The savings can be significant, in the same program from above the 200K TUs
7767 share 8K stmt_list tables.
7768
7769 FUNC is expected to call get_type_unit_group, which will create the
7770 struct type_unit_group if necessary and add it to
7771 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7772
7773 static void
7774 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7775 {
7776 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7777 abbrev_table_up abbrev_table;
7778 sect_offset abbrev_offset;
7779
7780 /* It's up to the caller to not call us multiple times. */
7781 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7782
7783 if (per_objfile->per_bfd->all_type_units.empty ())
7784 return;
7785
7786 /* TUs typically share abbrev tables, and there can be way more TUs than
7787 abbrev tables. Sort by abbrev table to reduce the number of times we
7788 read each abbrev table in.
7789 Alternatives are to punt or to maintain a cache of abbrev tables.
7790 This is simpler and efficient enough for now.
7791
7792 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7793 symtab to use). Typically TUs with the same abbrev offset have the same
7794 stmt_list value too so in practice this should work well.
7795
7796 The basic algorithm here is:
7797
7798 sort TUs by abbrev table
7799 for each TU with same abbrev table:
7800 read abbrev table if first user
7801 read TU top level DIE
7802 [IWBN if DWO skeletons had DW_AT_stmt_list]
7803 call FUNC */
7804
7805 if (dwarf_read_debug)
7806 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7807
7808 /* Sort in a separate table to maintain the order of all_type_units
7809 for .gdb_index: TU indices directly index all_type_units. */
7810 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7811 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7812
7813 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7814 sorted_by_abbrev.emplace_back
7815 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7816 sig_type->per_cu.sect_off));
7817
7818 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7819 sort_tu_by_abbrev_offset);
7820
7821 abbrev_offset = (sect_offset) ~(unsigned) 0;
7822
7823 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7824 {
7825 /* Switch to the next abbrev table if necessary. */
7826 if (abbrev_table == NULL
7827 || tu.abbrev_offset != abbrev_offset)
7828 {
7829 abbrev_offset = tu.abbrev_offset;
7830 abbrev_table =
7831 abbrev_table::read (per_objfile->objfile,
7832 &per_objfile->per_bfd->abbrev, abbrev_offset);
7833 ++tu_stats->nr_uniq_abbrev_tables;
7834 }
7835
7836 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7837 abbrev_table.get (), nullptr, false);
7838 if (!reader.dummy_p)
7839 build_type_psymtabs_reader (&reader, reader.info_ptr,
7840 reader.comp_unit_die);
7841 }
7842 }
7843
7844 /* Print collected type unit statistics. */
7845
7846 static void
7847 print_tu_stats (dwarf2_per_objfile *per_objfile)
7848 {
7849 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7850
7851 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7852 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7853 per_objfile->per_bfd->all_type_units.size ());
7854 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7855 tu_stats->nr_uniq_abbrev_tables);
7856 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7857 tu_stats->nr_symtabs);
7858 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7859 tu_stats->nr_symtab_sharers);
7860 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7861 tu_stats->nr_stmt_less_type_units);
7862 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7863 tu_stats->nr_all_type_units_reallocs);
7864 }
7865
7866 /* Traversal function for build_type_psymtabs. */
7867
7868 static int
7869 build_type_psymtab_dependencies (void **slot, void *info)
7870 {
7871 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7872 struct objfile *objfile = per_objfile->objfile;
7873 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7874 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7875 dwarf2_psymtab *pst = per_cu->v.psymtab;
7876 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7877 int i;
7878
7879 gdb_assert (len > 0);
7880 gdb_assert (per_cu->type_unit_group_p ());
7881
7882 pst->number_of_dependencies = len;
7883 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7884 for (i = 0; i < len; ++i)
7885 {
7886 struct signatured_type *iter = tu_group->tus->at (i);
7887 gdb_assert (iter->per_cu.is_debug_types);
7888 pst->dependencies[i] = iter->per_cu.v.psymtab;
7889 iter->type_unit_group = tu_group;
7890 }
7891
7892 delete tu_group->tus;
7893 tu_group->tus = nullptr;
7894
7895 return 1;
7896 }
7897
7898 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7899 Build partial symbol tables for the .debug_types comp-units. */
7900
7901 static void
7902 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7903 {
7904 if (! create_all_type_units (per_objfile))
7905 return;
7906
7907 build_type_psymtabs_1 (per_objfile);
7908 }
7909
7910 /* Traversal function for process_skeletonless_type_unit.
7911 Read a TU in a DWO file and build partial symbols for it. */
7912
7913 static int
7914 process_skeletonless_type_unit (void **slot, void *info)
7915 {
7916 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7917 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7918 struct signatured_type find_entry, *entry;
7919
7920 /* If this TU doesn't exist in the global table, add it and read it in. */
7921
7922 if (per_objfile->per_bfd->signatured_types == NULL)
7923 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7924
7925 find_entry.signature = dwo_unit->signature;
7926 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7927 &find_entry, INSERT);
7928 /* If we've already seen this type there's nothing to do. What's happening
7929 is we're doing our own version of comdat-folding here. */
7930 if (*slot != NULL)
7931 return 1;
7932
7933 /* This does the job that create_all_type_units would have done for
7934 this TU. */
7935 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7936 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7937 *slot = entry;
7938
7939 /* This does the job that build_type_psymtabs_1 would have done. */
7940 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7941 if (!reader.dummy_p)
7942 build_type_psymtabs_reader (&reader, reader.info_ptr,
7943 reader.comp_unit_die);
7944
7945 return 1;
7946 }
7947
7948 /* Traversal function for process_skeletonless_type_units. */
7949
7950 static int
7951 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7952 {
7953 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7954
7955 if (dwo_file->tus != NULL)
7956 htab_traverse_noresize (dwo_file->tus.get (),
7957 process_skeletonless_type_unit, info);
7958
7959 return 1;
7960 }
7961
7962 /* Scan all TUs of DWO files, verifying we've processed them.
7963 This is needed in case a TU was emitted without its skeleton.
7964 Note: This can't be done until we know what all the DWO files are. */
7965
7966 static void
7967 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7968 {
7969 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7970 if (get_dwp_file (per_objfile) == NULL
7971 && per_objfile->per_bfd->dwo_files != NULL)
7972 {
7973 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7974 process_dwo_file_for_skeletonless_type_units,
7975 per_objfile);
7976 }
7977 }
7978
7979 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7980
7981 static void
7982 set_partial_user (dwarf2_per_objfile *per_objfile)
7983 {
7984 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
7985 {
7986 dwarf2_psymtab *pst = per_cu->v.psymtab;
7987
7988 if (pst == NULL)
7989 continue;
7990
7991 for (int j = 0; j < pst->number_of_dependencies; ++j)
7992 {
7993 /* Set the 'user' field only if it is not already set. */
7994 if (pst->dependencies[j]->user == NULL)
7995 pst->dependencies[j]->user = pst;
7996 }
7997 }
7998 }
7999
8000 /* Build the partial symbol table by doing a quick pass through the
8001 .debug_info and .debug_abbrev sections. */
8002
8003 static void
8004 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8005 {
8006 struct objfile *objfile = per_objfile->objfile;
8007
8008 if (dwarf_read_debug)
8009 {
8010 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8011 objfile_name (objfile));
8012 }
8013
8014 scoped_restore restore_reading_psyms
8015 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8016 true);
8017
8018 per_objfile->per_bfd->info.read (objfile);
8019
8020 /* Any cached compilation units will be linked by the per-objfile
8021 read_in_chain. Make sure to free them when we're done. */
8022 free_cached_comp_units freer (per_objfile);
8023
8024 build_type_psymtabs (per_objfile);
8025
8026 create_all_comp_units (per_objfile);
8027
8028 /* Create a temporary address map on a temporary obstack. We later
8029 copy this to the final obstack. */
8030 auto_obstack temp_obstack;
8031
8032 scoped_restore save_psymtabs_addrmap
8033 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8034 addrmap_create_mutable (&temp_obstack));
8035
8036 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8037 {
8038 if (per_cu->v.psymtab != NULL)
8039 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8040 continue;
8041 process_psymtab_comp_unit (per_cu, per_objfile, false,
8042 language_minimal);
8043 }
8044
8045 /* This has to wait until we read the CUs, we need the list of DWOs. */
8046 process_skeletonless_type_units (per_objfile);
8047
8048 /* Now that all TUs have been processed we can fill in the dependencies. */
8049 if (per_objfile->per_bfd->type_unit_groups != NULL)
8050 {
8051 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8052 build_type_psymtab_dependencies, per_objfile);
8053 }
8054
8055 if (dwarf_read_debug)
8056 print_tu_stats (per_objfile);
8057
8058 set_partial_user (per_objfile);
8059
8060 objfile->partial_symtabs->psymtabs_addrmap
8061 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8062 objfile->partial_symtabs->obstack ());
8063 /* At this point we want to keep the address map. */
8064 save_psymtabs_addrmap.release ();
8065
8066 if (dwarf_read_debug)
8067 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8068 objfile_name (objfile));
8069 }
8070
8071 /* Load the partial DIEs for a secondary CU into memory.
8072 This is also used when rereading a primary CU with load_all_dies. */
8073
8074 static void
8075 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8076 dwarf2_per_objfile *per_objfile,
8077 dwarf2_cu *existing_cu)
8078 {
8079 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8080
8081 if (!reader.dummy_p)
8082 {
8083 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8084 language_minimal);
8085
8086 /* Check if comp unit has_children.
8087 If so, read the rest of the partial symbols from this comp unit.
8088 If not, there's no more debug_info for this comp unit. */
8089 if (reader.comp_unit_die->has_children)
8090 load_partial_dies (&reader, reader.info_ptr, 0);
8091
8092 reader.keep ();
8093 }
8094 }
8095
8096 static void
8097 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8098 struct dwarf2_section_info *section,
8099 struct dwarf2_section_info *abbrev_section,
8100 unsigned int is_dwz)
8101 {
8102 const gdb_byte *info_ptr;
8103 struct objfile *objfile = per_objfile->objfile;
8104
8105 if (dwarf_read_debug)
8106 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8107 section->get_name (),
8108 section->get_file_name ());
8109
8110 section->read (objfile);
8111
8112 info_ptr = section->buffer;
8113
8114 while (info_ptr < section->buffer + section->size)
8115 {
8116 struct dwarf2_per_cu_data *this_cu;
8117
8118 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8119
8120 comp_unit_head cu_header;
8121 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8122 abbrev_section, info_ptr,
8123 rcuh_kind::COMPILE);
8124
8125 /* Save the compilation unit for later lookup. */
8126 if (cu_header.unit_type != DW_UT_type)
8127 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8128 else
8129 {
8130 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8131 sig_type->signature = cu_header.signature;
8132 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8133 this_cu = &sig_type->per_cu;
8134 }
8135 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8136 this_cu->sect_off = sect_off;
8137 this_cu->length = cu_header.length + cu_header.initial_length_size;
8138 this_cu->is_dwz = is_dwz;
8139 this_cu->section = section;
8140
8141 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8142
8143 info_ptr = info_ptr + this_cu->length;
8144 }
8145 }
8146
8147 /* Create a list of all compilation units in OBJFILE.
8148 This is only done for -readnow and building partial symtabs. */
8149
8150 static void
8151 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8152 {
8153 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8154 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8155 &per_objfile->per_bfd->abbrev, 0);
8156
8157 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8158 if (dwz != NULL)
8159 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8160 }
8161
8162 /* Process all loaded DIEs for compilation unit CU, starting at
8163 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8164 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8165 DW_AT_ranges). See the comments of add_partial_subprogram on how
8166 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8167
8168 static void
8169 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8170 CORE_ADDR *highpc, int set_addrmap,
8171 struct dwarf2_cu *cu)
8172 {
8173 struct partial_die_info *pdi;
8174
8175 /* Now, march along the PDI's, descending into ones which have
8176 interesting children but skipping the children of the other ones,
8177 until we reach the end of the compilation unit. */
8178
8179 pdi = first_die;
8180
8181 while (pdi != NULL)
8182 {
8183 pdi->fixup (cu);
8184
8185 /* Anonymous namespaces or modules have no name but have interesting
8186 children, so we need to look at them. Ditto for anonymous
8187 enums. */
8188
8189 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8190 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8191 || pdi->tag == DW_TAG_imported_unit
8192 || pdi->tag == DW_TAG_inlined_subroutine)
8193 {
8194 switch (pdi->tag)
8195 {
8196 case DW_TAG_subprogram:
8197 case DW_TAG_inlined_subroutine:
8198 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8199 if (cu->language == language_cplus)
8200 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8201 set_addrmap, cu);
8202 break;
8203 case DW_TAG_constant:
8204 case DW_TAG_variable:
8205 case DW_TAG_typedef:
8206 case DW_TAG_union_type:
8207 if (!pdi->is_declaration
8208 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8209 {
8210 add_partial_symbol (pdi, cu);
8211 }
8212 break;
8213 case DW_TAG_class_type:
8214 case DW_TAG_interface_type:
8215 case DW_TAG_structure_type:
8216 if (!pdi->is_declaration)
8217 {
8218 add_partial_symbol (pdi, cu);
8219 }
8220 if ((cu->language == language_rust
8221 || cu->language == language_cplus) && pdi->has_children)
8222 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8223 set_addrmap, cu);
8224 break;
8225 case DW_TAG_enumeration_type:
8226 if (!pdi->is_declaration)
8227 add_partial_enumeration (pdi, cu);
8228 break;
8229 case DW_TAG_base_type:
8230 case DW_TAG_subrange_type:
8231 /* File scope base type definitions are added to the partial
8232 symbol table. */
8233 add_partial_symbol (pdi, cu);
8234 break;
8235 case DW_TAG_namespace:
8236 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8237 break;
8238 case DW_TAG_module:
8239 if (!pdi->is_declaration)
8240 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8241 break;
8242 case DW_TAG_imported_unit:
8243 {
8244 struct dwarf2_per_cu_data *per_cu;
8245
8246 /* For now we don't handle imported units in type units. */
8247 if (cu->per_cu->is_debug_types)
8248 {
8249 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8250 " supported in type units [in module %s]"),
8251 objfile_name (cu->per_objfile->objfile));
8252 }
8253
8254 per_cu = dwarf2_find_containing_comp_unit
8255 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8256
8257 /* Go read the partial unit, if needed. */
8258 if (per_cu->v.psymtab == NULL)
8259 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8260 cu->language);
8261
8262 cu->per_cu->imported_symtabs_push (per_cu);
8263 }
8264 break;
8265 case DW_TAG_imported_declaration:
8266 add_partial_symbol (pdi, cu);
8267 break;
8268 default:
8269 break;
8270 }
8271 }
8272
8273 /* If the die has a sibling, skip to the sibling. */
8274
8275 pdi = pdi->die_sibling;
8276 }
8277 }
8278
8279 /* Functions used to compute the fully scoped name of a partial DIE.
8280
8281 Normally, this is simple. For C++, the parent DIE's fully scoped
8282 name is concatenated with "::" and the partial DIE's name.
8283 Enumerators are an exception; they use the scope of their parent
8284 enumeration type, i.e. the name of the enumeration type is not
8285 prepended to the enumerator.
8286
8287 There are two complexities. One is DW_AT_specification; in this
8288 case "parent" means the parent of the target of the specification,
8289 instead of the direct parent of the DIE. The other is compilers
8290 which do not emit DW_TAG_namespace; in this case we try to guess
8291 the fully qualified name of structure types from their members'
8292 linkage names. This must be done using the DIE's children rather
8293 than the children of any DW_AT_specification target. We only need
8294 to do this for structures at the top level, i.e. if the target of
8295 any DW_AT_specification (if any; otherwise the DIE itself) does not
8296 have a parent. */
8297
8298 /* Compute the scope prefix associated with PDI's parent, in
8299 compilation unit CU. The result will be allocated on CU's
8300 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8301 field. NULL is returned if no prefix is necessary. */
8302 static const char *
8303 partial_die_parent_scope (struct partial_die_info *pdi,
8304 struct dwarf2_cu *cu)
8305 {
8306 const char *grandparent_scope;
8307 struct partial_die_info *parent, *real_pdi;
8308
8309 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8310 then this means the parent of the specification DIE. */
8311
8312 real_pdi = pdi;
8313 while (real_pdi->has_specification)
8314 {
8315 auto res = find_partial_die (real_pdi->spec_offset,
8316 real_pdi->spec_is_dwz, cu);
8317 real_pdi = res.pdi;
8318 cu = res.cu;
8319 }
8320
8321 parent = real_pdi->die_parent;
8322 if (parent == NULL)
8323 return NULL;
8324
8325 if (parent->scope_set)
8326 return parent->scope;
8327
8328 parent->fixup (cu);
8329
8330 grandparent_scope = partial_die_parent_scope (parent, cu);
8331
8332 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8333 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8334 Work around this problem here. */
8335 if (cu->language == language_cplus
8336 && parent->tag == DW_TAG_namespace
8337 && strcmp (parent->name (cu), "::") == 0
8338 && grandparent_scope == NULL)
8339 {
8340 parent->scope = NULL;
8341 parent->scope_set = 1;
8342 return NULL;
8343 }
8344
8345 /* Nested subroutines in Fortran get a prefix. */
8346 if (pdi->tag == DW_TAG_enumerator)
8347 /* Enumerators should not get the name of the enumeration as a prefix. */
8348 parent->scope = grandparent_scope;
8349 else if (parent->tag == DW_TAG_namespace
8350 || parent->tag == DW_TAG_module
8351 || parent->tag == DW_TAG_structure_type
8352 || parent->tag == DW_TAG_class_type
8353 || parent->tag == DW_TAG_interface_type
8354 || parent->tag == DW_TAG_union_type
8355 || parent->tag == DW_TAG_enumeration_type
8356 || (cu->language == language_fortran
8357 && parent->tag == DW_TAG_subprogram
8358 && pdi->tag == DW_TAG_subprogram))
8359 {
8360 if (grandparent_scope == NULL)
8361 parent->scope = parent->name (cu);
8362 else
8363 parent->scope = typename_concat (&cu->comp_unit_obstack,
8364 grandparent_scope,
8365 parent->name (cu), 0, cu);
8366 }
8367 else
8368 {
8369 /* FIXME drow/2004-04-01: What should we be doing with
8370 function-local names? For partial symbols, we should probably be
8371 ignoring them. */
8372 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8373 dwarf_tag_name (parent->tag),
8374 sect_offset_str (pdi->sect_off));
8375 parent->scope = grandparent_scope;
8376 }
8377
8378 parent->scope_set = 1;
8379 return parent->scope;
8380 }
8381
8382 /* Return the fully scoped name associated with PDI, from compilation unit
8383 CU. The result will be allocated with malloc. */
8384
8385 static gdb::unique_xmalloc_ptr<char>
8386 partial_die_full_name (struct partial_die_info *pdi,
8387 struct dwarf2_cu *cu)
8388 {
8389 const char *parent_scope;
8390
8391 /* If this is a template instantiation, we can not work out the
8392 template arguments from partial DIEs. So, unfortunately, we have
8393 to go through the full DIEs. At least any work we do building
8394 types here will be reused if full symbols are loaded later. */
8395 if (pdi->has_template_arguments)
8396 {
8397 pdi->fixup (cu);
8398
8399 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8400 {
8401 struct die_info *die;
8402 struct attribute attr;
8403 struct dwarf2_cu *ref_cu = cu;
8404
8405 /* DW_FORM_ref_addr is using section offset. */
8406 attr.name = (enum dwarf_attribute) 0;
8407 attr.form = DW_FORM_ref_addr;
8408 attr.u.unsnd = to_underlying (pdi->sect_off);
8409 die = follow_die_ref (NULL, &attr, &ref_cu);
8410
8411 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8412 }
8413 }
8414
8415 parent_scope = partial_die_parent_scope (pdi, cu);
8416 if (parent_scope == NULL)
8417 return NULL;
8418 else
8419 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8420 pdi->name (cu),
8421 0, cu));
8422 }
8423
8424 static void
8425 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8426 {
8427 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8428 struct objfile *objfile = per_objfile->objfile;
8429 struct gdbarch *gdbarch = objfile->arch ();
8430 CORE_ADDR addr = 0;
8431 const char *actual_name = NULL;
8432 CORE_ADDR baseaddr;
8433
8434 baseaddr = objfile->text_section_offset ();
8435
8436 gdb::unique_xmalloc_ptr<char> built_actual_name
8437 = partial_die_full_name (pdi, cu);
8438 if (built_actual_name != NULL)
8439 actual_name = built_actual_name.get ();
8440
8441 if (actual_name == NULL)
8442 actual_name = pdi->name (cu);
8443
8444 partial_symbol psymbol;
8445 memset (&psymbol, 0, sizeof (psymbol));
8446 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8447 psymbol.ginfo.section = -1;
8448
8449 /* The code below indicates that the psymbol should be installed by
8450 setting this. */
8451 gdb::optional<psymbol_placement> where;
8452
8453 switch (pdi->tag)
8454 {
8455 case DW_TAG_inlined_subroutine:
8456 case DW_TAG_subprogram:
8457 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8458 - baseaddr);
8459 if (pdi->is_external
8460 || cu->language == language_ada
8461 || (cu->language == language_fortran
8462 && pdi->die_parent != NULL
8463 && pdi->die_parent->tag == DW_TAG_subprogram))
8464 {
8465 /* Normally, only "external" DIEs are part of the global scope.
8466 But in Ada and Fortran, we want to be able to access nested
8467 procedures globally. So all Ada and Fortran subprograms are
8468 stored in the global scope. */
8469 where = psymbol_placement::GLOBAL;
8470 }
8471 else
8472 where = psymbol_placement::STATIC;
8473
8474 psymbol.domain = VAR_DOMAIN;
8475 psymbol.aclass = LOC_BLOCK;
8476 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8477 psymbol.ginfo.value.address = addr;
8478
8479 if (pdi->main_subprogram && actual_name != NULL)
8480 set_objfile_main_name (objfile, actual_name, cu->language);
8481 break;
8482 case DW_TAG_constant:
8483 psymbol.domain = VAR_DOMAIN;
8484 psymbol.aclass = LOC_STATIC;
8485 where = (pdi->is_external
8486 ? psymbol_placement::GLOBAL
8487 : psymbol_placement::STATIC);
8488 break;
8489 case DW_TAG_variable:
8490 if (pdi->d.locdesc)
8491 addr = decode_locdesc (pdi->d.locdesc, cu);
8492
8493 if (pdi->d.locdesc
8494 && addr == 0
8495 && !per_objfile->per_bfd->has_section_at_zero)
8496 {
8497 /* A global or static variable may also have been stripped
8498 out by the linker if unused, in which case its address
8499 will be nullified; do not add such variables into partial
8500 symbol table then. */
8501 }
8502 else if (pdi->is_external)
8503 {
8504 /* Global Variable.
8505 Don't enter into the minimal symbol tables as there is
8506 a minimal symbol table entry from the ELF symbols already.
8507 Enter into partial symbol table if it has a location
8508 descriptor or a type.
8509 If the location descriptor is missing, new_symbol will create
8510 a LOC_UNRESOLVED symbol, the address of the variable will then
8511 be determined from the minimal symbol table whenever the variable
8512 is referenced.
8513 The address for the partial symbol table entry is not
8514 used by GDB, but it comes in handy for debugging partial symbol
8515 table building. */
8516
8517 if (pdi->d.locdesc || pdi->has_type)
8518 {
8519 psymbol.domain = VAR_DOMAIN;
8520 psymbol.aclass = LOC_STATIC;
8521 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8522 psymbol.ginfo.value.address = addr;
8523 where = psymbol_placement::GLOBAL;
8524 }
8525 }
8526 else
8527 {
8528 int has_loc = pdi->d.locdesc != NULL;
8529
8530 /* Static Variable. Skip symbols whose value we cannot know (those
8531 without location descriptors or constant values). */
8532 if (!has_loc && !pdi->has_const_value)
8533 return;
8534
8535 psymbol.domain = VAR_DOMAIN;
8536 psymbol.aclass = LOC_STATIC;
8537 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8538 if (has_loc)
8539 psymbol.ginfo.value.address = addr;
8540 where = psymbol_placement::STATIC;
8541 }
8542 break;
8543 case DW_TAG_typedef:
8544 case DW_TAG_base_type:
8545 case DW_TAG_subrange_type:
8546 psymbol.domain = VAR_DOMAIN;
8547 psymbol.aclass = LOC_TYPEDEF;
8548 where = psymbol_placement::STATIC;
8549 break;
8550 case DW_TAG_imported_declaration:
8551 case DW_TAG_namespace:
8552 psymbol.domain = VAR_DOMAIN;
8553 psymbol.aclass = LOC_TYPEDEF;
8554 where = psymbol_placement::GLOBAL;
8555 break;
8556 case DW_TAG_module:
8557 /* With Fortran 77 there might be a "BLOCK DATA" module
8558 available without any name. If so, we skip the module as it
8559 doesn't bring any value. */
8560 if (actual_name != nullptr)
8561 {
8562 psymbol.domain = MODULE_DOMAIN;
8563 psymbol.aclass = LOC_TYPEDEF;
8564 where = psymbol_placement::GLOBAL;
8565 }
8566 break;
8567 case DW_TAG_class_type:
8568 case DW_TAG_interface_type:
8569 case DW_TAG_structure_type:
8570 case DW_TAG_union_type:
8571 case DW_TAG_enumeration_type:
8572 /* Skip external references. The DWARF standard says in the section
8573 about "Structure, Union, and Class Type Entries": "An incomplete
8574 structure, union or class type is represented by a structure,
8575 union or class entry that does not have a byte size attribute
8576 and that has a DW_AT_declaration attribute." */
8577 if (!pdi->has_byte_size && pdi->is_declaration)
8578 return;
8579
8580 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8581 static vs. global. */
8582 psymbol.domain = STRUCT_DOMAIN;
8583 psymbol.aclass = LOC_TYPEDEF;
8584 where = (cu->language == language_cplus
8585 ? psymbol_placement::GLOBAL
8586 : psymbol_placement::STATIC);
8587 break;
8588 case DW_TAG_enumerator:
8589 psymbol.domain = VAR_DOMAIN;
8590 psymbol.aclass = LOC_CONST;
8591 where = (cu->language == language_cplus
8592 ? psymbol_placement::GLOBAL
8593 : psymbol_placement::STATIC);
8594 break;
8595 default:
8596 break;
8597 }
8598
8599 if (where.has_value ())
8600 {
8601 if (built_actual_name != nullptr)
8602 actual_name = objfile->intern (actual_name);
8603 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8604 psymbol.ginfo.set_linkage_name (actual_name);
8605 else
8606 {
8607 psymbol.ginfo.set_demangled_name (actual_name,
8608 &objfile->objfile_obstack);
8609 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8610 }
8611 add_psymbol_to_list (psymbol, *where, objfile);
8612 }
8613 }
8614
8615 /* Read a partial die corresponding to a namespace; also, add a symbol
8616 corresponding to that namespace to the symbol table. NAMESPACE is
8617 the name of the enclosing namespace. */
8618
8619 static void
8620 add_partial_namespace (struct partial_die_info *pdi,
8621 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8622 int set_addrmap, struct dwarf2_cu *cu)
8623 {
8624 /* Add a symbol for the namespace. */
8625
8626 add_partial_symbol (pdi, cu);
8627
8628 /* Now scan partial symbols in that namespace. */
8629
8630 if (pdi->has_children)
8631 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8632 }
8633
8634 /* Read a partial die corresponding to a Fortran module. */
8635
8636 static void
8637 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8638 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8639 {
8640 /* Add a symbol for the namespace. */
8641
8642 add_partial_symbol (pdi, cu);
8643
8644 /* Now scan partial symbols in that module. */
8645
8646 if (pdi->has_children)
8647 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8648 }
8649
8650 /* Read a partial die corresponding to a subprogram or an inlined
8651 subprogram and create a partial symbol for that subprogram.
8652 When the CU language allows it, this routine also defines a partial
8653 symbol for each nested subprogram that this subprogram contains.
8654 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8655 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8656
8657 PDI may also be a lexical block, in which case we simply search
8658 recursively for subprograms defined inside that lexical block.
8659 Again, this is only performed when the CU language allows this
8660 type of definitions. */
8661
8662 static void
8663 add_partial_subprogram (struct partial_die_info *pdi,
8664 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8665 int set_addrmap, struct dwarf2_cu *cu)
8666 {
8667 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8668 {
8669 if (pdi->has_pc_info)
8670 {
8671 if (pdi->lowpc < *lowpc)
8672 *lowpc = pdi->lowpc;
8673 if (pdi->highpc > *highpc)
8674 *highpc = pdi->highpc;
8675 if (set_addrmap)
8676 {
8677 struct objfile *objfile = cu->per_objfile->objfile;
8678 struct gdbarch *gdbarch = objfile->arch ();
8679 CORE_ADDR baseaddr;
8680 CORE_ADDR this_highpc;
8681 CORE_ADDR this_lowpc;
8682
8683 baseaddr = objfile->text_section_offset ();
8684 this_lowpc
8685 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8686 pdi->lowpc + baseaddr)
8687 - baseaddr);
8688 this_highpc
8689 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8690 pdi->highpc + baseaddr)
8691 - baseaddr);
8692 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8693 this_lowpc, this_highpc - 1,
8694 cu->per_cu->v.psymtab);
8695 }
8696 }
8697
8698 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8699 {
8700 if (!pdi->is_declaration)
8701 /* Ignore subprogram DIEs that do not have a name, they are
8702 illegal. Do not emit a complaint at this point, we will
8703 do so when we convert this psymtab into a symtab. */
8704 if (pdi->name (cu))
8705 add_partial_symbol (pdi, cu);
8706 }
8707 }
8708
8709 if (! pdi->has_children)
8710 return;
8711
8712 if (cu->language == language_ada || cu->language == language_fortran)
8713 {
8714 pdi = pdi->die_child;
8715 while (pdi != NULL)
8716 {
8717 pdi->fixup (cu);
8718 if (pdi->tag == DW_TAG_subprogram
8719 || pdi->tag == DW_TAG_inlined_subroutine
8720 || pdi->tag == DW_TAG_lexical_block)
8721 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8722 pdi = pdi->die_sibling;
8723 }
8724 }
8725 }
8726
8727 /* Read a partial die corresponding to an enumeration type. */
8728
8729 static void
8730 add_partial_enumeration (struct partial_die_info *enum_pdi,
8731 struct dwarf2_cu *cu)
8732 {
8733 struct partial_die_info *pdi;
8734
8735 if (enum_pdi->name (cu) != NULL)
8736 add_partial_symbol (enum_pdi, cu);
8737
8738 pdi = enum_pdi->die_child;
8739 while (pdi)
8740 {
8741 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8742 complaint (_("malformed enumerator DIE ignored"));
8743 else
8744 add_partial_symbol (pdi, cu);
8745 pdi = pdi->die_sibling;
8746 }
8747 }
8748
8749 /* Return the initial uleb128 in the die at INFO_PTR. */
8750
8751 static unsigned int
8752 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8753 {
8754 unsigned int bytes_read;
8755
8756 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8757 }
8758
8759 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8760 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8761
8762 Return the corresponding abbrev, or NULL if the number is zero (indicating
8763 an empty DIE). In either case *BYTES_READ will be set to the length of
8764 the initial number. */
8765
8766 static struct abbrev_info *
8767 peek_die_abbrev (const die_reader_specs &reader,
8768 const gdb_byte *info_ptr, unsigned int *bytes_read)
8769 {
8770 dwarf2_cu *cu = reader.cu;
8771 bfd *abfd = cu->per_objfile->objfile->obfd;
8772 unsigned int abbrev_number
8773 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8774
8775 if (abbrev_number == 0)
8776 return NULL;
8777
8778 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8779 if (!abbrev)
8780 {
8781 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8782 " at offset %s [in module %s]"),
8783 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8784 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8785 }
8786
8787 return abbrev;
8788 }
8789
8790 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8791 Returns a pointer to the end of a series of DIEs, terminated by an empty
8792 DIE. Any children of the skipped DIEs will also be skipped. */
8793
8794 static const gdb_byte *
8795 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8796 {
8797 while (1)
8798 {
8799 unsigned int bytes_read;
8800 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8801
8802 if (abbrev == NULL)
8803 return info_ptr + bytes_read;
8804 else
8805 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8806 }
8807 }
8808
8809 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8810 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8811 abbrev corresponding to that skipped uleb128 should be passed in
8812 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8813 children. */
8814
8815 static const gdb_byte *
8816 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8817 struct abbrev_info *abbrev)
8818 {
8819 unsigned int bytes_read;
8820 struct attribute attr;
8821 bfd *abfd = reader->abfd;
8822 struct dwarf2_cu *cu = reader->cu;
8823 const gdb_byte *buffer = reader->buffer;
8824 const gdb_byte *buffer_end = reader->buffer_end;
8825 unsigned int form, i;
8826
8827 for (i = 0; i < abbrev->num_attrs; i++)
8828 {
8829 /* The only abbrev we care about is DW_AT_sibling. */
8830 if (abbrev->attrs[i].name == DW_AT_sibling)
8831 {
8832 bool ignored;
8833 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8834 &ignored);
8835 if (attr.form == DW_FORM_ref_addr)
8836 complaint (_("ignoring absolute DW_AT_sibling"));
8837 else
8838 {
8839 sect_offset off = attr.get_ref_die_offset ();
8840 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8841
8842 if (sibling_ptr < info_ptr)
8843 complaint (_("DW_AT_sibling points backwards"));
8844 else if (sibling_ptr > reader->buffer_end)
8845 reader->die_section->overflow_complaint ();
8846 else
8847 return sibling_ptr;
8848 }
8849 }
8850
8851 /* If it isn't DW_AT_sibling, skip this attribute. */
8852 form = abbrev->attrs[i].form;
8853 skip_attribute:
8854 switch (form)
8855 {
8856 case DW_FORM_ref_addr:
8857 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8858 and later it is offset sized. */
8859 if (cu->header.version == 2)
8860 info_ptr += cu->header.addr_size;
8861 else
8862 info_ptr += cu->header.offset_size;
8863 break;
8864 case DW_FORM_GNU_ref_alt:
8865 info_ptr += cu->header.offset_size;
8866 break;
8867 case DW_FORM_addr:
8868 info_ptr += cu->header.addr_size;
8869 break;
8870 case DW_FORM_data1:
8871 case DW_FORM_ref1:
8872 case DW_FORM_flag:
8873 case DW_FORM_strx1:
8874 info_ptr += 1;
8875 break;
8876 case DW_FORM_flag_present:
8877 case DW_FORM_implicit_const:
8878 break;
8879 case DW_FORM_data2:
8880 case DW_FORM_ref2:
8881 case DW_FORM_strx2:
8882 info_ptr += 2;
8883 break;
8884 case DW_FORM_strx3:
8885 info_ptr += 3;
8886 break;
8887 case DW_FORM_data4:
8888 case DW_FORM_ref4:
8889 case DW_FORM_strx4:
8890 info_ptr += 4;
8891 break;
8892 case DW_FORM_data8:
8893 case DW_FORM_ref8:
8894 case DW_FORM_ref_sig8:
8895 info_ptr += 8;
8896 break;
8897 case DW_FORM_data16:
8898 info_ptr += 16;
8899 break;
8900 case DW_FORM_string:
8901 read_direct_string (abfd, info_ptr, &bytes_read);
8902 info_ptr += bytes_read;
8903 break;
8904 case DW_FORM_sec_offset:
8905 case DW_FORM_strp:
8906 case DW_FORM_GNU_strp_alt:
8907 info_ptr += cu->header.offset_size;
8908 break;
8909 case DW_FORM_exprloc:
8910 case DW_FORM_block:
8911 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8912 info_ptr += bytes_read;
8913 break;
8914 case DW_FORM_block1:
8915 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8916 break;
8917 case DW_FORM_block2:
8918 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8919 break;
8920 case DW_FORM_block4:
8921 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8922 break;
8923 case DW_FORM_addrx:
8924 case DW_FORM_strx:
8925 case DW_FORM_sdata:
8926 case DW_FORM_udata:
8927 case DW_FORM_ref_udata:
8928 case DW_FORM_GNU_addr_index:
8929 case DW_FORM_GNU_str_index:
8930 case DW_FORM_rnglistx:
8931 case DW_FORM_loclistx:
8932 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8933 break;
8934 case DW_FORM_indirect:
8935 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8936 info_ptr += bytes_read;
8937 /* We need to continue parsing from here, so just go back to
8938 the top. */
8939 goto skip_attribute;
8940
8941 default:
8942 error (_("Dwarf Error: Cannot handle %s "
8943 "in DWARF reader [in module %s]"),
8944 dwarf_form_name (form),
8945 bfd_get_filename (abfd));
8946 }
8947 }
8948
8949 if (abbrev->has_children)
8950 return skip_children (reader, info_ptr);
8951 else
8952 return info_ptr;
8953 }
8954
8955 /* Locate ORIG_PDI's sibling.
8956 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8957
8958 static const gdb_byte *
8959 locate_pdi_sibling (const struct die_reader_specs *reader,
8960 struct partial_die_info *orig_pdi,
8961 const gdb_byte *info_ptr)
8962 {
8963 /* Do we know the sibling already? */
8964
8965 if (orig_pdi->sibling)
8966 return orig_pdi->sibling;
8967
8968 /* Are there any children to deal with? */
8969
8970 if (!orig_pdi->has_children)
8971 return info_ptr;
8972
8973 /* Skip the children the long way. */
8974
8975 return skip_children (reader, info_ptr);
8976 }
8977
8978 /* Expand this partial symbol table into a full symbol table. SELF is
8979 not NULL. */
8980
8981 void
8982 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8983 {
8984 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8985
8986 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8987
8988 /* If this psymtab is constructed from a debug-only objfile, the
8989 has_section_at_zero flag will not necessarily be correct. We
8990 can get the correct value for this flag by looking at the data
8991 associated with the (presumably stripped) associated objfile. */
8992 if (objfile->separate_debug_objfile_backlink)
8993 {
8994 dwarf2_per_objfile *per_objfile_backlink
8995 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8996
8997 per_objfile->per_bfd->has_section_at_zero
8998 = per_objfile_backlink->per_bfd->has_section_at_zero;
8999 }
9000
9001 expand_psymtab (objfile);
9002
9003 process_cu_includes (per_objfile);
9004 }
9005 \f
9006 /* Reading in full CUs. */
9007
9008 /* Add PER_CU to the queue. */
9009
9010 static void
9011 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9012 dwarf2_per_objfile *per_objfile,
9013 enum language pretend_language)
9014 {
9015 per_cu->queued = 1;
9016 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
9017 }
9018
9019 /* If PER_CU is not yet queued, add it to the queue.
9020 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9021 dependency.
9022 The result is non-zero if PER_CU was queued, otherwise the result is zero
9023 meaning either PER_CU is already queued or it is already loaded.
9024
9025 N.B. There is an invariant here that if a CU is queued then it is loaded.
9026 The caller is required to load PER_CU if we return non-zero. */
9027
9028 static int
9029 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9030 dwarf2_per_cu_data *per_cu,
9031 dwarf2_per_objfile *per_objfile,
9032 enum language pretend_language)
9033 {
9034 /* We may arrive here during partial symbol reading, if we need full
9035 DIEs to process an unusual case (e.g. template arguments). Do
9036 not queue PER_CU, just tell our caller to load its DIEs. */
9037 if (per_cu->per_bfd->reading_partial_symbols)
9038 {
9039 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9040
9041 if (cu == NULL || cu->dies == NULL)
9042 return 1;
9043 return 0;
9044 }
9045
9046 /* Mark the dependence relation so that we don't flush PER_CU
9047 too early. */
9048 if (dependent_cu != NULL)
9049 dwarf2_add_dependence (dependent_cu, per_cu);
9050
9051 /* If it's already on the queue, we have nothing to do. */
9052 if (per_cu->queued)
9053 return 0;
9054
9055 /* If the compilation unit is already loaded, just mark it as
9056 used. */
9057 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9058 if (cu != nullptr)
9059 {
9060 cu->last_used = 0;
9061 return 0;
9062 }
9063
9064 /* Add it to the queue. */
9065 queue_comp_unit (per_cu, per_objfile, pretend_language);
9066
9067 return 1;
9068 }
9069
9070 /* Process the queue. */
9071
9072 static void
9073 process_queue (dwarf2_per_objfile *per_objfile)
9074 {
9075 if (dwarf_read_debug)
9076 {
9077 fprintf_unfiltered (gdb_stdlog,
9078 "Expanding one or more symtabs of objfile %s ...\n",
9079 objfile_name (per_objfile->objfile));
9080 }
9081
9082 /* The queue starts out with one item, but following a DIE reference
9083 may load a new CU, adding it to the end of the queue. */
9084 while (!per_objfile->per_bfd->queue.empty ())
9085 {
9086 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
9087 dwarf2_per_cu_data *per_cu = item.per_cu;
9088
9089 if (!per_objfile->symtab_set_p (per_cu))
9090 {
9091 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9092
9093 /* Skip dummy CUs. */
9094 if (cu != nullptr)
9095 {
9096 unsigned int debug_print_threshold;
9097 char buf[100];
9098
9099 if (per_cu->is_debug_types)
9100 {
9101 struct signatured_type *sig_type =
9102 (struct signatured_type *) per_cu;
9103
9104 sprintf (buf, "TU %s at offset %s",
9105 hex_string (sig_type->signature),
9106 sect_offset_str (per_cu->sect_off));
9107 /* There can be 100s of TUs.
9108 Only print them in verbose mode. */
9109 debug_print_threshold = 2;
9110 }
9111 else
9112 {
9113 sprintf (buf, "CU at offset %s",
9114 sect_offset_str (per_cu->sect_off));
9115 debug_print_threshold = 1;
9116 }
9117
9118 if (dwarf_read_debug >= debug_print_threshold)
9119 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9120
9121 if (per_cu->is_debug_types)
9122 process_full_type_unit (cu, item.pretend_language);
9123 else
9124 process_full_comp_unit (cu, item.pretend_language);
9125
9126 if (dwarf_read_debug >= debug_print_threshold)
9127 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9128 }
9129 }
9130
9131 per_cu->queued = 0;
9132 per_objfile->per_bfd->queue.pop ();
9133 }
9134
9135 if (dwarf_read_debug)
9136 {
9137 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9138 objfile_name (per_objfile->objfile));
9139 }
9140 }
9141
9142 /* Read in full symbols for PST, and anything it depends on. */
9143
9144 void
9145 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9146 {
9147 gdb_assert (!readin_p (objfile));
9148
9149 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9150 free_cached_comp_units freer (per_objfile);
9151 expand_dependencies (objfile);
9152
9153 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9154 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9155 }
9156
9157 /* See psympriv.h. */
9158
9159 bool
9160 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9161 {
9162 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9163 return per_objfile->symtab_set_p (per_cu_data);
9164 }
9165
9166 /* See psympriv.h. */
9167
9168 compunit_symtab *
9169 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9170 {
9171 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9172 return per_objfile->get_symtab (per_cu_data);
9173 }
9174
9175 /* Trivial hash function for die_info: the hash value of a DIE
9176 is its offset in .debug_info for this objfile. */
9177
9178 static hashval_t
9179 die_hash (const void *item)
9180 {
9181 const struct die_info *die = (const struct die_info *) item;
9182
9183 return to_underlying (die->sect_off);
9184 }
9185
9186 /* Trivial comparison function for die_info structures: two DIEs
9187 are equal if they have the same offset. */
9188
9189 static int
9190 die_eq (const void *item_lhs, const void *item_rhs)
9191 {
9192 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9193 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9194
9195 return die_lhs->sect_off == die_rhs->sect_off;
9196 }
9197
9198 /* Load the DIEs associated with PER_CU into memory. */
9199
9200 static void
9201 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9202 dwarf2_per_objfile *per_objfile,
9203 bool skip_partial,
9204 enum language pretend_language)
9205 {
9206 gdb_assert (! this_cu->is_debug_types);
9207
9208 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9209 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9210 if (reader.dummy_p)
9211 return;
9212
9213 struct dwarf2_cu *cu = reader.cu;
9214 const gdb_byte *info_ptr = reader.info_ptr;
9215
9216 gdb_assert (cu->die_hash == NULL);
9217 cu->die_hash =
9218 htab_create_alloc_ex (cu->header.length / 12,
9219 die_hash,
9220 die_eq,
9221 NULL,
9222 &cu->comp_unit_obstack,
9223 hashtab_obstack_allocate,
9224 dummy_obstack_deallocate);
9225
9226 if (reader.comp_unit_die->has_children)
9227 reader.comp_unit_die->child
9228 = read_die_and_siblings (&reader, reader.info_ptr,
9229 &info_ptr, reader.comp_unit_die);
9230 cu->dies = reader.comp_unit_die;
9231 /* comp_unit_die is not stored in die_hash, no need. */
9232
9233 /* We try not to read any attributes in this function, because not
9234 all CUs needed for references have been loaded yet, and symbol
9235 table processing isn't initialized. But we have to set the CU language,
9236 or we won't be able to build types correctly.
9237 Similarly, if we do not read the producer, we can not apply
9238 producer-specific interpretation. */
9239 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9240
9241 reader.keep ();
9242 }
9243
9244 /* Add a DIE to the delayed physname list. */
9245
9246 static void
9247 add_to_method_list (struct type *type, int fnfield_index, int index,
9248 const char *name, struct die_info *die,
9249 struct dwarf2_cu *cu)
9250 {
9251 struct delayed_method_info mi;
9252 mi.type = type;
9253 mi.fnfield_index = fnfield_index;
9254 mi.index = index;
9255 mi.name = name;
9256 mi.die = die;
9257 cu->method_list.push_back (mi);
9258 }
9259
9260 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9261 "const" / "volatile". If so, decrements LEN by the length of the
9262 modifier and return true. Otherwise return false. */
9263
9264 template<size_t N>
9265 static bool
9266 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9267 {
9268 size_t mod_len = sizeof (mod) - 1;
9269 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9270 {
9271 len -= mod_len;
9272 return true;
9273 }
9274 return false;
9275 }
9276
9277 /* Compute the physnames of any methods on the CU's method list.
9278
9279 The computation of method physnames is delayed in order to avoid the
9280 (bad) condition that one of the method's formal parameters is of an as yet
9281 incomplete type. */
9282
9283 static void
9284 compute_delayed_physnames (struct dwarf2_cu *cu)
9285 {
9286 /* Only C++ delays computing physnames. */
9287 if (cu->method_list.empty ())
9288 return;
9289 gdb_assert (cu->language == language_cplus);
9290
9291 for (const delayed_method_info &mi : cu->method_list)
9292 {
9293 const char *physname;
9294 struct fn_fieldlist *fn_flp
9295 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9296 physname = dwarf2_physname (mi.name, mi.die, cu);
9297 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9298 = physname ? physname : "";
9299
9300 /* Since there's no tag to indicate whether a method is a
9301 const/volatile overload, extract that information out of the
9302 demangled name. */
9303 if (physname != NULL)
9304 {
9305 size_t len = strlen (physname);
9306
9307 while (1)
9308 {
9309 if (physname[len] == ')') /* shortcut */
9310 break;
9311 else if (check_modifier (physname, len, " const"))
9312 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9313 else if (check_modifier (physname, len, " volatile"))
9314 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9315 else
9316 break;
9317 }
9318 }
9319 }
9320
9321 /* The list is no longer needed. */
9322 cu->method_list.clear ();
9323 }
9324
9325 /* Go objects should be embedded in a DW_TAG_module DIE,
9326 and it's not clear if/how imported objects will appear.
9327 To keep Go support simple until that's worked out,
9328 go back through what we've read and create something usable.
9329 We could do this while processing each DIE, and feels kinda cleaner,
9330 but that way is more invasive.
9331 This is to, for example, allow the user to type "p var" or "b main"
9332 without having to specify the package name, and allow lookups
9333 of module.object to work in contexts that use the expression
9334 parser. */
9335
9336 static void
9337 fixup_go_packaging (struct dwarf2_cu *cu)
9338 {
9339 gdb::unique_xmalloc_ptr<char> package_name;
9340 struct pending *list;
9341 int i;
9342
9343 for (list = *cu->get_builder ()->get_global_symbols ();
9344 list != NULL;
9345 list = list->next)
9346 {
9347 for (i = 0; i < list->nsyms; ++i)
9348 {
9349 struct symbol *sym = list->symbol[i];
9350
9351 if (sym->language () == language_go
9352 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9353 {
9354 gdb::unique_xmalloc_ptr<char> this_package_name
9355 (go_symbol_package_name (sym));
9356
9357 if (this_package_name == NULL)
9358 continue;
9359 if (package_name == NULL)
9360 package_name = std::move (this_package_name);
9361 else
9362 {
9363 struct objfile *objfile = cu->per_objfile->objfile;
9364 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9365 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9366 (symbol_symtab (sym) != NULL
9367 ? symtab_to_filename_for_display
9368 (symbol_symtab (sym))
9369 : objfile_name (objfile)),
9370 this_package_name.get (), package_name.get ());
9371 }
9372 }
9373 }
9374 }
9375
9376 if (package_name != NULL)
9377 {
9378 struct objfile *objfile = cu->per_objfile->objfile;
9379 const char *saved_package_name = objfile->intern (package_name.get ());
9380 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9381 saved_package_name);
9382 struct symbol *sym;
9383
9384 sym = new (&objfile->objfile_obstack) symbol;
9385 sym->set_language (language_go, &objfile->objfile_obstack);
9386 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9387 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9388 e.g., "main" finds the "main" module and not C's main(). */
9389 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9390 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9391 SYMBOL_TYPE (sym) = type;
9392
9393 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9394 }
9395 }
9396
9397 /* Allocate a fully-qualified name consisting of the two parts on the
9398 obstack. */
9399
9400 static const char *
9401 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9402 {
9403 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9404 }
9405
9406 /* A helper that allocates a variant part to attach to a Rust enum
9407 type. OBSTACK is where the results should be allocated. TYPE is
9408 the type we're processing. DISCRIMINANT_INDEX is the index of the
9409 discriminant. It must be the index of one of the fields of TYPE.
9410 DEFAULT_INDEX is the index of the default field; or -1 if there is
9411 no default. RANGES is indexed by "effective" field number (the
9412 field index, but omitting the discriminant and default fields) and
9413 must hold the discriminant values used by the variants. Note that
9414 RANGES must have a lifetime at least as long as OBSTACK -- either
9415 already allocated on it, or static. */
9416
9417 static void
9418 alloc_rust_variant (struct obstack *obstack, struct type *type,
9419 int discriminant_index, int default_index,
9420 gdb::array_view<discriminant_range> ranges)
9421 {
9422 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9423 must be handled by the caller. */
9424 gdb_assert (discriminant_index >= 0
9425 && discriminant_index < type->num_fields ());
9426 gdb_assert (default_index == -1
9427 || (default_index >= 0 && default_index < type->num_fields ()));
9428
9429 /* We have one variant for each non-discriminant field. */
9430 int n_variants = type->num_fields () - 1;
9431
9432 variant *variants = new (obstack) variant[n_variants];
9433 int var_idx = 0;
9434 int range_idx = 0;
9435 for (int i = 0; i < type->num_fields (); ++i)
9436 {
9437 if (i == discriminant_index)
9438 continue;
9439
9440 variants[var_idx].first_field = i;
9441 variants[var_idx].last_field = i + 1;
9442
9443 /* The default field does not need a range, but other fields do.
9444 We skipped the discriminant above. */
9445 if (i != default_index)
9446 {
9447 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9448 ++range_idx;
9449 }
9450
9451 ++var_idx;
9452 }
9453
9454 gdb_assert (range_idx == ranges.size ());
9455 gdb_assert (var_idx == n_variants);
9456
9457 variant_part *part = new (obstack) variant_part;
9458 part->discriminant_index = discriminant_index;
9459 part->is_unsigned = TYPE_UNSIGNED (type->field (discriminant_index).type ());
9460 part->variants = gdb::array_view<variant> (variants, n_variants);
9461
9462 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9463 gdb::array_view<variant_part> *prop_value
9464 = new (storage) gdb::array_view<variant_part> (part, 1);
9465
9466 struct dynamic_prop prop;
9467 prop.kind = PROP_VARIANT_PARTS;
9468 prop.data.variant_parts = prop_value;
9469
9470 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9471 }
9472
9473 /* Some versions of rustc emitted enums in an unusual way.
9474
9475 Ordinary enums were emitted as unions. The first element of each
9476 structure in the union was named "RUST$ENUM$DISR". This element
9477 held the discriminant.
9478
9479 These versions of Rust also implemented the "non-zero"
9480 optimization. When the enum had two values, and one is empty and
9481 the other holds a pointer that cannot be zero, the pointer is used
9482 as the discriminant, with a zero value meaning the empty variant.
9483 Here, the union's first member is of the form
9484 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9485 where the fieldnos are the indices of the fields that should be
9486 traversed in order to find the field (which may be several fields deep)
9487 and the variantname is the name of the variant of the case when the
9488 field is zero.
9489
9490 This function recognizes whether TYPE is of one of these forms,
9491 and, if so, smashes it to be a variant type. */
9492
9493 static void
9494 quirk_rust_enum (struct type *type, struct objfile *objfile)
9495 {
9496 gdb_assert (type->code () == TYPE_CODE_UNION);
9497
9498 /* We don't need to deal with empty enums. */
9499 if (type->num_fields () == 0)
9500 return;
9501
9502 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9503 if (type->num_fields () == 1
9504 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9505 {
9506 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9507
9508 /* Decode the field name to find the offset of the
9509 discriminant. */
9510 ULONGEST bit_offset = 0;
9511 struct type *field_type = type->field (0).type ();
9512 while (name[0] >= '0' && name[0] <= '9')
9513 {
9514 char *tail;
9515 unsigned long index = strtoul (name, &tail, 10);
9516 name = tail;
9517 if (*name != '$'
9518 || index >= field_type->num_fields ()
9519 || (TYPE_FIELD_LOC_KIND (field_type, index)
9520 != FIELD_LOC_KIND_BITPOS))
9521 {
9522 complaint (_("Could not parse Rust enum encoding string \"%s\""
9523 "[in module %s]"),
9524 TYPE_FIELD_NAME (type, 0),
9525 objfile_name (objfile));
9526 return;
9527 }
9528 ++name;
9529
9530 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9531 field_type = field_type->field (index).type ();
9532 }
9533
9534 /* Smash this type to be a structure type. We have to do this
9535 because the type has already been recorded. */
9536 type->set_code (TYPE_CODE_STRUCT);
9537 type->set_num_fields (3);
9538 /* Save the field we care about. */
9539 struct field saved_field = type->field (0);
9540 type->set_fields
9541 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9542
9543 /* Put the discriminant at index 0. */
9544 type->field (0).set_type (field_type);
9545 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9546 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9547 SET_FIELD_BITPOS (type->field (0), bit_offset);
9548
9549 /* The order of fields doesn't really matter, so put the real
9550 field at index 1 and the data-less field at index 2. */
9551 type->field (1) = saved_field;
9552 TYPE_FIELD_NAME (type, 1)
9553 = rust_last_path_segment (type->field (1).type ()->name ());
9554 type->field (1).type ()->set_name
9555 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9556 TYPE_FIELD_NAME (type, 1)));
9557
9558 const char *dataless_name
9559 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9560 name);
9561 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9562 dataless_name);
9563 type->field (2).set_type (dataless_type);
9564 /* NAME points into the original discriminant name, which
9565 already has the correct lifetime. */
9566 TYPE_FIELD_NAME (type, 2) = name;
9567 SET_FIELD_BITPOS (type->field (2), 0);
9568
9569 /* Indicate that this is a variant type. */
9570 static discriminant_range ranges[1] = { { 0, 0 } };
9571 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9572 }
9573 /* A union with a single anonymous field is probably an old-style
9574 univariant enum. */
9575 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9576 {
9577 /* Smash this type to be a structure type. We have to do this
9578 because the type has already been recorded. */
9579 type->set_code (TYPE_CODE_STRUCT);
9580
9581 struct type *field_type = type->field (0).type ();
9582 const char *variant_name
9583 = rust_last_path_segment (field_type->name ());
9584 TYPE_FIELD_NAME (type, 0) = variant_name;
9585 field_type->set_name
9586 (rust_fully_qualify (&objfile->objfile_obstack,
9587 type->name (), variant_name));
9588 }
9589 else
9590 {
9591 struct type *disr_type = nullptr;
9592 for (int i = 0; i < type->num_fields (); ++i)
9593 {
9594 disr_type = type->field (i).type ();
9595
9596 if (disr_type->code () != TYPE_CODE_STRUCT)
9597 {
9598 /* All fields of a true enum will be structs. */
9599 return;
9600 }
9601 else if (disr_type->num_fields () == 0)
9602 {
9603 /* Could be data-less variant, so keep going. */
9604 disr_type = nullptr;
9605 }
9606 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9607 "RUST$ENUM$DISR") != 0)
9608 {
9609 /* Not a Rust enum. */
9610 return;
9611 }
9612 else
9613 {
9614 /* Found one. */
9615 break;
9616 }
9617 }
9618
9619 /* If we got here without a discriminant, then it's probably
9620 just a union. */
9621 if (disr_type == nullptr)
9622 return;
9623
9624 /* Smash this type to be a structure type. We have to do this
9625 because the type has already been recorded. */
9626 type->set_code (TYPE_CODE_STRUCT);
9627
9628 /* Make space for the discriminant field. */
9629 struct field *disr_field = &disr_type->field (0);
9630 field *new_fields
9631 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9632 * sizeof (struct field)));
9633 memcpy (new_fields + 1, type->fields (),
9634 type->num_fields () * sizeof (struct field));
9635 type->set_fields (new_fields);
9636 type->set_num_fields (type->num_fields () + 1);
9637
9638 /* Install the discriminant at index 0 in the union. */
9639 type->field (0) = *disr_field;
9640 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9641 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9642
9643 /* We need a way to find the correct discriminant given a
9644 variant name. For convenience we build a map here. */
9645 struct type *enum_type = disr_field->type ();
9646 std::unordered_map<std::string, ULONGEST> discriminant_map;
9647 for (int i = 0; i < enum_type->num_fields (); ++i)
9648 {
9649 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9650 {
9651 const char *name
9652 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9653 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9654 }
9655 }
9656
9657 int n_fields = type->num_fields ();
9658 /* We don't need a range entry for the discriminant, but we do
9659 need one for every other field, as there is no default
9660 variant. */
9661 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9662 discriminant_range,
9663 n_fields - 1);
9664 /* Skip the discriminant here. */
9665 for (int i = 1; i < n_fields; ++i)
9666 {
9667 /* Find the final word in the name of this variant's type.
9668 That name can be used to look up the correct
9669 discriminant. */
9670 const char *variant_name
9671 = rust_last_path_segment (type->field (i).type ()->name ());
9672
9673 auto iter = discriminant_map.find (variant_name);
9674 if (iter != discriminant_map.end ())
9675 {
9676 ranges[i].low = iter->second;
9677 ranges[i].high = iter->second;
9678 }
9679
9680 /* Remove the discriminant field, if it exists. */
9681 struct type *sub_type = type->field (i).type ();
9682 if (sub_type->num_fields () > 0)
9683 {
9684 sub_type->set_num_fields (sub_type->num_fields () - 1);
9685 sub_type->set_fields (sub_type->fields () + 1);
9686 }
9687 TYPE_FIELD_NAME (type, i) = variant_name;
9688 sub_type->set_name
9689 (rust_fully_qualify (&objfile->objfile_obstack,
9690 type->name (), variant_name));
9691 }
9692
9693 /* Indicate that this is a variant type. */
9694 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9695 gdb::array_view<discriminant_range> (ranges,
9696 n_fields - 1));
9697 }
9698 }
9699
9700 /* Rewrite some Rust unions to be structures with variants parts. */
9701
9702 static void
9703 rust_union_quirks (struct dwarf2_cu *cu)
9704 {
9705 gdb_assert (cu->language == language_rust);
9706 for (type *type_ : cu->rust_unions)
9707 quirk_rust_enum (type_, cu->per_objfile->objfile);
9708 /* We don't need this any more. */
9709 cu->rust_unions.clear ();
9710 }
9711
9712 /* See read.h. */
9713
9714 type_unit_group_unshareable *
9715 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9716 {
9717 auto iter = this->m_type_units.find (tu_group);
9718 if (iter != this->m_type_units.end ())
9719 return iter->second.get ();
9720
9721 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9722 type_unit_group_unshareable *result = uniq.get ();
9723 this->m_type_units[tu_group] = std::move (uniq);
9724 return result;
9725 }
9726
9727 struct type *
9728 dwarf2_per_objfile::get_type_for_signatured_type
9729 (signatured_type *sig_type) const
9730 {
9731 auto iter = this->m_type_map.find (sig_type);
9732 if (iter == this->m_type_map.end ())
9733 return nullptr;
9734
9735 return iter->second;
9736 }
9737
9738 void dwarf2_per_objfile::set_type_for_signatured_type
9739 (signatured_type *sig_type, struct type *type)
9740 {
9741 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9742
9743 this->m_type_map[sig_type] = type;
9744 }
9745
9746 /* A helper function for computing the list of all symbol tables
9747 included by PER_CU. */
9748
9749 static void
9750 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9751 htab_t all_children, htab_t all_type_symtabs,
9752 dwarf2_per_cu_data *per_cu,
9753 dwarf2_per_objfile *per_objfile,
9754 struct compunit_symtab *immediate_parent)
9755 {
9756 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9757 if (*slot != NULL)
9758 {
9759 /* This inclusion and its children have been processed. */
9760 return;
9761 }
9762
9763 *slot = per_cu;
9764
9765 /* Only add a CU if it has a symbol table. */
9766 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9767 if (cust != NULL)
9768 {
9769 /* If this is a type unit only add its symbol table if we haven't
9770 seen it yet (type unit per_cu's can share symtabs). */
9771 if (per_cu->is_debug_types)
9772 {
9773 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9774 if (*slot == NULL)
9775 {
9776 *slot = cust;
9777 result->push_back (cust);
9778 if (cust->user == NULL)
9779 cust->user = immediate_parent;
9780 }
9781 }
9782 else
9783 {
9784 result->push_back (cust);
9785 if (cust->user == NULL)
9786 cust->user = immediate_parent;
9787 }
9788 }
9789
9790 if (!per_cu->imported_symtabs_empty ())
9791 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9792 {
9793 recursively_compute_inclusions (result, all_children,
9794 all_type_symtabs, ptr, per_objfile,
9795 cust);
9796 }
9797 }
9798
9799 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9800 PER_CU. */
9801
9802 static void
9803 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9804 dwarf2_per_objfile *per_objfile)
9805 {
9806 gdb_assert (! per_cu->is_debug_types);
9807
9808 if (!per_cu->imported_symtabs_empty ())
9809 {
9810 int len;
9811 std::vector<compunit_symtab *> result_symtabs;
9812 htab_t all_children, all_type_symtabs;
9813 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9814
9815 /* If we don't have a symtab, we can just skip this case. */
9816 if (cust == NULL)
9817 return;
9818
9819 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9820 NULL, xcalloc, xfree);
9821 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9822 NULL, xcalloc, xfree);
9823
9824 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9825 {
9826 recursively_compute_inclusions (&result_symtabs, all_children,
9827 all_type_symtabs, ptr, per_objfile,
9828 cust);
9829 }
9830
9831 /* Now we have a transitive closure of all the included symtabs. */
9832 len = result_symtabs.size ();
9833 cust->includes
9834 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9835 struct compunit_symtab *, len + 1);
9836 memcpy (cust->includes, result_symtabs.data (),
9837 len * sizeof (compunit_symtab *));
9838 cust->includes[len] = NULL;
9839
9840 htab_delete (all_children);
9841 htab_delete (all_type_symtabs);
9842 }
9843 }
9844
9845 /* Compute the 'includes' field for the symtabs of all the CUs we just
9846 read. */
9847
9848 static void
9849 process_cu_includes (dwarf2_per_objfile *per_objfile)
9850 {
9851 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9852 {
9853 if (! iter->is_debug_types)
9854 compute_compunit_symtab_includes (iter, per_objfile);
9855 }
9856
9857 per_objfile->per_bfd->just_read_cus.clear ();
9858 }
9859
9860 /* Generate full symbol information for CU, whose DIEs have
9861 already been loaded into memory. */
9862
9863 static void
9864 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9865 {
9866 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9867 struct objfile *objfile = per_objfile->objfile;
9868 struct gdbarch *gdbarch = objfile->arch ();
9869 CORE_ADDR lowpc, highpc;
9870 struct compunit_symtab *cust;
9871 CORE_ADDR baseaddr;
9872 struct block *static_block;
9873 CORE_ADDR addr;
9874
9875 baseaddr = objfile->text_section_offset ();
9876
9877 /* Clear the list here in case something was left over. */
9878 cu->method_list.clear ();
9879
9880 cu->language = pretend_language;
9881 cu->language_defn = language_def (cu->language);
9882
9883 /* Do line number decoding in read_file_scope () */
9884 process_die (cu->dies, cu);
9885
9886 /* For now fudge the Go package. */
9887 if (cu->language == language_go)
9888 fixup_go_packaging (cu);
9889
9890 /* Now that we have processed all the DIEs in the CU, all the types
9891 should be complete, and it should now be safe to compute all of the
9892 physnames. */
9893 compute_delayed_physnames (cu);
9894
9895 if (cu->language == language_rust)
9896 rust_union_quirks (cu);
9897
9898 /* Some compilers don't define a DW_AT_high_pc attribute for the
9899 compilation unit. If the DW_AT_high_pc is missing, synthesize
9900 it, by scanning the DIE's below the compilation unit. */
9901 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9902
9903 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9904 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9905
9906 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9907 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9908 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9909 addrmap to help ensure it has an accurate map of pc values belonging to
9910 this comp unit. */
9911 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9912
9913 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9914 SECT_OFF_TEXT (objfile),
9915 0);
9916
9917 if (cust != NULL)
9918 {
9919 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9920
9921 /* Set symtab language to language from DW_AT_language. If the
9922 compilation is from a C file generated by language preprocessors, do
9923 not set the language if it was already deduced by start_subfile. */
9924 if (!(cu->language == language_c
9925 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9926 COMPUNIT_FILETABS (cust)->language = cu->language;
9927
9928 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9929 produce DW_AT_location with location lists but it can be possibly
9930 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9931 there were bugs in prologue debug info, fixed later in GCC-4.5
9932 by "unwind info for epilogues" patch (which is not directly related).
9933
9934 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9935 needed, it would be wrong due to missing DW_AT_producer there.
9936
9937 Still one can confuse GDB by using non-standard GCC compilation
9938 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9939 */
9940 if (cu->has_loclist && gcc_4_minor >= 5)
9941 cust->locations_valid = 1;
9942
9943 if (gcc_4_minor >= 5)
9944 cust->epilogue_unwind_valid = 1;
9945
9946 cust->call_site_htab = cu->call_site_htab;
9947 }
9948
9949 per_objfile->set_symtab (cu->per_cu, cust);
9950
9951 /* Push it for inclusion processing later. */
9952 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9953
9954 /* Not needed any more. */
9955 cu->reset_builder ();
9956 }
9957
9958 /* Generate full symbol information for type unit CU, whose DIEs have
9959 already been loaded into memory. */
9960
9961 static void
9962 process_full_type_unit (dwarf2_cu *cu,
9963 enum language pretend_language)
9964 {
9965 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9966 struct objfile *objfile = per_objfile->objfile;
9967 struct compunit_symtab *cust;
9968 struct signatured_type *sig_type;
9969
9970 gdb_assert (cu->per_cu->is_debug_types);
9971 sig_type = (struct signatured_type *) cu->per_cu;
9972
9973 /* Clear the list here in case something was left over. */
9974 cu->method_list.clear ();
9975
9976 cu->language = pretend_language;
9977 cu->language_defn = language_def (cu->language);
9978
9979 /* The symbol tables are set up in read_type_unit_scope. */
9980 process_die (cu->dies, cu);
9981
9982 /* For now fudge the Go package. */
9983 if (cu->language == language_go)
9984 fixup_go_packaging (cu);
9985
9986 /* Now that we have processed all the DIEs in the CU, all the types
9987 should be complete, and it should now be safe to compute all of the
9988 physnames. */
9989 compute_delayed_physnames (cu);
9990
9991 if (cu->language == language_rust)
9992 rust_union_quirks (cu);
9993
9994 /* TUs share symbol tables.
9995 If this is the first TU to use this symtab, complete the construction
9996 of it with end_expandable_symtab. Otherwise, complete the addition of
9997 this TU's symbols to the existing symtab. */
9998 type_unit_group_unshareable *tug_unshare =
9999 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10000 if (tug_unshare->compunit_symtab == NULL)
10001 {
10002 buildsym_compunit *builder = cu->get_builder ();
10003 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10004 tug_unshare->compunit_symtab = cust;
10005
10006 if (cust != NULL)
10007 {
10008 /* Set symtab language to language from DW_AT_language. If the
10009 compilation is from a C file generated by language preprocessors,
10010 do not set the language if it was already deduced by
10011 start_subfile. */
10012 if (!(cu->language == language_c
10013 && COMPUNIT_FILETABS (cust)->language != language_c))
10014 COMPUNIT_FILETABS (cust)->language = cu->language;
10015 }
10016 }
10017 else
10018 {
10019 cu->get_builder ()->augment_type_symtab ();
10020 cust = tug_unshare->compunit_symtab;
10021 }
10022
10023 per_objfile->set_symtab (cu->per_cu, cust);
10024
10025 /* Not needed any more. */
10026 cu->reset_builder ();
10027 }
10028
10029 /* Process an imported unit DIE. */
10030
10031 static void
10032 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10033 {
10034 struct attribute *attr;
10035
10036 /* For now we don't handle imported units in type units. */
10037 if (cu->per_cu->is_debug_types)
10038 {
10039 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10040 " supported in type units [in module %s]"),
10041 objfile_name (cu->per_objfile->objfile));
10042 }
10043
10044 attr = dwarf2_attr (die, DW_AT_import, cu);
10045 if (attr != NULL)
10046 {
10047 sect_offset sect_off = attr->get_ref_die_offset ();
10048 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10049 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10050 dwarf2_per_cu_data *per_cu
10051 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10052
10053 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10054 into another compilation unit, at root level. Regard this as a hint,
10055 and ignore it. */
10056 if (die->parent && die->parent->parent == NULL
10057 && per_cu->unit_type == DW_UT_compile
10058 && per_cu->lang == language_cplus)
10059 return;
10060
10061 /* If necessary, add it to the queue and load its DIEs. */
10062 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10063 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
10064
10065 cu->per_cu->imported_symtabs_push (per_cu);
10066 }
10067 }
10068
10069 /* RAII object that represents a process_die scope: i.e.,
10070 starts/finishes processing a DIE. */
10071 class process_die_scope
10072 {
10073 public:
10074 process_die_scope (die_info *die, dwarf2_cu *cu)
10075 : m_die (die), m_cu (cu)
10076 {
10077 /* We should only be processing DIEs not already in process. */
10078 gdb_assert (!m_die->in_process);
10079 m_die->in_process = true;
10080 }
10081
10082 ~process_die_scope ()
10083 {
10084 m_die->in_process = false;
10085
10086 /* If we're done processing the DIE for the CU that owns the line
10087 header, we don't need the line header anymore. */
10088 if (m_cu->line_header_die_owner == m_die)
10089 {
10090 delete m_cu->line_header;
10091 m_cu->line_header = NULL;
10092 m_cu->line_header_die_owner = NULL;
10093 }
10094 }
10095
10096 private:
10097 die_info *m_die;
10098 dwarf2_cu *m_cu;
10099 };
10100
10101 /* Process a die and its children. */
10102
10103 static void
10104 process_die (struct die_info *die, struct dwarf2_cu *cu)
10105 {
10106 process_die_scope scope (die, cu);
10107
10108 switch (die->tag)
10109 {
10110 case DW_TAG_padding:
10111 break;
10112 case DW_TAG_compile_unit:
10113 case DW_TAG_partial_unit:
10114 read_file_scope (die, cu);
10115 break;
10116 case DW_TAG_type_unit:
10117 read_type_unit_scope (die, cu);
10118 break;
10119 case DW_TAG_subprogram:
10120 /* Nested subprograms in Fortran get a prefix. */
10121 if (cu->language == language_fortran
10122 && die->parent != NULL
10123 && die->parent->tag == DW_TAG_subprogram)
10124 cu->processing_has_namespace_info = true;
10125 /* Fall through. */
10126 case DW_TAG_inlined_subroutine:
10127 read_func_scope (die, cu);
10128 break;
10129 case DW_TAG_lexical_block:
10130 case DW_TAG_try_block:
10131 case DW_TAG_catch_block:
10132 read_lexical_block_scope (die, cu);
10133 break;
10134 case DW_TAG_call_site:
10135 case DW_TAG_GNU_call_site:
10136 read_call_site_scope (die, cu);
10137 break;
10138 case DW_TAG_class_type:
10139 case DW_TAG_interface_type:
10140 case DW_TAG_structure_type:
10141 case DW_TAG_union_type:
10142 process_structure_scope (die, cu);
10143 break;
10144 case DW_TAG_enumeration_type:
10145 process_enumeration_scope (die, cu);
10146 break;
10147
10148 /* These dies have a type, but processing them does not create
10149 a symbol or recurse to process the children. Therefore we can
10150 read them on-demand through read_type_die. */
10151 case DW_TAG_subroutine_type:
10152 case DW_TAG_set_type:
10153 case DW_TAG_array_type:
10154 case DW_TAG_pointer_type:
10155 case DW_TAG_ptr_to_member_type:
10156 case DW_TAG_reference_type:
10157 case DW_TAG_rvalue_reference_type:
10158 case DW_TAG_string_type:
10159 break;
10160
10161 case DW_TAG_base_type:
10162 case DW_TAG_subrange_type:
10163 case DW_TAG_typedef:
10164 /* Add a typedef symbol for the type definition, if it has a
10165 DW_AT_name. */
10166 new_symbol (die, read_type_die (die, cu), cu);
10167 break;
10168 case DW_TAG_common_block:
10169 read_common_block (die, cu);
10170 break;
10171 case DW_TAG_common_inclusion:
10172 break;
10173 case DW_TAG_namespace:
10174 cu->processing_has_namespace_info = true;
10175 read_namespace (die, cu);
10176 break;
10177 case DW_TAG_module:
10178 cu->processing_has_namespace_info = true;
10179 read_module (die, cu);
10180 break;
10181 case DW_TAG_imported_declaration:
10182 cu->processing_has_namespace_info = true;
10183 if (read_namespace_alias (die, cu))
10184 break;
10185 /* The declaration is not a global namespace alias. */
10186 /* Fall through. */
10187 case DW_TAG_imported_module:
10188 cu->processing_has_namespace_info = true;
10189 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10190 || cu->language != language_fortran))
10191 complaint (_("Tag '%s' has unexpected children"),
10192 dwarf_tag_name (die->tag));
10193 read_import_statement (die, cu);
10194 break;
10195
10196 case DW_TAG_imported_unit:
10197 process_imported_unit_die (die, cu);
10198 break;
10199
10200 case DW_TAG_variable:
10201 read_variable (die, cu);
10202 break;
10203
10204 default:
10205 new_symbol (die, NULL, cu);
10206 break;
10207 }
10208 }
10209 \f
10210 /* DWARF name computation. */
10211
10212 /* A helper function for dwarf2_compute_name which determines whether DIE
10213 needs to have the name of the scope prepended to the name listed in the
10214 die. */
10215
10216 static int
10217 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10218 {
10219 struct attribute *attr;
10220
10221 switch (die->tag)
10222 {
10223 case DW_TAG_namespace:
10224 case DW_TAG_typedef:
10225 case DW_TAG_class_type:
10226 case DW_TAG_interface_type:
10227 case DW_TAG_structure_type:
10228 case DW_TAG_union_type:
10229 case DW_TAG_enumeration_type:
10230 case DW_TAG_enumerator:
10231 case DW_TAG_subprogram:
10232 case DW_TAG_inlined_subroutine:
10233 case DW_TAG_member:
10234 case DW_TAG_imported_declaration:
10235 return 1;
10236
10237 case DW_TAG_variable:
10238 case DW_TAG_constant:
10239 /* We only need to prefix "globally" visible variables. These include
10240 any variable marked with DW_AT_external or any variable that
10241 lives in a namespace. [Variables in anonymous namespaces
10242 require prefixing, but they are not DW_AT_external.] */
10243
10244 if (dwarf2_attr (die, DW_AT_specification, cu))
10245 {
10246 struct dwarf2_cu *spec_cu = cu;
10247
10248 return die_needs_namespace (die_specification (die, &spec_cu),
10249 spec_cu);
10250 }
10251
10252 attr = dwarf2_attr (die, DW_AT_external, cu);
10253 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10254 && die->parent->tag != DW_TAG_module)
10255 return 0;
10256 /* A variable in a lexical block of some kind does not need a
10257 namespace, even though in C++ such variables may be external
10258 and have a mangled name. */
10259 if (die->parent->tag == DW_TAG_lexical_block
10260 || die->parent->tag == DW_TAG_try_block
10261 || die->parent->tag == DW_TAG_catch_block
10262 || die->parent->tag == DW_TAG_subprogram)
10263 return 0;
10264 return 1;
10265
10266 default:
10267 return 0;
10268 }
10269 }
10270
10271 /* Return the DIE's linkage name attribute, 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 struct attribute *
10276 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10277 {
10278 struct attribute *attr;
10279
10280 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10281 if (attr == NULL)
10282 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10283
10284 return attr;
10285 }
10286
10287 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10288 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10289 defined for the given DIE. */
10290
10291 static const char *
10292 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10293 {
10294 const char *linkage_name;
10295
10296 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10297 if (linkage_name == NULL)
10298 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10299
10300 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10301 See https://github.com/rust-lang/rust/issues/32925. */
10302 if (cu->language == language_rust && linkage_name != NULL
10303 && strchr (linkage_name, '{') != NULL)
10304 linkage_name = NULL;
10305
10306 return linkage_name;
10307 }
10308
10309 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10310 compute the physname for the object, which include a method's:
10311 - formal parameters (C++),
10312 - receiver type (Go),
10313
10314 The term "physname" is a bit confusing.
10315 For C++, for example, it is the demangled name.
10316 For Go, for example, it's the mangled name.
10317
10318 For Ada, return the DIE's linkage name rather than the fully qualified
10319 name. PHYSNAME is ignored..
10320
10321 The result is allocated on the objfile->per_bfd's obstack and
10322 canonicalized. */
10323
10324 static const char *
10325 dwarf2_compute_name (const char *name,
10326 struct die_info *die, struct dwarf2_cu *cu,
10327 int physname)
10328 {
10329 struct objfile *objfile = cu->per_objfile->objfile;
10330
10331 if (name == NULL)
10332 name = dwarf2_name (die, cu);
10333
10334 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10335 but otherwise compute it by typename_concat inside GDB.
10336 FIXME: Actually this is not really true, or at least not always true.
10337 It's all very confusing. compute_and_set_names doesn't try to demangle
10338 Fortran names because there is no mangling standard. So new_symbol
10339 will set the demangled name to the result of dwarf2_full_name, and it is
10340 the demangled name that GDB uses if it exists. */
10341 if (cu->language == language_ada
10342 || (cu->language == language_fortran && physname))
10343 {
10344 /* For Ada unit, we prefer the linkage name over the name, as
10345 the former contains the exported name, which the user expects
10346 to be able to reference. Ideally, we want the user to be able
10347 to reference this entity using either natural or linkage name,
10348 but we haven't started looking at this enhancement yet. */
10349 const char *linkage_name = dw2_linkage_name (die, cu);
10350
10351 if (linkage_name != NULL)
10352 return linkage_name;
10353 }
10354
10355 /* These are the only languages we know how to qualify names in. */
10356 if (name != NULL
10357 && (cu->language == language_cplus
10358 || cu->language == language_fortran || cu->language == language_d
10359 || cu->language == language_rust))
10360 {
10361 if (die_needs_namespace (die, cu))
10362 {
10363 const char *prefix;
10364 const char *canonical_name = NULL;
10365
10366 string_file buf;
10367
10368 prefix = determine_prefix (die, cu);
10369 if (*prefix != '\0')
10370 {
10371 gdb::unique_xmalloc_ptr<char> prefixed_name
10372 (typename_concat (NULL, prefix, name, physname, cu));
10373
10374 buf.puts (prefixed_name.get ());
10375 }
10376 else
10377 buf.puts (name);
10378
10379 /* Template parameters may be specified in the DIE's DW_AT_name, or
10380 as children with DW_TAG_template_type_param or
10381 DW_TAG_value_type_param. If the latter, add them to the name
10382 here. If the name already has template parameters, then
10383 skip this step; some versions of GCC emit both, and
10384 it is more efficient to use the pre-computed name.
10385
10386 Something to keep in mind about this process: it is very
10387 unlikely, or in some cases downright impossible, to produce
10388 something that will match the mangled name of a function.
10389 If the definition of the function has the same debug info,
10390 we should be able to match up with it anyway. But fallbacks
10391 using the minimal symbol, for instance to find a method
10392 implemented in a stripped copy of libstdc++, will not work.
10393 If we do not have debug info for the definition, we will have to
10394 match them up some other way.
10395
10396 When we do name matching there is a related problem with function
10397 templates; two instantiated function templates are allowed to
10398 differ only by their return types, which we do not add here. */
10399
10400 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10401 {
10402 struct attribute *attr;
10403 struct die_info *child;
10404 int first = 1;
10405
10406 die->building_fullname = 1;
10407
10408 for (child = die->child; child != NULL; child = child->sibling)
10409 {
10410 struct type *type;
10411 LONGEST value;
10412 const gdb_byte *bytes;
10413 struct dwarf2_locexpr_baton *baton;
10414 struct value *v;
10415
10416 if (child->tag != DW_TAG_template_type_param
10417 && child->tag != DW_TAG_template_value_param)
10418 continue;
10419
10420 if (first)
10421 {
10422 buf.puts ("<");
10423 first = 0;
10424 }
10425 else
10426 buf.puts (", ");
10427
10428 attr = dwarf2_attr (child, DW_AT_type, cu);
10429 if (attr == NULL)
10430 {
10431 complaint (_("template parameter missing DW_AT_type"));
10432 buf.puts ("UNKNOWN_TYPE");
10433 continue;
10434 }
10435 type = die_type (child, cu);
10436
10437 if (child->tag == DW_TAG_template_type_param)
10438 {
10439 c_print_type (type, "", &buf, -1, 0, cu->language,
10440 &type_print_raw_options);
10441 continue;
10442 }
10443
10444 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10445 if (attr == NULL)
10446 {
10447 complaint (_("template parameter missing "
10448 "DW_AT_const_value"));
10449 buf.puts ("UNKNOWN_VALUE");
10450 continue;
10451 }
10452
10453 dwarf2_const_value_attr (attr, type, name,
10454 &cu->comp_unit_obstack, cu,
10455 &value, &bytes, &baton);
10456
10457 if (TYPE_NOSIGN (type))
10458 /* GDB prints characters as NUMBER 'CHAR'. If that's
10459 changed, this can use value_print instead. */
10460 c_printchar (value, type, &buf);
10461 else
10462 {
10463 struct value_print_options opts;
10464
10465 if (baton != NULL)
10466 v = dwarf2_evaluate_loc_desc (type, NULL,
10467 baton->data,
10468 baton->size,
10469 baton->per_cu,
10470 baton->per_objfile);
10471 else if (bytes != NULL)
10472 {
10473 v = allocate_value (type);
10474 memcpy (value_contents_writeable (v), bytes,
10475 TYPE_LENGTH (type));
10476 }
10477 else
10478 v = value_from_longest (type, value);
10479
10480 /* Specify decimal so that we do not depend on
10481 the radix. */
10482 get_formatted_print_options (&opts, 'd');
10483 opts.raw = 1;
10484 value_print (v, &buf, &opts);
10485 release_value (v);
10486 }
10487 }
10488
10489 die->building_fullname = 0;
10490
10491 if (!first)
10492 {
10493 /* Close the argument list, with a space if necessary
10494 (nested templates). */
10495 if (!buf.empty () && buf.string ().back () == '>')
10496 buf.puts (" >");
10497 else
10498 buf.puts (">");
10499 }
10500 }
10501
10502 /* For C++ methods, append formal parameter type
10503 information, if PHYSNAME. */
10504
10505 if (physname && die->tag == DW_TAG_subprogram
10506 && cu->language == language_cplus)
10507 {
10508 struct type *type = read_type_die (die, cu);
10509
10510 c_type_print_args (type, &buf, 1, cu->language,
10511 &type_print_raw_options);
10512
10513 if (cu->language == language_cplus)
10514 {
10515 /* Assume that an artificial first parameter is
10516 "this", but do not crash if it is not. RealView
10517 marks unnamed (and thus unused) parameters as
10518 artificial; there is no way to differentiate
10519 the two cases. */
10520 if (type->num_fields () > 0
10521 && TYPE_FIELD_ARTIFICIAL (type, 0)
10522 && type->field (0).type ()->code () == TYPE_CODE_PTR
10523 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10524 buf.puts (" const");
10525 }
10526 }
10527
10528 const std::string &intermediate_name = buf.string ();
10529
10530 if (cu->language == language_cplus)
10531 canonical_name
10532 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10533 objfile);
10534
10535 /* If we only computed INTERMEDIATE_NAME, or if
10536 INTERMEDIATE_NAME is already canonical, then we need to
10537 intern it. */
10538 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10539 name = objfile->intern (intermediate_name);
10540 else
10541 name = canonical_name;
10542 }
10543 }
10544
10545 return name;
10546 }
10547
10548 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10549 If scope qualifiers are appropriate they will be added. The result
10550 will be allocated on the storage_obstack, or NULL if the DIE does
10551 not have a name. NAME may either be from a previous call to
10552 dwarf2_name or NULL.
10553
10554 The output string will be canonicalized (if C++). */
10555
10556 static const char *
10557 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10558 {
10559 return dwarf2_compute_name (name, die, cu, 0);
10560 }
10561
10562 /* Construct a physname for the given DIE in CU. NAME may either be
10563 from a previous call to dwarf2_name or NULL. The result will be
10564 allocated on the objfile_objstack or NULL if the DIE does not have a
10565 name.
10566
10567 The output string will be canonicalized (if C++). */
10568
10569 static const char *
10570 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10571 {
10572 struct objfile *objfile = cu->per_objfile->objfile;
10573 const char *retval, *mangled = NULL, *canon = NULL;
10574 int need_copy = 1;
10575
10576 /* In this case dwarf2_compute_name is just a shortcut not building anything
10577 on its own. */
10578 if (!die_needs_namespace (die, cu))
10579 return dwarf2_compute_name (name, die, cu, 1);
10580
10581 if (cu->language != language_rust)
10582 mangled = dw2_linkage_name (die, cu);
10583
10584 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10585 has computed. */
10586 gdb::unique_xmalloc_ptr<char> demangled;
10587 if (mangled != NULL)
10588 {
10589
10590 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10591 {
10592 /* Do nothing (do not demangle the symbol name). */
10593 }
10594 else if (cu->language == language_go)
10595 {
10596 /* This is a lie, but we already lie to the caller new_symbol.
10597 new_symbol assumes we return the mangled name.
10598 This just undoes that lie until things are cleaned up. */
10599 }
10600 else
10601 {
10602 /* Use DMGL_RET_DROP for C++ template functions to suppress
10603 their return type. It is easier for GDB users to search
10604 for such functions as `name(params)' than `long name(params)'.
10605 In such case the minimal symbol names do not match the full
10606 symbol names but for template functions there is never a need
10607 to look up their definition from their declaration so
10608 the only disadvantage remains the minimal symbol variant
10609 `long name(params)' does not have the proper inferior type. */
10610 demangled.reset (gdb_demangle (mangled,
10611 (DMGL_PARAMS | DMGL_ANSI
10612 | DMGL_RET_DROP)));
10613 }
10614 if (demangled)
10615 canon = demangled.get ();
10616 else
10617 {
10618 canon = mangled;
10619 need_copy = 0;
10620 }
10621 }
10622
10623 if (canon == NULL || check_physname)
10624 {
10625 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10626
10627 if (canon != NULL && strcmp (physname, canon) != 0)
10628 {
10629 /* It may not mean a bug in GDB. The compiler could also
10630 compute DW_AT_linkage_name incorrectly. But in such case
10631 GDB would need to be bug-to-bug compatible. */
10632
10633 complaint (_("Computed physname <%s> does not match demangled <%s> "
10634 "(from linkage <%s>) - DIE at %s [in module %s]"),
10635 physname, canon, mangled, sect_offset_str (die->sect_off),
10636 objfile_name (objfile));
10637
10638 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10639 is available here - over computed PHYSNAME. It is safer
10640 against both buggy GDB and buggy compilers. */
10641
10642 retval = canon;
10643 }
10644 else
10645 {
10646 retval = physname;
10647 need_copy = 0;
10648 }
10649 }
10650 else
10651 retval = canon;
10652
10653 if (need_copy)
10654 retval = objfile->intern (retval);
10655
10656 return retval;
10657 }
10658
10659 /* Inspect DIE in CU for a namespace alias. If one exists, record
10660 a new symbol for it.
10661
10662 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10663
10664 static int
10665 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10666 {
10667 struct attribute *attr;
10668
10669 /* If the die does not have a name, this is not a namespace
10670 alias. */
10671 attr = dwarf2_attr (die, DW_AT_name, cu);
10672 if (attr != NULL)
10673 {
10674 int num;
10675 struct die_info *d = die;
10676 struct dwarf2_cu *imported_cu = cu;
10677
10678 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10679 keep inspecting DIEs until we hit the underlying import. */
10680 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10681 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10682 {
10683 attr = dwarf2_attr (d, DW_AT_import, cu);
10684 if (attr == NULL)
10685 break;
10686
10687 d = follow_die_ref (d, attr, &imported_cu);
10688 if (d->tag != DW_TAG_imported_declaration)
10689 break;
10690 }
10691
10692 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10693 {
10694 complaint (_("DIE at %s has too many recursively imported "
10695 "declarations"), sect_offset_str (d->sect_off));
10696 return 0;
10697 }
10698
10699 if (attr != NULL)
10700 {
10701 struct type *type;
10702 sect_offset sect_off = attr->get_ref_die_offset ();
10703
10704 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10705 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10706 {
10707 /* This declaration is a global namespace alias. Add
10708 a symbol for it whose type is the aliased namespace. */
10709 new_symbol (die, type, cu);
10710 return 1;
10711 }
10712 }
10713 }
10714
10715 return 0;
10716 }
10717
10718 /* Return the using directives repository (global or local?) to use in the
10719 current context for CU.
10720
10721 For Ada, imported declarations can materialize renamings, which *may* be
10722 global. However it is impossible (for now?) in DWARF to distinguish
10723 "external" imported declarations and "static" ones. As all imported
10724 declarations seem to be static in all other languages, make them all CU-wide
10725 global only in Ada. */
10726
10727 static struct using_direct **
10728 using_directives (struct dwarf2_cu *cu)
10729 {
10730 if (cu->language == language_ada
10731 && cu->get_builder ()->outermost_context_p ())
10732 return cu->get_builder ()->get_global_using_directives ();
10733 else
10734 return cu->get_builder ()->get_local_using_directives ();
10735 }
10736
10737 /* Read the import statement specified by the given die and record it. */
10738
10739 static void
10740 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10741 {
10742 struct objfile *objfile = cu->per_objfile->objfile;
10743 struct attribute *import_attr;
10744 struct die_info *imported_die, *child_die;
10745 struct dwarf2_cu *imported_cu;
10746 const char *imported_name;
10747 const char *imported_name_prefix;
10748 const char *canonical_name;
10749 const char *import_alias;
10750 const char *imported_declaration = NULL;
10751 const char *import_prefix;
10752 std::vector<const char *> excludes;
10753
10754 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10755 if (import_attr == NULL)
10756 {
10757 complaint (_("Tag '%s' has no DW_AT_import"),
10758 dwarf_tag_name (die->tag));
10759 return;
10760 }
10761
10762 imported_cu = cu;
10763 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10764 imported_name = dwarf2_name (imported_die, imported_cu);
10765 if (imported_name == NULL)
10766 {
10767 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10768
10769 The import in the following code:
10770 namespace A
10771 {
10772 typedef int B;
10773 }
10774
10775 int main ()
10776 {
10777 using A::B;
10778 B b;
10779 return b;
10780 }
10781
10782 ...
10783 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10784 <52> DW_AT_decl_file : 1
10785 <53> DW_AT_decl_line : 6
10786 <54> DW_AT_import : <0x75>
10787 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10788 <59> DW_AT_name : B
10789 <5b> DW_AT_decl_file : 1
10790 <5c> DW_AT_decl_line : 2
10791 <5d> DW_AT_type : <0x6e>
10792 ...
10793 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10794 <76> DW_AT_byte_size : 4
10795 <77> DW_AT_encoding : 5 (signed)
10796
10797 imports the wrong die ( 0x75 instead of 0x58 ).
10798 This case will be ignored until the gcc bug is fixed. */
10799 return;
10800 }
10801
10802 /* Figure out the local name after import. */
10803 import_alias = dwarf2_name (die, cu);
10804
10805 /* Figure out where the statement is being imported to. */
10806 import_prefix = determine_prefix (die, cu);
10807
10808 /* Figure out what the scope of the imported die is and prepend it
10809 to the name of the imported die. */
10810 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10811
10812 if (imported_die->tag != DW_TAG_namespace
10813 && imported_die->tag != DW_TAG_module)
10814 {
10815 imported_declaration = imported_name;
10816 canonical_name = imported_name_prefix;
10817 }
10818 else if (strlen (imported_name_prefix) > 0)
10819 canonical_name = obconcat (&objfile->objfile_obstack,
10820 imported_name_prefix,
10821 (cu->language == language_d ? "." : "::"),
10822 imported_name, (char *) NULL);
10823 else
10824 canonical_name = imported_name;
10825
10826 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10827 for (child_die = die->child; child_die && child_die->tag;
10828 child_die = child_die->sibling)
10829 {
10830 /* DWARF-4: A Fortran use statement with a “rename list” may be
10831 represented by an imported module entry with an import attribute
10832 referring to the module and owned entries corresponding to those
10833 entities that are renamed as part of being imported. */
10834
10835 if (child_die->tag != DW_TAG_imported_declaration)
10836 {
10837 complaint (_("child DW_TAG_imported_declaration expected "
10838 "- DIE at %s [in module %s]"),
10839 sect_offset_str (child_die->sect_off),
10840 objfile_name (objfile));
10841 continue;
10842 }
10843
10844 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10845 if (import_attr == NULL)
10846 {
10847 complaint (_("Tag '%s' has no DW_AT_import"),
10848 dwarf_tag_name (child_die->tag));
10849 continue;
10850 }
10851
10852 imported_cu = cu;
10853 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10854 &imported_cu);
10855 imported_name = dwarf2_name (imported_die, imported_cu);
10856 if (imported_name == NULL)
10857 {
10858 complaint (_("child DW_TAG_imported_declaration has unknown "
10859 "imported name - DIE at %s [in module %s]"),
10860 sect_offset_str (child_die->sect_off),
10861 objfile_name (objfile));
10862 continue;
10863 }
10864
10865 excludes.push_back (imported_name);
10866
10867 process_die (child_die, cu);
10868 }
10869
10870 add_using_directive (using_directives (cu),
10871 import_prefix,
10872 canonical_name,
10873 import_alias,
10874 imported_declaration,
10875 excludes,
10876 0,
10877 &objfile->objfile_obstack);
10878 }
10879
10880 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10881 types, but gives them a size of zero. Starting with version 14,
10882 ICC is compatible with GCC. */
10883
10884 static bool
10885 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10886 {
10887 if (!cu->checked_producer)
10888 check_producer (cu);
10889
10890 return cu->producer_is_icc_lt_14;
10891 }
10892
10893 /* ICC generates a DW_AT_type for C void functions. This was observed on
10894 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10895 which says that void functions should not have a DW_AT_type. */
10896
10897 static bool
10898 producer_is_icc (struct dwarf2_cu *cu)
10899 {
10900 if (!cu->checked_producer)
10901 check_producer (cu);
10902
10903 return cu->producer_is_icc;
10904 }
10905
10906 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10907 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10908 this, it was first present in GCC release 4.3.0. */
10909
10910 static bool
10911 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10912 {
10913 if (!cu->checked_producer)
10914 check_producer (cu);
10915
10916 return cu->producer_is_gcc_lt_4_3;
10917 }
10918
10919 static file_and_directory
10920 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10921 {
10922 file_and_directory res;
10923
10924 /* Find the filename. Do not use dwarf2_name here, since the filename
10925 is not a source language identifier. */
10926 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10927 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10928
10929 if (res.comp_dir == NULL
10930 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10931 && IS_ABSOLUTE_PATH (res.name))
10932 {
10933 res.comp_dir_storage = ldirname (res.name);
10934 if (!res.comp_dir_storage.empty ())
10935 res.comp_dir = res.comp_dir_storage.c_str ();
10936 }
10937 if (res.comp_dir != NULL)
10938 {
10939 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10940 directory, get rid of it. */
10941 const char *cp = strchr (res.comp_dir, ':');
10942
10943 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10944 res.comp_dir = cp + 1;
10945 }
10946
10947 if (res.name == NULL)
10948 res.name = "<unknown>";
10949
10950 return res;
10951 }
10952
10953 /* Handle DW_AT_stmt_list for a compilation unit.
10954 DIE is the DW_TAG_compile_unit die for CU.
10955 COMP_DIR is the compilation directory. LOWPC is passed to
10956 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10957
10958 static void
10959 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10960 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10961 {
10962 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10963 struct attribute *attr;
10964 struct line_header line_header_local;
10965 hashval_t line_header_local_hash;
10966 void **slot;
10967 int decode_mapping;
10968
10969 gdb_assert (! cu->per_cu->is_debug_types);
10970
10971 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10972 if (attr == NULL)
10973 return;
10974
10975 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10976
10977 /* The line header hash table is only created if needed (it exists to
10978 prevent redundant reading of the line table for partial_units).
10979 If we're given a partial_unit, we'll need it. If we're given a
10980 compile_unit, then use the line header hash table if it's already
10981 created, but don't create one just yet. */
10982
10983 if (per_objfile->line_header_hash == NULL
10984 && die->tag == DW_TAG_partial_unit)
10985 {
10986 per_objfile->line_header_hash
10987 .reset (htab_create_alloc (127, line_header_hash_voidp,
10988 line_header_eq_voidp,
10989 free_line_header_voidp,
10990 xcalloc, xfree));
10991 }
10992
10993 line_header_local.sect_off = line_offset;
10994 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10995 line_header_local_hash = line_header_hash (&line_header_local);
10996 if (per_objfile->line_header_hash != NULL)
10997 {
10998 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10999 &line_header_local,
11000 line_header_local_hash, NO_INSERT);
11001
11002 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11003 is not present in *SLOT (since if there is something in *SLOT then
11004 it will be for a partial_unit). */
11005 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11006 {
11007 gdb_assert (*slot != NULL);
11008 cu->line_header = (struct line_header *) *slot;
11009 return;
11010 }
11011 }
11012
11013 /* dwarf_decode_line_header does not yet provide sufficient information.
11014 We always have to call also dwarf_decode_lines for it. */
11015 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11016 if (lh == NULL)
11017 return;
11018
11019 cu->line_header = lh.release ();
11020 cu->line_header_die_owner = die;
11021
11022 if (per_objfile->line_header_hash == NULL)
11023 slot = NULL;
11024 else
11025 {
11026 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11027 &line_header_local,
11028 line_header_local_hash, INSERT);
11029 gdb_assert (slot != NULL);
11030 }
11031 if (slot != NULL && *slot == NULL)
11032 {
11033 /* This newly decoded line number information unit will be owned
11034 by line_header_hash hash table. */
11035 *slot = cu->line_header;
11036 cu->line_header_die_owner = NULL;
11037 }
11038 else
11039 {
11040 /* We cannot free any current entry in (*slot) as that struct line_header
11041 may be already used by multiple CUs. Create only temporary decoded
11042 line_header for this CU - it may happen at most once for each line
11043 number information unit. And if we're not using line_header_hash
11044 then this is what we want as well. */
11045 gdb_assert (die->tag != DW_TAG_partial_unit);
11046 }
11047 decode_mapping = (die->tag != DW_TAG_partial_unit);
11048 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11049 decode_mapping);
11050
11051 }
11052
11053 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11054
11055 static void
11056 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11057 {
11058 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11059 struct objfile *objfile = per_objfile->objfile;
11060 struct gdbarch *gdbarch = objfile->arch ();
11061 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11062 CORE_ADDR highpc = ((CORE_ADDR) 0);
11063 struct attribute *attr;
11064 struct die_info *child_die;
11065 CORE_ADDR baseaddr;
11066
11067 prepare_one_comp_unit (cu, die, cu->language);
11068 baseaddr = objfile->text_section_offset ();
11069
11070 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11071
11072 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11073 from finish_block. */
11074 if (lowpc == ((CORE_ADDR) -1))
11075 lowpc = highpc;
11076 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11077
11078 file_and_directory fnd = find_file_and_directory (die, cu);
11079
11080 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11081 standardised yet. As a workaround for the language detection we fall
11082 back to the DW_AT_producer string. */
11083 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11084 cu->language = language_opencl;
11085
11086 /* Similar hack for Go. */
11087 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11088 set_cu_language (DW_LANG_Go, cu);
11089
11090 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11091
11092 /* Decode line number information if present. We do this before
11093 processing child DIEs, so that the line header table is available
11094 for DW_AT_decl_file. */
11095 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11096
11097 /* Process all dies in compilation unit. */
11098 if (die->child != NULL)
11099 {
11100 child_die = die->child;
11101 while (child_die && child_die->tag)
11102 {
11103 process_die (child_die, cu);
11104 child_die = child_die->sibling;
11105 }
11106 }
11107
11108 /* Decode macro information, if present. Dwarf 2 macro information
11109 refers to information in the line number info statement program
11110 header, so we can only read it if we've read the header
11111 successfully. */
11112 attr = dwarf2_attr (die, DW_AT_macros, cu);
11113 if (attr == NULL)
11114 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11115 if (attr && cu->line_header)
11116 {
11117 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11118 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11119
11120 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11121 }
11122 else
11123 {
11124 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11125 if (attr && cu->line_header)
11126 {
11127 unsigned int macro_offset = DW_UNSND (attr);
11128
11129 dwarf_decode_macros (cu, macro_offset, 0);
11130 }
11131 }
11132 }
11133
11134 void
11135 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11136 {
11137 struct type_unit_group *tu_group;
11138 int first_time;
11139 struct attribute *attr;
11140 unsigned int i;
11141 struct signatured_type *sig_type;
11142
11143 gdb_assert (per_cu->is_debug_types);
11144 sig_type = (struct signatured_type *) per_cu;
11145
11146 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11147
11148 /* If we're using .gdb_index (includes -readnow) then
11149 per_cu->type_unit_group may not have been set up yet. */
11150 if (sig_type->type_unit_group == NULL)
11151 sig_type->type_unit_group = get_type_unit_group (this, attr);
11152 tu_group = sig_type->type_unit_group;
11153
11154 /* If we've already processed this stmt_list there's no real need to
11155 do it again, we could fake it and just recreate the part we need
11156 (file name,index -> symtab mapping). If data shows this optimization
11157 is useful we can do it then. */
11158 type_unit_group_unshareable *tug_unshare
11159 = per_objfile->get_type_unit_group_unshareable (tu_group);
11160 first_time = tug_unshare->compunit_symtab == NULL;
11161
11162 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11163 debug info. */
11164 line_header_up lh;
11165 if (attr != NULL)
11166 {
11167 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11168 lh = dwarf_decode_line_header (line_offset, this);
11169 }
11170 if (lh == NULL)
11171 {
11172 if (first_time)
11173 start_symtab ("", NULL, 0);
11174 else
11175 {
11176 gdb_assert (tug_unshare->symtabs == NULL);
11177 gdb_assert (m_builder == nullptr);
11178 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11179 m_builder.reset (new struct buildsym_compunit
11180 (COMPUNIT_OBJFILE (cust), "",
11181 COMPUNIT_DIRNAME (cust),
11182 compunit_language (cust),
11183 0, cust));
11184 list_in_scope = get_builder ()->get_file_symbols ();
11185 }
11186 return;
11187 }
11188
11189 line_header = lh.release ();
11190 line_header_die_owner = die;
11191
11192 if (first_time)
11193 {
11194 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11195
11196 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11197 still initializing it, and our caller (a few levels up)
11198 process_full_type_unit still needs to know if this is the first
11199 time. */
11200
11201 tug_unshare->symtabs
11202 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11203 struct symtab *, line_header->file_names_size ());
11204
11205 auto &file_names = line_header->file_names ();
11206 for (i = 0; i < file_names.size (); ++i)
11207 {
11208 file_entry &fe = file_names[i];
11209 dwarf2_start_subfile (this, fe.name,
11210 fe.include_dir (line_header));
11211 buildsym_compunit *b = get_builder ();
11212 if (b->get_current_subfile ()->symtab == NULL)
11213 {
11214 /* NOTE: start_subfile will recognize when it's been
11215 passed a file it has already seen. So we can't
11216 assume there's a simple mapping from
11217 cu->line_header->file_names to subfiles, plus
11218 cu->line_header->file_names may contain dups. */
11219 b->get_current_subfile ()->symtab
11220 = allocate_symtab (cust, b->get_current_subfile ()->name);
11221 }
11222
11223 fe.symtab = b->get_current_subfile ()->symtab;
11224 tug_unshare->symtabs[i] = fe.symtab;
11225 }
11226 }
11227 else
11228 {
11229 gdb_assert (m_builder == nullptr);
11230 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11231 m_builder.reset (new struct buildsym_compunit
11232 (COMPUNIT_OBJFILE (cust), "",
11233 COMPUNIT_DIRNAME (cust),
11234 compunit_language (cust),
11235 0, cust));
11236 list_in_scope = get_builder ()->get_file_symbols ();
11237
11238 auto &file_names = line_header->file_names ();
11239 for (i = 0; i < file_names.size (); ++i)
11240 {
11241 file_entry &fe = file_names[i];
11242 fe.symtab = tug_unshare->symtabs[i];
11243 }
11244 }
11245
11246 /* The main symtab is allocated last. Type units don't have DW_AT_name
11247 so they don't have a "real" (so to speak) symtab anyway.
11248 There is later code that will assign the main symtab to all symbols
11249 that don't have one. We need to handle the case of a symbol with a
11250 missing symtab (DW_AT_decl_file) anyway. */
11251 }
11252
11253 /* Process DW_TAG_type_unit.
11254 For TUs we want to skip the first top level sibling if it's not the
11255 actual type being defined by this TU. In this case the first top
11256 level sibling is there to provide context only. */
11257
11258 static void
11259 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11260 {
11261 struct die_info *child_die;
11262
11263 prepare_one_comp_unit (cu, die, language_minimal);
11264
11265 /* Initialize (or reinitialize) the machinery for building symtabs.
11266 We do this before processing child DIEs, so that the line header table
11267 is available for DW_AT_decl_file. */
11268 cu->setup_type_unit_groups (die);
11269
11270 if (die->child != NULL)
11271 {
11272 child_die = die->child;
11273 while (child_die && child_die->tag)
11274 {
11275 process_die (child_die, cu);
11276 child_die = child_die->sibling;
11277 }
11278 }
11279 }
11280 \f
11281 /* DWO/DWP files.
11282
11283 http://gcc.gnu.org/wiki/DebugFission
11284 http://gcc.gnu.org/wiki/DebugFissionDWP
11285
11286 To simplify handling of both DWO files ("object" files with the DWARF info)
11287 and DWP files (a file with the DWOs packaged up into one file), we treat
11288 DWP files as having a collection of virtual DWO files. */
11289
11290 static hashval_t
11291 hash_dwo_file (const void *item)
11292 {
11293 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11294 hashval_t hash;
11295
11296 hash = htab_hash_string (dwo_file->dwo_name);
11297 if (dwo_file->comp_dir != NULL)
11298 hash += htab_hash_string (dwo_file->comp_dir);
11299 return hash;
11300 }
11301
11302 static int
11303 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11304 {
11305 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11306 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11307
11308 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11309 return 0;
11310 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11311 return lhs->comp_dir == rhs->comp_dir;
11312 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11313 }
11314
11315 /* Allocate a hash table for DWO files. */
11316
11317 static htab_up
11318 allocate_dwo_file_hash_table ()
11319 {
11320 auto delete_dwo_file = [] (void *item)
11321 {
11322 struct dwo_file *dwo_file = (struct dwo_file *) item;
11323
11324 delete dwo_file;
11325 };
11326
11327 return htab_up (htab_create_alloc (41,
11328 hash_dwo_file,
11329 eq_dwo_file,
11330 delete_dwo_file,
11331 xcalloc, xfree));
11332 }
11333
11334 /* Lookup DWO file DWO_NAME. */
11335
11336 static void **
11337 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11338 const char *dwo_name,
11339 const char *comp_dir)
11340 {
11341 struct dwo_file find_entry;
11342 void **slot;
11343
11344 if (per_objfile->per_bfd->dwo_files == NULL)
11345 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11346
11347 find_entry.dwo_name = dwo_name;
11348 find_entry.comp_dir = comp_dir;
11349 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11350 INSERT);
11351
11352 return slot;
11353 }
11354
11355 static hashval_t
11356 hash_dwo_unit (const void *item)
11357 {
11358 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11359
11360 /* This drops the top 32 bits of the id, but is ok for a hash. */
11361 return dwo_unit->signature;
11362 }
11363
11364 static int
11365 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11366 {
11367 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11368 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11369
11370 /* The signature is assumed to be unique within the DWO file.
11371 So while object file CU dwo_id's always have the value zero,
11372 that's OK, assuming each object file DWO file has only one CU,
11373 and that's the rule for now. */
11374 return lhs->signature == rhs->signature;
11375 }
11376
11377 /* Allocate a hash table for DWO CUs,TUs.
11378 There is one of these tables for each of CUs,TUs for each DWO file. */
11379
11380 static htab_up
11381 allocate_dwo_unit_table ()
11382 {
11383 /* Start out with a pretty small number.
11384 Generally DWO files contain only one CU and maybe some TUs. */
11385 return htab_up (htab_create_alloc (3,
11386 hash_dwo_unit,
11387 eq_dwo_unit,
11388 NULL, xcalloc, xfree));
11389 }
11390
11391 /* die_reader_func for create_dwo_cu. */
11392
11393 static void
11394 create_dwo_cu_reader (const struct die_reader_specs *reader,
11395 const gdb_byte *info_ptr,
11396 struct die_info *comp_unit_die,
11397 struct dwo_file *dwo_file,
11398 struct dwo_unit *dwo_unit)
11399 {
11400 struct dwarf2_cu *cu = reader->cu;
11401 sect_offset sect_off = cu->per_cu->sect_off;
11402 struct dwarf2_section_info *section = cu->per_cu->section;
11403
11404 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11405 if (!signature.has_value ())
11406 {
11407 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11408 " its dwo_id [in module %s]"),
11409 sect_offset_str (sect_off), dwo_file->dwo_name);
11410 return;
11411 }
11412
11413 dwo_unit->dwo_file = dwo_file;
11414 dwo_unit->signature = *signature;
11415 dwo_unit->section = section;
11416 dwo_unit->sect_off = sect_off;
11417 dwo_unit->length = cu->per_cu->length;
11418
11419 if (dwarf_read_debug)
11420 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11421 sect_offset_str (sect_off),
11422 hex_string (dwo_unit->signature));
11423 }
11424
11425 /* Create the dwo_units for the CUs in a DWO_FILE.
11426 Note: This function processes DWO files only, not DWP files. */
11427
11428 static void
11429 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11430 dwarf2_cu *cu, struct dwo_file &dwo_file,
11431 dwarf2_section_info &section, htab_up &cus_htab)
11432 {
11433 struct objfile *objfile = per_objfile->objfile;
11434 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11435 const gdb_byte *info_ptr, *end_ptr;
11436
11437 section.read (objfile);
11438 info_ptr = section.buffer;
11439
11440 if (info_ptr == NULL)
11441 return;
11442
11443 if (dwarf_read_debug)
11444 {
11445 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11446 section.get_name (),
11447 section.get_file_name ());
11448 }
11449
11450 end_ptr = info_ptr + section.size;
11451 while (info_ptr < end_ptr)
11452 {
11453 struct dwarf2_per_cu_data per_cu;
11454 struct dwo_unit read_unit {};
11455 struct dwo_unit *dwo_unit;
11456 void **slot;
11457 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11458
11459 memset (&per_cu, 0, sizeof (per_cu));
11460 per_cu.per_bfd = per_bfd;
11461 per_cu.is_debug_types = 0;
11462 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11463 per_cu.section = &section;
11464
11465 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11466 if (!reader.dummy_p)
11467 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11468 &dwo_file, &read_unit);
11469 info_ptr += per_cu.length;
11470
11471 // If the unit could not be parsed, skip it.
11472 if (read_unit.dwo_file == NULL)
11473 continue;
11474
11475 if (cus_htab == NULL)
11476 cus_htab = allocate_dwo_unit_table ();
11477
11478 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11479 struct dwo_unit);
11480 *dwo_unit = read_unit;
11481 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11482 gdb_assert (slot != NULL);
11483 if (*slot != NULL)
11484 {
11485 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11486 sect_offset dup_sect_off = dup_cu->sect_off;
11487
11488 complaint (_("debug cu entry at offset %s is duplicate to"
11489 " the entry at offset %s, signature %s"),
11490 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11491 hex_string (dwo_unit->signature));
11492 }
11493 *slot = (void *)dwo_unit;
11494 }
11495 }
11496
11497 /* DWP file .debug_{cu,tu}_index section format:
11498 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11499
11500 DWP Version 1:
11501
11502 Both index sections have the same format, and serve to map a 64-bit
11503 signature to a set of section numbers. Each section begins with a header,
11504 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11505 indexes, and a pool of 32-bit section numbers. The index sections will be
11506 aligned at 8-byte boundaries in the file.
11507
11508 The index section header consists of:
11509
11510 V, 32 bit version number
11511 -, 32 bits unused
11512 N, 32 bit number of compilation units or type units in the index
11513 M, 32 bit number of slots in the hash table
11514
11515 Numbers are recorded using the byte order of the application binary.
11516
11517 The hash table begins at offset 16 in the section, and consists of an array
11518 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11519 order of the application binary). Unused slots in the hash table are 0.
11520 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11521
11522 The parallel table begins immediately after the hash table
11523 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11524 array of 32-bit indexes (using the byte order of the application binary),
11525 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11526 table contains a 32-bit index into the pool of section numbers. For unused
11527 hash table slots, the corresponding entry in the parallel table will be 0.
11528
11529 The pool of section numbers begins immediately following the hash table
11530 (at offset 16 + 12 * M from the beginning of the section). The pool of
11531 section numbers consists of an array of 32-bit words (using the byte order
11532 of the application binary). Each item in the array is indexed starting
11533 from 0. The hash table entry provides the index of the first section
11534 number in the set. Additional section numbers in the set follow, and the
11535 set is terminated by a 0 entry (section number 0 is not used in ELF).
11536
11537 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11538 section must be the first entry in the set, and the .debug_abbrev.dwo must
11539 be the second entry. Other members of the set may follow in any order.
11540
11541 ---
11542
11543 DWP Version 2:
11544
11545 DWP Version 2 combines all the .debug_info, etc. sections into one,
11546 and the entries in the index tables are now offsets into these sections.
11547 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11548 section.
11549
11550 Index Section Contents:
11551 Header
11552 Hash Table of Signatures dwp_hash_table.hash_table
11553 Parallel Table of Indices dwp_hash_table.unit_table
11554 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11555 Table of Section Sizes dwp_hash_table.v2.sizes
11556
11557 The index section header consists of:
11558
11559 V, 32 bit version number
11560 L, 32 bit number of columns in the table of section offsets
11561 N, 32 bit number of compilation units or type units in the index
11562 M, 32 bit number of slots in the hash table
11563
11564 Numbers are recorded using the byte order of the application binary.
11565
11566 The hash table has the same format as version 1.
11567 The parallel table of indices has the same format as version 1,
11568 except that the entries are origin-1 indices into the table of sections
11569 offsets and the table of section sizes.
11570
11571 The table of offsets begins immediately following the parallel table
11572 (at offset 16 + 12 * M from the beginning of the section). The table is
11573 a two-dimensional array of 32-bit words (using the byte order of the
11574 application binary), with L columns and N+1 rows, in row-major order.
11575 Each row in the array is indexed starting from 0. The first row provides
11576 a key to the remaining rows: each column in this row provides an identifier
11577 for a debug section, and the offsets in the same column of subsequent rows
11578 refer to that section. The section identifiers are:
11579
11580 DW_SECT_INFO 1 .debug_info.dwo
11581 DW_SECT_TYPES 2 .debug_types.dwo
11582 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11583 DW_SECT_LINE 4 .debug_line.dwo
11584 DW_SECT_LOC 5 .debug_loc.dwo
11585 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11586 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11587 DW_SECT_MACRO 8 .debug_macro.dwo
11588
11589 The offsets provided by the CU and TU index sections are the base offsets
11590 for the contributions made by each CU or TU to the corresponding section
11591 in the package file. Each CU and TU header contains an abbrev_offset
11592 field, used to find the abbreviations table for that CU or TU within the
11593 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11594 be interpreted as relative to the base offset given in the index section.
11595 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11596 should be interpreted as relative to the base offset for .debug_line.dwo,
11597 and offsets into other debug sections obtained from DWARF attributes should
11598 also be interpreted as relative to the corresponding base offset.
11599
11600 The table of sizes begins immediately following the table of offsets.
11601 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11602 with L columns and N rows, in row-major order. Each row in the array is
11603 indexed starting from 1 (row 0 is shared by the two tables).
11604
11605 ---
11606
11607 Hash table lookup is handled the same in version 1 and 2:
11608
11609 We assume that N and M will not exceed 2^32 - 1.
11610 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11611
11612 Given a 64-bit compilation unit signature or a type signature S, an entry
11613 in the hash table is located as follows:
11614
11615 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11616 the low-order k bits all set to 1.
11617
11618 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11619
11620 3) If the hash table entry at index H matches the signature, use that
11621 entry. If the hash table entry at index H is unused (all zeroes),
11622 terminate the search: the signature is not present in the table.
11623
11624 4) Let H = (H + H') modulo M. Repeat at Step 3.
11625
11626 Because M > N and H' and M are relatively prime, the search is guaranteed
11627 to stop at an unused slot or find the match. */
11628
11629 /* Create a hash table to map DWO IDs to their CU/TU entry in
11630 .debug_{info,types}.dwo in DWP_FILE.
11631 Returns NULL if there isn't one.
11632 Note: This function processes DWP files only, not DWO files. */
11633
11634 static struct dwp_hash_table *
11635 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11636 struct dwp_file *dwp_file, int is_debug_types)
11637 {
11638 struct objfile *objfile = per_objfile->objfile;
11639 bfd *dbfd = dwp_file->dbfd.get ();
11640 const gdb_byte *index_ptr, *index_end;
11641 struct dwarf2_section_info *index;
11642 uint32_t version, nr_columns, nr_units, nr_slots;
11643 struct dwp_hash_table *htab;
11644
11645 if (is_debug_types)
11646 index = &dwp_file->sections.tu_index;
11647 else
11648 index = &dwp_file->sections.cu_index;
11649
11650 if (index->empty ())
11651 return NULL;
11652 index->read (objfile);
11653
11654 index_ptr = index->buffer;
11655 index_end = index_ptr + index->size;
11656
11657 version = read_4_bytes (dbfd, index_ptr);
11658 index_ptr += 4;
11659 if (version == 2)
11660 nr_columns = read_4_bytes (dbfd, index_ptr);
11661 else
11662 nr_columns = 0;
11663 index_ptr += 4;
11664 nr_units = read_4_bytes (dbfd, index_ptr);
11665 index_ptr += 4;
11666 nr_slots = read_4_bytes (dbfd, index_ptr);
11667 index_ptr += 4;
11668
11669 if (version != 1 && version != 2)
11670 {
11671 error (_("Dwarf Error: unsupported DWP file version (%s)"
11672 " [in module %s]"),
11673 pulongest (version), dwp_file->name);
11674 }
11675 if (nr_slots != (nr_slots & -nr_slots))
11676 {
11677 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11678 " is not power of 2 [in module %s]"),
11679 pulongest (nr_slots), dwp_file->name);
11680 }
11681
11682 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11683 htab->version = version;
11684 htab->nr_columns = nr_columns;
11685 htab->nr_units = nr_units;
11686 htab->nr_slots = nr_slots;
11687 htab->hash_table = index_ptr;
11688 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11689
11690 /* Exit early if the table is empty. */
11691 if (nr_slots == 0 || nr_units == 0
11692 || (version == 2 && nr_columns == 0))
11693 {
11694 /* All must be zero. */
11695 if (nr_slots != 0 || nr_units != 0
11696 || (version == 2 && nr_columns != 0))
11697 {
11698 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11699 " all zero [in modules %s]"),
11700 dwp_file->name);
11701 }
11702 return htab;
11703 }
11704
11705 if (version == 1)
11706 {
11707 htab->section_pool.v1.indices =
11708 htab->unit_table + sizeof (uint32_t) * nr_slots;
11709 /* It's harder to decide whether the section is too small in v1.
11710 V1 is deprecated anyway so we punt. */
11711 }
11712 else
11713 {
11714 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11715 int *ids = htab->section_pool.v2.section_ids;
11716 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11717 /* Reverse map for error checking. */
11718 int ids_seen[DW_SECT_MAX + 1];
11719 int i;
11720
11721 if (nr_columns < 2)
11722 {
11723 error (_("Dwarf Error: bad DWP hash table, too few columns"
11724 " in section table [in module %s]"),
11725 dwp_file->name);
11726 }
11727 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11728 {
11729 error (_("Dwarf Error: bad DWP hash table, too many columns"
11730 " in section table [in module %s]"),
11731 dwp_file->name);
11732 }
11733 memset (ids, 255, sizeof_ids);
11734 memset (ids_seen, 255, sizeof (ids_seen));
11735 for (i = 0; i < nr_columns; ++i)
11736 {
11737 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11738
11739 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11740 {
11741 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11742 " in section table [in module %s]"),
11743 id, dwp_file->name);
11744 }
11745 if (ids_seen[id] != -1)
11746 {
11747 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11748 " id %d in section table [in module %s]"),
11749 id, dwp_file->name);
11750 }
11751 ids_seen[id] = i;
11752 ids[i] = id;
11753 }
11754 /* Must have exactly one info or types section. */
11755 if (((ids_seen[DW_SECT_INFO] != -1)
11756 + (ids_seen[DW_SECT_TYPES] != -1))
11757 != 1)
11758 {
11759 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11760 " DWO info/types section [in module %s]"),
11761 dwp_file->name);
11762 }
11763 /* Must have an abbrev section. */
11764 if (ids_seen[DW_SECT_ABBREV] == -1)
11765 {
11766 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11767 " section [in module %s]"),
11768 dwp_file->name);
11769 }
11770 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11771 htab->section_pool.v2.sizes =
11772 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11773 * nr_units * nr_columns);
11774 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11775 * nr_units * nr_columns))
11776 > index_end)
11777 {
11778 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11779 " [in module %s]"),
11780 dwp_file->name);
11781 }
11782 }
11783
11784 return htab;
11785 }
11786
11787 /* Update SECTIONS with the data from SECTP.
11788
11789 This function is like the other "locate" section routines that are
11790 passed to bfd_map_over_sections, but in this context the sections to
11791 read comes from the DWP V1 hash table, not the full ELF section table.
11792
11793 The result is non-zero for success, or zero if an error was found. */
11794
11795 static int
11796 locate_v1_virtual_dwo_sections (asection *sectp,
11797 struct virtual_v1_dwo_sections *sections)
11798 {
11799 const struct dwop_section_names *names = &dwop_section_names;
11800
11801 if (section_is_p (sectp->name, &names->abbrev_dwo))
11802 {
11803 /* There can be only one. */
11804 if (sections->abbrev.s.section != NULL)
11805 return 0;
11806 sections->abbrev.s.section = sectp;
11807 sections->abbrev.size = bfd_section_size (sectp);
11808 }
11809 else if (section_is_p (sectp->name, &names->info_dwo)
11810 || section_is_p (sectp->name, &names->types_dwo))
11811 {
11812 /* There can be only one. */
11813 if (sections->info_or_types.s.section != NULL)
11814 return 0;
11815 sections->info_or_types.s.section = sectp;
11816 sections->info_or_types.size = bfd_section_size (sectp);
11817 }
11818 else if (section_is_p (sectp->name, &names->line_dwo))
11819 {
11820 /* There can be only one. */
11821 if (sections->line.s.section != NULL)
11822 return 0;
11823 sections->line.s.section = sectp;
11824 sections->line.size = bfd_section_size (sectp);
11825 }
11826 else if (section_is_p (sectp->name, &names->loc_dwo))
11827 {
11828 /* There can be only one. */
11829 if (sections->loc.s.section != NULL)
11830 return 0;
11831 sections->loc.s.section = sectp;
11832 sections->loc.size = bfd_section_size (sectp);
11833 }
11834 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11835 {
11836 /* There can be only one. */
11837 if (sections->macinfo.s.section != NULL)
11838 return 0;
11839 sections->macinfo.s.section = sectp;
11840 sections->macinfo.size = bfd_section_size (sectp);
11841 }
11842 else if (section_is_p (sectp->name, &names->macro_dwo))
11843 {
11844 /* There can be only one. */
11845 if (sections->macro.s.section != NULL)
11846 return 0;
11847 sections->macro.s.section = sectp;
11848 sections->macro.size = bfd_section_size (sectp);
11849 }
11850 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11851 {
11852 /* There can be only one. */
11853 if (sections->str_offsets.s.section != NULL)
11854 return 0;
11855 sections->str_offsets.s.section = sectp;
11856 sections->str_offsets.size = bfd_section_size (sectp);
11857 }
11858 else
11859 {
11860 /* No other kind of section is valid. */
11861 return 0;
11862 }
11863
11864 return 1;
11865 }
11866
11867 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11868 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11869 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11870 This is for DWP version 1 files. */
11871
11872 static struct dwo_unit *
11873 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11874 struct dwp_file *dwp_file,
11875 uint32_t unit_index,
11876 const char *comp_dir,
11877 ULONGEST signature, int is_debug_types)
11878 {
11879 const struct dwp_hash_table *dwp_htab =
11880 is_debug_types ? dwp_file->tus : dwp_file->cus;
11881 bfd *dbfd = dwp_file->dbfd.get ();
11882 const char *kind = is_debug_types ? "TU" : "CU";
11883 struct dwo_file *dwo_file;
11884 struct dwo_unit *dwo_unit;
11885 struct virtual_v1_dwo_sections sections;
11886 void **dwo_file_slot;
11887 int i;
11888
11889 gdb_assert (dwp_file->version == 1);
11890
11891 if (dwarf_read_debug)
11892 {
11893 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11894 kind,
11895 pulongest (unit_index), hex_string (signature),
11896 dwp_file->name);
11897 }
11898
11899 /* Fetch the sections of this DWO unit.
11900 Put a limit on the number of sections we look for so that bad data
11901 doesn't cause us to loop forever. */
11902
11903 #define MAX_NR_V1_DWO_SECTIONS \
11904 (1 /* .debug_info or .debug_types */ \
11905 + 1 /* .debug_abbrev */ \
11906 + 1 /* .debug_line */ \
11907 + 1 /* .debug_loc */ \
11908 + 1 /* .debug_str_offsets */ \
11909 + 1 /* .debug_macro or .debug_macinfo */ \
11910 + 1 /* trailing zero */)
11911
11912 memset (&sections, 0, sizeof (sections));
11913
11914 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11915 {
11916 asection *sectp;
11917 uint32_t section_nr =
11918 read_4_bytes (dbfd,
11919 dwp_htab->section_pool.v1.indices
11920 + (unit_index + i) * sizeof (uint32_t));
11921
11922 if (section_nr == 0)
11923 break;
11924 if (section_nr >= dwp_file->num_sections)
11925 {
11926 error (_("Dwarf Error: bad DWP hash table, section number too large"
11927 " [in module %s]"),
11928 dwp_file->name);
11929 }
11930
11931 sectp = dwp_file->elf_sections[section_nr];
11932 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11933 {
11934 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11935 " [in module %s]"),
11936 dwp_file->name);
11937 }
11938 }
11939
11940 if (i < 2
11941 || sections.info_or_types.empty ()
11942 || sections.abbrev.empty ())
11943 {
11944 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11945 " [in module %s]"),
11946 dwp_file->name);
11947 }
11948 if (i == MAX_NR_V1_DWO_SECTIONS)
11949 {
11950 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11951 " [in module %s]"),
11952 dwp_file->name);
11953 }
11954
11955 /* It's easier for the rest of the code if we fake a struct dwo_file and
11956 have dwo_unit "live" in that. At least for now.
11957
11958 The DWP file can be made up of a random collection of CUs and TUs.
11959 However, for each CU + set of TUs that came from the same original DWO
11960 file, we can combine them back into a virtual DWO file to save space
11961 (fewer struct dwo_file objects to allocate). Remember that for really
11962 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11963
11964 std::string virtual_dwo_name =
11965 string_printf ("virtual-dwo/%d-%d-%d-%d",
11966 sections.abbrev.get_id (),
11967 sections.line.get_id (),
11968 sections.loc.get_id (),
11969 sections.str_offsets.get_id ());
11970 /* Can we use an existing virtual DWO file? */
11971 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11972 comp_dir);
11973 /* Create one if necessary. */
11974 if (*dwo_file_slot == NULL)
11975 {
11976 if (dwarf_read_debug)
11977 {
11978 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11979 virtual_dwo_name.c_str ());
11980 }
11981 dwo_file = new struct dwo_file;
11982 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11983 dwo_file->comp_dir = comp_dir;
11984 dwo_file->sections.abbrev = sections.abbrev;
11985 dwo_file->sections.line = sections.line;
11986 dwo_file->sections.loc = sections.loc;
11987 dwo_file->sections.macinfo = sections.macinfo;
11988 dwo_file->sections.macro = sections.macro;
11989 dwo_file->sections.str_offsets = sections.str_offsets;
11990 /* The "str" section is global to the entire DWP file. */
11991 dwo_file->sections.str = dwp_file->sections.str;
11992 /* The info or types section is assigned below to dwo_unit,
11993 there's no need to record it in dwo_file.
11994 Also, we can't simply record type sections in dwo_file because
11995 we record a pointer into the vector in dwo_unit. As we collect more
11996 types we'll grow the vector and eventually have to reallocate space
11997 for it, invalidating all copies of pointers into the previous
11998 contents. */
11999 *dwo_file_slot = dwo_file;
12000 }
12001 else
12002 {
12003 if (dwarf_read_debug)
12004 {
12005 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12006 virtual_dwo_name.c_str ());
12007 }
12008 dwo_file = (struct dwo_file *) *dwo_file_slot;
12009 }
12010
12011 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12012 dwo_unit->dwo_file = dwo_file;
12013 dwo_unit->signature = signature;
12014 dwo_unit->section =
12015 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12016 *dwo_unit->section = sections.info_or_types;
12017 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12018
12019 return dwo_unit;
12020 }
12021
12022 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12023 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12024 piece within that section used by a TU/CU, return a virtual section
12025 of just that piece. */
12026
12027 static struct dwarf2_section_info
12028 create_dwp_v2_section (dwarf2_per_objfile *per_objfile,
12029 struct dwarf2_section_info *section,
12030 bfd_size_type offset, bfd_size_type size)
12031 {
12032 struct dwarf2_section_info result;
12033 asection *sectp;
12034
12035 gdb_assert (section != NULL);
12036 gdb_assert (!section->is_virtual);
12037
12038 memset (&result, 0, sizeof (result));
12039 result.s.containing_section = section;
12040 result.is_virtual = true;
12041
12042 if (size == 0)
12043 return result;
12044
12045 sectp = section->get_bfd_section ();
12046
12047 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12048 bounds of the real section. This is a pretty-rare event, so just
12049 flag an error (easier) instead of a warning and trying to cope. */
12050 if (sectp == NULL
12051 || offset + size > bfd_section_size (sectp))
12052 {
12053 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12054 " in section %s [in module %s]"),
12055 sectp ? bfd_section_name (sectp) : "<unknown>",
12056 objfile_name (per_objfile->objfile));
12057 }
12058
12059 result.virtual_offset = offset;
12060 result.size = size;
12061 return result;
12062 }
12063
12064 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12065 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12066 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12067 This is for DWP version 2 files. */
12068
12069 static struct dwo_unit *
12070 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12071 struct dwp_file *dwp_file,
12072 uint32_t unit_index,
12073 const char *comp_dir,
12074 ULONGEST signature, int is_debug_types)
12075 {
12076 const struct dwp_hash_table *dwp_htab =
12077 is_debug_types ? dwp_file->tus : dwp_file->cus;
12078 bfd *dbfd = dwp_file->dbfd.get ();
12079 const char *kind = is_debug_types ? "TU" : "CU";
12080 struct dwo_file *dwo_file;
12081 struct dwo_unit *dwo_unit;
12082 struct virtual_v2_dwo_sections sections;
12083 void **dwo_file_slot;
12084 int i;
12085
12086 gdb_assert (dwp_file->version == 2);
12087
12088 if (dwarf_read_debug)
12089 {
12090 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12091 kind,
12092 pulongest (unit_index), hex_string (signature),
12093 dwp_file->name);
12094 }
12095
12096 /* Fetch the section offsets of this DWO unit. */
12097
12098 memset (&sections, 0, sizeof (sections));
12099
12100 for (i = 0; i < dwp_htab->nr_columns; ++i)
12101 {
12102 uint32_t offset = read_4_bytes (dbfd,
12103 dwp_htab->section_pool.v2.offsets
12104 + (((unit_index - 1) * dwp_htab->nr_columns
12105 + i)
12106 * sizeof (uint32_t)));
12107 uint32_t size = read_4_bytes (dbfd,
12108 dwp_htab->section_pool.v2.sizes
12109 + (((unit_index - 1) * dwp_htab->nr_columns
12110 + i)
12111 * sizeof (uint32_t)));
12112
12113 switch (dwp_htab->section_pool.v2.section_ids[i])
12114 {
12115 case DW_SECT_INFO:
12116 case DW_SECT_TYPES:
12117 sections.info_or_types_offset = offset;
12118 sections.info_or_types_size = size;
12119 break;
12120 case DW_SECT_ABBREV:
12121 sections.abbrev_offset = offset;
12122 sections.abbrev_size = size;
12123 break;
12124 case DW_SECT_LINE:
12125 sections.line_offset = offset;
12126 sections.line_size = size;
12127 break;
12128 case DW_SECT_LOC:
12129 sections.loc_offset = offset;
12130 sections.loc_size = size;
12131 break;
12132 case DW_SECT_STR_OFFSETS:
12133 sections.str_offsets_offset = offset;
12134 sections.str_offsets_size = size;
12135 break;
12136 case DW_SECT_MACINFO:
12137 sections.macinfo_offset = offset;
12138 sections.macinfo_size = size;
12139 break;
12140 case DW_SECT_MACRO:
12141 sections.macro_offset = offset;
12142 sections.macro_size = size;
12143 break;
12144 }
12145 }
12146
12147 /* It's easier for the rest of the code if we fake a struct dwo_file and
12148 have dwo_unit "live" in that. At least for now.
12149
12150 The DWP file can be made up of a random collection of CUs and TUs.
12151 However, for each CU + set of TUs that came from the same original DWO
12152 file, we can combine them back into a virtual DWO file to save space
12153 (fewer struct dwo_file objects to allocate). Remember that for really
12154 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12155
12156 std::string virtual_dwo_name =
12157 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12158 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12159 (long) (sections.line_size ? sections.line_offset : 0),
12160 (long) (sections.loc_size ? sections.loc_offset : 0),
12161 (long) (sections.str_offsets_size
12162 ? sections.str_offsets_offset : 0));
12163 /* Can we use an existing virtual DWO file? */
12164 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12165 comp_dir);
12166 /* Create one if necessary. */
12167 if (*dwo_file_slot == NULL)
12168 {
12169 if (dwarf_read_debug)
12170 {
12171 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12172 virtual_dwo_name.c_str ());
12173 }
12174 dwo_file = new struct dwo_file;
12175 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12176 dwo_file->comp_dir = comp_dir;
12177 dwo_file->sections.abbrev =
12178 create_dwp_v2_section (per_objfile, &dwp_file->sections.abbrev,
12179 sections.abbrev_offset, sections.abbrev_size);
12180 dwo_file->sections.line =
12181 create_dwp_v2_section (per_objfile, &dwp_file->sections.line,
12182 sections.line_offset, sections.line_size);
12183 dwo_file->sections.loc =
12184 create_dwp_v2_section (per_objfile, &dwp_file->sections.loc,
12185 sections.loc_offset, sections.loc_size);
12186 dwo_file->sections.macinfo =
12187 create_dwp_v2_section (per_objfile, &dwp_file->sections.macinfo,
12188 sections.macinfo_offset, sections.macinfo_size);
12189 dwo_file->sections.macro =
12190 create_dwp_v2_section (per_objfile, &dwp_file->sections.macro,
12191 sections.macro_offset, sections.macro_size);
12192 dwo_file->sections.str_offsets =
12193 create_dwp_v2_section (per_objfile,
12194 &dwp_file->sections.str_offsets,
12195 sections.str_offsets_offset,
12196 sections.str_offsets_size);
12197 /* The "str" section is global to the entire DWP file. */
12198 dwo_file->sections.str = dwp_file->sections.str;
12199 /* The info or types section is assigned below to dwo_unit,
12200 there's no need to record it in dwo_file.
12201 Also, we can't simply record type sections in dwo_file because
12202 we record a pointer into the vector in dwo_unit. As we collect more
12203 types we'll grow the vector and eventually have to reallocate space
12204 for it, invalidating all copies of pointers into the previous
12205 contents. */
12206 *dwo_file_slot = dwo_file;
12207 }
12208 else
12209 {
12210 if (dwarf_read_debug)
12211 {
12212 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12213 virtual_dwo_name.c_str ());
12214 }
12215 dwo_file = (struct dwo_file *) *dwo_file_slot;
12216 }
12217
12218 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12219 dwo_unit->dwo_file = dwo_file;
12220 dwo_unit->signature = signature;
12221 dwo_unit->section =
12222 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12223 *dwo_unit->section = create_dwp_v2_section (per_objfile,
12224 is_debug_types
12225 ? &dwp_file->sections.types
12226 : &dwp_file->sections.info,
12227 sections.info_or_types_offset,
12228 sections.info_or_types_size);
12229 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12230
12231 return dwo_unit;
12232 }
12233
12234 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12235 Returns NULL if the signature isn't found. */
12236
12237 static struct dwo_unit *
12238 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12239 struct dwp_file *dwp_file, const char *comp_dir,
12240 ULONGEST signature, int is_debug_types)
12241 {
12242 const struct dwp_hash_table *dwp_htab =
12243 is_debug_types ? dwp_file->tus : dwp_file->cus;
12244 bfd *dbfd = dwp_file->dbfd.get ();
12245 uint32_t mask = dwp_htab->nr_slots - 1;
12246 uint32_t hash = signature & mask;
12247 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12248 unsigned int i;
12249 void **slot;
12250 struct dwo_unit find_dwo_cu;
12251
12252 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12253 find_dwo_cu.signature = signature;
12254 slot = htab_find_slot (is_debug_types
12255 ? dwp_file->loaded_tus.get ()
12256 : dwp_file->loaded_cus.get (),
12257 &find_dwo_cu, INSERT);
12258
12259 if (*slot != NULL)
12260 return (struct dwo_unit *) *slot;
12261
12262 /* Use a for loop so that we don't loop forever on bad debug info. */
12263 for (i = 0; i < dwp_htab->nr_slots; ++i)
12264 {
12265 ULONGEST signature_in_table;
12266
12267 signature_in_table =
12268 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12269 if (signature_in_table == signature)
12270 {
12271 uint32_t unit_index =
12272 read_4_bytes (dbfd,
12273 dwp_htab->unit_table + hash * sizeof (uint32_t));
12274
12275 if (dwp_file->version == 1)
12276 {
12277 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12278 unit_index, comp_dir,
12279 signature, is_debug_types);
12280 }
12281 else
12282 {
12283 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12284 unit_index, comp_dir,
12285 signature, is_debug_types);
12286 }
12287 return (struct dwo_unit *) *slot;
12288 }
12289 if (signature_in_table == 0)
12290 return NULL;
12291 hash = (hash + hash2) & mask;
12292 }
12293
12294 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12295 " [in module %s]"),
12296 dwp_file->name);
12297 }
12298
12299 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12300 Open the file specified by FILE_NAME and hand it off to BFD for
12301 preliminary analysis. Return a newly initialized bfd *, which
12302 includes a canonicalized copy of FILE_NAME.
12303 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12304 SEARCH_CWD is true if the current directory is to be searched.
12305 It will be searched before debug-file-directory.
12306 If successful, the file is added to the bfd include table of the
12307 objfile's bfd (see gdb_bfd_record_inclusion).
12308 If unable to find/open the file, return NULL.
12309 NOTE: This function is derived from symfile_bfd_open. */
12310
12311 static gdb_bfd_ref_ptr
12312 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12313 const char *file_name, int is_dwp, int search_cwd)
12314 {
12315 int desc;
12316 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12317 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12318 to debug_file_directory. */
12319 const char *search_path;
12320 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12321
12322 gdb::unique_xmalloc_ptr<char> search_path_holder;
12323 if (search_cwd)
12324 {
12325 if (*debug_file_directory != '\0')
12326 {
12327 search_path_holder.reset (concat (".", dirname_separator_string,
12328 debug_file_directory,
12329 (char *) NULL));
12330 search_path = search_path_holder.get ();
12331 }
12332 else
12333 search_path = ".";
12334 }
12335 else
12336 search_path = debug_file_directory;
12337
12338 openp_flags flags = OPF_RETURN_REALPATH;
12339 if (is_dwp)
12340 flags |= OPF_SEARCH_IN_PATH;
12341
12342 gdb::unique_xmalloc_ptr<char> absolute_name;
12343 desc = openp (search_path, flags, file_name,
12344 O_RDONLY | O_BINARY, &absolute_name);
12345 if (desc < 0)
12346 return NULL;
12347
12348 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12349 gnutarget, desc));
12350 if (sym_bfd == NULL)
12351 return NULL;
12352 bfd_set_cacheable (sym_bfd.get (), 1);
12353
12354 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12355 return NULL;
12356
12357 /* Success. Record the bfd as having been included by the objfile's bfd.
12358 This is important because things like demangled_names_hash lives in the
12359 objfile's per_bfd space and may have references to things like symbol
12360 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12361 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12362
12363 return sym_bfd;
12364 }
12365
12366 /* Try to open DWO file FILE_NAME.
12367 COMP_DIR is the DW_AT_comp_dir attribute.
12368 The result is the bfd handle of the file.
12369 If there is a problem finding or opening the file, return NULL.
12370 Upon success, the canonicalized path of the file is stored in the bfd,
12371 same as symfile_bfd_open. */
12372
12373 static gdb_bfd_ref_ptr
12374 open_dwo_file (dwarf2_per_objfile *per_objfile,
12375 const char *file_name, const char *comp_dir)
12376 {
12377 if (IS_ABSOLUTE_PATH (file_name))
12378 return try_open_dwop_file (per_objfile, file_name,
12379 0 /*is_dwp*/, 0 /*search_cwd*/);
12380
12381 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12382
12383 if (comp_dir != NULL)
12384 {
12385 gdb::unique_xmalloc_ptr<char> path_to_try
12386 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12387
12388 /* NOTE: If comp_dir is a relative path, this will also try the
12389 search path, which seems useful. */
12390 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12391 0 /*is_dwp*/,
12392 1 /*search_cwd*/));
12393 if (abfd != NULL)
12394 return abfd;
12395 }
12396
12397 /* That didn't work, try debug-file-directory, which, despite its name,
12398 is a list of paths. */
12399
12400 if (*debug_file_directory == '\0')
12401 return NULL;
12402
12403 return try_open_dwop_file (per_objfile, file_name,
12404 0 /*is_dwp*/, 1 /*search_cwd*/);
12405 }
12406
12407 /* This function is mapped across the sections and remembers the offset and
12408 size of each of the DWO debugging sections we are interested in. */
12409
12410 static void
12411 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12412 {
12413 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12414 const struct dwop_section_names *names = &dwop_section_names;
12415
12416 if (section_is_p (sectp->name, &names->abbrev_dwo))
12417 {
12418 dwo_sections->abbrev.s.section = sectp;
12419 dwo_sections->abbrev.size = bfd_section_size (sectp);
12420 }
12421 else if (section_is_p (sectp->name, &names->info_dwo))
12422 {
12423 dwo_sections->info.s.section = sectp;
12424 dwo_sections->info.size = bfd_section_size (sectp);
12425 }
12426 else if (section_is_p (sectp->name, &names->line_dwo))
12427 {
12428 dwo_sections->line.s.section = sectp;
12429 dwo_sections->line.size = bfd_section_size (sectp);
12430 }
12431 else if (section_is_p (sectp->name, &names->loc_dwo))
12432 {
12433 dwo_sections->loc.s.section = sectp;
12434 dwo_sections->loc.size = bfd_section_size (sectp);
12435 }
12436 else if (section_is_p (sectp->name, &names->loclists_dwo))
12437 {
12438 dwo_sections->loclists.s.section = sectp;
12439 dwo_sections->loclists.size = bfd_section_size (sectp);
12440 }
12441 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12442 {
12443 dwo_sections->macinfo.s.section = sectp;
12444 dwo_sections->macinfo.size = bfd_section_size (sectp);
12445 }
12446 else if (section_is_p (sectp->name, &names->macro_dwo))
12447 {
12448 dwo_sections->macro.s.section = sectp;
12449 dwo_sections->macro.size = bfd_section_size (sectp);
12450 }
12451 else if (section_is_p (sectp->name, &names->str_dwo))
12452 {
12453 dwo_sections->str.s.section = sectp;
12454 dwo_sections->str.size = bfd_section_size (sectp);
12455 }
12456 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12457 {
12458 dwo_sections->str_offsets.s.section = sectp;
12459 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12460 }
12461 else if (section_is_p (sectp->name, &names->types_dwo))
12462 {
12463 struct dwarf2_section_info type_section;
12464
12465 memset (&type_section, 0, sizeof (type_section));
12466 type_section.s.section = sectp;
12467 type_section.size = bfd_section_size (sectp);
12468 dwo_sections->types.push_back (type_section);
12469 }
12470 }
12471
12472 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12473 by PER_CU. This is for the non-DWP case.
12474 The result is NULL if DWO_NAME can't be found. */
12475
12476 static struct dwo_file *
12477 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12478 const char *comp_dir)
12479 {
12480 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12481
12482 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12483 if (dbfd == NULL)
12484 {
12485 if (dwarf_read_debug)
12486 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12487 return NULL;
12488 }
12489
12490 dwo_file_up dwo_file (new struct dwo_file);
12491 dwo_file->dwo_name = dwo_name;
12492 dwo_file->comp_dir = comp_dir;
12493 dwo_file->dbfd = std::move (dbfd);
12494
12495 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12496 &dwo_file->sections);
12497
12498 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12499 dwo_file->cus);
12500
12501 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12502 dwo_file->sections.types, dwo_file->tus);
12503
12504 if (dwarf_read_debug)
12505 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12506
12507 return dwo_file.release ();
12508 }
12509
12510 /* This function is mapped across the sections and remembers the offset and
12511 size of each of the DWP debugging sections common to version 1 and 2 that
12512 we are interested in. */
12513
12514 static void
12515 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12516 void *dwp_file_ptr)
12517 {
12518 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12519 const struct dwop_section_names *names = &dwop_section_names;
12520 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12521
12522 /* Record the ELF section number for later lookup: this is what the
12523 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12524 gdb_assert (elf_section_nr < dwp_file->num_sections);
12525 dwp_file->elf_sections[elf_section_nr] = sectp;
12526
12527 /* Look for specific sections that we need. */
12528 if (section_is_p (sectp->name, &names->str_dwo))
12529 {
12530 dwp_file->sections.str.s.section = sectp;
12531 dwp_file->sections.str.size = bfd_section_size (sectp);
12532 }
12533 else if (section_is_p (sectp->name, &names->cu_index))
12534 {
12535 dwp_file->sections.cu_index.s.section = sectp;
12536 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12537 }
12538 else if (section_is_p (sectp->name, &names->tu_index))
12539 {
12540 dwp_file->sections.tu_index.s.section = sectp;
12541 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12542 }
12543 }
12544
12545 /* This function is mapped across the sections and remembers the offset and
12546 size of each of the DWP version 2 debugging sections that we are interested
12547 in. This is split into a separate function because we don't know if we
12548 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12549
12550 static void
12551 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12552 {
12553 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12554 const struct dwop_section_names *names = &dwop_section_names;
12555 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12556
12557 /* Record the ELF section number for later lookup: this is what the
12558 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12559 gdb_assert (elf_section_nr < dwp_file->num_sections);
12560 dwp_file->elf_sections[elf_section_nr] = sectp;
12561
12562 /* Look for specific sections that we need. */
12563 if (section_is_p (sectp->name, &names->abbrev_dwo))
12564 {
12565 dwp_file->sections.abbrev.s.section = sectp;
12566 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12567 }
12568 else if (section_is_p (sectp->name, &names->info_dwo))
12569 {
12570 dwp_file->sections.info.s.section = sectp;
12571 dwp_file->sections.info.size = bfd_section_size (sectp);
12572 }
12573 else if (section_is_p (sectp->name, &names->line_dwo))
12574 {
12575 dwp_file->sections.line.s.section = sectp;
12576 dwp_file->sections.line.size = bfd_section_size (sectp);
12577 }
12578 else if (section_is_p (sectp->name, &names->loc_dwo))
12579 {
12580 dwp_file->sections.loc.s.section = sectp;
12581 dwp_file->sections.loc.size = bfd_section_size (sectp);
12582 }
12583 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12584 {
12585 dwp_file->sections.macinfo.s.section = sectp;
12586 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12587 }
12588 else if (section_is_p (sectp->name, &names->macro_dwo))
12589 {
12590 dwp_file->sections.macro.s.section = sectp;
12591 dwp_file->sections.macro.size = bfd_section_size (sectp);
12592 }
12593 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12594 {
12595 dwp_file->sections.str_offsets.s.section = sectp;
12596 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12597 }
12598 else if (section_is_p (sectp->name, &names->types_dwo))
12599 {
12600 dwp_file->sections.types.s.section = sectp;
12601 dwp_file->sections.types.size = bfd_section_size (sectp);
12602 }
12603 }
12604
12605 /* Hash function for dwp_file loaded CUs/TUs. */
12606
12607 static hashval_t
12608 hash_dwp_loaded_cutus (const void *item)
12609 {
12610 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12611
12612 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12613 return dwo_unit->signature;
12614 }
12615
12616 /* Equality function for dwp_file loaded CUs/TUs. */
12617
12618 static int
12619 eq_dwp_loaded_cutus (const void *a, const void *b)
12620 {
12621 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12622 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12623
12624 return dua->signature == dub->signature;
12625 }
12626
12627 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12628
12629 static htab_up
12630 allocate_dwp_loaded_cutus_table ()
12631 {
12632 return htab_up (htab_create_alloc (3,
12633 hash_dwp_loaded_cutus,
12634 eq_dwp_loaded_cutus,
12635 NULL, xcalloc, xfree));
12636 }
12637
12638 /* Try to open DWP file FILE_NAME.
12639 The result is the bfd handle of the file.
12640 If there is a problem finding or opening the file, return NULL.
12641 Upon success, the canonicalized path of the file is stored in the bfd,
12642 same as symfile_bfd_open. */
12643
12644 static gdb_bfd_ref_ptr
12645 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12646 {
12647 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12648 1 /*is_dwp*/,
12649 1 /*search_cwd*/));
12650 if (abfd != NULL)
12651 return abfd;
12652
12653 /* Work around upstream bug 15652.
12654 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12655 [Whether that's a "bug" is debatable, but it is getting in our way.]
12656 We have no real idea where the dwp file is, because gdb's realpath-ing
12657 of the executable's path may have discarded the needed info.
12658 [IWBN if the dwp file name was recorded in the executable, akin to
12659 .gnu_debuglink, but that doesn't exist yet.]
12660 Strip the directory from FILE_NAME and search again. */
12661 if (*debug_file_directory != '\0')
12662 {
12663 /* Don't implicitly search the current directory here.
12664 If the user wants to search "." to handle this case,
12665 it must be added to debug-file-directory. */
12666 return try_open_dwop_file (per_objfile, lbasename (file_name),
12667 1 /*is_dwp*/,
12668 0 /*search_cwd*/);
12669 }
12670
12671 return NULL;
12672 }
12673
12674 /* Initialize the use of the DWP file for the current objfile.
12675 By convention the name of the DWP file is ${objfile}.dwp.
12676 The result is NULL if it can't be found. */
12677
12678 static std::unique_ptr<struct dwp_file>
12679 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12680 {
12681 struct objfile *objfile = per_objfile->objfile;
12682
12683 /* Try to find first .dwp for the binary file before any symbolic links
12684 resolving. */
12685
12686 /* If the objfile is a debug file, find the name of the real binary
12687 file and get the name of dwp file from there. */
12688 std::string dwp_name;
12689 if (objfile->separate_debug_objfile_backlink != NULL)
12690 {
12691 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12692 const char *backlink_basename = lbasename (backlink->original_name);
12693
12694 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12695 }
12696 else
12697 dwp_name = objfile->original_name;
12698
12699 dwp_name += ".dwp";
12700
12701 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12702 if (dbfd == NULL
12703 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12704 {
12705 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12706 dwp_name = objfile_name (objfile);
12707 dwp_name += ".dwp";
12708 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12709 }
12710
12711 if (dbfd == NULL)
12712 {
12713 if (dwarf_read_debug)
12714 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12715 return std::unique_ptr<dwp_file> ();
12716 }
12717
12718 const char *name = bfd_get_filename (dbfd.get ());
12719 std::unique_ptr<struct dwp_file> dwp_file
12720 (new struct dwp_file (name, std::move (dbfd)));
12721
12722 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12723 dwp_file->elf_sections =
12724 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12725 dwp_file->num_sections, asection *);
12726
12727 bfd_map_over_sections (dwp_file->dbfd.get (),
12728 dwarf2_locate_common_dwp_sections,
12729 dwp_file.get ());
12730
12731 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12732
12733 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12734
12735 /* The DWP file version is stored in the hash table. Oh well. */
12736 if (dwp_file->cus && dwp_file->tus
12737 && dwp_file->cus->version != dwp_file->tus->version)
12738 {
12739 /* Technically speaking, we should try to limp along, but this is
12740 pretty bizarre. We use pulongest here because that's the established
12741 portability solution (e.g, we cannot use %u for uint32_t). */
12742 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12743 " TU version %s [in DWP file %s]"),
12744 pulongest (dwp_file->cus->version),
12745 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12746 }
12747
12748 if (dwp_file->cus)
12749 dwp_file->version = dwp_file->cus->version;
12750 else if (dwp_file->tus)
12751 dwp_file->version = dwp_file->tus->version;
12752 else
12753 dwp_file->version = 2;
12754
12755 if (dwp_file->version == 2)
12756 bfd_map_over_sections (dwp_file->dbfd.get (),
12757 dwarf2_locate_v2_dwp_sections,
12758 dwp_file.get ());
12759
12760 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12761 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12762
12763 if (dwarf_read_debug)
12764 {
12765 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12766 fprintf_unfiltered (gdb_stdlog,
12767 " %s CUs, %s TUs\n",
12768 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12769 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12770 }
12771
12772 return dwp_file;
12773 }
12774
12775 /* Wrapper around open_and_init_dwp_file, only open it once. */
12776
12777 static struct dwp_file *
12778 get_dwp_file (dwarf2_per_objfile *per_objfile)
12779 {
12780 if (!per_objfile->per_bfd->dwp_checked)
12781 {
12782 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12783 per_objfile->per_bfd->dwp_checked = 1;
12784 }
12785 return per_objfile->per_bfd->dwp_file.get ();
12786 }
12787
12788 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12789 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12790 or in the DWP file for the objfile, referenced by THIS_UNIT.
12791 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12792 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12793
12794 This is called, for example, when wanting to read a variable with a
12795 complex location. Therefore we don't want to do file i/o for every call.
12796 Therefore we don't want to look for a DWO file on every call.
12797 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12798 then we check if we've already seen DWO_NAME, and only THEN do we check
12799 for a DWO file.
12800
12801 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12802 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12803
12804 static struct dwo_unit *
12805 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12806 ULONGEST signature, int is_debug_types)
12807 {
12808 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12809 struct objfile *objfile = per_objfile->objfile;
12810 const char *kind = is_debug_types ? "TU" : "CU";
12811 void **dwo_file_slot;
12812 struct dwo_file *dwo_file;
12813 struct dwp_file *dwp_file;
12814
12815 /* First see if there's a DWP file.
12816 If we have a DWP file but didn't find the DWO inside it, don't
12817 look for the original DWO file. It makes gdb behave differently
12818 depending on whether one is debugging in the build tree. */
12819
12820 dwp_file = get_dwp_file (per_objfile);
12821 if (dwp_file != NULL)
12822 {
12823 const struct dwp_hash_table *dwp_htab =
12824 is_debug_types ? dwp_file->tus : dwp_file->cus;
12825
12826 if (dwp_htab != NULL)
12827 {
12828 struct dwo_unit *dwo_cutu =
12829 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12830 is_debug_types);
12831
12832 if (dwo_cutu != NULL)
12833 {
12834 if (dwarf_read_debug)
12835 {
12836 fprintf_unfiltered (gdb_stdlog,
12837 "Virtual DWO %s %s found: @%s\n",
12838 kind, hex_string (signature),
12839 host_address_to_string (dwo_cutu));
12840 }
12841 return dwo_cutu;
12842 }
12843 }
12844 }
12845 else
12846 {
12847 /* No DWP file, look for the DWO file. */
12848
12849 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12850 if (*dwo_file_slot == NULL)
12851 {
12852 /* Read in the file and build a table of the CUs/TUs it contains. */
12853 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12854 }
12855 /* NOTE: This will be NULL if unable to open the file. */
12856 dwo_file = (struct dwo_file *) *dwo_file_slot;
12857
12858 if (dwo_file != NULL)
12859 {
12860 struct dwo_unit *dwo_cutu = NULL;
12861
12862 if (is_debug_types && dwo_file->tus)
12863 {
12864 struct dwo_unit find_dwo_cutu;
12865
12866 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12867 find_dwo_cutu.signature = signature;
12868 dwo_cutu
12869 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12870 &find_dwo_cutu);
12871 }
12872 else if (!is_debug_types && dwo_file->cus)
12873 {
12874 struct dwo_unit find_dwo_cutu;
12875
12876 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12877 find_dwo_cutu.signature = signature;
12878 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12879 &find_dwo_cutu);
12880 }
12881
12882 if (dwo_cutu != NULL)
12883 {
12884 if (dwarf_read_debug)
12885 {
12886 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12887 kind, dwo_name, hex_string (signature),
12888 host_address_to_string (dwo_cutu));
12889 }
12890 return dwo_cutu;
12891 }
12892 }
12893 }
12894
12895 /* We didn't find it. This could mean a dwo_id mismatch, or
12896 someone deleted the DWO/DWP file, or the search path isn't set up
12897 correctly to find the file. */
12898
12899 if (dwarf_read_debug)
12900 {
12901 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12902 kind, dwo_name, hex_string (signature));
12903 }
12904
12905 /* This is a warning and not a complaint because it can be caused by
12906 pilot error (e.g., user accidentally deleting the DWO). */
12907 {
12908 /* Print the name of the DWP file if we looked there, helps the user
12909 better diagnose the problem. */
12910 std::string dwp_text;
12911
12912 if (dwp_file != NULL)
12913 dwp_text = string_printf (" [in DWP file %s]",
12914 lbasename (dwp_file->name));
12915
12916 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12917 " [in module %s]"),
12918 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12919 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12920 }
12921 return NULL;
12922 }
12923
12924 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12925 See lookup_dwo_cutu_unit for details. */
12926
12927 static struct dwo_unit *
12928 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12929 ULONGEST signature)
12930 {
12931 gdb_assert (!cu->per_cu->is_debug_types);
12932
12933 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12934 }
12935
12936 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12937 See lookup_dwo_cutu_unit for details. */
12938
12939 static struct dwo_unit *
12940 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12941 {
12942 gdb_assert (cu->per_cu->is_debug_types);
12943
12944 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12945
12946 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12947 }
12948
12949 /* Traversal function for queue_and_load_all_dwo_tus. */
12950
12951 static int
12952 queue_and_load_dwo_tu (void **slot, void *info)
12953 {
12954 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12955 dwarf2_cu *cu = (dwarf2_cu *) info;
12956 ULONGEST signature = dwo_unit->signature;
12957 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12958
12959 if (sig_type != NULL)
12960 {
12961 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12962
12963 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12964 a real dependency of PER_CU on SIG_TYPE. That is detected later
12965 while processing PER_CU. */
12966 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12967 load_full_type_unit (sig_cu, cu->per_objfile);
12968 cu->per_cu->imported_symtabs_push (sig_cu);
12969 }
12970
12971 return 1;
12972 }
12973
12974 /* Queue all TUs contained in the DWO of CU to be read in.
12975 The DWO may have the only definition of the type, though it may not be
12976 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12977 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12978
12979 static void
12980 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12981 {
12982 struct dwo_unit *dwo_unit;
12983 struct dwo_file *dwo_file;
12984
12985 gdb_assert (cu != nullptr);
12986 gdb_assert (!cu->per_cu->is_debug_types);
12987 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12988
12989 dwo_unit = cu->dwo_unit;
12990 gdb_assert (dwo_unit != NULL);
12991
12992 dwo_file = dwo_unit->dwo_file;
12993 if (dwo_file->tus != NULL)
12994 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12995 }
12996
12997 /* Read in various DIEs. */
12998
12999 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13000 Inherit only the children of the DW_AT_abstract_origin DIE not being
13001 already referenced by DW_AT_abstract_origin from the children of the
13002 current DIE. */
13003
13004 static void
13005 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13006 {
13007 struct die_info *child_die;
13008 sect_offset *offsetp;
13009 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13010 struct die_info *origin_die;
13011 /* Iterator of the ORIGIN_DIE children. */
13012 struct die_info *origin_child_die;
13013 struct attribute *attr;
13014 struct dwarf2_cu *origin_cu;
13015 struct pending **origin_previous_list_in_scope;
13016
13017 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13018 if (!attr)
13019 return;
13020
13021 /* Note that following die references may follow to a die in a
13022 different cu. */
13023
13024 origin_cu = cu;
13025 origin_die = follow_die_ref (die, attr, &origin_cu);
13026
13027 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13028 symbols in. */
13029 origin_previous_list_in_scope = origin_cu->list_in_scope;
13030 origin_cu->list_in_scope = cu->list_in_scope;
13031
13032 if (die->tag != origin_die->tag
13033 && !(die->tag == DW_TAG_inlined_subroutine
13034 && origin_die->tag == DW_TAG_subprogram))
13035 complaint (_("DIE %s and its abstract origin %s have different tags"),
13036 sect_offset_str (die->sect_off),
13037 sect_offset_str (origin_die->sect_off));
13038
13039 std::vector<sect_offset> offsets;
13040
13041 for (child_die = die->child;
13042 child_die && child_die->tag;
13043 child_die = child_die->sibling)
13044 {
13045 struct die_info *child_origin_die;
13046 struct dwarf2_cu *child_origin_cu;
13047
13048 /* We are trying to process concrete instance entries:
13049 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13050 it's not relevant to our analysis here. i.e. detecting DIEs that are
13051 present in the abstract instance but not referenced in the concrete
13052 one. */
13053 if (child_die->tag == DW_TAG_call_site
13054 || child_die->tag == DW_TAG_GNU_call_site)
13055 continue;
13056
13057 /* For each CHILD_DIE, find the corresponding child of
13058 ORIGIN_DIE. If there is more than one layer of
13059 DW_AT_abstract_origin, follow them all; there shouldn't be,
13060 but GCC versions at least through 4.4 generate this (GCC PR
13061 40573). */
13062 child_origin_die = child_die;
13063 child_origin_cu = cu;
13064 while (1)
13065 {
13066 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13067 child_origin_cu);
13068 if (attr == NULL)
13069 break;
13070 child_origin_die = follow_die_ref (child_origin_die, attr,
13071 &child_origin_cu);
13072 }
13073
13074 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13075 counterpart may exist. */
13076 if (child_origin_die != child_die)
13077 {
13078 if (child_die->tag != child_origin_die->tag
13079 && !(child_die->tag == DW_TAG_inlined_subroutine
13080 && child_origin_die->tag == DW_TAG_subprogram))
13081 complaint (_("Child DIE %s and its abstract origin %s have "
13082 "different tags"),
13083 sect_offset_str (child_die->sect_off),
13084 sect_offset_str (child_origin_die->sect_off));
13085 if (child_origin_die->parent != origin_die)
13086 complaint (_("Child DIE %s and its abstract origin %s have "
13087 "different parents"),
13088 sect_offset_str (child_die->sect_off),
13089 sect_offset_str (child_origin_die->sect_off));
13090 else
13091 offsets.push_back (child_origin_die->sect_off);
13092 }
13093 }
13094 std::sort (offsets.begin (), offsets.end ());
13095 sect_offset *offsets_end = offsets.data () + offsets.size ();
13096 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13097 if (offsetp[-1] == *offsetp)
13098 complaint (_("Multiple children of DIE %s refer "
13099 "to DIE %s as their abstract origin"),
13100 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13101
13102 offsetp = offsets.data ();
13103 origin_child_die = origin_die->child;
13104 while (origin_child_die && origin_child_die->tag)
13105 {
13106 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13107 while (offsetp < offsets_end
13108 && *offsetp < origin_child_die->sect_off)
13109 offsetp++;
13110 if (offsetp >= offsets_end
13111 || *offsetp > origin_child_die->sect_off)
13112 {
13113 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13114 Check whether we're already processing ORIGIN_CHILD_DIE.
13115 This can happen with mutually referenced abstract_origins.
13116 PR 16581. */
13117 if (!origin_child_die->in_process)
13118 process_die (origin_child_die, origin_cu);
13119 }
13120 origin_child_die = origin_child_die->sibling;
13121 }
13122 origin_cu->list_in_scope = origin_previous_list_in_scope;
13123
13124 if (cu != origin_cu)
13125 compute_delayed_physnames (origin_cu);
13126 }
13127
13128 static void
13129 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13130 {
13131 struct objfile *objfile = cu->per_objfile->objfile;
13132 struct gdbarch *gdbarch = objfile->arch ();
13133 struct context_stack *newobj;
13134 CORE_ADDR lowpc;
13135 CORE_ADDR highpc;
13136 struct die_info *child_die;
13137 struct attribute *attr, *call_line, *call_file;
13138 const char *name;
13139 CORE_ADDR baseaddr;
13140 struct block *block;
13141 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13142 std::vector<struct symbol *> template_args;
13143 struct template_symbol *templ_func = NULL;
13144
13145 if (inlined_func)
13146 {
13147 /* If we do not have call site information, we can't show the
13148 caller of this inlined function. That's too confusing, so
13149 only use the scope for local variables. */
13150 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13151 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13152 if (call_line == NULL || call_file == NULL)
13153 {
13154 read_lexical_block_scope (die, cu);
13155 return;
13156 }
13157 }
13158
13159 baseaddr = objfile->text_section_offset ();
13160
13161 name = dwarf2_name (die, cu);
13162
13163 /* Ignore functions with missing or empty names. These are actually
13164 illegal according to the DWARF standard. */
13165 if (name == NULL)
13166 {
13167 complaint (_("missing name for subprogram DIE at %s"),
13168 sect_offset_str (die->sect_off));
13169 return;
13170 }
13171
13172 /* Ignore functions with missing or invalid low and high pc attributes. */
13173 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13174 <= PC_BOUNDS_INVALID)
13175 {
13176 attr = dwarf2_attr (die, DW_AT_external, cu);
13177 if (!attr || !DW_UNSND (attr))
13178 complaint (_("cannot get low and high bounds "
13179 "for subprogram DIE at %s"),
13180 sect_offset_str (die->sect_off));
13181 return;
13182 }
13183
13184 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13185 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13186
13187 /* If we have any template arguments, then we must allocate a
13188 different sort of symbol. */
13189 for (child_die = die->child; child_die; child_die = child_die->sibling)
13190 {
13191 if (child_die->tag == DW_TAG_template_type_param
13192 || child_die->tag == DW_TAG_template_value_param)
13193 {
13194 templ_func = new (&objfile->objfile_obstack) template_symbol;
13195 templ_func->subclass = SYMBOL_TEMPLATE;
13196 break;
13197 }
13198 }
13199
13200 newobj = cu->get_builder ()->push_context (0, lowpc);
13201 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13202 (struct symbol *) templ_func);
13203
13204 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13205 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13206 cu->language);
13207
13208 /* If there is a location expression for DW_AT_frame_base, record
13209 it. */
13210 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13211 if (attr != nullptr)
13212 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13213
13214 /* If there is a location for the static link, record it. */
13215 newobj->static_link = NULL;
13216 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13217 if (attr != nullptr)
13218 {
13219 newobj->static_link
13220 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13221 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13222 cu->addr_type ());
13223 }
13224
13225 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13226
13227 if (die->child != NULL)
13228 {
13229 child_die = die->child;
13230 while (child_die && child_die->tag)
13231 {
13232 if (child_die->tag == DW_TAG_template_type_param
13233 || child_die->tag == DW_TAG_template_value_param)
13234 {
13235 struct symbol *arg = new_symbol (child_die, NULL, cu);
13236
13237 if (arg != NULL)
13238 template_args.push_back (arg);
13239 }
13240 else
13241 process_die (child_die, cu);
13242 child_die = child_die->sibling;
13243 }
13244 }
13245
13246 inherit_abstract_dies (die, cu);
13247
13248 /* If we have a DW_AT_specification, we might need to import using
13249 directives from the context of the specification DIE. See the
13250 comment in determine_prefix. */
13251 if (cu->language == language_cplus
13252 && dwarf2_attr (die, DW_AT_specification, cu))
13253 {
13254 struct dwarf2_cu *spec_cu = cu;
13255 struct die_info *spec_die = die_specification (die, &spec_cu);
13256
13257 while (spec_die)
13258 {
13259 child_die = spec_die->child;
13260 while (child_die && child_die->tag)
13261 {
13262 if (child_die->tag == DW_TAG_imported_module)
13263 process_die (child_die, spec_cu);
13264 child_die = child_die->sibling;
13265 }
13266
13267 /* In some cases, GCC generates specification DIEs that
13268 themselves contain DW_AT_specification attributes. */
13269 spec_die = die_specification (spec_die, &spec_cu);
13270 }
13271 }
13272
13273 struct context_stack cstk = cu->get_builder ()->pop_context ();
13274 /* Make a block for the local symbols within. */
13275 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13276 cstk.static_link, lowpc, highpc);
13277
13278 /* For C++, set the block's scope. */
13279 if ((cu->language == language_cplus
13280 || cu->language == language_fortran
13281 || cu->language == language_d
13282 || cu->language == language_rust)
13283 && cu->processing_has_namespace_info)
13284 block_set_scope (block, determine_prefix (die, cu),
13285 &objfile->objfile_obstack);
13286
13287 /* If we have address ranges, record them. */
13288 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13289
13290 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13291
13292 /* Attach template arguments to function. */
13293 if (!template_args.empty ())
13294 {
13295 gdb_assert (templ_func != NULL);
13296
13297 templ_func->n_template_arguments = template_args.size ();
13298 templ_func->template_arguments
13299 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13300 templ_func->n_template_arguments);
13301 memcpy (templ_func->template_arguments,
13302 template_args.data (),
13303 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13304
13305 /* Make sure that the symtab is set on the new symbols. Even
13306 though they don't appear in this symtab directly, other parts
13307 of gdb assume that symbols do, and this is reasonably
13308 true. */
13309 for (symbol *sym : template_args)
13310 symbol_set_symtab (sym, symbol_symtab (templ_func));
13311 }
13312
13313 /* In C++, we can have functions nested inside functions (e.g., when
13314 a function declares a class that has methods). This means that
13315 when we finish processing a function scope, we may need to go
13316 back to building a containing block's symbol lists. */
13317 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13318 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13319
13320 /* If we've finished processing a top-level function, subsequent
13321 symbols go in the file symbol list. */
13322 if (cu->get_builder ()->outermost_context_p ())
13323 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13324 }
13325
13326 /* Process all the DIES contained within a lexical block scope. Start
13327 a new scope, process the dies, and then close the scope. */
13328
13329 static void
13330 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13331 {
13332 struct objfile *objfile = cu->per_objfile->objfile;
13333 struct gdbarch *gdbarch = objfile->arch ();
13334 CORE_ADDR lowpc, highpc;
13335 struct die_info *child_die;
13336 CORE_ADDR baseaddr;
13337
13338 baseaddr = objfile->text_section_offset ();
13339
13340 /* Ignore blocks with missing or invalid low and high pc attributes. */
13341 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13342 as multiple lexical blocks? Handling children in a sane way would
13343 be nasty. Might be easier to properly extend generic blocks to
13344 describe ranges. */
13345 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13346 {
13347 case PC_BOUNDS_NOT_PRESENT:
13348 /* DW_TAG_lexical_block has no attributes, process its children as if
13349 there was no wrapping by that DW_TAG_lexical_block.
13350 GCC does no longer produces such DWARF since GCC r224161. */
13351 for (child_die = die->child;
13352 child_die != NULL && child_die->tag;
13353 child_die = child_die->sibling)
13354 {
13355 /* We might already be processing this DIE. This can happen
13356 in an unusual circumstance -- where a subroutine A
13357 appears lexically in another subroutine B, but A actually
13358 inlines B. The recursion is broken here, rather than in
13359 inherit_abstract_dies, because it seems better to simply
13360 drop concrete children here. */
13361 if (!child_die->in_process)
13362 process_die (child_die, cu);
13363 }
13364 return;
13365 case PC_BOUNDS_INVALID:
13366 return;
13367 }
13368 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13369 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13370
13371 cu->get_builder ()->push_context (0, lowpc);
13372 if (die->child != NULL)
13373 {
13374 child_die = die->child;
13375 while (child_die && child_die->tag)
13376 {
13377 process_die (child_die, cu);
13378 child_die = child_die->sibling;
13379 }
13380 }
13381 inherit_abstract_dies (die, cu);
13382 struct context_stack cstk = cu->get_builder ()->pop_context ();
13383
13384 if (*cu->get_builder ()->get_local_symbols () != NULL
13385 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13386 {
13387 struct block *block
13388 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13389 cstk.start_addr, highpc);
13390
13391 /* Note that recording ranges after traversing children, as we
13392 do here, means that recording a parent's ranges entails
13393 walking across all its children's ranges as they appear in
13394 the address map, which is quadratic behavior.
13395
13396 It would be nicer to record the parent's ranges before
13397 traversing its children, simply overriding whatever you find
13398 there. But since we don't even decide whether to create a
13399 block until after we've traversed its children, that's hard
13400 to do. */
13401 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13402 }
13403 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13404 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13405 }
13406
13407 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13408
13409 static void
13410 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13411 {
13412 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13413 struct objfile *objfile = per_objfile->objfile;
13414 struct gdbarch *gdbarch = objfile->arch ();
13415 CORE_ADDR pc, baseaddr;
13416 struct attribute *attr;
13417 struct call_site *call_site, call_site_local;
13418 void **slot;
13419 int nparams;
13420 struct die_info *child_die;
13421
13422 baseaddr = objfile->text_section_offset ();
13423
13424 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13425 if (attr == NULL)
13426 {
13427 /* This was a pre-DWARF-5 GNU extension alias
13428 for DW_AT_call_return_pc. */
13429 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13430 }
13431 if (!attr)
13432 {
13433 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13434 "DIE %s [in module %s]"),
13435 sect_offset_str (die->sect_off), objfile_name (objfile));
13436 return;
13437 }
13438 pc = attr->value_as_address () + baseaddr;
13439 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13440
13441 if (cu->call_site_htab == NULL)
13442 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13443 NULL, &objfile->objfile_obstack,
13444 hashtab_obstack_allocate, NULL);
13445 call_site_local.pc = pc;
13446 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13447 if (*slot != NULL)
13448 {
13449 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13450 "DIE %s [in module %s]"),
13451 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13452 objfile_name (objfile));
13453 return;
13454 }
13455
13456 /* Count parameters at the caller. */
13457
13458 nparams = 0;
13459 for (child_die = die->child; child_die && child_die->tag;
13460 child_die = child_die->sibling)
13461 {
13462 if (child_die->tag != DW_TAG_call_site_parameter
13463 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13464 {
13465 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13466 "DW_TAG_call_site child DIE %s [in module %s]"),
13467 child_die->tag, sect_offset_str (child_die->sect_off),
13468 objfile_name (objfile));
13469 continue;
13470 }
13471
13472 nparams++;
13473 }
13474
13475 call_site
13476 = ((struct call_site *)
13477 obstack_alloc (&objfile->objfile_obstack,
13478 sizeof (*call_site)
13479 + (sizeof (*call_site->parameter) * (nparams - 1))));
13480 *slot = call_site;
13481 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13482 call_site->pc = pc;
13483
13484 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13485 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13486 {
13487 struct die_info *func_die;
13488
13489 /* Skip also over DW_TAG_inlined_subroutine. */
13490 for (func_die = die->parent;
13491 func_die && func_die->tag != DW_TAG_subprogram
13492 && func_die->tag != DW_TAG_subroutine_type;
13493 func_die = func_die->parent);
13494
13495 /* DW_AT_call_all_calls is a superset
13496 of DW_AT_call_all_tail_calls. */
13497 if (func_die
13498 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13499 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13500 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13501 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13502 {
13503 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13504 not complete. But keep CALL_SITE for look ups via call_site_htab,
13505 both the initial caller containing the real return address PC and
13506 the final callee containing the current PC of a chain of tail
13507 calls do not need to have the tail call list complete. But any
13508 function candidate for a virtual tail call frame searched via
13509 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13510 determined unambiguously. */
13511 }
13512 else
13513 {
13514 struct type *func_type = NULL;
13515
13516 if (func_die)
13517 func_type = get_die_type (func_die, cu);
13518 if (func_type != NULL)
13519 {
13520 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13521
13522 /* Enlist this call site to the function. */
13523 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13524 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13525 }
13526 else
13527 complaint (_("Cannot find function owning DW_TAG_call_site "
13528 "DIE %s [in module %s]"),
13529 sect_offset_str (die->sect_off), objfile_name (objfile));
13530 }
13531 }
13532
13533 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13534 if (attr == NULL)
13535 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13536 if (attr == NULL)
13537 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13538 if (attr == NULL)
13539 {
13540 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13541 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13542 }
13543 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13544 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13545 /* Keep NULL DWARF_BLOCK. */;
13546 else if (attr->form_is_block ())
13547 {
13548 struct dwarf2_locexpr_baton *dlbaton;
13549
13550 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13551 dlbaton->data = DW_BLOCK (attr)->data;
13552 dlbaton->size = DW_BLOCK (attr)->size;
13553 dlbaton->per_objfile = per_objfile;
13554 dlbaton->per_cu = cu->per_cu;
13555
13556 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13557 }
13558 else if (attr->form_is_ref ())
13559 {
13560 struct dwarf2_cu *target_cu = cu;
13561 struct die_info *target_die;
13562
13563 target_die = follow_die_ref (die, attr, &target_cu);
13564 gdb_assert (target_cu->per_objfile->objfile == objfile);
13565 if (die_is_declaration (target_die, target_cu))
13566 {
13567 const char *target_physname;
13568
13569 /* Prefer the mangled name; otherwise compute the demangled one. */
13570 target_physname = dw2_linkage_name (target_die, target_cu);
13571 if (target_physname == NULL)
13572 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13573 if (target_physname == NULL)
13574 complaint (_("DW_AT_call_target target DIE has invalid "
13575 "physname, for referencing DIE %s [in module %s]"),
13576 sect_offset_str (die->sect_off), objfile_name (objfile));
13577 else
13578 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13579 }
13580 else
13581 {
13582 CORE_ADDR lowpc;
13583
13584 /* DW_AT_entry_pc should be preferred. */
13585 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13586 <= PC_BOUNDS_INVALID)
13587 complaint (_("DW_AT_call_target target DIE has invalid "
13588 "low pc, for referencing DIE %s [in module %s]"),
13589 sect_offset_str (die->sect_off), objfile_name (objfile));
13590 else
13591 {
13592 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13593 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13594 }
13595 }
13596 }
13597 else
13598 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13599 "block nor reference, for DIE %s [in module %s]"),
13600 sect_offset_str (die->sect_off), objfile_name (objfile));
13601
13602 call_site->per_cu = cu->per_cu;
13603 call_site->per_objfile = per_objfile;
13604
13605 for (child_die = die->child;
13606 child_die && child_die->tag;
13607 child_die = child_die->sibling)
13608 {
13609 struct call_site_parameter *parameter;
13610 struct attribute *loc, *origin;
13611
13612 if (child_die->tag != DW_TAG_call_site_parameter
13613 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13614 {
13615 /* Already printed the complaint above. */
13616 continue;
13617 }
13618
13619 gdb_assert (call_site->parameter_count < nparams);
13620 parameter = &call_site->parameter[call_site->parameter_count];
13621
13622 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13623 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13624 register is contained in DW_AT_call_value. */
13625
13626 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13627 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13628 if (origin == NULL)
13629 {
13630 /* This was a pre-DWARF-5 GNU extension alias
13631 for DW_AT_call_parameter. */
13632 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13633 }
13634 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13635 {
13636 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13637
13638 sect_offset sect_off = origin->get_ref_die_offset ();
13639 if (!cu->header.offset_in_cu_p (sect_off))
13640 {
13641 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13642 binding can be done only inside one CU. Such referenced DIE
13643 therefore cannot be even moved to DW_TAG_partial_unit. */
13644 complaint (_("DW_AT_call_parameter offset is not in CU for "
13645 "DW_TAG_call_site child DIE %s [in module %s]"),
13646 sect_offset_str (child_die->sect_off),
13647 objfile_name (objfile));
13648 continue;
13649 }
13650 parameter->u.param_cu_off
13651 = (cu_offset) (sect_off - cu->header.sect_off);
13652 }
13653 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13654 {
13655 complaint (_("No DW_FORM_block* DW_AT_location for "
13656 "DW_TAG_call_site child DIE %s [in module %s]"),
13657 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13658 continue;
13659 }
13660 else
13661 {
13662 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13663 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13664 if (parameter->u.dwarf_reg != -1)
13665 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13666 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13667 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13668 &parameter->u.fb_offset))
13669 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13670 else
13671 {
13672 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13673 "for DW_FORM_block* DW_AT_location is supported for "
13674 "DW_TAG_call_site child DIE %s "
13675 "[in module %s]"),
13676 sect_offset_str (child_die->sect_off),
13677 objfile_name (objfile));
13678 continue;
13679 }
13680 }
13681
13682 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13683 if (attr == NULL)
13684 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13685 if (attr == NULL || !attr->form_is_block ())
13686 {
13687 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13688 "DW_TAG_call_site child DIE %s [in module %s]"),
13689 sect_offset_str (child_die->sect_off),
13690 objfile_name (objfile));
13691 continue;
13692 }
13693 parameter->value = DW_BLOCK (attr)->data;
13694 parameter->value_size = DW_BLOCK (attr)->size;
13695
13696 /* Parameters are not pre-cleared by memset above. */
13697 parameter->data_value = NULL;
13698 parameter->data_value_size = 0;
13699 call_site->parameter_count++;
13700
13701 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13702 if (attr == NULL)
13703 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13704 if (attr != nullptr)
13705 {
13706 if (!attr->form_is_block ())
13707 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13708 "DW_TAG_call_site child DIE %s [in module %s]"),
13709 sect_offset_str (child_die->sect_off),
13710 objfile_name (objfile));
13711 else
13712 {
13713 parameter->data_value = DW_BLOCK (attr)->data;
13714 parameter->data_value_size = DW_BLOCK (attr)->size;
13715 }
13716 }
13717 }
13718 }
13719
13720 /* Helper function for read_variable. If DIE represents a virtual
13721 table, then return the type of the concrete object that is
13722 associated with the virtual table. Otherwise, return NULL. */
13723
13724 static struct type *
13725 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13726 {
13727 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13728 if (attr == NULL)
13729 return NULL;
13730
13731 /* Find the type DIE. */
13732 struct die_info *type_die = NULL;
13733 struct dwarf2_cu *type_cu = cu;
13734
13735 if (attr->form_is_ref ())
13736 type_die = follow_die_ref (die, attr, &type_cu);
13737 if (type_die == NULL)
13738 return NULL;
13739
13740 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13741 return NULL;
13742 return die_containing_type (type_die, type_cu);
13743 }
13744
13745 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13746
13747 static void
13748 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13749 {
13750 struct rust_vtable_symbol *storage = NULL;
13751
13752 if (cu->language == language_rust)
13753 {
13754 struct type *containing_type = rust_containing_type (die, cu);
13755
13756 if (containing_type != NULL)
13757 {
13758 struct objfile *objfile = cu->per_objfile->objfile;
13759
13760 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13761 storage->concrete_type = containing_type;
13762 storage->subclass = SYMBOL_RUST_VTABLE;
13763 }
13764 }
13765
13766 struct symbol *res = new_symbol (die, NULL, cu, storage);
13767 struct attribute *abstract_origin
13768 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13769 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13770 if (res == NULL && loc && abstract_origin)
13771 {
13772 /* We have a variable without a name, but with a location and an abstract
13773 origin. This may be a concrete instance of an abstract variable
13774 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13775 later. */
13776 struct dwarf2_cu *origin_cu = cu;
13777 struct die_info *origin_die
13778 = follow_die_ref (die, abstract_origin, &origin_cu);
13779 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13780 per_objfile->per_bfd->abstract_to_concrete
13781 [origin_die->sect_off].push_back (die->sect_off);
13782 }
13783 }
13784
13785 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13786 reading .debug_rnglists.
13787 Callback's type should be:
13788 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13789 Return true if the attributes are present and valid, otherwise,
13790 return false. */
13791
13792 template <typename Callback>
13793 static bool
13794 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13795 Callback &&callback)
13796 {
13797 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13798 struct objfile *objfile = per_objfile->objfile;
13799 bfd *obfd = objfile->obfd;
13800 /* Base address selection entry. */
13801 gdb::optional<CORE_ADDR> base;
13802 const gdb_byte *buffer;
13803 CORE_ADDR baseaddr;
13804 bool overflow = false;
13805
13806 base = cu->base_address;
13807
13808 per_objfile->per_bfd->rnglists.read (objfile);
13809 if (offset >= per_objfile->per_bfd->rnglists.size)
13810 {
13811 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13812 offset);
13813 return false;
13814 }
13815 buffer = per_objfile->per_bfd->rnglists.buffer + offset;
13816
13817 baseaddr = objfile->text_section_offset ();
13818
13819 while (1)
13820 {
13821 /* Initialize it due to a false compiler warning. */
13822 CORE_ADDR range_beginning = 0, range_end = 0;
13823 const gdb_byte *buf_end = (per_objfile->per_bfd->rnglists.buffer
13824 + per_objfile->per_bfd->rnglists.size);
13825 unsigned int bytes_read;
13826
13827 if (buffer == buf_end)
13828 {
13829 overflow = true;
13830 break;
13831 }
13832 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13833 switch (rlet)
13834 {
13835 case DW_RLE_end_of_list:
13836 break;
13837 case DW_RLE_base_address:
13838 if (buffer + cu->header.addr_size > buf_end)
13839 {
13840 overflow = true;
13841 break;
13842 }
13843 base = cu->header.read_address (obfd, buffer, &bytes_read);
13844 buffer += bytes_read;
13845 break;
13846 case DW_RLE_start_length:
13847 if (buffer + cu->header.addr_size > buf_end)
13848 {
13849 overflow = true;
13850 break;
13851 }
13852 range_beginning = cu->header.read_address (obfd, buffer,
13853 &bytes_read);
13854 buffer += bytes_read;
13855 range_end = (range_beginning
13856 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13857 buffer += bytes_read;
13858 if (buffer > buf_end)
13859 {
13860 overflow = true;
13861 break;
13862 }
13863 break;
13864 case DW_RLE_offset_pair:
13865 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13866 buffer += bytes_read;
13867 if (buffer > buf_end)
13868 {
13869 overflow = true;
13870 break;
13871 }
13872 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13873 buffer += bytes_read;
13874 if (buffer > buf_end)
13875 {
13876 overflow = true;
13877 break;
13878 }
13879 break;
13880 case DW_RLE_start_end:
13881 if (buffer + 2 * cu->header.addr_size > buf_end)
13882 {
13883 overflow = true;
13884 break;
13885 }
13886 range_beginning = cu->header.read_address (obfd, buffer,
13887 &bytes_read);
13888 buffer += bytes_read;
13889 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13890 buffer += bytes_read;
13891 break;
13892 default:
13893 complaint (_("Invalid .debug_rnglists data (no base address)"));
13894 return false;
13895 }
13896 if (rlet == DW_RLE_end_of_list || overflow)
13897 break;
13898 if (rlet == DW_RLE_base_address)
13899 continue;
13900
13901 if (!base.has_value ())
13902 {
13903 /* We have no valid base address for the ranges
13904 data. */
13905 complaint (_("Invalid .debug_rnglists data (no base address)"));
13906 return false;
13907 }
13908
13909 if (range_beginning > range_end)
13910 {
13911 /* Inverted range entries are invalid. */
13912 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13913 return false;
13914 }
13915
13916 /* Empty range entries have no effect. */
13917 if (range_beginning == range_end)
13918 continue;
13919
13920 range_beginning += *base;
13921 range_end += *base;
13922
13923 /* A not-uncommon case of bad debug info.
13924 Don't pollute the addrmap with bad data. */
13925 if (range_beginning + baseaddr == 0
13926 && !per_objfile->per_bfd->has_section_at_zero)
13927 {
13928 complaint (_(".debug_rnglists entry has start address of zero"
13929 " [in module %s]"), objfile_name (objfile));
13930 continue;
13931 }
13932
13933 callback (range_beginning, range_end);
13934 }
13935
13936 if (overflow)
13937 {
13938 complaint (_("Offset %d is not terminated "
13939 "for DW_AT_ranges attribute"),
13940 offset);
13941 return false;
13942 }
13943
13944 return true;
13945 }
13946
13947 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13948 Callback's type should be:
13949 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13950 Return 1 if the attributes are present and valid, otherwise, return 0. */
13951
13952 template <typename Callback>
13953 static int
13954 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13955 Callback &&callback)
13956 {
13957 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13958 struct objfile *objfile = per_objfile->objfile;
13959 struct comp_unit_head *cu_header = &cu->header;
13960 bfd *obfd = objfile->obfd;
13961 unsigned int addr_size = cu_header->addr_size;
13962 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13963 /* Base address selection entry. */
13964 gdb::optional<CORE_ADDR> base;
13965 unsigned int dummy;
13966 const gdb_byte *buffer;
13967 CORE_ADDR baseaddr;
13968
13969 if (cu_header->version >= 5)
13970 return dwarf2_rnglists_process (offset, cu, callback);
13971
13972 base = cu->base_address;
13973
13974 per_objfile->per_bfd->ranges.read (objfile);
13975 if (offset >= per_objfile->per_bfd->ranges.size)
13976 {
13977 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13978 offset);
13979 return 0;
13980 }
13981 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13982
13983 baseaddr = objfile->text_section_offset ();
13984
13985 while (1)
13986 {
13987 CORE_ADDR range_beginning, range_end;
13988
13989 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13990 buffer += addr_size;
13991 range_end = cu->header.read_address (obfd, buffer, &dummy);
13992 buffer += addr_size;
13993 offset += 2 * addr_size;
13994
13995 /* An end of list marker is a pair of zero addresses. */
13996 if (range_beginning == 0 && range_end == 0)
13997 /* Found the end of list entry. */
13998 break;
13999
14000 /* Each base address selection entry is a pair of 2 values.
14001 The first is the largest possible address, the second is
14002 the base address. Check for a base address here. */
14003 if ((range_beginning & mask) == mask)
14004 {
14005 /* If we found the largest possible address, then we already
14006 have the base address in range_end. */
14007 base = range_end;
14008 continue;
14009 }
14010
14011 if (!base.has_value ())
14012 {
14013 /* We have no valid base address for the ranges
14014 data. */
14015 complaint (_("Invalid .debug_ranges data (no base address)"));
14016 return 0;
14017 }
14018
14019 if (range_beginning > range_end)
14020 {
14021 /* Inverted range entries are invalid. */
14022 complaint (_("Invalid .debug_ranges data (inverted range)"));
14023 return 0;
14024 }
14025
14026 /* Empty range entries have no effect. */
14027 if (range_beginning == range_end)
14028 continue;
14029
14030 range_beginning += *base;
14031 range_end += *base;
14032
14033 /* A not-uncommon case of bad debug info.
14034 Don't pollute the addrmap with bad data. */
14035 if (range_beginning + baseaddr == 0
14036 && !per_objfile->per_bfd->has_section_at_zero)
14037 {
14038 complaint (_(".debug_ranges entry has start address of zero"
14039 " [in module %s]"), objfile_name (objfile));
14040 continue;
14041 }
14042
14043 callback (range_beginning, range_end);
14044 }
14045
14046 return 1;
14047 }
14048
14049 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14050 Return 1 if the attributes are present and valid, otherwise, return 0.
14051 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14052
14053 static int
14054 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14055 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14056 dwarf2_psymtab *ranges_pst)
14057 {
14058 struct objfile *objfile = cu->per_objfile->objfile;
14059 struct gdbarch *gdbarch = objfile->arch ();
14060 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14061 int low_set = 0;
14062 CORE_ADDR low = 0;
14063 CORE_ADDR high = 0;
14064 int retval;
14065
14066 retval = dwarf2_ranges_process (offset, cu,
14067 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14068 {
14069 if (ranges_pst != NULL)
14070 {
14071 CORE_ADDR lowpc;
14072 CORE_ADDR highpc;
14073
14074 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14075 range_beginning + baseaddr)
14076 - baseaddr);
14077 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14078 range_end + baseaddr)
14079 - baseaddr);
14080 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14081 lowpc, highpc - 1, ranges_pst);
14082 }
14083
14084 /* FIXME: This is recording everything as a low-high
14085 segment of consecutive addresses. We should have a
14086 data structure for discontiguous block ranges
14087 instead. */
14088 if (! low_set)
14089 {
14090 low = range_beginning;
14091 high = range_end;
14092 low_set = 1;
14093 }
14094 else
14095 {
14096 if (range_beginning < low)
14097 low = range_beginning;
14098 if (range_end > high)
14099 high = range_end;
14100 }
14101 });
14102 if (!retval)
14103 return 0;
14104
14105 if (! low_set)
14106 /* If the first entry is an end-of-list marker, the range
14107 describes an empty scope, i.e. no instructions. */
14108 return 0;
14109
14110 if (low_return)
14111 *low_return = low;
14112 if (high_return)
14113 *high_return = high;
14114 return 1;
14115 }
14116
14117 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14118 definition for the return value. *LOWPC and *HIGHPC are set iff
14119 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14120
14121 static enum pc_bounds_kind
14122 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14123 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14124 dwarf2_psymtab *pst)
14125 {
14126 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14127 struct attribute *attr;
14128 struct attribute *attr_high;
14129 CORE_ADDR low = 0;
14130 CORE_ADDR high = 0;
14131 enum pc_bounds_kind ret;
14132
14133 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14134 if (attr_high)
14135 {
14136 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14137 if (attr != nullptr)
14138 {
14139 low = attr->value_as_address ();
14140 high = attr_high->value_as_address ();
14141 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14142 high += low;
14143 }
14144 else
14145 /* Found high w/o low attribute. */
14146 return PC_BOUNDS_INVALID;
14147
14148 /* Found consecutive range of addresses. */
14149 ret = PC_BOUNDS_HIGH_LOW;
14150 }
14151 else
14152 {
14153 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14154 if (attr != NULL)
14155 {
14156 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14157 We take advantage of the fact that DW_AT_ranges does not appear
14158 in DW_TAG_compile_unit of DWO files. */
14159 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14160 unsigned int ranges_offset = (DW_UNSND (attr)
14161 + (need_ranges_base
14162 ? cu->ranges_base
14163 : 0));
14164
14165 /* Value of the DW_AT_ranges attribute is the offset in the
14166 .debug_ranges section. */
14167 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14168 return PC_BOUNDS_INVALID;
14169 /* Found discontinuous range of addresses. */
14170 ret = PC_BOUNDS_RANGES;
14171 }
14172 else
14173 return PC_BOUNDS_NOT_PRESENT;
14174 }
14175
14176 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14177 if (high <= low)
14178 return PC_BOUNDS_INVALID;
14179
14180 /* When using the GNU linker, .gnu.linkonce. sections are used to
14181 eliminate duplicate copies of functions and vtables and such.
14182 The linker will arbitrarily choose one and discard the others.
14183 The AT_*_pc values for such functions refer to local labels in
14184 these sections. If the section from that file was discarded, the
14185 labels are not in the output, so the relocs get a value of 0.
14186 If this is a discarded function, mark the pc bounds as invalid,
14187 so that GDB will ignore it. */
14188 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14189 return PC_BOUNDS_INVALID;
14190
14191 *lowpc = low;
14192 if (highpc)
14193 *highpc = high;
14194 return ret;
14195 }
14196
14197 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14198 its low and high PC addresses. Do nothing if these addresses could not
14199 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14200 and HIGHPC to the high address if greater than HIGHPC. */
14201
14202 static void
14203 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14204 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14205 struct dwarf2_cu *cu)
14206 {
14207 CORE_ADDR low, high;
14208 struct die_info *child = die->child;
14209
14210 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14211 {
14212 *lowpc = std::min (*lowpc, low);
14213 *highpc = std::max (*highpc, high);
14214 }
14215
14216 /* If the language does not allow nested subprograms (either inside
14217 subprograms or lexical blocks), we're done. */
14218 if (cu->language != language_ada)
14219 return;
14220
14221 /* Check all the children of the given DIE. If it contains nested
14222 subprograms, then check their pc bounds. Likewise, we need to
14223 check lexical blocks as well, as they may also contain subprogram
14224 definitions. */
14225 while (child && child->tag)
14226 {
14227 if (child->tag == DW_TAG_subprogram
14228 || child->tag == DW_TAG_lexical_block)
14229 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14230 child = child->sibling;
14231 }
14232 }
14233
14234 /* Get the low and high pc's represented by the scope DIE, and store
14235 them in *LOWPC and *HIGHPC. If the correct values can't be
14236 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14237
14238 static void
14239 get_scope_pc_bounds (struct die_info *die,
14240 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14241 struct dwarf2_cu *cu)
14242 {
14243 CORE_ADDR best_low = (CORE_ADDR) -1;
14244 CORE_ADDR best_high = (CORE_ADDR) 0;
14245 CORE_ADDR current_low, current_high;
14246
14247 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14248 >= PC_BOUNDS_RANGES)
14249 {
14250 best_low = current_low;
14251 best_high = current_high;
14252 }
14253 else
14254 {
14255 struct die_info *child = die->child;
14256
14257 while (child && child->tag)
14258 {
14259 switch (child->tag) {
14260 case DW_TAG_subprogram:
14261 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14262 break;
14263 case DW_TAG_namespace:
14264 case DW_TAG_module:
14265 /* FIXME: carlton/2004-01-16: Should we do this for
14266 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14267 that current GCC's always emit the DIEs corresponding
14268 to definitions of methods of classes as children of a
14269 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14270 the DIEs giving the declarations, which could be
14271 anywhere). But I don't see any reason why the
14272 standards says that they have to be there. */
14273 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14274
14275 if (current_low != ((CORE_ADDR) -1))
14276 {
14277 best_low = std::min (best_low, current_low);
14278 best_high = std::max (best_high, current_high);
14279 }
14280 break;
14281 default:
14282 /* Ignore. */
14283 break;
14284 }
14285
14286 child = child->sibling;
14287 }
14288 }
14289
14290 *lowpc = best_low;
14291 *highpc = best_high;
14292 }
14293
14294 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14295 in DIE. */
14296
14297 static void
14298 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14299 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14300 {
14301 struct objfile *objfile = cu->per_objfile->objfile;
14302 struct gdbarch *gdbarch = objfile->arch ();
14303 struct attribute *attr;
14304 struct attribute *attr_high;
14305
14306 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14307 if (attr_high)
14308 {
14309 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14310 if (attr != nullptr)
14311 {
14312 CORE_ADDR low = attr->value_as_address ();
14313 CORE_ADDR high = attr_high->value_as_address ();
14314
14315 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14316 high += low;
14317
14318 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14319 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14320 cu->get_builder ()->record_block_range (block, low, high - 1);
14321 }
14322 }
14323
14324 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14325 if (attr != nullptr)
14326 {
14327 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14328 We take advantage of the fact that DW_AT_ranges does not appear
14329 in DW_TAG_compile_unit of DWO files. */
14330 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14331
14332 /* The value of the DW_AT_ranges attribute is the offset of the
14333 address range list in the .debug_ranges section. */
14334 unsigned long offset = (DW_UNSND (attr)
14335 + (need_ranges_base ? cu->ranges_base : 0));
14336
14337 std::vector<blockrange> blockvec;
14338 dwarf2_ranges_process (offset, cu,
14339 [&] (CORE_ADDR start, CORE_ADDR end)
14340 {
14341 start += baseaddr;
14342 end += baseaddr;
14343 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14344 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14345 cu->get_builder ()->record_block_range (block, start, end - 1);
14346 blockvec.emplace_back (start, end);
14347 });
14348
14349 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14350 }
14351 }
14352
14353 /* Check whether the producer field indicates either of GCC < 4.6, or the
14354 Intel C/C++ compiler, and cache the result in CU. */
14355
14356 static void
14357 check_producer (struct dwarf2_cu *cu)
14358 {
14359 int major, minor;
14360
14361 if (cu->producer == NULL)
14362 {
14363 /* For unknown compilers expect their behavior is DWARF version
14364 compliant.
14365
14366 GCC started to support .debug_types sections by -gdwarf-4 since
14367 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14368 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14369 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14370 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14371 }
14372 else if (producer_is_gcc (cu->producer, &major, &minor))
14373 {
14374 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14375 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14376 }
14377 else if (producer_is_icc (cu->producer, &major, &minor))
14378 {
14379 cu->producer_is_icc = true;
14380 cu->producer_is_icc_lt_14 = major < 14;
14381 }
14382 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14383 cu->producer_is_codewarrior = true;
14384 else
14385 {
14386 /* For other non-GCC compilers, expect their behavior is DWARF version
14387 compliant. */
14388 }
14389
14390 cu->checked_producer = true;
14391 }
14392
14393 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14394 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14395 during 4.6.0 experimental. */
14396
14397 static bool
14398 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14399 {
14400 if (!cu->checked_producer)
14401 check_producer (cu);
14402
14403 return cu->producer_is_gxx_lt_4_6;
14404 }
14405
14406
14407 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14408 with incorrect is_stmt attributes. */
14409
14410 static bool
14411 producer_is_codewarrior (struct dwarf2_cu *cu)
14412 {
14413 if (!cu->checked_producer)
14414 check_producer (cu);
14415
14416 return cu->producer_is_codewarrior;
14417 }
14418
14419 /* Return the default accessibility type if it is not overridden by
14420 DW_AT_accessibility. */
14421
14422 static enum dwarf_access_attribute
14423 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14424 {
14425 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14426 {
14427 /* The default DWARF 2 accessibility for members is public, the default
14428 accessibility for inheritance is private. */
14429
14430 if (die->tag != DW_TAG_inheritance)
14431 return DW_ACCESS_public;
14432 else
14433 return DW_ACCESS_private;
14434 }
14435 else
14436 {
14437 /* DWARF 3+ defines the default accessibility a different way. The same
14438 rules apply now for DW_TAG_inheritance as for the members and it only
14439 depends on the container kind. */
14440
14441 if (die->parent->tag == DW_TAG_class_type)
14442 return DW_ACCESS_private;
14443 else
14444 return DW_ACCESS_public;
14445 }
14446 }
14447
14448 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14449 offset. If the attribute was not found return 0, otherwise return
14450 1. If it was found but could not properly be handled, set *OFFSET
14451 to 0. */
14452
14453 static int
14454 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14455 LONGEST *offset)
14456 {
14457 struct attribute *attr;
14458
14459 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14460 if (attr != NULL)
14461 {
14462 *offset = 0;
14463
14464 /* Note that we do not check for a section offset first here.
14465 This is because DW_AT_data_member_location is new in DWARF 4,
14466 so if we see it, we can assume that a constant form is really
14467 a constant and not a section offset. */
14468 if (attr->form_is_constant ())
14469 *offset = attr->constant_value (0);
14470 else if (attr->form_is_section_offset ())
14471 dwarf2_complex_location_expr_complaint ();
14472 else if (attr->form_is_block ())
14473 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14474 else
14475 dwarf2_complex_location_expr_complaint ();
14476
14477 return 1;
14478 }
14479
14480 return 0;
14481 }
14482
14483 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14484
14485 static void
14486 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14487 struct field *field)
14488 {
14489 struct attribute *attr;
14490
14491 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14492 if (attr != NULL)
14493 {
14494 if (attr->form_is_constant ())
14495 {
14496 LONGEST offset = attr->constant_value (0);
14497 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14498 }
14499 else if (attr->form_is_section_offset ())
14500 dwarf2_complex_location_expr_complaint ();
14501 else if (attr->form_is_block ())
14502 {
14503 bool handled;
14504 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14505 if (handled)
14506 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14507 else
14508 {
14509 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14510 struct objfile *objfile = per_objfile->objfile;
14511 struct dwarf2_locexpr_baton *dlbaton
14512 = XOBNEW (&objfile->objfile_obstack,
14513 struct dwarf2_locexpr_baton);
14514 dlbaton->data = DW_BLOCK (attr)->data;
14515 dlbaton->size = DW_BLOCK (attr)->size;
14516 /* When using this baton, we want to compute the address
14517 of the field, not the value. This is why
14518 is_reference is set to false here. */
14519 dlbaton->is_reference = false;
14520 dlbaton->per_objfile = per_objfile;
14521 dlbaton->per_cu = cu->per_cu;
14522
14523 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14524 }
14525 }
14526 else
14527 dwarf2_complex_location_expr_complaint ();
14528 }
14529 }
14530
14531 /* Add an aggregate field to the field list. */
14532
14533 static void
14534 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14535 struct dwarf2_cu *cu)
14536 {
14537 struct objfile *objfile = cu->per_objfile->objfile;
14538 struct gdbarch *gdbarch = objfile->arch ();
14539 struct nextfield *new_field;
14540 struct attribute *attr;
14541 struct field *fp;
14542 const char *fieldname = "";
14543
14544 if (die->tag == DW_TAG_inheritance)
14545 {
14546 fip->baseclasses.emplace_back ();
14547 new_field = &fip->baseclasses.back ();
14548 }
14549 else
14550 {
14551 fip->fields.emplace_back ();
14552 new_field = &fip->fields.back ();
14553 }
14554
14555 new_field->offset = die->sect_off;
14556
14557 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14558 if (attr != nullptr)
14559 new_field->accessibility = DW_UNSND (attr);
14560 else
14561 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14562 if (new_field->accessibility != DW_ACCESS_public)
14563 fip->non_public_fields = 1;
14564
14565 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14566 if (attr != nullptr)
14567 new_field->virtuality = DW_UNSND (attr);
14568 else
14569 new_field->virtuality = DW_VIRTUALITY_none;
14570
14571 fp = &new_field->field;
14572
14573 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14574 {
14575 /* Data member other than a C++ static data member. */
14576
14577 /* Get type of field. */
14578 fp->set_type (die_type (die, cu));
14579
14580 SET_FIELD_BITPOS (*fp, 0);
14581
14582 /* Get bit size of field (zero if none). */
14583 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14584 if (attr != nullptr)
14585 {
14586 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14587 }
14588 else
14589 {
14590 FIELD_BITSIZE (*fp) = 0;
14591 }
14592
14593 /* Get bit offset of field. */
14594 handle_data_member_location (die, cu, fp);
14595 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14596 if (attr != nullptr)
14597 {
14598 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14599 {
14600 /* For big endian bits, the DW_AT_bit_offset gives the
14601 additional bit offset from the MSB of the containing
14602 anonymous object to the MSB of the field. We don't
14603 have to do anything special since we don't need to
14604 know the size of the anonymous object. */
14605 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14606 }
14607 else
14608 {
14609 /* For little endian bits, compute the bit offset to the
14610 MSB of the anonymous object, subtract off the number of
14611 bits from the MSB of the field to the MSB of the
14612 object, and then subtract off the number of bits of
14613 the field itself. The result is the bit offset of
14614 the LSB of the field. */
14615 int anonymous_size;
14616 int bit_offset = DW_UNSND (attr);
14617
14618 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14619 if (attr != nullptr)
14620 {
14621 /* The size of the anonymous object containing
14622 the bit field is explicit, so use the
14623 indicated size (in bytes). */
14624 anonymous_size = DW_UNSND (attr);
14625 }
14626 else
14627 {
14628 /* The size of the anonymous object containing
14629 the bit field must be inferred from the type
14630 attribute of the data member containing the
14631 bit field. */
14632 anonymous_size = TYPE_LENGTH (fp->type ());
14633 }
14634 SET_FIELD_BITPOS (*fp,
14635 (FIELD_BITPOS (*fp)
14636 + anonymous_size * bits_per_byte
14637 - bit_offset - FIELD_BITSIZE (*fp)));
14638 }
14639 }
14640 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14641 if (attr != NULL)
14642 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14643 + attr->constant_value (0)));
14644
14645 /* Get name of field. */
14646 fieldname = dwarf2_name (die, cu);
14647 if (fieldname == NULL)
14648 fieldname = "";
14649
14650 /* The name is already allocated along with this objfile, so we don't
14651 need to duplicate it for the type. */
14652 fp->name = fieldname;
14653
14654 /* Change accessibility for artificial fields (e.g. virtual table
14655 pointer or virtual base class pointer) to private. */
14656 if (dwarf2_attr (die, DW_AT_artificial, cu))
14657 {
14658 FIELD_ARTIFICIAL (*fp) = 1;
14659 new_field->accessibility = DW_ACCESS_private;
14660 fip->non_public_fields = 1;
14661 }
14662 }
14663 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14664 {
14665 /* C++ static member. */
14666
14667 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14668 is a declaration, but all versions of G++ as of this writing
14669 (so through at least 3.2.1) incorrectly generate
14670 DW_TAG_variable tags. */
14671
14672 const char *physname;
14673
14674 /* Get name of field. */
14675 fieldname = dwarf2_name (die, cu);
14676 if (fieldname == NULL)
14677 return;
14678
14679 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14680 if (attr
14681 /* Only create a symbol if this is an external value.
14682 new_symbol checks this and puts the value in the global symbol
14683 table, which we want. If it is not external, new_symbol
14684 will try to put the value in cu->list_in_scope which is wrong. */
14685 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14686 {
14687 /* A static const member, not much different than an enum as far as
14688 we're concerned, except that we can support more types. */
14689 new_symbol (die, NULL, cu);
14690 }
14691
14692 /* Get physical name. */
14693 physname = dwarf2_physname (fieldname, die, cu);
14694
14695 /* The name is already allocated along with this objfile, so we don't
14696 need to duplicate it for the type. */
14697 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14698 fp->set_type (die_type (die, cu));
14699 FIELD_NAME (*fp) = fieldname;
14700 }
14701 else if (die->tag == DW_TAG_inheritance)
14702 {
14703 /* C++ base class field. */
14704 handle_data_member_location (die, cu, fp);
14705 FIELD_BITSIZE (*fp) = 0;
14706 fp->set_type (die_type (die, cu));
14707 FIELD_NAME (*fp) = fp->type ()->name ();
14708 }
14709 else
14710 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14711 }
14712
14713 /* Can the type given by DIE define another type? */
14714
14715 static bool
14716 type_can_define_types (const struct die_info *die)
14717 {
14718 switch (die->tag)
14719 {
14720 case DW_TAG_typedef:
14721 case DW_TAG_class_type:
14722 case DW_TAG_structure_type:
14723 case DW_TAG_union_type:
14724 case DW_TAG_enumeration_type:
14725 return true;
14726
14727 default:
14728 return false;
14729 }
14730 }
14731
14732 /* Add a type definition defined in the scope of the FIP's class. */
14733
14734 static void
14735 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14736 struct dwarf2_cu *cu)
14737 {
14738 struct decl_field fp;
14739 memset (&fp, 0, sizeof (fp));
14740
14741 gdb_assert (type_can_define_types (die));
14742
14743 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14744 fp.name = dwarf2_name (die, cu);
14745 fp.type = read_type_die (die, cu);
14746
14747 /* Save accessibility. */
14748 enum dwarf_access_attribute accessibility;
14749 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14750 if (attr != NULL)
14751 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14752 else
14753 accessibility = dwarf2_default_access_attribute (die, cu);
14754 switch (accessibility)
14755 {
14756 case DW_ACCESS_public:
14757 /* The assumed value if neither private nor protected. */
14758 break;
14759 case DW_ACCESS_private:
14760 fp.is_private = 1;
14761 break;
14762 case DW_ACCESS_protected:
14763 fp.is_protected = 1;
14764 break;
14765 default:
14766 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14767 }
14768
14769 if (die->tag == DW_TAG_typedef)
14770 fip->typedef_field_list.push_back (fp);
14771 else
14772 fip->nested_types_list.push_back (fp);
14773 }
14774
14775 /* A convenience typedef that's used when finding the discriminant
14776 field for a variant part. */
14777 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14778 offset_map_type;
14779
14780 /* Compute the discriminant range for a given variant. OBSTACK is
14781 where the results will be stored. VARIANT is the variant to
14782 process. IS_UNSIGNED indicates whether the discriminant is signed
14783 or unsigned. */
14784
14785 static const gdb::array_view<discriminant_range>
14786 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14787 bool is_unsigned)
14788 {
14789 std::vector<discriminant_range> ranges;
14790
14791 if (variant.default_branch)
14792 return {};
14793
14794 if (variant.discr_list_data == nullptr)
14795 {
14796 discriminant_range r
14797 = {variant.discriminant_value, variant.discriminant_value};
14798 ranges.push_back (r);
14799 }
14800 else
14801 {
14802 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14803 variant.discr_list_data->size);
14804 while (!data.empty ())
14805 {
14806 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14807 {
14808 complaint (_("invalid discriminant marker: %d"), data[0]);
14809 break;
14810 }
14811 bool is_range = data[0] == DW_DSC_range;
14812 data = data.slice (1);
14813
14814 ULONGEST low, high;
14815 unsigned int bytes_read;
14816
14817 if (data.empty ())
14818 {
14819 complaint (_("DW_AT_discr_list missing low value"));
14820 break;
14821 }
14822 if (is_unsigned)
14823 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14824 else
14825 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14826 &bytes_read);
14827 data = data.slice (bytes_read);
14828
14829 if (is_range)
14830 {
14831 if (data.empty ())
14832 {
14833 complaint (_("DW_AT_discr_list missing high value"));
14834 break;
14835 }
14836 if (is_unsigned)
14837 high = read_unsigned_leb128 (nullptr, data.data (),
14838 &bytes_read);
14839 else
14840 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14841 &bytes_read);
14842 data = data.slice (bytes_read);
14843 }
14844 else
14845 high = low;
14846
14847 ranges.push_back ({ low, high });
14848 }
14849 }
14850
14851 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14852 ranges.size ());
14853 std::copy (ranges.begin (), ranges.end (), result);
14854 return gdb::array_view<discriminant_range> (result, ranges.size ());
14855 }
14856
14857 static const gdb::array_view<variant_part> create_variant_parts
14858 (struct obstack *obstack,
14859 const offset_map_type &offset_map,
14860 struct field_info *fi,
14861 const std::vector<variant_part_builder> &variant_parts);
14862
14863 /* Fill in a "struct variant" for a given variant field. RESULT is
14864 the variant to fill in. OBSTACK is where any needed allocations
14865 will be done. OFFSET_MAP holds the mapping from section offsets to
14866 fields for the type. FI describes the fields of the type we're
14867 processing. FIELD is the variant field we're converting. */
14868
14869 static void
14870 create_one_variant (variant &result, struct obstack *obstack,
14871 const offset_map_type &offset_map,
14872 struct field_info *fi, const variant_field &field)
14873 {
14874 result.discriminants = convert_variant_range (obstack, field, false);
14875 result.first_field = field.first_field + fi->baseclasses.size ();
14876 result.last_field = field.last_field + fi->baseclasses.size ();
14877 result.parts = create_variant_parts (obstack, offset_map, fi,
14878 field.variant_parts);
14879 }
14880
14881 /* Fill in a "struct variant_part" for a given variant part. RESULT
14882 is the variant part to fill in. OBSTACK is where any needed
14883 allocations will be done. OFFSET_MAP holds the mapping from
14884 section offsets to fields for the type. FI describes the fields of
14885 the type we're processing. BUILDER is the variant part to be
14886 converted. */
14887
14888 static void
14889 create_one_variant_part (variant_part &result,
14890 struct obstack *obstack,
14891 const offset_map_type &offset_map,
14892 struct field_info *fi,
14893 const variant_part_builder &builder)
14894 {
14895 auto iter = offset_map.find (builder.discriminant_offset);
14896 if (iter == offset_map.end ())
14897 {
14898 result.discriminant_index = -1;
14899 /* Doesn't matter. */
14900 result.is_unsigned = false;
14901 }
14902 else
14903 {
14904 result.discriminant_index = iter->second;
14905 result.is_unsigned
14906 = TYPE_UNSIGNED (fi->fields[result.discriminant_index].field.type ());
14907 }
14908
14909 size_t n = builder.variants.size ();
14910 variant *output = new (obstack) variant[n];
14911 for (size_t i = 0; i < n; ++i)
14912 create_one_variant (output[i], obstack, offset_map, fi,
14913 builder.variants[i]);
14914
14915 result.variants = gdb::array_view<variant> (output, n);
14916 }
14917
14918 /* Create a vector of variant parts that can be attached to a type.
14919 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14920 holds the mapping from section offsets to fields for the type. FI
14921 describes the fields of the type we're processing. VARIANT_PARTS
14922 is the vector to convert. */
14923
14924 static const gdb::array_view<variant_part>
14925 create_variant_parts (struct obstack *obstack,
14926 const offset_map_type &offset_map,
14927 struct field_info *fi,
14928 const std::vector<variant_part_builder> &variant_parts)
14929 {
14930 if (variant_parts.empty ())
14931 return {};
14932
14933 size_t n = variant_parts.size ();
14934 variant_part *result = new (obstack) variant_part[n];
14935 for (size_t i = 0; i < n; ++i)
14936 create_one_variant_part (result[i], obstack, offset_map, fi,
14937 variant_parts[i]);
14938
14939 return gdb::array_view<variant_part> (result, n);
14940 }
14941
14942 /* Compute the variant part vector for FIP, attaching it to TYPE when
14943 done. */
14944
14945 static void
14946 add_variant_property (struct field_info *fip, struct type *type,
14947 struct dwarf2_cu *cu)
14948 {
14949 /* Map section offsets of fields to their field index. Note the
14950 field index here does not take the number of baseclasses into
14951 account. */
14952 offset_map_type offset_map;
14953 for (int i = 0; i < fip->fields.size (); ++i)
14954 offset_map[fip->fields[i].offset] = i;
14955
14956 struct objfile *objfile = cu->per_objfile->objfile;
14957 gdb::array_view<variant_part> parts
14958 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14959 fip->variant_parts);
14960
14961 struct dynamic_prop prop;
14962 prop.kind = PROP_VARIANT_PARTS;
14963 prop.data.variant_parts
14964 = ((gdb::array_view<variant_part> *)
14965 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14966
14967 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14968 }
14969
14970 /* Create the vector of fields, and attach it to the type. */
14971
14972 static void
14973 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14974 struct dwarf2_cu *cu)
14975 {
14976 int nfields = fip->nfields ();
14977
14978 /* Record the field count, allocate space for the array of fields,
14979 and create blank accessibility bitfields if necessary. */
14980 type->set_num_fields (nfields);
14981 type->set_fields
14982 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14983
14984 if (fip->non_public_fields && cu->language != language_ada)
14985 {
14986 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14987
14988 TYPE_FIELD_PRIVATE_BITS (type) =
14989 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14990 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14991
14992 TYPE_FIELD_PROTECTED_BITS (type) =
14993 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14994 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14995
14996 TYPE_FIELD_IGNORE_BITS (type) =
14997 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14998 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14999 }
15000
15001 /* If the type has baseclasses, allocate and clear a bit vector for
15002 TYPE_FIELD_VIRTUAL_BITS. */
15003 if (!fip->baseclasses.empty () && cu->language != language_ada)
15004 {
15005 int num_bytes = B_BYTES (fip->baseclasses.size ());
15006 unsigned char *pointer;
15007
15008 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15009 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15010 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15011 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15012 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15013 }
15014
15015 if (!fip->variant_parts.empty ())
15016 add_variant_property (fip, type, cu);
15017
15018 /* Copy the saved-up fields into the field vector. */
15019 for (int i = 0; i < nfields; ++i)
15020 {
15021 struct nextfield &field
15022 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15023 : fip->fields[i - fip->baseclasses.size ()]);
15024
15025 type->field (i) = field.field;
15026 switch (field.accessibility)
15027 {
15028 case DW_ACCESS_private:
15029 if (cu->language != language_ada)
15030 SET_TYPE_FIELD_PRIVATE (type, i);
15031 break;
15032
15033 case DW_ACCESS_protected:
15034 if (cu->language != language_ada)
15035 SET_TYPE_FIELD_PROTECTED (type, i);
15036 break;
15037
15038 case DW_ACCESS_public:
15039 break;
15040
15041 default:
15042 /* Unknown accessibility. Complain and treat it as public. */
15043 {
15044 complaint (_("unsupported accessibility %d"),
15045 field.accessibility);
15046 }
15047 break;
15048 }
15049 if (i < fip->baseclasses.size ())
15050 {
15051 switch (field.virtuality)
15052 {
15053 case DW_VIRTUALITY_virtual:
15054 case DW_VIRTUALITY_pure_virtual:
15055 if (cu->language == language_ada)
15056 error (_("unexpected virtuality in component of Ada type"));
15057 SET_TYPE_FIELD_VIRTUAL (type, i);
15058 break;
15059 }
15060 }
15061 }
15062 }
15063
15064 /* Return true if this member function is a constructor, false
15065 otherwise. */
15066
15067 static int
15068 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15069 {
15070 const char *fieldname;
15071 const char *type_name;
15072 int len;
15073
15074 if (die->parent == NULL)
15075 return 0;
15076
15077 if (die->parent->tag != DW_TAG_structure_type
15078 && die->parent->tag != DW_TAG_union_type
15079 && die->parent->tag != DW_TAG_class_type)
15080 return 0;
15081
15082 fieldname = dwarf2_name (die, cu);
15083 type_name = dwarf2_name (die->parent, cu);
15084 if (fieldname == NULL || type_name == NULL)
15085 return 0;
15086
15087 len = strlen (fieldname);
15088 return (strncmp (fieldname, type_name, len) == 0
15089 && (type_name[len] == '\0' || type_name[len] == '<'));
15090 }
15091
15092 /* Check if the given VALUE is a recognized enum
15093 dwarf_defaulted_attribute constant according to DWARF5 spec,
15094 Table 7.24. */
15095
15096 static bool
15097 is_valid_DW_AT_defaulted (ULONGEST value)
15098 {
15099 switch (value)
15100 {
15101 case DW_DEFAULTED_no:
15102 case DW_DEFAULTED_in_class:
15103 case DW_DEFAULTED_out_of_class:
15104 return true;
15105 }
15106
15107 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15108 return false;
15109 }
15110
15111 /* Add a member function to the proper fieldlist. */
15112
15113 static void
15114 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15115 struct type *type, struct dwarf2_cu *cu)
15116 {
15117 struct objfile *objfile = cu->per_objfile->objfile;
15118 struct attribute *attr;
15119 int i;
15120 struct fnfieldlist *flp = nullptr;
15121 struct fn_field *fnp;
15122 const char *fieldname;
15123 struct type *this_type;
15124 enum dwarf_access_attribute accessibility;
15125
15126 if (cu->language == language_ada)
15127 error (_("unexpected member function in Ada type"));
15128
15129 /* Get name of member function. */
15130 fieldname = dwarf2_name (die, cu);
15131 if (fieldname == NULL)
15132 return;
15133
15134 /* Look up member function name in fieldlist. */
15135 for (i = 0; i < fip->fnfieldlists.size (); i++)
15136 {
15137 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15138 {
15139 flp = &fip->fnfieldlists[i];
15140 break;
15141 }
15142 }
15143
15144 /* Create a new fnfieldlist if necessary. */
15145 if (flp == nullptr)
15146 {
15147 fip->fnfieldlists.emplace_back ();
15148 flp = &fip->fnfieldlists.back ();
15149 flp->name = fieldname;
15150 i = fip->fnfieldlists.size () - 1;
15151 }
15152
15153 /* Create a new member function field and add it to the vector of
15154 fnfieldlists. */
15155 flp->fnfields.emplace_back ();
15156 fnp = &flp->fnfields.back ();
15157
15158 /* Delay processing of the physname until later. */
15159 if (cu->language == language_cplus)
15160 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15161 die, cu);
15162 else
15163 {
15164 const char *physname = dwarf2_physname (fieldname, die, cu);
15165 fnp->physname = physname ? physname : "";
15166 }
15167
15168 fnp->type = alloc_type (objfile);
15169 this_type = read_type_die (die, cu);
15170 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15171 {
15172 int nparams = this_type->num_fields ();
15173
15174 /* TYPE is the domain of this method, and THIS_TYPE is the type
15175 of the method itself (TYPE_CODE_METHOD). */
15176 smash_to_method_type (fnp->type, type,
15177 TYPE_TARGET_TYPE (this_type),
15178 this_type->fields (),
15179 this_type->num_fields (),
15180 TYPE_VARARGS (this_type));
15181
15182 /* Handle static member functions.
15183 Dwarf2 has no clean way to discern C++ static and non-static
15184 member functions. G++ helps GDB by marking the first
15185 parameter for non-static member functions (which is the this
15186 pointer) as artificial. We obtain this information from
15187 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15188 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15189 fnp->voffset = VOFFSET_STATIC;
15190 }
15191 else
15192 complaint (_("member function type missing for '%s'"),
15193 dwarf2_full_name (fieldname, die, cu));
15194
15195 /* Get fcontext from DW_AT_containing_type if present. */
15196 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15197 fnp->fcontext = die_containing_type (die, cu);
15198
15199 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15200 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15201
15202 /* Get accessibility. */
15203 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15204 if (attr != nullptr)
15205 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15206 else
15207 accessibility = dwarf2_default_access_attribute (die, cu);
15208 switch (accessibility)
15209 {
15210 case DW_ACCESS_private:
15211 fnp->is_private = 1;
15212 break;
15213 case DW_ACCESS_protected:
15214 fnp->is_protected = 1;
15215 break;
15216 }
15217
15218 /* Check for artificial methods. */
15219 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15220 if (attr && DW_UNSND (attr) != 0)
15221 fnp->is_artificial = 1;
15222
15223 /* Check for defaulted methods. */
15224 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15225 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15226 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15227
15228 /* Check for deleted methods. */
15229 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15230 if (attr != nullptr && DW_UNSND (attr) != 0)
15231 fnp->is_deleted = 1;
15232
15233 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15234
15235 /* Get index in virtual function table if it is a virtual member
15236 function. For older versions of GCC, this is an offset in the
15237 appropriate virtual table, as specified by DW_AT_containing_type.
15238 For everyone else, it is an expression to be evaluated relative
15239 to the object address. */
15240
15241 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15242 if (attr != nullptr)
15243 {
15244 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15245 {
15246 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15247 {
15248 /* Old-style GCC. */
15249 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15250 }
15251 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15252 || (DW_BLOCK (attr)->size > 1
15253 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15254 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15255 {
15256 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15257 if ((fnp->voffset % cu->header.addr_size) != 0)
15258 dwarf2_complex_location_expr_complaint ();
15259 else
15260 fnp->voffset /= cu->header.addr_size;
15261 fnp->voffset += 2;
15262 }
15263 else
15264 dwarf2_complex_location_expr_complaint ();
15265
15266 if (!fnp->fcontext)
15267 {
15268 /* If there is no `this' field and no DW_AT_containing_type,
15269 we cannot actually find a base class context for the
15270 vtable! */
15271 if (this_type->num_fields () == 0
15272 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15273 {
15274 complaint (_("cannot determine context for virtual member "
15275 "function \"%s\" (offset %s)"),
15276 fieldname, sect_offset_str (die->sect_off));
15277 }
15278 else
15279 {
15280 fnp->fcontext
15281 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15282 }
15283 }
15284 }
15285 else if (attr->form_is_section_offset ())
15286 {
15287 dwarf2_complex_location_expr_complaint ();
15288 }
15289 else
15290 {
15291 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15292 fieldname);
15293 }
15294 }
15295 else
15296 {
15297 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15298 if (attr && DW_UNSND (attr))
15299 {
15300 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15301 complaint (_("Member function \"%s\" (offset %s) is virtual "
15302 "but the vtable offset is not specified"),
15303 fieldname, sect_offset_str (die->sect_off));
15304 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15305 TYPE_CPLUS_DYNAMIC (type) = 1;
15306 }
15307 }
15308 }
15309
15310 /* Create the vector of member function fields, and attach it to the type. */
15311
15312 static void
15313 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15314 struct dwarf2_cu *cu)
15315 {
15316 if (cu->language == language_ada)
15317 error (_("unexpected member functions in Ada type"));
15318
15319 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15320 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15321 TYPE_ALLOC (type,
15322 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15323
15324 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15325 {
15326 struct fnfieldlist &nf = fip->fnfieldlists[i];
15327 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15328
15329 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15330 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15331 fn_flp->fn_fields = (struct fn_field *)
15332 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15333
15334 for (int k = 0; k < nf.fnfields.size (); ++k)
15335 fn_flp->fn_fields[k] = nf.fnfields[k];
15336 }
15337
15338 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15339 }
15340
15341 /* Returns non-zero if NAME is the name of a vtable member in CU's
15342 language, zero otherwise. */
15343 static int
15344 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15345 {
15346 static const char vptr[] = "_vptr";
15347
15348 /* Look for the C++ form of the vtable. */
15349 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15350 return 1;
15351
15352 return 0;
15353 }
15354
15355 /* GCC outputs unnamed structures that are really pointers to member
15356 functions, with the ABI-specified layout. If TYPE describes
15357 such a structure, smash it into a member function type.
15358
15359 GCC shouldn't do this; it should just output pointer to member DIEs.
15360 This is GCC PR debug/28767. */
15361
15362 static void
15363 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15364 {
15365 struct type *pfn_type, *self_type, *new_type;
15366
15367 /* Check for a structure with no name and two children. */
15368 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15369 return;
15370
15371 /* Check for __pfn and __delta members. */
15372 if (TYPE_FIELD_NAME (type, 0) == NULL
15373 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15374 || TYPE_FIELD_NAME (type, 1) == NULL
15375 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15376 return;
15377
15378 /* Find the type of the method. */
15379 pfn_type = type->field (0).type ();
15380 if (pfn_type == NULL
15381 || pfn_type->code () != TYPE_CODE_PTR
15382 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15383 return;
15384
15385 /* Look for the "this" argument. */
15386 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15387 if (pfn_type->num_fields () == 0
15388 /* || pfn_type->field (0).type () == NULL */
15389 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15390 return;
15391
15392 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15393 new_type = alloc_type (objfile);
15394 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15395 pfn_type->fields (), pfn_type->num_fields (),
15396 TYPE_VARARGS (pfn_type));
15397 smash_to_methodptr_type (type, new_type);
15398 }
15399
15400 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15401 appropriate error checking and issuing complaints if there is a
15402 problem. */
15403
15404 static ULONGEST
15405 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15406 {
15407 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15408
15409 if (attr == nullptr)
15410 return 0;
15411
15412 if (!attr->form_is_constant ())
15413 {
15414 complaint (_("DW_AT_alignment must have constant form"
15415 " - DIE at %s [in module %s]"),
15416 sect_offset_str (die->sect_off),
15417 objfile_name (cu->per_objfile->objfile));
15418 return 0;
15419 }
15420
15421 ULONGEST align;
15422 if (attr->form == DW_FORM_sdata)
15423 {
15424 LONGEST val = DW_SND (attr);
15425 if (val < 0)
15426 {
15427 complaint (_("DW_AT_alignment value must not be negative"
15428 " - DIE at %s [in module %s]"),
15429 sect_offset_str (die->sect_off),
15430 objfile_name (cu->per_objfile->objfile));
15431 return 0;
15432 }
15433 align = val;
15434 }
15435 else
15436 align = DW_UNSND (attr);
15437
15438 if (align == 0)
15439 {
15440 complaint (_("DW_AT_alignment value must not be zero"
15441 " - DIE at %s [in module %s]"),
15442 sect_offset_str (die->sect_off),
15443 objfile_name (cu->per_objfile->objfile));
15444 return 0;
15445 }
15446 if ((align & (align - 1)) != 0)
15447 {
15448 complaint (_("DW_AT_alignment value must be a power of 2"
15449 " - DIE at %s [in module %s]"),
15450 sect_offset_str (die->sect_off),
15451 objfile_name (cu->per_objfile->objfile));
15452 return 0;
15453 }
15454
15455 return align;
15456 }
15457
15458 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15459 the alignment for TYPE. */
15460
15461 static void
15462 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15463 struct type *type)
15464 {
15465 if (!set_type_align (type, get_alignment (cu, die)))
15466 complaint (_("DW_AT_alignment value too large"
15467 " - DIE at %s [in module %s]"),
15468 sect_offset_str (die->sect_off),
15469 objfile_name (cu->per_objfile->objfile));
15470 }
15471
15472 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15473 constant for a type, according to DWARF5 spec, Table 5.5. */
15474
15475 static bool
15476 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15477 {
15478 switch (value)
15479 {
15480 case DW_CC_normal:
15481 case DW_CC_pass_by_reference:
15482 case DW_CC_pass_by_value:
15483 return true;
15484
15485 default:
15486 complaint (_("unrecognized DW_AT_calling_convention value "
15487 "(%s) for a type"), pulongest (value));
15488 return false;
15489 }
15490 }
15491
15492 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15493 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15494 also according to GNU-specific values (see include/dwarf2.h). */
15495
15496 static bool
15497 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15498 {
15499 switch (value)
15500 {
15501 case DW_CC_normal:
15502 case DW_CC_program:
15503 case DW_CC_nocall:
15504 return true;
15505
15506 case DW_CC_GNU_renesas_sh:
15507 case DW_CC_GNU_borland_fastcall_i386:
15508 case DW_CC_GDB_IBM_OpenCL:
15509 return true;
15510
15511 default:
15512 complaint (_("unrecognized DW_AT_calling_convention value "
15513 "(%s) for a subroutine"), pulongest (value));
15514 return false;
15515 }
15516 }
15517
15518 /* Called when we find the DIE that starts a structure or union scope
15519 (definition) to create a type for the structure or union. Fill in
15520 the type's name and general properties; the members will not be
15521 processed until process_structure_scope. A symbol table entry for
15522 the type will also not be done until process_structure_scope (assuming
15523 the type has a name).
15524
15525 NOTE: we need to call these functions regardless of whether or not the
15526 DIE has a DW_AT_name attribute, since it might be an anonymous
15527 structure or union. This gets the type entered into our set of
15528 user defined types. */
15529
15530 static struct type *
15531 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15532 {
15533 struct objfile *objfile = cu->per_objfile->objfile;
15534 struct type *type;
15535 struct attribute *attr;
15536 const char *name;
15537
15538 /* If the definition of this type lives in .debug_types, read that type.
15539 Don't follow DW_AT_specification though, that will take us back up
15540 the chain and we want to go down. */
15541 attr = die->attr (DW_AT_signature);
15542 if (attr != nullptr)
15543 {
15544 type = get_DW_AT_signature_type (die, attr, cu);
15545
15546 /* The type's CU may not be the same as CU.
15547 Ensure TYPE is recorded with CU in die_type_hash. */
15548 return set_die_type (die, type, cu);
15549 }
15550
15551 type = alloc_type (objfile);
15552 INIT_CPLUS_SPECIFIC (type);
15553
15554 name = dwarf2_name (die, cu);
15555 if (name != NULL)
15556 {
15557 if (cu->language == language_cplus
15558 || cu->language == language_d
15559 || cu->language == language_rust)
15560 {
15561 const char *full_name = dwarf2_full_name (name, die, cu);
15562
15563 /* dwarf2_full_name might have already finished building the DIE's
15564 type. If so, there is no need to continue. */
15565 if (get_die_type (die, cu) != NULL)
15566 return get_die_type (die, cu);
15567
15568 type->set_name (full_name);
15569 }
15570 else
15571 {
15572 /* The name is already allocated along with this objfile, so
15573 we don't need to duplicate it for the type. */
15574 type->set_name (name);
15575 }
15576 }
15577
15578 if (die->tag == DW_TAG_structure_type)
15579 {
15580 type->set_code (TYPE_CODE_STRUCT);
15581 }
15582 else if (die->tag == DW_TAG_union_type)
15583 {
15584 type->set_code (TYPE_CODE_UNION);
15585 }
15586 else
15587 {
15588 type->set_code (TYPE_CODE_STRUCT);
15589 }
15590
15591 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15592 TYPE_DECLARED_CLASS (type) = 1;
15593
15594 /* Store the calling convention in the type if it's available in
15595 the die. Otherwise the calling convention remains set to
15596 the default value DW_CC_normal. */
15597 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15598 if (attr != nullptr
15599 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15600 {
15601 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15602 TYPE_CPLUS_CALLING_CONVENTION (type)
15603 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15604 }
15605
15606 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15607 if (attr != nullptr)
15608 {
15609 if (attr->form_is_constant ())
15610 TYPE_LENGTH (type) = DW_UNSND (attr);
15611 else
15612 {
15613 struct dynamic_prop prop;
15614 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15615 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15616 TYPE_LENGTH (type) = 0;
15617 }
15618 }
15619 else
15620 {
15621 TYPE_LENGTH (type) = 0;
15622 }
15623
15624 maybe_set_alignment (cu, die, type);
15625
15626 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15627 {
15628 /* ICC<14 does not output the required DW_AT_declaration on
15629 incomplete types, but gives them a size of zero. */
15630 TYPE_STUB (type) = 1;
15631 }
15632 else
15633 TYPE_STUB_SUPPORTED (type) = 1;
15634
15635 if (die_is_declaration (die, cu))
15636 TYPE_STUB (type) = 1;
15637 else if (attr == NULL && die->child == NULL
15638 && producer_is_realview (cu->producer))
15639 /* RealView does not output the required DW_AT_declaration
15640 on incomplete types. */
15641 TYPE_STUB (type) = 1;
15642
15643 /* We need to add the type field to the die immediately so we don't
15644 infinitely recurse when dealing with pointers to the structure
15645 type within the structure itself. */
15646 set_die_type (die, type, cu);
15647
15648 /* set_die_type should be already done. */
15649 set_descriptive_type (type, die, cu);
15650
15651 return type;
15652 }
15653
15654 static void handle_struct_member_die
15655 (struct die_info *child_die,
15656 struct type *type,
15657 struct field_info *fi,
15658 std::vector<struct symbol *> *template_args,
15659 struct dwarf2_cu *cu);
15660
15661 /* A helper for handle_struct_member_die that handles
15662 DW_TAG_variant_part. */
15663
15664 static void
15665 handle_variant_part (struct die_info *die, struct type *type,
15666 struct field_info *fi,
15667 std::vector<struct symbol *> *template_args,
15668 struct dwarf2_cu *cu)
15669 {
15670 variant_part_builder *new_part;
15671 if (fi->current_variant_part == nullptr)
15672 {
15673 fi->variant_parts.emplace_back ();
15674 new_part = &fi->variant_parts.back ();
15675 }
15676 else if (!fi->current_variant_part->processing_variant)
15677 {
15678 complaint (_("nested DW_TAG_variant_part seen "
15679 "- DIE at %s [in module %s]"),
15680 sect_offset_str (die->sect_off),
15681 objfile_name (cu->per_objfile->objfile));
15682 return;
15683 }
15684 else
15685 {
15686 variant_field &current = fi->current_variant_part->variants.back ();
15687 current.variant_parts.emplace_back ();
15688 new_part = &current.variant_parts.back ();
15689 }
15690
15691 /* When we recurse, we want callees to add to this new variant
15692 part. */
15693 scoped_restore save_current_variant_part
15694 = make_scoped_restore (&fi->current_variant_part, new_part);
15695
15696 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15697 if (discr == NULL)
15698 {
15699 /* It's a univariant form, an extension we support. */
15700 }
15701 else if (discr->form_is_ref ())
15702 {
15703 struct dwarf2_cu *target_cu = cu;
15704 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15705
15706 new_part->discriminant_offset = target_die->sect_off;
15707 }
15708 else
15709 {
15710 complaint (_("DW_AT_discr does not have DIE reference form"
15711 " - DIE at %s [in module %s]"),
15712 sect_offset_str (die->sect_off),
15713 objfile_name (cu->per_objfile->objfile));
15714 }
15715
15716 for (die_info *child_die = die->child;
15717 child_die != NULL;
15718 child_die = child_die->sibling)
15719 handle_struct_member_die (child_die, type, fi, template_args, cu);
15720 }
15721
15722 /* A helper for handle_struct_member_die that handles
15723 DW_TAG_variant. */
15724
15725 static void
15726 handle_variant (struct die_info *die, struct type *type,
15727 struct field_info *fi,
15728 std::vector<struct symbol *> *template_args,
15729 struct dwarf2_cu *cu)
15730 {
15731 if (fi->current_variant_part == nullptr)
15732 {
15733 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15734 "- DIE at %s [in module %s]"),
15735 sect_offset_str (die->sect_off),
15736 objfile_name (cu->per_objfile->objfile));
15737 return;
15738 }
15739 if (fi->current_variant_part->processing_variant)
15740 {
15741 complaint (_("nested DW_TAG_variant seen "
15742 "- DIE at %s [in module %s]"),
15743 sect_offset_str (die->sect_off),
15744 objfile_name (cu->per_objfile->objfile));
15745 return;
15746 }
15747
15748 scoped_restore save_processing_variant
15749 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15750 true);
15751
15752 fi->current_variant_part->variants.emplace_back ();
15753 variant_field &variant = fi->current_variant_part->variants.back ();
15754 variant.first_field = fi->fields.size ();
15755
15756 /* In a variant we want to get the discriminant and also add a
15757 field for our sole member child. */
15758 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15759 if (discr == nullptr)
15760 {
15761 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15762 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15763 variant.default_branch = true;
15764 else
15765 variant.discr_list_data = DW_BLOCK (discr);
15766 }
15767 else
15768 variant.discriminant_value = DW_UNSND (discr);
15769
15770 for (die_info *variant_child = die->child;
15771 variant_child != NULL;
15772 variant_child = variant_child->sibling)
15773 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15774
15775 variant.last_field = fi->fields.size ();
15776 }
15777
15778 /* A helper for process_structure_scope that handles a single member
15779 DIE. */
15780
15781 static void
15782 handle_struct_member_die (struct die_info *child_die, struct type *type,
15783 struct field_info *fi,
15784 std::vector<struct symbol *> *template_args,
15785 struct dwarf2_cu *cu)
15786 {
15787 if (child_die->tag == DW_TAG_member
15788 || child_die->tag == DW_TAG_variable)
15789 {
15790 /* NOTE: carlton/2002-11-05: A C++ static data member
15791 should be a DW_TAG_member that is a declaration, but
15792 all versions of G++ as of this writing (so through at
15793 least 3.2.1) incorrectly generate DW_TAG_variable
15794 tags for them instead. */
15795 dwarf2_add_field (fi, child_die, cu);
15796 }
15797 else if (child_die->tag == DW_TAG_subprogram)
15798 {
15799 /* Rust doesn't have member functions in the C++ sense.
15800 However, it does emit ordinary functions as children
15801 of a struct DIE. */
15802 if (cu->language == language_rust)
15803 read_func_scope (child_die, cu);
15804 else
15805 {
15806 /* C++ member function. */
15807 dwarf2_add_member_fn (fi, child_die, type, cu);
15808 }
15809 }
15810 else if (child_die->tag == DW_TAG_inheritance)
15811 {
15812 /* C++ base class field. */
15813 dwarf2_add_field (fi, child_die, cu);
15814 }
15815 else if (type_can_define_types (child_die))
15816 dwarf2_add_type_defn (fi, child_die, cu);
15817 else if (child_die->tag == DW_TAG_template_type_param
15818 || child_die->tag == DW_TAG_template_value_param)
15819 {
15820 struct symbol *arg = new_symbol (child_die, NULL, cu);
15821
15822 if (arg != NULL)
15823 template_args->push_back (arg);
15824 }
15825 else if (child_die->tag == DW_TAG_variant_part)
15826 handle_variant_part (child_die, type, fi, template_args, cu);
15827 else if (child_die->tag == DW_TAG_variant)
15828 handle_variant (child_die, type, fi, template_args, cu);
15829 }
15830
15831 /* Finish creating a structure or union type, including filling in
15832 its members and creating a symbol for it. */
15833
15834 static void
15835 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15836 {
15837 struct objfile *objfile = cu->per_objfile->objfile;
15838 struct die_info *child_die;
15839 struct type *type;
15840
15841 type = get_die_type (die, cu);
15842 if (type == NULL)
15843 type = read_structure_type (die, cu);
15844
15845 bool has_template_parameters = false;
15846 if (die->child != NULL && ! die_is_declaration (die, cu))
15847 {
15848 struct field_info fi;
15849 std::vector<struct symbol *> template_args;
15850
15851 child_die = die->child;
15852
15853 while (child_die && child_die->tag)
15854 {
15855 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15856 child_die = child_die->sibling;
15857 }
15858
15859 /* Attach template arguments to type. */
15860 if (!template_args.empty ())
15861 {
15862 has_template_parameters = true;
15863 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15864 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15865 TYPE_TEMPLATE_ARGUMENTS (type)
15866 = XOBNEWVEC (&objfile->objfile_obstack,
15867 struct symbol *,
15868 TYPE_N_TEMPLATE_ARGUMENTS (type));
15869 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15870 template_args.data (),
15871 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15872 * sizeof (struct symbol *)));
15873 }
15874
15875 /* Attach fields and member functions to the type. */
15876 if (fi.nfields () > 0)
15877 dwarf2_attach_fields_to_type (&fi, type, cu);
15878 if (!fi.fnfieldlists.empty ())
15879 {
15880 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15881
15882 /* Get the type which refers to the base class (possibly this
15883 class itself) which contains the vtable pointer for the current
15884 class from the DW_AT_containing_type attribute. This use of
15885 DW_AT_containing_type is a GNU extension. */
15886
15887 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15888 {
15889 struct type *t = die_containing_type (die, cu);
15890
15891 set_type_vptr_basetype (type, t);
15892 if (type == t)
15893 {
15894 int i;
15895
15896 /* Our own class provides vtbl ptr. */
15897 for (i = t->num_fields () - 1;
15898 i >= TYPE_N_BASECLASSES (t);
15899 --i)
15900 {
15901 const char *fieldname = TYPE_FIELD_NAME (t, i);
15902
15903 if (is_vtable_name (fieldname, cu))
15904 {
15905 set_type_vptr_fieldno (type, i);
15906 break;
15907 }
15908 }
15909
15910 /* Complain if virtual function table field not found. */
15911 if (i < TYPE_N_BASECLASSES (t))
15912 complaint (_("virtual function table pointer "
15913 "not found when defining class '%s'"),
15914 type->name () ? type->name () : "");
15915 }
15916 else
15917 {
15918 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15919 }
15920 }
15921 else if (cu->producer
15922 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15923 {
15924 /* The IBM XLC compiler does not provide direct indication
15925 of the containing type, but the vtable pointer is
15926 always named __vfp. */
15927
15928 int i;
15929
15930 for (i = type->num_fields () - 1;
15931 i >= TYPE_N_BASECLASSES (type);
15932 --i)
15933 {
15934 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15935 {
15936 set_type_vptr_fieldno (type, i);
15937 set_type_vptr_basetype (type, type);
15938 break;
15939 }
15940 }
15941 }
15942 }
15943
15944 /* Copy fi.typedef_field_list linked list elements content into the
15945 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15946 if (!fi.typedef_field_list.empty ())
15947 {
15948 int count = fi.typedef_field_list.size ();
15949
15950 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15951 TYPE_TYPEDEF_FIELD_ARRAY (type)
15952 = ((struct decl_field *)
15953 TYPE_ALLOC (type,
15954 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15955 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15956
15957 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15958 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15959 }
15960
15961 /* Copy fi.nested_types_list linked list elements content into the
15962 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15963 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15964 {
15965 int count = fi.nested_types_list.size ();
15966
15967 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15968 TYPE_NESTED_TYPES_ARRAY (type)
15969 = ((struct decl_field *)
15970 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15971 TYPE_NESTED_TYPES_COUNT (type) = count;
15972
15973 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15974 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15975 }
15976 }
15977
15978 quirk_gcc_member_function_pointer (type, objfile);
15979 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15980 cu->rust_unions.push_back (type);
15981
15982 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15983 snapshots) has been known to create a die giving a declaration
15984 for a class that has, as a child, a die giving a definition for a
15985 nested class. So we have to process our children even if the
15986 current die is a declaration. Normally, of course, a declaration
15987 won't have any children at all. */
15988
15989 child_die = die->child;
15990
15991 while (child_die != NULL && child_die->tag)
15992 {
15993 if (child_die->tag == DW_TAG_member
15994 || child_die->tag == DW_TAG_variable
15995 || child_die->tag == DW_TAG_inheritance
15996 || child_die->tag == DW_TAG_template_value_param
15997 || child_die->tag == DW_TAG_template_type_param)
15998 {
15999 /* Do nothing. */
16000 }
16001 else
16002 process_die (child_die, cu);
16003
16004 child_die = child_die->sibling;
16005 }
16006
16007 /* Do not consider external references. According to the DWARF standard,
16008 these DIEs are identified by the fact that they have no byte_size
16009 attribute, and a declaration attribute. */
16010 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16011 || !die_is_declaration (die, cu)
16012 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16013 {
16014 struct symbol *sym = new_symbol (die, type, cu);
16015
16016 if (has_template_parameters)
16017 {
16018 struct symtab *symtab;
16019 if (sym != nullptr)
16020 symtab = symbol_symtab (sym);
16021 else if (cu->line_header != nullptr)
16022 {
16023 /* Any related symtab will do. */
16024 symtab
16025 = cu->line_header->file_names ()[0].symtab;
16026 }
16027 else
16028 {
16029 symtab = nullptr;
16030 complaint (_("could not find suitable "
16031 "symtab for template parameter"
16032 " - DIE at %s [in module %s]"),
16033 sect_offset_str (die->sect_off),
16034 objfile_name (objfile));
16035 }
16036
16037 if (symtab != nullptr)
16038 {
16039 /* Make sure that the symtab is set on the new symbols.
16040 Even though they don't appear in this symtab directly,
16041 other parts of gdb assume that symbols do, and this is
16042 reasonably true. */
16043 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16044 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16045 }
16046 }
16047 }
16048 }
16049
16050 /* Assuming DIE is an enumeration type, and TYPE is its associated
16051 type, update TYPE using some information only available in DIE's
16052 children. In particular, the fields are computed. */
16053
16054 static void
16055 update_enumeration_type_from_children (struct die_info *die,
16056 struct type *type,
16057 struct dwarf2_cu *cu)
16058 {
16059 struct die_info *child_die;
16060 int unsigned_enum = 1;
16061 int flag_enum = 1;
16062
16063 auto_obstack obstack;
16064 std::vector<struct field> fields;
16065
16066 for (child_die = die->child;
16067 child_die != NULL && child_die->tag;
16068 child_die = child_die->sibling)
16069 {
16070 struct attribute *attr;
16071 LONGEST value;
16072 const gdb_byte *bytes;
16073 struct dwarf2_locexpr_baton *baton;
16074 const char *name;
16075
16076 if (child_die->tag != DW_TAG_enumerator)
16077 continue;
16078
16079 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16080 if (attr == NULL)
16081 continue;
16082
16083 name = dwarf2_name (child_die, cu);
16084 if (name == NULL)
16085 name = "<anonymous enumerator>";
16086
16087 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16088 &value, &bytes, &baton);
16089 if (value < 0)
16090 {
16091 unsigned_enum = 0;
16092 flag_enum = 0;
16093 }
16094 else
16095 {
16096 if (count_one_bits_ll (value) >= 2)
16097 flag_enum = 0;
16098 }
16099
16100 fields.emplace_back ();
16101 struct field &field = fields.back ();
16102 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16103 SET_FIELD_ENUMVAL (field, value);
16104 }
16105
16106 if (!fields.empty ())
16107 {
16108 type->set_num_fields (fields.size ());
16109 type->set_fields
16110 ((struct field *)
16111 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16112 memcpy (type->fields (), fields.data (),
16113 sizeof (struct field) * fields.size ());
16114 }
16115
16116 if (unsigned_enum)
16117 TYPE_UNSIGNED (type) = 1;
16118 if (flag_enum)
16119 TYPE_FLAG_ENUM (type) = 1;
16120 }
16121
16122 /* Given a DW_AT_enumeration_type die, set its type. We do not
16123 complete the type's fields yet, or create any symbols. */
16124
16125 static struct type *
16126 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16127 {
16128 struct objfile *objfile = cu->per_objfile->objfile;
16129 struct type *type;
16130 struct attribute *attr;
16131 const char *name;
16132
16133 /* If the definition of this type lives in .debug_types, read that type.
16134 Don't follow DW_AT_specification though, that will take us back up
16135 the chain and we want to go down. */
16136 attr = die->attr (DW_AT_signature);
16137 if (attr != nullptr)
16138 {
16139 type = get_DW_AT_signature_type (die, attr, cu);
16140
16141 /* The type's CU may not be the same as CU.
16142 Ensure TYPE is recorded with CU in die_type_hash. */
16143 return set_die_type (die, type, cu);
16144 }
16145
16146 type = alloc_type (objfile);
16147
16148 type->set_code (TYPE_CODE_ENUM);
16149 name = dwarf2_full_name (NULL, die, cu);
16150 if (name != NULL)
16151 type->set_name (name);
16152
16153 attr = dwarf2_attr (die, DW_AT_type, cu);
16154 if (attr != NULL)
16155 {
16156 struct type *underlying_type = die_type (die, cu);
16157
16158 TYPE_TARGET_TYPE (type) = underlying_type;
16159 }
16160
16161 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16162 if (attr != nullptr)
16163 {
16164 TYPE_LENGTH (type) = DW_UNSND (attr);
16165 }
16166 else
16167 {
16168 TYPE_LENGTH (type) = 0;
16169 }
16170
16171 maybe_set_alignment (cu, die, type);
16172
16173 /* The enumeration DIE can be incomplete. In Ada, any type can be
16174 declared as private in the package spec, and then defined only
16175 inside the package body. Such types are known as Taft Amendment
16176 Types. When another package uses such a type, an incomplete DIE
16177 may be generated by the compiler. */
16178 if (die_is_declaration (die, cu))
16179 TYPE_STUB (type) = 1;
16180
16181 /* If this type has an underlying type that is not a stub, then we
16182 may use its attributes. We always use the "unsigned" attribute
16183 in this situation, because ordinarily we guess whether the type
16184 is unsigned -- but the guess can be wrong and the underlying type
16185 can tell us the reality. However, we defer to a local size
16186 attribute if one exists, because this lets the compiler override
16187 the underlying type if needed. */
16188 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16189 {
16190 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16191 underlying_type = check_typedef (underlying_type);
16192 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16193 if (TYPE_LENGTH (type) == 0)
16194 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16195 if (TYPE_RAW_ALIGN (type) == 0
16196 && TYPE_RAW_ALIGN (underlying_type) != 0)
16197 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16198 }
16199
16200 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16201
16202 set_die_type (die, type, cu);
16203
16204 /* Finish the creation of this type by using the enum's children.
16205 Note that, as usual, this must come after set_die_type to avoid
16206 infinite recursion when trying to compute the names of the
16207 enumerators. */
16208 update_enumeration_type_from_children (die, type, cu);
16209
16210 return type;
16211 }
16212
16213 /* Given a pointer to a die which begins an enumeration, process all
16214 the dies that define the members of the enumeration, and create the
16215 symbol for the enumeration type.
16216
16217 NOTE: We reverse the order of the element list. */
16218
16219 static void
16220 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16221 {
16222 struct type *this_type;
16223
16224 this_type = get_die_type (die, cu);
16225 if (this_type == NULL)
16226 this_type = read_enumeration_type (die, cu);
16227
16228 if (die->child != NULL)
16229 {
16230 struct die_info *child_die;
16231 const char *name;
16232
16233 child_die = die->child;
16234 while (child_die && child_die->tag)
16235 {
16236 if (child_die->tag != DW_TAG_enumerator)
16237 {
16238 process_die (child_die, cu);
16239 }
16240 else
16241 {
16242 name = dwarf2_name (child_die, cu);
16243 if (name)
16244 new_symbol (child_die, this_type, cu);
16245 }
16246
16247 child_die = child_die->sibling;
16248 }
16249 }
16250
16251 /* If we are reading an enum from a .debug_types unit, and the enum
16252 is a declaration, and the enum is not the signatured type in the
16253 unit, then we do not want to add a symbol for it. Adding a
16254 symbol would in some cases obscure the true definition of the
16255 enum, giving users an incomplete type when the definition is
16256 actually available. Note that we do not want to do this for all
16257 enums which are just declarations, because C++0x allows forward
16258 enum declarations. */
16259 if (cu->per_cu->is_debug_types
16260 && die_is_declaration (die, cu))
16261 {
16262 struct signatured_type *sig_type;
16263
16264 sig_type = (struct signatured_type *) cu->per_cu;
16265 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16266 if (sig_type->type_offset_in_section != die->sect_off)
16267 return;
16268 }
16269
16270 new_symbol (die, this_type, cu);
16271 }
16272
16273 /* Extract all information from a DW_TAG_array_type DIE and put it in
16274 the DIE's type field. For now, this only handles one dimensional
16275 arrays. */
16276
16277 static struct type *
16278 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16279 {
16280 struct objfile *objfile = cu->per_objfile->objfile;
16281 struct die_info *child_die;
16282 struct type *type;
16283 struct type *element_type, *range_type, *index_type;
16284 struct attribute *attr;
16285 const char *name;
16286 struct dynamic_prop *byte_stride_prop = NULL;
16287 unsigned int bit_stride = 0;
16288
16289 element_type = die_type (die, cu);
16290
16291 /* The die_type call above may have already set the type for this DIE. */
16292 type = get_die_type (die, cu);
16293 if (type)
16294 return type;
16295
16296 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16297 if (attr != NULL)
16298 {
16299 int stride_ok;
16300 struct type *prop_type = cu->addr_sized_int_type (false);
16301
16302 byte_stride_prop
16303 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16304 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16305 prop_type);
16306 if (!stride_ok)
16307 {
16308 complaint (_("unable to read array DW_AT_byte_stride "
16309 " - DIE at %s [in module %s]"),
16310 sect_offset_str (die->sect_off),
16311 objfile_name (cu->per_objfile->objfile));
16312 /* Ignore this attribute. We will likely not be able to print
16313 arrays of this type correctly, but there is little we can do
16314 to help if we cannot read the attribute's value. */
16315 byte_stride_prop = NULL;
16316 }
16317 }
16318
16319 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16320 if (attr != NULL)
16321 bit_stride = DW_UNSND (attr);
16322
16323 /* Irix 6.2 native cc creates array types without children for
16324 arrays with unspecified length. */
16325 if (die->child == NULL)
16326 {
16327 index_type = objfile_type (objfile)->builtin_int;
16328 range_type = create_static_range_type (NULL, index_type, 0, -1);
16329 type = create_array_type_with_stride (NULL, element_type, range_type,
16330 byte_stride_prop, bit_stride);
16331 return set_die_type (die, type, cu);
16332 }
16333
16334 std::vector<struct type *> range_types;
16335 child_die = die->child;
16336 while (child_die && child_die->tag)
16337 {
16338 if (child_die->tag == DW_TAG_subrange_type)
16339 {
16340 struct type *child_type = read_type_die (child_die, cu);
16341
16342 if (child_type != NULL)
16343 {
16344 /* The range type was succesfully read. Save it for the
16345 array type creation. */
16346 range_types.push_back (child_type);
16347 }
16348 }
16349 child_die = child_die->sibling;
16350 }
16351
16352 /* Dwarf2 dimensions are output from left to right, create the
16353 necessary array types in backwards order. */
16354
16355 type = element_type;
16356
16357 if (read_array_order (die, cu) == DW_ORD_col_major)
16358 {
16359 int i = 0;
16360
16361 while (i < range_types.size ())
16362 type = create_array_type_with_stride (NULL, type, range_types[i++],
16363 byte_stride_prop, bit_stride);
16364 }
16365 else
16366 {
16367 size_t ndim = range_types.size ();
16368 while (ndim-- > 0)
16369 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16370 byte_stride_prop, bit_stride);
16371 }
16372
16373 /* Understand Dwarf2 support for vector types (like they occur on
16374 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16375 array type. This is not part of the Dwarf2/3 standard yet, but a
16376 custom vendor extension. The main difference between a regular
16377 array and the vector variant is that vectors are passed by value
16378 to functions. */
16379 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16380 if (attr != nullptr)
16381 make_vector_type (type);
16382
16383 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16384 implementation may choose to implement triple vectors using this
16385 attribute. */
16386 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16387 if (attr != nullptr)
16388 {
16389 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16390 TYPE_LENGTH (type) = DW_UNSND (attr);
16391 else
16392 complaint (_("DW_AT_byte_size for array type smaller "
16393 "than the total size of elements"));
16394 }
16395
16396 name = dwarf2_name (die, cu);
16397 if (name)
16398 type->set_name (name);
16399
16400 maybe_set_alignment (cu, die, type);
16401
16402 /* Install the type in the die. */
16403 set_die_type (die, type, cu);
16404
16405 /* set_die_type should be already done. */
16406 set_descriptive_type (type, die, cu);
16407
16408 return type;
16409 }
16410
16411 static enum dwarf_array_dim_ordering
16412 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16413 {
16414 struct attribute *attr;
16415
16416 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16417
16418 if (attr != nullptr)
16419 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16420
16421 /* GNU F77 is a special case, as at 08/2004 array type info is the
16422 opposite order to the dwarf2 specification, but data is still
16423 laid out as per normal fortran.
16424
16425 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16426 version checking. */
16427
16428 if (cu->language == language_fortran
16429 && cu->producer && strstr (cu->producer, "GNU F77"))
16430 {
16431 return DW_ORD_row_major;
16432 }
16433
16434 switch (cu->language_defn->la_array_ordering)
16435 {
16436 case array_column_major:
16437 return DW_ORD_col_major;
16438 case array_row_major:
16439 default:
16440 return DW_ORD_row_major;
16441 };
16442 }
16443
16444 /* Extract all information from a DW_TAG_set_type DIE and put it in
16445 the DIE's type field. */
16446
16447 static struct type *
16448 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16449 {
16450 struct type *domain_type, *set_type;
16451 struct attribute *attr;
16452
16453 domain_type = die_type (die, cu);
16454
16455 /* The die_type call above may have already set the type for this DIE. */
16456 set_type = get_die_type (die, cu);
16457 if (set_type)
16458 return set_type;
16459
16460 set_type = create_set_type (NULL, domain_type);
16461
16462 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16463 if (attr != nullptr)
16464 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16465
16466 maybe_set_alignment (cu, die, set_type);
16467
16468 return set_die_type (die, set_type, cu);
16469 }
16470
16471 /* A helper for read_common_block that creates a locexpr baton.
16472 SYM is the symbol which we are marking as computed.
16473 COMMON_DIE is the DIE for the common block.
16474 COMMON_LOC is the location expression attribute for the common
16475 block itself.
16476 MEMBER_LOC is the location expression attribute for the particular
16477 member of the common block that we are processing.
16478 CU is the CU from which the above come. */
16479
16480 static void
16481 mark_common_block_symbol_computed (struct symbol *sym,
16482 struct die_info *common_die,
16483 struct attribute *common_loc,
16484 struct attribute *member_loc,
16485 struct dwarf2_cu *cu)
16486 {
16487 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16488 struct objfile *objfile = per_objfile->objfile;
16489 struct dwarf2_locexpr_baton *baton;
16490 gdb_byte *ptr;
16491 unsigned int cu_off;
16492 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16493 LONGEST offset = 0;
16494
16495 gdb_assert (common_loc && member_loc);
16496 gdb_assert (common_loc->form_is_block ());
16497 gdb_assert (member_loc->form_is_block ()
16498 || member_loc->form_is_constant ());
16499
16500 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16501 baton->per_objfile = per_objfile;
16502 baton->per_cu = cu->per_cu;
16503 gdb_assert (baton->per_cu);
16504
16505 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16506
16507 if (member_loc->form_is_constant ())
16508 {
16509 offset = member_loc->constant_value (0);
16510 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16511 }
16512 else
16513 baton->size += DW_BLOCK (member_loc)->size;
16514
16515 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16516 baton->data = ptr;
16517
16518 *ptr++ = DW_OP_call4;
16519 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16520 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16521 ptr += 4;
16522
16523 if (member_loc->form_is_constant ())
16524 {
16525 *ptr++ = DW_OP_addr;
16526 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16527 ptr += cu->header.addr_size;
16528 }
16529 else
16530 {
16531 /* We have to copy the data here, because DW_OP_call4 will only
16532 use a DW_AT_location attribute. */
16533 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16534 ptr += DW_BLOCK (member_loc)->size;
16535 }
16536
16537 *ptr++ = DW_OP_plus;
16538 gdb_assert (ptr - baton->data == baton->size);
16539
16540 SYMBOL_LOCATION_BATON (sym) = baton;
16541 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16542 }
16543
16544 /* Create appropriate locally-scoped variables for all the
16545 DW_TAG_common_block entries. Also create a struct common_block
16546 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16547 is used to separate the common blocks name namespace from regular
16548 variable names. */
16549
16550 static void
16551 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16552 {
16553 struct attribute *attr;
16554
16555 attr = dwarf2_attr (die, DW_AT_location, cu);
16556 if (attr != nullptr)
16557 {
16558 /* Support the .debug_loc offsets. */
16559 if (attr->form_is_block ())
16560 {
16561 /* Ok. */
16562 }
16563 else if (attr->form_is_section_offset ())
16564 {
16565 dwarf2_complex_location_expr_complaint ();
16566 attr = NULL;
16567 }
16568 else
16569 {
16570 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16571 "common block member");
16572 attr = NULL;
16573 }
16574 }
16575
16576 if (die->child != NULL)
16577 {
16578 struct objfile *objfile = cu->per_objfile->objfile;
16579 struct die_info *child_die;
16580 size_t n_entries = 0, size;
16581 struct common_block *common_block;
16582 struct symbol *sym;
16583
16584 for (child_die = die->child;
16585 child_die && child_die->tag;
16586 child_die = child_die->sibling)
16587 ++n_entries;
16588
16589 size = (sizeof (struct common_block)
16590 + (n_entries - 1) * sizeof (struct symbol *));
16591 common_block
16592 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16593 size);
16594 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16595 common_block->n_entries = 0;
16596
16597 for (child_die = die->child;
16598 child_die && child_die->tag;
16599 child_die = child_die->sibling)
16600 {
16601 /* Create the symbol in the DW_TAG_common_block block in the current
16602 symbol scope. */
16603 sym = new_symbol (child_die, NULL, cu);
16604 if (sym != NULL)
16605 {
16606 struct attribute *member_loc;
16607
16608 common_block->contents[common_block->n_entries++] = sym;
16609
16610 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16611 cu);
16612 if (member_loc)
16613 {
16614 /* GDB has handled this for a long time, but it is
16615 not specified by DWARF. It seems to have been
16616 emitted by gfortran at least as recently as:
16617 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16618 complaint (_("Variable in common block has "
16619 "DW_AT_data_member_location "
16620 "- DIE at %s [in module %s]"),
16621 sect_offset_str (child_die->sect_off),
16622 objfile_name (objfile));
16623
16624 if (member_loc->form_is_section_offset ())
16625 dwarf2_complex_location_expr_complaint ();
16626 else if (member_loc->form_is_constant ()
16627 || member_loc->form_is_block ())
16628 {
16629 if (attr != nullptr)
16630 mark_common_block_symbol_computed (sym, die, attr,
16631 member_loc, cu);
16632 }
16633 else
16634 dwarf2_complex_location_expr_complaint ();
16635 }
16636 }
16637 }
16638
16639 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16640 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16641 }
16642 }
16643
16644 /* Create a type for a C++ namespace. */
16645
16646 static struct type *
16647 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16648 {
16649 struct objfile *objfile = cu->per_objfile->objfile;
16650 const char *previous_prefix, *name;
16651 int is_anonymous;
16652 struct type *type;
16653
16654 /* For extensions, reuse the type of the original namespace. */
16655 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16656 {
16657 struct die_info *ext_die;
16658 struct dwarf2_cu *ext_cu = cu;
16659
16660 ext_die = dwarf2_extension (die, &ext_cu);
16661 type = read_type_die (ext_die, ext_cu);
16662
16663 /* EXT_CU may not be the same as CU.
16664 Ensure TYPE is recorded with CU in die_type_hash. */
16665 return set_die_type (die, type, cu);
16666 }
16667
16668 name = namespace_name (die, &is_anonymous, cu);
16669
16670 /* Now build the name of the current namespace. */
16671
16672 previous_prefix = determine_prefix (die, cu);
16673 if (previous_prefix[0] != '\0')
16674 name = typename_concat (&objfile->objfile_obstack,
16675 previous_prefix, name, 0, cu);
16676
16677 /* Create the type. */
16678 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16679
16680 return set_die_type (die, type, cu);
16681 }
16682
16683 /* Read a namespace scope. */
16684
16685 static void
16686 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16687 {
16688 struct objfile *objfile = cu->per_objfile->objfile;
16689 int is_anonymous;
16690
16691 /* Add a symbol associated to this if we haven't seen the namespace
16692 before. Also, add a using directive if it's an anonymous
16693 namespace. */
16694
16695 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16696 {
16697 struct type *type;
16698
16699 type = read_type_die (die, cu);
16700 new_symbol (die, type, cu);
16701
16702 namespace_name (die, &is_anonymous, cu);
16703 if (is_anonymous)
16704 {
16705 const char *previous_prefix = determine_prefix (die, cu);
16706
16707 std::vector<const char *> excludes;
16708 add_using_directive (using_directives (cu),
16709 previous_prefix, type->name (), NULL,
16710 NULL, excludes, 0, &objfile->objfile_obstack);
16711 }
16712 }
16713
16714 if (die->child != NULL)
16715 {
16716 struct die_info *child_die = die->child;
16717
16718 while (child_die && child_die->tag)
16719 {
16720 process_die (child_die, cu);
16721 child_die = child_die->sibling;
16722 }
16723 }
16724 }
16725
16726 /* Read a Fortran module as type. This DIE can be only a declaration used for
16727 imported module. Still we need that type as local Fortran "use ... only"
16728 declaration imports depend on the created type in determine_prefix. */
16729
16730 static struct type *
16731 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16732 {
16733 struct objfile *objfile = cu->per_objfile->objfile;
16734 const char *module_name;
16735 struct type *type;
16736
16737 module_name = dwarf2_name (die, cu);
16738 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16739
16740 return set_die_type (die, type, cu);
16741 }
16742
16743 /* Read a Fortran module. */
16744
16745 static void
16746 read_module (struct die_info *die, struct dwarf2_cu *cu)
16747 {
16748 struct die_info *child_die = die->child;
16749 struct type *type;
16750
16751 type = read_type_die (die, cu);
16752 new_symbol (die, type, cu);
16753
16754 while (child_die && child_die->tag)
16755 {
16756 process_die (child_die, cu);
16757 child_die = child_die->sibling;
16758 }
16759 }
16760
16761 /* Return the name of the namespace represented by DIE. Set
16762 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16763 namespace. */
16764
16765 static const char *
16766 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16767 {
16768 struct die_info *current_die;
16769 const char *name = NULL;
16770
16771 /* Loop through the extensions until we find a name. */
16772
16773 for (current_die = die;
16774 current_die != NULL;
16775 current_die = dwarf2_extension (die, &cu))
16776 {
16777 /* We don't use dwarf2_name here so that we can detect the absence
16778 of a name -> anonymous namespace. */
16779 name = dwarf2_string_attr (die, DW_AT_name, cu);
16780
16781 if (name != NULL)
16782 break;
16783 }
16784
16785 /* Is it an anonymous namespace? */
16786
16787 *is_anonymous = (name == NULL);
16788 if (*is_anonymous)
16789 name = CP_ANONYMOUS_NAMESPACE_STR;
16790
16791 return name;
16792 }
16793
16794 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16795 the user defined type vector. */
16796
16797 static struct type *
16798 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16799 {
16800 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16801 struct comp_unit_head *cu_header = &cu->header;
16802 struct type *type;
16803 struct attribute *attr_byte_size;
16804 struct attribute *attr_address_class;
16805 int byte_size, addr_class;
16806 struct type *target_type;
16807
16808 target_type = die_type (die, cu);
16809
16810 /* The die_type call above may have already set the type for this DIE. */
16811 type = get_die_type (die, cu);
16812 if (type)
16813 return type;
16814
16815 type = lookup_pointer_type (target_type);
16816
16817 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16818 if (attr_byte_size)
16819 byte_size = DW_UNSND (attr_byte_size);
16820 else
16821 byte_size = cu_header->addr_size;
16822
16823 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16824 if (attr_address_class)
16825 addr_class = DW_UNSND (attr_address_class);
16826 else
16827 addr_class = DW_ADDR_none;
16828
16829 ULONGEST alignment = get_alignment (cu, die);
16830
16831 /* If the pointer size, alignment, or address class is different
16832 than the default, create a type variant marked as such and set
16833 the length accordingly. */
16834 if (TYPE_LENGTH (type) != byte_size
16835 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16836 && alignment != TYPE_RAW_ALIGN (type))
16837 || addr_class != DW_ADDR_none)
16838 {
16839 if (gdbarch_address_class_type_flags_p (gdbarch))
16840 {
16841 int type_flags;
16842
16843 type_flags = gdbarch_address_class_type_flags
16844 (gdbarch, byte_size, addr_class);
16845 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16846 == 0);
16847 type = make_type_with_address_space (type, type_flags);
16848 }
16849 else if (TYPE_LENGTH (type) != byte_size)
16850 {
16851 complaint (_("invalid pointer size %d"), byte_size);
16852 }
16853 else if (TYPE_RAW_ALIGN (type) != alignment)
16854 {
16855 complaint (_("Invalid DW_AT_alignment"
16856 " - DIE at %s [in module %s]"),
16857 sect_offset_str (die->sect_off),
16858 objfile_name (cu->per_objfile->objfile));
16859 }
16860 else
16861 {
16862 /* Should we also complain about unhandled address classes? */
16863 }
16864 }
16865
16866 TYPE_LENGTH (type) = byte_size;
16867 set_type_align (type, alignment);
16868 return set_die_type (die, type, cu);
16869 }
16870
16871 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16872 the user defined type vector. */
16873
16874 static struct type *
16875 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16876 {
16877 struct type *type;
16878 struct type *to_type;
16879 struct type *domain;
16880
16881 to_type = die_type (die, cu);
16882 domain = die_containing_type (die, cu);
16883
16884 /* The calls above may have already set the type for this DIE. */
16885 type = get_die_type (die, cu);
16886 if (type)
16887 return type;
16888
16889 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16890 type = lookup_methodptr_type (to_type);
16891 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16892 {
16893 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16894
16895 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16896 to_type->fields (), to_type->num_fields (),
16897 TYPE_VARARGS (to_type));
16898 type = lookup_methodptr_type (new_type);
16899 }
16900 else
16901 type = lookup_memberptr_type (to_type, domain);
16902
16903 return set_die_type (die, type, cu);
16904 }
16905
16906 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16907 the user defined type vector. */
16908
16909 static struct type *
16910 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16911 enum type_code refcode)
16912 {
16913 struct comp_unit_head *cu_header = &cu->header;
16914 struct type *type, *target_type;
16915 struct attribute *attr;
16916
16917 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16918
16919 target_type = die_type (die, cu);
16920
16921 /* The die_type call above may have already set the type for this DIE. */
16922 type = get_die_type (die, cu);
16923 if (type)
16924 return type;
16925
16926 type = lookup_reference_type (target_type, refcode);
16927 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16928 if (attr != nullptr)
16929 {
16930 TYPE_LENGTH (type) = DW_UNSND (attr);
16931 }
16932 else
16933 {
16934 TYPE_LENGTH (type) = cu_header->addr_size;
16935 }
16936 maybe_set_alignment (cu, die, type);
16937 return set_die_type (die, type, cu);
16938 }
16939
16940 /* Add the given cv-qualifiers to the element type of the array. GCC
16941 outputs DWARF type qualifiers that apply to an array, not the
16942 element type. But GDB relies on the array element type to carry
16943 the cv-qualifiers. This mimics section 6.7.3 of the C99
16944 specification. */
16945
16946 static struct type *
16947 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16948 struct type *base_type, int cnst, int voltl)
16949 {
16950 struct type *el_type, *inner_array;
16951
16952 base_type = copy_type (base_type);
16953 inner_array = base_type;
16954
16955 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16956 {
16957 TYPE_TARGET_TYPE (inner_array) =
16958 copy_type (TYPE_TARGET_TYPE (inner_array));
16959 inner_array = TYPE_TARGET_TYPE (inner_array);
16960 }
16961
16962 el_type = TYPE_TARGET_TYPE (inner_array);
16963 cnst |= TYPE_CONST (el_type);
16964 voltl |= TYPE_VOLATILE (el_type);
16965 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16966
16967 return set_die_type (die, base_type, cu);
16968 }
16969
16970 static struct type *
16971 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16972 {
16973 struct type *base_type, *cv_type;
16974
16975 base_type = die_type (die, cu);
16976
16977 /* The die_type call above may have already set the type for this DIE. */
16978 cv_type = get_die_type (die, cu);
16979 if (cv_type)
16980 return cv_type;
16981
16982 /* In case the const qualifier is applied to an array type, the element type
16983 is so qualified, not the array type (section 6.7.3 of C99). */
16984 if (base_type->code () == TYPE_CODE_ARRAY)
16985 return add_array_cv_type (die, cu, base_type, 1, 0);
16986
16987 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16988 return set_die_type (die, cv_type, cu);
16989 }
16990
16991 static struct type *
16992 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16993 {
16994 struct type *base_type, *cv_type;
16995
16996 base_type = die_type (die, cu);
16997
16998 /* The die_type call above may have already set the type for this DIE. */
16999 cv_type = get_die_type (die, cu);
17000 if (cv_type)
17001 return cv_type;
17002
17003 /* In case the volatile qualifier is applied to an array type, the
17004 element type is so qualified, not the array type (section 6.7.3
17005 of C99). */
17006 if (base_type->code () == TYPE_CODE_ARRAY)
17007 return add_array_cv_type (die, cu, base_type, 0, 1);
17008
17009 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17010 return set_die_type (die, cv_type, cu);
17011 }
17012
17013 /* Handle DW_TAG_restrict_type. */
17014
17015 static struct type *
17016 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17017 {
17018 struct type *base_type, *cv_type;
17019
17020 base_type = die_type (die, cu);
17021
17022 /* The die_type call above may have already set the type for this DIE. */
17023 cv_type = get_die_type (die, cu);
17024 if (cv_type)
17025 return cv_type;
17026
17027 cv_type = make_restrict_type (base_type);
17028 return set_die_type (die, cv_type, cu);
17029 }
17030
17031 /* Handle DW_TAG_atomic_type. */
17032
17033 static struct type *
17034 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17035 {
17036 struct type *base_type, *cv_type;
17037
17038 base_type = die_type (die, cu);
17039
17040 /* The die_type call above may have already set the type for this DIE. */
17041 cv_type = get_die_type (die, cu);
17042 if (cv_type)
17043 return cv_type;
17044
17045 cv_type = make_atomic_type (base_type);
17046 return set_die_type (die, cv_type, cu);
17047 }
17048
17049 /* Extract all information from a DW_TAG_string_type DIE and add to
17050 the user defined type vector. It isn't really a user defined type,
17051 but it behaves like one, with other DIE's using an AT_user_def_type
17052 attribute to reference it. */
17053
17054 static struct type *
17055 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17056 {
17057 struct objfile *objfile = cu->per_objfile->objfile;
17058 struct gdbarch *gdbarch = objfile->arch ();
17059 struct type *type, *range_type, *index_type, *char_type;
17060 struct attribute *attr;
17061 struct dynamic_prop prop;
17062 bool length_is_constant = true;
17063 LONGEST length;
17064
17065 /* There are a couple of places where bit sizes might be made use of
17066 when parsing a DW_TAG_string_type, however, no producer that we know
17067 of make use of these. Handling bit sizes that are a multiple of the
17068 byte size is easy enough, but what about other bit sizes? Lets deal
17069 with that problem when we have to. Warn about these attributes being
17070 unsupported, then parse the type and ignore them like we always
17071 have. */
17072 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17073 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17074 {
17075 static bool warning_printed = false;
17076 if (!warning_printed)
17077 {
17078 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17079 "currently supported on DW_TAG_string_type."));
17080 warning_printed = true;
17081 }
17082 }
17083
17084 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17085 if (attr != nullptr && !attr->form_is_constant ())
17086 {
17087 /* The string length describes the location at which the length of
17088 the string can be found. The size of the length field can be
17089 specified with one of the attributes below. */
17090 struct type *prop_type;
17091 struct attribute *len
17092 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17093 if (len == nullptr)
17094 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17095 if (len != nullptr && len->form_is_constant ())
17096 {
17097 /* Pass 0 as the default as we know this attribute is constant
17098 and the default value will not be returned. */
17099 LONGEST sz = len->constant_value (0);
17100 prop_type = cu->per_objfile->int_type (sz, true);
17101 }
17102 else
17103 {
17104 /* If the size is not specified then we assume it is the size of
17105 an address on this target. */
17106 prop_type = cu->addr_sized_int_type (true);
17107 }
17108
17109 /* Convert the attribute into a dynamic property. */
17110 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17111 length = 1;
17112 else
17113 length_is_constant = false;
17114 }
17115 else if (attr != nullptr)
17116 {
17117 /* This DW_AT_string_length just contains the length with no
17118 indirection. There's no need to create a dynamic property in this
17119 case. Pass 0 for the default value as we know it will not be
17120 returned in this case. */
17121 length = attr->constant_value (0);
17122 }
17123 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17124 {
17125 /* We don't currently support non-constant byte sizes for strings. */
17126 length = attr->constant_value (1);
17127 }
17128 else
17129 {
17130 /* Use 1 as a fallback length if we have nothing else. */
17131 length = 1;
17132 }
17133
17134 index_type = objfile_type (objfile)->builtin_int;
17135 if (length_is_constant)
17136 range_type = create_static_range_type (NULL, index_type, 1, length);
17137 else
17138 {
17139 struct dynamic_prop low_bound;
17140
17141 low_bound.kind = PROP_CONST;
17142 low_bound.data.const_val = 1;
17143 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17144 }
17145 char_type = language_string_char_type (cu->language_defn, gdbarch);
17146 type = create_string_type (NULL, char_type, range_type);
17147
17148 return set_die_type (die, type, cu);
17149 }
17150
17151 /* Assuming that DIE corresponds to a function, returns nonzero
17152 if the function is prototyped. */
17153
17154 static int
17155 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17156 {
17157 struct attribute *attr;
17158
17159 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17160 if (attr && (DW_UNSND (attr) != 0))
17161 return 1;
17162
17163 /* The DWARF standard implies that the DW_AT_prototyped attribute
17164 is only meaningful for C, but the concept also extends to other
17165 languages that allow unprototyped functions (Eg: Objective C).
17166 For all other languages, assume that functions are always
17167 prototyped. */
17168 if (cu->language != language_c
17169 && cu->language != language_objc
17170 && cu->language != language_opencl)
17171 return 1;
17172
17173 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17174 prototyped and unprototyped functions; default to prototyped,
17175 since that is more common in modern code (and RealView warns
17176 about unprototyped functions). */
17177 if (producer_is_realview (cu->producer))
17178 return 1;
17179
17180 return 0;
17181 }
17182
17183 /* Handle DIES due to C code like:
17184
17185 struct foo
17186 {
17187 int (*funcp)(int a, long l);
17188 int b;
17189 };
17190
17191 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17192
17193 static struct type *
17194 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17195 {
17196 struct objfile *objfile = cu->per_objfile->objfile;
17197 struct type *type; /* Type that this function returns. */
17198 struct type *ftype; /* Function that returns above type. */
17199 struct attribute *attr;
17200
17201 type = die_type (die, cu);
17202
17203 /* The die_type call above may have already set the type for this DIE. */
17204 ftype = get_die_type (die, cu);
17205 if (ftype)
17206 return ftype;
17207
17208 ftype = lookup_function_type (type);
17209
17210 if (prototyped_function_p (die, cu))
17211 TYPE_PROTOTYPED (ftype) = 1;
17212
17213 /* Store the calling convention in the type if it's available in
17214 the subroutine die. Otherwise set the calling convention to
17215 the default value DW_CC_normal. */
17216 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17217 if (attr != nullptr
17218 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17219 TYPE_CALLING_CONVENTION (ftype)
17220 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17221 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17222 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17223 else
17224 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17225
17226 /* Record whether the function returns normally to its caller or not
17227 if the DWARF producer set that information. */
17228 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17229 if (attr && (DW_UNSND (attr) != 0))
17230 TYPE_NO_RETURN (ftype) = 1;
17231
17232 /* We need to add the subroutine type to the die immediately so
17233 we don't infinitely recurse when dealing with parameters
17234 declared as the same subroutine type. */
17235 set_die_type (die, ftype, cu);
17236
17237 if (die->child != NULL)
17238 {
17239 struct type *void_type = objfile_type (objfile)->builtin_void;
17240 struct die_info *child_die;
17241 int nparams, iparams;
17242
17243 /* Count the number of parameters.
17244 FIXME: GDB currently ignores vararg functions, but knows about
17245 vararg member functions. */
17246 nparams = 0;
17247 child_die = die->child;
17248 while (child_die && child_die->tag)
17249 {
17250 if (child_die->tag == DW_TAG_formal_parameter)
17251 nparams++;
17252 else if (child_die->tag == DW_TAG_unspecified_parameters)
17253 TYPE_VARARGS (ftype) = 1;
17254 child_die = child_die->sibling;
17255 }
17256
17257 /* Allocate storage for parameters and fill them in. */
17258 ftype->set_num_fields (nparams);
17259 ftype->set_fields
17260 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17261
17262 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17263 even if we error out during the parameters reading below. */
17264 for (iparams = 0; iparams < nparams; iparams++)
17265 ftype->field (iparams).set_type (void_type);
17266
17267 iparams = 0;
17268 child_die = die->child;
17269 while (child_die && child_die->tag)
17270 {
17271 if (child_die->tag == DW_TAG_formal_parameter)
17272 {
17273 struct type *arg_type;
17274
17275 /* DWARF version 2 has no clean way to discern C++
17276 static and non-static member functions. G++ helps
17277 GDB by marking the first parameter for non-static
17278 member functions (which is the this pointer) as
17279 artificial. We pass this information to
17280 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17281
17282 DWARF version 3 added DW_AT_object_pointer, which GCC
17283 4.5 does not yet generate. */
17284 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17285 if (attr != nullptr)
17286 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17287 else
17288 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17289 arg_type = die_type (child_die, cu);
17290
17291 /* RealView does not mark THIS as const, which the testsuite
17292 expects. GCC marks THIS as const in method definitions,
17293 but not in the class specifications (GCC PR 43053). */
17294 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17295 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17296 {
17297 int is_this = 0;
17298 struct dwarf2_cu *arg_cu = cu;
17299 const char *name = dwarf2_name (child_die, cu);
17300
17301 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17302 if (attr != nullptr)
17303 {
17304 /* If the compiler emits this, use it. */
17305 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17306 is_this = 1;
17307 }
17308 else if (name && strcmp (name, "this") == 0)
17309 /* Function definitions will have the argument names. */
17310 is_this = 1;
17311 else if (name == NULL && iparams == 0)
17312 /* Declarations may not have the names, so like
17313 elsewhere in GDB, assume an artificial first
17314 argument is "this". */
17315 is_this = 1;
17316
17317 if (is_this)
17318 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17319 arg_type, 0);
17320 }
17321
17322 ftype->field (iparams).set_type (arg_type);
17323 iparams++;
17324 }
17325 child_die = child_die->sibling;
17326 }
17327 }
17328
17329 return ftype;
17330 }
17331
17332 static struct type *
17333 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17334 {
17335 struct objfile *objfile = cu->per_objfile->objfile;
17336 const char *name = NULL;
17337 struct type *this_type, *target_type;
17338
17339 name = dwarf2_full_name (NULL, die, cu);
17340 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17341 TYPE_TARGET_STUB (this_type) = 1;
17342 set_die_type (die, this_type, cu);
17343 target_type = die_type (die, cu);
17344 if (target_type != this_type)
17345 TYPE_TARGET_TYPE (this_type) = target_type;
17346 else
17347 {
17348 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17349 spec and cause infinite loops in GDB. */
17350 complaint (_("Self-referential DW_TAG_typedef "
17351 "- DIE at %s [in module %s]"),
17352 sect_offset_str (die->sect_off), objfile_name (objfile));
17353 TYPE_TARGET_TYPE (this_type) = NULL;
17354 }
17355 if (name == NULL)
17356 {
17357 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17358 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17359 Handle these by just returning the target type, rather than
17360 constructing an anonymous typedef type and trying to handle this
17361 elsewhere. */
17362 set_die_type (die, target_type, cu);
17363 return target_type;
17364 }
17365 return this_type;
17366 }
17367
17368 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17369 (which may be different from NAME) to the architecture back-end to allow
17370 it to guess the correct format if necessary. */
17371
17372 static struct type *
17373 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17374 const char *name_hint, enum bfd_endian byte_order)
17375 {
17376 struct gdbarch *gdbarch = objfile->arch ();
17377 const struct floatformat **format;
17378 struct type *type;
17379
17380 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17381 if (format)
17382 type = init_float_type (objfile, bits, name, format, byte_order);
17383 else
17384 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17385
17386 return type;
17387 }
17388
17389 /* Allocate an integer type of size BITS and name NAME. */
17390
17391 static struct type *
17392 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17393 int bits, int unsigned_p, const char *name)
17394 {
17395 struct type *type;
17396
17397 /* Versions of Intel's C Compiler generate an integer type called "void"
17398 instead of using DW_TAG_unspecified_type. This has been seen on
17399 at least versions 14, 17, and 18. */
17400 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17401 && strcmp (name, "void") == 0)
17402 type = objfile_type (objfile)->builtin_void;
17403 else
17404 type = init_integer_type (objfile, bits, unsigned_p, name);
17405
17406 return type;
17407 }
17408
17409 /* Initialise and return a floating point type of size BITS suitable for
17410 use as a component of a complex number. The NAME_HINT is passed through
17411 when initialising the floating point type and is the name of the complex
17412 type.
17413
17414 As DWARF doesn't currently provide an explicit name for the components
17415 of a complex number, but it can be helpful to have these components
17416 named, we try to select a suitable name based on the size of the
17417 component. */
17418 static struct type *
17419 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17420 struct objfile *objfile,
17421 int bits, const char *name_hint,
17422 enum bfd_endian byte_order)
17423 {
17424 gdbarch *gdbarch = objfile->arch ();
17425 struct type *tt = nullptr;
17426
17427 /* Try to find a suitable floating point builtin type of size BITS.
17428 We're going to use the name of this type as the name for the complex
17429 target type that we are about to create. */
17430 switch (cu->language)
17431 {
17432 case language_fortran:
17433 switch (bits)
17434 {
17435 case 32:
17436 tt = builtin_f_type (gdbarch)->builtin_real;
17437 break;
17438 case 64:
17439 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17440 break;
17441 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17442 case 128:
17443 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17444 break;
17445 }
17446 break;
17447 default:
17448 switch (bits)
17449 {
17450 case 32:
17451 tt = builtin_type (gdbarch)->builtin_float;
17452 break;
17453 case 64:
17454 tt = builtin_type (gdbarch)->builtin_double;
17455 break;
17456 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17457 case 128:
17458 tt = builtin_type (gdbarch)->builtin_long_double;
17459 break;
17460 }
17461 break;
17462 }
17463
17464 /* If the type we found doesn't match the size we were looking for, then
17465 pretend we didn't find a type at all, the complex target type we
17466 create will then be nameless. */
17467 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17468 tt = nullptr;
17469
17470 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17471 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17472 }
17473
17474 /* Find a representation of a given base type and install
17475 it in the TYPE field of the die. */
17476
17477 static struct type *
17478 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17479 {
17480 struct objfile *objfile = cu->per_objfile->objfile;
17481 struct type *type;
17482 struct attribute *attr;
17483 int encoding = 0, bits = 0;
17484 const char *name;
17485 gdbarch *arch;
17486
17487 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17488 if (attr != nullptr)
17489 encoding = DW_UNSND (attr);
17490 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17491 if (attr != nullptr)
17492 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17493 name = dwarf2_name (die, cu);
17494 if (!name)
17495 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17496
17497 arch = objfile->arch ();
17498 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17499
17500 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17501 if (attr)
17502 {
17503 int endianity = DW_UNSND (attr);
17504
17505 switch (endianity)
17506 {
17507 case DW_END_big:
17508 byte_order = BFD_ENDIAN_BIG;
17509 break;
17510 case DW_END_little:
17511 byte_order = BFD_ENDIAN_LITTLE;
17512 break;
17513 default:
17514 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17515 break;
17516 }
17517 }
17518
17519 switch (encoding)
17520 {
17521 case DW_ATE_address:
17522 /* Turn DW_ATE_address into a void * pointer. */
17523 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17524 type = init_pointer_type (objfile, bits, name, type);
17525 break;
17526 case DW_ATE_boolean:
17527 type = init_boolean_type (objfile, bits, 1, name);
17528 break;
17529 case DW_ATE_complex_float:
17530 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17531 byte_order);
17532 if (type->code () == TYPE_CODE_ERROR)
17533 {
17534 if (name == nullptr)
17535 {
17536 struct obstack *obstack
17537 = &cu->per_objfile->objfile->objfile_obstack;
17538 name = obconcat (obstack, "_Complex ", type->name (),
17539 nullptr);
17540 }
17541 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17542 }
17543 else
17544 type = init_complex_type (name, type);
17545 break;
17546 case DW_ATE_decimal_float:
17547 type = init_decfloat_type (objfile, bits, name);
17548 break;
17549 case DW_ATE_float:
17550 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17551 break;
17552 case DW_ATE_signed:
17553 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17554 break;
17555 case DW_ATE_unsigned:
17556 if (cu->language == language_fortran
17557 && name
17558 && startswith (name, "character("))
17559 type = init_character_type (objfile, bits, 1, name);
17560 else
17561 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17562 break;
17563 case DW_ATE_signed_char:
17564 if (cu->language == language_ada || cu->language == language_m2
17565 || cu->language == language_pascal
17566 || cu->language == language_fortran)
17567 type = init_character_type (objfile, bits, 0, name);
17568 else
17569 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17570 break;
17571 case DW_ATE_unsigned_char:
17572 if (cu->language == language_ada || cu->language == language_m2
17573 || cu->language == language_pascal
17574 || cu->language == language_fortran
17575 || cu->language == language_rust)
17576 type = init_character_type (objfile, bits, 1, name);
17577 else
17578 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17579 break;
17580 case DW_ATE_UTF:
17581 {
17582 if (bits == 16)
17583 type = builtin_type (arch)->builtin_char16;
17584 else if (bits == 32)
17585 type = builtin_type (arch)->builtin_char32;
17586 else
17587 {
17588 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17589 bits);
17590 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17591 }
17592 return set_die_type (die, type, cu);
17593 }
17594 break;
17595
17596 default:
17597 complaint (_("unsupported DW_AT_encoding: '%s'"),
17598 dwarf_type_encoding_name (encoding));
17599 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17600 break;
17601 }
17602
17603 if (name && strcmp (name, "char") == 0)
17604 TYPE_NOSIGN (type) = 1;
17605
17606 maybe_set_alignment (cu, die, type);
17607
17608 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17609
17610 return set_die_type (die, type, cu);
17611 }
17612
17613 /* Parse dwarf attribute if it's a block, reference or constant and put the
17614 resulting value of the attribute into struct bound_prop.
17615 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17616
17617 static int
17618 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17619 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17620 struct type *default_type)
17621 {
17622 struct dwarf2_property_baton *baton;
17623 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17624 struct objfile *objfile = per_objfile->objfile;
17625 struct obstack *obstack = &objfile->objfile_obstack;
17626
17627 gdb_assert (default_type != NULL);
17628
17629 if (attr == NULL || prop == NULL)
17630 return 0;
17631
17632 if (attr->form_is_block ())
17633 {
17634 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17635 baton->property_type = default_type;
17636 baton->locexpr.per_cu = cu->per_cu;
17637 baton->locexpr.per_objfile = per_objfile;
17638 baton->locexpr.size = DW_BLOCK (attr)->size;
17639 baton->locexpr.data = DW_BLOCK (attr)->data;
17640 switch (attr->name)
17641 {
17642 case DW_AT_string_length:
17643 baton->locexpr.is_reference = true;
17644 break;
17645 default:
17646 baton->locexpr.is_reference = false;
17647 break;
17648 }
17649 prop->data.baton = baton;
17650 prop->kind = PROP_LOCEXPR;
17651 gdb_assert (prop->data.baton != NULL);
17652 }
17653 else if (attr->form_is_ref ())
17654 {
17655 struct dwarf2_cu *target_cu = cu;
17656 struct die_info *target_die;
17657 struct attribute *target_attr;
17658
17659 target_die = follow_die_ref (die, attr, &target_cu);
17660 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17661 if (target_attr == NULL)
17662 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17663 target_cu);
17664 if (target_attr == NULL)
17665 return 0;
17666
17667 switch (target_attr->name)
17668 {
17669 case DW_AT_location:
17670 if (target_attr->form_is_section_offset ())
17671 {
17672 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17673 baton->property_type = die_type (target_die, target_cu);
17674 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17675 prop->data.baton = baton;
17676 prop->kind = PROP_LOCLIST;
17677 gdb_assert (prop->data.baton != NULL);
17678 }
17679 else if (target_attr->form_is_block ())
17680 {
17681 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17682 baton->property_type = die_type (target_die, target_cu);
17683 baton->locexpr.per_cu = cu->per_cu;
17684 baton->locexpr.per_objfile = per_objfile;
17685 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17686 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17687 baton->locexpr.is_reference = true;
17688 prop->data.baton = baton;
17689 prop->kind = PROP_LOCEXPR;
17690 gdb_assert (prop->data.baton != NULL);
17691 }
17692 else
17693 {
17694 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17695 "dynamic property");
17696 return 0;
17697 }
17698 break;
17699 case DW_AT_data_member_location:
17700 {
17701 LONGEST offset;
17702
17703 if (!handle_data_member_location (target_die, target_cu,
17704 &offset))
17705 return 0;
17706
17707 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17708 baton->property_type = read_type_die (target_die->parent,
17709 target_cu);
17710 baton->offset_info.offset = offset;
17711 baton->offset_info.type = die_type (target_die, target_cu);
17712 prop->data.baton = baton;
17713 prop->kind = PROP_ADDR_OFFSET;
17714 break;
17715 }
17716 }
17717 }
17718 else if (attr->form_is_constant ())
17719 {
17720 prop->data.const_val = attr->constant_value (0);
17721 prop->kind = PROP_CONST;
17722 }
17723 else
17724 {
17725 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17726 dwarf2_name (die, cu));
17727 return 0;
17728 }
17729
17730 return 1;
17731 }
17732
17733 /* See read.h. */
17734
17735 struct type *
17736 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17737 {
17738 struct type *int_type;
17739
17740 /* Helper macro to examine the various builtin types. */
17741 #define TRY_TYPE(F) \
17742 int_type = (unsigned_p \
17743 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17744 : objfile_type (objfile)->builtin_ ## F); \
17745 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17746 return int_type
17747
17748 TRY_TYPE (char);
17749 TRY_TYPE (short);
17750 TRY_TYPE (int);
17751 TRY_TYPE (long);
17752 TRY_TYPE (long_long);
17753
17754 #undef TRY_TYPE
17755
17756 gdb_assert_not_reached ("unable to find suitable integer type");
17757 }
17758
17759 /* See read.h. */
17760
17761 struct type *
17762 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17763 {
17764 int addr_size = this->per_cu->addr_size ();
17765 return this->per_objfile->int_type (addr_size, unsigned_p);
17766 }
17767
17768 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17769 present (which is valid) then compute the default type based on the
17770 compilation units address size. */
17771
17772 static struct type *
17773 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17774 {
17775 struct type *index_type = die_type (die, cu);
17776
17777 /* Dwarf-2 specifications explicitly allows to create subrange types
17778 without specifying a base type.
17779 In that case, the base type must be set to the type of
17780 the lower bound, upper bound or count, in that order, if any of these
17781 three attributes references an object that has a type.
17782 If no base type is found, the Dwarf-2 specifications say that
17783 a signed integer type of size equal to the size of an address should
17784 be used.
17785 For the following C code: `extern char gdb_int [];'
17786 GCC produces an empty range DIE.
17787 FIXME: muller/2010-05-28: Possible references to object for low bound,
17788 high bound or count are not yet handled by this code. */
17789 if (index_type->code () == TYPE_CODE_VOID)
17790 index_type = cu->addr_sized_int_type (false);
17791
17792 return index_type;
17793 }
17794
17795 /* Read the given DW_AT_subrange DIE. */
17796
17797 static struct type *
17798 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17799 {
17800 struct type *base_type, *orig_base_type;
17801 struct type *range_type;
17802 struct attribute *attr;
17803 struct dynamic_prop low, high;
17804 int low_default_is_valid;
17805 int high_bound_is_count = 0;
17806 const char *name;
17807 ULONGEST negative_mask;
17808
17809 orig_base_type = read_subrange_index_type (die, cu);
17810
17811 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17812 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17813 creating the range type, but we use the result of check_typedef
17814 when examining properties of the type. */
17815 base_type = check_typedef (orig_base_type);
17816
17817 /* The die_type call above may have already set the type for this DIE. */
17818 range_type = get_die_type (die, cu);
17819 if (range_type)
17820 return range_type;
17821
17822 low.kind = PROP_CONST;
17823 high.kind = PROP_CONST;
17824 high.data.const_val = 0;
17825
17826 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17827 omitting DW_AT_lower_bound. */
17828 switch (cu->language)
17829 {
17830 case language_c:
17831 case language_cplus:
17832 low.data.const_val = 0;
17833 low_default_is_valid = 1;
17834 break;
17835 case language_fortran:
17836 low.data.const_val = 1;
17837 low_default_is_valid = 1;
17838 break;
17839 case language_d:
17840 case language_objc:
17841 case language_rust:
17842 low.data.const_val = 0;
17843 low_default_is_valid = (cu->header.version >= 4);
17844 break;
17845 case language_ada:
17846 case language_m2:
17847 case language_pascal:
17848 low.data.const_val = 1;
17849 low_default_is_valid = (cu->header.version >= 4);
17850 break;
17851 default:
17852 low.data.const_val = 0;
17853 low_default_is_valid = 0;
17854 break;
17855 }
17856
17857 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17858 if (attr != nullptr)
17859 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17860 else if (!low_default_is_valid)
17861 complaint (_("Missing DW_AT_lower_bound "
17862 "- DIE at %s [in module %s]"),
17863 sect_offset_str (die->sect_off),
17864 objfile_name (cu->per_objfile->objfile));
17865
17866 struct attribute *attr_ub, *attr_count;
17867 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17868 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17869 {
17870 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17871 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17872 {
17873 /* If bounds are constant do the final calculation here. */
17874 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17875 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17876 else
17877 high_bound_is_count = 1;
17878 }
17879 else
17880 {
17881 if (attr_ub != NULL)
17882 complaint (_("Unresolved DW_AT_upper_bound "
17883 "- DIE at %s [in module %s]"),
17884 sect_offset_str (die->sect_off),
17885 objfile_name (cu->per_objfile->objfile));
17886 if (attr_count != NULL)
17887 complaint (_("Unresolved DW_AT_count "
17888 "- DIE at %s [in module %s]"),
17889 sect_offset_str (die->sect_off),
17890 objfile_name (cu->per_objfile->objfile));
17891 }
17892 }
17893
17894 LONGEST bias = 0;
17895 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17896 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17897 bias = bias_attr->constant_value (0);
17898
17899 /* Normally, the DWARF producers are expected to use a signed
17900 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17901 But this is unfortunately not always the case, as witnessed
17902 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17903 is used instead. To work around that ambiguity, we treat
17904 the bounds as signed, and thus sign-extend their values, when
17905 the base type is signed. */
17906 negative_mask =
17907 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17908 if (low.kind == PROP_CONST
17909 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17910 low.data.const_val |= negative_mask;
17911 if (high.kind == PROP_CONST
17912 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17913 high.data.const_val |= negative_mask;
17914
17915 /* Check for bit and byte strides. */
17916 struct dynamic_prop byte_stride_prop;
17917 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17918 if (attr_byte_stride != nullptr)
17919 {
17920 struct type *prop_type = cu->addr_sized_int_type (false);
17921 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17922 prop_type);
17923 }
17924
17925 struct dynamic_prop bit_stride_prop;
17926 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17927 if (attr_bit_stride != nullptr)
17928 {
17929 /* It only makes sense to have either a bit or byte stride. */
17930 if (attr_byte_stride != nullptr)
17931 {
17932 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17933 "- DIE at %s [in module %s]"),
17934 sect_offset_str (die->sect_off),
17935 objfile_name (cu->per_objfile->objfile));
17936 attr_bit_stride = nullptr;
17937 }
17938 else
17939 {
17940 struct type *prop_type = cu->addr_sized_int_type (false);
17941 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17942 prop_type);
17943 }
17944 }
17945
17946 if (attr_byte_stride != nullptr
17947 || attr_bit_stride != nullptr)
17948 {
17949 bool byte_stride_p = (attr_byte_stride != nullptr);
17950 struct dynamic_prop *stride
17951 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17952
17953 range_type
17954 = create_range_type_with_stride (NULL, orig_base_type, &low,
17955 &high, bias, stride, byte_stride_p);
17956 }
17957 else
17958 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17959
17960 if (high_bound_is_count)
17961 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17962
17963 /* Ada expects an empty array on no boundary attributes. */
17964 if (attr == NULL && cu->language != language_ada)
17965 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17966
17967 name = dwarf2_name (die, cu);
17968 if (name)
17969 range_type->set_name (name);
17970
17971 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17972 if (attr != nullptr)
17973 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17974
17975 maybe_set_alignment (cu, die, range_type);
17976
17977 set_die_type (die, range_type, cu);
17978
17979 /* set_die_type should be already done. */
17980 set_descriptive_type (range_type, die, cu);
17981
17982 return range_type;
17983 }
17984
17985 static struct type *
17986 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17987 {
17988 struct type *type;
17989
17990 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17991 type->set_name (dwarf2_name (die, cu));
17992
17993 /* In Ada, an unspecified type is typically used when the description
17994 of the type is deferred to a different unit. When encountering
17995 such a type, we treat it as a stub, and try to resolve it later on,
17996 when needed. */
17997 if (cu->language == language_ada)
17998 TYPE_STUB (type) = 1;
17999
18000 return set_die_type (die, type, cu);
18001 }
18002
18003 /* Read a single die and all its descendents. Set the die's sibling
18004 field to NULL; set other fields in the die correctly, and set all
18005 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18006 location of the info_ptr after reading all of those dies. PARENT
18007 is the parent of the die in question. */
18008
18009 static struct die_info *
18010 read_die_and_children (const struct die_reader_specs *reader,
18011 const gdb_byte *info_ptr,
18012 const gdb_byte **new_info_ptr,
18013 struct die_info *parent)
18014 {
18015 struct die_info *die;
18016 const gdb_byte *cur_ptr;
18017
18018 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18019 if (die == NULL)
18020 {
18021 *new_info_ptr = cur_ptr;
18022 return NULL;
18023 }
18024 store_in_ref_table (die, reader->cu);
18025
18026 if (die->has_children)
18027 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18028 else
18029 {
18030 die->child = NULL;
18031 *new_info_ptr = cur_ptr;
18032 }
18033
18034 die->sibling = NULL;
18035 die->parent = parent;
18036 return die;
18037 }
18038
18039 /* Read a die, all of its descendents, and all of its siblings; set
18040 all of the fields of all of the dies correctly. Arguments are as
18041 in read_die_and_children. */
18042
18043 static struct die_info *
18044 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18045 const gdb_byte *info_ptr,
18046 const gdb_byte **new_info_ptr,
18047 struct die_info *parent)
18048 {
18049 struct die_info *first_die, *last_sibling;
18050 const gdb_byte *cur_ptr;
18051
18052 cur_ptr = info_ptr;
18053 first_die = last_sibling = NULL;
18054
18055 while (1)
18056 {
18057 struct die_info *die
18058 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18059
18060 if (die == NULL)
18061 {
18062 *new_info_ptr = cur_ptr;
18063 return first_die;
18064 }
18065
18066 if (!first_die)
18067 first_die = die;
18068 else
18069 last_sibling->sibling = die;
18070
18071 last_sibling = die;
18072 }
18073 }
18074
18075 /* Read a die, all of its descendents, and all of its siblings; set
18076 all of the fields of all of the dies correctly. Arguments are as
18077 in read_die_and_children.
18078 This the main entry point for reading a DIE and all its children. */
18079
18080 static struct die_info *
18081 read_die_and_siblings (const struct die_reader_specs *reader,
18082 const gdb_byte *info_ptr,
18083 const gdb_byte **new_info_ptr,
18084 struct die_info *parent)
18085 {
18086 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18087 new_info_ptr, parent);
18088
18089 if (dwarf_die_debug)
18090 {
18091 fprintf_unfiltered (gdb_stdlog,
18092 "Read die from %s@0x%x of %s:\n",
18093 reader->die_section->get_name (),
18094 (unsigned) (info_ptr - reader->die_section->buffer),
18095 bfd_get_filename (reader->abfd));
18096 dump_die (die, dwarf_die_debug);
18097 }
18098
18099 return die;
18100 }
18101
18102 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18103 attributes.
18104 The caller is responsible for filling in the extra attributes
18105 and updating (*DIEP)->num_attrs.
18106 Set DIEP to point to a newly allocated die with its information,
18107 except for its child, sibling, and parent fields. */
18108
18109 static const gdb_byte *
18110 read_full_die_1 (const struct die_reader_specs *reader,
18111 struct die_info **diep, const gdb_byte *info_ptr,
18112 int num_extra_attrs)
18113 {
18114 unsigned int abbrev_number, bytes_read, i;
18115 struct abbrev_info *abbrev;
18116 struct die_info *die;
18117 struct dwarf2_cu *cu = reader->cu;
18118 bfd *abfd = reader->abfd;
18119
18120 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18121 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18122 info_ptr += bytes_read;
18123 if (!abbrev_number)
18124 {
18125 *diep = NULL;
18126 return info_ptr;
18127 }
18128
18129 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18130 if (!abbrev)
18131 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18132 abbrev_number,
18133 bfd_get_filename (abfd));
18134
18135 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18136 die->sect_off = sect_off;
18137 die->tag = abbrev->tag;
18138 die->abbrev = abbrev_number;
18139 die->has_children = abbrev->has_children;
18140
18141 /* Make the result usable.
18142 The caller needs to update num_attrs after adding the extra
18143 attributes. */
18144 die->num_attrs = abbrev->num_attrs;
18145
18146 std::vector<int> indexes_that_need_reprocess;
18147 for (i = 0; i < abbrev->num_attrs; ++i)
18148 {
18149 bool need_reprocess;
18150 info_ptr =
18151 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18152 info_ptr, &need_reprocess);
18153 if (need_reprocess)
18154 indexes_that_need_reprocess.push_back (i);
18155 }
18156
18157 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18158 if (attr != nullptr)
18159 cu->str_offsets_base = DW_UNSND (attr);
18160
18161 attr = die->attr (DW_AT_loclists_base);
18162 if (attr != nullptr)
18163 cu->loclist_base = DW_UNSND (attr);
18164
18165 auto maybe_addr_base = die->addr_base ();
18166 if (maybe_addr_base.has_value ())
18167 cu->addr_base = *maybe_addr_base;
18168 for (int index : indexes_that_need_reprocess)
18169 read_attribute_reprocess (reader, &die->attrs[index]);
18170 *diep = die;
18171 return info_ptr;
18172 }
18173
18174 /* Read a die and all its attributes.
18175 Set DIEP to point to a newly allocated die with its information,
18176 except for its child, sibling, and parent fields. */
18177
18178 static const gdb_byte *
18179 read_full_die (const struct die_reader_specs *reader,
18180 struct die_info **diep, const gdb_byte *info_ptr)
18181 {
18182 const gdb_byte *result;
18183
18184 result = read_full_die_1 (reader, diep, info_ptr, 0);
18185
18186 if (dwarf_die_debug)
18187 {
18188 fprintf_unfiltered (gdb_stdlog,
18189 "Read die from %s@0x%x of %s:\n",
18190 reader->die_section->get_name (),
18191 (unsigned) (info_ptr - reader->die_section->buffer),
18192 bfd_get_filename (reader->abfd));
18193 dump_die (*diep, dwarf_die_debug);
18194 }
18195
18196 return result;
18197 }
18198 \f
18199
18200 /* Returns nonzero if TAG represents a type that we might generate a partial
18201 symbol for. */
18202
18203 static int
18204 is_type_tag_for_partial (int tag)
18205 {
18206 switch (tag)
18207 {
18208 #if 0
18209 /* Some types that would be reasonable to generate partial symbols for,
18210 that we don't at present. */
18211 case DW_TAG_array_type:
18212 case DW_TAG_file_type:
18213 case DW_TAG_ptr_to_member_type:
18214 case DW_TAG_set_type:
18215 case DW_TAG_string_type:
18216 case DW_TAG_subroutine_type:
18217 #endif
18218 case DW_TAG_base_type:
18219 case DW_TAG_class_type:
18220 case DW_TAG_interface_type:
18221 case DW_TAG_enumeration_type:
18222 case DW_TAG_structure_type:
18223 case DW_TAG_subrange_type:
18224 case DW_TAG_typedef:
18225 case DW_TAG_union_type:
18226 return 1;
18227 default:
18228 return 0;
18229 }
18230 }
18231
18232 /* Load all DIEs that are interesting for partial symbols into memory. */
18233
18234 static struct partial_die_info *
18235 load_partial_dies (const struct die_reader_specs *reader,
18236 const gdb_byte *info_ptr, int building_psymtab)
18237 {
18238 struct dwarf2_cu *cu = reader->cu;
18239 struct objfile *objfile = cu->per_objfile->objfile;
18240 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18241 unsigned int bytes_read;
18242 unsigned int load_all = 0;
18243 int nesting_level = 1;
18244
18245 parent_die = NULL;
18246 last_die = NULL;
18247
18248 gdb_assert (cu->per_cu != NULL);
18249 if (cu->per_cu->load_all_dies)
18250 load_all = 1;
18251
18252 cu->partial_dies
18253 = htab_create_alloc_ex (cu->header.length / 12,
18254 partial_die_hash,
18255 partial_die_eq,
18256 NULL,
18257 &cu->comp_unit_obstack,
18258 hashtab_obstack_allocate,
18259 dummy_obstack_deallocate);
18260
18261 while (1)
18262 {
18263 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18264
18265 /* A NULL abbrev means the end of a series of children. */
18266 if (abbrev == NULL)
18267 {
18268 if (--nesting_level == 0)
18269 return first_die;
18270
18271 info_ptr += bytes_read;
18272 last_die = parent_die;
18273 parent_die = parent_die->die_parent;
18274 continue;
18275 }
18276
18277 /* Check for template arguments. We never save these; if
18278 they're seen, we just mark the parent, and go on our way. */
18279 if (parent_die != NULL
18280 && cu->language == language_cplus
18281 && (abbrev->tag == DW_TAG_template_type_param
18282 || abbrev->tag == DW_TAG_template_value_param))
18283 {
18284 parent_die->has_template_arguments = 1;
18285
18286 if (!load_all)
18287 {
18288 /* We don't need a partial DIE for the template argument. */
18289 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18290 continue;
18291 }
18292 }
18293
18294 /* We only recurse into c++ subprograms looking for template arguments.
18295 Skip their other children. */
18296 if (!load_all
18297 && cu->language == language_cplus
18298 && parent_die != NULL
18299 && parent_die->tag == DW_TAG_subprogram
18300 && abbrev->tag != DW_TAG_inlined_subroutine)
18301 {
18302 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18303 continue;
18304 }
18305
18306 /* Check whether this DIE is interesting enough to save. Normally
18307 we would not be interested in members here, but there may be
18308 later variables referencing them via DW_AT_specification (for
18309 static members). */
18310 if (!load_all
18311 && !is_type_tag_for_partial (abbrev->tag)
18312 && abbrev->tag != DW_TAG_constant
18313 && abbrev->tag != DW_TAG_enumerator
18314 && abbrev->tag != DW_TAG_subprogram
18315 && abbrev->tag != DW_TAG_inlined_subroutine
18316 && abbrev->tag != DW_TAG_lexical_block
18317 && abbrev->tag != DW_TAG_variable
18318 && abbrev->tag != DW_TAG_namespace
18319 && abbrev->tag != DW_TAG_module
18320 && abbrev->tag != DW_TAG_member
18321 && abbrev->tag != DW_TAG_imported_unit
18322 && abbrev->tag != DW_TAG_imported_declaration)
18323 {
18324 /* Otherwise we skip to the next sibling, if any. */
18325 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18326 continue;
18327 }
18328
18329 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18330 abbrev);
18331
18332 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18333
18334 /* This two-pass algorithm for processing partial symbols has a
18335 high cost in cache pressure. Thus, handle some simple cases
18336 here which cover the majority of C partial symbols. DIEs
18337 which neither have specification tags in them, nor could have
18338 specification tags elsewhere pointing at them, can simply be
18339 processed and discarded.
18340
18341 This segment is also optional; scan_partial_symbols and
18342 add_partial_symbol will handle these DIEs if we chain
18343 them in normally. When compilers which do not emit large
18344 quantities of duplicate debug information are more common,
18345 this code can probably be removed. */
18346
18347 /* Any complete simple types at the top level (pretty much all
18348 of them, for a language without namespaces), can be processed
18349 directly. */
18350 if (parent_die == NULL
18351 && pdi.has_specification == 0
18352 && pdi.is_declaration == 0
18353 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18354 || pdi.tag == DW_TAG_base_type
18355 || pdi.tag == DW_TAG_subrange_type))
18356 {
18357 if (building_psymtab && pdi.raw_name != NULL)
18358 add_partial_symbol (&pdi, cu);
18359
18360 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18361 continue;
18362 }
18363
18364 /* The exception for DW_TAG_typedef with has_children above is
18365 a workaround of GCC PR debug/47510. In the case of this complaint
18366 type_name_or_error will error on such types later.
18367
18368 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18369 it could not find the child DIEs referenced later, this is checked
18370 above. In correct DWARF DW_TAG_typedef should have no children. */
18371
18372 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18373 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18374 "- DIE at %s [in module %s]"),
18375 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18376
18377 /* If we're at the second level, and we're an enumerator, and
18378 our parent has no specification (meaning possibly lives in a
18379 namespace elsewhere), then we can add the partial symbol now
18380 instead of queueing it. */
18381 if (pdi.tag == DW_TAG_enumerator
18382 && parent_die != NULL
18383 && parent_die->die_parent == NULL
18384 && parent_die->tag == DW_TAG_enumeration_type
18385 && parent_die->has_specification == 0)
18386 {
18387 if (pdi.raw_name == NULL)
18388 complaint (_("malformed enumerator DIE ignored"));
18389 else if (building_psymtab)
18390 add_partial_symbol (&pdi, cu);
18391
18392 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18393 continue;
18394 }
18395
18396 struct partial_die_info *part_die
18397 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18398
18399 /* We'll save this DIE so link it in. */
18400 part_die->die_parent = parent_die;
18401 part_die->die_sibling = NULL;
18402 part_die->die_child = NULL;
18403
18404 if (last_die && last_die == parent_die)
18405 last_die->die_child = part_die;
18406 else if (last_die)
18407 last_die->die_sibling = part_die;
18408
18409 last_die = part_die;
18410
18411 if (first_die == NULL)
18412 first_die = part_die;
18413
18414 /* Maybe add the DIE to the hash table. Not all DIEs that we
18415 find interesting need to be in the hash table, because we
18416 also have the parent/sibling/child chains; only those that we
18417 might refer to by offset later during partial symbol reading.
18418
18419 For now this means things that might have be the target of a
18420 DW_AT_specification, DW_AT_abstract_origin, or
18421 DW_AT_extension. DW_AT_extension will refer only to
18422 namespaces; DW_AT_abstract_origin refers to functions (and
18423 many things under the function DIE, but we do not recurse
18424 into function DIEs during partial symbol reading) and
18425 possibly variables as well; DW_AT_specification refers to
18426 declarations. Declarations ought to have the DW_AT_declaration
18427 flag. It happens that GCC forgets to put it in sometimes, but
18428 only for functions, not for types.
18429
18430 Adding more things than necessary to the hash table is harmless
18431 except for the performance cost. Adding too few will result in
18432 wasted time in find_partial_die, when we reread the compilation
18433 unit with load_all_dies set. */
18434
18435 if (load_all
18436 || abbrev->tag == DW_TAG_constant
18437 || abbrev->tag == DW_TAG_subprogram
18438 || abbrev->tag == DW_TAG_variable
18439 || abbrev->tag == DW_TAG_namespace
18440 || part_die->is_declaration)
18441 {
18442 void **slot;
18443
18444 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18445 to_underlying (part_die->sect_off),
18446 INSERT);
18447 *slot = part_die;
18448 }
18449
18450 /* For some DIEs we want to follow their children (if any). For C
18451 we have no reason to follow the children of structures; for other
18452 languages we have to, so that we can get at method physnames
18453 to infer fully qualified class names, for DW_AT_specification,
18454 and for C++ template arguments. For C++, we also look one level
18455 inside functions to find template arguments (if the name of the
18456 function does not already contain the template arguments).
18457
18458 For Ada and Fortran, we need to scan the children of subprograms
18459 and lexical blocks as well because these languages allow the
18460 definition of nested entities that could be interesting for the
18461 debugger, such as nested subprograms for instance. */
18462 if (last_die->has_children
18463 && (load_all
18464 || last_die->tag == DW_TAG_namespace
18465 || last_die->tag == DW_TAG_module
18466 || last_die->tag == DW_TAG_enumeration_type
18467 || (cu->language == language_cplus
18468 && last_die->tag == DW_TAG_subprogram
18469 && (last_die->raw_name == NULL
18470 || strchr (last_die->raw_name, '<') == NULL))
18471 || (cu->language != language_c
18472 && (last_die->tag == DW_TAG_class_type
18473 || last_die->tag == DW_TAG_interface_type
18474 || last_die->tag == DW_TAG_structure_type
18475 || last_die->tag == DW_TAG_union_type))
18476 || ((cu->language == language_ada
18477 || cu->language == language_fortran)
18478 && (last_die->tag == DW_TAG_subprogram
18479 || last_die->tag == DW_TAG_lexical_block))))
18480 {
18481 nesting_level++;
18482 parent_die = last_die;
18483 continue;
18484 }
18485
18486 /* Otherwise we skip to the next sibling, if any. */
18487 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18488
18489 /* Back to the top, do it again. */
18490 }
18491 }
18492
18493 partial_die_info::partial_die_info (sect_offset sect_off_,
18494 struct abbrev_info *abbrev)
18495 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18496 {
18497 }
18498
18499 /* See class definition. */
18500
18501 const char *
18502 partial_die_info::name (dwarf2_cu *cu)
18503 {
18504 if (!canonical_name && raw_name != nullptr)
18505 {
18506 struct objfile *objfile = cu->per_objfile->objfile;
18507 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18508 canonical_name = 1;
18509 }
18510
18511 return raw_name;
18512 }
18513
18514 /* Read a minimal amount of information into the minimal die structure.
18515 INFO_PTR should point just after the initial uleb128 of a DIE. */
18516
18517 const gdb_byte *
18518 partial_die_info::read (const struct die_reader_specs *reader,
18519 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18520 {
18521 struct dwarf2_cu *cu = reader->cu;
18522 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18523 unsigned int i;
18524 int has_low_pc_attr = 0;
18525 int has_high_pc_attr = 0;
18526 int high_pc_relative = 0;
18527
18528 for (i = 0; i < abbrev.num_attrs; ++i)
18529 {
18530 attribute attr;
18531 bool need_reprocess;
18532 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18533 info_ptr, &need_reprocess);
18534 /* String and address offsets that need to do the reprocessing have
18535 already been read at this point, so there is no need to wait until
18536 the loop terminates to do the reprocessing. */
18537 if (need_reprocess)
18538 read_attribute_reprocess (reader, &attr);
18539 /* Store the data if it is of an attribute we want to keep in a
18540 partial symbol table. */
18541 switch (attr.name)
18542 {
18543 case DW_AT_name:
18544 switch (tag)
18545 {
18546 case DW_TAG_compile_unit:
18547 case DW_TAG_partial_unit:
18548 case DW_TAG_type_unit:
18549 /* Compilation units have a DW_AT_name that is a filename, not
18550 a source language identifier. */
18551 case DW_TAG_enumeration_type:
18552 case DW_TAG_enumerator:
18553 /* These tags always have simple identifiers already; no need
18554 to canonicalize them. */
18555 canonical_name = 1;
18556 raw_name = DW_STRING (&attr);
18557 break;
18558 default:
18559 canonical_name = 0;
18560 raw_name = DW_STRING (&attr);
18561 break;
18562 }
18563 break;
18564 case DW_AT_linkage_name:
18565 case DW_AT_MIPS_linkage_name:
18566 /* Note that both forms of linkage name might appear. We
18567 assume they will be the same, and we only store the last
18568 one we see. */
18569 linkage_name = attr.value_as_string ();
18570 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18571 See https://github.com/rust-lang/rust/issues/32925. */
18572 if (cu->language == language_rust && linkage_name != NULL
18573 && strchr (linkage_name, '{') != NULL)
18574 linkage_name = NULL;
18575 break;
18576 case DW_AT_low_pc:
18577 has_low_pc_attr = 1;
18578 lowpc = attr.value_as_address ();
18579 break;
18580 case DW_AT_high_pc:
18581 has_high_pc_attr = 1;
18582 highpc = attr.value_as_address ();
18583 if (cu->header.version >= 4 && attr.form_is_constant ())
18584 high_pc_relative = 1;
18585 break;
18586 case DW_AT_location:
18587 /* Support the .debug_loc offsets. */
18588 if (attr.form_is_block ())
18589 {
18590 d.locdesc = DW_BLOCK (&attr);
18591 }
18592 else if (attr.form_is_section_offset ())
18593 {
18594 dwarf2_complex_location_expr_complaint ();
18595 }
18596 else
18597 {
18598 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18599 "partial symbol information");
18600 }
18601 break;
18602 case DW_AT_external:
18603 is_external = DW_UNSND (&attr);
18604 break;
18605 case DW_AT_declaration:
18606 is_declaration = DW_UNSND (&attr);
18607 break;
18608 case DW_AT_type:
18609 has_type = 1;
18610 break;
18611 case DW_AT_abstract_origin:
18612 case DW_AT_specification:
18613 case DW_AT_extension:
18614 has_specification = 1;
18615 spec_offset = attr.get_ref_die_offset ();
18616 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18617 || cu->per_cu->is_dwz);
18618 break;
18619 case DW_AT_sibling:
18620 /* Ignore absolute siblings, they might point outside of
18621 the current compile unit. */
18622 if (attr.form == DW_FORM_ref_addr)
18623 complaint (_("ignoring absolute DW_AT_sibling"));
18624 else
18625 {
18626 const gdb_byte *buffer = reader->buffer;
18627 sect_offset off = attr.get_ref_die_offset ();
18628 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18629
18630 if (sibling_ptr < info_ptr)
18631 complaint (_("DW_AT_sibling points backwards"));
18632 else if (sibling_ptr > reader->buffer_end)
18633 reader->die_section->overflow_complaint ();
18634 else
18635 sibling = sibling_ptr;
18636 }
18637 break;
18638 case DW_AT_byte_size:
18639 has_byte_size = 1;
18640 break;
18641 case DW_AT_const_value:
18642 has_const_value = 1;
18643 break;
18644 case DW_AT_calling_convention:
18645 /* DWARF doesn't provide a way to identify a program's source-level
18646 entry point. DW_AT_calling_convention attributes are only meant
18647 to describe functions' calling conventions.
18648
18649 However, because it's a necessary piece of information in
18650 Fortran, and before DWARF 4 DW_CC_program was the only
18651 piece of debugging information whose definition refers to
18652 a 'main program' at all, several compilers marked Fortran
18653 main programs with DW_CC_program --- even when those
18654 functions use the standard calling conventions.
18655
18656 Although DWARF now specifies a way to provide this
18657 information, we support this practice for backward
18658 compatibility. */
18659 if (DW_UNSND (&attr) == DW_CC_program
18660 && cu->language == language_fortran)
18661 main_subprogram = 1;
18662 break;
18663 case DW_AT_inline:
18664 if (DW_UNSND (&attr) == DW_INL_inlined
18665 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18666 may_be_inlined = 1;
18667 break;
18668
18669 case DW_AT_import:
18670 if (tag == DW_TAG_imported_unit)
18671 {
18672 d.sect_off = attr.get_ref_die_offset ();
18673 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18674 || cu->per_cu->is_dwz);
18675 }
18676 break;
18677
18678 case DW_AT_main_subprogram:
18679 main_subprogram = DW_UNSND (&attr);
18680 break;
18681
18682 case DW_AT_ranges:
18683 {
18684 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18685 but that requires a full DIE, so instead we just
18686 reimplement it. */
18687 int need_ranges_base = tag != DW_TAG_compile_unit;
18688 unsigned int ranges_offset = (DW_UNSND (&attr)
18689 + (need_ranges_base
18690 ? cu->ranges_base
18691 : 0));
18692
18693 /* Value of the DW_AT_ranges attribute is the offset in the
18694 .debug_ranges section. */
18695 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18696 nullptr))
18697 has_pc_info = 1;
18698 }
18699 break;
18700
18701 default:
18702 break;
18703 }
18704 }
18705
18706 /* For Ada, if both the name and the linkage name appear, we prefer
18707 the latter. This lets "catch exception" work better, regardless
18708 of the order in which the name and linkage name were emitted.
18709 Really, though, this is just a workaround for the fact that gdb
18710 doesn't store both the name and the linkage name. */
18711 if (cu->language == language_ada && linkage_name != nullptr)
18712 raw_name = linkage_name;
18713
18714 if (high_pc_relative)
18715 highpc += lowpc;
18716
18717 if (has_low_pc_attr && has_high_pc_attr)
18718 {
18719 /* When using the GNU linker, .gnu.linkonce. sections are used to
18720 eliminate duplicate copies of functions and vtables and such.
18721 The linker will arbitrarily choose one and discard the others.
18722 The AT_*_pc values for such functions refer to local labels in
18723 these sections. If the section from that file was discarded, the
18724 labels are not in the output, so the relocs get a value of 0.
18725 If this is a discarded function, mark the pc bounds as invalid,
18726 so that GDB will ignore it. */
18727 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
18728 {
18729 struct objfile *objfile = per_objfile->objfile;
18730 struct gdbarch *gdbarch = objfile->arch ();
18731
18732 complaint (_("DW_AT_low_pc %s is zero "
18733 "for DIE at %s [in module %s]"),
18734 paddress (gdbarch, lowpc),
18735 sect_offset_str (sect_off),
18736 objfile_name (objfile));
18737 }
18738 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18739 else if (lowpc >= highpc)
18740 {
18741 struct objfile *objfile = per_objfile->objfile;
18742 struct gdbarch *gdbarch = objfile->arch ();
18743
18744 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18745 "for DIE at %s [in module %s]"),
18746 paddress (gdbarch, lowpc),
18747 paddress (gdbarch, highpc),
18748 sect_offset_str (sect_off),
18749 objfile_name (objfile));
18750 }
18751 else
18752 has_pc_info = 1;
18753 }
18754
18755 return info_ptr;
18756 }
18757
18758 /* Find a cached partial DIE at OFFSET in CU. */
18759
18760 struct partial_die_info *
18761 dwarf2_cu::find_partial_die (sect_offset sect_off)
18762 {
18763 struct partial_die_info *lookup_die = NULL;
18764 struct partial_die_info part_die (sect_off);
18765
18766 lookup_die = ((struct partial_die_info *)
18767 htab_find_with_hash (partial_dies, &part_die,
18768 to_underlying (sect_off)));
18769
18770 return lookup_die;
18771 }
18772
18773 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18774 except in the case of .debug_types DIEs which do not reference
18775 outside their CU (they do however referencing other types via
18776 DW_FORM_ref_sig8). */
18777
18778 static const struct cu_partial_die_info
18779 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18780 {
18781 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18782 struct objfile *objfile = per_objfile->objfile;
18783 struct partial_die_info *pd = NULL;
18784
18785 if (offset_in_dwz == cu->per_cu->is_dwz
18786 && cu->header.offset_in_cu_p (sect_off))
18787 {
18788 pd = cu->find_partial_die (sect_off);
18789 if (pd != NULL)
18790 return { cu, pd };
18791 /* We missed recording what we needed.
18792 Load all dies and try again. */
18793 }
18794 else
18795 {
18796 /* TUs don't reference other CUs/TUs (except via type signatures). */
18797 if (cu->per_cu->is_debug_types)
18798 {
18799 error (_("Dwarf Error: Type Unit at offset %s contains"
18800 " external reference to offset %s [in module %s].\n"),
18801 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18802 bfd_get_filename (objfile->obfd));
18803 }
18804 dwarf2_per_cu_data *per_cu
18805 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18806 per_objfile);
18807
18808 cu = per_objfile->get_cu (per_cu);
18809 if (cu == NULL || cu->partial_dies == NULL)
18810 load_partial_comp_unit (per_cu, per_objfile, nullptr);
18811
18812 cu = per_objfile->get_cu (per_cu);
18813
18814 cu->last_used = 0;
18815 pd = cu->find_partial_die (sect_off);
18816 }
18817
18818 /* If we didn't find it, and not all dies have been loaded,
18819 load them all and try again. */
18820
18821 if (pd == NULL && cu->per_cu->load_all_dies == 0)
18822 {
18823 cu->per_cu->load_all_dies = 1;
18824
18825 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18826 THIS_CU->cu may already be in use. So we can't just free it and
18827 replace its DIEs with the ones we read in. Instead, we leave those
18828 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18829 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18830 set. */
18831 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
18832
18833 pd = cu->find_partial_die (sect_off);
18834 }
18835
18836 if (pd == NULL)
18837 internal_error (__FILE__, __LINE__,
18838 _("could not find partial DIE %s "
18839 "in cache [from module %s]\n"),
18840 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18841 return { cu, pd };
18842 }
18843
18844 /* See if we can figure out if the class lives in a namespace. We do
18845 this by looking for a member function; its demangled name will
18846 contain namespace info, if there is any. */
18847
18848 static void
18849 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18850 struct dwarf2_cu *cu)
18851 {
18852 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18853 what template types look like, because the demangler
18854 frequently doesn't give the same name as the debug info. We
18855 could fix this by only using the demangled name to get the
18856 prefix (but see comment in read_structure_type). */
18857
18858 struct partial_die_info *real_pdi;
18859 struct partial_die_info *child_pdi;
18860
18861 /* If this DIE (this DIE's specification, if any) has a parent, then
18862 we should not do this. We'll prepend the parent's fully qualified
18863 name when we create the partial symbol. */
18864
18865 real_pdi = struct_pdi;
18866 while (real_pdi->has_specification)
18867 {
18868 auto res = find_partial_die (real_pdi->spec_offset,
18869 real_pdi->spec_is_dwz, cu);
18870 real_pdi = res.pdi;
18871 cu = res.cu;
18872 }
18873
18874 if (real_pdi->die_parent != NULL)
18875 return;
18876
18877 for (child_pdi = struct_pdi->die_child;
18878 child_pdi != NULL;
18879 child_pdi = child_pdi->die_sibling)
18880 {
18881 if (child_pdi->tag == DW_TAG_subprogram
18882 && child_pdi->linkage_name != NULL)
18883 {
18884 gdb::unique_xmalloc_ptr<char> actual_class_name
18885 (cu->language_defn->class_name_from_physname
18886 (child_pdi->linkage_name));
18887 if (actual_class_name != NULL)
18888 {
18889 struct objfile *objfile = cu->per_objfile->objfile;
18890 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
18891 struct_pdi->canonical_name = 1;
18892 }
18893 break;
18894 }
18895 }
18896 }
18897
18898 /* Return true if a DIE with TAG may have the DW_AT_const_value
18899 attribute. */
18900
18901 static bool
18902 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18903 {
18904 switch (tag)
18905 {
18906 case DW_TAG_constant:
18907 case DW_TAG_enumerator:
18908 case DW_TAG_formal_parameter:
18909 case DW_TAG_template_value_param:
18910 case DW_TAG_variable:
18911 return true;
18912 }
18913
18914 return false;
18915 }
18916
18917 void
18918 partial_die_info::fixup (struct dwarf2_cu *cu)
18919 {
18920 /* Once we've fixed up a die, there's no point in doing so again.
18921 This also avoids a memory leak if we were to call
18922 guess_partial_die_structure_name multiple times. */
18923 if (fixup_called)
18924 return;
18925
18926 /* If we found a reference attribute and the DIE has no name, try
18927 to find a name in the referred to DIE. */
18928
18929 if (raw_name == NULL && has_specification)
18930 {
18931 struct partial_die_info *spec_die;
18932
18933 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18934 spec_die = res.pdi;
18935 cu = res.cu;
18936
18937 spec_die->fixup (cu);
18938
18939 if (spec_die->raw_name)
18940 {
18941 raw_name = spec_die->raw_name;
18942 canonical_name = spec_die->canonical_name;
18943
18944 /* Copy DW_AT_external attribute if it is set. */
18945 if (spec_die->is_external)
18946 is_external = spec_die->is_external;
18947 }
18948 }
18949
18950 if (!has_const_value && has_specification
18951 && can_have_DW_AT_const_value_p (tag))
18952 {
18953 struct partial_die_info *spec_die;
18954
18955 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18956 spec_die = res.pdi;
18957 cu = res.cu;
18958
18959 spec_die->fixup (cu);
18960
18961 if (spec_die->has_const_value)
18962 {
18963 /* Copy DW_AT_const_value attribute if it is set. */
18964 has_const_value = spec_die->has_const_value;
18965 }
18966 }
18967
18968 /* Set default names for some unnamed DIEs. */
18969
18970 if (raw_name == NULL && tag == DW_TAG_namespace)
18971 {
18972 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
18973 canonical_name = 1;
18974 }
18975
18976 /* If there is no parent die to provide a namespace, and there are
18977 children, see if we can determine the namespace from their linkage
18978 name. */
18979 if (cu->language == language_cplus
18980 && !cu->per_objfile->per_bfd->types.empty ()
18981 && die_parent == NULL
18982 && has_children
18983 && (tag == DW_TAG_class_type
18984 || tag == DW_TAG_structure_type
18985 || tag == DW_TAG_union_type))
18986 guess_partial_die_structure_name (this, cu);
18987
18988 /* GCC might emit a nameless struct or union that has a linkage
18989 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18990 if (raw_name == NULL
18991 && (tag == DW_TAG_class_type
18992 || tag == DW_TAG_interface_type
18993 || tag == DW_TAG_structure_type
18994 || tag == DW_TAG_union_type)
18995 && linkage_name != NULL)
18996 {
18997 gdb::unique_xmalloc_ptr<char> demangled
18998 (gdb_demangle (linkage_name, DMGL_TYPES));
18999 if (demangled != nullptr)
19000 {
19001 const char *base;
19002
19003 /* Strip any leading namespaces/classes, keep only the base name.
19004 DW_AT_name for named DIEs does not contain the prefixes. */
19005 base = strrchr (demangled.get (), ':');
19006 if (base && base > demangled.get () && base[-1] == ':')
19007 base++;
19008 else
19009 base = demangled.get ();
19010
19011 struct objfile *objfile = cu->per_objfile->objfile;
19012 raw_name = objfile->intern (base);
19013 canonical_name = 1;
19014 }
19015 }
19016
19017 fixup_called = 1;
19018 }
19019
19020 /* Read the .debug_loclists header contents from the given SECTION in the
19021 HEADER. */
19022 static void
19023 read_loclist_header (struct loclist_header *header,
19024 struct dwarf2_section_info *section)
19025 {
19026 unsigned int bytes_read;
19027 bfd *abfd = section->get_bfd_owner ();
19028 const gdb_byte *info_ptr = section->buffer;
19029 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19030 info_ptr += bytes_read;
19031 header->version = read_2_bytes (abfd, info_ptr);
19032 info_ptr += 2;
19033 header->addr_size = read_1_byte (abfd, info_ptr);
19034 info_ptr += 1;
19035 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19036 info_ptr += 1;
19037 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19038 }
19039
19040 /* Return the DW_AT_loclists_base value for the CU. */
19041 static ULONGEST
19042 lookup_loclist_base (struct dwarf2_cu *cu)
19043 {
19044 /* For the .dwo unit, the loclist_base points to the first offset following
19045 the header. The header consists of the following entities-
19046 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19047 bit format)
19048 2. version (2 bytes)
19049 3. address size (1 byte)
19050 4. segment selector size (1 byte)
19051 5. offset entry count (4 bytes)
19052 These sizes are derived as per the DWARFv5 standard. */
19053 if (cu->dwo_unit != nullptr)
19054 {
19055 if (cu->header.initial_length_size == 4)
19056 return LOCLIST_HEADER_SIZE32;
19057 return LOCLIST_HEADER_SIZE64;
19058 }
19059 return cu->loclist_base;
19060 }
19061
19062 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19063 array of offsets in the .debug_loclists section. */
19064 static CORE_ADDR
19065 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19066 {
19067 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19068 struct objfile *objfile = per_objfile->objfile;
19069 bfd *abfd = objfile->obfd;
19070 ULONGEST loclist_base = lookup_loclist_base (cu);
19071 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19072
19073 section->read (objfile);
19074 if (section->buffer == NULL)
19075 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19076 "section [in module %s]"), objfile_name (objfile));
19077 struct loclist_header header;
19078 read_loclist_header (&header, section);
19079 if (loclist_index >= header.offset_entry_count)
19080 complaint (_("DW_FORM_loclistx pointing outside of "
19081 ".debug_loclists offset array [in module %s]"),
19082 objfile_name (objfile));
19083 if (loclist_base + loclist_index * cu->header.offset_size
19084 >= section->size)
19085 complaint (_("DW_FORM_loclistx pointing outside of "
19086 ".debug_loclists section [in module %s]"),
19087 objfile_name (objfile));
19088 const gdb_byte *info_ptr
19089 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19090
19091 if (cu->header.offset_size == 4)
19092 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19093 else
19094 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19095 }
19096
19097 /* Process the attributes that had to be skipped in the first round. These
19098 attributes are the ones that need str_offsets_base or addr_base attributes.
19099 They could not have been processed in the first round, because at the time
19100 the values of str_offsets_base or addr_base may not have been known. */
19101 static void
19102 read_attribute_reprocess (const struct die_reader_specs *reader,
19103 struct attribute *attr)
19104 {
19105 struct dwarf2_cu *cu = reader->cu;
19106 switch (attr->form)
19107 {
19108 case DW_FORM_addrx:
19109 case DW_FORM_GNU_addr_index:
19110 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19111 break;
19112 case DW_FORM_loclistx:
19113 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19114 break;
19115 case DW_FORM_strx:
19116 case DW_FORM_strx1:
19117 case DW_FORM_strx2:
19118 case DW_FORM_strx3:
19119 case DW_FORM_strx4:
19120 case DW_FORM_GNU_str_index:
19121 {
19122 unsigned int str_index = DW_UNSND (attr);
19123 if (reader->dwo_file != NULL)
19124 {
19125 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19126 DW_STRING_IS_CANONICAL (attr) = 0;
19127 }
19128 else
19129 {
19130 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19131 DW_STRING_IS_CANONICAL (attr) = 0;
19132 }
19133 break;
19134 }
19135 default:
19136 gdb_assert_not_reached (_("Unexpected DWARF form."));
19137 }
19138 }
19139
19140 /* Read an attribute value described by an attribute form. */
19141
19142 static const gdb_byte *
19143 read_attribute_value (const struct die_reader_specs *reader,
19144 struct attribute *attr, unsigned form,
19145 LONGEST implicit_const, const gdb_byte *info_ptr,
19146 bool *need_reprocess)
19147 {
19148 struct dwarf2_cu *cu = reader->cu;
19149 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19150 struct objfile *objfile = per_objfile->objfile;
19151 bfd *abfd = reader->abfd;
19152 struct comp_unit_head *cu_header = &cu->header;
19153 unsigned int bytes_read;
19154 struct dwarf_block *blk;
19155 *need_reprocess = false;
19156
19157 attr->form = (enum dwarf_form) form;
19158 switch (form)
19159 {
19160 case DW_FORM_ref_addr:
19161 if (cu->header.version == 2)
19162 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19163 &bytes_read);
19164 else
19165 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19166 &bytes_read);
19167 info_ptr += bytes_read;
19168 break;
19169 case DW_FORM_GNU_ref_alt:
19170 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19171 info_ptr += bytes_read;
19172 break;
19173 case DW_FORM_addr:
19174 {
19175 struct gdbarch *gdbarch = objfile->arch ();
19176 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19177 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19178 info_ptr += bytes_read;
19179 }
19180 break;
19181 case DW_FORM_block2:
19182 blk = dwarf_alloc_block (cu);
19183 blk->size = read_2_bytes (abfd, info_ptr);
19184 info_ptr += 2;
19185 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19186 info_ptr += blk->size;
19187 DW_BLOCK (attr) = blk;
19188 break;
19189 case DW_FORM_block4:
19190 blk = dwarf_alloc_block (cu);
19191 blk->size = read_4_bytes (abfd, info_ptr);
19192 info_ptr += 4;
19193 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19194 info_ptr += blk->size;
19195 DW_BLOCK (attr) = blk;
19196 break;
19197 case DW_FORM_data2:
19198 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19199 info_ptr += 2;
19200 break;
19201 case DW_FORM_data4:
19202 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19203 info_ptr += 4;
19204 break;
19205 case DW_FORM_data8:
19206 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19207 info_ptr += 8;
19208 break;
19209 case DW_FORM_data16:
19210 blk = dwarf_alloc_block (cu);
19211 blk->size = 16;
19212 blk->data = read_n_bytes (abfd, info_ptr, 16);
19213 info_ptr += 16;
19214 DW_BLOCK (attr) = blk;
19215 break;
19216 case DW_FORM_sec_offset:
19217 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19218 info_ptr += bytes_read;
19219 break;
19220 case DW_FORM_loclistx:
19221 {
19222 *need_reprocess = true;
19223 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19224 info_ptr += bytes_read;
19225 }
19226 break;
19227 case DW_FORM_string:
19228 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19229 DW_STRING_IS_CANONICAL (attr) = 0;
19230 info_ptr += bytes_read;
19231 break;
19232 case DW_FORM_strp:
19233 if (!cu->per_cu->is_dwz)
19234 {
19235 DW_STRING (attr) = read_indirect_string (per_objfile,
19236 abfd, info_ptr, cu_header,
19237 &bytes_read);
19238 DW_STRING_IS_CANONICAL (attr) = 0;
19239 info_ptr += bytes_read;
19240 break;
19241 }
19242 /* FALLTHROUGH */
19243 case DW_FORM_line_strp:
19244 if (!cu->per_cu->is_dwz)
19245 {
19246 DW_STRING (attr) = per_objfile->read_line_string (info_ptr, cu_header,
19247 &bytes_read);
19248 DW_STRING_IS_CANONICAL (attr) = 0;
19249 info_ptr += bytes_read;
19250 break;
19251 }
19252 /* FALLTHROUGH */
19253 case DW_FORM_GNU_strp_alt:
19254 {
19255 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
19256 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19257 &bytes_read);
19258
19259 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19260 DW_STRING_IS_CANONICAL (attr) = 0;
19261 info_ptr += bytes_read;
19262 }
19263 break;
19264 case DW_FORM_exprloc:
19265 case DW_FORM_block:
19266 blk = dwarf_alloc_block (cu);
19267 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19268 info_ptr += bytes_read;
19269 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19270 info_ptr += blk->size;
19271 DW_BLOCK (attr) = blk;
19272 break;
19273 case DW_FORM_block1:
19274 blk = dwarf_alloc_block (cu);
19275 blk->size = read_1_byte (abfd, info_ptr);
19276 info_ptr += 1;
19277 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19278 info_ptr += blk->size;
19279 DW_BLOCK (attr) = blk;
19280 break;
19281 case DW_FORM_data1:
19282 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19283 info_ptr += 1;
19284 break;
19285 case DW_FORM_flag:
19286 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19287 info_ptr += 1;
19288 break;
19289 case DW_FORM_flag_present:
19290 DW_UNSND (attr) = 1;
19291 break;
19292 case DW_FORM_sdata:
19293 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19294 info_ptr += bytes_read;
19295 break;
19296 case DW_FORM_udata:
19297 case DW_FORM_rnglistx:
19298 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19299 info_ptr += bytes_read;
19300 break;
19301 case DW_FORM_ref1:
19302 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19303 + read_1_byte (abfd, info_ptr));
19304 info_ptr += 1;
19305 break;
19306 case DW_FORM_ref2:
19307 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19308 + read_2_bytes (abfd, info_ptr));
19309 info_ptr += 2;
19310 break;
19311 case DW_FORM_ref4:
19312 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19313 + read_4_bytes (abfd, info_ptr));
19314 info_ptr += 4;
19315 break;
19316 case DW_FORM_ref8:
19317 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19318 + read_8_bytes (abfd, info_ptr));
19319 info_ptr += 8;
19320 break;
19321 case DW_FORM_ref_sig8:
19322 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19323 info_ptr += 8;
19324 break;
19325 case DW_FORM_ref_udata:
19326 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19327 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19328 info_ptr += bytes_read;
19329 break;
19330 case DW_FORM_indirect:
19331 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19332 info_ptr += bytes_read;
19333 if (form == DW_FORM_implicit_const)
19334 {
19335 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19336 info_ptr += bytes_read;
19337 }
19338 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19339 info_ptr, need_reprocess);
19340 break;
19341 case DW_FORM_implicit_const:
19342 DW_SND (attr) = implicit_const;
19343 break;
19344 case DW_FORM_addrx:
19345 case DW_FORM_GNU_addr_index:
19346 *need_reprocess = true;
19347 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19348 info_ptr += bytes_read;
19349 break;
19350 case DW_FORM_strx:
19351 case DW_FORM_strx1:
19352 case DW_FORM_strx2:
19353 case DW_FORM_strx3:
19354 case DW_FORM_strx4:
19355 case DW_FORM_GNU_str_index:
19356 {
19357 ULONGEST str_index;
19358 if (form == DW_FORM_strx1)
19359 {
19360 str_index = read_1_byte (abfd, info_ptr);
19361 info_ptr += 1;
19362 }
19363 else if (form == DW_FORM_strx2)
19364 {
19365 str_index = read_2_bytes (abfd, info_ptr);
19366 info_ptr += 2;
19367 }
19368 else if (form == DW_FORM_strx3)
19369 {
19370 str_index = read_3_bytes (abfd, info_ptr);
19371 info_ptr += 3;
19372 }
19373 else if (form == DW_FORM_strx4)
19374 {
19375 str_index = read_4_bytes (abfd, info_ptr);
19376 info_ptr += 4;
19377 }
19378 else
19379 {
19380 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19381 info_ptr += bytes_read;
19382 }
19383 *need_reprocess = true;
19384 DW_UNSND (attr) = str_index;
19385 }
19386 break;
19387 default:
19388 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19389 dwarf_form_name (form),
19390 bfd_get_filename (abfd));
19391 }
19392
19393 /* Super hack. */
19394 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19395 attr->form = DW_FORM_GNU_ref_alt;
19396
19397 /* We have seen instances where the compiler tried to emit a byte
19398 size attribute of -1 which ended up being encoded as an unsigned
19399 0xffffffff. Although 0xffffffff is technically a valid size value,
19400 an object of this size seems pretty unlikely so we can relatively
19401 safely treat these cases as if the size attribute was invalid and
19402 treat them as zero by default. */
19403 if (attr->name == DW_AT_byte_size
19404 && form == DW_FORM_data4
19405 && DW_UNSND (attr) >= 0xffffffff)
19406 {
19407 complaint
19408 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19409 hex_string (DW_UNSND (attr)));
19410 DW_UNSND (attr) = 0;
19411 }
19412
19413 return info_ptr;
19414 }
19415
19416 /* Read an attribute described by an abbreviated attribute. */
19417
19418 static const gdb_byte *
19419 read_attribute (const struct die_reader_specs *reader,
19420 struct attribute *attr, struct attr_abbrev *abbrev,
19421 const gdb_byte *info_ptr, bool *need_reprocess)
19422 {
19423 attr->name = abbrev->name;
19424 return read_attribute_value (reader, attr, abbrev->form,
19425 abbrev->implicit_const, info_ptr,
19426 need_reprocess);
19427 }
19428
19429 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19430
19431 static const char *
19432 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19433 LONGEST str_offset)
19434 {
19435 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19436 str_offset, "DW_FORM_strp");
19437 }
19438
19439 /* Return pointer to string at .debug_str offset as read from BUF.
19440 BUF is assumed to be in a compilation unit described by CU_HEADER.
19441 Return *BYTES_READ_PTR count of bytes read from BUF. */
19442
19443 static const char *
19444 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19445 const gdb_byte *buf,
19446 const struct comp_unit_head *cu_header,
19447 unsigned int *bytes_read_ptr)
19448 {
19449 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19450
19451 return read_indirect_string_at_offset (per_objfile, str_offset);
19452 }
19453
19454 /* See read.h. */
19455
19456 const char *
19457 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19458 const struct comp_unit_head *cu_header,
19459 unsigned int *bytes_read_ptr)
19460 {
19461 bfd *abfd = objfile->obfd;
19462 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19463
19464 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19465 }
19466
19467 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19468 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19469 ADDR_SIZE is the size of addresses from the CU header. */
19470
19471 static CORE_ADDR
19472 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19473 gdb::optional<ULONGEST> addr_base, int addr_size)
19474 {
19475 struct objfile *objfile = per_objfile->objfile;
19476 bfd *abfd = objfile->obfd;
19477 const gdb_byte *info_ptr;
19478 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19479
19480 per_objfile->per_bfd->addr.read (objfile);
19481 if (per_objfile->per_bfd->addr.buffer == NULL)
19482 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19483 objfile_name (objfile));
19484 if (addr_base_or_zero + addr_index * addr_size
19485 >= per_objfile->per_bfd->addr.size)
19486 error (_("DW_FORM_addr_index pointing outside of "
19487 ".debug_addr section [in module %s]"),
19488 objfile_name (objfile));
19489 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19490 + addr_index * addr_size);
19491 if (addr_size == 4)
19492 return bfd_get_32 (abfd, info_ptr);
19493 else
19494 return bfd_get_64 (abfd, info_ptr);
19495 }
19496
19497 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19498
19499 static CORE_ADDR
19500 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19501 {
19502 return read_addr_index_1 (cu->per_objfile, addr_index,
19503 cu->addr_base, cu->header.addr_size);
19504 }
19505
19506 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19507
19508 static CORE_ADDR
19509 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19510 unsigned int *bytes_read)
19511 {
19512 bfd *abfd = cu->per_objfile->objfile->obfd;
19513 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19514
19515 return read_addr_index (cu, addr_index);
19516 }
19517
19518 /* See read.h. */
19519
19520 CORE_ADDR
19521 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19522 dwarf2_per_objfile *per_objfile,
19523 unsigned int addr_index)
19524 {
19525 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
19526 gdb::optional<ULONGEST> addr_base;
19527 int addr_size;
19528
19529 /* We need addr_base and addr_size.
19530 If we don't have PER_CU->cu, we have to get it.
19531 Nasty, but the alternative is storing the needed info in PER_CU,
19532 which at this point doesn't seem justified: it's not clear how frequently
19533 it would get used and it would increase the size of every PER_CU.
19534 Entry points like dwarf2_per_cu_addr_size do a similar thing
19535 so we're not in uncharted territory here.
19536 Alas we need to be a bit more complicated as addr_base is contained
19537 in the DIE.
19538
19539 We don't need to read the entire CU(/TU).
19540 We just need the header and top level die.
19541
19542 IWBN to use the aging mechanism to let us lazily later discard the CU.
19543 For now we skip this optimization. */
19544
19545 if (cu != NULL)
19546 {
19547 addr_base = cu->addr_base;
19548 addr_size = cu->header.addr_size;
19549 }
19550 else
19551 {
19552 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
19553 addr_base = reader.cu->addr_base;
19554 addr_size = reader.cu->header.addr_size;
19555 }
19556
19557 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
19558 }
19559
19560 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19561 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19562 DWO file. */
19563
19564 static const char *
19565 read_str_index (struct dwarf2_cu *cu,
19566 struct dwarf2_section_info *str_section,
19567 struct dwarf2_section_info *str_offsets_section,
19568 ULONGEST str_offsets_base, ULONGEST str_index)
19569 {
19570 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19571 struct objfile *objfile = per_objfile->objfile;
19572 const char *objf_name = objfile_name (objfile);
19573 bfd *abfd = objfile->obfd;
19574 const gdb_byte *info_ptr;
19575 ULONGEST str_offset;
19576 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19577
19578 str_section->read (objfile);
19579 str_offsets_section->read (objfile);
19580 if (str_section->buffer == NULL)
19581 error (_("%s used without %s section"
19582 " in CU at offset %s [in module %s]"),
19583 form_name, str_section->get_name (),
19584 sect_offset_str (cu->header.sect_off), objf_name);
19585 if (str_offsets_section->buffer == NULL)
19586 error (_("%s used without %s section"
19587 " in CU at offset %s [in module %s]"),
19588 form_name, str_section->get_name (),
19589 sect_offset_str (cu->header.sect_off), objf_name);
19590 info_ptr = (str_offsets_section->buffer
19591 + str_offsets_base
19592 + str_index * cu->header.offset_size);
19593 if (cu->header.offset_size == 4)
19594 str_offset = bfd_get_32 (abfd, info_ptr);
19595 else
19596 str_offset = bfd_get_64 (abfd, info_ptr);
19597 if (str_offset >= str_section->size)
19598 error (_("Offset from %s pointing outside of"
19599 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19600 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19601 return (const char *) (str_section->buffer + str_offset);
19602 }
19603
19604 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19605
19606 static const char *
19607 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19608 {
19609 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19610 ? reader->cu->header.addr_size : 0;
19611 return read_str_index (reader->cu,
19612 &reader->dwo_file->sections.str,
19613 &reader->dwo_file->sections.str_offsets,
19614 str_offsets_base, str_index);
19615 }
19616
19617 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19618
19619 static const char *
19620 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19621 {
19622 struct objfile *objfile = cu->per_objfile->objfile;
19623 const char *objf_name = objfile_name (objfile);
19624 static const char form_name[] = "DW_FORM_GNU_str_index";
19625 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19626
19627 if (!cu->str_offsets_base.has_value ())
19628 error (_("%s used in Fission stub without %s"
19629 " in CU at offset 0x%lx [in module %s]"),
19630 form_name, str_offsets_attr_name,
19631 (long) cu->header.offset_size, objf_name);
19632
19633 return read_str_index (cu,
19634 &cu->per_objfile->per_bfd->str,
19635 &cu->per_objfile->per_bfd->str_offsets,
19636 *cu->str_offsets_base, str_index);
19637 }
19638
19639 /* Return the length of an LEB128 number in BUF. */
19640
19641 static int
19642 leb128_size (const gdb_byte *buf)
19643 {
19644 const gdb_byte *begin = buf;
19645 gdb_byte byte;
19646
19647 while (1)
19648 {
19649 byte = *buf++;
19650 if ((byte & 128) == 0)
19651 return buf - begin;
19652 }
19653 }
19654
19655 static void
19656 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19657 {
19658 switch (lang)
19659 {
19660 case DW_LANG_C89:
19661 case DW_LANG_C99:
19662 case DW_LANG_C11:
19663 case DW_LANG_C:
19664 case DW_LANG_UPC:
19665 cu->language = language_c;
19666 break;
19667 case DW_LANG_Java:
19668 case DW_LANG_C_plus_plus:
19669 case DW_LANG_C_plus_plus_11:
19670 case DW_LANG_C_plus_plus_14:
19671 cu->language = language_cplus;
19672 break;
19673 case DW_LANG_D:
19674 cu->language = language_d;
19675 break;
19676 case DW_LANG_Fortran77:
19677 case DW_LANG_Fortran90:
19678 case DW_LANG_Fortran95:
19679 case DW_LANG_Fortran03:
19680 case DW_LANG_Fortran08:
19681 cu->language = language_fortran;
19682 break;
19683 case DW_LANG_Go:
19684 cu->language = language_go;
19685 break;
19686 case DW_LANG_Mips_Assembler:
19687 cu->language = language_asm;
19688 break;
19689 case DW_LANG_Ada83:
19690 case DW_LANG_Ada95:
19691 cu->language = language_ada;
19692 break;
19693 case DW_LANG_Modula2:
19694 cu->language = language_m2;
19695 break;
19696 case DW_LANG_Pascal83:
19697 cu->language = language_pascal;
19698 break;
19699 case DW_LANG_ObjC:
19700 cu->language = language_objc;
19701 break;
19702 case DW_LANG_Rust:
19703 case DW_LANG_Rust_old:
19704 cu->language = language_rust;
19705 break;
19706 case DW_LANG_Cobol74:
19707 case DW_LANG_Cobol85:
19708 default:
19709 cu->language = language_minimal;
19710 break;
19711 }
19712 cu->language_defn = language_def (cu->language);
19713 }
19714
19715 /* Return the named attribute or NULL if not there. */
19716
19717 static struct attribute *
19718 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19719 {
19720 for (;;)
19721 {
19722 unsigned int i;
19723 struct attribute *spec = NULL;
19724
19725 for (i = 0; i < die->num_attrs; ++i)
19726 {
19727 if (die->attrs[i].name == name)
19728 return &die->attrs[i];
19729 if (die->attrs[i].name == DW_AT_specification
19730 || die->attrs[i].name == DW_AT_abstract_origin)
19731 spec = &die->attrs[i];
19732 }
19733
19734 if (!spec)
19735 break;
19736
19737 die = follow_die_ref (die, spec, &cu);
19738 }
19739
19740 return NULL;
19741 }
19742
19743 /* Return the string associated with a string-typed attribute, or NULL if it
19744 is either not found or is of an incorrect type. */
19745
19746 static const char *
19747 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19748 {
19749 struct attribute *attr;
19750 const char *str = NULL;
19751
19752 attr = dwarf2_attr (die, name, cu);
19753
19754 if (attr != NULL)
19755 {
19756 str = attr->value_as_string ();
19757 if (str == nullptr)
19758 complaint (_("string type expected for attribute %s for "
19759 "DIE at %s in module %s"),
19760 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19761 objfile_name (cu->per_objfile->objfile));
19762 }
19763
19764 return str;
19765 }
19766
19767 /* Return the dwo name or NULL if not present. If present, it is in either
19768 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19769 static const char *
19770 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19771 {
19772 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19773 if (dwo_name == nullptr)
19774 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19775 return dwo_name;
19776 }
19777
19778 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19779 and holds a non-zero value. This function should only be used for
19780 DW_FORM_flag or DW_FORM_flag_present attributes. */
19781
19782 static int
19783 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19784 {
19785 struct attribute *attr = dwarf2_attr (die, name, cu);
19786
19787 return (attr && DW_UNSND (attr));
19788 }
19789
19790 static int
19791 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19792 {
19793 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19794 which value is non-zero. However, we have to be careful with
19795 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19796 (via dwarf2_flag_true_p) follows this attribute. So we may
19797 end up accidently finding a declaration attribute that belongs
19798 to a different DIE referenced by the specification attribute,
19799 even though the given DIE does not have a declaration attribute. */
19800 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19801 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19802 }
19803
19804 /* Return the die giving the specification for DIE, if there is
19805 one. *SPEC_CU is the CU containing DIE on input, and the CU
19806 containing the return value on output. If there is no
19807 specification, but there is an abstract origin, that is
19808 returned. */
19809
19810 static struct die_info *
19811 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19812 {
19813 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19814 *spec_cu);
19815
19816 if (spec_attr == NULL)
19817 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19818
19819 if (spec_attr == NULL)
19820 return NULL;
19821 else
19822 return follow_die_ref (die, spec_attr, spec_cu);
19823 }
19824
19825 /* Stub for free_line_header to match void * callback types. */
19826
19827 static void
19828 free_line_header_voidp (void *arg)
19829 {
19830 struct line_header *lh = (struct line_header *) arg;
19831
19832 delete lh;
19833 }
19834
19835 /* A convenience function to find the proper .debug_line section for a CU. */
19836
19837 static struct dwarf2_section_info *
19838 get_debug_line_section (struct dwarf2_cu *cu)
19839 {
19840 struct dwarf2_section_info *section;
19841 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19842
19843 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19844 DWO file. */
19845 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19846 section = &cu->dwo_unit->dwo_file->sections.line;
19847 else if (cu->per_cu->is_dwz)
19848 {
19849 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
19850
19851 section = &dwz->line;
19852 }
19853 else
19854 section = &per_objfile->per_bfd->line;
19855
19856 return section;
19857 }
19858
19859 /* Read the statement program header starting at OFFSET in
19860 .debug_line, or .debug_line.dwo. Return a pointer
19861 to a struct line_header, allocated using xmalloc.
19862 Returns NULL if there is a problem reading the header, e.g., if it
19863 has a version we don't understand.
19864
19865 NOTE: the strings in the include directory and file name tables of
19866 the returned object point into the dwarf line section buffer,
19867 and must not be freed. */
19868
19869 static line_header_up
19870 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19871 {
19872 struct dwarf2_section_info *section;
19873 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19874
19875 section = get_debug_line_section (cu);
19876 section->read (per_objfile->objfile);
19877 if (section->buffer == NULL)
19878 {
19879 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19880 complaint (_("missing .debug_line.dwo section"));
19881 else
19882 complaint (_("missing .debug_line section"));
19883 return 0;
19884 }
19885
19886 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19887 per_objfile, section, &cu->header);
19888 }
19889
19890 /* Subroutine of dwarf_decode_lines to simplify it.
19891 Return the file name of the psymtab for the given file_entry.
19892 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19893 If space for the result is malloc'd, *NAME_HOLDER will be set.
19894 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19895
19896 static const char *
19897 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19898 const dwarf2_psymtab *pst,
19899 const char *comp_dir,
19900 gdb::unique_xmalloc_ptr<char> *name_holder)
19901 {
19902 const char *include_name = fe.name;
19903 const char *include_name_to_compare = include_name;
19904 const char *pst_filename;
19905 int file_is_pst;
19906
19907 const char *dir_name = fe.include_dir (lh);
19908
19909 gdb::unique_xmalloc_ptr<char> hold_compare;
19910 if (!IS_ABSOLUTE_PATH (include_name)
19911 && (dir_name != NULL || comp_dir != NULL))
19912 {
19913 /* Avoid creating a duplicate psymtab for PST.
19914 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19915 Before we do the comparison, however, we need to account
19916 for DIR_NAME and COMP_DIR.
19917 First prepend dir_name (if non-NULL). If we still don't
19918 have an absolute path prepend comp_dir (if non-NULL).
19919 However, the directory we record in the include-file's
19920 psymtab does not contain COMP_DIR (to match the
19921 corresponding symtab(s)).
19922
19923 Example:
19924
19925 bash$ cd /tmp
19926 bash$ gcc -g ./hello.c
19927 include_name = "hello.c"
19928 dir_name = "."
19929 DW_AT_comp_dir = comp_dir = "/tmp"
19930 DW_AT_name = "./hello.c"
19931
19932 */
19933
19934 if (dir_name != NULL)
19935 {
19936 name_holder->reset (concat (dir_name, SLASH_STRING,
19937 include_name, (char *) NULL));
19938 include_name = name_holder->get ();
19939 include_name_to_compare = include_name;
19940 }
19941 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19942 {
19943 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19944 include_name, (char *) NULL));
19945 include_name_to_compare = hold_compare.get ();
19946 }
19947 }
19948
19949 pst_filename = pst->filename;
19950 gdb::unique_xmalloc_ptr<char> copied_name;
19951 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19952 {
19953 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19954 pst_filename, (char *) NULL));
19955 pst_filename = copied_name.get ();
19956 }
19957
19958 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19959
19960 if (file_is_pst)
19961 return NULL;
19962 return include_name;
19963 }
19964
19965 /* State machine to track the state of the line number program. */
19966
19967 class lnp_state_machine
19968 {
19969 public:
19970 /* Initialize a machine state for the start of a line number
19971 program. */
19972 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19973 bool record_lines_p);
19974
19975 file_entry *current_file ()
19976 {
19977 /* lh->file_names is 0-based, but the file name numbers in the
19978 statement program are 1-based. */
19979 return m_line_header->file_name_at (m_file);
19980 }
19981
19982 /* Record the line in the state machine. END_SEQUENCE is true if
19983 we're processing the end of a sequence. */
19984 void record_line (bool end_sequence);
19985
19986 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19987 nop-out rest of the lines in this sequence. */
19988 void check_line_address (struct dwarf2_cu *cu,
19989 const gdb_byte *line_ptr,
19990 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19991
19992 void handle_set_discriminator (unsigned int discriminator)
19993 {
19994 m_discriminator = discriminator;
19995 m_line_has_non_zero_discriminator |= discriminator != 0;
19996 }
19997
19998 /* Handle DW_LNE_set_address. */
19999 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20000 {
20001 m_op_index = 0;
20002 address += baseaddr;
20003 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20004 }
20005
20006 /* Handle DW_LNS_advance_pc. */
20007 void handle_advance_pc (CORE_ADDR adjust);
20008
20009 /* Handle a special opcode. */
20010 void handle_special_opcode (unsigned char op_code);
20011
20012 /* Handle DW_LNS_advance_line. */
20013 void handle_advance_line (int line_delta)
20014 {
20015 advance_line (line_delta);
20016 }
20017
20018 /* Handle DW_LNS_set_file. */
20019 void handle_set_file (file_name_index file);
20020
20021 /* Handle DW_LNS_negate_stmt. */
20022 void handle_negate_stmt ()
20023 {
20024 m_is_stmt = !m_is_stmt;
20025 }
20026
20027 /* Handle DW_LNS_const_add_pc. */
20028 void handle_const_add_pc ();
20029
20030 /* Handle DW_LNS_fixed_advance_pc. */
20031 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20032 {
20033 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20034 m_op_index = 0;
20035 }
20036
20037 /* Handle DW_LNS_copy. */
20038 void handle_copy ()
20039 {
20040 record_line (false);
20041 m_discriminator = 0;
20042 }
20043
20044 /* Handle DW_LNE_end_sequence. */
20045 void handle_end_sequence ()
20046 {
20047 m_currently_recording_lines = true;
20048 }
20049
20050 private:
20051 /* Advance the line by LINE_DELTA. */
20052 void advance_line (int line_delta)
20053 {
20054 m_line += line_delta;
20055
20056 if (line_delta != 0)
20057 m_line_has_non_zero_discriminator = m_discriminator != 0;
20058 }
20059
20060 struct dwarf2_cu *m_cu;
20061
20062 gdbarch *m_gdbarch;
20063
20064 /* True if we're recording lines.
20065 Otherwise we're building partial symtabs and are just interested in
20066 finding include files mentioned by the line number program. */
20067 bool m_record_lines_p;
20068
20069 /* The line number header. */
20070 line_header *m_line_header;
20071
20072 /* These are part of the standard DWARF line number state machine,
20073 and initialized according to the DWARF spec. */
20074
20075 unsigned char m_op_index = 0;
20076 /* The line table index of the current file. */
20077 file_name_index m_file = 1;
20078 unsigned int m_line = 1;
20079
20080 /* These are initialized in the constructor. */
20081
20082 CORE_ADDR m_address;
20083 bool m_is_stmt;
20084 unsigned int m_discriminator;
20085
20086 /* Additional bits of state we need to track. */
20087
20088 /* The last file that we called dwarf2_start_subfile for.
20089 This is only used for TLLs. */
20090 unsigned int m_last_file = 0;
20091 /* The last file a line number was recorded for. */
20092 struct subfile *m_last_subfile = NULL;
20093
20094 /* The address of the last line entry. */
20095 CORE_ADDR m_last_address;
20096
20097 /* Set to true when a previous line at the same address (using
20098 m_last_address) had m_is_stmt true. This is reset to false when a
20099 line entry at a new address (m_address different to m_last_address) is
20100 processed. */
20101 bool m_stmt_at_address = false;
20102
20103 /* When true, record the lines we decode. */
20104 bool m_currently_recording_lines = false;
20105
20106 /* The last line number that was recorded, used to coalesce
20107 consecutive entries for the same line. This can happen, for
20108 example, when discriminators are present. PR 17276. */
20109 unsigned int m_last_line = 0;
20110 bool m_line_has_non_zero_discriminator = false;
20111 };
20112
20113 void
20114 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20115 {
20116 CORE_ADDR addr_adj = (((m_op_index + adjust)
20117 / m_line_header->maximum_ops_per_instruction)
20118 * m_line_header->minimum_instruction_length);
20119 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20120 m_op_index = ((m_op_index + adjust)
20121 % m_line_header->maximum_ops_per_instruction);
20122 }
20123
20124 void
20125 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20126 {
20127 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20128 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20129 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20130 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20131 / m_line_header->maximum_ops_per_instruction)
20132 * m_line_header->minimum_instruction_length);
20133 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20134 m_op_index = ((m_op_index + adj_opcode_d)
20135 % m_line_header->maximum_ops_per_instruction);
20136
20137 int line_delta = m_line_header->line_base + adj_opcode_r;
20138 advance_line (line_delta);
20139 record_line (false);
20140 m_discriminator = 0;
20141 }
20142
20143 void
20144 lnp_state_machine::handle_set_file (file_name_index file)
20145 {
20146 m_file = file;
20147
20148 const file_entry *fe = current_file ();
20149 if (fe == NULL)
20150 dwarf2_debug_line_missing_file_complaint ();
20151 else if (m_record_lines_p)
20152 {
20153 const char *dir = fe->include_dir (m_line_header);
20154
20155 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20156 m_line_has_non_zero_discriminator = m_discriminator != 0;
20157 dwarf2_start_subfile (m_cu, fe->name, dir);
20158 }
20159 }
20160
20161 void
20162 lnp_state_machine::handle_const_add_pc ()
20163 {
20164 CORE_ADDR adjust
20165 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20166
20167 CORE_ADDR addr_adj
20168 = (((m_op_index + adjust)
20169 / m_line_header->maximum_ops_per_instruction)
20170 * m_line_header->minimum_instruction_length);
20171
20172 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20173 m_op_index = ((m_op_index + adjust)
20174 % m_line_header->maximum_ops_per_instruction);
20175 }
20176
20177 /* Return non-zero if we should add LINE to the line number table.
20178 LINE is the line to add, LAST_LINE is the last line that was added,
20179 LAST_SUBFILE is the subfile for LAST_LINE.
20180 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20181 had a non-zero discriminator.
20182
20183 We have to be careful in the presence of discriminators.
20184 E.g., for this line:
20185
20186 for (i = 0; i < 100000; i++);
20187
20188 clang can emit four line number entries for that one line,
20189 each with a different discriminator.
20190 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20191
20192 However, we want gdb to coalesce all four entries into one.
20193 Otherwise the user could stepi into the middle of the line and
20194 gdb would get confused about whether the pc really was in the
20195 middle of the line.
20196
20197 Things are further complicated by the fact that two consecutive
20198 line number entries for the same line is a heuristic used by gcc
20199 to denote the end of the prologue. So we can't just discard duplicate
20200 entries, we have to be selective about it. The heuristic we use is
20201 that we only collapse consecutive entries for the same line if at least
20202 one of those entries has a non-zero discriminator. PR 17276.
20203
20204 Note: Addresses in the line number state machine can never go backwards
20205 within one sequence, thus this coalescing is ok. */
20206
20207 static int
20208 dwarf_record_line_p (struct dwarf2_cu *cu,
20209 unsigned int line, unsigned int last_line,
20210 int line_has_non_zero_discriminator,
20211 struct subfile *last_subfile)
20212 {
20213 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20214 return 1;
20215 if (line != last_line)
20216 return 1;
20217 /* Same line for the same file that we've seen already.
20218 As a last check, for pr 17276, only record the line if the line
20219 has never had a non-zero discriminator. */
20220 if (!line_has_non_zero_discriminator)
20221 return 1;
20222 return 0;
20223 }
20224
20225 /* Use the CU's builder to record line number LINE beginning at
20226 address ADDRESS in the line table of subfile SUBFILE. */
20227
20228 static void
20229 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20230 unsigned int line, CORE_ADDR address, bool is_stmt,
20231 struct dwarf2_cu *cu)
20232 {
20233 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20234
20235 if (dwarf_line_debug)
20236 {
20237 fprintf_unfiltered (gdb_stdlog,
20238 "Recording line %u, file %s, address %s\n",
20239 line, lbasename (subfile->name),
20240 paddress (gdbarch, address));
20241 }
20242
20243 if (cu != nullptr)
20244 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20245 }
20246
20247 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20248 Mark the end of a set of line number records.
20249 The arguments are the same as for dwarf_record_line_1.
20250 If SUBFILE is NULL the request is ignored. */
20251
20252 static void
20253 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20254 CORE_ADDR address, struct dwarf2_cu *cu)
20255 {
20256 if (subfile == NULL)
20257 return;
20258
20259 if (dwarf_line_debug)
20260 {
20261 fprintf_unfiltered (gdb_stdlog,
20262 "Finishing current line, file %s, address %s\n",
20263 lbasename (subfile->name),
20264 paddress (gdbarch, address));
20265 }
20266
20267 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20268 }
20269
20270 void
20271 lnp_state_machine::record_line (bool end_sequence)
20272 {
20273 if (dwarf_line_debug)
20274 {
20275 fprintf_unfiltered (gdb_stdlog,
20276 "Processing actual line %u: file %u,"
20277 " address %s, is_stmt %u, discrim %u%s\n",
20278 m_line, m_file,
20279 paddress (m_gdbarch, m_address),
20280 m_is_stmt, m_discriminator,
20281 (end_sequence ? "\t(end sequence)" : ""));
20282 }
20283
20284 file_entry *fe = current_file ();
20285
20286 if (fe == NULL)
20287 dwarf2_debug_line_missing_file_complaint ();
20288 /* For now we ignore lines not starting on an instruction boundary.
20289 But not when processing end_sequence for compatibility with the
20290 previous version of the code. */
20291 else if (m_op_index == 0 || end_sequence)
20292 {
20293 fe->included_p = 1;
20294 if (m_record_lines_p)
20295 {
20296 /* When we switch files we insert an end maker in the first file,
20297 switch to the second file and add a new line entry. The
20298 problem is that the end marker inserted in the first file will
20299 discard any previous line entries at the same address. If the
20300 line entries in the first file are marked as is-stmt, while
20301 the new line in the second file is non-stmt, then this means
20302 the end marker will discard is-stmt lines so we can have a
20303 non-stmt line. This means that there are less addresses at
20304 which the user can insert a breakpoint.
20305
20306 To improve this we track the last address in m_last_address,
20307 and whether we have seen an is-stmt at this address. Then
20308 when switching files, if we have seen a stmt at the current
20309 address, and we are switching to create a non-stmt line, then
20310 discard the new line. */
20311 bool file_changed
20312 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20313 bool ignore_this_line
20314 = (file_changed && !end_sequence && m_last_address == m_address
20315 && !m_is_stmt && m_stmt_at_address);
20316
20317 if ((file_changed && !ignore_this_line) || end_sequence)
20318 {
20319 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20320 m_currently_recording_lines ? m_cu : nullptr);
20321 }
20322
20323 if (!end_sequence && !ignore_this_line)
20324 {
20325 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20326
20327 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20328 m_line_has_non_zero_discriminator,
20329 m_last_subfile))
20330 {
20331 buildsym_compunit *builder = m_cu->get_builder ();
20332 dwarf_record_line_1 (m_gdbarch,
20333 builder->get_current_subfile (),
20334 m_line, m_address, is_stmt,
20335 m_currently_recording_lines ? m_cu : nullptr);
20336 }
20337 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20338 m_last_line = m_line;
20339 }
20340 }
20341 }
20342
20343 /* Track whether we have seen any m_is_stmt true at m_address in case we
20344 have multiple line table entries all at m_address. */
20345 if (m_last_address != m_address)
20346 {
20347 m_stmt_at_address = false;
20348 m_last_address = m_address;
20349 }
20350 m_stmt_at_address |= m_is_stmt;
20351 }
20352
20353 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20354 line_header *lh, bool record_lines_p)
20355 {
20356 m_cu = cu;
20357 m_gdbarch = arch;
20358 m_record_lines_p = record_lines_p;
20359 m_line_header = lh;
20360
20361 m_currently_recording_lines = true;
20362
20363 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20364 was a line entry for it so that the backend has a chance to adjust it
20365 and also record it in case it needs it. This is currently used by MIPS
20366 code, cf. `mips_adjust_dwarf2_line'. */
20367 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20368 m_is_stmt = lh->default_is_stmt;
20369 m_discriminator = 0;
20370
20371 m_last_address = m_address;
20372 m_stmt_at_address = false;
20373 }
20374
20375 void
20376 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20377 const gdb_byte *line_ptr,
20378 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20379 {
20380 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20381 the pc range of the CU. However, we restrict the test to only ADDRESS
20382 values of zero to preserve GDB's previous behaviour which is to handle
20383 the specific case of a function being GC'd by the linker. */
20384
20385 if (address == 0 && address < unrelocated_lowpc)
20386 {
20387 /* This line table is for a function which has been
20388 GCd by the linker. Ignore it. PR gdb/12528 */
20389
20390 struct objfile *objfile = cu->per_objfile->objfile;
20391 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20392
20393 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20394 line_offset, objfile_name (objfile));
20395 m_currently_recording_lines = false;
20396 /* Note: m_currently_recording_lines is left as false until we see
20397 DW_LNE_end_sequence. */
20398 }
20399 }
20400
20401 /* Subroutine of dwarf_decode_lines to simplify it.
20402 Process the line number information in LH.
20403 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20404 program in order to set included_p for every referenced header. */
20405
20406 static void
20407 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20408 const int decode_for_pst_p, CORE_ADDR lowpc)
20409 {
20410 const gdb_byte *line_ptr, *extended_end;
20411 const gdb_byte *line_end;
20412 unsigned int bytes_read, extended_len;
20413 unsigned char op_code, extended_op;
20414 CORE_ADDR baseaddr;
20415 struct objfile *objfile = cu->per_objfile->objfile;
20416 bfd *abfd = objfile->obfd;
20417 struct gdbarch *gdbarch = objfile->arch ();
20418 /* True if we're recording line info (as opposed to building partial
20419 symtabs and just interested in finding include files mentioned by
20420 the line number program). */
20421 bool record_lines_p = !decode_for_pst_p;
20422
20423 baseaddr = objfile->text_section_offset ();
20424
20425 line_ptr = lh->statement_program_start;
20426 line_end = lh->statement_program_end;
20427
20428 /* Read the statement sequences until there's nothing left. */
20429 while (line_ptr < line_end)
20430 {
20431 /* The DWARF line number program state machine. Reset the state
20432 machine at the start of each sequence. */
20433 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20434 bool end_sequence = false;
20435
20436 if (record_lines_p)
20437 {
20438 /* Start a subfile for the current file of the state
20439 machine. */
20440 const file_entry *fe = state_machine.current_file ();
20441
20442 if (fe != NULL)
20443 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20444 }
20445
20446 /* Decode the table. */
20447 while (line_ptr < line_end && !end_sequence)
20448 {
20449 op_code = read_1_byte (abfd, line_ptr);
20450 line_ptr += 1;
20451
20452 if (op_code >= lh->opcode_base)
20453 {
20454 /* Special opcode. */
20455 state_machine.handle_special_opcode (op_code);
20456 }
20457 else switch (op_code)
20458 {
20459 case DW_LNS_extended_op:
20460 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20461 &bytes_read);
20462 line_ptr += bytes_read;
20463 extended_end = line_ptr + extended_len;
20464 extended_op = read_1_byte (abfd, line_ptr);
20465 line_ptr += 1;
20466 switch (extended_op)
20467 {
20468 case DW_LNE_end_sequence:
20469 state_machine.handle_end_sequence ();
20470 end_sequence = true;
20471 break;
20472 case DW_LNE_set_address:
20473 {
20474 CORE_ADDR address
20475 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20476 line_ptr += bytes_read;
20477
20478 state_machine.check_line_address (cu, line_ptr,
20479 lowpc - baseaddr, address);
20480 state_machine.handle_set_address (baseaddr, address);
20481 }
20482 break;
20483 case DW_LNE_define_file:
20484 {
20485 const char *cur_file;
20486 unsigned int mod_time, length;
20487 dir_index dindex;
20488
20489 cur_file = read_direct_string (abfd, line_ptr,
20490 &bytes_read);
20491 line_ptr += bytes_read;
20492 dindex = (dir_index)
20493 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20494 line_ptr += bytes_read;
20495 mod_time =
20496 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20497 line_ptr += bytes_read;
20498 length =
20499 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20500 line_ptr += bytes_read;
20501 lh->add_file_name (cur_file, dindex, mod_time, length);
20502 }
20503 break;
20504 case DW_LNE_set_discriminator:
20505 {
20506 /* The discriminator is not interesting to the
20507 debugger; just ignore it. We still need to
20508 check its value though:
20509 if there are consecutive entries for the same
20510 (non-prologue) line we want to coalesce them.
20511 PR 17276. */
20512 unsigned int discr
20513 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20514 line_ptr += bytes_read;
20515
20516 state_machine.handle_set_discriminator (discr);
20517 }
20518 break;
20519 default:
20520 complaint (_("mangled .debug_line section"));
20521 return;
20522 }
20523 /* Make sure that we parsed the extended op correctly. If e.g.
20524 we expected a different address size than the producer used,
20525 we may have read the wrong number of bytes. */
20526 if (line_ptr != extended_end)
20527 {
20528 complaint (_("mangled .debug_line section"));
20529 return;
20530 }
20531 break;
20532 case DW_LNS_copy:
20533 state_machine.handle_copy ();
20534 break;
20535 case DW_LNS_advance_pc:
20536 {
20537 CORE_ADDR adjust
20538 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20539 line_ptr += bytes_read;
20540
20541 state_machine.handle_advance_pc (adjust);
20542 }
20543 break;
20544 case DW_LNS_advance_line:
20545 {
20546 int line_delta
20547 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20548 line_ptr += bytes_read;
20549
20550 state_machine.handle_advance_line (line_delta);
20551 }
20552 break;
20553 case DW_LNS_set_file:
20554 {
20555 file_name_index file
20556 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20557 &bytes_read);
20558 line_ptr += bytes_read;
20559
20560 state_machine.handle_set_file (file);
20561 }
20562 break;
20563 case DW_LNS_set_column:
20564 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20565 line_ptr += bytes_read;
20566 break;
20567 case DW_LNS_negate_stmt:
20568 state_machine.handle_negate_stmt ();
20569 break;
20570 case DW_LNS_set_basic_block:
20571 break;
20572 /* Add to the address register of the state machine the
20573 address increment value corresponding to special opcode
20574 255. I.e., this value is scaled by the minimum
20575 instruction length since special opcode 255 would have
20576 scaled the increment. */
20577 case DW_LNS_const_add_pc:
20578 state_machine.handle_const_add_pc ();
20579 break;
20580 case DW_LNS_fixed_advance_pc:
20581 {
20582 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20583 line_ptr += 2;
20584
20585 state_machine.handle_fixed_advance_pc (addr_adj);
20586 }
20587 break;
20588 default:
20589 {
20590 /* Unknown standard opcode, ignore it. */
20591 int i;
20592
20593 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20594 {
20595 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20596 line_ptr += bytes_read;
20597 }
20598 }
20599 }
20600 }
20601
20602 if (!end_sequence)
20603 dwarf2_debug_line_missing_end_sequence_complaint ();
20604
20605 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20606 in which case we still finish recording the last line). */
20607 state_machine.record_line (true);
20608 }
20609 }
20610
20611 /* Decode the Line Number Program (LNP) for the given line_header
20612 structure and CU. The actual information extracted and the type
20613 of structures created from the LNP depends on the value of PST.
20614
20615 1. If PST is NULL, then this procedure uses the data from the program
20616 to create all necessary symbol tables, and their linetables.
20617
20618 2. If PST is not NULL, this procedure reads the program to determine
20619 the list of files included by the unit represented by PST, and
20620 builds all the associated partial symbol tables.
20621
20622 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20623 It is used for relative paths in the line table.
20624 NOTE: When processing partial symtabs (pst != NULL),
20625 comp_dir == pst->dirname.
20626
20627 NOTE: It is important that psymtabs have the same file name (via strcmp)
20628 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20629 symtab we don't use it in the name of the psymtabs we create.
20630 E.g. expand_line_sal requires this when finding psymtabs to expand.
20631 A good testcase for this is mb-inline.exp.
20632
20633 LOWPC is the lowest address in CU (or 0 if not known).
20634
20635 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20636 for its PC<->lines mapping information. Otherwise only the filename
20637 table is read in. */
20638
20639 static void
20640 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20641 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20642 CORE_ADDR lowpc, int decode_mapping)
20643 {
20644 struct objfile *objfile = cu->per_objfile->objfile;
20645 const int decode_for_pst_p = (pst != NULL);
20646
20647 if (decode_mapping)
20648 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20649
20650 if (decode_for_pst_p)
20651 {
20652 /* Now that we're done scanning the Line Header Program, we can
20653 create the psymtab of each included file. */
20654 for (auto &file_entry : lh->file_names ())
20655 if (file_entry.included_p == 1)
20656 {
20657 gdb::unique_xmalloc_ptr<char> name_holder;
20658 const char *include_name =
20659 psymtab_include_file_name (lh, file_entry, pst,
20660 comp_dir, &name_holder);
20661 if (include_name != NULL)
20662 dwarf2_create_include_psymtab (include_name, pst, objfile);
20663 }
20664 }
20665 else
20666 {
20667 /* Make sure a symtab is created for every file, even files
20668 which contain only variables (i.e. no code with associated
20669 line numbers). */
20670 buildsym_compunit *builder = cu->get_builder ();
20671 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20672
20673 for (auto &fe : lh->file_names ())
20674 {
20675 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20676 if (builder->get_current_subfile ()->symtab == NULL)
20677 {
20678 builder->get_current_subfile ()->symtab
20679 = allocate_symtab (cust,
20680 builder->get_current_subfile ()->name);
20681 }
20682 fe.symtab = builder->get_current_subfile ()->symtab;
20683 }
20684 }
20685 }
20686
20687 /* Start a subfile for DWARF. FILENAME is the name of the file and
20688 DIRNAME the name of the source directory which contains FILENAME
20689 or NULL if not known.
20690 This routine tries to keep line numbers from identical absolute and
20691 relative file names in a common subfile.
20692
20693 Using the `list' example from the GDB testsuite, which resides in
20694 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20695 of /srcdir/list0.c yields the following debugging information for list0.c:
20696
20697 DW_AT_name: /srcdir/list0.c
20698 DW_AT_comp_dir: /compdir
20699 files.files[0].name: list0.h
20700 files.files[0].dir: /srcdir
20701 files.files[1].name: list0.c
20702 files.files[1].dir: /srcdir
20703
20704 The line number information for list0.c has to end up in a single
20705 subfile, so that `break /srcdir/list0.c:1' works as expected.
20706 start_subfile will ensure that this happens provided that we pass the
20707 concatenation of files.files[1].dir and files.files[1].name as the
20708 subfile's name. */
20709
20710 static void
20711 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20712 const char *dirname)
20713 {
20714 gdb::unique_xmalloc_ptr<char> copy;
20715
20716 /* In order not to lose the line information directory,
20717 we concatenate it to the filename when it makes sense.
20718 Note that the Dwarf3 standard says (speaking of filenames in line
20719 information): ``The directory index is ignored for file names
20720 that represent full path names''. Thus ignoring dirname in the
20721 `else' branch below isn't an issue. */
20722
20723 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20724 {
20725 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20726 filename = copy.get ();
20727 }
20728
20729 cu->get_builder ()->start_subfile (filename);
20730 }
20731
20732 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20733 buildsym_compunit constructor. */
20734
20735 struct compunit_symtab *
20736 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20737 CORE_ADDR low_pc)
20738 {
20739 gdb_assert (m_builder == nullptr);
20740
20741 m_builder.reset (new struct buildsym_compunit
20742 (this->per_objfile->objfile,
20743 name, comp_dir, language, low_pc));
20744
20745 list_in_scope = get_builder ()->get_file_symbols ();
20746
20747 get_builder ()->record_debugformat ("DWARF 2");
20748 get_builder ()->record_producer (producer);
20749
20750 processing_has_namespace_info = false;
20751
20752 return get_builder ()->get_compunit_symtab ();
20753 }
20754
20755 static void
20756 var_decode_location (struct attribute *attr, struct symbol *sym,
20757 struct dwarf2_cu *cu)
20758 {
20759 struct objfile *objfile = cu->per_objfile->objfile;
20760 struct comp_unit_head *cu_header = &cu->header;
20761
20762 /* NOTE drow/2003-01-30: There used to be a comment and some special
20763 code here to turn a symbol with DW_AT_external and a
20764 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20765 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20766 with some versions of binutils) where shared libraries could have
20767 relocations against symbols in their debug information - the
20768 minimal symbol would have the right address, but the debug info
20769 would not. It's no longer necessary, because we will explicitly
20770 apply relocations when we read in the debug information now. */
20771
20772 /* A DW_AT_location attribute with no contents indicates that a
20773 variable has been optimized away. */
20774 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20775 {
20776 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20777 return;
20778 }
20779
20780 /* Handle one degenerate form of location expression specially, to
20781 preserve GDB's previous behavior when section offsets are
20782 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20783 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20784
20785 if (attr->form_is_block ()
20786 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20787 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20788 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20789 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20790 && (DW_BLOCK (attr)->size
20791 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20792 {
20793 unsigned int dummy;
20794
20795 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20796 SET_SYMBOL_VALUE_ADDRESS
20797 (sym, cu->header.read_address (objfile->obfd,
20798 DW_BLOCK (attr)->data + 1,
20799 &dummy));
20800 else
20801 SET_SYMBOL_VALUE_ADDRESS
20802 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20803 &dummy));
20804 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20805 fixup_symbol_section (sym, objfile);
20806 SET_SYMBOL_VALUE_ADDRESS
20807 (sym,
20808 SYMBOL_VALUE_ADDRESS (sym)
20809 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20810 return;
20811 }
20812
20813 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20814 expression evaluator, and use LOC_COMPUTED only when necessary
20815 (i.e. when the value of a register or memory location is
20816 referenced, or a thread-local block, etc.). Then again, it might
20817 not be worthwhile. I'm assuming that it isn't unless performance
20818 or memory numbers show me otherwise. */
20819
20820 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20821
20822 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20823 cu->has_loclist = true;
20824 }
20825
20826 /* Given a pointer to a DWARF information entry, figure out if we need
20827 to make a symbol table entry for it, and if so, create a new entry
20828 and return a pointer to it.
20829 If TYPE is NULL, determine symbol type from the die, otherwise
20830 used the passed type.
20831 If SPACE is not NULL, use it to hold the new symbol. If it is
20832 NULL, allocate a new symbol on the objfile's obstack. */
20833
20834 static struct symbol *
20835 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20836 struct symbol *space)
20837 {
20838 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20839 struct objfile *objfile = per_objfile->objfile;
20840 struct gdbarch *gdbarch = objfile->arch ();
20841 struct symbol *sym = NULL;
20842 const char *name;
20843 struct attribute *attr = NULL;
20844 struct attribute *attr2 = NULL;
20845 CORE_ADDR baseaddr;
20846 struct pending **list_to_add = NULL;
20847
20848 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20849
20850 baseaddr = objfile->text_section_offset ();
20851
20852 name = dwarf2_name (die, cu);
20853 if (name)
20854 {
20855 int suppress_add = 0;
20856
20857 if (space)
20858 sym = space;
20859 else
20860 sym = new (&objfile->objfile_obstack) symbol;
20861 OBJSTAT (objfile, n_syms++);
20862
20863 /* Cache this symbol's name and the name's demangled form (if any). */
20864 sym->set_language (cu->language, &objfile->objfile_obstack);
20865 /* Fortran does not have mangling standard and the mangling does differ
20866 between gfortran, iFort etc. */
20867 const char *physname
20868 = (cu->language == language_fortran
20869 ? dwarf2_full_name (name, die, cu)
20870 : dwarf2_physname (name, die, cu));
20871 const char *linkagename = dw2_linkage_name (die, cu);
20872
20873 if (linkagename == nullptr || cu->language == language_ada)
20874 sym->set_linkage_name (physname);
20875 else
20876 {
20877 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20878 sym->set_linkage_name (linkagename);
20879 }
20880
20881 /* Default assumptions.
20882 Use the passed type or decode it from the die. */
20883 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20884 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20885 if (type != NULL)
20886 SYMBOL_TYPE (sym) = type;
20887 else
20888 SYMBOL_TYPE (sym) = die_type (die, cu);
20889 attr = dwarf2_attr (die,
20890 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20891 cu);
20892 if (attr != nullptr)
20893 {
20894 SYMBOL_LINE (sym) = DW_UNSND (attr);
20895 }
20896
20897 attr = dwarf2_attr (die,
20898 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20899 cu);
20900 if (attr != nullptr)
20901 {
20902 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20903 struct file_entry *fe;
20904
20905 if (cu->line_header != NULL)
20906 fe = cu->line_header->file_name_at (file_index);
20907 else
20908 fe = NULL;
20909
20910 if (fe == NULL)
20911 complaint (_("file index out of range"));
20912 else
20913 symbol_set_symtab (sym, fe->symtab);
20914 }
20915
20916 switch (die->tag)
20917 {
20918 case DW_TAG_label:
20919 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20920 if (attr != nullptr)
20921 {
20922 CORE_ADDR addr;
20923
20924 addr = attr->value_as_address ();
20925 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20926 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20927 }
20928 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20929 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20930 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20931 add_symbol_to_list (sym, cu->list_in_scope);
20932 break;
20933 case DW_TAG_subprogram:
20934 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20935 finish_block. */
20936 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20937 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20938 if ((attr2 && (DW_UNSND (attr2) != 0))
20939 || cu->language == language_ada
20940 || cu->language == language_fortran)
20941 {
20942 /* Subprograms marked external are stored as a global symbol.
20943 Ada and Fortran subprograms, whether marked external or
20944 not, are always stored as a global symbol, because we want
20945 to be able to access them globally. For instance, we want
20946 to be able to break on a nested subprogram without having
20947 to specify the context. */
20948 list_to_add = cu->get_builder ()->get_global_symbols ();
20949 }
20950 else
20951 {
20952 list_to_add = cu->list_in_scope;
20953 }
20954 break;
20955 case DW_TAG_inlined_subroutine:
20956 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20957 finish_block. */
20958 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20959 SYMBOL_INLINED (sym) = 1;
20960 list_to_add = cu->list_in_scope;
20961 break;
20962 case DW_TAG_template_value_param:
20963 suppress_add = 1;
20964 /* Fall through. */
20965 case DW_TAG_constant:
20966 case DW_TAG_variable:
20967 case DW_TAG_member:
20968 /* Compilation with minimal debug info may result in
20969 variables with missing type entries. Change the
20970 misleading `void' type to something sensible. */
20971 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20972 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20973
20974 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20975 /* In the case of DW_TAG_member, we should only be called for
20976 static const members. */
20977 if (die->tag == DW_TAG_member)
20978 {
20979 /* dwarf2_add_field uses die_is_declaration,
20980 so we do the same. */
20981 gdb_assert (die_is_declaration (die, cu));
20982 gdb_assert (attr);
20983 }
20984 if (attr != nullptr)
20985 {
20986 dwarf2_const_value (attr, sym, cu);
20987 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20988 if (!suppress_add)
20989 {
20990 if (attr2 && (DW_UNSND (attr2) != 0))
20991 list_to_add = cu->get_builder ()->get_global_symbols ();
20992 else
20993 list_to_add = cu->list_in_scope;
20994 }
20995 break;
20996 }
20997 attr = dwarf2_attr (die, DW_AT_location, cu);
20998 if (attr != nullptr)
20999 {
21000 var_decode_location (attr, sym, cu);
21001 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21002
21003 /* Fortran explicitly imports any global symbols to the local
21004 scope by DW_TAG_common_block. */
21005 if (cu->language == language_fortran && die->parent
21006 && die->parent->tag == DW_TAG_common_block)
21007 attr2 = NULL;
21008
21009 if (SYMBOL_CLASS (sym) == LOC_STATIC
21010 && SYMBOL_VALUE_ADDRESS (sym) == 0
21011 && !per_objfile->per_bfd->has_section_at_zero)
21012 {
21013 /* When a static variable is eliminated by the linker,
21014 the corresponding debug information is not stripped
21015 out, but the variable address is set to null;
21016 do not add such variables into symbol table. */
21017 }
21018 else if (attr2 && (DW_UNSND (attr2) != 0))
21019 {
21020 if (SYMBOL_CLASS (sym) == LOC_STATIC
21021 && (objfile->flags & OBJF_MAINLINE) == 0
21022 && per_objfile->per_bfd->can_copy)
21023 {
21024 /* A global static variable might be subject to
21025 copy relocation. We first check for a local
21026 minsym, though, because maybe the symbol was
21027 marked hidden, in which case this would not
21028 apply. */
21029 bound_minimal_symbol found
21030 = (lookup_minimal_symbol_linkage
21031 (sym->linkage_name (), objfile));
21032 if (found.minsym != nullptr)
21033 sym->maybe_copied = 1;
21034 }
21035
21036 /* A variable with DW_AT_external is never static,
21037 but it may be block-scoped. */
21038 list_to_add
21039 = ((cu->list_in_scope
21040 == cu->get_builder ()->get_file_symbols ())
21041 ? cu->get_builder ()->get_global_symbols ()
21042 : cu->list_in_scope);
21043 }
21044 else
21045 list_to_add = cu->list_in_scope;
21046 }
21047 else
21048 {
21049 /* We do not know the address of this symbol.
21050 If it is an external symbol and we have type information
21051 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21052 The address of the variable will then be determined from
21053 the minimal symbol table whenever the variable is
21054 referenced. */
21055 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21056
21057 /* Fortran explicitly imports any global symbols to the local
21058 scope by DW_TAG_common_block. */
21059 if (cu->language == language_fortran && die->parent
21060 && die->parent->tag == DW_TAG_common_block)
21061 {
21062 /* SYMBOL_CLASS doesn't matter here because
21063 read_common_block is going to reset it. */
21064 if (!suppress_add)
21065 list_to_add = cu->list_in_scope;
21066 }
21067 else if (attr2 && (DW_UNSND (attr2) != 0)
21068 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21069 {
21070 /* A variable with DW_AT_external is never static, but it
21071 may be block-scoped. */
21072 list_to_add
21073 = ((cu->list_in_scope
21074 == cu->get_builder ()->get_file_symbols ())
21075 ? cu->get_builder ()->get_global_symbols ()
21076 : cu->list_in_scope);
21077
21078 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21079 }
21080 else if (!die_is_declaration (die, cu))
21081 {
21082 /* Use the default LOC_OPTIMIZED_OUT class. */
21083 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21084 if (!suppress_add)
21085 list_to_add = cu->list_in_scope;
21086 }
21087 }
21088 break;
21089 case DW_TAG_formal_parameter:
21090 {
21091 /* If we are inside a function, mark this as an argument. If
21092 not, we might be looking at an argument to an inlined function
21093 when we do not have enough information to show inlined frames;
21094 pretend it's a local variable in that case so that the user can
21095 still see it. */
21096 struct context_stack *curr
21097 = cu->get_builder ()->get_current_context_stack ();
21098 if (curr != nullptr && curr->name != nullptr)
21099 SYMBOL_IS_ARGUMENT (sym) = 1;
21100 attr = dwarf2_attr (die, DW_AT_location, cu);
21101 if (attr != nullptr)
21102 {
21103 var_decode_location (attr, sym, cu);
21104 }
21105 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21106 if (attr != nullptr)
21107 {
21108 dwarf2_const_value (attr, sym, cu);
21109 }
21110
21111 list_to_add = cu->list_in_scope;
21112 }
21113 break;
21114 case DW_TAG_unspecified_parameters:
21115 /* From varargs functions; gdb doesn't seem to have any
21116 interest in this information, so just ignore it for now.
21117 (FIXME?) */
21118 break;
21119 case DW_TAG_template_type_param:
21120 suppress_add = 1;
21121 /* Fall through. */
21122 case DW_TAG_class_type:
21123 case DW_TAG_interface_type:
21124 case DW_TAG_structure_type:
21125 case DW_TAG_union_type:
21126 case DW_TAG_set_type:
21127 case DW_TAG_enumeration_type:
21128 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21129 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21130
21131 {
21132 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21133 really ever be static objects: otherwise, if you try
21134 to, say, break of a class's method and you're in a file
21135 which doesn't mention that class, it won't work unless
21136 the check for all static symbols in lookup_symbol_aux
21137 saves you. See the OtherFileClass tests in
21138 gdb.c++/namespace.exp. */
21139
21140 if (!suppress_add)
21141 {
21142 buildsym_compunit *builder = cu->get_builder ();
21143 list_to_add
21144 = (cu->list_in_scope == builder->get_file_symbols ()
21145 && cu->language == language_cplus
21146 ? builder->get_global_symbols ()
21147 : cu->list_in_scope);
21148
21149 /* The semantics of C++ state that "struct foo {
21150 ... }" also defines a typedef for "foo". */
21151 if (cu->language == language_cplus
21152 || cu->language == language_ada
21153 || cu->language == language_d
21154 || cu->language == language_rust)
21155 {
21156 /* The symbol's name is already allocated along
21157 with this objfile, so we don't need to
21158 duplicate it for the type. */
21159 if (SYMBOL_TYPE (sym)->name () == 0)
21160 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21161 }
21162 }
21163 }
21164 break;
21165 case DW_TAG_typedef:
21166 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21167 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21168 list_to_add = cu->list_in_scope;
21169 break;
21170 case DW_TAG_base_type:
21171 case DW_TAG_subrange_type:
21172 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21173 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21174 list_to_add = cu->list_in_scope;
21175 break;
21176 case DW_TAG_enumerator:
21177 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21178 if (attr != nullptr)
21179 {
21180 dwarf2_const_value (attr, sym, cu);
21181 }
21182 {
21183 /* NOTE: carlton/2003-11-10: See comment above in the
21184 DW_TAG_class_type, etc. block. */
21185
21186 list_to_add
21187 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21188 && cu->language == language_cplus
21189 ? cu->get_builder ()->get_global_symbols ()
21190 : cu->list_in_scope);
21191 }
21192 break;
21193 case DW_TAG_imported_declaration:
21194 case DW_TAG_namespace:
21195 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21196 list_to_add = cu->get_builder ()->get_global_symbols ();
21197 break;
21198 case DW_TAG_module:
21199 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21200 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21201 list_to_add = cu->get_builder ()->get_global_symbols ();
21202 break;
21203 case DW_TAG_common_block:
21204 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21205 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21206 add_symbol_to_list (sym, cu->list_in_scope);
21207 break;
21208 default:
21209 /* Not a tag we recognize. Hopefully we aren't processing
21210 trash data, but since we must specifically ignore things
21211 we don't recognize, there is nothing else we should do at
21212 this point. */
21213 complaint (_("unsupported tag: '%s'"),
21214 dwarf_tag_name (die->tag));
21215 break;
21216 }
21217
21218 if (suppress_add)
21219 {
21220 sym->hash_next = objfile->template_symbols;
21221 objfile->template_symbols = sym;
21222 list_to_add = NULL;
21223 }
21224
21225 if (list_to_add != NULL)
21226 add_symbol_to_list (sym, list_to_add);
21227
21228 /* For the benefit of old versions of GCC, check for anonymous
21229 namespaces based on the demangled name. */
21230 if (!cu->processing_has_namespace_info
21231 && cu->language == language_cplus)
21232 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21233 }
21234 return (sym);
21235 }
21236
21237 /* Given an attr with a DW_FORM_dataN value in host byte order,
21238 zero-extend it as appropriate for the symbol's type. The DWARF
21239 standard (v4) is not entirely clear about the meaning of using
21240 DW_FORM_dataN for a constant with a signed type, where the type is
21241 wider than the data. The conclusion of a discussion on the DWARF
21242 list was that this is unspecified. We choose to always zero-extend
21243 because that is the interpretation long in use by GCC. */
21244
21245 static gdb_byte *
21246 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21247 struct dwarf2_cu *cu, LONGEST *value, int bits)
21248 {
21249 struct objfile *objfile = cu->per_objfile->objfile;
21250 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21251 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21252 LONGEST l = DW_UNSND (attr);
21253
21254 if (bits < sizeof (*value) * 8)
21255 {
21256 l &= ((LONGEST) 1 << bits) - 1;
21257 *value = l;
21258 }
21259 else if (bits == sizeof (*value) * 8)
21260 *value = l;
21261 else
21262 {
21263 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21264 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21265 return bytes;
21266 }
21267
21268 return NULL;
21269 }
21270
21271 /* Read a constant value from an attribute. Either set *VALUE, or if
21272 the value does not fit in *VALUE, set *BYTES - either already
21273 allocated on the objfile obstack, or newly allocated on OBSTACK,
21274 or, set *BATON, if we translated the constant to a location
21275 expression. */
21276
21277 static void
21278 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21279 const char *name, struct obstack *obstack,
21280 struct dwarf2_cu *cu,
21281 LONGEST *value, const gdb_byte **bytes,
21282 struct dwarf2_locexpr_baton **baton)
21283 {
21284 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21285 struct objfile *objfile = per_objfile->objfile;
21286 struct comp_unit_head *cu_header = &cu->header;
21287 struct dwarf_block *blk;
21288 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21289 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21290
21291 *value = 0;
21292 *bytes = NULL;
21293 *baton = NULL;
21294
21295 switch (attr->form)
21296 {
21297 case DW_FORM_addr:
21298 case DW_FORM_addrx:
21299 case DW_FORM_GNU_addr_index:
21300 {
21301 gdb_byte *data;
21302
21303 if (TYPE_LENGTH (type) != cu_header->addr_size)
21304 dwarf2_const_value_length_mismatch_complaint (name,
21305 cu_header->addr_size,
21306 TYPE_LENGTH (type));
21307 /* Symbols of this form are reasonably rare, so we just
21308 piggyback on the existing location code rather than writing
21309 a new implementation of symbol_computed_ops. */
21310 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21311 (*baton)->per_objfile = per_objfile;
21312 (*baton)->per_cu = cu->per_cu;
21313 gdb_assert ((*baton)->per_cu);
21314
21315 (*baton)->size = 2 + cu_header->addr_size;
21316 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21317 (*baton)->data = data;
21318
21319 data[0] = DW_OP_addr;
21320 store_unsigned_integer (&data[1], cu_header->addr_size,
21321 byte_order, DW_ADDR (attr));
21322 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21323 }
21324 break;
21325 case DW_FORM_string:
21326 case DW_FORM_strp:
21327 case DW_FORM_strx:
21328 case DW_FORM_GNU_str_index:
21329 case DW_FORM_GNU_strp_alt:
21330 /* DW_STRING is already allocated on the objfile obstack, point
21331 directly to it. */
21332 *bytes = (const gdb_byte *) DW_STRING (attr);
21333 break;
21334 case DW_FORM_block1:
21335 case DW_FORM_block2:
21336 case DW_FORM_block4:
21337 case DW_FORM_block:
21338 case DW_FORM_exprloc:
21339 case DW_FORM_data16:
21340 blk = DW_BLOCK (attr);
21341 if (TYPE_LENGTH (type) != blk->size)
21342 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21343 TYPE_LENGTH (type));
21344 *bytes = blk->data;
21345 break;
21346
21347 /* The DW_AT_const_value attributes are supposed to carry the
21348 symbol's value "represented as it would be on the target
21349 architecture." By the time we get here, it's already been
21350 converted to host endianness, so we just need to sign- or
21351 zero-extend it as appropriate. */
21352 case DW_FORM_data1:
21353 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21354 break;
21355 case DW_FORM_data2:
21356 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21357 break;
21358 case DW_FORM_data4:
21359 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21360 break;
21361 case DW_FORM_data8:
21362 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21363 break;
21364
21365 case DW_FORM_sdata:
21366 case DW_FORM_implicit_const:
21367 *value = DW_SND (attr);
21368 break;
21369
21370 case DW_FORM_udata:
21371 *value = DW_UNSND (attr);
21372 break;
21373
21374 default:
21375 complaint (_("unsupported const value attribute form: '%s'"),
21376 dwarf_form_name (attr->form));
21377 *value = 0;
21378 break;
21379 }
21380 }
21381
21382
21383 /* Copy constant value from an attribute to a symbol. */
21384
21385 static void
21386 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21387 struct dwarf2_cu *cu)
21388 {
21389 struct objfile *objfile = cu->per_objfile->objfile;
21390 LONGEST value;
21391 const gdb_byte *bytes;
21392 struct dwarf2_locexpr_baton *baton;
21393
21394 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21395 sym->print_name (),
21396 &objfile->objfile_obstack, cu,
21397 &value, &bytes, &baton);
21398
21399 if (baton != NULL)
21400 {
21401 SYMBOL_LOCATION_BATON (sym) = baton;
21402 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21403 }
21404 else if (bytes != NULL)
21405 {
21406 SYMBOL_VALUE_BYTES (sym) = bytes;
21407 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21408 }
21409 else
21410 {
21411 SYMBOL_VALUE (sym) = value;
21412 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21413 }
21414 }
21415
21416 /* Return the type of the die in question using its DW_AT_type attribute. */
21417
21418 static struct type *
21419 die_type (struct die_info *die, struct dwarf2_cu *cu)
21420 {
21421 struct attribute *type_attr;
21422
21423 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21424 if (!type_attr)
21425 {
21426 struct objfile *objfile = cu->per_objfile->objfile;
21427 /* A missing DW_AT_type represents a void type. */
21428 return objfile_type (objfile)->builtin_void;
21429 }
21430
21431 return lookup_die_type (die, type_attr, cu);
21432 }
21433
21434 /* True iff CU's producer generates GNAT Ada auxiliary information
21435 that allows to find parallel types through that information instead
21436 of having to do expensive parallel lookups by type name. */
21437
21438 static int
21439 need_gnat_info (struct dwarf2_cu *cu)
21440 {
21441 /* Assume that the Ada compiler was GNAT, which always produces
21442 the auxiliary information. */
21443 return (cu->language == language_ada);
21444 }
21445
21446 /* Return the auxiliary type of the die in question using its
21447 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21448 attribute is not present. */
21449
21450 static struct type *
21451 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21452 {
21453 struct attribute *type_attr;
21454
21455 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21456 if (!type_attr)
21457 return NULL;
21458
21459 return lookup_die_type (die, type_attr, cu);
21460 }
21461
21462 /* If DIE has a descriptive_type attribute, then set the TYPE's
21463 descriptive type accordingly. */
21464
21465 static void
21466 set_descriptive_type (struct type *type, struct die_info *die,
21467 struct dwarf2_cu *cu)
21468 {
21469 struct type *descriptive_type = die_descriptive_type (die, cu);
21470
21471 if (descriptive_type)
21472 {
21473 ALLOCATE_GNAT_AUX_TYPE (type);
21474 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21475 }
21476 }
21477
21478 /* Return the containing type of the die in question using its
21479 DW_AT_containing_type attribute. */
21480
21481 static struct type *
21482 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21483 {
21484 struct attribute *type_attr;
21485 struct objfile *objfile = cu->per_objfile->objfile;
21486
21487 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21488 if (!type_attr)
21489 error (_("Dwarf Error: Problem turning containing type into gdb type "
21490 "[in module %s]"), objfile_name (objfile));
21491
21492 return lookup_die_type (die, type_attr, cu);
21493 }
21494
21495 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21496
21497 static struct type *
21498 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21499 {
21500 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21501 struct objfile *objfile = per_objfile->objfile;
21502 char *saved;
21503
21504 std::string message
21505 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21506 objfile_name (objfile),
21507 sect_offset_str (cu->header.sect_off),
21508 sect_offset_str (die->sect_off));
21509 saved = obstack_strdup (&objfile->objfile_obstack, message);
21510
21511 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21512 }
21513
21514 /* Look up the type of DIE in CU using its type attribute ATTR.
21515 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21516 DW_AT_containing_type.
21517 If there is no type substitute an error marker. */
21518
21519 static struct type *
21520 lookup_die_type (struct die_info *die, const struct attribute *attr,
21521 struct dwarf2_cu *cu)
21522 {
21523 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21524 struct objfile *objfile = per_objfile->objfile;
21525 struct type *this_type;
21526
21527 gdb_assert (attr->name == DW_AT_type
21528 || attr->name == DW_AT_GNAT_descriptive_type
21529 || attr->name == DW_AT_containing_type);
21530
21531 /* First see if we have it cached. */
21532
21533 if (attr->form == DW_FORM_GNU_ref_alt)
21534 {
21535 struct dwarf2_per_cu_data *per_cu;
21536 sect_offset sect_off = attr->get_ref_die_offset ();
21537
21538 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
21539 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
21540 }
21541 else if (attr->form_is_ref ())
21542 {
21543 sect_offset sect_off = attr->get_ref_die_offset ();
21544
21545 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
21546 }
21547 else if (attr->form == DW_FORM_ref_sig8)
21548 {
21549 ULONGEST signature = DW_SIGNATURE (attr);
21550
21551 return get_signatured_type (die, signature, cu);
21552 }
21553 else
21554 {
21555 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21556 " at %s [in module %s]"),
21557 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21558 objfile_name (objfile));
21559 return build_error_marker_type (cu, die);
21560 }
21561
21562 /* If not cached we need to read it in. */
21563
21564 if (this_type == NULL)
21565 {
21566 struct die_info *type_die = NULL;
21567 struct dwarf2_cu *type_cu = cu;
21568
21569 if (attr->form_is_ref ())
21570 type_die = follow_die_ref (die, attr, &type_cu);
21571 if (type_die == NULL)
21572 return build_error_marker_type (cu, die);
21573 /* If we find the type now, it's probably because the type came
21574 from an inter-CU reference and the type's CU got expanded before
21575 ours. */
21576 this_type = read_type_die (type_die, type_cu);
21577 }
21578
21579 /* If we still don't have a type use an error marker. */
21580
21581 if (this_type == NULL)
21582 return build_error_marker_type (cu, die);
21583
21584 return this_type;
21585 }
21586
21587 /* Return the type in DIE, CU.
21588 Returns NULL for invalid types.
21589
21590 This first does a lookup in die_type_hash,
21591 and only reads the die in if necessary.
21592
21593 NOTE: This can be called when reading in partial or full symbols. */
21594
21595 static struct type *
21596 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21597 {
21598 struct type *this_type;
21599
21600 this_type = get_die_type (die, cu);
21601 if (this_type)
21602 return this_type;
21603
21604 return read_type_die_1 (die, cu);
21605 }
21606
21607 /* Read the type in DIE, CU.
21608 Returns NULL for invalid types. */
21609
21610 static struct type *
21611 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21612 {
21613 struct type *this_type = NULL;
21614
21615 switch (die->tag)
21616 {
21617 case DW_TAG_class_type:
21618 case DW_TAG_interface_type:
21619 case DW_TAG_structure_type:
21620 case DW_TAG_union_type:
21621 this_type = read_structure_type (die, cu);
21622 break;
21623 case DW_TAG_enumeration_type:
21624 this_type = read_enumeration_type (die, cu);
21625 break;
21626 case DW_TAG_subprogram:
21627 case DW_TAG_subroutine_type:
21628 case DW_TAG_inlined_subroutine:
21629 this_type = read_subroutine_type (die, cu);
21630 break;
21631 case DW_TAG_array_type:
21632 this_type = read_array_type (die, cu);
21633 break;
21634 case DW_TAG_set_type:
21635 this_type = read_set_type (die, cu);
21636 break;
21637 case DW_TAG_pointer_type:
21638 this_type = read_tag_pointer_type (die, cu);
21639 break;
21640 case DW_TAG_ptr_to_member_type:
21641 this_type = read_tag_ptr_to_member_type (die, cu);
21642 break;
21643 case DW_TAG_reference_type:
21644 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21645 break;
21646 case DW_TAG_rvalue_reference_type:
21647 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21648 break;
21649 case DW_TAG_const_type:
21650 this_type = read_tag_const_type (die, cu);
21651 break;
21652 case DW_TAG_volatile_type:
21653 this_type = read_tag_volatile_type (die, cu);
21654 break;
21655 case DW_TAG_restrict_type:
21656 this_type = read_tag_restrict_type (die, cu);
21657 break;
21658 case DW_TAG_string_type:
21659 this_type = read_tag_string_type (die, cu);
21660 break;
21661 case DW_TAG_typedef:
21662 this_type = read_typedef (die, cu);
21663 break;
21664 case DW_TAG_subrange_type:
21665 this_type = read_subrange_type (die, cu);
21666 break;
21667 case DW_TAG_base_type:
21668 this_type = read_base_type (die, cu);
21669 break;
21670 case DW_TAG_unspecified_type:
21671 this_type = read_unspecified_type (die, cu);
21672 break;
21673 case DW_TAG_namespace:
21674 this_type = read_namespace_type (die, cu);
21675 break;
21676 case DW_TAG_module:
21677 this_type = read_module_type (die, cu);
21678 break;
21679 case DW_TAG_atomic_type:
21680 this_type = read_tag_atomic_type (die, cu);
21681 break;
21682 default:
21683 complaint (_("unexpected tag in read_type_die: '%s'"),
21684 dwarf_tag_name (die->tag));
21685 break;
21686 }
21687
21688 return this_type;
21689 }
21690
21691 /* See if we can figure out if the class lives in a namespace. We do
21692 this by looking for a member function; its demangled name will
21693 contain namespace info, if there is any.
21694 Return the computed name or NULL.
21695 Space for the result is allocated on the objfile's obstack.
21696 This is the full-die version of guess_partial_die_structure_name.
21697 In this case we know DIE has no useful parent. */
21698
21699 static const char *
21700 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21701 {
21702 struct die_info *spec_die;
21703 struct dwarf2_cu *spec_cu;
21704 struct die_info *child;
21705 struct objfile *objfile = cu->per_objfile->objfile;
21706
21707 spec_cu = cu;
21708 spec_die = die_specification (die, &spec_cu);
21709 if (spec_die != NULL)
21710 {
21711 die = spec_die;
21712 cu = spec_cu;
21713 }
21714
21715 for (child = die->child;
21716 child != NULL;
21717 child = child->sibling)
21718 {
21719 if (child->tag == DW_TAG_subprogram)
21720 {
21721 const char *linkage_name = dw2_linkage_name (child, cu);
21722
21723 if (linkage_name != NULL)
21724 {
21725 gdb::unique_xmalloc_ptr<char> actual_name
21726 (cu->language_defn->class_name_from_physname (linkage_name));
21727 const char *name = NULL;
21728
21729 if (actual_name != NULL)
21730 {
21731 const char *die_name = dwarf2_name (die, cu);
21732
21733 if (die_name != NULL
21734 && strcmp (die_name, actual_name.get ()) != 0)
21735 {
21736 /* Strip off the class name from the full name.
21737 We want the prefix. */
21738 int die_name_len = strlen (die_name);
21739 int actual_name_len = strlen (actual_name.get ());
21740 const char *ptr = actual_name.get ();
21741
21742 /* Test for '::' as a sanity check. */
21743 if (actual_name_len > die_name_len + 2
21744 && ptr[actual_name_len - die_name_len - 1] == ':')
21745 name = obstack_strndup (
21746 &objfile->per_bfd->storage_obstack,
21747 ptr, actual_name_len - die_name_len - 2);
21748 }
21749 }
21750 return name;
21751 }
21752 }
21753 }
21754
21755 return NULL;
21756 }
21757
21758 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21759 prefix part in such case. See
21760 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21761
21762 static const char *
21763 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21764 {
21765 struct attribute *attr;
21766 const char *base;
21767
21768 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21769 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21770 return NULL;
21771
21772 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21773 return NULL;
21774
21775 attr = dw2_linkage_name_attr (die, cu);
21776 if (attr == NULL || DW_STRING (attr) == NULL)
21777 return NULL;
21778
21779 /* dwarf2_name had to be already called. */
21780 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21781
21782 /* Strip the base name, keep any leading namespaces/classes. */
21783 base = strrchr (DW_STRING (attr), ':');
21784 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21785 return "";
21786
21787 struct objfile *objfile = cu->per_objfile->objfile;
21788 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21789 DW_STRING (attr),
21790 &base[-1] - DW_STRING (attr));
21791 }
21792
21793 /* Return the name of the namespace/class that DIE is defined within,
21794 or "" if we can't tell. The caller should not xfree the result.
21795
21796 For example, if we're within the method foo() in the following
21797 code:
21798
21799 namespace N {
21800 class C {
21801 void foo () {
21802 }
21803 };
21804 }
21805
21806 then determine_prefix on foo's die will return "N::C". */
21807
21808 static const char *
21809 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21810 {
21811 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21812 struct die_info *parent, *spec_die;
21813 struct dwarf2_cu *spec_cu;
21814 struct type *parent_type;
21815 const char *retval;
21816
21817 if (cu->language != language_cplus
21818 && cu->language != language_fortran && cu->language != language_d
21819 && cu->language != language_rust)
21820 return "";
21821
21822 retval = anonymous_struct_prefix (die, cu);
21823 if (retval)
21824 return retval;
21825
21826 /* We have to be careful in the presence of DW_AT_specification.
21827 For example, with GCC 3.4, given the code
21828
21829 namespace N {
21830 void foo() {
21831 // Definition of N::foo.
21832 }
21833 }
21834
21835 then we'll have a tree of DIEs like this:
21836
21837 1: DW_TAG_compile_unit
21838 2: DW_TAG_namespace // N
21839 3: DW_TAG_subprogram // declaration of N::foo
21840 4: DW_TAG_subprogram // definition of N::foo
21841 DW_AT_specification // refers to die #3
21842
21843 Thus, when processing die #4, we have to pretend that we're in
21844 the context of its DW_AT_specification, namely the contex of die
21845 #3. */
21846 spec_cu = cu;
21847 spec_die = die_specification (die, &spec_cu);
21848 if (spec_die == NULL)
21849 parent = die->parent;
21850 else
21851 {
21852 parent = spec_die->parent;
21853 cu = spec_cu;
21854 }
21855
21856 if (parent == NULL)
21857 return "";
21858 else if (parent->building_fullname)
21859 {
21860 const char *name;
21861 const char *parent_name;
21862
21863 /* It has been seen on RealView 2.2 built binaries,
21864 DW_TAG_template_type_param types actually _defined_ as
21865 children of the parent class:
21866
21867 enum E {};
21868 template class <class Enum> Class{};
21869 Class<enum E> class_e;
21870
21871 1: DW_TAG_class_type (Class)
21872 2: DW_TAG_enumeration_type (E)
21873 3: DW_TAG_enumerator (enum1:0)
21874 3: DW_TAG_enumerator (enum2:1)
21875 ...
21876 2: DW_TAG_template_type_param
21877 DW_AT_type DW_FORM_ref_udata (E)
21878
21879 Besides being broken debug info, it can put GDB into an
21880 infinite loop. Consider:
21881
21882 When we're building the full name for Class<E>, we'll start
21883 at Class, and go look over its template type parameters,
21884 finding E. We'll then try to build the full name of E, and
21885 reach here. We're now trying to build the full name of E,
21886 and look over the parent DIE for containing scope. In the
21887 broken case, if we followed the parent DIE of E, we'd again
21888 find Class, and once again go look at its template type
21889 arguments, etc., etc. Simply don't consider such parent die
21890 as source-level parent of this die (it can't be, the language
21891 doesn't allow it), and break the loop here. */
21892 name = dwarf2_name (die, cu);
21893 parent_name = dwarf2_name (parent, cu);
21894 complaint (_("template param type '%s' defined within parent '%s'"),
21895 name ? name : "<unknown>",
21896 parent_name ? parent_name : "<unknown>");
21897 return "";
21898 }
21899 else
21900 switch (parent->tag)
21901 {
21902 case DW_TAG_namespace:
21903 parent_type = read_type_die (parent, cu);
21904 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21905 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21906 Work around this problem here. */
21907 if (cu->language == language_cplus
21908 && strcmp (parent_type->name (), "::") == 0)
21909 return "";
21910 /* We give a name to even anonymous namespaces. */
21911 return parent_type->name ();
21912 case DW_TAG_class_type:
21913 case DW_TAG_interface_type:
21914 case DW_TAG_structure_type:
21915 case DW_TAG_union_type:
21916 case DW_TAG_module:
21917 parent_type = read_type_die (parent, cu);
21918 if (parent_type->name () != NULL)
21919 return parent_type->name ();
21920 else
21921 /* An anonymous structure is only allowed non-static data
21922 members; no typedefs, no member functions, et cetera.
21923 So it does not need a prefix. */
21924 return "";
21925 case DW_TAG_compile_unit:
21926 case DW_TAG_partial_unit:
21927 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21928 if (cu->language == language_cplus
21929 && !per_objfile->per_bfd->types.empty ()
21930 && die->child != NULL
21931 && (die->tag == DW_TAG_class_type
21932 || die->tag == DW_TAG_structure_type
21933 || die->tag == DW_TAG_union_type))
21934 {
21935 const char *name = guess_full_die_structure_name (die, cu);
21936 if (name != NULL)
21937 return name;
21938 }
21939 return "";
21940 case DW_TAG_subprogram:
21941 /* Nested subroutines in Fortran get a prefix with the name
21942 of the parent's subroutine. */
21943 if (cu->language == language_fortran)
21944 {
21945 if ((die->tag == DW_TAG_subprogram)
21946 && (dwarf2_name (parent, cu) != NULL))
21947 return dwarf2_name (parent, cu);
21948 }
21949 return determine_prefix (parent, cu);
21950 case DW_TAG_enumeration_type:
21951 parent_type = read_type_die (parent, cu);
21952 if (TYPE_DECLARED_CLASS (parent_type))
21953 {
21954 if (parent_type->name () != NULL)
21955 return parent_type->name ();
21956 return "";
21957 }
21958 /* Fall through. */
21959 default:
21960 return determine_prefix (parent, cu);
21961 }
21962 }
21963
21964 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21965 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21966 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21967 an obconcat, otherwise allocate storage for the result. The CU argument is
21968 used to determine the language and hence, the appropriate separator. */
21969
21970 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21971
21972 static char *
21973 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21974 int physname, struct dwarf2_cu *cu)
21975 {
21976 const char *lead = "";
21977 const char *sep;
21978
21979 if (suffix == NULL || suffix[0] == '\0'
21980 || prefix == NULL || prefix[0] == '\0')
21981 sep = "";
21982 else if (cu->language == language_d)
21983 {
21984 /* For D, the 'main' function could be defined in any module, but it
21985 should never be prefixed. */
21986 if (strcmp (suffix, "D main") == 0)
21987 {
21988 prefix = "";
21989 sep = "";
21990 }
21991 else
21992 sep = ".";
21993 }
21994 else if (cu->language == language_fortran && physname)
21995 {
21996 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21997 DW_AT_MIPS_linkage_name is preferred and used instead. */
21998
21999 lead = "__";
22000 sep = "_MOD_";
22001 }
22002 else
22003 sep = "::";
22004
22005 if (prefix == NULL)
22006 prefix = "";
22007 if (suffix == NULL)
22008 suffix = "";
22009
22010 if (obs == NULL)
22011 {
22012 char *retval
22013 = ((char *)
22014 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22015
22016 strcpy (retval, lead);
22017 strcat (retval, prefix);
22018 strcat (retval, sep);
22019 strcat (retval, suffix);
22020 return retval;
22021 }
22022 else
22023 {
22024 /* We have an obstack. */
22025 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22026 }
22027 }
22028
22029 /* Get name of a die, return NULL if not found. */
22030
22031 static const char *
22032 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22033 struct objfile *objfile)
22034 {
22035 if (name && cu->language == language_cplus)
22036 {
22037 gdb::unique_xmalloc_ptr<char> canon_name
22038 = cp_canonicalize_string (name);
22039
22040 if (canon_name != nullptr)
22041 name = objfile->intern (canon_name.get ());
22042 }
22043
22044 return name;
22045 }
22046
22047 /* Get name of a die, return NULL if not found.
22048 Anonymous namespaces are converted to their magic string. */
22049
22050 static const char *
22051 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22052 {
22053 struct attribute *attr;
22054 struct objfile *objfile = cu->per_objfile->objfile;
22055
22056 attr = dwarf2_attr (die, DW_AT_name, cu);
22057 if ((!attr || !DW_STRING (attr))
22058 && die->tag != DW_TAG_namespace
22059 && die->tag != DW_TAG_class_type
22060 && die->tag != DW_TAG_interface_type
22061 && die->tag != DW_TAG_structure_type
22062 && die->tag != DW_TAG_union_type)
22063 return NULL;
22064
22065 switch (die->tag)
22066 {
22067 case DW_TAG_compile_unit:
22068 case DW_TAG_partial_unit:
22069 /* Compilation units have a DW_AT_name that is a filename, not
22070 a source language identifier. */
22071 case DW_TAG_enumeration_type:
22072 case DW_TAG_enumerator:
22073 /* These tags always have simple identifiers already; no need
22074 to canonicalize them. */
22075 return DW_STRING (attr);
22076
22077 case DW_TAG_namespace:
22078 if (attr != NULL && DW_STRING (attr) != NULL)
22079 return DW_STRING (attr);
22080 return CP_ANONYMOUS_NAMESPACE_STR;
22081
22082 case DW_TAG_class_type:
22083 case DW_TAG_interface_type:
22084 case DW_TAG_structure_type:
22085 case DW_TAG_union_type:
22086 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22087 structures or unions. These were of the form "._%d" in GCC 4.1,
22088 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22089 and GCC 4.4. We work around this problem by ignoring these. */
22090 if (attr && DW_STRING (attr)
22091 && (startswith (DW_STRING (attr), "._")
22092 || startswith (DW_STRING (attr), "<anonymous")))
22093 return NULL;
22094
22095 /* GCC might emit a nameless typedef that has a linkage name. See
22096 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22097 if (!attr || DW_STRING (attr) == NULL)
22098 {
22099 attr = dw2_linkage_name_attr (die, cu);
22100 if (attr == NULL || DW_STRING (attr) == NULL)
22101 return NULL;
22102
22103 /* Avoid demangling DW_STRING (attr) the second time on a second
22104 call for the same DIE. */
22105 if (!DW_STRING_IS_CANONICAL (attr))
22106 {
22107 gdb::unique_xmalloc_ptr<char> demangled
22108 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22109 if (demangled == nullptr)
22110 return nullptr;
22111
22112 DW_STRING (attr) = objfile->intern (demangled.get ());
22113 DW_STRING_IS_CANONICAL (attr) = 1;
22114 }
22115
22116 /* Strip any leading namespaces/classes, keep only the base name.
22117 DW_AT_name for named DIEs does not contain the prefixes. */
22118 const char *base = strrchr (DW_STRING (attr), ':');
22119 if (base && base > DW_STRING (attr) && base[-1] == ':')
22120 return &base[1];
22121 else
22122 return DW_STRING (attr);
22123 }
22124 break;
22125
22126 default:
22127 break;
22128 }
22129
22130 if (!DW_STRING_IS_CANONICAL (attr))
22131 {
22132 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22133 objfile);
22134 DW_STRING_IS_CANONICAL (attr) = 1;
22135 }
22136 return DW_STRING (attr);
22137 }
22138
22139 /* Return the die that this die in an extension of, or NULL if there
22140 is none. *EXT_CU is the CU containing DIE on input, and the CU
22141 containing the return value on output. */
22142
22143 static struct die_info *
22144 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22145 {
22146 struct attribute *attr;
22147
22148 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22149 if (attr == NULL)
22150 return NULL;
22151
22152 return follow_die_ref (die, attr, ext_cu);
22153 }
22154
22155 static void
22156 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22157 {
22158 unsigned int i;
22159
22160 print_spaces (indent, f);
22161 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22162 dwarf_tag_name (die->tag), die->abbrev,
22163 sect_offset_str (die->sect_off));
22164
22165 if (die->parent != NULL)
22166 {
22167 print_spaces (indent, f);
22168 fprintf_unfiltered (f, " parent at offset: %s\n",
22169 sect_offset_str (die->parent->sect_off));
22170 }
22171
22172 print_spaces (indent, f);
22173 fprintf_unfiltered (f, " has children: %s\n",
22174 dwarf_bool_name (die->child != NULL));
22175
22176 print_spaces (indent, f);
22177 fprintf_unfiltered (f, " attributes:\n");
22178
22179 for (i = 0; i < die->num_attrs; ++i)
22180 {
22181 print_spaces (indent, f);
22182 fprintf_unfiltered (f, " %s (%s) ",
22183 dwarf_attr_name (die->attrs[i].name),
22184 dwarf_form_name (die->attrs[i].form));
22185
22186 switch (die->attrs[i].form)
22187 {
22188 case DW_FORM_addr:
22189 case DW_FORM_addrx:
22190 case DW_FORM_GNU_addr_index:
22191 fprintf_unfiltered (f, "address: ");
22192 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22193 break;
22194 case DW_FORM_block2:
22195 case DW_FORM_block4:
22196 case DW_FORM_block:
22197 case DW_FORM_block1:
22198 fprintf_unfiltered (f, "block: size %s",
22199 pulongest (DW_BLOCK (&die->attrs[i])->size));
22200 break;
22201 case DW_FORM_exprloc:
22202 fprintf_unfiltered (f, "expression: size %s",
22203 pulongest (DW_BLOCK (&die->attrs[i])->size));
22204 break;
22205 case DW_FORM_data16:
22206 fprintf_unfiltered (f, "constant of 16 bytes");
22207 break;
22208 case DW_FORM_ref_addr:
22209 fprintf_unfiltered (f, "ref address: ");
22210 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22211 break;
22212 case DW_FORM_GNU_ref_alt:
22213 fprintf_unfiltered (f, "alt ref address: ");
22214 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22215 break;
22216 case DW_FORM_ref1:
22217 case DW_FORM_ref2:
22218 case DW_FORM_ref4:
22219 case DW_FORM_ref8:
22220 case DW_FORM_ref_udata:
22221 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22222 (long) (DW_UNSND (&die->attrs[i])));
22223 break;
22224 case DW_FORM_data1:
22225 case DW_FORM_data2:
22226 case DW_FORM_data4:
22227 case DW_FORM_data8:
22228 case DW_FORM_udata:
22229 case DW_FORM_sdata:
22230 fprintf_unfiltered (f, "constant: %s",
22231 pulongest (DW_UNSND (&die->attrs[i])));
22232 break;
22233 case DW_FORM_sec_offset:
22234 fprintf_unfiltered (f, "section offset: %s",
22235 pulongest (DW_UNSND (&die->attrs[i])));
22236 break;
22237 case DW_FORM_ref_sig8:
22238 fprintf_unfiltered (f, "signature: %s",
22239 hex_string (DW_SIGNATURE (&die->attrs[i])));
22240 break;
22241 case DW_FORM_string:
22242 case DW_FORM_strp:
22243 case DW_FORM_line_strp:
22244 case DW_FORM_strx:
22245 case DW_FORM_GNU_str_index:
22246 case DW_FORM_GNU_strp_alt:
22247 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22248 DW_STRING (&die->attrs[i])
22249 ? DW_STRING (&die->attrs[i]) : "",
22250 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22251 break;
22252 case DW_FORM_flag:
22253 if (DW_UNSND (&die->attrs[i]))
22254 fprintf_unfiltered (f, "flag: TRUE");
22255 else
22256 fprintf_unfiltered (f, "flag: FALSE");
22257 break;
22258 case DW_FORM_flag_present:
22259 fprintf_unfiltered (f, "flag: TRUE");
22260 break;
22261 case DW_FORM_indirect:
22262 /* The reader will have reduced the indirect form to
22263 the "base form" so this form should not occur. */
22264 fprintf_unfiltered (f,
22265 "unexpected attribute form: DW_FORM_indirect");
22266 break;
22267 case DW_FORM_implicit_const:
22268 fprintf_unfiltered (f, "constant: %s",
22269 plongest (DW_SND (&die->attrs[i])));
22270 break;
22271 default:
22272 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22273 die->attrs[i].form);
22274 break;
22275 }
22276 fprintf_unfiltered (f, "\n");
22277 }
22278 }
22279
22280 static void
22281 dump_die_for_error (struct die_info *die)
22282 {
22283 dump_die_shallow (gdb_stderr, 0, die);
22284 }
22285
22286 static void
22287 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22288 {
22289 int indent = level * 4;
22290
22291 gdb_assert (die != NULL);
22292
22293 if (level >= max_level)
22294 return;
22295
22296 dump_die_shallow (f, indent, die);
22297
22298 if (die->child != NULL)
22299 {
22300 print_spaces (indent, f);
22301 fprintf_unfiltered (f, " Children:");
22302 if (level + 1 < max_level)
22303 {
22304 fprintf_unfiltered (f, "\n");
22305 dump_die_1 (f, level + 1, max_level, die->child);
22306 }
22307 else
22308 {
22309 fprintf_unfiltered (f,
22310 " [not printed, max nesting level reached]\n");
22311 }
22312 }
22313
22314 if (die->sibling != NULL && level > 0)
22315 {
22316 dump_die_1 (f, level, max_level, die->sibling);
22317 }
22318 }
22319
22320 /* This is called from the pdie macro in gdbinit.in.
22321 It's not static so gcc will keep a copy callable from gdb. */
22322
22323 void
22324 dump_die (struct die_info *die, int max_level)
22325 {
22326 dump_die_1 (gdb_stdlog, 0, max_level, die);
22327 }
22328
22329 static void
22330 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22331 {
22332 void **slot;
22333
22334 slot = htab_find_slot_with_hash (cu->die_hash, die,
22335 to_underlying (die->sect_off),
22336 INSERT);
22337
22338 *slot = die;
22339 }
22340
22341 /* Follow reference or signature attribute ATTR of SRC_DIE.
22342 On entry *REF_CU is the CU of SRC_DIE.
22343 On exit *REF_CU is the CU of the result. */
22344
22345 static struct die_info *
22346 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22347 struct dwarf2_cu **ref_cu)
22348 {
22349 struct die_info *die;
22350
22351 if (attr->form_is_ref ())
22352 die = follow_die_ref (src_die, attr, ref_cu);
22353 else if (attr->form == DW_FORM_ref_sig8)
22354 die = follow_die_sig (src_die, attr, ref_cu);
22355 else
22356 {
22357 dump_die_for_error (src_die);
22358 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22359 objfile_name ((*ref_cu)->per_objfile->objfile));
22360 }
22361
22362 return die;
22363 }
22364
22365 /* Follow reference OFFSET.
22366 On entry *REF_CU is the CU of the source die referencing OFFSET.
22367 On exit *REF_CU is the CU of the result.
22368 Returns NULL if OFFSET is invalid. */
22369
22370 static struct die_info *
22371 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22372 struct dwarf2_cu **ref_cu)
22373 {
22374 struct die_info temp_die;
22375 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22376 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22377
22378 gdb_assert (cu->per_cu != NULL);
22379
22380 target_cu = cu;
22381
22382 if (cu->per_cu->is_debug_types)
22383 {
22384 /* .debug_types CUs cannot reference anything outside their CU.
22385 If they need to, they have to reference a signatured type via
22386 DW_FORM_ref_sig8. */
22387 if (!cu->header.offset_in_cu_p (sect_off))
22388 return NULL;
22389 }
22390 else if (offset_in_dwz != cu->per_cu->is_dwz
22391 || !cu->header.offset_in_cu_p (sect_off))
22392 {
22393 struct dwarf2_per_cu_data *per_cu;
22394
22395 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22396 per_objfile);
22397
22398 /* If necessary, add it to the queue and load its DIEs. */
22399 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22400 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
22401
22402 target_cu = per_objfile->get_cu (per_cu);
22403 }
22404 else if (cu->dies == NULL)
22405 {
22406 /* We're loading full DIEs during partial symbol reading. */
22407 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22408 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
22409 }
22410
22411 *ref_cu = target_cu;
22412 temp_die.sect_off = sect_off;
22413
22414 if (target_cu != cu)
22415 target_cu->ancestor = cu;
22416
22417 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22418 &temp_die,
22419 to_underlying (sect_off));
22420 }
22421
22422 /* Follow reference attribute ATTR of SRC_DIE.
22423 On entry *REF_CU is the CU of SRC_DIE.
22424 On exit *REF_CU is the CU of the result. */
22425
22426 static struct die_info *
22427 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22428 struct dwarf2_cu **ref_cu)
22429 {
22430 sect_offset sect_off = attr->get_ref_die_offset ();
22431 struct dwarf2_cu *cu = *ref_cu;
22432 struct die_info *die;
22433
22434 die = follow_die_offset (sect_off,
22435 (attr->form == DW_FORM_GNU_ref_alt
22436 || cu->per_cu->is_dwz),
22437 ref_cu);
22438 if (!die)
22439 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22440 "at %s [in module %s]"),
22441 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22442 objfile_name (cu->per_objfile->objfile));
22443
22444 return die;
22445 }
22446
22447 /* See read.h. */
22448
22449 struct dwarf2_locexpr_baton
22450 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22451 dwarf2_per_cu_data *per_cu,
22452 dwarf2_per_objfile *per_objfile,
22453 CORE_ADDR (*get_frame_pc) (void *baton),
22454 void *baton, bool resolve_abstract_p)
22455 {
22456 struct die_info *die;
22457 struct attribute *attr;
22458 struct dwarf2_locexpr_baton retval;
22459 struct objfile *objfile = per_objfile->objfile;
22460
22461 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22462 if (cu == nullptr)
22463 cu = load_cu (per_cu, per_objfile, false);
22464
22465 if (cu == nullptr)
22466 {
22467 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22468 Instead just throw an error, not much else we can do. */
22469 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22470 sect_offset_str (sect_off), objfile_name (objfile));
22471 }
22472
22473 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22474 if (!die)
22475 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22476 sect_offset_str (sect_off), objfile_name (objfile));
22477
22478 attr = dwarf2_attr (die, DW_AT_location, cu);
22479 if (!attr && resolve_abstract_p
22480 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22481 != per_objfile->per_bfd->abstract_to_concrete.end ()))
22482 {
22483 CORE_ADDR pc = (*get_frame_pc) (baton);
22484 CORE_ADDR baseaddr = objfile->text_section_offset ();
22485 struct gdbarch *gdbarch = objfile->arch ();
22486
22487 for (const auto &cand_off
22488 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22489 {
22490 struct dwarf2_cu *cand_cu = cu;
22491 struct die_info *cand
22492 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22493 if (!cand
22494 || !cand->parent
22495 || cand->parent->tag != DW_TAG_subprogram)
22496 continue;
22497
22498 CORE_ADDR pc_low, pc_high;
22499 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22500 if (pc_low == ((CORE_ADDR) -1))
22501 continue;
22502 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22503 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22504 if (!(pc_low <= pc && pc < pc_high))
22505 continue;
22506
22507 die = cand;
22508 attr = dwarf2_attr (die, DW_AT_location, cu);
22509 break;
22510 }
22511 }
22512
22513 if (!attr)
22514 {
22515 /* DWARF: "If there is no such attribute, then there is no effect.".
22516 DATA is ignored if SIZE is 0. */
22517
22518 retval.data = NULL;
22519 retval.size = 0;
22520 }
22521 else if (attr->form_is_section_offset ())
22522 {
22523 struct dwarf2_loclist_baton loclist_baton;
22524 CORE_ADDR pc = (*get_frame_pc) (baton);
22525 size_t size;
22526
22527 fill_in_loclist_baton (cu, &loclist_baton, attr);
22528
22529 retval.data = dwarf2_find_location_expression (&loclist_baton,
22530 &size, pc);
22531 retval.size = size;
22532 }
22533 else
22534 {
22535 if (!attr->form_is_block ())
22536 error (_("Dwarf Error: DIE at %s referenced in module %s "
22537 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22538 sect_offset_str (sect_off), objfile_name (objfile));
22539
22540 retval.data = DW_BLOCK (attr)->data;
22541 retval.size = DW_BLOCK (attr)->size;
22542 }
22543 retval.per_objfile = per_objfile;
22544 retval.per_cu = cu->per_cu;
22545
22546 per_objfile->age_comp_units ();
22547
22548 return retval;
22549 }
22550
22551 /* See read.h. */
22552
22553 struct dwarf2_locexpr_baton
22554 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22555 dwarf2_per_cu_data *per_cu,
22556 dwarf2_per_objfile *per_objfile,
22557 CORE_ADDR (*get_frame_pc) (void *baton),
22558 void *baton)
22559 {
22560 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22561
22562 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22563 get_frame_pc, baton);
22564 }
22565
22566 /* Write a constant of a given type as target-ordered bytes into
22567 OBSTACK. */
22568
22569 static const gdb_byte *
22570 write_constant_as_bytes (struct obstack *obstack,
22571 enum bfd_endian byte_order,
22572 struct type *type,
22573 ULONGEST value,
22574 LONGEST *len)
22575 {
22576 gdb_byte *result;
22577
22578 *len = TYPE_LENGTH (type);
22579 result = (gdb_byte *) obstack_alloc (obstack, *len);
22580 store_unsigned_integer (result, *len, byte_order, value);
22581
22582 return result;
22583 }
22584
22585 /* See read.h. */
22586
22587 const gdb_byte *
22588 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22589 dwarf2_per_cu_data *per_cu,
22590 dwarf2_per_objfile *per_objfile,
22591 obstack *obstack,
22592 LONGEST *len)
22593 {
22594 struct die_info *die;
22595 struct attribute *attr;
22596 const gdb_byte *result = NULL;
22597 struct type *type;
22598 LONGEST value;
22599 enum bfd_endian byte_order;
22600 struct objfile *objfile = per_objfile->objfile;
22601
22602 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22603 if (cu == nullptr)
22604 cu = load_cu (per_cu, per_objfile, false);
22605
22606 if (cu == nullptr)
22607 {
22608 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22609 Instead just throw an error, not much else we can do. */
22610 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22611 sect_offset_str (sect_off), objfile_name (objfile));
22612 }
22613
22614 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22615 if (!die)
22616 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22617 sect_offset_str (sect_off), objfile_name (objfile));
22618
22619 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22620 if (attr == NULL)
22621 return NULL;
22622
22623 byte_order = (bfd_big_endian (objfile->obfd)
22624 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22625
22626 switch (attr->form)
22627 {
22628 case DW_FORM_addr:
22629 case DW_FORM_addrx:
22630 case DW_FORM_GNU_addr_index:
22631 {
22632 gdb_byte *tem;
22633
22634 *len = cu->header.addr_size;
22635 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22636 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22637 result = tem;
22638 }
22639 break;
22640 case DW_FORM_string:
22641 case DW_FORM_strp:
22642 case DW_FORM_strx:
22643 case DW_FORM_GNU_str_index:
22644 case DW_FORM_GNU_strp_alt:
22645 /* DW_STRING is already allocated on the objfile obstack, point
22646 directly to it. */
22647 result = (const gdb_byte *) DW_STRING (attr);
22648 *len = strlen (DW_STRING (attr));
22649 break;
22650 case DW_FORM_block1:
22651 case DW_FORM_block2:
22652 case DW_FORM_block4:
22653 case DW_FORM_block:
22654 case DW_FORM_exprloc:
22655 case DW_FORM_data16:
22656 result = DW_BLOCK (attr)->data;
22657 *len = DW_BLOCK (attr)->size;
22658 break;
22659
22660 /* The DW_AT_const_value attributes are supposed to carry the
22661 symbol's value "represented as it would be on the target
22662 architecture." By the time we get here, it's already been
22663 converted to host endianness, so we just need to sign- or
22664 zero-extend it as appropriate. */
22665 case DW_FORM_data1:
22666 type = die_type (die, cu);
22667 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22668 if (result == NULL)
22669 result = write_constant_as_bytes (obstack, byte_order,
22670 type, value, len);
22671 break;
22672 case DW_FORM_data2:
22673 type = die_type (die, cu);
22674 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22675 if (result == NULL)
22676 result = write_constant_as_bytes (obstack, byte_order,
22677 type, value, len);
22678 break;
22679 case DW_FORM_data4:
22680 type = die_type (die, cu);
22681 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22682 if (result == NULL)
22683 result = write_constant_as_bytes (obstack, byte_order,
22684 type, value, len);
22685 break;
22686 case DW_FORM_data8:
22687 type = die_type (die, cu);
22688 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22689 if (result == NULL)
22690 result = write_constant_as_bytes (obstack, byte_order,
22691 type, value, len);
22692 break;
22693
22694 case DW_FORM_sdata:
22695 case DW_FORM_implicit_const:
22696 type = die_type (die, cu);
22697 result = write_constant_as_bytes (obstack, byte_order,
22698 type, DW_SND (attr), len);
22699 break;
22700
22701 case DW_FORM_udata:
22702 type = die_type (die, cu);
22703 result = write_constant_as_bytes (obstack, byte_order,
22704 type, DW_UNSND (attr), len);
22705 break;
22706
22707 default:
22708 complaint (_("unsupported const value attribute form: '%s'"),
22709 dwarf_form_name (attr->form));
22710 break;
22711 }
22712
22713 return result;
22714 }
22715
22716 /* See read.h. */
22717
22718 struct type *
22719 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22720 dwarf2_per_cu_data *per_cu,
22721 dwarf2_per_objfile *per_objfile)
22722 {
22723 struct die_info *die;
22724
22725 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22726 if (cu == nullptr)
22727 cu = load_cu (per_cu, per_objfile, false);
22728
22729 if (cu == nullptr)
22730 return nullptr;
22731
22732 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22733 if (!die)
22734 return NULL;
22735
22736 return die_type (die, cu);
22737 }
22738
22739 /* See read.h. */
22740
22741 struct type *
22742 dwarf2_get_die_type (cu_offset die_offset,
22743 dwarf2_per_cu_data *per_cu,
22744 dwarf2_per_objfile *per_objfile)
22745 {
22746 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22747 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22748 }
22749
22750 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22751 On entry *REF_CU is the CU of SRC_DIE.
22752 On exit *REF_CU is the CU of the result.
22753 Returns NULL if the referenced DIE isn't found. */
22754
22755 static struct die_info *
22756 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22757 struct dwarf2_cu **ref_cu)
22758 {
22759 struct die_info temp_die;
22760 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22761 struct die_info *die;
22762 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
22763
22764
22765 /* While it might be nice to assert sig_type->type == NULL here,
22766 we can get here for DW_AT_imported_declaration where we need
22767 the DIE not the type. */
22768
22769 /* If necessary, add it to the queue and load its DIEs. */
22770
22771 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
22772 language_minimal))
22773 read_signatured_type (sig_type, per_objfile);
22774
22775 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
22776 gdb_assert (sig_cu != NULL);
22777 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22778 temp_die.sect_off = sig_type->type_offset_in_section;
22779 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22780 to_underlying (temp_die.sect_off));
22781 if (die)
22782 {
22783 /* For .gdb_index version 7 keep track of included TUs.
22784 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22785 if (per_objfile->per_bfd->index_table != NULL
22786 && per_objfile->per_bfd->index_table->version <= 7)
22787 {
22788 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22789 }
22790
22791 *ref_cu = sig_cu;
22792 if (sig_cu != cu)
22793 sig_cu->ancestor = cu;
22794
22795 return die;
22796 }
22797
22798 return NULL;
22799 }
22800
22801 /* Follow signatured type referenced by ATTR in SRC_DIE.
22802 On entry *REF_CU is the CU of SRC_DIE.
22803 On exit *REF_CU is the CU of the result.
22804 The result is the DIE of the type.
22805 If the referenced type cannot be found an error is thrown. */
22806
22807 static struct die_info *
22808 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22809 struct dwarf2_cu **ref_cu)
22810 {
22811 ULONGEST signature = DW_SIGNATURE (attr);
22812 struct signatured_type *sig_type;
22813 struct die_info *die;
22814
22815 gdb_assert (attr->form == DW_FORM_ref_sig8);
22816
22817 sig_type = lookup_signatured_type (*ref_cu, signature);
22818 /* sig_type will be NULL if the signatured type is missing from
22819 the debug info. */
22820 if (sig_type == NULL)
22821 {
22822 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22823 " from DIE at %s [in module %s]"),
22824 hex_string (signature), sect_offset_str (src_die->sect_off),
22825 objfile_name ((*ref_cu)->per_objfile->objfile));
22826 }
22827
22828 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22829 if (die == NULL)
22830 {
22831 dump_die_for_error (src_die);
22832 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22833 " from DIE at %s [in module %s]"),
22834 hex_string (signature), sect_offset_str (src_die->sect_off),
22835 objfile_name ((*ref_cu)->per_objfile->objfile));
22836 }
22837
22838 return die;
22839 }
22840
22841 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22842 reading in and processing the type unit if necessary. */
22843
22844 static struct type *
22845 get_signatured_type (struct die_info *die, ULONGEST signature,
22846 struct dwarf2_cu *cu)
22847 {
22848 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22849 struct signatured_type *sig_type;
22850 struct dwarf2_cu *type_cu;
22851 struct die_info *type_die;
22852 struct type *type;
22853
22854 sig_type = lookup_signatured_type (cu, signature);
22855 /* sig_type will be NULL if the signatured type is missing from
22856 the debug info. */
22857 if (sig_type == NULL)
22858 {
22859 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22860 " from DIE at %s [in module %s]"),
22861 hex_string (signature), sect_offset_str (die->sect_off),
22862 objfile_name (per_objfile->objfile));
22863 return build_error_marker_type (cu, die);
22864 }
22865
22866 /* If we already know the type we're done. */
22867 type = per_objfile->get_type_for_signatured_type (sig_type);
22868 if (type != nullptr)
22869 return type;
22870
22871 type_cu = cu;
22872 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22873 if (type_die != NULL)
22874 {
22875 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22876 is created. This is important, for example, because for c++ classes
22877 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22878 type = read_type_die (type_die, type_cu);
22879 if (type == NULL)
22880 {
22881 complaint (_("Dwarf Error: Cannot build signatured type %s"
22882 " referenced from DIE at %s [in module %s]"),
22883 hex_string (signature), sect_offset_str (die->sect_off),
22884 objfile_name (per_objfile->objfile));
22885 type = build_error_marker_type (cu, die);
22886 }
22887 }
22888 else
22889 {
22890 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22891 " from DIE at %s [in module %s]"),
22892 hex_string (signature), sect_offset_str (die->sect_off),
22893 objfile_name (per_objfile->objfile));
22894 type = build_error_marker_type (cu, die);
22895 }
22896
22897 per_objfile->set_type_for_signatured_type (sig_type, type);
22898
22899 return type;
22900 }
22901
22902 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22903 reading in and processing the type unit if necessary. */
22904
22905 static struct type *
22906 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22907 struct dwarf2_cu *cu) /* ARI: editCase function */
22908 {
22909 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22910 if (attr->form_is_ref ())
22911 {
22912 struct dwarf2_cu *type_cu = cu;
22913 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22914
22915 return read_type_die (type_die, type_cu);
22916 }
22917 else if (attr->form == DW_FORM_ref_sig8)
22918 {
22919 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22920 }
22921 else
22922 {
22923 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22924
22925 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22926 " at %s [in module %s]"),
22927 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22928 objfile_name (per_objfile->objfile));
22929 return build_error_marker_type (cu, die);
22930 }
22931 }
22932
22933 /* Load the DIEs associated with type unit PER_CU into memory. */
22934
22935 static void
22936 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22937 dwarf2_per_objfile *per_objfile)
22938 {
22939 struct signatured_type *sig_type;
22940
22941 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22942 gdb_assert (! per_cu->type_unit_group_p ());
22943
22944 /* We have the per_cu, but we need the signatured_type.
22945 Fortunately this is an easy translation. */
22946 gdb_assert (per_cu->is_debug_types);
22947 sig_type = (struct signatured_type *) per_cu;
22948
22949 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22950
22951 read_signatured_type (sig_type, per_objfile);
22952
22953 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22954 }
22955
22956 /* Read in a signatured type and build its CU and DIEs.
22957 If the type is a stub for the real type in a DWO file,
22958 read in the real type from the DWO file as well. */
22959
22960 static void
22961 read_signatured_type (signatured_type *sig_type,
22962 dwarf2_per_objfile *per_objfile)
22963 {
22964 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22965
22966 gdb_assert (per_cu->is_debug_types);
22967 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22968
22969 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
22970
22971 if (!reader.dummy_p)
22972 {
22973 struct dwarf2_cu *cu = reader.cu;
22974 const gdb_byte *info_ptr = reader.info_ptr;
22975
22976 gdb_assert (cu->die_hash == NULL);
22977 cu->die_hash =
22978 htab_create_alloc_ex (cu->header.length / 12,
22979 die_hash,
22980 die_eq,
22981 NULL,
22982 &cu->comp_unit_obstack,
22983 hashtab_obstack_allocate,
22984 dummy_obstack_deallocate);
22985
22986 if (reader.comp_unit_die->has_children)
22987 reader.comp_unit_die->child
22988 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22989 reader.comp_unit_die);
22990 cu->dies = reader.comp_unit_die;
22991 /* comp_unit_die is not stored in die_hash, no need. */
22992
22993 /* We try not to read any attributes in this function, because
22994 not all CUs needed for references have been loaded yet, and
22995 symbol table processing isn't initialized. But we have to
22996 set the CU language, or we won't be able to build types
22997 correctly. Similarly, if we do not read the producer, we can
22998 not apply producer-specific interpretation. */
22999 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23000
23001 reader.keep ();
23002 }
23003
23004 sig_type->per_cu.tu_read = 1;
23005 }
23006
23007 /* Decode simple location descriptions.
23008 Given a pointer to a dwarf block that defines a location, compute
23009 the location and return the value. If COMPUTED is non-null, it is
23010 set to true to indicate that decoding was successful, and false
23011 otherwise. If COMPUTED is null, then this function may emit a
23012 complaint. */
23013
23014 static CORE_ADDR
23015 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23016 {
23017 struct objfile *objfile = cu->per_objfile->objfile;
23018 size_t i;
23019 size_t size = blk->size;
23020 const gdb_byte *data = blk->data;
23021 CORE_ADDR stack[64];
23022 int stacki;
23023 unsigned int bytes_read, unsnd;
23024 gdb_byte op;
23025
23026 if (computed != nullptr)
23027 *computed = false;
23028
23029 i = 0;
23030 stacki = 0;
23031 stack[stacki] = 0;
23032 stack[++stacki] = 0;
23033
23034 while (i < size)
23035 {
23036 op = data[i++];
23037 switch (op)
23038 {
23039 case DW_OP_lit0:
23040 case DW_OP_lit1:
23041 case DW_OP_lit2:
23042 case DW_OP_lit3:
23043 case DW_OP_lit4:
23044 case DW_OP_lit5:
23045 case DW_OP_lit6:
23046 case DW_OP_lit7:
23047 case DW_OP_lit8:
23048 case DW_OP_lit9:
23049 case DW_OP_lit10:
23050 case DW_OP_lit11:
23051 case DW_OP_lit12:
23052 case DW_OP_lit13:
23053 case DW_OP_lit14:
23054 case DW_OP_lit15:
23055 case DW_OP_lit16:
23056 case DW_OP_lit17:
23057 case DW_OP_lit18:
23058 case DW_OP_lit19:
23059 case DW_OP_lit20:
23060 case DW_OP_lit21:
23061 case DW_OP_lit22:
23062 case DW_OP_lit23:
23063 case DW_OP_lit24:
23064 case DW_OP_lit25:
23065 case DW_OP_lit26:
23066 case DW_OP_lit27:
23067 case DW_OP_lit28:
23068 case DW_OP_lit29:
23069 case DW_OP_lit30:
23070 case DW_OP_lit31:
23071 stack[++stacki] = op - DW_OP_lit0;
23072 break;
23073
23074 case DW_OP_reg0:
23075 case DW_OP_reg1:
23076 case DW_OP_reg2:
23077 case DW_OP_reg3:
23078 case DW_OP_reg4:
23079 case DW_OP_reg5:
23080 case DW_OP_reg6:
23081 case DW_OP_reg7:
23082 case DW_OP_reg8:
23083 case DW_OP_reg9:
23084 case DW_OP_reg10:
23085 case DW_OP_reg11:
23086 case DW_OP_reg12:
23087 case DW_OP_reg13:
23088 case DW_OP_reg14:
23089 case DW_OP_reg15:
23090 case DW_OP_reg16:
23091 case DW_OP_reg17:
23092 case DW_OP_reg18:
23093 case DW_OP_reg19:
23094 case DW_OP_reg20:
23095 case DW_OP_reg21:
23096 case DW_OP_reg22:
23097 case DW_OP_reg23:
23098 case DW_OP_reg24:
23099 case DW_OP_reg25:
23100 case DW_OP_reg26:
23101 case DW_OP_reg27:
23102 case DW_OP_reg28:
23103 case DW_OP_reg29:
23104 case DW_OP_reg30:
23105 case DW_OP_reg31:
23106 stack[++stacki] = op - DW_OP_reg0;
23107 if (i < size)
23108 {
23109 if (computed == nullptr)
23110 dwarf2_complex_location_expr_complaint ();
23111 else
23112 return 0;
23113 }
23114 break;
23115
23116 case DW_OP_regx:
23117 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23118 i += bytes_read;
23119 stack[++stacki] = unsnd;
23120 if (i < size)
23121 {
23122 if (computed == nullptr)
23123 dwarf2_complex_location_expr_complaint ();
23124 else
23125 return 0;
23126 }
23127 break;
23128
23129 case DW_OP_addr:
23130 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23131 &bytes_read);
23132 i += bytes_read;
23133 break;
23134
23135 case DW_OP_const1u:
23136 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23137 i += 1;
23138 break;
23139
23140 case DW_OP_const1s:
23141 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23142 i += 1;
23143 break;
23144
23145 case DW_OP_const2u:
23146 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23147 i += 2;
23148 break;
23149
23150 case DW_OP_const2s:
23151 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23152 i += 2;
23153 break;
23154
23155 case DW_OP_const4u:
23156 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23157 i += 4;
23158 break;
23159
23160 case DW_OP_const4s:
23161 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23162 i += 4;
23163 break;
23164
23165 case DW_OP_const8u:
23166 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23167 i += 8;
23168 break;
23169
23170 case DW_OP_constu:
23171 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23172 &bytes_read);
23173 i += bytes_read;
23174 break;
23175
23176 case DW_OP_consts:
23177 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23178 i += bytes_read;
23179 break;
23180
23181 case DW_OP_dup:
23182 stack[stacki + 1] = stack[stacki];
23183 stacki++;
23184 break;
23185
23186 case DW_OP_plus:
23187 stack[stacki - 1] += stack[stacki];
23188 stacki--;
23189 break;
23190
23191 case DW_OP_plus_uconst:
23192 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23193 &bytes_read);
23194 i += bytes_read;
23195 break;
23196
23197 case DW_OP_minus:
23198 stack[stacki - 1] -= stack[stacki];
23199 stacki--;
23200 break;
23201
23202 case DW_OP_deref:
23203 /* If we're not the last op, then we definitely can't encode
23204 this using GDB's address_class enum. This is valid for partial
23205 global symbols, although the variable's address will be bogus
23206 in the psymtab. */
23207 if (i < size)
23208 {
23209 if (computed == nullptr)
23210 dwarf2_complex_location_expr_complaint ();
23211 else
23212 return 0;
23213 }
23214 break;
23215
23216 case DW_OP_GNU_push_tls_address:
23217 case DW_OP_form_tls_address:
23218 /* The top of the stack has the offset from the beginning
23219 of the thread control block at which the variable is located. */
23220 /* Nothing should follow this operator, so the top of stack would
23221 be returned. */
23222 /* This is valid for partial global symbols, but the variable's
23223 address will be bogus in the psymtab. Make it always at least
23224 non-zero to not look as a variable garbage collected by linker
23225 which have DW_OP_addr 0. */
23226 if (i < size)
23227 {
23228 if (computed == nullptr)
23229 dwarf2_complex_location_expr_complaint ();
23230 else
23231 return 0;
23232 }
23233 stack[stacki]++;
23234 break;
23235
23236 case DW_OP_GNU_uninit:
23237 if (computed != nullptr)
23238 return 0;
23239 break;
23240
23241 case DW_OP_addrx:
23242 case DW_OP_GNU_addr_index:
23243 case DW_OP_GNU_const_index:
23244 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23245 &bytes_read);
23246 i += bytes_read;
23247 break;
23248
23249 default:
23250 if (computed == nullptr)
23251 {
23252 const char *name = get_DW_OP_name (op);
23253
23254 if (name)
23255 complaint (_("unsupported stack op: '%s'"),
23256 name);
23257 else
23258 complaint (_("unsupported stack op: '%02x'"),
23259 op);
23260 }
23261
23262 return (stack[stacki]);
23263 }
23264
23265 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23266 outside of the allocated space. Also enforce minimum>0. */
23267 if (stacki >= ARRAY_SIZE (stack) - 1)
23268 {
23269 if (computed == nullptr)
23270 complaint (_("location description stack overflow"));
23271 return 0;
23272 }
23273
23274 if (stacki <= 0)
23275 {
23276 if (computed == nullptr)
23277 complaint (_("location description stack underflow"));
23278 return 0;
23279 }
23280 }
23281
23282 if (computed != nullptr)
23283 *computed = true;
23284 return (stack[stacki]);
23285 }
23286
23287 /* memory allocation interface */
23288
23289 static struct dwarf_block *
23290 dwarf_alloc_block (struct dwarf2_cu *cu)
23291 {
23292 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23293 }
23294
23295 static struct die_info *
23296 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23297 {
23298 struct die_info *die;
23299 size_t size = sizeof (struct die_info);
23300
23301 if (num_attrs > 1)
23302 size += (num_attrs - 1) * sizeof (struct attribute);
23303
23304 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23305 memset (die, 0, sizeof (struct die_info));
23306 return (die);
23307 }
23308
23309 \f
23310
23311 /* Macro support. */
23312
23313 /* An overload of dwarf_decode_macros that finds the correct section
23314 and ensures it is read in before calling the other overload. */
23315
23316 static void
23317 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23318 int section_is_gnu)
23319 {
23320 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23321 struct objfile *objfile = per_objfile->objfile;
23322 const struct line_header *lh = cu->line_header;
23323 unsigned int offset_size = cu->header.offset_size;
23324 struct dwarf2_section_info *section;
23325 const char *section_name;
23326
23327 if (cu->dwo_unit != nullptr)
23328 {
23329 if (section_is_gnu)
23330 {
23331 section = &cu->dwo_unit->dwo_file->sections.macro;
23332 section_name = ".debug_macro.dwo";
23333 }
23334 else
23335 {
23336 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23337 section_name = ".debug_macinfo.dwo";
23338 }
23339 }
23340 else
23341 {
23342 if (section_is_gnu)
23343 {
23344 section = &per_objfile->per_bfd->macro;
23345 section_name = ".debug_macro";
23346 }
23347 else
23348 {
23349 section = &per_objfile->per_bfd->macinfo;
23350 section_name = ".debug_macinfo";
23351 }
23352 }
23353
23354 section->read (objfile);
23355 if (section->buffer == nullptr)
23356 {
23357 complaint (_("missing %s section"), section_name);
23358 return;
23359 }
23360
23361 buildsym_compunit *builder = cu->get_builder ();
23362
23363 dwarf_decode_macros (per_objfile, builder, section, lh,
23364 offset_size, offset, section_is_gnu);
23365 }
23366
23367 /* Return the .debug_loc section to use for CU.
23368 For DWO files use .debug_loc.dwo. */
23369
23370 static struct dwarf2_section_info *
23371 cu_debug_loc_section (struct dwarf2_cu *cu)
23372 {
23373 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23374
23375 if (cu->dwo_unit)
23376 {
23377 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23378
23379 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23380 }
23381 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23382 : &per_objfile->per_bfd->loc);
23383 }
23384
23385 /* A helper function that fills in a dwarf2_loclist_baton. */
23386
23387 static void
23388 fill_in_loclist_baton (struct dwarf2_cu *cu,
23389 struct dwarf2_loclist_baton *baton,
23390 const struct attribute *attr)
23391 {
23392 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23393 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23394
23395 section->read (per_objfile->objfile);
23396
23397 baton->per_objfile = per_objfile;
23398 baton->per_cu = cu->per_cu;
23399 gdb_assert (baton->per_cu);
23400 /* We don't know how long the location list is, but make sure we
23401 don't run off the edge of the section. */
23402 baton->size = section->size - DW_UNSND (attr);
23403 baton->data = section->buffer + DW_UNSND (attr);
23404 if (cu->base_address.has_value ())
23405 baton->base_address = *cu->base_address;
23406 else
23407 baton->base_address = 0;
23408 baton->from_dwo = cu->dwo_unit != NULL;
23409 }
23410
23411 static void
23412 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23413 struct dwarf2_cu *cu, int is_block)
23414 {
23415 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23416 struct objfile *objfile = per_objfile->objfile;
23417 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23418
23419 if (attr->form_is_section_offset ()
23420 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23421 the section. If so, fall through to the complaint in the
23422 other branch. */
23423 && DW_UNSND (attr) < section->get_size (objfile))
23424 {
23425 struct dwarf2_loclist_baton *baton;
23426
23427 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23428
23429 fill_in_loclist_baton (cu, baton, attr);
23430
23431 if (!cu->base_address.has_value ())
23432 complaint (_("Location list used without "
23433 "specifying the CU base address."));
23434
23435 SYMBOL_ACLASS_INDEX (sym) = (is_block
23436 ? dwarf2_loclist_block_index
23437 : dwarf2_loclist_index);
23438 SYMBOL_LOCATION_BATON (sym) = baton;
23439 }
23440 else
23441 {
23442 struct dwarf2_locexpr_baton *baton;
23443
23444 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23445 baton->per_objfile = per_objfile;
23446 baton->per_cu = cu->per_cu;
23447 gdb_assert (baton->per_cu);
23448
23449 if (attr->form_is_block ())
23450 {
23451 /* Note that we're just copying the block's data pointer
23452 here, not the actual data. We're still pointing into the
23453 info_buffer for SYM's objfile; right now we never release
23454 that buffer, but when we do clean up properly this may
23455 need to change. */
23456 baton->size = DW_BLOCK (attr)->size;
23457 baton->data = DW_BLOCK (attr)->data;
23458 }
23459 else
23460 {
23461 dwarf2_invalid_attrib_class_complaint ("location description",
23462 sym->natural_name ());
23463 baton->size = 0;
23464 }
23465
23466 SYMBOL_ACLASS_INDEX (sym) = (is_block
23467 ? dwarf2_locexpr_block_index
23468 : dwarf2_locexpr_index);
23469 SYMBOL_LOCATION_BATON (sym) = baton;
23470 }
23471 }
23472
23473 /* See read.h. */
23474
23475 const comp_unit_head *
23476 dwarf2_per_cu_data::get_header () const
23477 {
23478 if (!m_header_read_in)
23479 {
23480 const gdb_byte *info_ptr
23481 = this->section->buffer + to_underlying (this->sect_off);
23482
23483 memset (&m_header, 0, sizeof (m_header));
23484
23485 read_comp_unit_head (&m_header, info_ptr, this->section,
23486 rcuh_kind::COMPILE);
23487 }
23488
23489 return &m_header;
23490 }
23491
23492 /* See read.h. */
23493
23494 int
23495 dwarf2_per_cu_data::addr_size () const
23496 {
23497 return this->get_header ()->addr_size;
23498 }
23499
23500 /* See read.h. */
23501
23502 int
23503 dwarf2_per_cu_data::offset_size () const
23504 {
23505 return this->get_header ()->offset_size;
23506 }
23507
23508 /* See read.h. */
23509
23510 int
23511 dwarf2_per_cu_data::ref_addr_size () const
23512 {
23513 const comp_unit_head *header = this->get_header ();
23514
23515 if (header->version == 2)
23516 return header->addr_size;
23517 else
23518 return header->offset_size;
23519 }
23520
23521 /* See read.h. */
23522
23523 struct type *
23524 dwarf2_cu::addr_type () const
23525 {
23526 struct objfile *objfile = this->per_objfile->objfile;
23527 struct type *void_type = objfile_type (objfile)->builtin_void;
23528 struct type *addr_type = lookup_pointer_type (void_type);
23529 int addr_size = this->per_cu->addr_size ();
23530
23531 if (TYPE_LENGTH (addr_type) == addr_size)
23532 return addr_type;
23533
23534 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23535 return addr_type;
23536 }
23537
23538 /* A helper function for dwarf2_find_containing_comp_unit that returns
23539 the index of the result, and that searches a vector. It will
23540 return a result even if the offset in question does not actually
23541 occur in any CU. This is separate so that it can be unit
23542 tested. */
23543
23544 static int
23545 dwarf2_find_containing_comp_unit
23546 (sect_offset sect_off,
23547 unsigned int offset_in_dwz,
23548 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23549 {
23550 int low, high;
23551
23552 low = 0;
23553 high = all_comp_units.size () - 1;
23554 while (high > low)
23555 {
23556 struct dwarf2_per_cu_data *mid_cu;
23557 int mid = low + (high - low) / 2;
23558
23559 mid_cu = all_comp_units[mid];
23560 if (mid_cu->is_dwz > offset_in_dwz
23561 || (mid_cu->is_dwz == offset_in_dwz
23562 && mid_cu->sect_off + mid_cu->length > sect_off))
23563 high = mid;
23564 else
23565 low = mid + 1;
23566 }
23567 gdb_assert (low == high);
23568 return low;
23569 }
23570
23571 /* Locate the .debug_info compilation unit from CU's objfile which contains
23572 the DIE at OFFSET. Raises an error on failure. */
23573
23574 static struct dwarf2_per_cu_data *
23575 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23576 unsigned int offset_in_dwz,
23577 dwarf2_per_objfile *per_objfile)
23578 {
23579 int low = dwarf2_find_containing_comp_unit
23580 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
23581 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
23582
23583 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23584 {
23585 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23586 error (_("Dwarf Error: could not find partial DIE containing "
23587 "offset %s [in module %s]"),
23588 sect_offset_str (sect_off),
23589 bfd_get_filename (per_objfile->objfile->obfd));
23590
23591 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23592 <= sect_off);
23593 return per_objfile->per_bfd->all_comp_units[low-1];
23594 }
23595 else
23596 {
23597 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
23598 && sect_off >= this_cu->sect_off + this_cu->length)
23599 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23600 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23601 return this_cu;
23602 }
23603 }
23604
23605 #if GDB_SELF_TEST
23606
23607 namespace selftests {
23608 namespace find_containing_comp_unit {
23609
23610 static void
23611 run_test ()
23612 {
23613 struct dwarf2_per_cu_data one {};
23614 struct dwarf2_per_cu_data two {};
23615 struct dwarf2_per_cu_data three {};
23616 struct dwarf2_per_cu_data four {};
23617
23618 one.length = 5;
23619 two.sect_off = sect_offset (one.length);
23620 two.length = 7;
23621
23622 three.length = 5;
23623 three.is_dwz = 1;
23624 four.sect_off = sect_offset (three.length);
23625 four.length = 7;
23626 four.is_dwz = 1;
23627
23628 std::vector<dwarf2_per_cu_data *> units;
23629 units.push_back (&one);
23630 units.push_back (&two);
23631 units.push_back (&three);
23632 units.push_back (&four);
23633
23634 int result;
23635
23636 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23637 SELF_CHECK (units[result] == &one);
23638 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23639 SELF_CHECK (units[result] == &one);
23640 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23641 SELF_CHECK (units[result] == &two);
23642
23643 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23644 SELF_CHECK (units[result] == &three);
23645 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23646 SELF_CHECK (units[result] == &three);
23647 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23648 SELF_CHECK (units[result] == &four);
23649 }
23650
23651 }
23652 }
23653
23654 #endif /* GDB_SELF_TEST */
23655
23656 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23657
23658 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23659 dwarf2_per_objfile *per_objfile)
23660 : per_cu (per_cu),
23661 per_objfile (per_objfile),
23662 mark (false),
23663 has_loclist (false),
23664 checked_producer (false),
23665 producer_is_gxx_lt_4_6 (false),
23666 producer_is_gcc_lt_4_3 (false),
23667 producer_is_icc (false),
23668 producer_is_icc_lt_14 (false),
23669 producer_is_codewarrior (false),
23670 processing_has_namespace_info (false)
23671 {
23672 }
23673
23674 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23675
23676 static void
23677 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23678 enum language pretend_language)
23679 {
23680 struct attribute *attr;
23681
23682 /* Set the language we're debugging. */
23683 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23684 if (attr != nullptr)
23685 set_cu_language (DW_UNSND (attr), cu);
23686 else
23687 {
23688 cu->language = pretend_language;
23689 cu->language_defn = language_def (cu->language);
23690 }
23691
23692 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23693 }
23694
23695 /* See read.h. */
23696
23697 dwarf2_cu *
23698 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23699 {
23700 auto it = m_dwarf2_cus.find (per_cu);
23701 if (it == m_dwarf2_cus.end ())
23702 return nullptr;
23703
23704 return it->second;
23705 }
23706
23707 /* See read.h. */
23708
23709 void
23710 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23711 {
23712 gdb_assert (this->get_cu (per_cu) == nullptr);
23713
23714 m_dwarf2_cus[per_cu] = cu;
23715 }
23716
23717 /* See read.h. */
23718
23719 void
23720 dwarf2_per_objfile::age_comp_units ()
23721 {
23722 /* Start by clearing all marks. */
23723 for (auto pair : m_dwarf2_cus)
23724 pair.second->mark = false;
23725
23726 /* Traverse all CUs, mark them and their dependencies if used recently
23727 enough. */
23728 for (auto pair : m_dwarf2_cus)
23729 {
23730 dwarf2_cu *cu = pair.second;
23731
23732 cu->last_used++;
23733 if (cu->last_used <= dwarf_max_cache_age)
23734 dwarf2_mark (cu);
23735 }
23736
23737 /* Delete all CUs still not marked. */
23738 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23739 {
23740 dwarf2_cu *cu = it->second;
23741
23742 if (!cu->mark)
23743 {
23744 delete cu;
23745 it = m_dwarf2_cus.erase (it);
23746 }
23747 else
23748 it++;
23749 }
23750 }
23751
23752 /* See read.h. */
23753
23754 void
23755 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23756 {
23757 auto it = m_dwarf2_cus.find (per_cu);
23758 if (it == m_dwarf2_cus.end ())
23759 return;
23760
23761 delete it->second;
23762
23763 m_dwarf2_cus.erase (it);
23764 }
23765
23766 dwarf2_per_objfile::~dwarf2_per_objfile ()
23767 {
23768 remove_all_cus ();
23769 }
23770
23771 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23772 We store these in a hash table separate from the DIEs, and preserve them
23773 when the DIEs are flushed out of cache.
23774
23775 The CU "per_cu" pointer is needed because offset alone is not enough to
23776 uniquely identify the type. A file may have multiple .debug_types sections,
23777 or the type may come from a DWO file. Furthermore, while it's more logical
23778 to use per_cu->section+offset, with Fission the section with the data is in
23779 the DWO file but we don't know that section at the point we need it.
23780 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23781 because we can enter the lookup routine, get_die_type_at_offset, from
23782 outside this file, and thus won't necessarily have PER_CU->cu.
23783 Fortunately, PER_CU is stable for the life of the objfile. */
23784
23785 struct dwarf2_per_cu_offset_and_type
23786 {
23787 const struct dwarf2_per_cu_data *per_cu;
23788 sect_offset sect_off;
23789 struct type *type;
23790 };
23791
23792 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23793
23794 static hashval_t
23795 per_cu_offset_and_type_hash (const void *item)
23796 {
23797 const struct dwarf2_per_cu_offset_and_type *ofs
23798 = (const struct dwarf2_per_cu_offset_and_type *) item;
23799
23800 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23801 }
23802
23803 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23804
23805 static int
23806 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23807 {
23808 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23809 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23810 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23811 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23812
23813 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23814 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23815 }
23816
23817 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23818 table if necessary. For convenience, return TYPE.
23819
23820 The DIEs reading must have careful ordering to:
23821 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23822 reading current DIE.
23823 * Not trying to dereference contents of still incompletely read in types
23824 while reading in other DIEs.
23825 * Enable referencing still incompletely read in types just by a pointer to
23826 the type without accessing its fields.
23827
23828 Therefore caller should follow these rules:
23829 * Try to fetch any prerequisite types we may need to build this DIE type
23830 before building the type and calling set_die_type.
23831 * After building type call set_die_type for current DIE as soon as
23832 possible before fetching more types to complete the current type.
23833 * Make the type as complete as possible before fetching more types. */
23834
23835 static struct type *
23836 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23837 {
23838 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23839 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23840 struct objfile *objfile = per_objfile->objfile;
23841 struct attribute *attr;
23842 struct dynamic_prop prop;
23843
23844 /* For Ada types, make sure that the gnat-specific data is always
23845 initialized (if not already set). There are a few types where
23846 we should not be doing so, because the type-specific area is
23847 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23848 where the type-specific area is used to store the floatformat).
23849 But this is not a problem, because the gnat-specific information
23850 is actually not needed for these types. */
23851 if (need_gnat_info (cu)
23852 && type->code () != TYPE_CODE_FUNC
23853 && type->code () != TYPE_CODE_FLT
23854 && type->code () != TYPE_CODE_METHODPTR
23855 && type->code () != TYPE_CODE_MEMBERPTR
23856 && type->code () != TYPE_CODE_METHOD
23857 && !HAVE_GNAT_AUX_INFO (type))
23858 INIT_GNAT_SPECIFIC (type);
23859
23860 /* Read DW_AT_allocated and set in type. */
23861 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23862 if (attr != NULL && attr->form_is_block ())
23863 {
23864 struct type *prop_type = cu->addr_sized_int_type (false);
23865 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23866 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23867 }
23868 else if (attr != NULL)
23869 {
23870 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23871 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23872 sect_offset_str (die->sect_off));
23873 }
23874
23875 /* Read DW_AT_associated and set in type. */
23876 attr = dwarf2_attr (die, DW_AT_associated, cu);
23877 if (attr != NULL && attr->form_is_block ())
23878 {
23879 struct type *prop_type = cu->addr_sized_int_type (false);
23880 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23881 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23882 }
23883 else if (attr != NULL)
23884 {
23885 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23886 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23887 sect_offset_str (die->sect_off));
23888 }
23889
23890 /* Read DW_AT_data_location and set in type. */
23891 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23892 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23893 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23894
23895 if (per_objfile->die_type_hash == NULL)
23896 per_objfile->die_type_hash
23897 = htab_up (htab_create_alloc (127,
23898 per_cu_offset_and_type_hash,
23899 per_cu_offset_and_type_eq,
23900 NULL, xcalloc, xfree));
23901
23902 ofs.per_cu = cu->per_cu;
23903 ofs.sect_off = die->sect_off;
23904 ofs.type = type;
23905 slot = (struct dwarf2_per_cu_offset_and_type **)
23906 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
23907 if (*slot)
23908 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23909 sect_offset_str (die->sect_off));
23910 *slot = XOBNEW (&objfile->objfile_obstack,
23911 struct dwarf2_per_cu_offset_and_type);
23912 **slot = ofs;
23913 return type;
23914 }
23915
23916 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23917 or return NULL if the die does not have a saved type. */
23918
23919 static struct type *
23920 get_die_type_at_offset (sect_offset sect_off,
23921 dwarf2_per_cu_data *per_cu,
23922 dwarf2_per_objfile *per_objfile)
23923 {
23924 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23925
23926 if (per_objfile->die_type_hash == NULL)
23927 return NULL;
23928
23929 ofs.per_cu = per_cu;
23930 ofs.sect_off = sect_off;
23931 slot = ((struct dwarf2_per_cu_offset_and_type *)
23932 htab_find (per_objfile->die_type_hash.get (), &ofs));
23933 if (slot)
23934 return slot->type;
23935 else
23936 return NULL;
23937 }
23938
23939 /* Look up the type for DIE in CU in die_type_hash,
23940 or return NULL if DIE does not have a saved type. */
23941
23942 static struct type *
23943 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23944 {
23945 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23946 }
23947
23948 /* Add a dependence relationship from CU to REF_PER_CU. */
23949
23950 static void
23951 dwarf2_add_dependence (struct dwarf2_cu *cu,
23952 struct dwarf2_per_cu_data *ref_per_cu)
23953 {
23954 void **slot;
23955
23956 if (cu->dependencies == NULL)
23957 cu->dependencies
23958 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23959 NULL, &cu->comp_unit_obstack,
23960 hashtab_obstack_allocate,
23961 dummy_obstack_deallocate);
23962
23963 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23964 if (*slot == NULL)
23965 *slot = ref_per_cu;
23966 }
23967
23968 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23969 Set the mark field in every compilation unit in the
23970 cache that we must keep because we are keeping CU.
23971
23972 DATA is the dwarf2_per_objfile object in which to look up CUs. */
23973
23974 static int
23975 dwarf2_mark_helper (void **slot, void *data)
23976 {
23977 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23978 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23979 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23980
23981 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23982 reading of the chain. As such dependencies remain valid it is not much
23983 useful to track and undo them during QUIT cleanups. */
23984 if (cu == nullptr)
23985 return 1;
23986
23987 if (cu->mark)
23988 return 1;
23989
23990 cu->mark = true;
23991
23992 if (cu->dependencies != nullptr)
23993 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
23994
23995 return 1;
23996 }
23997
23998 /* Set the mark field in CU and in every other compilation unit in the
23999 cache that we must keep because we are keeping CU. */
24000
24001 static void
24002 dwarf2_mark (struct dwarf2_cu *cu)
24003 {
24004 if (cu->mark)
24005 return;
24006
24007 cu->mark = true;
24008
24009 if (cu->dependencies != nullptr)
24010 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
24011 }
24012
24013 /* Trivial hash function for partial_die_info: the hash value of a DIE
24014 is its offset in .debug_info for this objfile. */
24015
24016 static hashval_t
24017 partial_die_hash (const void *item)
24018 {
24019 const struct partial_die_info *part_die
24020 = (const struct partial_die_info *) item;
24021
24022 return to_underlying (part_die->sect_off);
24023 }
24024
24025 /* Trivial comparison function for partial_die_info structures: two DIEs
24026 are equal if they have the same offset. */
24027
24028 static int
24029 partial_die_eq (const void *item_lhs, const void *item_rhs)
24030 {
24031 const struct partial_die_info *part_die_lhs
24032 = (const struct partial_die_info *) item_lhs;
24033 const struct partial_die_info *part_die_rhs
24034 = (const struct partial_die_info *) item_rhs;
24035
24036 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24037 }
24038
24039 struct cmd_list_element *set_dwarf_cmdlist;
24040 struct cmd_list_element *show_dwarf_cmdlist;
24041
24042 static void
24043 show_check_physname (struct ui_file *file, int from_tty,
24044 struct cmd_list_element *c, const char *value)
24045 {
24046 fprintf_filtered (file,
24047 _("Whether to check \"physname\" is %s.\n"),
24048 value);
24049 }
24050
24051 void _initialize_dwarf2_read ();
24052 void
24053 _initialize_dwarf2_read ()
24054 {
24055 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24056 Set DWARF specific variables.\n\
24057 Configure DWARF variables such as the cache size."),
24058 &set_dwarf_cmdlist, "maintenance set dwarf ",
24059 0/*allow-unknown*/, &maintenance_set_cmdlist);
24060
24061 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24062 Show DWARF specific variables.\n\
24063 Show DWARF variables such as the cache size."),
24064 &show_dwarf_cmdlist, "maintenance show dwarf ",
24065 0/*allow-unknown*/, &maintenance_show_cmdlist);
24066
24067 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24068 &dwarf_max_cache_age, _("\
24069 Set the upper bound on the age of cached DWARF compilation units."), _("\
24070 Show the upper bound on the age of cached DWARF compilation units."), _("\
24071 A higher limit means that cached compilation units will be stored\n\
24072 in memory longer, and more total memory will be used. Zero disables\n\
24073 caching, which can slow down startup."),
24074 NULL,
24075 show_dwarf_max_cache_age,
24076 &set_dwarf_cmdlist,
24077 &show_dwarf_cmdlist);
24078
24079 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24080 Set debugging of the DWARF reader."), _("\
24081 Show debugging of the DWARF reader."), _("\
24082 When enabled (non-zero), debugging messages are printed during DWARF\n\
24083 reading and symtab expansion. A value of 1 (one) provides basic\n\
24084 information. A value greater than 1 provides more verbose information."),
24085 NULL,
24086 NULL,
24087 &setdebuglist, &showdebuglist);
24088
24089 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24090 Set debugging of the DWARF DIE reader."), _("\
24091 Show debugging of the DWARF DIE reader."), _("\
24092 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24093 The value is the maximum depth to print."),
24094 NULL,
24095 NULL,
24096 &setdebuglist, &showdebuglist);
24097
24098 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24099 Set debugging of the dwarf line reader."), _("\
24100 Show debugging of the dwarf line reader."), _("\
24101 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24102 A value of 1 (one) provides basic information.\n\
24103 A value greater than 1 provides more verbose information."),
24104 NULL,
24105 NULL,
24106 &setdebuglist, &showdebuglist);
24107
24108 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24109 Set cross-checking of \"physname\" code against demangler."), _("\
24110 Show cross-checking of \"physname\" code against demangler."), _("\
24111 When enabled, GDB's internal \"physname\" code is checked against\n\
24112 the demangler."),
24113 NULL, show_check_physname,
24114 &setdebuglist, &showdebuglist);
24115
24116 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24117 no_class, &use_deprecated_index_sections, _("\
24118 Set whether to use deprecated gdb_index sections."), _("\
24119 Show whether to use deprecated gdb_index sections."), _("\
24120 When enabled, deprecated .gdb_index sections are used anyway.\n\
24121 Normally they are ignored either because of a missing feature or\n\
24122 performance issue.\n\
24123 Warning: This option must be enabled before gdb reads the file."),
24124 NULL,
24125 NULL,
24126 &setlist, &showlist);
24127
24128 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24129 &dwarf2_locexpr_funcs);
24130 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24131 &dwarf2_loclist_funcs);
24132
24133 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24134 &dwarf2_block_frame_base_locexpr_funcs);
24135 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24136 &dwarf2_block_frame_base_loclist_funcs);
24137
24138 #if GDB_SELF_TEST
24139 selftests::register_test ("dw2_expand_symtabs_matching",
24140 selftests::dw2_expand_symtabs_matching::run_test);
24141 selftests::register_test ("dwarf2_find_containing_comp_unit",
24142 selftests::find_containing_comp_unit::run_test);
24143 #endif
24144 }
This page took 0.678695 seconds and 4 git commands to generate.