[gdb/symtab] Fix missing breakpoint location for inlined function
[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
3072 gdb::array_view<const gdb_byte> main_index_contents
3073 = get_gdb_index_contents (objfile, per_objfile->per_bfd);
3074
3075 if (main_index_contents.empty ())
3076 return 0;
3077
3078 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3079 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3080 use_deprecated_index_sections,
3081 main_index_contents, map.get (), &cu_list,
3082 &cu_list_elements, &types_list,
3083 &types_list_elements))
3084 return 0;
3085
3086 /* Don't use the index if it's empty. */
3087 if (map->symbol_table.empty ())
3088 return 0;
3089
3090 /* If there is a .dwz file, read it so we can get its CU list as
3091 well. */
3092 dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
3093 if (dwz != NULL)
3094 {
3095 struct mapped_index dwz_map;
3096 const gdb_byte *dwz_types_ignore;
3097 offset_type dwz_types_elements_ignore;
3098
3099 gdb::array_view<const gdb_byte> dwz_index_content
3100 = get_gdb_index_contents_dwz (objfile, dwz);
3101
3102 if (dwz_index_content.empty ())
3103 return 0;
3104
3105 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3106 1, dwz_index_content, &dwz_map,
3107 &dwz_list, &dwz_list_elements,
3108 &dwz_types_ignore,
3109 &dwz_types_elements_ignore))
3110 {
3111 warning (_("could not read '.gdb_index' section from %s; skipping"),
3112 bfd_get_filename (dwz->dwz_bfd.get ()));
3113 return 0;
3114 }
3115 }
3116
3117 create_cus_from_index (per_objfile->per_bfd, cu_list, cu_list_elements,
3118 dwz_list, dwz_list_elements);
3119
3120 if (types_list_elements)
3121 {
3122 /* We can only handle a single .debug_types when we have an
3123 index. */
3124 if (per_objfile->per_bfd->types.size () != 1)
3125 return 0;
3126
3127 dwarf2_section_info *section = &per_objfile->per_bfd->types[0];
3128
3129 create_signatured_type_table_from_index (per_objfile->per_bfd,
3130 section, types_list,
3131 types_list_elements);
3132 }
3133
3134 create_addrmap_from_index (per_objfile, map.get ());
3135
3136 per_objfile->per_bfd->index_table = std::move (map);
3137 per_objfile->per_bfd->using_index = 1;
3138 per_objfile->per_bfd->quick_file_names_table =
3139 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
3140
3141 return 1;
3142 }
3143
3144 /* die_reader_func for dw2_get_file_names. */
3145
3146 static void
3147 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3148 const gdb_byte *info_ptr,
3149 struct die_info *comp_unit_die)
3150 {
3151 struct dwarf2_cu *cu = reader->cu;
3152 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3153 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3154 struct dwarf2_per_cu_data *lh_cu;
3155 struct attribute *attr;
3156 void **slot;
3157 struct quick_file_names *qfn;
3158
3159 gdb_assert (! this_cu->is_debug_types);
3160
3161 /* Our callers never want to match partial units -- instead they
3162 will match the enclosing full CU. */
3163 if (comp_unit_die->tag == DW_TAG_partial_unit)
3164 {
3165 this_cu->v.quick->no_file_data = 1;
3166 return;
3167 }
3168
3169 lh_cu = this_cu;
3170 slot = NULL;
3171
3172 line_header_up lh;
3173 sect_offset line_offset {};
3174
3175 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3176 if (attr != nullptr)
3177 {
3178 struct quick_file_names find_entry;
3179
3180 line_offset = (sect_offset) DW_UNSND (attr);
3181
3182 /* We may have already read in this line header (TU line header sharing).
3183 If we have we're done. */
3184 find_entry.hash.dwo_unit = cu->dwo_unit;
3185 find_entry.hash.line_sect_off = line_offset;
3186 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3187 &find_entry, INSERT);
3188 if (*slot != NULL)
3189 {
3190 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3191 return;
3192 }
3193
3194 lh = dwarf_decode_line_header (line_offset, cu);
3195 }
3196 if (lh == NULL)
3197 {
3198 lh_cu->v.quick->no_file_data = 1;
3199 return;
3200 }
3201
3202 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3203 qfn->hash.dwo_unit = cu->dwo_unit;
3204 qfn->hash.line_sect_off = line_offset;
3205 gdb_assert (slot != NULL);
3206 *slot = qfn;
3207
3208 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3209
3210 int offset = 0;
3211 if (strcmp (fnd.name, "<unknown>") != 0)
3212 ++offset;
3213
3214 qfn->num_file_names = offset + lh->file_names_size ();
3215 qfn->file_names =
3216 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3217 qfn->num_file_names);
3218 if (offset != 0)
3219 qfn->file_names[0] = xstrdup (fnd.name);
3220 for (int i = 0; i < lh->file_names_size (); ++i)
3221 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3222 fnd.comp_dir).release ();
3223 qfn->real_names = NULL;
3224
3225 lh_cu->v.quick->file_names = qfn;
3226 }
3227
3228 /* A helper for the "quick" functions which attempts to read the line
3229 table for THIS_CU. */
3230
3231 static struct quick_file_names *
3232 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3233 dwarf2_per_objfile *per_objfile)
3234 {
3235 /* This should never be called for TUs. */
3236 gdb_assert (! this_cu->is_debug_types);
3237 /* Nor type unit groups. */
3238 gdb_assert (! this_cu->type_unit_group_p ());
3239
3240 if (this_cu->v.quick->file_names != NULL)
3241 return this_cu->v.quick->file_names;
3242 /* If we know there is no line data, no point in looking again. */
3243 if (this_cu->v.quick->no_file_data)
3244 return NULL;
3245
3246 cutu_reader reader (this_cu, per_objfile);
3247 if (!reader.dummy_p)
3248 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3249
3250 if (this_cu->v.quick->no_file_data)
3251 return NULL;
3252 return this_cu->v.quick->file_names;
3253 }
3254
3255 /* A helper for the "quick" functions which computes and caches the
3256 real path for a given file name from the line table. */
3257
3258 static const char *
3259 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3260 struct quick_file_names *qfn, int index)
3261 {
3262 if (qfn->real_names == NULL)
3263 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3264 qfn->num_file_names, const char *);
3265
3266 if (qfn->real_names[index] == NULL)
3267 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3268
3269 return qfn->real_names[index];
3270 }
3271
3272 static struct symtab *
3273 dw2_find_last_source_symtab (struct objfile *objfile)
3274 {
3275 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3276 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3277 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3278
3279 if (cust == NULL)
3280 return NULL;
3281
3282 return compunit_primary_filetab (cust);
3283 }
3284
3285 /* Traversal function for dw2_forget_cached_source_info. */
3286
3287 static int
3288 dw2_free_cached_file_names (void **slot, void *info)
3289 {
3290 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3291
3292 if (file_data->real_names)
3293 {
3294 int i;
3295
3296 for (i = 0; i < file_data->num_file_names; ++i)
3297 {
3298 xfree ((void*) file_data->real_names[i]);
3299 file_data->real_names[i] = NULL;
3300 }
3301 }
3302
3303 return 1;
3304 }
3305
3306 static void
3307 dw2_forget_cached_source_info (struct objfile *objfile)
3308 {
3309 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3310
3311 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3312 dw2_free_cached_file_names, NULL);
3313 }
3314
3315 /* Helper function for dw2_map_symtabs_matching_filename that expands
3316 the symtabs and calls the iterator. */
3317
3318 static int
3319 dw2_map_expand_apply (struct objfile *objfile,
3320 struct dwarf2_per_cu_data *per_cu,
3321 const char *name, const char *real_path,
3322 gdb::function_view<bool (symtab *)> callback)
3323 {
3324 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3325
3326 /* Don't visit already-expanded CUs. */
3327 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3328 if (per_objfile->symtab_set_p (per_cu))
3329 return 0;
3330
3331 /* This may expand more than one symtab, and we want to iterate over
3332 all of them. */
3333 dw2_instantiate_symtab (per_cu, per_objfile, false);
3334
3335 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3336 last_made, callback);
3337 }
3338
3339 /* Implementation of the map_symtabs_matching_filename method. */
3340
3341 static bool
3342 dw2_map_symtabs_matching_filename
3343 (struct objfile *objfile, const char *name, const char *real_path,
3344 gdb::function_view<bool (symtab *)> callback)
3345 {
3346 const char *name_basename = lbasename (name);
3347 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3348
3349 /* The rule is CUs specify all the files, including those used by
3350 any TU, so there's no need to scan TUs here. */
3351
3352 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3353 {
3354 /* We only need to look at symtabs not already expanded. */
3355 if (per_objfile->symtab_set_p (per_cu))
3356 continue;
3357
3358 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3359 if (file_data == NULL)
3360 continue;
3361
3362 for (int j = 0; j < file_data->num_file_names; ++j)
3363 {
3364 const char *this_name = file_data->file_names[j];
3365 const char *this_real_name;
3366
3367 if (compare_filenames_for_search (this_name, name))
3368 {
3369 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3370 callback))
3371 return true;
3372 continue;
3373 }
3374
3375 /* Before we invoke realpath, which can get expensive when many
3376 files are involved, do a quick comparison of the basenames. */
3377 if (! basenames_may_differ
3378 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3379 continue;
3380
3381 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3382 if (compare_filenames_for_search (this_real_name, name))
3383 {
3384 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3385 callback))
3386 return true;
3387 continue;
3388 }
3389
3390 if (real_path != NULL)
3391 {
3392 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3393 gdb_assert (IS_ABSOLUTE_PATH (name));
3394 if (this_real_name != NULL
3395 && FILENAME_CMP (real_path, this_real_name) == 0)
3396 {
3397 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3398 callback))
3399 return true;
3400 continue;
3401 }
3402 }
3403 }
3404 }
3405
3406 return false;
3407 }
3408
3409 /* Struct used to manage iterating over all CUs looking for a symbol. */
3410
3411 struct dw2_symtab_iterator
3412 {
3413 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3414 dwarf2_per_objfile *per_objfile;
3415 /* If set, only look for symbols that match that block. Valid values are
3416 GLOBAL_BLOCK and STATIC_BLOCK. */
3417 gdb::optional<block_enum> block_index;
3418 /* The kind of symbol we're looking for. */
3419 domain_enum domain;
3420 /* The list of CUs from the index entry of the symbol,
3421 or NULL if not found. */
3422 offset_type *vec;
3423 /* The next element in VEC to look at. */
3424 int next;
3425 /* The number of elements in VEC, or zero if there is no match. */
3426 int length;
3427 /* Have we seen a global version of the symbol?
3428 If so we can ignore all further global instances.
3429 This is to work around gold/15646, inefficient gold-generated
3430 indices. */
3431 int global_seen;
3432 };
3433
3434 /* Initialize the index symtab iterator ITER. */
3435
3436 static void
3437 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3438 dwarf2_per_objfile *per_objfile,
3439 gdb::optional<block_enum> block_index,
3440 domain_enum domain,
3441 const char *name)
3442 {
3443 iter->per_objfile = per_objfile;
3444 iter->block_index = block_index;
3445 iter->domain = domain;
3446 iter->next = 0;
3447 iter->global_seen = 0;
3448
3449 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3450
3451 /* index is NULL if OBJF_READNOW. */
3452 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3453 iter->length = MAYBE_SWAP (*iter->vec);
3454 else
3455 {
3456 iter->vec = NULL;
3457 iter->length = 0;
3458 }
3459 }
3460
3461 /* Return the next matching CU or NULL if there are no more. */
3462
3463 static struct dwarf2_per_cu_data *
3464 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3465 {
3466 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3467
3468 for ( ; iter->next < iter->length; ++iter->next)
3469 {
3470 offset_type cu_index_and_attrs =
3471 MAYBE_SWAP (iter->vec[iter->next + 1]);
3472 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3473 gdb_index_symbol_kind symbol_kind =
3474 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3475 /* Only check the symbol attributes if they're present.
3476 Indices prior to version 7 don't record them,
3477 and indices >= 7 may elide them for certain symbols
3478 (gold does this). */
3479 int attrs_valid =
3480 (per_objfile->per_bfd->index_table->version >= 7
3481 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3482
3483 /* Don't crash on bad data. */
3484 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3485 + per_objfile->per_bfd->all_type_units.size ()))
3486 {
3487 complaint (_(".gdb_index entry has bad CU index"
3488 " [in module %s]"), objfile_name (per_objfile->objfile));
3489 continue;
3490 }
3491
3492 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3493
3494 /* Skip if already read in. */
3495 if (per_objfile->symtab_set_p (per_cu))
3496 continue;
3497
3498 /* Check static vs global. */
3499 if (attrs_valid)
3500 {
3501 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3502
3503 if (iter->block_index.has_value ())
3504 {
3505 bool want_static = *iter->block_index == STATIC_BLOCK;
3506
3507 if (is_static != want_static)
3508 continue;
3509 }
3510
3511 /* Work around gold/15646. */
3512 if (!is_static
3513 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3514 {
3515 if (iter->global_seen)
3516 continue;
3517
3518 iter->global_seen = 1;
3519 }
3520 }
3521
3522 /* Only check the symbol's kind if it has one. */
3523 if (attrs_valid)
3524 {
3525 switch (iter->domain)
3526 {
3527 case VAR_DOMAIN:
3528 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3529 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3530 /* Some types are also in VAR_DOMAIN. */
3531 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3532 continue;
3533 break;
3534 case STRUCT_DOMAIN:
3535 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3536 continue;
3537 break;
3538 case LABEL_DOMAIN:
3539 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3540 continue;
3541 break;
3542 case MODULE_DOMAIN:
3543 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3544 continue;
3545 break;
3546 default:
3547 break;
3548 }
3549 }
3550
3551 ++iter->next;
3552 return per_cu;
3553 }
3554
3555 return NULL;
3556 }
3557
3558 static struct compunit_symtab *
3559 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3560 const char *name, domain_enum domain)
3561 {
3562 struct compunit_symtab *stab_best = NULL;
3563 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3564
3565 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3566
3567 struct dw2_symtab_iterator iter;
3568 struct dwarf2_per_cu_data *per_cu;
3569
3570 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3571
3572 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3573 {
3574 struct symbol *sym, *with_opaque = NULL;
3575 struct compunit_symtab *stab
3576 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3577 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3578 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3579
3580 sym = block_find_symbol (block, name, domain,
3581 block_find_non_opaque_type_preferred,
3582 &with_opaque);
3583
3584 /* Some caution must be observed with overloaded functions
3585 and methods, since the index will not contain any overload
3586 information (but NAME might contain it). */
3587
3588 if (sym != NULL
3589 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3590 return stab;
3591 if (with_opaque != NULL
3592 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3593 stab_best = stab;
3594
3595 /* Keep looking through other CUs. */
3596 }
3597
3598 return stab_best;
3599 }
3600
3601 static void
3602 dw2_print_stats (struct objfile *objfile)
3603 {
3604 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3605 int total = (per_objfile->per_bfd->all_comp_units.size ()
3606 + per_objfile->per_bfd->all_type_units.size ());
3607 int count = 0;
3608
3609 for (int i = 0; i < total; ++i)
3610 {
3611 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3612
3613 if (!per_objfile->symtab_set_p (per_cu))
3614 ++count;
3615 }
3616 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3617 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3618 }
3619
3620 /* This dumps minimal information about the index.
3621 It is called via "mt print objfiles".
3622 One use is to verify .gdb_index has been loaded by the
3623 gdb.dwarf2/gdb-index.exp testcase. */
3624
3625 static void
3626 dw2_dump (struct objfile *objfile)
3627 {
3628 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3629
3630 gdb_assert (per_objfile->per_bfd->using_index);
3631 printf_filtered (".gdb_index:");
3632 if (per_objfile->per_bfd->index_table != NULL)
3633 {
3634 printf_filtered (" version %d\n",
3635 per_objfile->per_bfd->index_table->version);
3636 }
3637 else
3638 printf_filtered (" faked for \"readnow\"\n");
3639 printf_filtered ("\n");
3640 }
3641
3642 static void
3643 dw2_expand_symtabs_for_function (struct objfile *objfile,
3644 const char *func_name)
3645 {
3646 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3647
3648 struct dw2_symtab_iterator iter;
3649 struct dwarf2_per_cu_data *per_cu;
3650
3651 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3652
3653 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3654 dw2_instantiate_symtab (per_cu, per_objfile, false);
3655
3656 }
3657
3658 static void
3659 dw2_expand_all_symtabs (struct objfile *objfile)
3660 {
3661 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3662 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3663 + per_objfile->per_bfd->all_type_units.size ());
3664
3665 for (int i = 0; i < total_units; ++i)
3666 {
3667 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3668
3669 /* We don't want to directly expand a partial CU, because if we
3670 read it with the wrong language, then assertion failures can
3671 be triggered later on. See PR symtab/23010. So, tell
3672 dw2_instantiate_symtab to skip partial CUs -- any important
3673 partial CU will be read via DW_TAG_imported_unit anyway. */
3674 dw2_instantiate_symtab (per_cu, per_objfile, true);
3675 }
3676 }
3677
3678 static void
3679 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3680 const char *fullname)
3681 {
3682 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3683
3684 /* We don't need to consider type units here.
3685 This is only called for examining code, e.g. expand_line_sal.
3686 There can be an order of magnitude (or more) more type units
3687 than comp units, and we avoid them if we can. */
3688
3689 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3690 {
3691 /* We only need to look at symtabs not already expanded. */
3692 if (per_objfile->symtab_set_p (per_cu))
3693 continue;
3694
3695 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3696 if (file_data == NULL)
3697 continue;
3698
3699 for (int j = 0; j < file_data->num_file_names; ++j)
3700 {
3701 const char *this_fullname = file_data->file_names[j];
3702
3703 if (filename_cmp (this_fullname, fullname) == 0)
3704 {
3705 dw2_instantiate_symtab (per_cu, per_objfile, false);
3706 break;
3707 }
3708 }
3709 }
3710 }
3711
3712 static void
3713 dw2_expand_symtabs_matching_symbol
3714 (mapped_index_base &index,
3715 const lookup_name_info &lookup_name_in,
3716 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3717 enum search_domain kind,
3718 gdb::function_view<bool (offset_type)> match_callback,
3719 dwarf2_per_objfile *per_objfile);
3720
3721 static void
3722 dw2_expand_symtabs_matching_one
3723 (dwarf2_per_cu_data *per_cu,
3724 dwarf2_per_objfile *per_objfile,
3725 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3726 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3727
3728 static void
3729 dw2_map_matching_symbols
3730 (struct objfile *objfile,
3731 const lookup_name_info &name, domain_enum domain,
3732 int global,
3733 gdb::function_view<symbol_found_callback_ftype> callback,
3734 symbol_compare_ftype *ordered_compare)
3735 {
3736 /* Used for Ada. */
3737 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3738
3739 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3740
3741 if (per_objfile->per_bfd->index_table != nullptr)
3742 {
3743 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3744 here though if the current language is Ada for a non-Ada objfile
3745 using GNU index. */
3746 mapped_index &index = *per_objfile->per_bfd->index_table;
3747
3748 const char *match_name = name.ada ().lookup_name ().c_str ();
3749 auto matcher = [&] (const char *symname)
3750 {
3751 if (ordered_compare == nullptr)
3752 return true;
3753 return ordered_compare (symname, match_name) == 0;
3754 };
3755
3756 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3757 [&] (offset_type namei)
3758 {
3759 struct dw2_symtab_iterator iter;
3760 struct dwarf2_per_cu_data *per_cu;
3761
3762 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3763 match_name);
3764 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3765 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3766 nullptr);
3767 return true;
3768 }, per_objfile);
3769 }
3770 else
3771 {
3772 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3773 proceed assuming all symtabs have been read in. */
3774 }
3775
3776 for (compunit_symtab *cust : objfile->compunits ())
3777 {
3778 const struct block *block;
3779
3780 if (cust == NULL)
3781 continue;
3782 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3783 if (!iterate_over_symbols_terminated (block, name,
3784 domain, callback))
3785 return;
3786 }
3787 }
3788
3789 /* Starting from a search name, return the string that finds the upper
3790 bound of all strings that start with SEARCH_NAME in a sorted name
3791 list. Returns the empty string to indicate that the upper bound is
3792 the end of the list. */
3793
3794 static std::string
3795 make_sort_after_prefix_name (const char *search_name)
3796 {
3797 /* When looking to complete "func", we find the upper bound of all
3798 symbols that start with "func" by looking for where we'd insert
3799 the closest string that would follow "func" in lexicographical
3800 order. Usually, that's "func"-with-last-character-incremented,
3801 i.e. "fund". Mind non-ASCII characters, though. Usually those
3802 will be UTF-8 multi-byte sequences, but we can't be certain.
3803 Especially mind the 0xff character, which is a valid character in
3804 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3805 rule out compilers allowing it in identifiers. Note that
3806 conveniently, strcmp/strcasecmp are specified to compare
3807 characters interpreted as unsigned char. So what we do is treat
3808 the whole string as a base 256 number composed of a sequence of
3809 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3810 to 0, and carries 1 to the following more-significant position.
3811 If the very first character in SEARCH_NAME ends up incremented
3812 and carries/overflows, then the upper bound is the end of the
3813 list. The string after the empty string is also the empty
3814 string.
3815
3816 Some examples of this operation:
3817
3818 SEARCH_NAME => "+1" RESULT
3819
3820 "abc" => "abd"
3821 "ab\xff" => "ac"
3822 "\xff" "a" "\xff" => "\xff" "b"
3823 "\xff" => ""
3824 "\xff\xff" => ""
3825 "" => ""
3826
3827 Then, with these symbols for example:
3828
3829 func
3830 func1
3831 fund
3832
3833 completing "func" looks for symbols between "func" and
3834 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3835 which finds "func" and "func1", but not "fund".
3836
3837 And with:
3838
3839 funcÿ (Latin1 'ÿ' [0xff])
3840 funcÿ1
3841 fund
3842
3843 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3844 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3845
3846 And with:
3847
3848 ÿÿ (Latin1 'ÿ' [0xff])
3849 ÿÿ1
3850
3851 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3852 the end of the list.
3853 */
3854 std::string after = search_name;
3855 while (!after.empty () && (unsigned char) after.back () == 0xff)
3856 after.pop_back ();
3857 if (!after.empty ())
3858 after.back () = (unsigned char) after.back () + 1;
3859 return after;
3860 }
3861
3862 /* See declaration. */
3863
3864 std::pair<std::vector<name_component>::const_iterator,
3865 std::vector<name_component>::const_iterator>
3866 mapped_index_base::find_name_components_bounds
3867 (const lookup_name_info &lookup_name_without_params, language lang,
3868 dwarf2_per_objfile *per_objfile) const
3869 {
3870 auto *name_cmp
3871 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3872
3873 const char *lang_name
3874 = lookup_name_without_params.language_lookup_name (lang);
3875
3876 /* Comparison function object for lower_bound that matches against a
3877 given symbol name. */
3878 auto lookup_compare_lower = [&] (const name_component &elem,
3879 const char *name)
3880 {
3881 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3882 const char *elem_name = elem_qualified + elem.name_offset;
3883 return name_cmp (elem_name, name) < 0;
3884 };
3885
3886 /* Comparison function object for upper_bound that matches against a
3887 given symbol name. */
3888 auto lookup_compare_upper = [&] (const char *name,
3889 const name_component &elem)
3890 {
3891 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3892 const char *elem_name = elem_qualified + elem.name_offset;
3893 return name_cmp (name, elem_name) < 0;
3894 };
3895
3896 auto begin = this->name_components.begin ();
3897 auto end = this->name_components.end ();
3898
3899 /* Find the lower bound. */
3900 auto lower = [&] ()
3901 {
3902 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3903 return begin;
3904 else
3905 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3906 } ();
3907
3908 /* Find the upper bound. */
3909 auto upper = [&] ()
3910 {
3911 if (lookup_name_without_params.completion_mode ())
3912 {
3913 /* In completion mode, we want UPPER to point past all
3914 symbols names that have the same prefix. I.e., with
3915 these symbols, and completing "func":
3916
3917 function << lower bound
3918 function1
3919 other_function << upper bound
3920
3921 We find the upper bound by looking for the insertion
3922 point of "func"-with-last-character-incremented,
3923 i.e. "fund". */
3924 std::string after = make_sort_after_prefix_name (lang_name);
3925 if (after.empty ())
3926 return end;
3927 return std::lower_bound (lower, end, after.c_str (),
3928 lookup_compare_lower);
3929 }
3930 else
3931 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3932 } ();
3933
3934 return {lower, upper};
3935 }
3936
3937 /* See declaration. */
3938
3939 void
3940 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3941 {
3942 if (!this->name_components.empty ())
3943 return;
3944
3945 this->name_components_casing = case_sensitivity;
3946 auto *name_cmp
3947 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3948
3949 /* The code below only knows how to break apart components of C++
3950 symbol names (and other languages that use '::' as
3951 namespace/module separator) and Ada symbol names. */
3952 auto count = this->symbol_name_count ();
3953 for (offset_type idx = 0; idx < count; idx++)
3954 {
3955 if (this->symbol_name_slot_invalid (idx))
3956 continue;
3957
3958 const char *name = this->symbol_name_at (idx, per_objfile);
3959
3960 /* Add each name component to the name component table. */
3961 unsigned int previous_len = 0;
3962
3963 if (strstr (name, "::") != nullptr)
3964 {
3965 for (unsigned int current_len = cp_find_first_component (name);
3966 name[current_len] != '\0';
3967 current_len += cp_find_first_component (name + current_len))
3968 {
3969 gdb_assert (name[current_len] == ':');
3970 this->name_components.push_back ({previous_len, idx});
3971 /* Skip the '::'. */
3972 current_len += 2;
3973 previous_len = current_len;
3974 }
3975 }
3976 else
3977 {
3978 /* Handle the Ada encoded (aka mangled) form here. */
3979 for (const char *iter = strstr (name, "__");
3980 iter != nullptr;
3981 iter = strstr (iter, "__"))
3982 {
3983 this->name_components.push_back ({previous_len, idx});
3984 iter += 2;
3985 previous_len = iter - name;
3986 }
3987 }
3988
3989 this->name_components.push_back ({previous_len, idx});
3990 }
3991
3992 /* Sort name_components elements by name. */
3993 auto name_comp_compare = [&] (const name_component &left,
3994 const name_component &right)
3995 {
3996 const char *left_qualified
3997 = this->symbol_name_at (left.idx, per_objfile);
3998 const char *right_qualified
3999 = this->symbol_name_at (right.idx, per_objfile);
4000
4001 const char *left_name = left_qualified + left.name_offset;
4002 const char *right_name = right_qualified + right.name_offset;
4003
4004 return name_cmp (left_name, right_name) < 0;
4005 };
4006
4007 std::sort (this->name_components.begin (),
4008 this->name_components.end (),
4009 name_comp_compare);
4010 }
4011
4012 /* Helper for dw2_expand_symtabs_matching that works with a
4013 mapped_index_base instead of the containing objfile. This is split
4014 to a separate function in order to be able to unit test the
4015 name_components matching using a mock mapped_index_base. For each
4016 symbol name that matches, calls MATCH_CALLBACK, passing it the
4017 symbol's index in the mapped_index_base symbol table. */
4018
4019 static void
4020 dw2_expand_symtabs_matching_symbol
4021 (mapped_index_base &index,
4022 const lookup_name_info &lookup_name_in,
4023 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4024 enum search_domain kind,
4025 gdb::function_view<bool (offset_type)> match_callback,
4026 dwarf2_per_objfile *per_objfile)
4027 {
4028 lookup_name_info lookup_name_without_params
4029 = lookup_name_in.make_ignore_params ();
4030
4031 /* Build the symbol name component sorted vector, if we haven't
4032 yet. */
4033 index.build_name_components (per_objfile);
4034
4035 /* The same symbol may appear more than once in the range though.
4036 E.g., if we're looking for symbols that complete "w", and we have
4037 a symbol named "w1::w2", we'll find the two name components for
4038 that same symbol in the range. To be sure we only call the
4039 callback once per symbol, we first collect the symbol name
4040 indexes that matched in a temporary vector and ignore
4041 duplicates. */
4042 std::vector<offset_type> matches;
4043
4044 struct name_and_matcher
4045 {
4046 symbol_name_matcher_ftype *matcher;
4047 const char *name;
4048
4049 bool operator== (const name_and_matcher &other) const
4050 {
4051 return matcher == other.matcher && strcmp (name, other.name) == 0;
4052 }
4053 };
4054
4055 /* A vector holding all the different symbol name matchers, for all
4056 languages. */
4057 std::vector<name_and_matcher> matchers;
4058
4059 for (int i = 0; i < nr_languages; i++)
4060 {
4061 enum language lang_e = (enum language) i;
4062
4063 const language_defn *lang = language_def (lang_e);
4064 symbol_name_matcher_ftype *name_matcher
4065 = get_symbol_name_matcher (lang, lookup_name_without_params);
4066
4067 name_and_matcher key {
4068 name_matcher,
4069 lookup_name_without_params.language_lookup_name (lang_e)
4070 };
4071
4072 /* Don't insert the same comparison routine more than once.
4073 Note that we do this linear walk. This is not a problem in
4074 practice because the number of supported languages is
4075 low. */
4076 if (std::find (matchers.begin (), matchers.end (), key)
4077 != matchers.end ())
4078 continue;
4079 matchers.push_back (std::move (key));
4080
4081 auto bounds
4082 = index.find_name_components_bounds (lookup_name_without_params,
4083 lang_e, per_objfile);
4084
4085 /* Now for each symbol name in range, check to see if we have a name
4086 match, and if so, call the MATCH_CALLBACK callback. */
4087
4088 for (; bounds.first != bounds.second; ++bounds.first)
4089 {
4090 const char *qualified
4091 = index.symbol_name_at (bounds.first->idx, per_objfile);
4092
4093 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4094 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4095 continue;
4096
4097 matches.push_back (bounds.first->idx);
4098 }
4099 }
4100
4101 std::sort (matches.begin (), matches.end ());
4102
4103 /* Finally call the callback, once per match. */
4104 ULONGEST prev = -1;
4105 for (offset_type idx : matches)
4106 {
4107 if (prev != idx)
4108 {
4109 if (!match_callback (idx))
4110 break;
4111 prev = idx;
4112 }
4113 }
4114
4115 /* Above we use a type wider than idx's for 'prev', since 0 and
4116 (offset_type)-1 are both possible values. */
4117 static_assert (sizeof (prev) > sizeof (offset_type), "");
4118 }
4119
4120 #if GDB_SELF_TEST
4121
4122 namespace selftests { namespace dw2_expand_symtabs_matching {
4123
4124 /* A mock .gdb_index/.debug_names-like name index table, enough to
4125 exercise dw2_expand_symtabs_matching_symbol, which works with the
4126 mapped_index_base interface. Builds an index from the symbol list
4127 passed as parameter to the constructor. */
4128 class mock_mapped_index : public mapped_index_base
4129 {
4130 public:
4131 mock_mapped_index (gdb::array_view<const char *> symbols)
4132 : m_symbol_table (symbols)
4133 {}
4134
4135 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4136
4137 /* Return the number of names in the symbol table. */
4138 size_t symbol_name_count () const override
4139 {
4140 return m_symbol_table.size ();
4141 }
4142
4143 /* Get the name of the symbol at IDX in the symbol table. */
4144 const char *symbol_name_at
4145 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4146 {
4147 return m_symbol_table[idx];
4148 }
4149
4150 private:
4151 gdb::array_view<const char *> m_symbol_table;
4152 };
4153
4154 /* Convenience function that converts a NULL pointer to a "<null>"
4155 string, to pass to print routines. */
4156
4157 static const char *
4158 string_or_null (const char *str)
4159 {
4160 return str != NULL ? str : "<null>";
4161 }
4162
4163 /* Check if a lookup_name_info built from
4164 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4165 index. EXPECTED_LIST is the list of expected matches, in expected
4166 matching order. If no match expected, then an empty list is
4167 specified. Returns true on success. On failure prints a warning
4168 indicating the file:line that failed, and returns false. */
4169
4170 static bool
4171 check_match (const char *file, int line,
4172 mock_mapped_index &mock_index,
4173 const char *name, symbol_name_match_type match_type,
4174 bool completion_mode,
4175 std::initializer_list<const char *> expected_list,
4176 dwarf2_per_objfile *per_objfile)
4177 {
4178 lookup_name_info lookup_name (name, match_type, completion_mode);
4179
4180 bool matched = true;
4181
4182 auto mismatch = [&] (const char *expected_str,
4183 const char *got)
4184 {
4185 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4186 "expected=\"%s\", got=\"%s\"\n"),
4187 file, line,
4188 (match_type == symbol_name_match_type::FULL
4189 ? "FULL" : "WILD"),
4190 name, string_or_null (expected_str), string_or_null (got));
4191 matched = false;
4192 };
4193
4194 auto expected_it = expected_list.begin ();
4195 auto expected_end = expected_list.end ();
4196
4197 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4198 NULL, ALL_DOMAIN,
4199 [&] (offset_type idx)
4200 {
4201 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4202 const char *expected_str
4203 = expected_it == expected_end ? NULL : *expected_it++;
4204
4205 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4206 mismatch (expected_str, matched_name);
4207 return true;
4208 }, per_objfile);
4209
4210 const char *expected_str
4211 = expected_it == expected_end ? NULL : *expected_it++;
4212 if (expected_str != NULL)
4213 mismatch (expected_str, NULL);
4214
4215 return matched;
4216 }
4217
4218 /* The symbols added to the mock mapped_index for testing (in
4219 canonical form). */
4220 static const char *test_symbols[] = {
4221 "function",
4222 "std::bar",
4223 "std::zfunction",
4224 "std::zfunction2",
4225 "w1::w2",
4226 "ns::foo<char*>",
4227 "ns::foo<int>",
4228 "ns::foo<long>",
4229 "ns2::tmpl<int>::foo2",
4230 "(anonymous namespace)::A::B::C",
4231
4232 /* These are used to check that the increment-last-char in the
4233 matching algorithm for completion doesn't match "t1_fund" when
4234 completing "t1_func". */
4235 "t1_func",
4236 "t1_func1",
4237 "t1_fund",
4238 "t1_fund1",
4239
4240 /* A UTF-8 name with multi-byte sequences to make sure that
4241 cp-name-parser understands this as a single identifier ("função"
4242 is "function" in PT). */
4243 u8"u8função",
4244
4245 /* \377 (0xff) is Latin1 'ÿ'. */
4246 "yfunc\377",
4247
4248 /* \377 (0xff) is Latin1 'ÿ'. */
4249 "\377",
4250 "\377\377123",
4251
4252 /* A name with all sorts of complications. Starts with "z" to make
4253 it easier for the completion tests below. */
4254 #define Z_SYM_NAME \
4255 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4256 "::tuple<(anonymous namespace)::ui*, " \
4257 "std::default_delete<(anonymous namespace)::ui>, void>"
4258
4259 Z_SYM_NAME
4260 };
4261
4262 /* Returns true if the mapped_index_base::find_name_component_bounds
4263 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4264 in completion mode. */
4265
4266 static bool
4267 check_find_bounds_finds (mapped_index_base &index,
4268 const char *search_name,
4269 gdb::array_view<const char *> expected_syms,
4270 dwarf2_per_objfile *per_objfile)
4271 {
4272 lookup_name_info lookup_name (search_name,
4273 symbol_name_match_type::FULL, true);
4274
4275 auto bounds = index.find_name_components_bounds (lookup_name,
4276 language_cplus,
4277 per_objfile);
4278
4279 size_t distance = std::distance (bounds.first, bounds.second);
4280 if (distance != expected_syms.size ())
4281 return false;
4282
4283 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4284 {
4285 auto nc_elem = bounds.first + exp_elem;
4286 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4287 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4288 return false;
4289 }
4290
4291 return true;
4292 }
4293
4294 /* Test the lower-level mapped_index::find_name_component_bounds
4295 method. */
4296
4297 static void
4298 test_mapped_index_find_name_component_bounds ()
4299 {
4300 mock_mapped_index mock_index (test_symbols);
4301
4302 mock_index.build_name_components (NULL /* per_objfile */);
4303
4304 /* Test the lower-level mapped_index::find_name_component_bounds
4305 method in completion mode. */
4306 {
4307 static const char *expected_syms[] = {
4308 "t1_func",
4309 "t1_func1",
4310 };
4311
4312 SELF_CHECK (check_find_bounds_finds
4313 (mock_index, "t1_func", expected_syms,
4314 NULL /* per_objfile */));
4315 }
4316
4317 /* Check that the increment-last-char in the name matching algorithm
4318 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4319 {
4320 static const char *expected_syms1[] = {
4321 "\377",
4322 "\377\377123",
4323 };
4324 SELF_CHECK (check_find_bounds_finds
4325 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4326
4327 static const char *expected_syms2[] = {
4328 "\377\377123",
4329 };
4330 SELF_CHECK (check_find_bounds_finds
4331 (mock_index, "\377\377", expected_syms2,
4332 NULL /* per_objfile */));
4333 }
4334 }
4335
4336 /* Test dw2_expand_symtabs_matching_symbol. */
4337
4338 static void
4339 test_dw2_expand_symtabs_matching_symbol ()
4340 {
4341 mock_mapped_index mock_index (test_symbols);
4342
4343 /* We let all tests run until the end even if some fails, for debug
4344 convenience. */
4345 bool any_mismatch = false;
4346
4347 /* Create the expected symbols list (an initializer_list). Needed
4348 because lists have commas, and we need to pass them to CHECK,
4349 which is a macro. */
4350 #define EXPECT(...) { __VA_ARGS__ }
4351
4352 /* Wrapper for check_match that passes down the current
4353 __FILE__/__LINE__. */
4354 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4355 any_mismatch |= !check_match (__FILE__, __LINE__, \
4356 mock_index, \
4357 NAME, MATCH_TYPE, COMPLETION_MODE, \
4358 EXPECTED_LIST, NULL)
4359
4360 /* Identity checks. */
4361 for (const char *sym : test_symbols)
4362 {
4363 /* Should be able to match all existing symbols. */
4364 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4365 EXPECT (sym));
4366
4367 /* Should be able to match all existing symbols with
4368 parameters. */
4369 std::string with_params = std::string (sym) + "(int)";
4370 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4371 EXPECT (sym));
4372
4373 /* Should be able to match all existing symbols with
4374 parameters and qualifiers. */
4375 with_params = std::string (sym) + " ( int ) const";
4376 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4377 EXPECT (sym));
4378
4379 /* This should really find sym, but cp-name-parser.y doesn't
4380 know about lvalue/rvalue qualifiers yet. */
4381 with_params = std::string (sym) + " ( int ) &&";
4382 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4383 {});
4384 }
4385
4386 /* Check that the name matching algorithm for completion doesn't get
4387 confused with Latin1 'ÿ' / 0xff. */
4388 {
4389 static const char str[] = "\377";
4390 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4391 EXPECT ("\377", "\377\377123"));
4392 }
4393
4394 /* Check that the increment-last-char in the matching algorithm for
4395 completion doesn't match "t1_fund" when completing "t1_func". */
4396 {
4397 static const char str[] = "t1_func";
4398 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4399 EXPECT ("t1_func", "t1_func1"));
4400 }
4401
4402 /* Check that completion mode works at each prefix of the expected
4403 symbol name. */
4404 {
4405 static const char str[] = "function(int)";
4406 size_t len = strlen (str);
4407 std::string lookup;
4408
4409 for (size_t i = 1; i < len; i++)
4410 {
4411 lookup.assign (str, i);
4412 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4413 EXPECT ("function"));
4414 }
4415 }
4416
4417 /* While "w" is a prefix of both components, the match function
4418 should still only be called once. */
4419 {
4420 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4421 EXPECT ("w1::w2"));
4422 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4423 EXPECT ("w1::w2"));
4424 }
4425
4426 /* Same, with a "complicated" symbol. */
4427 {
4428 static const char str[] = Z_SYM_NAME;
4429 size_t len = strlen (str);
4430 std::string lookup;
4431
4432 for (size_t i = 1; i < len; i++)
4433 {
4434 lookup.assign (str, i);
4435 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4436 EXPECT (Z_SYM_NAME));
4437 }
4438 }
4439
4440 /* In FULL mode, an incomplete symbol doesn't match. */
4441 {
4442 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4443 {});
4444 }
4445
4446 /* A complete symbol with parameters matches any overload, since the
4447 index has no overload info. */
4448 {
4449 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4450 EXPECT ("std::zfunction", "std::zfunction2"));
4451 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4452 EXPECT ("std::zfunction", "std::zfunction2"));
4453 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4454 EXPECT ("std::zfunction", "std::zfunction2"));
4455 }
4456
4457 /* Check that whitespace is ignored appropriately. A symbol with a
4458 template argument list. */
4459 {
4460 static const char expected[] = "ns::foo<int>";
4461 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4462 EXPECT (expected));
4463 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4464 EXPECT (expected));
4465 }
4466
4467 /* Check that whitespace is ignored appropriately. A symbol with a
4468 template argument list that includes a pointer. */
4469 {
4470 static const char expected[] = "ns::foo<char*>";
4471 /* Try both completion and non-completion modes. */
4472 static const bool completion_mode[2] = {false, true};
4473 for (size_t i = 0; i < 2; i++)
4474 {
4475 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4476 completion_mode[i], EXPECT (expected));
4477 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4478 completion_mode[i], EXPECT (expected));
4479
4480 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4481 completion_mode[i], EXPECT (expected));
4482 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4483 completion_mode[i], EXPECT (expected));
4484 }
4485 }
4486
4487 {
4488 /* Check method qualifiers are ignored. */
4489 static const char expected[] = "ns::foo<char*>";
4490 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4491 symbol_name_match_type::FULL, true, EXPECT (expected));
4492 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4493 symbol_name_match_type::FULL, true, EXPECT (expected));
4494 CHECK_MATCH ("foo < char * > ( int ) const",
4495 symbol_name_match_type::WILD, true, EXPECT (expected));
4496 CHECK_MATCH ("foo < char * > ( int ) &&",
4497 symbol_name_match_type::WILD, true, EXPECT (expected));
4498 }
4499
4500 /* Test lookup names that don't match anything. */
4501 {
4502 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4503 {});
4504
4505 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4506 {});
4507 }
4508
4509 /* Some wild matching tests, exercising "(anonymous namespace)",
4510 which should not be confused with a parameter list. */
4511 {
4512 static const char *syms[] = {
4513 "A::B::C",
4514 "B::C",
4515 "C",
4516 "A :: B :: C ( int )",
4517 "B :: C ( int )",
4518 "C ( int )",
4519 };
4520
4521 for (const char *s : syms)
4522 {
4523 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4524 EXPECT ("(anonymous namespace)::A::B::C"));
4525 }
4526 }
4527
4528 {
4529 static const char expected[] = "ns2::tmpl<int>::foo2";
4530 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4531 EXPECT (expected));
4532 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4533 EXPECT (expected));
4534 }
4535
4536 SELF_CHECK (!any_mismatch);
4537
4538 #undef EXPECT
4539 #undef CHECK_MATCH
4540 }
4541
4542 static void
4543 run_test ()
4544 {
4545 test_mapped_index_find_name_component_bounds ();
4546 test_dw2_expand_symtabs_matching_symbol ();
4547 }
4548
4549 }} // namespace selftests::dw2_expand_symtabs_matching
4550
4551 #endif /* GDB_SELF_TEST */
4552
4553 /* If FILE_MATCHER is NULL or if PER_CU has
4554 dwarf2_per_cu_quick_data::MARK set (see
4555 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4556 EXPANSION_NOTIFY on it. */
4557
4558 static void
4559 dw2_expand_symtabs_matching_one
4560 (dwarf2_per_cu_data *per_cu,
4561 dwarf2_per_objfile *per_objfile,
4562 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4563 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4564 {
4565 if (file_matcher == NULL || per_cu->v.quick->mark)
4566 {
4567 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4568
4569 compunit_symtab *symtab
4570 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4571 gdb_assert (symtab != nullptr);
4572
4573 if (expansion_notify != NULL && symtab_was_null)
4574 expansion_notify (symtab);
4575 }
4576 }
4577
4578 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4579 matched, to expand corresponding CUs that were marked. IDX is the
4580 index of the symbol name that matched. */
4581
4582 static void
4583 dw2_expand_marked_cus
4584 (dwarf2_per_objfile *per_objfile, offset_type idx,
4585 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4586 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4587 search_domain kind)
4588 {
4589 offset_type *vec, vec_len, vec_idx;
4590 bool global_seen = false;
4591 mapped_index &index = *per_objfile->per_bfd->index_table;
4592
4593 vec = (offset_type *) (index.constant_pool
4594 + MAYBE_SWAP (index.symbol_table[idx].vec));
4595 vec_len = MAYBE_SWAP (vec[0]);
4596 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4597 {
4598 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4599 /* This value is only valid for index versions >= 7. */
4600 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4601 gdb_index_symbol_kind symbol_kind =
4602 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4603 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4604 /* Only check the symbol attributes if they're present.
4605 Indices prior to version 7 don't record them,
4606 and indices >= 7 may elide them for certain symbols
4607 (gold does this). */
4608 int attrs_valid =
4609 (index.version >= 7
4610 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4611
4612 /* Work around gold/15646. */
4613 if (attrs_valid
4614 && !is_static
4615 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4616 {
4617 if (global_seen)
4618 continue;
4619
4620 global_seen = true;
4621 }
4622
4623 /* Only check the symbol's kind if it has one. */
4624 if (attrs_valid)
4625 {
4626 switch (kind)
4627 {
4628 case VARIABLES_DOMAIN:
4629 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4630 continue;
4631 break;
4632 case FUNCTIONS_DOMAIN:
4633 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4634 continue;
4635 break;
4636 case TYPES_DOMAIN:
4637 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4638 continue;
4639 break;
4640 case MODULES_DOMAIN:
4641 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4642 continue;
4643 break;
4644 default:
4645 break;
4646 }
4647 }
4648
4649 /* Don't crash on bad data. */
4650 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4651 + per_objfile->per_bfd->all_type_units.size ()))
4652 {
4653 complaint (_(".gdb_index entry has bad CU index"
4654 " [in module %s]"), objfile_name (per_objfile->objfile));
4655 continue;
4656 }
4657
4658 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4659 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4660 expansion_notify);
4661 }
4662 }
4663
4664 /* If FILE_MATCHER is non-NULL, set all the
4665 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4666 that match FILE_MATCHER. */
4667
4668 static void
4669 dw_expand_symtabs_matching_file_matcher
4670 (dwarf2_per_objfile *per_objfile,
4671 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4672 {
4673 if (file_matcher == NULL)
4674 return;
4675
4676 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4677 htab_eq_pointer,
4678 NULL, xcalloc, xfree));
4679 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4680 htab_eq_pointer,
4681 NULL, xcalloc, xfree));
4682
4683 /* The rule is CUs specify all the files, including those used by
4684 any TU, so there's no need to scan TUs here. */
4685
4686 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4687 {
4688 QUIT;
4689
4690 per_cu->v.quick->mark = 0;
4691
4692 /* We only need to look at symtabs not already expanded. */
4693 if (per_objfile->symtab_set_p (per_cu))
4694 continue;
4695
4696 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4697 if (file_data == NULL)
4698 continue;
4699
4700 if (htab_find (visited_not_found.get (), file_data) != NULL)
4701 continue;
4702 else if (htab_find (visited_found.get (), file_data) != NULL)
4703 {
4704 per_cu->v.quick->mark = 1;
4705 continue;
4706 }
4707
4708 for (int j = 0; j < file_data->num_file_names; ++j)
4709 {
4710 const char *this_real_name;
4711
4712 if (file_matcher (file_data->file_names[j], false))
4713 {
4714 per_cu->v.quick->mark = 1;
4715 break;
4716 }
4717
4718 /* Before we invoke realpath, which can get expensive when many
4719 files are involved, do a quick comparison of the basenames. */
4720 if (!basenames_may_differ
4721 && !file_matcher (lbasename (file_data->file_names[j]),
4722 true))
4723 continue;
4724
4725 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4726 if (file_matcher (this_real_name, false))
4727 {
4728 per_cu->v.quick->mark = 1;
4729 break;
4730 }
4731 }
4732
4733 void **slot = htab_find_slot (per_cu->v.quick->mark
4734 ? visited_found.get ()
4735 : visited_not_found.get (),
4736 file_data, INSERT);
4737 *slot = file_data;
4738 }
4739 }
4740
4741 static void
4742 dw2_expand_symtabs_matching
4743 (struct objfile *objfile,
4744 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4745 const lookup_name_info *lookup_name,
4746 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4747 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4748 enum search_domain kind)
4749 {
4750 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4751
4752 /* index_table is NULL if OBJF_READNOW. */
4753 if (!per_objfile->per_bfd->index_table)
4754 return;
4755
4756 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4757
4758 if (symbol_matcher == NULL && lookup_name == NULL)
4759 {
4760 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4761 {
4762 QUIT;
4763
4764 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4765 file_matcher, expansion_notify);
4766 }
4767 return;
4768 }
4769
4770 mapped_index &index = *per_objfile->per_bfd->index_table;
4771
4772 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4773 symbol_matcher,
4774 kind, [&] (offset_type idx)
4775 {
4776 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4777 kind);
4778 return true;
4779 }, per_objfile);
4780 }
4781
4782 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4783 symtab. */
4784
4785 static struct compunit_symtab *
4786 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4787 CORE_ADDR pc)
4788 {
4789 int i;
4790
4791 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4792 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4793 return cust;
4794
4795 if (cust->includes == NULL)
4796 return NULL;
4797
4798 for (i = 0; cust->includes[i]; ++i)
4799 {
4800 struct compunit_symtab *s = cust->includes[i];
4801
4802 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4803 if (s != NULL)
4804 return s;
4805 }
4806
4807 return NULL;
4808 }
4809
4810 static struct compunit_symtab *
4811 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4812 struct bound_minimal_symbol msymbol,
4813 CORE_ADDR pc,
4814 struct obj_section *section,
4815 int warn_if_readin)
4816 {
4817 struct dwarf2_per_cu_data *data;
4818 struct compunit_symtab *result;
4819
4820 if (!objfile->partial_symtabs->psymtabs_addrmap)
4821 return NULL;
4822
4823 CORE_ADDR baseaddr = objfile->text_section_offset ();
4824 data = (struct dwarf2_per_cu_data *) addrmap_find
4825 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4826 if (!data)
4827 return NULL;
4828
4829 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4830 if (warn_if_readin && per_objfile->symtab_set_p (data))
4831 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4832 paddress (objfile->arch (), pc));
4833
4834 result = recursively_find_pc_sect_compunit_symtab
4835 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4836
4837 gdb_assert (result != NULL);
4838 return result;
4839 }
4840
4841 static void
4842 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4843 void *data, int need_fullname)
4844 {
4845 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4846
4847 if (!per_objfile->per_bfd->filenames_cache)
4848 {
4849 per_objfile->per_bfd->filenames_cache.emplace ();
4850
4851 htab_up visited (htab_create_alloc (10,
4852 htab_hash_pointer, htab_eq_pointer,
4853 NULL, xcalloc, xfree));
4854
4855 /* The rule is CUs specify all the files, including those used
4856 by any TU, so there's no need to scan TUs here. We can
4857 ignore file names coming from already-expanded CUs. */
4858
4859 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4860 {
4861 if (per_objfile->symtab_set_p (per_cu))
4862 {
4863 void **slot = htab_find_slot (visited.get (),
4864 per_cu->v.quick->file_names,
4865 INSERT);
4866
4867 *slot = per_cu->v.quick->file_names;
4868 }
4869 }
4870
4871 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4872 {
4873 /* We only need to look at symtabs not already expanded. */
4874 if (per_objfile->symtab_set_p (per_cu))
4875 continue;
4876
4877 quick_file_names *file_data
4878 = dw2_get_file_names (per_cu, per_objfile);
4879 if (file_data == NULL)
4880 continue;
4881
4882 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4883 if (*slot)
4884 {
4885 /* Already visited. */
4886 continue;
4887 }
4888 *slot = file_data;
4889
4890 for (int j = 0; j < file_data->num_file_names; ++j)
4891 {
4892 const char *filename = file_data->file_names[j];
4893 per_objfile->per_bfd->filenames_cache->seen (filename);
4894 }
4895 }
4896 }
4897
4898 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4899 {
4900 gdb::unique_xmalloc_ptr<char> this_real_name;
4901
4902 if (need_fullname)
4903 this_real_name = gdb_realpath (filename);
4904 (*fun) (filename, this_real_name.get (), data);
4905 });
4906 }
4907
4908 static int
4909 dw2_has_symbols (struct objfile *objfile)
4910 {
4911 return 1;
4912 }
4913
4914 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4915 {
4916 dw2_has_symbols,
4917 dw2_find_last_source_symtab,
4918 dw2_forget_cached_source_info,
4919 dw2_map_symtabs_matching_filename,
4920 dw2_lookup_symbol,
4921 NULL,
4922 dw2_print_stats,
4923 dw2_dump,
4924 dw2_expand_symtabs_for_function,
4925 dw2_expand_all_symtabs,
4926 dw2_expand_symtabs_with_fullname,
4927 dw2_map_matching_symbols,
4928 dw2_expand_symtabs_matching,
4929 dw2_find_pc_sect_compunit_symtab,
4930 NULL,
4931 dw2_map_symbol_filenames
4932 };
4933
4934 /* DWARF-5 debug_names reader. */
4935
4936 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4937 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4938
4939 /* A helper function that reads the .debug_names section in SECTION
4940 and fills in MAP. FILENAME is the name of the file containing the
4941 section; it is used for error reporting.
4942
4943 Returns true if all went well, false otherwise. */
4944
4945 static bool
4946 read_debug_names_from_section (struct objfile *objfile,
4947 const char *filename,
4948 struct dwarf2_section_info *section,
4949 mapped_debug_names &map)
4950 {
4951 if (section->empty ())
4952 return false;
4953
4954 /* Older elfutils strip versions could keep the section in the main
4955 executable while splitting it for the separate debug info file. */
4956 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4957 return false;
4958
4959 section->read (objfile);
4960
4961 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4962
4963 const gdb_byte *addr = section->buffer;
4964
4965 bfd *const abfd = section->get_bfd_owner ();
4966
4967 unsigned int bytes_read;
4968 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4969 addr += bytes_read;
4970
4971 map.dwarf5_is_dwarf64 = bytes_read != 4;
4972 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4973 if (bytes_read + length != section->size)
4974 {
4975 /* There may be multiple per-CU indices. */
4976 warning (_("Section .debug_names in %s length %s does not match "
4977 "section length %s, ignoring .debug_names."),
4978 filename, plongest (bytes_read + length),
4979 pulongest (section->size));
4980 return false;
4981 }
4982
4983 /* The version number. */
4984 uint16_t version = read_2_bytes (abfd, addr);
4985 addr += 2;
4986 if (version != 5)
4987 {
4988 warning (_("Section .debug_names in %s has unsupported version %d, "
4989 "ignoring .debug_names."),
4990 filename, version);
4991 return false;
4992 }
4993
4994 /* Padding. */
4995 uint16_t padding = read_2_bytes (abfd, addr);
4996 addr += 2;
4997 if (padding != 0)
4998 {
4999 warning (_("Section .debug_names in %s has unsupported padding %d, "
5000 "ignoring .debug_names."),
5001 filename, padding);
5002 return false;
5003 }
5004
5005 /* comp_unit_count - The number of CUs in the CU list. */
5006 map.cu_count = read_4_bytes (abfd, addr);
5007 addr += 4;
5008
5009 /* local_type_unit_count - The number of TUs in the local TU
5010 list. */
5011 map.tu_count = read_4_bytes (abfd, addr);
5012 addr += 4;
5013
5014 /* foreign_type_unit_count - The number of TUs in the foreign TU
5015 list. */
5016 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5017 addr += 4;
5018 if (foreign_tu_count != 0)
5019 {
5020 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5021 "ignoring .debug_names."),
5022 filename, static_cast<unsigned long> (foreign_tu_count));
5023 return false;
5024 }
5025
5026 /* bucket_count - The number of hash buckets in the hash lookup
5027 table. */
5028 map.bucket_count = read_4_bytes (abfd, addr);
5029 addr += 4;
5030
5031 /* name_count - The number of unique names in the index. */
5032 map.name_count = read_4_bytes (abfd, addr);
5033 addr += 4;
5034
5035 /* abbrev_table_size - The size in bytes of the abbreviations
5036 table. */
5037 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5038 addr += 4;
5039
5040 /* augmentation_string_size - The size in bytes of the augmentation
5041 string. This value is rounded up to a multiple of 4. */
5042 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5043 addr += 4;
5044 map.augmentation_is_gdb = ((augmentation_string_size
5045 == sizeof (dwarf5_augmentation))
5046 && memcmp (addr, dwarf5_augmentation,
5047 sizeof (dwarf5_augmentation)) == 0);
5048 augmentation_string_size += (-augmentation_string_size) & 3;
5049 addr += augmentation_string_size;
5050
5051 /* List of CUs */
5052 map.cu_table_reordered = addr;
5053 addr += map.cu_count * map.offset_size;
5054
5055 /* List of Local TUs */
5056 map.tu_table_reordered = addr;
5057 addr += map.tu_count * map.offset_size;
5058
5059 /* Hash Lookup Table */
5060 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5061 addr += map.bucket_count * 4;
5062 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5063 addr += map.name_count * 4;
5064
5065 /* Name Table */
5066 map.name_table_string_offs_reordered = addr;
5067 addr += map.name_count * map.offset_size;
5068 map.name_table_entry_offs_reordered = addr;
5069 addr += map.name_count * map.offset_size;
5070
5071 const gdb_byte *abbrev_table_start = addr;
5072 for (;;)
5073 {
5074 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5075 addr += bytes_read;
5076 if (index_num == 0)
5077 break;
5078
5079 const auto insertpair
5080 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5081 if (!insertpair.second)
5082 {
5083 warning (_("Section .debug_names in %s has duplicate index %s, "
5084 "ignoring .debug_names."),
5085 filename, pulongest (index_num));
5086 return false;
5087 }
5088 mapped_debug_names::index_val &indexval = insertpair.first->second;
5089 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5090 addr += bytes_read;
5091
5092 for (;;)
5093 {
5094 mapped_debug_names::index_val::attr attr;
5095 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5096 addr += bytes_read;
5097 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5098 addr += bytes_read;
5099 if (attr.form == DW_FORM_implicit_const)
5100 {
5101 attr.implicit_const = read_signed_leb128 (abfd, addr,
5102 &bytes_read);
5103 addr += bytes_read;
5104 }
5105 if (attr.dw_idx == 0 && attr.form == 0)
5106 break;
5107 indexval.attr_vec.push_back (std::move (attr));
5108 }
5109 }
5110 if (addr != abbrev_table_start + abbrev_table_size)
5111 {
5112 warning (_("Section .debug_names in %s has abbreviation_table "
5113 "of size %s vs. written as %u, ignoring .debug_names."),
5114 filename, plongest (addr - abbrev_table_start),
5115 abbrev_table_size);
5116 return false;
5117 }
5118 map.entry_pool = addr;
5119
5120 return true;
5121 }
5122
5123 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5124 list. */
5125
5126 static void
5127 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5128 const mapped_debug_names &map,
5129 dwarf2_section_info &section,
5130 bool is_dwz)
5131 {
5132 if (!map.augmentation_is_gdb)
5133 {
5134 for (uint32_t i = 0; i < map.cu_count; ++i)
5135 {
5136 sect_offset sect_off
5137 = (sect_offset) (extract_unsigned_integer
5138 (map.cu_table_reordered + i * map.offset_size,
5139 map.offset_size,
5140 map.dwarf5_byte_order));
5141 /* We don't know the length of the CU, because the CU list in a
5142 .debug_names index can be incomplete, so we can't use the start of
5143 the next CU as end of this CU. We create the CUs here with length 0,
5144 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5145 dwarf2_per_cu_data *per_cu
5146 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5147 per_bfd->all_comp_units.push_back (per_cu);
5148 }
5149 }
5150
5151 sect_offset sect_off_prev;
5152 for (uint32_t i = 0; i <= map.cu_count; ++i)
5153 {
5154 sect_offset sect_off_next;
5155 if (i < map.cu_count)
5156 {
5157 sect_off_next
5158 = (sect_offset) (extract_unsigned_integer
5159 (map.cu_table_reordered + i * map.offset_size,
5160 map.offset_size,
5161 map.dwarf5_byte_order));
5162 }
5163 else
5164 sect_off_next = (sect_offset) section.size;
5165 if (i >= 1)
5166 {
5167 const ULONGEST length = sect_off_next - sect_off_prev;
5168 dwarf2_per_cu_data *per_cu
5169 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5170 sect_off_prev, length);
5171 per_bfd->all_comp_units.push_back (per_cu);
5172 }
5173 sect_off_prev = sect_off_next;
5174 }
5175 }
5176
5177 /* Read the CU list from the mapped index, and use it to create all
5178 the CU objects for this dwarf2_per_objfile. */
5179
5180 static void
5181 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5182 const mapped_debug_names &map,
5183 const mapped_debug_names &dwz_map)
5184 {
5185 gdb_assert (per_bfd->all_comp_units.empty ());
5186 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5187
5188 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5189 false /* is_dwz */);
5190
5191 if (dwz_map.cu_count == 0)
5192 return;
5193
5194 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5195 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5196 true /* is_dwz */);
5197 }
5198
5199 /* Read .debug_names. If everything went ok, initialize the "quick"
5200 elements of all the CUs and return true. Otherwise, return false. */
5201
5202 static bool
5203 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5204 {
5205 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5206 mapped_debug_names dwz_map;
5207 struct objfile *objfile = per_objfile->objfile;
5208
5209 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5210 &per_objfile->per_bfd->debug_names, *map))
5211 return false;
5212
5213 /* Don't use the index if it's empty. */
5214 if (map->name_count == 0)
5215 return false;
5216
5217 /* If there is a .dwz file, read it so we can get its CU list as
5218 well. */
5219 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
5220 if (dwz != NULL)
5221 {
5222 if (!read_debug_names_from_section (objfile,
5223 bfd_get_filename (dwz->dwz_bfd.get ()),
5224 &dwz->debug_names, dwz_map))
5225 {
5226 warning (_("could not read '.debug_names' section from %s; skipping"),
5227 bfd_get_filename (dwz->dwz_bfd.get ()));
5228 return false;
5229 }
5230 }
5231
5232 create_cus_from_debug_names (per_objfile->per_bfd, *map, dwz_map);
5233
5234 if (map->tu_count != 0)
5235 {
5236 /* We can only handle a single .debug_types when we have an
5237 index. */
5238 if (per_objfile->per_bfd->types.size () != 1)
5239 return false;
5240
5241 dwarf2_section_info *section = &per_objfile->per_bfd->types[0];
5242
5243 create_signatured_type_table_from_debug_names
5244 (per_objfile, *map, section, &per_objfile->per_bfd->abbrev);
5245 }
5246
5247 create_addrmap_from_aranges (per_objfile,
5248 &per_objfile->per_bfd->debug_aranges);
5249
5250 per_objfile->per_bfd->debug_names_table = std::move (map);
5251 per_objfile->per_bfd->using_index = 1;
5252 per_objfile->per_bfd->quick_file_names_table =
5253 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5254
5255 return true;
5256 }
5257
5258 /* Type used to manage iterating over all CUs looking for a symbol for
5259 .debug_names. */
5260
5261 class dw2_debug_names_iterator
5262 {
5263 public:
5264 dw2_debug_names_iterator (const mapped_debug_names &map,
5265 gdb::optional<block_enum> block_index,
5266 domain_enum domain,
5267 const char *name, dwarf2_per_objfile *per_objfile)
5268 : m_map (map), m_block_index (block_index), m_domain (domain),
5269 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5270 m_per_objfile (per_objfile)
5271 {}
5272
5273 dw2_debug_names_iterator (const mapped_debug_names &map,
5274 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5275 : m_map (map),
5276 m_search (search),
5277 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5278 m_per_objfile (per_objfile)
5279 {}
5280
5281 dw2_debug_names_iterator (const mapped_debug_names &map,
5282 block_enum block_index, domain_enum domain,
5283 uint32_t namei, dwarf2_per_objfile *per_objfile)
5284 : m_map (map), m_block_index (block_index), m_domain (domain),
5285 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5286 m_per_objfile (per_objfile)
5287 {}
5288
5289 /* Return the next matching CU or NULL if there are no more. */
5290 dwarf2_per_cu_data *next ();
5291
5292 private:
5293 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5294 const char *name,
5295 dwarf2_per_objfile *per_objfile);
5296 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5297 uint32_t namei,
5298 dwarf2_per_objfile *per_objfile);
5299
5300 /* The internalized form of .debug_names. */
5301 const mapped_debug_names &m_map;
5302
5303 /* If set, only look for symbols that match that block. Valid values are
5304 GLOBAL_BLOCK and STATIC_BLOCK. */
5305 const gdb::optional<block_enum> m_block_index;
5306
5307 /* The kind of symbol we're looking for. */
5308 const domain_enum m_domain = UNDEF_DOMAIN;
5309 const search_domain m_search = ALL_DOMAIN;
5310
5311 /* The list of CUs from the index entry of the symbol, or NULL if
5312 not found. */
5313 const gdb_byte *m_addr;
5314
5315 dwarf2_per_objfile *m_per_objfile;
5316 };
5317
5318 const char *
5319 mapped_debug_names::namei_to_name
5320 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5321 {
5322 const ULONGEST namei_string_offs
5323 = extract_unsigned_integer ((name_table_string_offs_reordered
5324 + namei * offset_size),
5325 offset_size,
5326 dwarf5_byte_order);
5327 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5328 }
5329
5330 /* Find a slot in .debug_names for the object named NAME. If NAME is
5331 found, return pointer to its pool data. If NAME cannot be found,
5332 return NULL. */
5333
5334 const gdb_byte *
5335 dw2_debug_names_iterator::find_vec_in_debug_names
5336 (const mapped_debug_names &map, const char *name,
5337 dwarf2_per_objfile *per_objfile)
5338 {
5339 int (*cmp) (const char *, const char *);
5340
5341 gdb::unique_xmalloc_ptr<char> without_params;
5342 if (current_language->la_language == language_cplus
5343 || current_language->la_language == language_fortran
5344 || current_language->la_language == language_d)
5345 {
5346 /* NAME is already canonical. Drop any qualifiers as
5347 .debug_names does not contain any. */
5348
5349 if (strchr (name, '(') != NULL)
5350 {
5351 without_params = cp_remove_params (name);
5352 if (without_params != NULL)
5353 name = without_params.get ();
5354 }
5355 }
5356
5357 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5358
5359 const uint32_t full_hash = dwarf5_djb_hash (name);
5360 uint32_t namei
5361 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5362 (map.bucket_table_reordered
5363 + (full_hash % map.bucket_count)), 4,
5364 map.dwarf5_byte_order);
5365 if (namei == 0)
5366 return NULL;
5367 --namei;
5368 if (namei >= map.name_count)
5369 {
5370 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5371 "[in module %s]"),
5372 namei, map.name_count,
5373 objfile_name (per_objfile->objfile));
5374 return NULL;
5375 }
5376
5377 for (;;)
5378 {
5379 const uint32_t namei_full_hash
5380 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5381 (map.hash_table_reordered + namei), 4,
5382 map.dwarf5_byte_order);
5383 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5384 return NULL;
5385
5386 if (full_hash == namei_full_hash)
5387 {
5388 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5389
5390 #if 0 /* An expensive sanity check. */
5391 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5392 {
5393 complaint (_("Wrong .debug_names hash for string at index %u "
5394 "[in module %s]"),
5395 namei, objfile_name (dwarf2_per_objfile->objfile));
5396 return NULL;
5397 }
5398 #endif
5399
5400 if (cmp (namei_string, name) == 0)
5401 {
5402 const ULONGEST namei_entry_offs
5403 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5404 + namei * map.offset_size),
5405 map.offset_size, map.dwarf5_byte_order);
5406 return map.entry_pool + namei_entry_offs;
5407 }
5408 }
5409
5410 ++namei;
5411 if (namei >= map.name_count)
5412 return NULL;
5413 }
5414 }
5415
5416 const gdb_byte *
5417 dw2_debug_names_iterator::find_vec_in_debug_names
5418 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5419 {
5420 if (namei >= map.name_count)
5421 {
5422 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5423 "[in module %s]"),
5424 namei, map.name_count,
5425 objfile_name (per_objfile->objfile));
5426 return NULL;
5427 }
5428
5429 const ULONGEST namei_entry_offs
5430 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5431 + namei * map.offset_size),
5432 map.offset_size, map.dwarf5_byte_order);
5433 return map.entry_pool + namei_entry_offs;
5434 }
5435
5436 /* See dw2_debug_names_iterator. */
5437
5438 dwarf2_per_cu_data *
5439 dw2_debug_names_iterator::next ()
5440 {
5441 if (m_addr == NULL)
5442 return NULL;
5443
5444 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5445 struct objfile *objfile = m_per_objfile->objfile;
5446 bfd *const abfd = objfile->obfd;
5447
5448 again:
5449
5450 unsigned int bytes_read;
5451 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5452 m_addr += bytes_read;
5453 if (abbrev == 0)
5454 return NULL;
5455
5456 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5457 if (indexval_it == m_map.abbrev_map.cend ())
5458 {
5459 complaint (_("Wrong .debug_names undefined abbrev code %s "
5460 "[in module %s]"),
5461 pulongest (abbrev), objfile_name (objfile));
5462 return NULL;
5463 }
5464 const mapped_debug_names::index_val &indexval = indexval_it->second;
5465 enum class symbol_linkage {
5466 unknown,
5467 static_,
5468 extern_,
5469 } symbol_linkage_ = symbol_linkage::unknown;
5470 dwarf2_per_cu_data *per_cu = NULL;
5471 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5472 {
5473 ULONGEST ull;
5474 switch (attr.form)
5475 {
5476 case DW_FORM_implicit_const:
5477 ull = attr.implicit_const;
5478 break;
5479 case DW_FORM_flag_present:
5480 ull = 1;
5481 break;
5482 case DW_FORM_udata:
5483 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5484 m_addr += bytes_read;
5485 break;
5486 case DW_FORM_ref4:
5487 ull = read_4_bytes (abfd, m_addr);
5488 m_addr += 4;
5489 break;
5490 case DW_FORM_ref8:
5491 ull = read_8_bytes (abfd, m_addr);
5492 m_addr += 8;
5493 break;
5494 case DW_FORM_ref_sig8:
5495 ull = read_8_bytes (abfd, m_addr);
5496 m_addr += 8;
5497 break;
5498 default:
5499 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5500 dwarf_form_name (attr.form),
5501 objfile_name (objfile));
5502 return NULL;
5503 }
5504 switch (attr.dw_idx)
5505 {
5506 case DW_IDX_compile_unit:
5507 /* Don't crash on bad data. */
5508 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5509 {
5510 complaint (_(".debug_names entry has bad CU index %s"
5511 " [in module %s]"),
5512 pulongest (ull),
5513 objfile_name (objfile));
5514 continue;
5515 }
5516 per_cu = per_bfd->get_cutu (ull);
5517 break;
5518 case DW_IDX_type_unit:
5519 /* Don't crash on bad data. */
5520 if (ull >= per_bfd->all_type_units.size ())
5521 {
5522 complaint (_(".debug_names entry has bad TU index %s"
5523 " [in module %s]"),
5524 pulongest (ull),
5525 objfile_name (objfile));
5526 continue;
5527 }
5528 per_cu = &per_bfd->get_tu (ull)->per_cu;
5529 break;
5530 case DW_IDX_die_offset:
5531 /* In a per-CU index (as opposed to a per-module index), index
5532 entries without CU attribute implicitly refer to the single CU. */
5533 if (per_cu == NULL)
5534 per_cu = per_bfd->get_cu (0);
5535 break;
5536 case DW_IDX_GNU_internal:
5537 if (!m_map.augmentation_is_gdb)
5538 break;
5539 symbol_linkage_ = symbol_linkage::static_;
5540 break;
5541 case DW_IDX_GNU_external:
5542 if (!m_map.augmentation_is_gdb)
5543 break;
5544 symbol_linkage_ = symbol_linkage::extern_;
5545 break;
5546 }
5547 }
5548
5549 /* Skip if already read in. */
5550 if (m_per_objfile->symtab_set_p (per_cu))
5551 goto again;
5552
5553 /* Check static vs global. */
5554 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5555 {
5556 const bool want_static = *m_block_index == STATIC_BLOCK;
5557 const bool symbol_is_static =
5558 symbol_linkage_ == symbol_linkage::static_;
5559 if (want_static != symbol_is_static)
5560 goto again;
5561 }
5562
5563 /* Match dw2_symtab_iter_next, symbol_kind
5564 and debug_names::psymbol_tag. */
5565 switch (m_domain)
5566 {
5567 case VAR_DOMAIN:
5568 switch (indexval.dwarf_tag)
5569 {
5570 case DW_TAG_variable:
5571 case DW_TAG_subprogram:
5572 /* Some types are also in VAR_DOMAIN. */
5573 case DW_TAG_typedef:
5574 case DW_TAG_structure_type:
5575 break;
5576 default:
5577 goto again;
5578 }
5579 break;
5580 case STRUCT_DOMAIN:
5581 switch (indexval.dwarf_tag)
5582 {
5583 case DW_TAG_typedef:
5584 case DW_TAG_structure_type:
5585 break;
5586 default:
5587 goto again;
5588 }
5589 break;
5590 case LABEL_DOMAIN:
5591 switch (indexval.dwarf_tag)
5592 {
5593 case 0:
5594 case DW_TAG_variable:
5595 break;
5596 default:
5597 goto again;
5598 }
5599 break;
5600 case MODULE_DOMAIN:
5601 switch (indexval.dwarf_tag)
5602 {
5603 case DW_TAG_module:
5604 break;
5605 default:
5606 goto again;
5607 }
5608 break;
5609 default:
5610 break;
5611 }
5612
5613 /* Match dw2_expand_symtabs_matching, symbol_kind and
5614 debug_names::psymbol_tag. */
5615 switch (m_search)
5616 {
5617 case VARIABLES_DOMAIN:
5618 switch (indexval.dwarf_tag)
5619 {
5620 case DW_TAG_variable:
5621 break;
5622 default:
5623 goto again;
5624 }
5625 break;
5626 case FUNCTIONS_DOMAIN:
5627 switch (indexval.dwarf_tag)
5628 {
5629 case DW_TAG_subprogram:
5630 break;
5631 default:
5632 goto again;
5633 }
5634 break;
5635 case TYPES_DOMAIN:
5636 switch (indexval.dwarf_tag)
5637 {
5638 case DW_TAG_typedef:
5639 case DW_TAG_structure_type:
5640 break;
5641 default:
5642 goto again;
5643 }
5644 break;
5645 case MODULES_DOMAIN:
5646 switch (indexval.dwarf_tag)
5647 {
5648 case DW_TAG_module:
5649 break;
5650 default:
5651 goto again;
5652 }
5653 default:
5654 break;
5655 }
5656
5657 return per_cu;
5658 }
5659
5660 static struct compunit_symtab *
5661 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5662 const char *name, domain_enum domain)
5663 {
5664 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5665
5666 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5667 if (!mapp)
5668 {
5669 /* index is NULL if OBJF_READNOW. */
5670 return NULL;
5671 }
5672 const auto &map = *mapp;
5673
5674 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5675
5676 struct compunit_symtab *stab_best = NULL;
5677 struct dwarf2_per_cu_data *per_cu;
5678 while ((per_cu = iter.next ()) != NULL)
5679 {
5680 struct symbol *sym, *with_opaque = NULL;
5681 compunit_symtab *stab
5682 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5683 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5684 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5685
5686 sym = block_find_symbol (block, name, domain,
5687 block_find_non_opaque_type_preferred,
5688 &with_opaque);
5689
5690 /* Some caution must be observed with overloaded functions and
5691 methods, since the index will not contain any overload
5692 information (but NAME might contain it). */
5693
5694 if (sym != NULL
5695 && strcmp_iw (sym->search_name (), name) == 0)
5696 return stab;
5697 if (with_opaque != NULL
5698 && strcmp_iw (with_opaque->search_name (), name) == 0)
5699 stab_best = stab;
5700
5701 /* Keep looking through other CUs. */
5702 }
5703
5704 return stab_best;
5705 }
5706
5707 /* This dumps minimal information about .debug_names. It is called
5708 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5709 uses this to verify that .debug_names has been loaded. */
5710
5711 static void
5712 dw2_debug_names_dump (struct objfile *objfile)
5713 {
5714 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5715
5716 gdb_assert (per_objfile->per_bfd->using_index);
5717 printf_filtered (".debug_names:");
5718 if (per_objfile->per_bfd->debug_names_table)
5719 printf_filtered (" exists\n");
5720 else
5721 printf_filtered (" faked for \"readnow\"\n");
5722 printf_filtered ("\n");
5723 }
5724
5725 static void
5726 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5727 const char *func_name)
5728 {
5729 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5730
5731 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5732 if (per_objfile->per_bfd->debug_names_table)
5733 {
5734 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5735
5736 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5737 per_objfile);
5738
5739 struct dwarf2_per_cu_data *per_cu;
5740 while ((per_cu = iter.next ()) != NULL)
5741 dw2_instantiate_symtab (per_cu, per_objfile, false);
5742 }
5743 }
5744
5745 static void
5746 dw2_debug_names_map_matching_symbols
5747 (struct objfile *objfile,
5748 const lookup_name_info &name, domain_enum domain,
5749 int global,
5750 gdb::function_view<symbol_found_callback_ftype> callback,
5751 symbol_compare_ftype *ordered_compare)
5752 {
5753 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5754
5755 /* debug_names_table is NULL if OBJF_READNOW. */
5756 if (!per_objfile->per_bfd->debug_names_table)
5757 return;
5758
5759 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5760 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5761
5762 const char *match_name = name.ada ().lookup_name ().c_str ();
5763 auto matcher = [&] (const char *symname)
5764 {
5765 if (ordered_compare == nullptr)
5766 return true;
5767 return ordered_compare (symname, match_name) == 0;
5768 };
5769
5770 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5771 [&] (offset_type namei)
5772 {
5773 /* The name was matched, now expand corresponding CUs that were
5774 marked. */
5775 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5776 per_objfile);
5777
5778 struct dwarf2_per_cu_data *per_cu;
5779 while ((per_cu = iter.next ()) != NULL)
5780 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5781 nullptr);
5782 return true;
5783 }, per_objfile);
5784
5785 /* It's a shame we couldn't do this inside the
5786 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5787 that have already been expanded. Instead, this loop matches what
5788 the psymtab code does. */
5789 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5790 {
5791 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5792 if (symtab != nullptr)
5793 {
5794 const struct block *block
5795 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5796 if (!iterate_over_symbols_terminated (block, name,
5797 domain, callback))
5798 break;
5799 }
5800 }
5801 }
5802
5803 static void
5804 dw2_debug_names_expand_symtabs_matching
5805 (struct objfile *objfile,
5806 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5807 const lookup_name_info *lookup_name,
5808 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5809 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5810 enum search_domain kind)
5811 {
5812 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5813
5814 /* debug_names_table is NULL if OBJF_READNOW. */
5815 if (!per_objfile->per_bfd->debug_names_table)
5816 return;
5817
5818 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5819
5820 if (symbol_matcher == NULL && lookup_name == NULL)
5821 {
5822 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5823 {
5824 QUIT;
5825
5826 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5827 expansion_notify);
5828 }
5829 return;
5830 }
5831
5832 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5833
5834 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5835 symbol_matcher,
5836 kind, [&] (offset_type namei)
5837 {
5838 /* The name was matched, now expand corresponding CUs that were
5839 marked. */
5840 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5841
5842 struct dwarf2_per_cu_data *per_cu;
5843 while ((per_cu = iter.next ()) != NULL)
5844 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5845 expansion_notify);
5846 return true;
5847 }, per_objfile);
5848 }
5849
5850 const struct quick_symbol_functions dwarf2_debug_names_functions =
5851 {
5852 dw2_has_symbols,
5853 dw2_find_last_source_symtab,
5854 dw2_forget_cached_source_info,
5855 dw2_map_symtabs_matching_filename,
5856 dw2_debug_names_lookup_symbol,
5857 NULL,
5858 dw2_print_stats,
5859 dw2_debug_names_dump,
5860 dw2_debug_names_expand_symtabs_for_function,
5861 dw2_expand_all_symtabs,
5862 dw2_expand_symtabs_with_fullname,
5863 dw2_debug_names_map_matching_symbols,
5864 dw2_debug_names_expand_symtabs_matching,
5865 dw2_find_pc_sect_compunit_symtab,
5866 NULL,
5867 dw2_map_symbol_filenames
5868 };
5869
5870 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5871 to either a dwarf2_per_bfd or dwz_file object. */
5872
5873 template <typename T>
5874 static gdb::array_view<const gdb_byte>
5875 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5876 {
5877 dwarf2_section_info *section = &section_owner->gdb_index;
5878
5879 if (section->empty ())
5880 return {};
5881
5882 /* Older elfutils strip versions could keep the section in the main
5883 executable while splitting it for the separate debug info file. */
5884 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5885 return {};
5886
5887 section->read (obj);
5888
5889 /* dwarf2_section_info::size is a bfd_size_type, while
5890 gdb::array_view works with size_t. On 32-bit hosts, with
5891 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5892 is 32-bit. So we need an explicit narrowing conversion here.
5893 This is fine, because it's impossible to allocate or mmap an
5894 array/buffer larger than what size_t can represent. */
5895 return gdb::make_array_view (section->buffer, section->size);
5896 }
5897
5898 /* Lookup the index cache for the contents of the index associated to
5899 DWARF2_OBJ. */
5900
5901 static gdb::array_view<const gdb_byte>
5902 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5903 {
5904 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5905 if (build_id == nullptr)
5906 return {};
5907
5908 return global_index_cache.lookup_gdb_index (build_id,
5909 &dwarf2_per_bfd->index_cache_res);
5910 }
5911
5912 /* Same as the above, but for DWZ. */
5913
5914 static gdb::array_view<const gdb_byte>
5915 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5916 {
5917 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5918 if (build_id == nullptr)
5919 return {};
5920
5921 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5922 }
5923
5924 /* See symfile.h. */
5925
5926 bool
5927 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5928 {
5929 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5930 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5931
5932 /* If we're about to read full symbols, don't bother with the
5933 indices. In this case we also don't care if some other debug
5934 format is making psymtabs, because they are all about to be
5935 expanded anyway. */
5936 if ((objfile->flags & OBJF_READNOW))
5937 {
5938 /* When using READNOW, the using_index flag (set below) indicates that
5939 PER_BFD was already initialized, when we loaded some other objfile. */
5940 if (per_bfd->using_index)
5941 {
5942 *index_kind = dw_index_kind::GDB_INDEX;
5943 per_objfile->resize_symtabs ();
5944 return true;
5945 }
5946
5947 per_bfd->using_index = 1;
5948 create_all_comp_units (per_objfile);
5949 create_all_type_units (per_objfile);
5950 per_bfd->quick_file_names_table
5951 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5952 per_objfile->resize_symtabs ();
5953
5954 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5955 + per_bfd->all_type_units.size ()); ++i)
5956 {
5957 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
5958
5959 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5960 struct dwarf2_per_cu_quick_data);
5961 }
5962
5963 /* Return 1 so that gdb sees the "quick" functions. However,
5964 these functions will be no-ops because we will have expanded
5965 all symtabs. */
5966 *index_kind = dw_index_kind::GDB_INDEX;
5967 return true;
5968 }
5969
5970 /* Was a debug names index already read when we processed an objfile sharing
5971 PER_BFD? */
5972 if (per_bfd->debug_names_table != nullptr)
5973 {
5974 *index_kind = dw_index_kind::DEBUG_NAMES;
5975 per_objfile->resize_symtabs ();
5976 return true;
5977 }
5978
5979 /* Was a GDB index already read when we processed an objfile sharing
5980 PER_BFD? */
5981 if (per_bfd->index_table != nullptr)
5982 {
5983 *index_kind = dw_index_kind::GDB_INDEX;
5984 per_objfile->resize_symtabs ();
5985 return true;
5986 }
5987
5988 if (dwarf2_read_debug_names (per_objfile))
5989 {
5990 *index_kind = dw_index_kind::DEBUG_NAMES;
5991 per_objfile->resize_symtabs ();
5992 return true;
5993 }
5994
5995 if (dwarf2_read_gdb_index (per_objfile,
5996 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5997 get_gdb_index_contents_from_section<dwz_file>))
5998 {
5999 *index_kind = dw_index_kind::GDB_INDEX;
6000 per_objfile->resize_symtabs ();
6001 return true;
6002 }
6003
6004 /* ... otherwise, try to find the index in the index cache. */
6005 if (dwarf2_read_gdb_index (per_objfile,
6006 get_gdb_index_contents_from_cache,
6007 get_gdb_index_contents_from_cache_dwz))
6008 {
6009 global_index_cache.hit ();
6010 *index_kind = dw_index_kind::GDB_INDEX;
6011 per_objfile->resize_symtabs ();
6012 return true;
6013 }
6014
6015 global_index_cache.miss ();
6016 return false;
6017 }
6018
6019 \f
6020
6021 /* Build a partial symbol table. */
6022
6023 void
6024 dwarf2_build_psymtabs (struct objfile *objfile)
6025 {
6026 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6027 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6028
6029 if (per_bfd->partial_symtabs != nullptr)
6030 {
6031 /* Partial symbols were already read, so now we can simply
6032 attach them. */
6033 objfile->partial_symtabs = per_bfd->partial_symtabs;
6034 per_objfile->resize_symtabs ();
6035 return;
6036 }
6037
6038 init_psymbol_list (objfile, 1024);
6039
6040 try
6041 {
6042 /* This isn't really ideal: all the data we allocate on the
6043 objfile's obstack is still uselessly kept around. However,
6044 freeing it seems unsafe. */
6045 psymtab_discarder psymtabs (objfile);
6046 dwarf2_build_psymtabs_hard (per_objfile);
6047 psymtabs.keep ();
6048
6049 per_objfile->resize_symtabs ();
6050
6051 /* (maybe) store an index in the cache. */
6052 global_index_cache.store (per_objfile);
6053 }
6054 catch (const gdb_exception_error &except)
6055 {
6056 exception_print (gdb_stderr, except);
6057 }
6058
6059 /* Finish by setting the local reference to partial symtabs, so that
6060 we don't try to read them again if reading another objfile with the same
6061 BFD. If we can't in fact share, this won't make a difference anyway as
6062 the dwarf2_per_bfd object won't be shared. */
6063 per_bfd->partial_symtabs = objfile->partial_symtabs;
6064 }
6065
6066 /* Find the base address of the compilation unit for range lists and
6067 location lists. It will normally be specified by DW_AT_low_pc.
6068 In DWARF-3 draft 4, the base address could be overridden by
6069 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6070 compilation units with discontinuous ranges. */
6071
6072 static void
6073 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6074 {
6075 struct attribute *attr;
6076
6077 cu->base_address.reset ();
6078
6079 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6080 if (attr != nullptr)
6081 cu->base_address = attr->value_as_address ();
6082 else
6083 {
6084 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6085 if (attr != nullptr)
6086 cu->base_address = attr->value_as_address ();
6087 }
6088 }
6089
6090 /* Helper function that returns the proper abbrev section for
6091 THIS_CU. */
6092
6093 static struct dwarf2_section_info *
6094 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6095 {
6096 struct dwarf2_section_info *abbrev;
6097 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6098
6099 if (this_cu->is_dwz)
6100 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6101 else
6102 abbrev = &per_bfd->abbrev;
6103
6104 return abbrev;
6105 }
6106
6107 /* Fetch the abbreviation table offset from a comp or type unit header. */
6108
6109 static sect_offset
6110 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6111 struct dwarf2_section_info *section,
6112 sect_offset sect_off)
6113 {
6114 bfd *abfd = section->get_bfd_owner ();
6115 const gdb_byte *info_ptr;
6116 unsigned int initial_length_size, offset_size;
6117 uint16_t version;
6118
6119 section->read (per_objfile->objfile);
6120 info_ptr = section->buffer + to_underlying (sect_off);
6121 read_initial_length (abfd, info_ptr, &initial_length_size);
6122 offset_size = initial_length_size == 4 ? 4 : 8;
6123 info_ptr += initial_length_size;
6124
6125 version = read_2_bytes (abfd, info_ptr);
6126 info_ptr += 2;
6127 if (version >= 5)
6128 {
6129 /* Skip unit type and address size. */
6130 info_ptr += 2;
6131 }
6132
6133 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6134 }
6135
6136 /* A partial symtab that is used only for include files. */
6137 struct dwarf2_include_psymtab : public partial_symtab
6138 {
6139 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6140 : partial_symtab (filename, objfile)
6141 {
6142 }
6143
6144 void read_symtab (struct objfile *objfile) override
6145 {
6146 /* It's an include file, no symbols to read for it.
6147 Everything is in the includer symtab. */
6148
6149 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6150 expansion of the includer psymtab. We use the dependencies[0] field to
6151 model the includer. But if we go the regular route of calling
6152 expand_psymtab here, and having expand_psymtab call expand_dependencies
6153 to expand the includer, we'll only use expand_psymtab on the includer
6154 (making it a non-toplevel psymtab), while if we expand the includer via
6155 another path, we'll use read_symtab (making it a toplevel psymtab).
6156 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6157 psymtab, and trigger read_symtab on the includer here directly. */
6158 includer ()->read_symtab (objfile);
6159 }
6160
6161 void expand_psymtab (struct objfile *objfile) override
6162 {
6163 /* This is not called by read_symtab, and should not be called by any
6164 expand_dependencies. */
6165 gdb_assert (false);
6166 }
6167
6168 bool readin_p (struct objfile *objfile) const override
6169 {
6170 return includer ()->readin_p (objfile);
6171 }
6172
6173 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6174 {
6175 return nullptr;
6176 }
6177
6178 private:
6179 partial_symtab *includer () const
6180 {
6181 /* An include psymtab has exactly one dependency: the psymtab that
6182 includes it. */
6183 gdb_assert (this->number_of_dependencies == 1);
6184 return this->dependencies[0];
6185 }
6186 };
6187
6188 /* Allocate a new partial symtab for file named NAME and mark this new
6189 partial symtab as being an include of PST. */
6190
6191 static void
6192 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6193 struct objfile *objfile)
6194 {
6195 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6196
6197 if (!IS_ABSOLUTE_PATH (subpst->filename))
6198 subpst->dirname = pst->dirname;
6199
6200 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6201 subpst->dependencies[0] = pst;
6202 subpst->number_of_dependencies = 1;
6203 }
6204
6205 /* Read the Line Number Program data and extract the list of files
6206 included by the source file represented by PST. Build an include
6207 partial symtab for each of these included files. */
6208
6209 static void
6210 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6211 struct die_info *die,
6212 dwarf2_psymtab *pst)
6213 {
6214 line_header_up lh;
6215 struct attribute *attr;
6216
6217 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6218 if (attr != nullptr)
6219 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6220 if (lh == NULL)
6221 return; /* No linetable, so no includes. */
6222
6223 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6224 that we pass in the raw text_low here; that is ok because we're
6225 only decoding the line table to make include partial symtabs, and
6226 so the addresses aren't really used. */
6227 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6228 pst->raw_text_low (), 1);
6229 }
6230
6231 static hashval_t
6232 hash_signatured_type (const void *item)
6233 {
6234 const struct signatured_type *sig_type
6235 = (const struct signatured_type *) item;
6236
6237 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6238 return sig_type->signature;
6239 }
6240
6241 static int
6242 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6243 {
6244 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6245 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6246
6247 return lhs->signature == rhs->signature;
6248 }
6249
6250 /* Allocate a hash table for signatured types. */
6251
6252 static htab_up
6253 allocate_signatured_type_table ()
6254 {
6255 return htab_up (htab_create_alloc (41,
6256 hash_signatured_type,
6257 eq_signatured_type,
6258 NULL, xcalloc, xfree));
6259 }
6260
6261 /* A helper function to add a signatured type CU to a table. */
6262
6263 static int
6264 add_signatured_type_cu_to_table (void **slot, void *datum)
6265 {
6266 struct signatured_type *sigt = (struct signatured_type *) *slot;
6267 std::vector<signatured_type *> *all_type_units
6268 = (std::vector<signatured_type *> *) datum;
6269
6270 all_type_units->push_back (sigt);
6271
6272 return 1;
6273 }
6274
6275 /* A helper for create_debug_types_hash_table. Read types from SECTION
6276 and fill them into TYPES_HTAB. It will process only type units,
6277 therefore DW_UT_type. */
6278
6279 static void
6280 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6281 struct dwo_file *dwo_file,
6282 dwarf2_section_info *section, htab_up &types_htab,
6283 rcuh_kind section_kind)
6284 {
6285 struct objfile *objfile = per_objfile->objfile;
6286 struct dwarf2_section_info *abbrev_section;
6287 bfd *abfd;
6288 const gdb_byte *info_ptr, *end_ptr;
6289
6290 abbrev_section = (dwo_file != NULL
6291 ? &dwo_file->sections.abbrev
6292 : &per_objfile->per_bfd->abbrev);
6293
6294 if (dwarf_read_debug)
6295 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6296 section->get_name (),
6297 abbrev_section->get_file_name ());
6298
6299 section->read (objfile);
6300 info_ptr = section->buffer;
6301
6302 if (info_ptr == NULL)
6303 return;
6304
6305 /* We can't set abfd until now because the section may be empty or
6306 not present, in which case the bfd is unknown. */
6307 abfd = section->get_bfd_owner ();
6308
6309 /* We don't use cutu_reader here because we don't need to read
6310 any dies: the signature is in the header. */
6311
6312 end_ptr = info_ptr + section->size;
6313 while (info_ptr < end_ptr)
6314 {
6315 struct signatured_type *sig_type;
6316 struct dwo_unit *dwo_tu;
6317 void **slot;
6318 const gdb_byte *ptr = info_ptr;
6319 struct comp_unit_head header;
6320 unsigned int length;
6321
6322 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6323
6324 /* Initialize it due to a false compiler warning. */
6325 header.signature = -1;
6326 header.type_cu_offset_in_tu = (cu_offset) -1;
6327
6328 /* We need to read the type's signature in order to build the hash
6329 table, but we don't need anything else just yet. */
6330
6331 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6332 abbrev_section, ptr, section_kind);
6333
6334 length = header.get_length ();
6335
6336 /* Skip dummy type units. */
6337 if (ptr >= info_ptr + length
6338 || peek_abbrev_code (abfd, ptr) == 0
6339 || header.unit_type != DW_UT_type)
6340 {
6341 info_ptr += length;
6342 continue;
6343 }
6344
6345 if (types_htab == NULL)
6346 {
6347 if (dwo_file)
6348 types_htab = allocate_dwo_unit_table ();
6349 else
6350 types_htab = allocate_signatured_type_table ();
6351 }
6352
6353 if (dwo_file)
6354 {
6355 sig_type = NULL;
6356 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6357 dwo_tu->dwo_file = dwo_file;
6358 dwo_tu->signature = header.signature;
6359 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6360 dwo_tu->section = section;
6361 dwo_tu->sect_off = sect_off;
6362 dwo_tu->length = length;
6363 }
6364 else
6365 {
6366 /* N.B.: type_offset is not usable if this type uses a DWO file.
6367 The real type_offset is in the DWO file. */
6368 dwo_tu = NULL;
6369 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6370 sig_type->signature = header.signature;
6371 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6372 sig_type->per_cu.is_debug_types = 1;
6373 sig_type->per_cu.section = section;
6374 sig_type->per_cu.sect_off = sect_off;
6375 sig_type->per_cu.length = length;
6376 }
6377
6378 slot = htab_find_slot (types_htab.get (),
6379 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6380 INSERT);
6381 gdb_assert (slot != NULL);
6382 if (*slot != NULL)
6383 {
6384 sect_offset dup_sect_off;
6385
6386 if (dwo_file)
6387 {
6388 const struct dwo_unit *dup_tu
6389 = (const struct dwo_unit *) *slot;
6390
6391 dup_sect_off = dup_tu->sect_off;
6392 }
6393 else
6394 {
6395 const struct signatured_type *dup_tu
6396 = (const struct signatured_type *) *slot;
6397
6398 dup_sect_off = dup_tu->per_cu.sect_off;
6399 }
6400
6401 complaint (_("debug type entry at offset %s is duplicate to"
6402 " the entry at offset %s, signature %s"),
6403 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6404 hex_string (header.signature));
6405 }
6406 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6407
6408 if (dwarf_read_debug > 1)
6409 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6410 sect_offset_str (sect_off),
6411 hex_string (header.signature));
6412
6413 info_ptr += length;
6414 }
6415 }
6416
6417 /* Create the hash table of all entries in the .debug_types
6418 (or .debug_types.dwo) section(s).
6419 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6420 otherwise it is NULL.
6421
6422 The result is a pointer to the hash table or NULL if there are no types.
6423
6424 Note: This function processes DWO files only, not DWP files. */
6425
6426 static void
6427 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6428 struct dwo_file *dwo_file,
6429 gdb::array_view<dwarf2_section_info> type_sections,
6430 htab_up &types_htab)
6431 {
6432 for (dwarf2_section_info &section : type_sections)
6433 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6434 rcuh_kind::TYPE);
6435 }
6436
6437 /* Create the hash table of all entries in the .debug_types section,
6438 and initialize all_type_units.
6439 The result is zero if there is an error (e.g. missing .debug_types section),
6440 otherwise non-zero. */
6441
6442 static int
6443 create_all_type_units (dwarf2_per_objfile *per_objfile)
6444 {
6445 htab_up types_htab;
6446
6447 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6448 types_htab, rcuh_kind::COMPILE);
6449 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6450 types_htab);
6451 if (types_htab == NULL)
6452 {
6453 per_objfile->per_bfd->signatured_types = NULL;
6454 return 0;
6455 }
6456
6457 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6458
6459 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6460 per_objfile->per_bfd->all_type_units.reserve
6461 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6462
6463 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6464 add_signatured_type_cu_to_table,
6465 &per_objfile->per_bfd->all_type_units);
6466
6467 return 1;
6468 }
6469
6470 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6471 If SLOT is non-NULL, it is the entry to use in the hash table.
6472 Otherwise we find one. */
6473
6474 static struct signatured_type *
6475 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6476 {
6477 if (per_objfile->per_bfd->all_type_units.size ()
6478 == per_objfile->per_bfd->all_type_units.capacity ())
6479 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6480
6481 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6482
6483 per_objfile->resize_symtabs ();
6484
6485 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6486 sig_type->signature = sig;
6487 sig_type->per_cu.is_debug_types = 1;
6488 if (per_objfile->per_bfd->using_index)
6489 {
6490 sig_type->per_cu.v.quick =
6491 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6492 struct dwarf2_per_cu_quick_data);
6493 }
6494
6495 if (slot == NULL)
6496 {
6497 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6498 sig_type, INSERT);
6499 }
6500 gdb_assert (*slot == NULL);
6501 *slot = sig_type;
6502 /* The rest of sig_type must be filled in by the caller. */
6503 return sig_type;
6504 }
6505
6506 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6507 Fill in SIG_ENTRY with DWO_ENTRY. */
6508
6509 static void
6510 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6511 struct signatured_type *sig_entry,
6512 struct dwo_unit *dwo_entry)
6513 {
6514 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6515
6516 /* Make sure we're not clobbering something we don't expect to. */
6517 gdb_assert (! sig_entry->per_cu.queued);
6518 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6519 if (per_bfd->using_index)
6520 {
6521 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6522 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6523 }
6524 else
6525 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6526 gdb_assert (sig_entry->signature == dwo_entry->signature);
6527 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6528 gdb_assert (sig_entry->type_unit_group == NULL);
6529 gdb_assert (sig_entry->dwo_unit == NULL);
6530
6531 sig_entry->per_cu.section = dwo_entry->section;
6532 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6533 sig_entry->per_cu.length = dwo_entry->length;
6534 sig_entry->per_cu.reading_dwo_directly = 1;
6535 sig_entry->per_cu.per_bfd = per_bfd;
6536 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6537 sig_entry->dwo_unit = dwo_entry;
6538 }
6539
6540 /* Subroutine of lookup_signatured_type.
6541 If we haven't read the TU yet, create the signatured_type data structure
6542 for a TU to be read in directly from a DWO file, bypassing the stub.
6543 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6544 using .gdb_index, then when reading a CU we want to stay in the DWO file
6545 containing that CU. Otherwise we could end up reading several other DWO
6546 files (due to comdat folding) to process the transitive closure of all the
6547 mentioned TUs, and that can be slow. The current DWO file will have every
6548 type signature that it needs.
6549 We only do this for .gdb_index because in the psymtab case we already have
6550 to read all the DWOs to build the type unit groups. */
6551
6552 static struct signatured_type *
6553 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6554 {
6555 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6556 struct dwo_file *dwo_file;
6557 struct dwo_unit find_dwo_entry, *dwo_entry;
6558 struct signatured_type find_sig_entry, *sig_entry;
6559 void **slot;
6560
6561 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6562
6563 /* If TU skeletons have been removed then we may not have read in any
6564 TUs yet. */
6565 if (per_objfile->per_bfd->signatured_types == NULL)
6566 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6567
6568 /* We only ever need to read in one copy of a signatured type.
6569 Use the global signatured_types array to do our own comdat-folding
6570 of types. If this is the first time we're reading this TU, and
6571 the TU has an entry in .gdb_index, replace the recorded data from
6572 .gdb_index with this TU. */
6573
6574 find_sig_entry.signature = sig;
6575 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6576 &find_sig_entry, INSERT);
6577 sig_entry = (struct signatured_type *) *slot;
6578
6579 /* We can get here with the TU already read, *or* in the process of being
6580 read. Don't reassign the global entry to point to this DWO if that's
6581 the case. Also note that if the TU is already being read, it may not
6582 have come from a DWO, the program may be a mix of Fission-compiled
6583 code and non-Fission-compiled code. */
6584
6585 /* Have we already tried to read this TU?
6586 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6587 needn't exist in the global table yet). */
6588 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6589 return sig_entry;
6590
6591 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6592 dwo_unit of the TU itself. */
6593 dwo_file = cu->dwo_unit->dwo_file;
6594
6595 /* Ok, this is the first time we're reading this TU. */
6596 if (dwo_file->tus == NULL)
6597 return NULL;
6598 find_dwo_entry.signature = sig;
6599 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6600 &find_dwo_entry);
6601 if (dwo_entry == NULL)
6602 return NULL;
6603
6604 /* If the global table doesn't have an entry for this TU, add one. */
6605 if (sig_entry == NULL)
6606 sig_entry = add_type_unit (per_objfile, sig, slot);
6607
6608 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6609 sig_entry->per_cu.tu_read = 1;
6610 return sig_entry;
6611 }
6612
6613 /* Subroutine of lookup_signatured_type.
6614 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6615 then try the DWP file. If the TU stub (skeleton) has been removed then
6616 it won't be in .gdb_index. */
6617
6618 static struct signatured_type *
6619 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6620 {
6621 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6622 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6623 struct dwo_unit *dwo_entry;
6624 struct signatured_type find_sig_entry, *sig_entry;
6625 void **slot;
6626
6627 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6628 gdb_assert (dwp_file != NULL);
6629
6630 /* If TU skeletons have been removed then we may not have read in any
6631 TUs yet. */
6632 if (per_objfile->per_bfd->signatured_types == NULL)
6633 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6634
6635 find_sig_entry.signature = sig;
6636 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6637 &find_sig_entry, INSERT);
6638 sig_entry = (struct signatured_type *) *slot;
6639
6640 /* Have we already tried to read this TU?
6641 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6642 needn't exist in the global table yet). */
6643 if (sig_entry != NULL)
6644 return sig_entry;
6645
6646 if (dwp_file->tus == NULL)
6647 return NULL;
6648 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6649 1 /* is_debug_types */);
6650 if (dwo_entry == NULL)
6651 return NULL;
6652
6653 sig_entry = add_type_unit (per_objfile, sig, slot);
6654 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6655
6656 return sig_entry;
6657 }
6658
6659 /* Lookup a signature based type for DW_FORM_ref_sig8.
6660 Returns NULL if signature SIG is not present in the table.
6661 It is up to the caller to complain about this. */
6662
6663 static struct signatured_type *
6664 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6665 {
6666 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6667
6668 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6669 {
6670 /* We're in a DWO/DWP file, and we're using .gdb_index.
6671 These cases require special processing. */
6672 if (get_dwp_file (per_objfile) == NULL)
6673 return lookup_dwo_signatured_type (cu, sig);
6674 else
6675 return lookup_dwp_signatured_type (cu, sig);
6676 }
6677 else
6678 {
6679 struct signatured_type find_entry, *entry;
6680
6681 if (per_objfile->per_bfd->signatured_types == NULL)
6682 return NULL;
6683 find_entry.signature = sig;
6684 entry = ((struct signatured_type *)
6685 htab_find (per_objfile->per_bfd->signatured_types.get (),
6686 &find_entry));
6687 return entry;
6688 }
6689 }
6690
6691 /* Low level DIE reading support. */
6692
6693 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6694
6695 static void
6696 init_cu_die_reader (struct die_reader_specs *reader,
6697 struct dwarf2_cu *cu,
6698 struct dwarf2_section_info *section,
6699 struct dwo_file *dwo_file,
6700 struct abbrev_table *abbrev_table)
6701 {
6702 gdb_assert (section->readin && section->buffer != NULL);
6703 reader->abfd = section->get_bfd_owner ();
6704 reader->cu = cu;
6705 reader->dwo_file = dwo_file;
6706 reader->die_section = section;
6707 reader->buffer = section->buffer;
6708 reader->buffer_end = section->buffer + section->size;
6709 reader->abbrev_table = abbrev_table;
6710 }
6711
6712 /* Subroutine of cutu_reader to simplify it.
6713 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6714 There's just a lot of work to do, and cutu_reader is big enough
6715 already.
6716
6717 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6718 from it to the DIE in the DWO. If NULL we are skipping the stub.
6719 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6720 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6721 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6722 STUB_COMP_DIR may be non-NULL.
6723 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6724 are filled in with the info of the DIE from the DWO file.
6725 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6726 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6727 kept around for at least as long as *RESULT_READER.
6728
6729 The result is non-zero if a valid (non-dummy) DIE was found. */
6730
6731 static int
6732 read_cutu_die_from_dwo (dwarf2_cu *cu,
6733 struct dwo_unit *dwo_unit,
6734 struct die_info *stub_comp_unit_die,
6735 const char *stub_comp_dir,
6736 struct die_reader_specs *result_reader,
6737 const gdb_byte **result_info_ptr,
6738 struct die_info **result_comp_unit_die,
6739 abbrev_table_up *result_dwo_abbrev_table)
6740 {
6741 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6742 dwarf2_per_cu_data *per_cu = cu->per_cu;
6743 struct objfile *objfile = per_objfile->objfile;
6744 bfd *abfd;
6745 const gdb_byte *begin_info_ptr, *info_ptr;
6746 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6747 int i,num_extra_attrs;
6748 struct dwarf2_section_info *dwo_abbrev_section;
6749 struct die_info *comp_unit_die;
6750
6751 /* At most one of these may be provided. */
6752 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6753
6754 /* These attributes aren't processed until later:
6755 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6756 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6757 referenced later. However, these attributes are found in the stub
6758 which we won't have later. In order to not impose this complication
6759 on the rest of the code, we read them here and copy them to the
6760 DWO CU/TU die. */
6761
6762 stmt_list = NULL;
6763 low_pc = NULL;
6764 high_pc = NULL;
6765 ranges = NULL;
6766 comp_dir = NULL;
6767
6768 if (stub_comp_unit_die != NULL)
6769 {
6770 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6771 DWO file. */
6772 if (!per_cu->is_debug_types)
6773 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6774 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6775 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6776 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6777 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6778
6779 cu->addr_base = stub_comp_unit_die->addr_base ();
6780
6781 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6782 here (if needed). We need the value before we can process
6783 DW_AT_ranges. */
6784 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6785 }
6786 else if (stub_comp_dir != NULL)
6787 {
6788 /* Reconstruct the comp_dir attribute to simplify the code below. */
6789 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6790 comp_dir->name = DW_AT_comp_dir;
6791 comp_dir->form = DW_FORM_string;
6792 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6793 DW_STRING (comp_dir) = stub_comp_dir;
6794 }
6795
6796 /* Set up for reading the DWO CU/TU. */
6797 cu->dwo_unit = dwo_unit;
6798 dwarf2_section_info *section = dwo_unit->section;
6799 section->read (objfile);
6800 abfd = section->get_bfd_owner ();
6801 begin_info_ptr = info_ptr = (section->buffer
6802 + to_underlying (dwo_unit->sect_off));
6803 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6804
6805 if (per_cu->is_debug_types)
6806 {
6807 signatured_type *sig_type = (struct signatured_type *) per_cu;
6808
6809 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6810 section, dwo_abbrev_section,
6811 info_ptr, rcuh_kind::TYPE);
6812 /* This is not an assert because it can be caused by bad debug info. */
6813 if (sig_type->signature != cu->header.signature)
6814 {
6815 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6816 " TU at offset %s [in module %s]"),
6817 hex_string (sig_type->signature),
6818 hex_string (cu->header.signature),
6819 sect_offset_str (dwo_unit->sect_off),
6820 bfd_get_filename (abfd));
6821 }
6822 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6823 /* For DWOs coming from DWP files, we don't know the CU length
6824 nor the type's offset in the TU until now. */
6825 dwo_unit->length = cu->header.get_length ();
6826 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6827
6828 /* Establish the type offset that can be used to lookup the type.
6829 For DWO files, we don't know it until now. */
6830 sig_type->type_offset_in_section
6831 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6832 }
6833 else
6834 {
6835 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6836 section, dwo_abbrev_section,
6837 info_ptr, rcuh_kind::COMPILE);
6838 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6839 /* For DWOs coming from DWP files, we don't know the CU length
6840 until now. */
6841 dwo_unit->length = cu->header.get_length ();
6842 }
6843
6844 *result_dwo_abbrev_table
6845 = abbrev_table::read (objfile, dwo_abbrev_section,
6846 cu->header.abbrev_sect_off);
6847 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6848 result_dwo_abbrev_table->get ());
6849
6850 /* Read in the die, but leave space to copy over the attributes
6851 from the stub. This has the benefit of simplifying the rest of
6852 the code - all the work to maintain the illusion of a single
6853 DW_TAG_{compile,type}_unit DIE is done here. */
6854 num_extra_attrs = ((stmt_list != NULL)
6855 + (low_pc != NULL)
6856 + (high_pc != NULL)
6857 + (ranges != NULL)
6858 + (comp_dir != NULL));
6859 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6860 num_extra_attrs);
6861
6862 /* Copy over the attributes from the stub to the DIE we just read in. */
6863 comp_unit_die = *result_comp_unit_die;
6864 i = comp_unit_die->num_attrs;
6865 if (stmt_list != NULL)
6866 comp_unit_die->attrs[i++] = *stmt_list;
6867 if (low_pc != NULL)
6868 comp_unit_die->attrs[i++] = *low_pc;
6869 if (high_pc != NULL)
6870 comp_unit_die->attrs[i++] = *high_pc;
6871 if (ranges != NULL)
6872 comp_unit_die->attrs[i++] = *ranges;
6873 if (comp_dir != NULL)
6874 comp_unit_die->attrs[i++] = *comp_dir;
6875 comp_unit_die->num_attrs += num_extra_attrs;
6876
6877 if (dwarf_die_debug)
6878 {
6879 fprintf_unfiltered (gdb_stdlog,
6880 "Read die from %s@0x%x of %s:\n",
6881 section->get_name (),
6882 (unsigned) (begin_info_ptr - section->buffer),
6883 bfd_get_filename (abfd));
6884 dump_die (comp_unit_die, dwarf_die_debug);
6885 }
6886
6887 /* Skip dummy compilation units. */
6888 if (info_ptr >= begin_info_ptr + dwo_unit->length
6889 || peek_abbrev_code (abfd, info_ptr) == 0)
6890 return 0;
6891
6892 *result_info_ptr = info_ptr;
6893 return 1;
6894 }
6895
6896 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6897 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6898 signature is part of the header. */
6899 static gdb::optional<ULONGEST>
6900 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6901 {
6902 if (cu->header.version >= 5)
6903 return cu->header.signature;
6904 struct attribute *attr;
6905 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6906 if (attr == nullptr)
6907 return gdb::optional<ULONGEST> ();
6908 return DW_UNSND (attr);
6909 }
6910
6911 /* Subroutine of cutu_reader to simplify it.
6912 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6913 Returns NULL if the specified DWO unit cannot be found. */
6914
6915 static struct dwo_unit *
6916 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6917 {
6918 dwarf2_per_cu_data *per_cu = cu->per_cu;
6919 struct dwo_unit *dwo_unit;
6920 const char *comp_dir;
6921
6922 gdb_assert (cu != NULL);
6923
6924 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6925 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6926 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6927
6928 if (per_cu->is_debug_types)
6929 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6930 else
6931 {
6932 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6933
6934 if (!signature.has_value ())
6935 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6936 " [in module %s]"),
6937 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6938
6939 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6940 }
6941
6942 return dwo_unit;
6943 }
6944
6945 /* Subroutine of cutu_reader to simplify it.
6946 See it for a description of the parameters.
6947 Read a TU directly from a DWO file, bypassing the stub. */
6948
6949 void
6950 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6951 dwarf2_per_objfile *per_objfile,
6952 dwarf2_cu *existing_cu)
6953 {
6954 struct signatured_type *sig_type;
6955
6956 /* Verify we can do the following downcast, and that we have the
6957 data we need. */
6958 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6959 sig_type = (struct signatured_type *) this_cu;
6960 gdb_assert (sig_type->dwo_unit != NULL);
6961
6962 dwarf2_cu *cu;
6963
6964 if (existing_cu != nullptr)
6965 {
6966 cu = existing_cu;
6967 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6968 /* There's no need to do the rereading_dwo_cu handling that
6969 cutu_reader does since we don't read the stub. */
6970 }
6971 else
6972 {
6973 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6974 in per_objfile yet. */
6975 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6976 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6977 cu = m_new_cu.get ();
6978 }
6979
6980 /* A future optimization, if needed, would be to use an existing
6981 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6982 could share abbrev tables. */
6983
6984 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6985 NULL /* stub_comp_unit_die */,
6986 sig_type->dwo_unit->dwo_file->comp_dir,
6987 this, &info_ptr,
6988 &comp_unit_die,
6989 &m_dwo_abbrev_table) == 0)
6990 {
6991 /* Dummy die. */
6992 dummy_p = true;
6993 }
6994 }
6995
6996 /* Initialize a CU (or TU) and read its DIEs.
6997 If the CU defers to a DWO file, read the DWO file as well.
6998
6999 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7000 Otherwise the table specified in the comp unit header is read in and used.
7001 This is an optimization for when we already have the abbrev table.
7002
7003 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7004 allocated. */
7005
7006 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7007 dwarf2_per_objfile *per_objfile,
7008 struct abbrev_table *abbrev_table,
7009 dwarf2_cu *existing_cu,
7010 bool skip_partial)
7011 : die_reader_specs {},
7012 m_this_cu (this_cu)
7013 {
7014 struct objfile *objfile = per_objfile->objfile;
7015 struct dwarf2_section_info *section = this_cu->section;
7016 bfd *abfd = section->get_bfd_owner ();
7017 const gdb_byte *begin_info_ptr;
7018 struct signatured_type *sig_type = NULL;
7019 struct dwarf2_section_info *abbrev_section;
7020 /* Non-zero if CU currently points to a DWO file and we need to
7021 reread it. When this happens we need to reread the skeleton die
7022 before we can reread the DWO file (this only applies to CUs, not TUs). */
7023 int rereading_dwo_cu = 0;
7024
7025 if (dwarf_die_debug)
7026 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7027 this_cu->is_debug_types ? "type" : "comp",
7028 sect_offset_str (this_cu->sect_off));
7029
7030 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7031 file (instead of going through the stub), short-circuit all of this. */
7032 if (this_cu->reading_dwo_directly)
7033 {
7034 /* Narrow down the scope of possibilities to have to understand. */
7035 gdb_assert (this_cu->is_debug_types);
7036 gdb_assert (abbrev_table == NULL);
7037 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7038 return;
7039 }
7040
7041 /* This is cheap if the section is already read in. */
7042 section->read (objfile);
7043
7044 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7045
7046 abbrev_section = get_abbrev_section_for_cu (this_cu);
7047
7048 dwarf2_cu *cu;
7049
7050 if (existing_cu != nullptr)
7051 {
7052 cu = existing_cu;
7053 /* If this CU is from a DWO file we need to start over, we need to
7054 refetch the attributes from the skeleton CU.
7055 This could be optimized by retrieving those attributes from when we
7056 were here the first time: the previous comp_unit_die was stored in
7057 comp_unit_obstack. But there's no data yet that we need this
7058 optimization. */
7059 if (cu->dwo_unit != NULL)
7060 rereading_dwo_cu = 1;
7061 }
7062 else
7063 {
7064 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7065 in per_objfile yet. */
7066 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7067 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7068 cu = m_new_cu.get ();
7069 }
7070
7071 /* Get the header. */
7072 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7073 {
7074 /* We already have the header, there's no need to read it in again. */
7075 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7076 }
7077 else
7078 {
7079 if (this_cu->is_debug_types)
7080 {
7081 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7082 section, abbrev_section,
7083 info_ptr, rcuh_kind::TYPE);
7084
7085 /* Since per_cu is the first member of struct signatured_type,
7086 we can go from a pointer to one to a pointer to the other. */
7087 sig_type = (struct signatured_type *) this_cu;
7088 gdb_assert (sig_type->signature == cu->header.signature);
7089 gdb_assert (sig_type->type_offset_in_tu
7090 == cu->header.type_cu_offset_in_tu);
7091 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7092
7093 /* LENGTH has not been set yet for type units if we're
7094 using .gdb_index. */
7095 this_cu->length = cu->header.get_length ();
7096
7097 /* Establish the type offset that can be used to lookup the type. */
7098 sig_type->type_offset_in_section =
7099 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7100
7101 this_cu->dwarf_version = cu->header.version;
7102 }
7103 else
7104 {
7105 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7106 section, abbrev_section,
7107 info_ptr,
7108 rcuh_kind::COMPILE);
7109
7110 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7111 if (this_cu->length == 0)
7112 this_cu->length = cu->header.get_length ();
7113 else
7114 gdb_assert (this_cu->length == cu->header.get_length ());
7115 this_cu->dwarf_version = cu->header.version;
7116 }
7117 }
7118
7119 /* Skip dummy compilation units. */
7120 if (info_ptr >= begin_info_ptr + this_cu->length
7121 || peek_abbrev_code (abfd, info_ptr) == 0)
7122 {
7123 dummy_p = true;
7124 return;
7125 }
7126
7127 /* If we don't have them yet, read the abbrevs for this compilation unit.
7128 And if we need to read them now, make sure they're freed when we're
7129 done. */
7130 if (abbrev_table != NULL)
7131 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7132 else
7133 {
7134 m_abbrev_table_holder
7135 = abbrev_table::read (objfile, abbrev_section,
7136 cu->header.abbrev_sect_off);
7137 abbrev_table = m_abbrev_table_holder.get ();
7138 }
7139
7140 /* Read the top level CU/TU die. */
7141 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7142 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7143
7144 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7145 {
7146 dummy_p = true;
7147 return;
7148 }
7149
7150 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7151 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7152 table from the DWO file and pass the ownership over to us. It will be
7153 referenced from READER, so we must make sure to free it after we're done
7154 with READER.
7155
7156 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7157 DWO CU, that this test will fail (the attribute will not be present). */
7158 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7159 if (dwo_name != nullptr)
7160 {
7161 struct dwo_unit *dwo_unit;
7162 struct die_info *dwo_comp_unit_die;
7163
7164 if (comp_unit_die->has_children)
7165 {
7166 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7167 " has children (offset %s) [in module %s]"),
7168 sect_offset_str (this_cu->sect_off),
7169 bfd_get_filename (abfd));
7170 }
7171 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7172 if (dwo_unit != NULL)
7173 {
7174 if (read_cutu_die_from_dwo (cu, dwo_unit,
7175 comp_unit_die, NULL,
7176 this, &info_ptr,
7177 &dwo_comp_unit_die,
7178 &m_dwo_abbrev_table) == 0)
7179 {
7180 /* Dummy die. */
7181 dummy_p = true;
7182 return;
7183 }
7184 comp_unit_die = dwo_comp_unit_die;
7185 }
7186 else
7187 {
7188 /* Yikes, we couldn't find the rest of the DIE, we only have
7189 the stub. A complaint has already been logged. There's
7190 not much more we can do except pass on the stub DIE to
7191 die_reader_func. We don't want to throw an error on bad
7192 debug info. */
7193 }
7194 }
7195 }
7196
7197 void
7198 cutu_reader::keep ()
7199 {
7200 /* Done, clean up. */
7201 gdb_assert (!dummy_p);
7202 if (m_new_cu != NULL)
7203 {
7204 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7205 now. */
7206 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7207 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7208 }
7209 }
7210
7211 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7212 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7213 assumed to have already done the lookup to find the DWO file).
7214
7215 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7216 THIS_CU->is_debug_types, but nothing else.
7217
7218 We fill in THIS_CU->length.
7219
7220 THIS_CU->cu is always freed when done.
7221 This is done in order to not leave THIS_CU->cu in a state where we have
7222 to care whether it refers to the "main" CU or the DWO CU.
7223
7224 When parent_cu is passed, it is used to provide a default value for
7225 str_offsets_base and addr_base from the parent. */
7226
7227 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7228 dwarf2_per_objfile *per_objfile,
7229 struct dwarf2_cu *parent_cu,
7230 struct dwo_file *dwo_file)
7231 : die_reader_specs {},
7232 m_this_cu (this_cu)
7233 {
7234 struct objfile *objfile = per_objfile->objfile;
7235 struct dwarf2_section_info *section = this_cu->section;
7236 bfd *abfd = section->get_bfd_owner ();
7237 struct dwarf2_section_info *abbrev_section;
7238 const gdb_byte *begin_info_ptr, *info_ptr;
7239
7240 if (dwarf_die_debug)
7241 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7242 this_cu->is_debug_types ? "type" : "comp",
7243 sect_offset_str (this_cu->sect_off));
7244
7245 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7246
7247 abbrev_section = (dwo_file != NULL
7248 ? &dwo_file->sections.abbrev
7249 : get_abbrev_section_for_cu (this_cu));
7250
7251 /* This is cheap if the section is already read in. */
7252 section->read (objfile);
7253
7254 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7255
7256 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7257 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7258 section, abbrev_section, info_ptr,
7259 (this_cu->is_debug_types
7260 ? rcuh_kind::TYPE
7261 : rcuh_kind::COMPILE));
7262
7263 if (parent_cu != nullptr)
7264 {
7265 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7266 m_new_cu->addr_base = parent_cu->addr_base;
7267 }
7268 this_cu->length = m_new_cu->header.get_length ();
7269
7270 /* Skip dummy compilation units. */
7271 if (info_ptr >= begin_info_ptr + this_cu->length
7272 || peek_abbrev_code (abfd, info_ptr) == 0)
7273 {
7274 dummy_p = true;
7275 return;
7276 }
7277
7278 m_abbrev_table_holder
7279 = abbrev_table::read (objfile, abbrev_section,
7280 m_new_cu->header.abbrev_sect_off);
7281
7282 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7283 m_abbrev_table_holder.get ());
7284 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7285 }
7286
7287 \f
7288 /* Type Unit Groups.
7289
7290 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7291 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7292 so that all types coming from the same compilation (.o file) are grouped
7293 together. A future step could be to put the types in the same symtab as
7294 the CU the types ultimately came from. */
7295
7296 static hashval_t
7297 hash_type_unit_group (const void *item)
7298 {
7299 const struct type_unit_group *tu_group
7300 = (const struct type_unit_group *) item;
7301
7302 return hash_stmt_list_entry (&tu_group->hash);
7303 }
7304
7305 static int
7306 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7307 {
7308 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7309 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7310
7311 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7312 }
7313
7314 /* Allocate a hash table for type unit groups. */
7315
7316 static htab_up
7317 allocate_type_unit_groups_table ()
7318 {
7319 return htab_up (htab_create_alloc (3,
7320 hash_type_unit_group,
7321 eq_type_unit_group,
7322 NULL, xcalloc, xfree));
7323 }
7324
7325 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7326 partial symtabs. We combine several TUs per psymtab to not let the size
7327 of any one psymtab grow too big. */
7328 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7329 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7330
7331 /* Helper routine for get_type_unit_group.
7332 Create the type_unit_group object used to hold one or more TUs. */
7333
7334 static struct type_unit_group *
7335 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7336 {
7337 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7338 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7339 struct dwarf2_per_cu_data *per_cu;
7340 struct type_unit_group *tu_group;
7341
7342 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7343 per_cu = &tu_group->per_cu;
7344 per_cu->per_bfd = per_bfd;
7345
7346 if (per_bfd->using_index)
7347 {
7348 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7349 struct dwarf2_per_cu_quick_data);
7350 }
7351 else
7352 {
7353 unsigned int line_offset = to_underlying (line_offset_struct);
7354 dwarf2_psymtab *pst;
7355 std::string name;
7356
7357 /* Give the symtab a useful name for debug purposes. */
7358 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7359 name = string_printf ("<type_units_%d>",
7360 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7361 else
7362 name = string_printf ("<type_units_at_0x%x>", line_offset);
7363
7364 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7365 pst->anonymous = true;
7366 }
7367
7368 tu_group->hash.dwo_unit = cu->dwo_unit;
7369 tu_group->hash.line_sect_off = line_offset_struct;
7370
7371 return tu_group;
7372 }
7373
7374 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7375 STMT_LIST is a DW_AT_stmt_list attribute. */
7376
7377 static struct type_unit_group *
7378 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7379 {
7380 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7381 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7382 struct type_unit_group *tu_group;
7383 void **slot;
7384 unsigned int line_offset;
7385 struct type_unit_group type_unit_group_for_lookup;
7386
7387 if (per_objfile->per_bfd->type_unit_groups == NULL)
7388 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7389
7390 /* Do we need to create a new group, or can we use an existing one? */
7391
7392 if (stmt_list)
7393 {
7394 line_offset = DW_UNSND (stmt_list);
7395 ++tu_stats->nr_symtab_sharers;
7396 }
7397 else
7398 {
7399 /* Ugh, no stmt_list. Rare, but we have to handle it.
7400 We can do various things here like create one group per TU or
7401 spread them over multiple groups to split up the expansion work.
7402 To avoid worst case scenarios (too many groups or too large groups)
7403 we, umm, group them in bunches. */
7404 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7405 | (tu_stats->nr_stmt_less_type_units
7406 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7407 ++tu_stats->nr_stmt_less_type_units;
7408 }
7409
7410 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7411 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7412 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7413 &type_unit_group_for_lookup, INSERT);
7414 if (*slot != NULL)
7415 {
7416 tu_group = (struct type_unit_group *) *slot;
7417 gdb_assert (tu_group != NULL);
7418 }
7419 else
7420 {
7421 sect_offset line_offset_struct = (sect_offset) line_offset;
7422 tu_group = create_type_unit_group (cu, line_offset_struct);
7423 *slot = tu_group;
7424 ++tu_stats->nr_symtabs;
7425 }
7426
7427 return tu_group;
7428 }
7429 \f
7430 /* Partial symbol tables. */
7431
7432 /* Create a psymtab named NAME and assign it to PER_CU.
7433
7434 The caller must fill in the following details:
7435 dirname, textlow, texthigh. */
7436
7437 static dwarf2_psymtab *
7438 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7439 dwarf2_per_objfile *per_objfile,
7440 const char *name)
7441 {
7442 struct objfile *objfile = per_objfile->objfile;
7443 dwarf2_psymtab *pst;
7444
7445 pst = new dwarf2_psymtab (name, objfile, per_cu);
7446
7447 pst->psymtabs_addrmap_supported = true;
7448
7449 /* This is the glue that links PST into GDB's symbol API. */
7450 per_cu->v.psymtab = pst;
7451
7452 return pst;
7453 }
7454
7455 /* DIE reader function for process_psymtab_comp_unit. */
7456
7457 static void
7458 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7459 const gdb_byte *info_ptr,
7460 struct die_info *comp_unit_die,
7461 enum language pretend_language)
7462 {
7463 struct dwarf2_cu *cu = reader->cu;
7464 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7465 struct objfile *objfile = per_objfile->objfile;
7466 struct gdbarch *gdbarch = objfile->arch ();
7467 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7468 CORE_ADDR baseaddr;
7469 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7470 dwarf2_psymtab *pst;
7471 enum pc_bounds_kind cu_bounds_kind;
7472 const char *filename;
7473
7474 gdb_assert (! per_cu->is_debug_types);
7475
7476 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7477
7478 /* Allocate a new partial symbol table structure. */
7479 gdb::unique_xmalloc_ptr<char> debug_filename;
7480 static const char artificial[] = "<artificial>";
7481 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7482 if (filename == NULL)
7483 filename = "";
7484 else if (strcmp (filename, artificial) == 0)
7485 {
7486 debug_filename.reset (concat (artificial, "@",
7487 sect_offset_str (per_cu->sect_off),
7488 (char *) NULL));
7489 filename = debug_filename.get ();
7490 }
7491
7492 pst = create_partial_symtab (per_cu, per_objfile, filename);
7493
7494 /* This must be done before calling dwarf2_build_include_psymtabs. */
7495 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7496
7497 baseaddr = objfile->text_section_offset ();
7498
7499 dwarf2_find_base_address (comp_unit_die, cu);
7500
7501 /* Possibly set the default values of LOWPC and HIGHPC from
7502 `DW_AT_ranges'. */
7503 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7504 &best_highpc, cu, pst);
7505 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7506 {
7507 CORE_ADDR low
7508 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7509 - baseaddr);
7510 CORE_ADDR high
7511 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7512 - baseaddr - 1);
7513 /* Store the contiguous range if it is not empty; it can be
7514 empty for CUs with no code. */
7515 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7516 low, high, pst);
7517 }
7518
7519 /* Check if comp unit has_children.
7520 If so, read the rest of the partial symbols from this comp unit.
7521 If not, there's no more debug_info for this comp unit. */
7522 if (comp_unit_die->has_children)
7523 {
7524 struct partial_die_info *first_die;
7525 CORE_ADDR lowpc, highpc;
7526
7527 lowpc = ((CORE_ADDR) -1);
7528 highpc = ((CORE_ADDR) 0);
7529
7530 first_die = load_partial_dies (reader, info_ptr, 1);
7531
7532 scan_partial_symbols (first_die, &lowpc, &highpc,
7533 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7534
7535 /* If we didn't find a lowpc, set it to highpc to avoid
7536 complaints from `maint check'. */
7537 if (lowpc == ((CORE_ADDR) -1))
7538 lowpc = highpc;
7539
7540 /* If the compilation unit didn't have an explicit address range,
7541 then use the information extracted from its child dies. */
7542 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7543 {
7544 best_lowpc = lowpc;
7545 best_highpc = highpc;
7546 }
7547 }
7548 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7549 best_lowpc + baseaddr)
7550 - baseaddr);
7551 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7552 best_highpc + baseaddr)
7553 - baseaddr);
7554
7555 end_psymtab_common (objfile, pst);
7556
7557 if (!cu->per_cu->imported_symtabs_empty ())
7558 {
7559 int i;
7560 int len = cu->per_cu->imported_symtabs_size ();
7561
7562 /* Fill in 'dependencies' here; we fill in 'users' in a
7563 post-pass. */
7564 pst->number_of_dependencies = len;
7565 pst->dependencies
7566 = objfile->partial_symtabs->allocate_dependencies (len);
7567 for (i = 0; i < len; ++i)
7568 {
7569 pst->dependencies[i]
7570 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7571 }
7572
7573 cu->per_cu->imported_symtabs_free ();
7574 }
7575
7576 /* Get the list of files included in the current compilation unit,
7577 and build a psymtab for each of them. */
7578 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7579
7580 if (dwarf_read_debug)
7581 fprintf_unfiltered (gdb_stdlog,
7582 "Psymtab for %s unit @%s: %s - %s"
7583 ", %d global, %d static syms\n",
7584 per_cu->is_debug_types ? "type" : "comp",
7585 sect_offset_str (per_cu->sect_off),
7586 paddress (gdbarch, pst->text_low (objfile)),
7587 paddress (gdbarch, pst->text_high (objfile)),
7588 pst->n_global_syms, pst->n_static_syms);
7589 }
7590
7591 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7592 Process compilation unit THIS_CU for a psymtab. */
7593
7594 static void
7595 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7596 dwarf2_per_objfile *per_objfile,
7597 bool want_partial_unit,
7598 enum language pretend_language)
7599 {
7600 /* If this compilation unit was already read in, free the
7601 cached copy in order to read it in again. This is
7602 necessary because we skipped some symbols when we first
7603 read in the compilation unit (see load_partial_dies).
7604 This problem could be avoided, but the benefit is unclear. */
7605 per_objfile->remove_cu (this_cu);
7606
7607 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7608
7609 switch (reader.comp_unit_die->tag)
7610 {
7611 case DW_TAG_compile_unit:
7612 this_cu->unit_type = DW_UT_compile;
7613 break;
7614 case DW_TAG_partial_unit:
7615 this_cu->unit_type = DW_UT_partial;
7616 break;
7617 default:
7618 abort ();
7619 }
7620
7621 if (reader.dummy_p)
7622 {
7623 /* Nothing. */
7624 }
7625 else if (this_cu->is_debug_types)
7626 build_type_psymtabs_reader (&reader, reader.info_ptr,
7627 reader.comp_unit_die);
7628 else if (want_partial_unit
7629 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7630 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7631 reader.comp_unit_die,
7632 pretend_language);
7633
7634 this_cu->lang = reader.cu->language;
7635
7636 /* Age out any secondary CUs. */
7637 per_objfile->age_comp_units ();
7638 }
7639
7640 /* Reader function for build_type_psymtabs. */
7641
7642 static void
7643 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7644 const gdb_byte *info_ptr,
7645 struct die_info *type_unit_die)
7646 {
7647 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7648 struct objfile *objfile = per_objfile->objfile;
7649 struct dwarf2_cu *cu = reader->cu;
7650 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7651 struct signatured_type *sig_type;
7652 struct type_unit_group *tu_group;
7653 struct attribute *attr;
7654 struct partial_die_info *first_die;
7655 CORE_ADDR lowpc, highpc;
7656 dwarf2_psymtab *pst;
7657
7658 gdb_assert (per_cu->is_debug_types);
7659 sig_type = (struct signatured_type *) per_cu;
7660
7661 if (! type_unit_die->has_children)
7662 return;
7663
7664 attr = type_unit_die->attr (DW_AT_stmt_list);
7665 tu_group = get_type_unit_group (cu, attr);
7666
7667 if (tu_group->tus == nullptr)
7668 tu_group->tus = new std::vector<signatured_type *>;
7669 tu_group->tus->push_back (sig_type);
7670
7671 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7672 pst = create_partial_symtab (per_cu, per_objfile, "");
7673 pst->anonymous = true;
7674
7675 first_die = load_partial_dies (reader, info_ptr, 1);
7676
7677 lowpc = (CORE_ADDR) -1;
7678 highpc = (CORE_ADDR) 0;
7679 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7680
7681 end_psymtab_common (objfile, pst);
7682 }
7683
7684 /* Struct used to sort TUs by their abbreviation table offset. */
7685
7686 struct tu_abbrev_offset
7687 {
7688 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7689 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7690 {}
7691
7692 signatured_type *sig_type;
7693 sect_offset abbrev_offset;
7694 };
7695
7696 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7697
7698 static bool
7699 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7700 const struct tu_abbrev_offset &b)
7701 {
7702 return a.abbrev_offset < b.abbrev_offset;
7703 }
7704
7705 /* Efficiently read all the type units.
7706 This does the bulk of the work for build_type_psymtabs.
7707
7708 The efficiency is because we sort TUs by the abbrev table they use and
7709 only read each abbrev table once. In one program there are 200K TUs
7710 sharing 8K abbrev tables.
7711
7712 The main purpose of this function is to support building the
7713 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7714 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7715 can collapse the search space by grouping them by stmt_list.
7716 The savings can be significant, in the same program from above the 200K TUs
7717 share 8K stmt_list tables.
7718
7719 FUNC is expected to call get_type_unit_group, which will create the
7720 struct type_unit_group if necessary and add it to
7721 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7722
7723 static void
7724 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7725 {
7726 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7727 abbrev_table_up abbrev_table;
7728 sect_offset abbrev_offset;
7729
7730 /* It's up to the caller to not call us multiple times. */
7731 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7732
7733 if (per_objfile->per_bfd->all_type_units.empty ())
7734 return;
7735
7736 /* TUs typically share abbrev tables, and there can be way more TUs than
7737 abbrev tables. Sort by abbrev table to reduce the number of times we
7738 read each abbrev table in.
7739 Alternatives are to punt or to maintain a cache of abbrev tables.
7740 This is simpler and efficient enough for now.
7741
7742 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7743 symtab to use). Typically TUs with the same abbrev offset have the same
7744 stmt_list value too so in practice this should work well.
7745
7746 The basic algorithm here is:
7747
7748 sort TUs by abbrev table
7749 for each TU with same abbrev table:
7750 read abbrev table if first user
7751 read TU top level DIE
7752 [IWBN if DWO skeletons had DW_AT_stmt_list]
7753 call FUNC */
7754
7755 if (dwarf_read_debug)
7756 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7757
7758 /* Sort in a separate table to maintain the order of all_type_units
7759 for .gdb_index: TU indices directly index all_type_units. */
7760 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7761 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7762
7763 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7764 sorted_by_abbrev.emplace_back
7765 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7766 sig_type->per_cu.sect_off));
7767
7768 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7769 sort_tu_by_abbrev_offset);
7770
7771 abbrev_offset = (sect_offset) ~(unsigned) 0;
7772
7773 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7774 {
7775 /* Switch to the next abbrev table if necessary. */
7776 if (abbrev_table == NULL
7777 || tu.abbrev_offset != abbrev_offset)
7778 {
7779 abbrev_offset = tu.abbrev_offset;
7780 abbrev_table =
7781 abbrev_table::read (per_objfile->objfile,
7782 &per_objfile->per_bfd->abbrev, abbrev_offset);
7783 ++tu_stats->nr_uniq_abbrev_tables;
7784 }
7785
7786 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7787 abbrev_table.get (), nullptr, false);
7788 if (!reader.dummy_p)
7789 build_type_psymtabs_reader (&reader, reader.info_ptr,
7790 reader.comp_unit_die);
7791 }
7792 }
7793
7794 /* Print collected type unit statistics. */
7795
7796 static void
7797 print_tu_stats (dwarf2_per_objfile *per_objfile)
7798 {
7799 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7800
7801 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7802 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7803 per_objfile->per_bfd->all_type_units.size ());
7804 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7805 tu_stats->nr_uniq_abbrev_tables);
7806 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7807 tu_stats->nr_symtabs);
7808 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7809 tu_stats->nr_symtab_sharers);
7810 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7811 tu_stats->nr_stmt_less_type_units);
7812 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7813 tu_stats->nr_all_type_units_reallocs);
7814 }
7815
7816 /* Traversal function for build_type_psymtabs. */
7817
7818 static int
7819 build_type_psymtab_dependencies (void **slot, void *info)
7820 {
7821 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7822 struct objfile *objfile = per_objfile->objfile;
7823 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7824 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7825 dwarf2_psymtab *pst = per_cu->v.psymtab;
7826 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7827 int i;
7828
7829 gdb_assert (len > 0);
7830 gdb_assert (per_cu->type_unit_group_p ());
7831
7832 pst->number_of_dependencies = len;
7833 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7834 for (i = 0; i < len; ++i)
7835 {
7836 struct signatured_type *iter = tu_group->tus->at (i);
7837 gdb_assert (iter->per_cu.is_debug_types);
7838 pst->dependencies[i] = iter->per_cu.v.psymtab;
7839 iter->type_unit_group = tu_group;
7840 }
7841
7842 delete tu_group->tus;
7843 tu_group->tus = nullptr;
7844
7845 return 1;
7846 }
7847
7848 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7849 Build partial symbol tables for the .debug_types comp-units. */
7850
7851 static void
7852 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7853 {
7854 if (! create_all_type_units (per_objfile))
7855 return;
7856
7857 build_type_psymtabs_1 (per_objfile);
7858 }
7859
7860 /* Traversal function for process_skeletonless_type_unit.
7861 Read a TU in a DWO file and build partial symbols for it. */
7862
7863 static int
7864 process_skeletonless_type_unit (void **slot, void *info)
7865 {
7866 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7867 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7868 struct signatured_type find_entry, *entry;
7869
7870 /* If this TU doesn't exist in the global table, add it and read it in. */
7871
7872 if (per_objfile->per_bfd->signatured_types == NULL)
7873 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7874
7875 find_entry.signature = dwo_unit->signature;
7876 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7877 &find_entry, INSERT);
7878 /* If we've already seen this type there's nothing to do. What's happening
7879 is we're doing our own version of comdat-folding here. */
7880 if (*slot != NULL)
7881 return 1;
7882
7883 /* This does the job that create_all_type_units would have done for
7884 this TU. */
7885 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7886 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7887 *slot = entry;
7888
7889 /* This does the job that build_type_psymtabs_1 would have done. */
7890 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7891 if (!reader.dummy_p)
7892 build_type_psymtabs_reader (&reader, reader.info_ptr,
7893 reader.comp_unit_die);
7894
7895 return 1;
7896 }
7897
7898 /* Traversal function for process_skeletonless_type_units. */
7899
7900 static int
7901 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7902 {
7903 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7904
7905 if (dwo_file->tus != NULL)
7906 htab_traverse_noresize (dwo_file->tus.get (),
7907 process_skeletonless_type_unit, info);
7908
7909 return 1;
7910 }
7911
7912 /* Scan all TUs of DWO files, verifying we've processed them.
7913 This is needed in case a TU was emitted without its skeleton.
7914 Note: This can't be done until we know what all the DWO files are. */
7915
7916 static void
7917 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7918 {
7919 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7920 if (get_dwp_file (per_objfile) == NULL
7921 && per_objfile->per_bfd->dwo_files != NULL)
7922 {
7923 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7924 process_dwo_file_for_skeletonless_type_units,
7925 per_objfile);
7926 }
7927 }
7928
7929 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7930
7931 static void
7932 set_partial_user (dwarf2_per_objfile *per_objfile)
7933 {
7934 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
7935 {
7936 dwarf2_psymtab *pst = per_cu->v.psymtab;
7937
7938 if (pst == NULL)
7939 continue;
7940
7941 for (int j = 0; j < pst->number_of_dependencies; ++j)
7942 {
7943 /* Set the 'user' field only if it is not already set. */
7944 if (pst->dependencies[j]->user == NULL)
7945 pst->dependencies[j]->user = pst;
7946 }
7947 }
7948 }
7949
7950 /* Build the partial symbol table by doing a quick pass through the
7951 .debug_info and .debug_abbrev sections. */
7952
7953 static void
7954 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7955 {
7956 struct objfile *objfile = per_objfile->objfile;
7957
7958 if (dwarf_read_debug)
7959 {
7960 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7961 objfile_name (objfile));
7962 }
7963
7964 scoped_restore restore_reading_psyms
7965 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
7966 true);
7967
7968 per_objfile->per_bfd->info.read (objfile);
7969
7970 /* Any cached compilation units will be linked by the per-objfile
7971 read_in_chain. Make sure to free them when we're done. */
7972 free_cached_comp_units freer (per_objfile);
7973
7974 build_type_psymtabs (per_objfile);
7975
7976 create_all_comp_units (per_objfile);
7977
7978 /* Create a temporary address map on a temporary obstack. We later
7979 copy this to the final obstack. */
7980 auto_obstack temp_obstack;
7981
7982 scoped_restore save_psymtabs_addrmap
7983 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7984 addrmap_create_mutable (&temp_obstack));
7985
7986 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
7987 {
7988 if (per_cu->v.psymtab != NULL)
7989 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7990 continue;
7991 process_psymtab_comp_unit (per_cu, per_objfile, false,
7992 language_minimal);
7993 }
7994
7995 /* This has to wait until we read the CUs, we need the list of DWOs. */
7996 process_skeletonless_type_units (per_objfile);
7997
7998 /* Now that all TUs have been processed we can fill in the dependencies. */
7999 if (per_objfile->per_bfd->type_unit_groups != NULL)
8000 {
8001 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8002 build_type_psymtab_dependencies, per_objfile);
8003 }
8004
8005 if (dwarf_read_debug)
8006 print_tu_stats (per_objfile);
8007
8008 set_partial_user (per_objfile);
8009
8010 objfile->partial_symtabs->psymtabs_addrmap
8011 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8012 objfile->partial_symtabs->obstack ());
8013 /* At this point we want to keep the address map. */
8014 save_psymtabs_addrmap.release ();
8015
8016 if (dwarf_read_debug)
8017 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8018 objfile_name (objfile));
8019 }
8020
8021 /* Load the partial DIEs for a secondary CU into memory.
8022 This is also used when rereading a primary CU with load_all_dies. */
8023
8024 static void
8025 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8026 dwarf2_per_objfile *per_objfile,
8027 dwarf2_cu *existing_cu)
8028 {
8029 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8030
8031 if (!reader.dummy_p)
8032 {
8033 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8034 language_minimal);
8035
8036 /* Check if comp unit has_children.
8037 If so, read the rest of the partial symbols from this comp unit.
8038 If not, there's no more debug_info for this comp unit. */
8039 if (reader.comp_unit_die->has_children)
8040 load_partial_dies (&reader, reader.info_ptr, 0);
8041
8042 reader.keep ();
8043 }
8044 }
8045
8046 static void
8047 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8048 struct dwarf2_section_info *section,
8049 struct dwarf2_section_info *abbrev_section,
8050 unsigned int is_dwz)
8051 {
8052 const gdb_byte *info_ptr;
8053 struct objfile *objfile = per_objfile->objfile;
8054
8055 if (dwarf_read_debug)
8056 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8057 section->get_name (),
8058 section->get_file_name ());
8059
8060 section->read (objfile);
8061
8062 info_ptr = section->buffer;
8063
8064 while (info_ptr < section->buffer + section->size)
8065 {
8066 struct dwarf2_per_cu_data *this_cu;
8067
8068 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8069
8070 comp_unit_head cu_header;
8071 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8072 abbrev_section, info_ptr,
8073 rcuh_kind::COMPILE);
8074
8075 /* Save the compilation unit for later lookup. */
8076 if (cu_header.unit_type != DW_UT_type)
8077 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8078 else
8079 {
8080 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8081 sig_type->signature = cu_header.signature;
8082 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8083 this_cu = &sig_type->per_cu;
8084 }
8085 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8086 this_cu->sect_off = sect_off;
8087 this_cu->length = cu_header.length + cu_header.initial_length_size;
8088 this_cu->is_dwz = is_dwz;
8089 this_cu->section = section;
8090
8091 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8092
8093 info_ptr = info_ptr + this_cu->length;
8094 }
8095 }
8096
8097 /* Create a list of all compilation units in OBJFILE.
8098 This is only done for -readnow and building partial symtabs. */
8099
8100 static void
8101 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8102 {
8103 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8104 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8105 &per_objfile->per_bfd->abbrev, 0);
8106
8107 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8108 if (dwz != NULL)
8109 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8110 }
8111
8112 /* Process all loaded DIEs for compilation unit CU, starting at
8113 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8114 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8115 DW_AT_ranges). See the comments of add_partial_subprogram on how
8116 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8117
8118 static void
8119 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8120 CORE_ADDR *highpc, int set_addrmap,
8121 struct dwarf2_cu *cu)
8122 {
8123 struct partial_die_info *pdi;
8124
8125 /* Now, march along the PDI's, descending into ones which have
8126 interesting children but skipping the children of the other ones,
8127 until we reach the end of the compilation unit. */
8128
8129 pdi = first_die;
8130
8131 while (pdi != NULL)
8132 {
8133 pdi->fixup (cu);
8134
8135 /* Anonymous namespaces or modules have no name but have interesting
8136 children, so we need to look at them. Ditto for anonymous
8137 enums. */
8138
8139 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8140 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8141 || pdi->tag == DW_TAG_imported_unit
8142 || pdi->tag == DW_TAG_inlined_subroutine)
8143 {
8144 switch (pdi->tag)
8145 {
8146 case DW_TAG_subprogram:
8147 case DW_TAG_inlined_subroutine:
8148 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8149 if (cu->language == language_cplus)
8150 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8151 set_addrmap, cu);
8152 break;
8153 case DW_TAG_constant:
8154 case DW_TAG_variable:
8155 case DW_TAG_typedef:
8156 case DW_TAG_union_type:
8157 if (!pdi->is_declaration
8158 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8159 {
8160 add_partial_symbol (pdi, cu);
8161 }
8162 break;
8163 case DW_TAG_class_type:
8164 case DW_TAG_interface_type:
8165 case DW_TAG_structure_type:
8166 if (!pdi->is_declaration)
8167 {
8168 add_partial_symbol (pdi, cu);
8169 }
8170 if ((cu->language == language_rust
8171 || cu->language == language_cplus) && pdi->has_children)
8172 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8173 set_addrmap, cu);
8174 break;
8175 case DW_TAG_enumeration_type:
8176 if (!pdi->is_declaration)
8177 add_partial_enumeration (pdi, cu);
8178 break;
8179 case DW_TAG_base_type:
8180 case DW_TAG_subrange_type:
8181 /* File scope base type definitions are added to the partial
8182 symbol table. */
8183 add_partial_symbol (pdi, cu);
8184 break;
8185 case DW_TAG_namespace:
8186 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8187 break;
8188 case DW_TAG_module:
8189 if (!pdi->is_declaration)
8190 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8191 break;
8192 case DW_TAG_imported_unit:
8193 {
8194 struct dwarf2_per_cu_data *per_cu;
8195
8196 /* For now we don't handle imported units in type units. */
8197 if (cu->per_cu->is_debug_types)
8198 {
8199 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8200 " supported in type units [in module %s]"),
8201 objfile_name (cu->per_objfile->objfile));
8202 }
8203
8204 per_cu = dwarf2_find_containing_comp_unit
8205 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8206
8207 /* Go read the partial unit, if needed. */
8208 if (per_cu->v.psymtab == NULL)
8209 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8210 cu->language);
8211
8212 cu->per_cu->imported_symtabs_push (per_cu);
8213 }
8214 break;
8215 case DW_TAG_imported_declaration:
8216 add_partial_symbol (pdi, cu);
8217 break;
8218 default:
8219 break;
8220 }
8221 }
8222
8223 /* If the die has a sibling, skip to the sibling. */
8224
8225 pdi = pdi->die_sibling;
8226 }
8227 }
8228
8229 /* Functions used to compute the fully scoped name of a partial DIE.
8230
8231 Normally, this is simple. For C++, the parent DIE's fully scoped
8232 name is concatenated with "::" and the partial DIE's name.
8233 Enumerators are an exception; they use the scope of their parent
8234 enumeration type, i.e. the name of the enumeration type is not
8235 prepended to the enumerator.
8236
8237 There are two complexities. One is DW_AT_specification; in this
8238 case "parent" means the parent of the target of the specification,
8239 instead of the direct parent of the DIE. The other is compilers
8240 which do not emit DW_TAG_namespace; in this case we try to guess
8241 the fully qualified name of structure types from their members'
8242 linkage names. This must be done using the DIE's children rather
8243 than the children of any DW_AT_specification target. We only need
8244 to do this for structures at the top level, i.e. if the target of
8245 any DW_AT_specification (if any; otherwise the DIE itself) does not
8246 have a parent. */
8247
8248 /* Compute the scope prefix associated with PDI's parent, in
8249 compilation unit CU. The result will be allocated on CU's
8250 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8251 field. NULL is returned if no prefix is necessary. */
8252 static const char *
8253 partial_die_parent_scope (struct partial_die_info *pdi,
8254 struct dwarf2_cu *cu)
8255 {
8256 const char *grandparent_scope;
8257 struct partial_die_info *parent, *real_pdi;
8258
8259 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8260 then this means the parent of the specification DIE. */
8261
8262 real_pdi = pdi;
8263 while (real_pdi->has_specification)
8264 {
8265 auto res = find_partial_die (real_pdi->spec_offset,
8266 real_pdi->spec_is_dwz, cu);
8267 real_pdi = res.pdi;
8268 cu = res.cu;
8269 }
8270
8271 parent = real_pdi->die_parent;
8272 if (parent == NULL)
8273 return NULL;
8274
8275 if (parent->scope_set)
8276 return parent->scope;
8277
8278 parent->fixup (cu);
8279
8280 grandparent_scope = partial_die_parent_scope (parent, cu);
8281
8282 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8283 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8284 Work around this problem here. */
8285 if (cu->language == language_cplus
8286 && parent->tag == DW_TAG_namespace
8287 && strcmp (parent->name (cu), "::") == 0
8288 && grandparent_scope == NULL)
8289 {
8290 parent->scope = NULL;
8291 parent->scope_set = 1;
8292 return NULL;
8293 }
8294
8295 /* Nested subroutines in Fortran get a prefix. */
8296 if (pdi->tag == DW_TAG_enumerator)
8297 /* Enumerators should not get the name of the enumeration as a prefix. */
8298 parent->scope = grandparent_scope;
8299 else if (parent->tag == DW_TAG_namespace
8300 || parent->tag == DW_TAG_module
8301 || parent->tag == DW_TAG_structure_type
8302 || parent->tag == DW_TAG_class_type
8303 || parent->tag == DW_TAG_interface_type
8304 || parent->tag == DW_TAG_union_type
8305 || parent->tag == DW_TAG_enumeration_type
8306 || (cu->language == language_fortran
8307 && parent->tag == DW_TAG_subprogram
8308 && pdi->tag == DW_TAG_subprogram))
8309 {
8310 if (grandparent_scope == NULL)
8311 parent->scope = parent->name (cu);
8312 else
8313 parent->scope = typename_concat (&cu->comp_unit_obstack,
8314 grandparent_scope,
8315 parent->name (cu), 0, cu);
8316 }
8317 else
8318 {
8319 /* FIXME drow/2004-04-01: What should we be doing with
8320 function-local names? For partial symbols, we should probably be
8321 ignoring them. */
8322 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8323 dwarf_tag_name (parent->tag),
8324 sect_offset_str (pdi->sect_off));
8325 parent->scope = grandparent_scope;
8326 }
8327
8328 parent->scope_set = 1;
8329 return parent->scope;
8330 }
8331
8332 /* Return the fully scoped name associated with PDI, from compilation unit
8333 CU. The result will be allocated with malloc. */
8334
8335 static gdb::unique_xmalloc_ptr<char>
8336 partial_die_full_name (struct partial_die_info *pdi,
8337 struct dwarf2_cu *cu)
8338 {
8339 const char *parent_scope;
8340
8341 /* If this is a template instantiation, we can not work out the
8342 template arguments from partial DIEs. So, unfortunately, we have
8343 to go through the full DIEs. At least any work we do building
8344 types here will be reused if full symbols are loaded later. */
8345 if (pdi->has_template_arguments)
8346 {
8347 pdi->fixup (cu);
8348
8349 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8350 {
8351 struct die_info *die;
8352 struct attribute attr;
8353 struct dwarf2_cu *ref_cu = cu;
8354
8355 /* DW_FORM_ref_addr is using section offset. */
8356 attr.name = (enum dwarf_attribute) 0;
8357 attr.form = DW_FORM_ref_addr;
8358 attr.u.unsnd = to_underlying (pdi->sect_off);
8359 die = follow_die_ref (NULL, &attr, &ref_cu);
8360
8361 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8362 }
8363 }
8364
8365 parent_scope = partial_die_parent_scope (pdi, cu);
8366 if (parent_scope == NULL)
8367 return NULL;
8368 else
8369 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8370 pdi->name (cu),
8371 0, cu));
8372 }
8373
8374 static void
8375 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8376 {
8377 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8378 struct objfile *objfile = per_objfile->objfile;
8379 struct gdbarch *gdbarch = objfile->arch ();
8380 CORE_ADDR addr = 0;
8381 const char *actual_name = NULL;
8382 CORE_ADDR baseaddr;
8383
8384 baseaddr = objfile->text_section_offset ();
8385
8386 gdb::unique_xmalloc_ptr<char> built_actual_name
8387 = partial_die_full_name (pdi, cu);
8388 if (built_actual_name != NULL)
8389 actual_name = built_actual_name.get ();
8390
8391 if (actual_name == NULL)
8392 actual_name = pdi->name (cu);
8393
8394 partial_symbol psymbol;
8395 memset (&psymbol, 0, sizeof (psymbol));
8396 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8397 psymbol.ginfo.section = -1;
8398
8399 /* The code below indicates that the psymbol should be installed by
8400 setting this. */
8401 gdb::optional<psymbol_placement> where;
8402
8403 switch (pdi->tag)
8404 {
8405 case DW_TAG_inlined_subroutine:
8406 case DW_TAG_subprogram:
8407 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8408 - baseaddr);
8409 if (pdi->is_external
8410 || cu->language == language_ada
8411 || (cu->language == language_fortran
8412 && pdi->die_parent != NULL
8413 && pdi->die_parent->tag == DW_TAG_subprogram))
8414 {
8415 /* Normally, only "external" DIEs are part of the global scope.
8416 But in Ada and Fortran, we want to be able to access nested
8417 procedures globally. So all Ada and Fortran subprograms are
8418 stored in the global scope. */
8419 where = psymbol_placement::GLOBAL;
8420 }
8421 else
8422 where = psymbol_placement::STATIC;
8423
8424 psymbol.domain = VAR_DOMAIN;
8425 psymbol.aclass = LOC_BLOCK;
8426 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8427 psymbol.ginfo.value.address = addr;
8428
8429 if (pdi->main_subprogram && actual_name != NULL)
8430 set_objfile_main_name (objfile, actual_name, cu->language);
8431 break;
8432 case DW_TAG_constant:
8433 psymbol.domain = VAR_DOMAIN;
8434 psymbol.aclass = LOC_STATIC;
8435 where = (pdi->is_external
8436 ? psymbol_placement::GLOBAL
8437 : psymbol_placement::STATIC);
8438 break;
8439 case DW_TAG_variable:
8440 if (pdi->d.locdesc)
8441 addr = decode_locdesc (pdi->d.locdesc, cu);
8442
8443 if (pdi->d.locdesc
8444 && addr == 0
8445 && !per_objfile->per_bfd->has_section_at_zero)
8446 {
8447 /* A global or static variable may also have been stripped
8448 out by the linker if unused, in which case its address
8449 will be nullified; do not add such variables into partial
8450 symbol table then. */
8451 }
8452 else if (pdi->is_external)
8453 {
8454 /* Global Variable.
8455 Don't enter into the minimal symbol tables as there is
8456 a minimal symbol table entry from the ELF symbols already.
8457 Enter into partial symbol table if it has a location
8458 descriptor or a type.
8459 If the location descriptor is missing, new_symbol will create
8460 a LOC_UNRESOLVED symbol, the address of the variable will then
8461 be determined from the minimal symbol table whenever the variable
8462 is referenced.
8463 The address for the partial symbol table entry is not
8464 used by GDB, but it comes in handy for debugging partial symbol
8465 table building. */
8466
8467 if (pdi->d.locdesc || pdi->has_type)
8468 {
8469 psymbol.domain = VAR_DOMAIN;
8470 psymbol.aclass = LOC_STATIC;
8471 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8472 psymbol.ginfo.value.address = addr;
8473 where = psymbol_placement::GLOBAL;
8474 }
8475 }
8476 else
8477 {
8478 int has_loc = pdi->d.locdesc != NULL;
8479
8480 /* Static Variable. Skip symbols whose value we cannot know (those
8481 without location descriptors or constant values). */
8482 if (!has_loc && !pdi->has_const_value)
8483 return;
8484
8485 psymbol.domain = VAR_DOMAIN;
8486 psymbol.aclass = LOC_STATIC;
8487 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8488 if (has_loc)
8489 psymbol.ginfo.value.address = addr;
8490 where = psymbol_placement::STATIC;
8491 }
8492 break;
8493 case DW_TAG_typedef:
8494 case DW_TAG_base_type:
8495 case DW_TAG_subrange_type:
8496 psymbol.domain = VAR_DOMAIN;
8497 psymbol.aclass = LOC_TYPEDEF;
8498 where = psymbol_placement::STATIC;
8499 break;
8500 case DW_TAG_imported_declaration:
8501 case DW_TAG_namespace:
8502 psymbol.domain = VAR_DOMAIN;
8503 psymbol.aclass = LOC_TYPEDEF;
8504 where = psymbol_placement::GLOBAL;
8505 break;
8506 case DW_TAG_module:
8507 /* With Fortran 77 there might be a "BLOCK DATA" module
8508 available without any name. If so, we skip the module as it
8509 doesn't bring any value. */
8510 if (actual_name != nullptr)
8511 {
8512 psymbol.domain = MODULE_DOMAIN;
8513 psymbol.aclass = LOC_TYPEDEF;
8514 where = psymbol_placement::GLOBAL;
8515 }
8516 break;
8517 case DW_TAG_class_type:
8518 case DW_TAG_interface_type:
8519 case DW_TAG_structure_type:
8520 case DW_TAG_union_type:
8521 case DW_TAG_enumeration_type:
8522 /* Skip external references. The DWARF standard says in the section
8523 about "Structure, Union, and Class Type Entries": "An incomplete
8524 structure, union or class type is represented by a structure,
8525 union or class entry that does not have a byte size attribute
8526 and that has a DW_AT_declaration attribute." */
8527 if (!pdi->has_byte_size && pdi->is_declaration)
8528 return;
8529
8530 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8531 static vs. global. */
8532 psymbol.domain = STRUCT_DOMAIN;
8533 psymbol.aclass = LOC_TYPEDEF;
8534 where = (cu->language == language_cplus
8535 ? psymbol_placement::GLOBAL
8536 : psymbol_placement::STATIC);
8537 break;
8538 case DW_TAG_enumerator:
8539 psymbol.domain = VAR_DOMAIN;
8540 psymbol.aclass = LOC_CONST;
8541 where = (cu->language == language_cplus
8542 ? psymbol_placement::GLOBAL
8543 : psymbol_placement::STATIC);
8544 break;
8545 default:
8546 break;
8547 }
8548
8549 if (where.has_value ())
8550 {
8551 if (built_actual_name != nullptr)
8552 actual_name = objfile->intern (actual_name);
8553 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8554 psymbol.ginfo.set_linkage_name (actual_name);
8555 else
8556 {
8557 psymbol.ginfo.set_demangled_name (actual_name,
8558 &objfile->objfile_obstack);
8559 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8560 }
8561 add_psymbol_to_list (psymbol, *where, objfile);
8562 }
8563 }
8564
8565 /* Read a partial die corresponding to a namespace; also, add a symbol
8566 corresponding to that namespace to the symbol table. NAMESPACE is
8567 the name of the enclosing namespace. */
8568
8569 static void
8570 add_partial_namespace (struct partial_die_info *pdi,
8571 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8572 int set_addrmap, struct dwarf2_cu *cu)
8573 {
8574 /* Add a symbol for the namespace. */
8575
8576 add_partial_symbol (pdi, cu);
8577
8578 /* Now scan partial symbols in that namespace. */
8579
8580 if (pdi->has_children)
8581 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8582 }
8583
8584 /* Read a partial die corresponding to a Fortran module. */
8585
8586 static void
8587 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8588 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8589 {
8590 /* Add a symbol for the namespace. */
8591
8592 add_partial_symbol (pdi, cu);
8593
8594 /* Now scan partial symbols in that module. */
8595
8596 if (pdi->has_children)
8597 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8598 }
8599
8600 /* Read a partial die corresponding to a subprogram or an inlined
8601 subprogram and create a partial symbol for that subprogram.
8602 When the CU language allows it, this routine also defines a partial
8603 symbol for each nested subprogram that this subprogram contains.
8604 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8605 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8606
8607 PDI may also be a lexical block, in which case we simply search
8608 recursively for subprograms defined inside that lexical block.
8609 Again, this is only performed when the CU language allows this
8610 type of definitions. */
8611
8612 static void
8613 add_partial_subprogram (struct partial_die_info *pdi,
8614 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8615 int set_addrmap, struct dwarf2_cu *cu)
8616 {
8617 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8618 {
8619 if (pdi->has_pc_info)
8620 {
8621 if (pdi->lowpc < *lowpc)
8622 *lowpc = pdi->lowpc;
8623 if (pdi->highpc > *highpc)
8624 *highpc = pdi->highpc;
8625 if (set_addrmap)
8626 {
8627 struct objfile *objfile = cu->per_objfile->objfile;
8628 struct gdbarch *gdbarch = objfile->arch ();
8629 CORE_ADDR baseaddr;
8630 CORE_ADDR this_highpc;
8631 CORE_ADDR this_lowpc;
8632
8633 baseaddr = objfile->text_section_offset ();
8634 this_lowpc
8635 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8636 pdi->lowpc + baseaddr)
8637 - baseaddr);
8638 this_highpc
8639 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8640 pdi->highpc + baseaddr)
8641 - baseaddr);
8642 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8643 this_lowpc, this_highpc - 1,
8644 cu->per_cu->v.psymtab);
8645 }
8646 }
8647
8648 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8649 {
8650 if (!pdi->is_declaration)
8651 /* Ignore subprogram DIEs that do not have a name, they are
8652 illegal. Do not emit a complaint at this point, we will
8653 do so when we convert this psymtab into a symtab. */
8654 if (pdi->name (cu))
8655 add_partial_symbol (pdi, cu);
8656 }
8657 }
8658
8659 if (! pdi->has_children)
8660 return;
8661
8662 if (cu->language == language_ada || cu->language == language_fortran)
8663 {
8664 pdi = pdi->die_child;
8665 while (pdi != NULL)
8666 {
8667 pdi->fixup (cu);
8668 if (pdi->tag == DW_TAG_subprogram
8669 || pdi->tag == DW_TAG_inlined_subroutine
8670 || pdi->tag == DW_TAG_lexical_block)
8671 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8672 pdi = pdi->die_sibling;
8673 }
8674 }
8675 }
8676
8677 /* Read a partial die corresponding to an enumeration type. */
8678
8679 static void
8680 add_partial_enumeration (struct partial_die_info *enum_pdi,
8681 struct dwarf2_cu *cu)
8682 {
8683 struct partial_die_info *pdi;
8684
8685 if (enum_pdi->name (cu) != NULL)
8686 add_partial_symbol (enum_pdi, cu);
8687
8688 pdi = enum_pdi->die_child;
8689 while (pdi)
8690 {
8691 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8692 complaint (_("malformed enumerator DIE ignored"));
8693 else
8694 add_partial_symbol (pdi, cu);
8695 pdi = pdi->die_sibling;
8696 }
8697 }
8698
8699 /* Return the initial uleb128 in the die at INFO_PTR. */
8700
8701 static unsigned int
8702 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8703 {
8704 unsigned int bytes_read;
8705
8706 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8707 }
8708
8709 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8710 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8711
8712 Return the corresponding abbrev, or NULL if the number is zero (indicating
8713 an empty DIE). In either case *BYTES_READ will be set to the length of
8714 the initial number. */
8715
8716 static struct abbrev_info *
8717 peek_die_abbrev (const die_reader_specs &reader,
8718 const gdb_byte *info_ptr, unsigned int *bytes_read)
8719 {
8720 dwarf2_cu *cu = reader.cu;
8721 bfd *abfd = cu->per_objfile->objfile->obfd;
8722 unsigned int abbrev_number
8723 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8724
8725 if (abbrev_number == 0)
8726 return NULL;
8727
8728 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8729 if (!abbrev)
8730 {
8731 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8732 " at offset %s [in module %s]"),
8733 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8734 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8735 }
8736
8737 return abbrev;
8738 }
8739
8740 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8741 Returns a pointer to the end of a series of DIEs, terminated by an empty
8742 DIE. Any children of the skipped DIEs will also be skipped. */
8743
8744 static const gdb_byte *
8745 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8746 {
8747 while (1)
8748 {
8749 unsigned int bytes_read;
8750 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8751
8752 if (abbrev == NULL)
8753 return info_ptr + bytes_read;
8754 else
8755 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8756 }
8757 }
8758
8759 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8760 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8761 abbrev corresponding to that skipped uleb128 should be passed in
8762 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8763 children. */
8764
8765 static const gdb_byte *
8766 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8767 struct abbrev_info *abbrev)
8768 {
8769 unsigned int bytes_read;
8770 struct attribute attr;
8771 bfd *abfd = reader->abfd;
8772 struct dwarf2_cu *cu = reader->cu;
8773 const gdb_byte *buffer = reader->buffer;
8774 const gdb_byte *buffer_end = reader->buffer_end;
8775 unsigned int form, i;
8776
8777 for (i = 0; i < abbrev->num_attrs; i++)
8778 {
8779 /* The only abbrev we care about is DW_AT_sibling. */
8780 if (abbrev->attrs[i].name == DW_AT_sibling)
8781 {
8782 bool ignored;
8783 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8784 &ignored);
8785 if (attr.form == DW_FORM_ref_addr)
8786 complaint (_("ignoring absolute DW_AT_sibling"));
8787 else
8788 {
8789 sect_offset off = attr.get_ref_die_offset ();
8790 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8791
8792 if (sibling_ptr < info_ptr)
8793 complaint (_("DW_AT_sibling points backwards"));
8794 else if (sibling_ptr > reader->buffer_end)
8795 reader->die_section->overflow_complaint ();
8796 else
8797 return sibling_ptr;
8798 }
8799 }
8800
8801 /* If it isn't DW_AT_sibling, skip this attribute. */
8802 form = abbrev->attrs[i].form;
8803 skip_attribute:
8804 switch (form)
8805 {
8806 case DW_FORM_ref_addr:
8807 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8808 and later it is offset sized. */
8809 if (cu->header.version == 2)
8810 info_ptr += cu->header.addr_size;
8811 else
8812 info_ptr += cu->header.offset_size;
8813 break;
8814 case DW_FORM_GNU_ref_alt:
8815 info_ptr += cu->header.offset_size;
8816 break;
8817 case DW_FORM_addr:
8818 info_ptr += cu->header.addr_size;
8819 break;
8820 case DW_FORM_data1:
8821 case DW_FORM_ref1:
8822 case DW_FORM_flag:
8823 case DW_FORM_strx1:
8824 info_ptr += 1;
8825 break;
8826 case DW_FORM_flag_present:
8827 case DW_FORM_implicit_const:
8828 break;
8829 case DW_FORM_data2:
8830 case DW_FORM_ref2:
8831 case DW_FORM_strx2:
8832 info_ptr += 2;
8833 break;
8834 case DW_FORM_strx3:
8835 info_ptr += 3;
8836 break;
8837 case DW_FORM_data4:
8838 case DW_FORM_ref4:
8839 case DW_FORM_strx4:
8840 info_ptr += 4;
8841 break;
8842 case DW_FORM_data8:
8843 case DW_FORM_ref8:
8844 case DW_FORM_ref_sig8:
8845 info_ptr += 8;
8846 break;
8847 case DW_FORM_data16:
8848 info_ptr += 16;
8849 break;
8850 case DW_FORM_string:
8851 read_direct_string (abfd, info_ptr, &bytes_read);
8852 info_ptr += bytes_read;
8853 break;
8854 case DW_FORM_sec_offset:
8855 case DW_FORM_strp:
8856 case DW_FORM_GNU_strp_alt:
8857 info_ptr += cu->header.offset_size;
8858 break;
8859 case DW_FORM_exprloc:
8860 case DW_FORM_block:
8861 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8862 info_ptr += bytes_read;
8863 break;
8864 case DW_FORM_block1:
8865 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8866 break;
8867 case DW_FORM_block2:
8868 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8869 break;
8870 case DW_FORM_block4:
8871 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8872 break;
8873 case DW_FORM_addrx:
8874 case DW_FORM_strx:
8875 case DW_FORM_sdata:
8876 case DW_FORM_udata:
8877 case DW_FORM_ref_udata:
8878 case DW_FORM_GNU_addr_index:
8879 case DW_FORM_GNU_str_index:
8880 case DW_FORM_rnglistx:
8881 case DW_FORM_loclistx:
8882 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8883 break;
8884 case DW_FORM_indirect:
8885 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8886 info_ptr += bytes_read;
8887 /* We need to continue parsing from here, so just go back to
8888 the top. */
8889 goto skip_attribute;
8890
8891 default:
8892 error (_("Dwarf Error: Cannot handle %s "
8893 "in DWARF reader [in module %s]"),
8894 dwarf_form_name (form),
8895 bfd_get_filename (abfd));
8896 }
8897 }
8898
8899 if (abbrev->has_children)
8900 return skip_children (reader, info_ptr);
8901 else
8902 return info_ptr;
8903 }
8904
8905 /* Locate ORIG_PDI's sibling.
8906 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8907
8908 static const gdb_byte *
8909 locate_pdi_sibling (const struct die_reader_specs *reader,
8910 struct partial_die_info *orig_pdi,
8911 const gdb_byte *info_ptr)
8912 {
8913 /* Do we know the sibling already? */
8914
8915 if (orig_pdi->sibling)
8916 return orig_pdi->sibling;
8917
8918 /* Are there any children to deal with? */
8919
8920 if (!orig_pdi->has_children)
8921 return info_ptr;
8922
8923 /* Skip the children the long way. */
8924
8925 return skip_children (reader, info_ptr);
8926 }
8927
8928 /* Expand this partial symbol table into a full symbol table. SELF is
8929 not NULL. */
8930
8931 void
8932 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8933 {
8934 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8935
8936 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8937
8938 /* If this psymtab is constructed from a debug-only objfile, the
8939 has_section_at_zero flag will not necessarily be correct. We
8940 can get the correct value for this flag by looking at the data
8941 associated with the (presumably stripped) associated objfile. */
8942 if (objfile->separate_debug_objfile_backlink)
8943 {
8944 dwarf2_per_objfile *per_objfile_backlink
8945 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8946
8947 per_objfile->per_bfd->has_section_at_zero
8948 = per_objfile_backlink->per_bfd->has_section_at_zero;
8949 }
8950
8951 expand_psymtab (objfile);
8952
8953 process_cu_includes (per_objfile);
8954 }
8955 \f
8956 /* Reading in full CUs. */
8957
8958 /* Add PER_CU to the queue. */
8959
8960 static void
8961 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8962 dwarf2_per_objfile *per_objfile,
8963 enum language pretend_language)
8964 {
8965 per_cu->queued = 1;
8966 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
8967 }
8968
8969 /* If PER_CU is not yet queued, add it to the queue.
8970 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8971 dependency.
8972 The result is non-zero if PER_CU was queued, otherwise the result is zero
8973 meaning either PER_CU is already queued or it is already loaded.
8974
8975 N.B. There is an invariant here that if a CU is queued then it is loaded.
8976 The caller is required to load PER_CU if we return non-zero. */
8977
8978 static int
8979 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8980 dwarf2_per_cu_data *per_cu,
8981 dwarf2_per_objfile *per_objfile,
8982 enum language pretend_language)
8983 {
8984 /* We may arrive here during partial symbol reading, if we need full
8985 DIEs to process an unusual case (e.g. template arguments). Do
8986 not queue PER_CU, just tell our caller to load its DIEs. */
8987 if (per_cu->per_bfd->reading_partial_symbols)
8988 {
8989 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8990
8991 if (cu == NULL || cu->dies == NULL)
8992 return 1;
8993 return 0;
8994 }
8995
8996 /* Mark the dependence relation so that we don't flush PER_CU
8997 too early. */
8998 if (dependent_cu != NULL)
8999 dwarf2_add_dependence (dependent_cu, per_cu);
9000
9001 /* If it's already on the queue, we have nothing to do. */
9002 if (per_cu->queued)
9003 return 0;
9004
9005 /* If the compilation unit is already loaded, just mark it as
9006 used. */
9007 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9008 if (cu != nullptr)
9009 {
9010 cu->last_used = 0;
9011 return 0;
9012 }
9013
9014 /* Add it to the queue. */
9015 queue_comp_unit (per_cu, per_objfile, pretend_language);
9016
9017 return 1;
9018 }
9019
9020 /* Process the queue. */
9021
9022 static void
9023 process_queue (dwarf2_per_objfile *per_objfile)
9024 {
9025 if (dwarf_read_debug)
9026 {
9027 fprintf_unfiltered (gdb_stdlog,
9028 "Expanding one or more symtabs of objfile %s ...\n",
9029 objfile_name (per_objfile->objfile));
9030 }
9031
9032 /* The queue starts out with one item, but following a DIE reference
9033 may load a new CU, adding it to the end of the queue. */
9034 while (!per_objfile->per_bfd->queue.empty ())
9035 {
9036 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
9037 dwarf2_per_cu_data *per_cu = item.per_cu;
9038
9039 if (!per_objfile->symtab_set_p (per_cu))
9040 {
9041 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9042
9043 /* Skip dummy CUs. */
9044 if (cu != nullptr)
9045 {
9046 unsigned int debug_print_threshold;
9047 char buf[100];
9048
9049 if (per_cu->is_debug_types)
9050 {
9051 struct signatured_type *sig_type =
9052 (struct signatured_type *) per_cu;
9053
9054 sprintf (buf, "TU %s at offset %s",
9055 hex_string (sig_type->signature),
9056 sect_offset_str (per_cu->sect_off));
9057 /* There can be 100s of TUs.
9058 Only print them in verbose mode. */
9059 debug_print_threshold = 2;
9060 }
9061 else
9062 {
9063 sprintf (buf, "CU at offset %s",
9064 sect_offset_str (per_cu->sect_off));
9065 debug_print_threshold = 1;
9066 }
9067
9068 if (dwarf_read_debug >= debug_print_threshold)
9069 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9070
9071 if (per_cu->is_debug_types)
9072 process_full_type_unit (cu, item.pretend_language);
9073 else
9074 process_full_comp_unit (cu, item.pretend_language);
9075
9076 if (dwarf_read_debug >= debug_print_threshold)
9077 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9078 }
9079 }
9080
9081 per_cu->queued = 0;
9082 per_objfile->per_bfd->queue.pop ();
9083 }
9084
9085 if (dwarf_read_debug)
9086 {
9087 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9088 objfile_name (per_objfile->objfile));
9089 }
9090 }
9091
9092 /* Read in full symbols for PST, and anything it depends on. */
9093
9094 void
9095 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9096 {
9097 gdb_assert (!readin_p (objfile));
9098
9099 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9100 free_cached_comp_units freer (per_objfile);
9101 expand_dependencies (objfile);
9102
9103 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9104 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9105 }
9106
9107 /* See psympriv.h. */
9108
9109 bool
9110 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9111 {
9112 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9113 return per_objfile->symtab_set_p (per_cu_data);
9114 }
9115
9116 /* See psympriv.h. */
9117
9118 compunit_symtab *
9119 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9120 {
9121 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9122 return per_objfile->get_symtab (per_cu_data);
9123 }
9124
9125 /* Trivial hash function for die_info: the hash value of a DIE
9126 is its offset in .debug_info for this objfile. */
9127
9128 static hashval_t
9129 die_hash (const void *item)
9130 {
9131 const struct die_info *die = (const struct die_info *) item;
9132
9133 return to_underlying (die->sect_off);
9134 }
9135
9136 /* Trivial comparison function for die_info structures: two DIEs
9137 are equal if they have the same offset. */
9138
9139 static int
9140 die_eq (const void *item_lhs, const void *item_rhs)
9141 {
9142 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9143 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9144
9145 return die_lhs->sect_off == die_rhs->sect_off;
9146 }
9147
9148 /* Load the DIEs associated with PER_CU into memory. */
9149
9150 static void
9151 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9152 dwarf2_per_objfile *per_objfile,
9153 bool skip_partial,
9154 enum language pretend_language)
9155 {
9156 gdb_assert (! this_cu->is_debug_types);
9157
9158 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9159 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9160 if (reader.dummy_p)
9161 return;
9162
9163 struct dwarf2_cu *cu = reader.cu;
9164 const gdb_byte *info_ptr = reader.info_ptr;
9165
9166 gdb_assert (cu->die_hash == NULL);
9167 cu->die_hash =
9168 htab_create_alloc_ex (cu->header.length / 12,
9169 die_hash,
9170 die_eq,
9171 NULL,
9172 &cu->comp_unit_obstack,
9173 hashtab_obstack_allocate,
9174 dummy_obstack_deallocate);
9175
9176 if (reader.comp_unit_die->has_children)
9177 reader.comp_unit_die->child
9178 = read_die_and_siblings (&reader, reader.info_ptr,
9179 &info_ptr, reader.comp_unit_die);
9180 cu->dies = reader.comp_unit_die;
9181 /* comp_unit_die is not stored in die_hash, no need. */
9182
9183 /* We try not to read any attributes in this function, because not
9184 all CUs needed for references have been loaded yet, and symbol
9185 table processing isn't initialized. But we have to set the CU language,
9186 or we won't be able to build types correctly.
9187 Similarly, if we do not read the producer, we can not apply
9188 producer-specific interpretation. */
9189 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9190
9191 reader.keep ();
9192 }
9193
9194 /* Add a DIE to the delayed physname list. */
9195
9196 static void
9197 add_to_method_list (struct type *type, int fnfield_index, int index,
9198 const char *name, struct die_info *die,
9199 struct dwarf2_cu *cu)
9200 {
9201 struct delayed_method_info mi;
9202 mi.type = type;
9203 mi.fnfield_index = fnfield_index;
9204 mi.index = index;
9205 mi.name = name;
9206 mi.die = die;
9207 cu->method_list.push_back (mi);
9208 }
9209
9210 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9211 "const" / "volatile". If so, decrements LEN by the length of the
9212 modifier and return true. Otherwise return false. */
9213
9214 template<size_t N>
9215 static bool
9216 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9217 {
9218 size_t mod_len = sizeof (mod) - 1;
9219 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9220 {
9221 len -= mod_len;
9222 return true;
9223 }
9224 return false;
9225 }
9226
9227 /* Compute the physnames of any methods on the CU's method list.
9228
9229 The computation of method physnames is delayed in order to avoid the
9230 (bad) condition that one of the method's formal parameters is of an as yet
9231 incomplete type. */
9232
9233 static void
9234 compute_delayed_physnames (struct dwarf2_cu *cu)
9235 {
9236 /* Only C++ delays computing physnames. */
9237 if (cu->method_list.empty ())
9238 return;
9239 gdb_assert (cu->language == language_cplus);
9240
9241 for (const delayed_method_info &mi : cu->method_list)
9242 {
9243 const char *physname;
9244 struct fn_fieldlist *fn_flp
9245 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9246 physname = dwarf2_physname (mi.name, mi.die, cu);
9247 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9248 = physname ? physname : "";
9249
9250 /* Since there's no tag to indicate whether a method is a
9251 const/volatile overload, extract that information out of the
9252 demangled name. */
9253 if (physname != NULL)
9254 {
9255 size_t len = strlen (physname);
9256
9257 while (1)
9258 {
9259 if (physname[len] == ')') /* shortcut */
9260 break;
9261 else if (check_modifier (physname, len, " const"))
9262 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9263 else if (check_modifier (physname, len, " volatile"))
9264 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9265 else
9266 break;
9267 }
9268 }
9269 }
9270
9271 /* The list is no longer needed. */
9272 cu->method_list.clear ();
9273 }
9274
9275 /* Go objects should be embedded in a DW_TAG_module DIE,
9276 and it's not clear if/how imported objects will appear.
9277 To keep Go support simple until that's worked out,
9278 go back through what we've read and create something usable.
9279 We could do this while processing each DIE, and feels kinda cleaner,
9280 but that way is more invasive.
9281 This is to, for example, allow the user to type "p var" or "b main"
9282 without having to specify the package name, and allow lookups
9283 of module.object to work in contexts that use the expression
9284 parser. */
9285
9286 static void
9287 fixup_go_packaging (struct dwarf2_cu *cu)
9288 {
9289 gdb::unique_xmalloc_ptr<char> package_name;
9290 struct pending *list;
9291 int i;
9292
9293 for (list = *cu->get_builder ()->get_global_symbols ();
9294 list != NULL;
9295 list = list->next)
9296 {
9297 for (i = 0; i < list->nsyms; ++i)
9298 {
9299 struct symbol *sym = list->symbol[i];
9300
9301 if (sym->language () == language_go
9302 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9303 {
9304 gdb::unique_xmalloc_ptr<char> this_package_name
9305 (go_symbol_package_name (sym));
9306
9307 if (this_package_name == NULL)
9308 continue;
9309 if (package_name == NULL)
9310 package_name = std::move (this_package_name);
9311 else
9312 {
9313 struct objfile *objfile = cu->per_objfile->objfile;
9314 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9315 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9316 (symbol_symtab (sym) != NULL
9317 ? symtab_to_filename_for_display
9318 (symbol_symtab (sym))
9319 : objfile_name (objfile)),
9320 this_package_name.get (), package_name.get ());
9321 }
9322 }
9323 }
9324 }
9325
9326 if (package_name != NULL)
9327 {
9328 struct objfile *objfile = cu->per_objfile->objfile;
9329 const char *saved_package_name = objfile->intern (package_name.get ());
9330 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9331 saved_package_name);
9332 struct symbol *sym;
9333
9334 sym = new (&objfile->objfile_obstack) symbol;
9335 sym->set_language (language_go, &objfile->objfile_obstack);
9336 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9337 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9338 e.g., "main" finds the "main" module and not C's main(). */
9339 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9340 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9341 SYMBOL_TYPE (sym) = type;
9342
9343 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9344 }
9345 }
9346
9347 /* Allocate a fully-qualified name consisting of the two parts on the
9348 obstack. */
9349
9350 static const char *
9351 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9352 {
9353 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9354 }
9355
9356 /* A helper that allocates a variant part to attach to a Rust enum
9357 type. OBSTACK is where the results should be allocated. TYPE is
9358 the type we're processing. DISCRIMINANT_INDEX is the index of the
9359 discriminant. It must be the index of one of the fields of TYPE.
9360 DEFAULT_INDEX is the index of the default field; or -1 if there is
9361 no default. RANGES is indexed by "effective" field number (the
9362 field index, but omitting the discriminant and default fields) and
9363 must hold the discriminant values used by the variants. Note that
9364 RANGES must have a lifetime at least as long as OBSTACK -- either
9365 already allocated on it, or static. */
9366
9367 static void
9368 alloc_rust_variant (struct obstack *obstack, struct type *type,
9369 int discriminant_index, int default_index,
9370 gdb::array_view<discriminant_range> ranges)
9371 {
9372 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9373 must be handled by the caller. */
9374 gdb_assert (discriminant_index >= 0
9375 && discriminant_index < type->num_fields ());
9376 gdb_assert (default_index == -1
9377 || (default_index >= 0 && default_index < type->num_fields ()));
9378
9379 /* We have one variant for each non-discriminant field. */
9380 int n_variants = type->num_fields () - 1;
9381
9382 variant *variants = new (obstack) variant[n_variants];
9383 int var_idx = 0;
9384 int range_idx = 0;
9385 for (int i = 0; i < type->num_fields (); ++i)
9386 {
9387 if (i == discriminant_index)
9388 continue;
9389
9390 variants[var_idx].first_field = i;
9391 variants[var_idx].last_field = i + 1;
9392
9393 /* The default field does not need a range, but other fields do.
9394 We skipped the discriminant above. */
9395 if (i != default_index)
9396 {
9397 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9398 ++range_idx;
9399 }
9400
9401 ++var_idx;
9402 }
9403
9404 gdb_assert (range_idx == ranges.size ());
9405 gdb_assert (var_idx == n_variants);
9406
9407 variant_part *part = new (obstack) variant_part;
9408 part->discriminant_index = discriminant_index;
9409 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9410 discriminant_index));
9411 part->variants = gdb::array_view<variant> (variants, n_variants);
9412
9413 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9414 gdb::array_view<variant_part> *prop_value
9415 = new (storage) gdb::array_view<variant_part> (part, 1);
9416
9417 struct dynamic_prop prop;
9418 prop.kind = PROP_VARIANT_PARTS;
9419 prop.data.variant_parts = prop_value;
9420
9421 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9422 }
9423
9424 /* Some versions of rustc emitted enums in an unusual way.
9425
9426 Ordinary enums were emitted as unions. The first element of each
9427 structure in the union was named "RUST$ENUM$DISR". This element
9428 held the discriminant.
9429
9430 These versions of Rust also implemented the "non-zero"
9431 optimization. When the enum had two values, and one is empty and
9432 the other holds a pointer that cannot be zero, the pointer is used
9433 as the discriminant, with a zero value meaning the empty variant.
9434 Here, the union's first member is of the form
9435 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9436 where the fieldnos are the indices of the fields that should be
9437 traversed in order to find the field (which may be several fields deep)
9438 and the variantname is the name of the variant of the case when the
9439 field is zero.
9440
9441 This function recognizes whether TYPE is of one of these forms,
9442 and, if so, smashes it to be a variant type. */
9443
9444 static void
9445 quirk_rust_enum (struct type *type, struct objfile *objfile)
9446 {
9447 gdb_assert (type->code () == TYPE_CODE_UNION);
9448
9449 /* We don't need to deal with empty enums. */
9450 if (type->num_fields () == 0)
9451 return;
9452
9453 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9454 if (type->num_fields () == 1
9455 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9456 {
9457 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9458
9459 /* Decode the field name to find the offset of the
9460 discriminant. */
9461 ULONGEST bit_offset = 0;
9462 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9463 while (name[0] >= '0' && name[0] <= '9')
9464 {
9465 char *tail;
9466 unsigned long index = strtoul (name, &tail, 10);
9467 name = tail;
9468 if (*name != '$'
9469 || index >= field_type->num_fields ()
9470 || (TYPE_FIELD_LOC_KIND (field_type, index)
9471 != FIELD_LOC_KIND_BITPOS))
9472 {
9473 complaint (_("Could not parse Rust enum encoding string \"%s\""
9474 "[in module %s]"),
9475 TYPE_FIELD_NAME (type, 0),
9476 objfile_name (objfile));
9477 return;
9478 }
9479 ++name;
9480
9481 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9482 field_type = TYPE_FIELD_TYPE (field_type, index);
9483 }
9484
9485 /* Smash this type to be a structure type. We have to do this
9486 because the type has already been recorded. */
9487 type->set_code (TYPE_CODE_STRUCT);
9488 type->set_num_fields (3);
9489 /* Save the field we care about. */
9490 struct field saved_field = type->field (0);
9491 type->set_fields
9492 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9493
9494 /* Put the discriminant at index 0. */
9495 TYPE_FIELD_TYPE (type, 0) = field_type;
9496 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9497 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9498 SET_FIELD_BITPOS (type->field (0), bit_offset);
9499
9500 /* The order of fields doesn't really matter, so put the real
9501 field at index 1 and the data-less field at index 2. */
9502 type->field (1) = saved_field;
9503 TYPE_FIELD_NAME (type, 1)
9504 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9505 TYPE_FIELD_TYPE (type, 1)->set_name
9506 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9507 TYPE_FIELD_NAME (type, 1)));
9508
9509 const char *dataless_name
9510 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9511 name);
9512 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9513 dataless_name);
9514 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9515 /* NAME points into the original discriminant name, which
9516 already has the correct lifetime. */
9517 TYPE_FIELD_NAME (type, 2) = name;
9518 SET_FIELD_BITPOS (type->field (2), 0);
9519
9520 /* Indicate that this is a variant type. */
9521 static discriminant_range ranges[1] = { { 0, 0 } };
9522 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9523 }
9524 /* A union with a single anonymous field is probably an old-style
9525 univariant enum. */
9526 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9527 {
9528 /* Smash this type to be a structure type. We have to do this
9529 because the type has already been recorded. */
9530 type->set_code (TYPE_CODE_STRUCT);
9531
9532 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9533 const char *variant_name
9534 = rust_last_path_segment (field_type->name ());
9535 TYPE_FIELD_NAME (type, 0) = variant_name;
9536 field_type->set_name
9537 (rust_fully_qualify (&objfile->objfile_obstack,
9538 type->name (), variant_name));
9539 }
9540 else
9541 {
9542 struct type *disr_type = nullptr;
9543 for (int i = 0; i < type->num_fields (); ++i)
9544 {
9545 disr_type = TYPE_FIELD_TYPE (type, i);
9546
9547 if (disr_type->code () != TYPE_CODE_STRUCT)
9548 {
9549 /* All fields of a true enum will be structs. */
9550 return;
9551 }
9552 else if (disr_type->num_fields () == 0)
9553 {
9554 /* Could be data-less variant, so keep going. */
9555 disr_type = nullptr;
9556 }
9557 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9558 "RUST$ENUM$DISR") != 0)
9559 {
9560 /* Not a Rust enum. */
9561 return;
9562 }
9563 else
9564 {
9565 /* Found one. */
9566 break;
9567 }
9568 }
9569
9570 /* If we got here without a discriminant, then it's probably
9571 just a union. */
9572 if (disr_type == nullptr)
9573 return;
9574
9575 /* Smash this type to be a structure type. We have to do this
9576 because the type has already been recorded. */
9577 type->set_code (TYPE_CODE_STRUCT);
9578
9579 /* Make space for the discriminant field. */
9580 struct field *disr_field = &disr_type->field (0);
9581 field *new_fields
9582 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9583 * sizeof (struct field)));
9584 memcpy (new_fields + 1, type->fields (),
9585 type->num_fields () * sizeof (struct field));
9586 type->set_fields (new_fields);
9587 type->set_num_fields (type->num_fields () + 1);
9588
9589 /* Install the discriminant at index 0 in the union. */
9590 type->field (0) = *disr_field;
9591 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9592 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9593
9594 /* We need a way to find the correct discriminant given a
9595 variant name. For convenience we build a map here. */
9596 struct type *enum_type = FIELD_TYPE (*disr_field);
9597 std::unordered_map<std::string, ULONGEST> discriminant_map;
9598 for (int i = 0; i < enum_type->num_fields (); ++i)
9599 {
9600 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9601 {
9602 const char *name
9603 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9604 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9605 }
9606 }
9607
9608 int n_fields = type->num_fields ();
9609 /* We don't need a range entry for the discriminant, but we do
9610 need one for every other field, as there is no default
9611 variant. */
9612 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9613 discriminant_range,
9614 n_fields - 1);
9615 /* Skip the discriminant here. */
9616 for (int i = 1; i < n_fields; ++i)
9617 {
9618 /* Find the final word in the name of this variant's type.
9619 That name can be used to look up the correct
9620 discriminant. */
9621 const char *variant_name
9622 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9623
9624 auto iter = discriminant_map.find (variant_name);
9625 if (iter != discriminant_map.end ())
9626 {
9627 ranges[i].low = iter->second;
9628 ranges[i].high = iter->second;
9629 }
9630
9631 /* Remove the discriminant field, if it exists. */
9632 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9633 if (sub_type->num_fields () > 0)
9634 {
9635 sub_type->set_num_fields (sub_type->num_fields () - 1);
9636 sub_type->set_fields (sub_type->fields () + 1);
9637 }
9638 TYPE_FIELD_NAME (type, i) = variant_name;
9639 sub_type->set_name
9640 (rust_fully_qualify (&objfile->objfile_obstack,
9641 type->name (), variant_name));
9642 }
9643
9644 /* Indicate that this is a variant type. */
9645 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9646 gdb::array_view<discriminant_range> (ranges,
9647 n_fields - 1));
9648 }
9649 }
9650
9651 /* Rewrite some Rust unions to be structures with variants parts. */
9652
9653 static void
9654 rust_union_quirks (struct dwarf2_cu *cu)
9655 {
9656 gdb_assert (cu->language == language_rust);
9657 for (type *type_ : cu->rust_unions)
9658 quirk_rust_enum (type_, cu->per_objfile->objfile);
9659 /* We don't need this any more. */
9660 cu->rust_unions.clear ();
9661 }
9662
9663 /* See read.h. */
9664
9665 type_unit_group_unshareable *
9666 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9667 {
9668 auto iter = this->m_type_units.find (tu_group);
9669 if (iter != this->m_type_units.end ())
9670 return iter->second.get ();
9671
9672 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9673 type_unit_group_unshareable *result = uniq.get ();
9674 this->m_type_units[tu_group] = std::move (uniq);
9675 return result;
9676 }
9677
9678 struct type *
9679 dwarf2_per_objfile::get_type_for_signatured_type
9680 (signatured_type *sig_type) const
9681 {
9682 auto iter = this->m_type_map.find (sig_type);
9683 if (iter == this->m_type_map.end ())
9684 return nullptr;
9685
9686 return iter->second;
9687 }
9688
9689 void dwarf2_per_objfile::set_type_for_signatured_type
9690 (signatured_type *sig_type, struct type *type)
9691 {
9692 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9693
9694 this->m_type_map[sig_type] = type;
9695 }
9696
9697 /* A helper function for computing the list of all symbol tables
9698 included by PER_CU. */
9699
9700 static void
9701 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9702 htab_t all_children, htab_t all_type_symtabs,
9703 dwarf2_per_cu_data *per_cu,
9704 dwarf2_per_objfile *per_objfile,
9705 struct compunit_symtab *immediate_parent)
9706 {
9707 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9708 if (*slot != NULL)
9709 {
9710 /* This inclusion and its children have been processed. */
9711 return;
9712 }
9713
9714 *slot = per_cu;
9715
9716 /* Only add a CU if it has a symbol table. */
9717 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9718 if (cust != NULL)
9719 {
9720 /* If this is a type unit only add its symbol table if we haven't
9721 seen it yet (type unit per_cu's can share symtabs). */
9722 if (per_cu->is_debug_types)
9723 {
9724 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9725 if (*slot == NULL)
9726 {
9727 *slot = cust;
9728 result->push_back (cust);
9729 if (cust->user == NULL)
9730 cust->user = immediate_parent;
9731 }
9732 }
9733 else
9734 {
9735 result->push_back (cust);
9736 if (cust->user == NULL)
9737 cust->user = immediate_parent;
9738 }
9739 }
9740
9741 if (!per_cu->imported_symtabs_empty ())
9742 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9743 {
9744 recursively_compute_inclusions (result, all_children,
9745 all_type_symtabs, ptr, per_objfile,
9746 cust);
9747 }
9748 }
9749
9750 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9751 PER_CU. */
9752
9753 static void
9754 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9755 dwarf2_per_objfile *per_objfile)
9756 {
9757 gdb_assert (! per_cu->is_debug_types);
9758
9759 if (!per_cu->imported_symtabs_empty ())
9760 {
9761 int len;
9762 std::vector<compunit_symtab *> result_symtabs;
9763 htab_t all_children, all_type_symtabs;
9764 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9765
9766 /* If we don't have a symtab, we can just skip this case. */
9767 if (cust == NULL)
9768 return;
9769
9770 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9771 NULL, xcalloc, xfree);
9772 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9773 NULL, xcalloc, xfree);
9774
9775 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9776 {
9777 recursively_compute_inclusions (&result_symtabs, all_children,
9778 all_type_symtabs, ptr, per_objfile,
9779 cust);
9780 }
9781
9782 /* Now we have a transitive closure of all the included symtabs. */
9783 len = result_symtabs.size ();
9784 cust->includes
9785 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9786 struct compunit_symtab *, len + 1);
9787 memcpy (cust->includes, result_symtabs.data (),
9788 len * sizeof (compunit_symtab *));
9789 cust->includes[len] = NULL;
9790
9791 htab_delete (all_children);
9792 htab_delete (all_type_symtabs);
9793 }
9794 }
9795
9796 /* Compute the 'includes' field for the symtabs of all the CUs we just
9797 read. */
9798
9799 static void
9800 process_cu_includes (dwarf2_per_objfile *per_objfile)
9801 {
9802 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9803 {
9804 if (! iter->is_debug_types)
9805 compute_compunit_symtab_includes (iter, per_objfile);
9806 }
9807
9808 per_objfile->per_bfd->just_read_cus.clear ();
9809 }
9810
9811 /* Generate full symbol information for CU, whose DIEs have
9812 already been loaded into memory. */
9813
9814 static void
9815 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9816 {
9817 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9818 struct objfile *objfile = per_objfile->objfile;
9819 struct gdbarch *gdbarch = objfile->arch ();
9820 CORE_ADDR lowpc, highpc;
9821 struct compunit_symtab *cust;
9822 CORE_ADDR baseaddr;
9823 struct block *static_block;
9824 CORE_ADDR addr;
9825
9826 baseaddr = objfile->text_section_offset ();
9827
9828 /* Clear the list here in case something was left over. */
9829 cu->method_list.clear ();
9830
9831 cu->language = pretend_language;
9832 cu->language_defn = language_def (cu->language);
9833
9834 /* Do line number decoding in read_file_scope () */
9835 process_die (cu->dies, cu);
9836
9837 /* For now fudge the Go package. */
9838 if (cu->language == language_go)
9839 fixup_go_packaging (cu);
9840
9841 /* Now that we have processed all the DIEs in the CU, all the types
9842 should be complete, and it should now be safe to compute all of the
9843 physnames. */
9844 compute_delayed_physnames (cu);
9845
9846 if (cu->language == language_rust)
9847 rust_union_quirks (cu);
9848
9849 /* Some compilers don't define a DW_AT_high_pc attribute for the
9850 compilation unit. If the DW_AT_high_pc is missing, synthesize
9851 it, by scanning the DIE's below the compilation unit. */
9852 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9853
9854 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9855 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9856
9857 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9858 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9859 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9860 addrmap to help ensure it has an accurate map of pc values belonging to
9861 this comp unit. */
9862 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9863
9864 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9865 SECT_OFF_TEXT (objfile),
9866 0);
9867
9868 if (cust != NULL)
9869 {
9870 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9871
9872 /* Set symtab language to language from DW_AT_language. If the
9873 compilation is from a C file generated by language preprocessors, do
9874 not set the language if it was already deduced by start_subfile. */
9875 if (!(cu->language == language_c
9876 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9877 COMPUNIT_FILETABS (cust)->language = cu->language;
9878
9879 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9880 produce DW_AT_location with location lists but it can be possibly
9881 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9882 there were bugs in prologue debug info, fixed later in GCC-4.5
9883 by "unwind info for epilogues" patch (which is not directly related).
9884
9885 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9886 needed, it would be wrong due to missing DW_AT_producer there.
9887
9888 Still one can confuse GDB by using non-standard GCC compilation
9889 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9890 */
9891 if (cu->has_loclist && gcc_4_minor >= 5)
9892 cust->locations_valid = 1;
9893
9894 if (gcc_4_minor >= 5)
9895 cust->epilogue_unwind_valid = 1;
9896
9897 cust->call_site_htab = cu->call_site_htab;
9898 }
9899
9900 per_objfile->set_symtab (cu->per_cu, cust);
9901
9902 /* Push it for inclusion processing later. */
9903 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9904
9905 /* Not needed any more. */
9906 cu->reset_builder ();
9907 }
9908
9909 /* Generate full symbol information for type unit CU, whose DIEs have
9910 already been loaded into memory. */
9911
9912 static void
9913 process_full_type_unit (dwarf2_cu *cu,
9914 enum language pretend_language)
9915 {
9916 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9917 struct objfile *objfile = per_objfile->objfile;
9918 struct compunit_symtab *cust;
9919 struct signatured_type *sig_type;
9920
9921 gdb_assert (cu->per_cu->is_debug_types);
9922 sig_type = (struct signatured_type *) cu->per_cu;
9923
9924 /* Clear the list here in case something was left over. */
9925 cu->method_list.clear ();
9926
9927 cu->language = pretend_language;
9928 cu->language_defn = language_def (cu->language);
9929
9930 /* The symbol tables are set up in read_type_unit_scope. */
9931 process_die (cu->dies, cu);
9932
9933 /* For now fudge the Go package. */
9934 if (cu->language == language_go)
9935 fixup_go_packaging (cu);
9936
9937 /* Now that we have processed all the DIEs in the CU, all the types
9938 should be complete, and it should now be safe to compute all of the
9939 physnames. */
9940 compute_delayed_physnames (cu);
9941
9942 if (cu->language == language_rust)
9943 rust_union_quirks (cu);
9944
9945 /* TUs share symbol tables.
9946 If this is the first TU to use this symtab, complete the construction
9947 of it with end_expandable_symtab. Otherwise, complete the addition of
9948 this TU's symbols to the existing symtab. */
9949 type_unit_group_unshareable *tug_unshare =
9950 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9951 if (tug_unshare->compunit_symtab == NULL)
9952 {
9953 buildsym_compunit *builder = cu->get_builder ();
9954 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9955 tug_unshare->compunit_symtab = cust;
9956
9957 if (cust != NULL)
9958 {
9959 /* Set symtab language to language from DW_AT_language. If the
9960 compilation is from a C file generated by language preprocessors,
9961 do not set the language if it was already deduced by
9962 start_subfile. */
9963 if (!(cu->language == language_c
9964 && COMPUNIT_FILETABS (cust)->language != language_c))
9965 COMPUNIT_FILETABS (cust)->language = cu->language;
9966 }
9967 }
9968 else
9969 {
9970 cu->get_builder ()->augment_type_symtab ();
9971 cust = tug_unshare->compunit_symtab;
9972 }
9973
9974 per_objfile->set_symtab (cu->per_cu, cust);
9975
9976 /* Not needed any more. */
9977 cu->reset_builder ();
9978 }
9979
9980 /* Process an imported unit DIE. */
9981
9982 static void
9983 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9984 {
9985 struct attribute *attr;
9986
9987 /* For now we don't handle imported units in type units. */
9988 if (cu->per_cu->is_debug_types)
9989 {
9990 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9991 " supported in type units [in module %s]"),
9992 objfile_name (cu->per_objfile->objfile));
9993 }
9994
9995 attr = dwarf2_attr (die, DW_AT_import, cu);
9996 if (attr != NULL)
9997 {
9998 sect_offset sect_off = attr->get_ref_die_offset ();
9999 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10000 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10001 dwarf2_per_cu_data *per_cu
10002 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10003
10004 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10005 into another compilation unit, at root level. Regard this as a hint,
10006 and ignore it. */
10007 if (die->parent && die->parent->parent == NULL
10008 && per_cu->unit_type == DW_UT_compile
10009 && per_cu->lang == language_cplus)
10010 return;
10011
10012 /* If necessary, add it to the queue and load its DIEs. */
10013 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10014 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
10015
10016 cu->per_cu->imported_symtabs_push (per_cu);
10017 }
10018 }
10019
10020 /* RAII object that represents a process_die scope: i.e.,
10021 starts/finishes processing a DIE. */
10022 class process_die_scope
10023 {
10024 public:
10025 process_die_scope (die_info *die, dwarf2_cu *cu)
10026 : m_die (die), m_cu (cu)
10027 {
10028 /* We should only be processing DIEs not already in process. */
10029 gdb_assert (!m_die->in_process);
10030 m_die->in_process = true;
10031 }
10032
10033 ~process_die_scope ()
10034 {
10035 m_die->in_process = false;
10036
10037 /* If we're done processing the DIE for the CU that owns the line
10038 header, we don't need the line header anymore. */
10039 if (m_cu->line_header_die_owner == m_die)
10040 {
10041 delete m_cu->line_header;
10042 m_cu->line_header = NULL;
10043 m_cu->line_header_die_owner = NULL;
10044 }
10045 }
10046
10047 private:
10048 die_info *m_die;
10049 dwarf2_cu *m_cu;
10050 };
10051
10052 /* Process a die and its children. */
10053
10054 static void
10055 process_die (struct die_info *die, struct dwarf2_cu *cu)
10056 {
10057 process_die_scope scope (die, cu);
10058
10059 switch (die->tag)
10060 {
10061 case DW_TAG_padding:
10062 break;
10063 case DW_TAG_compile_unit:
10064 case DW_TAG_partial_unit:
10065 read_file_scope (die, cu);
10066 break;
10067 case DW_TAG_type_unit:
10068 read_type_unit_scope (die, cu);
10069 break;
10070 case DW_TAG_subprogram:
10071 /* Nested subprograms in Fortran get a prefix. */
10072 if (cu->language == language_fortran
10073 && die->parent != NULL
10074 && die->parent->tag == DW_TAG_subprogram)
10075 cu->processing_has_namespace_info = true;
10076 /* Fall through. */
10077 case DW_TAG_inlined_subroutine:
10078 read_func_scope (die, cu);
10079 break;
10080 case DW_TAG_lexical_block:
10081 case DW_TAG_try_block:
10082 case DW_TAG_catch_block:
10083 read_lexical_block_scope (die, cu);
10084 break;
10085 case DW_TAG_call_site:
10086 case DW_TAG_GNU_call_site:
10087 read_call_site_scope (die, cu);
10088 break;
10089 case DW_TAG_class_type:
10090 case DW_TAG_interface_type:
10091 case DW_TAG_structure_type:
10092 case DW_TAG_union_type:
10093 process_structure_scope (die, cu);
10094 break;
10095 case DW_TAG_enumeration_type:
10096 process_enumeration_scope (die, cu);
10097 break;
10098
10099 /* These dies have a type, but processing them does not create
10100 a symbol or recurse to process the children. Therefore we can
10101 read them on-demand through read_type_die. */
10102 case DW_TAG_subroutine_type:
10103 case DW_TAG_set_type:
10104 case DW_TAG_array_type:
10105 case DW_TAG_pointer_type:
10106 case DW_TAG_ptr_to_member_type:
10107 case DW_TAG_reference_type:
10108 case DW_TAG_rvalue_reference_type:
10109 case DW_TAG_string_type:
10110 break;
10111
10112 case DW_TAG_base_type:
10113 case DW_TAG_subrange_type:
10114 case DW_TAG_typedef:
10115 /* Add a typedef symbol for the type definition, if it has a
10116 DW_AT_name. */
10117 new_symbol (die, read_type_die (die, cu), cu);
10118 break;
10119 case DW_TAG_common_block:
10120 read_common_block (die, cu);
10121 break;
10122 case DW_TAG_common_inclusion:
10123 break;
10124 case DW_TAG_namespace:
10125 cu->processing_has_namespace_info = true;
10126 read_namespace (die, cu);
10127 break;
10128 case DW_TAG_module:
10129 cu->processing_has_namespace_info = true;
10130 read_module (die, cu);
10131 break;
10132 case DW_TAG_imported_declaration:
10133 cu->processing_has_namespace_info = true;
10134 if (read_namespace_alias (die, cu))
10135 break;
10136 /* The declaration is not a global namespace alias. */
10137 /* Fall through. */
10138 case DW_TAG_imported_module:
10139 cu->processing_has_namespace_info = true;
10140 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10141 || cu->language != language_fortran))
10142 complaint (_("Tag '%s' has unexpected children"),
10143 dwarf_tag_name (die->tag));
10144 read_import_statement (die, cu);
10145 break;
10146
10147 case DW_TAG_imported_unit:
10148 process_imported_unit_die (die, cu);
10149 break;
10150
10151 case DW_TAG_variable:
10152 read_variable (die, cu);
10153 break;
10154
10155 default:
10156 new_symbol (die, NULL, cu);
10157 break;
10158 }
10159 }
10160 \f
10161 /* DWARF name computation. */
10162
10163 /* A helper function for dwarf2_compute_name which determines whether DIE
10164 needs to have the name of the scope prepended to the name listed in the
10165 die. */
10166
10167 static int
10168 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10169 {
10170 struct attribute *attr;
10171
10172 switch (die->tag)
10173 {
10174 case DW_TAG_namespace:
10175 case DW_TAG_typedef:
10176 case DW_TAG_class_type:
10177 case DW_TAG_interface_type:
10178 case DW_TAG_structure_type:
10179 case DW_TAG_union_type:
10180 case DW_TAG_enumeration_type:
10181 case DW_TAG_enumerator:
10182 case DW_TAG_subprogram:
10183 case DW_TAG_inlined_subroutine:
10184 case DW_TAG_member:
10185 case DW_TAG_imported_declaration:
10186 return 1;
10187
10188 case DW_TAG_variable:
10189 case DW_TAG_constant:
10190 /* We only need to prefix "globally" visible variables. These include
10191 any variable marked with DW_AT_external or any variable that
10192 lives in a namespace. [Variables in anonymous namespaces
10193 require prefixing, but they are not DW_AT_external.] */
10194
10195 if (dwarf2_attr (die, DW_AT_specification, cu))
10196 {
10197 struct dwarf2_cu *spec_cu = cu;
10198
10199 return die_needs_namespace (die_specification (die, &spec_cu),
10200 spec_cu);
10201 }
10202
10203 attr = dwarf2_attr (die, DW_AT_external, cu);
10204 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10205 && die->parent->tag != DW_TAG_module)
10206 return 0;
10207 /* A variable in a lexical block of some kind does not need a
10208 namespace, even though in C++ such variables may be external
10209 and have a mangled name. */
10210 if (die->parent->tag == DW_TAG_lexical_block
10211 || die->parent->tag == DW_TAG_try_block
10212 || die->parent->tag == DW_TAG_catch_block
10213 || die->parent->tag == DW_TAG_subprogram)
10214 return 0;
10215 return 1;
10216
10217 default:
10218 return 0;
10219 }
10220 }
10221
10222 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10223 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10224 defined for the given DIE. */
10225
10226 static struct attribute *
10227 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10228 {
10229 struct attribute *attr;
10230
10231 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10232 if (attr == NULL)
10233 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10234
10235 return attr;
10236 }
10237
10238 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10239 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10240 defined for the given DIE. */
10241
10242 static const char *
10243 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10244 {
10245 const char *linkage_name;
10246
10247 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10248 if (linkage_name == NULL)
10249 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10250
10251 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10252 See https://github.com/rust-lang/rust/issues/32925. */
10253 if (cu->language == language_rust && linkage_name != NULL
10254 && strchr (linkage_name, '{') != NULL)
10255 linkage_name = NULL;
10256
10257 return linkage_name;
10258 }
10259
10260 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10261 compute the physname for the object, which include a method's:
10262 - formal parameters (C++),
10263 - receiver type (Go),
10264
10265 The term "physname" is a bit confusing.
10266 For C++, for example, it is the demangled name.
10267 For Go, for example, it's the mangled name.
10268
10269 For Ada, return the DIE's linkage name rather than the fully qualified
10270 name. PHYSNAME is ignored..
10271
10272 The result is allocated on the objfile->per_bfd's obstack and
10273 canonicalized. */
10274
10275 static const char *
10276 dwarf2_compute_name (const char *name,
10277 struct die_info *die, struct dwarf2_cu *cu,
10278 int physname)
10279 {
10280 struct objfile *objfile = cu->per_objfile->objfile;
10281
10282 if (name == NULL)
10283 name = dwarf2_name (die, cu);
10284
10285 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10286 but otherwise compute it by typename_concat inside GDB.
10287 FIXME: Actually this is not really true, or at least not always true.
10288 It's all very confusing. compute_and_set_names doesn't try to demangle
10289 Fortran names because there is no mangling standard. So new_symbol
10290 will set the demangled name to the result of dwarf2_full_name, and it is
10291 the demangled name that GDB uses if it exists. */
10292 if (cu->language == language_ada
10293 || (cu->language == language_fortran && physname))
10294 {
10295 /* For Ada unit, we prefer the linkage name over the name, as
10296 the former contains the exported name, which the user expects
10297 to be able to reference. Ideally, we want the user to be able
10298 to reference this entity using either natural or linkage name,
10299 but we haven't started looking at this enhancement yet. */
10300 const char *linkage_name = dw2_linkage_name (die, cu);
10301
10302 if (linkage_name != NULL)
10303 return linkage_name;
10304 }
10305
10306 /* These are the only languages we know how to qualify names in. */
10307 if (name != NULL
10308 && (cu->language == language_cplus
10309 || cu->language == language_fortran || cu->language == language_d
10310 || cu->language == language_rust))
10311 {
10312 if (die_needs_namespace (die, cu))
10313 {
10314 const char *prefix;
10315 const char *canonical_name = NULL;
10316
10317 string_file buf;
10318
10319 prefix = determine_prefix (die, cu);
10320 if (*prefix != '\0')
10321 {
10322 gdb::unique_xmalloc_ptr<char> prefixed_name
10323 (typename_concat (NULL, prefix, name, physname, cu));
10324
10325 buf.puts (prefixed_name.get ());
10326 }
10327 else
10328 buf.puts (name);
10329
10330 /* Template parameters may be specified in the DIE's DW_AT_name, or
10331 as children with DW_TAG_template_type_param or
10332 DW_TAG_value_type_param. If the latter, add them to the name
10333 here. If the name already has template parameters, then
10334 skip this step; some versions of GCC emit both, and
10335 it is more efficient to use the pre-computed name.
10336
10337 Something to keep in mind about this process: it is very
10338 unlikely, or in some cases downright impossible, to produce
10339 something that will match the mangled name of a function.
10340 If the definition of the function has the same debug info,
10341 we should be able to match up with it anyway. But fallbacks
10342 using the minimal symbol, for instance to find a method
10343 implemented in a stripped copy of libstdc++, will not work.
10344 If we do not have debug info for the definition, we will have to
10345 match them up some other way.
10346
10347 When we do name matching there is a related problem with function
10348 templates; two instantiated function templates are allowed to
10349 differ only by their return types, which we do not add here. */
10350
10351 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10352 {
10353 struct attribute *attr;
10354 struct die_info *child;
10355 int first = 1;
10356
10357 die->building_fullname = 1;
10358
10359 for (child = die->child; child != NULL; child = child->sibling)
10360 {
10361 struct type *type;
10362 LONGEST value;
10363 const gdb_byte *bytes;
10364 struct dwarf2_locexpr_baton *baton;
10365 struct value *v;
10366
10367 if (child->tag != DW_TAG_template_type_param
10368 && child->tag != DW_TAG_template_value_param)
10369 continue;
10370
10371 if (first)
10372 {
10373 buf.puts ("<");
10374 first = 0;
10375 }
10376 else
10377 buf.puts (", ");
10378
10379 attr = dwarf2_attr (child, DW_AT_type, cu);
10380 if (attr == NULL)
10381 {
10382 complaint (_("template parameter missing DW_AT_type"));
10383 buf.puts ("UNKNOWN_TYPE");
10384 continue;
10385 }
10386 type = die_type (child, cu);
10387
10388 if (child->tag == DW_TAG_template_type_param)
10389 {
10390 c_print_type (type, "", &buf, -1, 0, cu->language,
10391 &type_print_raw_options);
10392 continue;
10393 }
10394
10395 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10396 if (attr == NULL)
10397 {
10398 complaint (_("template parameter missing "
10399 "DW_AT_const_value"));
10400 buf.puts ("UNKNOWN_VALUE");
10401 continue;
10402 }
10403
10404 dwarf2_const_value_attr (attr, type, name,
10405 &cu->comp_unit_obstack, cu,
10406 &value, &bytes, &baton);
10407
10408 if (TYPE_NOSIGN (type))
10409 /* GDB prints characters as NUMBER 'CHAR'. If that's
10410 changed, this can use value_print instead. */
10411 c_printchar (value, type, &buf);
10412 else
10413 {
10414 struct value_print_options opts;
10415
10416 if (baton != NULL)
10417 v = dwarf2_evaluate_loc_desc (type, NULL,
10418 baton->data,
10419 baton->size,
10420 baton->per_cu,
10421 baton->per_objfile);
10422 else if (bytes != NULL)
10423 {
10424 v = allocate_value (type);
10425 memcpy (value_contents_writeable (v), bytes,
10426 TYPE_LENGTH (type));
10427 }
10428 else
10429 v = value_from_longest (type, value);
10430
10431 /* Specify decimal so that we do not depend on
10432 the radix. */
10433 get_formatted_print_options (&opts, 'd');
10434 opts.raw = 1;
10435 value_print (v, &buf, &opts);
10436 release_value (v);
10437 }
10438 }
10439
10440 die->building_fullname = 0;
10441
10442 if (!first)
10443 {
10444 /* Close the argument list, with a space if necessary
10445 (nested templates). */
10446 if (!buf.empty () && buf.string ().back () == '>')
10447 buf.puts (" >");
10448 else
10449 buf.puts (">");
10450 }
10451 }
10452
10453 /* For C++ methods, append formal parameter type
10454 information, if PHYSNAME. */
10455
10456 if (physname && die->tag == DW_TAG_subprogram
10457 && cu->language == language_cplus)
10458 {
10459 struct type *type = read_type_die (die, cu);
10460
10461 c_type_print_args (type, &buf, 1, cu->language,
10462 &type_print_raw_options);
10463
10464 if (cu->language == language_cplus)
10465 {
10466 /* Assume that an artificial first parameter is
10467 "this", but do not crash if it is not. RealView
10468 marks unnamed (and thus unused) parameters as
10469 artificial; there is no way to differentiate
10470 the two cases. */
10471 if (type->num_fields () > 0
10472 && TYPE_FIELD_ARTIFICIAL (type, 0)
10473 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10474 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10475 0))))
10476 buf.puts (" const");
10477 }
10478 }
10479
10480 const std::string &intermediate_name = buf.string ();
10481
10482 if (cu->language == language_cplus)
10483 canonical_name
10484 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10485 objfile);
10486
10487 /* If we only computed INTERMEDIATE_NAME, or if
10488 INTERMEDIATE_NAME is already canonical, then we need to
10489 intern it. */
10490 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10491 name = objfile->intern (intermediate_name);
10492 else
10493 name = canonical_name;
10494 }
10495 }
10496
10497 return name;
10498 }
10499
10500 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10501 If scope qualifiers are appropriate they will be added. The result
10502 will be allocated on the storage_obstack, or NULL if the DIE does
10503 not have a name. NAME may either be from a previous call to
10504 dwarf2_name or NULL.
10505
10506 The output string will be canonicalized (if C++). */
10507
10508 static const char *
10509 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10510 {
10511 return dwarf2_compute_name (name, die, cu, 0);
10512 }
10513
10514 /* Construct a physname for the given DIE in CU. NAME may either be
10515 from a previous call to dwarf2_name or NULL. The result will be
10516 allocated on the objfile_objstack or NULL if the DIE does not have a
10517 name.
10518
10519 The output string will be canonicalized (if C++). */
10520
10521 static const char *
10522 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10523 {
10524 struct objfile *objfile = cu->per_objfile->objfile;
10525 const char *retval, *mangled = NULL, *canon = NULL;
10526 int need_copy = 1;
10527
10528 /* In this case dwarf2_compute_name is just a shortcut not building anything
10529 on its own. */
10530 if (!die_needs_namespace (die, cu))
10531 return dwarf2_compute_name (name, die, cu, 1);
10532
10533 if (cu->language != language_rust)
10534 mangled = dw2_linkage_name (die, cu);
10535
10536 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10537 has computed. */
10538 gdb::unique_xmalloc_ptr<char> demangled;
10539 if (mangled != NULL)
10540 {
10541
10542 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10543 {
10544 /* Do nothing (do not demangle the symbol name). */
10545 }
10546 else if (cu->language == language_go)
10547 {
10548 /* This is a lie, but we already lie to the caller new_symbol.
10549 new_symbol assumes we return the mangled name.
10550 This just undoes that lie until things are cleaned up. */
10551 }
10552 else
10553 {
10554 /* Use DMGL_RET_DROP for C++ template functions to suppress
10555 their return type. It is easier for GDB users to search
10556 for such functions as `name(params)' than `long name(params)'.
10557 In such case the minimal symbol names do not match the full
10558 symbol names but for template functions there is never a need
10559 to look up their definition from their declaration so
10560 the only disadvantage remains the minimal symbol variant
10561 `long name(params)' does not have the proper inferior type. */
10562 demangled.reset (gdb_demangle (mangled,
10563 (DMGL_PARAMS | DMGL_ANSI
10564 | DMGL_RET_DROP)));
10565 }
10566 if (demangled)
10567 canon = demangled.get ();
10568 else
10569 {
10570 canon = mangled;
10571 need_copy = 0;
10572 }
10573 }
10574
10575 if (canon == NULL || check_physname)
10576 {
10577 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10578
10579 if (canon != NULL && strcmp (physname, canon) != 0)
10580 {
10581 /* It may not mean a bug in GDB. The compiler could also
10582 compute DW_AT_linkage_name incorrectly. But in such case
10583 GDB would need to be bug-to-bug compatible. */
10584
10585 complaint (_("Computed physname <%s> does not match demangled <%s> "
10586 "(from linkage <%s>) - DIE at %s [in module %s]"),
10587 physname, canon, mangled, sect_offset_str (die->sect_off),
10588 objfile_name (objfile));
10589
10590 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10591 is available here - over computed PHYSNAME. It is safer
10592 against both buggy GDB and buggy compilers. */
10593
10594 retval = canon;
10595 }
10596 else
10597 {
10598 retval = physname;
10599 need_copy = 0;
10600 }
10601 }
10602 else
10603 retval = canon;
10604
10605 if (need_copy)
10606 retval = objfile->intern (retval);
10607
10608 return retval;
10609 }
10610
10611 /* Inspect DIE in CU for a namespace alias. If one exists, record
10612 a new symbol for it.
10613
10614 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10615
10616 static int
10617 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10618 {
10619 struct attribute *attr;
10620
10621 /* If the die does not have a name, this is not a namespace
10622 alias. */
10623 attr = dwarf2_attr (die, DW_AT_name, cu);
10624 if (attr != NULL)
10625 {
10626 int num;
10627 struct die_info *d = die;
10628 struct dwarf2_cu *imported_cu = cu;
10629
10630 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10631 keep inspecting DIEs until we hit the underlying import. */
10632 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10633 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10634 {
10635 attr = dwarf2_attr (d, DW_AT_import, cu);
10636 if (attr == NULL)
10637 break;
10638
10639 d = follow_die_ref (d, attr, &imported_cu);
10640 if (d->tag != DW_TAG_imported_declaration)
10641 break;
10642 }
10643
10644 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10645 {
10646 complaint (_("DIE at %s has too many recursively imported "
10647 "declarations"), sect_offset_str (d->sect_off));
10648 return 0;
10649 }
10650
10651 if (attr != NULL)
10652 {
10653 struct type *type;
10654 sect_offset sect_off = attr->get_ref_die_offset ();
10655
10656 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10657 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10658 {
10659 /* This declaration is a global namespace alias. Add
10660 a symbol for it whose type is the aliased namespace. */
10661 new_symbol (die, type, cu);
10662 return 1;
10663 }
10664 }
10665 }
10666
10667 return 0;
10668 }
10669
10670 /* Return the using directives repository (global or local?) to use in the
10671 current context for CU.
10672
10673 For Ada, imported declarations can materialize renamings, which *may* be
10674 global. However it is impossible (for now?) in DWARF to distinguish
10675 "external" imported declarations and "static" ones. As all imported
10676 declarations seem to be static in all other languages, make them all CU-wide
10677 global only in Ada. */
10678
10679 static struct using_direct **
10680 using_directives (struct dwarf2_cu *cu)
10681 {
10682 if (cu->language == language_ada
10683 && cu->get_builder ()->outermost_context_p ())
10684 return cu->get_builder ()->get_global_using_directives ();
10685 else
10686 return cu->get_builder ()->get_local_using_directives ();
10687 }
10688
10689 /* Read the import statement specified by the given die and record it. */
10690
10691 static void
10692 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10693 {
10694 struct objfile *objfile = cu->per_objfile->objfile;
10695 struct attribute *import_attr;
10696 struct die_info *imported_die, *child_die;
10697 struct dwarf2_cu *imported_cu;
10698 const char *imported_name;
10699 const char *imported_name_prefix;
10700 const char *canonical_name;
10701 const char *import_alias;
10702 const char *imported_declaration = NULL;
10703 const char *import_prefix;
10704 std::vector<const char *> excludes;
10705
10706 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10707 if (import_attr == NULL)
10708 {
10709 complaint (_("Tag '%s' has no DW_AT_import"),
10710 dwarf_tag_name (die->tag));
10711 return;
10712 }
10713
10714 imported_cu = cu;
10715 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10716 imported_name = dwarf2_name (imported_die, imported_cu);
10717 if (imported_name == NULL)
10718 {
10719 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10720
10721 The import in the following code:
10722 namespace A
10723 {
10724 typedef int B;
10725 }
10726
10727 int main ()
10728 {
10729 using A::B;
10730 B b;
10731 return b;
10732 }
10733
10734 ...
10735 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10736 <52> DW_AT_decl_file : 1
10737 <53> DW_AT_decl_line : 6
10738 <54> DW_AT_import : <0x75>
10739 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10740 <59> DW_AT_name : B
10741 <5b> DW_AT_decl_file : 1
10742 <5c> DW_AT_decl_line : 2
10743 <5d> DW_AT_type : <0x6e>
10744 ...
10745 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10746 <76> DW_AT_byte_size : 4
10747 <77> DW_AT_encoding : 5 (signed)
10748
10749 imports the wrong die ( 0x75 instead of 0x58 ).
10750 This case will be ignored until the gcc bug is fixed. */
10751 return;
10752 }
10753
10754 /* Figure out the local name after import. */
10755 import_alias = dwarf2_name (die, cu);
10756
10757 /* Figure out where the statement is being imported to. */
10758 import_prefix = determine_prefix (die, cu);
10759
10760 /* Figure out what the scope of the imported die is and prepend it
10761 to the name of the imported die. */
10762 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10763
10764 if (imported_die->tag != DW_TAG_namespace
10765 && imported_die->tag != DW_TAG_module)
10766 {
10767 imported_declaration = imported_name;
10768 canonical_name = imported_name_prefix;
10769 }
10770 else if (strlen (imported_name_prefix) > 0)
10771 canonical_name = obconcat (&objfile->objfile_obstack,
10772 imported_name_prefix,
10773 (cu->language == language_d ? "." : "::"),
10774 imported_name, (char *) NULL);
10775 else
10776 canonical_name = imported_name;
10777
10778 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10779 for (child_die = die->child; child_die && child_die->tag;
10780 child_die = child_die->sibling)
10781 {
10782 /* DWARF-4: A Fortran use statement with a “rename list” may be
10783 represented by an imported module entry with an import attribute
10784 referring to the module and owned entries corresponding to those
10785 entities that are renamed as part of being imported. */
10786
10787 if (child_die->tag != DW_TAG_imported_declaration)
10788 {
10789 complaint (_("child DW_TAG_imported_declaration expected "
10790 "- DIE at %s [in module %s]"),
10791 sect_offset_str (child_die->sect_off),
10792 objfile_name (objfile));
10793 continue;
10794 }
10795
10796 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10797 if (import_attr == NULL)
10798 {
10799 complaint (_("Tag '%s' has no DW_AT_import"),
10800 dwarf_tag_name (child_die->tag));
10801 continue;
10802 }
10803
10804 imported_cu = cu;
10805 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10806 &imported_cu);
10807 imported_name = dwarf2_name (imported_die, imported_cu);
10808 if (imported_name == NULL)
10809 {
10810 complaint (_("child DW_TAG_imported_declaration has unknown "
10811 "imported name - DIE at %s [in module %s]"),
10812 sect_offset_str (child_die->sect_off),
10813 objfile_name (objfile));
10814 continue;
10815 }
10816
10817 excludes.push_back (imported_name);
10818
10819 process_die (child_die, cu);
10820 }
10821
10822 add_using_directive (using_directives (cu),
10823 import_prefix,
10824 canonical_name,
10825 import_alias,
10826 imported_declaration,
10827 excludes,
10828 0,
10829 &objfile->objfile_obstack);
10830 }
10831
10832 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10833 types, but gives them a size of zero. Starting with version 14,
10834 ICC is compatible with GCC. */
10835
10836 static bool
10837 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10838 {
10839 if (!cu->checked_producer)
10840 check_producer (cu);
10841
10842 return cu->producer_is_icc_lt_14;
10843 }
10844
10845 /* ICC generates a DW_AT_type for C void functions. This was observed on
10846 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10847 which says that void functions should not have a DW_AT_type. */
10848
10849 static bool
10850 producer_is_icc (struct dwarf2_cu *cu)
10851 {
10852 if (!cu->checked_producer)
10853 check_producer (cu);
10854
10855 return cu->producer_is_icc;
10856 }
10857
10858 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10859 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10860 this, it was first present in GCC release 4.3.0. */
10861
10862 static bool
10863 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10864 {
10865 if (!cu->checked_producer)
10866 check_producer (cu);
10867
10868 return cu->producer_is_gcc_lt_4_3;
10869 }
10870
10871 static file_and_directory
10872 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10873 {
10874 file_and_directory res;
10875
10876 /* Find the filename. Do not use dwarf2_name here, since the filename
10877 is not a source language identifier. */
10878 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10879 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10880
10881 if (res.comp_dir == NULL
10882 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10883 && IS_ABSOLUTE_PATH (res.name))
10884 {
10885 res.comp_dir_storage = ldirname (res.name);
10886 if (!res.comp_dir_storage.empty ())
10887 res.comp_dir = res.comp_dir_storage.c_str ();
10888 }
10889 if (res.comp_dir != NULL)
10890 {
10891 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10892 directory, get rid of it. */
10893 const char *cp = strchr (res.comp_dir, ':');
10894
10895 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10896 res.comp_dir = cp + 1;
10897 }
10898
10899 if (res.name == NULL)
10900 res.name = "<unknown>";
10901
10902 return res;
10903 }
10904
10905 /* Handle DW_AT_stmt_list for a compilation unit.
10906 DIE is the DW_TAG_compile_unit die for CU.
10907 COMP_DIR is the compilation directory. LOWPC is passed to
10908 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10909
10910 static void
10911 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10912 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10913 {
10914 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10915 struct attribute *attr;
10916 struct line_header line_header_local;
10917 hashval_t line_header_local_hash;
10918 void **slot;
10919 int decode_mapping;
10920
10921 gdb_assert (! cu->per_cu->is_debug_types);
10922
10923 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10924 if (attr == NULL)
10925 return;
10926
10927 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10928
10929 /* The line header hash table is only created if needed (it exists to
10930 prevent redundant reading of the line table for partial_units).
10931 If we're given a partial_unit, we'll need it. If we're given a
10932 compile_unit, then use the line header hash table if it's already
10933 created, but don't create one just yet. */
10934
10935 if (per_objfile->line_header_hash == NULL
10936 && die->tag == DW_TAG_partial_unit)
10937 {
10938 per_objfile->line_header_hash
10939 .reset (htab_create_alloc (127, line_header_hash_voidp,
10940 line_header_eq_voidp,
10941 free_line_header_voidp,
10942 xcalloc, xfree));
10943 }
10944
10945 line_header_local.sect_off = line_offset;
10946 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10947 line_header_local_hash = line_header_hash (&line_header_local);
10948 if (per_objfile->line_header_hash != NULL)
10949 {
10950 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10951 &line_header_local,
10952 line_header_local_hash, NO_INSERT);
10953
10954 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10955 is not present in *SLOT (since if there is something in *SLOT then
10956 it will be for a partial_unit). */
10957 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10958 {
10959 gdb_assert (*slot != NULL);
10960 cu->line_header = (struct line_header *) *slot;
10961 return;
10962 }
10963 }
10964
10965 /* dwarf_decode_line_header does not yet provide sufficient information.
10966 We always have to call also dwarf_decode_lines for it. */
10967 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10968 if (lh == NULL)
10969 return;
10970
10971 cu->line_header = lh.release ();
10972 cu->line_header_die_owner = die;
10973
10974 if (per_objfile->line_header_hash == NULL)
10975 slot = NULL;
10976 else
10977 {
10978 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10979 &line_header_local,
10980 line_header_local_hash, INSERT);
10981 gdb_assert (slot != NULL);
10982 }
10983 if (slot != NULL && *slot == NULL)
10984 {
10985 /* This newly decoded line number information unit will be owned
10986 by line_header_hash hash table. */
10987 *slot = cu->line_header;
10988 cu->line_header_die_owner = NULL;
10989 }
10990 else
10991 {
10992 /* We cannot free any current entry in (*slot) as that struct line_header
10993 may be already used by multiple CUs. Create only temporary decoded
10994 line_header for this CU - it may happen at most once for each line
10995 number information unit. And if we're not using line_header_hash
10996 then this is what we want as well. */
10997 gdb_assert (die->tag != DW_TAG_partial_unit);
10998 }
10999 decode_mapping = (die->tag != DW_TAG_partial_unit);
11000 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11001 decode_mapping);
11002
11003 }
11004
11005 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11006
11007 static void
11008 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11009 {
11010 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11011 struct objfile *objfile = per_objfile->objfile;
11012 struct gdbarch *gdbarch = objfile->arch ();
11013 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11014 CORE_ADDR highpc = ((CORE_ADDR) 0);
11015 struct attribute *attr;
11016 struct die_info *child_die;
11017 CORE_ADDR baseaddr;
11018
11019 prepare_one_comp_unit (cu, die, cu->language);
11020 baseaddr = objfile->text_section_offset ();
11021
11022 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11023
11024 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11025 from finish_block. */
11026 if (lowpc == ((CORE_ADDR) -1))
11027 lowpc = highpc;
11028 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11029
11030 file_and_directory fnd = find_file_and_directory (die, cu);
11031
11032 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11033 standardised yet. As a workaround for the language detection we fall
11034 back to the DW_AT_producer string. */
11035 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11036 cu->language = language_opencl;
11037
11038 /* Similar hack for Go. */
11039 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11040 set_cu_language (DW_LANG_Go, cu);
11041
11042 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11043
11044 /* Decode line number information if present. We do this before
11045 processing child DIEs, so that the line header table is available
11046 for DW_AT_decl_file. */
11047 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11048
11049 /* Process all dies in compilation unit. */
11050 if (die->child != NULL)
11051 {
11052 child_die = die->child;
11053 while (child_die && child_die->tag)
11054 {
11055 process_die (child_die, cu);
11056 child_die = child_die->sibling;
11057 }
11058 }
11059
11060 /* Decode macro information, if present. Dwarf 2 macro information
11061 refers to information in the line number info statement program
11062 header, so we can only read it if we've read the header
11063 successfully. */
11064 attr = dwarf2_attr (die, DW_AT_macros, cu);
11065 if (attr == NULL)
11066 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11067 if (attr && cu->line_header)
11068 {
11069 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11070 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11071
11072 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11073 }
11074 else
11075 {
11076 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11077 if (attr && cu->line_header)
11078 {
11079 unsigned int macro_offset = DW_UNSND (attr);
11080
11081 dwarf_decode_macros (cu, macro_offset, 0);
11082 }
11083 }
11084 }
11085
11086 void
11087 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11088 {
11089 struct type_unit_group *tu_group;
11090 int first_time;
11091 struct attribute *attr;
11092 unsigned int i;
11093 struct signatured_type *sig_type;
11094
11095 gdb_assert (per_cu->is_debug_types);
11096 sig_type = (struct signatured_type *) per_cu;
11097
11098 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11099
11100 /* If we're using .gdb_index (includes -readnow) then
11101 per_cu->type_unit_group may not have been set up yet. */
11102 if (sig_type->type_unit_group == NULL)
11103 sig_type->type_unit_group = get_type_unit_group (this, attr);
11104 tu_group = sig_type->type_unit_group;
11105
11106 /* If we've already processed this stmt_list there's no real need to
11107 do it again, we could fake it and just recreate the part we need
11108 (file name,index -> symtab mapping). If data shows this optimization
11109 is useful we can do it then. */
11110 type_unit_group_unshareable *tug_unshare
11111 = per_objfile->get_type_unit_group_unshareable (tu_group);
11112 first_time = tug_unshare->compunit_symtab == NULL;
11113
11114 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11115 debug info. */
11116 line_header_up lh;
11117 if (attr != NULL)
11118 {
11119 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11120 lh = dwarf_decode_line_header (line_offset, this);
11121 }
11122 if (lh == NULL)
11123 {
11124 if (first_time)
11125 start_symtab ("", NULL, 0);
11126 else
11127 {
11128 gdb_assert (tug_unshare->symtabs == NULL);
11129 gdb_assert (m_builder == nullptr);
11130 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11131 m_builder.reset (new struct buildsym_compunit
11132 (COMPUNIT_OBJFILE (cust), "",
11133 COMPUNIT_DIRNAME (cust),
11134 compunit_language (cust),
11135 0, cust));
11136 list_in_scope = get_builder ()->get_file_symbols ();
11137 }
11138 return;
11139 }
11140
11141 line_header = lh.release ();
11142 line_header_die_owner = die;
11143
11144 if (first_time)
11145 {
11146 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11147
11148 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11149 still initializing it, and our caller (a few levels up)
11150 process_full_type_unit still needs to know if this is the first
11151 time. */
11152
11153 tug_unshare->symtabs
11154 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11155 struct symtab *, line_header->file_names_size ());
11156
11157 auto &file_names = line_header->file_names ();
11158 for (i = 0; i < file_names.size (); ++i)
11159 {
11160 file_entry &fe = file_names[i];
11161 dwarf2_start_subfile (this, fe.name,
11162 fe.include_dir (line_header));
11163 buildsym_compunit *b = get_builder ();
11164 if (b->get_current_subfile ()->symtab == NULL)
11165 {
11166 /* NOTE: start_subfile will recognize when it's been
11167 passed a file it has already seen. So we can't
11168 assume there's a simple mapping from
11169 cu->line_header->file_names to subfiles, plus
11170 cu->line_header->file_names may contain dups. */
11171 b->get_current_subfile ()->symtab
11172 = allocate_symtab (cust, b->get_current_subfile ()->name);
11173 }
11174
11175 fe.symtab = b->get_current_subfile ()->symtab;
11176 tug_unshare->symtabs[i] = fe.symtab;
11177 }
11178 }
11179 else
11180 {
11181 gdb_assert (m_builder == nullptr);
11182 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11183 m_builder.reset (new struct buildsym_compunit
11184 (COMPUNIT_OBJFILE (cust), "",
11185 COMPUNIT_DIRNAME (cust),
11186 compunit_language (cust),
11187 0, cust));
11188 list_in_scope = get_builder ()->get_file_symbols ();
11189
11190 auto &file_names = line_header->file_names ();
11191 for (i = 0; i < file_names.size (); ++i)
11192 {
11193 file_entry &fe = file_names[i];
11194 fe.symtab = tug_unshare->symtabs[i];
11195 }
11196 }
11197
11198 /* The main symtab is allocated last. Type units don't have DW_AT_name
11199 so they don't have a "real" (so to speak) symtab anyway.
11200 There is later code that will assign the main symtab to all symbols
11201 that don't have one. We need to handle the case of a symbol with a
11202 missing symtab (DW_AT_decl_file) anyway. */
11203 }
11204
11205 /* Process DW_TAG_type_unit.
11206 For TUs we want to skip the first top level sibling if it's not the
11207 actual type being defined by this TU. In this case the first top
11208 level sibling is there to provide context only. */
11209
11210 static void
11211 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11212 {
11213 struct die_info *child_die;
11214
11215 prepare_one_comp_unit (cu, die, language_minimal);
11216
11217 /* Initialize (or reinitialize) the machinery for building symtabs.
11218 We do this before processing child DIEs, so that the line header table
11219 is available for DW_AT_decl_file. */
11220 cu->setup_type_unit_groups (die);
11221
11222 if (die->child != NULL)
11223 {
11224 child_die = die->child;
11225 while (child_die && child_die->tag)
11226 {
11227 process_die (child_die, cu);
11228 child_die = child_die->sibling;
11229 }
11230 }
11231 }
11232 \f
11233 /* DWO/DWP files.
11234
11235 http://gcc.gnu.org/wiki/DebugFission
11236 http://gcc.gnu.org/wiki/DebugFissionDWP
11237
11238 To simplify handling of both DWO files ("object" files with the DWARF info)
11239 and DWP files (a file with the DWOs packaged up into one file), we treat
11240 DWP files as having a collection of virtual DWO files. */
11241
11242 static hashval_t
11243 hash_dwo_file (const void *item)
11244 {
11245 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11246 hashval_t hash;
11247
11248 hash = htab_hash_string (dwo_file->dwo_name);
11249 if (dwo_file->comp_dir != NULL)
11250 hash += htab_hash_string (dwo_file->comp_dir);
11251 return hash;
11252 }
11253
11254 static int
11255 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11256 {
11257 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11258 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11259
11260 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11261 return 0;
11262 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11263 return lhs->comp_dir == rhs->comp_dir;
11264 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11265 }
11266
11267 /* Allocate a hash table for DWO files. */
11268
11269 static htab_up
11270 allocate_dwo_file_hash_table ()
11271 {
11272 auto delete_dwo_file = [] (void *item)
11273 {
11274 struct dwo_file *dwo_file = (struct dwo_file *) item;
11275
11276 delete dwo_file;
11277 };
11278
11279 return htab_up (htab_create_alloc (41,
11280 hash_dwo_file,
11281 eq_dwo_file,
11282 delete_dwo_file,
11283 xcalloc, xfree));
11284 }
11285
11286 /* Lookup DWO file DWO_NAME. */
11287
11288 static void **
11289 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11290 const char *dwo_name,
11291 const char *comp_dir)
11292 {
11293 struct dwo_file find_entry;
11294 void **slot;
11295
11296 if (per_objfile->per_bfd->dwo_files == NULL)
11297 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11298
11299 find_entry.dwo_name = dwo_name;
11300 find_entry.comp_dir = comp_dir;
11301 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11302 INSERT);
11303
11304 return slot;
11305 }
11306
11307 static hashval_t
11308 hash_dwo_unit (const void *item)
11309 {
11310 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11311
11312 /* This drops the top 32 bits of the id, but is ok for a hash. */
11313 return dwo_unit->signature;
11314 }
11315
11316 static int
11317 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11318 {
11319 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11320 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11321
11322 /* The signature is assumed to be unique within the DWO file.
11323 So while object file CU dwo_id's always have the value zero,
11324 that's OK, assuming each object file DWO file has only one CU,
11325 and that's the rule for now. */
11326 return lhs->signature == rhs->signature;
11327 }
11328
11329 /* Allocate a hash table for DWO CUs,TUs.
11330 There is one of these tables for each of CUs,TUs for each DWO file. */
11331
11332 static htab_up
11333 allocate_dwo_unit_table ()
11334 {
11335 /* Start out with a pretty small number.
11336 Generally DWO files contain only one CU and maybe some TUs. */
11337 return htab_up (htab_create_alloc (3,
11338 hash_dwo_unit,
11339 eq_dwo_unit,
11340 NULL, xcalloc, xfree));
11341 }
11342
11343 /* die_reader_func for create_dwo_cu. */
11344
11345 static void
11346 create_dwo_cu_reader (const struct die_reader_specs *reader,
11347 const gdb_byte *info_ptr,
11348 struct die_info *comp_unit_die,
11349 struct dwo_file *dwo_file,
11350 struct dwo_unit *dwo_unit)
11351 {
11352 struct dwarf2_cu *cu = reader->cu;
11353 sect_offset sect_off = cu->per_cu->sect_off;
11354 struct dwarf2_section_info *section = cu->per_cu->section;
11355
11356 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11357 if (!signature.has_value ())
11358 {
11359 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11360 " its dwo_id [in module %s]"),
11361 sect_offset_str (sect_off), dwo_file->dwo_name);
11362 return;
11363 }
11364
11365 dwo_unit->dwo_file = dwo_file;
11366 dwo_unit->signature = *signature;
11367 dwo_unit->section = section;
11368 dwo_unit->sect_off = sect_off;
11369 dwo_unit->length = cu->per_cu->length;
11370
11371 if (dwarf_read_debug)
11372 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11373 sect_offset_str (sect_off),
11374 hex_string (dwo_unit->signature));
11375 }
11376
11377 /* Create the dwo_units for the CUs in a DWO_FILE.
11378 Note: This function processes DWO files only, not DWP files. */
11379
11380 static void
11381 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11382 dwarf2_cu *cu, struct dwo_file &dwo_file,
11383 dwarf2_section_info &section, htab_up &cus_htab)
11384 {
11385 struct objfile *objfile = per_objfile->objfile;
11386 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11387 const gdb_byte *info_ptr, *end_ptr;
11388
11389 section.read (objfile);
11390 info_ptr = section.buffer;
11391
11392 if (info_ptr == NULL)
11393 return;
11394
11395 if (dwarf_read_debug)
11396 {
11397 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11398 section.get_name (),
11399 section.get_file_name ());
11400 }
11401
11402 end_ptr = info_ptr + section.size;
11403 while (info_ptr < end_ptr)
11404 {
11405 struct dwarf2_per_cu_data per_cu;
11406 struct dwo_unit read_unit {};
11407 struct dwo_unit *dwo_unit;
11408 void **slot;
11409 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11410
11411 memset (&per_cu, 0, sizeof (per_cu));
11412 per_cu.per_bfd = per_bfd;
11413 per_cu.is_debug_types = 0;
11414 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11415 per_cu.section = &section;
11416
11417 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11418 if (!reader.dummy_p)
11419 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11420 &dwo_file, &read_unit);
11421 info_ptr += per_cu.length;
11422
11423 // If the unit could not be parsed, skip it.
11424 if (read_unit.dwo_file == NULL)
11425 continue;
11426
11427 if (cus_htab == NULL)
11428 cus_htab = allocate_dwo_unit_table ();
11429
11430 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11431 struct dwo_unit);
11432 *dwo_unit = read_unit;
11433 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11434 gdb_assert (slot != NULL);
11435 if (*slot != NULL)
11436 {
11437 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11438 sect_offset dup_sect_off = dup_cu->sect_off;
11439
11440 complaint (_("debug cu entry at offset %s is duplicate to"
11441 " the entry at offset %s, signature %s"),
11442 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11443 hex_string (dwo_unit->signature));
11444 }
11445 *slot = (void *)dwo_unit;
11446 }
11447 }
11448
11449 /* DWP file .debug_{cu,tu}_index section format:
11450 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11451
11452 DWP Version 1:
11453
11454 Both index sections have the same format, and serve to map a 64-bit
11455 signature to a set of section numbers. Each section begins with a header,
11456 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11457 indexes, and a pool of 32-bit section numbers. The index sections will be
11458 aligned at 8-byte boundaries in the file.
11459
11460 The index section header consists of:
11461
11462 V, 32 bit version number
11463 -, 32 bits unused
11464 N, 32 bit number of compilation units or type units in the index
11465 M, 32 bit number of slots in the hash table
11466
11467 Numbers are recorded using the byte order of the application binary.
11468
11469 The hash table begins at offset 16 in the section, and consists of an array
11470 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11471 order of the application binary). Unused slots in the hash table are 0.
11472 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11473
11474 The parallel table begins immediately after the hash table
11475 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11476 array of 32-bit indexes (using the byte order of the application binary),
11477 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11478 table contains a 32-bit index into the pool of section numbers. For unused
11479 hash table slots, the corresponding entry in the parallel table will be 0.
11480
11481 The pool of section numbers begins immediately following the hash table
11482 (at offset 16 + 12 * M from the beginning of the section). The pool of
11483 section numbers consists of an array of 32-bit words (using the byte order
11484 of the application binary). Each item in the array is indexed starting
11485 from 0. The hash table entry provides the index of the first section
11486 number in the set. Additional section numbers in the set follow, and the
11487 set is terminated by a 0 entry (section number 0 is not used in ELF).
11488
11489 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11490 section must be the first entry in the set, and the .debug_abbrev.dwo must
11491 be the second entry. Other members of the set may follow in any order.
11492
11493 ---
11494
11495 DWP Version 2:
11496
11497 DWP Version 2 combines all the .debug_info, etc. sections into one,
11498 and the entries in the index tables are now offsets into these sections.
11499 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11500 section.
11501
11502 Index Section Contents:
11503 Header
11504 Hash Table of Signatures dwp_hash_table.hash_table
11505 Parallel Table of Indices dwp_hash_table.unit_table
11506 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11507 Table of Section Sizes dwp_hash_table.v2.sizes
11508
11509 The index section header consists of:
11510
11511 V, 32 bit version number
11512 L, 32 bit number of columns in the table of section offsets
11513 N, 32 bit number of compilation units or type units in the index
11514 M, 32 bit number of slots in the hash table
11515
11516 Numbers are recorded using the byte order of the application binary.
11517
11518 The hash table has the same format as version 1.
11519 The parallel table of indices has the same format as version 1,
11520 except that the entries are origin-1 indices into the table of sections
11521 offsets and the table of section sizes.
11522
11523 The table of offsets begins immediately following the parallel table
11524 (at offset 16 + 12 * M from the beginning of the section). The table is
11525 a two-dimensional array of 32-bit words (using the byte order of the
11526 application binary), with L columns and N+1 rows, in row-major order.
11527 Each row in the array is indexed starting from 0. The first row provides
11528 a key to the remaining rows: each column in this row provides an identifier
11529 for a debug section, and the offsets in the same column of subsequent rows
11530 refer to that section. The section identifiers are:
11531
11532 DW_SECT_INFO 1 .debug_info.dwo
11533 DW_SECT_TYPES 2 .debug_types.dwo
11534 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11535 DW_SECT_LINE 4 .debug_line.dwo
11536 DW_SECT_LOC 5 .debug_loc.dwo
11537 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11538 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11539 DW_SECT_MACRO 8 .debug_macro.dwo
11540
11541 The offsets provided by the CU and TU index sections are the base offsets
11542 for the contributions made by each CU or TU to the corresponding section
11543 in the package file. Each CU and TU header contains an abbrev_offset
11544 field, used to find the abbreviations table for that CU or TU within the
11545 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11546 be interpreted as relative to the base offset given in the index section.
11547 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11548 should be interpreted as relative to the base offset for .debug_line.dwo,
11549 and offsets into other debug sections obtained from DWARF attributes should
11550 also be interpreted as relative to the corresponding base offset.
11551
11552 The table of sizes begins immediately following the table of offsets.
11553 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11554 with L columns and N rows, in row-major order. Each row in the array is
11555 indexed starting from 1 (row 0 is shared by the two tables).
11556
11557 ---
11558
11559 Hash table lookup is handled the same in version 1 and 2:
11560
11561 We assume that N and M will not exceed 2^32 - 1.
11562 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11563
11564 Given a 64-bit compilation unit signature or a type signature S, an entry
11565 in the hash table is located as follows:
11566
11567 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11568 the low-order k bits all set to 1.
11569
11570 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11571
11572 3) If the hash table entry at index H matches the signature, use that
11573 entry. If the hash table entry at index H is unused (all zeroes),
11574 terminate the search: the signature is not present in the table.
11575
11576 4) Let H = (H + H') modulo M. Repeat at Step 3.
11577
11578 Because M > N and H' and M are relatively prime, the search is guaranteed
11579 to stop at an unused slot or find the match. */
11580
11581 /* Create a hash table to map DWO IDs to their CU/TU entry in
11582 .debug_{info,types}.dwo in DWP_FILE.
11583 Returns NULL if there isn't one.
11584 Note: This function processes DWP files only, not DWO files. */
11585
11586 static struct dwp_hash_table *
11587 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11588 struct dwp_file *dwp_file, int is_debug_types)
11589 {
11590 struct objfile *objfile = per_objfile->objfile;
11591 bfd *dbfd = dwp_file->dbfd.get ();
11592 const gdb_byte *index_ptr, *index_end;
11593 struct dwarf2_section_info *index;
11594 uint32_t version, nr_columns, nr_units, nr_slots;
11595 struct dwp_hash_table *htab;
11596
11597 if (is_debug_types)
11598 index = &dwp_file->sections.tu_index;
11599 else
11600 index = &dwp_file->sections.cu_index;
11601
11602 if (index->empty ())
11603 return NULL;
11604 index->read (objfile);
11605
11606 index_ptr = index->buffer;
11607 index_end = index_ptr + index->size;
11608
11609 version = read_4_bytes (dbfd, index_ptr);
11610 index_ptr += 4;
11611 if (version == 2)
11612 nr_columns = read_4_bytes (dbfd, index_ptr);
11613 else
11614 nr_columns = 0;
11615 index_ptr += 4;
11616 nr_units = read_4_bytes (dbfd, index_ptr);
11617 index_ptr += 4;
11618 nr_slots = read_4_bytes (dbfd, index_ptr);
11619 index_ptr += 4;
11620
11621 if (version != 1 && version != 2)
11622 {
11623 error (_("Dwarf Error: unsupported DWP file version (%s)"
11624 " [in module %s]"),
11625 pulongest (version), dwp_file->name);
11626 }
11627 if (nr_slots != (nr_slots & -nr_slots))
11628 {
11629 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11630 " is not power of 2 [in module %s]"),
11631 pulongest (nr_slots), dwp_file->name);
11632 }
11633
11634 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11635 htab->version = version;
11636 htab->nr_columns = nr_columns;
11637 htab->nr_units = nr_units;
11638 htab->nr_slots = nr_slots;
11639 htab->hash_table = index_ptr;
11640 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11641
11642 /* Exit early if the table is empty. */
11643 if (nr_slots == 0 || nr_units == 0
11644 || (version == 2 && nr_columns == 0))
11645 {
11646 /* All must be zero. */
11647 if (nr_slots != 0 || nr_units != 0
11648 || (version == 2 && nr_columns != 0))
11649 {
11650 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11651 " all zero [in modules %s]"),
11652 dwp_file->name);
11653 }
11654 return htab;
11655 }
11656
11657 if (version == 1)
11658 {
11659 htab->section_pool.v1.indices =
11660 htab->unit_table + sizeof (uint32_t) * nr_slots;
11661 /* It's harder to decide whether the section is too small in v1.
11662 V1 is deprecated anyway so we punt. */
11663 }
11664 else
11665 {
11666 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11667 int *ids = htab->section_pool.v2.section_ids;
11668 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11669 /* Reverse map for error checking. */
11670 int ids_seen[DW_SECT_MAX + 1];
11671 int i;
11672
11673 if (nr_columns < 2)
11674 {
11675 error (_("Dwarf Error: bad DWP hash table, too few columns"
11676 " in section table [in module %s]"),
11677 dwp_file->name);
11678 }
11679 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11680 {
11681 error (_("Dwarf Error: bad DWP hash table, too many columns"
11682 " in section table [in module %s]"),
11683 dwp_file->name);
11684 }
11685 memset (ids, 255, sizeof_ids);
11686 memset (ids_seen, 255, sizeof (ids_seen));
11687 for (i = 0; i < nr_columns; ++i)
11688 {
11689 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11690
11691 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11692 {
11693 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11694 " in section table [in module %s]"),
11695 id, dwp_file->name);
11696 }
11697 if (ids_seen[id] != -1)
11698 {
11699 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11700 " id %d in section table [in module %s]"),
11701 id, dwp_file->name);
11702 }
11703 ids_seen[id] = i;
11704 ids[i] = id;
11705 }
11706 /* Must have exactly one info or types section. */
11707 if (((ids_seen[DW_SECT_INFO] != -1)
11708 + (ids_seen[DW_SECT_TYPES] != -1))
11709 != 1)
11710 {
11711 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11712 " DWO info/types section [in module %s]"),
11713 dwp_file->name);
11714 }
11715 /* Must have an abbrev section. */
11716 if (ids_seen[DW_SECT_ABBREV] == -1)
11717 {
11718 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11719 " section [in module %s]"),
11720 dwp_file->name);
11721 }
11722 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11723 htab->section_pool.v2.sizes =
11724 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11725 * nr_units * nr_columns);
11726 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11727 * nr_units * nr_columns))
11728 > index_end)
11729 {
11730 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11731 " [in module %s]"),
11732 dwp_file->name);
11733 }
11734 }
11735
11736 return htab;
11737 }
11738
11739 /* Update SECTIONS with the data from SECTP.
11740
11741 This function is like the other "locate" section routines that are
11742 passed to bfd_map_over_sections, but in this context the sections to
11743 read comes from the DWP V1 hash table, not the full ELF section table.
11744
11745 The result is non-zero for success, or zero if an error was found. */
11746
11747 static int
11748 locate_v1_virtual_dwo_sections (asection *sectp,
11749 struct virtual_v1_dwo_sections *sections)
11750 {
11751 const struct dwop_section_names *names = &dwop_section_names;
11752
11753 if (section_is_p (sectp->name, &names->abbrev_dwo))
11754 {
11755 /* There can be only one. */
11756 if (sections->abbrev.s.section != NULL)
11757 return 0;
11758 sections->abbrev.s.section = sectp;
11759 sections->abbrev.size = bfd_section_size (sectp);
11760 }
11761 else if (section_is_p (sectp->name, &names->info_dwo)
11762 || section_is_p (sectp->name, &names->types_dwo))
11763 {
11764 /* There can be only one. */
11765 if (sections->info_or_types.s.section != NULL)
11766 return 0;
11767 sections->info_or_types.s.section = sectp;
11768 sections->info_or_types.size = bfd_section_size (sectp);
11769 }
11770 else if (section_is_p (sectp->name, &names->line_dwo))
11771 {
11772 /* There can be only one. */
11773 if (sections->line.s.section != NULL)
11774 return 0;
11775 sections->line.s.section = sectp;
11776 sections->line.size = bfd_section_size (sectp);
11777 }
11778 else if (section_is_p (sectp->name, &names->loc_dwo))
11779 {
11780 /* There can be only one. */
11781 if (sections->loc.s.section != NULL)
11782 return 0;
11783 sections->loc.s.section = sectp;
11784 sections->loc.size = bfd_section_size (sectp);
11785 }
11786 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11787 {
11788 /* There can be only one. */
11789 if (sections->macinfo.s.section != NULL)
11790 return 0;
11791 sections->macinfo.s.section = sectp;
11792 sections->macinfo.size = bfd_section_size (sectp);
11793 }
11794 else if (section_is_p (sectp->name, &names->macro_dwo))
11795 {
11796 /* There can be only one. */
11797 if (sections->macro.s.section != NULL)
11798 return 0;
11799 sections->macro.s.section = sectp;
11800 sections->macro.size = bfd_section_size (sectp);
11801 }
11802 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11803 {
11804 /* There can be only one. */
11805 if (sections->str_offsets.s.section != NULL)
11806 return 0;
11807 sections->str_offsets.s.section = sectp;
11808 sections->str_offsets.size = bfd_section_size (sectp);
11809 }
11810 else
11811 {
11812 /* No other kind of section is valid. */
11813 return 0;
11814 }
11815
11816 return 1;
11817 }
11818
11819 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11820 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11821 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11822 This is for DWP version 1 files. */
11823
11824 static struct dwo_unit *
11825 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11826 struct dwp_file *dwp_file,
11827 uint32_t unit_index,
11828 const char *comp_dir,
11829 ULONGEST signature, int is_debug_types)
11830 {
11831 const struct dwp_hash_table *dwp_htab =
11832 is_debug_types ? dwp_file->tus : dwp_file->cus;
11833 bfd *dbfd = dwp_file->dbfd.get ();
11834 const char *kind = is_debug_types ? "TU" : "CU";
11835 struct dwo_file *dwo_file;
11836 struct dwo_unit *dwo_unit;
11837 struct virtual_v1_dwo_sections sections;
11838 void **dwo_file_slot;
11839 int i;
11840
11841 gdb_assert (dwp_file->version == 1);
11842
11843 if (dwarf_read_debug)
11844 {
11845 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11846 kind,
11847 pulongest (unit_index), hex_string (signature),
11848 dwp_file->name);
11849 }
11850
11851 /* Fetch the sections of this DWO unit.
11852 Put a limit on the number of sections we look for so that bad data
11853 doesn't cause us to loop forever. */
11854
11855 #define MAX_NR_V1_DWO_SECTIONS \
11856 (1 /* .debug_info or .debug_types */ \
11857 + 1 /* .debug_abbrev */ \
11858 + 1 /* .debug_line */ \
11859 + 1 /* .debug_loc */ \
11860 + 1 /* .debug_str_offsets */ \
11861 + 1 /* .debug_macro or .debug_macinfo */ \
11862 + 1 /* trailing zero */)
11863
11864 memset (&sections, 0, sizeof (sections));
11865
11866 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11867 {
11868 asection *sectp;
11869 uint32_t section_nr =
11870 read_4_bytes (dbfd,
11871 dwp_htab->section_pool.v1.indices
11872 + (unit_index + i) * sizeof (uint32_t));
11873
11874 if (section_nr == 0)
11875 break;
11876 if (section_nr >= dwp_file->num_sections)
11877 {
11878 error (_("Dwarf Error: bad DWP hash table, section number too large"
11879 " [in module %s]"),
11880 dwp_file->name);
11881 }
11882
11883 sectp = dwp_file->elf_sections[section_nr];
11884 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11885 {
11886 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11887 " [in module %s]"),
11888 dwp_file->name);
11889 }
11890 }
11891
11892 if (i < 2
11893 || sections.info_or_types.empty ()
11894 || sections.abbrev.empty ())
11895 {
11896 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11897 " [in module %s]"),
11898 dwp_file->name);
11899 }
11900 if (i == MAX_NR_V1_DWO_SECTIONS)
11901 {
11902 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11903 " [in module %s]"),
11904 dwp_file->name);
11905 }
11906
11907 /* It's easier for the rest of the code if we fake a struct dwo_file and
11908 have dwo_unit "live" in that. At least for now.
11909
11910 The DWP file can be made up of a random collection of CUs and TUs.
11911 However, for each CU + set of TUs that came from the same original DWO
11912 file, we can combine them back into a virtual DWO file to save space
11913 (fewer struct dwo_file objects to allocate). Remember that for really
11914 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11915
11916 std::string virtual_dwo_name =
11917 string_printf ("virtual-dwo/%d-%d-%d-%d",
11918 sections.abbrev.get_id (),
11919 sections.line.get_id (),
11920 sections.loc.get_id (),
11921 sections.str_offsets.get_id ());
11922 /* Can we use an existing virtual DWO file? */
11923 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11924 comp_dir);
11925 /* Create one if necessary. */
11926 if (*dwo_file_slot == NULL)
11927 {
11928 if (dwarf_read_debug)
11929 {
11930 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11931 virtual_dwo_name.c_str ());
11932 }
11933 dwo_file = new struct dwo_file;
11934 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11935 dwo_file->comp_dir = comp_dir;
11936 dwo_file->sections.abbrev = sections.abbrev;
11937 dwo_file->sections.line = sections.line;
11938 dwo_file->sections.loc = sections.loc;
11939 dwo_file->sections.macinfo = sections.macinfo;
11940 dwo_file->sections.macro = sections.macro;
11941 dwo_file->sections.str_offsets = sections.str_offsets;
11942 /* The "str" section is global to the entire DWP file. */
11943 dwo_file->sections.str = dwp_file->sections.str;
11944 /* The info or types section is assigned below to dwo_unit,
11945 there's no need to record it in dwo_file.
11946 Also, we can't simply record type sections in dwo_file because
11947 we record a pointer into the vector in dwo_unit. As we collect more
11948 types we'll grow the vector and eventually have to reallocate space
11949 for it, invalidating all copies of pointers into the previous
11950 contents. */
11951 *dwo_file_slot = dwo_file;
11952 }
11953 else
11954 {
11955 if (dwarf_read_debug)
11956 {
11957 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11958 virtual_dwo_name.c_str ());
11959 }
11960 dwo_file = (struct dwo_file *) *dwo_file_slot;
11961 }
11962
11963 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11964 dwo_unit->dwo_file = dwo_file;
11965 dwo_unit->signature = signature;
11966 dwo_unit->section =
11967 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11968 *dwo_unit->section = sections.info_or_types;
11969 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11970
11971 return dwo_unit;
11972 }
11973
11974 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11975 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11976 piece within that section used by a TU/CU, return a virtual section
11977 of just that piece. */
11978
11979 static struct dwarf2_section_info
11980 create_dwp_v2_section (dwarf2_per_objfile *per_objfile,
11981 struct dwarf2_section_info *section,
11982 bfd_size_type offset, bfd_size_type size)
11983 {
11984 struct dwarf2_section_info result;
11985 asection *sectp;
11986
11987 gdb_assert (section != NULL);
11988 gdb_assert (!section->is_virtual);
11989
11990 memset (&result, 0, sizeof (result));
11991 result.s.containing_section = section;
11992 result.is_virtual = true;
11993
11994 if (size == 0)
11995 return result;
11996
11997 sectp = section->get_bfd_section ();
11998
11999 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12000 bounds of the real section. This is a pretty-rare event, so just
12001 flag an error (easier) instead of a warning and trying to cope. */
12002 if (sectp == NULL
12003 || offset + size > bfd_section_size (sectp))
12004 {
12005 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12006 " in section %s [in module %s]"),
12007 sectp ? bfd_section_name (sectp) : "<unknown>",
12008 objfile_name (per_objfile->objfile));
12009 }
12010
12011 result.virtual_offset = offset;
12012 result.size = size;
12013 return result;
12014 }
12015
12016 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12017 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12018 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12019 This is for DWP version 2 files. */
12020
12021 static struct dwo_unit *
12022 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12023 struct dwp_file *dwp_file,
12024 uint32_t unit_index,
12025 const char *comp_dir,
12026 ULONGEST signature, int is_debug_types)
12027 {
12028 const struct dwp_hash_table *dwp_htab =
12029 is_debug_types ? dwp_file->tus : dwp_file->cus;
12030 bfd *dbfd = dwp_file->dbfd.get ();
12031 const char *kind = is_debug_types ? "TU" : "CU";
12032 struct dwo_file *dwo_file;
12033 struct dwo_unit *dwo_unit;
12034 struct virtual_v2_dwo_sections sections;
12035 void **dwo_file_slot;
12036 int i;
12037
12038 gdb_assert (dwp_file->version == 2);
12039
12040 if (dwarf_read_debug)
12041 {
12042 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12043 kind,
12044 pulongest (unit_index), hex_string (signature),
12045 dwp_file->name);
12046 }
12047
12048 /* Fetch the section offsets of this DWO unit. */
12049
12050 memset (&sections, 0, sizeof (sections));
12051
12052 for (i = 0; i < dwp_htab->nr_columns; ++i)
12053 {
12054 uint32_t offset = read_4_bytes (dbfd,
12055 dwp_htab->section_pool.v2.offsets
12056 + (((unit_index - 1) * dwp_htab->nr_columns
12057 + i)
12058 * sizeof (uint32_t)));
12059 uint32_t size = read_4_bytes (dbfd,
12060 dwp_htab->section_pool.v2.sizes
12061 + (((unit_index - 1) * dwp_htab->nr_columns
12062 + i)
12063 * sizeof (uint32_t)));
12064
12065 switch (dwp_htab->section_pool.v2.section_ids[i])
12066 {
12067 case DW_SECT_INFO:
12068 case DW_SECT_TYPES:
12069 sections.info_or_types_offset = offset;
12070 sections.info_or_types_size = size;
12071 break;
12072 case DW_SECT_ABBREV:
12073 sections.abbrev_offset = offset;
12074 sections.abbrev_size = size;
12075 break;
12076 case DW_SECT_LINE:
12077 sections.line_offset = offset;
12078 sections.line_size = size;
12079 break;
12080 case DW_SECT_LOC:
12081 sections.loc_offset = offset;
12082 sections.loc_size = size;
12083 break;
12084 case DW_SECT_STR_OFFSETS:
12085 sections.str_offsets_offset = offset;
12086 sections.str_offsets_size = size;
12087 break;
12088 case DW_SECT_MACINFO:
12089 sections.macinfo_offset = offset;
12090 sections.macinfo_size = size;
12091 break;
12092 case DW_SECT_MACRO:
12093 sections.macro_offset = offset;
12094 sections.macro_size = size;
12095 break;
12096 }
12097 }
12098
12099 /* It's easier for the rest of the code if we fake a struct dwo_file and
12100 have dwo_unit "live" in that. At least for now.
12101
12102 The DWP file can be made up of a random collection of CUs and TUs.
12103 However, for each CU + set of TUs that came from the same original DWO
12104 file, we can combine them back into a virtual DWO file to save space
12105 (fewer struct dwo_file objects to allocate). Remember that for really
12106 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12107
12108 std::string virtual_dwo_name =
12109 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12110 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12111 (long) (sections.line_size ? sections.line_offset : 0),
12112 (long) (sections.loc_size ? sections.loc_offset : 0),
12113 (long) (sections.str_offsets_size
12114 ? sections.str_offsets_offset : 0));
12115 /* Can we use an existing virtual DWO file? */
12116 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12117 comp_dir);
12118 /* Create one if necessary. */
12119 if (*dwo_file_slot == NULL)
12120 {
12121 if (dwarf_read_debug)
12122 {
12123 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12124 virtual_dwo_name.c_str ());
12125 }
12126 dwo_file = new struct dwo_file;
12127 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12128 dwo_file->comp_dir = comp_dir;
12129 dwo_file->sections.abbrev =
12130 create_dwp_v2_section (per_objfile, &dwp_file->sections.abbrev,
12131 sections.abbrev_offset, sections.abbrev_size);
12132 dwo_file->sections.line =
12133 create_dwp_v2_section (per_objfile, &dwp_file->sections.line,
12134 sections.line_offset, sections.line_size);
12135 dwo_file->sections.loc =
12136 create_dwp_v2_section (per_objfile, &dwp_file->sections.loc,
12137 sections.loc_offset, sections.loc_size);
12138 dwo_file->sections.macinfo =
12139 create_dwp_v2_section (per_objfile, &dwp_file->sections.macinfo,
12140 sections.macinfo_offset, sections.macinfo_size);
12141 dwo_file->sections.macro =
12142 create_dwp_v2_section (per_objfile, &dwp_file->sections.macro,
12143 sections.macro_offset, sections.macro_size);
12144 dwo_file->sections.str_offsets =
12145 create_dwp_v2_section (per_objfile,
12146 &dwp_file->sections.str_offsets,
12147 sections.str_offsets_offset,
12148 sections.str_offsets_size);
12149 /* The "str" section is global to the entire DWP file. */
12150 dwo_file->sections.str = dwp_file->sections.str;
12151 /* The info or types section is assigned below to dwo_unit,
12152 there's no need to record it in dwo_file.
12153 Also, we can't simply record type sections in dwo_file because
12154 we record a pointer into the vector in dwo_unit. As we collect more
12155 types we'll grow the vector and eventually have to reallocate space
12156 for it, invalidating all copies of pointers into the previous
12157 contents. */
12158 *dwo_file_slot = dwo_file;
12159 }
12160 else
12161 {
12162 if (dwarf_read_debug)
12163 {
12164 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12165 virtual_dwo_name.c_str ());
12166 }
12167 dwo_file = (struct dwo_file *) *dwo_file_slot;
12168 }
12169
12170 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12171 dwo_unit->dwo_file = dwo_file;
12172 dwo_unit->signature = signature;
12173 dwo_unit->section =
12174 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12175 *dwo_unit->section = create_dwp_v2_section (per_objfile,
12176 is_debug_types
12177 ? &dwp_file->sections.types
12178 : &dwp_file->sections.info,
12179 sections.info_or_types_offset,
12180 sections.info_or_types_size);
12181 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12182
12183 return dwo_unit;
12184 }
12185
12186 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12187 Returns NULL if the signature isn't found. */
12188
12189 static struct dwo_unit *
12190 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12191 struct dwp_file *dwp_file, const char *comp_dir,
12192 ULONGEST signature, int is_debug_types)
12193 {
12194 const struct dwp_hash_table *dwp_htab =
12195 is_debug_types ? dwp_file->tus : dwp_file->cus;
12196 bfd *dbfd = dwp_file->dbfd.get ();
12197 uint32_t mask = dwp_htab->nr_slots - 1;
12198 uint32_t hash = signature & mask;
12199 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12200 unsigned int i;
12201 void **slot;
12202 struct dwo_unit find_dwo_cu;
12203
12204 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12205 find_dwo_cu.signature = signature;
12206 slot = htab_find_slot (is_debug_types
12207 ? dwp_file->loaded_tus.get ()
12208 : dwp_file->loaded_cus.get (),
12209 &find_dwo_cu, INSERT);
12210
12211 if (*slot != NULL)
12212 return (struct dwo_unit *) *slot;
12213
12214 /* Use a for loop so that we don't loop forever on bad debug info. */
12215 for (i = 0; i < dwp_htab->nr_slots; ++i)
12216 {
12217 ULONGEST signature_in_table;
12218
12219 signature_in_table =
12220 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12221 if (signature_in_table == signature)
12222 {
12223 uint32_t unit_index =
12224 read_4_bytes (dbfd,
12225 dwp_htab->unit_table + hash * sizeof (uint32_t));
12226
12227 if (dwp_file->version == 1)
12228 {
12229 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12230 unit_index, comp_dir,
12231 signature, is_debug_types);
12232 }
12233 else
12234 {
12235 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12236 unit_index, comp_dir,
12237 signature, is_debug_types);
12238 }
12239 return (struct dwo_unit *) *slot;
12240 }
12241 if (signature_in_table == 0)
12242 return NULL;
12243 hash = (hash + hash2) & mask;
12244 }
12245
12246 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12247 " [in module %s]"),
12248 dwp_file->name);
12249 }
12250
12251 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12252 Open the file specified by FILE_NAME and hand it off to BFD for
12253 preliminary analysis. Return a newly initialized bfd *, which
12254 includes a canonicalized copy of FILE_NAME.
12255 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12256 SEARCH_CWD is true if the current directory is to be searched.
12257 It will be searched before debug-file-directory.
12258 If successful, the file is added to the bfd include table of the
12259 objfile's bfd (see gdb_bfd_record_inclusion).
12260 If unable to find/open the file, return NULL.
12261 NOTE: This function is derived from symfile_bfd_open. */
12262
12263 static gdb_bfd_ref_ptr
12264 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12265 const char *file_name, int is_dwp, int search_cwd)
12266 {
12267 int desc;
12268 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12269 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12270 to debug_file_directory. */
12271 const char *search_path;
12272 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12273
12274 gdb::unique_xmalloc_ptr<char> search_path_holder;
12275 if (search_cwd)
12276 {
12277 if (*debug_file_directory != '\0')
12278 {
12279 search_path_holder.reset (concat (".", dirname_separator_string,
12280 debug_file_directory,
12281 (char *) NULL));
12282 search_path = search_path_holder.get ();
12283 }
12284 else
12285 search_path = ".";
12286 }
12287 else
12288 search_path = debug_file_directory;
12289
12290 openp_flags flags = OPF_RETURN_REALPATH;
12291 if (is_dwp)
12292 flags |= OPF_SEARCH_IN_PATH;
12293
12294 gdb::unique_xmalloc_ptr<char> absolute_name;
12295 desc = openp (search_path, flags, file_name,
12296 O_RDONLY | O_BINARY, &absolute_name);
12297 if (desc < 0)
12298 return NULL;
12299
12300 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12301 gnutarget, desc));
12302 if (sym_bfd == NULL)
12303 return NULL;
12304 bfd_set_cacheable (sym_bfd.get (), 1);
12305
12306 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12307 return NULL;
12308
12309 /* Success. Record the bfd as having been included by the objfile's bfd.
12310 This is important because things like demangled_names_hash lives in the
12311 objfile's per_bfd space and may have references to things like symbol
12312 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12313 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12314
12315 return sym_bfd;
12316 }
12317
12318 /* Try to open DWO file FILE_NAME.
12319 COMP_DIR is the DW_AT_comp_dir attribute.
12320 The result is the bfd handle of the file.
12321 If there is a problem finding or opening the file, return NULL.
12322 Upon success, the canonicalized path of the file is stored in the bfd,
12323 same as symfile_bfd_open. */
12324
12325 static gdb_bfd_ref_ptr
12326 open_dwo_file (dwarf2_per_objfile *per_objfile,
12327 const char *file_name, const char *comp_dir)
12328 {
12329 if (IS_ABSOLUTE_PATH (file_name))
12330 return try_open_dwop_file (per_objfile, file_name,
12331 0 /*is_dwp*/, 0 /*search_cwd*/);
12332
12333 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12334
12335 if (comp_dir != NULL)
12336 {
12337 gdb::unique_xmalloc_ptr<char> path_to_try
12338 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12339
12340 /* NOTE: If comp_dir is a relative path, this will also try the
12341 search path, which seems useful. */
12342 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12343 0 /*is_dwp*/,
12344 1 /*search_cwd*/));
12345 if (abfd != NULL)
12346 return abfd;
12347 }
12348
12349 /* That didn't work, try debug-file-directory, which, despite its name,
12350 is a list of paths. */
12351
12352 if (*debug_file_directory == '\0')
12353 return NULL;
12354
12355 return try_open_dwop_file (per_objfile, file_name,
12356 0 /*is_dwp*/, 1 /*search_cwd*/);
12357 }
12358
12359 /* This function is mapped across the sections and remembers the offset and
12360 size of each of the DWO debugging sections we are interested in. */
12361
12362 static void
12363 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12364 {
12365 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12366 const struct dwop_section_names *names = &dwop_section_names;
12367
12368 if (section_is_p (sectp->name, &names->abbrev_dwo))
12369 {
12370 dwo_sections->abbrev.s.section = sectp;
12371 dwo_sections->abbrev.size = bfd_section_size (sectp);
12372 }
12373 else if (section_is_p (sectp->name, &names->info_dwo))
12374 {
12375 dwo_sections->info.s.section = sectp;
12376 dwo_sections->info.size = bfd_section_size (sectp);
12377 }
12378 else if (section_is_p (sectp->name, &names->line_dwo))
12379 {
12380 dwo_sections->line.s.section = sectp;
12381 dwo_sections->line.size = bfd_section_size (sectp);
12382 }
12383 else if (section_is_p (sectp->name, &names->loc_dwo))
12384 {
12385 dwo_sections->loc.s.section = sectp;
12386 dwo_sections->loc.size = bfd_section_size (sectp);
12387 }
12388 else if (section_is_p (sectp->name, &names->loclists_dwo))
12389 {
12390 dwo_sections->loclists.s.section = sectp;
12391 dwo_sections->loclists.size = bfd_section_size (sectp);
12392 }
12393 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12394 {
12395 dwo_sections->macinfo.s.section = sectp;
12396 dwo_sections->macinfo.size = bfd_section_size (sectp);
12397 }
12398 else if (section_is_p (sectp->name, &names->macro_dwo))
12399 {
12400 dwo_sections->macro.s.section = sectp;
12401 dwo_sections->macro.size = bfd_section_size (sectp);
12402 }
12403 else if (section_is_p (sectp->name, &names->str_dwo))
12404 {
12405 dwo_sections->str.s.section = sectp;
12406 dwo_sections->str.size = bfd_section_size (sectp);
12407 }
12408 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12409 {
12410 dwo_sections->str_offsets.s.section = sectp;
12411 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12412 }
12413 else if (section_is_p (sectp->name, &names->types_dwo))
12414 {
12415 struct dwarf2_section_info type_section;
12416
12417 memset (&type_section, 0, sizeof (type_section));
12418 type_section.s.section = sectp;
12419 type_section.size = bfd_section_size (sectp);
12420 dwo_sections->types.push_back (type_section);
12421 }
12422 }
12423
12424 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12425 by PER_CU. This is for the non-DWP case.
12426 The result is NULL if DWO_NAME can't be found. */
12427
12428 static struct dwo_file *
12429 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12430 const char *comp_dir)
12431 {
12432 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12433
12434 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12435 if (dbfd == NULL)
12436 {
12437 if (dwarf_read_debug)
12438 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12439 return NULL;
12440 }
12441
12442 dwo_file_up dwo_file (new struct dwo_file);
12443 dwo_file->dwo_name = dwo_name;
12444 dwo_file->comp_dir = comp_dir;
12445 dwo_file->dbfd = std::move (dbfd);
12446
12447 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12448 &dwo_file->sections);
12449
12450 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12451 dwo_file->cus);
12452
12453 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12454 dwo_file->sections.types, dwo_file->tus);
12455
12456 if (dwarf_read_debug)
12457 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12458
12459 return dwo_file.release ();
12460 }
12461
12462 /* This function is mapped across the sections and remembers the offset and
12463 size of each of the DWP debugging sections common to version 1 and 2 that
12464 we are interested in. */
12465
12466 static void
12467 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12468 void *dwp_file_ptr)
12469 {
12470 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12471 const struct dwop_section_names *names = &dwop_section_names;
12472 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12473
12474 /* Record the ELF section number for later lookup: this is what the
12475 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12476 gdb_assert (elf_section_nr < dwp_file->num_sections);
12477 dwp_file->elf_sections[elf_section_nr] = sectp;
12478
12479 /* Look for specific sections that we need. */
12480 if (section_is_p (sectp->name, &names->str_dwo))
12481 {
12482 dwp_file->sections.str.s.section = sectp;
12483 dwp_file->sections.str.size = bfd_section_size (sectp);
12484 }
12485 else if (section_is_p (sectp->name, &names->cu_index))
12486 {
12487 dwp_file->sections.cu_index.s.section = sectp;
12488 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12489 }
12490 else if (section_is_p (sectp->name, &names->tu_index))
12491 {
12492 dwp_file->sections.tu_index.s.section = sectp;
12493 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12494 }
12495 }
12496
12497 /* This function is mapped across the sections and remembers the offset and
12498 size of each of the DWP version 2 debugging sections that we are interested
12499 in. This is split into a separate function because we don't know if we
12500 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12501
12502 static void
12503 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12504 {
12505 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12506 const struct dwop_section_names *names = &dwop_section_names;
12507 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12508
12509 /* Record the ELF section number for later lookup: this is what the
12510 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12511 gdb_assert (elf_section_nr < dwp_file->num_sections);
12512 dwp_file->elf_sections[elf_section_nr] = sectp;
12513
12514 /* Look for specific sections that we need. */
12515 if (section_is_p (sectp->name, &names->abbrev_dwo))
12516 {
12517 dwp_file->sections.abbrev.s.section = sectp;
12518 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12519 }
12520 else if (section_is_p (sectp->name, &names->info_dwo))
12521 {
12522 dwp_file->sections.info.s.section = sectp;
12523 dwp_file->sections.info.size = bfd_section_size (sectp);
12524 }
12525 else if (section_is_p (sectp->name, &names->line_dwo))
12526 {
12527 dwp_file->sections.line.s.section = sectp;
12528 dwp_file->sections.line.size = bfd_section_size (sectp);
12529 }
12530 else if (section_is_p (sectp->name, &names->loc_dwo))
12531 {
12532 dwp_file->sections.loc.s.section = sectp;
12533 dwp_file->sections.loc.size = bfd_section_size (sectp);
12534 }
12535 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12536 {
12537 dwp_file->sections.macinfo.s.section = sectp;
12538 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12539 }
12540 else if (section_is_p (sectp->name, &names->macro_dwo))
12541 {
12542 dwp_file->sections.macro.s.section = sectp;
12543 dwp_file->sections.macro.size = bfd_section_size (sectp);
12544 }
12545 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12546 {
12547 dwp_file->sections.str_offsets.s.section = sectp;
12548 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12549 }
12550 else if (section_is_p (sectp->name, &names->types_dwo))
12551 {
12552 dwp_file->sections.types.s.section = sectp;
12553 dwp_file->sections.types.size = bfd_section_size (sectp);
12554 }
12555 }
12556
12557 /* Hash function for dwp_file loaded CUs/TUs. */
12558
12559 static hashval_t
12560 hash_dwp_loaded_cutus (const void *item)
12561 {
12562 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12563
12564 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12565 return dwo_unit->signature;
12566 }
12567
12568 /* Equality function for dwp_file loaded CUs/TUs. */
12569
12570 static int
12571 eq_dwp_loaded_cutus (const void *a, const void *b)
12572 {
12573 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12574 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12575
12576 return dua->signature == dub->signature;
12577 }
12578
12579 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12580
12581 static htab_up
12582 allocate_dwp_loaded_cutus_table ()
12583 {
12584 return htab_up (htab_create_alloc (3,
12585 hash_dwp_loaded_cutus,
12586 eq_dwp_loaded_cutus,
12587 NULL, xcalloc, xfree));
12588 }
12589
12590 /* Try to open DWP file FILE_NAME.
12591 The result is the bfd handle of the file.
12592 If there is a problem finding or opening the file, return NULL.
12593 Upon success, the canonicalized path of the file is stored in the bfd,
12594 same as symfile_bfd_open. */
12595
12596 static gdb_bfd_ref_ptr
12597 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12598 {
12599 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12600 1 /*is_dwp*/,
12601 1 /*search_cwd*/));
12602 if (abfd != NULL)
12603 return abfd;
12604
12605 /* Work around upstream bug 15652.
12606 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12607 [Whether that's a "bug" is debatable, but it is getting in our way.]
12608 We have no real idea where the dwp file is, because gdb's realpath-ing
12609 of the executable's path may have discarded the needed info.
12610 [IWBN if the dwp file name was recorded in the executable, akin to
12611 .gnu_debuglink, but that doesn't exist yet.]
12612 Strip the directory from FILE_NAME and search again. */
12613 if (*debug_file_directory != '\0')
12614 {
12615 /* Don't implicitly search the current directory here.
12616 If the user wants to search "." to handle this case,
12617 it must be added to debug-file-directory. */
12618 return try_open_dwop_file (per_objfile, lbasename (file_name),
12619 1 /*is_dwp*/,
12620 0 /*search_cwd*/);
12621 }
12622
12623 return NULL;
12624 }
12625
12626 /* Initialize the use of the DWP file for the current objfile.
12627 By convention the name of the DWP file is ${objfile}.dwp.
12628 The result is NULL if it can't be found. */
12629
12630 static std::unique_ptr<struct dwp_file>
12631 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12632 {
12633 struct objfile *objfile = per_objfile->objfile;
12634
12635 /* Try to find first .dwp for the binary file before any symbolic links
12636 resolving. */
12637
12638 /* If the objfile is a debug file, find the name of the real binary
12639 file and get the name of dwp file from there. */
12640 std::string dwp_name;
12641 if (objfile->separate_debug_objfile_backlink != NULL)
12642 {
12643 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12644 const char *backlink_basename = lbasename (backlink->original_name);
12645
12646 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12647 }
12648 else
12649 dwp_name = objfile->original_name;
12650
12651 dwp_name += ".dwp";
12652
12653 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12654 if (dbfd == NULL
12655 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12656 {
12657 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12658 dwp_name = objfile_name (objfile);
12659 dwp_name += ".dwp";
12660 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12661 }
12662
12663 if (dbfd == NULL)
12664 {
12665 if (dwarf_read_debug)
12666 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12667 return std::unique_ptr<dwp_file> ();
12668 }
12669
12670 const char *name = bfd_get_filename (dbfd.get ());
12671 std::unique_ptr<struct dwp_file> dwp_file
12672 (new struct dwp_file (name, std::move (dbfd)));
12673
12674 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12675 dwp_file->elf_sections =
12676 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12677 dwp_file->num_sections, asection *);
12678
12679 bfd_map_over_sections (dwp_file->dbfd.get (),
12680 dwarf2_locate_common_dwp_sections,
12681 dwp_file.get ());
12682
12683 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12684
12685 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12686
12687 /* The DWP file version is stored in the hash table. Oh well. */
12688 if (dwp_file->cus && dwp_file->tus
12689 && dwp_file->cus->version != dwp_file->tus->version)
12690 {
12691 /* Technically speaking, we should try to limp along, but this is
12692 pretty bizarre. We use pulongest here because that's the established
12693 portability solution (e.g, we cannot use %u for uint32_t). */
12694 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12695 " TU version %s [in DWP file %s]"),
12696 pulongest (dwp_file->cus->version),
12697 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12698 }
12699
12700 if (dwp_file->cus)
12701 dwp_file->version = dwp_file->cus->version;
12702 else if (dwp_file->tus)
12703 dwp_file->version = dwp_file->tus->version;
12704 else
12705 dwp_file->version = 2;
12706
12707 if (dwp_file->version == 2)
12708 bfd_map_over_sections (dwp_file->dbfd.get (),
12709 dwarf2_locate_v2_dwp_sections,
12710 dwp_file.get ());
12711
12712 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12713 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12714
12715 if (dwarf_read_debug)
12716 {
12717 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12718 fprintf_unfiltered (gdb_stdlog,
12719 " %s CUs, %s TUs\n",
12720 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12721 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12722 }
12723
12724 return dwp_file;
12725 }
12726
12727 /* Wrapper around open_and_init_dwp_file, only open it once. */
12728
12729 static struct dwp_file *
12730 get_dwp_file (dwarf2_per_objfile *per_objfile)
12731 {
12732 if (!per_objfile->per_bfd->dwp_checked)
12733 {
12734 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12735 per_objfile->per_bfd->dwp_checked = 1;
12736 }
12737 return per_objfile->per_bfd->dwp_file.get ();
12738 }
12739
12740 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12741 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12742 or in the DWP file for the objfile, referenced by THIS_UNIT.
12743 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12744 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12745
12746 This is called, for example, when wanting to read a variable with a
12747 complex location. Therefore we don't want to do file i/o for every call.
12748 Therefore we don't want to look for a DWO file on every call.
12749 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12750 then we check if we've already seen DWO_NAME, and only THEN do we check
12751 for a DWO file.
12752
12753 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12754 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12755
12756 static struct dwo_unit *
12757 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12758 ULONGEST signature, int is_debug_types)
12759 {
12760 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12761 struct objfile *objfile = per_objfile->objfile;
12762 const char *kind = is_debug_types ? "TU" : "CU";
12763 void **dwo_file_slot;
12764 struct dwo_file *dwo_file;
12765 struct dwp_file *dwp_file;
12766
12767 /* First see if there's a DWP file.
12768 If we have a DWP file but didn't find the DWO inside it, don't
12769 look for the original DWO file. It makes gdb behave differently
12770 depending on whether one is debugging in the build tree. */
12771
12772 dwp_file = get_dwp_file (per_objfile);
12773 if (dwp_file != NULL)
12774 {
12775 const struct dwp_hash_table *dwp_htab =
12776 is_debug_types ? dwp_file->tus : dwp_file->cus;
12777
12778 if (dwp_htab != NULL)
12779 {
12780 struct dwo_unit *dwo_cutu =
12781 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12782 is_debug_types);
12783
12784 if (dwo_cutu != NULL)
12785 {
12786 if (dwarf_read_debug)
12787 {
12788 fprintf_unfiltered (gdb_stdlog,
12789 "Virtual DWO %s %s found: @%s\n",
12790 kind, hex_string (signature),
12791 host_address_to_string (dwo_cutu));
12792 }
12793 return dwo_cutu;
12794 }
12795 }
12796 }
12797 else
12798 {
12799 /* No DWP file, look for the DWO file. */
12800
12801 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12802 if (*dwo_file_slot == NULL)
12803 {
12804 /* Read in the file and build a table of the CUs/TUs it contains. */
12805 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12806 }
12807 /* NOTE: This will be NULL if unable to open the file. */
12808 dwo_file = (struct dwo_file *) *dwo_file_slot;
12809
12810 if (dwo_file != NULL)
12811 {
12812 struct dwo_unit *dwo_cutu = NULL;
12813
12814 if (is_debug_types && dwo_file->tus)
12815 {
12816 struct dwo_unit find_dwo_cutu;
12817
12818 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12819 find_dwo_cutu.signature = signature;
12820 dwo_cutu
12821 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12822 &find_dwo_cutu);
12823 }
12824 else if (!is_debug_types && dwo_file->cus)
12825 {
12826 struct dwo_unit find_dwo_cutu;
12827
12828 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12829 find_dwo_cutu.signature = signature;
12830 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12831 &find_dwo_cutu);
12832 }
12833
12834 if (dwo_cutu != NULL)
12835 {
12836 if (dwarf_read_debug)
12837 {
12838 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12839 kind, dwo_name, hex_string (signature),
12840 host_address_to_string (dwo_cutu));
12841 }
12842 return dwo_cutu;
12843 }
12844 }
12845 }
12846
12847 /* We didn't find it. This could mean a dwo_id mismatch, or
12848 someone deleted the DWO/DWP file, or the search path isn't set up
12849 correctly to find the file. */
12850
12851 if (dwarf_read_debug)
12852 {
12853 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12854 kind, dwo_name, hex_string (signature));
12855 }
12856
12857 /* This is a warning and not a complaint because it can be caused by
12858 pilot error (e.g., user accidentally deleting the DWO). */
12859 {
12860 /* Print the name of the DWP file if we looked there, helps the user
12861 better diagnose the problem. */
12862 std::string dwp_text;
12863
12864 if (dwp_file != NULL)
12865 dwp_text = string_printf (" [in DWP file %s]",
12866 lbasename (dwp_file->name));
12867
12868 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12869 " [in module %s]"),
12870 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12871 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12872 }
12873 return NULL;
12874 }
12875
12876 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12877 See lookup_dwo_cutu_unit for details. */
12878
12879 static struct dwo_unit *
12880 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12881 ULONGEST signature)
12882 {
12883 gdb_assert (!cu->per_cu->is_debug_types);
12884
12885 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12886 }
12887
12888 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12889 See lookup_dwo_cutu_unit for details. */
12890
12891 static struct dwo_unit *
12892 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12893 {
12894 gdb_assert (cu->per_cu->is_debug_types);
12895
12896 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12897
12898 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12899 }
12900
12901 /* Traversal function for queue_and_load_all_dwo_tus. */
12902
12903 static int
12904 queue_and_load_dwo_tu (void **slot, void *info)
12905 {
12906 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12907 dwarf2_cu *cu = (dwarf2_cu *) info;
12908 ULONGEST signature = dwo_unit->signature;
12909 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12910
12911 if (sig_type != NULL)
12912 {
12913 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12914
12915 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12916 a real dependency of PER_CU on SIG_TYPE. That is detected later
12917 while processing PER_CU. */
12918 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12919 load_full_type_unit (sig_cu, cu->per_objfile);
12920 cu->per_cu->imported_symtabs_push (sig_cu);
12921 }
12922
12923 return 1;
12924 }
12925
12926 /* Queue all TUs contained in the DWO of CU to be read in.
12927 The DWO may have the only definition of the type, though it may not be
12928 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12929 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12930
12931 static void
12932 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12933 {
12934 struct dwo_unit *dwo_unit;
12935 struct dwo_file *dwo_file;
12936
12937 gdb_assert (cu != nullptr);
12938 gdb_assert (!cu->per_cu->is_debug_types);
12939 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12940
12941 dwo_unit = cu->dwo_unit;
12942 gdb_assert (dwo_unit != NULL);
12943
12944 dwo_file = dwo_unit->dwo_file;
12945 if (dwo_file->tus != NULL)
12946 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12947 }
12948
12949 /* Read in various DIEs. */
12950
12951 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12952 Inherit only the children of the DW_AT_abstract_origin DIE not being
12953 already referenced by DW_AT_abstract_origin from the children of the
12954 current DIE. */
12955
12956 static void
12957 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12958 {
12959 struct die_info *child_die;
12960 sect_offset *offsetp;
12961 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12962 struct die_info *origin_die;
12963 /* Iterator of the ORIGIN_DIE children. */
12964 struct die_info *origin_child_die;
12965 struct attribute *attr;
12966 struct dwarf2_cu *origin_cu;
12967 struct pending **origin_previous_list_in_scope;
12968
12969 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12970 if (!attr)
12971 return;
12972
12973 /* Note that following die references may follow to a die in a
12974 different cu. */
12975
12976 origin_cu = cu;
12977 origin_die = follow_die_ref (die, attr, &origin_cu);
12978
12979 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12980 symbols in. */
12981 origin_previous_list_in_scope = origin_cu->list_in_scope;
12982 origin_cu->list_in_scope = cu->list_in_scope;
12983
12984 if (die->tag != origin_die->tag
12985 && !(die->tag == DW_TAG_inlined_subroutine
12986 && origin_die->tag == DW_TAG_subprogram))
12987 complaint (_("DIE %s and its abstract origin %s have different tags"),
12988 sect_offset_str (die->sect_off),
12989 sect_offset_str (origin_die->sect_off));
12990
12991 std::vector<sect_offset> offsets;
12992
12993 for (child_die = die->child;
12994 child_die && child_die->tag;
12995 child_die = child_die->sibling)
12996 {
12997 struct die_info *child_origin_die;
12998 struct dwarf2_cu *child_origin_cu;
12999
13000 /* We are trying to process concrete instance entries:
13001 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13002 it's not relevant to our analysis here. i.e. detecting DIEs that are
13003 present in the abstract instance but not referenced in the concrete
13004 one. */
13005 if (child_die->tag == DW_TAG_call_site
13006 || child_die->tag == DW_TAG_GNU_call_site)
13007 continue;
13008
13009 /* For each CHILD_DIE, find the corresponding child of
13010 ORIGIN_DIE. If there is more than one layer of
13011 DW_AT_abstract_origin, follow them all; there shouldn't be,
13012 but GCC versions at least through 4.4 generate this (GCC PR
13013 40573). */
13014 child_origin_die = child_die;
13015 child_origin_cu = cu;
13016 while (1)
13017 {
13018 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13019 child_origin_cu);
13020 if (attr == NULL)
13021 break;
13022 child_origin_die = follow_die_ref (child_origin_die, attr,
13023 &child_origin_cu);
13024 }
13025
13026 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13027 counterpart may exist. */
13028 if (child_origin_die != child_die)
13029 {
13030 if (child_die->tag != child_origin_die->tag
13031 && !(child_die->tag == DW_TAG_inlined_subroutine
13032 && child_origin_die->tag == DW_TAG_subprogram))
13033 complaint (_("Child DIE %s and its abstract origin %s have "
13034 "different tags"),
13035 sect_offset_str (child_die->sect_off),
13036 sect_offset_str (child_origin_die->sect_off));
13037 if (child_origin_die->parent != origin_die)
13038 complaint (_("Child DIE %s and its abstract origin %s have "
13039 "different parents"),
13040 sect_offset_str (child_die->sect_off),
13041 sect_offset_str (child_origin_die->sect_off));
13042 else
13043 offsets.push_back (child_origin_die->sect_off);
13044 }
13045 }
13046 std::sort (offsets.begin (), offsets.end ());
13047 sect_offset *offsets_end = offsets.data () + offsets.size ();
13048 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13049 if (offsetp[-1] == *offsetp)
13050 complaint (_("Multiple children of DIE %s refer "
13051 "to DIE %s as their abstract origin"),
13052 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13053
13054 offsetp = offsets.data ();
13055 origin_child_die = origin_die->child;
13056 while (origin_child_die && origin_child_die->tag)
13057 {
13058 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13059 while (offsetp < offsets_end
13060 && *offsetp < origin_child_die->sect_off)
13061 offsetp++;
13062 if (offsetp >= offsets_end
13063 || *offsetp > origin_child_die->sect_off)
13064 {
13065 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13066 Check whether we're already processing ORIGIN_CHILD_DIE.
13067 This can happen with mutually referenced abstract_origins.
13068 PR 16581. */
13069 if (!origin_child_die->in_process)
13070 process_die (origin_child_die, origin_cu);
13071 }
13072 origin_child_die = origin_child_die->sibling;
13073 }
13074 origin_cu->list_in_scope = origin_previous_list_in_scope;
13075
13076 if (cu != origin_cu)
13077 compute_delayed_physnames (origin_cu);
13078 }
13079
13080 static void
13081 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13082 {
13083 struct objfile *objfile = cu->per_objfile->objfile;
13084 struct gdbarch *gdbarch = objfile->arch ();
13085 struct context_stack *newobj;
13086 CORE_ADDR lowpc;
13087 CORE_ADDR highpc;
13088 struct die_info *child_die;
13089 struct attribute *attr, *call_line, *call_file;
13090 const char *name;
13091 CORE_ADDR baseaddr;
13092 struct block *block;
13093 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13094 std::vector<struct symbol *> template_args;
13095 struct template_symbol *templ_func = NULL;
13096
13097 if (inlined_func)
13098 {
13099 /* If we do not have call site information, we can't show the
13100 caller of this inlined function. That's too confusing, so
13101 only use the scope for local variables. */
13102 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13103 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13104 if (call_line == NULL || call_file == NULL)
13105 {
13106 read_lexical_block_scope (die, cu);
13107 return;
13108 }
13109 }
13110
13111 baseaddr = objfile->text_section_offset ();
13112
13113 name = dwarf2_name (die, cu);
13114
13115 /* Ignore functions with missing or empty names. These are actually
13116 illegal according to the DWARF standard. */
13117 if (name == NULL)
13118 {
13119 complaint (_("missing name for subprogram DIE at %s"),
13120 sect_offset_str (die->sect_off));
13121 return;
13122 }
13123
13124 /* Ignore functions with missing or invalid low and high pc attributes. */
13125 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13126 <= PC_BOUNDS_INVALID)
13127 {
13128 attr = dwarf2_attr (die, DW_AT_external, cu);
13129 if (!attr || !DW_UNSND (attr))
13130 complaint (_("cannot get low and high bounds "
13131 "for subprogram DIE at %s"),
13132 sect_offset_str (die->sect_off));
13133 return;
13134 }
13135
13136 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13137 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13138
13139 /* If we have any template arguments, then we must allocate a
13140 different sort of symbol. */
13141 for (child_die = die->child; child_die; child_die = child_die->sibling)
13142 {
13143 if (child_die->tag == DW_TAG_template_type_param
13144 || child_die->tag == DW_TAG_template_value_param)
13145 {
13146 templ_func = new (&objfile->objfile_obstack) template_symbol;
13147 templ_func->subclass = SYMBOL_TEMPLATE;
13148 break;
13149 }
13150 }
13151
13152 newobj = cu->get_builder ()->push_context (0, lowpc);
13153 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13154 (struct symbol *) templ_func);
13155
13156 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13157 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13158 cu->language);
13159
13160 /* If there is a location expression for DW_AT_frame_base, record
13161 it. */
13162 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13163 if (attr != nullptr)
13164 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13165
13166 /* If there is a location for the static link, record it. */
13167 newobj->static_link = NULL;
13168 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13169 if (attr != nullptr)
13170 {
13171 newobj->static_link
13172 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13173 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13174 cu->addr_type ());
13175 }
13176
13177 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13178
13179 if (die->child != NULL)
13180 {
13181 child_die = die->child;
13182 while (child_die && child_die->tag)
13183 {
13184 if (child_die->tag == DW_TAG_template_type_param
13185 || child_die->tag == DW_TAG_template_value_param)
13186 {
13187 struct symbol *arg = new_symbol (child_die, NULL, cu);
13188
13189 if (arg != NULL)
13190 template_args.push_back (arg);
13191 }
13192 else
13193 process_die (child_die, cu);
13194 child_die = child_die->sibling;
13195 }
13196 }
13197
13198 inherit_abstract_dies (die, cu);
13199
13200 /* If we have a DW_AT_specification, we might need to import using
13201 directives from the context of the specification DIE. See the
13202 comment in determine_prefix. */
13203 if (cu->language == language_cplus
13204 && dwarf2_attr (die, DW_AT_specification, cu))
13205 {
13206 struct dwarf2_cu *spec_cu = cu;
13207 struct die_info *spec_die = die_specification (die, &spec_cu);
13208
13209 while (spec_die)
13210 {
13211 child_die = spec_die->child;
13212 while (child_die && child_die->tag)
13213 {
13214 if (child_die->tag == DW_TAG_imported_module)
13215 process_die (child_die, spec_cu);
13216 child_die = child_die->sibling;
13217 }
13218
13219 /* In some cases, GCC generates specification DIEs that
13220 themselves contain DW_AT_specification attributes. */
13221 spec_die = die_specification (spec_die, &spec_cu);
13222 }
13223 }
13224
13225 struct context_stack cstk = cu->get_builder ()->pop_context ();
13226 /* Make a block for the local symbols within. */
13227 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13228 cstk.static_link, lowpc, highpc);
13229
13230 /* For C++, set the block's scope. */
13231 if ((cu->language == language_cplus
13232 || cu->language == language_fortran
13233 || cu->language == language_d
13234 || cu->language == language_rust)
13235 && cu->processing_has_namespace_info)
13236 block_set_scope (block, determine_prefix (die, cu),
13237 &objfile->objfile_obstack);
13238
13239 /* If we have address ranges, record them. */
13240 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13241
13242 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13243
13244 /* Attach template arguments to function. */
13245 if (!template_args.empty ())
13246 {
13247 gdb_assert (templ_func != NULL);
13248
13249 templ_func->n_template_arguments = template_args.size ();
13250 templ_func->template_arguments
13251 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13252 templ_func->n_template_arguments);
13253 memcpy (templ_func->template_arguments,
13254 template_args.data (),
13255 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13256
13257 /* Make sure that the symtab is set on the new symbols. Even
13258 though they don't appear in this symtab directly, other parts
13259 of gdb assume that symbols do, and this is reasonably
13260 true. */
13261 for (symbol *sym : template_args)
13262 symbol_set_symtab (sym, symbol_symtab (templ_func));
13263 }
13264
13265 /* In C++, we can have functions nested inside functions (e.g., when
13266 a function declares a class that has methods). This means that
13267 when we finish processing a function scope, we may need to go
13268 back to building a containing block's symbol lists. */
13269 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13270 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13271
13272 /* If we've finished processing a top-level function, subsequent
13273 symbols go in the file symbol list. */
13274 if (cu->get_builder ()->outermost_context_p ())
13275 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13276 }
13277
13278 /* Process all the DIES contained within a lexical block scope. Start
13279 a new scope, process the dies, and then close the scope. */
13280
13281 static void
13282 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13283 {
13284 struct objfile *objfile = cu->per_objfile->objfile;
13285 struct gdbarch *gdbarch = objfile->arch ();
13286 CORE_ADDR lowpc, highpc;
13287 struct die_info *child_die;
13288 CORE_ADDR baseaddr;
13289
13290 baseaddr = objfile->text_section_offset ();
13291
13292 /* Ignore blocks with missing or invalid low and high pc attributes. */
13293 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13294 as multiple lexical blocks? Handling children in a sane way would
13295 be nasty. Might be easier to properly extend generic blocks to
13296 describe ranges. */
13297 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13298 {
13299 case PC_BOUNDS_NOT_PRESENT:
13300 /* DW_TAG_lexical_block has no attributes, process its children as if
13301 there was no wrapping by that DW_TAG_lexical_block.
13302 GCC does no longer produces such DWARF since GCC r224161. */
13303 for (child_die = die->child;
13304 child_die != NULL && child_die->tag;
13305 child_die = child_die->sibling)
13306 {
13307 /* We might already be processing this DIE. This can happen
13308 in an unusual circumstance -- where a subroutine A
13309 appears lexically in another subroutine B, but A actually
13310 inlines B. The recursion is broken here, rather than in
13311 inherit_abstract_dies, because it seems better to simply
13312 drop concrete children here. */
13313 if (!child_die->in_process)
13314 process_die (child_die, cu);
13315 }
13316 return;
13317 case PC_BOUNDS_INVALID:
13318 return;
13319 }
13320 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13321 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13322
13323 cu->get_builder ()->push_context (0, lowpc);
13324 if (die->child != NULL)
13325 {
13326 child_die = die->child;
13327 while (child_die && child_die->tag)
13328 {
13329 process_die (child_die, cu);
13330 child_die = child_die->sibling;
13331 }
13332 }
13333 inherit_abstract_dies (die, cu);
13334 struct context_stack cstk = cu->get_builder ()->pop_context ();
13335
13336 if (*cu->get_builder ()->get_local_symbols () != NULL
13337 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13338 {
13339 struct block *block
13340 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13341 cstk.start_addr, highpc);
13342
13343 /* Note that recording ranges after traversing children, as we
13344 do here, means that recording a parent's ranges entails
13345 walking across all its children's ranges as they appear in
13346 the address map, which is quadratic behavior.
13347
13348 It would be nicer to record the parent's ranges before
13349 traversing its children, simply overriding whatever you find
13350 there. But since we don't even decide whether to create a
13351 block until after we've traversed its children, that's hard
13352 to do. */
13353 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13354 }
13355 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13356 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13357 }
13358
13359 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13360
13361 static void
13362 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13363 {
13364 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13365 struct objfile *objfile = per_objfile->objfile;
13366 struct gdbarch *gdbarch = objfile->arch ();
13367 CORE_ADDR pc, baseaddr;
13368 struct attribute *attr;
13369 struct call_site *call_site, call_site_local;
13370 void **slot;
13371 int nparams;
13372 struct die_info *child_die;
13373
13374 baseaddr = objfile->text_section_offset ();
13375
13376 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13377 if (attr == NULL)
13378 {
13379 /* This was a pre-DWARF-5 GNU extension alias
13380 for DW_AT_call_return_pc. */
13381 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13382 }
13383 if (!attr)
13384 {
13385 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13386 "DIE %s [in module %s]"),
13387 sect_offset_str (die->sect_off), objfile_name (objfile));
13388 return;
13389 }
13390 pc = attr->value_as_address () + baseaddr;
13391 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13392
13393 if (cu->call_site_htab == NULL)
13394 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13395 NULL, &objfile->objfile_obstack,
13396 hashtab_obstack_allocate, NULL);
13397 call_site_local.pc = pc;
13398 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13399 if (*slot != NULL)
13400 {
13401 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13402 "DIE %s [in module %s]"),
13403 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13404 objfile_name (objfile));
13405 return;
13406 }
13407
13408 /* Count parameters at the caller. */
13409
13410 nparams = 0;
13411 for (child_die = die->child; child_die && child_die->tag;
13412 child_die = child_die->sibling)
13413 {
13414 if (child_die->tag != DW_TAG_call_site_parameter
13415 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13416 {
13417 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13418 "DW_TAG_call_site child DIE %s [in module %s]"),
13419 child_die->tag, sect_offset_str (child_die->sect_off),
13420 objfile_name (objfile));
13421 continue;
13422 }
13423
13424 nparams++;
13425 }
13426
13427 call_site
13428 = ((struct call_site *)
13429 obstack_alloc (&objfile->objfile_obstack,
13430 sizeof (*call_site)
13431 + (sizeof (*call_site->parameter) * (nparams - 1))));
13432 *slot = call_site;
13433 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13434 call_site->pc = pc;
13435
13436 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13437 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13438 {
13439 struct die_info *func_die;
13440
13441 /* Skip also over DW_TAG_inlined_subroutine. */
13442 for (func_die = die->parent;
13443 func_die && func_die->tag != DW_TAG_subprogram
13444 && func_die->tag != DW_TAG_subroutine_type;
13445 func_die = func_die->parent);
13446
13447 /* DW_AT_call_all_calls is a superset
13448 of DW_AT_call_all_tail_calls. */
13449 if (func_die
13450 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13451 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13452 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13453 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13454 {
13455 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13456 not complete. But keep CALL_SITE for look ups via call_site_htab,
13457 both the initial caller containing the real return address PC and
13458 the final callee containing the current PC of a chain of tail
13459 calls do not need to have the tail call list complete. But any
13460 function candidate for a virtual tail call frame searched via
13461 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13462 determined unambiguously. */
13463 }
13464 else
13465 {
13466 struct type *func_type = NULL;
13467
13468 if (func_die)
13469 func_type = get_die_type (func_die, cu);
13470 if (func_type != NULL)
13471 {
13472 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13473
13474 /* Enlist this call site to the function. */
13475 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13476 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13477 }
13478 else
13479 complaint (_("Cannot find function owning DW_TAG_call_site "
13480 "DIE %s [in module %s]"),
13481 sect_offset_str (die->sect_off), objfile_name (objfile));
13482 }
13483 }
13484
13485 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13486 if (attr == NULL)
13487 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13488 if (attr == NULL)
13489 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13490 if (attr == NULL)
13491 {
13492 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13493 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13494 }
13495 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13496 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13497 /* Keep NULL DWARF_BLOCK. */;
13498 else if (attr->form_is_block ())
13499 {
13500 struct dwarf2_locexpr_baton *dlbaton;
13501
13502 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13503 dlbaton->data = DW_BLOCK (attr)->data;
13504 dlbaton->size = DW_BLOCK (attr)->size;
13505 dlbaton->per_objfile = per_objfile;
13506 dlbaton->per_cu = cu->per_cu;
13507
13508 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13509 }
13510 else if (attr->form_is_ref ())
13511 {
13512 struct dwarf2_cu *target_cu = cu;
13513 struct die_info *target_die;
13514
13515 target_die = follow_die_ref (die, attr, &target_cu);
13516 gdb_assert (target_cu->per_objfile->objfile == objfile);
13517 if (die_is_declaration (target_die, target_cu))
13518 {
13519 const char *target_physname;
13520
13521 /* Prefer the mangled name; otherwise compute the demangled one. */
13522 target_physname = dw2_linkage_name (target_die, target_cu);
13523 if (target_physname == NULL)
13524 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13525 if (target_physname == NULL)
13526 complaint (_("DW_AT_call_target target DIE has invalid "
13527 "physname, for referencing DIE %s [in module %s]"),
13528 sect_offset_str (die->sect_off), objfile_name (objfile));
13529 else
13530 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13531 }
13532 else
13533 {
13534 CORE_ADDR lowpc;
13535
13536 /* DW_AT_entry_pc should be preferred. */
13537 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13538 <= PC_BOUNDS_INVALID)
13539 complaint (_("DW_AT_call_target target DIE has invalid "
13540 "low pc, for referencing DIE %s [in module %s]"),
13541 sect_offset_str (die->sect_off), objfile_name (objfile));
13542 else
13543 {
13544 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13545 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13546 }
13547 }
13548 }
13549 else
13550 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13551 "block nor reference, for DIE %s [in module %s]"),
13552 sect_offset_str (die->sect_off), objfile_name (objfile));
13553
13554 call_site->per_cu = cu->per_cu;
13555 call_site->per_objfile = per_objfile;
13556
13557 for (child_die = die->child;
13558 child_die && child_die->tag;
13559 child_die = child_die->sibling)
13560 {
13561 struct call_site_parameter *parameter;
13562 struct attribute *loc, *origin;
13563
13564 if (child_die->tag != DW_TAG_call_site_parameter
13565 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13566 {
13567 /* Already printed the complaint above. */
13568 continue;
13569 }
13570
13571 gdb_assert (call_site->parameter_count < nparams);
13572 parameter = &call_site->parameter[call_site->parameter_count];
13573
13574 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13575 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13576 register is contained in DW_AT_call_value. */
13577
13578 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13579 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13580 if (origin == NULL)
13581 {
13582 /* This was a pre-DWARF-5 GNU extension alias
13583 for DW_AT_call_parameter. */
13584 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13585 }
13586 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13587 {
13588 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13589
13590 sect_offset sect_off = origin->get_ref_die_offset ();
13591 if (!cu->header.offset_in_cu_p (sect_off))
13592 {
13593 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13594 binding can be done only inside one CU. Such referenced DIE
13595 therefore cannot be even moved to DW_TAG_partial_unit. */
13596 complaint (_("DW_AT_call_parameter offset is not in CU for "
13597 "DW_TAG_call_site child DIE %s [in module %s]"),
13598 sect_offset_str (child_die->sect_off),
13599 objfile_name (objfile));
13600 continue;
13601 }
13602 parameter->u.param_cu_off
13603 = (cu_offset) (sect_off - cu->header.sect_off);
13604 }
13605 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13606 {
13607 complaint (_("No DW_FORM_block* DW_AT_location for "
13608 "DW_TAG_call_site child DIE %s [in module %s]"),
13609 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13610 continue;
13611 }
13612 else
13613 {
13614 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13615 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13616 if (parameter->u.dwarf_reg != -1)
13617 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13618 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13619 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13620 &parameter->u.fb_offset))
13621 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13622 else
13623 {
13624 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13625 "for DW_FORM_block* DW_AT_location is supported for "
13626 "DW_TAG_call_site child DIE %s "
13627 "[in module %s]"),
13628 sect_offset_str (child_die->sect_off),
13629 objfile_name (objfile));
13630 continue;
13631 }
13632 }
13633
13634 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13635 if (attr == NULL)
13636 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13637 if (attr == NULL || !attr->form_is_block ())
13638 {
13639 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13640 "DW_TAG_call_site child DIE %s [in module %s]"),
13641 sect_offset_str (child_die->sect_off),
13642 objfile_name (objfile));
13643 continue;
13644 }
13645 parameter->value = DW_BLOCK (attr)->data;
13646 parameter->value_size = DW_BLOCK (attr)->size;
13647
13648 /* Parameters are not pre-cleared by memset above. */
13649 parameter->data_value = NULL;
13650 parameter->data_value_size = 0;
13651 call_site->parameter_count++;
13652
13653 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13654 if (attr == NULL)
13655 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13656 if (attr != nullptr)
13657 {
13658 if (!attr->form_is_block ())
13659 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13660 "DW_TAG_call_site child DIE %s [in module %s]"),
13661 sect_offset_str (child_die->sect_off),
13662 objfile_name (objfile));
13663 else
13664 {
13665 parameter->data_value = DW_BLOCK (attr)->data;
13666 parameter->data_value_size = DW_BLOCK (attr)->size;
13667 }
13668 }
13669 }
13670 }
13671
13672 /* Helper function for read_variable. If DIE represents a virtual
13673 table, then return the type of the concrete object that is
13674 associated with the virtual table. Otherwise, return NULL. */
13675
13676 static struct type *
13677 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13678 {
13679 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13680 if (attr == NULL)
13681 return NULL;
13682
13683 /* Find the type DIE. */
13684 struct die_info *type_die = NULL;
13685 struct dwarf2_cu *type_cu = cu;
13686
13687 if (attr->form_is_ref ())
13688 type_die = follow_die_ref (die, attr, &type_cu);
13689 if (type_die == NULL)
13690 return NULL;
13691
13692 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13693 return NULL;
13694 return die_containing_type (type_die, type_cu);
13695 }
13696
13697 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13698
13699 static void
13700 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13701 {
13702 struct rust_vtable_symbol *storage = NULL;
13703
13704 if (cu->language == language_rust)
13705 {
13706 struct type *containing_type = rust_containing_type (die, cu);
13707
13708 if (containing_type != NULL)
13709 {
13710 struct objfile *objfile = cu->per_objfile->objfile;
13711
13712 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13713 storage->concrete_type = containing_type;
13714 storage->subclass = SYMBOL_RUST_VTABLE;
13715 }
13716 }
13717
13718 struct symbol *res = new_symbol (die, NULL, cu, storage);
13719 struct attribute *abstract_origin
13720 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13721 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13722 if (res == NULL && loc && abstract_origin)
13723 {
13724 /* We have a variable without a name, but with a location and an abstract
13725 origin. This may be a concrete instance of an abstract variable
13726 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13727 later. */
13728 struct dwarf2_cu *origin_cu = cu;
13729 struct die_info *origin_die
13730 = follow_die_ref (die, abstract_origin, &origin_cu);
13731 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13732 per_objfile->per_bfd->abstract_to_concrete
13733 [origin_die->sect_off].push_back (die->sect_off);
13734 }
13735 }
13736
13737 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13738 reading .debug_rnglists.
13739 Callback's type should be:
13740 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13741 Return true if the attributes are present and valid, otherwise,
13742 return false. */
13743
13744 template <typename Callback>
13745 static bool
13746 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13747 Callback &&callback)
13748 {
13749 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13750 struct objfile *objfile = per_objfile->objfile;
13751 bfd *obfd = objfile->obfd;
13752 /* Base address selection entry. */
13753 gdb::optional<CORE_ADDR> base;
13754 const gdb_byte *buffer;
13755 CORE_ADDR baseaddr;
13756 bool overflow = false;
13757
13758 base = cu->base_address;
13759
13760 per_objfile->per_bfd->rnglists.read (objfile);
13761 if (offset >= per_objfile->per_bfd->rnglists.size)
13762 {
13763 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13764 offset);
13765 return false;
13766 }
13767 buffer = per_objfile->per_bfd->rnglists.buffer + offset;
13768
13769 baseaddr = objfile->text_section_offset ();
13770
13771 while (1)
13772 {
13773 /* Initialize it due to a false compiler warning. */
13774 CORE_ADDR range_beginning = 0, range_end = 0;
13775 const gdb_byte *buf_end = (per_objfile->per_bfd->rnglists.buffer
13776 + per_objfile->per_bfd->rnglists.size);
13777 unsigned int bytes_read;
13778
13779 if (buffer == buf_end)
13780 {
13781 overflow = true;
13782 break;
13783 }
13784 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13785 switch (rlet)
13786 {
13787 case DW_RLE_end_of_list:
13788 break;
13789 case DW_RLE_base_address:
13790 if (buffer + cu->header.addr_size > buf_end)
13791 {
13792 overflow = true;
13793 break;
13794 }
13795 base = cu->header.read_address (obfd, buffer, &bytes_read);
13796 buffer += bytes_read;
13797 break;
13798 case DW_RLE_start_length:
13799 if (buffer + cu->header.addr_size > buf_end)
13800 {
13801 overflow = true;
13802 break;
13803 }
13804 range_beginning = cu->header.read_address (obfd, buffer,
13805 &bytes_read);
13806 buffer += bytes_read;
13807 range_end = (range_beginning
13808 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13809 buffer += bytes_read;
13810 if (buffer > buf_end)
13811 {
13812 overflow = true;
13813 break;
13814 }
13815 break;
13816 case DW_RLE_offset_pair:
13817 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13818 buffer += bytes_read;
13819 if (buffer > buf_end)
13820 {
13821 overflow = true;
13822 break;
13823 }
13824 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13825 buffer += bytes_read;
13826 if (buffer > buf_end)
13827 {
13828 overflow = true;
13829 break;
13830 }
13831 break;
13832 case DW_RLE_start_end:
13833 if (buffer + 2 * cu->header.addr_size > buf_end)
13834 {
13835 overflow = true;
13836 break;
13837 }
13838 range_beginning = cu->header.read_address (obfd, buffer,
13839 &bytes_read);
13840 buffer += bytes_read;
13841 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13842 buffer += bytes_read;
13843 break;
13844 default:
13845 complaint (_("Invalid .debug_rnglists data (no base address)"));
13846 return false;
13847 }
13848 if (rlet == DW_RLE_end_of_list || overflow)
13849 break;
13850 if (rlet == DW_RLE_base_address)
13851 continue;
13852
13853 if (!base.has_value ())
13854 {
13855 /* We have no valid base address for the ranges
13856 data. */
13857 complaint (_("Invalid .debug_rnglists data (no base address)"));
13858 return false;
13859 }
13860
13861 if (range_beginning > range_end)
13862 {
13863 /* Inverted range entries are invalid. */
13864 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13865 return false;
13866 }
13867
13868 /* Empty range entries have no effect. */
13869 if (range_beginning == range_end)
13870 continue;
13871
13872 range_beginning += *base;
13873 range_end += *base;
13874
13875 /* A not-uncommon case of bad debug info.
13876 Don't pollute the addrmap with bad data. */
13877 if (range_beginning + baseaddr == 0
13878 && !per_objfile->per_bfd->has_section_at_zero)
13879 {
13880 complaint (_(".debug_rnglists entry has start address of zero"
13881 " [in module %s]"), objfile_name (objfile));
13882 continue;
13883 }
13884
13885 callback (range_beginning, range_end);
13886 }
13887
13888 if (overflow)
13889 {
13890 complaint (_("Offset %d is not terminated "
13891 "for DW_AT_ranges attribute"),
13892 offset);
13893 return false;
13894 }
13895
13896 return true;
13897 }
13898
13899 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13900 Callback's type should be:
13901 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13902 Return 1 if the attributes are present and valid, otherwise, return 0. */
13903
13904 template <typename Callback>
13905 static int
13906 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13907 Callback &&callback)
13908 {
13909 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13910 struct objfile *objfile = per_objfile->objfile;
13911 struct comp_unit_head *cu_header = &cu->header;
13912 bfd *obfd = objfile->obfd;
13913 unsigned int addr_size = cu_header->addr_size;
13914 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13915 /* Base address selection entry. */
13916 gdb::optional<CORE_ADDR> base;
13917 unsigned int dummy;
13918 const gdb_byte *buffer;
13919 CORE_ADDR baseaddr;
13920
13921 if (cu_header->version >= 5)
13922 return dwarf2_rnglists_process (offset, cu, callback);
13923
13924 base = cu->base_address;
13925
13926 per_objfile->per_bfd->ranges.read (objfile);
13927 if (offset >= per_objfile->per_bfd->ranges.size)
13928 {
13929 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13930 offset);
13931 return 0;
13932 }
13933 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13934
13935 baseaddr = objfile->text_section_offset ();
13936
13937 while (1)
13938 {
13939 CORE_ADDR range_beginning, range_end;
13940
13941 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13942 buffer += addr_size;
13943 range_end = cu->header.read_address (obfd, buffer, &dummy);
13944 buffer += addr_size;
13945 offset += 2 * addr_size;
13946
13947 /* An end of list marker is a pair of zero addresses. */
13948 if (range_beginning == 0 && range_end == 0)
13949 /* Found the end of list entry. */
13950 break;
13951
13952 /* Each base address selection entry is a pair of 2 values.
13953 The first is the largest possible address, the second is
13954 the base address. Check for a base address here. */
13955 if ((range_beginning & mask) == mask)
13956 {
13957 /* If we found the largest possible address, then we already
13958 have the base address in range_end. */
13959 base = range_end;
13960 continue;
13961 }
13962
13963 if (!base.has_value ())
13964 {
13965 /* We have no valid base address for the ranges
13966 data. */
13967 complaint (_("Invalid .debug_ranges data (no base address)"));
13968 return 0;
13969 }
13970
13971 if (range_beginning > range_end)
13972 {
13973 /* Inverted range entries are invalid. */
13974 complaint (_("Invalid .debug_ranges data (inverted range)"));
13975 return 0;
13976 }
13977
13978 /* Empty range entries have no effect. */
13979 if (range_beginning == range_end)
13980 continue;
13981
13982 range_beginning += *base;
13983 range_end += *base;
13984
13985 /* A not-uncommon case of bad debug info.
13986 Don't pollute the addrmap with bad data. */
13987 if (range_beginning + baseaddr == 0
13988 && !per_objfile->per_bfd->has_section_at_zero)
13989 {
13990 complaint (_(".debug_ranges entry has start address of zero"
13991 " [in module %s]"), objfile_name (objfile));
13992 continue;
13993 }
13994
13995 callback (range_beginning, range_end);
13996 }
13997
13998 return 1;
13999 }
14000
14001 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14002 Return 1 if the attributes are present and valid, otherwise, return 0.
14003 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14004
14005 static int
14006 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14007 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14008 dwarf2_psymtab *ranges_pst)
14009 {
14010 struct objfile *objfile = cu->per_objfile->objfile;
14011 struct gdbarch *gdbarch = objfile->arch ();
14012 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14013 int low_set = 0;
14014 CORE_ADDR low = 0;
14015 CORE_ADDR high = 0;
14016 int retval;
14017
14018 retval = dwarf2_ranges_process (offset, cu,
14019 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14020 {
14021 if (ranges_pst != NULL)
14022 {
14023 CORE_ADDR lowpc;
14024 CORE_ADDR highpc;
14025
14026 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14027 range_beginning + baseaddr)
14028 - baseaddr);
14029 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14030 range_end + baseaddr)
14031 - baseaddr);
14032 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14033 lowpc, highpc - 1, ranges_pst);
14034 }
14035
14036 /* FIXME: This is recording everything as a low-high
14037 segment of consecutive addresses. We should have a
14038 data structure for discontiguous block ranges
14039 instead. */
14040 if (! low_set)
14041 {
14042 low = range_beginning;
14043 high = range_end;
14044 low_set = 1;
14045 }
14046 else
14047 {
14048 if (range_beginning < low)
14049 low = range_beginning;
14050 if (range_end > high)
14051 high = range_end;
14052 }
14053 });
14054 if (!retval)
14055 return 0;
14056
14057 if (! low_set)
14058 /* If the first entry is an end-of-list marker, the range
14059 describes an empty scope, i.e. no instructions. */
14060 return 0;
14061
14062 if (low_return)
14063 *low_return = low;
14064 if (high_return)
14065 *high_return = high;
14066 return 1;
14067 }
14068
14069 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14070 definition for the return value. *LOWPC and *HIGHPC are set iff
14071 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14072
14073 static enum pc_bounds_kind
14074 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14075 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14076 dwarf2_psymtab *pst)
14077 {
14078 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14079 struct attribute *attr;
14080 struct attribute *attr_high;
14081 CORE_ADDR low = 0;
14082 CORE_ADDR high = 0;
14083 enum pc_bounds_kind ret;
14084
14085 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14086 if (attr_high)
14087 {
14088 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14089 if (attr != nullptr)
14090 {
14091 low = attr->value_as_address ();
14092 high = attr_high->value_as_address ();
14093 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14094 high += low;
14095 }
14096 else
14097 /* Found high w/o low attribute. */
14098 return PC_BOUNDS_INVALID;
14099
14100 /* Found consecutive range of addresses. */
14101 ret = PC_BOUNDS_HIGH_LOW;
14102 }
14103 else
14104 {
14105 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14106 if (attr != NULL)
14107 {
14108 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14109 We take advantage of the fact that DW_AT_ranges does not appear
14110 in DW_TAG_compile_unit of DWO files. */
14111 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14112 unsigned int ranges_offset = (DW_UNSND (attr)
14113 + (need_ranges_base
14114 ? cu->ranges_base
14115 : 0));
14116
14117 /* Value of the DW_AT_ranges attribute is the offset in the
14118 .debug_ranges section. */
14119 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14120 return PC_BOUNDS_INVALID;
14121 /* Found discontinuous range of addresses. */
14122 ret = PC_BOUNDS_RANGES;
14123 }
14124 else
14125 return PC_BOUNDS_NOT_PRESENT;
14126 }
14127
14128 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14129 if (high <= low)
14130 return PC_BOUNDS_INVALID;
14131
14132 /* When using the GNU linker, .gnu.linkonce. sections are used to
14133 eliminate duplicate copies of functions and vtables and such.
14134 The linker will arbitrarily choose one and discard the others.
14135 The AT_*_pc values for such functions refer to local labels in
14136 these sections. If the section from that file was discarded, the
14137 labels are not in the output, so the relocs get a value of 0.
14138 If this is a discarded function, mark the pc bounds as invalid,
14139 so that GDB will ignore it. */
14140 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14141 return PC_BOUNDS_INVALID;
14142
14143 *lowpc = low;
14144 if (highpc)
14145 *highpc = high;
14146 return ret;
14147 }
14148
14149 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14150 its low and high PC addresses. Do nothing if these addresses could not
14151 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14152 and HIGHPC to the high address if greater than HIGHPC. */
14153
14154 static void
14155 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14156 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14157 struct dwarf2_cu *cu)
14158 {
14159 CORE_ADDR low, high;
14160 struct die_info *child = die->child;
14161
14162 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14163 {
14164 *lowpc = std::min (*lowpc, low);
14165 *highpc = std::max (*highpc, high);
14166 }
14167
14168 /* If the language does not allow nested subprograms (either inside
14169 subprograms or lexical blocks), we're done. */
14170 if (cu->language != language_ada)
14171 return;
14172
14173 /* Check all the children of the given DIE. If it contains nested
14174 subprograms, then check their pc bounds. Likewise, we need to
14175 check lexical blocks as well, as they may also contain subprogram
14176 definitions. */
14177 while (child && child->tag)
14178 {
14179 if (child->tag == DW_TAG_subprogram
14180 || child->tag == DW_TAG_lexical_block)
14181 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14182 child = child->sibling;
14183 }
14184 }
14185
14186 /* Get the low and high pc's represented by the scope DIE, and store
14187 them in *LOWPC and *HIGHPC. If the correct values can't be
14188 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14189
14190 static void
14191 get_scope_pc_bounds (struct die_info *die,
14192 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14193 struct dwarf2_cu *cu)
14194 {
14195 CORE_ADDR best_low = (CORE_ADDR) -1;
14196 CORE_ADDR best_high = (CORE_ADDR) 0;
14197 CORE_ADDR current_low, current_high;
14198
14199 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14200 >= PC_BOUNDS_RANGES)
14201 {
14202 best_low = current_low;
14203 best_high = current_high;
14204 }
14205 else
14206 {
14207 struct die_info *child = die->child;
14208
14209 while (child && child->tag)
14210 {
14211 switch (child->tag) {
14212 case DW_TAG_subprogram:
14213 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14214 break;
14215 case DW_TAG_namespace:
14216 case DW_TAG_module:
14217 /* FIXME: carlton/2004-01-16: Should we do this for
14218 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14219 that current GCC's always emit the DIEs corresponding
14220 to definitions of methods of classes as children of a
14221 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14222 the DIEs giving the declarations, which could be
14223 anywhere). But I don't see any reason why the
14224 standards says that they have to be there. */
14225 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14226
14227 if (current_low != ((CORE_ADDR) -1))
14228 {
14229 best_low = std::min (best_low, current_low);
14230 best_high = std::max (best_high, current_high);
14231 }
14232 break;
14233 default:
14234 /* Ignore. */
14235 break;
14236 }
14237
14238 child = child->sibling;
14239 }
14240 }
14241
14242 *lowpc = best_low;
14243 *highpc = best_high;
14244 }
14245
14246 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14247 in DIE. */
14248
14249 static void
14250 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14251 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14252 {
14253 struct objfile *objfile = cu->per_objfile->objfile;
14254 struct gdbarch *gdbarch = objfile->arch ();
14255 struct attribute *attr;
14256 struct attribute *attr_high;
14257
14258 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14259 if (attr_high)
14260 {
14261 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14262 if (attr != nullptr)
14263 {
14264 CORE_ADDR low = attr->value_as_address ();
14265 CORE_ADDR high = attr_high->value_as_address ();
14266
14267 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14268 high += low;
14269
14270 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14271 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14272 cu->get_builder ()->record_block_range (block, low, high - 1);
14273 }
14274 }
14275
14276 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14277 if (attr != nullptr)
14278 {
14279 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14280 We take advantage of the fact that DW_AT_ranges does not appear
14281 in DW_TAG_compile_unit of DWO files. */
14282 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14283
14284 /* The value of the DW_AT_ranges attribute is the offset of the
14285 address range list in the .debug_ranges section. */
14286 unsigned long offset = (DW_UNSND (attr)
14287 + (need_ranges_base ? cu->ranges_base : 0));
14288
14289 std::vector<blockrange> blockvec;
14290 dwarf2_ranges_process (offset, cu,
14291 [&] (CORE_ADDR start, CORE_ADDR end)
14292 {
14293 start += baseaddr;
14294 end += baseaddr;
14295 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14296 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14297 cu->get_builder ()->record_block_range (block, start, end - 1);
14298 blockvec.emplace_back (start, end);
14299 });
14300
14301 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14302 }
14303 }
14304
14305 /* Check whether the producer field indicates either of GCC < 4.6, or the
14306 Intel C/C++ compiler, and cache the result in CU. */
14307
14308 static void
14309 check_producer (struct dwarf2_cu *cu)
14310 {
14311 int major, minor;
14312
14313 if (cu->producer == NULL)
14314 {
14315 /* For unknown compilers expect their behavior is DWARF version
14316 compliant.
14317
14318 GCC started to support .debug_types sections by -gdwarf-4 since
14319 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14320 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14321 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14322 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14323 }
14324 else if (producer_is_gcc (cu->producer, &major, &minor))
14325 {
14326 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14327 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14328 }
14329 else if (producer_is_icc (cu->producer, &major, &minor))
14330 {
14331 cu->producer_is_icc = true;
14332 cu->producer_is_icc_lt_14 = major < 14;
14333 }
14334 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14335 cu->producer_is_codewarrior = true;
14336 else
14337 {
14338 /* For other non-GCC compilers, expect their behavior is DWARF version
14339 compliant. */
14340 }
14341
14342 cu->checked_producer = true;
14343 }
14344
14345 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14346 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14347 during 4.6.0 experimental. */
14348
14349 static bool
14350 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14351 {
14352 if (!cu->checked_producer)
14353 check_producer (cu);
14354
14355 return cu->producer_is_gxx_lt_4_6;
14356 }
14357
14358
14359 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14360 with incorrect is_stmt attributes. */
14361
14362 static bool
14363 producer_is_codewarrior (struct dwarf2_cu *cu)
14364 {
14365 if (!cu->checked_producer)
14366 check_producer (cu);
14367
14368 return cu->producer_is_codewarrior;
14369 }
14370
14371 /* Return the default accessibility type if it is not overridden by
14372 DW_AT_accessibility. */
14373
14374 static enum dwarf_access_attribute
14375 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14376 {
14377 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14378 {
14379 /* The default DWARF 2 accessibility for members is public, the default
14380 accessibility for inheritance is private. */
14381
14382 if (die->tag != DW_TAG_inheritance)
14383 return DW_ACCESS_public;
14384 else
14385 return DW_ACCESS_private;
14386 }
14387 else
14388 {
14389 /* DWARF 3+ defines the default accessibility a different way. The same
14390 rules apply now for DW_TAG_inheritance as for the members and it only
14391 depends on the container kind. */
14392
14393 if (die->parent->tag == DW_TAG_class_type)
14394 return DW_ACCESS_private;
14395 else
14396 return DW_ACCESS_public;
14397 }
14398 }
14399
14400 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14401 offset. If the attribute was not found return 0, otherwise return
14402 1. If it was found but could not properly be handled, set *OFFSET
14403 to 0. */
14404
14405 static int
14406 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14407 LONGEST *offset)
14408 {
14409 struct attribute *attr;
14410
14411 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14412 if (attr != NULL)
14413 {
14414 *offset = 0;
14415
14416 /* Note that we do not check for a section offset first here.
14417 This is because DW_AT_data_member_location is new in DWARF 4,
14418 so if we see it, we can assume that a constant form is really
14419 a constant and not a section offset. */
14420 if (attr->form_is_constant ())
14421 *offset = attr->constant_value (0);
14422 else if (attr->form_is_section_offset ())
14423 dwarf2_complex_location_expr_complaint ();
14424 else if (attr->form_is_block ())
14425 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14426 else
14427 dwarf2_complex_location_expr_complaint ();
14428
14429 return 1;
14430 }
14431
14432 return 0;
14433 }
14434
14435 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14436
14437 static void
14438 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14439 struct field *field)
14440 {
14441 struct attribute *attr;
14442
14443 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14444 if (attr != NULL)
14445 {
14446 if (attr->form_is_constant ())
14447 {
14448 LONGEST offset = attr->constant_value (0);
14449 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14450 }
14451 else if (attr->form_is_section_offset ())
14452 dwarf2_complex_location_expr_complaint ();
14453 else if (attr->form_is_block ())
14454 {
14455 bool handled;
14456 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14457 if (handled)
14458 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14459 else
14460 {
14461 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14462 struct objfile *objfile = per_objfile->objfile;
14463 struct dwarf2_locexpr_baton *dlbaton
14464 = XOBNEW (&objfile->objfile_obstack,
14465 struct dwarf2_locexpr_baton);
14466 dlbaton->data = DW_BLOCK (attr)->data;
14467 dlbaton->size = DW_BLOCK (attr)->size;
14468 /* When using this baton, we want to compute the address
14469 of the field, not the value. This is why
14470 is_reference is set to false here. */
14471 dlbaton->is_reference = false;
14472 dlbaton->per_objfile = per_objfile;
14473 dlbaton->per_cu = cu->per_cu;
14474
14475 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14476 }
14477 }
14478 else
14479 dwarf2_complex_location_expr_complaint ();
14480 }
14481 }
14482
14483 /* Add an aggregate field to the field list. */
14484
14485 static void
14486 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14487 struct dwarf2_cu *cu)
14488 {
14489 struct objfile *objfile = cu->per_objfile->objfile;
14490 struct gdbarch *gdbarch = objfile->arch ();
14491 struct nextfield *new_field;
14492 struct attribute *attr;
14493 struct field *fp;
14494 const char *fieldname = "";
14495
14496 if (die->tag == DW_TAG_inheritance)
14497 {
14498 fip->baseclasses.emplace_back ();
14499 new_field = &fip->baseclasses.back ();
14500 }
14501 else
14502 {
14503 fip->fields.emplace_back ();
14504 new_field = &fip->fields.back ();
14505 }
14506
14507 new_field->offset = die->sect_off;
14508
14509 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14510 if (attr != nullptr)
14511 new_field->accessibility = DW_UNSND (attr);
14512 else
14513 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14514 if (new_field->accessibility != DW_ACCESS_public)
14515 fip->non_public_fields = 1;
14516
14517 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14518 if (attr != nullptr)
14519 new_field->virtuality = DW_UNSND (attr);
14520 else
14521 new_field->virtuality = DW_VIRTUALITY_none;
14522
14523 fp = &new_field->field;
14524
14525 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14526 {
14527 /* Data member other than a C++ static data member. */
14528
14529 /* Get type of field. */
14530 fp->type = die_type (die, cu);
14531
14532 SET_FIELD_BITPOS (*fp, 0);
14533
14534 /* Get bit size of field (zero if none). */
14535 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14536 if (attr != nullptr)
14537 {
14538 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14539 }
14540 else
14541 {
14542 FIELD_BITSIZE (*fp) = 0;
14543 }
14544
14545 /* Get bit offset of field. */
14546 handle_data_member_location (die, cu, fp);
14547 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14548 if (attr != nullptr)
14549 {
14550 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14551 {
14552 /* For big endian bits, the DW_AT_bit_offset gives the
14553 additional bit offset from the MSB of the containing
14554 anonymous object to the MSB of the field. We don't
14555 have to do anything special since we don't need to
14556 know the size of the anonymous object. */
14557 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14558 }
14559 else
14560 {
14561 /* For little endian bits, compute the bit offset to the
14562 MSB of the anonymous object, subtract off the number of
14563 bits from the MSB of the field to the MSB of the
14564 object, and then subtract off the number of bits of
14565 the field itself. The result is the bit offset of
14566 the LSB of the field. */
14567 int anonymous_size;
14568 int bit_offset = DW_UNSND (attr);
14569
14570 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14571 if (attr != nullptr)
14572 {
14573 /* The size of the anonymous object containing
14574 the bit field is explicit, so use the
14575 indicated size (in bytes). */
14576 anonymous_size = DW_UNSND (attr);
14577 }
14578 else
14579 {
14580 /* The size of the anonymous object containing
14581 the bit field must be inferred from the type
14582 attribute of the data member containing the
14583 bit field. */
14584 anonymous_size = TYPE_LENGTH (fp->type);
14585 }
14586 SET_FIELD_BITPOS (*fp,
14587 (FIELD_BITPOS (*fp)
14588 + anonymous_size * bits_per_byte
14589 - bit_offset - FIELD_BITSIZE (*fp)));
14590 }
14591 }
14592 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14593 if (attr != NULL)
14594 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14595 + attr->constant_value (0)));
14596
14597 /* Get name of field. */
14598 fieldname = dwarf2_name (die, cu);
14599 if (fieldname == NULL)
14600 fieldname = "";
14601
14602 /* The name is already allocated along with this objfile, so we don't
14603 need to duplicate it for the type. */
14604 fp->name = fieldname;
14605
14606 /* Change accessibility for artificial fields (e.g. virtual table
14607 pointer or virtual base class pointer) to private. */
14608 if (dwarf2_attr (die, DW_AT_artificial, cu))
14609 {
14610 FIELD_ARTIFICIAL (*fp) = 1;
14611 new_field->accessibility = DW_ACCESS_private;
14612 fip->non_public_fields = 1;
14613 }
14614 }
14615 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14616 {
14617 /* C++ static member. */
14618
14619 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14620 is a declaration, but all versions of G++ as of this writing
14621 (so through at least 3.2.1) incorrectly generate
14622 DW_TAG_variable tags. */
14623
14624 const char *physname;
14625
14626 /* Get name of field. */
14627 fieldname = dwarf2_name (die, cu);
14628 if (fieldname == NULL)
14629 return;
14630
14631 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14632 if (attr
14633 /* Only create a symbol if this is an external value.
14634 new_symbol checks this and puts the value in the global symbol
14635 table, which we want. If it is not external, new_symbol
14636 will try to put the value in cu->list_in_scope which is wrong. */
14637 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14638 {
14639 /* A static const member, not much different than an enum as far as
14640 we're concerned, except that we can support more types. */
14641 new_symbol (die, NULL, cu);
14642 }
14643
14644 /* Get physical name. */
14645 physname = dwarf2_physname (fieldname, die, cu);
14646
14647 /* The name is already allocated along with this objfile, so we don't
14648 need to duplicate it for the type. */
14649 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14650 FIELD_TYPE (*fp) = die_type (die, cu);
14651 FIELD_NAME (*fp) = fieldname;
14652 }
14653 else if (die->tag == DW_TAG_inheritance)
14654 {
14655 /* C++ base class field. */
14656 handle_data_member_location (die, cu, fp);
14657 FIELD_BITSIZE (*fp) = 0;
14658 FIELD_TYPE (*fp) = die_type (die, cu);
14659 FIELD_NAME (*fp) = fp->type->name ();
14660 }
14661 else
14662 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14663 }
14664
14665 /* Can the type given by DIE define another type? */
14666
14667 static bool
14668 type_can_define_types (const struct die_info *die)
14669 {
14670 switch (die->tag)
14671 {
14672 case DW_TAG_typedef:
14673 case DW_TAG_class_type:
14674 case DW_TAG_structure_type:
14675 case DW_TAG_union_type:
14676 case DW_TAG_enumeration_type:
14677 return true;
14678
14679 default:
14680 return false;
14681 }
14682 }
14683
14684 /* Add a type definition defined in the scope of the FIP's class. */
14685
14686 static void
14687 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14688 struct dwarf2_cu *cu)
14689 {
14690 struct decl_field fp;
14691 memset (&fp, 0, sizeof (fp));
14692
14693 gdb_assert (type_can_define_types (die));
14694
14695 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14696 fp.name = dwarf2_name (die, cu);
14697 fp.type = read_type_die (die, cu);
14698
14699 /* Save accessibility. */
14700 enum dwarf_access_attribute accessibility;
14701 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14702 if (attr != NULL)
14703 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14704 else
14705 accessibility = dwarf2_default_access_attribute (die, cu);
14706 switch (accessibility)
14707 {
14708 case DW_ACCESS_public:
14709 /* The assumed value if neither private nor protected. */
14710 break;
14711 case DW_ACCESS_private:
14712 fp.is_private = 1;
14713 break;
14714 case DW_ACCESS_protected:
14715 fp.is_protected = 1;
14716 break;
14717 default:
14718 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14719 }
14720
14721 if (die->tag == DW_TAG_typedef)
14722 fip->typedef_field_list.push_back (fp);
14723 else
14724 fip->nested_types_list.push_back (fp);
14725 }
14726
14727 /* A convenience typedef that's used when finding the discriminant
14728 field for a variant part. */
14729 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14730 offset_map_type;
14731
14732 /* Compute the discriminant range for a given variant. OBSTACK is
14733 where the results will be stored. VARIANT is the variant to
14734 process. IS_UNSIGNED indicates whether the discriminant is signed
14735 or unsigned. */
14736
14737 static const gdb::array_view<discriminant_range>
14738 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14739 bool is_unsigned)
14740 {
14741 std::vector<discriminant_range> ranges;
14742
14743 if (variant.default_branch)
14744 return {};
14745
14746 if (variant.discr_list_data == nullptr)
14747 {
14748 discriminant_range r
14749 = {variant.discriminant_value, variant.discriminant_value};
14750 ranges.push_back (r);
14751 }
14752 else
14753 {
14754 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14755 variant.discr_list_data->size);
14756 while (!data.empty ())
14757 {
14758 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14759 {
14760 complaint (_("invalid discriminant marker: %d"), data[0]);
14761 break;
14762 }
14763 bool is_range = data[0] == DW_DSC_range;
14764 data = data.slice (1);
14765
14766 ULONGEST low, high;
14767 unsigned int bytes_read;
14768
14769 if (data.empty ())
14770 {
14771 complaint (_("DW_AT_discr_list missing low value"));
14772 break;
14773 }
14774 if (is_unsigned)
14775 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14776 else
14777 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14778 &bytes_read);
14779 data = data.slice (bytes_read);
14780
14781 if (is_range)
14782 {
14783 if (data.empty ())
14784 {
14785 complaint (_("DW_AT_discr_list missing high value"));
14786 break;
14787 }
14788 if (is_unsigned)
14789 high = read_unsigned_leb128 (nullptr, data.data (),
14790 &bytes_read);
14791 else
14792 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14793 &bytes_read);
14794 data = data.slice (bytes_read);
14795 }
14796 else
14797 high = low;
14798
14799 ranges.push_back ({ low, high });
14800 }
14801 }
14802
14803 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14804 ranges.size ());
14805 std::copy (ranges.begin (), ranges.end (), result);
14806 return gdb::array_view<discriminant_range> (result, ranges.size ());
14807 }
14808
14809 static const gdb::array_view<variant_part> create_variant_parts
14810 (struct obstack *obstack,
14811 const offset_map_type &offset_map,
14812 struct field_info *fi,
14813 const std::vector<variant_part_builder> &variant_parts);
14814
14815 /* Fill in a "struct variant" for a given variant field. RESULT is
14816 the variant to fill in. OBSTACK is where any needed allocations
14817 will be done. OFFSET_MAP holds the mapping from section offsets to
14818 fields for the type. FI describes the fields of the type we're
14819 processing. FIELD is the variant field we're converting. */
14820
14821 static void
14822 create_one_variant (variant &result, struct obstack *obstack,
14823 const offset_map_type &offset_map,
14824 struct field_info *fi, const variant_field &field)
14825 {
14826 result.discriminants = convert_variant_range (obstack, field, false);
14827 result.first_field = field.first_field + fi->baseclasses.size ();
14828 result.last_field = field.last_field + fi->baseclasses.size ();
14829 result.parts = create_variant_parts (obstack, offset_map, fi,
14830 field.variant_parts);
14831 }
14832
14833 /* Fill in a "struct variant_part" for a given variant part. RESULT
14834 is the variant part to fill in. OBSTACK is where any needed
14835 allocations will be done. OFFSET_MAP holds the mapping from
14836 section offsets to fields for the type. FI describes the fields of
14837 the type we're processing. BUILDER is the variant part to be
14838 converted. */
14839
14840 static void
14841 create_one_variant_part (variant_part &result,
14842 struct obstack *obstack,
14843 const offset_map_type &offset_map,
14844 struct field_info *fi,
14845 const variant_part_builder &builder)
14846 {
14847 auto iter = offset_map.find (builder.discriminant_offset);
14848 if (iter == offset_map.end ())
14849 {
14850 result.discriminant_index = -1;
14851 /* Doesn't matter. */
14852 result.is_unsigned = false;
14853 }
14854 else
14855 {
14856 result.discriminant_index = iter->second;
14857 result.is_unsigned
14858 = TYPE_UNSIGNED (FIELD_TYPE
14859 (fi->fields[result.discriminant_index].field));
14860 }
14861
14862 size_t n = builder.variants.size ();
14863 variant *output = new (obstack) variant[n];
14864 for (size_t i = 0; i < n; ++i)
14865 create_one_variant (output[i], obstack, offset_map, fi,
14866 builder.variants[i]);
14867
14868 result.variants = gdb::array_view<variant> (output, n);
14869 }
14870
14871 /* Create a vector of variant parts that can be attached to a type.
14872 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14873 holds the mapping from section offsets to fields for the type. FI
14874 describes the fields of the type we're processing. VARIANT_PARTS
14875 is the vector to convert. */
14876
14877 static const gdb::array_view<variant_part>
14878 create_variant_parts (struct obstack *obstack,
14879 const offset_map_type &offset_map,
14880 struct field_info *fi,
14881 const std::vector<variant_part_builder> &variant_parts)
14882 {
14883 if (variant_parts.empty ())
14884 return {};
14885
14886 size_t n = variant_parts.size ();
14887 variant_part *result = new (obstack) variant_part[n];
14888 for (size_t i = 0; i < n; ++i)
14889 create_one_variant_part (result[i], obstack, offset_map, fi,
14890 variant_parts[i]);
14891
14892 return gdb::array_view<variant_part> (result, n);
14893 }
14894
14895 /* Compute the variant part vector for FIP, attaching it to TYPE when
14896 done. */
14897
14898 static void
14899 add_variant_property (struct field_info *fip, struct type *type,
14900 struct dwarf2_cu *cu)
14901 {
14902 /* Map section offsets of fields to their field index. Note the
14903 field index here does not take the number of baseclasses into
14904 account. */
14905 offset_map_type offset_map;
14906 for (int i = 0; i < fip->fields.size (); ++i)
14907 offset_map[fip->fields[i].offset] = i;
14908
14909 struct objfile *objfile = cu->per_objfile->objfile;
14910 gdb::array_view<variant_part> parts
14911 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14912 fip->variant_parts);
14913
14914 struct dynamic_prop prop;
14915 prop.kind = PROP_VARIANT_PARTS;
14916 prop.data.variant_parts
14917 = ((gdb::array_view<variant_part> *)
14918 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14919
14920 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14921 }
14922
14923 /* Create the vector of fields, and attach it to the type. */
14924
14925 static void
14926 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14927 struct dwarf2_cu *cu)
14928 {
14929 int nfields = fip->nfields ();
14930
14931 /* Record the field count, allocate space for the array of fields,
14932 and create blank accessibility bitfields if necessary. */
14933 type->set_num_fields (nfields);
14934 type->set_fields
14935 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14936
14937 if (fip->non_public_fields && cu->language != language_ada)
14938 {
14939 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14940
14941 TYPE_FIELD_PRIVATE_BITS (type) =
14942 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14943 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14944
14945 TYPE_FIELD_PROTECTED_BITS (type) =
14946 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14947 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14948
14949 TYPE_FIELD_IGNORE_BITS (type) =
14950 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14951 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14952 }
14953
14954 /* If the type has baseclasses, allocate and clear a bit vector for
14955 TYPE_FIELD_VIRTUAL_BITS. */
14956 if (!fip->baseclasses.empty () && cu->language != language_ada)
14957 {
14958 int num_bytes = B_BYTES (fip->baseclasses.size ());
14959 unsigned char *pointer;
14960
14961 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14962 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14963 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14964 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14965 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14966 }
14967
14968 if (!fip->variant_parts.empty ())
14969 add_variant_property (fip, type, cu);
14970
14971 /* Copy the saved-up fields into the field vector. */
14972 for (int i = 0; i < nfields; ++i)
14973 {
14974 struct nextfield &field
14975 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14976 : fip->fields[i - fip->baseclasses.size ()]);
14977
14978 type->field (i) = field.field;
14979 switch (field.accessibility)
14980 {
14981 case DW_ACCESS_private:
14982 if (cu->language != language_ada)
14983 SET_TYPE_FIELD_PRIVATE (type, i);
14984 break;
14985
14986 case DW_ACCESS_protected:
14987 if (cu->language != language_ada)
14988 SET_TYPE_FIELD_PROTECTED (type, i);
14989 break;
14990
14991 case DW_ACCESS_public:
14992 break;
14993
14994 default:
14995 /* Unknown accessibility. Complain and treat it as public. */
14996 {
14997 complaint (_("unsupported accessibility %d"),
14998 field.accessibility);
14999 }
15000 break;
15001 }
15002 if (i < fip->baseclasses.size ())
15003 {
15004 switch (field.virtuality)
15005 {
15006 case DW_VIRTUALITY_virtual:
15007 case DW_VIRTUALITY_pure_virtual:
15008 if (cu->language == language_ada)
15009 error (_("unexpected virtuality in component of Ada type"));
15010 SET_TYPE_FIELD_VIRTUAL (type, i);
15011 break;
15012 }
15013 }
15014 }
15015 }
15016
15017 /* Return true if this member function is a constructor, false
15018 otherwise. */
15019
15020 static int
15021 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15022 {
15023 const char *fieldname;
15024 const char *type_name;
15025 int len;
15026
15027 if (die->parent == NULL)
15028 return 0;
15029
15030 if (die->parent->tag != DW_TAG_structure_type
15031 && die->parent->tag != DW_TAG_union_type
15032 && die->parent->tag != DW_TAG_class_type)
15033 return 0;
15034
15035 fieldname = dwarf2_name (die, cu);
15036 type_name = dwarf2_name (die->parent, cu);
15037 if (fieldname == NULL || type_name == NULL)
15038 return 0;
15039
15040 len = strlen (fieldname);
15041 return (strncmp (fieldname, type_name, len) == 0
15042 && (type_name[len] == '\0' || type_name[len] == '<'));
15043 }
15044
15045 /* Check if the given VALUE is a recognized enum
15046 dwarf_defaulted_attribute constant according to DWARF5 spec,
15047 Table 7.24. */
15048
15049 static bool
15050 is_valid_DW_AT_defaulted (ULONGEST value)
15051 {
15052 switch (value)
15053 {
15054 case DW_DEFAULTED_no:
15055 case DW_DEFAULTED_in_class:
15056 case DW_DEFAULTED_out_of_class:
15057 return true;
15058 }
15059
15060 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15061 return false;
15062 }
15063
15064 /* Add a member function to the proper fieldlist. */
15065
15066 static void
15067 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15068 struct type *type, struct dwarf2_cu *cu)
15069 {
15070 struct objfile *objfile = cu->per_objfile->objfile;
15071 struct attribute *attr;
15072 int i;
15073 struct fnfieldlist *flp = nullptr;
15074 struct fn_field *fnp;
15075 const char *fieldname;
15076 struct type *this_type;
15077 enum dwarf_access_attribute accessibility;
15078
15079 if (cu->language == language_ada)
15080 error (_("unexpected member function in Ada type"));
15081
15082 /* Get name of member function. */
15083 fieldname = dwarf2_name (die, cu);
15084 if (fieldname == NULL)
15085 return;
15086
15087 /* Look up member function name in fieldlist. */
15088 for (i = 0; i < fip->fnfieldlists.size (); i++)
15089 {
15090 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15091 {
15092 flp = &fip->fnfieldlists[i];
15093 break;
15094 }
15095 }
15096
15097 /* Create a new fnfieldlist if necessary. */
15098 if (flp == nullptr)
15099 {
15100 fip->fnfieldlists.emplace_back ();
15101 flp = &fip->fnfieldlists.back ();
15102 flp->name = fieldname;
15103 i = fip->fnfieldlists.size () - 1;
15104 }
15105
15106 /* Create a new member function field and add it to the vector of
15107 fnfieldlists. */
15108 flp->fnfields.emplace_back ();
15109 fnp = &flp->fnfields.back ();
15110
15111 /* Delay processing of the physname until later. */
15112 if (cu->language == language_cplus)
15113 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15114 die, cu);
15115 else
15116 {
15117 const char *physname = dwarf2_physname (fieldname, die, cu);
15118 fnp->physname = physname ? physname : "";
15119 }
15120
15121 fnp->type = alloc_type (objfile);
15122 this_type = read_type_die (die, cu);
15123 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15124 {
15125 int nparams = this_type->num_fields ();
15126
15127 /* TYPE is the domain of this method, and THIS_TYPE is the type
15128 of the method itself (TYPE_CODE_METHOD). */
15129 smash_to_method_type (fnp->type, type,
15130 TYPE_TARGET_TYPE (this_type),
15131 this_type->fields (),
15132 this_type->num_fields (),
15133 TYPE_VARARGS (this_type));
15134
15135 /* Handle static member functions.
15136 Dwarf2 has no clean way to discern C++ static and non-static
15137 member functions. G++ helps GDB by marking the first
15138 parameter for non-static member functions (which is the this
15139 pointer) as artificial. We obtain this information from
15140 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15141 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15142 fnp->voffset = VOFFSET_STATIC;
15143 }
15144 else
15145 complaint (_("member function type missing for '%s'"),
15146 dwarf2_full_name (fieldname, die, cu));
15147
15148 /* Get fcontext from DW_AT_containing_type if present. */
15149 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15150 fnp->fcontext = die_containing_type (die, cu);
15151
15152 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15153 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15154
15155 /* Get accessibility. */
15156 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15157 if (attr != nullptr)
15158 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15159 else
15160 accessibility = dwarf2_default_access_attribute (die, cu);
15161 switch (accessibility)
15162 {
15163 case DW_ACCESS_private:
15164 fnp->is_private = 1;
15165 break;
15166 case DW_ACCESS_protected:
15167 fnp->is_protected = 1;
15168 break;
15169 }
15170
15171 /* Check for artificial methods. */
15172 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15173 if (attr && DW_UNSND (attr) != 0)
15174 fnp->is_artificial = 1;
15175
15176 /* Check for defaulted methods. */
15177 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15178 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15179 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15180
15181 /* Check for deleted methods. */
15182 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15183 if (attr != nullptr && DW_UNSND (attr) != 0)
15184 fnp->is_deleted = 1;
15185
15186 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15187
15188 /* Get index in virtual function table if it is a virtual member
15189 function. For older versions of GCC, this is an offset in the
15190 appropriate virtual table, as specified by DW_AT_containing_type.
15191 For everyone else, it is an expression to be evaluated relative
15192 to the object address. */
15193
15194 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15195 if (attr != nullptr)
15196 {
15197 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15198 {
15199 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15200 {
15201 /* Old-style GCC. */
15202 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15203 }
15204 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15205 || (DW_BLOCK (attr)->size > 1
15206 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15207 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15208 {
15209 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15210 if ((fnp->voffset % cu->header.addr_size) != 0)
15211 dwarf2_complex_location_expr_complaint ();
15212 else
15213 fnp->voffset /= cu->header.addr_size;
15214 fnp->voffset += 2;
15215 }
15216 else
15217 dwarf2_complex_location_expr_complaint ();
15218
15219 if (!fnp->fcontext)
15220 {
15221 /* If there is no `this' field and no DW_AT_containing_type,
15222 we cannot actually find a base class context for the
15223 vtable! */
15224 if (this_type->num_fields () == 0
15225 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15226 {
15227 complaint (_("cannot determine context for virtual member "
15228 "function \"%s\" (offset %s)"),
15229 fieldname, sect_offset_str (die->sect_off));
15230 }
15231 else
15232 {
15233 fnp->fcontext
15234 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15235 }
15236 }
15237 }
15238 else if (attr->form_is_section_offset ())
15239 {
15240 dwarf2_complex_location_expr_complaint ();
15241 }
15242 else
15243 {
15244 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15245 fieldname);
15246 }
15247 }
15248 else
15249 {
15250 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15251 if (attr && DW_UNSND (attr))
15252 {
15253 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15254 complaint (_("Member function \"%s\" (offset %s) is virtual "
15255 "but the vtable offset is not specified"),
15256 fieldname, sect_offset_str (die->sect_off));
15257 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15258 TYPE_CPLUS_DYNAMIC (type) = 1;
15259 }
15260 }
15261 }
15262
15263 /* Create the vector of member function fields, and attach it to the type. */
15264
15265 static void
15266 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15267 struct dwarf2_cu *cu)
15268 {
15269 if (cu->language == language_ada)
15270 error (_("unexpected member functions in Ada type"));
15271
15272 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15273 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15274 TYPE_ALLOC (type,
15275 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15276
15277 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15278 {
15279 struct fnfieldlist &nf = fip->fnfieldlists[i];
15280 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15281
15282 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15283 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15284 fn_flp->fn_fields = (struct fn_field *)
15285 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15286
15287 for (int k = 0; k < nf.fnfields.size (); ++k)
15288 fn_flp->fn_fields[k] = nf.fnfields[k];
15289 }
15290
15291 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15292 }
15293
15294 /* Returns non-zero if NAME is the name of a vtable member in CU's
15295 language, zero otherwise. */
15296 static int
15297 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15298 {
15299 static const char vptr[] = "_vptr";
15300
15301 /* Look for the C++ form of the vtable. */
15302 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15303 return 1;
15304
15305 return 0;
15306 }
15307
15308 /* GCC outputs unnamed structures that are really pointers to member
15309 functions, with the ABI-specified layout. If TYPE describes
15310 such a structure, smash it into a member function type.
15311
15312 GCC shouldn't do this; it should just output pointer to member DIEs.
15313 This is GCC PR debug/28767. */
15314
15315 static void
15316 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15317 {
15318 struct type *pfn_type, *self_type, *new_type;
15319
15320 /* Check for a structure with no name and two children. */
15321 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15322 return;
15323
15324 /* Check for __pfn and __delta members. */
15325 if (TYPE_FIELD_NAME (type, 0) == NULL
15326 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15327 || TYPE_FIELD_NAME (type, 1) == NULL
15328 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15329 return;
15330
15331 /* Find the type of the method. */
15332 pfn_type = TYPE_FIELD_TYPE (type, 0);
15333 if (pfn_type == NULL
15334 || pfn_type->code () != TYPE_CODE_PTR
15335 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15336 return;
15337
15338 /* Look for the "this" argument. */
15339 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15340 if (pfn_type->num_fields () == 0
15341 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15342 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15343 return;
15344
15345 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15346 new_type = alloc_type (objfile);
15347 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15348 pfn_type->fields (), pfn_type->num_fields (),
15349 TYPE_VARARGS (pfn_type));
15350 smash_to_methodptr_type (type, new_type);
15351 }
15352
15353 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15354 appropriate error checking and issuing complaints if there is a
15355 problem. */
15356
15357 static ULONGEST
15358 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15359 {
15360 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15361
15362 if (attr == nullptr)
15363 return 0;
15364
15365 if (!attr->form_is_constant ())
15366 {
15367 complaint (_("DW_AT_alignment must have constant form"
15368 " - DIE at %s [in module %s]"),
15369 sect_offset_str (die->sect_off),
15370 objfile_name (cu->per_objfile->objfile));
15371 return 0;
15372 }
15373
15374 ULONGEST align;
15375 if (attr->form == DW_FORM_sdata)
15376 {
15377 LONGEST val = DW_SND (attr);
15378 if (val < 0)
15379 {
15380 complaint (_("DW_AT_alignment value must not be negative"
15381 " - DIE at %s [in module %s]"),
15382 sect_offset_str (die->sect_off),
15383 objfile_name (cu->per_objfile->objfile));
15384 return 0;
15385 }
15386 align = val;
15387 }
15388 else
15389 align = DW_UNSND (attr);
15390
15391 if (align == 0)
15392 {
15393 complaint (_("DW_AT_alignment value must not be zero"
15394 " - DIE at %s [in module %s]"),
15395 sect_offset_str (die->sect_off),
15396 objfile_name (cu->per_objfile->objfile));
15397 return 0;
15398 }
15399 if ((align & (align - 1)) != 0)
15400 {
15401 complaint (_("DW_AT_alignment value must be a power of 2"
15402 " - DIE at %s [in module %s]"),
15403 sect_offset_str (die->sect_off),
15404 objfile_name (cu->per_objfile->objfile));
15405 return 0;
15406 }
15407
15408 return align;
15409 }
15410
15411 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15412 the alignment for TYPE. */
15413
15414 static void
15415 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15416 struct type *type)
15417 {
15418 if (!set_type_align (type, get_alignment (cu, die)))
15419 complaint (_("DW_AT_alignment value too large"
15420 " - DIE at %s [in module %s]"),
15421 sect_offset_str (die->sect_off),
15422 objfile_name (cu->per_objfile->objfile));
15423 }
15424
15425 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15426 constant for a type, according to DWARF5 spec, Table 5.5. */
15427
15428 static bool
15429 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15430 {
15431 switch (value)
15432 {
15433 case DW_CC_normal:
15434 case DW_CC_pass_by_reference:
15435 case DW_CC_pass_by_value:
15436 return true;
15437
15438 default:
15439 complaint (_("unrecognized DW_AT_calling_convention value "
15440 "(%s) for a type"), pulongest (value));
15441 return false;
15442 }
15443 }
15444
15445 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15446 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15447 also according to GNU-specific values (see include/dwarf2.h). */
15448
15449 static bool
15450 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15451 {
15452 switch (value)
15453 {
15454 case DW_CC_normal:
15455 case DW_CC_program:
15456 case DW_CC_nocall:
15457 return true;
15458
15459 case DW_CC_GNU_renesas_sh:
15460 case DW_CC_GNU_borland_fastcall_i386:
15461 case DW_CC_GDB_IBM_OpenCL:
15462 return true;
15463
15464 default:
15465 complaint (_("unrecognized DW_AT_calling_convention value "
15466 "(%s) for a subroutine"), pulongest (value));
15467 return false;
15468 }
15469 }
15470
15471 /* Called when we find the DIE that starts a structure or union scope
15472 (definition) to create a type for the structure or union. Fill in
15473 the type's name and general properties; the members will not be
15474 processed until process_structure_scope. A symbol table entry for
15475 the type will also not be done until process_structure_scope (assuming
15476 the type has a name).
15477
15478 NOTE: we need to call these functions regardless of whether or not the
15479 DIE has a DW_AT_name attribute, since it might be an anonymous
15480 structure or union. This gets the type entered into our set of
15481 user defined types. */
15482
15483 static struct type *
15484 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15485 {
15486 struct objfile *objfile = cu->per_objfile->objfile;
15487 struct type *type;
15488 struct attribute *attr;
15489 const char *name;
15490
15491 /* If the definition of this type lives in .debug_types, read that type.
15492 Don't follow DW_AT_specification though, that will take us back up
15493 the chain and we want to go down. */
15494 attr = die->attr (DW_AT_signature);
15495 if (attr != nullptr)
15496 {
15497 type = get_DW_AT_signature_type (die, attr, cu);
15498
15499 /* The type's CU may not be the same as CU.
15500 Ensure TYPE is recorded with CU in die_type_hash. */
15501 return set_die_type (die, type, cu);
15502 }
15503
15504 type = alloc_type (objfile);
15505 INIT_CPLUS_SPECIFIC (type);
15506
15507 name = dwarf2_name (die, cu);
15508 if (name != NULL)
15509 {
15510 if (cu->language == language_cplus
15511 || cu->language == language_d
15512 || cu->language == language_rust)
15513 {
15514 const char *full_name = dwarf2_full_name (name, die, cu);
15515
15516 /* dwarf2_full_name might have already finished building the DIE's
15517 type. If so, there is no need to continue. */
15518 if (get_die_type (die, cu) != NULL)
15519 return get_die_type (die, cu);
15520
15521 type->set_name (full_name);
15522 }
15523 else
15524 {
15525 /* The name is already allocated along with this objfile, so
15526 we don't need to duplicate it for the type. */
15527 type->set_name (name);
15528 }
15529 }
15530
15531 if (die->tag == DW_TAG_structure_type)
15532 {
15533 type->set_code (TYPE_CODE_STRUCT);
15534 }
15535 else if (die->tag == DW_TAG_union_type)
15536 {
15537 type->set_code (TYPE_CODE_UNION);
15538 }
15539 else
15540 {
15541 type->set_code (TYPE_CODE_STRUCT);
15542 }
15543
15544 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15545 TYPE_DECLARED_CLASS (type) = 1;
15546
15547 /* Store the calling convention in the type if it's available in
15548 the die. Otherwise the calling convention remains set to
15549 the default value DW_CC_normal. */
15550 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15551 if (attr != nullptr
15552 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15553 {
15554 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15555 TYPE_CPLUS_CALLING_CONVENTION (type)
15556 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15557 }
15558
15559 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15560 if (attr != nullptr)
15561 {
15562 if (attr->form_is_constant ())
15563 TYPE_LENGTH (type) = DW_UNSND (attr);
15564 else
15565 {
15566 struct dynamic_prop prop;
15567 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15568 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15569 TYPE_LENGTH (type) = 0;
15570 }
15571 }
15572 else
15573 {
15574 TYPE_LENGTH (type) = 0;
15575 }
15576
15577 maybe_set_alignment (cu, die, type);
15578
15579 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15580 {
15581 /* ICC<14 does not output the required DW_AT_declaration on
15582 incomplete types, but gives them a size of zero. */
15583 TYPE_STUB (type) = 1;
15584 }
15585 else
15586 TYPE_STUB_SUPPORTED (type) = 1;
15587
15588 if (die_is_declaration (die, cu))
15589 TYPE_STUB (type) = 1;
15590 else if (attr == NULL && die->child == NULL
15591 && producer_is_realview (cu->producer))
15592 /* RealView does not output the required DW_AT_declaration
15593 on incomplete types. */
15594 TYPE_STUB (type) = 1;
15595
15596 /* We need to add the type field to the die immediately so we don't
15597 infinitely recurse when dealing with pointers to the structure
15598 type within the structure itself. */
15599 set_die_type (die, type, cu);
15600
15601 /* set_die_type should be already done. */
15602 set_descriptive_type (type, die, cu);
15603
15604 return type;
15605 }
15606
15607 static void handle_struct_member_die
15608 (struct die_info *child_die,
15609 struct type *type,
15610 struct field_info *fi,
15611 std::vector<struct symbol *> *template_args,
15612 struct dwarf2_cu *cu);
15613
15614 /* A helper for handle_struct_member_die that handles
15615 DW_TAG_variant_part. */
15616
15617 static void
15618 handle_variant_part (struct die_info *die, struct type *type,
15619 struct field_info *fi,
15620 std::vector<struct symbol *> *template_args,
15621 struct dwarf2_cu *cu)
15622 {
15623 variant_part_builder *new_part;
15624 if (fi->current_variant_part == nullptr)
15625 {
15626 fi->variant_parts.emplace_back ();
15627 new_part = &fi->variant_parts.back ();
15628 }
15629 else if (!fi->current_variant_part->processing_variant)
15630 {
15631 complaint (_("nested DW_TAG_variant_part seen "
15632 "- DIE at %s [in module %s]"),
15633 sect_offset_str (die->sect_off),
15634 objfile_name (cu->per_objfile->objfile));
15635 return;
15636 }
15637 else
15638 {
15639 variant_field &current = fi->current_variant_part->variants.back ();
15640 current.variant_parts.emplace_back ();
15641 new_part = &current.variant_parts.back ();
15642 }
15643
15644 /* When we recurse, we want callees to add to this new variant
15645 part. */
15646 scoped_restore save_current_variant_part
15647 = make_scoped_restore (&fi->current_variant_part, new_part);
15648
15649 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15650 if (discr == NULL)
15651 {
15652 /* It's a univariant form, an extension we support. */
15653 }
15654 else if (discr->form_is_ref ())
15655 {
15656 struct dwarf2_cu *target_cu = cu;
15657 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15658
15659 new_part->discriminant_offset = target_die->sect_off;
15660 }
15661 else
15662 {
15663 complaint (_("DW_AT_discr does not have DIE reference form"
15664 " - DIE at %s [in module %s]"),
15665 sect_offset_str (die->sect_off),
15666 objfile_name (cu->per_objfile->objfile));
15667 }
15668
15669 for (die_info *child_die = die->child;
15670 child_die != NULL;
15671 child_die = child_die->sibling)
15672 handle_struct_member_die (child_die, type, fi, template_args, cu);
15673 }
15674
15675 /* A helper for handle_struct_member_die that handles
15676 DW_TAG_variant. */
15677
15678 static void
15679 handle_variant (struct die_info *die, struct type *type,
15680 struct field_info *fi,
15681 std::vector<struct symbol *> *template_args,
15682 struct dwarf2_cu *cu)
15683 {
15684 if (fi->current_variant_part == nullptr)
15685 {
15686 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15687 "- DIE at %s [in module %s]"),
15688 sect_offset_str (die->sect_off),
15689 objfile_name (cu->per_objfile->objfile));
15690 return;
15691 }
15692 if (fi->current_variant_part->processing_variant)
15693 {
15694 complaint (_("nested DW_TAG_variant seen "
15695 "- DIE at %s [in module %s]"),
15696 sect_offset_str (die->sect_off),
15697 objfile_name (cu->per_objfile->objfile));
15698 return;
15699 }
15700
15701 scoped_restore save_processing_variant
15702 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15703 true);
15704
15705 fi->current_variant_part->variants.emplace_back ();
15706 variant_field &variant = fi->current_variant_part->variants.back ();
15707 variant.first_field = fi->fields.size ();
15708
15709 /* In a variant we want to get the discriminant and also add a
15710 field for our sole member child. */
15711 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15712 if (discr == nullptr)
15713 {
15714 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15715 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15716 variant.default_branch = true;
15717 else
15718 variant.discr_list_data = DW_BLOCK (discr);
15719 }
15720 else
15721 variant.discriminant_value = DW_UNSND (discr);
15722
15723 for (die_info *variant_child = die->child;
15724 variant_child != NULL;
15725 variant_child = variant_child->sibling)
15726 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15727
15728 variant.last_field = fi->fields.size ();
15729 }
15730
15731 /* A helper for process_structure_scope that handles a single member
15732 DIE. */
15733
15734 static void
15735 handle_struct_member_die (struct die_info *child_die, struct type *type,
15736 struct field_info *fi,
15737 std::vector<struct symbol *> *template_args,
15738 struct dwarf2_cu *cu)
15739 {
15740 if (child_die->tag == DW_TAG_member
15741 || child_die->tag == DW_TAG_variable)
15742 {
15743 /* NOTE: carlton/2002-11-05: A C++ static data member
15744 should be a DW_TAG_member that is a declaration, but
15745 all versions of G++ as of this writing (so through at
15746 least 3.2.1) incorrectly generate DW_TAG_variable
15747 tags for them instead. */
15748 dwarf2_add_field (fi, child_die, cu);
15749 }
15750 else if (child_die->tag == DW_TAG_subprogram)
15751 {
15752 /* Rust doesn't have member functions in the C++ sense.
15753 However, it does emit ordinary functions as children
15754 of a struct DIE. */
15755 if (cu->language == language_rust)
15756 read_func_scope (child_die, cu);
15757 else
15758 {
15759 /* C++ member function. */
15760 dwarf2_add_member_fn (fi, child_die, type, cu);
15761 }
15762 }
15763 else if (child_die->tag == DW_TAG_inheritance)
15764 {
15765 /* C++ base class field. */
15766 dwarf2_add_field (fi, child_die, cu);
15767 }
15768 else if (type_can_define_types (child_die))
15769 dwarf2_add_type_defn (fi, child_die, cu);
15770 else if (child_die->tag == DW_TAG_template_type_param
15771 || child_die->tag == DW_TAG_template_value_param)
15772 {
15773 struct symbol *arg = new_symbol (child_die, NULL, cu);
15774
15775 if (arg != NULL)
15776 template_args->push_back (arg);
15777 }
15778 else if (child_die->tag == DW_TAG_variant_part)
15779 handle_variant_part (child_die, type, fi, template_args, cu);
15780 else if (child_die->tag == DW_TAG_variant)
15781 handle_variant (child_die, type, fi, template_args, cu);
15782 }
15783
15784 /* Finish creating a structure or union type, including filling in
15785 its members and creating a symbol for it. */
15786
15787 static void
15788 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15789 {
15790 struct objfile *objfile = cu->per_objfile->objfile;
15791 struct die_info *child_die;
15792 struct type *type;
15793
15794 type = get_die_type (die, cu);
15795 if (type == NULL)
15796 type = read_structure_type (die, cu);
15797
15798 bool has_template_parameters = false;
15799 if (die->child != NULL && ! die_is_declaration (die, cu))
15800 {
15801 struct field_info fi;
15802 std::vector<struct symbol *> template_args;
15803
15804 child_die = die->child;
15805
15806 while (child_die && child_die->tag)
15807 {
15808 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15809 child_die = child_die->sibling;
15810 }
15811
15812 /* Attach template arguments to type. */
15813 if (!template_args.empty ())
15814 {
15815 has_template_parameters = true;
15816 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15817 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15818 TYPE_TEMPLATE_ARGUMENTS (type)
15819 = XOBNEWVEC (&objfile->objfile_obstack,
15820 struct symbol *,
15821 TYPE_N_TEMPLATE_ARGUMENTS (type));
15822 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15823 template_args.data (),
15824 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15825 * sizeof (struct symbol *)));
15826 }
15827
15828 /* Attach fields and member functions to the type. */
15829 if (fi.nfields () > 0)
15830 dwarf2_attach_fields_to_type (&fi, type, cu);
15831 if (!fi.fnfieldlists.empty ())
15832 {
15833 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15834
15835 /* Get the type which refers to the base class (possibly this
15836 class itself) which contains the vtable pointer for the current
15837 class from the DW_AT_containing_type attribute. This use of
15838 DW_AT_containing_type is a GNU extension. */
15839
15840 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15841 {
15842 struct type *t = die_containing_type (die, cu);
15843
15844 set_type_vptr_basetype (type, t);
15845 if (type == t)
15846 {
15847 int i;
15848
15849 /* Our own class provides vtbl ptr. */
15850 for (i = t->num_fields () - 1;
15851 i >= TYPE_N_BASECLASSES (t);
15852 --i)
15853 {
15854 const char *fieldname = TYPE_FIELD_NAME (t, i);
15855
15856 if (is_vtable_name (fieldname, cu))
15857 {
15858 set_type_vptr_fieldno (type, i);
15859 break;
15860 }
15861 }
15862
15863 /* Complain if virtual function table field not found. */
15864 if (i < TYPE_N_BASECLASSES (t))
15865 complaint (_("virtual function table pointer "
15866 "not found when defining class '%s'"),
15867 type->name () ? type->name () : "");
15868 }
15869 else
15870 {
15871 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15872 }
15873 }
15874 else if (cu->producer
15875 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15876 {
15877 /* The IBM XLC compiler does not provide direct indication
15878 of the containing type, but the vtable pointer is
15879 always named __vfp. */
15880
15881 int i;
15882
15883 for (i = type->num_fields () - 1;
15884 i >= TYPE_N_BASECLASSES (type);
15885 --i)
15886 {
15887 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15888 {
15889 set_type_vptr_fieldno (type, i);
15890 set_type_vptr_basetype (type, type);
15891 break;
15892 }
15893 }
15894 }
15895 }
15896
15897 /* Copy fi.typedef_field_list linked list elements content into the
15898 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15899 if (!fi.typedef_field_list.empty ())
15900 {
15901 int count = fi.typedef_field_list.size ();
15902
15903 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15904 TYPE_TYPEDEF_FIELD_ARRAY (type)
15905 = ((struct decl_field *)
15906 TYPE_ALLOC (type,
15907 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15908 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15909
15910 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15911 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15912 }
15913
15914 /* Copy fi.nested_types_list linked list elements content into the
15915 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15916 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15917 {
15918 int count = fi.nested_types_list.size ();
15919
15920 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15921 TYPE_NESTED_TYPES_ARRAY (type)
15922 = ((struct decl_field *)
15923 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15924 TYPE_NESTED_TYPES_COUNT (type) = count;
15925
15926 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15927 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15928 }
15929 }
15930
15931 quirk_gcc_member_function_pointer (type, objfile);
15932 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15933 cu->rust_unions.push_back (type);
15934
15935 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15936 snapshots) has been known to create a die giving a declaration
15937 for a class that has, as a child, a die giving a definition for a
15938 nested class. So we have to process our children even if the
15939 current die is a declaration. Normally, of course, a declaration
15940 won't have any children at all. */
15941
15942 child_die = die->child;
15943
15944 while (child_die != NULL && child_die->tag)
15945 {
15946 if (child_die->tag == DW_TAG_member
15947 || child_die->tag == DW_TAG_variable
15948 || child_die->tag == DW_TAG_inheritance
15949 || child_die->tag == DW_TAG_template_value_param
15950 || child_die->tag == DW_TAG_template_type_param)
15951 {
15952 /* Do nothing. */
15953 }
15954 else
15955 process_die (child_die, cu);
15956
15957 child_die = child_die->sibling;
15958 }
15959
15960 /* Do not consider external references. According to the DWARF standard,
15961 these DIEs are identified by the fact that they have no byte_size
15962 attribute, and a declaration attribute. */
15963 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15964 || !die_is_declaration (die, cu)
15965 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15966 {
15967 struct symbol *sym = new_symbol (die, type, cu);
15968
15969 if (has_template_parameters)
15970 {
15971 struct symtab *symtab;
15972 if (sym != nullptr)
15973 symtab = symbol_symtab (sym);
15974 else if (cu->line_header != nullptr)
15975 {
15976 /* Any related symtab will do. */
15977 symtab
15978 = cu->line_header->file_names ()[0].symtab;
15979 }
15980 else
15981 {
15982 symtab = nullptr;
15983 complaint (_("could not find suitable "
15984 "symtab for template parameter"
15985 " - DIE at %s [in module %s]"),
15986 sect_offset_str (die->sect_off),
15987 objfile_name (objfile));
15988 }
15989
15990 if (symtab != nullptr)
15991 {
15992 /* Make sure that the symtab is set on the new symbols.
15993 Even though they don't appear in this symtab directly,
15994 other parts of gdb assume that symbols do, and this is
15995 reasonably true. */
15996 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15997 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15998 }
15999 }
16000 }
16001 }
16002
16003 /* Assuming DIE is an enumeration type, and TYPE is its associated
16004 type, update TYPE using some information only available in DIE's
16005 children. In particular, the fields are computed. */
16006
16007 static void
16008 update_enumeration_type_from_children (struct die_info *die,
16009 struct type *type,
16010 struct dwarf2_cu *cu)
16011 {
16012 struct die_info *child_die;
16013 int unsigned_enum = 1;
16014 int flag_enum = 1;
16015
16016 auto_obstack obstack;
16017 std::vector<struct field> fields;
16018
16019 for (child_die = die->child;
16020 child_die != NULL && child_die->tag;
16021 child_die = child_die->sibling)
16022 {
16023 struct attribute *attr;
16024 LONGEST value;
16025 const gdb_byte *bytes;
16026 struct dwarf2_locexpr_baton *baton;
16027 const char *name;
16028
16029 if (child_die->tag != DW_TAG_enumerator)
16030 continue;
16031
16032 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16033 if (attr == NULL)
16034 continue;
16035
16036 name = dwarf2_name (child_die, cu);
16037 if (name == NULL)
16038 name = "<anonymous enumerator>";
16039
16040 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16041 &value, &bytes, &baton);
16042 if (value < 0)
16043 {
16044 unsigned_enum = 0;
16045 flag_enum = 0;
16046 }
16047 else
16048 {
16049 if (count_one_bits_ll (value) >= 2)
16050 flag_enum = 0;
16051 }
16052
16053 fields.emplace_back ();
16054 struct field &field = fields.back ();
16055 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16056 SET_FIELD_ENUMVAL (field, value);
16057 }
16058
16059 if (!fields.empty ())
16060 {
16061 type->set_num_fields (fields.size ());
16062 type->set_fields
16063 ((struct field *)
16064 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16065 memcpy (type->fields (), fields.data (),
16066 sizeof (struct field) * fields.size ());
16067 }
16068
16069 if (unsigned_enum)
16070 TYPE_UNSIGNED (type) = 1;
16071 if (flag_enum)
16072 TYPE_FLAG_ENUM (type) = 1;
16073 }
16074
16075 /* Given a DW_AT_enumeration_type die, set its type. We do not
16076 complete the type's fields yet, or create any symbols. */
16077
16078 static struct type *
16079 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16080 {
16081 struct objfile *objfile = cu->per_objfile->objfile;
16082 struct type *type;
16083 struct attribute *attr;
16084 const char *name;
16085
16086 /* If the definition of this type lives in .debug_types, read that type.
16087 Don't follow DW_AT_specification though, that will take us back up
16088 the chain and we want to go down. */
16089 attr = die->attr (DW_AT_signature);
16090 if (attr != nullptr)
16091 {
16092 type = get_DW_AT_signature_type (die, attr, cu);
16093
16094 /* The type's CU may not be the same as CU.
16095 Ensure TYPE is recorded with CU in die_type_hash. */
16096 return set_die_type (die, type, cu);
16097 }
16098
16099 type = alloc_type (objfile);
16100
16101 type->set_code (TYPE_CODE_ENUM);
16102 name = dwarf2_full_name (NULL, die, cu);
16103 if (name != NULL)
16104 type->set_name (name);
16105
16106 attr = dwarf2_attr (die, DW_AT_type, cu);
16107 if (attr != NULL)
16108 {
16109 struct type *underlying_type = die_type (die, cu);
16110
16111 TYPE_TARGET_TYPE (type) = underlying_type;
16112 }
16113
16114 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16115 if (attr != nullptr)
16116 {
16117 TYPE_LENGTH (type) = DW_UNSND (attr);
16118 }
16119 else
16120 {
16121 TYPE_LENGTH (type) = 0;
16122 }
16123
16124 maybe_set_alignment (cu, die, type);
16125
16126 /* The enumeration DIE can be incomplete. In Ada, any type can be
16127 declared as private in the package spec, and then defined only
16128 inside the package body. Such types are known as Taft Amendment
16129 Types. When another package uses such a type, an incomplete DIE
16130 may be generated by the compiler. */
16131 if (die_is_declaration (die, cu))
16132 TYPE_STUB (type) = 1;
16133
16134 /* If this type has an underlying type that is not a stub, then we
16135 may use its attributes. We always use the "unsigned" attribute
16136 in this situation, because ordinarily we guess whether the type
16137 is unsigned -- but the guess can be wrong and the underlying type
16138 can tell us the reality. However, we defer to a local size
16139 attribute if one exists, because this lets the compiler override
16140 the underlying type if needed. */
16141 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16142 {
16143 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16144 underlying_type = check_typedef (underlying_type);
16145 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16146 if (TYPE_LENGTH (type) == 0)
16147 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16148 if (TYPE_RAW_ALIGN (type) == 0
16149 && TYPE_RAW_ALIGN (underlying_type) != 0)
16150 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16151 }
16152
16153 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16154
16155 set_die_type (die, type, cu);
16156
16157 /* Finish the creation of this type by using the enum's children.
16158 Note that, as usual, this must come after set_die_type to avoid
16159 infinite recursion when trying to compute the names of the
16160 enumerators. */
16161 update_enumeration_type_from_children (die, type, cu);
16162
16163 return type;
16164 }
16165
16166 /* Given a pointer to a die which begins an enumeration, process all
16167 the dies that define the members of the enumeration, and create the
16168 symbol for the enumeration type.
16169
16170 NOTE: We reverse the order of the element list. */
16171
16172 static void
16173 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16174 {
16175 struct type *this_type;
16176
16177 this_type = get_die_type (die, cu);
16178 if (this_type == NULL)
16179 this_type = read_enumeration_type (die, cu);
16180
16181 if (die->child != NULL)
16182 {
16183 struct die_info *child_die;
16184 const char *name;
16185
16186 child_die = die->child;
16187 while (child_die && child_die->tag)
16188 {
16189 if (child_die->tag != DW_TAG_enumerator)
16190 {
16191 process_die (child_die, cu);
16192 }
16193 else
16194 {
16195 name = dwarf2_name (child_die, cu);
16196 if (name)
16197 new_symbol (child_die, this_type, cu);
16198 }
16199
16200 child_die = child_die->sibling;
16201 }
16202 }
16203
16204 /* If we are reading an enum from a .debug_types unit, and the enum
16205 is a declaration, and the enum is not the signatured type in the
16206 unit, then we do not want to add a symbol for it. Adding a
16207 symbol would in some cases obscure the true definition of the
16208 enum, giving users an incomplete type when the definition is
16209 actually available. Note that we do not want to do this for all
16210 enums which are just declarations, because C++0x allows forward
16211 enum declarations. */
16212 if (cu->per_cu->is_debug_types
16213 && die_is_declaration (die, cu))
16214 {
16215 struct signatured_type *sig_type;
16216
16217 sig_type = (struct signatured_type *) cu->per_cu;
16218 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16219 if (sig_type->type_offset_in_section != die->sect_off)
16220 return;
16221 }
16222
16223 new_symbol (die, this_type, cu);
16224 }
16225
16226 /* Extract all information from a DW_TAG_array_type DIE and put it in
16227 the DIE's type field. For now, this only handles one dimensional
16228 arrays. */
16229
16230 static struct type *
16231 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16232 {
16233 struct objfile *objfile = cu->per_objfile->objfile;
16234 struct die_info *child_die;
16235 struct type *type;
16236 struct type *element_type, *range_type, *index_type;
16237 struct attribute *attr;
16238 const char *name;
16239 struct dynamic_prop *byte_stride_prop = NULL;
16240 unsigned int bit_stride = 0;
16241
16242 element_type = die_type (die, cu);
16243
16244 /* The die_type call above may have already set the type for this DIE. */
16245 type = get_die_type (die, cu);
16246 if (type)
16247 return type;
16248
16249 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16250 if (attr != NULL)
16251 {
16252 int stride_ok;
16253 struct type *prop_type = cu->addr_sized_int_type (false);
16254
16255 byte_stride_prop
16256 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16257 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16258 prop_type);
16259 if (!stride_ok)
16260 {
16261 complaint (_("unable to read array DW_AT_byte_stride "
16262 " - DIE at %s [in module %s]"),
16263 sect_offset_str (die->sect_off),
16264 objfile_name (cu->per_objfile->objfile));
16265 /* Ignore this attribute. We will likely not be able to print
16266 arrays of this type correctly, but there is little we can do
16267 to help if we cannot read the attribute's value. */
16268 byte_stride_prop = NULL;
16269 }
16270 }
16271
16272 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16273 if (attr != NULL)
16274 bit_stride = DW_UNSND (attr);
16275
16276 /* Irix 6.2 native cc creates array types without children for
16277 arrays with unspecified length. */
16278 if (die->child == NULL)
16279 {
16280 index_type = objfile_type (objfile)->builtin_int;
16281 range_type = create_static_range_type (NULL, index_type, 0, -1);
16282 type = create_array_type_with_stride (NULL, element_type, range_type,
16283 byte_stride_prop, bit_stride);
16284 return set_die_type (die, type, cu);
16285 }
16286
16287 std::vector<struct type *> range_types;
16288 child_die = die->child;
16289 while (child_die && child_die->tag)
16290 {
16291 if (child_die->tag == DW_TAG_subrange_type)
16292 {
16293 struct type *child_type = read_type_die (child_die, cu);
16294
16295 if (child_type != NULL)
16296 {
16297 /* The range type was succesfully read. Save it for the
16298 array type creation. */
16299 range_types.push_back (child_type);
16300 }
16301 }
16302 child_die = child_die->sibling;
16303 }
16304
16305 /* Dwarf2 dimensions are output from left to right, create the
16306 necessary array types in backwards order. */
16307
16308 type = element_type;
16309
16310 if (read_array_order (die, cu) == DW_ORD_col_major)
16311 {
16312 int i = 0;
16313
16314 while (i < range_types.size ())
16315 type = create_array_type_with_stride (NULL, type, range_types[i++],
16316 byte_stride_prop, bit_stride);
16317 }
16318 else
16319 {
16320 size_t ndim = range_types.size ();
16321 while (ndim-- > 0)
16322 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16323 byte_stride_prop, bit_stride);
16324 }
16325
16326 /* Understand Dwarf2 support for vector types (like they occur on
16327 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16328 array type. This is not part of the Dwarf2/3 standard yet, but a
16329 custom vendor extension. The main difference between a regular
16330 array and the vector variant is that vectors are passed by value
16331 to functions. */
16332 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16333 if (attr != nullptr)
16334 make_vector_type (type);
16335
16336 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16337 implementation may choose to implement triple vectors using this
16338 attribute. */
16339 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16340 if (attr != nullptr)
16341 {
16342 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16343 TYPE_LENGTH (type) = DW_UNSND (attr);
16344 else
16345 complaint (_("DW_AT_byte_size for array type smaller "
16346 "than the total size of elements"));
16347 }
16348
16349 name = dwarf2_name (die, cu);
16350 if (name)
16351 type->set_name (name);
16352
16353 maybe_set_alignment (cu, die, type);
16354
16355 /* Install the type in the die. */
16356 set_die_type (die, type, cu);
16357
16358 /* set_die_type should be already done. */
16359 set_descriptive_type (type, die, cu);
16360
16361 return type;
16362 }
16363
16364 static enum dwarf_array_dim_ordering
16365 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16366 {
16367 struct attribute *attr;
16368
16369 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16370
16371 if (attr != nullptr)
16372 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16373
16374 /* GNU F77 is a special case, as at 08/2004 array type info is the
16375 opposite order to the dwarf2 specification, but data is still
16376 laid out as per normal fortran.
16377
16378 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16379 version checking. */
16380
16381 if (cu->language == language_fortran
16382 && cu->producer && strstr (cu->producer, "GNU F77"))
16383 {
16384 return DW_ORD_row_major;
16385 }
16386
16387 switch (cu->language_defn->la_array_ordering)
16388 {
16389 case array_column_major:
16390 return DW_ORD_col_major;
16391 case array_row_major:
16392 default:
16393 return DW_ORD_row_major;
16394 };
16395 }
16396
16397 /* Extract all information from a DW_TAG_set_type DIE and put it in
16398 the DIE's type field. */
16399
16400 static struct type *
16401 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16402 {
16403 struct type *domain_type, *set_type;
16404 struct attribute *attr;
16405
16406 domain_type = die_type (die, cu);
16407
16408 /* The die_type call above may have already set the type for this DIE. */
16409 set_type = get_die_type (die, cu);
16410 if (set_type)
16411 return set_type;
16412
16413 set_type = create_set_type (NULL, domain_type);
16414
16415 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16416 if (attr != nullptr)
16417 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16418
16419 maybe_set_alignment (cu, die, set_type);
16420
16421 return set_die_type (die, set_type, cu);
16422 }
16423
16424 /* A helper for read_common_block that creates a locexpr baton.
16425 SYM is the symbol which we are marking as computed.
16426 COMMON_DIE is the DIE for the common block.
16427 COMMON_LOC is the location expression attribute for the common
16428 block itself.
16429 MEMBER_LOC is the location expression attribute for the particular
16430 member of the common block that we are processing.
16431 CU is the CU from which the above come. */
16432
16433 static void
16434 mark_common_block_symbol_computed (struct symbol *sym,
16435 struct die_info *common_die,
16436 struct attribute *common_loc,
16437 struct attribute *member_loc,
16438 struct dwarf2_cu *cu)
16439 {
16440 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16441 struct objfile *objfile = per_objfile->objfile;
16442 struct dwarf2_locexpr_baton *baton;
16443 gdb_byte *ptr;
16444 unsigned int cu_off;
16445 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16446 LONGEST offset = 0;
16447
16448 gdb_assert (common_loc && member_loc);
16449 gdb_assert (common_loc->form_is_block ());
16450 gdb_assert (member_loc->form_is_block ()
16451 || member_loc->form_is_constant ());
16452
16453 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16454 baton->per_objfile = per_objfile;
16455 baton->per_cu = cu->per_cu;
16456 gdb_assert (baton->per_cu);
16457
16458 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16459
16460 if (member_loc->form_is_constant ())
16461 {
16462 offset = member_loc->constant_value (0);
16463 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16464 }
16465 else
16466 baton->size += DW_BLOCK (member_loc)->size;
16467
16468 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16469 baton->data = ptr;
16470
16471 *ptr++ = DW_OP_call4;
16472 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16473 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16474 ptr += 4;
16475
16476 if (member_loc->form_is_constant ())
16477 {
16478 *ptr++ = DW_OP_addr;
16479 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16480 ptr += cu->header.addr_size;
16481 }
16482 else
16483 {
16484 /* We have to copy the data here, because DW_OP_call4 will only
16485 use a DW_AT_location attribute. */
16486 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16487 ptr += DW_BLOCK (member_loc)->size;
16488 }
16489
16490 *ptr++ = DW_OP_plus;
16491 gdb_assert (ptr - baton->data == baton->size);
16492
16493 SYMBOL_LOCATION_BATON (sym) = baton;
16494 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16495 }
16496
16497 /* Create appropriate locally-scoped variables for all the
16498 DW_TAG_common_block entries. Also create a struct common_block
16499 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16500 is used to separate the common blocks name namespace from regular
16501 variable names. */
16502
16503 static void
16504 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16505 {
16506 struct attribute *attr;
16507
16508 attr = dwarf2_attr (die, DW_AT_location, cu);
16509 if (attr != nullptr)
16510 {
16511 /* Support the .debug_loc offsets. */
16512 if (attr->form_is_block ())
16513 {
16514 /* Ok. */
16515 }
16516 else if (attr->form_is_section_offset ())
16517 {
16518 dwarf2_complex_location_expr_complaint ();
16519 attr = NULL;
16520 }
16521 else
16522 {
16523 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16524 "common block member");
16525 attr = NULL;
16526 }
16527 }
16528
16529 if (die->child != NULL)
16530 {
16531 struct objfile *objfile = cu->per_objfile->objfile;
16532 struct die_info *child_die;
16533 size_t n_entries = 0, size;
16534 struct common_block *common_block;
16535 struct symbol *sym;
16536
16537 for (child_die = die->child;
16538 child_die && child_die->tag;
16539 child_die = child_die->sibling)
16540 ++n_entries;
16541
16542 size = (sizeof (struct common_block)
16543 + (n_entries - 1) * sizeof (struct symbol *));
16544 common_block
16545 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16546 size);
16547 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16548 common_block->n_entries = 0;
16549
16550 for (child_die = die->child;
16551 child_die && child_die->tag;
16552 child_die = child_die->sibling)
16553 {
16554 /* Create the symbol in the DW_TAG_common_block block in the current
16555 symbol scope. */
16556 sym = new_symbol (child_die, NULL, cu);
16557 if (sym != NULL)
16558 {
16559 struct attribute *member_loc;
16560
16561 common_block->contents[common_block->n_entries++] = sym;
16562
16563 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16564 cu);
16565 if (member_loc)
16566 {
16567 /* GDB has handled this for a long time, but it is
16568 not specified by DWARF. It seems to have been
16569 emitted by gfortran at least as recently as:
16570 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16571 complaint (_("Variable in common block has "
16572 "DW_AT_data_member_location "
16573 "- DIE at %s [in module %s]"),
16574 sect_offset_str (child_die->sect_off),
16575 objfile_name (objfile));
16576
16577 if (member_loc->form_is_section_offset ())
16578 dwarf2_complex_location_expr_complaint ();
16579 else if (member_loc->form_is_constant ()
16580 || member_loc->form_is_block ())
16581 {
16582 if (attr != nullptr)
16583 mark_common_block_symbol_computed (sym, die, attr,
16584 member_loc, cu);
16585 }
16586 else
16587 dwarf2_complex_location_expr_complaint ();
16588 }
16589 }
16590 }
16591
16592 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16593 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16594 }
16595 }
16596
16597 /* Create a type for a C++ namespace. */
16598
16599 static struct type *
16600 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16601 {
16602 struct objfile *objfile = cu->per_objfile->objfile;
16603 const char *previous_prefix, *name;
16604 int is_anonymous;
16605 struct type *type;
16606
16607 /* For extensions, reuse the type of the original namespace. */
16608 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16609 {
16610 struct die_info *ext_die;
16611 struct dwarf2_cu *ext_cu = cu;
16612
16613 ext_die = dwarf2_extension (die, &ext_cu);
16614 type = read_type_die (ext_die, ext_cu);
16615
16616 /* EXT_CU may not be the same as CU.
16617 Ensure TYPE is recorded with CU in die_type_hash. */
16618 return set_die_type (die, type, cu);
16619 }
16620
16621 name = namespace_name (die, &is_anonymous, cu);
16622
16623 /* Now build the name of the current namespace. */
16624
16625 previous_prefix = determine_prefix (die, cu);
16626 if (previous_prefix[0] != '\0')
16627 name = typename_concat (&objfile->objfile_obstack,
16628 previous_prefix, name, 0, cu);
16629
16630 /* Create the type. */
16631 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16632
16633 return set_die_type (die, type, cu);
16634 }
16635
16636 /* Read a namespace scope. */
16637
16638 static void
16639 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16640 {
16641 struct objfile *objfile = cu->per_objfile->objfile;
16642 int is_anonymous;
16643
16644 /* Add a symbol associated to this if we haven't seen the namespace
16645 before. Also, add a using directive if it's an anonymous
16646 namespace. */
16647
16648 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16649 {
16650 struct type *type;
16651
16652 type = read_type_die (die, cu);
16653 new_symbol (die, type, cu);
16654
16655 namespace_name (die, &is_anonymous, cu);
16656 if (is_anonymous)
16657 {
16658 const char *previous_prefix = determine_prefix (die, cu);
16659
16660 std::vector<const char *> excludes;
16661 add_using_directive (using_directives (cu),
16662 previous_prefix, type->name (), NULL,
16663 NULL, excludes, 0, &objfile->objfile_obstack);
16664 }
16665 }
16666
16667 if (die->child != NULL)
16668 {
16669 struct die_info *child_die = die->child;
16670
16671 while (child_die && child_die->tag)
16672 {
16673 process_die (child_die, cu);
16674 child_die = child_die->sibling;
16675 }
16676 }
16677 }
16678
16679 /* Read a Fortran module as type. This DIE can be only a declaration used for
16680 imported module. Still we need that type as local Fortran "use ... only"
16681 declaration imports depend on the created type in determine_prefix. */
16682
16683 static struct type *
16684 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16685 {
16686 struct objfile *objfile = cu->per_objfile->objfile;
16687 const char *module_name;
16688 struct type *type;
16689
16690 module_name = dwarf2_name (die, cu);
16691 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16692
16693 return set_die_type (die, type, cu);
16694 }
16695
16696 /* Read a Fortran module. */
16697
16698 static void
16699 read_module (struct die_info *die, struct dwarf2_cu *cu)
16700 {
16701 struct die_info *child_die = die->child;
16702 struct type *type;
16703
16704 type = read_type_die (die, cu);
16705 new_symbol (die, type, cu);
16706
16707 while (child_die && child_die->tag)
16708 {
16709 process_die (child_die, cu);
16710 child_die = child_die->sibling;
16711 }
16712 }
16713
16714 /* Return the name of the namespace represented by DIE. Set
16715 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16716 namespace. */
16717
16718 static const char *
16719 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16720 {
16721 struct die_info *current_die;
16722 const char *name = NULL;
16723
16724 /* Loop through the extensions until we find a name. */
16725
16726 for (current_die = die;
16727 current_die != NULL;
16728 current_die = dwarf2_extension (die, &cu))
16729 {
16730 /* We don't use dwarf2_name here so that we can detect the absence
16731 of a name -> anonymous namespace. */
16732 name = dwarf2_string_attr (die, DW_AT_name, cu);
16733
16734 if (name != NULL)
16735 break;
16736 }
16737
16738 /* Is it an anonymous namespace? */
16739
16740 *is_anonymous = (name == NULL);
16741 if (*is_anonymous)
16742 name = CP_ANONYMOUS_NAMESPACE_STR;
16743
16744 return name;
16745 }
16746
16747 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16748 the user defined type vector. */
16749
16750 static struct type *
16751 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16752 {
16753 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16754 struct comp_unit_head *cu_header = &cu->header;
16755 struct type *type;
16756 struct attribute *attr_byte_size;
16757 struct attribute *attr_address_class;
16758 int byte_size, addr_class;
16759 struct type *target_type;
16760
16761 target_type = die_type (die, cu);
16762
16763 /* The die_type call above may have already set the type for this DIE. */
16764 type = get_die_type (die, cu);
16765 if (type)
16766 return type;
16767
16768 type = lookup_pointer_type (target_type);
16769
16770 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16771 if (attr_byte_size)
16772 byte_size = DW_UNSND (attr_byte_size);
16773 else
16774 byte_size = cu_header->addr_size;
16775
16776 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16777 if (attr_address_class)
16778 addr_class = DW_UNSND (attr_address_class);
16779 else
16780 addr_class = DW_ADDR_none;
16781
16782 ULONGEST alignment = get_alignment (cu, die);
16783
16784 /* If the pointer size, alignment, or address class is different
16785 than the default, create a type variant marked as such and set
16786 the length accordingly. */
16787 if (TYPE_LENGTH (type) != byte_size
16788 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16789 && alignment != TYPE_RAW_ALIGN (type))
16790 || addr_class != DW_ADDR_none)
16791 {
16792 if (gdbarch_address_class_type_flags_p (gdbarch))
16793 {
16794 int type_flags;
16795
16796 type_flags = gdbarch_address_class_type_flags
16797 (gdbarch, byte_size, addr_class);
16798 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16799 == 0);
16800 type = make_type_with_address_space (type, type_flags);
16801 }
16802 else if (TYPE_LENGTH (type) != byte_size)
16803 {
16804 complaint (_("invalid pointer size %d"), byte_size);
16805 }
16806 else if (TYPE_RAW_ALIGN (type) != alignment)
16807 {
16808 complaint (_("Invalid DW_AT_alignment"
16809 " - DIE at %s [in module %s]"),
16810 sect_offset_str (die->sect_off),
16811 objfile_name (cu->per_objfile->objfile));
16812 }
16813 else
16814 {
16815 /* Should we also complain about unhandled address classes? */
16816 }
16817 }
16818
16819 TYPE_LENGTH (type) = byte_size;
16820 set_type_align (type, alignment);
16821 return set_die_type (die, type, cu);
16822 }
16823
16824 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16825 the user defined type vector. */
16826
16827 static struct type *
16828 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16829 {
16830 struct type *type;
16831 struct type *to_type;
16832 struct type *domain;
16833
16834 to_type = die_type (die, cu);
16835 domain = die_containing_type (die, cu);
16836
16837 /* The calls above may have already set the type for this DIE. */
16838 type = get_die_type (die, cu);
16839 if (type)
16840 return type;
16841
16842 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16843 type = lookup_methodptr_type (to_type);
16844 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16845 {
16846 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16847
16848 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16849 to_type->fields (), to_type->num_fields (),
16850 TYPE_VARARGS (to_type));
16851 type = lookup_methodptr_type (new_type);
16852 }
16853 else
16854 type = lookup_memberptr_type (to_type, domain);
16855
16856 return set_die_type (die, type, cu);
16857 }
16858
16859 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16860 the user defined type vector. */
16861
16862 static struct type *
16863 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16864 enum type_code refcode)
16865 {
16866 struct comp_unit_head *cu_header = &cu->header;
16867 struct type *type, *target_type;
16868 struct attribute *attr;
16869
16870 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16871
16872 target_type = die_type (die, cu);
16873
16874 /* The die_type call above may have already set the type for this DIE. */
16875 type = get_die_type (die, cu);
16876 if (type)
16877 return type;
16878
16879 type = lookup_reference_type (target_type, refcode);
16880 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16881 if (attr != nullptr)
16882 {
16883 TYPE_LENGTH (type) = DW_UNSND (attr);
16884 }
16885 else
16886 {
16887 TYPE_LENGTH (type) = cu_header->addr_size;
16888 }
16889 maybe_set_alignment (cu, die, type);
16890 return set_die_type (die, type, cu);
16891 }
16892
16893 /* Add the given cv-qualifiers to the element type of the array. GCC
16894 outputs DWARF type qualifiers that apply to an array, not the
16895 element type. But GDB relies on the array element type to carry
16896 the cv-qualifiers. This mimics section 6.7.3 of the C99
16897 specification. */
16898
16899 static struct type *
16900 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16901 struct type *base_type, int cnst, int voltl)
16902 {
16903 struct type *el_type, *inner_array;
16904
16905 base_type = copy_type (base_type);
16906 inner_array = base_type;
16907
16908 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16909 {
16910 TYPE_TARGET_TYPE (inner_array) =
16911 copy_type (TYPE_TARGET_TYPE (inner_array));
16912 inner_array = TYPE_TARGET_TYPE (inner_array);
16913 }
16914
16915 el_type = TYPE_TARGET_TYPE (inner_array);
16916 cnst |= TYPE_CONST (el_type);
16917 voltl |= TYPE_VOLATILE (el_type);
16918 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16919
16920 return set_die_type (die, base_type, cu);
16921 }
16922
16923 static struct type *
16924 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16925 {
16926 struct type *base_type, *cv_type;
16927
16928 base_type = die_type (die, cu);
16929
16930 /* The die_type call above may have already set the type for this DIE. */
16931 cv_type = get_die_type (die, cu);
16932 if (cv_type)
16933 return cv_type;
16934
16935 /* In case the const qualifier is applied to an array type, the element type
16936 is so qualified, not the array type (section 6.7.3 of C99). */
16937 if (base_type->code () == TYPE_CODE_ARRAY)
16938 return add_array_cv_type (die, cu, base_type, 1, 0);
16939
16940 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16941 return set_die_type (die, cv_type, cu);
16942 }
16943
16944 static struct type *
16945 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16946 {
16947 struct type *base_type, *cv_type;
16948
16949 base_type = die_type (die, cu);
16950
16951 /* The die_type call above may have already set the type for this DIE. */
16952 cv_type = get_die_type (die, cu);
16953 if (cv_type)
16954 return cv_type;
16955
16956 /* In case the volatile qualifier is applied to an array type, the
16957 element type is so qualified, not the array type (section 6.7.3
16958 of C99). */
16959 if (base_type->code () == TYPE_CODE_ARRAY)
16960 return add_array_cv_type (die, cu, base_type, 0, 1);
16961
16962 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16963 return set_die_type (die, cv_type, cu);
16964 }
16965
16966 /* Handle DW_TAG_restrict_type. */
16967
16968 static struct type *
16969 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16970 {
16971 struct type *base_type, *cv_type;
16972
16973 base_type = die_type (die, cu);
16974
16975 /* The die_type call above may have already set the type for this DIE. */
16976 cv_type = get_die_type (die, cu);
16977 if (cv_type)
16978 return cv_type;
16979
16980 cv_type = make_restrict_type (base_type);
16981 return set_die_type (die, cv_type, cu);
16982 }
16983
16984 /* Handle DW_TAG_atomic_type. */
16985
16986 static struct type *
16987 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16988 {
16989 struct type *base_type, *cv_type;
16990
16991 base_type = die_type (die, cu);
16992
16993 /* The die_type call above may have already set the type for this DIE. */
16994 cv_type = get_die_type (die, cu);
16995 if (cv_type)
16996 return cv_type;
16997
16998 cv_type = make_atomic_type (base_type);
16999 return set_die_type (die, cv_type, cu);
17000 }
17001
17002 /* Extract all information from a DW_TAG_string_type DIE and add to
17003 the user defined type vector. It isn't really a user defined type,
17004 but it behaves like one, with other DIE's using an AT_user_def_type
17005 attribute to reference it. */
17006
17007 static struct type *
17008 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17009 {
17010 struct objfile *objfile = cu->per_objfile->objfile;
17011 struct gdbarch *gdbarch = objfile->arch ();
17012 struct type *type, *range_type, *index_type, *char_type;
17013 struct attribute *attr;
17014 struct dynamic_prop prop;
17015 bool length_is_constant = true;
17016 LONGEST length;
17017
17018 /* There are a couple of places where bit sizes might be made use of
17019 when parsing a DW_TAG_string_type, however, no producer that we know
17020 of make use of these. Handling bit sizes that are a multiple of the
17021 byte size is easy enough, but what about other bit sizes? Lets deal
17022 with that problem when we have to. Warn about these attributes being
17023 unsupported, then parse the type and ignore them like we always
17024 have. */
17025 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17026 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17027 {
17028 static bool warning_printed = false;
17029 if (!warning_printed)
17030 {
17031 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17032 "currently supported on DW_TAG_string_type."));
17033 warning_printed = true;
17034 }
17035 }
17036
17037 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17038 if (attr != nullptr && !attr->form_is_constant ())
17039 {
17040 /* The string length describes the location at which the length of
17041 the string can be found. The size of the length field can be
17042 specified with one of the attributes below. */
17043 struct type *prop_type;
17044 struct attribute *len
17045 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17046 if (len == nullptr)
17047 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17048 if (len != nullptr && len->form_is_constant ())
17049 {
17050 /* Pass 0 as the default as we know this attribute is constant
17051 and the default value will not be returned. */
17052 LONGEST sz = len->constant_value (0);
17053 prop_type = cu->per_objfile->int_type (sz, true);
17054 }
17055 else
17056 {
17057 /* If the size is not specified then we assume it is the size of
17058 an address on this target. */
17059 prop_type = cu->addr_sized_int_type (true);
17060 }
17061
17062 /* Convert the attribute into a dynamic property. */
17063 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17064 length = 1;
17065 else
17066 length_is_constant = false;
17067 }
17068 else if (attr != nullptr)
17069 {
17070 /* This DW_AT_string_length just contains the length with no
17071 indirection. There's no need to create a dynamic property in this
17072 case. Pass 0 for the default value as we know it will not be
17073 returned in this case. */
17074 length = attr->constant_value (0);
17075 }
17076 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17077 {
17078 /* We don't currently support non-constant byte sizes for strings. */
17079 length = attr->constant_value (1);
17080 }
17081 else
17082 {
17083 /* Use 1 as a fallback length if we have nothing else. */
17084 length = 1;
17085 }
17086
17087 index_type = objfile_type (objfile)->builtin_int;
17088 if (length_is_constant)
17089 range_type = create_static_range_type (NULL, index_type, 1, length);
17090 else
17091 {
17092 struct dynamic_prop low_bound;
17093
17094 low_bound.kind = PROP_CONST;
17095 low_bound.data.const_val = 1;
17096 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17097 }
17098 char_type = language_string_char_type (cu->language_defn, gdbarch);
17099 type = create_string_type (NULL, char_type, range_type);
17100
17101 return set_die_type (die, type, cu);
17102 }
17103
17104 /* Assuming that DIE corresponds to a function, returns nonzero
17105 if the function is prototyped. */
17106
17107 static int
17108 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17109 {
17110 struct attribute *attr;
17111
17112 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17113 if (attr && (DW_UNSND (attr) != 0))
17114 return 1;
17115
17116 /* The DWARF standard implies that the DW_AT_prototyped attribute
17117 is only meaningful for C, but the concept also extends to other
17118 languages that allow unprototyped functions (Eg: Objective C).
17119 For all other languages, assume that functions are always
17120 prototyped. */
17121 if (cu->language != language_c
17122 && cu->language != language_objc
17123 && cu->language != language_opencl)
17124 return 1;
17125
17126 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17127 prototyped and unprototyped functions; default to prototyped,
17128 since that is more common in modern code (and RealView warns
17129 about unprototyped functions). */
17130 if (producer_is_realview (cu->producer))
17131 return 1;
17132
17133 return 0;
17134 }
17135
17136 /* Handle DIES due to C code like:
17137
17138 struct foo
17139 {
17140 int (*funcp)(int a, long l);
17141 int b;
17142 };
17143
17144 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17145
17146 static struct type *
17147 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17148 {
17149 struct objfile *objfile = cu->per_objfile->objfile;
17150 struct type *type; /* Type that this function returns. */
17151 struct type *ftype; /* Function that returns above type. */
17152 struct attribute *attr;
17153
17154 type = die_type (die, cu);
17155
17156 /* The die_type call above may have already set the type for this DIE. */
17157 ftype = get_die_type (die, cu);
17158 if (ftype)
17159 return ftype;
17160
17161 ftype = lookup_function_type (type);
17162
17163 if (prototyped_function_p (die, cu))
17164 TYPE_PROTOTYPED (ftype) = 1;
17165
17166 /* Store the calling convention in the type if it's available in
17167 the subroutine die. Otherwise set the calling convention to
17168 the default value DW_CC_normal. */
17169 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17170 if (attr != nullptr
17171 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17172 TYPE_CALLING_CONVENTION (ftype)
17173 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17174 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17175 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17176 else
17177 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17178
17179 /* Record whether the function returns normally to its caller or not
17180 if the DWARF producer set that information. */
17181 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17182 if (attr && (DW_UNSND (attr) != 0))
17183 TYPE_NO_RETURN (ftype) = 1;
17184
17185 /* We need to add the subroutine type to the die immediately so
17186 we don't infinitely recurse when dealing with parameters
17187 declared as the same subroutine type. */
17188 set_die_type (die, ftype, cu);
17189
17190 if (die->child != NULL)
17191 {
17192 struct type *void_type = objfile_type (objfile)->builtin_void;
17193 struct die_info *child_die;
17194 int nparams, iparams;
17195
17196 /* Count the number of parameters.
17197 FIXME: GDB currently ignores vararg functions, but knows about
17198 vararg member functions. */
17199 nparams = 0;
17200 child_die = die->child;
17201 while (child_die && child_die->tag)
17202 {
17203 if (child_die->tag == DW_TAG_formal_parameter)
17204 nparams++;
17205 else if (child_die->tag == DW_TAG_unspecified_parameters)
17206 TYPE_VARARGS (ftype) = 1;
17207 child_die = child_die->sibling;
17208 }
17209
17210 /* Allocate storage for parameters and fill them in. */
17211 ftype->set_num_fields (nparams);
17212 ftype->set_fields
17213 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17214
17215 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17216 even if we error out during the parameters reading below. */
17217 for (iparams = 0; iparams < nparams; iparams++)
17218 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17219
17220 iparams = 0;
17221 child_die = die->child;
17222 while (child_die && child_die->tag)
17223 {
17224 if (child_die->tag == DW_TAG_formal_parameter)
17225 {
17226 struct type *arg_type;
17227
17228 /* DWARF version 2 has no clean way to discern C++
17229 static and non-static member functions. G++ helps
17230 GDB by marking the first parameter for non-static
17231 member functions (which is the this pointer) as
17232 artificial. We pass this information to
17233 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17234
17235 DWARF version 3 added DW_AT_object_pointer, which GCC
17236 4.5 does not yet generate. */
17237 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17238 if (attr != nullptr)
17239 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17240 else
17241 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17242 arg_type = die_type (child_die, cu);
17243
17244 /* RealView does not mark THIS as const, which the testsuite
17245 expects. GCC marks THIS as const in method definitions,
17246 but not in the class specifications (GCC PR 43053). */
17247 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17248 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17249 {
17250 int is_this = 0;
17251 struct dwarf2_cu *arg_cu = cu;
17252 const char *name = dwarf2_name (child_die, cu);
17253
17254 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17255 if (attr != nullptr)
17256 {
17257 /* If the compiler emits this, use it. */
17258 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17259 is_this = 1;
17260 }
17261 else if (name && strcmp (name, "this") == 0)
17262 /* Function definitions will have the argument names. */
17263 is_this = 1;
17264 else if (name == NULL && iparams == 0)
17265 /* Declarations may not have the names, so like
17266 elsewhere in GDB, assume an artificial first
17267 argument is "this". */
17268 is_this = 1;
17269
17270 if (is_this)
17271 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17272 arg_type, 0);
17273 }
17274
17275 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17276 iparams++;
17277 }
17278 child_die = child_die->sibling;
17279 }
17280 }
17281
17282 return ftype;
17283 }
17284
17285 static struct type *
17286 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17287 {
17288 struct objfile *objfile = cu->per_objfile->objfile;
17289 const char *name = NULL;
17290 struct type *this_type, *target_type;
17291
17292 name = dwarf2_full_name (NULL, die, cu);
17293 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17294 TYPE_TARGET_STUB (this_type) = 1;
17295 set_die_type (die, this_type, cu);
17296 target_type = die_type (die, cu);
17297 if (target_type != this_type)
17298 TYPE_TARGET_TYPE (this_type) = target_type;
17299 else
17300 {
17301 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17302 spec and cause infinite loops in GDB. */
17303 complaint (_("Self-referential DW_TAG_typedef "
17304 "- DIE at %s [in module %s]"),
17305 sect_offset_str (die->sect_off), objfile_name (objfile));
17306 TYPE_TARGET_TYPE (this_type) = NULL;
17307 }
17308 if (name == NULL)
17309 {
17310 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17311 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17312 Handle these by just returning the target type, rather than
17313 constructing an anonymous typedef type and trying to handle this
17314 elsewhere. */
17315 set_die_type (die, target_type, cu);
17316 return target_type;
17317 }
17318 return this_type;
17319 }
17320
17321 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17322 (which may be different from NAME) to the architecture back-end to allow
17323 it to guess the correct format if necessary. */
17324
17325 static struct type *
17326 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17327 const char *name_hint, enum bfd_endian byte_order)
17328 {
17329 struct gdbarch *gdbarch = objfile->arch ();
17330 const struct floatformat **format;
17331 struct type *type;
17332
17333 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17334 if (format)
17335 type = init_float_type (objfile, bits, name, format, byte_order);
17336 else
17337 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17338
17339 return type;
17340 }
17341
17342 /* Allocate an integer type of size BITS and name NAME. */
17343
17344 static struct type *
17345 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17346 int bits, int unsigned_p, const char *name)
17347 {
17348 struct type *type;
17349
17350 /* Versions of Intel's C Compiler generate an integer type called "void"
17351 instead of using DW_TAG_unspecified_type. This has been seen on
17352 at least versions 14, 17, and 18. */
17353 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17354 && strcmp (name, "void") == 0)
17355 type = objfile_type (objfile)->builtin_void;
17356 else
17357 type = init_integer_type (objfile, bits, unsigned_p, name);
17358
17359 return type;
17360 }
17361
17362 /* Initialise and return a floating point type of size BITS suitable for
17363 use as a component of a complex number. The NAME_HINT is passed through
17364 when initialising the floating point type and is the name of the complex
17365 type.
17366
17367 As DWARF doesn't currently provide an explicit name for the components
17368 of a complex number, but it can be helpful to have these components
17369 named, we try to select a suitable name based on the size of the
17370 component. */
17371 static struct type *
17372 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17373 struct objfile *objfile,
17374 int bits, const char *name_hint,
17375 enum bfd_endian byte_order)
17376 {
17377 gdbarch *gdbarch = objfile->arch ();
17378 struct type *tt = nullptr;
17379
17380 /* Try to find a suitable floating point builtin type of size BITS.
17381 We're going to use the name of this type as the name for the complex
17382 target type that we are about to create. */
17383 switch (cu->language)
17384 {
17385 case language_fortran:
17386 switch (bits)
17387 {
17388 case 32:
17389 tt = builtin_f_type (gdbarch)->builtin_real;
17390 break;
17391 case 64:
17392 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17393 break;
17394 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17395 case 128:
17396 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17397 break;
17398 }
17399 break;
17400 default:
17401 switch (bits)
17402 {
17403 case 32:
17404 tt = builtin_type (gdbarch)->builtin_float;
17405 break;
17406 case 64:
17407 tt = builtin_type (gdbarch)->builtin_double;
17408 break;
17409 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17410 case 128:
17411 tt = builtin_type (gdbarch)->builtin_long_double;
17412 break;
17413 }
17414 break;
17415 }
17416
17417 /* If the type we found doesn't match the size we were looking for, then
17418 pretend we didn't find a type at all, the complex target type we
17419 create will then be nameless. */
17420 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17421 tt = nullptr;
17422
17423 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17424 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17425 }
17426
17427 /* Find a representation of a given base type and install
17428 it in the TYPE field of the die. */
17429
17430 static struct type *
17431 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17432 {
17433 struct objfile *objfile = cu->per_objfile->objfile;
17434 struct type *type;
17435 struct attribute *attr;
17436 int encoding = 0, bits = 0;
17437 const char *name;
17438 gdbarch *arch;
17439
17440 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17441 if (attr != nullptr)
17442 encoding = DW_UNSND (attr);
17443 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17444 if (attr != nullptr)
17445 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17446 name = dwarf2_name (die, cu);
17447 if (!name)
17448 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17449
17450 arch = objfile->arch ();
17451 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17452
17453 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17454 if (attr)
17455 {
17456 int endianity = DW_UNSND (attr);
17457
17458 switch (endianity)
17459 {
17460 case DW_END_big:
17461 byte_order = BFD_ENDIAN_BIG;
17462 break;
17463 case DW_END_little:
17464 byte_order = BFD_ENDIAN_LITTLE;
17465 break;
17466 default:
17467 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17468 break;
17469 }
17470 }
17471
17472 switch (encoding)
17473 {
17474 case DW_ATE_address:
17475 /* Turn DW_ATE_address into a void * pointer. */
17476 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17477 type = init_pointer_type (objfile, bits, name, type);
17478 break;
17479 case DW_ATE_boolean:
17480 type = init_boolean_type (objfile, bits, 1, name);
17481 break;
17482 case DW_ATE_complex_float:
17483 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17484 byte_order);
17485 if (type->code () == TYPE_CODE_ERROR)
17486 {
17487 if (name == nullptr)
17488 {
17489 struct obstack *obstack
17490 = &cu->per_objfile->objfile->objfile_obstack;
17491 name = obconcat (obstack, "_Complex ", type->name (),
17492 nullptr);
17493 }
17494 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17495 }
17496 else
17497 type = init_complex_type (name, type);
17498 break;
17499 case DW_ATE_decimal_float:
17500 type = init_decfloat_type (objfile, bits, name);
17501 break;
17502 case DW_ATE_float:
17503 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17504 break;
17505 case DW_ATE_signed:
17506 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17507 break;
17508 case DW_ATE_unsigned:
17509 if (cu->language == language_fortran
17510 && name
17511 && startswith (name, "character("))
17512 type = init_character_type (objfile, bits, 1, name);
17513 else
17514 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17515 break;
17516 case DW_ATE_signed_char:
17517 if (cu->language == language_ada || cu->language == language_m2
17518 || cu->language == language_pascal
17519 || cu->language == language_fortran)
17520 type = init_character_type (objfile, bits, 0, name);
17521 else
17522 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17523 break;
17524 case DW_ATE_unsigned_char:
17525 if (cu->language == language_ada || cu->language == language_m2
17526 || cu->language == language_pascal
17527 || cu->language == language_fortran
17528 || cu->language == language_rust)
17529 type = init_character_type (objfile, bits, 1, name);
17530 else
17531 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17532 break;
17533 case DW_ATE_UTF:
17534 {
17535 if (bits == 16)
17536 type = builtin_type (arch)->builtin_char16;
17537 else if (bits == 32)
17538 type = builtin_type (arch)->builtin_char32;
17539 else
17540 {
17541 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17542 bits);
17543 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17544 }
17545 return set_die_type (die, type, cu);
17546 }
17547 break;
17548
17549 default:
17550 complaint (_("unsupported DW_AT_encoding: '%s'"),
17551 dwarf_type_encoding_name (encoding));
17552 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17553 break;
17554 }
17555
17556 if (name && strcmp (name, "char") == 0)
17557 TYPE_NOSIGN (type) = 1;
17558
17559 maybe_set_alignment (cu, die, type);
17560
17561 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17562
17563 return set_die_type (die, type, cu);
17564 }
17565
17566 /* Parse dwarf attribute if it's a block, reference or constant and put the
17567 resulting value of the attribute into struct bound_prop.
17568 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17569
17570 static int
17571 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17572 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17573 struct type *default_type)
17574 {
17575 struct dwarf2_property_baton *baton;
17576 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17577 struct objfile *objfile = per_objfile->objfile;
17578 struct obstack *obstack = &objfile->objfile_obstack;
17579
17580 gdb_assert (default_type != NULL);
17581
17582 if (attr == NULL || prop == NULL)
17583 return 0;
17584
17585 if (attr->form_is_block ())
17586 {
17587 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17588 baton->property_type = default_type;
17589 baton->locexpr.per_cu = cu->per_cu;
17590 baton->locexpr.per_objfile = per_objfile;
17591 baton->locexpr.size = DW_BLOCK (attr)->size;
17592 baton->locexpr.data = DW_BLOCK (attr)->data;
17593 switch (attr->name)
17594 {
17595 case DW_AT_string_length:
17596 baton->locexpr.is_reference = true;
17597 break;
17598 default:
17599 baton->locexpr.is_reference = false;
17600 break;
17601 }
17602 prop->data.baton = baton;
17603 prop->kind = PROP_LOCEXPR;
17604 gdb_assert (prop->data.baton != NULL);
17605 }
17606 else if (attr->form_is_ref ())
17607 {
17608 struct dwarf2_cu *target_cu = cu;
17609 struct die_info *target_die;
17610 struct attribute *target_attr;
17611
17612 target_die = follow_die_ref (die, attr, &target_cu);
17613 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17614 if (target_attr == NULL)
17615 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17616 target_cu);
17617 if (target_attr == NULL)
17618 return 0;
17619
17620 switch (target_attr->name)
17621 {
17622 case DW_AT_location:
17623 if (target_attr->form_is_section_offset ())
17624 {
17625 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17626 baton->property_type = die_type (target_die, target_cu);
17627 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17628 prop->data.baton = baton;
17629 prop->kind = PROP_LOCLIST;
17630 gdb_assert (prop->data.baton != NULL);
17631 }
17632 else if (target_attr->form_is_block ())
17633 {
17634 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17635 baton->property_type = die_type (target_die, target_cu);
17636 baton->locexpr.per_cu = cu->per_cu;
17637 baton->locexpr.per_objfile = per_objfile;
17638 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17639 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17640 baton->locexpr.is_reference = true;
17641 prop->data.baton = baton;
17642 prop->kind = PROP_LOCEXPR;
17643 gdb_assert (prop->data.baton != NULL);
17644 }
17645 else
17646 {
17647 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17648 "dynamic property");
17649 return 0;
17650 }
17651 break;
17652 case DW_AT_data_member_location:
17653 {
17654 LONGEST offset;
17655
17656 if (!handle_data_member_location (target_die, target_cu,
17657 &offset))
17658 return 0;
17659
17660 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17661 baton->property_type = read_type_die (target_die->parent,
17662 target_cu);
17663 baton->offset_info.offset = offset;
17664 baton->offset_info.type = die_type (target_die, target_cu);
17665 prop->data.baton = baton;
17666 prop->kind = PROP_ADDR_OFFSET;
17667 break;
17668 }
17669 }
17670 }
17671 else if (attr->form_is_constant ())
17672 {
17673 prop->data.const_val = attr->constant_value (0);
17674 prop->kind = PROP_CONST;
17675 }
17676 else
17677 {
17678 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17679 dwarf2_name (die, cu));
17680 return 0;
17681 }
17682
17683 return 1;
17684 }
17685
17686 /* See read.h. */
17687
17688 struct type *
17689 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17690 {
17691 struct type *int_type;
17692
17693 /* Helper macro to examine the various builtin types. */
17694 #define TRY_TYPE(F) \
17695 int_type = (unsigned_p \
17696 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17697 : objfile_type (objfile)->builtin_ ## F); \
17698 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17699 return int_type
17700
17701 TRY_TYPE (char);
17702 TRY_TYPE (short);
17703 TRY_TYPE (int);
17704 TRY_TYPE (long);
17705 TRY_TYPE (long_long);
17706
17707 #undef TRY_TYPE
17708
17709 gdb_assert_not_reached ("unable to find suitable integer type");
17710 }
17711
17712 /* See read.h. */
17713
17714 struct type *
17715 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17716 {
17717 int addr_size = this->per_cu->addr_size ();
17718 return this->per_objfile->int_type (addr_size, unsigned_p);
17719 }
17720
17721 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17722 present (which is valid) then compute the default type based on the
17723 compilation units address size. */
17724
17725 static struct type *
17726 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17727 {
17728 struct type *index_type = die_type (die, cu);
17729
17730 /* Dwarf-2 specifications explicitly allows to create subrange types
17731 without specifying a base type.
17732 In that case, the base type must be set to the type of
17733 the lower bound, upper bound or count, in that order, if any of these
17734 three attributes references an object that has a type.
17735 If no base type is found, the Dwarf-2 specifications say that
17736 a signed integer type of size equal to the size of an address should
17737 be used.
17738 For the following C code: `extern char gdb_int [];'
17739 GCC produces an empty range DIE.
17740 FIXME: muller/2010-05-28: Possible references to object for low bound,
17741 high bound or count are not yet handled by this code. */
17742 if (index_type->code () == TYPE_CODE_VOID)
17743 index_type = cu->addr_sized_int_type (false);
17744
17745 return index_type;
17746 }
17747
17748 /* Read the given DW_AT_subrange DIE. */
17749
17750 static struct type *
17751 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17752 {
17753 struct type *base_type, *orig_base_type;
17754 struct type *range_type;
17755 struct attribute *attr;
17756 struct dynamic_prop low, high;
17757 int low_default_is_valid;
17758 int high_bound_is_count = 0;
17759 const char *name;
17760 ULONGEST negative_mask;
17761
17762 orig_base_type = read_subrange_index_type (die, cu);
17763
17764 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17765 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17766 creating the range type, but we use the result of check_typedef
17767 when examining properties of the type. */
17768 base_type = check_typedef (orig_base_type);
17769
17770 /* The die_type call above may have already set the type for this DIE. */
17771 range_type = get_die_type (die, cu);
17772 if (range_type)
17773 return range_type;
17774
17775 low.kind = PROP_CONST;
17776 high.kind = PROP_CONST;
17777 high.data.const_val = 0;
17778
17779 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17780 omitting DW_AT_lower_bound. */
17781 switch (cu->language)
17782 {
17783 case language_c:
17784 case language_cplus:
17785 low.data.const_val = 0;
17786 low_default_is_valid = 1;
17787 break;
17788 case language_fortran:
17789 low.data.const_val = 1;
17790 low_default_is_valid = 1;
17791 break;
17792 case language_d:
17793 case language_objc:
17794 case language_rust:
17795 low.data.const_val = 0;
17796 low_default_is_valid = (cu->header.version >= 4);
17797 break;
17798 case language_ada:
17799 case language_m2:
17800 case language_pascal:
17801 low.data.const_val = 1;
17802 low_default_is_valid = (cu->header.version >= 4);
17803 break;
17804 default:
17805 low.data.const_val = 0;
17806 low_default_is_valid = 0;
17807 break;
17808 }
17809
17810 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17811 if (attr != nullptr)
17812 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17813 else if (!low_default_is_valid)
17814 complaint (_("Missing DW_AT_lower_bound "
17815 "- DIE at %s [in module %s]"),
17816 sect_offset_str (die->sect_off),
17817 objfile_name (cu->per_objfile->objfile));
17818
17819 struct attribute *attr_ub, *attr_count;
17820 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17821 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17822 {
17823 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17824 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17825 {
17826 /* If bounds are constant do the final calculation here. */
17827 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17828 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17829 else
17830 high_bound_is_count = 1;
17831 }
17832 else
17833 {
17834 if (attr_ub != NULL)
17835 complaint (_("Unresolved DW_AT_upper_bound "
17836 "- DIE at %s [in module %s]"),
17837 sect_offset_str (die->sect_off),
17838 objfile_name (cu->per_objfile->objfile));
17839 if (attr_count != NULL)
17840 complaint (_("Unresolved DW_AT_count "
17841 "- DIE at %s [in module %s]"),
17842 sect_offset_str (die->sect_off),
17843 objfile_name (cu->per_objfile->objfile));
17844 }
17845 }
17846
17847 LONGEST bias = 0;
17848 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17849 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17850 bias = bias_attr->constant_value (0);
17851
17852 /* Normally, the DWARF producers are expected to use a signed
17853 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17854 But this is unfortunately not always the case, as witnessed
17855 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17856 is used instead. To work around that ambiguity, we treat
17857 the bounds as signed, and thus sign-extend their values, when
17858 the base type is signed. */
17859 negative_mask =
17860 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17861 if (low.kind == PROP_CONST
17862 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17863 low.data.const_val |= negative_mask;
17864 if (high.kind == PROP_CONST
17865 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17866 high.data.const_val |= negative_mask;
17867
17868 /* Check for bit and byte strides. */
17869 struct dynamic_prop byte_stride_prop;
17870 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17871 if (attr_byte_stride != nullptr)
17872 {
17873 struct type *prop_type = cu->addr_sized_int_type (false);
17874 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17875 prop_type);
17876 }
17877
17878 struct dynamic_prop bit_stride_prop;
17879 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17880 if (attr_bit_stride != nullptr)
17881 {
17882 /* It only makes sense to have either a bit or byte stride. */
17883 if (attr_byte_stride != nullptr)
17884 {
17885 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17886 "- DIE at %s [in module %s]"),
17887 sect_offset_str (die->sect_off),
17888 objfile_name (cu->per_objfile->objfile));
17889 attr_bit_stride = nullptr;
17890 }
17891 else
17892 {
17893 struct type *prop_type = cu->addr_sized_int_type (false);
17894 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17895 prop_type);
17896 }
17897 }
17898
17899 if (attr_byte_stride != nullptr
17900 || attr_bit_stride != nullptr)
17901 {
17902 bool byte_stride_p = (attr_byte_stride != nullptr);
17903 struct dynamic_prop *stride
17904 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17905
17906 range_type
17907 = create_range_type_with_stride (NULL, orig_base_type, &low,
17908 &high, bias, stride, byte_stride_p);
17909 }
17910 else
17911 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17912
17913 if (high_bound_is_count)
17914 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17915
17916 /* Ada expects an empty array on no boundary attributes. */
17917 if (attr == NULL && cu->language != language_ada)
17918 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17919
17920 name = dwarf2_name (die, cu);
17921 if (name)
17922 range_type->set_name (name);
17923
17924 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17925 if (attr != nullptr)
17926 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17927
17928 maybe_set_alignment (cu, die, range_type);
17929
17930 set_die_type (die, range_type, cu);
17931
17932 /* set_die_type should be already done. */
17933 set_descriptive_type (range_type, die, cu);
17934
17935 return range_type;
17936 }
17937
17938 static struct type *
17939 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17940 {
17941 struct type *type;
17942
17943 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17944 type->set_name (dwarf2_name (die, cu));
17945
17946 /* In Ada, an unspecified type is typically used when the description
17947 of the type is deferred to a different unit. When encountering
17948 such a type, we treat it as a stub, and try to resolve it later on,
17949 when needed. */
17950 if (cu->language == language_ada)
17951 TYPE_STUB (type) = 1;
17952
17953 return set_die_type (die, type, cu);
17954 }
17955
17956 /* Read a single die and all its descendents. Set the die's sibling
17957 field to NULL; set other fields in the die correctly, and set all
17958 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17959 location of the info_ptr after reading all of those dies. PARENT
17960 is the parent of the die in question. */
17961
17962 static struct die_info *
17963 read_die_and_children (const struct die_reader_specs *reader,
17964 const gdb_byte *info_ptr,
17965 const gdb_byte **new_info_ptr,
17966 struct die_info *parent)
17967 {
17968 struct die_info *die;
17969 const gdb_byte *cur_ptr;
17970
17971 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17972 if (die == NULL)
17973 {
17974 *new_info_ptr = cur_ptr;
17975 return NULL;
17976 }
17977 store_in_ref_table (die, reader->cu);
17978
17979 if (die->has_children)
17980 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17981 else
17982 {
17983 die->child = NULL;
17984 *new_info_ptr = cur_ptr;
17985 }
17986
17987 die->sibling = NULL;
17988 die->parent = parent;
17989 return die;
17990 }
17991
17992 /* Read a die, all of its descendents, and all of its siblings; set
17993 all of the fields of all of the dies correctly. Arguments are as
17994 in read_die_and_children. */
17995
17996 static struct die_info *
17997 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17998 const gdb_byte *info_ptr,
17999 const gdb_byte **new_info_ptr,
18000 struct die_info *parent)
18001 {
18002 struct die_info *first_die, *last_sibling;
18003 const gdb_byte *cur_ptr;
18004
18005 cur_ptr = info_ptr;
18006 first_die = last_sibling = NULL;
18007
18008 while (1)
18009 {
18010 struct die_info *die
18011 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18012
18013 if (die == NULL)
18014 {
18015 *new_info_ptr = cur_ptr;
18016 return first_die;
18017 }
18018
18019 if (!first_die)
18020 first_die = die;
18021 else
18022 last_sibling->sibling = die;
18023
18024 last_sibling = die;
18025 }
18026 }
18027
18028 /* Read a die, all of its descendents, and all of its siblings; set
18029 all of the fields of all of the dies correctly. Arguments are as
18030 in read_die_and_children.
18031 This the main entry point for reading a DIE and all its children. */
18032
18033 static struct die_info *
18034 read_die_and_siblings (const struct die_reader_specs *reader,
18035 const gdb_byte *info_ptr,
18036 const gdb_byte **new_info_ptr,
18037 struct die_info *parent)
18038 {
18039 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18040 new_info_ptr, parent);
18041
18042 if (dwarf_die_debug)
18043 {
18044 fprintf_unfiltered (gdb_stdlog,
18045 "Read die from %s@0x%x of %s:\n",
18046 reader->die_section->get_name (),
18047 (unsigned) (info_ptr - reader->die_section->buffer),
18048 bfd_get_filename (reader->abfd));
18049 dump_die (die, dwarf_die_debug);
18050 }
18051
18052 return die;
18053 }
18054
18055 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18056 attributes.
18057 The caller is responsible for filling in the extra attributes
18058 and updating (*DIEP)->num_attrs.
18059 Set DIEP to point to a newly allocated die with its information,
18060 except for its child, sibling, and parent fields. */
18061
18062 static const gdb_byte *
18063 read_full_die_1 (const struct die_reader_specs *reader,
18064 struct die_info **diep, const gdb_byte *info_ptr,
18065 int num_extra_attrs)
18066 {
18067 unsigned int abbrev_number, bytes_read, i;
18068 struct abbrev_info *abbrev;
18069 struct die_info *die;
18070 struct dwarf2_cu *cu = reader->cu;
18071 bfd *abfd = reader->abfd;
18072
18073 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18074 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18075 info_ptr += bytes_read;
18076 if (!abbrev_number)
18077 {
18078 *diep = NULL;
18079 return info_ptr;
18080 }
18081
18082 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18083 if (!abbrev)
18084 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18085 abbrev_number,
18086 bfd_get_filename (abfd));
18087
18088 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18089 die->sect_off = sect_off;
18090 die->tag = abbrev->tag;
18091 die->abbrev = abbrev_number;
18092 die->has_children = abbrev->has_children;
18093
18094 /* Make the result usable.
18095 The caller needs to update num_attrs after adding the extra
18096 attributes. */
18097 die->num_attrs = abbrev->num_attrs;
18098
18099 std::vector<int> indexes_that_need_reprocess;
18100 for (i = 0; i < abbrev->num_attrs; ++i)
18101 {
18102 bool need_reprocess;
18103 info_ptr =
18104 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18105 info_ptr, &need_reprocess);
18106 if (need_reprocess)
18107 indexes_that_need_reprocess.push_back (i);
18108 }
18109
18110 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18111 if (attr != nullptr)
18112 cu->str_offsets_base = DW_UNSND (attr);
18113
18114 attr = die->attr (DW_AT_loclists_base);
18115 if (attr != nullptr)
18116 cu->loclist_base = DW_UNSND (attr);
18117
18118 auto maybe_addr_base = die->addr_base ();
18119 if (maybe_addr_base.has_value ())
18120 cu->addr_base = *maybe_addr_base;
18121 for (int index : indexes_that_need_reprocess)
18122 read_attribute_reprocess (reader, &die->attrs[index]);
18123 *diep = die;
18124 return info_ptr;
18125 }
18126
18127 /* Read a die and all its attributes.
18128 Set DIEP to point to a newly allocated die with its information,
18129 except for its child, sibling, and parent fields. */
18130
18131 static const gdb_byte *
18132 read_full_die (const struct die_reader_specs *reader,
18133 struct die_info **diep, const gdb_byte *info_ptr)
18134 {
18135 const gdb_byte *result;
18136
18137 result = read_full_die_1 (reader, diep, info_ptr, 0);
18138
18139 if (dwarf_die_debug)
18140 {
18141 fprintf_unfiltered (gdb_stdlog,
18142 "Read die from %s@0x%x of %s:\n",
18143 reader->die_section->get_name (),
18144 (unsigned) (info_ptr - reader->die_section->buffer),
18145 bfd_get_filename (reader->abfd));
18146 dump_die (*diep, dwarf_die_debug);
18147 }
18148
18149 return result;
18150 }
18151 \f
18152
18153 /* Returns nonzero if TAG represents a type that we might generate a partial
18154 symbol for. */
18155
18156 static int
18157 is_type_tag_for_partial (int tag)
18158 {
18159 switch (tag)
18160 {
18161 #if 0
18162 /* Some types that would be reasonable to generate partial symbols for,
18163 that we don't at present. */
18164 case DW_TAG_array_type:
18165 case DW_TAG_file_type:
18166 case DW_TAG_ptr_to_member_type:
18167 case DW_TAG_set_type:
18168 case DW_TAG_string_type:
18169 case DW_TAG_subroutine_type:
18170 #endif
18171 case DW_TAG_base_type:
18172 case DW_TAG_class_type:
18173 case DW_TAG_interface_type:
18174 case DW_TAG_enumeration_type:
18175 case DW_TAG_structure_type:
18176 case DW_TAG_subrange_type:
18177 case DW_TAG_typedef:
18178 case DW_TAG_union_type:
18179 return 1;
18180 default:
18181 return 0;
18182 }
18183 }
18184
18185 /* Load all DIEs that are interesting for partial symbols into memory. */
18186
18187 static struct partial_die_info *
18188 load_partial_dies (const struct die_reader_specs *reader,
18189 const gdb_byte *info_ptr, int building_psymtab)
18190 {
18191 struct dwarf2_cu *cu = reader->cu;
18192 struct objfile *objfile = cu->per_objfile->objfile;
18193 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18194 unsigned int bytes_read;
18195 unsigned int load_all = 0;
18196 int nesting_level = 1;
18197
18198 parent_die = NULL;
18199 last_die = NULL;
18200
18201 gdb_assert (cu->per_cu != NULL);
18202 if (cu->per_cu->load_all_dies)
18203 load_all = 1;
18204
18205 cu->partial_dies
18206 = htab_create_alloc_ex (cu->header.length / 12,
18207 partial_die_hash,
18208 partial_die_eq,
18209 NULL,
18210 &cu->comp_unit_obstack,
18211 hashtab_obstack_allocate,
18212 dummy_obstack_deallocate);
18213
18214 while (1)
18215 {
18216 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18217
18218 /* A NULL abbrev means the end of a series of children. */
18219 if (abbrev == NULL)
18220 {
18221 if (--nesting_level == 0)
18222 return first_die;
18223
18224 info_ptr += bytes_read;
18225 last_die = parent_die;
18226 parent_die = parent_die->die_parent;
18227 continue;
18228 }
18229
18230 /* Check for template arguments. We never save these; if
18231 they're seen, we just mark the parent, and go on our way. */
18232 if (parent_die != NULL
18233 && cu->language == language_cplus
18234 && (abbrev->tag == DW_TAG_template_type_param
18235 || abbrev->tag == DW_TAG_template_value_param))
18236 {
18237 parent_die->has_template_arguments = 1;
18238
18239 if (!load_all)
18240 {
18241 /* We don't need a partial DIE for the template argument. */
18242 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18243 continue;
18244 }
18245 }
18246
18247 /* We only recurse into c++ subprograms looking for template arguments.
18248 Skip their other children. */
18249 if (!load_all
18250 && cu->language == language_cplus
18251 && parent_die != NULL
18252 && parent_die->tag == DW_TAG_subprogram
18253 && abbrev->tag != DW_TAG_inlined_subroutine)
18254 {
18255 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18256 continue;
18257 }
18258
18259 /* Check whether this DIE is interesting enough to save. Normally
18260 we would not be interested in members here, but there may be
18261 later variables referencing them via DW_AT_specification (for
18262 static members). */
18263 if (!load_all
18264 && !is_type_tag_for_partial (abbrev->tag)
18265 && abbrev->tag != DW_TAG_constant
18266 && abbrev->tag != DW_TAG_enumerator
18267 && abbrev->tag != DW_TAG_subprogram
18268 && abbrev->tag != DW_TAG_inlined_subroutine
18269 && abbrev->tag != DW_TAG_lexical_block
18270 && abbrev->tag != DW_TAG_variable
18271 && abbrev->tag != DW_TAG_namespace
18272 && abbrev->tag != DW_TAG_module
18273 && abbrev->tag != DW_TAG_member
18274 && abbrev->tag != DW_TAG_imported_unit
18275 && abbrev->tag != DW_TAG_imported_declaration)
18276 {
18277 /* Otherwise we skip to the next sibling, if any. */
18278 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18279 continue;
18280 }
18281
18282 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18283 abbrev);
18284
18285 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18286
18287 /* This two-pass algorithm for processing partial symbols has a
18288 high cost in cache pressure. Thus, handle some simple cases
18289 here which cover the majority of C partial symbols. DIEs
18290 which neither have specification tags in them, nor could have
18291 specification tags elsewhere pointing at them, can simply be
18292 processed and discarded.
18293
18294 This segment is also optional; scan_partial_symbols and
18295 add_partial_symbol will handle these DIEs if we chain
18296 them in normally. When compilers which do not emit large
18297 quantities of duplicate debug information are more common,
18298 this code can probably be removed. */
18299
18300 /* Any complete simple types at the top level (pretty much all
18301 of them, for a language without namespaces), can be processed
18302 directly. */
18303 if (parent_die == NULL
18304 && pdi.has_specification == 0
18305 && pdi.is_declaration == 0
18306 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18307 || pdi.tag == DW_TAG_base_type
18308 || pdi.tag == DW_TAG_subrange_type))
18309 {
18310 if (building_psymtab && pdi.raw_name != NULL)
18311 add_partial_symbol (&pdi, cu);
18312
18313 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18314 continue;
18315 }
18316
18317 /* The exception for DW_TAG_typedef with has_children above is
18318 a workaround of GCC PR debug/47510. In the case of this complaint
18319 type_name_or_error will error on such types later.
18320
18321 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18322 it could not find the child DIEs referenced later, this is checked
18323 above. In correct DWARF DW_TAG_typedef should have no children. */
18324
18325 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18326 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18327 "- DIE at %s [in module %s]"),
18328 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18329
18330 /* If we're at the second level, and we're an enumerator, and
18331 our parent has no specification (meaning possibly lives in a
18332 namespace elsewhere), then we can add the partial symbol now
18333 instead of queueing it. */
18334 if (pdi.tag == DW_TAG_enumerator
18335 && parent_die != NULL
18336 && parent_die->die_parent == NULL
18337 && parent_die->tag == DW_TAG_enumeration_type
18338 && parent_die->has_specification == 0)
18339 {
18340 if (pdi.raw_name == NULL)
18341 complaint (_("malformed enumerator DIE ignored"));
18342 else if (building_psymtab)
18343 add_partial_symbol (&pdi, cu);
18344
18345 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18346 continue;
18347 }
18348
18349 struct partial_die_info *part_die
18350 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18351
18352 /* We'll save this DIE so link it in. */
18353 part_die->die_parent = parent_die;
18354 part_die->die_sibling = NULL;
18355 part_die->die_child = NULL;
18356
18357 if (last_die && last_die == parent_die)
18358 last_die->die_child = part_die;
18359 else if (last_die)
18360 last_die->die_sibling = part_die;
18361
18362 last_die = part_die;
18363
18364 if (first_die == NULL)
18365 first_die = part_die;
18366
18367 /* Maybe add the DIE to the hash table. Not all DIEs that we
18368 find interesting need to be in the hash table, because we
18369 also have the parent/sibling/child chains; only those that we
18370 might refer to by offset later during partial symbol reading.
18371
18372 For now this means things that might have be the target of a
18373 DW_AT_specification, DW_AT_abstract_origin, or
18374 DW_AT_extension. DW_AT_extension will refer only to
18375 namespaces; DW_AT_abstract_origin refers to functions (and
18376 many things under the function DIE, but we do not recurse
18377 into function DIEs during partial symbol reading) and
18378 possibly variables as well; DW_AT_specification refers to
18379 declarations. Declarations ought to have the DW_AT_declaration
18380 flag. It happens that GCC forgets to put it in sometimes, but
18381 only for functions, not for types.
18382
18383 Adding more things than necessary to the hash table is harmless
18384 except for the performance cost. Adding too few will result in
18385 wasted time in find_partial_die, when we reread the compilation
18386 unit with load_all_dies set. */
18387
18388 if (load_all
18389 || abbrev->tag == DW_TAG_constant
18390 || abbrev->tag == DW_TAG_subprogram
18391 || abbrev->tag == DW_TAG_variable
18392 || abbrev->tag == DW_TAG_namespace
18393 || part_die->is_declaration)
18394 {
18395 void **slot;
18396
18397 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18398 to_underlying (part_die->sect_off),
18399 INSERT);
18400 *slot = part_die;
18401 }
18402
18403 /* For some DIEs we want to follow their children (if any). For C
18404 we have no reason to follow the children of structures; for other
18405 languages we have to, so that we can get at method physnames
18406 to infer fully qualified class names, for DW_AT_specification,
18407 and for C++ template arguments. For C++, we also look one level
18408 inside functions to find template arguments (if the name of the
18409 function does not already contain the template arguments).
18410
18411 For Ada and Fortran, we need to scan the children of subprograms
18412 and lexical blocks as well because these languages allow the
18413 definition of nested entities that could be interesting for the
18414 debugger, such as nested subprograms for instance. */
18415 if (last_die->has_children
18416 && (load_all
18417 || last_die->tag == DW_TAG_namespace
18418 || last_die->tag == DW_TAG_module
18419 || last_die->tag == DW_TAG_enumeration_type
18420 || (cu->language == language_cplus
18421 && last_die->tag == DW_TAG_subprogram
18422 && (last_die->raw_name == NULL
18423 || strchr (last_die->raw_name, '<') == NULL))
18424 || (cu->language != language_c
18425 && (last_die->tag == DW_TAG_class_type
18426 || last_die->tag == DW_TAG_interface_type
18427 || last_die->tag == DW_TAG_structure_type
18428 || last_die->tag == DW_TAG_union_type))
18429 || ((cu->language == language_ada
18430 || cu->language == language_fortran)
18431 && (last_die->tag == DW_TAG_subprogram
18432 || last_die->tag == DW_TAG_lexical_block))))
18433 {
18434 nesting_level++;
18435 parent_die = last_die;
18436 continue;
18437 }
18438
18439 /* Otherwise we skip to the next sibling, if any. */
18440 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18441
18442 /* Back to the top, do it again. */
18443 }
18444 }
18445
18446 partial_die_info::partial_die_info (sect_offset sect_off_,
18447 struct abbrev_info *abbrev)
18448 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18449 {
18450 }
18451
18452 /* See class definition. */
18453
18454 const char *
18455 partial_die_info::name (dwarf2_cu *cu)
18456 {
18457 if (!canonical_name && raw_name != nullptr)
18458 {
18459 struct objfile *objfile = cu->per_objfile->objfile;
18460 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18461 canonical_name = 1;
18462 }
18463
18464 return raw_name;
18465 }
18466
18467 /* Read a minimal amount of information into the minimal die structure.
18468 INFO_PTR should point just after the initial uleb128 of a DIE. */
18469
18470 const gdb_byte *
18471 partial_die_info::read (const struct die_reader_specs *reader,
18472 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18473 {
18474 struct dwarf2_cu *cu = reader->cu;
18475 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18476 unsigned int i;
18477 int has_low_pc_attr = 0;
18478 int has_high_pc_attr = 0;
18479 int high_pc_relative = 0;
18480
18481 for (i = 0; i < abbrev.num_attrs; ++i)
18482 {
18483 attribute attr;
18484 bool need_reprocess;
18485 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18486 info_ptr, &need_reprocess);
18487 /* String and address offsets that need to do the reprocessing have
18488 already been read at this point, so there is no need to wait until
18489 the loop terminates to do the reprocessing. */
18490 if (need_reprocess)
18491 read_attribute_reprocess (reader, &attr);
18492 /* Store the data if it is of an attribute we want to keep in a
18493 partial symbol table. */
18494 switch (attr.name)
18495 {
18496 case DW_AT_name:
18497 switch (tag)
18498 {
18499 case DW_TAG_compile_unit:
18500 case DW_TAG_partial_unit:
18501 case DW_TAG_type_unit:
18502 /* Compilation units have a DW_AT_name that is a filename, not
18503 a source language identifier. */
18504 case DW_TAG_enumeration_type:
18505 case DW_TAG_enumerator:
18506 /* These tags always have simple identifiers already; no need
18507 to canonicalize them. */
18508 canonical_name = 1;
18509 raw_name = DW_STRING (&attr);
18510 break;
18511 default:
18512 canonical_name = 0;
18513 raw_name = DW_STRING (&attr);
18514 break;
18515 }
18516 break;
18517 case DW_AT_linkage_name:
18518 case DW_AT_MIPS_linkage_name:
18519 /* Note that both forms of linkage name might appear. We
18520 assume they will be the same, and we only store the last
18521 one we see. */
18522 linkage_name = attr.value_as_string ();
18523 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18524 See https://github.com/rust-lang/rust/issues/32925. */
18525 if (cu->language == language_rust && linkage_name != NULL
18526 && strchr (linkage_name, '{') != NULL)
18527 linkage_name = NULL;
18528 break;
18529 case DW_AT_low_pc:
18530 has_low_pc_attr = 1;
18531 lowpc = attr.value_as_address ();
18532 break;
18533 case DW_AT_high_pc:
18534 has_high_pc_attr = 1;
18535 highpc = attr.value_as_address ();
18536 if (cu->header.version >= 4 && attr.form_is_constant ())
18537 high_pc_relative = 1;
18538 break;
18539 case DW_AT_location:
18540 /* Support the .debug_loc offsets. */
18541 if (attr.form_is_block ())
18542 {
18543 d.locdesc = DW_BLOCK (&attr);
18544 }
18545 else if (attr.form_is_section_offset ())
18546 {
18547 dwarf2_complex_location_expr_complaint ();
18548 }
18549 else
18550 {
18551 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18552 "partial symbol information");
18553 }
18554 break;
18555 case DW_AT_external:
18556 is_external = DW_UNSND (&attr);
18557 break;
18558 case DW_AT_declaration:
18559 is_declaration = DW_UNSND (&attr);
18560 break;
18561 case DW_AT_type:
18562 has_type = 1;
18563 break;
18564 case DW_AT_abstract_origin:
18565 case DW_AT_specification:
18566 case DW_AT_extension:
18567 has_specification = 1;
18568 spec_offset = attr.get_ref_die_offset ();
18569 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18570 || cu->per_cu->is_dwz);
18571 break;
18572 case DW_AT_sibling:
18573 /* Ignore absolute siblings, they might point outside of
18574 the current compile unit. */
18575 if (attr.form == DW_FORM_ref_addr)
18576 complaint (_("ignoring absolute DW_AT_sibling"));
18577 else
18578 {
18579 const gdb_byte *buffer = reader->buffer;
18580 sect_offset off = attr.get_ref_die_offset ();
18581 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18582
18583 if (sibling_ptr < info_ptr)
18584 complaint (_("DW_AT_sibling points backwards"));
18585 else if (sibling_ptr > reader->buffer_end)
18586 reader->die_section->overflow_complaint ();
18587 else
18588 sibling = sibling_ptr;
18589 }
18590 break;
18591 case DW_AT_byte_size:
18592 has_byte_size = 1;
18593 break;
18594 case DW_AT_const_value:
18595 has_const_value = 1;
18596 break;
18597 case DW_AT_calling_convention:
18598 /* DWARF doesn't provide a way to identify a program's source-level
18599 entry point. DW_AT_calling_convention attributes are only meant
18600 to describe functions' calling conventions.
18601
18602 However, because it's a necessary piece of information in
18603 Fortran, and before DWARF 4 DW_CC_program was the only
18604 piece of debugging information whose definition refers to
18605 a 'main program' at all, several compilers marked Fortran
18606 main programs with DW_CC_program --- even when those
18607 functions use the standard calling conventions.
18608
18609 Although DWARF now specifies a way to provide this
18610 information, we support this practice for backward
18611 compatibility. */
18612 if (DW_UNSND (&attr) == DW_CC_program
18613 && cu->language == language_fortran)
18614 main_subprogram = 1;
18615 break;
18616 case DW_AT_inline:
18617 if (DW_UNSND (&attr) == DW_INL_inlined
18618 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18619 may_be_inlined = 1;
18620 break;
18621
18622 case DW_AT_import:
18623 if (tag == DW_TAG_imported_unit)
18624 {
18625 d.sect_off = attr.get_ref_die_offset ();
18626 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18627 || cu->per_cu->is_dwz);
18628 }
18629 break;
18630
18631 case DW_AT_main_subprogram:
18632 main_subprogram = DW_UNSND (&attr);
18633 break;
18634
18635 case DW_AT_ranges:
18636 {
18637 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18638 but that requires a full DIE, so instead we just
18639 reimplement it. */
18640 int need_ranges_base = tag != DW_TAG_compile_unit;
18641 unsigned int ranges_offset = (DW_UNSND (&attr)
18642 + (need_ranges_base
18643 ? cu->ranges_base
18644 : 0));
18645
18646 /* Value of the DW_AT_ranges attribute is the offset in the
18647 .debug_ranges section. */
18648 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18649 nullptr))
18650 has_pc_info = 1;
18651 }
18652 break;
18653
18654 default:
18655 break;
18656 }
18657 }
18658
18659 /* For Ada, if both the name and the linkage name appear, we prefer
18660 the latter. This lets "catch exception" work better, regardless
18661 of the order in which the name and linkage name were emitted.
18662 Really, though, this is just a workaround for the fact that gdb
18663 doesn't store both the name and the linkage name. */
18664 if (cu->language == language_ada && linkage_name != nullptr)
18665 raw_name = linkage_name;
18666
18667 if (high_pc_relative)
18668 highpc += lowpc;
18669
18670 if (has_low_pc_attr && has_high_pc_attr)
18671 {
18672 /* When using the GNU linker, .gnu.linkonce. sections are used to
18673 eliminate duplicate copies of functions and vtables and such.
18674 The linker will arbitrarily choose one and discard the others.
18675 The AT_*_pc values for such functions refer to local labels in
18676 these sections. If the section from that file was discarded, the
18677 labels are not in the output, so the relocs get a value of 0.
18678 If this is a discarded function, mark the pc bounds as invalid,
18679 so that GDB will ignore it. */
18680 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
18681 {
18682 struct objfile *objfile = per_objfile->objfile;
18683 struct gdbarch *gdbarch = objfile->arch ();
18684
18685 complaint (_("DW_AT_low_pc %s is zero "
18686 "for DIE at %s [in module %s]"),
18687 paddress (gdbarch, lowpc),
18688 sect_offset_str (sect_off),
18689 objfile_name (objfile));
18690 }
18691 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18692 else if (lowpc >= highpc)
18693 {
18694 struct objfile *objfile = per_objfile->objfile;
18695 struct gdbarch *gdbarch = objfile->arch ();
18696
18697 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18698 "for DIE at %s [in module %s]"),
18699 paddress (gdbarch, lowpc),
18700 paddress (gdbarch, highpc),
18701 sect_offset_str (sect_off),
18702 objfile_name (objfile));
18703 }
18704 else
18705 has_pc_info = 1;
18706 }
18707
18708 return info_ptr;
18709 }
18710
18711 /* Find a cached partial DIE at OFFSET in CU. */
18712
18713 struct partial_die_info *
18714 dwarf2_cu::find_partial_die (sect_offset sect_off)
18715 {
18716 struct partial_die_info *lookup_die = NULL;
18717 struct partial_die_info part_die (sect_off);
18718
18719 lookup_die = ((struct partial_die_info *)
18720 htab_find_with_hash (partial_dies, &part_die,
18721 to_underlying (sect_off)));
18722
18723 return lookup_die;
18724 }
18725
18726 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18727 except in the case of .debug_types DIEs which do not reference
18728 outside their CU (they do however referencing other types via
18729 DW_FORM_ref_sig8). */
18730
18731 static const struct cu_partial_die_info
18732 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18733 {
18734 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18735 struct objfile *objfile = per_objfile->objfile;
18736 struct partial_die_info *pd = NULL;
18737
18738 if (offset_in_dwz == cu->per_cu->is_dwz
18739 && cu->header.offset_in_cu_p (sect_off))
18740 {
18741 pd = cu->find_partial_die (sect_off);
18742 if (pd != NULL)
18743 return { cu, pd };
18744 /* We missed recording what we needed.
18745 Load all dies and try again. */
18746 }
18747 else
18748 {
18749 /* TUs don't reference other CUs/TUs (except via type signatures). */
18750 if (cu->per_cu->is_debug_types)
18751 {
18752 error (_("Dwarf Error: Type Unit at offset %s contains"
18753 " external reference to offset %s [in module %s].\n"),
18754 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18755 bfd_get_filename (objfile->obfd));
18756 }
18757 dwarf2_per_cu_data *per_cu
18758 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18759 per_objfile);
18760
18761 cu = per_objfile->get_cu (per_cu);
18762 if (cu == NULL || cu->partial_dies == NULL)
18763 load_partial_comp_unit (per_cu, per_objfile, nullptr);
18764
18765 cu = per_objfile->get_cu (per_cu);
18766
18767 cu->last_used = 0;
18768 pd = cu->find_partial_die (sect_off);
18769 }
18770
18771 /* If we didn't find it, and not all dies have been loaded,
18772 load them all and try again. */
18773
18774 if (pd == NULL && cu->per_cu->load_all_dies == 0)
18775 {
18776 cu->per_cu->load_all_dies = 1;
18777
18778 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18779 THIS_CU->cu may already be in use. So we can't just free it and
18780 replace its DIEs with the ones we read in. Instead, we leave those
18781 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18782 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18783 set. */
18784 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
18785
18786 pd = cu->find_partial_die (sect_off);
18787 }
18788
18789 if (pd == NULL)
18790 internal_error (__FILE__, __LINE__,
18791 _("could not find partial DIE %s "
18792 "in cache [from module %s]\n"),
18793 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18794 return { cu, pd };
18795 }
18796
18797 /* See if we can figure out if the class lives in a namespace. We do
18798 this by looking for a member function; its demangled name will
18799 contain namespace info, if there is any. */
18800
18801 static void
18802 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18803 struct dwarf2_cu *cu)
18804 {
18805 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18806 what template types look like, because the demangler
18807 frequently doesn't give the same name as the debug info. We
18808 could fix this by only using the demangled name to get the
18809 prefix (but see comment in read_structure_type). */
18810
18811 struct partial_die_info *real_pdi;
18812 struct partial_die_info *child_pdi;
18813
18814 /* If this DIE (this DIE's specification, if any) has a parent, then
18815 we should not do this. We'll prepend the parent's fully qualified
18816 name when we create the partial symbol. */
18817
18818 real_pdi = struct_pdi;
18819 while (real_pdi->has_specification)
18820 {
18821 auto res = find_partial_die (real_pdi->spec_offset,
18822 real_pdi->spec_is_dwz, cu);
18823 real_pdi = res.pdi;
18824 cu = res.cu;
18825 }
18826
18827 if (real_pdi->die_parent != NULL)
18828 return;
18829
18830 for (child_pdi = struct_pdi->die_child;
18831 child_pdi != NULL;
18832 child_pdi = child_pdi->die_sibling)
18833 {
18834 if (child_pdi->tag == DW_TAG_subprogram
18835 && child_pdi->linkage_name != NULL)
18836 {
18837 gdb::unique_xmalloc_ptr<char> actual_class_name
18838 (language_class_name_from_physname (cu->language_defn,
18839 child_pdi->linkage_name));
18840 if (actual_class_name != NULL)
18841 {
18842 struct objfile *objfile = cu->per_objfile->objfile;
18843 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
18844 struct_pdi->canonical_name = 1;
18845 }
18846 break;
18847 }
18848 }
18849 }
18850
18851 /* Return true if a DIE with TAG may have the DW_AT_const_value
18852 attribute. */
18853
18854 static bool
18855 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18856 {
18857 switch (tag)
18858 {
18859 case DW_TAG_constant:
18860 case DW_TAG_enumerator:
18861 case DW_TAG_formal_parameter:
18862 case DW_TAG_template_value_param:
18863 case DW_TAG_variable:
18864 return true;
18865 }
18866
18867 return false;
18868 }
18869
18870 void
18871 partial_die_info::fixup (struct dwarf2_cu *cu)
18872 {
18873 /* Once we've fixed up a die, there's no point in doing so again.
18874 This also avoids a memory leak if we were to call
18875 guess_partial_die_structure_name multiple times. */
18876 if (fixup_called)
18877 return;
18878
18879 /* If we found a reference attribute and the DIE has no name, try
18880 to find a name in the referred to DIE. */
18881
18882 if (raw_name == NULL && has_specification)
18883 {
18884 struct partial_die_info *spec_die;
18885
18886 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18887 spec_die = res.pdi;
18888 cu = res.cu;
18889
18890 spec_die->fixup (cu);
18891
18892 if (spec_die->raw_name)
18893 {
18894 raw_name = spec_die->raw_name;
18895 canonical_name = spec_die->canonical_name;
18896
18897 /* Copy DW_AT_external attribute if it is set. */
18898 if (spec_die->is_external)
18899 is_external = spec_die->is_external;
18900 }
18901 }
18902
18903 if (!has_const_value && has_specification
18904 && can_have_DW_AT_const_value_p (tag))
18905 {
18906 struct partial_die_info *spec_die;
18907
18908 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18909 spec_die = res.pdi;
18910 cu = res.cu;
18911
18912 spec_die->fixup (cu);
18913
18914 if (spec_die->has_const_value)
18915 {
18916 /* Copy DW_AT_const_value attribute if it is set. */
18917 has_const_value = spec_die->has_const_value;
18918 }
18919 }
18920
18921 /* Set default names for some unnamed DIEs. */
18922
18923 if (raw_name == NULL && tag == DW_TAG_namespace)
18924 {
18925 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
18926 canonical_name = 1;
18927 }
18928
18929 /* If there is no parent die to provide a namespace, and there are
18930 children, see if we can determine the namespace from their linkage
18931 name. */
18932 if (cu->language == language_cplus
18933 && !cu->per_objfile->per_bfd->types.empty ()
18934 && die_parent == NULL
18935 && has_children
18936 && (tag == DW_TAG_class_type
18937 || tag == DW_TAG_structure_type
18938 || tag == DW_TAG_union_type))
18939 guess_partial_die_structure_name (this, cu);
18940
18941 /* GCC might emit a nameless struct or union that has a linkage
18942 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18943 if (raw_name == NULL
18944 && (tag == DW_TAG_class_type
18945 || tag == DW_TAG_interface_type
18946 || tag == DW_TAG_structure_type
18947 || tag == DW_TAG_union_type)
18948 && linkage_name != NULL)
18949 {
18950 gdb::unique_xmalloc_ptr<char> demangled
18951 (gdb_demangle (linkage_name, DMGL_TYPES));
18952 if (demangled != nullptr)
18953 {
18954 const char *base;
18955
18956 /* Strip any leading namespaces/classes, keep only the base name.
18957 DW_AT_name for named DIEs does not contain the prefixes. */
18958 base = strrchr (demangled.get (), ':');
18959 if (base && base > demangled.get () && base[-1] == ':')
18960 base++;
18961 else
18962 base = demangled.get ();
18963
18964 struct objfile *objfile = cu->per_objfile->objfile;
18965 raw_name = objfile->intern (base);
18966 canonical_name = 1;
18967 }
18968 }
18969
18970 fixup_called = 1;
18971 }
18972
18973 /* Read the .debug_loclists header contents from the given SECTION in the
18974 HEADER. */
18975 static void
18976 read_loclist_header (struct loclist_header *header,
18977 struct dwarf2_section_info *section)
18978 {
18979 unsigned int bytes_read;
18980 bfd *abfd = section->get_bfd_owner ();
18981 const gdb_byte *info_ptr = section->buffer;
18982 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18983 info_ptr += bytes_read;
18984 header->version = read_2_bytes (abfd, info_ptr);
18985 info_ptr += 2;
18986 header->addr_size = read_1_byte (abfd, info_ptr);
18987 info_ptr += 1;
18988 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18989 info_ptr += 1;
18990 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18991 }
18992
18993 /* Return the DW_AT_loclists_base value for the CU. */
18994 static ULONGEST
18995 lookup_loclist_base (struct dwarf2_cu *cu)
18996 {
18997 /* For the .dwo unit, the loclist_base points to the first offset following
18998 the header. The header consists of the following entities-
18999 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19000 bit format)
19001 2. version (2 bytes)
19002 3. address size (1 byte)
19003 4. segment selector size (1 byte)
19004 5. offset entry count (4 bytes)
19005 These sizes are derived as per the DWARFv5 standard. */
19006 if (cu->dwo_unit != nullptr)
19007 {
19008 if (cu->header.initial_length_size == 4)
19009 return LOCLIST_HEADER_SIZE32;
19010 return LOCLIST_HEADER_SIZE64;
19011 }
19012 return cu->loclist_base;
19013 }
19014
19015 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19016 array of offsets in the .debug_loclists section. */
19017 static CORE_ADDR
19018 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19019 {
19020 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19021 struct objfile *objfile = per_objfile->objfile;
19022 bfd *abfd = objfile->obfd;
19023 ULONGEST loclist_base = lookup_loclist_base (cu);
19024 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19025
19026 section->read (objfile);
19027 if (section->buffer == NULL)
19028 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19029 "section [in module %s]"), objfile_name (objfile));
19030 struct loclist_header header;
19031 read_loclist_header (&header, section);
19032 if (loclist_index >= header.offset_entry_count)
19033 complaint (_("DW_FORM_loclistx pointing outside of "
19034 ".debug_loclists offset array [in module %s]"),
19035 objfile_name (objfile));
19036 if (loclist_base + loclist_index * cu->header.offset_size
19037 >= section->size)
19038 complaint (_("DW_FORM_loclistx pointing outside of "
19039 ".debug_loclists section [in module %s]"),
19040 objfile_name (objfile));
19041 const gdb_byte *info_ptr
19042 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19043
19044 if (cu->header.offset_size == 4)
19045 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19046 else
19047 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19048 }
19049
19050 /* Process the attributes that had to be skipped in the first round. These
19051 attributes are the ones that need str_offsets_base or addr_base attributes.
19052 They could not have been processed in the first round, because at the time
19053 the values of str_offsets_base or addr_base may not have been known. */
19054 static void
19055 read_attribute_reprocess (const struct die_reader_specs *reader,
19056 struct attribute *attr)
19057 {
19058 struct dwarf2_cu *cu = reader->cu;
19059 switch (attr->form)
19060 {
19061 case DW_FORM_addrx:
19062 case DW_FORM_GNU_addr_index:
19063 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19064 break;
19065 case DW_FORM_loclistx:
19066 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19067 break;
19068 case DW_FORM_strx:
19069 case DW_FORM_strx1:
19070 case DW_FORM_strx2:
19071 case DW_FORM_strx3:
19072 case DW_FORM_strx4:
19073 case DW_FORM_GNU_str_index:
19074 {
19075 unsigned int str_index = DW_UNSND (attr);
19076 if (reader->dwo_file != NULL)
19077 {
19078 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19079 DW_STRING_IS_CANONICAL (attr) = 0;
19080 }
19081 else
19082 {
19083 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19084 DW_STRING_IS_CANONICAL (attr) = 0;
19085 }
19086 break;
19087 }
19088 default:
19089 gdb_assert_not_reached (_("Unexpected DWARF form."));
19090 }
19091 }
19092
19093 /* Read an attribute value described by an attribute form. */
19094
19095 static const gdb_byte *
19096 read_attribute_value (const struct die_reader_specs *reader,
19097 struct attribute *attr, unsigned form,
19098 LONGEST implicit_const, const gdb_byte *info_ptr,
19099 bool *need_reprocess)
19100 {
19101 struct dwarf2_cu *cu = reader->cu;
19102 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19103 struct objfile *objfile = per_objfile->objfile;
19104 bfd *abfd = reader->abfd;
19105 struct comp_unit_head *cu_header = &cu->header;
19106 unsigned int bytes_read;
19107 struct dwarf_block *blk;
19108 *need_reprocess = false;
19109
19110 attr->form = (enum dwarf_form) form;
19111 switch (form)
19112 {
19113 case DW_FORM_ref_addr:
19114 if (cu->header.version == 2)
19115 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19116 &bytes_read);
19117 else
19118 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19119 &bytes_read);
19120 info_ptr += bytes_read;
19121 break;
19122 case DW_FORM_GNU_ref_alt:
19123 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19124 info_ptr += bytes_read;
19125 break;
19126 case DW_FORM_addr:
19127 {
19128 struct gdbarch *gdbarch = objfile->arch ();
19129 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19130 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19131 info_ptr += bytes_read;
19132 }
19133 break;
19134 case DW_FORM_block2:
19135 blk = dwarf_alloc_block (cu);
19136 blk->size = read_2_bytes (abfd, info_ptr);
19137 info_ptr += 2;
19138 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19139 info_ptr += blk->size;
19140 DW_BLOCK (attr) = blk;
19141 break;
19142 case DW_FORM_block4:
19143 blk = dwarf_alloc_block (cu);
19144 blk->size = read_4_bytes (abfd, info_ptr);
19145 info_ptr += 4;
19146 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19147 info_ptr += blk->size;
19148 DW_BLOCK (attr) = blk;
19149 break;
19150 case DW_FORM_data2:
19151 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19152 info_ptr += 2;
19153 break;
19154 case DW_FORM_data4:
19155 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19156 info_ptr += 4;
19157 break;
19158 case DW_FORM_data8:
19159 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19160 info_ptr += 8;
19161 break;
19162 case DW_FORM_data16:
19163 blk = dwarf_alloc_block (cu);
19164 blk->size = 16;
19165 blk->data = read_n_bytes (abfd, info_ptr, 16);
19166 info_ptr += 16;
19167 DW_BLOCK (attr) = blk;
19168 break;
19169 case DW_FORM_sec_offset:
19170 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19171 info_ptr += bytes_read;
19172 break;
19173 case DW_FORM_loclistx:
19174 {
19175 *need_reprocess = true;
19176 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19177 info_ptr += bytes_read;
19178 }
19179 break;
19180 case DW_FORM_string:
19181 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19182 DW_STRING_IS_CANONICAL (attr) = 0;
19183 info_ptr += bytes_read;
19184 break;
19185 case DW_FORM_strp:
19186 if (!cu->per_cu->is_dwz)
19187 {
19188 DW_STRING (attr) = read_indirect_string (per_objfile,
19189 abfd, info_ptr, cu_header,
19190 &bytes_read);
19191 DW_STRING_IS_CANONICAL (attr) = 0;
19192 info_ptr += bytes_read;
19193 break;
19194 }
19195 /* FALLTHROUGH */
19196 case DW_FORM_line_strp:
19197 if (!cu->per_cu->is_dwz)
19198 {
19199 DW_STRING (attr) = per_objfile->read_line_string (info_ptr, cu_header,
19200 &bytes_read);
19201 DW_STRING_IS_CANONICAL (attr) = 0;
19202 info_ptr += bytes_read;
19203 break;
19204 }
19205 /* FALLTHROUGH */
19206 case DW_FORM_GNU_strp_alt:
19207 {
19208 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
19209 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19210 &bytes_read);
19211
19212 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19213 DW_STRING_IS_CANONICAL (attr) = 0;
19214 info_ptr += bytes_read;
19215 }
19216 break;
19217 case DW_FORM_exprloc:
19218 case DW_FORM_block:
19219 blk = dwarf_alloc_block (cu);
19220 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19221 info_ptr += bytes_read;
19222 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19223 info_ptr += blk->size;
19224 DW_BLOCK (attr) = blk;
19225 break;
19226 case DW_FORM_block1:
19227 blk = dwarf_alloc_block (cu);
19228 blk->size = read_1_byte (abfd, info_ptr);
19229 info_ptr += 1;
19230 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19231 info_ptr += blk->size;
19232 DW_BLOCK (attr) = blk;
19233 break;
19234 case DW_FORM_data1:
19235 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19236 info_ptr += 1;
19237 break;
19238 case DW_FORM_flag:
19239 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19240 info_ptr += 1;
19241 break;
19242 case DW_FORM_flag_present:
19243 DW_UNSND (attr) = 1;
19244 break;
19245 case DW_FORM_sdata:
19246 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19247 info_ptr += bytes_read;
19248 break;
19249 case DW_FORM_udata:
19250 case DW_FORM_rnglistx:
19251 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19252 info_ptr += bytes_read;
19253 break;
19254 case DW_FORM_ref1:
19255 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19256 + read_1_byte (abfd, info_ptr));
19257 info_ptr += 1;
19258 break;
19259 case DW_FORM_ref2:
19260 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19261 + read_2_bytes (abfd, info_ptr));
19262 info_ptr += 2;
19263 break;
19264 case DW_FORM_ref4:
19265 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19266 + read_4_bytes (abfd, info_ptr));
19267 info_ptr += 4;
19268 break;
19269 case DW_FORM_ref8:
19270 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19271 + read_8_bytes (abfd, info_ptr));
19272 info_ptr += 8;
19273 break;
19274 case DW_FORM_ref_sig8:
19275 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19276 info_ptr += 8;
19277 break;
19278 case DW_FORM_ref_udata:
19279 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19280 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19281 info_ptr += bytes_read;
19282 break;
19283 case DW_FORM_indirect:
19284 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19285 info_ptr += bytes_read;
19286 if (form == DW_FORM_implicit_const)
19287 {
19288 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19289 info_ptr += bytes_read;
19290 }
19291 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19292 info_ptr, need_reprocess);
19293 break;
19294 case DW_FORM_implicit_const:
19295 DW_SND (attr) = implicit_const;
19296 break;
19297 case DW_FORM_addrx:
19298 case DW_FORM_GNU_addr_index:
19299 *need_reprocess = true;
19300 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19301 info_ptr += bytes_read;
19302 break;
19303 case DW_FORM_strx:
19304 case DW_FORM_strx1:
19305 case DW_FORM_strx2:
19306 case DW_FORM_strx3:
19307 case DW_FORM_strx4:
19308 case DW_FORM_GNU_str_index:
19309 {
19310 ULONGEST str_index;
19311 if (form == DW_FORM_strx1)
19312 {
19313 str_index = read_1_byte (abfd, info_ptr);
19314 info_ptr += 1;
19315 }
19316 else if (form == DW_FORM_strx2)
19317 {
19318 str_index = read_2_bytes (abfd, info_ptr);
19319 info_ptr += 2;
19320 }
19321 else if (form == DW_FORM_strx3)
19322 {
19323 str_index = read_3_bytes (abfd, info_ptr);
19324 info_ptr += 3;
19325 }
19326 else if (form == DW_FORM_strx4)
19327 {
19328 str_index = read_4_bytes (abfd, info_ptr);
19329 info_ptr += 4;
19330 }
19331 else
19332 {
19333 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19334 info_ptr += bytes_read;
19335 }
19336 *need_reprocess = true;
19337 DW_UNSND (attr) = str_index;
19338 }
19339 break;
19340 default:
19341 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19342 dwarf_form_name (form),
19343 bfd_get_filename (abfd));
19344 }
19345
19346 /* Super hack. */
19347 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19348 attr->form = DW_FORM_GNU_ref_alt;
19349
19350 /* We have seen instances where the compiler tried to emit a byte
19351 size attribute of -1 which ended up being encoded as an unsigned
19352 0xffffffff. Although 0xffffffff is technically a valid size value,
19353 an object of this size seems pretty unlikely so we can relatively
19354 safely treat these cases as if the size attribute was invalid and
19355 treat them as zero by default. */
19356 if (attr->name == DW_AT_byte_size
19357 && form == DW_FORM_data4
19358 && DW_UNSND (attr) >= 0xffffffff)
19359 {
19360 complaint
19361 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19362 hex_string (DW_UNSND (attr)));
19363 DW_UNSND (attr) = 0;
19364 }
19365
19366 return info_ptr;
19367 }
19368
19369 /* Read an attribute described by an abbreviated attribute. */
19370
19371 static const gdb_byte *
19372 read_attribute (const struct die_reader_specs *reader,
19373 struct attribute *attr, struct attr_abbrev *abbrev,
19374 const gdb_byte *info_ptr, bool *need_reprocess)
19375 {
19376 attr->name = abbrev->name;
19377 return read_attribute_value (reader, attr, abbrev->form,
19378 abbrev->implicit_const, info_ptr,
19379 need_reprocess);
19380 }
19381
19382 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19383
19384 static const char *
19385 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19386 LONGEST str_offset)
19387 {
19388 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19389 str_offset, "DW_FORM_strp");
19390 }
19391
19392 /* Return pointer to string at .debug_str offset as read from BUF.
19393 BUF is assumed to be in a compilation unit described by CU_HEADER.
19394 Return *BYTES_READ_PTR count of bytes read from BUF. */
19395
19396 static const char *
19397 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19398 const gdb_byte *buf,
19399 const struct comp_unit_head *cu_header,
19400 unsigned int *bytes_read_ptr)
19401 {
19402 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19403
19404 return read_indirect_string_at_offset (per_objfile, str_offset);
19405 }
19406
19407 /* See read.h. */
19408
19409 const char *
19410 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19411 const struct comp_unit_head *cu_header,
19412 unsigned int *bytes_read_ptr)
19413 {
19414 bfd *abfd = objfile->obfd;
19415 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19416
19417 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19418 }
19419
19420 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19421 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19422 ADDR_SIZE is the size of addresses from the CU header. */
19423
19424 static CORE_ADDR
19425 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19426 gdb::optional<ULONGEST> addr_base, int addr_size)
19427 {
19428 struct objfile *objfile = per_objfile->objfile;
19429 bfd *abfd = objfile->obfd;
19430 const gdb_byte *info_ptr;
19431 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19432
19433 per_objfile->per_bfd->addr.read (objfile);
19434 if (per_objfile->per_bfd->addr.buffer == NULL)
19435 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19436 objfile_name (objfile));
19437 if (addr_base_or_zero + addr_index * addr_size
19438 >= per_objfile->per_bfd->addr.size)
19439 error (_("DW_FORM_addr_index pointing outside of "
19440 ".debug_addr section [in module %s]"),
19441 objfile_name (objfile));
19442 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19443 + addr_index * addr_size);
19444 if (addr_size == 4)
19445 return bfd_get_32 (abfd, info_ptr);
19446 else
19447 return bfd_get_64 (abfd, info_ptr);
19448 }
19449
19450 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19451
19452 static CORE_ADDR
19453 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19454 {
19455 return read_addr_index_1 (cu->per_objfile, addr_index,
19456 cu->addr_base, cu->header.addr_size);
19457 }
19458
19459 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19460
19461 static CORE_ADDR
19462 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19463 unsigned int *bytes_read)
19464 {
19465 bfd *abfd = cu->per_objfile->objfile->obfd;
19466 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19467
19468 return read_addr_index (cu, addr_index);
19469 }
19470
19471 /* See read.h. */
19472
19473 CORE_ADDR
19474 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19475 dwarf2_per_objfile *per_objfile,
19476 unsigned int addr_index)
19477 {
19478 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
19479 gdb::optional<ULONGEST> addr_base;
19480 int addr_size;
19481
19482 /* We need addr_base and addr_size.
19483 If we don't have PER_CU->cu, we have to get it.
19484 Nasty, but the alternative is storing the needed info in PER_CU,
19485 which at this point doesn't seem justified: it's not clear how frequently
19486 it would get used and it would increase the size of every PER_CU.
19487 Entry points like dwarf2_per_cu_addr_size do a similar thing
19488 so we're not in uncharted territory here.
19489 Alas we need to be a bit more complicated as addr_base is contained
19490 in the DIE.
19491
19492 We don't need to read the entire CU(/TU).
19493 We just need the header and top level die.
19494
19495 IWBN to use the aging mechanism to let us lazily later discard the CU.
19496 For now we skip this optimization. */
19497
19498 if (cu != NULL)
19499 {
19500 addr_base = cu->addr_base;
19501 addr_size = cu->header.addr_size;
19502 }
19503 else
19504 {
19505 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
19506 addr_base = reader.cu->addr_base;
19507 addr_size = reader.cu->header.addr_size;
19508 }
19509
19510 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
19511 }
19512
19513 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19514 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19515 DWO file. */
19516
19517 static const char *
19518 read_str_index (struct dwarf2_cu *cu,
19519 struct dwarf2_section_info *str_section,
19520 struct dwarf2_section_info *str_offsets_section,
19521 ULONGEST str_offsets_base, ULONGEST str_index)
19522 {
19523 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19524 struct objfile *objfile = per_objfile->objfile;
19525 const char *objf_name = objfile_name (objfile);
19526 bfd *abfd = objfile->obfd;
19527 const gdb_byte *info_ptr;
19528 ULONGEST str_offset;
19529 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19530
19531 str_section->read (objfile);
19532 str_offsets_section->read (objfile);
19533 if (str_section->buffer == NULL)
19534 error (_("%s used without %s section"
19535 " in CU at offset %s [in module %s]"),
19536 form_name, str_section->get_name (),
19537 sect_offset_str (cu->header.sect_off), objf_name);
19538 if (str_offsets_section->buffer == NULL)
19539 error (_("%s used without %s section"
19540 " in CU at offset %s [in module %s]"),
19541 form_name, str_section->get_name (),
19542 sect_offset_str (cu->header.sect_off), objf_name);
19543 info_ptr = (str_offsets_section->buffer
19544 + str_offsets_base
19545 + str_index * cu->header.offset_size);
19546 if (cu->header.offset_size == 4)
19547 str_offset = bfd_get_32 (abfd, info_ptr);
19548 else
19549 str_offset = bfd_get_64 (abfd, info_ptr);
19550 if (str_offset >= str_section->size)
19551 error (_("Offset from %s pointing outside of"
19552 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19553 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19554 return (const char *) (str_section->buffer + str_offset);
19555 }
19556
19557 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19558
19559 static const char *
19560 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19561 {
19562 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19563 ? reader->cu->header.addr_size : 0;
19564 return read_str_index (reader->cu,
19565 &reader->dwo_file->sections.str,
19566 &reader->dwo_file->sections.str_offsets,
19567 str_offsets_base, str_index);
19568 }
19569
19570 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19571
19572 static const char *
19573 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19574 {
19575 struct objfile *objfile = cu->per_objfile->objfile;
19576 const char *objf_name = objfile_name (objfile);
19577 static const char form_name[] = "DW_FORM_GNU_str_index";
19578 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19579
19580 if (!cu->str_offsets_base.has_value ())
19581 error (_("%s used in Fission stub without %s"
19582 " in CU at offset 0x%lx [in module %s]"),
19583 form_name, str_offsets_attr_name,
19584 (long) cu->header.offset_size, objf_name);
19585
19586 return read_str_index (cu,
19587 &cu->per_objfile->per_bfd->str,
19588 &cu->per_objfile->per_bfd->str_offsets,
19589 *cu->str_offsets_base, str_index);
19590 }
19591
19592 /* Return the length of an LEB128 number in BUF. */
19593
19594 static int
19595 leb128_size (const gdb_byte *buf)
19596 {
19597 const gdb_byte *begin = buf;
19598 gdb_byte byte;
19599
19600 while (1)
19601 {
19602 byte = *buf++;
19603 if ((byte & 128) == 0)
19604 return buf - begin;
19605 }
19606 }
19607
19608 static void
19609 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19610 {
19611 switch (lang)
19612 {
19613 case DW_LANG_C89:
19614 case DW_LANG_C99:
19615 case DW_LANG_C11:
19616 case DW_LANG_C:
19617 case DW_LANG_UPC:
19618 cu->language = language_c;
19619 break;
19620 case DW_LANG_Java:
19621 case DW_LANG_C_plus_plus:
19622 case DW_LANG_C_plus_plus_11:
19623 case DW_LANG_C_plus_plus_14:
19624 cu->language = language_cplus;
19625 break;
19626 case DW_LANG_D:
19627 cu->language = language_d;
19628 break;
19629 case DW_LANG_Fortran77:
19630 case DW_LANG_Fortran90:
19631 case DW_LANG_Fortran95:
19632 case DW_LANG_Fortran03:
19633 case DW_LANG_Fortran08:
19634 cu->language = language_fortran;
19635 break;
19636 case DW_LANG_Go:
19637 cu->language = language_go;
19638 break;
19639 case DW_LANG_Mips_Assembler:
19640 cu->language = language_asm;
19641 break;
19642 case DW_LANG_Ada83:
19643 case DW_LANG_Ada95:
19644 cu->language = language_ada;
19645 break;
19646 case DW_LANG_Modula2:
19647 cu->language = language_m2;
19648 break;
19649 case DW_LANG_Pascal83:
19650 cu->language = language_pascal;
19651 break;
19652 case DW_LANG_ObjC:
19653 cu->language = language_objc;
19654 break;
19655 case DW_LANG_Rust:
19656 case DW_LANG_Rust_old:
19657 cu->language = language_rust;
19658 break;
19659 case DW_LANG_Cobol74:
19660 case DW_LANG_Cobol85:
19661 default:
19662 cu->language = language_minimal;
19663 break;
19664 }
19665 cu->language_defn = language_def (cu->language);
19666 }
19667
19668 /* Return the named attribute or NULL if not there. */
19669
19670 static struct attribute *
19671 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19672 {
19673 for (;;)
19674 {
19675 unsigned int i;
19676 struct attribute *spec = NULL;
19677
19678 for (i = 0; i < die->num_attrs; ++i)
19679 {
19680 if (die->attrs[i].name == name)
19681 return &die->attrs[i];
19682 if (die->attrs[i].name == DW_AT_specification
19683 || die->attrs[i].name == DW_AT_abstract_origin)
19684 spec = &die->attrs[i];
19685 }
19686
19687 if (!spec)
19688 break;
19689
19690 die = follow_die_ref (die, spec, &cu);
19691 }
19692
19693 return NULL;
19694 }
19695
19696 /* Return the string associated with a string-typed attribute, or NULL if it
19697 is either not found or is of an incorrect type. */
19698
19699 static const char *
19700 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19701 {
19702 struct attribute *attr;
19703 const char *str = NULL;
19704
19705 attr = dwarf2_attr (die, name, cu);
19706
19707 if (attr != NULL)
19708 {
19709 str = attr->value_as_string ();
19710 if (str == nullptr)
19711 complaint (_("string type expected for attribute %s for "
19712 "DIE at %s in module %s"),
19713 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19714 objfile_name (cu->per_objfile->objfile));
19715 }
19716
19717 return str;
19718 }
19719
19720 /* Return the dwo name or NULL if not present. If present, it is in either
19721 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19722 static const char *
19723 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19724 {
19725 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19726 if (dwo_name == nullptr)
19727 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19728 return dwo_name;
19729 }
19730
19731 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19732 and holds a non-zero value. This function should only be used for
19733 DW_FORM_flag or DW_FORM_flag_present attributes. */
19734
19735 static int
19736 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19737 {
19738 struct attribute *attr = dwarf2_attr (die, name, cu);
19739
19740 return (attr && DW_UNSND (attr));
19741 }
19742
19743 static int
19744 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19745 {
19746 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19747 which value is non-zero. However, we have to be careful with
19748 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19749 (via dwarf2_flag_true_p) follows this attribute. So we may
19750 end up accidently finding a declaration attribute that belongs
19751 to a different DIE referenced by the specification attribute,
19752 even though the given DIE does not have a declaration attribute. */
19753 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19754 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19755 }
19756
19757 /* Return the die giving the specification for DIE, if there is
19758 one. *SPEC_CU is the CU containing DIE on input, and the CU
19759 containing the return value on output. If there is no
19760 specification, but there is an abstract origin, that is
19761 returned. */
19762
19763 static struct die_info *
19764 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19765 {
19766 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19767 *spec_cu);
19768
19769 if (spec_attr == NULL)
19770 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19771
19772 if (spec_attr == NULL)
19773 return NULL;
19774 else
19775 return follow_die_ref (die, spec_attr, spec_cu);
19776 }
19777
19778 /* Stub for free_line_header to match void * callback types. */
19779
19780 static void
19781 free_line_header_voidp (void *arg)
19782 {
19783 struct line_header *lh = (struct line_header *) arg;
19784
19785 delete lh;
19786 }
19787
19788 /* A convenience function to find the proper .debug_line section for a CU. */
19789
19790 static struct dwarf2_section_info *
19791 get_debug_line_section (struct dwarf2_cu *cu)
19792 {
19793 struct dwarf2_section_info *section;
19794 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19795
19796 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19797 DWO file. */
19798 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19799 section = &cu->dwo_unit->dwo_file->sections.line;
19800 else if (cu->per_cu->is_dwz)
19801 {
19802 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
19803
19804 section = &dwz->line;
19805 }
19806 else
19807 section = &per_objfile->per_bfd->line;
19808
19809 return section;
19810 }
19811
19812 /* Read the statement program header starting at OFFSET in
19813 .debug_line, or .debug_line.dwo. Return a pointer
19814 to a struct line_header, allocated using xmalloc.
19815 Returns NULL if there is a problem reading the header, e.g., if it
19816 has a version we don't understand.
19817
19818 NOTE: the strings in the include directory and file name tables of
19819 the returned object point into the dwarf line section buffer,
19820 and must not be freed. */
19821
19822 static line_header_up
19823 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19824 {
19825 struct dwarf2_section_info *section;
19826 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19827
19828 section = get_debug_line_section (cu);
19829 section->read (per_objfile->objfile);
19830 if (section->buffer == NULL)
19831 {
19832 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19833 complaint (_("missing .debug_line.dwo section"));
19834 else
19835 complaint (_("missing .debug_line section"));
19836 return 0;
19837 }
19838
19839 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19840 per_objfile, section, &cu->header);
19841 }
19842
19843 /* Subroutine of dwarf_decode_lines to simplify it.
19844 Return the file name of the psymtab for the given file_entry.
19845 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19846 If space for the result is malloc'd, *NAME_HOLDER will be set.
19847 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19848
19849 static const char *
19850 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19851 const dwarf2_psymtab *pst,
19852 const char *comp_dir,
19853 gdb::unique_xmalloc_ptr<char> *name_holder)
19854 {
19855 const char *include_name = fe.name;
19856 const char *include_name_to_compare = include_name;
19857 const char *pst_filename;
19858 int file_is_pst;
19859
19860 const char *dir_name = fe.include_dir (lh);
19861
19862 gdb::unique_xmalloc_ptr<char> hold_compare;
19863 if (!IS_ABSOLUTE_PATH (include_name)
19864 && (dir_name != NULL || comp_dir != NULL))
19865 {
19866 /* Avoid creating a duplicate psymtab for PST.
19867 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19868 Before we do the comparison, however, we need to account
19869 for DIR_NAME and COMP_DIR.
19870 First prepend dir_name (if non-NULL). If we still don't
19871 have an absolute path prepend comp_dir (if non-NULL).
19872 However, the directory we record in the include-file's
19873 psymtab does not contain COMP_DIR (to match the
19874 corresponding symtab(s)).
19875
19876 Example:
19877
19878 bash$ cd /tmp
19879 bash$ gcc -g ./hello.c
19880 include_name = "hello.c"
19881 dir_name = "."
19882 DW_AT_comp_dir = comp_dir = "/tmp"
19883 DW_AT_name = "./hello.c"
19884
19885 */
19886
19887 if (dir_name != NULL)
19888 {
19889 name_holder->reset (concat (dir_name, SLASH_STRING,
19890 include_name, (char *) NULL));
19891 include_name = name_holder->get ();
19892 include_name_to_compare = include_name;
19893 }
19894 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19895 {
19896 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19897 include_name, (char *) NULL));
19898 include_name_to_compare = hold_compare.get ();
19899 }
19900 }
19901
19902 pst_filename = pst->filename;
19903 gdb::unique_xmalloc_ptr<char> copied_name;
19904 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19905 {
19906 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19907 pst_filename, (char *) NULL));
19908 pst_filename = copied_name.get ();
19909 }
19910
19911 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19912
19913 if (file_is_pst)
19914 return NULL;
19915 return include_name;
19916 }
19917
19918 /* State machine to track the state of the line number program. */
19919
19920 class lnp_state_machine
19921 {
19922 public:
19923 /* Initialize a machine state for the start of a line number
19924 program. */
19925 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19926 bool record_lines_p);
19927
19928 file_entry *current_file ()
19929 {
19930 /* lh->file_names is 0-based, but the file name numbers in the
19931 statement program are 1-based. */
19932 return m_line_header->file_name_at (m_file);
19933 }
19934
19935 /* Record the line in the state machine. END_SEQUENCE is true if
19936 we're processing the end of a sequence. */
19937 void record_line (bool end_sequence);
19938
19939 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19940 nop-out rest of the lines in this sequence. */
19941 void check_line_address (struct dwarf2_cu *cu,
19942 const gdb_byte *line_ptr,
19943 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19944
19945 void handle_set_discriminator (unsigned int discriminator)
19946 {
19947 m_discriminator = discriminator;
19948 m_line_has_non_zero_discriminator |= discriminator != 0;
19949 }
19950
19951 /* Handle DW_LNE_set_address. */
19952 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19953 {
19954 m_op_index = 0;
19955 address += baseaddr;
19956 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19957 }
19958
19959 /* Handle DW_LNS_advance_pc. */
19960 void handle_advance_pc (CORE_ADDR adjust);
19961
19962 /* Handle a special opcode. */
19963 void handle_special_opcode (unsigned char op_code);
19964
19965 /* Handle DW_LNS_advance_line. */
19966 void handle_advance_line (int line_delta)
19967 {
19968 advance_line (line_delta);
19969 }
19970
19971 /* Handle DW_LNS_set_file. */
19972 void handle_set_file (file_name_index file);
19973
19974 /* Handle DW_LNS_negate_stmt. */
19975 void handle_negate_stmt ()
19976 {
19977 m_is_stmt = !m_is_stmt;
19978 }
19979
19980 /* Handle DW_LNS_const_add_pc. */
19981 void handle_const_add_pc ();
19982
19983 /* Handle DW_LNS_fixed_advance_pc. */
19984 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19985 {
19986 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19987 m_op_index = 0;
19988 }
19989
19990 /* Handle DW_LNS_copy. */
19991 void handle_copy ()
19992 {
19993 record_line (false);
19994 m_discriminator = 0;
19995 }
19996
19997 /* Handle DW_LNE_end_sequence. */
19998 void handle_end_sequence ()
19999 {
20000 m_currently_recording_lines = true;
20001 }
20002
20003 private:
20004 /* Advance the line by LINE_DELTA. */
20005 void advance_line (int line_delta)
20006 {
20007 m_line += line_delta;
20008
20009 if (line_delta != 0)
20010 m_line_has_non_zero_discriminator = m_discriminator != 0;
20011 }
20012
20013 struct dwarf2_cu *m_cu;
20014
20015 gdbarch *m_gdbarch;
20016
20017 /* True if we're recording lines.
20018 Otherwise we're building partial symtabs and are just interested in
20019 finding include files mentioned by the line number program. */
20020 bool m_record_lines_p;
20021
20022 /* The line number header. */
20023 line_header *m_line_header;
20024
20025 /* These are part of the standard DWARF line number state machine,
20026 and initialized according to the DWARF spec. */
20027
20028 unsigned char m_op_index = 0;
20029 /* The line table index of the current file. */
20030 file_name_index m_file = 1;
20031 unsigned int m_line = 1;
20032
20033 /* These are initialized in the constructor. */
20034
20035 CORE_ADDR m_address;
20036 bool m_is_stmt;
20037 unsigned int m_discriminator;
20038
20039 /* Additional bits of state we need to track. */
20040
20041 /* The last file that we called dwarf2_start_subfile for.
20042 This is only used for TLLs. */
20043 unsigned int m_last_file = 0;
20044 /* The last file a line number was recorded for. */
20045 struct subfile *m_last_subfile = NULL;
20046
20047 /* The address of the last line entry. */
20048 CORE_ADDR m_last_address;
20049
20050 /* Set to true when a previous line at the same address (using
20051 m_last_address) had m_is_stmt true. This is reset to false when a
20052 line entry at a new address (m_address different to m_last_address) is
20053 processed. */
20054 bool m_stmt_at_address = false;
20055
20056 /* When true, record the lines we decode. */
20057 bool m_currently_recording_lines = false;
20058
20059 /* The last line number that was recorded, used to coalesce
20060 consecutive entries for the same line. This can happen, for
20061 example, when discriminators are present. PR 17276. */
20062 unsigned int m_last_line = 0;
20063 bool m_line_has_non_zero_discriminator = false;
20064 };
20065
20066 void
20067 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20068 {
20069 CORE_ADDR addr_adj = (((m_op_index + adjust)
20070 / m_line_header->maximum_ops_per_instruction)
20071 * m_line_header->minimum_instruction_length);
20072 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20073 m_op_index = ((m_op_index + adjust)
20074 % m_line_header->maximum_ops_per_instruction);
20075 }
20076
20077 void
20078 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20079 {
20080 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20081 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20082 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20083 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20084 / m_line_header->maximum_ops_per_instruction)
20085 * m_line_header->minimum_instruction_length);
20086 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20087 m_op_index = ((m_op_index + adj_opcode_d)
20088 % m_line_header->maximum_ops_per_instruction);
20089
20090 int line_delta = m_line_header->line_base + adj_opcode_r;
20091 advance_line (line_delta);
20092 record_line (false);
20093 m_discriminator = 0;
20094 }
20095
20096 void
20097 lnp_state_machine::handle_set_file (file_name_index file)
20098 {
20099 m_file = file;
20100
20101 const file_entry *fe = current_file ();
20102 if (fe == NULL)
20103 dwarf2_debug_line_missing_file_complaint ();
20104 else if (m_record_lines_p)
20105 {
20106 const char *dir = fe->include_dir (m_line_header);
20107
20108 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20109 m_line_has_non_zero_discriminator = m_discriminator != 0;
20110 dwarf2_start_subfile (m_cu, fe->name, dir);
20111 }
20112 }
20113
20114 void
20115 lnp_state_machine::handle_const_add_pc ()
20116 {
20117 CORE_ADDR adjust
20118 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20119
20120 CORE_ADDR addr_adj
20121 = (((m_op_index + adjust)
20122 / m_line_header->maximum_ops_per_instruction)
20123 * m_line_header->minimum_instruction_length);
20124
20125 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20126 m_op_index = ((m_op_index + adjust)
20127 % m_line_header->maximum_ops_per_instruction);
20128 }
20129
20130 /* Return non-zero if we should add LINE to the line number table.
20131 LINE is the line to add, LAST_LINE is the last line that was added,
20132 LAST_SUBFILE is the subfile for LAST_LINE.
20133 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20134 had a non-zero discriminator.
20135
20136 We have to be careful in the presence of discriminators.
20137 E.g., for this line:
20138
20139 for (i = 0; i < 100000; i++);
20140
20141 clang can emit four line number entries for that one line,
20142 each with a different discriminator.
20143 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20144
20145 However, we want gdb to coalesce all four entries into one.
20146 Otherwise the user could stepi into the middle of the line and
20147 gdb would get confused about whether the pc really was in the
20148 middle of the line.
20149
20150 Things are further complicated by the fact that two consecutive
20151 line number entries for the same line is a heuristic used by gcc
20152 to denote the end of the prologue. So we can't just discard duplicate
20153 entries, we have to be selective about it. The heuristic we use is
20154 that we only collapse consecutive entries for the same line if at least
20155 one of those entries has a non-zero discriminator. PR 17276.
20156
20157 Note: Addresses in the line number state machine can never go backwards
20158 within one sequence, thus this coalescing is ok. */
20159
20160 static int
20161 dwarf_record_line_p (struct dwarf2_cu *cu,
20162 unsigned int line, unsigned int last_line,
20163 int line_has_non_zero_discriminator,
20164 struct subfile *last_subfile)
20165 {
20166 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20167 return 1;
20168 if (line != last_line)
20169 return 1;
20170 /* Same line for the same file that we've seen already.
20171 As a last check, for pr 17276, only record the line if the line
20172 has never had a non-zero discriminator. */
20173 if (!line_has_non_zero_discriminator)
20174 return 1;
20175 return 0;
20176 }
20177
20178 /* Use the CU's builder to record line number LINE beginning at
20179 address ADDRESS in the line table of subfile SUBFILE. */
20180
20181 static void
20182 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20183 unsigned int line, CORE_ADDR address, bool is_stmt,
20184 struct dwarf2_cu *cu)
20185 {
20186 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20187
20188 if (dwarf_line_debug)
20189 {
20190 fprintf_unfiltered (gdb_stdlog,
20191 "Recording line %u, file %s, address %s\n",
20192 line, lbasename (subfile->name),
20193 paddress (gdbarch, address));
20194 }
20195
20196 if (cu != nullptr)
20197 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20198 }
20199
20200 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20201 Mark the end of a set of line number records.
20202 The arguments are the same as for dwarf_record_line_1.
20203 If SUBFILE is NULL the request is ignored. */
20204
20205 static void
20206 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20207 CORE_ADDR address, struct dwarf2_cu *cu)
20208 {
20209 if (subfile == NULL)
20210 return;
20211
20212 if (dwarf_line_debug)
20213 {
20214 fprintf_unfiltered (gdb_stdlog,
20215 "Finishing current line, file %s, address %s\n",
20216 lbasename (subfile->name),
20217 paddress (gdbarch, address));
20218 }
20219
20220 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20221 }
20222
20223 void
20224 lnp_state_machine::record_line (bool end_sequence)
20225 {
20226 if (dwarf_line_debug)
20227 {
20228 fprintf_unfiltered (gdb_stdlog,
20229 "Processing actual line %u: file %u,"
20230 " address %s, is_stmt %u, discrim %u%s\n",
20231 m_line, m_file,
20232 paddress (m_gdbarch, m_address),
20233 m_is_stmt, m_discriminator,
20234 (end_sequence ? "\t(end sequence)" : ""));
20235 }
20236
20237 file_entry *fe = current_file ();
20238
20239 if (fe == NULL)
20240 dwarf2_debug_line_missing_file_complaint ();
20241 /* For now we ignore lines not starting on an instruction boundary.
20242 But not when processing end_sequence for compatibility with the
20243 previous version of the code. */
20244 else if (m_op_index == 0 || end_sequence)
20245 {
20246 fe->included_p = 1;
20247 if (m_record_lines_p)
20248 {
20249 /* When we switch files we insert an end maker in the first file,
20250 switch to the second file and add a new line entry. The
20251 problem is that the end marker inserted in the first file will
20252 discard any previous line entries at the same address. If the
20253 line entries in the first file are marked as is-stmt, while
20254 the new line in the second file is non-stmt, then this means
20255 the end marker will discard is-stmt lines so we can have a
20256 non-stmt line. This means that there are less addresses at
20257 which the user can insert a breakpoint.
20258
20259 To improve this we track the last address in m_last_address,
20260 and whether we have seen an is-stmt at this address. Then
20261 when switching files, if we have seen a stmt at the current
20262 address, and we are switching to create a non-stmt line, then
20263 discard the new line. */
20264 bool file_changed
20265 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20266 bool ignore_this_line
20267 = (file_changed && !end_sequence && m_last_address == m_address
20268 && !m_is_stmt && m_stmt_at_address);
20269
20270 if ((file_changed && !ignore_this_line) || end_sequence)
20271 {
20272 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20273 m_currently_recording_lines ? m_cu : nullptr);
20274 }
20275
20276 if (!end_sequence && !ignore_this_line)
20277 {
20278 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20279
20280 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20281 m_line_has_non_zero_discriminator,
20282 m_last_subfile))
20283 {
20284 buildsym_compunit *builder = m_cu->get_builder ();
20285 dwarf_record_line_1 (m_gdbarch,
20286 builder->get_current_subfile (),
20287 m_line, m_address, is_stmt,
20288 m_currently_recording_lines ? m_cu : nullptr);
20289 }
20290 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20291 m_last_line = m_line;
20292 }
20293 }
20294 }
20295
20296 /* Track whether we have seen any m_is_stmt true at m_address in case we
20297 have multiple line table entries all at m_address. */
20298 if (m_last_address != m_address)
20299 {
20300 m_stmt_at_address = false;
20301 m_last_address = m_address;
20302 }
20303 m_stmt_at_address |= m_is_stmt;
20304 }
20305
20306 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20307 line_header *lh, bool record_lines_p)
20308 {
20309 m_cu = cu;
20310 m_gdbarch = arch;
20311 m_record_lines_p = record_lines_p;
20312 m_line_header = lh;
20313
20314 m_currently_recording_lines = true;
20315
20316 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20317 was a line entry for it so that the backend has a chance to adjust it
20318 and also record it in case it needs it. This is currently used by MIPS
20319 code, cf. `mips_adjust_dwarf2_line'. */
20320 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20321 m_is_stmt = lh->default_is_stmt;
20322 m_discriminator = 0;
20323
20324 m_last_address = m_address;
20325 m_stmt_at_address = false;
20326 }
20327
20328 void
20329 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20330 const gdb_byte *line_ptr,
20331 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20332 {
20333 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20334 the pc range of the CU. However, we restrict the test to only ADDRESS
20335 values of zero to preserve GDB's previous behaviour which is to handle
20336 the specific case of a function being GC'd by the linker. */
20337
20338 if (address == 0 && address < unrelocated_lowpc)
20339 {
20340 /* This line table is for a function which has been
20341 GCd by the linker. Ignore it. PR gdb/12528 */
20342
20343 struct objfile *objfile = cu->per_objfile->objfile;
20344 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20345
20346 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20347 line_offset, objfile_name (objfile));
20348 m_currently_recording_lines = false;
20349 /* Note: m_currently_recording_lines is left as false until we see
20350 DW_LNE_end_sequence. */
20351 }
20352 }
20353
20354 /* Subroutine of dwarf_decode_lines to simplify it.
20355 Process the line number information in LH.
20356 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20357 program in order to set included_p for every referenced header. */
20358
20359 static void
20360 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20361 const int decode_for_pst_p, CORE_ADDR lowpc)
20362 {
20363 const gdb_byte *line_ptr, *extended_end;
20364 const gdb_byte *line_end;
20365 unsigned int bytes_read, extended_len;
20366 unsigned char op_code, extended_op;
20367 CORE_ADDR baseaddr;
20368 struct objfile *objfile = cu->per_objfile->objfile;
20369 bfd *abfd = objfile->obfd;
20370 struct gdbarch *gdbarch = objfile->arch ();
20371 /* True if we're recording line info (as opposed to building partial
20372 symtabs and just interested in finding include files mentioned by
20373 the line number program). */
20374 bool record_lines_p = !decode_for_pst_p;
20375
20376 baseaddr = objfile->text_section_offset ();
20377
20378 line_ptr = lh->statement_program_start;
20379 line_end = lh->statement_program_end;
20380
20381 /* Read the statement sequences until there's nothing left. */
20382 while (line_ptr < line_end)
20383 {
20384 /* The DWARF line number program state machine. Reset the state
20385 machine at the start of each sequence. */
20386 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20387 bool end_sequence = false;
20388
20389 if (record_lines_p)
20390 {
20391 /* Start a subfile for the current file of the state
20392 machine. */
20393 const file_entry *fe = state_machine.current_file ();
20394
20395 if (fe != NULL)
20396 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20397 }
20398
20399 /* Decode the table. */
20400 while (line_ptr < line_end && !end_sequence)
20401 {
20402 op_code = read_1_byte (abfd, line_ptr);
20403 line_ptr += 1;
20404
20405 if (op_code >= lh->opcode_base)
20406 {
20407 /* Special opcode. */
20408 state_machine.handle_special_opcode (op_code);
20409 }
20410 else switch (op_code)
20411 {
20412 case DW_LNS_extended_op:
20413 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20414 &bytes_read);
20415 line_ptr += bytes_read;
20416 extended_end = line_ptr + extended_len;
20417 extended_op = read_1_byte (abfd, line_ptr);
20418 line_ptr += 1;
20419 switch (extended_op)
20420 {
20421 case DW_LNE_end_sequence:
20422 state_machine.handle_end_sequence ();
20423 end_sequence = true;
20424 break;
20425 case DW_LNE_set_address:
20426 {
20427 CORE_ADDR address
20428 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20429 line_ptr += bytes_read;
20430
20431 state_machine.check_line_address (cu, line_ptr,
20432 lowpc - baseaddr, address);
20433 state_machine.handle_set_address (baseaddr, address);
20434 }
20435 break;
20436 case DW_LNE_define_file:
20437 {
20438 const char *cur_file;
20439 unsigned int mod_time, length;
20440 dir_index dindex;
20441
20442 cur_file = read_direct_string (abfd, line_ptr,
20443 &bytes_read);
20444 line_ptr += bytes_read;
20445 dindex = (dir_index)
20446 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20447 line_ptr += bytes_read;
20448 mod_time =
20449 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20450 line_ptr += bytes_read;
20451 length =
20452 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20453 line_ptr += bytes_read;
20454 lh->add_file_name (cur_file, dindex, mod_time, length);
20455 }
20456 break;
20457 case DW_LNE_set_discriminator:
20458 {
20459 /* The discriminator is not interesting to the
20460 debugger; just ignore it. We still need to
20461 check its value though:
20462 if there are consecutive entries for the same
20463 (non-prologue) line we want to coalesce them.
20464 PR 17276. */
20465 unsigned int discr
20466 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20467 line_ptr += bytes_read;
20468
20469 state_machine.handle_set_discriminator (discr);
20470 }
20471 break;
20472 default:
20473 complaint (_("mangled .debug_line section"));
20474 return;
20475 }
20476 /* Make sure that we parsed the extended op correctly. If e.g.
20477 we expected a different address size than the producer used,
20478 we may have read the wrong number of bytes. */
20479 if (line_ptr != extended_end)
20480 {
20481 complaint (_("mangled .debug_line section"));
20482 return;
20483 }
20484 break;
20485 case DW_LNS_copy:
20486 state_machine.handle_copy ();
20487 break;
20488 case DW_LNS_advance_pc:
20489 {
20490 CORE_ADDR adjust
20491 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20492 line_ptr += bytes_read;
20493
20494 state_machine.handle_advance_pc (adjust);
20495 }
20496 break;
20497 case DW_LNS_advance_line:
20498 {
20499 int line_delta
20500 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20501 line_ptr += bytes_read;
20502
20503 state_machine.handle_advance_line (line_delta);
20504 }
20505 break;
20506 case DW_LNS_set_file:
20507 {
20508 file_name_index file
20509 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20510 &bytes_read);
20511 line_ptr += bytes_read;
20512
20513 state_machine.handle_set_file (file);
20514 }
20515 break;
20516 case DW_LNS_set_column:
20517 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20518 line_ptr += bytes_read;
20519 break;
20520 case DW_LNS_negate_stmt:
20521 state_machine.handle_negate_stmt ();
20522 break;
20523 case DW_LNS_set_basic_block:
20524 break;
20525 /* Add to the address register of the state machine the
20526 address increment value corresponding to special opcode
20527 255. I.e., this value is scaled by the minimum
20528 instruction length since special opcode 255 would have
20529 scaled the increment. */
20530 case DW_LNS_const_add_pc:
20531 state_machine.handle_const_add_pc ();
20532 break;
20533 case DW_LNS_fixed_advance_pc:
20534 {
20535 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20536 line_ptr += 2;
20537
20538 state_machine.handle_fixed_advance_pc (addr_adj);
20539 }
20540 break;
20541 default:
20542 {
20543 /* Unknown standard opcode, ignore it. */
20544 int i;
20545
20546 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20547 {
20548 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20549 line_ptr += bytes_read;
20550 }
20551 }
20552 }
20553 }
20554
20555 if (!end_sequence)
20556 dwarf2_debug_line_missing_end_sequence_complaint ();
20557
20558 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20559 in which case we still finish recording the last line). */
20560 state_machine.record_line (true);
20561 }
20562 }
20563
20564 /* Decode the Line Number Program (LNP) for the given line_header
20565 structure and CU. The actual information extracted and the type
20566 of structures created from the LNP depends on the value of PST.
20567
20568 1. If PST is NULL, then this procedure uses the data from the program
20569 to create all necessary symbol tables, and their linetables.
20570
20571 2. If PST is not NULL, this procedure reads the program to determine
20572 the list of files included by the unit represented by PST, and
20573 builds all the associated partial symbol tables.
20574
20575 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20576 It is used for relative paths in the line table.
20577 NOTE: When processing partial symtabs (pst != NULL),
20578 comp_dir == pst->dirname.
20579
20580 NOTE: It is important that psymtabs have the same file name (via strcmp)
20581 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20582 symtab we don't use it in the name of the psymtabs we create.
20583 E.g. expand_line_sal requires this when finding psymtabs to expand.
20584 A good testcase for this is mb-inline.exp.
20585
20586 LOWPC is the lowest address in CU (or 0 if not known).
20587
20588 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20589 for its PC<->lines mapping information. Otherwise only the filename
20590 table is read in. */
20591
20592 static void
20593 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20594 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20595 CORE_ADDR lowpc, int decode_mapping)
20596 {
20597 struct objfile *objfile = cu->per_objfile->objfile;
20598 const int decode_for_pst_p = (pst != NULL);
20599
20600 if (decode_mapping)
20601 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20602
20603 if (decode_for_pst_p)
20604 {
20605 /* Now that we're done scanning the Line Header Program, we can
20606 create the psymtab of each included file. */
20607 for (auto &file_entry : lh->file_names ())
20608 if (file_entry.included_p == 1)
20609 {
20610 gdb::unique_xmalloc_ptr<char> name_holder;
20611 const char *include_name =
20612 psymtab_include_file_name (lh, file_entry, pst,
20613 comp_dir, &name_holder);
20614 if (include_name != NULL)
20615 dwarf2_create_include_psymtab (include_name, pst, objfile);
20616 }
20617 }
20618 else
20619 {
20620 /* Make sure a symtab is created for every file, even files
20621 which contain only variables (i.e. no code with associated
20622 line numbers). */
20623 buildsym_compunit *builder = cu->get_builder ();
20624 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20625
20626 for (auto &fe : lh->file_names ())
20627 {
20628 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20629 if (builder->get_current_subfile ()->symtab == NULL)
20630 {
20631 builder->get_current_subfile ()->symtab
20632 = allocate_symtab (cust,
20633 builder->get_current_subfile ()->name);
20634 }
20635 fe.symtab = builder->get_current_subfile ()->symtab;
20636 }
20637 }
20638 }
20639
20640 /* Start a subfile for DWARF. FILENAME is the name of the file and
20641 DIRNAME the name of the source directory which contains FILENAME
20642 or NULL if not known.
20643 This routine tries to keep line numbers from identical absolute and
20644 relative file names in a common subfile.
20645
20646 Using the `list' example from the GDB testsuite, which resides in
20647 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20648 of /srcdir/list0.c yields the following debugging information for list0.c:
20649
20650 DW_AT_name: /srcdir/list0.c
20651 DW_AT_comp_dir: /compdir
20652 files.files[0].name: list0.h
20653 files.files[0].dir: /srcdir
20654 files.files[1].name: list0.c
20655 files.files[1].dir: /srcdir
20656
20657 The line number information for list0.c has to end up in a single
20658 subfile, so that `break /srcdir/list0.c:1' works as expected.
20659 start_subfile will ensure that this happens provided that we pass the
20660 concatenation of files.files[1].dir and files.files[1].name as the
20661 subfile's name. */
20662
20663 static void
20664 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20665 const char *dirname)
20666 {
20667 gdb::unique_xmalloc_ptr<char> copy;
20668
20669 /* In order not to lose the line information directory,
20670 we concatenate it to the filename when it makes sense.
20671 Note that the Dwarf3 standard says (speaking of filenames in line
20672 information): ``The directory index is ignored for file names
20673 that represent full path names''. Thus ignoring dirname in the
20674 `else' branch below isn't an issue. */
20675
20676 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20677 {
20678 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20679 filename = copy.get ();
20680 }
20681
20682 cu->get_builder ()->start_subfile (filename);
20683 }
20684
20685 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20686 buildsym_compunit constructor. */
20687
20688 struct compunit_symtab *
20689 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20690 CORE_ADDR low_pc)
20691 {
20692 gdb_assert (m_builder == nullptr);
20693
20694 m_builder.reset (new struct buildsym_compunit
20695 (this->per_objfile->objfile,
20696 name, comp_dir, language, low_pc));
20697
20698 list_in_scope = get_builder ()->get_file_symbols ();
20699
20700 get_builder ()->record_debugformat ("DWARF 2");
20701 get_builder ()->record_producer (producer);
20702
20703 processing_has_namespace_info = false;
20704
20705 return get_builder ()->get_compunit_symtab ();
20706 }
20707
20708 static void
20709 var_decode_location (struct attribute *attr, struct symbol *sym,
20710 struct dwarf2_cu *cu)
20711 {
20712 struct objfile *objfile = cu->per_objfile->objfile;
20713 struct comp_unit_head *cu_header = &cu->header;
20714
20715 /* NOTE drow/2003-01-30: There used to be a comment and some special
20716 code here to turn a symbol with DW_AT_external and a
20717 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20718 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20719 with some versions of binutils) where shared libraries could have
20720 relocations against symbols in their debug information - the
20721 minimal symbol would have the right address, but the debug info
20722 would not. It's no longer necessary, because we will explicitly
20723 apply relocations when we read in the debug information now. */
20724
20725 /* A DW_AT_location attribute with no contents indicates that a
20726 variable has been optimized away. */
20727 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20728 {
20729 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20730 return;
20731 }
20732
20733 /* Handle one degenerate form of location expression specially, to
20734 preserve GDB's previous behavior when section offsets are
20735 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20736 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20737
20738 if (attr->form_is_block ()
20739 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20740 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20741 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20742 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20743 && (DW_BLOCK (attr)->size
20744 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20745 {
20746 unsigned int dummy;
20747
20748 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20749 SET_SYMBOL_VALUE_ADDRESS
20750 (sym, cu->header.read_address (objfile->obfd,
20751 DW_BLOCK (attr)->data + 1,
20752 &dummy));
20753 else
20754 SET_SYMBOL_VALUE_ADDRESS
20755 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20756 &dummy));
20757 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20758 fixup_symbol_section (sym, objfile);
20759 SET_SYMBOL_VALUE_ADDRESS
20760 (sym,
20761 SYMBOL_VALUE_ADDRESS (sym)
20762 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20763 return;
20764 }
20765
20766 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20767 expression evaluator, and use LOC_COMPUTED only when necessary
20768 (i.e. when the value of a register or memory location is
20769 referenced, or a thread-local block, etc.). Then again, it might
20770 not be worthwhile. I'm assuming that it isn't unless performance
20771 or memory numbers show me otherwise. */
20772
20773 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20774
20775 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20776 cu->has_loclist = true;
20777 }
20778
20779 /* Given a pointer to a DWARF information entry, figure out if we need
20780 to make a symbol table entry for it, and if so, create a new entry
20781 and return a pointer to it.
20782 If TYPE is NULL, determine symbol type from the die, otherwise
20783 used the passed type.
20784 If SPACE is not NULL, use it to hold the new symbol. If it is
20785 NULL, allocate a new symbol on the objfile's obstack. */
20786
20787 static struct symbol *
20788 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20789 struct symbol *space)
20790 {
20791 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20792 struct objfile *objfile = per_objfile->objfile;
20793 struct gdbarch *gdbarch = objfile->arch ();
20794 struct symbol *sym = NULL;
20795 const char *name;
20796 struct attribute *attr = NULL;
20797 struct attribute *attr2 = NULL;
20798 CORE_ADDR baseaddr;
20799 struct pending **list_to_add = NULL;
20800
20801 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20802
20803 baseaddr = objfile->text_section_offset ();
20804
20805 name = dwarf2_name (die, cu);
20806 if (name)
20807 {
20808 int suppress_add = 0;
20809
20810 if (space)
20811 sym = space;
20812 else
20813 sym = new (&objfile->objfile_obstack) symbol;
20814 OBJSTAT (objfile, n_syms++);
20815
20816 /* Cache this symbol's name and the name's demangled form (if any). */
20817 sym->set_language (cu->language, &objfile->objfile_obstack);
20818 /* Fortran does not have mangling standard and the mangling does differ
20819 between gfortran, iFort etc. */
20820 const char *physname
20821 = (cu->language == language_fortran
20822 ? dwarf2_full_name (name, die, cu)
20823 : dwarf2_physname (name, die, cu));
20824 const char *linkagename = dw2_linkage_name (die, cu);
20825
20826 if (linkagename == nullptr || cu->language == language_ada)
20827 sym->set_linkage_name (physname);
20828 else
20829 {
20830 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20831 sym->set_linkage_name (linkagename);
20832 }
20833
20834 /* Default assumptions.
20835 Use the passed type or decode it from the die. */
20836 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20837 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20838 if (type != NULL)
20839 SYMBOL_TYPE (sym) = type;
20840 else
20841 SYMBOL_TYPE (sym) = die_type (die, cu);
20842 attr = dwarf2_attr (die,
20843 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20844 cu);
20845 if (attr != nullptr)
20846 {
20847 SYMBOL_LINE (sym) = DW_UNSND (attr);
20848 }
20849
20850 attr = dwarf2_attr (die,
20851 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20852 cu);
20853 if (attr != nullptr)
20854 {
20855 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20856 struct file_entry *fe;
20857
20858 if (cu->line_header != NULL)
20859 fe = cu->line_header->file_name_at (file_index);
20860 else
20861 fe = NULL;
20862
20863 if (fe == NULL)
20864 complaint (_("file index out of range"));
20865 else
20866 symbol_set_symtab (sym, fe->symtab);
20867 }
20868
20869 switch (die->tag)
20870 {
20871 case DW_TAG_label:
20872 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20873 if (attr != nullptr)
20874 {
20875 CORE_ADDR addr;
20876
20877 addr = attr->value_as_address ();
20878 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20879 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20880 }
20881 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20882 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20883 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20884 add_symbol_to_list (sym, cu->list_in_scope);
20885 break;
20886 case DW_TAG_subprogram:
20887 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20888 finish_block. */
20889 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20890 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20891 if ((attr2 && (DW_UNSND (attr2) != 0))
20892 || cu->language == language_ada
20893 || cu->language == language_fortran)
20894 {
20895 /* Subprograms marked external are stored as a global symbol.
20896 Ada and Fortran subprograms, whether marked external or
20897 not, are always stored as a global symbol, because we want
20898 to be able to access them globally. For instance, we want
20899 to be able to break on a nested subprogram without having
20900 to specify the context. */
20901 list_to_add = cu->get_builder ()->get_global_symbols ();
20902 }
20903 else
20904 {
20905 list_to_add = cu->list_in_scope;
20906 }
20907 break;
20908 case DW_TAG_inlined_subroutine:
20909 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20910 finish_block. */
20911 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20912 SYMBOL_INLINED (sym) = 1;
20913 list_to_add = cu->list_in_scope;
20914 break;
20915 case DW_TAG_template_value_param:
20916 suppress_add = 1;
20917 /* Fall through. */
20918 case DW_TAG_constant:
20919 case DW_TAG_variable:
20920 case DW_TAG_member:
20921 /* Compilation with minimal debug info may result in
20922 variables with missing type entries. Change the
20923 misleading `void' type to something sensible. */
20924 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20925 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20926
20927 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20928 /* In the case of DW_TAG_member, we should only be called for
20929 static const members. */
20930 if (die->tag == DW_TAG_member)
20931 {
20932 /* dwarf2_add_field uses die_is_declaration,
20933 so we do the same. */
20934 gdb_assert (die_is_declaration (die, cu));
20935 gdb_assert (attr);
20936 }
20937 if (attr != nullptr)
20938 {
20939 dwarf2_const_value (attr, sym, cu);
20940 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20941 if (!suppress_add)
20942 {
20943 if (attr2 && (DW_UNSND (attr2) != 0))
20944 list_to_add = cu->get_builder ()->get_global_symbols ();
20945 else
20946 list_to_add = cu->list_in_scope;
20947 }
20948 break;
20949 }
20950 attr = dwarf2_attr (die, DW_AT_location, cu);
20951 if (attr != nullptr)
20952 {
20953 var_decode_location (attr, sym, cu);
20954 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20955
20956 /* Fortran explicitly imports any global symbols to the local
20957 scope by DW_TAG_common_block. */
20958 if (cu->language == language_fortran && die->parent
20959 && die->parent->tag == DW_TAG_common_block)
20960 attr2 = NULL;
20961
20962 if (SYMBOL_CLASS (sym) == LOC_STATIC
20963 && SYMBOL_VALUE_ADDRESS (sym) == 0
20964 && !per_objfile->per_bfd->has_section_at_zero)
20965 {
20966 /* When a static variable is eliminated by the linker,
20967 the corresponding debug information is not stripped
20968 out, but the variable address is set to null;
20969 do not add such variables into symbol table. */
20970 }
20971 else if (attr2 && (DW_UNSND (attr2) != 0))
20972 {
20973 if (SYMBOL_CLASS (sym) == LOC_STATIC
20974 && (objfile->flags & OBJF_MAINLINE) == 0
20975 && per_objfile->per_bfd->can_copy)
20976 {
20977 /* A global static variable might be subject to
20978 copy relocation. We first check for a local
20979 minsym, though, because maybe the symbol was
20980 marked hidden, in which case this would not
20981 apply. */
20982 bound_minimal_symbol found
20983 = (lookup_minimal_symbol_linkage
20984 (sym->linkage_name (), objfile));
20985 if (found.minsym != nullptr)
20986 sym->maybe_copied = 1;
20987 }
20988
20989 /* A variable with DW_AT_external is never static,
20990 but it may be block-scoped. */
20991 list_to_add
20992 = ((cu->list_in_scope
20993 == cu->get_builder ()->get_file_symbols ())
20994 ? cu->get_builder ()->get_global_symbols ()
20995 : cu->list_in_scope);
20996 }
20997 else
20998 list_to_add = cu->list_in_scope;
20999 }
21000 else
21001 {
21002 /* We do not know the address of this symbol.
21003 If it is an external symbol and we have type information
21004 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21005 The address of the variable will then be determined from
21006 the minimal symbol table whenever the variable is
21007 referenced. */
21008 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21009
21010 /* Fortran explicitly imports any global symbols to the local
21011 scope by DW_TAG_common_block. */
21012 if (cu->language == language_fortran && die->parent
21013 && die->parent->tag == DW_TAG_common_block)
21014 {
21015 /* SYMBOL_CLASS doesn't matter here because
21016 read_common_block is going to reset it. */
21017 if (!suppress_add)
21018 list_to_add = cu->list_in_scope;
21019 }
21020 else if (attr2 && (DW_UNSND (attr2) != 0)
21021 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21022 {
21023 /* A variable with DW_AT_external is never static, but it
21024 may be block-scoped. */
21025 list_to_add
21026 = ((cu->list_in_scope
21027 == cu->get_builder ()->get_file_symbols ())
21028 ? cu->get_builder ()->get_global_symbols ()
21029 : cu->list_in_scope);
21030
21031 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21032 }
21033 else if (!die_is_declaration (die, cu))
21034 {
21035 /* Use the default LOC_OPTIMIZED_OUT class. */
21036 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21037 if (!suppress_add)
21038 list_to_add = cu->list_in_scope;
21039 }
21040 }
21041 break;
21042 case DW_TAG_formal_parameter:
21043 {
21044 /* If we are inside a function, mark this as an argument. If
21045 not, we might be looking at an argument to an inlined function
21046 when we do not have enough information to show inlined frames;
21047 pretend it's a local variable in that case so that the user can
21048 still see it. */
21049 struct context_stack *curr
21050 = cu->get_builder ()->get_current_context_stack ();
21051 if (curr != nullptr && curr->name != nullptr)
21052 SYMBOL_IS_ARGUMENT (sym) = 1;
21053 attr = dwarf2_attr (die, DW_AT_location, cu);
21054 if (attr != nullptr)
21055 {
21056 var_decode_location (attr, sym, cu);
21057 }
21058 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21059 if (attr != nullptr)
21060 {
21061 dwarf2_const_value (attr, sym, cu);
21062 }
21063
21064 list_to_add = cu->list_in_scope;
21065 }
21066 break;
21067 case DW_TAG_unspecified_parameters:
21068 /* From varargs functions; gdb doesn't seem to have any
21069 interest in this information, so just ignore it for now.
21070 (FIXME?) */
21071 break;
21072 case DW_TAG_template_type_param:
21073 suppress_add = 1;
21074 /* Fall through. */
21075 case DW_TAG_class_type:
21076 case DW_TAG_interface_type:
21077 case DW_TAG_structure_type:
21078 case DW_TAG_union_type:
21079 case DW_TAG_set_type:
21080 case DW_TAG_enumeration_type:
21081 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21082 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21083
21084 {
21085 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21086 really ever be static objects: otherwise, if you try
21087 to, say, break of a class's method and you're in a file
21088 which doesn't mention that class, it won't work unless
21089 the check for all static symbols in lookup_symbol_aux
21090 saves you. See the OtherFileClass tests in
21091 gdb.c++/namespace.exp. */
21092
21093 if (!suppress_add)
21094 {
21095 buildsym_compunit *builder = cu->get_builder ();
21096 list_to_add
21097 = (cu->list_in_scope == builder->get_file_symbols ()
21098 && cu->language == language_cplus
21099 ? builder->get_global_symbols ()
21100 : cu->list_in_scope);
21101
21102 /* The semantics of C++ state that "struct foo {
21103 ... }" also defines a typedef for "foo". */
21104 if (cu->language == language_cplus
21105 || cu->language == language_ada
21106 || cu->language == language_d
21107 || cu->language == language_rust)
21108 {
21109 /* The symbol's name is already allocated along
21110 with this objfile, so we don't need to
21111 duplicate it for the type. */
21112 if (SYMBOL_TYPE (sym)->name () == 0)
21113 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21114 }
21115 }
21116 }
21117 break;
21118 case DW_TAG_typedef:
21119 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21120 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21121 list_to_add = cu->list_in_scope;
21122 break;
21123 case DW_TAG_base_type:
21124 case DW_TAG_subrange_type:
21125 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21126 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21127 list_to_add = cu->list_in_scope;
21128 break;
21129 case DW_TAG_enumerator:
21130 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21131 if (attr != nullptr)
21132 {
21133 dwarf2_const_value (attr, sym, cu);
21134 }
21135 {
21136 /* NOTE: carlton/2003-11-10: See comment above in the
21137 DW_TAG_class_type, etc. block. */
21138
21139 list_to_add
21140 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21141 && cu->language == language_cplus
21142 ? cu->get_builder ()->get_global_symbols ()
21143 : cu->list_in_scope);
21144 }
21145 break;
21146 case DW_TAG_imported_declaration:
21147 case DW_TAG_namespace:
21148 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21149 list_to_add = cu->get_builder ()->get_global_symbols ();
21150 break;
21151 case DW_TAG_module:
21152 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21153 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21154 list_to_add = cu->get_builder ()->get_global_symbols ();
21155 break;
21156 case DW_TAG_common_block:
21157 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21158 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21159 add_symbol_to_list (sym, cu->list_in_scope);
21160 break;
21161 default:
21162 /* Not a tag we recognize. Hopefully we aren't processing
21163 trash data, but since we must specifically ignore things
21164 we don't recognize, there is nothing else we should do at
21165 this point. */
21166 complaint (_("unsupported tag: '%s'"),
21167 dwarf_tag_name (die->tag));
21168 break;
21169 }
21170
21171 if (suppress_add)
21172 {
21173 sym->hash_next = objfile->template_symbols;
21174 objfile->template_symbols = sym;
21175 list_to_add = NULL;
21176 }
21177
21178 if (list_to_add != NULL)
21179 add_symbol_to_list (sym, list_to_add);
21180
21181 /* For the benefit of old versions of GCC, check for anonymous
21182 namespaces based on the demangled name. */
21183 if (!cu->processing_has_namespace_info
21184 && cu->language == language_cplus)
21185 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21186 }
21187 return (sym);
21188 }
21189
21190 /* Given an attr with a DW_FORM_dataN value in host byte order,
21191 zero-extend it as appropriate for the symbol's type. The DWARF
21192 standard (v4) is not entirely clear about the meaning of using
21193 DW_FORM_dataN for a constant with a signed type, where the type is
21194 wider than the data. The conclusion of a discussion on the DWARF
21195 list was that this is unspecified. We choose to always zero-extend
21196 because that is the interpretation long in use by GCC. */
21197
21198 static gdb_byte *
21199 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21200 struct dwarf2_cu *cu, LONGEST *value, int bits)
21201 {
21202 struct objfile *objfile = cu->per_objfile->objfile;
21203 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21204 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21205 LONGEST l = DW_UNSND (attr);
21206
21207 if (bits < sizeof (*value) * 8)
21208 {
21209 l &= ((LONGEST) 1 << bits) - 1;
21210 *value = l;
21211 }
21212 else if (bits == sizeof (*value) * 8)
21213 *value = l;
21214 else
21215 {
21216 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21217 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21218 return bytes;
21219 }
21220
21221 return NULL;
21222 }
21223
21224 /* Read a constant value from an attribute. Either set *VALUE, or if
21225 the value does not fit in *VALUE, set *BYTES - either already
21226 allocated on the objfile obstack, or newly allocated on OBSTACK,
21227 or, set *BATON, if we translated the constant to a location
21228 expression. */
21229
21230 static void
21231 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21232 const char *name, struct obstack *obstack,
21233 struct dwarf2_cu *cu,
21234 LONGEST *value, const gdb_byte **bytes,
21235 struct dwarf2_locexpr_baton **baton)
21236 {
21237 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21238 struct objfile *objfile = per_objfile->objfile;
21239 struct comp_unit_head *cu_header = &cu->header;
21240 struct dwarf_block *blk;
21241 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21242 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21243
21244 *value = 0;
21245 *bytes = NULL;
21246 *baton = NULL;
21247
21248 switch (attr->form)
21249 {
21250 case DW_FORM_addr:
21251 case DW_FORM_addrx:
21252 case DW_FORM_GNU_addr_index:
21253 {
21254 gdb_byte *data;
21255
21256 if (TYPE_LENGTH (type) != cu_header->addr_size)
21257 dwarf2_const_value_length_mismatch_complaint (name,
21258 cu_header->addr_size,
21259 TYPE_LENGTH (type));
21260 /* Symbols of this form are reasonably rare, so we just
21261 piggyback on the existing location code rather than writing
21262 a new implementation of symbol_computed_ops. */
21263 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21264 (*baton)->per_objfile = per_objfile;
21265 (*baton)->per_cu = cu->per_cu;
21266 gdb_assert ((*baton)->per_cu);
21267
21268 (*baton)->size = 2 + cu_header->addr_size;
21269 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21270 (*baton)->data = data;
21271
21272 data[0] = DW_OP_addr;
21273 store_unsigned_integer (&data[1], cu_header->addr_size,
21274 byte_order, DW_ADDR (attr));
21275 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21276 }
21277 break;
21278 case DW_FORM_string:
21279 case DW_FORM_strp:
21280 case DW_FORM_strx:
21281 case DW_FORM_GNU_str_index:
21282 case DW_FORM_GNU_strp_alt:
21283 /* DW_STRING is already allocated on the objfile obstack, point
21284 directly to it. */
21285 *bytes = (const gdb_byte *) DW_STRING (attr);
21286 break;
21287 case DW_FORM_block1:
21288 case DW_FORM_block2:
21289 case DW_FORM_block4:
21290 case DW_FORM_block:
21291 case DW_FORM_exprloc:
21292 case DW_FORM_data16:
21293 blk = DW_BLOCK (attr);
21294 if (TYPE_LENGTH (type) != blk->size)
21295 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21296 TYPE_LENGTH (type));
21297 *bytes = blk->data;
21298 break;
21299
21300 /* The DW_AT_const_value attributes are supposed to carry the
21301 symbol's value "represented as it would be on the target
21302 architecture." By the time we get here, it's already been
21303 converted to host endianness, so we just need to sign- or
21304 zero-extend it as appropriate. */
21305 case DW_FORM_data1:
21306 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21307 break;
21308 case DW_FORM_data2:
21309 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21310 break;
21311 case DW_FORM_data4:
21312 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21313 break;
21314 case DW_FORM_data8:
21315 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21316 break;
21317
21318 case DW_FORM_sdata:
21319 case DW_FORM_implicit_const:
21320 *value = DW_SND (attr);
21321 break;
21322
21323 case DW_FORM_udata:
21324 *value = DW_UNSND (attr);
21325 break;
21326
21327 default:
21328 complaint (_("unsupported const value attribute form: '%s'"),
21329 dwarf_form_name (attr->form));
21330 *value = 0;
21331 break;
21332 }
21333 }
21334
21335
21336 /* Copy constant value from an attribute to a symbol. */
21337
21338 static void
21339 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21340 struct dwarf2_cu *cu)
21341 {
21342 struct objfile *objfile = cu->per_objfile->objfile;
21343 LONGEST value;
21344 const gdb_byte *bytes;
21345 struct dwarf2_locexpr_baton *baton;
21346
21347 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21348 sym->print_name (),
21349 &objfile->objfile_obstack, cu,
21350 &value, &bytes, &baton);
21351
21352 if (baton != NULL)
21353 {
21354 SYMBOL_LOCATION_BATON (sym) = baton;
21355 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21356 }
21357 else if (bytes != NULL)
21358 {
21359 SYMBOL_VALUE_BYTES (sym) = bytes;
21360 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21361 }
21362 else
21363 {
21364 SYMBOL_VALUE (sym) = value;
21365 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21366 }
21367 }
21368
21369 /* Return the type of the die in question using its DW_AT_type attribute. */
21370
21371 static struct type *
21372 die_type (struct die_info *die, struct dwarf2_cu *cu)
21373 {
21374 struct attribute *type_attr;
21375
21376 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21377 if (!type_attr)
21378 {
21379 struct objfile *objfile = cu->per_objfile->objfile;
21380 /* A missing DW_AT_type represents a void type. */
21381 return objfile_type (objfile)->builtin_void;
21382 }
21383
21384 return lookup_die_type (die, type_attr, cu);
21385 }
21386
21387 /* True iff CU's producer generates GNAT Ada auxiliary information
21388 that allows to find parallel types through that information instead
21389 of having to do expensive parallel lookups by type name. */
21390
21391 static int
21392 need_gnat_info (struct dwarf2_cu *cu)
21393 {
21394 /* Assume that the Ada compiler was GNAT, which always produces
21395 the auxiliary information. */
21396 return (cu->language == language_ada);
21397 }
21398
21399 /* Return the auxiliary type of the die in question using its
21400 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21401 attribute is not present. */
21402
21403 static struct type *
21404 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21405 {
21406 struct attribute *type_attr;
21407
21408 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21409 if (!type_attr)
21410 return NULL;
21411
21412 return lookup_die_type (die, type_attr, cu);
21413 }
21414
21415 /* If DIE has a descriptive_type attribute, then set the TYPE's
21416 descriptive type accordingly. */
21417
21418 static void
21419 set_descriptive_type (struct type *type, struct die_info *die,
21420 struct dwarf2_cu *cu)
21421 {
21422 struct type *descriptive_type = die_descriptive_type (die, cu);
21423
21424 if (descriptive_type)
21425 {
21426 ALLOCATE_GNAT_AUX_TYPE (type);
21427 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21428 }
21429 }
21430
21431 /* Return the containing type of the die in question using its
21432 DW_AT_containing_type attribute. */
21433
21434 static struct type *
21435 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21436 {
21437 struct attribute *type_attr;
21438 struct objfile *objfile = cu->per_objfile->objfile;
21439
21440 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21441 if (!type_attr)
21442 error (_("Dwarf Error: Problem turning containing type into gdb type "
21443 "[in module %s]"), objfile_name (objfile));
21444
21445 return lookup_die_type (die, type_attr, cu);
21446 }
21447
21448 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21449
21450 static struct type *
21451 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21452 {
21453 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21454 struct objfile *objfile = per_objfile->objfile;
21455 char *saved;
21456
21457 std::string message
21458 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21459 objfile_name (objfile),
21460 sect_offset_str (cu->header.sect_off),
21461 sect_offset_str (die->sect_off));
21462 saved = obstack_strdup (&objfile->objfile_obstack, message);
21463
21464 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21465 }
21466
21467 /* Look up the type of DIE in CU using its type attribute ATTR.
21468 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21469 DW_AT_containing_type.
21470 If there is no type substitute an error marker. */
21471
21472 static struct type *
21473 lookup_die_type (struct die_info *die, const struct attribute *attr,
21474 struct dwarf2_cu *cu)
21475 {
21476 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21477 struct objfile *objfile = per_objfile->objfile;
21478 struct type *this_type;
21479
21480 gdb_assert (attr->name == DW_AT_type
21481 || attr->name == DW_AT_GNAT_descriptive_type
21482 || attr->name == DW_AT_containing_type);
21483
21484 /* First see if we have it cached. */
21485
21486 if (attr->form == DW_FORM_GNU_ref_alt)
21487 {
21488 struct dwarf2_per_cu_data *per_cu;
21489 sect_offset sect_off = attr->get_ref_die_offset ();
21490
21491 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
21492 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
21493 }
21494 else if (attr->form_is_ref ())
21495 {
21496 sect_offset sect_off = attr->get_ref_die_offset ();
21497
21498 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
21499 }
21500 else if (attr->form == DW_FORM_ref_sig8)
21501 {
21502 ULONGEST signature = DW_SIGNATURE (attr);
21503
21504 return get_signatured_type (die, signature, cu);
21505 }
21506 else
21507 {
21508 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21509 " at %s [in module %s]"),
21510 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21511 objfile_name (objfile));
21512 return build_error_marker_type (cu, die);
21513 }
21514
21515 /* If not cached we need to read it in. */
21516
21517 if (this_type == NULL)
21518 {
21519 struct die_info *type_die = NULL;
21520 struct dwarf2_cu *type_cu = cu;
21521
21522 if (attr->form_is_ref ())
21523 type_die = follow_die_ref (die, attr, &type_cu);
21524 if (type_die == NULL)
21525 return build_error_marker_type (cu, die);
21526 /* If we find the type now, it's probably because the type came
21527 from an inter-CU reference and the type's CU got expanded before
21528 ours. */
21529 this_type = read_type_die (type_die, type_cu);
21530 }
21531
21532 /* If we still don't have a type use an error marker. */
21533
21534 if (this_type == NULL)
21535 return build_error_marker_type (cu, die);
21536
21537 return this_type;
21538 }
21539
21540 /* Return the type in DIE, CU.
21541 Returns NULL for invalid types.
21542
21543 This first does a lookup in die_type_hash,
21544 and only reads the die in if necessary.
21545
21546 NOTE: This can be called when reading in partial or full symbols. */
21547
21548 static struct type *
21549 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21550 {
21551 struct type *this_type;
21552
21553 this_type = get_die_type (die, cu);
21554 if (this_type)
21555 return this_type;
21556
21557 return read_type_die_1 (die, cu);
21558 }
21559
21560 /* Read the type in DIE, CU.
21561 Returns NULL for invalid types. */
21562
21563 static struct type *
21564 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21565 {
21566 struct type *this_type = NULL;
21567
21568 switch (die->tag)
21569 {
21570 case DW_TAG_class_type:
21571 case DW_TAG_interface_type:
21572 case DW_TAG_structure_type:
21573 case DW_TAG_union_type:
21574 this_type = read_structure_type (die, cu);
21575 break;
21576 case DW_TAG_enumeration_type:
21577 this_type = read_enumeration_type (die, cu);
21578 break;
21579 case DW_TAG_subprogram:
21580 case DW_TAG_subroutine_type:
21581 case DW_TAG_inlined_subroutine:
21582 this_type = read_subroutine_type (die, cu);
21583 break;
21584 case DW_TAG_array_type:
21585 this_type = read_array_type (die, cu);
21586 break;
21587 case DW_TAG_set_type:
21588 this_type = read_set_type (die, cu);
21589 break;
21590 case DW_TAG_pointer_type:
21591 this_type = read_tag_pointer_type (die, cu);
21592 break;
21593 case DW_TAG_ptr_to_member_type:
21594 this_type = read_tag_ptr_to_member_type (die, cu);
21595 break;
21596 case DW_TAG_reference_type:
21597 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21598 break;
21599 case DW_TAG_rvalue_reference_type:
21600 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21601 break;
21602 case DW_TAG_const_type:
21603 this_type = read_tag_const_type (die, cu);
21604 break;
21605 case DW_TAG_volatile_type:
21606 this_type = read_tag_volatile_type (die, cu);
21607 break;
21608 case DW_TAG_restrict_type:
21609 this_type = read_tag_restrict_type (die, cu);
21610 break;
21611 case DW_TAG_string_type:
21612 this_type = read_tag_string_type (die, cu);
21613 break;
21614 case DW_TAG_typedef:
21615 this_type = read_typedef (die, cu);
21616 break;
21617 case DW_TAG_subrange_type:
21618 this_type = read_subrange_type (die, cu);
21619 break;
21620 case DW_TAG_base_type:
21621 this_type = read_base_type (die, cu);
21622 break;
21623 case DW_TAG_unspecified_type:
21624 this_type = read_unspecified_type (die, cu);
21625 break;
21626 case DW_TAG_namespace:
21627 this_type = read_namespace_type (die, cu);
21628 break;
21629 case DW_TAG_module:
21630 this_type = read_module_type (die, cu);
21631 break;
21632 case DW_TAG_atomic_type:
21633 this_type = read_tag_atomic_type (die, cu);
21634 break;
21635 default:
21636 complaint (_("unexpected tag in read_type_die: '%s'"),
21637 dwarf_tag_name (die->tag));
21638 break;
21639 }
21640
21641 return this_type;
21642 }
21643
21644 /* See if we can figure out if the class lives in a namespace. We do
21645 this by looking for a member function; its demangled name will
21646 contain namespace info, if there is any.
21647 Return the computed name or NULL.
21648 Space for the result is allocated on the objfile's obstack.
21649 This is the full-die version of guess_partial_die_structure_name.
21650 In this case we know DIE has no useful parent. */
21651
21652 static const char *
21653 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21654 {
21655 struct die_info *spec_die;
21656 struct dwarf2_cu *spec_cu;
21657 struct die_info *child;
21658 struct objfile *objfile = cu->per_objfile->objfile;
21659
21660 spec_cu = cu;
21661 spec_die = die_specification (die, &spec_cu);
21662 if (spec_die != NULL)
21663 {
21664 die = spec_die;
21665 cu = spec_cu;
21666 }
21667
21668 for (child = die->child;
21669 child != NULL;
21670 child = child->sibling)
21671 {
21672 if (child->tag == DW_TAG_subprogram)
21673 {
21674 const char *linkage_name = dw2_linkage_name (child, cu);
21675
21676 if (linkage_name != NULL)
21677 {
21678 gdb::unique_xmalloc_ptr<char> actual_name
21679 (language_class_name_from_physname (cu->language_defn,
21680 linkage_name));
21681 const char *name = NULL;
21682
21683 if (actual_name != NULL)
21684 {
21685 const char *die_name = dwarf2_name (die, cu);
21686
21687 if (die_name != NULL
21688 && strcmp (die_name, actual_name.get ()) != 0)
21689 {
21690 /* Strip off the class name from the full name.
21691 We want the prefix. */
21692 int die_name_len = strlen (die_name);
21693 int actual_name_len = strlen (actual_name.get ());
21694 const char *ptr = actual_name.get ();
21695
21696 /* Test for '::' as a sanity check. */
21697 if (actual_name_len > die_name_len + 2
21698 && ptr[actual_name_len - die_name_len - 1] == ':')
21699 name = obstack_strndup (
21700 &objfile->per_bfd->storage_obstack,
21701 ptr, actual_name_len - die_name_len - 2);
21702 }
21703 }
21704 return name;
21705 }
21706 }
21707 }
21708
21709 return NULL;
21710 }
21711
21712 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21713 prefix part in such case. See
21714 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21715
21716 static const char *
21717 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21718 {
21719 struct attribute *attr;
21720 const char *base;
21721
21722 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21723 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21724 return NULL;
21725
21726 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21727 return NULL;
21728
21729 attr = dw2_linkage_name_attr (die, cu);
21730 if (attr == NULL || DW_STRING (attr) == NULL)
21731 return NULL;
21732
21733 /* dwarf2_name had to be already called. */
21734 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21735
21736 /* Strip the base name, keep any leading namespaces/classes. */
21737 base = strrchr (DW_STRING (attr), ':');
21738 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21739 return "";
21740
21741 struct objfile *objfile = cu->per_objfile->objfile;
21742 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21743 DW_STRING (attr),
21744 &base[-1] - DW_STRING (attr));
21745 }
21746
21747 /* Return the name of the namespace/class that DIE is defined within,
21748 or "" if we can't tell. The caller should not xfree the result.
21749
21750 For example, if we're within the method foo() in the following
21751 code:
21752
21753 namespace N {
21754 class C {
21755 void foo () {
21756 }
21757 };
21758 }
21759
21760 then determine_prefix on foo's die will return "N::C". */
21761
21762 static const char *
21763 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21764 {
21765 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21766 struct die_info *parent, *spec_die;
21767 struct dwarf2_cu *spec_cu;
21768 struct type *parent_type;
21769 const char *retval;
21770
21771 if (cu->language != language_cplus
21772 && cu->language != language_fortran && cu->language != language_d
21773 && cu->language != language_rust)
21774 return "";
21775
21776 retval = anonymous_struct_prefix (die, cu);
21777 if (retval)
21778 return retval;
21779
21780 /* We have to be careful in the presence of DW_AT_specification.
21781 For example, with GCC 3.4, given the code
21782
21783 namespace N {
21784 void foo() {
21785 // Definition of N::foo.
21786 }
21787 }
21788
21789 then we'll have a tree of DIEs like this:
21790
21791 1: DW_TAG_compile_unit
21792 2: DW_TAG_namespace // N
21793 3: DW_TAG_subprogram // declaration of N::foo
21794 4: DW_TAG_subprogram // definition of N::foo
21795 DW_AT_specification // refers to die #3
21796
21797 Thus, when processing die #4, we have to pretend that we're in
21798 the context of its DW_AT_specification, namely the contex of die
21799 #3. */
21800 spec_cu = cu;
21801 spec_die = die_specification (die, &spec_cu);
21802 if (spec_die == NULL)
21803 parent = die->parent;
21804 else
21805 {
21806 parent = spec_die->parent;
21807 cu = spec_cu;
21808 }
21809
21810 if (parent == NULL)
21811 return "";
21812 else if (parent->building_fullname)
21813 {
21814 const char *name;
21815 const char *parent_name;
21816
21817 /* It has been seen on RealView 2.2 built binaries,
21818 DW_TAG_template_type_param types actually _defined_ as
21819 children of the parent class:
21820
21821 enum E {};
21822 template class <class Enum> Class{};
21823 Class<enum E> class_e;
21824
21825 1: DW_TAG_class_type (Class)
21826 2: DW_TAG_enumeration_type (E)
21827 3: DW_TAG_enumerator (enum1:0)
21828 3: DW_TAG_enumerator (enum2:1)
21829 ...
21830 2: DW_TAG_template_type_param
21831 DW_AT_type DW_FORM_ref_udata (E)
21832
21833 Besides being broken debug info, it can put GDB into an
21834 infinite loop. Consider:
21835
21836 When we're building the full name for Class<E>, we'll start
21837 at Class, and go look over its template type parameters,
21838 finding E. We'll then try to build the full name of E, and
21839 reach here. We're now trying to build the full name of E,
21840 and look over the parent DIE for containing scope. In the
21841 broken case, if we followed the parent DIE of E, we'd again
21842 find Class, and once again go look at its template type
21843 arguments, etc., etc. Simply don't consider such parent die
21844 as source-level parent of this die (it can't be, the language
21845 doesn't allow it), and break the loop here. */
21846 name = dwarf2_name (die, cu);
21847 parent_name = dwarf2_name (parent, cu);
21848 complaint (_("template param type '%s' defined within parent '%s'"),
21849 name ? name : "<unknown>",
21850 parent_name ? parent_name : "<unknown>");
21851 return "";
21852 }
21853 else
21854 switch (parent->tag)
21855 {
21856 case DW_TAG_namespace:
21857 parent_type = read_type_die (parent, cu);
21858 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21859 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21860 Work around this problem here. */
21861 if (cu->language == language_cplus
21862 && strcmp (parent_type->name (), "::") == 0)
21863 return "";
21864 /* We give a name to even anonymous namespaces. */
21865 return parent_type->name ();
21866 case DW_TAG_class_type:
21867 case DW_TAG_interface_type:
21868 case DW_TAG_structure_type:
21869 case DW_TAG_union_type:
21870 case DW_TAG_module:
21871 parent_type = read_type_die (parent, cu);
21872 if (parent_type->name () != NULL)
21873 return parent_type->name ();
21874 else
21875 /* An anonymous structure is only allowed non-static data
21876 members; no typedefs, no member functions, et cetera.
21877 So it does not need a prefix. */
21878 return "";
21879 case DW_TAG_compile_unit:
21880 case DW_TAG_partial_unit:
21881 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21882 if (cu->language == language_cplus
21883 && !per_objfile->per_bfd->types.empty ()
21884 && die->child != NULL
21885 && (die->tag == DW_TAG_class_type
21886 || die->tag == DW_TAG_structure_type
21887 || die->tag == DW_TAG_union_type))
21888 {
21889 const char *name = guess_full_die_structure_name (die, cu);
21890 if (name != NULL)
21891 return name;
21892 }
21893 return "";
21894 case DW_TAG_subprogram:
21895 /* Nested subroutines in Fortran get a prefix with the name
21896 of the parent's subroutine. */
21897 if (cu->language == language_fortran)
21898 {
21899 if ((die->tag == DW_TAG_subprogram)
21900 && (dwarf2_name (parent, cu) != NULL))
21901 return dwarf2_name (parent, cu);
21902 }
21903 return determine_prefix (parent, cu);
21904 case DW_TAG_enumeration_type:
21905 parent_type = read_type_die (parent, cu);
21906 if (TYPE_DECLARED_CLASS (parent_type))
21907 {
21908 if (parent_type->name () != NULL)
21909 return parent_type->name ();
21910 return "";
21911 }
21912 /* Fall through. */
21913 default:
21914 return determine_prefix (parent, cu);
21915 }
21916 }
21917
21918 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21919 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21920 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21921 an obconcat, otherwise allocate storage for the result. The CU argument is
21922 used to determine the language and hence, the appropriate separator. */
21923
21924 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21925
21926 static char *
21927 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21928 int physname, struct dwarf2_cu *cu)
21929 {
21930 const char *lead = "";
21931 const char *sep;
21932
21933 if (suffix == NULL || suffix[0] == '\0'
21934 || prefix == NULL || prefix[0] == '\0')
21935 sep = "";
21936 else if (cu->language == language_d)
21937 {
21938 /* For D, the 'main' function could be defined in any module, but it
21939 should never be prefixed. */
21940 if (strcmp (suffix, "D main") == 0)
21941 {
21942 prefix = "";
21943 sep = "";
21944 }
21945 else
21946 sep = ".";
21947 }
21948 else if (cu->language == language_fortran && physname)
21949 {
21950 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21951 DW_AT_MIPS_linkage_name is preferred and used instead. */
21952
21953 lead = "__";
21954 sep = "_MOD_";
21955 }
21956 else
21957 sep = "::";
21958
21959 if (prefix == NULL)
21960 prefix = "";
21961 if (suffix == NULL)
21962 suffix = "";
21963
21964 if (obs == NULL)
21965 {
21966 char *retval
21967 = ((char *)
21968 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21969
21970 strcpy (retval, lead);
21971 strcat (retval, prefix);
21972 strcat (retval, sep);
21973 strcat (retval, suffix);
21974 return retval;
21975 }
21976 else
21977 {
21978 /* We have an obstack. */
21979 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21980 }
21981 }
21982
21983 /* Get name of a die, return NULL if not found. */
21984
21985 static const char *
21986 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21987 struct objfile *objfile)
21988 {
21989 if (name && cu->language == language_cplus)
21990 {
21991 gdb::unique_xmalloc_ptr<char> canon_name
21992 = cp_canonicalize_string (name);
21993
21994 if (canon_name != nullptr)
21995 name = objfile->intern (canon_name.get ());
21996 }
21997
21998 return name;
21999 }
22000
22001 /* Get name of a die, return NULL if not found.
22002 Anonymous namespaces are converted to their magic string. */
22003
22004 static const char *
22005 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22006 {
22007 struct attribute *attr;
22008 struct objfile *objfile = cu->per_objfile->objfile;
22009
22010 attr = dwarf2_attr (die, DW_AT_name, cu);
22011 if ((!attr || !DW_STRING (attr))
22012 && die->tag != DW_TAG_namespace
22013 && die->tag != DW_TAG_class_type
22014 && die->tag != DW_TAG_interface_type
22015 && die->tag != DW_TAG_structure_type
22016 && die->tag != DW_TAG_union_type)
22017 return NULL;
22018
22019 switch (die->tag)
22020 {
22021 case DW_TAG_compile_unit:
22022 case DW_TAG_partial_unit:
22023 /* Compilation units have a DW_AT_name that is a filename, not
22024 a source language identifier. */
22025 case DW_TAG_enumeration_type:
22026 case DW_TAG_enumerator:
22027 /* These tags always have simple identifiers already; no need
22028 to canonicalize them. */
22029 return DW_STRING (attr);
22030
22031 case DW_TAG_namespace:
22032 if (attr != NULL && DW_STRING (attr) != NULL)
22033 return DW_STRING (attr);
22034 return CP_ANONYMOUS_NAMESPACE_STR;
22035
22036 case DW_TAG_class_type:
22037 case DW_TAG_interface_type:
22038 case DW_TAG_structure_type:
22039 case DW_TAG_union_type:
22040 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22041 structures or unions. These were of the form "._%d" in GCC 4.1,
22042 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22043 and GCC 4.4. We work around this problem by ignoring these. */
22044 if (attr && DW_STRING (attr)
22045 && (startswith (DW_STRING (attr), "._")
22046 || startswith (DW_STRING (attr), "<anonymous")))
22047 return NULL;
22048
22049 /* GCC might emit a nameless typedef that has a linkage name. See
22050 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22051 if (!attr || DW_STRING (attr) == NULL)
22052 {
22053 attr = dw2_linkage_name_attr (die, cu);
22054 if (attr == NULL || DW_STRING (attr) == NULL)
22055 return NULL;
22056
22057 /* Avoid demangling DW_STRING (attr) the second time on a second
22058 call for the same DIE. */
22059 if (!DW_STRING_IS_CANONICAL (attr))
22060 {
22061 gdb::unique_xmalloc_ptr<char> demangled
22062 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22063 if (demangled == nullptr)
22064 return nullptr;
22065
22066 DW_STRING (attr) = objfile->intern (demangled.get ());
22067 DW_STRING_IS_CANONICAL (attr) = 1;
22068 }
22069
22070 /* Strip any leading namespaces/classes, keep only the base name.
22071 DW_AT_name for named DIEs does not contain the prefixes. */
22072 const char *base = strrchr (DW_STRING (attr), ':');
22073 if (base && base > DW_STRING (attr) && base[-1] == ':')
22074 return &base[1];
22075 else
22076 return DW_STRING (attr);
22077 }
22078 break;
22079
22080 default:
22081 break;
22082 }
22083
22084 if (!DW_STRING_IS_CANONICAL (attr))
22085 {
22086 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22087 objfile);
22088 DW_STRING_IS_CANONICAL (attr) = 1;
22089 }
22090 return DW_STRING (attr);
22091 }
22092
22093 /* Return the die that this die in an extension of, or NULL if there
22094 is none. *EXT_CU is the CU containing DIE on input, and the CU
22095 containing the return value on output. */
22096
22097 static struct die_info *
22098 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22099 {
22100 struct attribute *attr;
22101
22102 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22103 if (attr == NULL)
22104 return NULL;
22105
22106 return follow_die_ref (die, attr, ext_cu);
22107 }
22108
22109 static void
22110 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22111 {
22112 unsigned int i;
22113
22114 print_spaces (indent, f);
22115 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22116 dwarf_tag_name (die->tag), die->abbrev,
22117 sect_offset_str (die->sect_off));
22118
22119 if (die->parent != NULL)
22120 {
22121 print_spaces (indent, f);
22122 fprintf_unfiltered (f, " parent at offset: %s\n",
22123 sect_offset_str (die->parent->sect_off));
22124 }
22125
22126 print_spaces (indent, f);
22127 fprintf_unfiltered (f, " has children: %s\n",
22128 dwarf_bool_name (die->child != NULL));
22129
22130 print_spaces (indent, f);
22131 fprintf_unfiltered (f, " attributes:\n");
22132
22133 for (i = 0; i < die->num_attrs; ++i)
22134 {
22135 print_spaces (indent, f);
22136 fprintf_unfiltered (f, " %s (%s) ",
22137 dwarf_attr_name (die->attrs[i].name),
22138 dwarf_form_name (die->attrs[i].form));
22139
22140 switch (die->attrs[i].form)
22141 {
22142 case DW_FORM_addr:
22143 case DW_FORM_addrx:
22144 case DW_FORM_GNU_addr_index:
22145 fprintf_unfiltered (f, "address: ");
22146 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22147 break;
22148 case DW_FORM_block2:
22149 case DW_FORM_block4:
22150 case DW_FORM_block:
22151 case DW_FORM_block1:
22152 fprintf_unfiltered (f, "block: size %s",
22153 pulongest (DW_BLOCK (&die->attrs[i])->size));
22154 break;
22155 case DW_FORM_exprloc:
22156 fprintf_unfiltered (f, "expression: size %s",
22157 pulongest (DW_BLOCK (&die->attrs[i])->size));
22158 break;
22159 case DW_FORM_data16:
22160 fprintf_unfiltered (f, "constant of 16 bytes");
22161 break;
22162 case DW_FORM_ref_addr:
22163 fprintf_unfiltered (f, "ref address: ");
22164 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22165 break;
22166 case DW_FORM_GNU_ref_alt:
22167 fprintf_unfiltered (f, "alt ref address: ");
22168 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22169 break;
22170 case DW_FORM_ref1:
22171 case DW_FORM_ref2:
22172 case DW_FORM_ref4:
22173 case DW_FORM_ref8:
22174 case DW_FORM_ref_udata:
22175 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22176 (long) (DW_UNSND (&die->attrs[i])));
22177 break;
22178 case DW_FORM_data1:
22179 case DW_FORM_data2:
22180 case DW_FORM_data4:
22181 case DW_FORM_data8:
22182 case DW_FORM_udata:
22183 case DW_FORM_sdata:
22184 fprintf_unfiltered (f, "constant: %s",
22185 pulongest (DW_UNSND (&die->attrs[i])));
22186 break;
22187 case DW_FORM_sec_offset:
22188 fprintf_unfiltered (f, "section offset: %s",
22189 pulongest (DW_UNSND (&die->attrs[i])));
22190 break;
22191 case DW_FORM_ref_sig8:
22192 fprintf_unfiltered (f, "signature: %s",
22193 hex_string (DW_SIGNATURE (&die->attrs[i])));
22194 break;
22195 case DW_FORM_string:
22196 case DW_FORM_strp:
22197 case DW_FORM_line_strp:
22198 case DW_FORM_strx:
22199 case DW_FORM_GNU_str_index:
22200 case DW_FORM_GNU_strp_alt:
22201 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22202 DW_STRING (&die->attrs[i])
22203 ? DW_STRING (&die->attrs[i]) : "",
22204 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22205 break;
22206 case DW_FORM_flag:
22207 if (DW_UNSND (&die->attrs[i]))
22208 fprintf_unfiltered (f, "flag: TRUE");
22209 else
22210 fprintf_unfiltered (f, "flag: FALSE");
22211 break;
22212 case DW_FORM_flag_present:
22213 fprintf_unfiltered (f, "flag: TRUE");
22214 break;
22215 case DW_FORM_indirect:
22216 /* The reader will have reduced the indirect form to
22217 the "base form" so this form should not occur. */
22218 fprintf_unfiltered (f,
22219 "unexpected attribute form: DW_FORM_indirect");
22220 break;
22221 case DW_FORM_implicit_const:
22222 fprintf_unfiltered (f, "constant: %s",
22223 plongest (DW_SND (&die->attrs[i])));
22224 break;
22225 default:
22226 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22227 die->attrs[i].form);
22228 break;
22229 }
22230 fprintf_unfiltered (f, "\n");
22231 }
22232 }
22233
22234 static void
22235 dump_die_for_error (struct die_info *die)
22236 {
22237 dump_die_shallow (gdb_stderr, 0, die);
22238 }
22239
22240 static void
22241 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22242 {
22243 int indent = level * 4;
22244
22245 gdb_assert (die != NULL);
22246
22247 if (level >= max_level)
22248 return;
22249
22250 dump_die_shallow (f, indent, die);
22251
22252 if (die->child != NULL)
22253 {
22254 print_spaces (indent, f);
22255 fprintf_unfiltered (f, " Children:");
22256 if (level + 1 < max_level)
22257 {
22258 fprintf_unfiltered (f, "\n");
22259 dump_die_1 (f, level + 1, max_level, die->child);
22260 }
22261 else
22262 {
22263 fprintf_unfiltered (f,
22264 " [not printed, max nesting level reached]\n");
22265 }
22266 }
22267
22268 if (die->sibling != NULL && level > 0)
22269 {
22270 dump_die_1 (f, level, max_level, die->sibling);
22271 }
22272 }
22273
22274 /* This is called from the pdie macro in gdbinit.in.
22275 It's not static so gcc will keep a copy callable from gdb. */
22276
22277 void
22278 dump_die (struct die_info *die, int max_level)
22279 {
22280 dump_die_1 (gdb_stdlog, 0, max_level, die);
22281 }
22282
22283 static void
22284 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22285 {
22286 void **slot;
22287
22288 slot = htab_find_slot_with_hash (cu->die_hash, die,
22289 to_underlying (die->sect_off),
22290 INSERT);
22291
22292 *slot = die;
22293 }
22294
22295 /* Follow reference or signature attribute ATTR of SRC_DIE.
22296 On entry *REF_CU is the CU of SRC_DIE.
22297 On exit *REF_CU is the CU of the result. */
22298
22299 static struct die_info *
22300 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22301 struct dwarf2_cu **ref_cu)
22302 {
22303 struct die_info *die;
22304
22305 if (attr->form_is_ref ())
22306 die = follow_die_ref (src_die, attr, ref_cu);
22307 else if (attr->form == DW_FORM_ref_sig8)
22308 die = follow_die_sig (src_die, attr, ref_cu);
22309 else
22310 {
22311 dump_die_for_error (src_die);
22312 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22313 objfile_name ((*ref_cu)->per_objfile->objfile));
22314 }
22315
22316 return die;
22317 }
22318
22319 /* Follow reference OFFSET.
22320 On entry *REF_CU is the CU of the source die referencing OFFSET.
22321 On exit *REF_CU is the CU of the result.
22322 Returns NULL if OFFSET is invalid. */
22323
22324 static struct die_info *
22325 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22326 struct dwarf2_cu **ref_cu)
22327 {
22328 struct die_info temp_die;
22329 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22330 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22331
22332 gdb_assert (cu->per_cu != NULL);
22333
22334 target_cu = cu;
22335
22336 if (cu->per_cu->is_debug_types)
22337 {
22338 /* .debug_types CUs cannot reference anything outside their CU.
22339 If they need to, they have to reference a signatured type via
22340 DW_FORM_ref_sig8. */
22341 if (!cu->header.offset_in_cu_p (sect_off))
22342 return NULL;
22343 }
22344 else if (offset_in_dwz != cu->per_cu->is_dwz
22345 || !cu->header.offset_in_cu_p (sect_off))
22346 {
22347 struct dwarf2_per_cu_data *per_cu;
22348
22349 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22350 per_objfile);
22351
22352 /* If necessary, add it to the queue and load its DIEs. */
22353 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22354 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
22355
22356 target_cu = per_objfile->get_cu (per_cu);
22357 }
22358 else if (cu->dies == NULL)
22359 {
22360 /* We're loading full DIEs during partial symbol reading. */
22361 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22362 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
22363 }
22364
22365 *ref_cu = target_cu;
22366 temp_die.sect_off = sect_off;
22367
22368 if (target_cu != cu)
22369 target_cu->ancestor = cu;
22370
22371 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22372 &temp_die,
22373 to_underlying (sect_off));
22374 }
22375
22376 /* Follow reference attribute ATTR of SRC_DIE.
22377 On entry *REF_CU is the CU of SRC_DIE.
22378 On exit *REF_CU is the CU of the result. */
22379
22380 static struct die_info *
22381 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22382 struct dwarf2_cu **ref_cu)
22383 {
22384 sect_offset sect_off = attr->get_ref_die_offset ();
22385 struct dwarf2_cu *cu = *ref_cu;
22386 struct die_info *die;
22387
22388 die = follow_die_offset (sect_off,
22389 (attr->form == DW_FORM_GNU_ref_alt
22390 || cu->per_cu->is_dwz),
22391 ref_cu);
22392 if (!die)
22393 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22394 "at %s [in module %s]"),
22395 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22396 objfile_name (cu->per_objfile->objfile));
22397
22398 return die;
22399 }
22400
22401 /* See read.h. */
22402
22403 struct dwarf2_locexpr_baton
22404 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22405 dwarf2_per_cu_data *per_cu,
22406 dwarf2_per_objfile *per_objfile,
22407 CORE_ADDR (*get_frame_pc) (void *baton),
22408 void *baton, bool resolve_abstract_p)
22409 {
22410 struct die_info *die;
22411 struct attribute *attr;
22412 struct dwarf2_locexpr_baton retval;
22413 struct objfile *objfile = per_objfile->objfile;
22414
22415 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22416 if (cu == nullptr)
22417 cu = load_cu (per_cu, per_objfile, false);
22418
22419 if (cu == nullptr)
22420 {
22421 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22422 Instead just throw an error, not much else we can do. */
22423 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22424 sect_offset_str (sect_off), objfile_name (objfile));
22425 }
22426
22427 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22428 if (!die)
22429 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22430 sect_offset_str (sect_off), objfile_name (objfile));
22431
22432 attr = dwarf2_attr (die, DW_AT_location, cu);
22433 if (!attr && resolve_abstract_p
22434 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22435 != per_objfile->per_bfd->abstract_to_concrete.end ()))
22436 {
22437 CORE_ADDR pc = (*get_frame_pc) (baton);
22438 CORE_ADDR baseaddr = objfile->text_section_offset ();
22439 struct gdbarch *gdbarch = objfile->arch ();
22440
22441 for (const auto &cand_off
22442 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22443 {
22444 struct dwarf2_cu *cand_cu = cu;
22445 struct die_info *cand
22446 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22447 if (!cand
22448 || !cand->parent
22449 || cand->parent->tag != DW_TAG_subprogram)
22450 continue;
22451
22452 CORE_ADDR pc_low, pc_high;
22453 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22454 if (pc_low == ((CORE_ADDR) -1))
22455 continue;
22456 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22457 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22458 if (!(pc_low <= pc && pc < pc_high))
22459 continue;
22460
22461 die = cand;
22462 attr = dwarf2_attr (die, DW_AT_location, cu);
22463 break;
22464 }
22465 }
22466
22467 if (!attr)
22468 {
22469 /* DWARF: "If there is no such attribute, then there is no effect.".
22470 DATA is ignored if SIZE is 0. */
22471
22472 retval.data = NULL;
22473 retval.size = 0;
22474 }
22475 else if (attr->form_is_section_offset ())
22476 {
22477 struct dwarf2_loclist_baton loclist_baton;
22478 CORE_ADDR pc = (*get_frame_pc) (baton);
22479 size_t size;
22480
22481 fill_in_loclist_baton (cu, &loclist_baton, attr);
22482
22483 retval.data = dwarf2_find_location_expression (&loclist_baton,
22484 &size, pc);
22485 retval.size = size;
22486 }
22487 else
22488 {
22489 if (!attr->form_is_block ())
22490 error (_("Dwarf Error: DIE at %s referenced in module %s "
22491 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22492 sect_offset_str (sect_off), objfile_name (objfile));
22493
22494 retval.data = DW_BLOCK (attr)->data;
22495 retval.size = DW_BLOCK (attr)->size;
22496 }
22497 retval.per_objfile = per_objfile;
22498 retval.per_cu = cu->per_cu;
22499
22500 per_objfile->age_comp_units ();
22501
22502 return retval;
22503 }
22504
22505 /* See read.h. */
22506
22507 struct dwarf2_locexpr_baton
22508 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22509 dwarf2_per_cu_data *per_cu,
22510 dwarf2_per_objfile *per_objfile,
22511 CORE_ADDR (*get_frame_pc) (void *baton),
22512 void *baton)
22513 {
22514 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22515
22516 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22517 get_frame_pc, baton);
22518 }
22519
22520 /* Write a constant of a given type as target-ordered bytes into
22521 OBSTACK. */
22522
22523 static const gdb_byte *
22524 write_constant_as_bytes (struct obstack *obstack,
22525 enum bfd_endian byte_order,
22526 struct type *type,
22527 ULONGEST value,
22528 LONGEST *len)
22529 {
22530 gdb_byte *result;
22531
22532 *len = TYPE_LENGTH (type);
22533 result = (gdb_byte *) obstack_alloc (obstack, *len);
22534 store_unsigned_integer (result, *len, byte_order, value);
22535
22536 return result;
22537 }
22538
22539 /* See read.h. */
22540
22541 const gdb_byte *
22542 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22543 dwarf2_per_cu_data *per_cu,
22544 dwarf2_per_objfile *per_objfile,
22545 obstack *obstack,
22546 LONGEST *len)
22547 {
22548 struct die_info *die;
22549 struct attribute *attr;
22550 const gdb_byte *result = NULL;
22551 struct type *type;
22552 LONGEST value;
22553 enum bfd_endian byte_order;
22554 struct objfile *objfile = per_objfile->objfile;
22555
22556 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22557 if (cu == nullptr)
22558 cu = load_cu (per_cu, per_objfile, false);
22559
22560 if (cu == nullptr)
22561 {
22562 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22563 Instead just throw an error, not much else we can do. */
22564 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22565 sect_offset_str (sect_off), objfile_name (objfile));
22566 }
22567
22568 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22569 if (!die)
22570 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22571 sect_offset_str (sect_off), objfile_name (objfile));
22572
22573 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22574 if (attr == NULL)
22575 return NULL;
22576
22577 byte_order = (bfd_big_endian (objfile->obfd)
22578 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22579
22580 switch (attr->form)
22581 {
22582 case DW_FORM_addr:
22583 case DW_FORM_addrx:
22584 case DW_FORM_GNU_addr_index:
22585 {
22586 gdb_byte *tem;
22587
22588 *len = cu->header.addr_size;
22589 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22590 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22591 result = tem;
22592 }
22593 break;
22594 case DW_FORM_string:
22595 case DW_FORM_strp:
22596 case DW_FORM_strx:
22597 case DW_FORM_GNU_str_index:
22598 case DW_FORM_GNU_strp_alt:
22599 /* DW_STRING is already allocated on the objfile obstack, point
22600 directly to it. */
22601 result = (const gdb_byte *) DW_STRING (attr);
22602 *len = strlen (DW_STRING (attr));
22603 break;
22604 case DW_FORM_block1:
22605 case DW_FORM_block2:
22606 case DW_FORM_block4:
22607 case DW_FORM_block:
22608 case DW_FORM_exprloc:
22609 case DW_FORM_data16:
22610 result = DW_BLOCK (attr)->data;
22611 *len = DW_BLOCK (attr)->size;
22612 break;
22613
22614 /* The DW_AT_const_value attributes are supposed to carry the
22615 symbol's value "represented as it would be on the target
22616 architecture." By the time we get here, it's already been
22617 converted to host endianness, so we just need to sign- or
22618 zero-extend it as appropriate. */
22619 case DW_FORM_data1:
22620 type = die_type (die, cu);
22621 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22622 if (result == NULL)
22623 result = write_constant_as_bytes (obstack, byte_order,
22624 type, value, len);
22625 break;
22626 case DW_FORM_data2:
22627 type = die_type (die, cu);
22628 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22629 if (result == NULL)
22630 result = write_constant_as_bytes (obstack, byte_order,
22631 type, value, len);
22632 break;
22633 case DW_FORM_data4:
22634 type = die_type (die, cu);
22635 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22636 if (result == NULL)
22637 result = write_constant_as_bytes (obstack, byte_order,
22638 type, value, len);
22639 break;
22640 case DW_FORM_data8:
22641 type = die_type (die, cu);
22642 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22643 if (result == NULL)
22644 result = write_constant_as_bytes (obstack, byte_order,
22645 type, value, len);
22646 break;
22647
22648 case DW_FORM_sdata:
22649 case DW_FORM_implicit_const:
22650 type = die_type (die, cu);
22651 result = write_constant_as_bytes (obstack, byte_order,
22652 type, DW_SND (attr), len);
22653 break;
22654
22655 case DW_FORM_udata:
22656 type = die_type (die, cu);
22657 result = write_constant_as_bytes (obstack, byte_order,
22658 type, DW_UNSND (attr), len);
22659 break;
22660
22661 default:
22662 complaint (_("unsupported const value attribute form: '%s'"),
22663 dwarf_form_name (attr->form));
22664 break;
22665 }
22666
22667 return result;
22668 }
22669
22670 /* See read.h. */
22671
22672 struct type *
22673 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22674 dwarf2_per_cu_data *per_cu,
22675 dwarf2_per_objfile *per_objfile)
22676 {
22677 struct die_info *die;
22678
22679 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22680 if (cu == nullptr)
22681 cu = load_cu (per_cu, per_objfile, false);
22682
22683 if (cu == nullptr)
22684 return nullptr;
22685
22686 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22687 if (!die)
22688 return NULL;
22689
22690 return die_type (die, cu);
22691 }
22692
22693 /* See read.h. */
22694
22695 struct type *
22696 dwarf2_get_die_type (cu_offset die_offset,
22697 dwarf2_per_cu_data *per_cu,
22698 dwarf2_per_objfile *per_objfile)
22699 {
22700 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22701 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22702 }
22703
22704 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22705 On entry *REF_CU is the CU of SRC_DIE.
22706 On exit *REF_CU is the CU of the result.
22707 Returns NULL if the referenced DIE isn't found. */
22708
22709 static struct die_info *
22710 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22711 struct dwarf2_cu **ref_cu)
22712 {
22713 struct die_info temp_die;
22714 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22715 struct die_info *die;
22716 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
22717
22718
22719 /* While it might be nice to assert sig_type->type == NULL here,
22720 we can get here for DW_AT_imported_declaration where we need
22721 the DIE not the type. */
22722
22723 /* If necessary, add it to the queue and load its DIEs. */
22724
22725 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
22726 language_minimal))
22727 read_signatured_type (sig_type, per_objfile);
22728
22729 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
22730 gdb_assert (sig_cu != NULL);
22731 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22732 temp_die.sect_off = sig_type->type_offset_in_section;
22733 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22734 to_underlying (temp_die.sect_off));
22735 if (die)
22736 {
22737 /* For .gdb_index version 7 keep track of included TUs.
22738 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22739 if (per_objfile->per_bfd->index_table != NULL
22740 && per_objfile->per_bfd->index_table->version <= 7)
22741 {
22742 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22743 }
22744
22745 *ref_cu = sig_cu;
22746 if (sig_cu != cu)
22747 sig_cu->ancestor = cu;
22748
22749 return die;
22750 }
22751
22752 return NULL;
22753 }
22754
22755 /* Follow signatured type referenced by ATTR in SRC_DIE.
22756 On entry *REF_CU is the CU of SRC_DIE.
22757 On exit *REF_CU is the CU of the result.
22758 The result is the DIE of the type.
22759 If the referenced type cannot be found an error is thrown. */
22760
22761 static struct die_info *
22762 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22763 struct dwarf2_cu **ref_cu)
22764 {
22765 ULONGEST signature = DW_SIGNATURE (attr);
22766 struct signatured_type *sig_type;
22767 struct die_info *die;
22768
22769 gdb_assert (attr->form == DW_FORM_ref_sig8);
22770
22771 sig_type = lookup_signatured_type (*ref_cu, signature);
22772 /* sig_type will be NULL if the signatured type is missing from
22773 the debug info. */
22774 if (sig_type == NULL)
22775 {
22776 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22777 " from DIE at %s [in module %s]"),
22778 hex_string (signature), sect_offset_str (src_die->sect_off),
22779 objfile_name ((*ref_cu)->per_objfile->objfile));
22780 }
22781
22782 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22783 if (die == NULL)
22784 {
22785 dump_die_for_error (src_die);
22786 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22787 " from DIE at %s [in module %s]"),
22788 hex_string (signature), sect_offset_str (src_die->sect_off),
22789 objfile_name ((*ref_cu)->per_objfile->objfile));
22790 }
22791
22792 return die;
22793 }
22794
22795 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22796 reading in and processing the type unit if necessary. */
22797
22798 static struct type *
22799 get_signatured_type (struct die_info *die, ULONGEST signature,
22800 struct dwarf2_cu *cu)
22801 {
22802 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22803 struct signatured_type *sig_type;
22804 struct dwarf2_cu *type_cu;
22805 struct die_info *type_die;
22806 struct type *type;
22807
22808 sig_type = lookup_signatured_type (cu, signature);
22809 /* sig_type will be NULL if the signatured type is missing from
22810 the debug info. */
22811 if (sig_type == NULL)
22812 {
22813 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22814 " from DIE at %s [in module %s]"),
22815 hex_string (signature), sect_offset_str (die->sect_off),
22816 objfile_name (per_objfile->objfile));
22817 return build_error_marker_type (cu, die);
22818 }
22819
22820 /* If we already know the type we're done. */
22821 type = per_objfile->get_type_for_signatured_type (sig_type);
22822 if (type != nullptr)
22823 return type;
22824
22825 type_cu = cu;
22826 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22827 if (type_die != NULL)
22828 {
22829 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22830 is created. This is important, for example, because for c++ classes
22831 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22832 type = read_type_die (type_die, type_cu);
22833 if (type == NULL)
22834 {
22835 complaint (_("Dwarf Error: Cannot build signatured type %s"
22836 " referenced from DIE at %s [in module %s]"),
22837 hex_string (signature), sect_offset_str (die->sect_off),
22838 objfile_name (per_objfile->objfile));
22839 type = build_error_marker_type (cu, die);
22840 }
22841 }
22842 else
22843 {
22844 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22845 " from DIE at %s [in module %s]"),
22846 hex_string (signature), sect_offset_str (die->sect_off),
22847 objfile_name (per_objfile->objfile));
22848 type = build_error_marker_type (cu, die);
22849 }
22850
22851 per_objfile->set_type_for_signatured_type (sig_type, type);
22852
22853 return type;
22854 }
22855
22856 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22857 reading in and processing the type unit if necessary. */
22858
22859 static struct type *
22860 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22861 struct dwarf2_cu *cu) /* ARI: editCase function */
22862 {
22863 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22864 if (attr->form_is_ref ())
22865 {
22866 struct dwarf2_cu *type_cu = cu;
22867 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22868
22869 return read_type_die (type_die, type_cu);
22870 }
22871 else if (attr->form == DW_FORM_ref_sig8)
22872 {
22873 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22874 }
22875 else
22876 {
22877 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22878
22879 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22880 " at %s [in module %s]"),
22881 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22882 objfile_name (per_objfile->objfile));
22883 return build_error_marker_type (cu, die);
22884 }
22885 }
22886
22887 /* Load the DIEs associated with type unit PER_CU into memory. */
22888
22889 static void
22890 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22891 dwarf2_per_objfile *per_objfile)
22892 {
22893 struct signatured_type *sig_type;
22894
22895 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22896 gdb_assert (! per_cu->type_unit_group_p ());
22897
22898 /* We have the per_cu, but we need the signatured_type.
22899 Fortunately this is an easy translation. */
22900 gdb_assert (per_cu->is_debug_types);
22901 sig_type = (struct signatured_type *) per_cu;
22902
22903 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22904
22905 read_signatured_type (sig_type, per_objfile);
22906
22907 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22908 }
22909
22910 /* Read in a signatured type and build its CU and DIEs.
22911 If the type is a stub for the real type in a DWO file,
22912 read in the real type from the DWO file as well. */
22913
22914 static void
22915 read_signatured_type (signatured_type *sig_type,
22916 dwarf2_per_objfile *per_objfile)
22917 {
22918 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22919
22920 gdb_assert (per_cu->is_debug_types);
22921 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22922
22923 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
22924
22925 if (!reader.dummy_p)
22926 {
22927 struct dwarf2_cu *cu = reader.cu;
22928 const gdb_byte *info_ptr = reader.info_ptr;
22929
22930 gdb_assert (cu->die_hash == NULL);
22931 cu->die_hash =
22932 htab_create_alloc_ex (cu->header.length / 12,
22933 die_hash,
22934 die_eq,
22935 NULL,
22936 &cu->comp_unit_obstack,
22937 hashtab_obstack_allocate,
22938 dummy_obstack_deallocate);
22939
22940 if (reader.comp_unit_die->has_children)
22941 reader.comp_unit_die->child
22942 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22943 reader.comp_unit_die);
22944 cu->dies = reader.comp_unit_die;
22945 /* comp_unit_die is not stored in die_hash, no need. */
22946
22947 /* We try not to read any attributes in this function, because
22948 not all CUs needed for references have been loaded yet, and
22949 symbol table processing isn't initialized. But we have to
22950 set the CU language, or we won't be able to build types
22951 correctly. Similarly, if we do not read the producer, we can
22952 not apply producer-specific interpretation. */
22953 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22954
22955 reader.keep ();
22956 }
22957
22958 sig_type->per_cu.tu_read = 1;
22959 }
22960
22961 /* Decode simple location descriptions.
22962 Given a pointer to a dwarf block that defines a location, compute
22963 the location and return the value. If COMPUTED is non-null, it is
22964 set to true to indicate that decoding was successful, and false
22965 otherwise. If COMPUTED is null, then this function may emit a
22966 complaint. */
22967
22968 static CORE_ADDR
22969 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22970 {
22971 struct objfile *objfile = cu->per_objfile->objfile;
22972 size_t i;
22973 size_t size = blk->size;
22974 const gdb_byte *data = blk->data;
22975 CORE_ADDR stack[64];
22976 int stacki;
22977 unsigned int bytes_read, unsnd;
22978 gdb_byte op;
22979
22980 if (computed != nullptr)
22981 *computed = false;
22982
22983 i = 0;
22984 stacki = 0;
22985 stack[stacki] = 0;
22986 stack[++stacki] = 0;
22987
22988 while (i < size)
22989 {
22990 op = data[i++];
22991 switch (op)
22992 {
22993 case DW_OP_lit0:
22994 case DW_OP_lit1:
22995 case DW_OP_lit2:
22996 case DW_OP_lit3:
22997 case DW_OP_lit4:
22998 case DW_OP_lit5:
22999 case DW_OP_lit6:
23000 case DW_OP_lit7:
23001 case DW_OP_lit8:
23002 case DW_OP_lit9:
23003 case DW_OP_lit10:
23004 case DW_OP_lit11:
23005 case DW_OP_lit12:
23006 case DW_OP_lit13:
23007 case DW_OP_lit14:
23008 case DW_OP_lit15:
23009 case DW_OP_lit16:
23010 case DW_OP_lit17:
23011 case DW_OP_lit18:
23012 case DW_OP_lit19:
23013 case DW_OP_lit20:
23014 case DW_OP_lit21:
23015 case DW_OP_lit22:
23016 case DW_OP_lit23:
23017 case DW_OP_lit24:
23018 case DW_OP_lit25:
23019 case DW_OP_lit26:
23020 case DW_OP_lit27:
23021 case DW_OP_lit28:
23022 case DW_OP_lit29:
23023 case DW_OP_lit30:
23024 case DW_OP_lit31:
23025 stack[++stacki] = op - DW_OP_lit0;
23026 break;
23027
23028 case DW_OP_reg0:
23029 case DW_OP_reg1:
23030 case DW_OP_reg2:
23031 case DW_OP_reg3:
23032 case DW_OP_reg4:
23033 case DW_OP_reg5:
23034 case DW_OP_reg6:
23035 case DW_OP_reg7:
23036 case DW_OP_reg8:
23037 case DW_OP_reg9:
23038 case DW_OP_reg10:
23039 case DW_OP_reg11:
23040 case DW_OP_reg12:
23041 case DW_OP_reg13:
23042 case DW_OP_reg14:
23043 case DW_OP_reg15:
23044 case DW_OP_reg16:
23045 case DW_OP_reg17:
23046 case DW_OP_reg18:
23047 case DW_OP_reg19:
23048 case DW_OP_reg20:
23049 case DW_OP_reg21:
23050 case DW_OP_reg22:
23051 case DW_OP_reg23:
23052 case DW_OP_reg24:
23053 case DW_OP_reg25:
23054 case DW_OP_reg26:
23055 case DW_OP_reg27:
23056 case DW_OP_reg28:
23057 case DW_OP_reg29:
23058 case DW_OP_reg30:
23059 case DW_OP_reg31:
23060 stack[++stacki] = op - DW_OP_reg0;
23061 if (i < size)
23062 {
23063 if (computed == nullptr)
23064 dwarf2_complex_location_expr_complaint ();
23065 else
23066 return 0;
23067 }
23068 break;
23069
23070 case DW_OP_regx:
23071 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23072 i += bytes_read;
23073 stack[++stacki] = unsnd;
23074 if (i < size)
23075 {
23076 if (computed == nullptr)
23077 dwarf2_complex_location_expr_complaint ();
23078 else
23079 return 0;
23080 }
23081 break;
23082
23083 case DW_OP_addr:
23084 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23085 &bytes_read);
23086 i += bytes_read;
23087 break;
23088
23089 case DW_OP_const1u:
23090 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23091 i += 1;
23092 break;
23093
23094 case DW_OP_const1s:
23095 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23096 i += 1;
23097 break;
23098
23099 case DW_OP_const2u:
23100 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23101 i += 2;
23102 break;
23103
23104 case DW_OP_const2s:
23105 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23106 i += 2;
23107 break;
23108
23109 case DW_OP_const4u:
23110 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23111 i += 4;
23112 break;
23113
23114 case DW_OP_const4s:
23115 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23116 i += 4;
23117 break;
23118
23119 case DW_OP_const8u:
23120 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23121 i += 8;
23122 break;
23123
23124 case DW_OP_constu:
23125 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23126 &bytes_read);
23127 i += bytes_read;
23128 break;
23129
23130 case DW_OP_consts:
23131 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23132 i += bytes_read;
23133 break;
23134
23135 case DW_OP_dup:
23136 stack[stacki + 1] = stack[stacki];
23137 stacki++;
23138 break;
23139
23140 case DW_OP_plus:
23141 stack[stacki - 1] += stack[stacki];
23142 stacki--;
23143 break;
23144
23145 case DW_OP_plus_uconst:
23146 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23147 &bytes_read);
23148 i += bytes_read;
23149 break;
23150
23151 case DW_OP_minus:
23152 stack[stacki - 1] -= stack[stacki];
23153 stacki--;
23154 break;
23155
23156 case DW_OP_deref:
23157 /* If we're not the last op, then we definitely can't encode
23158 this using GDB's address_class enum. This is valid for partial
23159 global symbols, although the variable's address will be bogus
23160 in the psymtab. */
23161 if (i < size)
23162 {
23163 if (computed == nullptr)
23164 dwarf2_complex_location_expr_complaint ();
23165 else
23166 return 0;
23167 }
23168 break;
23169
23170 case DW_OP_GNU_push_tls_address:
23171 case DW_OP_form_tls_address:
23172 /* The top of the stack has the offset from the beginning
23173 of the thread control block at which the variable is located. */
23174 /* Nothing should follow this operator, so the top of stack would
23175 be returned. */
23176 /* This is valid for partial global symbols, but the variable's
23177 address will be bogus in the psymtab. Make it always at least
23178 non-zero to not look as a variable garbage collected by linker
23179 which have DW_OP_addr 0. */
23180 if (i < size)
23181 {
23182 if (computed == nullptr)
23183 dwarf2_complex_location_expr_complaint ();
23184 else
23185 return 0;
23186 }
23187 stack[stacki]++;
23188 break;
23189
23190 case DW_OP_GNU_uninit:
23191 if (computed != nullptr)
23192 return 0;
23193 break;
23194
23195 case DW_OP_addrx:
23196 case DW_OP_GNU_addr_index:
23197 case DW_OP_GNU_const_index:
23198 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23199 &bytes_read);
23200 i += bytes_read;
23201 break;
23202
23203 default:
23204 if (computed == nullptr)
23205 {
23206 const char *name = get_DW_OP_name (op);
23207
23208 if (name)
23209 complaint (_("unsupported stack op: '%s'"),
23210 name);
23211 else
23212 complaint (_("unsupported stack op: '%02x'"),
23213 op);
23214 }
23215
23216 return (stack[stacki]);
23217 }
23218
23219 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23220 outside of the allocated space. Also enforce minimum>0. */
23221 if (stacki >= ARRAY_SIZE (stack) - 1)
23222 {
23223 if (computed == nullptr)
23224 complaint (_("location description stack overflow"));
23225 return 0;
23226 }
23227
23228 if (stacki <= 0)
23229 {
23230 if (computed == nullptr)
23231 complaint (_("location description stack underflow"));
23232 return 0;
23233 }
23234 }
23235
23236 if (computed != nullptr)
23237 *computed = true;
23238 return (stack[stacki]);
23239 }
23240
23241 /* memory allocation interface */
23242
23243 static struct dwarf_block *
23244 dwarf_alloc_block (struct dwarf2_cu *cu)
23245 {
23246 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23247 }
23248
23249 static struct die_info *
23250 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23251 {
23252 struct die_info *die;
23253 size_t size = sizeof (struct die_info);
23254
23255 if (num_attrs > 1)
23256 size += (num_attrs - 1) * sizeof (struct attribute);
23257
23258 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23259 memset (die, 0, sizeof (struct die_info));
23260 return (die);
23261 }
23262
23263 \f
23264
23265 /* Macro support. */
23266
23267 /* An overload of dwarf_decode_macros that finds the correct section
23268 and ensures it is read in before calling the other overload. */
23269
23270 static void
23271 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23272 int section_is_gnu)
23273 {
23274 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23275 struct objfile *objfile = per_objfile->objfile;
23276 const struct line_header *lh = cu->line_header;
23277 unsigned int offset_size = cu->header.offset_size;
23278 struct dwarf2_section_info *section;
23279 const char *section_name;
23280
23281 if (cu->dwo_unit != nullptr)
23282 {
23283 if (section_is_gnu)
23284 {
23285 section = &cu->dwo_unit->dwo_file->sections.macro;
23286 section_name = ".debug_macro.dwo";
23287 }
23288 else
23289 {
23290 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23291 section_name = ".debug_macinfo.dwo";
23292 }
23293 }
23294 else
23295 {
23296 if (section_is_gnu)
23297 {
23298 section = &per_objfile->per_bfd->macro;
23299 section_name = ".debug_macro";
23300 }
23301 else
23302 {
23303 section = &per_objfile->per_bfd->macinfo;
23304 section_name = ".debug_macinfo";
23305 }
23306 }
23307
23308 section->read (objfile);
23309 if (section->buffer == nullptr)
23310 {
23311 complaint (_("missing %s section"), section_name);
23312 return;
23313 }
23314
23315 buildsym_compunit *builder = cu->get_builder ();
23316
23317 dwarf_decode_macros (per_objfile, builder, section, lh,
23318 offset_size, offset, section_is_gnu);
23319 }
23320
23321 /* Return the .debug_loc section to use for CU.
23322 For DWO files use .debug_loc.dwo. */
23323
23324 static struct dwarf2_section_info *
23325 cu_debug_loc_section (struct dwarf2_cu *cu)
23326 {
23327 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23328
23329 if (cu->dwo_unit)
23330 {
23331 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23332
23333 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23334 }
23335 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23336 : &per_objfile->per_bfd->loc);
23337 }
23338
23339 /* A helper function that fills in a dwarf2_loclist_baton. */
23340
23341 static void
23342 fill_in_loclist_baton (struct dwarf2_cu *cu,
23343 struct dwarf2_loclist_baton *baton,
23344 const struct attribute *attr)
23345 {
23346 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23347 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23348
23349 section->read (per_objfile->objfile);
23350
23351 baton->per_objfile = per_objfile;
23352 baton->per_cu = cu->per_cu;
23353 gdb_assert (baton->per_cu);
23354 /* We don't know how long the location list is, but make sure we
23355 don't run off the edge of the section. */
23356 baton->size = section->size - DW_UNSND (attr);
23357 baton->data = section->buffer + DW_UNSND (attr);
23358 if (cu->base_address.has_value ())
23359 baton->base_address = *cu->base_address;
23360 else
23361 baton->base_address = 0;
23362 baton->from_dwo = cu->dwo_unit != NULL;
23363 }
23364
23365 static void
23366 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23367 struct dwarf2_cu *cu, int is_block)
23368 {
23369 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23370 struct objfile *objfile = per_objfile->objfile;
23371 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23372
23373 if (attr->form_is_section_offset ()
23374 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23375 the section. If so, fall through to the complaint in the
23376 other branch. */
23377 && DW_UNSND (attr) < section->get_size (objfile))
23378 {
23379 struct dwarf2_loclist_baton *baton;
23380
23381 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23382
23383 fill_in_loclist_baton (cu, baton, attr);
23384
23385 if (!cu->base_address.has_value ())
23386 complaint (_("Location list used without "
23387 "specifying the CU base address."));
23388
23389 SYMBOL_ACLASS_INDEX (sym) = (is_block
23390 ? dwarf2_loclist_block_index
23391 : dwarf2_loclist_index);
23392 SYMBOL_LOCATION_BATON (sym) = baton;
23393 }
23394 else
23395 {
23396 struct dwarf2_locexpr_baton *baton;
23397
23398 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23399 baton->per_objfile = per_objfile;
23400 baton->per_cu = cu->per_cu;
23401 gdb_assert (baton->per_cu);
23402
23403 if (attr->form_is_block ())
23404 {
23405 /* Note that we're just copying the block's data pointer
23406 here, not the actual data. We're still pointing into the
23407 info_buffer for SYM's objfile; right now we never release
23408 that buffer, but when we do clean up properly this may
23409 need to change. */
23410 baton->size = DW_BLOCK (attr)->size;
23411 baton->data = DW_BLOCK (attr)->data;
23412 }
23413 else
23414 {
23415 dwarf2_invalid_attrib_class_complaint ("location description",
23416 sym->natural_name ());
23417 baton->size = 0;
23418 }
23419
23420 SYMBOL_ACLASS_INDEX (sym) = (is_block
23421 ? dwarf2_locexpr_block_index
23422 : dwarf2_locexpr_index);
23423 SYMBOL_LOCATION_BATON (sym) = baton;
23424 }
23425 }
23426
23427 /* See read.h. */
23428
23429 const comp_unit_head *
23430 dwarf2_per_cu_data::get_header () const
23431 {
23432 if (!m_header_read_in)
23433 {
23434 const gdb_byte *info_ptr
23435 = this->section->buffer + to_underlying (this->sect_off);
23436
23437 memset (&m_header, 0, sizeof (m_header));
23438
23439 read_comp_unit_head (&m_header, info_ptr, this->section,
23440 rcuh_kind::COMPILE);
23441 }
23442
23443 return &m_header;
23444 }
23445
23446 /* See read.h. */
23447
23448 int
23449 dwarf2_per_cu_data::addr_size () const
23450 {
23451 return this->get_header ()->addr_size;
23452 }
23453
23454 /* See read.h. */
23455
23456 int
23457 dwarf2_per_cu_data::offset_size () const
23458 {
23459 return this->get_header ()->offset_size;
23460 }
23461
23462 /* See read.h. */
23463
23464 int
23465 dwarf2_per_cu_data::ref_addr_size () const
23466 {
23467 const comp_unit_head *header = this->get_header ();
23468
23469 if (header->version == 2)
23470 return header->addr_size;
23471 else
23472 return header->offset_size;
23473 }
23474
23475 /* See read.h. */
23476
23477 struct type *
23478 dwarf2_cu::addr_type () const
23479 {
23480 struct objfile *objfile = this->per_objfile->objfile;
23481 struct type *void_type = objfile_type (objfile)->builtin_void;
23482 struct type *addr_type = lookup_pointer_type (void_type);
23483 int addr_size = this->per_cu->addr_size ();
23484
23485 if (TYPE_LENGTH (addr_type) == addr_size)
23486 return addr_type;
23487
23488 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23489 return addr_type;
23490 }
23491
23492 /* A helper function for dwarf2_find_containing_comp_unit that returns
23493 the index of the result, and that searches a vector. It will
23494 return a result even if the offset in question does not actually
23495 occur in any CU. This is separate so that it can be unit
23496 tested. */
23497
23498 static int
23499 dwarf2_find_containing_comp_unit
23500 (sect_offset sect_off,
23501 unsigned int offset_in_dwz,
23502 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23503 {
23504 int low, high;
23505
23506 low = 0;
23507 high = all_comp_units.size () - 1;
23508 while (high > low)
23509 {
23510 struct dwarf2_per_cu_data *mid_cu;
23511 int mid = low + (high - low) / 2;
23512
23513 mid_cu = all_comp_units[mid];
23514 if (mid_cu->is_dwz > offset_in_dwz
23515 || (mid_cu->is_dwz == offset_in_dwz
23516 && mid_cu->sect_off + mid_cu->length > sect_off))
23517 high = mid;
23518 else
23519 low = mid + 1;
23520 }
23521 gdb_assert (low == high);
23522 return low;
23523 }
23524
23525 /* Locate the .debug_info compilation unit from CU's objfile which contains
23526 the DIE at OFFSET. Raises an error on failure. */
23527
23528 static struct dwarf2_per_cu_data *
23529 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23530 unsigned int offset_in_dwz,
23531 dwarf2_per_objfile *per_objfile)
23532 {
23533 int low = dwarf2_find_containing_comp_unit
23534 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
23535 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
23536
23537 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23538 {
23539 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23540 error (_("Dwarf Error: could not find partial DIE containing "
23541 "offset %s [in module %s]"),
23542 sect_offset_str (sect_off),
23543 bfd_get_filename (per_objfile->objfile->obfd));
23544
23545 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23546 <= sect_off);
23547 return per_objfile->per_bfd->all_comp_units[low-1];
23548 }
23549 else
23550 {
23551 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
23552 && sect_off >= this_cu->sect_off + this_cu->length)
23553 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23554 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23555 return this_cu;
23556 }
23557 }
23558
23559 #if GDB_SELF_TEST
23560
23561 namespace selftests {
23562 namespace find_containing_comp_unit {
23563
23564 static void
23565 run_test ()
23566 {
23567 struct dwarf2_per_cu_data one {};
23568 struct dwarf2_per_cu_data two {};
23569 struct dwarf2_per_cu_data three {};
23570 struct dwarf2_per_cu_data four {};
23571
23572 one.length = 5;
23573 two.sect_off = sect_offset (one.length);
23574 two.length = 7;
23575
23576 three.length = 5;
23577 three.is_dwz = 1;
23578 four.sect_off = sect_offset (three.length);
23579 four.length = 7;
23580 four.is_dwz = 1;
23581
23582 std::vector<dwarf2_per_cu_data *> units;
23583 units.push_back (&one);
23584 units.push_back (&two);
23585 units.push_back (&three);
23586 units.push_back (&four);
23587
23588 int result;
23589
23590 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23591 SELF_CHECK (units[result] == &one);
23592 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23593 SELF_CHECK (units[result] == &one);
23594 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23595 SELF_CHECK (units[result] == &two);
23596
23597 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23598 SELF_CHECK (units[result] == &three);
23599 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23600 SELF_CHECK (units[result] == &three);
23601 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23602 SELF_CHECK (units[result] == &four);
23603 }
23604
23605 }
23606 }
23607
23608 #endif /* GDB_SELF_TEST */
23609
23610 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23611
23612 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23613 dwarf2_per_objfile *per_objfile)
23614 : per_cu (per_cu),
23615 per_objfile (per_objfile),
23616 mark (false),
23617 has_loclist (false),
23618 checked_producer (false),
23619 producer_is_gxx_lt_4_6 (false),
23620 producer_is_gcc_lt_4_3 (false),
23621 producer_is_icc (false),
23622 producer_is_icc_lt_14 (false),
23623 producer_is_codewarrior (false),
23624 processing_has_namespace_info (false)
23625 {
23626 }
23627
23628 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23629
23630 static void
23631 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23632 enum language pretend_language)
23633 {
23634 struct attribute *attr;
23635
23636 /* Set the language we're debugging. */
23637 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23638 if (attr != nullptr)
23639 set_cu_language (DW_UNSND (attr), cu);
23640 else
23641 {
23642 cu->language = pretend_language;
23643 cu->language_defn = language_def (cu->language);
23644 }
23645
23646 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23647 }
23648
23649 /* See read.h. */
23650
23651 dwarf2_cu *
23652 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23653 {
23654 auto it = m_dwarf2_cus.find (per_cu);
23655 if (it == m_dwarf2_cus.end ())
23656 return nullptr;
23657
23658 return it->second;
23659 }
23660
23661 /* See read.h. */
23662
23663 void
23664 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23665 {
23666 gdb_assert (this->get_cu (per_cu) == nullptr);
23667
23668 m_dwarf2_cus[per_cu] = cu;
23669 }
23670
23671 /* See read.h. */
23672
23673 void
23674 dwarf2_per_objfile::age_comp_units ()
23675 {
23676 /* Start by clearing all marks. */
23677 for (auto pair : m_dwarf2_cus)
23678 pair.second->mark = false;
23679
23680 /* Traverse all CUs, mark them and their dependencies if used recently
23681 enough. */
23682 for (auto pair : m_dwarf2_cus)
23683 {
23684 dwarf2_cu *cu = pair.second;
23685
23686 cu->last_used++;
23687 if (cu->last_used <= dwarf_max_cache_age)
23688 dwarf2_mark (cu);
23689 }
23690
23691 /* Delete all CUs still not marked. */
23692 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23693 {
23694 dwarf2_cu *cu = it->second;
23695
23696 if (!cu->mark)
23697 {
23698 delete cu;
23699 it = m_dwarf2_cus.erase (it);
23700 }
23701 else
23702 it++;
23703 }
23704 }
23705
23706 /* See read.h. */
23707
23708 void
23709 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23710 {
23711 auto it = m_dwarf2_cus.find (per_cu);
23712 if (it == m_dwarf2_cus.end ())
23713 return;
23714
23715 delete it->second;
23716
23717 m_dwarf2_cus.erase (it);
23718 }
23719
23720 dwarf2_per_objfile::~dwarf2_per_objfile ()
23721 {
23722 remove_all_cus ();
23723 }
23724
23725 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23726 We store these in a hash table separate from the DIEs, and preserve them
23727 when the DIEs are flushed out of cache.
23728
23729 The CU "per_cu" pointer is needed because offset alone is not enough to
23730 uniquely identify the type. A file may have multiple .debug_types sections,
23731 or the type may come from a DWO file. Furthermore, while it's more logical
23732 to use per_cu->section+offset, with Fission the section with the data is in
23733 the DWO file but we don't know that section at the point we need it.
23734 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23735 because we can enter the lookup routine, get_die_type_at_offset, from
23736 outside this file, and thus won't necessarily have PER_CU->cu.
23737 Fortunately, PER_CU is stable for the life of the objfile. */
23738
23739 struct dwarf2_per_cu_offset_and_type
23740 {
23741 const struct dwarf2_per_cu_data *per_cu;
23742 sect_offset sect_off;
23743 struct type *type;
23744 };
23745
23746 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23747
23748 static hashval_t
23749 per_cu_offset_and_type_hash (const void *item)
23750 {
23751 const struct dwarf2_per_cu_offset_and_type *ofs
23752 = (const struct dwarf2_per_cu_offset_and_type *) item;
23753
23754 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23755 }
23756
23757 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23758
23759 static int
23760 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23761 {
23762 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23763 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23764 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23765 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23766
23767 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23768 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23769 }
23770
23771 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23772 table if necessary. For convenience, return TYPE.
23773
23774 The DIEs reading must have careful ordering to:
23775 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23776 reading current DIE.
23777 * Not trying to dereference contents of still incompletely read in types
23778 while reading in other DIEs.
23779 * Enable referencing still incompletely read in types just by a pointer to
23780 the type without accessing its fields.
23781
23782 Therefore caller should follow these rules:
23783 * Try to fetch any prerequisite types we may need to build this DIE type
23784 before building the type and calling set_die_type.
23785 * After building type call set_die_type for current DIE as soon as
23786 possible before fetching more types to complete the current type.
23787 * Make the type as complete as possible before fetching more types. */
23788
23789 static struct type *
23790 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23791 {
23792 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23793 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23794 struct objfile *objfile = per_objfile->objfile;
23795 struct attribute *attr;
23796 struct dynamic_prop prop;
23797
23798 /* For Ada types, make sure that the gnat-specific data is always
23799 initialized (if not already set). There are a few types where
23800 we should not be doing so, because the type-specific area is
23801 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23802 where the type-specific area is used to store the floatformat).
23803 But this is not a problem, because the gnat-specific information
23804 is actually not needed for these types. */
23805 if (need_gnat_info (cu)
23806 && type->code () != TYPE_CODE_FUNC
23807 && type->code () != TYPE_CODE_FLT
23808 && type->code () != TYPE_CODE_METHODPTR
23809 && type->code () != TYPE_CODE_MEMBERPTR
23810 && type->code () != TYPE_CODE_METHOD
23811 && !HAVE_GNAT_AUX_INFO (type))
23812 INIT_GNAT_SPECIFIC (type);
23813
23814 /* Read DW_AT_allocated and set in type. */
23815 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23816 if (attr != NULL && attr->form_is_block ())
23817 {
23818 struct type *prop_type = cu->addr_sized_int_type (false);
23819 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23820 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23821 }
23822 else if (attr != NULL)
23823 {
23824 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23825 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23826 sect_offset_str (die->sect_off));
23827 }
23828
23829 /* Read DW_AT_associated and set in type. */
23830 attr = dwarf2_attr (die, DW_AT_associated, cu);
23831 if (attr != NULL && attr->form_is_block ())
23832 {
23833 struct type *prop_type = cu->addr_sized_int_type (false);
23834 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23835 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23836 }
23837 else if (attr != NULL)
23838 {
23839 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23840 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23841 sect_offset_str (die->sect_off));
23842 }
23843
23844 /* Read DW_AT_data_location and set in type. */
23845 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23846 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23847 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23848
23849 if (per_objfile->die_type_hash == NULL)
23850 per_objfile->die_type_hash
23851 = htab_up (htab_create_alloc (127,
23852 per_cu_offset_and_type_hash,
23853 per_cu_offset_and_type_eq,
23854 NULL, xcalloc, xfree));
23855
23856 ofs.per_cu = cu->per_cu;
23857 ofs.sect_off = die->sect_off;
23858 ofs.type = type;
23859 slot = (struct dwarf2_per_cu_offset_and_type **)
23860 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
23861 if (*slot)
23862 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23863 sect_offset_str (die->sect_off));
23864 *slot = XOBNEW (&objfile->objfile_obstack,
23865 struct dwarf2_per_cu_offset_and_type);
23866 **slot = ofs;
23867 return type;
23868 }
23869
23870 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23871 or return NULL if the die does not have a saved type. */
23872
23873 static struct type *
23874 get_die_type_at_offset (sect_offset sect_off,
23875 dwarf2_per_cu_data *per_cu,
23876 dwarf2_per_objfile *per_objfile)
23877 {
23878 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23879
23880 if (per_objfile->die_type_hash == NULL)
23881 return NULL;
23882
23883 ofs.per_cu = per_cu;
23884 ofs.sect_off = sect_off;
23885 slot = ((struct dwarf2_per_cu_offset_and_type *)
23886 htab_find (per_objfile->die_type_hash.get (), &ofs));
23887 if (slot)
23888 return slot->type;
23889 else
23890 return NULL;
23891 }
23892
23893 /* Look up the type for DIE in CU in die_type_hash,
23894 or return NULL if DIE does not have a saved type. */
23895
23896 static struct type *
23897 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23898 {
23899 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23900 }
23901
23902 /* Add a dependence relationship from CU to REF_PER_CU. */
23903
23904 static void
23905 dwarf2_add_dependence (struct dwarf2_cu *cu,
23906 struct dwarf2_per_cu_data *ref_per_cu)
23907 {
23908 void **slot;
23909
23910 if (cu->dependencies == NULL)
23911 cu->dependencies
23912 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23913 NULL, &cu->comp_unit_obstack,
23914 hashtab_obstack_allocate,
23915 dummy_obstack_deallocate);
23916
23917 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23918 if (*slot == NULL)
23919 *slot = ref_per_cu;
23920 }
23921
23922 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23923 Set the mark field in every compilation unit in the
23924 cache that we must keep because we are keeping CU.
23925
23926 DATA is the dwarf2_per_objfile object in which to look up CUs. */
23927
23928 static int
23929 dwarf2_mark_helper (void **slot, void *data)
23930 {
23931 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23932 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23933 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23934
23935 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23936 reading of the chain. As such dependencies remain valid it is not much
23937 useful to track and undo them during QUIT cleanups. */
23938 if (cu == nullptr)
23939 return 1;
23940
23941 if (cu->mark)
23942 return 1;
23943
23944 cu->mark = true;
23945
23946 if (cu->dependencies != nullptr)
23947 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
23948
23949 return 1;
23950 }
23951
23952 /* Set the mark field in CU and in every other compilation unit in the
23953 cache that we must keep because we are keeping CU. */
23954
23955 static void
23956 dwarf2_mark (struct dwarf2_cu *cu)
23957 {
23958 if (cu->mark)
23959 return;
23960
23961 cu->mark = true;
23962
23963 if (cu->dependencies != nullptr)
23964 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
23965 }
23966
23967 /* Trivial hash function for partial_die_info: the hash value of a DIE
23968 is its offset in .debug_info for this objfile. */
23969
23970 static hashval_t
23971 partial_die_hash (const void *item)
23972 {
23973 const struct partial_die_info *part_die
23974 = (const struct partial_die_info *) item;
23975
23976 return to_underlying (part_die->sect_off);
23977 }
23978
23979 /* Trivial comparison function for partial_die_info structures: two DIEs
23980 are equal if they have the same offset. */
23981
23982 static int
23983 partial_die_eq (const void *item_lhs, const void *item_rhs)
23984 {
23985 const struct partial_die_info *part_die_lhs
23986 = (const struct partial_die_info *) item_lhs;
23987 const struct partial_die_info *part_die_rhs
23988 = (const struct partial_die_info *) item_rhs;
23989
23990 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23991 }
23992
23993 struct cmd_list_element *set_dwarf_cmdlist;
23994 struct cmd_list_element *show_dwarf_cmdlist;
23995
23996 static void
23997 show_check_physname (struct ui_file *file, int from_tty,
23998 struct cmd_list_element *c, const char *value)
23999 {
24000 fprintf_filtered (file,
24001 _("Whether to check \"physname\" is %s.\n"),
24002 value);
24003 }
24004
24005 void _initialize_dwarf2_read ();
24006 void
24007 _initialize_dwarf2_read ()
24008 {
24009 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24010 Set DWARF specific variables.\n\
24011 Configure DWARF variables such as the cache size."),
24012 &set_dwarf_cmdlist, "maintenance set dwarf ",
24013 0/*allow-unknown*/, &maintenance_set_cmdlist);
24014
24015 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24016 Show DWARF specific variables.\n\
24017 Show DWARF variables such as the cache size."),
24018 &show_dwarf_cmdlist, "maintenance show dwarf ",
24019 0/*allow-unknown*/, &maintenance_show_cmdlist);
24020
24021 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24022 &dwarf_max_cache_age, _("\
24023 Set the upper bound on the age of cached DWARF compilation units."), _("\
24024 Show the upper bound on the age of cached DWARF compilation units."), _("\
24025 A higher limit means that cached compilation units will be stored\n\
24026 in memory longer, and more total memory will be used. Zero disables\n\
24027 caching, which can slow down startup."),
24028 NULL,
24029 show_dwarf_max_cache_age,
24030 &set_dwarf_cmdlist,
24031 &show_dwarf_cmdlist);
24032
24033 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24034 Set debugging of the DWARF reader."), _("\
24035 Show debugging of the DWARF reader."), _("\
24036 When enabled (non-zero), debugging messages are printed during DWARF\n\
24037 reading and symtab expansion. A value of 1 (one) provides basic\n\
24038 information. A value greater than 1 provides more verbose information."),
24039 NULL,
24040 NULL,
24041 &setdebuglist, &showdebuglist);
24042
24043 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24044 Set debugging of the DWARF DIE reader."), _("\
24045 Show debugging of the DWARF DIE reader."), _("\
24046 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24047 The value is the maximum depth to print."),
24048 NULL,
24049 NULL,
24050 &setdebuglist, &showdebuglist);
24051
24052 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24053 Set debugging of the dwarf line reader."), _("\
24054 Show debugging of the dwarf line reader."), _("\
24055 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24056 A value of 1 (one) provides basic information.\n\
24057 A value greater than 1 provides more verbose information."),
24058 NULL,
24059 NULL,
24060 &setdebuglist, &showdebuglist);
24061
24062 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24063 Set cross-checking of \"physname\" code against demangler."), _("\
24064 Show cross-checking of \"physname\" code against demangler."), _("\
24065 When enabled, GDB's internal \"physname\" code is checked against\n\
24066 the demangler."),
24067 NULL, show_check_physname,
24068 &setdebuglist, &showdebuglist);
24069
24070 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24071 no_class, &use_deprecated_index_sections, _("\
24072 Set whether to use deprecated gdb_index sections."), _("\
24073 Show whether to use deprecated gdb_index sections."), _("\
24074 When enabled, deprecated .gdb_index sections are used anyway.\n\
24075 Normally they are ignored either because of a missing feature or\n\
24076 performance issue.\n\
24077 Warning: This option must be enabled before gdb reads the file."),
24078 NULL,
24079 NULL,
24080 &setlist, &showlist);
24081
24082 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24083 &dwarf2_locexpr_funcs);
24084 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24085 &dwarf2_loclist_funcs);
24086
24087 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24088 &dwarf2_block_frame_base_locexpr_funcs);
24089 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24090 &dwarf2_block_frame_base_loclist_funcs);
24091
24092 #if GDB_SELF_TEST
24093 selftests::register_test ("dw2_expand_symtabs_matching",
24094 selftests::dw2_expand_symtabs_matching::run_test);
24095 selftests::register_test ("dwarf2_find_containing_comp_unit",
24096 selftests::find_containing_comp_unit::run_test);
24097 #endif
24098 }
This page took 0.544131 seconds and 4 git commands to generate.