]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
4724738363be70b296cad1ff850d6717b4ff957b
[thirdparty/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 break;
8150 case DW_TAG_constant:
8151 case DW_TAG_variable:
8152 case DW_TAG_typedef:
8153 case DW_TAG_union_type:
8154 if (!pdi->is_declaration
8155 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8156 {
8157 add_partial_symbol (pdi, cu);
8158 }
8159 break;
8160 case DW_TAG_class_type:
8161 case DW_TAG_interface_type:
8162 case DW_TAG_structure_type:
8163 if (!pdi->is_declaration)
8164 {
8165 add_partial_symbol (pdi, cu);
8166 }
8167 if ((cu->language == language_rust
8168 || cu->language == language_cplus) && pdi->has_children)
8169 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8170 set_addrmap, cu);
8171 break;
8172 case DW_TAG_enumeration_type:
8173 if (!pdi->is_declaration)
8174 add_partial_enumeration (pdi, cu);
8175 break;
8176 case DW_TAG_base_type:
8177 case DW_TAG_subrange_type:
8178 /* File scope base type definitions are added to the partial
8179 symbol table. */
8180 add_partial_symbol (pdi, cu);
8181 break;
8182 case DW_TAG_namespace:
8183 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8184 break;
8185 case DW_TAG_module:
8186 if (!pdi->is_declaration)
8187 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8188 break;
8189 case DW_TAG_imported_unit:
8190 {
8191 struct dwarf2_per_cu_data *per_cu;
8192
8193 /* For now we don't handle imported units in type units. */
8194 if (cu->per_cu->is_debug_types)
8195 {
8196 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8197 " supported in type units [in module %s]"),
8198 objfile_name (cu->per_objfile->objfile));
8199 }
8200
8201 per_cu = dwarf2_find_containing_comp_unit
8202 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8203
8204 /* Go read the partial unit, if needed. */
8205 if (per_cu->v.psymtab == NULL)
8206 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8207 cu->language);
8208
8209 cu->per_cu->imported_symtabs_push (per_cu);
8210 }
8211 break;
8212 case DW_TAG_imported_declaration:
8213 add_partial_symbol (pdi, cu);
8214 break;
8215 default:
8216 break;
8217 }
8218 }
8219
8220 /* If the die has a sibling, skip to the sibling. */
8221
8222 pdi = pdi->die_sibling;
8223 }
8224 }
8225
8226 /* Functions used to compute the fully scoped name of a partial DIE.
8227
8228 Normally, this is simple. For C++, the parent DIE's fully scoped
8229 name is concatenated with "::" and the partial DIE's name.
8230 Enumerators are an exception; they use the scope of their parent
8231 enumeration type, i.e. the name of the enumeration type is not
8232 prepended to the enumerator.
8233
8234 There are two complexities. One is DW_AT_specification; in this
8235 case "parent" means the parent of the target of the specification,
8236 instead of the direct parent of the DIE. The other is compilers
8237 which do not emit DW_TAG_namespace; in this case we try to guess
8238 the fully qualified name of structure types from their members'
8239 linkage names. This must be done using the DIE's children rather
8240 than the children of any DW_AT_specification target. We only need
8241 to do this for structures at the top level, i.e. if the target of
8242 any DW_AT_specification (if any; otherwise the DIE itself) does not
8243 have a parent. */
8244
8245 /* Compute the scope prefix associated with PDI's parent, in
8246 compilation unit CU. The result will be allocated on CU's
8247 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8248 field. NULL is returned if no prefix is necessary. */
8249 static const char *
8250 partial_die_parent_scope (struct partial_die_info *pdi,
8251 struct dwarf2_cu *cu)
8252 {
8253 const char *grandparent_scope;
8254 struct partial_die_info *parent, *real_pdi;
8255
8256 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8257 then this means the parent of the specification DIE. */
8258
8259 real_pdi = pdi;
8260 while (real_pdi->has_specification)
8261 {
8262 auto res = find_partial_die (real_pdi->spec_offset,
8263 real_pdi->spec_is_dwz, cu);
8264 real_pdi = res.pdi;
8265 cu = res.cu;
8266 }
8267
8268 parent = real_pdi->die_parent;
8269 if (parent == NULL)
8270 return NULL;
8271
8272 if (parent->scope_set)
8273 return parent->scope;
8274
8275 parent->fixup (cu);
8276
8277 grandparent_scope = partial_die_parent_scope (parent, cu);
8278
8279 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8280 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8281 Work around this problem here. */
8282 if (cu->language == language_cplus
8283 && parent->tag == DW_TAG_namespace
8284 && strcmp (parent->name (cu), "::") == 0
8285 && grandparent_scope == NULL)
8286 {
8287 parent->scope = NULL;
8288 parent->scope_set = 1;
8289 return NULL;
8290 }
8291
8292 /* Nested subroutines in Fortran get a prefix. */
8293 if (pdi->tag == DW_TAG_enumerator)
8294 /* Enumerators should not get the name of the enumeration as a prefix. */
8295 parent->scope = grandparent_scope;
8296 else if (parent->tag == DW_TAG_namespace
8297 || parent->tag == DW_TAG_module
8298 || parent->tag == DW_TAG_structure_type
8299 || parent->tag == DW_TAG_class_type
8300 || parent->tag == DW_TAG_interface_type
8301 || parent->tag == DW_TAG_union_type
8302 || parent->tag == DW_TAG_enumeration_type
8303 || (cu->language == language_fortran
8304 && parent->tag == DW_TAG_subprogram
8305 && pdi->tag == DW_TAG_subprogram))
8306 {
8307 if (grandparent_scope == NULL)
8308 parent->scope = parent->name (cu);
8309 else
8310 parent->scope = typename_concat (&cu->comp_unit_obstack,
8311 grandparent_scope,
8312 parent->name (cu), 0, cu);
8313 }
8314 else
8315 {
8316 /* FIXME drow/2004-04-01: What should we be doing with
8317 function-local names? For partial symbols, we should probably be
8318 ignoring them. */
8319 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8320 dwarf_tag_name (parent->tag),
8321 sect_offset_str (pdi->sect_off));
8322 parent->scope = grandparent_scope;
8323 }
8324
8325 parent->scope_set = 1;
8326 return parent->scope;
8327 }
8328
8329 /* Return the fully scoped name associated with PDI, from compilation unit
8330 CU. The result will be allocated with malloc. */
8331
8332 static gdb::unique_xmalloc_ptr<char>
8333 partial_die_full_name (struct partial_die_info *pdi,
8334 struct dwarf2_cu *cu)
8335 {
8336 const char *parent_scope;
8337
8338 /* If this is a template instantiation, we can not work out the
8339 template arguments from partial DIEs. So, unfortunately, we have
8340 to go through the full DIEs. At least any work we do building
8341 types here will be reused if full symbols are loaded later. */
8342 if (pdi->has_template_arguments)
8343 {
8344 pdi->fixup (cu);
8345
8346 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8347 {
8348 struct die_info *die;
8349 struct attribute attr;
8350 struct dwarf2_cu *ref_cu = cu;
8351
8352 /* DW_FORM_ref_addr is using section offset. */
8353 attr.name = (enum dwarf_attribute) 0;
8354 attr.form = DW_FORM_ref_addr;
8355 attr.u.unsnd = to_underlying (pdi->sect_off);
8356 die = follow_die_ref (NULL, &attr, &ref_cu);
8357
8358 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8359 }
8360 }
8361
8362 parent_scope = partial_die_parent_scope (pdi, cu);
8363 if (parent_scope == NULL)
8364 return NULL;
8365 else
8366 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8367 pdi->name (cu),
8368 0, cu));
8369 }
8370
8371 static void
8372 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8373 {
8374 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8375 struct objfile *objfile = per_objfile->objfile;
8376 struct gdbarch *gdbarch = objfile->arch ();
8377 CORE_ADDR addr = 0;
8378 const char *actual_name = NULL;
8379 CORE_ADDR baseaddr;
8380
8381 baseaddr = objfile->text_section_offset ();
8382
8383 gdb::unique_xmalloc_ptr<char> built_actual_name
8384 = partial_die_full_name (pdi, cu);
8385 if (built_actual_name != NULL)
8386 actual_name = built_actual_name.get ();
8387
8388 if (actual_name == NULL)
8389 actual_name = pdi->name (cu);
8390
8391 partial_symbol psymbol;
8392 memset (&psymbol, 0, sizeof (psymbol));
8393 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8394 psymbol.ginfo.section = -1;
8395
8396 /* The code below indicates that the psymbol should be installed by
8397 setting this. */
8398 gdb::optional<psymbol_placement> where;
8399
8400 switch (pdi->tag)
8401 {
8402 case DW_TAG_inlined_subroutine:
8403 case DW_TAG_subprogram:
8404 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8405 - baseaddr);
8406 if (pdi->is_external
8407 || cu->language == language_ada
8408 || (cu->language == language_fortran
8409 && pdi->die_parent != NULL
8410 && pdi->die_parent->tag == DW_TAG_subprogram))
8411 {
8412 /* Normally, only "external" DIEs are part of the global scope.
8413 But in Ada and Fortran, we want to be able to access nested
8414 procedures globally. So all Ada and Fortran subprograms are
8415 stored in the global scope. */
8416 where = psymbol_placement::GLOBAL;
8417 }
8418 else
8419 where = psymbol_placement::STATIC;
8420
8421 psymbol.domain = VAR_DOMAIN;
8422 psymbol.aclass = LOC_BLOCK;
8423 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8424 psymbol.ginfo.value.address = addr;
8425
8426 if (pdi->main_subprogram && actual_name != NULL)
8427 set_objfile_main_name (objfile, actual_name, cu->language);
8428 break;
8429 case DW_TAG_constant:
8430 psymbol.domain = VAR_DOMAIN;
8431 psymbol.aclass = LOC_STATIC;
8432 where = (pdi->is_external
8433 ? psymbol_placement::GLOBAL
8434 : psymbol_placement::STATIC);
8435 break;
8436 case DW_TAG_variable:
8437 if (pdi->d.locdesc)
8438 addr = decode_locdesc (pdi->d.locdesc, cu);
8439
8440 if (pdi->d.locdesc
8441 && addr == 0
8442 && !per_objfile->per_bfd->has_section_at_zero)
8443 {
8444 /* A global or static variable may also have been stripped
8445 out by the linker if unused, in which case its address
8446 will be nullified; do not add such variables into partial
8447 symbol table then. */
8448 }
8449 else if (pdi->is_external)
8450 {
8451 /* Global Variable.
8452 Don't enter into the minimal symbol tables as there is
8453 a minimal symbol table entry from the ELF symbols already.
8454 Enter into partial symbol table if it has a location
8455 descriptor or a type.
8456 If the location descriptor is missing, new_symbol will create
8457 a LOC_UNRESOLVED symbol, the address of the variable will then
8458 be determined from the minimal symbol table whenever the variable
8459 is referenced.
8460 The address for the partial symbol table entry is not
8461 used by GDB, but it comes in handy for debugging partial symbol
8462 table building. */
8463
8464 if (pdi->d.locdesc || pdi->has_type)
8465 {
8466 psymbol.domain = VAR_DOMAIN;
8467 psymbol.aclass = LOC_STATIC;
8468 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8469 psymbol.ginfo.value.address = addr;
8470 where = psymbol_placement::GLOBAL;
8471 }
8472 }
8473 else
8474 {
8475 int has_loc = pdi->d.locdesc != NULL;
8476
8477 /* Static Variable. Skip symbols whose value we cannot know (those
8478 without location descriptors or constant values). */
8479 if (!has_loc && !pdi->has_const_value)
8480 return;
8481
8482 psymbol.domain = VAR_DOMAIN;
8483 psymbol.aclass = LOC_STATIC;
8484 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8485 if (has_loc)
8486 psymbol.ginfo.value.address = addr;
8487 where = psymbol_placement::STATIC;
8488 }
8489 break;
8490 case DW_TAG_typedef:
8491 case DW_TAG_base_type:
8492 case DW_TAG_subrange_type:
8493 psymbol.domain = VAR_DOMAIN;
8494 psymbol.aclass = LOC_TYPEDEF;
8495 where = psymbol_placement::STATIC;
8496 break;
8497 case DW_TAG_imported_declaration:
8498 case DW_TAG_namespace:
8499 psymbol.domain = VAR_DOMAIN;
8500 psymbol.aclass = LOC_TYPEDEF;
8501 where = psymbol_placement::GLOBAL;
8502 break;
8503 case DW_TAG_module:
8504 /* With Fortran 77 there might be a "BLOCK DATA" module
8505 available without any name. If so, we skip the module as it
8506 doesn't bring any value. */
8507 if (actual_name != nullptr)
8508 {
8509 psymbol.domain = MODULE_DOMAIN;
8510 psymbol.aclass = LOC_TYPEDEF;
8511 where = psymbol_placement::GLOBAL;
8512 }
8513 break;
8514 case DW_TAG_class_type:
8515 case DW_TAG_interface_type:
8516 case DW_TAG_structure_type:
8517 case DW_TAG_union_type:
8518 case DW_TAG_enumeration_type:
8519 /* Skip external references. The DWARF standard says in the section
8520 about "Structure, Union, and Class Type Entries": "An incomplete
8521 structure, union or class type is represented by a structure,
8522 union or class entry that does not have a byte size attribute
8523 and that has a DW_AT_declaration attribute." */
8524 if (!pdi->has_byte_size && pdi->is_declaration)
8525 return;
8526
8527 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8528 static vs. global. */
8529 psymbol.domain = STRUCT_DOMAIN;
8530 psymbol.aclass = LOC_TYPEDEF;
8531 where = (cu->language == language_cplus
8532 ? psymbol_placement::GLOBAL
8533 : psymbol_placement::STATIC);
8534 break;
8535 case DW_TAG_enumerator:
8536 psymbol.domain = VAR_DOMAIN;
8537 psymbol.aclass = LOC_CONST;
8538 where = (cu->language == language_cplus
8539 ? psymbol_placement::GLOBAL
8540 : psymbol_placement::STATIC);
8541 break;
8542 default:
8543 break;
8544 }
8545
8546 if (where.has_value ())
8547 {
8548 if (built_actual_name != nullptr)
8549 actual_name = objfile->intern (actual_name);
8550 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8551 psymbol.ginfo.set_linkage_name (actual_name);
8552 else
8553 {
8554 psymbol.ginfo.set_demangled_name (actual_name,
8555 &objfile->objfile_obstack);
8556 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8557 }
8558 add_psymbol_to_list (psymbol, *where, objfile);
8559 }
8560 }
8561
8562 /* Read a partial die corresponding to a namespace; also, add a symbol
8563 corresponding to that namespace to the symbol table. NAMESPACE is
8564 the name of the enclosing namespace. */
8565
8566 static void
8567 add_partial_namespace (struct partial_die_info *pdi,
8568 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8569 int set_addrmap, struct dwarf2_cu *cu)
8570 {
8571 /* Add a symbol for the namespace. */
8572
8573 add_partial_symbol (pdi, cu);
8574
8575 /* Now scan partial symbols in that namespace. */
8576
8577 if (pdi->has_children)
8578 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8579 }
8580
8581 /* Read a partial die corresponding to a Fortran module. */
8582
8583 static void
8584 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8585 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8586 {
8587 /* Add a symbol for the namespace. */
8588
8589 add_partial_symbol (pdi, cu);
8590
8591 /* Now scan partial symbols in that module. */
8592
8593 if (pdi->has_children)
8594 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8595 }
8596
8597 /* Read a partial die corresponding to a subprogram or an inlined
8598 subprogram and create a partial symbol for that subprogram.
8599 When the CU language allows it, this routine also defines a partial
8600 symbol for each nested subprogram that this subprogram contains.
8601 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8602 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8603
8604 PDI may also be a lexical block, in which case we simply search
8605 recursively for subprograms defined inside that lexical block.
8606 Again, this is only performed when the CU language allows this
8607 type of definitions. */
8608
8609 static void
8610 add_partial_subprogram (struct partial_die_info *pdi,
8611 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8612 int set_addrmap, struct dwarf2_cu *cu)
8613 {
8614 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8615 {
8616 if (pdi->has_pc_info)
8617 {
8618 if (pdi->lowpc < *lowpc)
8619 *lowpc = pdi->lowpc;
8620 if (pdi->highpc > *highpc)
8621 *highpc = pdi->highpc;
8622 if (set_addrmap)
8623 {
8624 struct objfile *objfile = cu->per_objfile->objfile;
8625 struct gdbarch *gdbarch = objfile->arch ();
8626 CORE_ADDR baseaddr;
8627 CORE_ADDR this_highpc;
8628 CORE_ADDR this_lowpc;
8629
8630 baseaddr = objfile->text_section_offset ();
8631 this_lowpc
8632 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8633 pdi->lowpc + baseaddr)
8634 - baseaddr);
8635 this_highpc
8636 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8637 pdi->highpc + baseaddr)
8638 - baseaddr);
8639 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8640 this_lowpc, this_highpc - 1,
8641 cu->per_cu->v.psymtab);
8642 }
8643 }
8644
8645 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8646 {
8647 if (!pdi->is_declaration)
8648 /* Ignore subprogram DIEs that do not have a name, they are
8649 illegal. Do not emit a complaint at this point, we will
8650 do so when we convert this psymtab into a symtab. */
8651 if (pdi->name (cu))
8652 add_partial_symbol (pdi, cu);
8653 }
8654 }
8655
8656 if (! pdi->has_children)
8657 return;
8658
8659 if (cu->language == language_ada || cu->language == language_fortran)
8660 {
8661 pdi = pdi->die_child;
8662 while (pdi != NULL)
8663 {
8664 pdi->fixup (cu);
8665 if (pdi->tag == DW_TAG_subprogram
8666 || pdi->tag == DW_TAG_inlined_subroutine
8667 || pdi->tag == DW_TAG_lexical_block)
8668 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8669 pdi = pdi->die_sibling;
8670 }
8671 }
8672 }
8673
8674 /* Read a partial die corresponding to an enumeration type. */
8675
8676 static void
8677 add_partial_enumeration (struct partial_die_info *enum_pdi,
8678 struct dwarf2_cu *cu)
8679 {
8680 struct partial_die_info *pdi;
8681
8682 if (enum_pdi->name (cu) != NULL)
8683 add_partial_symbol (enum_pdi, cu);
8684
8685 pdi = enum_pdi->die_child;
8686 while (pdi)
8687 {
8688 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8689 complaint (_("malformed enumerator DIE ignored"));
8690 else
8691 add_partial_symbol (pdi, cu);
8692 pdi = pdi->die_sibling;
8693 }
8694 }
8695
8696 /* Return the initial uleb128 in the die at INFO_PTR. */
8697
8698 static unsigned int
8699 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8700 {
8701 unsigned int bytes_read;
8702
8703 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8704 }
8705
8706 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8707 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8708
8709 Return the corresponding abbrev, or NULL if the number is zero (indicating
8710 an empty DIE). In either case *BYTES_READ will be set to the length of
8711 the initial number. */
8712
8713 static struct abbrev_info *
8714 peek_die_abbrev (const die_reader_specs &reader,
8715 const gdb_byte *info_ptr, unsigned int *bytes_read)
8716 {
8717 dwarf2_cu *cu = reader.cu;
8718 bfd *abfd = cu->per_objfile->objfile->obfd;
8719 unsigned int abbrev_number
8720 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8721
8722 if (abbrev_number == 0)
8723 return NULL;
8724
8725 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8726 if (!abbrev)
8727 {
8728 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8729 " at offset %s [in module %s]"),
8730 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8731 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8732 }
8733
8734 return abbrev;
8735 }
8736
8737 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8738 Returns a pointer to the end of a series of DIEs, terminated by an empty
8739 DIE. Any children of the skipped DIEs will also be skipped. */
8740
8741 static const gdb_byte *
8742 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8743 {
8744 while (1)
8745 {
8746 unsigned int bytes_read;
8747 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8748
8749 if (abbrev == NULL)
8750 return info_ptr + bytes_read;
8751 else
8752 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8753 }
8754 }
8755
8756 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8757 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8758 abbrev corresponding to that skipped uleb128 should be passed in
8759 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8760 children. */
8761
8762 static const gdb_byte *
8763 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8764 struct abbrev_info *abbrev)
8765 {
8766 unsigned int bytes_read;
8767 struct attribute attr;
8768 bfd *abfd = reader->abfd;
8769 struct dwarf2_cu *cu = reader->cu;
8770 const gdb_byte *buffer = reader->buffer;
8771 const gdb_byte *buffer_end = reader->buffer_end;
8772 unsigned int form, i;
8773
8774 for (i = 0; i < abbrev->num_attrs; i++)
8775 {
8776 /* The only abbrev we care about is DW_AT_sibling. */
8777 if (abbrev->attrs[i].name == DW_AT_sibling)
8778 {
8779 bool ignored;
8780 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8781 &ignored);
8782 if (attr.form == DW_FORM_ref_addr)
8783 complaint (_("ignoring absolute DW_AT_sibling"));
8784 else
8785 {
8786 sect_offset off = attr.get_ref_die_offset ();
8787 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8788
8789 if (sibling_ptr < info_ptr)
8790 complaint (_("DW_AT_sibling points backwards"));
8791 else if (sibling_ptr > reader->buffer_end)
8792 reader->die_section->overflow_complaint ();
8793 else
8794 return sibling_ptr;
8795 }
8796 }
8797
8798 /* If it isn't DW_AT_sibling, skip this attribute. */
8799 form = abbrev->attrs[i].form;
8800 skip_attribute:
8801 switch (form)
8802 {
8803 case DW_FORM_ref_addr:
8804 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8805 and later it is offset sized. */
8806 if (cu->header.version == 2)
8807 info_ptr += cu->header.addr_size;
8808 else
8809 info_ptr += cu->header.offset_size;
8810 break;
8811 case DW_FORM_GNU_ref_alt:
8812 info_ptr += cu->header.offset_size;
8813 break;
8814 case DW_FORM_addr:
8815 info_ptr += cu->header.addr_size;
8816 break;
8817 case DW_FORM_data1:
8818 case DW_FORM_ref1:
8819 case DW_FORM_flag:
8820 case DW_FORM_strx1:
8821 info_ptr += 1;
8822 break;
8823 case DW_FORM_flag_present:
8824 case DW_FORM_implicit_const:
8825 break;
8826 case DW_FORM_data2:
8827 case DW_FORM_ref2:
8828 case DW_FORM_strx2:
8829 info_ptr += 2;
8830 break;
8831 case DW_FORM_strx3:
8832 info_ptr += 3;
8833 break;
8834 case DW_FORM_data4:
8835 case DW_FORM_ref4:
8836 case DW_FORM_strx4:
8837 info_ptr += 4;
8838 break;
8839 case DW_FORM_data8:
8840 case DW_FORM_ref8:
8841 case DW_FORM_ref_sig8:
8842 info_ptr += 8;
8843 break;
8844 case DW_FORM_data16:
8845 info_ptr += 16;
8846 break;
8847 case DW_FORM_string:
8848 read_direct_string (abfd, info_ptr, &bytes_read);
8849 info_ptr += bytes_read;
8850 break;
8851 case DW_FORM_sec_offset:
8852 case DW_FORM_strp:
8853 case DW_FORM_GNU_strp_alt:
8854 info_ptr += cu->header.offset_size;
8855 break;
8856 case DW_FORM_exprloc:
8857 case DW_FORM_block:
8858 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8859 info_ptr += bytes_read;
8860 break;
8861 case DW_FORM_block1:
8862 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8863 break;
8864 case DW_FORM_block2:
8865 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8866 break;
8867 case DW_FORM_block4:
8868 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8869 break;
8870 case DW_FORM_addrx:
8871 case DW_FORM_strx:
8872 case DW_FORM_sdata:
8873 case DW_FORM_udata:
8874 case DW_FORM_ref_udata:
8875 case DW_FORM_GNU_addr_index:
8876 case DW_FORM_GNU_str_index:
8877 case DW_FORM_rnglistx:
8878 case DW_FORM_loclistx:
8879 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8880 break;
8881 case DW_FORM_indirect:
8882 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8883 info_ptr += bytes_read;
8884 /* We need to continue parsing from here, so just go back to
8885 the top. */
8886 goto skip_attribute;
8887
8888 default:
8889 error (_("Dwarf Error: Cannot handle %s "
8890 "in DWARF reader [in module %s]"),
8891 dwarf_form_name (form),
8892 bfd_get_filename (abfd));
8893 }
8894 }
8895
8896 if (abbrev->has_children)
8897 return skip_children (reader, info_ptr);
8898 else
8899 return info_ptr;
8900 }
8901
8902 /* Locate ORIG_PDI's sibling.
8903 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8904
8905 static const gdb_byte *
8906 locate_pdi_sibling (const struct die_reader_specs *reader,
8907 struct partial_die_info *orig_pdi,
8908 const gdb_byte *info_ptr)
8909 {
8910 /* Do we know the sibling already? */
8911
8912 if (orig_pdi->sibling)
8913 return orig_pdi->sibling;
8914
8915 /* Are there any children to deal with? */
8916
8917 if (!orig_pdi->has_children)
8918 return info_ptr;
8919
8920 /* Skip the children the long way. */
8921
8922 return skip_children (reader, info_ptr);
8923 }
8924
8925 /* Expand this partial symbol table into a full symbol table. SELF is
8926 not NULL. */
8927
8928 void
8929 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8930 {
8931 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8932
8933 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8934
8935 /* If this psymtab is constructed from a debug-only objfile, the
8936 has_section_at_zero flag will not necessarily be correct. We
8937 can get the correct value for this flag by looking at the data
8938 associated with the (presumably stripped) associated objfile. */
8939 if (objfile->separate_debug_objfile_backlink)
8940 {
8941 dwarf2_per_objfile *per_objfile_backlink
8942 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8943
8944 per_objfile->per_bfd->has_section_at_zero
8945 = per_objfile_backlink->per_bfd->has_section_at_zero;
8946 }
8947
8948 expand_psymtab (objfile);
8949
8950 process_cu_includes (per_objfile);
8951 }
8952 \f
8953 /* Reading in full CUs. */
8954
8955 /* Add PER_CU to the queue. */
8956
8957 static void
8958 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8959 dwarf2_per_objfile *per_objfile,
8960 enum language pretend_language)
8961 {
8962 per_cu->queued = 1;
8963 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
8964 }
8965
8966 /* If PER_CU is not yet queued, add it to the queue.
8967 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8968 dependency.
8969 The result is non-zero if PER_CU was queued, otherwise the result is zero
8970 meaning either PER_CU is already queued or it is already loaded.
8971
8972 N.B. There is an invariant here that if a CU is queued then it is loaded.
8973 The caller is required to load PER_CU if we return non-zero. */
8974
8975 static int
8976 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8977 dwarf2_per_cu_data *per_cu,
8978 dwarf2_per_objfile *per_objfile,
8979 enum language pretend_language)
8980 {
8981 /* We may arrive here during partial symbol reading, if we need full
8982 DIEs to process an unusual case (e.g. template arguments). Do
8983 not queue PER_CU, just tell our caller to load its DIEs. */
8984 if (per_cu->per_bfd->reading_partial_symbols)
8985 {
8986 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8987
8988 if (cu == NULL || cu->dies == NULL)
8989 return 1;
8990 return 0;
8991 }
8992
8993 /* Mark the dependence relation so that we don't flush PER_CU
8994 too early. */
8995 if (dependent_cu != NULL)
8996 dwarf2_add_dependence (dependent_cu, per_cu);
8997
8998 /* If it's already on the queue, we have nothing to do. */
8999 if (per_cu->queued)
9000 return 0;
9001
9002 /* If the compilation unit is already loaded, just mark it as
9003 used. */
9004 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9005 if (cu != nullptr)
9006 {
9007 cu->last_used = 0;
9008 return 0;
9009 }
9010
9011 /* Add it to the queue. */
9012 queue_comp_unit (per_cu, per_objfile, pretend_language);
9013
9014 return 1;
9015 }
9016
9017 /* Process the queue. */
9018
9019 static void
9020 process_queue (dwarf2_per_objfile *per_objfile)
9021 {
9022 if (dwarf_read_debug)
9023 {
9024 fprintf_unfiltered (gdb_stdlog,
9025 "Expanding one or more symtabs of objfile %s ...\n",
9026 objfile_name (per_objfile->objfile));
9027 }
9028
9029 /* The queue starts out with one item, but following a DIE reference
9030 may load a new CU, adding it to the end of the queue. */
9031 while (!per_objfile->per_bfd->queue.empty ())
9032 {
9033 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
9034 dwarf2_per_cu_data *per_cu = item.per_cu;
9035
9036 if (!per_objfile->symtab_set_p (per_cu))
9037 {
9038 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9039
9040 /* Skip dummy CUs. */
9041 if (cu != nullptr)
9042 {
9043 unsigned int debug_print_threshold;
9044 char buf[100];
9045
9046 if (per_cu->is_debug_types)
9047 {
9048 struct signatured_type *sig_type =
9049 (struct signatured_type *) per_cu;
9050
9051 sprintf (buf, "TU %s at offset %s",
9052 hex_string (sig_type->signature),
9053 sect_offset_str (per_cu->sect_off));
9054 /* There can be 100s of TUs.
9055 Only print them in verbose mode. */
9056 debug_print_threshold = 2;
9057 }
9058 else
9059 {
9060 sprintf (buf, "CU at offset %s",
9061 sect_offset_str (per_cu->sect_off));
9062 debug_print_threshold = 1;
9063 }
9064
9065 if (dwarf_read_debug >= debug_print_threshold)
9066 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9067
9068 if (per_cu->is_debug_types)
9069 process_full_type_unit (cu, item.pretend_language);
9070 else
9071 process_full_comp_unit (cu, item.pretend_language);
9072
9073 if (dwarf_read_debug >= debug_print_threshold)
9074 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9075 }
9076 }
9077
9078 per_cu->queued = 0;
9079 per_objfile->per_bfd->queue.pop ();
9080 }
9081
9082 if (dwarf_read_debug)
9083 {
9084 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9085 objfile_name (per_objfile->objfile));
9086 }
9087 }
9088
9089 /* Read in full symbols for PST, and anything it depends on. */
9090
9091 void
9092 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9093 {
9094 gdb_assert (!readin_p (objfile));
9095
9096 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9097 free_cached_comp_units freer (per_objfile);
9098 expand_dependencies (objfile);
9099
9100 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9101 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9102 }
9103
9104 /* See psympriv.h. */
9105
9106 bool
9107 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9108 {
9109 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9110 return per_objfile->symtab_set_p (per_cu_data);
9111 }
9112
9113 /* See psympriv.h. */
9114
9115 compunit_symtab *
9116 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9117 {
9118 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9119 return per_objfile->get_symtab (per_cu_data);
9120 }
9121
9122 /* Trivial hash function for die_info: the hash value of a DIE
9123 is its offset in .debug_info for this objfile. */
9124
9125 static hashval_t
9126 die_hash (const void *item)
9127 {
9128 const struct die_info *die = (const struct die_info *) item;
9129
9130 return to_underlying (die->sect_off);
9131 }
9132
9133 /* Trivial comparison function for die_info structures: two DIEs
9134 are equal if they have the same offset. */
9135
9136 static int
9137 die_eq (const void *item_lhs, const void *item_rhs)
9138 {
9139 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9140 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9141
9142 return die_lhs->sect_off == die_rhs->sect_off;
9143 }
9144
9145 /* Load the DIEs associated with PER_CU into memory. */
9146
9147 static void
9148 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9149 dwarf2_per_objfile *per_objfile,
9150 bool skip_partial,
9151 enum language pretend_language)
9152 {
9153 gdb_assert (! this_cu->is_debug_types);
9154
9155 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9156 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9157 if (reader.dummy_p)
9158 return;
9159
9160 struct dwarf2_cu *cu = reader.cu;
9161 const gdb_byte *info_ptr = reader.info_ptr;
9162
9163 gdb_assert (cu->die_hash == NULL);
9164 cu->die_hash =
9165 htab_create_alloc_ex (cu->header.length / 12,
9166 die_hash,
9167 die_eq,
9168 NULL,
9169 &cu->comp_unit_obstack,
9170 hashtab_obstack_allocate,
9171 dummy_obstack_deallocate);
9172
9173 if (reader.comp_unit_die->has_children)
9174 reader.comp_unit_die->child
9175 = read_die_and_siblings (&reader, reader.info_ptr,
9176 &info_ptr, reader.comp_unit_die);
9177 cu->dies = reader.comp_unit_die;
9178 /* comp_unit_die is not stored in die_hash, no need. */
9179
9180 /* We try not to read any attributes in this function, because not
9181 all CUs needed for references have been loaded yet, and symbol
9182 table processing isn't initialized. But we have to set the CU language,
9183 or we won't be able to build types correctly.
9184 Similarly, if we do not read the producer, we can not apply
9185 producer-specific interpretation. */
9186 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9187
9188 reader.keep ();
9189 }
9190
9191 /* Add a DIE to the delayed physname list. */
9192
9193 static void
9194 add_to_method_list (struct type *type, int fnfield_index, int index,
9195 const char *name, struct die_info *die,
9196 struct dwarf2_cu *cu)
9197 {
9198 struct delayed_method_info mi;
9199 mi.type = type;
9200 mi.fnfield_index = fnfield_index;
9201 mi.index = index;
9202 mi.name = name;
9203 mi.die = die;
9204 cu->method_list.push_back (mi);
9205 }
9206
9207 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9208 "const" / "volatile". If so, decrements LEN by the length of the
9209 modifier and return true. Otherwise return false. */
9210
9211 template<size_t N>
9212 static bool
9213 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9214 {
9215 size_t mod_len = sizeof (mod) - 1;
9216 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9217 {
9218 len -= mod_len;
9219 return true;
9220 }
9221 return false;
9222 }
9223
9224 /* Compute the physnames of any methods on the CU's method list.
9225
9226 The computation of method physnames is delayed in order to avoid the
9227 (bad) condition that one of the method's formal parameters is of an as yet
9228 incomplete type. */
9229
9230 static void
9231 compute_delayed_physnames (struct dwarf2_cu *cu)
9232 {
9233 /* Only C++ delays computing physnames. */
9234 if (cu->method_list.empty ())
9235 return;
9236 gdb_assert (cu->language == language_cplus);
9237
9238 for (const delayed_method_info &mi : cu->method_list)
9239 {
9240 const char *physname;
9241 struct fn_fieldlist *fn_flp
9242 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9243 physname = dwarf2_physname (mi.name, mi.die, cu);
9244 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9245 = physname ? physname : "";
9246
9247 /* Since there's no tag to indicate whether a method is a
9248 const/volatile overload, extract that information out of the
9249 demangled name. */
9250 if (physname != NULL)
9251 {
9252 size_t len = strlen (physname);
9253
9254 while (1)
9255 {
9256 if (physname[len] == ')') /* shortcut */
9257 break;
9258 else if (check_modifier (physname, len, " const"))
9259 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9260 else if (check_modifier (physname, len, " volatile"))
9261 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9262 else
9263 break;
9264 }
9265 }
9266 }
9267
9268 /* The list is no longer needed. */
9269 cu->method_list.clear ();
9270 }
9271
9272 /* Go objects should be embedded in a DW_TAG_module DIE,
9273 and it's not clear if/how imported objects will appear.
9274 To keep Go support simple until that's worked out,
9275 go back through what we've read and create something usable.
9276 We could do this while processing each DIE, and feels kinda cleaner,
9277 but that way is more invasive.
9278 This is to, for example, allow the user to type "p var" or "b main"
9279 without having to specify the package name, and allow lookups
9280 of module.object to work in contexts that use the expression
9281 parser. */
9282
9283 static void
9284 fixup_go_packaging (struct dwarf2_cu *cu)
9285 {
9286 gdb::unique_xmalloc_ptr<char> package_name;
9287 struct pending *list;
9288 int i;
9289
9290 for (list = *cu->get_builder ()->get_global_symbols ();
9291 list != NULL;
9292 list = list->next)
9293 {
9294 for (i = 0; i < list->nsyms; ++i)
9295 {
9296 struct symbol *sym = list->symbol[i];
9297
9298 if (sym->language () == language_go
9299 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9300 {
9301 gdb::unique_xmalloc_ptr<char> this_package_name
9302 (go_symbol_package_name (sym));
9303
9304 if (this_package_name == NULL)
9305 continue;
9306 if (package_name == NULL)
9307 package_name = std::move (this_package_name);
9308 else
9309 {
9310 struct objfile *objfile = cu->per_objfile->objfile;
9311 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9312 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9313 (symbol_symtab (sym) != NULL
9314 ? symtab_to_filename_for_display
9315 (symbol_symtab (sym))
9316 : objfile_name (objfile)),
9317 this_package_name.get (), package_name.get ());
9318 }
9319 }
9320 }
9321 }
9322
9323 if (package_name != NULL)
9324 {
9325 struct objfile *objfile = cu->per_objfile->objfile;
9326 const char *saved_package_name = objfile->intern (package_name.get ());
9327 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9328 saved_package_name);
9329 struct symbol *sym;
9330
9331 sym = new (&objfile->objfile_obstack) symbol;
9332 sym->set_language (language_go, &objfile->objfile_obstack);
9333 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9334 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9335 e.g., "main" finds the "main" module and not C's main(). */
9336 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9337 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9338 SYMBOL_TYPE (sym) = type;
9339
9340 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9341 }
9342 }
9343
9344 /* Allocate a fully-qualified name consisting of the two parts on the
9345 obstack. */
9346
9347 static const char *
9348 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9349 {
9350 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9351 }
9352
9353 /* A helper that allocates a variant part to attach to a Rust enum
9354 type. OBSTACK is where the results should be allocated. TYPE is
9355 the type we're processing. DISCRIMINANT_INDEX is the index of the
9356 discriminant. It must be the index of one of the fields of TYPE.
9357 DEFAULT_INDEX is the index of the default field; or -1 if there is
9358 no default. RANGES is indexed by "effective" field number (the
9359 field index, but omitting the discriminant and default fields) and
9360 must hold the discriminant values used by the variants. Note that
9361 RANGES must have a lifetime at least as long as OBSTACK -- either
9362 already allocated on it, or static. */
9363
9364 static void
9365 alloc_rust_variant (struct obstack *obstack, struct type *type,
9366 int discriminant_index, int default_index,
9367 gdb::array_view<discriminant_range> ranges)
9368 {
9369 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9370 must be handled by the caller. */
9371 gdb_assert (discriminant_index >= 0
9372 && discriminant_index < type->num_fields ());
9373 gdb_assert (default_index == -1
9374 || (default_index >= 0 && default_index < type->num_fields ()));
9375
9376 /* We have one variant for each non-discriminant field. */
9377 int n_variants = type->num_fields () - 1;
9378
9379 variant *variants = new (obstack) variant[n_variants];
9380 int var_idx = 0;
9381 int range_idx = 0;
9382 for (int i = 0; i < type->num_fields (); ++i)
9383 {
9384 if (i == discriminant_index)
9385 continue;
9386
9387 variants[var_idx].first_field = i;
9388 variants[var_idx].last_field = i + 1;
9389
9390 /* The default field does not need a range, but other fields do.
9391 We skipped the discriminant above. */
9392 if (i != default_index)
9393 {
9394 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9395 ++range_idx;
9396 }
9397
9398 ++var_idx;
9399 }
9400
9401 gdb_assert (range_idx == ranges.size ());
9402 gdb_assert (var_idx == n_variants);
9403
9404 variant_part *part = new (obstack) variant_part;
9405 part->discriminant_index = discriminant_index;
9406 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9407 discriminant_index));
9408 part->variants = gdb::array_view<variant> (variants, n_variants);
9409
9410 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9411 gdb::array_view<variant_part> *prop_value
9412 = new (storage) gdb::array_view<variant_part> (part, 1);
9413
9414 struct dynamic_prop prop;
9415 prop.kind = PROP_VARIANT_PARTS;
9416 prop.data.variant_parts = prop_value;
9417
9418 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9419 }
9420
9421 /* Some versions of rustc emitted enums in an unusual way.
9422
9423 Ordinary enums were emitted as unions. The first element of each
9424 structure in the union was named "RUST$ENUM$DISR". This element
9425 held the discriminant.
9426
9427 These versions of Rust also implemented the "non-zero"
9428 optimization. When the enum had two values, and one is empty and
9429 the other holds a pointer that cannot be zero, the pointer is used
9430 as the discriminant, with a zero value meaning the empty variant.
9431 Here, the union's first member is of the form
9432 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9433 where the fieldnos are the indices of the fields that should be
9434 traversed in order to find the field (which may be several fields deep)
9435 and the variantname is the name of the variant of the case when the
9436 field is zero.
9437
9438 This function recognizes whether TYPE is of one of these forms,
9439 and, if so, smashes it to be a variant type. */
9440
9441 static void
9442 quirk_rust_enum (struct type *type, struct objfile *objfile)
9443 {
9444 gdb_assert (type->code () == TYPE_CODE_UNION);
9445
9446 /* We don't need to deal with empty enums. */
9447 if (type->num_fields () == 0)
9448 return;
9449
9450 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9451 if (type->num_fields () == 1
9452 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9453 {
9454 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9455
9456 /* Decode the field name to find the offset of the
9457 discriminant. */
9458 ULONGEST bit_offset = 0;
9459 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9460 while (name[0] >= '0' && name[0] <= '9')
9461 {
9462 char *tail;
9463 unsigned long index = strtoul (name, &tail, 10);
9464 name = tail;
9465 if (*name != '$'
9466 || index >= field_type->num_fields ()
9467 || (TYPE_FIELD_LOC_KIND (field_type, index)
9468 != FIELD_LOC_KIND_BITPOS))
9469 {
9470 complaint (_("Could not parse Rust enum encoding string \"%s\""
9471 "[in module %s]"),
9472 TYPE_FIELD_NAME (type, 0),
9473 objfile_name (objfile));
9474 return;
9475 }
9476 ++name;
9477
9478 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9479 field_type = TYPE_FIELD_TYPE (field_type, index);
9480 }
9481
9482 /* Smash this type to be a structure type. We have to do this
9483 because the type has already been recorded. */
9484 type->set_code (TYPE_CODE_STRUCT);
9485 type->set_num_fields (3);
9486 /* Save the field we care about. */
9487 struct field saved_field = type->field (0);
9488 type->set_fields
9489 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9490
9491 /* Put the discriminant at index 0. */
9492 TYPE_FIELD_TYPE (type, 0) = field_type;
9493 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9494 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9495 SET_FIELD_BITPOS (type->field (0), bit_offset);
9496
9497 /* The order of fields doesn't really matter, so put the real
9498 field at index 1 and the data-less field at index 2. */
9499 type->field (1) = saved_field;
9500 TYPE_FIELD_NAME (type, 1)
9501 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9502 TYPE_FIELD_TYPE (type, 1)->set_name
9503 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9504 TYPE_FIELD_NAME (type, 1)));
9505
9506 const char *dataless_name
9507 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9508 name);
9509 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9510 dataless_name);
9511 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9512 /* NAME points into the original discriminant name, which
9513 already has the correct lifetime. */
9514 TYPE_FIELD_NAME (type, 2) = name;
9515 SET_FIELD_BITPOS (type->field (2), 0);
9516
9517 /* Indicate that this is a variant type. */
9518 static discriminant_range ranges[1] = { { 0, 0 } };
9519 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9520 }
9521 /* A union with a single anonymous field is probably an old-style
9522 univariant enum. */
9523 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9524 {
9525 /* Smash this type to be a structure type. We have to do this
9526 because the type has already been recorded. */
9527 type->set_code (TYPE_CODE_STRUCT);
9528
9529 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9530 const char *variant_name
9531 = rust_last_path_segment (field_type->name ());
9532 TYPE_FIELD_NAME (type, 0) = variant_name;
9533 field_type->set_name
9534 (rust_fully_qualify (&objfile->objfile_obstack,
9535 type->name (), variant_name));
9536 }
9537 else
9538 {
9539 struct type *disr_type = nullptr;
9540 for (int i = 0; i < type->num_fields (); ++i)
9541 {
9542 disr_type = TYPE_FIELD_TYPE (type, i);
9543
9544 if (disr_type->code () != TYPE_CODE_STRUCT)
9545 {
9546 /* All fields of a true enum will be structs. */
9547 return;
9548 }
9549 else if (disr_type->num_fields () == 0)
9550 {
9551 /* Could be data-less variant, so keep going. */
9552 disr_type = nullptr;
9553 }
9554 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9555 "RUST$ENUM$DISR") != 0)
9556 {
9557 /* Not a Rust enum. */
9558 return;
9559 }
9560 else
9561 {
9562 /* Found one. */
9563 break;
9564 }
9565 }
9566
9567 /* If we got here without a discriminant, then it's probably
9568 just a union. */
9569 if (disr_type == nullptr)
9570 return;
9571
9572 /* Smash this type to be a structure type. We have to do this
9573 because the type has already been recorded. */
9574 type->set_code (TYPE_CODE_STRUCT);
9575
9576 /* Make space for the discriminant field. */
9577 struct field *disr_field = &disr_type->field (0);
9578 field *new_fields
9579 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9580 * sizeof (struct field)));
9581 memcpy (new_fields + 1, type->fields (),
9582 type->num_fields () * sizeof (struct field));
9583 type->set_fields (new_fields);
9584 type->set_num_fields (type->num_fields () + 1);
9585
9586 /* Install the discriminant at index 0 in the union. */
9587 type->field (0) = *disr_field;
9588 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9589 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9590
9591 /* We need a way to find the correct discriminant given a
9592 variant name. For convenience we build a map here. */
9593 struct type *enum_type = FIELD_TYPE (*disr_field);
9594 std::unordered_map<std::string, ULONGEST> discriminant_map;
9595 for (int i = 0; i < enum_type->num_fields (); ++i)
9596 {
9597 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9598 {
9599 const char *name
9600 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9601 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9602 }
9603 }
9604
9605 int n_fields = type->num_fields ();
9606 /* We don't need a range entry for the discriminant, but we do
9607 need one for every other field, as there is no default
9608 variant. */
9609 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9610 discriminant_range,
9611 n_fields - 1);
9612 /* Skip the discriminant here. */
9613 for (int i = 1; i < n_fields; ++i)
9614 {
9615 /* Find the final word in the name of this variant's type.
9616 That name can be used to look up the correct
9617 discriminant. */
9618 const char *variant_name
9619 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9620
9621 auto iter = discriminant_map.find (variant_name);
9622 if (iter != discriminant_map.end ())
9623 {
9624 ranges[i].low = iter->second;
9625 ranges[i].high = iter->second;
9626 }
9627
9628 /* Remove the discriminant field, if it exists. */
9629 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9630 if (sub_type->num_fields () > 0)
9631 {
9632 sub_type->set_num_fields (sub_type->num_fields () - 1);
9633 sub_type->set_fields (sub_type->fields () + 1);
9634 }
9635 TYPE_FIELD_NAME (type, i) = variant_name;
9636 sub_type->set_name
9637 (rust_fully_qualify (&objfile->objfile_obstack,
9638 type->name (), variant_name));
9639 }
9640
9641 /* Indicate that this is a variant type. */
9642 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9643 gdb::array_view<discriminant_range> (ranges,
9644 n_fields - 1));
9645 }
9646 }
9647
9648 /* Rewrite some Rust unions to be structures with variants parts. */
9649
9650 static void
9651 rust_union_quirks (struct dwarf2_cu *cu)
9652 {
9653 gdb_assert (cu->language == language_rust);
9654 for (type *type_ : cu->rust_unions)
9655 quirk_rust_enum (type_, cu->per_objfile->objfile);
9656 /* We don't need this any more. */
9657 cu->rust_unions.clear ();
9658 }
9659
9660 /* See read.h. */
9661
9662 type_unit_group_unshareable *
9663 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9664 {
9665 auto iter = this->m_type_units.find (tu_group);
9666 if (iter != this->m_type_units.end ())
9667 return iter->second.get ();
9668
9669 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9670 type_unit_group_unshareable *result = uniq.get ();
9671 this->m_type_units[tu_group] = std::move (uniq);
9672 return result;
9673 }
9674
9675 struct type *
9676 dwarf2_per_objfile::get_type_for_signatured_type
9677 (signatured_type *sig_type) const
9678 {
9679 auto iter = this->m_type_map.find (sig_type);
9680 if (iter == this->m_type_map.end ())
9681 return nullptr;
9682
9683 return iter->second;
9684 }
9685
9686 void dwarf2_per_objfile::set_type_for_signatured_type
9687 (signatured_type *sig_type, struct type *type)
9688 {
9689 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9690
9691 this->m_type_map[sig_type] = type;
9692 }
9693
9694 /* A helper function for computing the list of all symbol tables
9695 included by PER_CU. */
9696
9697 static void
9698 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9699 htab_t all_children, htab_t all_type_symtabs,
9700 dwarf2_per_cu_data *per_cu,
9701 dwarf2_per_objfile *per_objfile,
9702 struct compunit_symtab *immediate_parent)
9703 {
9704 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9705 if (*slot != NULL)
9706 {
9707 /* This inclusion and its children have been processed. */
9708 return;
9709 }
9710
9711 *slot = per_cu;
9712
9713 /* Only add a CU if it has a symbol table. */
9714 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9715 if (cust != NULL)
9716 {
9717 /* If this is a type unit only add its symbol table if we haven't
9718 seen it yet (type unit per_cu's can share symtabs). */
9719 if (per_cu->is_debug_types)
9720 {
9721 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9722 if (*slot == NULL)
9723 {
9724 *slot = cust;
9725 result->push_back (cust);
9726 if (cust->user == NULL)
9727 cust->user = immediate_parent;
9728 }
9729 }
9730 else
9731 {
9732 result->push_back (cust);
9733 if (cust->user == NULL)
9734 cust->user = immediate_parent;
9735 }
9736 }
9737
9738 if (!per_cu->imported_symtabs_empty ())
9739 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9740 {
9741 recursively_compute_inclusions (result, all_children,
9742 all_type_symtabs, ptr, per_objfile,
9743 cust);
9744 }
9745 }
9746
9747 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9748 PER_CU. */
9749
9750 static void
9751 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9752 dwarf2_per_objfile *per_objfile)
9753 {
9754 gdb_assert (! per_cu->is_debug_types);
9755
9756 if (!per_cu->imported_symtabs_empty ())
9757 {
9758 int len;
9759 std::vector<compunit_symtab *> result_symtabs;
9760 htab_t all_children, all_type_symtabs;
9761 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9762
9763 /* If we don't have a symtab, we can just skip this case. */
9764 if (cust == NULL)
9765 return;
9766
9767 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9768 NULL, xcalloc, xfree);
9769 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9770 NULL, xcalloc, xfree);
9771
9772 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9773 {
9774 recursively_compute_inclusions (&result_symtabs, all_children,
9775 all_type_symtabs, ptr, per_objfile,
9776 cust);
9777 }
9778
9779 /* Now we have a transitive closure of all the included symtabs. */
9780 len = result_symtabs.size ();
9781 cust->includes
9782 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9783 struct compunit_symtab *, len + 1);
9784 memcpy (cust->includes, result_symtabs.data (),
9785 len * sizeof (compunit_symtab *));
9786 cust->includes[len] = NULL;
9787
9788 htab_delete (all_children);
9789 htab_delete (all_type_symtabs);
9790 }
9791 }
9792
9793 /* Compute the 'includes' field for the symtabs of all the CUs we just
9794 read. */
9795
9796 static void
9797 process_cu_includes (dwarf2_per_objfile *per_objfile)
9798 {
9799 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9800 {
9801 if (! iter->is_debug_types)
9802 compute_compunit_symtab_includes (iter, per_objfile);
9803 }
9804
9805 per_objfile->per_bfd->just_read_cus.clear ();
9806 }
9807
9808 /* Generate full symbol information for CU, whose DIEs have
9809 already been loaded into memory. */
9810
9811 static void
9812 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9813 {
9814 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9815 struct objfile *objfile = per_objfile->objfile;
9816 struct gdbarch *gdbarch = objfile->arch ();
9817 CORE_ADDR lowpc, highpc;
9818 struct compunit_symtab *cust;
9819 CORE_ADDR baseaddr;
9820 struct block *static_block;
9821 CORE_ADDR addr;
9822
9823 baseaddr = objfile->text_section_offset ();
9824
9825 /* Clear the list here in case something was left over. */
9826 cu->method_list.clear ();
9827
9828 cu->language = pretend_language;
9829 cu->language_defn = language_def (cu->language);
9830
9831 /* Do line number decoding in read_file_scope () */
9832 process_die (cu->dies, cu);
9833
9834 /* For now fudge the Go package. */
9835 if (cu->language == language_go)
9836 fixup_go_packaging (cu);
9837
9838 /* Now that we have processed all the DIEs in the CU, all the types
9839 should be complete, and it should now be safe to compute all of the
9840 physnames. */
9841 compute_delayed_physnames (cu);
9842
9843 if (cu->language == language_rust)
9844 rust_union_quirks (cu);
9845
9846 /* Some compilers don't define a DW_AT_high_pc attribute for the
9847 compilation unit. If the DW_AT_high_pc is missing, synthesize
9848 it, by scanning the DIE's below the compilation unit. */
9849 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9850
9851 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9852 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9853
9854 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9855 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9856 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9857 addrmap to help ensure it has an accurate map of pc values belonging to
9858 this comp unit. */
9859 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9860
9861 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9862 SECT_OFF_TEXT (objfile),
9863 0);
9864
9865 if (cust != NULL)
9866 {
9867 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9868
9869 /* Set symtab language to language from DW_AT_language. If the
9870 compilation is from a C file generated by language preprocessors, do
9871 not set the language if it was already deduced by start_subfile. */
9872 if (!(cu->language == language_c
9873 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9874 COMPUNIT_FILETABS (cust)->language = cu->language;
9875
9876 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9877 produce DW_AT_location with location lists but it can be possibly
9878 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9879 there were bugs in prologue debug info, fixed later in GCC-4.5
9880 by "unwind info for epilogues" patch (which is not directly related).
9881
9882 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9883 needed, it would be wrong due to missing DW_AT_producer there.
9884
9885 Still one can confuse GDB by using non-standard GCC compilation
9886 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9887 */
9888 if (cu->has_loclist && gcc_4_minor >= 5)
9889 cust->locations_valid = 1;
9890
9891 if (gcc_4_minor >= 5)
9892 cust->epilogue_unwind_valid = 1;
9893
9894 cust->call_site_htab = cu->call_site_htab;
9895 }
9896
9897 per_objfile->set_symtab (cu->per_cu, cust);
9898
9899 /* Push it for inclusion processing later. */
9900 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9901
9902 /* Not needed any more. */
9903 cu->reset_builder ();
9904 }
9905
9906 /* Generate full symbol information for type unit CU, whose DIEs have
9907 already been loaded into memory. */
9908
9909 static void
9910 process_full_type_unit (dwarf2_cu *cu,
9911 enum language pretend_language)
9912 {
9913 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9914 struct objfile *objfile = per_objfile->objfile;
9915 struct compunit_symtab *cust;
9916 struct signatured_type *sig_type;
9917
9918 gdb_assert (cu->per_cu->is_debug_types);
9919 sig_type = (struct signatured_type *) cu->per_cu;
9920
9921 /* Clear the list here in case something was left over. */
9922 cu->method_list.clear ();
9923
9924 cu->language = pretend_language;
9925 cu->language_defn = language_def (cu->language);
9926
9927 /* The symbol tables are set up in read_type_unit_scope. */
9928 process_die (cu->dies, cu);
9929
9930 /* For now fudge the Go package. */
9931 if (cu->language == language_go)
9932 fixup_go_packaging (cu);
9933
9934 /* Now that we have processed all the DIEs in the CU, all the types
9935 should be complete, and it should now be safe to compute all of the
9936 physnames. */
9937 compute_delayed_physnames (cu);
9938
9939 if (cu->language == language_rust)
9940 rust_union_quirks (cu);
9941
9942 /* TUs share symbol tables.
9943 If this is the first TU to use this symtab, complete the construction
9944 of it with end_expandable_symtab. Otherwise, complete the addition of
9945 this TU's symbols to the existing symtab. */
9946 type_unit_group_unshareable *tug_unshare =
9947 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9948 if (tug_unshare->compunit_symtab == NULL)
9949 {
9950 buildsym_compunit *builder = cu->get_builder ();
9951 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9952 tug_unshare->compunit_symtab = cust;
9953
9954 if (cust != NULL)
9955 {
9956 /* Set symtab language to language from DW_AT_language. If the
9957 compilation is from a C file generated by language preprocessors,
9958 do not set the language if it was already deduced by
9959 start_subfile. */
9960 if (!(cu->language == language_c
9961 && COMPUNIT_FILETABS (cust)->language != language_c))
9962 COMPUNIT_FILETABS (cust)->language = cu->language;
9963 }
9964 }
9965 else
9966 {
9967 cu->get_builder ()->augment_type_symtab ();
9968 cust = tug_unshare->compunit_symtab;
9969 }
9970
9971 per_objfile->set_symtab (cu->per_cu, cust);
9972
9973 /* Not needed any more. */
9974 cu->reset_builder ();
9975 }
9976
9977 /* Process an imported unit DIE. */
9978
9979 static void
9980 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9981 {
9982 struct attribute *attr;
9983
9984 /* For now we don't handle imported units in type units. */
9985 if (cu->per_cu->is_debug_types)
9986 {
9987 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9988 " supported in type units [in module %s]"),
9989 objfile_name (cu->per_objfile->objfile));
9990 }
9991
9992 attr = dwarf2_attr (die, DW_AT_import, cu);
9993 if (attr != NULL)
9994 {
9995 sect_offset sect_off = attr->get_ref_die_offset ();
9996 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9997 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9998 dwarf2_per_cu_data *per_cu
9999 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10000
10001 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10002 into another compilation unit, at root level. Regard this as a hint,
10003 and ignore it. */
10004 if (die->parent && die->parent->parent == NULL
10005 && per_cu->unit_type == DW_UT_compile
10006 && per_cu->lang == language_cplus)
10007 return;
10008
10009 /* If necessary, add it to the queue and load its DIEs. */
10010 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10011 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
10012
10013 cu->per_cu->imported_symtabs_push (per_cu);
10014 }
10015 }
10016
10017 /* RAII object that represents a process_die scope: i.e.,
10018 starts/finishes processing a DIE. */
10019 class process_die_scope
10020 {
10021 public:
10022 process_die_scope (die_info *die, dwarf2_cu *cu)
10023 : m_die (die), m_cu (cu)
10024 {
10025 /* We should only be processing DIEs not already in process. */
10026 gdb_assert (!m_die->in_process);
10027 m_die->in_process = true;
10028 }
10029
10030 ~process_die_scope ()
10031 {
10032 m_die->in_process = false;
10033
10034 /* If we're done processing the DIE for the CU that owns the line
10035 header, we don't need the line header anymore. */
10036 if (m_cu->line_header_die_owner == m_die)
10037 {
10038 delete m_cu->line_header;
10039 m_cu->line_header = NULL;
10040 m_cu->line_header_die_owner = NULL;
10041 }
10042 }
10043
10044 private:
10045 die_info *m_die;
10046 dwarf2_cu *m_cu;
10047 };
10048
10049 /* Process a die and its children. */
10050
10051 static void
10052 process_die (struct die_info *die, struct dwarf2_cu *cu)
10053 {
10054 process_die_scope scope (die, cu);
10055
10056 switch (die->tag)
10057 {
10058 case DW_TAG_padding:
10059 break;
10060 case DW_TAG_compile_unit:
10061 case DW_TAG_partial_unit:
10062 read_file_scope (die, cu);
10063 break;
10064 case DW_TAG_type_unit:
10065 read_type_unit_scope (die, cu);
10066 break;
10067 case DW_TAG_subprogram:
10068 /* Nested subprograms in Fortran get a prefix. */
10069 if (cu->language == language_fortran
10070 && die->parent != NULL
10071 && die->parent->tag == DW_TAG_subprogram)
10072 cu->processing_has_namespace_info = true;
10073 /* Fall through. */
10074 case DW_TAG_inlined_subroutine:
10075 read_func_scope (die, cu);
10076 break;
10077 case DW_TAG_lexical_block:
10078 case DW_TAG_try_block:
10079 case DW_TAG_catch_block:
10080 read_lexical_block_scope (die, cu);
10081 break;
10082 case DW_TAG_call_site:
10083 case DW_TAG_GNU_call_site:
10084 read_call_site_scope (die, cu);
10085 break;
10086 case DW_TAG_class_type:
10087 case DW_TAG_interface_type:
10088 case DW_TAG_structure_type:
10089 case DW_TAG_union_type:
10090 process_structure_scope (die, cu);
10091 break;
10092 case DW_TAG_enumeration_type:
10093 process_enumeration_scope (die, cu);
10094 break;
10095
10096 /* These dies have a type, but processing them does not create
10097 a symbol or recurse to process the children. Therefore we can
10098 read them on-demand through read_type_die. */
10099 case DW_TAG_subroutine_type:
10100 case DW_TAG_set_type:
10101 case DW_TAG_array_type:
10102 case DW_TAG_pointer_type:
10103 case DW_TAG_ptr_to_member_type:
10104 case DW_TAG_reference_type:
10105 case DW_TAG_rvalue_reference_type:
10106 case DW_TAG_string_type:
10107 break;
10108
10109 case DW_TAG_base_type:
10110 case DW_TAG_subrange_type:
10111 case DW_TAG_typedef:
10112 /* Add a typedef symbol for the type definition, if it has a
10113 DW_AT_name. */
10114 new_symbol (die, read_type_die (die, cu), cu);
10115 break;
10116 case DW_TAG_common_block:
10117 read_common_block (die, cu);
10118 break;
10119 case DW_TAG_common_inclusion:
10120 break;
10121 case DW_TAG_namespace:
10122 cu->processing_has_namespace_info = true;
10123 read_namespace (die, cu);
10124 break;
10125 case DW_TAG_module:
10126 cu->processing_has_namespace_info = true;
10127 read_module (die, cu);
10128 break;
10129 case DW_TAG_imported_declaration:
10130 cu->processing_has_namespace_info = true;
10131 if (read_namespace_alias (die, cu))
10132 break;
10133 /* The declaration is not a global namespace alias. */
10134 /* Fall through. */
10135 case DW_TAG_imported_module:
10136 cu->processing_has_namespace_info = true;
10137 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10138 || cu->language != language_fortran))
10139 complaint (_("Tag '%s' has unexpected children"),
10140 dwarf_tag_name (die->tag));
10141 read_import_statement (die, cu);
10142 break;
10143
10144 case DW_TAG_imported_unit:
10145 process_imported_unit_die (die, cu);
10146 break;
10147
10148 case DW_TAG_variable:
10149 read_variable (die, cu);
10150 break;
10151
10152 default:
10153 new_symbol (die, NULL, cu);
10154 break;
10155 }
10156 }
10157 \f
10158 /* DWARF name computation. */
10159
10160 /* A helper function for dwarf2_compute_name which determines whether DIE
10161 needs to have the name of the scope prepended to the name listed in the
10162 die. */
10163
10164 static int
10165 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10166 {
10167 struct attribute *attr;
10168
10169 switch (die->tag)
10170 {
10171 case DW_TAG_namespace:
10172 case DW_TAG_typedef:
10173 case DW_TAG_class_type:
10174 case DW_TAG_interface_type:
10175 case DW_TAG_structure_type:
10176 case DW_TAG_union_type:
10177 case DW_TAG_enumeration_type:
10178 case DW_TAG_enumerator:
10179 case DW_TAG_subprogram:
10180 case DW_TAG_inlined_subroutine:
10181 case DW_TAG_member:
10182 case DW_TAG_imported_declaration:
10183 return 1;
10184
10185 case DW_TAG_variable:
10186 case DW_TAG_constant:
10187 /* We only need to prefix "globally" visible variables. These include
10188 any variable marked with DW_AT_external or any variable that
10189 lives in a namespace. [Variables in anonymous namespaces
10190 require prefixing, but they are not DW_AT_external.] */
10191
10192 if (dwarf2_attr (die, DW_AT_specification, cu))
10193 {
10194 struct dwarf2_cu *spec_cu = cu;
10195
10196 return die_needs_namespace (die_specification (die, &spec_cu),
10197 spec_cu);
10198 }
10199
10200 attr = dwarf2_attr (die, DW_AT_external, cu);
10201 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10202 && die->parent->tag != DW_TAG_module)
10203 return 0;
10204 /* A variable in a lexical block of some kind does not need a
10205 namespace, even though in C++ such variables may be external
10206 and have a mangled name. */
10207 if (die->parent->tag == DW_TAG_lexical_block
10208 || die->parent->tag == DW_TAG_try_block
10209 || die->parent->tag == DW_TAG_catch_block
10210 || die->parent->tag == DW_TAG_subprogram)
10211 return 0;
10212 return 1;
10213
10214 default:
10215 return 0;
10216 }
10217 }
10218
10219 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10220 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10221 defined for the given DIE. */
10222
10223 static struct attribute *
10224 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10225 {
10226 struct attribute *attr;
10227
10228 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10229 if (attr == NULL)
10230 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10231
10232 return attr;
10233 }
10234
10235 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10236 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10237 defined for the given DIE. */
10238
10239 static const char *
10240 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10241 {
10242 const char *linkage_name;
10243
10244 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10245 if (linkage_name == NULL)
10246 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10247
10248 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10249 See https://github.com/rust-lang/rust/issues/32925. */
10250 if (cu->language == language_rust && linkage_name != NULL
10251 && strchr (linkage_name, '{') != NULL)
10252 linkage_name = NULL;
10253
10254 return linkage_name;
10255 }
10256
10257 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10258 compute the physname for the object, which include a method's:
10259 - formal parameters (C++),
10260 - receiver type (Go),
10261
10262 The term "physname" is a bit confusing.
10263 For C++, for example, it is the demangled name.
10264 For Go, for example, it's the mangled name.
10265
10266 For Ada, return the DIE's linkage name rather than the fully qualified
10267 name. PHYSNAME is ignored..
10268
10269 The result is allocated on the objfile->per_bfd's obstack and
10270 canonicalized. */
10271
10272 static const char *
10273 dwarf2_compute_name (const char *name,
10274 struct die_info *die, struct dwarf2_cu *cu,
10275 int physname)
10276 {
10277 struct objfile *objfile = cu->per_objfile->objfile;
10278
10279 if (name == NULL)
10280 name = dwarf2_name (die, cu);
10281
10282 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10283 but otherwise compute it by typename_concat inside GDB.
10284 FIXME: Actually this is not really true, or at least not always true.
10285 It's all very confusing. compute_and_set_names doesn't try to demangle
10286 Fortran names because there is no mangling standard. So new_symbol
10287 will set the demangled name to the result of dwarf2_full_name, and it is
10288 the demangled name that GDB uses if it exists. */
10289 if (cu->language == language_ada
10290 || (cu->language == language_fortran && physname))
10291 {
10292 /* For Ada unit, we prefer the linkage name over the name, as
10293 the former contains the exported name, which the user expects
10294 to be able to reference. Ideally, we want the user to be able
10295 to reference this entity using either natural or linkage name,
10296 but we haven't started looking at this enhancement yet. */
10297 const char *linkage_name = dw2_linkage_name (die, cu);
10298
10299 if (linkage_name != NULL)
10300 return linkage_name;
10301 }
10302
10303 /* These are the only languages we know how to qualify names in. */
10304 if (name != NULL
10305 && (cu->language == language_cplus
10306 || cu->language == language_fortran || cu->language == language_d
10307 || cu->language == language_rust))
10308 {
10309 if (die_needs_namespace (die, cu))
10310 {
10311 const char *prefix;
10312 const char *canonical_name = NULL;
10313
10314 string_file buf;
10315
10316 prefix = determine_prefix (die, cu);
10317 if (*prefix != '\0')
10318 {
10319 gdb::unique_xmalloc_ptr<char> prefixed_name
10320 (typename_concat (NULL, prefix, name, physname, cu));
10321
10322 buf.puts (prefixed_name.get ());
10323 }
10324 else
10325 buf.puts (name);
10326
10327 /* Template parameters may be specified in the DIE's DW_AT_name, or
10328 as children with DW_TAG_template_type_param or
10329 DW_TAG_value_type_param. If the latter, add them to the name
10330 here. If the name already has template parameters, then
10331 skip this step; some versions of GCC emit both, and
10332 it is more efficient to use the pre-computed name.
10333
10334 Something to keep in mind about this process: it is very
10335 unlikely, or in some cases downright impossible, to produce
10336 something that will match the mangled name of a function.
10337 If the definition of the function has the same debug info,
10338 we should be able to match up with it anyway. But fallbacks
10339 using the minimal symbol, for instance to find a method
10340 implemented in a stripped copy of libstdc++, will not work.
10341 If we do not have debug info for the definition, we will have to
10342 match them up some other way.
10343
10344 When we do name matching there is a related problem with function
10345 templates; two instantiated function templates are allowed to
10346 differ only by their return types, which we do not add here. */
10347
10348 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10349 {
10350 struct attribute *attr;
10351 struct die_info *child;
10352 int first = 1;
10353
10354 die->building_fullname = 1;
10355
10356 for (child = die->child; child != NULL; child = child->sibling)
10357 {
10358 struct type *type;
10359 LONGEST value;
10360 const gdb_byte *bytes;
10361 struct dwarf2_locexpr_baton *baton;
10362 struct value *v;
10363
10364 if (child->tag != DW_TAG_template_type_param
10365 && child->tag != DW_TAG_template_value_param)
10366 continue;
10367
10368 if (first)
10369 {
10370 buf.puts ("<");
10371 first = 0;
10372 }
10373 else
10374 buf.puts (", ");
10375
10376 attr = dwarf2_attr (child, DW_AT_type, cu);
10377 if (attr == NULL)
10378 {
10379 complaint (_("template parameter missing DW_AT_type"));
10380 buf.puts ("UNKNOWN_TYPE");
10381 continue;
10382 }
10383 type = die_type (child, cu);
10384
10385 if (child->tag == DW_TAG_template_type_param)
10386 {
10387 c_print_type (type, "", &buf, -1, 0, cu->language,
10388 &type_print_raw_options);
10389 continue;
10390 }
10391
10392 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10393 if (attr == NULL)
10394 {
10395 complaint (_("template parameter missing "
10396 "DW_AT_const_value"));
10397 buf.puts ("UNKNOWN_VALUE");
10398 continue;
10399 }
10400
10401 dwarf2_const_value_attr (attr, type, name,
10402 &cu->comp_unit_obstack, cu,
10403 &value, &bytes, &baton);
10404
10405 if (TYPE_NOSIGN (type))
10406 /* GDB prints characters as NUMBER 'CHAR'. If that's
10407 changed, this can use value_print instead. */
10408 c_printchar (value, type, &buf);
10409 else
10410 {
10411 struct value_print_options opts;
10412
10413 if (baton != NULL)
10414 v = dwarf2_evaluate_loc_desc (type, NULL,
10415 baton->data,
10416 baton->size,
10417 baton->per_cu,
10418 baton->per_objfile);
10419 else if (bytes != NULL)
10420 {
10421 v = allocate_value (type);
10422 memcpy (value_contents_writeable (v), bytes,
10423 TYPE_LENGTH (type));
10424 }
10425 else
10426 v = value_from_longest (type, value);
10427
10428 /* Specify decimal so that we do not depend on
10429 the radix. */
10430 get_formatted_print_options (&opts, 'd');
10431 opts.raw = 1;
10432 value_print (v, &buf, &opts);
10433 release_value (v);
10434 }
10435 }
10436
10437 die->building_fullname = 0;
10438
10439 if (!first)
10440 {
10441 /* Close the argument list, with a space if necessary
10442 (nested templates). */
10443 if (!buf.empty () && buf.string ().back () == '>')
10444 buf.puts (" >");
10445 else
10446 buf.puts (">");
10447 }
10448 }
10449
10450 /* For C++ methods, append formal parameter type
10451 information, if PHYSNAME. */
10452
10453 if (physname && die->tag == DW_TAG_subprogram
10454 && cu->language == language_cplus)
10455 {
10456 struct type *type = read_type_die (die, cu);
10457
10458 c_type_print_args (type, &buf, 1, cu->language,
10459 &type_print_raw_options);
10460
10461 if (cu->language == language_cplus)
10462 {
10463 /* Assume that an artificial first parameter is
10464 "this", but do not crash if it is not. RealView
10465 marks unnamed (and thus unused) parameters as
10466 artificial; there is no way to differentiate
10467 the two cases. */
10468 if (type->num_fields () > 0
10469 && TYPE_FIELD_ARTIFICIAL (type, 0)
10470 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10471 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10472 0))))
10473 buf.puts (" const");
10474 }
10475 }
10476
10477 const std::string &intermediate_name = buf.string ();
10478
10479 if (cu->language == language_cplus)
10480 canonical_name
10481 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10482 objfile);
10483
10484 /* If we only computed INTERMEDIATE_NAME, or if
10485 INTERMEDIATE_NAME is already canonical, then we need to
10486 intern it. */
10487 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10488 name = objfile->intern (intermediate_name);
10489 else
10490 name = canonical_name;
10491 }
10492 }
10493
10494 return name;
10495 }
10496
10497 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10498 If scope qualifiers are appropriate they will be added. The result
10499 will be allocated on the storage_obstack, or NULL if the DIE does
10500 not have a name. NAME may either be from a previous call to
10501 dwarf2_name or NULL.
10502
10503 The output string will be canonicalized (if C++). */
10504
10505 static const char *
10506 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10507 {
10508 return dwarf2_compute_name (name, die, cu, 0);
10509 }
10510
10511 /* Construct a physname for the given DIE in CU. NAME may either be
10512 from a previous call to dwarf2_name or NULL. The result will be
10513 allocated on the objfile_objstack or NULL if the DIE does not have a
10514 name.
10515
10516 The output string will be canonicalized (if C++). */
10517
10518 static const char *
10519 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10520 {
10521 struct objfile *objfile = cu->per_objfile->objfile;
10522 const char *retval, *mangled = NULL, *canon = NULL;
10523 int need_copy = 1;
10524
10525 /* In this case dwarf2_compute_name is just a shortcut not building anything
10526 on its own. */
10527 if (!die_needs_namespace (die, cu))
10528 return dwarf2_compute_name (name, die, cu, 1);
10529
10530 if (cu->language != language_rust)
10531 mangled = dw2_linkage_name (die, cu);
10532
10533 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10534 has computed. */
10535 gdb::unique_xmalloc_ptr<char> demangled;
10536 if (mangled != NULL)
10537 {
10538
10539 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10540 {
10541 /* Do nothing (do not demangle the symbol name). */
10542 }
10543 else if (cu->language == language_go)
10544 {
10545 /* This is a lie, but we already lie to the caller new_symbol.
10546 new_symbol assumes we return the mangled name.
10547 This just undoes that lie until things are cleaned up. */
10548 }
10549 else
10550 {
10551 /* Use DMGL_RET_DROP for C++ template functions to suppress
10552 their return type. It is easier for GDB users to search
10553 for such functions as `name(params)' than `long name(params)'.
10554 In such case the minimal symbol names do not match the full
10555 symbol names but for template functions there is never a need
10556 to look up their definition from their declaration so
10557 the only disadvantage remains the minimal symbol variant
10558 `long name(params)' does not have the proper inferior type. */
10559 demangled.reset (gdb_demangle (mangled,
10560 (DMGL_PARAMS | DMGL_ANSI
10561 | DMGL_RET_DROP)));
10562 }
10563 if (demangled)
10564 canon = demangled.get ();
10565 else
10566 {
10567 canon = mangled;
10568 need_copy = 0;
10569 }
10570 }
10571
10572 if (canon == NULL || check_physname)
10573 {
10574 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10575
10576 if (canon != NULL && strcmp (physname, canon) != 0)
10577 {
10578 /* It may not mean a bug in GDB. The compiler could also
10579 compute DW_AT_linkage_name incorrectly. But in such case
10580 GDB would need to be bug-to-bug compatible. */
10581
10582 complaint (_("Computed physname <%s> does not match demangled <%s> "
10583 "(from linkage <%s>) - DIE at %s [in module %s]"),
10584 physname, canon, mangled, sect_offset_str (die->sect_off),
10585 objfile_name (objfile));
10586
10587 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10588 is available here - over computed PHYSNAME. It is safer
10589 against both buggy GDB and buggy compilers. */
10590
10591 retval = canon;
10592 }
10593 else
10594 {
10595 retval = physname;
10596 need_copy = 0;
10597 }
10598 }
10599 else
10600 retval = canon;
10601
10602 if (need_copy)
10603 retval = objfile->intern (retval);
10604
10605 return retval;
10606 }
10607
10608 /* Inspect DIE in CU for a namespace alias. If one exists, record
10609 a new symbol for it.
10610
10611 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10612
10613 static int
10614 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10615 {
10616 struct attribute *attr;
10617
10618 /* If the die does not have a name, this is not a namespace
10619 alias. */
10620 attr = dwarf2_attr (die, DW_AT_name, cu);
10621 if (attr != NULL)
10622 {
10623 int num;
10624 struct die_info *d = die;
10625 struct dwarf2_cu *imported_cu = cu;
10626
10627 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10628 keep inspecting DIEs until we hit the underlying import. */
10629 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10630 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10631 {
10632 attr = dwarf2_attr (d, DW_AT_import, cu);
10633 if (attr == NULL)
10634 break;
10635
10636 d = follow_die_ref (d, attr, &imported_cu);
10637 if (d->tag != DW_TAG_imported_declaration)
10638 break;
10639 }
10640
10641 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10642 {
10643 complaint (_("DIE at %s has too many recursively imported "
10644 "declarations"), sect_offset_str (d->sect_off));
10645 return 0;
10646 }
10647
10648 if (attr != NULL)
10649 {
10650 struct type *type;
10651 sect_offset sect_off = attr->get_ref_die_offset ();
10652
10653 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10654 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10655 {
10656 /* This declaration is a global namespace alias. Add
10657 a symbol for it whose type is the aliased namespace. */
10658 new_symbol (die, type, cu);
10659 return 1;
10660 }
10661 }
10662 }
10663
10664 return 0;
10665 }
10666
10667 /* Return the using directives repository (global or local?) to use in the
10668 current context for CU.
10669
10670 For Ada, imported declarations can materialize renamings, which *may* be
10671 global. However it is impossible (for now?) in DWARF to distinguish
10672 "external" imported declarations and "static" ones. As all imported
10673 declarations seem to be static in all other languages, make them all CU-wide
10674 global only in Ada. */
10675
10676 static struct using_direct **
10677 using_directives (struct dwarf2_cu *cu)
10678 {
10679 if (cu->language == language_ada
10680 && cu->get_builder ()->outermost_context_p ())
10681 return cu->get_builder ()->get_global_using_directives ();
10682 else
10683 return cu->get_builder ()->get_local_using_directives ();
10684 }
10685
10686 /* Read the import statement specified by the given die and record it. */
10687
10688 static void
10689 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10690 {
10691 struct objfile *objfile = cu->per_objfile->objfile;
10692 struct attribute *import_attr;
10693 struct die_info *imported_die, *child_die;
10694 struct dwarf2_cu *imported_cu;
10695 const char *imported_name;
10696 const char *imported_name_prefix;
10697 const char *canonical_name;
10698 const char *import_alias;
10699 const char *imported_declaration = NULL;
10700 const char *import_prefix;
10701 std::vector<const char *> excludes;
10702
10703 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10704 if (import_attr == NULL)
10705 {
10706 complaint (_("Tag '%s' has no DW_AT_import"),
10707 dwarf_tag_name (die->tag));
10708 return;
10709 }
10710
10711 imported_cu = cu;
10712 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10713 imported_name = dwarf2_name (imported_die, imported_cu);
10714 if (imported_name == NULL)
10715 {
10716 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10717
10718 The import in the following code:
10719 namespace A
10720 {
10721 typedef int B;
10722 }
10723
10724 int main ()
10725 {
10726 using A::B;
10727 B b;
10728 return b;
10729 }
10730
10731 ...
10732 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10733 <52> DW_AT_decl_file : 1
10734 <53> DW_AT_decl_line : 6
10735 <54> DW_AT_import : <0x75>
10736 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10737 <59> DW_AT_name : B
10738 <5b> DW_AT_decl_file : 1
10739 <5c> DW_AT_decl_line : 2
10740 <5d> DW_AT_type : <0x6e>
10741 ...
10742 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10743 <76> DW_AT_byte_size : 4
10744 <77> DW_AT_encoding : 5 (signed)
10745
10746 imports the wrong die ( 0x75 instead of 0x58 ).
10747 This case will be ignored until the gcc bug is fixed. */
10748 return;
10749 }
10750
10751 /* Figure out the local name after import. */
10752 import_alias = dwarf2_name (die, cu);
10753
10754 /* Figure out where the statement is being imported to. */
10755 import_prefix = determine_prefix (die, cu);
10756
10757 /* Figure out what the scope of the imported die is and prepend it
10758 to the name of the imported die. */
10759 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10760
10761 if (imported_die->tag != DW_TAG_namespace
10762 && imported_die->tag != DW_TAG_module)
10763 {
10764 imported_declaration = imported_name;
10765 canonical_name = imported_name_prefix;
10766 }
10767 else if (strlen (imported_name_prefix) > 0)
10768 canonical_name = obconcat (&objfile->objfile_obstack,
10769 imported_name_prefix,
10770 (cu->language == language_d ? "." : "::"),
10771 imported_name, (char *) NULL);
10772 else
10773 canonical_name = imported_name;
10774
10775 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10776 for (child_die = die->child; child_die && child_die->tag;
10777 child_die = child_die->sibling)
10778 {
10779 /* DWARF-4: A Fortran use statement with a “rename list” may be
10780 represented by an imported module entry with an import attribute
10781 referring to the module and owned entries corresponding to those
10782 entities that are renamed as part of being imported. */
10783
10784 if (child_die->tag != DW_TAG_imported_declaration)
10785 {
10786 complaint (_("child DW_TAG_imported_declaration expected "
10787 "- DIE at %s [in module %s]"),
10788 sect_offset_str (child_die->sect_off),
10789 objfile_name (objfile));
10790 continue;
10791 }
10792
10793 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10794 if (import_attr == NULL)
10795 {
10796 complaint (_("Tag '%s' has no DW_AT_import"),
10797 dwarf_tag_name (child_die->tag));
10798 continue;
10799 }
10800
10801 imported_cu = cu;
10802 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10803 &imported_cu);
10804 imported_name = dwarf2_name (imported_die, imported_cu);
10805 if (imported_name == NULL)
10806 {
10807 complaint (_("child DW_TAG_imported_declaration has unknown "
10808 "imported name - DIE at %s [in module %s]"),
10809 sect_offset_str (child_die->sect_off),
10810 objfile_name (objfile));
10811 continue;
10812 }
10813
10814 excludes.push_back (imported_name);
10815
10816 process_die (child_die, cu);
10817 }
10818
10819 add_using_directive (using_directives (cu),
10820 import_prefix,
10821 canonical_name,
10822 import_alias,
10823 imported_declaration,
10824 excludes,
10825 0,
10826 &objfile->objfile_obstack);
10827 }
10828
10829 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10830 types, but gives them a size of zero. Starting with version 14,
10831 ICC is compatible with GCC. */
10832
10833 static bool
10834 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10835 {
10836 if (!cu->checked_producer)
10837 check_producer (cu);
10838
10839 return cu->producer_is_icc_lt_14;
10840 }
10841
10842 /* ICC generates a DW_AT_type for C void functions. This was observed on
10843 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10844 which says that void functions should not have a DW_AT_type. */
10845
10846 static bool
10847 producer_is_icc (struct dwarf2_cu *cu)
10848 {
10849 if (!cu->checked_producer)
10850 check_producer (cu);
10851
10852 return cu->producer_is_icc;
10853 }
10854
10855 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10856 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10857 this, it was first present in GCC release 4.3.0. */
10858
10859 static bool
10860 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10861 {
10862 if (!cu->checked_producer)
10863 check_producer (cu);
10864
10865 return cu->producer_is_gcc_lt_4_3;
10866 }
10867
10868 static file_and_directory
10869 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10870 {
10871 file_and_directory res;
10872
10873 /* Find the filename. Do not use dwarf2_name here, since the filename
10874 is not a source language identifier. */
10875 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10876 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10877
10878 if (res.comp_dir == NULL
10879 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10880 && IS_ABSOLUTE_PATH (res.name))
10881 {
10882 res.comp_dir_storage = ldirname (res.name);
10883 if (!res.comp_dir_storage.empty ())
10884 res.comp_dir = res.comp_dir_storage.c_str ();
10885 }
10886 if (res.comp_dir != NULL)
10887 {
10888 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10889 directory, get rid of it. */
10890 const char *cp = strchr (res.comp_dir, ':');
10891
10892 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10893 res.comp_dir = cp + 1;
10894 }
10895
10896 if (res.name == NULL)
10897 res.name = "<unknown>";
10898
10899 return res;
10900 }
10901
10902 /* Handle DW_AT_stmt_list for a compilation unit.
10903 DIE is the DW_TAG_compile_unit die for CU.
10904 COMP_DIR is the compilation directory. LOWPC is passed to
10905 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10906
10907 static void
10908 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10909 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10910 {
10911 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10912 struct attribute *attr;
10913 struct line_header line_header_local;
10914 hashval_t line_header_local_hash;
10915 void **slot;
10916 int decode_mapping;
10917
10918 gdb_assert (! cu->per_cu->is_debug_types);
10919
10920 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10921 if (attr == NULL)
10922 return;
10923
10924 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10925
10926 /* The line header hash table is only created if needed (it exists to
10927 prevent redundant reading of the line table for partial_units).
10928 If we're given a partial_unit, we'll need it. If we're given a
10929 compile_unit, then use the line header hash table if it's already
10930 created, but don't create one just yet. */
10931
10932 if (per_objfile->line_header_hash == NULL
10933 && die->tag == DW_TAG_partial_unit)
10934 {
10935 per_objfile->line_header_hash
10936 .reset (htab_create_alloc (127, line_header_hash_voidp,
10937 line_header_eq_voidp,
10938 free_line_header_voidp,
10939 xcalloc, xfree));
10940 }
10941
10942 line_header_local.sect_off = line_offset;
10943 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10944 line_header_local_hash = line_header_hash (&line_header_local);
10945 if (per_objfile->line_header_hash != NULL)
10946 {
10947 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10948 &line_header_local,
10949 line_header_local_hash, NO_INSERT);
10950
10951 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10952 is not present in *SLOT (since if there is something in *SLOT then
10953 it will be for a partial_unit). */
10954 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10955 {
10956 gdb_assert (*slot != NULL);
10957 cu->line_header = (struct line_header *) *slot;
10958 return;
10959 }
10960 }
10961
10962 /* dwarf_decode_line_header does not yet provide sufficient information.
10963 We always have to call also dwarf_decode_lines for it. */
10964 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10965 if (lh == NULL)
10966 return;
10967
10968 cu->line_header = lh.release ();
10969 cu->line_header_die_owner = die;
10970
10971 if (per_objfile->line_header_hash == NULL)
10972 slot = NULL;
10973 else
10974 {
10975 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10976 &line_header_local,
10977 line_header_local_hash, INSERT);
10978 gdb_assert (slot != NULL);
10979 }
10980 if (slot != NULL && *slot == NULL)
10981 {
10982 /* This newly decoded line number information unit will be owned
10983 by line_header_hash hash table. */
10984 *slot = cu->line_header;
10985 cu->line_header_die_owner = NULL;
10986 }
10987 else
10988 {
10989 /* We cannot free any current entry in (*slot) as that struct line_header
10990 may be already used by multiple CUs. Create only temporary decoded
10991 line_header for this CU - it may happen at most once for each line
10992 number information unit. And if we're not using line_header_hash
10993 then this is what we want as well. */
10994 gdb_assert (die->tag != DW_TAG_partial_unit);
10995 }
10996 decode_mapping = (die->tag != DW_TAG_partial_unit);
10997 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10998 decode_mapping);
10999
11000 }
11001
11002 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11003
11004 static void
11005 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11006 {
11007 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11008 struct objfile *objfile = per_objfile->objfile;
11009 struct gdbarch *gdbarch = objfile->arch ();
11010 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11011 CORE_ADDR highpc = ((CORE_ADDR) 0);
11012 struct attribute *attr;
11013 struct die_info *child_die;
11014 CORE_ADDR baseaddr;
11015
11016 prepare_one_comp_unit (cu, die, cu->language);
11017 baseaddr = objfile->text_section_offset ();
11018
11019 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11020
11021 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11022 from finish_block. */
11023 if (lowpc == ((CORE_ADDR) -1))
11024 lowpc = highpc;
11025 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11026
11027 file_and_directory fnd = find_file_and_directory (die, cu);
11028
11029 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11030 standardised yet. As a workaround for the language detection we fall
11031 back to the DW_AT_producer string. */
11032 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11033 cu->language = language_opencl;
11034
11035 /* Similar hack for Go. */
11036 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11037 set_cu_language (DW_LANG_Go, cu);
11038
11039 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11040
11041 /* Decode line number information if present. We do this before
11042 processing child DIEs, so that the line header table is available
11043 for DW_AT_decl_file. */
11044 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11045
11046 /* Process all dies in compilation unit. */
11047 if (die->child != NULL)
11048 {
11049 child_die = die->child;
11050 while (child_die && child_die->tag)
11051 {
11052 process_die (child_die, cu);
11053 child_die = child_die->sibling;
11054 }
11055 }
11056
11057 /* Decode macro information, if present. Dwarf 2 macro information
11058 refers to information in the line number info statement program
11059 header, so we can only read it if we've read the header
11060 successfully. */
11061 attr = dwarf2_attr (die, DW_AT_macros, cu);
11062 if (attr == NULL)
11063 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11064 if (attr && cu->line_header)
11065 {
11066 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11067 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11068
11069 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11070 }
11071 else
11072 {
11073 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11074 if (attr && cu->line_header)
11075 {
11076 unsigned int macro_offset = DW_UNSND (attr);
11077
11078 dwarf_decode_macros (cu, macro_offset, 0);
11079 }
11080 }
11081 }
11082
11083 void
11084 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11085 {
11086 struct type_unit_group *tu_group;
11087 int first_time;
11088 struct attribute *attr;
11089 unsigned int i;
11090 struct signatured_type *sig_type;
11091
11092 gdb_assert (per_cu->is_debug_types);
11093 sig_type = (struct signatured_type *) per_cu;
11094
11095 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11096
11097 /* If we're using .gdb_index (includes -readnow) then
11098 per_cu->type_unit_group may not have been set up yet. */
11099 if (sig_type->type_unit_group == NULL)
11100 sig_type->type_unit_group = get_type_unit_group (this, attr);
11101 tu_group = sig_type->type_unit_group;
11102
11103 /* If we've already processed this stmt_list there's no real need to
11104 do it again, we could fake it and just recreate the part we need
11105 (file name,index -> symtab mapping). If data shows this optimization
11106 is useful we can do it then. */
11107 type_unit_group_unshareable *tug_unshare
11108 = per_objfile->get_type_unit_group_unshareable (tu_group);
11109 first_time = tug_unshare->compunit_symtab == NULL;
11110
11111 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11112 debug info. */
11113 line_header_up lh;
11114 if (attr != NULL)
11115 {
11116 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11117 lh = dwarf_decode_line_header (line_offset, this);
11118 }
11119 if (lh == NULL)
11120 {
11121 if (first_time)
11122 start_symtab ("", NULL, 0);
11123 else
11124 {
11125 gdb_assert (tug_unshare->symtabs == NULL);
11126 gdb_assert (m_builder == nullptr);
11127 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11128 m_builder.reset (new struct buildsym_compunit
11129 (COMPUNIT_OBJFILE (cust), "",
11130 COMPUNIT_DIRNAME (cust),
11131 compunit_language (cust),
11132 0, cust));
11133 list_in_scope = get_builder ()->get_file_symbols ();
11134 }
11135 return;
11136 }
11137
11138 line_header = lh.release ();
11139 line_header_die_owner = die;
11140
11141 if (first_time)
11142 {
11143 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11144
11145 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11146 still initializing it, and our caller (a few levels up)
11147 process_full_type_unit still needs to know if this is the first
11148 time. */
11149
11150 tug_unshare->symtabs
11151 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11152 struct symtab *, line_header->file_names_size ());
11153
11154 auto &file_names = line_header->file_names ();
11155 for (i = 0; i < file_names.size (); ++i)
11156 {
11157 file_entry &fe = file_names[i];
11158 dwarf2_start_subfile (this, fe.name,
11159 fe.include_dir (line_header));
11160 buildsym_compunit *b = get_builder ();
11161 if (b->get_current_subfile ()->symtab == NULL)
11162 {
11163 /* NOTE: start_subfile will recognize when it's been
11164 passed a file it has already seen. So we can't
11165 assume there's a simple mapping from
11166 cu->line_header->file_names to subfiles, plus
11167 cu->line_header->file_names may contain dups. */
11168 b->get_current_subfile ()->symtab
11169 = allocate_symtab (cust, b->get_current_subfile ()->name);
11170 }
11171
11172 fe.symtab = b->get_current_subfile ()->symtab;
11173 tug_unshare->symtabs[i] = fe.symtab;
11174 }
11175 }
11176 else
11177 {
11178 gdb_assert (m_builder == nullptr);
11179 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11180 m_builder.reset (new struct buildsym_compunit
11181 (COMPUNIT_OBJFILE (cust), "",
11182 COMPUNIT_DIRNAME (cust),
11183 compunit_language (cust),
11184 0, cust));
11185 list_in_scope = get_builder ()->get_file_symbols ();
11186
11187 auto &file_names = line_header->file_names ();
11188 for (i = 0; i < file_names.size (); ++i)
11189 {
11190 file_entry &fe = file_names[i];
11191 fe.symtab = tug_unshare->symtabs[i];
11192 }
11193 }
11194
11195 /* The main symtab is allocated last. Type units don't have DW_AT_name
11196 so they don't have a "real" (so to speak) symtab anyway.
11197 There is later code that will assign the main symtab to all symbols
11198 that don't have one. We need to handle the case of a symbol with a
11199 missing symtab (DW_AT_decl_file) anyway. */
11200 }
11201
11202 /* Process DW_TAG_type_unit.
11203 For TUs we want to skip the first top level sibling if it's not the
11204 actual type being defined by this TU. In this case the first top
11205 level sibling is there to provide context only. */
11206
11207 static void
11208 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11209 {
11210 struct die_info *child_die;
11211
11212 prepare_one_comp_unit (cu, die, language_minimal);
11213
11214 /* Initialize (or reinitialize) the machinery for building symtabs.
11215 We do this before processing child DIEs, so that the line header table
11216 is available for DW_AT_decl_file. */
11217 cu->setup_type_unit_groups (die);
11218
11219 if (die->child != NULL)
11220 {
11221 child_die = die->child;
11222 while (child_die && child_die->tag)
11223 {
11224 process_die (child_die, cu);
11225 child_die = child_die->sibling;
11226 }
11227 }
11228 }
11229 \f
11230 /* DWO/DWP files.
11231
11232 http://gcc.gnu.org/wiki/DebugFission
11233 http://gcc.gnu.org/wiki/DebugFissionDWP
11234
11235 To simplify handling of both DWO files ("object" files with the DWARF info)
11236 and DWP files (a file with the DWOs packaged up into one file), we treat
11237 DWP files as having a collection of virtual DWO files. */
11238
11239 static hashval_t
11240 hash_dwo_file (const void *item)
11241 {
11242 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11243 hashval_t hash;
11244
11245 hash = htab_hash_string (dwo_file->dwo_name);
11246 if (dwo_file->comp_dir != NULL)
11247 hash += htab_hash_string (dwo_file->comp_dir);
11248 return hash;
11249 }
11250
11251 static int
11252 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11253 {
11254 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11255 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11256
11257 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11258 return 0;
11259 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11260 return lhs->comp_dir == rhs->comp_dir;
11261 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11262 }
11263
11264 /* Allocate a hash table for DWO files. */
11265
11266 static htab_up
11267 allocate_dwo_file_hash_table ()
11268 {
11269 auto delete_dwo_file = [] (void *item)
11270 {
11271 struct dwo_file *dwo_file = (struct dwo_file *) item;
11272
11273 delete dwo_file;
11274 };
11275
11276 return htab_up (htab_create_alloc (41,
11277 hash_dwo_file,
11278 eq_dwo_file,
11279 delete_dwo_file,
11280 xcalloc, xfree));
11281 }
11282
11283 /* Lookup DWO file DWO_NAME. */
11284
11285 static void **
11286 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11287 const char *dwo_name,
11288 const char *comp_dir)
11289 {
11290 struct dwo_file find_entry;
11291 void **slot;
11292
11293 if (per_objfile->per_bfd->dwo_files == NULL)
11294 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11295
11296 find_entry.dwo_name = dwo_name;
11297 find_entry.comp_dir = comp_dir;
11298 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11299 INSERT);
11300
11301 return slot;
11302 }
11303
11304 static hashval_t
11305 hash_dwo_unit (const void *item)
11306 {
11307 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11308
11309 /* This drops the top 32 bits of the id, but is ok for a hash. */
11310 return dwo_unit->signature;
11311 }
11312
11313 static int
11314 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11315 {
11316 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11317 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11318
11319 /* The signature is assumed to be unique within the DWO file.
11320 So while object file CU dwo_id's always have the value zero,
11321 that's OK, assuming each object file DWO file has only one CU,
11322 and that's the rule for now. */
11323 return lhs->signature == rhs->signature;
11324 }
11325
11326 /* Allocate a hash table for DWO CUs,TUs.
11327 There is one of these tables for each of CUs,TUs for each DWO file. */
11328
11329 static htab_up
11330 allocate_dwo_unit_table ()
11331 {
11332 /* Start out with a pretty small number.
11333 Generally DWO files contain only one CU and maybe some TUs. */
11334 return htab_up (htab_create_alloc (3,
11335 hash_dwo_unit,
11336 eq_dwo_unit,
11337 NULL, xcalloc, xfree));
11338 }
11339
11340 /* die_reader_func for create_dwo_cu. */
11341
11342 static void
11343 create_dwo_cu_reader (const struct die_reader_specs *reader,
11344 const gdb_byte *info_ptr,
11345 struct die_info *comp_unit_die,
11346 struct dwo_file *dwo_file,
11347 struct dwo_unit *dwo_unit)
11348 {
11349 struct dwarf2_cu *cu = reader->cu;
11350 sect_offset sect_off = cu->per_cu->sect_off;
11351 struct dwarf2_section_info *section = cu->per_cu->section;
11352
11353 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11354 if (!signature.has_value ())
11355 {
11356 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11357 " its dwo_id [in module %s]"),
11358 sect_offset_str (sect_off), dwo_file->dwo_name);
11359 return;
11360 }
11361
11362 dwo_unit->dwo_file = dwo_file;
11363 dwo_unit->signature = *signature;
11364 dwo_unit->section = section;
11365 dwo_unit->sect_off = sect_off;
11366 dwo_unit->length = cu->per_cu->length;
11367
11368 if (dwarf_read_debug)
11369 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11370 sect_offset_str (sect_off),
11371 hex_string (dwo_unit->signature));
11372 }
11373
11374 /* Create the dwo_units for the CUs in a DWO_FILE.
11375 Note: This function processes DWO files only, not DWP files. */
11376
11377 static void
11378 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11379 dwarf2_cu *cu, struct dwo_file &dwo_file,
11380 dwarf2_section_info &section, htab_up &cus_htab)
11381 {
11382 struct objfile *objfile = per_objfile->objfile;
11383 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11384 const gdb_byte *info_ptr, *end_ptr;
11385
11386 section.read (objfile);
11387 info_ptr = section.buffer;
11388
11389 if (info_ptr == NULL)
11390 return;
11391
11392 if (dwarf_read_debug)
11393 {
11394 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11395 section.get_name (),
11396 section.get_file_name ());
11397 }
11398
11399 end_ptr = info_ptr + section.size;
11400 while (info_ptr < end_ptr)
11401 {
11402 struct dwarf2_per_cu_data per_cu;
11403 struct dwo_unit read_unit {};
11404 struct dwo_unit *dwo_unit;
11405 void **slot;
11406 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11407
11408 memset (&per_cu, 0, sizeof (per_cu));
11409 per_cu.per_bfd = per_bfd;
11410 per_cu.is_debug_types = 0;
11411 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11412 per_cu.section = &section;
11413
11414 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11415 if (!reader.dummy_p)
11416 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11417 &dwo_file, &read_unit);
11418 info_ptr += per_cu.length;
11419
11420 // If the unit could not be parsed, skip it.
11421 if (read_unit.dwo_file == NULL)
11422 continue;
11423
11424 if (cus_htab == NULL)
11425 cus_htab = allocate_dwo_unit_table ();
11426
11427 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11428 struct dwo_unit);
11429 *dwo_unit = read_unit;
11430 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11431 gdb_assert (slot != NULL);
11432 if (*slot != NULL)
11433 {
11434 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11435 sect_offset dup_sect_off = dup_cu->sect_off;
11436
11437 complaint (_("debug cu entry at offset %s is duplicate to"
11438 " the entry at offset %s, signature %s"),
11439 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11440 hex_string (dwo_unit->signature));
11441 }
11442 *slot = (void *)dwo_unit;
11443 }
11444 }
11445
11446 /* DWP file .debug_{cu,tu}_index section format:
11447 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11448
11449 DWP Version 1:
11450
11451 Both index sections have the same format, and serve to map a 64-bit
11452 signature to a set of section numbers. Each section begins with a header,
11453 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11454 indexes, and a pool of 32-bit section numbers. The index sections will be
11455 aligned at 8-byte boundaries in the file.
11456
11457 The index section header consists of:
11458
11459 V, 32 bit version number
11460 -, 32 bits unused
11461 N, 32 bit number of compilation units or type units in the index
11462 M, 32 bit number of slots in the hash table
11463
11464 Numbers are recorded using the byte order of the application binary.
11465
11466 The hash table begins at offset 16 in the section, and consists of an array
11467 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11468 order of the application binary). Unused slots in the hash table are 0.
11469 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11470
11471 The parallel table begins immediately after the hash table
11472 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11473 array of 32-bit indexes (using the byte order of the application binary),
11474 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11475 table contains a 32-bit index into the pool of section numbers. For unused
11476 hash table slots, the corresponding entry in the parallel table will be 0.
11477
11478 The pool of section numbers begins immediately following the hash table
11479 (at offset 16 + 12 * M from the beginning of the section). The pool of
11480 section numbers consists of an array of 32-bit words (using the byte order
11481 of the application binary). Each item in the array is indexed starting
11482 from 0. The hash table entry provides the index of the first section
11483 number in the set. Additional section numbers in the set follow, and the
11484 set is terminated by a 0 entry (section number 0 is not used in ELF).
11485
11486 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11487 section must be the first entry in the set, and the .debug_abbrev.dwo must
11488 be the second entry. Other members of the set may follow in any order.
11489
11490 ---
11491
11492 DWP Version 2:
11493
11494 DWP Version 2 combines all the .debug_info, etc. sections into one,
11495 and the entries in the index tables are now offsets into these sections.
11496 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11497 section.
11498
11499 Index Section Contents:
11500 Header
11501 Hash Table of Signatures dwp_hash_table.hash_table
11502 Parallel Table of Indices dwp_hash_table.unit_table
11503 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11504 Table of Section Sizes dwp_hash_table.v2.sizes
11505
11506 The index section header consists of:
11507
11508 V, 32 bit version number
11509 L, 32 bit number of columns in the table of section offsets
11510 N, 32 bit number of compilation units or type units in the index
11511 M, 32 bit number of slots in the hash table
11512
11513 Numbers are recorded using the byte order of the application binary.
11514
11515 The hash table has the same format as version 1.
11516 The parallel table of indices has the same format as version 1,
11517 except that the entries are origin-1 indices into the table of sections
11518 offsets and the table of section sizes.
11519
11520 The table of offsets begins immediately following the parallel table
11521 (at offset 16 + 12 * M from the beginning of the section). The table is
11522 a two-dimensional array of 32-bit words (using the byte order of the
11523 application binary), with L columns and N+1 rows, in row-major order.
11524 Each row in the array is indexed starting from 0. The first row provides
11525 a key to the remaining rows: each column in this row provides an identifier
11526 for a debug section, and the offsets in the same column of subsequent rows
11527 refer to that section. The section identifiers are:
11528
11529 DW_SECT_INFO 1 .debug_info.dwo
11530 DW_SECT_TYPES 2 .debug_types.dwo
11531 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11532 DW_SECT_LINE 4 .debug_line.dwo
11533 DW_SECT_LOC 5 .debug_loc.dwo
11534 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11535 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11536 DW_SECT_MACRO 8 .debug_macro.dwo
11537
11538 The offsets provided by the CU and TU index sections are the base offsets
11539 for the contributions made by each CU or TU to the corresponding section
11540 in the package file. Each CU and TU header contains an abbrev_offset
11541 field, used to find the abbreviations table for that CU or TU within the
11542 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11543 be interpreted as relative to the base offset given in the index section.
11544 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11545 should be interpreted as relative to the base offset for .debug_line.dwo,
11546 and offsets into other debug sections obtained from DWARF attributes should
11547 also be interpreted as relative to the corresponding base offset.
11548
11549 The table of sizes begins immediately following the table of offsets.
11550 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11551 with L columns and N rows, in row-major order. Each row in the array is
11552 indexed starting from 1 (row 0 is shared by the two tables).
11553
11554 ---
11555
11556 Hash table lookup is handled the same in version 1 and 2:
11557
11558 We assume that N and M will not exceed 2^32 - 1.
11559 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11560
11561 Given a 64-bit compilation unit signature or a type signature S, an entry
11562 in the hash table is located as follows:
11563
11564 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11565 the low-order k bits all set to 1.
11566
11567 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11568
11569 3) If the hash table entry at index H matches the signature, use that
11570 entry. If the hash table entry at index H is unused (all zeroes),
11571 terminate the search: the signature is not present in the table.
11572
11573 4) Let H = (H + H') modulo M. Repeat at Step 3.
11574
11575 Because M > N and H' and M are relatively prime, the search is guaranteed
11576 to stop at an unused slot or find the match. */
11577
11578 /* Create a hash table to map DWO IDs to their CU/TU entry in
11579 .debug_{info,types}.dwo in DWP_FILE.
11580 Returns NULL if there isn't one.
11581 Note: This function processes DWP files only, not DWO files. */
11582
11583 static struct dwp_hash_table *
11584 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11585 struct dwp_file *dwp_file, int is_debug_types)
11586 {
11587 struct objfile *objfile = per_objfile->objfile;
11588 bfd *dbfd = dwp_file->dbfd.get ();
11589 const gdb_byte *index_ptr, *index_end;
11590 struct dwarf2_section_info *index;
11591 uint32_t version, nr_columns, nr_units, nr_slots;
11592 struct dwp_hash_table *htab;
11593
11594 if (is_debug_types)
11595 index = &dwp_file->sections.tu_index;
11596 else
11597 index = &dwp_file->sections.cu_index;
11598
11599 if (index->empty ())
11600 return NULL;
11601 index->read (objfile);
11602
11603 index_ptr = index->buffer;
11604 index_end = index_ptr + index->size;
11605
11606 version = read_4_bytes (dbfd, index_ptr);
11607 index_ptr += 4;
11608 if (version == 2)
11609 nr_columns = read_4_bytes (dbfd, index_ptr);
11610 else
11611 nr_columns = 0;
11612 index_ptr += 4;
11613 nr_units = read_4_bytes (dbfd, index_ptr);
11614 index_ptr += 4;
11615 nr_slots = read_4_bytes (dbfd, index_ptr);
11616 index_ptr += 4;
11617
11618 if (version != 1 && version != 2)
11619 {
11620 error (_("Dwarf Error: unsupported DWP file version (%s)"
11621 " [in module %s]"),
11622 pulongest (version), dwp_file->name);
11623 }
11624 if (nr_slots != (nr_slots & -nr_slots))
11625 {
11626 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11627 " is not power of 2 [in module %s]"),
11628 pulongest (nr_slots), dwp_file->name);
11629 }
11630
11631 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11632 htab->version = version;
11633 htab->nr_columns = nr_columns;
11634 htab->nr_units = nr_units;
11635 htab->nr_slots = nr_slots;
11636 htab->hash_table = index_ptr;
11637 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11638
11639 /* Exit early if the table is empty. */
11640 if (nr_slots == 0 || nr_units == 0
11641 || (version == 2 && nr_columns == 0))
11642 {
11643 /* All must be zero. */
11644 if (nr_slots != 0 || nr_units != 0
11645 || (version == 2 && nr_columns != 0))
11646 {
11647 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11648 " all zero [in modules %s]"),
11649 dwp_file->name);
11650 }
11651 return htab;
11652 }
11653
11654 if (version == 1)
11655 {
11656 htab->section_pool.v1.indices =
11657 htab->unit_table + sizeof (uint32_t) * nr_slots;
11658 /* It's harder to decide whether the section is too small in v1.
11659 V1 is deprecated anyway so we punt. */
11660 }
11661 else
11662 {
11663 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11664 int *ids = htab->section_pool.v2.section_ids;
11665 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11666 /* Reverse map for error checking. */
11667 int ids_seen[DW_SECT_MAX + 1];
11668 int i;
11669
11670 if (nr_columns < 2)
11671 {
11672 error (_("Dwarf Error: bad DWP hash table, too few columns"
11673 " in section table [in module %s]"),
11674 dwp_file->name);
11675 }
11676 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11677 {
11678 error (_("Dwarf Error: bad DWP hash table, too many columns"
11679 " in section table [in module %s]"),
11680 dwp_file->name);
11681 }
11682 memset (ids, 255, sizeof_ids);
11683 memset (ids_seen, 255, sizeof (ids_seen));
11684 for (i = 0; i < nr_columns; ++i)
11685 {
11686 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11687
11688 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11689 {
11690 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11691 " in section table [in module %s]"),
11692 id, dwp_file->name);
11693 }
11694 if (ids_seen[id] != -1)
11695 {
11696 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11697 " id %d in section table [in module %s]"),
11698 id, dwp_file->name);
11699 }
11700 ids_seen[id] = i;
11701 ids[i] = id;
11702 }
11703 /* Must have exactly one info or types section. */
11704 if (((ids_seen[DW_SECT_INFO] != -1)
11705 + (ids_seen[DW_SECT_TYPES] != -1))
11706 != 1)
11707 {
11708 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11709 " DWO info/types section [in module %s]"),
11710 dwp_file->name);
11711 }
11712 /* Must have an abbrev section. */
11713 if (ids_seen[DW_SECT_ABBREV] == -1)
11714 {
11715 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11716 " section [in module %s]"),
11717 dwp_file->name);
11718 }
11719 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11720 htab->section_pool.v2.sizes =
11721 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11722 * nr_units * nr_columns);
11723 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11724 * nr_units * nr_columns))
11725 > index_end)
11726 {
11727 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11728 " [in module %s]"),
11729 dwp_file->name);
11730 }
11731 }
11732
11733 return htab;
11734 }
11735
11736 /* Update SECTIONS with the data from SECTP.
11737
11738 This function is like the other "locate" section routines that are
11739 passed to bfd_map_over_sections, but in this context the sections to
11740 read comes from the DWP V1 hash table, not the full ELF section table.
11741
11742 The result is non-zero for success, or zero if an error was found. */
11743
11744 static int
11745 locate_v1_virtual_dwo_sections (asection *sectp,
11746 struct virtual_v1_dwo_sections *sections)
11747 {
11748 const struct dwop_section_names *names = &dwop_section_names;
11749
11750 if (section_is_p (sectp->name, &names->abbrev_dwo))
11751 {
11752 /* There can be only one. */
11753 if (sections->abbrev.s.section != NULL)
11754 return 0;
11755 sections->abbrev.s.section = sectp;
11756 sections->abbrev.size = bfd_section_size (sectp);
11757 }
11758 else if (section_is_p (sectp->name, &names->info_dwo)
11759 || section_is_p (sectp->name, &names->types_dwo))
11760 {
11761 /* There can be only one. */
11762 if (sections->info_or_types.s.section != NULL)
11763 return 0;
11764 sections->info_or_types.s.section = sectp;
11765 sections->info_or_types.size = bfd_section_size (sectp);
11766 }
11767 else if (section_is_p (sectp->name, &names->line_dwo))
11768 {
11769 /* There can be only one. */
11770 if (sections->line.s.section != NULL)
11771 return 0;
11772 sections->line.s.section = sectp;
11773 sections->line.size = bfd_section_size (sectp);
11774 }
11775 else if (section_is_p (sectp->name, &names->loc_dwo))
11776 {
11777 /* There can be only one. */
11778 if (sections->loc.s.section != NULL)
11779 return 0;
11780 sections->loc.s.section = sectp;
11781 sections->loc.size = bfd_section_size (sectp);
11782 }
11783 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11784 {
11785 /* There can be only one. */
11786 if (sections->macinfo.s.section != NULL)
11787 return 0;
11788 sections->macinfo.s.section = sectp;
11789 sections->macinfo.size = bfd_section_size (sectp);
11790 }
11791 else if (section_is_p (sectp->name, &names->macro_dwo))
11792 {
11793 /* There can be only one. */
11794 if (sections->macro.s.section != NULL)
11795 return 0;
11796 sections->macro.s.section = sectp;
11797 sections->macro.size = bfd_section_size (sectp);
11798 }
11799 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11800 {
11801 /* There can be only one. */
11802 if (sections->str_offsets.s.section != NULL)
11803 return 0;
11804 sections->str_offsets.s.section = sectp;
11805 sections->str_offsets.size = bfd_section_size (sectp);
11806 }
11807 else
11808 {
11809 /* No other kind of section is valid. */
11810 return 0;
11811 }
11812
11813 return 1;
11814 }
11815
11816 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11817 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11818 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11819 This is for DWP version 1 files. */
11820
11821 static struct dwo_unit *
11822 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11823 struct dwp_file *dwp_file,
11824 uint32_t unit_index,
11825 const char *comp_dir,
11826 ULONGEST signature, int is_debug_types)
11827 {
11828 const struct dwp_hash_table *dwp_htab =
11829 is_debug_types ? dwp_file->tus : dwp_file->cus;
11830 bfd *dbfd = dwp_file->dbfd.get ();
11831 const char *kind = is_debug_types ? "TU" : "CU";
11832 struct dwo_file *dwo_file;
11833 struct dwo_unit *dwo_unit;
11834 struct virtual_v1_dwo_sections sections;
11835 void **dwo_file_slot;
11836 int i;
11837
11838 gdb_assert (dwp_file->version == 1);
11839
11840 if (dwarf_read_debug)
11841 {
11842 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11843 kind,
11844 pulongest (unit_index), hex_string (signature),
11845 dwp_file->name);
11846 }
11847
11848 /* Fetch the sections of this DWO unit.
11849 Put a limit on the number of sections we look for so that bad data
11850 doesn't cause us to loop forever. */
11851
11852 #define MAX_NR_V1_DWO_SECTIONS \
11853 (1 /* .debug_info or .debug_types */ \
11854 + 1 /* .debug_abbrev */ \
11855 + 1 /* .debug_line */ \
11856 + 1 /* .debug_loc */ \
11857 + 1 /* .debug_str_offsets */ \
11858 + 1 /* .debug_macro or .debug_macinfo */ \
11859 + 1 /* trailing zero */)
11860
11861 memset (&sections, 0, sizeof (sections));
11862
11863 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11864 {
11865 asection *sectp;
11866 uint32_t section_nr =
11867 read_4_bytes (dbfd,
11868 dwp_htab->section_pool.v1.indices
11869 + (unit_index + i) * sizeof (uint32_t));
11870
11871 if (section_nr == 0)
11872 break;
11873 if (section_nr >= dwp_file->num_sections)
11874 {
11875 error (_("Dwarf Error: bad DWP hash table, section number too large"
11876 " [in module %s]"),
11877 dwp_file->name);
11878 }
11879
11880 sectp = dwp_file->elf_sections[section_nr];
11881 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11882 {
11883 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11884 " [in module %s]"),
11885 dwp_file->name);
11886 }
11887 }
11888
11889 if (i < 2
11890 || sections.info_or_types.empty ()
11891 || sections.abbrev.empty ())
11892 {
11893 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11894 " [in module %s]"),
11895 dwp_file->name);
11896 }
11897 if (i == MAX_NR_V1_DWO_SECTIONS)
11898 {
11899 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11900 " [in module %s]"),
11901 dwp_file->name);
11902 }
11903
11904 /* It's easier for the rest of the code if we fake a struct dwo_file and
11905 have dwo_unit "live" in that. At least for now.
11906
11907 The DWP file can be made up of a random collection of CUs and TUs.
11908 However, for each CU + set of TUs that came from the same original DWO
11909 file, we can combine them back into a virtual DWO file to save space
11910 (fewer struct dwo_file objects to allocate). Remember that for really
11911 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11912
11913 std::string virtual_dwo_name =
11914 string_printf ("virtual-dwo/%d-%d-%d-%d",
11915 sections.abbrev.get_id (),
11916 sections.line.get_id (),
11917 sections.loc.get_id (),
11918 sections.str_offsets.get_id ());
11919 /* Can we use an existing virtual DWO file? */
11920 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11921 comp_dir);
11922 /* Create one if necessary. */
11923 if (*dwo_file_slot == NULL)
11924 {
11925 if (dwarf_read_debug)
11926 {
11927 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11928 virtual_dwo_name.c_str ());
11929 }
11930 dwo_file = new struct dwo_file;
11931 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11932 dwo_file->comp_dir = comp_dir;
11933 dwo_file->sections.abbrev = sections.abbrev;
11934 dwo_file->sections.line = sections.line;
11935 dwo_file->sections.loc = sections.loc;
11936 dwo_file->sections.macinfo = sections.macinfo;
11937 dwo_file->sections.macro = sections.macro;
11938 dwo_file->sections.str_offsets = sections.str_offsets;
11939 /* The "str" section is global to the entire DWP file. */
11940 dwo_file->sections.str = dwp_file->sections.str;
11941 /* The info or types section is assigned below to dwo_unit,
11942 there's no need to record it in dwo_file.
11943 Also, we can't simply record type sections in dwo_file because
11944 we record a pointer into the vector in dwo_unit. As we collect more
11945 types we'll grow the vector and eventually have to reallocate space
11946 for it, invalidating all copies of pointers into the previous
11947 contents. */
11948 *dwo_file_slot = dwo_file;
11949 }
11950 else
11951 {
11952 if (dwarf_read_debug)
11953 {
11954 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11955 virtual_dwo_name.c_str ());
11956 }
11957 dwo_file = (struct dwo_file *) *dwo_file_slot;
11958 }
11959
11960 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11961 dwo_unit->dwo_file = dwo_file;
11962 dwo_unit->signature = signature;
11963 dwo_unit->section =
11964 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11965 *dwo_unit->section = sections.info_or_types;
11966 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11967
11968 return dwo_unit;
11969 }
11970
11971 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11972 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11973 piece within that section used by a TU/CU, return a virtual section
11974 of just that piece. */
11975
11976 static struct dwarf2_section_info
11977 create_dwp_v2_section (dwarf2_per_objfile *per_objfile,
11978 struct dwarf2_section_info *section,
11979 bfd_size_type offset, bfd_size_type size)
11980 {
11981 struct dwarf2_section_info result;
11982 asection *sectp;
11983
11984 gdb_assert (section != NULL);
11985 gdb_assert (!section->is_virtual);
11986
11987 memset (&result, 0, sizeof (result));
11988 result.s.containing_section = section;
11989 result.is_virtual = true;
11990
11991 if (size == 0)
11992 return result;
11993
11994 sectp = section->get_bfd_section ();
11995
11996 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11997 bounds of the real section. This is a pretty-rare event, so just
11998 flag an error (easier) instead of a warning and trying to cope. */
11999 if (sectp == NULL
12000 || offset + size > bfd_section_size (sectp))
12001 {
12002 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12003 " in section %s [in module %s]"),
12004 sectp ? bfd_section_name (sectp) : "<unknown>",
12005 objfile_name (per_objfile->objfile));
12006 }
12007
12008 result.virtual_offset = offset;
12009 result.size = size;
12010 return result;
12011 }
12012
12013 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12014 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12015 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12016 This is for DWP version 2 files. */
12017
12018 static struct dwo_unit *
12019 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12020 struct dwp_file *dwp_file,
12021 uint32_t unit_index,
12022 const char *comp_dir,
12023 ULONGEST signature, int is_debug_types)
12024 {
12025 const struct dwp_hash_table *dwp_htab =
12026 is_debug_types ? dwp_file->tus : dwp_file->cus;
12027 bfd *dbfd = dwp_file->dbfd.get ();
12028 const char *kind = is_debug_types ? "TU" : "CU";
12029 struct dwo_file *dwo_file;
12030 struct dwo_unit *dwo_unit;
12031 struct virtual_v2_dwo_sections sections;
12032 void **dwo_file_slot;
12033 int i;
12034
12035 gdb_assert (dwp_file->version == 2);
12036
12037 if (dwarf_read_debug)
12038 {
12039 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12040 kind,
12041 pulongest (unit_index), hex_string (signature),
12042 dwp_file->name);
12043 }
12044
12045 /* Fetch the section offsets of this DWO unit. */
12046
12047 memset (&sections, 0, sizeof (sections));
12048
12049 for (i = 0; i < dwp_htab->nr_columns; ++i)
12050 {
12051 uint32_t offset = read_4_bytes (dbfd,
12052 dwp_htab->section_pool.v2.offsets
12053 + (((unit_index - 1) * dwp_htab->nr_columns
12054 + i)
12055 * sizeof (uint32_t)));
12056 uint32_t size = read_4_bytes (dbfd,
12057 dwp_htab->section_pool.v2.sizes
12058 + (((unit_index - 1) * dwp_htab->nr_columns
12059 + i)
12060 * sizeof (uint32_t)));
12061
12062 switch (dwp_htab->section_pool.v2.section_ids[i])
12063 {
12064 case DW_SECT_INFO:
12065 case DW_SECT_TYPES:
12066 sections.info_or_types_offset = offset;
12067 sections.info_or_types_size = size;
12068 break;
12069 case DW_SECT_ABBREV:
12070 sections.abbrev_offset = offset;
12071 sections.abbrev_size = size;
12072 break;
12073 case DW_SECT_LINE:
12074 sections.line_offset = offset;
12075 sections.line_size = size;
12076 break;
12077 case DW_SECT_LOC:
12078 sections.loc_offset = offset;
12079 sections.loc_size = size;
12080 break;
12081 case DW_SECT_STR_OFFSETS:
12082 sections.str_offsets_offset = offset;
12083 sections.str_offsets_size = size;
12084 break;
12085 case DW_SECT_MACINFO:
12086 sections.macinfo_offset = offset;
12087 sections.macinfo_size = size;
12088 break;
12089 case DW_SECT_MACRO:
12090 sections.macro_offset = offset;
12091 sections.macro_size = size;
12092 break;
12093 }
12094 }
12095
12096 /* It's easier for the rest of the code if we fake a struct dwo_file and
12097 have dwo_unit "live" in that. At least for now.
12098
12099 The DWP file can be made up of a random collection of CUs and TUs.
12100 However, for each CU + set of TUs that came from the same original DWO
12101 file, we can combine them back into a virtual DWO file to save space
12102 (fewer struct dwo_file objects to allocate). Remember that for really
12103 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12104
12105 std::string virtual_dwo_name =
12106 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12107 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12108 (long) (sections.line_size ? sections.line_offset : 0),
12109 (long) (sections.loc_size ? sections.loc_offset : 0),
12110 (long) (sections.str_offsets_size
12111 ? sections.str_offsets_offset : 0));
12112 /* Can we use an existing virtual DWO file? */
12113 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12114 comp_dir);
12115 /* Create one if necessary. */
12116 if (*dwo_file_slot == NULL)
12117 {
12118 if (dwarf_read_debug)
12119 {
12120 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12121 virtual_dwo_name.c_str ());
12122 }
12123 dwo_file = new struct dwo_file;
12124 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12125 dwo_file->comp_dir = comp_dir;
12126 dwo_file->sections.abbrev =
12127 create_dwp_v2_section (per_objfile, &dwp_file->sections.abbrev,
12128 sections.abbrev_offset, sections.abbrev_size);
12129 dwo_file->sections.line =
12130 create_dwp_v2_section (per_objfile, &dwp_file->sections.line,
12131 sections.line_offset, sections.line_size);
12132 dwo_file->sections.loc =
12133 create_dwp_v2_section (per_objfile, &dwp_file->sections.loc,
12134 sections.loc_offset, sections.loc_size);
12135 dwo_file->sections.macinfo =
12136 create_dwp_v2_section (per_objfile, &dwp_file->sections.macinfo,
12137 sections.macinfo_offset, sections.macinfo_size);
12138 dwo_file->sections.macro =
12139 create_dwp_v2_section (per_objfile, &dwp_file->sections.macro,
12140 sections.macro_offset, sections.macro_size);
12141 dwo_file->sections.str_offsets =
12142 create_dwp_v2_section (per_objfile,
12143 &dwp_file->sections.str_offsets,
12144 sections.str_offsets_offset,
12145 sections.str_offsets_size);
12146 /* The "str" section is global to the entire DWP file. */
12147 dwo_file->sections.str = dwp_file->sections.str;
12148 /* The info or types section is assigned below to dwo_unit,
12149 there's no need to record it in dwo_file.
12150 Also, we can't simply record type sections in dwo_file because
12151 we record a pointer into the vector in dwo_unit. As we collect more
12152 types we'll grow the vector and eventually have to reallocate space
12153 for it, invalidating all copies of pointers into the previous
12154 contents. */
12155 *dwo_file_slot = dwo_file;
12156 }
12157 else
12158 {
12159 if (dwarf_read_debug)
12160 {
12161 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12162 virtual_dwo_name.c_str ());
12163 }
12164 dwo_file = (struct dwo_file *) *dwo_file_slot;
12165 }
12166
12167 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12168 dwo_unit->dwo_file = dwo_file;
12169 dwo_unit->signature = signature;
12170 dwo_unit->section =
12171 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12172 *dwo_unit->section = create_dwp_v2_section (per_objfile,
12173 is_debug_types
12174 ? &dwp_file->sections.types
12175 : &dwp_file->sections.info,
12176 sections.info_or_types_offset,
12177 sections.info_or_types_size);
12178 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12179
12180 return dwo_unit;
12181 }
12182
12183 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12184 Returns NULL if the signature isn't found. */
12185
12186 static struct dwo_unit *
12187 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12188 struct dwp_file *dwp_file, const char *comp_dir,
12189 ULONGEST signature, int is_debug_types)
12190 {
12191 const struct dwp_hash_table *dwp_htab =
12192 is_debug_types ? dwp_file->tus : dwp_file->cus;
12193 bfd *dbfd = dwp_file->dbfd.get ();
12194 uint32_t mask = dwp_htab->nr_slots - 1;
12195 uint32_t hash = signature & mask;
12196 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12197 unsigned int i;
12198 void **slot;
12199 struct dwo_unit find_dwo_cu;
12200
12201 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12202 find_dwo_cu.signature = signature;
12203 slot = htab_find_slot (is_debug_types
12204 ? dwp_file->loaded_tus.get ()
12205 : dwp_file->loaded_cus.get (),
12206 &find_dwo_cu, INSERT);
12207
12208 if (*slot != NULL)
12209 return (struct dwo_unit *) *slot;
12210
12211 /* Use a for loop so that we don't loop forever on bad debug info. */
12212 for (i = 0; i < dwp_htab->nr_slots; ++i)
12213 {
12214 ULONGEST signature_in_table;
12215
12216 signature_in_table =
12217 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12218 if (signature_in_table == signature)
12219 {
12220 uint32_t unit_index =
12221 read_4_bytes (dbfd,
12222 dwp_htab->unit_table + hash * sizeof (uint32_t));
12223
12224 if (dwp_file->version == 1)
12225 {
12226 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12227 unit_index, comp_dir,
12228 signature, is_debug_types);
12229 }
12230 else
12231 {
12232 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12233 unit_index, comp_dir,
12234 signature, is_debug_types);
12235 }
12236 return (struct dwo_unit *) *slot;
12237 }
12238 if (signature_in_table == 0)
12239 return NULL;
12240 hash = (hash + hash2) & mask;
12241 }
12242
12243 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12244 " [in module %s]"),
12245 dwp_file->name);
12246 }
12247
12248 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12249 Open the file specified by FILE_NAME and hand it off to BFD for
12250 preliminary analysis. Return a newly initialized bfd *, which
12251 includes a canonicalized copy of FILE_NAME.
12252 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12253 SEARCH_CWD is true if the current directory is to be searched.
12254 It will be searched before debug-file-directory.
12255 If successful, the file is added to the bfd include table of the
12256 objfile's bfd (see gdb_bfd_record_inclusion).
12257 If unable to find/open the file, return NULL.
12258 NOTE: This function is derived from symfile_bfd_open. */
12259
12260 static gdb_bfd_ref_ptr
12261 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12262 const char *file_name, int is_dwp, int search_cwd)
12263 {
12264 int desc;
12265 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12266 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12267 to debug_file_directory. */
12268 const char *search_path;
12269 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12270
12271 gdb::unique_xmalloc_ptr<char> search_path_holder;
12272 if (search_cwd)
12273 {
12274 if (*debug_file_directory != '\0')
12275 {
12276 search_path_holder.reset (concat (".", dirname_separator_string,
12277 debug_file_directory,
12278 (char *) NULL));
12279 search_path = search_path_holder.get ();
12280 }
12281 else
12282 search_path = ".";
12283 }
12284 else
12285 search_path = debug_file_directory;
12286
12287 openp_flags flags = OPF_RETURN_REALPATH;
12288 if (is_dwp)
12289 flags |= OPF_SEARCH_IN_PATH;
12290
12291 gdb::unique_xmalloc_ptr<char> absolute_name;
12292 desc = openp (search_path, flags, file_name,
12293 O_RDONLY | O_BINARY, &absolute_name);
12294 if (desc < 0)
12295 return NULL;
12296
12297 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12298 gnutarget, desc));
12299 if (sym_bfd == NULL)
12300 return NULL;
12301 bfd_set_cacheable (sym_bfd.get (), 1);
12302
12303 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12304 return NULL;
12305
12306 /* Success. Record the bfd as having been included by the objfile's bfd.
12307 This is important because things like demangled_names_hash lives in the
12308 objfile's per_bfd space and may have references to things like symbol
12309 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12310 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12311
12312 return sym_bfd;
12313 }
12314
12315 /* Try to open DWO file FILE_NAME.
12316 COMP_DIR is the DW_AT_comp_dir attribute.
12317 The result is the bfd handle of the file.
12318 If there is a problem finding or opening the file, return NULL.
12319 Upon success, the canonicalized path of the file is stored in the bfd,
12320 same as symfile_bfd_open. */
12321
12322 static gdb_bfd_ref_ptr
12323 open_dwo_file (dwarf2_per_objfile *per_objfile,
12324 const char *file_name, const char *comp_dir)
12325 {
12326 if (IS_ABSOLUTE_PATH (file_name))
12327 return try_open_dwop_file (per_objfile, file_name,
12328 0 /*is_dwp*/, 0 /*search_cwd*/);
12329
12330 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12331
12332 if (comp_dir != NULL)
12333 {
12334 gdb::unique_xmalloc_ptr<char> path_to_try
12335 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12336
12337 /* NOTE: If comp_dir is a relative path, this will also try the
12338 search path, which seems useful. */
12339 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12340 0 /*is_dwp*/,
12341 1 /*search_cwd*/));
12342 if (abfd != NULL)
12343 return abfd;
12344 }
12345
12346 /* That didn't work, try debug-file-directory, which, despite its name,
12347 is a list of paths. */
12348
12349 if (*debug_file_directory == '\0')
12350 return NULL;
12351
12352 return try_open_dwop_file (per_objfile, file_name,
12353 0 /*is_dwp*/, 1 /*search_cwd*/);
12354 }
12355
12356 /* This function is mapped across the sections and remembers the offset and
12357 size of each of the DWO debugging sections we are interested in. */
12358
12359 static void
12360 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12361 {
12362 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12363 const struct dwop_section_names *names = &dwop_section_names;
12364
12365 if (section_is_p (sectp->name, &names->abbrev_dwo))
12366 {
12367 dwo_sections->abbrev.s.section = sectp;
12368 dwo_sections->abbrev.size = bfd_section_size (sectp);
12369 }
12370 else if (section_is_p (sectp->name, &names->info_dwo))
12371 {
12372 dwo_sections->info.s.section = sectp;
12373 dwo_sections->info.size = bfd_section_size (sectp);
12374 }
12375 else if (section_is_p (sectp->name, &names->line_dwo))
12376 {
12377 dwo_sections->line.s.section = sectp;
12378 dwo_sections->line.size = bfd_section_size (sectp);
12379 }
12380 else if (section_is_p (sectp->name, &names->loc_dwo))
12381 {
12382 dwo_sections->loc.s.section = sectp;
12383 dwo_sections->loc.size = bfd_section_size (sectp);
12384 }
12385 else if (section_is_p (sectp->name, &names->loclists_dwo))
12386 {
12387 dwo_sections->loclists.s.section = sectp;
12388 dwo_sections->loclists.size = bfd_section_size (sectp);
12389 }
12390 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12391 {
12392 dwo_sections->macinfo.s.section = sectp;
12393 dwo_sections->macinfo.size = bfd_section_size (sectp);
12394 }
12395 else if (section_is_p (sectp->name, &names->macro_dwo))
12396 {
12397 dwo_sections->macro.s.section = sectp;
12398 dwo_sections->macro.size = bfd_section_size (sectp);
12399 }
12400 else if (section_is_p (sectp->name, &names->str_dwo))
12401 {
12402 dwo_sections->str.s.section = sectp;
12403 dwo_sections->str.size = bfd_section_size (sectp);
12404 }
12405 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12406 {
12407 dwo_sections->str_offsets.s.section = sectp;
12408 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12409 }
12410 else if (section_is_p (sectp->name, &names->types_dwo))
12411 {
12412 struct dwarf2_section_info type_section;
12413
12414 memset (&type_section, 0, sizeof (type_section));
12415 type_section.s.section = sectp;
12416 type_section.size = bfd_section_size (sectp);
12417 dwo_sections->types.push_back (type_section);
12418 }
12419 }
12420
12421 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12422 by PER_CU. This is for the non-DWP case.
12423 The result is NULL if DWO_NAME can't be found. */
12424
12425 static struct dwo_file *
12426 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12427 const char *comp_dir)
12428 {
12429 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12430
12431 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12432 if (dbfd == NULL)
12433 {
12434 if (dwarf_read_debug)
12435 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12436 return NULL;
12437 }
12438
12439 dwo_file_up dwo_file (new struct dwo_file);
12440 dwo_file->dwo_name = dwo_name;
12441 dwo_file->comp_dir = comp_dir;
12442 dwo_file->dbfd = std::move (dbfd);
12443
12444 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12445 &dwo_file->sections);
12446
12447 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12448 dwo_file->cus);
12449
12450 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12451 dwo_file->sections.types, dwo_file->tus);
12452
12453 if (dwarf_read_debug)
12454 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12455
12456 return dwo_file.release ();
12457 }
12458
12459 /* This function is mapped across the sections and remembers the offset and
12460 size of each of the DWP debugging sections common to version 1 and 2 that
12461 we are interested in. */
12462
12463 static void
12464 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12465 void *dwp_file_ptr)
12466 {
12467 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12468 const struct dwop_section_names *names = &dwop_section_names;
12469 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12470
12471 /* Record the ELF section number for later lookup: this is what the
12472 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12473 gdb_assert (elf_section_nr < dwp_file->num_sections);
12474 dwp_file->elf_sections[elf_section_nr] = sectp;
12475
12476 /* Look for specific sections that we need. */
12477 if (section_is_p (sectp->name, &names->str_dwo))
12478 {
12479 dwp_file->sections.str.s.section = sectp;
12480 dwp_file->sections.str.size = bfd_section_size (sectp);
12481 }
12482 else if (section_is_p (sectp->name, &names->cu_index))
12483 {
12484 dwp_file->sections.cu_index.s.section = sectp;
12485 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12486 }
12487 else if (section_is_p (sectp->name, &names->tu_index))
12488 {
12489 dwp_file->sections.tu_index.s.section = sectp;
12490 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12491 }
12492 }
12493
12494 /* This function is mapped across the sections and remembers the offset and
12495 size of each of the DWP version 2 debugging sections that we are interested
12496 in. This is split into a separate function because we don't know if we
12497 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12498
12499 static void
12500 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12501 {
12502 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12503 const struct dwop_section_names *names = &dwop_section_names;
12504 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12505
12506 /* Record the ELF section number for later lookup: this is what the
12507 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12508 gdb_assert (elf_section_nr < dwp_file->num_sections);
12509 dwp_file->elf_sections[elf_section_nr] = sectp;
12510
12511 /* Look for specific sections that we need. */
12512 if (section_is_p (sectp->name, &names->abbrev_dwo))
12513 {
12514 dwp_file->sections.abbrev.s.section = sectp;
12515 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12516 }
12517 else if (section_is_p (sectp->name, &names->info_dwo))
12518 {
12519 dwp_file->sections.info.s.section = sectp;
12520 dwp_file->sections.info.size = bfd_section_size (sectp);
12521 }
12522 else if (section_is_p (sectp->name, &names->line_dwo))
12523 {
12524 dwp_file->sections.line.s.section = sectp;
12525 dwp_file->sections.line.size = bfd_section_size (sectp);
12526 }
12527 else if (section_is_p (sectp->name, &names->loc_dwo))
12528 {
12529 dwp_file->sections.loc.s.section = sectp;
12530 dwp_file->sections.loc.size = bfd_section_size (sectp);
12531 }
12532 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12533 {
12534 dwp_file->sections.macinfo.s.section = sectp;
12535 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12536 }
12537 else if (section_is_p (sectp->name, &names->macro_dwo))
12538 {
12539 dwp_file->sections.macro.s.section = sectp;
12540 dwp_file->sections.macro.size = bfd_section_size (sectp);
12541 }
12542 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12543 {
12544 dwp_file->sections.str_offsets.s.section = sectp;
12545 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12546 }
12547 else if (section_is_p (sectp->name, &names->types_dwo))
12548 {
12549 dwp_file->sections.types.s.section = sectp;
12550 dwp_file->sections.types.size = bfd_section_size (sectp);
12551 }
12552 }
12553
12554 /* Hash function for dwp_file loaded CUs/TUs. */
12555
12556 static hashval_t
12557 hash_dwp_loaded_cutus (const void *item)
12558 {
12559 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12560
12561 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12562 return dwo_unit->signature;
12563 }
12564
12565 /* Equality function for dwp_file loaded CUs/TUs. */
12566
12567 static int
12568 eq_dwp_loaded_cutus (const void *a, const void *b)
12569 {
12570 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12571 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12572
12573 return dua->signature == dub->signature;
12574 }
12575
12576 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12577
12578 static htab_up
12579 allocate_dwp_loaded_cutus_table ()
12580 {
12581 return htab_up (htab_create_alloc (3,
12582 hash_dwp_loaded_cutus,
12583 eq_dwp_loaded_cutus,
12584 NULL, xcalloc, xfree));
12585 }
12586
12587 /* Try to open DWP file FILE_NAME.
12588 The result is the bfd handle of the file.
12589 If there is a problem finding or opening the file, return NULL.
12590 Upon success, the canonicalized path of the file is stored in the bfd,
12591 same as symfile_bfd_open. */
12592
12593 static gdb_bfd_ref_ptr
12594 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12595 {
12596 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12597 1 /*is_dwp*/,
12598 1 /*search_cwd*/));
12599 if (abfd != NULL)
12600 return abfd;
12601
12602 /* Work around upstream bug 15652.
12603 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12604 [Whether that's a "bug" is debatable, but it is getting in our way.]
12605 We have no real idea where the dwp file is, because gdb's realpath-ing
12606 of the executable's path may have discarded the needed info.
12607 [IWBN if the dwp file name was recorded in the executable, akin to
12608 .gnu_debuglink, but that doesn't exist yet.]
12609 Strip the directory from FILE_NAME and search again. */
12610 if (*debug_file_directory != '\0')
12611 {
12612 /* Don't implicitly search the current directory here.
12613 If the user wants to search "." to handle this case,
12614 it must be added to debug-file-directory. */
12615 return try_open_dwop_file (per_objfile, lbasename (file_name),
12616 1 /*is_dwp*/,
12617 0 /*search_cwd*/);
12618 }
12619
12620 return NULL;
12621 }
12622
12623 /* Initialize the use of the DWP file for the current objfile.
12624 By convention the name of the DWP file is ${objfile}.dwp.
12625 The result is NULL if it can't be found. */
12626
12627 static std::unique_ptr<struct dwp_file>
12628 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12629 {
12630 struct objfile *objfile = per_objfile->objfile;
12631
12632 /* Try to find first .dwp for the binary file before any symbolic links
12633 resolving. */
12634
12635 /* If the objfile is a debug file, find the name of the real binary
12636 file and get the name of dwp file from there. */
12637 std::string dwp_name;
12638 if (objfile->separate_debug_objfile_backlink != NULL)
12639 {
12640 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12641 const char *backlink_basename = lbasename (backlink->original_name);
12642
12643 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12644 }
12645 else
12646 dwp_name = objfile->original_name;
12647
12648 dwp_name += ".dwp";
12649
12650 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12651 if (dbfd == NULL
12652 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12653 {
12654 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12655 dwp_name = objfile_name (objfile);
12656 dwp_name += ".dwp";
12657 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12658 }
12659
12660 if (dbfd == NULL)
12661 {
12662 if (dwarf_read_debug)
12663 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12664 return std::unique_ptr<dwp_file> ();
12665 }
12666
12667 const char *name = bfd_get_filename (dbfd.get ());
12668 std::unique_ptr<struct dwp_file> dwp_file
12669 (new struct dwp_file (name, std::move (dbfd)));
12670
12671 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12672 dwp_file->elf_sections =
12673 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12674 dwp_file->num_sections, asection *);
12675
12676 bfd_map_over_sections (dwp_file->dbfd.get (),
12677 dwarf2_locate_common_dwp_sections,
12678 dwp_file.get ());
12679
12680 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12681
12682 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12683
12684 /* The DWP file version is stored in the hash table. Oh well. */
12685 if (dwp_file->cus && dwp_file->tus
12686 && dwp_file->cus->version != dwp_file->tus->version)
12687 {
12688 /* Technically speaking, we should try to limp along, but this is
12689 pretty bizarre. We use pulongest here because that's the established
12690 portability solution (e.g, we cannot use %u for uint32_t). */
12691 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12692 " TU version %s [in DWP file %s]"),
12693 pulongest (dwp_file->cus->version),
12694 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12695 }
12696
12697 if (dwp_file->cus)
12698 dwp_file->version = dwp_file->cus->version;
12699 else if (dwp_file->tus)
12700 dwp_file->version = dwp_file->tus->version;
12701 else
12702 dwp_file->version = 2;
12703
12704 if (dwp_file->version == 2)
12705 bfd_map_over_sections (dwp_file->dbfd.get (),
12706 dwarf2_locate_v2_dwp_sections,
12707 dwp_file.get ());
12708
12709 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12710 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12711
12712 if (dwarf_read_debug)
12713 {
12714 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12715 fprintf_unfiltered (gdb_stdlog,
12716 " %s CUs, %s TUs\n",
12717 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12718 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12719 }
12720
12721 return dwp_file;
12722 }
12723
12724 /* Wrapper around open_and_init_dwp_file, only open it once. */
12725
12726 static struct dwp_file *
12727 get_dwp_file (dwarf2_per_objfile *per_objfile)
12728 {
12729 if (!per_objfile->per_bfd->dwp_checked)
12730 {
12731 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12732 per_objfile->per_bfd->dwp_checked = 1;
12733 }
12734 return per_objfile->per_bfd->dwp_file.get ();
12735 }
12736
12737 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12738 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12739 or in the DWP file for the objfile, referenced by THIS_UNIT.
12740 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12741 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12742
12743 This is called, for example, when wanting to read a variable with a
12744 complex location. Therefore we don't want to do file i/o for every call.
12745 Therefore we don't want to look for a DWO file on every call.
12746 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12747 then we check if we've already seen DWO_NAME, and only THEN do we check
12748 for a DWO file.
12749
12750 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12751 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12752
12753 static struct dwo_unit *
12754 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12755 ULONGEST signature, int is_debug_types)
12756 {
12757 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12758 struct objfile *objfile = per_objfile->objfile;
12759 const char *kind = is_debug_types ? "TU" : "CU";
12760 void **dwo_file_slot;
12761 struct dwo_file *dwo_file;
12762 struct dwp_file *dwp_file;
12763
12764 /* First see if there's a DWP file.
12765 If we have a DWP file but didn't find the DWO inside it, don't
12766 look for the original DWO file. It makes gdb behave differently
12767 depending on whether one is debugging in the build tree. */
12768
12769 dwp_file = get_dwp_file (per_objfile);
12770 if (dwp_file != NULL)
12771 {
12772 const struct dwp_hash_table *dwp_htab =
12773 is_debug_types ? dwp_file->tus : dwp_file->cus;
12774
12775 if (dwp_htab != NULL)
12776 {
12777 struct dwo_unit *dwo_cutu =
12778 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12779 is_debug_types);
12780
12781 if (dwo_cutu != NULL)
12782 {
12783 if (dwarf_read_debug)
12784 {
12785 fprintf_unfiltered (gdb_stdlog,
12786 "Virtual DWO %s %s found: @%s\n",
12787 kind, hex_string (signature),
12788 host_address_to_string (dwo_cutu));
12789 }
12790 return dwo_cutu;
12791 }
12792 }
12793 }
12794 else
12795 {
12796 /* No DWP file, look for the DWO file. */
12797
12798 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12799 if (*dwo_file_slot == NULL)
12800 {
12801 /* Read in the file and build a table of the CUs/TUs it contains. */
12802 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12803 }
12804 /* NOTE: This will be NULL if unable to open the file. */
12805 dwo_file = (struct dwo_file *) *dwo_file_slot;
12806
12807 if (dwo_file != NULL)
12808 {
12809 struct dwo_unit *dwo_cutu = NULL;
12810
12811 if (is_debug_types && dwo_file->tus)
12812 {
12813 struct dwo_unit find_dwo_cutu;
12814
12815 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12816 find_dwo_cutu.signature = signature;
12817 dwo_cutu
12818 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12819 &find_dwo_cutu);
12820 }
12821 else if (!is_debug_types && dwo_file->cus)
12822 {
12823 struct dwo_unit find_dwo_cutu;
12824
12825 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12826 find_dwo_cutu.signature = signature;
12827 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12828 &find_dwo_cutu);
12829 }
12830
12831 if (dwo_cutu != NULL)
12832 {
12833 if (dwarf_read_debug)
12834 {
12835 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12836 kind, dwo_name, hex_string (signature),
12837 host_address_to_string (dwo_cutu));
12838 }
12839 return dwo_cutu;
12840 }
12841 }
12842 }
12843
12844 /* We didn't find it. This could mean a dwo_id mismatch, or
12845 someone deleted the DWO/DWP file, or the search path isn't set up
12846 correctly to find the file. */
12847
12848 if (dwarf_read_debug)
12849 {
12850 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12851 kind, dwo_name, hex_string (signature));
12852 }
12853
12854 /* This is a warning and not a complaint because it can be caused by
12855 pilot error (e.g., user accidentally deleting the DWO). */
12856 {
12857 /* Print the name of the DWP file if we looked there, helps the user
12858 better diagnose the problem. */
12859 std::string dwp_text;
12860
12861 if (dwp_file != NULL)
12862 dwp_text = string_printf (" [in DWP file %s]",
12863 lbasename (dwp_file->name));
12864
12865 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12866 " [in module %s]"),
12867 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12868 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12869 }
12870 return NULL;
12871 }
12872
12873 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12874 See lookup_dwo_cutu_unit for details. */
12875
12876 static struct dwo_unit *
12877 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12878 ULONGEST signature)
12879 {
12880 gdb_assert (!cu->per_cu->is_debug_types);
12881
12882 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12883 }
12884
12885 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12886 See lookup_dwo_cutu_unit for details. */
12887
12888 static struct dwo_unit *
12889 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12890 {
12891 gdb_assert (cu->per_cu->is_debug_types);
12892
12893 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12894
12895 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12896 }
12897
12898 /* Traversal function for queue_and_load_all_dwo_tus. */
12899
12900 static int
12901 queue_and_load_dwo_tu (void **slot, void *info)
12902 {
12903 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12904 dwarf2_cu *cu = (dwarf2_cu *) info;
12905 ULONGEST signature = dwo_unit->signature;
12906 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12907
12908 if (sig_type != NULL)
12909 {
12910 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12911
12912 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12913 a real dependency of PER_CU on SIG_TYPE. That is detected later
12914 while processing PER_CU. */
12915 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12916 load_full_type_unit (sig_cu, cu->per_objfile);
12917 cu->per_cu->imported_symtabs_push (sig_cu);
12918 }
12919
12920 return 1;
12921 }
12922
12923 /* Queue all TUs contained in the DWO of CU to be read in.
12924 The DWO may have the only definition of the type, though it may not be
12925 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12926 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12927
12928 static void
12929 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12930 {
12931 struct dwo_unit *dwo_unit;
12932 struct dwo_file *dwo_file;
12933
12934 gdb_assert (cu != nullptr);
12935 gdb_assert (!cu->per_cu->is_debug_types);
12936 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12937
12938 dwo_unit = cu->dwo_unit;
12939 gdb_assert (dwo_unit != NULL);
12940
12941 dwo_file = dwo_unit->dwo_file;
12942 if (dwo_file->tus != NULL)
12943 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12944 }
12945
12946 /* Read in various DIEs. */
12947
12948 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12949 Inherit only the children of the DW_AT_abstract_origin DIE not being
12950 already referenced by DW_AT_abstract_origin from the children of the
12951 current DIE. */
12952
12953 static void
12954 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12955 {
12956 struct die_info *child_die;
12957 sect_offset *offsetp;
12958 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12959 struct die_info *origin_die;
12960 /* Iterator of the ORIGIN_DIE children. */
12961 struct die_info *origin_child_die;
12962 struct attribute *attr;
12963 struct dwarf2_cu *origin_cu;
12964 struct pending **origin_previous_list_in_scope;
12965
12966 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12967 if (!attr)
12968 return;
12969
12970 /* Note that following die references may follow to a die in a
12971 different cu. */
12972
12973 origin_cu = cu;
12974 origin_die = follow_die_ref (die, attr, &origin_cu);
12975
12976 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12977 symbols in. */
12978 origin_previous_list_in_scope = origin_cu->list_in_scope;
12979 origin_cu->list_in_scope = cu->list_in_scope;
12980
12981 if (die->tag != origin_die->tag
12982 && !(die->tag == DW_TAG_inlined_subroutine
12983 && origin_die->tag == DW_TAG_subprogram))
12984 complaint (_("DIE %s and its abstract origin %s have different tags"),
12985 sect_offset_str (die->sect_off),
12986 sect_offset_str (origin_die->sect_off));
12987
12988 std::vector<sect_offset> offsets;
12989
12990 for (child_die = die->child;
12991 child_die && child_die->tag;
12992 child_die = child_die->sibling)
12993 {
12994 struct die_info *child_origin_die;
12995 struct dwarf2_cu *child_origin_cu;
12996
12997 /* We are trying to process concrete instance entries:
12998 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12999 it's not relevant to our analysis here. i.e. detecting DIEs that are
13000 present in the abstract instance but not referenced in the concrete
13001 one. */
13002 if (child_die->tag == DW_TAG_call_site
13003 || child_die->tag == DW_TAG_GNU_call_site)
13004 continue;
13005
13006 /* For each CHILD_DIE, find the corresponding child of
13007 ORIGIN_DIE. If there is more than one layer of
13008 DW_AT_abstract_origin, follow them all; there shouldn't be,
13009 but GCC versions at least through 4.4 generate this (GCC PR
13010 40573). */
13011 child_origin_die = child_die;
13012 child_origin_cu = cu;
13013 while (1)
13014 {
13015 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13016 child_origin_cu);
13017 if (attr == NULL)
13018 break;
13019 child_origin_die = follow_die_ref (child_origin_die, attr,
13020 &child_origin_cu);
13021 }
13022
13023 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13024 counterpart may exist. */
13025 if (child_origin_die != child_die)
13026 {
13027 if (child_die->tag != child_origin_die->tag
13028 && !(child_die->tag == DW_TAG_inlined_subroutine
13029 && child_origin_die->tag == DW_TAG_subprogram))
13030 complaint (_("Child DIE %s and its abstract origin %s have "
13031 "different tags"),
13032 sect_offset_str (child_die->sect_off),
13033 sect_offset_str (child_origin_die->sect_off));
13034 if (child_origin_die->parent != origin_die)
13035 complaint (_("Child DIE %s and its abstract origin %s have "
13036 "different parents"),
13037 sect_offset_str (child_die->sect_off),
13038 sect_offset_str (child_origin_die->sect_off));
13039 else
13040 offsets.push_back (child_origin_die->sect_off);
13041 }
13042 }
13043 std::sort (offsets.begin (), offsets.end ());
13044 sect_offset *offsets_end = offsets.data () + offsets.size ();
13045 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13046 if (offsetp[-1] == *offsetp)
13047 complaint (_("Multiple children of DIE %s refer "
13048 "to DIE %s as their abstract origin"),
13049 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13050
13051 offsetp = offsets.data ();
13052 origin_child_die = origin_die->child;
13053 while (origin_child_die && origin_child_die->tag)
13054 {
13055 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13056 while (offsetp < offsets_end
13057 && *offsetp < origin_child_die->sect_off)
13058 offsetp++;
13059 if (offsetp >= offsets_end
13060 || *offsetp > origin_child_die->sect_off)
13061 {
13062 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13063 Check whether we're already processing ORIGIN_CHILD_DIE.
13064 This can happen with mutually referenced abstract_origins.
13065 PR 16581. */
13066 if (!origin_child_die->in_process)
13067 process_die (origin_child_die, origin_cu);
13068 }
13069 origin_child_die = origin_child_die->sibling;
13070 }
13071 origin_cu->list_in_scope = origin_previous_list_in_scope;
13072
13073 if (cu != origin_cu)
13074 compute_delayed_physnames (origin_cu);
13075 }
13076
13077 static void
13078 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13079 {
13080 struct objfile *objfile = cu->per_objfile->objfile;
13081 struct gdbarch *gdbarch = objfile->arch ();
13082 struct context_stack *newobj;
13083 CORE_ADDR lowpc;
13084 CORE_ADDR highpc;
13085 struct die_info *child_die;
13086 struct attribute *attr, *call_line, *call_file;
13087 const char *name;
13088 CORE_ADDR baseaddr;
13089 struct block *block;
13090 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13091 std::vector<struct symbol *> template_args;
13092 struct template_symbol *templ_func = NULL;
13093
13094 if (inlined_func)
13095 {
13096 /* If we do not have call site information, we can't show the
13097 caller of this inlined function. That's too confusing, so
13098 only use the scope for local variables. */
13099 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13100 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13101 if (call_line == NULL || call_file == NULL)
13102 {
13103 read_lexical_block_scope (die, cu);
13104 return;
13105 }
13106 }
13107
13108 baseaddr = objfile->text_section_offset ();
13109
13110 name = dwarf2_name (die, cu);
13111
13112 /* Ignore functions with missing or empty names. These are actually
13113 illegal according to the DWARF standard. */
13114 if (name == NULL)
13115 {
13116 complaint (_("missing name for subprogram DIE at %s"),
13117 sect_offset_str (die->sect_off));
13118 return;
13119 }
13120
13121 /* Ignore functions with missing or invalid low and high pc attributes. */
13122 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13123 <= PC_BOUNDS_INVALID)
13124 {
13125 attr = dwarf2_attr (die, DW_AT_external, cu);
13126 if (!attr || !DW_UNSND (attr))
13127 complaint (_("cannot get low and high bounds "
13128 "for subprogram DIE at %s"),
13129 sect_offset_str (die->sect_off));
13130 return;
13131 }
13132
13133 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13134 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13135
13136 /* If we have any template arguments, then we must allocate a
13137 different sort of symbol. */
13138 for (child_die = die->child; child_die; child_die = child_die->sibling)
13139 {
13140 if (child_die->tag == DW_TAG_template_type_param
13141 || child_die->tag == DW_TAG_template_value_param)
13142 {
13143 templ_func = new (&objfile->objfile_obstack) template_symbol;
13144 templ_func->subclass = SYMBOL_TEMPLATE;
13145 break;
13146 }
13147 }
13148
13149 newobj = cu->get_builder ()->push_context (0, lowpc);
13150 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13151 (struct symbol *) templ_func);
13152
13153 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13154 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13155 cu->language);
13156
13157 /* If there is a location expression for DW_AT_frame_base, record
13158 it. */
13159 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13160 if (attr != nullptr)
13161 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13162
13163 /* If there is a location for the static link, record it. */
13164 newobj->static_link = NULL;
13165 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13166 if (attr != nullptr)
13167 {
13168 newobj->static_link
13169 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13170 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13171 cu->addr_type ());
13172 }
13173
13174 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13175
13176 if (die->child != NULL)
13177 {
13178 child_die = die->child;
13179 while (child_die && child_die->tag)
13180 {
13181 if (child_die->tag == DW_TAG_template_type_param
13182 || child_die->tag == DW_TAG_template_value_param)
13183 {
13184 struct symbol *arg = new_symbol (child_die, NULL, cu);
13185
13186 if (arg != NULL)
13187 template_args.push_back (arg);
13188 }
13189 else
13190 process_die (child_die, cu);
13191 child_die = child_die->sibling;
13192 }
13193 }
13194
13195 inherit_abstract_dies (die, cu);
13196
13197 /* If we have a DW_AT_specification, we might need to import using
13198 directives from the context of the specification DIE. See the
13199 comment in determine_prefix. */
13200 if (cu->language == language_cplus
13201 && dwarf2_attr (die, DW_AT_specification, cu))
13202 {
13203 struct dwarf2_cu *spec_cu = cu;
13204 struct die_info *spec_die = die_specification (die, &spec_cu);
13205
13206 while (spec_die)
13207 {
13208 child_die = spec_die->child;
13209 while (child_die && child_die->tag)
13210 {
13211 if (child_die->tag == DW_TAG_imported_module)
13212 process_die (child_die, spec_cu);
13213 child_die = child_die->sibling;
13214 }
13215
13216 /* In some cases, GCC generates specification DIEs that
13217 themselves contain DW_AT_specification attributes. */
13218 spec_die = die_specification (spec_die, &spec_cu);
13219 }
13220 }
13221
13222 struct context_stack cstk = cu->get_builder ()->pop_context ();
13223 /* Make a block for the local symbols within. */
13224 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13225 cstk.static_link, lowpc, highpc);
13226
13227 /* For C++, set the block's scope. */
13228 if ((cu->language == language_cplus
13229 || cu->language == language_fortran
13230 || cu->language == language_d
13231 || cu->language == language_rust)
13232 && cu->processing_has_namespace_info)
13233 block_set_scope (block, determine_prefix (die, cu),
13234 &objfile->objfile_obstack);
13235
13236 /* If we have address ranges, record them. */
13237 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13238
13239 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13240
13241 /* Attach template arguments to function. */
13242 if (!template_args.empty ())
13243 {
13244 gdb_assert (templ_func != NULL);
13245
13246 templ_func->n_template_arguments = template_args.size ();
13247 templ_func->template_arguments
13248 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13249 templ_func->n_template_arguments);
13250 memcpy (templ_func->template_arguments,
13251 template_args.data (),
13252 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13253
13254 /* Make sure that the symtab is set on the new symbols. Even
13255 though they don't appear in this symtab directly, other parts
13256 of gdb assume that symbols do, and this is reasonably
13257 true. */
13258 for (symbol *sym : template_args)
13259 symbol_set_symtab (sym, symbol_symtab (templ_func));
13260 }
13261
13262 /* In C++, we can have functions nested inside functions (e.g., when
13263 a function declares a class that has methods). This means that
13264 when we finish processing a function scope, we may need to go
13265 back to building a containing block's symbol lists. */
13266 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13267 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13268
13269 /* If we've finished processing a top-level function, subsequent
13270 symbols go in the file symbol list. */
13271 if (cu->get_builder ()->outermost_context_p ())
13272 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13273 }
13274
13275 /* Process all the DIES contained within a lexical block scope. Start
13276 a new scope, process the dies, and then close the scope. */
13277
13278 static void
13279 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13280 {
13281 struct objfile *objfile = cu->per_objfile->objfile;
13282 struct gdbarch *gdbarch = objfile->arch ();
13283 CORE_ADDR lowpc, highpc;
13284 struct die_info *child_die;
13285 CORE_ADDR baseaddr;
13286
13287 baseaddr = objfile->text_section_offset ();
13288
13289 /* Ignore blocks with missing or invalid low and high pc attributes. */
13290 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13291 as multiple lexical blocks? Handling children in a sane way would
13292 be nasty. Might be easier to properly extend generic blocks to
13293 describe ranges. */
13294 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13295 {
13296 case PC_BOUNDS_NOT_PRESENT:
13297 /* DW_TAG_lexical_block has no attributes, process its children as if
13298 there was no wrapping by that DW_TAG_lexical_block.
13299 GCC does no longer produces such DWARF since GCC r224161. */
13300 for (child_die = die->child;
13301 child_die != NULL && child_die->tag;
13302 child_die = child_die->sibling)
13303 {
13304 /* We might already be processing this DIE. This can happen
13305 in an unusual circumstance -- where a subroutine A
13306 appears lexically in another subroutine B, but A actually
13307 inlines B. The recursion is broken here, rather than in
13308 inherit_abstract_dies, because it seems better to simply
13309 drop concrete children here. */
13310 if (!child_die->in_process)
13311 process_die (child_die, cu);
13312 }
13313 return;
13314 case PC_BOUNDS_INVALID:
13315 return;
13316 }
13317 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13318 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13319
13320 cu->get_builder ()->push_context (0, lowpc);
13321 if (die->child != NULL)
13322 {
13323 child_die = die->child;
13324 while (child_die && child_die->tag)
13325 {
13326 process_die (child_die, cu);
13327 child_die = child_die->sibling;
13328 }
13329 }
13330 inherit_abstract_dies (die, cu);
13331 struct context_stack cstk = cu->get_builder ()->pop_context ();
13332
13333 if (*cu->get_builder ()->get_local_symbols () != NULL
13334 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13335 {
13336 struct block *block
13337 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13338 cstk.start_addr, highpc);
13339
13340 /* Note that recording ranges after traversing children, as we
13341 do here, means that recording a parent's ranges entails
13342 walking across all its children's ranges as they appear in
13343 the address map, which is quadratic behavior.
13344
13345 It would be nicer to record the parent's ranges before
13346 traversing its children, simply overriding whatever you find
13347 there. But since we don't even decide whether to create a
13348 block until after we've traversed its children, that's hard
13349 to do. */
13350 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13351 }
13352 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13353 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13354 }
13355
13356 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13357
13358 static void
13359 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13360 {
13361 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13362 struct objfile *objfile = per_objfile->objfile;
13363 struct gdbarch *gdbarch = objfile->arch ();
13364 CORE_ADDR pc, baseaddr;
13365 struct attribute *attr;
13366 struct call_site *call_site, call_site_local;
13367 void **slot;
13368 int nparams;
13369 struct die_info *child_die;
13370
13371 baseaddr = objfile->text_section_offset ();
13372
13373 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13374 if (attr == NULL)
13375 {
13376 /* This was a pre-DWARF-5 GNU extension alias
13377 for DW_AT_call_return_pc. */
13378 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13379 }
13380 if (!attr)
13381 {
13382 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13383 "DIE %s [in module %s]"),
13384 sect_offset_str (die->sect_off), objfile_name (objfile));
13385 return;
13386 }
13387 pc = attr->value_as_address () + baseaddr;
13388 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13389
13390 if (cu->call_site_htab == NULL)
13391 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13392 NULL, &objfile->objfile_obstack,
13393 hashtab_obstack_allocate, NULL);
13394 call_site_local.pc = pc;
13395 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13396 if (*slot != NULL)
13397 {
13398 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13399 "DIE %s [in module %s]"),
13400 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13401 objfile_name (objfile));
13402 return;
13403 }
13404
13405 /* Count parameters at the caller. */
13406
13407 nparams = 0;
13408 for (child_die = die->child; child_die && child_die->tag;
13409 child_die = child_die->sibling)
13410 {
13411 if (child_die->tag != DW_TAG_call_site_parameter
13412 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13413 {
13414 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13415 "DW_TAG_call_site child DIE %s [in module %s]"),
13416 child_die->tag, sect_offset_str (child_die->sect_off),
13417 objfile_name (objfile));
13418 continue;
13419 }
13420
13421 nparams++;
13422 }
13423
13424 call_site
13425 = ((struct call_site *)
13426 obstack_alloc (&objfile->objfile_obstack,
13427 sizeof (*call_site)
13428 + (sizeof (*call_site->parameter) * (nparams - 1))));
13429 *slot = call_site;
13430 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13431 call_site->pc = pc;
13432
13433 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13434 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13435 {
13436 struct die_info *func_die;
13437
13438 /* Skip also over DW_TAG_inlined_subroutine. */
13439 for (func_die = die->parent;
13440 func_die && func_die->tag != DW_TAG_subprogram
13441 && func_die->tag != DW_TAG_subroutine_type;
13442 func_die = func_die->parent);
13443
13444 /* DW_AT_call_all_calls is a superset
13445 of DW_AT_call_all_tail_calls. */
13446 if (func_die
13447 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13448 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13449 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13450 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13451 {
13452 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13453 not complete. But keep CALL_SITE for look ups via call_site_htab,
13454 both the initial caller containing the real return address PC and
13455 the final callee containing the current PC of a chain of tail
13456 calls do not need to have the tail call list complete. But any
13457 function candidate for a virtual tail call frame searched via
13458 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13459 determined unambiguously. */
13460 }
13461 else
13462 {
13463 struct type *func_type = NULL;
13464
13465 if (func_die)
13466 func_type = get_die_type (func_die, cu);
13467 if (func_type != NULL)
13468 {
13469 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13470
13471 /* Enlist this call site to the function. */
13472 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13473 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13474 }
13475 else
13476 complaint (_("Cannot find function owning DW_TAG_call_site "
13477 "DIE %s [in module %s]"),
13478 sect_offset_str (die->sect_off), objfile_name (objfile));
13479 }
13480 }
13481
13482 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13483 if (attr == NULL)
13484 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13485 if (attr == NULL)
13486 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13487 if (attr == NULL)
13488 {
13489 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13490 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13491 }
13492 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13493 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13494 /* Keep NULL DWARF_BLOCK. */;
13495 else if (attr->form_is_block ())
13496 {
13497 struct dwarf2_locexpr_baton *dlbaton;
13498
13499 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13500 dlbaton->data = DW_BLOCK (attr)->data;
13501 dlbaton->size = DW_BLOCK (attr)->size;
13502 dlbaton->per_objfile = per_objfile;
13503 dlbaton->per_cu = cu->per_cu;
13504
13505 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13506 }
13507 else if (attr->form_is_ref ())
13508 {
13509 struct dwarf2_cu *target_cu = cu;
13510 struct die_info *target_die;
13511
13512 target_die = follow_die_ref (die, attr, &target_cu);
13513 gdb_assert (target_cu->per_objfile->objfile == objfile);
13514 if (die_is_declaration (target_die, target_cu))
13515 {
13516 const char *target_physname;
13517
13518 /* Prefer the mangled name; otherwise compute the demangled one. */
13519 target_physname = dw2_linkage_name (target_die, target_cu);
13520 if (target_physname == NULL)
13521 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13522 if (target_physname == NULL)
13523 complaint (_("DW_AT_call_target target DIE has invalid "
13524 "physname, for referencing DIE %s [in module %s]"),
13525 sect_offset_str (die->sect_off), objfile_name (objfile));
13526 else
13527 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13528 }
13529 else
13530 {
13531 CORE_ADDR lowpc;
13532
13533 /* DW_AT_entry_pc should be preferred. */
13534 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13535 <= PC_BOUNDS_INVALID)
13536 complaint (_("DW_AT_call_target target DIE has invalid "
13537 "low pc, for referencing DIE %s [in module %s]"),
13538 sect_offset_str (die->sect_off), objfile_name (objfile));
13539 else
13540 {
13541 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13542 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13543 }
13544 }
13545 }
13546 else
13547 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13548 "block nor reference, for DIE %s [in module %s]"),
13549 sect_offset_str (die->sect_off), objfile_name (objfile));
13550
13551 call_site->per_cu = cu->per_cu;
13552 call_site->per_objfile = per_objfile;
13553
13554 for (child_die = die->child;
13555 child_die && child_die->tag;
13556 child_die = child_die->sibling)
13557 {
13558 struct call_site_parameter *parameter;
13559 struct attribute *loc, *origin;
13560
13561 if (child_die->tag != DW_TAG_call_site_parameter
13562 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13563 {
13564 /* Already printed the complaint above. */
13565 continue;
13566 }
13567
13568 gdb_assert (call_site->parameter_count < nparams);
13569 parameter = &call_site->parameter[call_site->parameter_count];
13570
13571 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13572 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13573 register is contained in DW_AT_call_value. */
13574
13575 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13576 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13577 if (origin == NULL)
13578 {
13579 /* This was a pre-DWARF-5 GNU extension alias
13580 for DW_AT_call_parameter. */
13581 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13582 }
13583 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13584 {
13585 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13586
13587 sect_offset sect_off = origin->get_ref_die_offset ();
13588 if (!cu->header.offset_in_cu_p (sect_off))
13589 {
13590 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13591 binding can be done only inside one CU. Such referenced DIE
13592 therefore cannot be even moved to DW_TAG_partial_unit. */
13593 complaint (_("DW_AT_call_parameter offset is not in CU for "
13594 "DW_TAG_call_site child DIE %s [in module %s]"),
13595 sect_offset_str (child_die->sect_off),
13596 objfile_name (objfile));
13597 continue;
13598 }
13599 parameter->u.param_cu_off
13600 = (cu_offset) (sect_off - cu->header.sect_off);
13601 }
13602 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13603 {
13604 complaint (_("No DW_FORM_block* DW_AT_location for "
13605 "DW_TAG_call_site child DIE %s [in module %s]"),
13606 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13607 continue;
13608 }
13609 else
13610 {
13611 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13612 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13613 if (parameter->u.dwarf_reg != -1)
13614 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13615 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13616 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13617 &parameter->u.fb_offset))
13618 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13619 else
13620 {
13621 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13622 "for DW_FORM_block* DW_AT_location is supported for "
13623 "DW_TAG_call_site child DIE %s "
13624 "[in module %s]"),
13625 sect_offset_str (child_die->sect_off),
13626 objfile_name (objfile));
13627 continue;
13628 }
13629 }
13630
13631 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13632 if (attr == NULL)
13633 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13634 if (attr == NULL || !attr->form_is_block ())
13635 {
13636 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13637 "DW_TAG_call_site child DIE %s [in module %s]"),
13638 sect_offset_str (child_die->sect_off),
13639 objfile_name (objfile));
13640 continue;
13641 }
13642 parameter->value = DW_BLOCK (attr)->data;
13643 parameter->value_size = DW_BLOCK (attr)->size;
13644
13645 /* Parameters are not pre-cleared by memset above. */
13646 parameter->data_value = NULL;
13647 parameter->data_value_size = 0;
13648 call_site->parameter_count++;
13649
13650 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13651 if (attr == NULL)
13652 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13653 if (attr != nullptr)
13654 {
13655 if (!attr->form_is_block ())
13656 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13657 "DW_TAG_call_site child DIE %s [in module %s]"),
13658 sect_offset_str (child_die->sect_off),
13659 objfile_name (objfile));
13660 else
13661 {
13662 parameter->data_value = DW_BLOCK (attr)->data;
13663 parameter->data_value_size = DW_BLOCK (attr)->size;
13664 }
13665 }
13666 }
13667 }
13668
13669 /* Helper function for read_variable. If DIE represents a virtual
13670 table, then return the type of the concrete object that is
13671 associated with the virtual table. Otherwise, return NULL. */
13672
13673 static struct type *
13674 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13675 {
13676 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13677 if (attr == NULL)
13678 return NULL;
13679
13680 /* Find the type DIE. */
13681 struct die_info *type_die = NULL;
13682 struct dwarf2_cu *type_cu = cu;
13683
13684 if (attr->form_is_ref ())
13685 type_die = follow_die_ref (die, attr, &type_cu);
13686 if (type_die == NULL)
13687 return NULL;
13688
13689 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13690 return NULL;
13691 return die_containing_type (type_die, type_cu);
13692 }
13693
13694 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13695
13696 static void
13697 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13698 {
13699 struct rust_vtable_symbol *storage = NULL;
13700
13701 if (cu->language == language_rust)
13702 {
13703 struct type *containing_type = rust_containing_type (die, cu);
13704
13705 if (containing_type != NULL)
13706 {
13707 struct objfile *objfile = cu->per_objfile->objfile;
13708
13709 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13710 storage->concrete_type = containing_type;
13711 storage->subclass = SYMBOL_RUST_VTABLE;
13712 }
13713 }
13714
13715 struct symbol *res = new_symbol (die, NULL, cu, storage);
13716 struct attribute *abstract_origin
13717 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13718 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13719 if (res == NULL && loc && abstract_origin)
13720 {
13721 /* We have a variable without a name, but with a location and an abstract
13722 origin. This may be a concrete instance of an abstract variable
13723 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13724 later. */
13725 struct dwarf2_cu *origin_cu = cu;
13726 struct die_info *origin_die
13727 = follow_die_ref (die, abstract_origin, &origin_cu);
13728 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13729 per_objfile->per_bfd->abstract_to_concrete
13730 [origin_die->sect_off].push_back (die->sect_off);
13731 }
13732 }
13733
13734 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13735 reading .debug_rnglists.
13736 Callback's type should be:
13737 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13738 Return true if the attributes are present and valid, otherwise,
13739 return false. */
13740
13741 template <typename Callback>
13742 static bool
13743 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13744 Callback &&callback)
13745 {
13746 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13747 struct objfile *objfile = per_objfile->objfile;
13748 bfd *obfd = objfile->obfd;
13749 /* Base address selection entry. */
13750 gdb::optional<CORE_ADDR> base;
13751 const gdb_byte *buffer;
13752 CORE_ADDR baseaddr;
13753 bool overflow = false;
13754
13755 base = cu->base_address;
13756
13757 per_objfile->per_bfd->rnglists.read (objfile);
13758 if (offset >= per_objfile->per_bfd->rnglists.size)
13759 {
13760 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13761 offset);
13762 return false;
13763 }
13764 buffer = per_objfile->per_bfd->rnglists.buffer + offset;
13765
13766 baseaddr = objfile->text_section_offset ();
13767
13768 while (1)
13769 {
13770 /* Initialize it due to a false compiler warning. */
13771 CORE_ADDR range_beginning = 0, range_end = 0;
13772 const gdb_byte *buf_end = (per_objfile->per_bfd->rnglists.buffer
13773 + per_objfile->per_bfd->rnglists.size);
13774 unsigned int bytes_read;
13775
13776 if (buffer == buf_end)
13777 {
13778 overflow = true;
13779 break;
13780 }
13781 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13782 switch (rlet)
13783 {
13784 case DW_RLE_end_of_list:
13785 break;
13786 case DW_RLE_base_address:
13787 if (buffer + cu->header.addr_size > buf_end)
13788 {
13789 overflow = true;
13790 break;
13791 }
13792 base = cu->header.read_address (obfd, buffer, &bytes_read);
13793 buffer += bytes_read;
13794 break;
13795 case DW_RLE_start_length:
13796 if (buffer + cu->header.addr_size > buf_end)
13797 {
13798 overflow = true;
13799 break;
13800 }
13801 range_beginning = cu->header.read_address (obfd, buffer,
13802 &bytes_read);
13803 buffer += bytes_read;
13804 range_end = (range_beginning
13805 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13806 buffer += bytes_read;
13807 if (buffer > buf_end)
13808 {
13809 overflow = true;
13810 break;
13811 }
13812 break;
13813 case DW_RLE_offset_pair:
13814 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13815 buffer += bytes_read;
13816 if (buffer > buf_end)
13817 {
13818 overflow = true;
13819 break;
13820 }
13821 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13822 buffer += bytes_read;
13823 if (buffer > buf_end)
13824 {
13825 overflow = true;
13826 break;
13827 }
13828 break;
13829 case DW_RLE_start_end:
13830 if (buffer + 2 * cu->header.addr_size > buf_end)
13831 {
13832 overflow = true;
13833 break;
13834 }
13835 range_beginning = cu->header.read_address (obfd, buffer,
13836 &bytes_read);
13837 buffer += bytes_read;
13838 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13839 buffer += bytes_read;
13840 break;
13841 default:
13842 complaint (_("Invalid .debug_rnglists data (no base address)"));
13843 return false;
13844 }
13845 if (rlet == DW_RLE_end_of_list || overflow)
13846 break;
13847 if (rlet == DW_RLE_base_address)
13848 continue;
13849
13850 if (!base.has_value ())
13851 {
13852 /* We have no valid base address for the ranges
13853 data. */
13854 complaint (_("Invalid .debug_rnglists data (no base address)"));
13855 return false;
13856 }
13857
13858 if (range_beginning > range_end)
13859 {
13860 /* Inverted range entries are invalid. */
13861 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13862 return false;
13863 }
13864
13865 /* Empty range entries have no effect. */
13866 if (range_beginning == range_end)
13867 continue;
13868
13869 range_beginning += *base;
13870 range_end += *base;
13871
13872 /* A not-uncommon case of bad debug info.
13873 Don't pollute the addrmap with bad data. */
13874 if (range_beginning + baseaddr == 0
13875 && !per_objfile->per_bfd->has_section_at_zero)
13876 {
13877 complaint (_(".debug_rnglists entry has start address of zero"
13878 " [in module %s]"), objfile_name (objfile));
13879 continue;
13880 }
13881
13882 callback (range_beginning, range_end);
13883 }
13884
13885 if (overflow)
13886 {
13887 complaint (_("Offset %d is not terminated "
13888 "for DW_AT_ranges attribute"),
13889 offset);
13890 return false;
13891 }
13892
13893 return true;
13894 }
13895
13896 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13897 Callback's type should be:
13898 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13899 Return 1 if the attributes are present and valid, otherwise, return 0. */
13900
13901 template <typename Callback>
13902 static int
13903 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13904 Callback &&callback)
13905 {
13906 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13907 struct objfile *objfile = per_objfile->objfile;
13908 struct comp_unit_head *cu_header = &cu->header;
13909 bfd *obfd = objfile->obfd;
13910 unsigned int addr_size = cu_header->addr_size;
13911 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13912 /* Base address selection entry. */
13913 gdb::optional<CORE_ADDR> base;
13914 unsigned int dummy;
13915 const gdb_byte *buffer;
13916 CORE_ADDR baseaddr;
13917
13918 if (cu_header->version >= 5)
13919 return dwarf2_rnglists_process (offset, cu, callback);
13920
13921 base = cu->base_address;
13922
13923 per_objfile->per_bfd->ranges.read (objfile);
13924 if (offset >= per_objfile->per_bfd->ranges.size)
13925 {
13926 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13927 offset);
13928 return 0;
13929 }
13930 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13931
13932 baseaddr = objfile->text_section_offset ();
13933
13934 while (1)
13935 {
13936 CORE_ADDR range_beginning, range_end;
13937
13938 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13939 buffer += addr_size;
13940 range_end = cu->header.read_address (obfd, buffer, &dummy);
13941 buffer += addr_size;
13942 offset += 2 * addr_size;
13943
13944 /* An end of list marker is a pair of zero addresses. */
13945 if (range_beginning == 0 && range_end == 0)
13946 /* Found the end of list entry. */
13947 break;
13948
13949 /* Each base address selection entry is a pair of 2 values.
13950 The first is the largest possible address, the second is
13951 the base address. Check for a base address here. */
13952 if ((range_beginning & mask) == mask)
13953 {
13954 /* If we found the largest possible address, then we already
13955 have the base address in range_end. */
13956 base = range_end;
13957 continue;
13958 }
13959
13960 if (!base.has_value ())
13961 {
13962 /* We have no valid base address for the ranges
13963 data. */
13964 complaint (_("Invalid .debug_ranges data (no base address)"));
13965 return 0;
13966 }
13967
13968 if (range_beginning > range_end)
13969 {
13970 /* Inverted range entries are invalid. */
13971 complaint (_("Invalid .debug_ranges data (inverted range)"));
13972 return 0;
13973 }
13974
13975 /* Empty range entries have no effect. */
13976 if (range_beginning == range_end)
13977 continue;
13978
13979 range_beginning += *base;
13980 range_end += *base;
13981
13982 /* A not-uncommon case of bad debug info.
13983 Don't pollute the addrmap with bad data. */
13984 if (range_beginning + baseaddr == 0
13985 && !per_objfile->per_bfd->has_section_at_zero)
13986 {
13987 complaint (_(".debug_ranges entry has start address of zero"
13988 " [in module %s]"), objfile_name (objfile));
13989 continue;
13990 }
13991
13992 callback (range_beginning, range_end);
13993 }
13994
13995 return 1;
13996 }
13997
13998 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13999 Return 1 if the attributes are present and valid, otherwise, return 0.
14000 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14001
14002 static int
14003 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14004 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14005 dwarf2_psymtab *ranges_pst)
14006 {
14007 struct objfile *objfile = cu->per_objfile->objfile;
14008 struct gdbarch *gdbarch = objfile->arch ();
14009 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14010 int low_set = 0;
14011 CORE_ADDR low = 0;
14012 CORE_ADDR high = 0;
14013 int retval;
14014
14015 retval = dwarf2_ranges_process (offset, cu,
14016 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14017 {
14018 if (ranges_pst != NULL)
14019 {
14020 CORE_ADDR lowpc;
14021 CORE_ADDR highpc;
14022
14023 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14024 range_beginning + baseaddr)
14025 - baseaddr);
14026 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14027 range_end + baseaddr)
14028 - baseaddr);
14029 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14030 lowpc, highpc - 1, ranges_pst);
14031 }
14032
14033 /* FIXME: This is recording everything as a low-high
14034 segment of consecutive addresses. We should have a
14035 data structure for discontiguous block ranges
14036 instead. */
14037 if (! low_set)
14038 {
14039 low = range_beginning;
14040 high = range_end;
14041 low_set = 1;
14042 }
14043 else
14044 {
14045 if (range_beginning < low)
14046 low = range_beginning;
14047 if (range_end > high)
14048 high = range_end;
14049 }
14050 });
14051 if (!retval)
14052 return 0;
14053
14054 if (! low_set)
14055 /* If the first entry is an end-of-list marker, the range
14056 describes an empty scope, i.e. no instructions. */
14057 return 0;
14058
14059 if (low_return)
14060 *low_return = low;
14061 if (high_return)
14062 *high_return = high;
14063 return 1;
14064 }
14065
14066 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14067 definition for the return value. *LOWPC and *HIGHPC are set iff
14068 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14069
14070 static enum pc_bounds_kind
14071 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14072 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14073 dwarf2_psymtab *pst)
14074 {
14075 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14076 struct attribute *attr;
14077 struct attribute *attr_high;
14078 CORE_ADDR low = 0;
14079 CORE_ADDR high = 0;
14080 enum pc_bounds_kind ret;
14081
14082 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14083 if (attr_high)
14084 {
14085 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14086 if (attr != nullptr)
14087 {
14088 low = attr->value_as_address ();
14089 high = attr_high->value_as_address ();
14090 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14091 high += low;
14092 }
14093 else
14094 /* Found high w/o low attribute. */
14095 return PC_BOUNDS_INVALID;
14096
14097 /* Found consecutive range of addresses. */
14098 ret = PC_BOUNDS_HIGH_LOW;
14099 }
14100 else
14101 {
14102 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14103 if (attr != NULL)
14104 {
14105 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14106 We take advantage of the fact that DW_AT_ranges does not appear
14107 in DW_TAG_compile_unit of DWO files. */
14108 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14109 unsigned int ranges_offset = (DW_UNSND (attr)
14110 + (need_ranges_base
14111 ? cu->ranges_base
14112 : 0));
14113
14114 /* Value of the DW_AT_ranges attribute is the offset in the
14115 .debug_ranges section. */
14116 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14117 return PC_BOUNDS_INVALID;
14118 /* Found discontinuous range of addresses. */
14119 ret = PC_BOUNDS_RANGES;
14120 }
14121 else
14122 return PC_BOUNDS_NOT_PRESENT;
14123 }
14124
14125 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14126 if (high <= low)
14127 return PC_BOUNDS_INVALID;
14128
14129 /* When using the GNU linker, .gnu.linkonce. sections are used to
14130 eliminate duplicate copies of functions and vtables and such.
14131 The linker will arbitrarily choose one and discard the others.
14132 The AT_*_pc values for such functions refer to local labels in
14133 these sections. If the section from that file was discarded, the
14134 labels are not in the output, so the relocs get a value of 0.
14135 If this is a discarded function, mark the pc bounds as invalid,
14136 so that GDB will ignore it. */
14137 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14138 return PC_BOUNDS_INVALID;
14139
14140 *lowpc = low;
14141 if (highpc)
14142 *highpc = high;
14143 return ret;
14144 }
14145
14146 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14147 its low and high PC addresses. Do nothing if these addresses could not
14148 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14149 and HIGHPC to the high address if greater than HIGHPC. */
14150
14151 static void
14152 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14153 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14154 struct dwarf2_cu *cu)
14155 {
14156 CORE_ADDR low, high;
14157 struct die_info *child = die->child;
14158
14159 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14160 {
14161 *lowpc = std::min (*lowpc, low);
14162 *highpc = std::max (*highpc, high);
14163 }
14164
14165 /* If the language does not allow nested subprograms (either inside
14166 subprograms or lexical blocks), we're done. */
14167 if (cu->language != language_ada)
14168 return;
14169
14170 /* Check all the children of the given DIE. If it contains nested
14171 subprograms, then check their pc bounds. Likewise, we need to
14172 check lexical blocks as well, as they may also contain subprogram
14173 definitions. */
14174 while (child && child->tag)
14175 {
14176 if (child->tag == DW_TAG_subprogram
14177 || child->tag == DW_TAG_lexical_block)
14178 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14179 child = child->sibling;
14180 }
14181 }
14182
14183 /* Get the low and high pc's represented by the scope DIE, and store
14184 them in *LOWPC and *HIGHPC. If the correct values can't be
14185 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14186
14187 static void
14188 get_scope_pc_bounds (struct die_info *die,
14189 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14190 struct dwarf2_cu *cu)
14191 {
14192 CORE_ADDR best_low = (CORE_ADDR) -1;
14193 CORE_ADDR best_high = (CORE_ADDR) 0;
14194 CORE_ADDR current_low, current_high;
14195
14196 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14197 >= PC_BOUNDS_RANGES)
14198 {
14199 best_low = current_low;
14200 best_high = current_high;
14201 }
14202 else
14203 {
14204 struct die_info *child = die->child;
14205
14206 while (child && child->tag)
14207 {
14208 switch (child->tag) {
14209 case DW_TAG_subprogram:
14210 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14211 break;
14212 case DW_TAG_namespace:
14213 case DW_TAG_module:
14214 /* FIXME: carlton/2004-01-16: Should we do this for
14215 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14216 that current GCC's always emit the DIEs corresponding
14217 to definitions of methods of classes as children of a
14218 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14219 the DIEs giving the declarations, which could be
14220 anywhere). But I don't see any reason why the
14221 standards says that they have to be there. */
14222 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14223
14224 if (current_low != ((CORE_ADDR) -1))
14225 {
14226 best_low = std::min (best_low, current_low);
14227 best_high = std::max (best_high, current_high);
14228 }
14229 break;
14230 default:
14231 /* Ignore. */
14232 break;
14233 }
14234
14235 child = child->sibling;
14236 }
14237 }
14238
14239 *lowpc = best_low;
14240 *highpc = best_high;
14241 }
14242
14243 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14244 in DIE. */
14245
14246 static void
14247 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14248 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14249 {
14250 struct objfile *objfile = cu->per_objfile->objfile;
14251 struct gdbarch *gdbarch = objfile->arch ();
14252 struct attribute *attr;
14253 struct attribute *attr_high;
14254
14255 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14256 if (attr_high)
14257 {
14258 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14259 if (attr != nullptr)
14260 {
14261 CORE_ADDR low = attr->value_as_address ();
14262 CORE_ADDR high = attr_high->value_as_address ();
14263
14264 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14265 high += low;
14266
14267 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14268 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14269 cu->get_builder ()->record_block_range (block, low, high - 1);
14270 }
14271 }
14272
14273 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14274 if (attr != nullptr)
14275 {
14276 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14277 We take advantage of the fact that DW_AT_ranges does not appear
14278 in DW_TAG_compile_unit of DWO files. */
14279 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14280
14281 /* The value of the DW_AT_ranges attribute is the offset of the
14282 address range list in the .debug_ranges section. */
14283 unsigned long offset = (DW_UNSND (attr)
14284 + (need_ranges_base ? cu->ranges_base : 0));
14285
14286 std::vector<blockrange> blockvec;
14287 dwarf2_ranges_process (offset, cu,
14288 [&] (CORE_ADDR start, CORE_ADDR end)
14289 {
14290 start += baseaddr;
14291 end += baseaddr;
14292 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14293 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14294 cu->get_builder ()->record_block_range (block, start, end - 1);
14295 blockvec.emplace_back (start, end);
14296 });
14297
14298 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14299 }
14300 }
14301
14302 /* Check whether the producer field indicates either of GCC < 4.6, or the
14303 Intel C/C++ compiler, and cache the result in CU. */
14304
14305 static void
14306 check_producer (struct dwarf2_cu *cu)
14307 {
14308 int major, minor;
14309
14310 if (cu->producer == NULL)
14311 {
14312 /* For unknown compilers expect their behavior is DWARF version
14313 compliant.
14314
14315 GCC started to support .debug_types sections by -gdwarf-4 since
14316 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14317 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14318 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14319 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14320 }
14321 else if (producer_is_gcc (cu->producer, &major, &minor))
14322 {
14323 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14324 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14325 }
14326 else if (producer_is_icc (cu->producer, &major, &minor))
14327 {
14328 cu->producer_is_icc = true;
14329 cu->producer_is_icc_lt_14 = major < 14;
14330 }
14331 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14332 cu->producer_is_codewarrior = true;
14333 else
14334 {
14335 /* For other non-GCC compilers, expect their behavior is DWARF version
14336 compliant. */
14337 }
14338
14339 cu->checked_producer = true;
14340 }
14341
14342 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14343 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14344 during 4.6.0 experimental. */
14345
14346 static bool
14347 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14348 {
14349 if (!cu->checked_producer)
14350 check_producer (cu);
14351
14352 return cu->producer_is_gxx_lt_4_6;
14353 }
14354
14355
14356 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14357 with incorrect is_stmt attributes. */
14358
14359 static bool
14360 producer_is_codewarrior (struct dwarf2_cu *cu)
14361 {
14362 if (!cu->checked_producer)
14363 check_producer (cu);
14364
14365 return cu->producer_is_codewarrior;
14366 }
14367
14368 /* Return the default accessibility type if it is not overridden by
14369 DW_AT_accessibility. */
14370
14371 static enum dwarf_access_attribute
14372 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14373 {
14374 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14375 {
14376 /* The default DWARF 2 accessibility for members is public, the default
14377 accessibility for inheritance is private. */
14378
14379 if (die->tag != DW_TAG_inheritance)
14380 return DW_ACCESS_public;
14381 else
14382 return DW_ACCESS_private;
14383 }
14384 else
14385 {
14386 /* DWARF 3+ defines the default accessibility a different way. The same
14387 rules apply now for DW_TAG_inheritance as for the members and it only
14388 depends on the container kind. */
14389
14390 if (die->parent->tag == DW_TAG_class_type)
14391 return DW_ACCESS_private;
14392 else
14393 return DW_ACCESS_public;
14394 }
14395 }
14396
14397 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14398 offset. If the attribute was not found return 0, otherwise return
14399 1. If it was found but could not properly be handled, set *OFFSET
14400 to 0. */
14401
14402 static int
14403 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14404 LONGEST *offset)
14405 {
14406 struct attribute *attr;
14407
14408 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14409 if (attr != NULL)
14410 {
14411 *offset = 0;
14412
14413 /* Note that we do not check for a section offset first here.
14414 This is because DW_AT_data_member_location is new in DWARF 4,
14415 so if we see it, we can assume that a constant form is really
14416 a constant and not a section offset. */
14417 if (attr->form_is_constant ())
14418 *offset = attr->constant_value (0);
14419 else if (attr->form_is_section_offset ())
14420 dwarf2_complex_location_expr_complaint ();
14421 else if (attr->form_is_block ())
14422 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14423 else
14424 dwarf2_complex_location_expr_complaint ();
14425
14426 return 1;
14427 }
14428
14429 return 0;
14430 }
14431
14432 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14433
14434 static void
14435 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14436 struct field *field)
14437 {
14438 struct attribute *attr;
14439
14440 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14441 if (attr != NULL)
14442 {
14443 if (attr->form_is_constant ())
14444 {
14445 LONGEST offset = attr->constant_value (0);
14446 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14447 }
14448 else if (attr->form_is_section_offset ())
14449 dwarf2_complex_location_expr_complaint ();
14450 else if (attr->form_is_block ())
14451 {
14452 bool handled;
14453 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14454 if (handled)
14455 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14456 else
14457 {
14458 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14459 struct objfile *objfile = per_objfile->objfile;
14460 struct dwarf2_locexpr_baton *dlbaton
14461 = XOBNEW (&objfile->objfile_obstack,
14462 struct dwarf2_locexpr_baton);
14463 dlbaton->data = DW_BLOCK (attr)->data;
14464 dlbaton->size = DW_BLOCK (attr)->size;
14465 /* When using this baton, we want to compute the address
14466 of the field, not the value. This is why
14467 is_reference is set to false here. */
14468 dlbaton->is_reference = false;
14469 dlbaton->per_objfile = per_objfile;
14470 dlbaton->per_cu = cu->per_cu;
14471
14472 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14473 }
14474 }
14475 else
14476 dwarf2_complex_location_expr_complaint ();
14477 }
14478 }
14479
14480 /* Add an aggregate field to the field list. */
14481
14482 static void
14483 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14484 struct dwarf2_cu *cu)
14485 {
14486 struct objfile *objfile = cu->per_objfile->objfile;
14487 struct gdbarch *gdbarch = objfile->arch ();
14488 struct nextfield *new_field;
14489 struct attribute *attr;
14490 struct field *fp;
14491 const char *fieldname = "";
14492
14493 if (die->tag == DW_TAG_inheritance)
14494 {
14495 fip->baseclasses.emplace_back ();
14496 new_field = &fip->baseclasses.back ();
14497 }
14498 else
14499 {
14500 fip->fields.emplace_back ();
14501 new_field = &fip->fields.back ();
14502 }
14503
14504 new_field->offset = die->sect_off;
14505
14506 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14507 if (attr != nullptr)
14508 new_field->accessibility = DW_UNSND (attr);
14509 else
14510 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14511 if (new_field->accessibility != DW_ACCESS_public)
14512 fip->non_public_fields = 1;
14513
14514 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14515 if (attr != nullptr)
14516 new_field->virtuality = DW_UNSND (attr);
14517 else
14518 new_field->virtuality = DW_VIRTUALITY_none;
14519
14520 fp = &new_field->field;
14521
14522 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14523 {
14524 /* Data member other than a C++ static data member. */
14525
14526 /* Get type of field. */
14527 fp->type = die_type (die, cu);
14528
14529 SET_FIELD_BITPOS (*fp, 0);
14530
14531 /* Get bit size of field (zero if none). */
14532 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14533 if (attr != nullptr)
14534 {
14535 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14536 }
14537 else
14538 {
14539 FIELD_BITSIZE (*fp) = 0;
14540 }
14541
14542 /* Get bit offset of field. */
14543 handle_data_member_location (die, cu, fp);
14544 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14545 if (attr != nullptr)
14546 {
14547 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14548 {
14549 /* For big endian bits, the DW_AT_bit_offset gives the
14550 additional bit offset from the MSB of the containing
14551 anonymous object to the MSB of the field. We don't
14552 have to do anything special since we don't need to
14553 know the size of the anonymous object. */
14554 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14555 }
14556 else
14557 {
14558 /* For little endian bits, compute the bit offset to the
14559 MSB of the anonymous object, subtract off the number of
14560 bits from the MSB of the field to the MSB of the
14561 object, and then subtract off the number of bits of
14562 the field itself. The result is the bit offset of
14563 the LSB of the field. */
14564 int anonymous_size;
14565 int bit_offset = DW_UNSND (attr);
14566
14567 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14568 if (attr != nullptr)
14569 {
14570 /* The size of the anonymous object containing
14571 the bit field is explicit, so use the
14572 indicated size (in bytes). */
14573 anonymous_size = DW_UNSND (attr);
14574 }
14575 else
14576 {
14577 /* The size of the anonymous object containing
14578 the bit field must be inferred from the type
14579 attribute of the data member containing the
14580 bit field. */
14581 anonymous_size = TYPE_LENGTH (fp->type);
14582 }
14583 SET_FIELD_BITPOS (*fp,
14584 (FIELD_BITPOS (*fp)
14585 + anonymous_size * bits_per_byte
14586 - bit_offset - FIELD_BITSIZE (*fp)));
14587 }
14588 }
14589 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14590 if (attr != NULL)
14591 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14592 + attr->constant_value (0)));
14593
14594 /* Get name of field. */
14595 fieldname = dwarf2_name (die, cu);
14596 if (fieldname == NULL)
14597 fieldname = "";
14598
14599 /* The name is already allocated along with this objfile, so we don't
14600 need to duplicate it for the type. */
14601 fp->name = fieldname;
14602
14603 /* Change accessibility for artificial fields (e.g. virtual table
14604 pointer or virtual base class pointer) to private. */
14605 if (dwarf2_attr (die, DW_AT_artificial, cu))
14606 {
14607 FIELD_ARTIFICIAL (*fp) = 1;
14608 new_field->accessibility = DW_ACCESS_private;
14609 fip->non_public_fields = 1;
14610 }
14611 }
14612 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14613 {
14614 /* C++ static member. */
14615
14616 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14617 is a declaration, but all versions of G++ as of this writing
14618 (so through at least 3.2.1) incorrectly generate
14619 DW_TAG_variable tags. */
14620
14621 const char *physname;
14622
14623 /* Get name of field. */
14624 fieldname = dwarf2_name (die, cu);
14625 if (fieldname == NULL)
14626 return;
14627
14628 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14629 if (attr
14630 /* Only create a symbol if this is an external value.
14631 new_symbol checks this and puts the value in the global symbol
14632 table, which we want. If it is not external, new_symbol
14633 will try to put the value in cu->list_in_scope which is wrong. */
14634 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14635 {
14636 /* A static const member, not much different than an enum as far as
14637 we're concerned, except that we can support more types. */
14638 new_symbol (die, NULL, cu);
14639 }
14640
14641 /* Get physical name. */
14642 physname = dwarf2_physname (fieldname, die, cu);
14643
14644 /* The name is already allocated along with this objfile, so we don't
14645 need to duplicate it for the type. */
14646 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14647 FIELD_TYPE (*fp) = die_type (die, cu);
14648 FIELD_NAME (*fp) = fieldname;
14649 }
14650 else if (die->tag == DW_TAG_inheritance)
14651 {
14652 /* C++ base class field. */
14653 handle_data_member_location (die, cu, fp);
14654 FIELD_BITSIZE (*fp) = 0;
14655 FIELD_TYPE (*fp) = die_type (die, cu);
14656 FIELD_NAME (*fp) = fp->type->name ();
14657 }
14658 else
14659 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14660 }
14661
14662 /* Can the type given by DIE define another type? */
14663
14664 static bool
14665 type_can_define_types (const struct die_info *die)
14666 {
14667 switch (die->tag)
14668 {
14669 case DW_TAG_typedef:
14670 case DW_TAG_class_type:
14671 case DW_TAG_structure_type:
14672 case DW_TAG_union_type:
14673 case DW_TAG_enumeration_type:
14674 return true;
14675
14676 default:
14677 return false;
14678 }
14679 }
14680
14681 /* Add a type definition defined in the scope of the FIP's class. */
14682
14683 static void
14684 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14685 struct dwarf2_cu *cu)
14686 {
14687 struct decl_field fp;
14688 memset (&fp, 0, sizeof (fp));
14689
14690 gdb_assert (type_can_define_types (die));
14691
14692 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14693 fp.name = dwarf2_name (die, cu);
14694 fp.type = read_type_die (die, cu);
14695
14696 /* Save accessibility. */
14697 enum dwarf_access_attribute accessibility;
14698 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14699 if (attr != NULL)
14700 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14701 else
14702 accessibility = dwarf2_default_access_attribute (die, cu);
14703 switch (accessibility)
14704 {
14705 case DW_ACCESS_public:
14706 /* The assumed value if neither private nor protected. */
14707 break;
14708 case DW_ACCESS_private:
14709 fp.is_private = 1;
14710 break;
14711 case DW_ACCESS_protected:
14712 fp.is_protected = 1;
14713 break;
14714 default:
14715 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14716 }
14717
14718 if (die->tag == DW_TAG_typedef)
14719 fip->typedef_field_list.push_back (fp);
14720 else
14721 fip->nested_types_list.push_back (fp);
14722 }
14723
14724 /* A convenience typedef that's used when finding the discriminant
14725 field for a variant part. */
14726 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14727 offset_map_type;
14728
14729 /* Compute the discriminant range for a given variant. OBSTACK is
14730 where the results will be stored. VARIANT is the variant to
14731 process. IS_UNSIGNED indicates whether the discriminant is signed
14732 or unsigned. */
14733
14734 static const gdb::array_view<discriminant_range>
14735 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14736 bool is_unsigned)
14737 {
14738 std::vector<discriminant_range> ranges;
14739
14740 if (variant.default_branch)
14741 return {};
14742
14743 if (variant.discr_list_data == nullptr)
14744 {
14745 discriminant_range r
14746 = {variant.discriminant_value, variant.discriminant_value};
14747 ranges.push_back (r);
14748 }
14749 else
14750 {
14751 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14752 variant.discr_list_data->size);
14753 while (!data.empty ())
14754 {
14755 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14756 {
14757 complaint (_("invalid discriminant marker: %d"), data[0]);
14758 break;
14759 }
14760 bool is_range = data[0] == DW_DSC_range;
14761 data = data.slice (1);
14762
14763 ULONGEST low, high;
14764 unsigned int bytes_read;
14765
14766 if (data.empty ())
14767 {
14768 complaint (_("DW_AT_discr_list missing low value"));
14769 break;
14770 }
14771 if (is_unsigned)
14772 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14773 else
14774 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14775 &bytes_read);
14776 data = data.slice (bytes_read);
14777
14778 if (is_range)
14779 {
14780 if (data.empty ())
14781 {
14782 complaint (_("DW_AT_discr_list missing high value"));
14783 break;
14784 }
14785 if (is_unsigned)
14786 high = read_unsigned_leb128 (nullptr, data.data (),
14787 &bytes_read);
14788 else
14789 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14790 &bytes_read);
14791 data = data.slice (bytes_read);
14792 }
14793 else
14794 high = low;
14795
14796 ranges.push_back ({ low, high });
14797 }
14798 }
14799
14800 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14801 ranges.size ());
14802 std::copy (ranges.begin (), ranges.end (), result);
14803 return gdb::array_view<discriminant_range> (result, ranges.size ());
14804 }
14805
14806 static const gdb::array_view<variant_part> create_variant_parts
14807 (struct obstack *obstack,
14808 const offset_map_type &offset_map,
14809 struct field_info *fi,
14810 const std::vector<variant_part_builder> &variant_parts);
14811
14812 /* Fill in a "struct variant" for a given variant field. RESULT is
14813 the variant to fill in. OBSTACK is where any needed allocations
14814 will be done. OFFSET_MAP holds the mapping from section offsets to
14815 fields for the type. FI describes the fields of the type we're
14816 processing. FIELD is the variant field we're converting. */
14817
14818 static void
14819 create_one_variant (variant &result, struct obstack *obstack,
14820 const offset_map_type &offset_map,
14821 struct field_info *fi, const variant_field &field)
14822 {
14823 result.discriminants = convert_variant_range (obstack, field, false);
14824 result.first_field = field.first_field + fi->baseclasses.size ();
14825 result.last_field = field.last_field + fi->baseclasses.size ();
14826 result.parts = create_variant_parts (obstack, offset_map, fi,
14827 field.variant_parts);
14828 }
14829
14830 /* Fill in a "struct variant_part" for a given variant part. RESULT
14831 is the variant part to fill in. OBSTACK is where any needed
14832 allocations will be done. OFFSET_MAP holds the mapping from
14833 section offsets to fields for the type. FI describes the fields of
14834 the type we're processing. BUILDER is the variant part to be
14835 converted. */
14836
14837 static void
14838 create_one_variant_part (variant_part &result,
14839 struct obstack *obstack,
14840 const offset_map_type &offset_map,
14841 struct field_info *fi,
14842 const variant_part_builder &builder)
14843 {
14844 auto iter = offset_map.find (builder.discriminant_offset);
14845 if (iter == offset_map.end ())
14846 {
14847 result.discriminant_index = -1;
14848 /* Doesn't matter. */
14849 result.is_unsigned = false;
14850 }
14851 else
14852 {
14853 result.discriminant_index = iter->second;
14854 result.is_unsigned
14855 = TYPE_UNSIGNED (FIELD_TYPE
14856 (fi->fields[result.discriminant_index].field));
14857 }
14858
14859 size_t n = builder.variants.size ();
14860 variant *output = new (obstack) variant[n];
14861 for (size_t i = 0; i < n; ++i)
14862 create_one_variant (output[i], obstack, offset_map, fi,
14863 builder.variants[i]);
14864
14865 result.variants = gdb::array_view<variant> (output, n);
14866 }
14867
14868 /* Create a vector of variant parts that can be attached to a type.
14869 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14870 holds the mapping from section offsets to fields for the type. FI
14871 describes the fields of the type we're processing. VARIANT_PARTS
14872 is the vector to convert. */
14873
14874 static const gdb::array_view<variant_part>
14875 create_variant_parts (struct obstack *obstack,
14876 const offset_map_type &offset_map,
14877 struct field_info *fi,
14878 const std::vector<variant_part_builder> &variant_parts)
14879 {
14880 if (variant_parts.empty ())
14881 return {};
14882
14883 size_t n = variant_parts.size ();
14884 variant_part *result = new (obstack) variant_part[n];
14885 for (size_t i = 0; i < n; ++i)
14886 create_one_variant_part (result[i], obstack, offset_map, fi,
14887 variant_parts[i]);
14888
14889 return gdb::array_view<variant_part> (result, n);
14890 }
14891
14892 /* Compute the variant part vector for FIP, attaching it to TYPE when
14893 done. */
14894
14895 static void
14896 add_variant_property (struct field_info *fip, struct type *type,
14897 struct dwarf2_cu *cu)
14898 {
14899 /* Map section offsets of fields to their field index. Note the
14900 field index here does not take the number of baseclasses into
14901 account. */
14902 offset_map_type offset_map;
14903 for (int i = 0; i < fip->fields.size (); ++i)
14904 offset_map[fip->fields[i].offset] = i;
14905
14906 struct objfile *objfile = cu->per_objfile->objfile;
14907 gdb::array_view<variant_part> parts
14908 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14909 fip->variant_parts);
14910
14911 struct dynamic_prop prop;
14912 prop.kind = PROP_VARIANT_PARTS;
14913 prop.data.variant_parts
14914 = ((gdb::array_view<variant_part> *)
14915 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14916
14917 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14918 }
14919
14920 /* Create the vector of fields, and attach it to the type. */
14921
14922 static void
14923 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14924 struct dwarf2_cu *cu)
14925 {
14926 int nfields = fip->nfields ();
14927
14928 /* Record the field count, allocate space for the array of fields,
14929 and create blank accessibility bitfields if necessary. */
14930 type->set_num_fields (nfields);
14931 type->set_fields
14932 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14933
14934 if (fip->non_public_fields && cu->language != language_ada)
14935 {
14936 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14937
14938 TYPE_FIELD_PRIVATE_BITS (type) =
14939 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14940 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14941
14942 TYPE_FIELD_PROTECTED_BITS (type) =
14943 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14944 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14945
14946 TYPE_FIELD_IGNORE_BITS (type) =
14947 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14948 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14949 }
14950
14951 /* If the type has baseclasses, allocate and clear a bit vector for
14952 TYPE_FIELD_VIRTUAL_BITS. */
14953 if (!fip->baseclasses.empty () && cu->language != language_ada)
14954 {
14955 int num_bytes = B_BYTES (fip->baseclasses.size ());
14956 unsigned char *pointer;
14957
14958 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14959 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14960 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14961 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14962 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14963 }
14964
14965 if (!fip->variant_parts.empty ())
14966 add_variant_property (fip, type, cu);
14967
14968 /* Copy the saved-up fields into the field vector. */
14969 for (int i = 0; i < nfields; ++i)
14970 {
14971 struct nextfield &field
14972 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14973 : fip->fields[i - fip->baseclasses.size ()]);
14974
14975 type->field (i) = field.field;
14976 switch (field.accessibility)
14977 {
14978 case DW_ACCESS_private:
14979 if (cu->language != language_ada)
14980 SET_TYPE_FIELD_PRIVATE (type, i);
14981 break;
14982
14983 case DW_ACCESS_protected:
14984 if (cu->language != language_ada)
14985 SET_TYPE_FIELD_PROTECTED (type, i);
14986 break;
14987
14988 case DW_ACCESS_public:
14989 break;
14990
14991 default:
14992 /* Unknown accessibility. Complain and treat it as public. */
14993 {
14994 complaint (_("unsupported accessibility %d"),
14995 field.accessibility);
14996 }
14997 break;
14998 }
14999 if (i < fip->baseclasses.size ())
15000 {
15001 switch (field.virtuality)
15002 {
15003 case DW_VIRTUALITY_virtual:
15004 case DW_VIRTUALITY_pure_virtual:
15005 if (cu->language == language_ada)
15006 error (_("unexpected virtuality in component of Ada type"));
15007 SET_TYPE_FIELD_VIRTUAL (type, i);
15008 break;
15009 }
15010 }
15011 }
15012 }
15013
15014 /* Return true if this member function is a constructor, false
15015 otherwise. */
15016
15017 static int
15018 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15019 {
15020 const char *fieldname;
15021 const char *type_name;
15022 int len;
15023
15024 if (die->parent == NULL)
15025 return 0;
15026
15027 if (die->parent->tag != DW_TAG_structure_type
15028 && die->parent->tag != DW_TAG_union_type
15029 && die->parent->tag != DW_TAG_class_type)
15030 return 0;
15031
15032 fieldname = dwarf2_name (die, cu);
15033 type_name = dwarf2_name (die->parent, cu);
15034 if (fieldname == NULL || type_name == NULL)
15035 return 0;
15036
15037 len = strlen (fieldname);
15038 return (strncmp (fieldname, type_name, len) == 0
15039 && (type_name[len] == '\0' || type_name[len] == '<'));
15040 }
15041
15042 /* Check if the given VALUE is a recognized enum
15043 dwarf_defaulted_attribute constant according to DWARF5 spec,
15044 Table 7.24. */
15045
15046 static bool
15047 is_valid_DW_AT_defaulted (ULONGEST value)
15048 {
15049 switch (value)
15050 {
15051 case DW_DEFAULTED_no:
15052 case DW_DEFAULTED_in_class:
15053 case DW_DEFAULTED_out_of_class:
15054 return true;
15055 }
15056
15057 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15058 return false;
15059 }
15060
15061 /* Add a member function to the proper fieldlist. */
15062
15063 static void
15064 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15065 struct type *type, struct dwarf2_cu *cu)
15066 {
15067 struct objfile *objfile = cu->per_objfile->objfile;
15068 struct attribute *attr;
15069 int i;
15070 struct fnfieldlist *flp = nullptr;
15071 struct fn_field *fnp;
15072 const char *fieldname;
15073 struct type *this_type;
15074 enum dwarf_access_attribute accessibility;
15075
15076 if (cu->language == language_ada)
15077 error (_("unexpected member function in Ada type"));
15078
15079 /* Get name of member function. */
15080 fieldname = dwarf2_name (die, cu);
15081 if (fieldname == NULL)
15082 return;
15083
15084 /* Look up member function name in fieldlist. */
15085 for (i = 0; i < fip->fnfieldlists.size (); i++)
15086 {
15087 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15088 {
15089 flp = &fip->fnfieldlists[i];
15090 break;
15091 }
15092 }
15093
15094 /* Create a new fnfieldlist if necessary. */
15095 if (flp == nullptr)
15096 {
15097 fip->fnfieldlists.emplace_back ();
15098 flp = &fip->fnfieldlists.back ();
15099 flp->name = fieldname;
15100 i = fip->fnfieldlists.size () - 1;
15101 }
15102
15103 /* Create a new member function field and add it to the vector of
15104 fnfieldlists. */
15105 flp->fnfields.emplace_back ();
15106 fnp = &flp->fnfields.back ();
15107
15108 /* Delay processing of the physname until later. */
15109 if (cu->language == language_cplus)
15110 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15111 die, cu);
15112 else
15113 {
15114 const char *physname = dwarf2_physname (fieldname, die, cu);
15115 fnp->physname = physname ? physname : "";
15116 }
15117
15118 fnp->type = alloc_type (objfile);
15119 this_type = read_type_die (die, cu);
15120 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15121 {
15122 int nparams = this_type->num_fields ();
15123
15124 /* TYPE is the domain of this method, and THIS_TYPE is the type
15125 of the method itself (TYPE_CODE_METHOD). */
15126 smash_to_method_type (fnp->type, type,
15127 TYPE_TARGET_TYPE (this_type),
15128 this_type->fields (),
15129 this_type->num_fields (),
15130 TYPE_VARARGS (this_type));
15131
15132 /* Handle static member functions.
15133 Dwarf2 has no clean way to discern C++ static and non-static
15134 member functions. G++ helps GDB by marking the first
15135 parameter for non-static member functions (which is the this
15136 pointer) as artificial. We obtain this information from
15137 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15138 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15139 fnp->voffset = VOFFSET_STATIC;
15140 }
15141 else
15142 complaint (_("member function type missing for '%s'"),
15143 dwarf2_full_name (fieldname, die, cu));
15144
15145 /* Get fcontext from DW_AT_containing_type if present. */
15146 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15147 fnp->fcontext = die_containing_type (die, cu);
15148
15149 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15150 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15151
15152 /* Get accessibility. */
15153 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15154 if (attr != nullptr)
15155 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15156 else
15157 accessibility = dwarf2_default_access_attribute (die, cu);
15158 switch (accessibility)
15159 {
15160 case DW_ACCESS_private:
15161 fnp->is_private = 1;
15162 break;
15163 case DW_ACCESS_protected:
15164 fnp->is_protected = 1;
15165 break;
15166 }
15167
15168 /* Check for artificial methods. */
15169 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15170 if (attr && DW_UNSND (attr) != 0)
15171 fnp->is_artificial = 1;
15172
15173 /* Check for defaulted methods. */
15174 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15175 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15176 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15177
15178 /* Check for deleted methods. */
15179 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15180 if (attr != nullptr && DW_UNSND (attr) != 0)
15181 fnp->is_deleted = 1;
15182
15183 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15184
15185 /* Get index in virtual function table if it is a virtual member
15186 function. For older versions of GCC, this is an offset in the
15187 appropriate virtual table, as specified by DW_AT_containing_type.
15188 For everyone else, it is an expression to be evaluated relative
15189 to the object address. */
15190
15191 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15192 if (attr != nullptr)
15193 {
15194 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15195 {
15196 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15197 {
15198 /* Old-style GCC. */
15199 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15200 }
15201 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15202 || (DW_BLOCK (attr)->size > 1
15203 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15204 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15205 {
15206 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15207 if ((fnp->voffset % cu->header.addr_size) != 0)
15208 dwarf2_complex_location_expr_complaint ();
15209 else
15210 fnp->voffset /= cu->header.addr_size;
15211 fnp->voffset += 2;
15212 }
15213 else
15214 dwarf2_complex_location_expr_complaint ();
15215
15216 if (!fnp->fcontext)
15217 {
15218 /* If there is no `this' field and no DW_AT_containing_type,
15219 we cannot actually find a base class context for the
15220 vtable! */
15221 if (this_type->num_fields () == 0
15222 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15223 {
15224 complaint (_("cannot determine context for virtual member "
15225 "function \"%s\" (offset %s)"),
15226 fieldname, sect_offset_str (die->sect_off));
15227 }
15228 else
15229 {
15230 fnp->fcontext
15231 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15232 }
15233 }
15234 }
15235 else if (attr->form_is_section_offset ())
15236 {
15237 dwarf2_complex_location_expr_complaint ();
15238 }
15239 else
15240 {
15241 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15242 fieldname);
15243 }
15244 }
15245 else
15246 {
15247 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15248 if (attr && DW_UNSND (attr))
15249 {
15250 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15251 complaint (_("Member function \"%s\" (offset %s) is virtual "
15252 "but the vtable offset is not specified"),
15253 fieldname, sect_offset_str (die->sect_off));
15254 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15255 TYPE_CPLUS_DYNAMIC (type) = 1;
15256 }
15257 }
15258 }
15259
15260 /* Create the vector of member function fields, and attach it to the type. */
15261
15262 static void
15263 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15264 struct dwarf2_cu *cu)
15265 {
15266 if (cu->language == language_ada)
15267 error (_("unexpected member functions in Ada type"));
15268
15269 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15270 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15271 TYPE_ALLOC (type,
15272 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15273
15274 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15275 {
15276 struct fnfieldlist &nf = fip->fnfieldlists[i];
15277 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15278
15279 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15280 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15281 fn_flp->fn_fields = (struct fn_field *)
15282 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15283
15284 for (int k = 0; k < nf.fnfields.size (); ++k)
15285 fn_flp->fn_fields[k] = nf.fnfields[k];
15286 }
15287
15288 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15289 }
15290
15291 /* Returns non-zero if NAME is the name of a vtable member in CU's
15292 language, zero otherwise. */
15293 static int
15294 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15295 {
15296 static const char vptr[] = "_vptr";
15297
15298 /* Look for the C++ form of the vtable. */
15299 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15300 return 1;
15301
15302 return 0;
15303 }
15304
15305 /* GCC outputs unnamed structures that are really pointers to member
15306 functions, with the ABI-specified layout. If TYPE describes
15307 such a structure, smash it into a member function type.
15308
15309 GCC shouldn't do this; it should just output pointer to member DIEs.
15310 This is GCC PR debug/28767. */
15311
15312 static void
15313 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15314 {
15315 struct type *pfn_type, *self_type, *new_type;
15316
15317 /* Check for a structure with no name and two children. */
15318 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15319 return;
15320
15321 /* Check for __pfn and __delta members. */
15322 if (TYPE_FIELD_NAME (type, 0) == NULL
15323 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15324 || TYPE_FIELD_NAME (type, 1) == NULL
15325 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15326 return;
15327
15328 /* Find the type of the method. */
15329 pfn_type = TYPE_FIELD_TYPE (type, 0);
15330 if (pfn_type == NULL
15331 || pfn_type->code () != TYPE_CODE_PTR
15332 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15333 return;
15334
15335 /* Look for the "this" argument. */
15336 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15337 if (pfn_type->num_fields () == 0
15338 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15339 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15340 return;
15341
15342 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15343 new_type = alloc_type (objfile);
15344 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15345 pfn_type->fields (), pfn_type->num_fields (),
15346 TYPE_VARARGS (pfn_type));
15347 smash_to_methodptr_type (type, new_type);
15348 }
15349
15350 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15351 appropriate error checking and issuing complaints if there is a
15352 problem. */
15353
15354 static ULONGEST
15355 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15356 {
15357 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15358
15359 if (attr == nullptr)
15360 return 0;
15361
15362 if (!attr->form_is_constant ())
15363 {
15364 complaint (_("DW_AT_alignment must have constant form"
15365 " - DIE at %s [in module %s]"),
15366 sect_offset_str (die->sect_off),
15367 objfile_name (cu->per_objfile->objfile));
15368 return 0;
15369 }
15370
15371 ULONGEST align;
15372 if (attr->form == DW_FORM_sdata)
15373 {
15374 LONGEST val = DW_SND (attr);
15375 if (val < 0)
15376 {
15377 complaint (_("DW_AT_alignment value must not be negative"
15378 " - DIE at %s [in module %s]"),
15379 sect_offset_str (die->sect_off),
15380 objfile_name (cu->per_objfile->objfile));
15381 return 0;
15382 }
15383 align = val;
15384 }
15385 else
15386 align = DW_UNSND (attr);
15387
15388 if (align == 0)
15389 {
15390 complaint (_("DW_AT_alignment value must not be zero"
15391 " - DIE at %s [in module %s]"),
15392 sect_offset_str (die->sect_off),
15393 objfile_name (cu->per_objfile->objfile));
15394 return 0;
15395 }
15396 if ((align & (align - 1)) != 0)
15397 {
15398 complaint (_("DW_AT_alignment value must be a power of 2"
15399 " - DIE at %s [in module %s]"),
15400 sect_offset_str (die->sect_off),
15401 objfile_name (cu->per_objfile->objfile));
15402 return 0;
15403 }
15404
15405 return align;
15406 }
15407
15408 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15409 the alignment for TYPE. */
15410
15411 static void
15412 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15413 struct type *type)
15414 {
15415 if (!set_type_align (type, get_alignment (cu, die)))
15416 complaint (_("DW_AT_alignment value too large"
15417 " - DIE at %s [in module %s]"),
15418 sect_offset_str (die->sect_off),
15419 objfile_name (cu->per_objfile->objfile));
15420 }
15421
15422 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15423 constant for a type, according to DWARF5 spec, Table 5.5. */
15424
15425 static bool
15426 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15427 {
15428 switch (value)
15429 {
15430 case DW_CC_normal:
15431 case DW_CC_pass_by_reference:
15432 case DW_CC_pass_by_value:
15433 return true;
15434
15435 default:
15436 complaint (_("unrecognized DW_AT_calling_convention value "
15437 "(%s) for a type"), pulongest (value));
15438 return false;
15439 }
15440 }
15441
15442 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15443 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15444 also according to GNU-specific values (see include/dwarf2.h). */
15445
15446 static bool
15447 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15448 {
15449 switch (value)
15450 {
15451 case DW_CC_normal:
15452 case DW_CC_program:
15453 case DW_CC_nocall:
15454 return true;
15455
15456 case DW_CC_GNU_renesas_sh:
15457 case DW_CC_GNU_borland_fastcall_i386:
15458 case DW_CC_GDB_IBM_OpenCL:
15459 return true;
15460
15461 default:
15462 complaint (_("unrecognized DW_AT_calling_convention value "
15463 "(%s) for a subroutine"), pulongest (value));
15464 return false;
15465 }
15466 }
15467
15468 /* Called when we find the DIE that starts a structure or union scope
15469 (definition) to create a type for the structure or union. Fill in
15470 the type's name and general properties; the members will not be
15471 processed until process_structure_scope. A symbol table entry for
15472 the type will also not be done until process_structure_scope (assuming
15473 the type has a name).
15474
15475 NOTE: we need to call these functions regardless of whether or not the
15476 DIE has a DW_AT_name attribute, since it might be an anonymous
15477 structure or union. This gets the type entered into our set of
15478 user defined types. */
15479
15480 static struct type *
15481 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15482 {
15483 struct objfile *objfile = cu->per_objfile->objfile;
15484 struct type *type;
15485 struct attribute *attr;
15486 const char *name;
15487
15488 /* If the definition of this type lives in .debug_types, read that type.
15489 Don't follow DW_AT_specification though, that will take us back up
15490 the chain and we want to go down. */
15491 attr = die->attr (DW_AT_signature);
15492 if (attr != nullptr)
15493 {
15494 type = get_DW_AT_signature_type (die, attr, cu);
15495
15496 /* The type's CU may not be the same as CU.
15497 Ensure TYPE is recorded with CU in die_type_hash. */
15498 return set_die_type (die, type, cu);
15499 }
15500
15501 type = alloc_type (objfile);
15502 INIT_CPLUS_SPECIFIC (type);
15503
15504 name = dwarf2_name (die, cu);
15505 if (name != NULL)
15506 {
15507 if (cu->language == language_cplus
15508 || cu->language == language_d
15509 || cu->language == language_rust)
15510 {
15511 const char *full_name = dwarf2_full_name (name, die, cu);
15512
15513 /* dwarf2_full_name might have already finished building the DIE's
15514 type. If so, there is no need to continue. */
15515 if (get_die_type (die, cu) != NULL)
15516 return get_die_type (die, cu);
15517
15518 type->set_name (full_name);
15519 }
15520 else
15521 {
15522 /* The name is already allocated along with this objfile, so
15523 we don't need to duplicate it for the type. */
15524 type->set_name (name);
15525 }
15526 }
15527
15528 if (die->tag == DW_TAG_structure_type)
15529 {
15530 type->set_code (TYPE_CODE_STRUCT);
15531 }
15532 else if (die->tag == DW_TAG_union_type)
15533 {
15534 type->set_code (TYPE_CODE_UNION);
15535 }
15536 else
15537 {
15538 type->set_code (TYPE_CODE_STRUCT);
15539 }
15540
15541 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15542 TYPE_DECLARED_CLASS (type) = 1;
15543
15544 /* Store the calling convention in the type if it's available in
15545 the die. Otherwise the calling convention remains set to
15546 the default value DW_CC_normal. */
15547 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15548 if (attr != nullptr
15549 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15550 {
15551 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15552 TYPE_CPLUS_CALLING_CONVENTION (type)
15553 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15554 }
15555
15556 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15557 if (attr != nullptr)
15558 {
15559 if (attr->form_is_constant ())
15560 TYPE_LENGTH (type) = DW_UNSND (attr);
15561 else
15562 {
15563 struct dynamic_prop prop;
15564 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15565 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15566 TYPE_LENGTH (type) = 0;
15567 }
15568 }
15569 else
15570 {
15571 TYPE_LENGTH (type) = 0;
15572 }
15573
15574 maybe_set_alignment (cu, die, type);
15575
15576 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15577 {
15578 /* ICC<14 does not output the required DW_AT_declaration on
15579 incomplete types, but gives them a size of zero. */
15580 TYPE_STUB (type) = 1;
15581 }
15582 else
15583 TYPE_STUB_SUPPORTED (type) = 1;
15584
15585 if (die_is_declaration (die, cu))
15586 TYPE_STUB (type) = 1;
15587 else if (attr == NULL && die->child == NULL
15588 && producer_is_realview (cu->producer))
15589 /* RealView does not output the required DW_AT_declaration
15590 on incomplete types. */
15591 TYPE_STUB (type) = 1;
15592
15593 /* We need to add the type field to the die immediately so we don't
15594 infinitely recurse when dealing with pointers to the structure
15595 type within the structure itself. */
15596 set_die_type (die, type, cu);
15597
15598 /* set_die_type should be already done. */
15599 set_descriptive_type (type, die, cu);
15600
15601 return type;
15602 }
15603
15604 static void handle_struct_member_die
15605 (struct die_info *child_die,
15606 struct type *type,
15607 struct field_info *fi,
15608 std::vector<struct symbol *> *template_args,
15609 struct dwarf2_cu *cu);
15610
15611 /* A helper for handle_struct_member_die that handles
15612 DW_TAG_variant_part. */
15613
15614 static void
15615 handle_variant_part (struct die_info *die, struct type *type,
15616 struct field_info *fi,
15617 std::vector<struct symbol *> *template_args,
15618 struct dwarf2_cu *cu)
15619 {
15620 variant_part_builder *new_part;
15621 if (fi->current_variant_part == nullptr)
15622 {
15623 fi->variant_parts.emplace_back ();
15624 new_part = &fi->variant_parts.back ();
15625 }
15626 else if (!fi->current_variant_part->processing_variant)
15627 {
15628 complaint (_("nested DW_TAG_variant_part seen "
15629 "- DIE at %s [in module %s]"),
15630 sect_offset_str (die->sect_off),
15631 objfile_name (cu->per_objfile->objfile));
15632 return;
15633 }
15634 else
15635 {
15636 variant_field &current = fi->current_variant_part->variants.back ();
15637 current.variant_parts.emplace_back ();
15638 new_part = &current.variant_parts.back ();
15639 }
15640
15641 /* When we recurse, we want callees to add to this new variant
15642 part. */
15643 scoped_restore save_current_variant_part
15644 = make_scoped_restore (&fi->current_variant_part, new_part);
15645
15646 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15647 if (discr == NULL)
15648 {
15649 /* It's a univariant form, an extension we support. */
15650 }
15651 else if (discr->form_is_ref ())
15652 {
15653 struct dwarf2_cu *target_cu = cu;
15654 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15655
15656 new_part->discriminant_offset = target_die->sect_off;
15657 }
15658 else
15659 {
15660 complaint (_("DW_AT_discr does not have DIE reference form"
15661 " - DIE at %s [in module %s]"),
15662 sect_offset_str (die->sect_off),
15663 objfile_name (cu->per_objfile->objfile));
15664 }
15665
15666 for (die_info *child_die = die->child;
15667 child_die != NULL;
15668 child_die = child_die->sibling)
15669 handle_struct_member_die (child_die, type, fi, template_args, cu);
15670 }
15671
15672 /* A helper for handle_struct_member_die that handles
15673 DW_TAG_variant. */
15674
15675 static void
15676 handle_variant (struct die_info *die, struct type *type,
15677 struct field_info *fi,
15678 std::vector<struct symbol *> *template_args,
15679 struct dwarf2_cu *cu)
15680 {
15681 if (fi->current_variant_part == nullptr)
15682 {
15683 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15684 "- DIE at %s [in module %s]"),
15685 sect_offset_str (die->sect_off),
15686 objfile_name (cu->per_objfile->objfile));
15687 return;
15688 }
15689 if (fi->current_variant_part->processing_variant)
15690 {
15691 complaint (_("nested DW_TAG_variant seen "
15692 "- DIE at %s [in module %s]"),
15693 sect_offset_str (die->sect_off),
15694 objfile_name (cu->per_objfile->objfile));
15695 return;
15696 }
15697
15698 scoped_restore save_processing_variant
15699 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15700 true);
15701
15702 fi->current_variant_part->variants.emplace_back ();
15703 variant_field &variant = fi->current_variant_part->variants.back ();
15704 variant.first_field = fi->fields.size ();
15705
15706 /* In a variant we want to get the discriminant and also add a
15707 field for our sole member child. */
15708 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15709 if (discr == nullptr)
15710 {
15711 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15712 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15713 variant.default_branch = true;
15714 else
15715 variant.discr_list_data = DW_BLOCK (discr);
15716 }
15717 else
15718 variant.discriminant_value = DW_UNSND (discr);
15719
15720 for (die_info *variant_child = die->child;
15721 variant_child != NULL;
15722 variant_child = variant_child->sibling)
15723 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15724
15725 variant.last_field = fi->fields.size ();
15726 }
15727
15728 /* A helper for process_structure_scope that handles a single member
15729 DIE. */
15730
15731 static void
15732 handle_struct_member_die (struct die_info *child_die, struct type *type,
15733 struct field_info *fi,
15734 std::vector<struct symbol *> *template_args,
15735 struct dwarf2_cu *cu)
15736 {
15737 if (child_die->tag == DW_TAG_member
15738 || child_die->tag == DW_TAG_variable)
15739 {
15740 /* NOTE: carlton/2002-11-05: A C++ static data member
15741 should be a DW_TAG_member that is a declaration, but
15742 all versions of G++ as of this writing (so through at
15743 least 3.2.1) incorrectly generate DW_TAG_variable
15744 tags for them instead. */
15745 dwarf2_add_field (fi, child_die, cu);
15746 }
15747 else if (child_die->tag == DW_TAG_subprogram)
15748 {
15749 /* Rust doesn't have member functions in the C++ sense.
15750 However, it does emit ordinary functions as children
15751 of a struct DIE. */
15752 if (cu->language == language_rust)
15753 read_func_scope (child_die, cu);
15754 else
15755 {
15756 /* C++ member function. */
15757 dwarf2_add_member_fn (fi, child_die, type, cu);
15758 }
15759 }
15760 else if (child_die->tag == DW_TAG_inheritance)
15761 {
15762 /* C++ base class field. */
15763 dwarf2_add_field (fi, child_die, cu);
15764 }
15765 else if (type_can_define_types (child_die))
15766 dwarf2_add_type_defn (fi, child_die, cu);
15767 else if (child_die->tag == DW_TAG_template_type_param
15768 || child_die->tag == DW_TAG_template_value_param)
15769 {
15770 struct symbol *arg = new_symbol (child_die, NULL, cu);
15771
15772 if (arg != NULL)
15773 template_args->push_back (arg);
15774 }
15775 else if (child_die->tag == DW_TAG_variant_part)
15776 handle_variant_part (child_die, type, fi, template_args, cu);
15777 else if (child_die->tag == DW_TAG_variant)
15778 handle_variant (child_die, type, fi, template_args, cu);
15779 }
15780
15781 /* Finish creating a structure or union type, including filling in
15782 its members and creating a symbol for it. */
15783
15784 static void
15785 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15786 {
15787 struct objfile *objfile = cu->per_objfile->objfile;
15788 struct die_info *child_die;
15789 struct type *type;
15790
15791 type = get_die_type (die, cu);
15792 if (type == NULL)
15793 type = read_structure_type (die, cu);
15794
15795 bool has_template_parameters = false;
15796 if (die->child != NULL && ! die_is_declaration (die, cu))
15797 {
15798 struct field_info fi;
15799 std::vector<struct symbol *> template_args;
15800
15801 child_die = die->child;
15802
15803 while (child_die && child_die->tag)
15804 {
15805 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15806 child_die = child_die->sibling;
15807 }
15808
15809 /* Attach template arguments to type. */
15810 if (!template_args.empty ())
15811 {
15812 has_template_parameters = true;
15813 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15814 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15815 TYPE_TEMPLATE_ARGUMENTS (type)
15816 = XOBNEWVEC (&objfile->objfile_obstack,
15817 struct symbol *,
15818 TYPE_N_TEMPLATE_ARGUMENTS (type));
15819 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15820 template_args.data (),
15821 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15822 * sizeof (struct symbol *)));
15823 }
15824
15825 /* Attach fields and member functions to the type. */
15826 if (fi.nfields () > 0)
15827 dwarf2_attach_fields_to_type (&fi, type, cu);
15828 if (!fi.fnfieldlists.empty ())
15829 {
15830 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15831
15832 /* Get the type which refers to the base class (possibly this
15833 class itself) which contains the vtable pointer for the current
15834 class from the DW_AT_containing_type attribute. This use of
15835 DW_AT_containing_type is a GNU extension. */
15836
15837 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15838 {
15839 struct type *t = die_containing_type (die, cu);
15840
15841 set_type_vptr_basetype (type, t);
15842 if (type == t)
15843 {
15844 int i;
15845
15846 /* Our own class provides vtbl ptr. */
15847 for (i = t->num_fields () - 1;
15848 i >= TYPE_N_BASECLASSES (t);
15849 --i)
15850 {
15851 const char *fieldname = TYPE_FIELD_NAME (t, i);
15852
15853 if (is_vtable_name (fieldname, cu))
15854 {
15855 set_type_vptr_fieldno (type, i);
15856 break;
15857 }
15858 }
15859
15860 /* Complain if virtual function table field not found. */
15861 if (i < TYPE_N_BASECLASSES (t))
15862 complaint (_("virtual function table pointer "
15863 "not found when defining class '%s'"),
15864 type->name () ? type->name () : "");
15865 }
15866 else
15867 {
15868 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15869 }
15870 }
15871 else if (cu->producer
15872 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15873 {
15874 /* The IBM XLC compiler does not provide direct indication
15875 of the containing type, but the vtable pointer is
15876 always named __vfp. */
15877
15878 int i;
15879
15880 for (i = type->num_fields () - 1;
15881 i >= TYPE_N_BASECLASSES (type);
15882 --i)
15883 {
15884 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15885 {
15886 set_type_vptr_fieldno (type, i);
15887 set_type_vptr_basetype (type, type);
15888 break;
15889 }
15890 }
15891 }
15892 }
15893
15894 /* Copy fi.typedef_field_list linked list elements content into the
15895 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15896 if (!fi.typedef_field_list.empty ())
15897 {
15898 int count = fi.typedef_field_list.size ();
15899
15900 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15901 TYPE_TYPEDEF_FIELD_ARRAY (type)
15902 = ((struct decl_field *)
15903 TYPE_ALLOC (type,
15904 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15905 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15906
15907 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15908 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15909 }
15910
15911 /* Copy fi.nested_types_list linked list elements content into the
15912 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15913 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15914 {
15915 int count = fi.nested_types_list.size ();
15916
15917 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15918 TYPE_NESTED_TYPES_ARRAY (type)
15919 = ((struct decl_field *)
15920 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15921 TYPE_NESTED_TYPES_COUNT (type) = count;
15922
15923 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15924 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15925 }
15926 }
15927
15928 quirk_gcc_member_function_pointer (type, objfile);
15929 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15930 cu->rust_unions.push_back (type);
15931
15932 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15933 snapshots) has been known to create a die giving a declaration
15934 for a class that has, as a child, a die giving a definition for a
15935 nested class. So we have to process our children even if the
15936 current die is a declaration. Normally, of course, a declaration
15937 won't have any children at all. */
15938
15939 child_die = die->child;
15940
15941 while (child_die != NULL && child_die->tag)
15942 {
15943 if (child_die->tag == DW_TAG_member
15944 || child_die->tag == DW_TAG_variable
15945 || child_die->tag == DW_TAG_inheritance
15946 || child_die->tag == DW_TAG_template_value_param
15947 || child_die->tag == DW_TAG_template_type_param)
15948 {
15949 /* Do nothing. */
15950 }
15951 else
15952 process_die (child_die, cu);
15953
15954 child_die = child_die->sibling;
15955 }
15956
15957 /* Do not consider external references. According to the DWARF standard,
15958 these DIEs are identified by the fact that they have no byte_size
15959 attribute, and a declaration attribute. */
15960 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15961 || !die_is_declaration (die, cu)
15962 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15963 {
15964 struct symbol *sym = new_symbol (die, type, cu);
15965
15966 if (has_template_parameters)
15967 {
15968 struct symtab *symtab;
15969 if (sym != nullptr)
15970 symtab = symbol_symtab (sym);
15971 else if (cu->line_header != nullptr)
15972 {
15973 /* Any related symtab will do. */
15974 symtab
15975 = cu->line_header->file_names ()[0].symtab;
15976 }
15977 else
15978 {
15979 symtab = nullptr;
15980 complaint (_("could not find suitable "
15981 "symtab for template parameter"
15982 " - DIE at %s [in module %s]"),
15983 sect_offset_str (die->sect_off),
15984 objfile_name (objfile));
15985 }
15986
15987 if (symtab != nullptr)
15988 {
15989 /* Make sure that the symtab is set on the new symbols.
15990 Even though they don't appear in this symtab directly,
15991 other parts of gdb assume that symbols do, and this is
15992 reasonably true. */
15993 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15994 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15995 }
15996 }
15997 }
15998 }
15999
16000 /* Assuming DIE is an enumeration type, and TYPE is its associated
16001 type, update TYPE using some information only available in DIE's
16002 children. In particular, the fields are computed. */
16003
16004 static void
16005 update_enumeration_type_from_children (struct die_info *die,
16006 struct type *type,
16007 struct dwarf2_cu *cu)
16008 {
16009 struct die_info *child_die;
16010 int unsigned_enum = 1;
16011 int flag_enum = 1;
16012
16013 auto_obstack obstack;
16014 std::vector<struct field> fields;
16015
16016 for (child_die = die->child;
16017 child_die != NULL && child_die->tag;
16018 child_die = child_die->sibling)
16019 {
16020 struct attribute *attr;
16021 LONGEST value;
16022 const gdb_byte *bytes;
16023 struct dwarf2_locexpr_baton *baton;
16024 const char *name;
16025
16026 if (child_die->tag != DW_TAG_enumerator)
16027 continue;
16028
16029 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16030 if (attr == NULL)
16031 continue;
16032
16033 name = dwarf2_name (child_die, cu);
16034 if (name == NULL)
16035 name = "<anonymous enumerator>";
16036
16037 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16038 &value, &bytes, &baton);
16039 if (value < 0)
16040 {
16041 unsigned_enum = 0;
16042 flag_enum = 0;
16043 }
16044 else
16045 {
16046 if (count_one_bits_ll (value) >= 2)
16047 flag_enum = 0;
16048 }
16049
16050 fields.emplace_back ();
16051 struct field &field = fields.back ();
16052 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16053 SET_FIELD_ENUMVAL (field, value);
16054 }
16055
16056 if (!fields.empty ())
16057 {
16058 type->set_num_fields (fields.size ());
16059 type->set_fields
16060 ((struct field *)
16061 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16062 memcpy (type->fields (), fields.data (),
16063 sizeof (struct field) * fields.size ());
16064 }
16065
16066 if (unsigned_enum)
16067 TYPE_UNSIGNED (type) = 1;
16068 if (flag_enum)
16069 TYPE_FLAG_ENUM (type) = 1;
16070 }
16071
16072 /* Given a DW_AT_enumeration_type die, set its type. We do not
16073 complete the type's fields yet, or create any symbols. */
16074
16075 static struct type *
16076 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16077 {
16078 struct objfile *objfile = cu->per_objfile->objfile;
16079 struct type *type;
16080 struct attribute *attr;
16081 const char *name;
16082
16083 /* If the definition of this type lives in .debug_types, read that type.
16084 Don't follow DW_AT_specification though, that will take us back up
16085 the chain and we want to go down. */
16086 attr = die->attr (DW_AT_signature);
16087 if (attr != nullptr)
16088 {
16089 type = get_DW_AT_signature_type (die, attr, cu);
16090
16091 /* The type's CU may not be the same as CU.
16092 Ensure TYPE is recorded with CU in die_type_hash. */
16093 return set_die_type (die, type, cu);
16094 }
16095
16096 type = alloc_type (objfile);
16097
16098 type->set_code (TYPE_CODE_ENUM);
16099 name = dwarf2_full_name (NULL, die, cu);
16100 if (name != NULL)
16101 type->set_name (name);
16102
16103 attr = dwarf2_attr (die, DW_AT_type, cu);
16104 if (attr != NULL)
16105 {
16106 struct type *underlying_type = die_type (die, cu);
16107
16108 TYPE_TARGET_TYPE (type) = underlying_type;
16109 }
16110
16111 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16112 if (attr != nullptr)
16113 {
16114 TYPE_LENGTH (type) = DW_UNSND (attr);
16115 }
16116 else
16117 {
16118 TYPE_LENGTH (type) = 0;
16119 }
16120
16121 maybe_set_alignment (cu, die, type);
16122
16123 /* The enumeration DIE can be incomplete. In Ada, any type can be
16124 declared as private in the package spec, and then defined only
16125 inside the package body. Such types are known as Taft Amendment
16126 Types. When another package uses such a type, an incomplete DIE
16127 may be generated by the compiler. */
16128 if (die_is_declaration (die, cu))
16129 TYPE_STUB (type) = 1;
16130
16131 /* If this type has an underlying type that is not a stub, then we
16132 may use its attributes. We always use the "unsigned" attribute
16133 in this situation, because ordinarily we guess whether the type
16134 is unsigned -- but the guess can be wrong and the underlying type
16135 can tell us the reality. However, we defer to a local size
16136 attribute if one exists, because this lets the compiler override
16137 the underlying type if needed. */
16138 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16139 {
16140 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16141 underlying_type = check_typedef (underlying_type);
16142 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16143 if (TYPE_LENGTH (type) == 0)
16144 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16145 if (TYPE_RAW_ALIGN (type) == 0
16146 && TYPE_RAW_ALIGN (underlying_type) != 0)
16147 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16148 }
16149
16150 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16151
16152 set_die_type (die, type, cu);
16153
16154 /* Finish the creation of this type by using the enum's children.
16155 Note that, as usual, this must come after set_die_type to avoid
16156 infinite recursion when trying to compute the names of the
16157 enumerators. */
16158 update_enumeration_type_from_children (die, type, cu);
16159
16160 return type;
16161 }
16162
16163 /* Given a pointer to a die which begins an enumeration, process all
16164 the dies that define the members of the enumeration, and create the
16165 symbol for the enumeration type.
16166
16167 NOTE: We reverse the order of the element list. */
16168
16169 static void
16170 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16171 {
16172 struct type *this_type;
16173
16174 this_type = get_die_type (die, cu);
16175 if (this_type == NULL)
16176 this_type = read_enumeration_type (die, cu);
16177
16178 if (die->child != NULL)
16179 {
16180 struct die_info *child_die;
16181 const char *name;
16182
16183 child_die = die->child;
16184 while (child_die && child_die->tag)
16185 {
16186 if (child_die->tag != DW_TAG_enumerator)
16187 {
16188 process_die (child_die, cu);
16189 }
16190 else
16191 {
16192 name = dwarf2_name (child_die, cu);
16193 if (name)
16194 new_symbol (child_die, this_type, cu);
16195 }
16196
16197 child_die = child_die->sibling;
16198 }
16199 }
16200
16201 /* If we are reading an enum from a .debug_types unit, and the enum
16202 is a declaration, and the enum is not the signatured type in the
16203 unit, then we do not want to add a symbol for it. Adding a
16204 symbol would in some cases obscure the true definition of the
16205 enum, giving users an incomplete type when the definition is
16206 actually available. Note that we do not want to do this for all
16207 enums which are just declarations, because C++0x allows forward
16208 enum declarations. */
16209 if (cu->per_cu->is_debug_types
16210 && die_is_declaration (die, cu))
16211 {
16212 struct signatured_type *sig_type;
16213
16214 sig_type = (struct signatured_type *) cu->per_cu;
16215 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16216 if (sig_type->type_offset_in_section != die->sect_off)
16217 return;
16218 }
16219
16220 new_symbol (die, this_type, cu);
16221 }
16222
16223 /* Extract all information from a DW_TAG_array_type DIE and put it in
16224 the DIE's type field. For now, this only handles one dimensional
16225 arrays. */
16226
16227 static struct type *
16228 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16229 {
16230 struct objfile *objfile = cu->per_objfile->objfile;
16231 struct die_info *child_die;
16232 struct type *type;
16233 struct type *element_type, *range_type, *index_type;
16234 struct attribute *attr;
16235 const char *name;
16236 struct dynamic_prop *byte_stride_prop = NULL;
16237 unsigned int bit_stride = 0;
16238
16239 element_type = die_type (die, cu);
16240
16241 /* The die_type call above may have already set the type for this DIE. */
16242 type = get_die_type (die, cu);
16243 if (type)
16244 return type;
16245
16246 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16247 if (attr != NULL)
16248 {
16249 int stride_ok;
16250 struct type *prop_type = cu->addr_sized_int_type (false);
16251
16252 byte_stride_prop
16253 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16254 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16255 prop_type);
16256 if (!stride_ok)
16257 {
16258 complaint (_("unable to read array DW_AT_byte_stride "
16259 " - DIE at %s [in module %s]"),
16260 sect_offset_str (die->sect_off),
16261 objfile_name (cu->per_objfile->objfile));
16262 /* Ignore this attribute. We will likely not be able to print
16263 arrays of this type correctly, but there is little we can do
16264 to help if we cannot read the attribute's value. */
16265 byte_stride_prop = NULL;
16266 }
16267 }
16268
16269 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16270 if (attr != NULL)
16271 bit_stride = DW_UNSND (attr);
16272
16273 /* Irix 6.2 native cc creates array types without children for
16274 arrays with unspecified length. */
16275 if (die->child == NULL)
16276 {
16277 index_type = objfile_type (objfile)->builtin_int;
16278 range_type = create_static_range_type (NULL, index_type, 0, -1);
16279 type = create_array_type_with_stride (NULL, element_type, range_type,
16280 byte_stride_prop, bit_stride);
16281 return set_die_type (die, type, cu);
16282 }
16283
16284 std::vector<struct type *> range_types;
16285 child_die = die->child;
16286 while (child_die && child_die->tag)
16287 {
16288 if (child_die->tag == DW_TAG_subrange_type)
16289 {
16290 struct type *child_type = read_type_die (child_die, cu);
16291
16292 if (child_type != NULL)
16293 {
16294 /* The range type was succesfully read. Save it for the
16295 array type creation. */
16296 range_types.push_back (child_type);
16297 }
16298 }
16299 child_die = child_die->sibling;
16300 }
16301
16302 /* Dwarf2 dimensions are output from left to right, create the
16303 necessary array types in backwards order. */
16304
16305 type = element_type;
16306
16307 if (read_array_order (die, cu) == DW_ORD_col_major)
16308 {
16309 int i = 0;
16310
16311 while (i < range_types.size ())
16312 type = create_array_type_with_stride (NULL, type, range_types[i++],
16313 byte_stride_prop, bit_stride);
16314 }
16315 else
16316 {
16317 size_t ndim = range_types.size ();
16318 while (ndim-- > 0)
16319 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16320 byte_stride_prop, bit_stride);
16321 }
16322
16323 /* Understand Dwarf2 support for vector types (like they occur on
16324 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16325 array type. This is not part of the Dwarf2/3 standard yet, but a
16326 custom vendor extension. The main difference between a regular
16327 array and the vector variant is that vectors are passed by value
16328 to functions. */
16329 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16330 if (attr != nullptr)
16331 make_vector_type (type);
16332
16333 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16334 implementation may choose to implement triple vectors using this
16335 attribute. */
16336 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16337 if (attr != nullptr)
16338 {
16339 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16340 TYPE_LENGTH (type) = DW_UNSND (attr);
16341 else
16342 complaint (_("DW_AT_byte_size for array type smaller "
16343 "than the total size of elements"));
16344 }
16345
16346 name = dwarf2_name (die, cu);
16347 if (name)
16348 type->set_name (name);
16349
16350 maybe_set_alignment (cu, die, type);
16351
16352 /* Install the type in the die. */
16353 set_die_type (die, type, cu);
16354
16355 /* set_die_type should be already done. */
16356 set_descriptive_type (type, die, cu);
16357
16358 return type;
16359 }
16360
16361 static enum dwarf_array_dim_ordering
16362 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16363 {
16364 struct attribute *attr;
16365
16366 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16367
16368 if (attr != nullptr)
16369 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16370
16371 /* GNU F77 is a special case, as at 08/2004 array type info is the
16372 opposite order to the dwarf2 specification, but data is still
16373 laid out as per normal fortran.
16374
16375 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16376 version checking. */
16377
16378 if (cu->language == language_fortran
16379 && cu->producer && strstr (cu->producer, "GNU F77"))
16380 {
16381 return DW_ORD_row_major;
16382 }
16383
16384 switch (cu->language_defn->la_array_ordering)
16385 {
16386 case array_column_major:
16387 return DW_ORD_col_major;
16388 case array_row_major:
16389 default:
16390 return DW_ORD_row_major;
16391 };
16392 }
16393
16394 /* Extract all information from a DW_TAG_set_type DIE and put it in
16395 the DIE's type field. */
16396
16397 static struct type *
16398 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16399 {
16400 struct type *domain_type, *set_type;
16401 struct attribute *attr;
16402
16403 domain_type = die_type (die, cu);
16404
16405 /* The die_type call above may have already set the type for this DIE. */
16406 set_type = get_die_type (die, cu);
16407 if (set_type)
16408 return set_type;
16409
16410 set_type = create_set_type (NULL, domain_type);
16411
16412 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16413 if (attr != nullptr)
16414 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16415
16416 maybe_set_alignment (cu, die, set_type);
16417
16418 return set_die_type (die, set_type, cu);
16419 }
16420
16421 /* A helper for read_common_block that creates a locexpr baton.
16422 SYM is the symbol which we are marking as computed.
16423 COMMON_DIE is the DIE for the common block.
16424 COMMON_LOC is the location expression attribute for the common
16425 block itself.
16426 MEMBER_LOC is the location expression attribute for the particular
16427 member of the common block that we are processing.
16428 CU is the CU from which the above come. */
16429
16430 static void
16431 mark_common_block_symbol_computed (struct symbol *sym,
16432 struct die_info *common_die,
16433 struct attribute *common_loc,
16434 struct attribute *member_loc,
16435 struct dwarf2_cu *cu)
16436 {
16437 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16438 struct objfile *objfile = per_objfile->objfile;
16439 struct dwarf2_locexpr_baton *baton;
16440 gdb_byte *ptr;
16441 unsigned int cu_off;
16442 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16443 LONGEST offset = 0;
16444
16445 gdb_assert (common_loc && member_loc);
16446 gdb_assert (common_loc->form_is_block ());
16447 gdb_assert (member_loc->form_is_block ()
16448 || member_loc->form_is_constant ());
16449
16450 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16451 baton->per_objfile = per_objfile;
16452 baton->per_cu = cu->per_cu;
16453 gdb_assert (baton->per_cu);
16454
16455 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16456
16457 if (member_loc->form_is_constant ())
16458 {
16459 offset = member_loc->constant_value (0);
16460 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16461 }
16462 else
16463 baton->size += DW_BLOCK (member_loc)->size;
16464
16465 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16466 baton->data = ptr;
16467
16468 *ptr++ = DW_OP_call4;
16469 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16470 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16471 ptr += 4;
16472
16473 if (member_loc->form_is_constant ())
16474 {
16475 *ptr++ = DW_OP_addr;
16476 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16477 ptr += cu->header.addr_size;
16478 }
16479 else
16480 {
16481 /* We have to copy the data here, because DW_OP_call4 will only
16482 use a DW_AT_location attribute. */
16483 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16484 ptr += DW_BLOCK (member_loc)->size;
16485 }
16486
16487 *ptr++ = DW_OP_plus;
16488 gdb_assert (ptr - baton->data == baton->size);
16489
16490 SYMBOL_LOCATION_BATON (sym) = baton;
16491 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16492 }
16493
16494 /* Create appropriate locally-scoped variables for all the
16495 DW_TAG_common_block entries. Also create a struct common_block
16496 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16497 is used to separate the common blocks name namespace from regular
16498 variable names. */
16499
16500 static void
16501 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16502 {
16503 struct attribute *attr;
16504
16505 attr = dwarf2_attr (die, DW_AT_location, cu);
16506 if (attr != nullptr)
16507 {
16508 /* Support the .debug_loc offsets. */
16509 if (attr->form_is_block ())
16510 {
16511 /* Ok. */
16512 }
16513 else if (attr->form_is_section_offset ())
16514 {
16515 dwarf2_complex_location_expr_complaint ();
16516 attr = NULL;
16517 }
16518 else
16519 {
16520 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16521 "common block member");
16522 attr = NULL;
16523 }
16524 }
16525
16526 if (die->child != NULL)
16527 {
16528 struct objfile *objfile = cu->per_objfile->objfile;
16529 struct die_info *child_die;
16530 size_t n_entries = 0, size;
16531 struct common_block *common_block;
16532 struct symbol *sym;
16533
16534 for (child_die = die->child;
16535 child_die && child_die->tag;
16536 child_die = child_die->sibling)
16537 ++n_entries;
16538
16539 size = (sizeof (struct common_block)
16540 + (n_entries - 1) * sizeof (struct symbol *));
16541 common_block
16542 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16543 size);
16544 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16545 common_block->n_entries = 0;
16546
16547 for (child_die = die->child;
16548 child_die && child_die->tag;
16549 child_die = child_die->sibling)
16550 {
16551 /* Create the symbol in the DW_TAG_common_block block in the current
16552 symbol scope. */
16553 sym = new_symbol (child_die, NULL, cu);
16554 if (sym != NULL)
16555 {
16556 struct attribute *member_loc;
16557
16558 common_block->contents[common_block->n_entries++] = sym;
16559
16560 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16561 cu);
16562 if (member_loc)
16563 {
16564 /* GDB has handled this for a long time, but it is
16565 not specified by DWARF. It seems to have been
16566 emitted by gfortran at least as recently as:
16567 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16568 complaint (_("Variable in common block has "
16569 "DW_AT_data_member_location "
16570 "- DIE at %s [in module %s]"),
16571 sect_offset_str (child_die->sect_off),
16572 objfile_name (objfile));
16573
16574 if (member_loc->form_is_section_offset ())
16575 dwarf2_complex_location_expr_complaint ();
16576 else if (member_loc->form_is_constant ()
16577 || member_loc->form_is_block ())
16578 {
16579 if (attr != nullptr)
16580 mark_common_block_symbol_computed (sym, die, attr,
16581 member_loc, cu);
16582 }
16583 else
16584 dwarf2_complex_location_expr_complaint ();
16585 }
16586 }
16587 }
16588
16589 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16590 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16591 }
16592 }
16593
16594 /* Create a type for a C++ namespace. */
16595
16596 static struct type *
16597 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16598 {
16599 struct objfile *objfile = cu->per_objfile->objfile;
16600 const char *previous_prefix, *name;
16601 int is_anonymous;
16602 struct type *type;
16603
16604 /* For extensions, reuse the type of the original namespace. */
16605 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16606 {
16607 struct die_info *ext_die;
16608 struct dwarf2_cu *ext_cu = cu;
16609
16610 ext_die = dwarf2_extension (die, &ext_cu);
16611 type = read_type_die (ext_die, ext_cu);
16612
16613 /* EXT_CU may not be the same as CU.
16614 Ensure TYPE is recorded with CU in die_type_hash. */
16615 return set_die_type (die, type, cu);
16616 }
16617
16618 name = namespace_name (die, &is_anonymous, cu);
16619
16620 /* Now build the name of the current namespace. */
16621
16622 previous_prefix = determine_prefix (die, cu);
16623 if (previous_prefix[0] != '\0')
16624 name = typename_concat (&objfile->objfile_obstack,
16625 previous_prefix, name, 0, cu);
16626
16627 /* Create the type. */
16628 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16629
16630 return set_die_type (die, type, cu);
16631 }
16632
16633 /* Read a namespace scope. */
16634
16635 static void
16636 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16637 {
16638 struct objfile *objfile = cu->per_objfile->objfile;
16639 int is_anonymous;
16640
16641 /* Add a symbol associated to this if we haven't seen the namespace
16642 before. Also, add a using directive if it's an anonymous
16643 namespace. */
16644
16645 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16646 {
16647 struct type *type;
16648
16649 type = read_type_die (die, cu);
16650 new_symbol (die, type, cu);
16651
16652 namespace_name (die, &is_anonymous, cu);
16653 if (is_anonymous)
16654 {
16655 const char *previous_prefix = determine_prefix (die, cu);
16656
16657 std::vector<const char *> excludes;
16658 add_using_directive (using_directives (cu),
16659 previous_prefix, type->name (), NULL,
16660 NULL, excludes, 0, &objfile->objfile_obstack);
16661 }
16662 }
16663
16664 if (die->child != NULL)
16665 {
16666 struct die_info *child_die = die->child;
16667
16668 while (child_die && child_die->tag)
16669 {
16670 process_die (child_die, cu);
16671 child_die = child_die->sibling;
16672 }
16673 }
16674 }
16675
16676 /* Read a Fortran module as type. This DIE can be only a declaration used for
16677 imported module. Still we need that type as local Fortran "use ... only"
16678 declaration imports depend on the created type in determine_prefix. */
16679
16680 static struct type *
16681 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16682 {
16683 struct objfile *objfile = cu->per_objfile->objfile;
16684 const char *module_name;
16685 struct type *type;
16686
16687 module_name = dwarf2_name (die, cu);
16688 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16689
16690 return set_die_type (die, type, cu);
16691 }
16692
16693 /* Read a Fortran module. */
16694
16695 static void
16696 read_module (struct die_info *die, struct dwarf2_cu *cu)
16697 {
16698 struct die_info *child_die = die->child;
16699 struct type *type;
16700
16701 type = read_type_die (die, cu);
16702 new_symbol (die, type, cu);
16703
16704 while (child_die && child_die->tag)
16705 {
16706 process_die (child_die, cu);
16707 child_die = child_die->sibling;
16708 }
16709 }
16710
16711 /* Return the name of the namespace represented by DIE. Set
16712 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16713 namespace. */
16714
16715 static const char *
16716 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16717 {
16718 struct die_info *current_die;
16719 const char *name = NULL;
16720
16721 /* Loop through the extensions until we find a name. */
16722
16723 for (current_die = die;
16724 current_die != NULL;
16725 current_die = dwarf2_extension (die, &cu))
16726 {
16727 /* We don't use dwarf2_name here so that we can detect the absence
16728 of a name -> anonymous namespace. */
16729 name = dwarf2_string_attr (die, DW_AT_name, cu);
16730
16731 if (name != NULL)
16732 break;
16733 }
16734
16735 /* Is it an anonymous namespace? */
16736
16737 *is_anonymous = (name == NULL);
16738 if (*is_anonymous)
16739 name = CP_ANONYMOUS_NAMESPACE_STR;
16740
16741 return name;
16742 }
16743
16744 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16745 the user defined type vector. */
16746
16747 static struct type *
16748 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16749 {
16750 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16751 struct comp_unit_head *cu_header = &cu->header;
16752 struct type *type;
16753 struct attribute *attr_byte_size;
16754 struct attribute *attr_address_class;
16755 int byte_size, addr_class;
16756 struct type *target_type;
16757
16758 target_type = die_type (die, cu);
16759
16760 /* The die_type call above may have already set the type for this DIE. */
16761 type = get_die_type (die, cu);
16762 if (type)
16763 return type;
16764
16765 type = lookup_pointer_type (target_type);
16766
16767 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16768 if (attr_byte_size)
16769 byte_size = DW_UNSND (attr_byte_size);
16770 else
16771 byte_size = cu_header->addr_size;
16772
16773 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16774 if (attr_address_class)
16775 addr_class = DW_UNSND (attr_address_class);
16776 else
16777 addr_class = DW_ADDR_none;
16778
16779 ULONGEST alignment = get_alignment (cu, die);
16780
16781 /* If the pointer size, alignment, or address class is different
16782 than the default, create a type variant marked as such and set
16783 the length accordingly. */
16784 if (TYPE_LENGTH (type) != byte_size
16785 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16786 && alignment != TYPE_RAW_ALIGN (type))
16787 || addr_class != DW_ADDR_none)
16788 {
16789 if (gdbarch_address_class_type_flags_p (gdbarch))
16790 {
16791 int type_flags;
16792
16793 type_flags = gdbarch_address_class_type_flags
16794 (gdbarch, byte_size, addr_class);
16795 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16796 == 0);
16797 type = make_type_with_address_space (type, type_flags);
16798 }
16799 else if (TYPE_LENGTH (type) != byte_size)
16800 {
16801 complaint (_("invalid pointer size %d"), byte_size);
16802 }
16803 else if (TYPE_RAW_ALIGN (type) != alignment)
16804 {
16805 complaint (_("Invalid DW_AT_alignment"
16806 " - DIE at %s [in module %s]"),
16807 sect_offset_str (die->sect_off),
16808 objfile_name (cu->per_objfile->objfile));
16809 }
16810 else
16811 {
16812 /* Should we also complain about unhandled address classes? */
16813 }
16814 }
16815
16816 TYPE_LENGTH (type) = byte_size;
16817 set_type_align (type, alignment);
16818 return set_die_type (die, type, cu);
16819 }
16820
16821 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16822 the user defined type vector. */
16823
16824 static struct type *
16825 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16826 {
16827 struct type *type;
16828 struct type *to_type;
16829 struct type *domain;
16830
16831 to_type = die_type (die, cu);
16832 domain = die_containing_type (die, cu);
16833
16834 /* The calls above may have already set the type for this DIE. */
16835 type = get_die_type (die, cu);
16836 if (type)
16837 return type;
16838
16839 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16840 type = lookup_methodptr_type (to_type);
16841 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16842 {
16843 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16844
16845 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16846 to_type->fields (), to_type->num_fields (),
16847 TYPE_VARARGS (to_type));
16848 type = lookup_methodptr_type (new_type);
16849 }
16850 else
16851 type = lookup_memberptr_type (to_type, domain);
16852
16853 return set_die_type (die, type, cu);
16854 }
16855
16856 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16857 the user defined type vector. */
16858
16859 static struct type *
16860 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16861 enum type_code refcode)
16862 {
16863 struct comp_unit_head *cu_header = &cu->header;
16864 struct type *type, *target_type;
16865 struct attribute *attr;
16866
16867 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16868
16869 target_type = die_type (die, cu);
16870
16871 /* The die_type call above may have already set the type for this DIE. */
16872 type = get_die_type (die, cu);
16873 if (type)
16874 return type;
16875
16876 type = lookup_reference_type (target_type, refcode);
16877 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16878 if (attr != nullptr)
16879 {
16880 TYPE_LENGTH (type) = DW_UNSND (attr);
16881 }
16882 else
16883 {
16884 TYPE_LENGTH (type) = cu_header->addr_size;
16885 }
16886 maybe_set_alignment (cu, die, type);
16887 return set_die_type (die, type, cu);
16888 }
16889
16890 /* Add the given cv-qualifiers to the element type of the array. GCC
16891 outputs DWARF type qualifiers that apply to an array, not the
16892 element type. But GDB relies on the array element type to carry
16893 the cv-qualifiers. This mimics section 6.7.3 of the C99
16894 specification. */
16895
16896 static struct type *
16897 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16898 struct type *base_type, int cnst, int voltl)
16899 {
16900 struct type *el_type, *inner_array;
16901
16902 base_type = copy_type (base_type);
16903 inner_array = base_type;
16904
16905 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16906 {
16907 TYPE_TARGET_TYPE (inner_array) =
16908 copy_type (TYPE_TARGET_TYPE (inner_array));
16909 inner_array = TYPE_TARGET_TYPE (inner_array);
16910 }
16911
16912 el_type = TYPE_TARGET_TYPE (inner_array);
16913 cnst |= TYPE_CONST (el_type);
16914 voltl |= TYPE_VOLATILE (el_type);
16915 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16916
16917 return set_die_type (die, base_type, cu);
16918 }
16919
16920 static struct type *
16921 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16922 {
16923 struct type *base_type, *cv_type;
16924
16925 base_type = die_type (die, cu);
16926
16927 /* The die_type call above may have already set the type for this DIE. */
16928 cv_type = get_die_type (die, cu);
16929 if (cv_type)
16930 return cv_type;
16931
16932 /* In case the const qualifier is applied to an array type, the element type
16933 is so qualified, not the array type (section 6.7.3 of C99). */
16934 if (base_type->code () == TYPE_CODE_ARRAY)
16935 return add_array_cv_type (die, cu, base_type, 1, 0);
16936
16937 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16938 return set_die_type (die, cv_type, cu);
16939 }
16940
16941 static struct type *
16942 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16943 {
16944 struct type *base_type, *cv_type;
16945
16946 base_type = die_type (die, cu);
16947
16948 /* The die_type call above may have already set the type for this DIE. */
16949 cv_type = get_die_type (die, cu);
16950 if (cv_type)
16951 return cv_type;
16952
16953 /* In case the volatile qualifier is applied to an array type, the
16954 element type is so qualified, not the array type (section 6.7.3
16955 of C99). */
16956 if (base_type->code () == TYPE_CODE_ARRAY)
16957 return add_array_cv_type (die, cu, base_type, 0, 1);
16958
16959 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16960 return set_die_type (die, cv_type, cu);
16961 }
16962
16963 /* Handle DW_TAG_restrict_type. */
16964
16965 static struct type *
16966 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16967 {
16968 struct type *base_type, *cv_type;
16969
16970 base_type = die_type (die, cu);
16971
16972 /* The die_type call above may have already set the type for this DIE. */
16973 cv_type = get_die_type (die, cu);
16974 if (cv_type)
16975 return cv_type;
16976
16977 cv_type = make_restrict_type (base_type);
16978 return set_die_type (die, cv_type, cu);
16979 }
16980
16981 /* Handle DW_TAG_atomic_type. */
16982
16983 static struct type *
16984 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16985 {
16986 struct type *base_type, *cv_type;
16987
16988 base_type = die_type (die, cu);
16989
16990 /* The die_type call above may have already set the type for this DIE. */
16991 cv_type = get_die_type (die, cu);
16992 if (cv_type)
16993 return cv_type;
16994
16995 cv_type = make_atomic_type (base_type);
16996 return set_die_type (die, cv_type, cu);
16997 }
16998
16999 /* Extract all information from a DW_TAG_string_type DIE and add to
17000 the user defined type vector. It isn't really a user defined type,
17001 but it behaves like one, with other DIE's using an AT_user_def_type
17002 attribute to reference it. */
17003
17004 static struct type *
17005 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17006 {
17007 struct objfile *objfile = cu->per_objfile->objfile;
17008 struct gdbarch *gdbarch = objfile->arch ();
17009 struct type *type, *range_type, *index_type, *char_type;
17010 struct attribute *attr;
17011 struct dynamic_prop prop;
17012 bool length_is_constant = true;
17013 LONGEST length;
17014
17015 /* There are a couple of places where bit sizes might be made use of
17016 when parsing a DW_TAG_string_type, however, no producer that we know
17017 of make use of these. Handling bit sizes that are a multiple of the
17018 byte size is easy enough, but what about other bit sizes? Lets deal
17019 with that problem when we have to. Warn about these attributes being
17020 unsupported, then parse the type and ignore them like we always
17021 have. */
17022 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17023 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17024 {
17025 static bool warning_printed = false;
17026 if (!warning_printed)
17027 {
17028 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17029 "currently supported on DW_TAG_string_type."));
17030 warning_printed = true;
17031 }
17032 }
17033
17034 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17035 if (attr != nullptr && !attr->form_is_constant ())
17036 {
17037 /* The string length describes the location at which the length of
17038 the string can be found. The size of the length field can be
17039 specified with one of the attributes below. */
17040 struct type *prop_type;
17041 struct attribute *len
17042 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17043 if (len == nullptr)
17044 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17045 if (len != nullptr && len->form_is_constant ())
17046 {
17047 /* Pass 0 as the default as we know this attribute is constant
17048 and the default value will not be returned. */
17049 LONGEST sz = len->constant_value (0);
17050 prop_type = cu->per_objfile->int_type (sz, true);
17051 }
17052 else
17053 {
17054 /* If the size is not specified then we assume it is the size of
17055 an address on this target. */
17056 prop_type = cu->addr_sized_int_type (true);
17057 }
17058
17059 /* Convert the attribute into a dynamic property. */
17060 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17061 length = 1;
17062 else
17063 length_is_constant = false;
17064 }
17065 else if (attr != nullptr)
17066 {
17067 /* This DW_AT_string_length just contains the length with no
17068 indirection. There's no need to create a dynamic property in this
17069 case. Pass 0 for the default value as we know it will not be
17070 returned in this case. */
17071 length = attr->constant_value (0);
17072 }
17073 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17074 {
17075 /* We don't currently support non-constant byte sizes for strings. */
17076 length = attr->constant_value (1);
17077 }
17078 else
17079 {
17080 /* Use 1 as a fallback length if we have nothing else. */
17081 length = 1;
17082 }
17083
17084 index_type = objfile_type (objfile)->builtin_int;
17085 if (length_is_constant)
17086 range_type = create_static_range_type (NULL, index_type, 1, length);
17087 else
17088 {
17089 struct dynamic_prop low_bound;
17090
17091 low_bound.kind = PROP_CONST;
17092 low_bound.data.const_val = 1;
17093 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17094 }
17095 char_type = language_string_char_type (cu->language_defn, gdbarch);
17096 type = create_string_type (NULL, char_type, range_type);
17097
17098 return set_die_type (die, type, cu);
17099 }
17100
17101 /* Assuming that DIE corresponds to a function, returns nonzero
17102 if the function is prototyped. */
17103
17104 static int
17105 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17106 {
17107 struct attribute *attr;
17108
17109 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17110 if (attr && (DW_UNSND (attr) != 0))
17111 return 1;
17112
17113 /* The DWARF standard implies that the DW_AT_prototyped attribute
17114 is only meaningful for C, but the concept also extends to other
17115 languages that allow unprototyped functions (Eg: Objective C).
17116 For all other languages, assume that functions are always
17117 prototyped. */
17118 if (cu->language != language_c
17119 && cu->language != language_objc
17120 && cu->language != language_opencl)
17121 return 1;
17122
17123 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17124 prototyped and unprototyped functions; default to prototyped,
17125 since that is more common in modern code (and RealView warns
17126 about unprototyped functions). */
17127 if (producer_is_realview (cu->producer))
17128 return 1;
17129
17130 return 0;
17131 }
17132
17133 /* Handle DIES due to C code like:
17134
17135 struct foo
17136 {
17137 int (*funcp)(int a, long l);
17138 int b;
17139 };
17140
17141 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17142
17143 static struct type *
17144 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17145 {
17146 struct objfile *objfile = cu->per_objfile->objfile;
17147 struct type *type; /* Type that this function returns. */
17148 struct type *ftype; /* Function that returns above type. */
17149 struct attribute *attr;
17150
17151 type = die_type (die, cu);
17152
17153 /* The die_type call above may have already set the type for this DIE. */
17154 ftype = get_die_type (die, cu);
17155 if (ftype)
17156 return ftype;
17157
17158 ftype = lookup_function_type (type);
17159
17160 if (prototyped_function_p (die, cu))
17161 TYPE_PROTOTYPED (ftype) = 1;
17162
17163 /* Store the calling convention in the type if it's available in
17164 the subroutine die. Otherwise set the calling convention to
17165 the default value DW_CC_normal. */
17166 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17167 if (attr != nullptr
17168 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17169 TYPE_CALLING_CONVENTION (ftype)
17170 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17171 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17172 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17173 else
17174 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17175
17176 /* Record whether the function returns normally to its caller or not
17177 if the DWARF producer set that information. */
17178 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17179 if (attr && (DW_UNSND (attr) != 0))
17180 TYPE_NO_RETURN (ftype) = 1;
17181
17182 /* We need to add the subroutine type to the die immediately so
17183 we don't infinitely recurse when dealing with parameters
17184 declared as the same subroutine type. */
17185 set_die_type (die, ftype, cu);
17186
17187 if (die->child != NULL)
17188 {
17189 struct type *void_type = objfile_type (objfile)->builtin_void;
17190 struct die_info *child_die;
17191 int nparams, iparams;
17192
17193 /* Count the number of parameters.
17194 FIXME: GDB currently ignores vararg functions, but knows about
17195 vararg member functions. */
17196 nparams = 0;
17197 child_die = die->child;
17198 while (child_die && child_die->tag)
17199 {
17200 if (child_die->tag == DW_TAG_formal_parameter)
17201 nparams++;
17202 else if (child_die->tag == DW_TAG_unspecified_parameters)
17203 TYPE_VARARGS (ftype) = 1;
17204 child_die = child_die->sibling;
17205 }
17206
17207 /* Allocate storage for parameters and fill them in. */
17208 ftype->set_num_fields (nparams);
17209 ftype->set_fields
17210 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17211
17212 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17213 even if we error out during the parameters reading below. */
17214 for (iparams = 0; iparams < nparams; iparams++)
17215 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17216
17217 iparams = 0;
17218 child_die = die->child;
17219 while (child_die && child_die->tag)
17220 {
17221 if (child_die->tag == DW_TAG_formal_parameter)
17222 {
17223 struct type *arg_type;
17224
17225 /* DWARF version 2 has no clean way to discern C++
17226 static and non-static member functions. G++ helps
17227 GDB by marking the first parameter for non-static
17228 member functions (which is the this pointer) as
17229 artificial. We pass this information to
17230 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17231
17232 DWARF version 3 added DW_AT_object_pointer, which GCC
17233 4.5 does not yet generate. */
17234 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17235 if (attr != nullptr)
17236 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17237 else
17238 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17239 arg_type = die_type (child_die, cu);
17240
17241 /* RealView does not mark THIS as const, which the testsuite
17242 expects. GCC marks THIS as const in method definitions,
17243 but not in the class specifications (GCC PR 43053). */
17244 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17245 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17246 {
17247 int is_this = 0;
17248 struct dwarf2_cu *arg_cu = cu;
17249 const char *name = dwarf2_name (child_die, cu);
17250
17251 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17252 if (attr != nullptr)
17253 {
17254 /* If the compiler emits this, use it. */
17255 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17256 is_this = 1;
17257 }
17258 else if (name && strcmp (name, "this") == 0)
17259 /* Function definitions will have the argument names. */
17260 is_this = 1;
17261 else if (name == NULL && iparams == 0)
17262 /* Declarations may not have the names, so like
17263 elsewhere in GDB, assume an artificial first
17264 argument is "this". */
17265 is_this = 1;
17266
17267 if (is_this)
17268 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17269 arg_type, 0);
17270 }
17271
17272 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17273 iparams++;
17274 }
17275 child_die = child_die->sibling;
17276 }
17277 }
17278
17279 return ftype;
17280 }
17281
17282 static struct type *
17283 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17284 {
17285 struct objfile *objfile = cu->per_objfile->objfile;
17286 const char *name = NULL;
17287 struct type *this_type, *target_type;
17288
17289 name = dwarf2_full_name (NULL, die, cu);
17290 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17291 TYPE_TARGET_STUB (this_type) = 1;
17292 set_die_type (die, this_type, cu);
17293 target_type = die_type (die, cu);
17294 if (target_type != this_type)
17295 TYPE_TARGET_TYPE (this_type) = target_type;
17296 else
17297 {
17298 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17299 spec and cause infinite loops in GDB. */
17300 complaint (_("Self-referential DW_TAG_typedef "
17301 "- DIE at %s [in module %s]"),
17302 sect_offset_str (die->sect_off), objfile_name (objfile));
17303 TYPE_TARGET_TYPE (this_type) = NULL;
17304 }
17305 if (name == NULL)
17306 {
17307 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17308 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17309 Handle these by just returning the target type, rather than
17310 constructing an anonymous typedef type and trying to handle this
17311 elsewhere. */
17312 set_die_type (die, target_type, cu);
17313 return target_type;
17314 }
17315 return this_type;
17316 }
17317
17318 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17319 (which may be different from NAME) to the architecture back-end to allow
17320 it to guess the correct format if necessary. */
17321
17322 static struct type *
17323 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17324 const char *name_hint, enum bfd_endian byte_order)
17325 {
17326 struct gdbarch *gdbarch = objfile->arch ();
17327 const struct floatformat **format;
17328 struct type *type;
17329
17330 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17331 if (format)
17332 type = init_float_type (objfile, bits, name, format, byte_order);
17333 else
17334 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17335
17336 return type;
17337 }
17338
17339 /* Allocate an integer type of size BITS and name NAME. */
17340
17341 static struct type *
17342 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17343 int bits, int unsigned_p, const char *name)
17344 {
17345 struct type *type;
17346
17347 /* Versions of Intel's C Compiler generate an integer type called "void"
17348 instead of using DW_TAG_unspecified_type. This has been seen on
17349 at least versions 14, 17, and 18. */
17350 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17351 && strcmp (name, "void") == 0)
17352 type = objfile_type (objfile)->builtin_void;
17353 else
17354 type = init_integer_type (objfile, bits, unsigned_p, name);
17355
17356 return type;
17357 }
17358
17359 /* Initialise and return a floating point type of size BITS suitable for
17360 use as a component of a complex number. The NAME_HINT is passed through
17361 when initialising the floating point type and is the name of the complex
17362 type.
17363
17364 As DWARF doesn't currently provide an explicit name for the components
17365 of a complex number, but it can be helpful to have these components
17366 named, we try to select a suitable name based on the size of the
17367 component. */
17368 static struct type *
17369 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17370 struct objfile *objfile,
17371 int bits, const char *name_hint,
17372 enum bfd_endian byte_order)
17373 {
17374 gdbarch *gdbarch = objfile->arch ();
17375 struct type *tt = nullptr;
17376
17377 /* Try to find a suitable floating point builtin type of size BITS.
17378 We're going to use the name of this type as the name for the complex
17379 target type that we are about to create. */
17380 switch (cu->language)
17381 {
17382 case language_fortran:
17383 switch (bits)
17384 {
17385 case 32:
17386 tt = builtin_f_type (gdbarch)->builtin_real;
17387 break;
17388 case 64:
17389 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17390 break;
17391 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17392 case 128:
17393 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17394 break;
17395 }
17396 break;
17397 default:
17398 switch (bits)
17399 {
17400 case 32:
17401 tt = builtin_type (gdbarch)->builtin_float;
17402 break;
17403 case 64:
17404 tt = builtin_type (gdbarch)->builtin_double;
17405 break;
17406 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17407 case 128:
17408 tt = builtin_type (gdbarch)->builtin_long_double;
17409 break;
17410 }
17411 break;
17412 }
17413
17414 /* If the type we found doesn't match the size we were looking for, then
17415 pretend we didn't find a type at all, the complex target type we
17416 create will then be nameless. */
17417 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17418 tt = nullptr;
17419
17420 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17421 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17422 }
17423
17424 /* Find a representation of a given base type and install
17425 it in the TYPE field of the die. */
17426
17427 static struct type *
17428 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17429 {
17430 struct objfile *objfile = cu->per_objfile->objfile;
17431 struct type *type;
17432 struct attribute *attr;
17433 int encoding = 0, bits = 0;
17434 const char *name;
17435 gdbarch *arch;
17436
17437 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17438 if (attr != nullptr)
17439 encoding = DW_UNSND (attr);
17440 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17441 if (attr != nullptr)
17442 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17443 name = dwarf2_name (die, cu);
17444 if (!name)
17445 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17446
17447 arch = objfile->arch ();
17448 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17449
17450 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17451 if (attr)
17452 {
17453 int endianity = DW_UNSND (attr);
17454
17455 switch (endianity)
17456 {
17457 case DW_END_big:
17458 byte_order = BFD_ENDIAN_BIG;
17459 break;
17460 case DW_END_little:
17461 byte_order = BFD_ENDIAN_LITTLE;
17462 break;
17463 default:
17464 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17465 break;
17466 }
17467 }
17468
17469 switch (encoding)
17470 {
17471 case DW_ATE_address:
17472 /* Turn DW_ATE_address into a void * pointer. */
17473 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17474 type = init_pointer_type (objfile, bits, name, type);
17475 break;
17476 case DW_ATE_boolean:
17477 type = init_boolean_type (objfile, bits, 1, name);
17478 break;
17479 case DW_ATE_complex_float:
17480 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17481 byte_order);
17482 if (type->code () == TYPE_CODE_ERROR)
17483 {
17484 if (name == nullptr)
17485 {
17486 struct obstack *obstack
17487 = &cu->per_objfile->objfile->objfile_obstack;
17488 name = obconcat (obstack, "_Complex ", type->name (),
17489 nullptr);
17490 }
17491 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17492 }
17493 else
17494 type = init_complex_type (name, type);
17495 break;
17496 case DW_ATE_decimal_float:
17497 type = init_decfloat_type (objfile, bits, name);
17498 break;
17499 case DW_ATE_float:
17500 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17501 break;
17502 case DW_ATE_signed:
17503 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17504 break;
17505 case DW_ATE_unsigned:
17506 if (cu->language == language_fortran
17507 && name
17508 && startswith (name, "character("))
17509 type = init_character_type (objfile, bits, 1, name);
17510 else
17511 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17512 break;
17513 case DW_ATE_signed_char:
17514 if (cu->language == language_ada || cu->language == language_m2
17515 || cu->language == language_pascal
17516 || cu->language == language_fortran)
17517 type = init_character_type (objfile, bits, 0, name);
17518 else
17519 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17520 break;
17521 case DW_ATE_unsigned_char:
17522 if (cu->language == language_ada || cu->language == language_m2
17523 || cu->language == language_pascal
17524 || cu->language == language_fortran
17525 || cu->language == language_rust)
17526 type = init_character_type (objfile, bits, 1, name);
17527 else
17528 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17529 break;
17530 case DW_ATE_UTF:
17531 {
17532 if (bits == 16)
17533 type = builtin_type (arch)->builtin_char16;
17534 else if (bits == 32)
17535 type = builtin_type (arch)->builtin_char32;
17536 else
17537 {
17538 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17539 bits);
17540 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17541 }
17542 return set_die_type (die, type, cu);
17543 }
17544 break;
17545
17546 default:
17547 complaint (_("unsupported DW_AT_encoding: '%s'"),
17548 dwarf_type_encoding_name (encoding));
17549 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17550 break;
17551 }
17552
17553 if (name && strcmp (name, "char") == 0)
17554 TYPE_NOSIGN (type) = 1;
17555
17556 maybe_set_alignment (cu, die, type);
17557
17558 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17559
17560 return set_die_type (die, type, cu);
17561 }
17562
17563 /* Parse dwarf attribute if it's a block, reference or constant and put the
17564 resulting value of the attribute into struct bound_prop.
17565 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17566
17567 static int
17568 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17569 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17570 struct type *default_type)
17571 {
17572 struct dwarf2_property_baton *baton;
17573 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17574 struct objfile *objfile = per_objfile->objfile;
17575 struct obstack *obstack = &objfile->objfile_obstack;
17576
17577 gdb_assert (default_type != NULL);
17578
17579 if (attr == NULL || prop == NULL)
17580 return 0;
17581
17582 if (attr->form_is_block ())
17583 {
17584 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17585 baton->property_type = default_type;
17586 baton->locexpr.per_cu = cu->per_cu;
17587 baton->locexpr.per_objfile = per_objfile;
17588 baton->locexpr.size = DW_BLOCK (attr)->size;
17589 baton->locexpr.data = DW_BLOCK (attr)->data;
17590 switch (attr->name)
17591 {
17592 case DW_AT_string_length:
17593 baton->locexpr.is_reference = true;
17594 break;
17595 default:
17596 baton->locexpr.is_reference = false;
17597 break;
17598 }
17599 prop->data.baton = baton;
17600 prop->kind = PROP_LOCEXPR;
17601 gdb_assert (prop->data.baton != NULL);
17602 }
17603 else if (attr->form_is_ref ())
17604 {
17605 struct dwarf2_cu *target_cu = cu;
17606 struct die_info *target_die;
17607 struct attribute *target_attr;
17608
17609 target_die = follow_die_ref (die, attr, &target_cu);
17610 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17611 if (target_attr == NULL)
17612 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17613 target_cu);
17614 if (target_attr == NULL)
17615 return 0;
17616
17617 switch (target_attr->name)
17618 {
17619 case DW_AT_location:
17620 if (target_attr->form_is_section_offset ())
17621 {
17622 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17623 baton->property_type = die_type (target_die, target_cu);
17624 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17625 prop->data.baton = baton;
17626 prop->kind = PROP_LOCLIST;
17627 gdb_assert (prop->data.baton != NULL);
17628 }
17629 else if (target_attr->form_is_block ())
17630 {
17631 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17632 baton->property_type = die_type (target_die, target_cu);
17633 baton->locexpr.per_cu = cu->per_cu;
17634 baton->locexpr.per_objfile = per_objfile;
17635 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17636 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17637 baton->locexpr.is_reference = true;
17638 prop->data.baton = baton;
17639 prop->kind = PROP_LOCEXPR;
17640 gdb_assert (prop->data.baton != NULL);
17641 }
17642 else
17643 {
17644 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17645 "dynamic property");
17646 return 0;
17647 }
17648 break;
17649 case DW_AT_data_member_location:
17650 {
17651 LONGEST offset;
17652
17653 if (!handle_data_member_location (target_die, target_cu,
17654 &offset))
17655 return 0;
17656
17657 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17658 baton->property_type = read_type_die (target_die->parent,
17659 target_cu);
17660 baton->offset_info.offset = offset;
17661 baton->offset_info.type = die_type (target_die, target_cu);
17662 prop->data.baton = baton;
17663 prop->kind = PROP_ADDR_OFFSET;
17664 break;
17665 }
17666 }
17667 }
17668 else if (attr->form_is_constant ())
17669 {
17670 prop->data.const_val = attr->constant_value (0);
17671 prop->kind = PROP_CONST;
17672 }
17673 else
17674 {
17675 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17676 dwarf2_name (die, cu));
17677 return 0;
17678 }
17679
17680 return 1;
17681 }
17682
17683 /* See read.h. */
17684
17685 struct type *
17686 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17687 {
17688 struct type *int_type;
17689
17690 /* Helper macro to examine the various builtin types. */
17691 #define TRY_TYPE(F) \
17692 int_type = (unsigned_p \
17693 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17694 : objfile_type (objfile)->builtin_ ## F); \
17695 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17696 return int_type
17697
17698 TRY_TYPE (char);
17699 TRY_TYPE (short);
17700 TRY_TYPE (int);
17701 TRY_TYPE (long);
17702 TRY_TYPE (long_long);
17703
17704 #undef TRY_TYPE
17705
17706 gdb_assert_not_reached ("unable to find suitable integer type");
17707 }
17708
17709 /* See read.h. */
17710
17711 struct type *
17712 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17713 {
17714 int addr_size = this->per_cu->addr_size ();
17715 return this->per_objfile->int_type (addr_size, unsigned_p);
17716 }
17717
17718 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17719 present (which is valid) then compute the default type based on the
17720 compilation units address size. */
17721
17722 static struct type *
17723 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17724 {
17725 struct type *index_type = die_type (die, cu);
17726
17727 /* Dwarf-2 specifications explicitly allows to create subrange types
17728 without specifying a base type.
17729 In that case, the base type must be set to the type of
17730 the lower bound, upper bound or count, in that order, if any of these
17731 three attributes references an object that has a type.
17732 If no base type is found, the Dwarf-2 specifications say that
17733 a signed integer type of size equal to the size of an address should
17734 be used.
17735 For the following C code: `extern char gdb_int [];'
17736 GCC produces an empty range DIE.
17737 FIXME: muller/2010-05-28: Possible references to object for low bound,
17738 high bound or count are not yet handled by this code. */
17739 if (index_type->code () == TYPE_CODE_VOID)
17740 index_type = cu->addr_sized_int_type (false);
17741
17742 return index_type;
17743 }
17744
17745 /* Read the given DW_AT_subrange DIE. */
17746
17747 static struct type *
17748 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17749 {
17750 struct type *base_type, *orig_base_type;
17751 struct type *range_type;
17752 struct attribute *attr;
17753 struct dynamic_prop low, high;
17754 int low_default_is_valid;
17755 int high_bound_is_count = 0;
17756 const char *name;
17757 ULONGEST negative_mask;
17758
17759 orig_base_type = read_subrange_index_type (die, cu);
17760
17761 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17762 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17763 creating the range type, but we use the result of check_typedef
17764 when examining properties of the type. */
17765 base_type = check_typedef (orig_base_type);
17766
17767 /* The die_type call above may have already set the type for this DIE. */
17768 range_type = get_die_type (die, cu);
17769 if (range_type)
17770 return range_type;
17771
17772 low.kind = PROP_CONST;
17773 high.kind = PROP_CONST;
17774 high.data.const_val = 0;
17775
17776 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17777 omitting DW_AT_lower_bound. */
17778 switch (cu->language)
17779 {
17780 case language_c:
17781 case language_cplus:
17782 low.data.const_val = 0;
17783 low_default_is_valid = 1;
17784 break;
17785 case language_fortran:
17786 low.data.const_val = 1;
17787 low_default_is_valid = 1;
17788 break;
17789 case language_d:
17790 case language_objc:
17791 case language_rust:
17792 low.data.const_val = 0;
17793 low_default_is_valid = (cu->header.version >= 4);
17794 break;
17795 case language_ada:
17796 case language_m2:
17797 case language_pascal:
17798 low.data.const_val = 1;
17799 low_default_is_valid = (cu->header.version >= 4);
17800 break;
17801 default:
17802 low.data.const_val = 0;
17803 low_default_is_valid = 0;
17804 break;
17805 }
17806
17807 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17808 if (attr != nullptr)
17809 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17810 else if (!low_default_is_valid)
17811 complaint (_("Missing DW_AT_lower_bound "
17812 "- DIE at %s [in module %s]"),
17813 sect_offset_str (die->sect_off),
17814 objfile_name (cu->per_objfile->objfile));
17815
17816 struct attribute *attr_ub, *attr_count;
17817 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17818 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17819 {
17820 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17821 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17822 {
17823 /* If bounds are constant do the final calculation here. */
17824 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17825 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17826 else
17827 high_bound_is_count = 1;
17828 }
17829 else
17830 {
17831 if (attr_ub != NULL)
17832 complaint (_("Unresolved DW_AT_upper_bound "
17833 "- DIE at %s [in module %s]"),
17834 sect_offset_str (die->sect_off),
17835 objfile_name (cu->per_objfile->objfile));
17836 if (attr_count != NULL)
17837 complaint (_("Unresolved DW_AT_count "
17838 "- DIE at %s [in module %s]"),
17839 sect_offset_str (die->sect_off),
17840 objfile_name (cu->per_objfile->objfile));
17841 }
17842 }
17843
17844 LONGEST bias = 0;
17845 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17846 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17847 bias = bias_attr->constant_value (0);
17848
17849 /* Normally, the DWARF producers are expected to use a signed
17850 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17851 But this is unfortunately not always the case, as witnessed
17852 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17853 is used instead. To work around that ambiguity, we treat
17854 the bounds as signed, and thus sign-extend their values, when
17855 the base type is signed. */
17856 negative_mask =
17857 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17858 if (low.kind == PROP_CONST
17859 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17860 low.data.const_val |= negative_mask;
17861 if (high.kind == PROP_CONST
17862 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17863 high.data.const_val |= negative_mask;
17864
17865 /* Check for bit and byte strides. */
17866 struct dynamic_prop byte_stride_prop;
17867 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17868 if (attr_byte_stride != nullptr)
17869 {
17870 struct type *prop_type = cu->addr_sized_int_type (false);
17871 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17872 prop_type);
17873 }
17874
17875 struct dynamic_prop bit_stride_prop;
17876 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17877 if (attr_bit_stride != nullptr)
17878 {
17879 /* It only makes sense to have either a bit or byte stride. */
17880 if (attr_byte_stride != nullptr)
17881 {
17882 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17883 "- DIE at %s [in module %s]"),
17884 sect_offset_str (die->sect_off),
17885 objfile_name (cu->per_objfile->objfile));
17886 attr_bit_stride = nullptr;
17887 }
17888 else
17889 {
17890 struct type *prop_type = cu->addr_sized_int_type (false);
17891 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17892 prop_type);
17893 }
17894 }
17895
17896 if (attr_byte_stride != nullptr
17897 || attr_bit_stride != nullptr)
17898 {
17899 bool byte_stride_p = (attr_byte_stride != nullptr);
17900 struct dynamic_prop *stride
17901 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17902
17903 range_type
17904 = create_range_type_with_stride (NULL, orig_base_type, &low,
17905 &high, bias, stride, byte_stride_p);
17906 }
17907 else
17908 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17909
17910 if (high_bound_is_count)
17911 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17912
17913 /* Ada expects an empty array on no boundary attributes. */
17914 if (attr == NULL && cu->language != language_ada)
17915 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17916
17917 name = dwarf2_name (die, cu);
17918 if (name)
17919 range_type->set_name (name);
17920
17921 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17922 if (attr != nullptr)
17923 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17924
17925 maybe_set_alignment (cu, die, range_type);
17926
17927 set_die_type (die, range_type, cu);
17928
17929 /* set_die_type should be already done. */
17930 set_descriptive_type (range_type, die, cu);
17931
17932 return range_type;
17933 }
17934
17935 static struct type *
17936 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17937 {
17938 struct type *type;
17939
17940 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17941 type->set_name (dwarf2_name (die, cu));
17942
17943 /* In Ada, an unspecified type is typically used when the description
17944 of the type is deferred to a different unit. When encountering
17945 such a type, we treat it as a stub, and try to resolve it later on,
17946 when needed. */
17947 if (cu->language == language_ada)
17948 TYPE_STUB (type) = 1;
17949
17950 return set_die_type (die, type, cu);
17951 }
17952
17953 /* Read a single die and all its descendents. Set the die's sibling
17954 field to NULL; set other fields in the die correctly, and set all
17955 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17956 location of the info_ptr after reading all of those dies. PARENT
17957 is the parent of the die in question. */
17958
17959 static struct die_info *
17960 read_die_and_children (const struct die_reader_specs *reader,
17961 const gdb_byte *info_ptr,
17962 const gdb_byte **new_info_ptr,
17963 struct die_info *parent)
17964 {
17965 struct die_info *die;
17966 const gdb_byte *cur_ptr;
17967
17968 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17969 if (die == NULL)
17970 {
17971 *new_info_ptr = cur_ptr;
17972 return NULL;
17973 }
17974 store_in_ref_table (die, reader->cu);
17975
17976 if (die->has_children)
17977 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17978 else
17979 {
17980 die->child = NULL;
17981 *new_info_ptr = cur_ptr;
17982 }
17983
17984 die->sibling = NULL;
17985 die->parent = parent;
17986 return die;
17987 }
17988
17989 /* Read a die, all of its descendents, and all of its siblings; set
17990 all of the fields of all of the dies correctly. Arguments are as
17991 in read_die_and_children. */
17992
17993 static struct die_info *
17994 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17995 const gdb_byte *info_ptr,
17996 const gdb_byte **new_info_ptr,
17997 struct die_info *parent)
17998 {
17999 struct die_info *first_die, *last_sibling;
18000 const gdb_byte *cur_ptr;
18001
18002 cur_ptr = info_ptr;
18003 first_die = last_sibling = NULL;
18004
18005 while (1)
18006 {
18007 struct die_info *die
18008 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18009
18010 if (die == NULL)
18011 {
18012 *new_info_ptr = cur_ptr;
18013 return first_die;
18014 }
18015
18016 if (!first_die)
18017 first_die = die;
18018 else
18019 last_sibling->sibling = die;
18020
18021 last_sibling = die;
18022 }
18023 }
18024
18025 /* Read a die, all of its descendents, and all of its siblings; set
18026 all of the fields of all of the dies correctly. Arguments are as
18027 in read_die_and_children.
18028 This the main entry point for reading a DIE and all its children. */
18029
18030 static struct die_info *
18031 read_die_and_siblings (const struct die_reader_specs *reader,
18032 const gdb_byte *info_ptr,
18033 const gdb_byte **new_info_ptr,
18034 struct die_info *parent)
18035 {
18036 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18037 new_info_ptr, parent);
18038
18039 if (dwarf_die_debug)
18040 {
18041 fprintf_unfiltered (gdb_stdlog,
18042 "Read die from %s@0x%x of %s:\n",
18043 reader->die_section->get_name (),
18044 (unsigned) (info_ptr - reader->die_section->buffer),
18045 bfd_get_filename (reader->abfd));
18046 dump_die (die, dwarf_die_debug);
18047 }
18048
18049 return die;
18050 }
18051
18052 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18053 attributes.
18054 The caller is responsible for filling in the extra attributes
18055 and updating (*DIEP)->num_attrs.
18056 Set DIEP to point to a newly allocated die with its information,
18057 except for its child, sibling, and parent fields. */
18058
18059 static const gdb_byte *
18060 read_full_die_1 (const struct die_reader_specs *reader,
18061 struct die_info **diep, const gdb_byte *info_ptr,
18062 int num_extra_attrs)
18063 {
18064 unsigned int abbrev_number, bytes_read, i;
18065 struct abbrev_info *abbrev;
18066 struct die_info *die;
18067 struct dwarf2_cu *cu = reader->cu;
18068 bfd *abfd = reader->abfd;
18069
18070 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18071 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18072 info_ptr += bytes_read;
18073 if (!abbrev_number)
18074 {
18075 *diep = NULL;
18076 return info_ptr;
18077 }
18078
18079 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18080 if (!abbrev)
18081 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18082 abbrev_number,
18083 bfd_get_filename (abfd));
18084
18085 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18086 die->sect_off = sect_off;
18087 die->tag = abbrev->tag;
18088 die->abbrev = abbrev_number;
18089 die->has_children = abbrev->has_children;
18090
18091 /* Make the result usable.
18092 The caller needs to update num_attrs after adding the extra
18093 attributes. */
18094 die->num_attrs = abbrev->num_attrs;
18095
18096 std::vector<int> indexes_that_need_reprocess;
18097 for (i = 0; i < abbrev->num_attrs; ++i)
18098 {
18099 bool need_reprocess;
18100 info_ptr =
18101 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18102 info_ptr, &need_reprocess);
18103 if (need_reprocess)
18104 indexes_that_need_reprocess.push_back (i);
18105 }
18106
18107 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18108 if (attr != nullptr)
18109 cu->str_offsets_base = DW_UNSND (attr);
18110
18111 attr = die->attr (DW_AT_loclists_base);
18112 if (attr != nullptr)
18113 cu->loclist_base = DW_UNSND (attr);
18114
18115 auto maybe_addr_base = die->addr_base ();
18116 if (maybe_addr_base.has_value ())
18117 cu->addr_base = *maybe_addr_base;
18118 for (int index : indexes_that_need_reprocess)
18119 read_attribute_reprocess (reader, &die->attrs[index]);
18120 *diep = die;
18121 return info_ptr;
18122 }
18123
18124 /* Read a die and all its attributes.
18125 Set DIEP to point to a newly allocated die with its information,
18126 except for its child, sibling, and parent fields. */
18127
18128 static const gdb_byte *
18129 read_full_die (const struct die_reader_specs *reader,
18130 struct die_info **diep, const gdb_byte *info_ptr)
18131 {
18132 const gdb_byte *result;
18133
18134 result = read_full_die_1 (reader, diep, info_ptr, 0);
18135
18136 if (dwarf_die_debug)
18137 {
18138 fprintf_unfiltered (gdb_stdlog,
18139 "Read die from %s@0x%x of %s:\n",
18140 reader->die_section->get_name (),
18141 (unsigned) (info_ptr - reader->die_section->buffer),
18142 bfd_get_filename (reader->abfd));
18143 dump_die (*diep, dwarf_die_debug);
18144 }
18145
18146 return result;
18147 }
18148 \f
18149
18150 /* Returns nonzero if TAG represents a type that we might generate a partial
18151 symbol for. */
18152
18153 static int
18154 is_type_tag_for_partial (int tag)
18155 {
18156 switch (tag)
18157 {
18158 #if 0
18159 /* Some types that would be reasonable to generate partial symbols for,
18160 that we don't at present. */
18161 case DW_TAG_array_type:
18162 case DW_TAG_file_type:
18163 case DW_TAG_ptr_to_member_type:
18164 case DW_TAG_set_type:
18165 case DW_TAG_string_type:
18166 case DW_TAG_subroutine_type:
18167 #endif
18168 case DW_TAG_base_type:
18169 case DW_TAG_class_type:
18170 case DW_TAG_interface_type:
18171 case DW_TAG_enumeration_type:
18172 case DW_TAG_structure_type:
18173 case DW_TAG_subrange_type:
18174 case DW_TAG_typedef:
18175 case DW_TAG_union_type:
18176 return 1;
18177 default:
18178 return 0;
18179 }
18180 }
18181
18182 /* Load all DIEs that are interesting for partial symbols into memory. */
18183
18184 static struct partial_die_info *
18185 load_partial_dies (const struct die_reader_specs *reader,
18186 const gdb_byte *info_ptr, int building_psymtab)
18187 {
18188 struct dwarf2_cu *cu = reader->cu;
18189 struct objfile *objfile = cu->per_objfile->objfile;
18190 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18191 unsigned int bytes_read;
18192 unsigned int load_all = 0;
18193 int nesting_level = 1;
18194
18195 parent_die = NULL;
18196 last_die = NULL;
18197
18198 gdb_assert (cu->per_cu != NULL);
18199 if (cu->per_cu->load_all_dies)
18200 load_all = 1;
18201
18202 cu->partial_dies
18203 = htab_create_alloc_ex (cu->header.length / 12,
18204 partial_die_hash,
18205 partial_die_eq,
18206 NULL,
18207 &cu->comp_unit_obstack,
18208 hashtab_obstack_allocate,
18209 dummy_obstack_deallocate);
18210
18211 while (1)
18212 {
18213 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18214
18215 /* A NULL abbrev means the end of a series of children. */
18216 if (abbrev == NULL)
18217 {
18218 if (--nesting_level == 0)
18219 return first_die;
18220
18221 info_ptr += bytes_read;
18222 last_die = parent_die;
18223 parent_die = parent_die->die_parent;
18224 continue;
18225 }
18226
18227 /* Check for template arguments. We never save these; if
18228 they're seen, we just mark the parent, and go on our way. */
18229 if (parent_die != NULL
18230 && cu->language == language_cplus
18231 && (abbrev->tag == DW_TAG_template_type_param
18232 || abbrev->tag == DW_TAG_template_value_param))
18233 {
18234 parent_die->has_template_arguments = 1;
18235
18236 if (!load_all)
18237 {
18238 /* We don't need a partial DIE for the template argument. */
18239 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18240 continue;
18241 }
18242 }
18243
18244 /* We only recurse into c++ subprograms looking for template arguments.
18245 Skip their other children. */
18246 if (!load_all
18247 && cu->language == language_cplus
18248 && parent_die != NULL
18249 && parent_die->tag == DW_TAG_subprogram)
18250 {
18251 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18252 continue;
18253 }
18254
18255 /* Check whether this DIE is interesting enough to save. Normally
18256 we would not be interested in members here, but there may be
18257 later variables referencing them via DW_AT_specification (for
18258 static members). */
18259 if (!load_all
18260 && !is_type_tag_for_partial (abbrev->tag)
18261 && abbrev->tag != DW_TAG_constant
18262 && abbrev->tag != DW_TAG_enumerator
18263 && abbrev->tag != DW_TAG_subprogram
18264 && abbrev->tag != DW_TAG_inlined_subroutine
18265 && abbrev->tag != DW_TAG_lexical_block
18266 && abbrev->tag != DW_TAG_variable
18267 && abbrev->tag != DW_TAG_namespace
18268 && abbrev->tag != DW_TAG_module
18269 && abbrev->tag != DW_TAG_member
18270 && abbrev->tag != DW_TAG_imported_unit
18271 && abbrev->tag != DW_TAG_imported_declaration)
18272 {
18273 /* Otherwise we skip to the next sibling, if any. */
18274 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18275 continue;
18276 }
18277
18278 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18279 abbrev);
18280
18281 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18282
18283 /* This two-pass algorithm for processing partial symbols has a
18284 high cost in cache pressure. Thus, handle some simple cases
18285 here which cover the majority of C partial symbols. DIEs
18286 which neither have specification tags in them, nor could have
18287 specification tags elsewhere pointing at them, can simply be
18288 processed and discarded.
18289
18290 This segment is also optional; scan_partial_symbols and
18291 add_partial_symbol will handle these DIEs if we chain
18292 them in normally. When compilers which do not emit large
18293 quantities of duplicate debug information are more common,
18294 this code can probably be removed. */
18295
18296 /* Any complete simple types at the top level (pretty much all
18297 of them, for a language without namespaces), can be processed
18298 directly. */
18299 if (parent_die == NULL
18300 && pdi.has_specification == 0
18301 && pdi.is_declaration == 0
18302 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18303 || pdi.tag == DW_TAG_base_type
18304 || pdi.tag == DW_TAG_subrange_type))
18305 {
18306 if (building_psymtab && pdi.raw_name != NULL)
18307 add_partial_symbol (&pdi, cu);
18308
18309 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18310 continue;
18311 }
18312
18313 /* The exception for DW_TAG_typedef with has_children above is
18314 a workaround of GCC PR debug/47510. In the case of this complaint
18315 type_name_or_error will error on such types later.
18316
18317 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18318 it could not find the child DIEs referenced later, this is checked
18319 above. In correct DWARF DW_TAG_typedef should have no children. */
18320
18321 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18322 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18323 "- DIE at %s [in module %s]"),
18324 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18325
18326 /* If we're at the second level, and we're an enumerator, and
18327 our parent has no specification (meaning possibly lives in a
18328 namespace elsewhere), then we can add the partial symbol now
18329 instead of queueing it. */
18330 if (pdi.tag == DW_TAG_enumerator
18331 && parent_die != NULL
18332 && parent_die->die_parent == NULL
18333 && parent_die->tag == DW_TAG_enumeration_type
18334 && parent_die->has_specification == 0)
18335 {
18336 if (pdi.raw_name == NULL)
18337 complaint (_("malformed enumerator DIE ignored"));
18338 else if (building_psymtab)
18339 add_partial_symbol (&pdi, cu);
18340
18341 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18342 continue;
18343 }
18344
18345 struct partial_die_info *part_die
18346 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18347
18348 /* We'll save this DIE so link it in. */
18349 part_die->die_parent = parent_die;
18350 part_die->die_sibling = NULL;
18351 part_die->die_child = NULL;
18352
18353 if (last_die && last_die == parent_die)
18354 last_die->die_child = part_die;
18355 else if (last_die)
18356 last_die->die_sibling = part_die;
18357
18358 last_die = part_die;
18359
18360 if (first_die == NULL)
18361 first_die = part_die;
18362
18363 /* Maybe add the DIE to the hash table. Not all DIEs that we
18364 find interesting need to be in the hash table, because we
18365 also have the parent/sibling/child chains; only those that we
18366 might refer to by offset later during partial symbol reading.
18367
18368 For now this means things that might have be the target of a
18369 DW_AT_specification, DW_AT_abstract_origin, or
18370 DW_AT_extension. DW_AT_extension will refer only to
18371 namespaces; DW_AT_abstract_origin refers to functions (and
18372 many things under the function DIE, but we do not recurse
18373 into function DIEs during partial symbol reading) and
18374 possibly variables as well; DW_AT_specification refers to
18375 declarations. Declarations ought to have the DW_AT_declaration
18376 flag. It happens that GCC forgets to put it in sometimes, but
18377 only for functions, not for types.
18378
18379 Adding more things than necessary to the hash table is harmless
18380 except for the performance cost. Adding too few will result in
18381 wasted time in find_partial_die, when we reread the compilation
18382 unit with load_all_dies set. */
18383
18384 if (load_all
18385 || abbrev->tag == DW_TAG_constant
18386 || abbrev->tag == DW_TAG_subprogram
18387 || abbrev->tag == DW_TAG_variable
18388 || abbrev->tag == DW_TAG_namespace
18389 || part_die->is_declaration)
18390 {
18391 void **slot;
18392
18393 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18394 to_underlying (part_die->sect_off),
18395 INSERT);
18396 *slot = part_die;
18397 }
18398
18399 /* For some DIEs we want to follow their children (if any). For C
18400 we have no reason to follow the children of structures; for other
18401 languages we have to, so that we can get at method physnames
18402 to infer fully qualified class names, for DW_AT_specification,
18403 and for C++ template arguments. For C++, we also look one level
18404 inside functions to find template arguments (if the name of the
18405 function does not already contain the template arguments).
18406
18407 For Ada and Fortran, we need to scan the children of subprograms
18408 and lexical blocks as well because these languages allow the
18409 definition of nested entities that could be interesting for the
18410 debugger, such as nested subprograms for instance. */
18411 if (last_die->has_children
18412 && (load_all
18413 || last_die->tag == DW_TAG_namespace
18414 || last_die->tag == DW_TAG_module
18415 || last_die->tag == DW_TAG_enumeration_type
18416 || (cu->language == language_cplus
18417 && last_die->tag == DW_TAG_subprogram
18418 && (last_die->raw_name == NULL
18419 || strchr (last_die->raw_name, '<') == NULL))
18420 || (cu->language != language_c
18421 && (last_die->tag == DW_TAG_class_type
18422 || last_die->tag == DW_TAG_interface_type
18423 || last_die->tag == DW_TAG_structure_type
18424 || last_die->tag == DW_TAG_union_type))
18425 || ((cu->language == language_ada
18426 || cu->language == language_fortran)
18427 && (last_die->tag == DW_TAG_subprogram
18428 || last_die->tag == DW_TAG_lexical_block))))
18429 {
18430 nesting_level++;
18431 parent_die = last_die;
18432 continue;
18433 }
18434
18435 /* Otherwise we skip to the next sibling, if any. */
18436 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18437
18438 /* Back to the top, do it again. */
18439 }
18440 }
18441
18442 partial_die_info::partial_die_info (sect_offset sect_off_,
18443 struct abbrev_info *abbrev)
18444 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18445 {
18446 }
18447
18448 /* See class definition. */
18449
18450 const char *
18451 partial_die_info::name (dwarf2_cu *cu)
18452 {
18453 if (!canonical_name && raw_name != nullptr)
18454 {
18455 struct objfile *objfile = cu->per_objfile->objfile;
18456 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18457 canonical_name = 1;
18458 }
18459
18460 return raw_name;
18461 }
18462
18463 /* Read a minimal amount of information into the minimal die structure.
18464 INFO_PTR should point just after the initial uleb128 of a DIE. */
18465
18466 const gdb_byte *
18467 partial_die_info::read (const struct die_reader_specs *reader,
18468 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18469 {
18470 struct dwarf2_cu *cu = reader->cu;
18471 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18472 unsigned int i;
18473 int has_low_pc_attr = 0;
18474 int has_high_pc_attr = 0;
18475 int high_pc_relative = 0;
18476
18477 for (i = 0; i < abbrev.num_attrs; ++i)
18478 {
18479 attribute attr;
18480 bool need_reprocess;
18481 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18482 info_ptr, &need_reprocess);
18483 /* String and address offsets that need to do the reprocessing have
18484 already been read at this point, so there is no need to wait until
18485 the loop terminates to do the reprocessing. */
18486 if (need_reprocess)
18487 read_attribute_reprocess (reader, &attr);
18488 /* Store the data if it is of an attribute we want to keep in a
18489 partial symbol table. */
18490 switch (attr.name)
18491 {
18492 case DW_AT_name:
18493 switch (tag)
18494 {
18495 case DW_TAG_compile_unit:
18496 case DW_TAG_partial_unit:
18497 case DW_TAG_type_unit:
18498 /* Compilation units have a DW_AT_name that is a filename, not
18499 a source language identifier. */
18500 case DW_TAG_enumeration_type:
18501 case DW_TAG_enumerator:
18502 /* These tags always have simple identifiers already; no need
18503 to canonicalize them. */
18504 canonical_name = 1;
18505 raw_name = DW_STRING (&attr);
18506 break;
18507 default:
18508 canonical_name = 0;
18509 raw_name = DW_STRING (&attr);
18510 break;
18511 }
18512 break;
18513 case DW_AT_linkage_name:
18514 case DW_AT_MIPS_linkage_name:
18515 /* Note that both forms of linkage name might appear. We
18516 assume they will be the same, and we only store the last
18517 one we see. */
18518 linkage_name = attr.value_as_string ();
18519 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18520 See https://github.com/rust-lang/rust/issues/32925. */
18521 if (cu->language == language_rust && linkage_name != NULL
18522 && strchr (linkage_name, '{') != NULL)
18523 linkage_name = NULL;
18524 break;
18525 case DW_AT_low_pc:
18526 has_low_pc_attr = 1;
18527 lowpc = attr.value_as_address ();
18528 break;
18529 case DW_AT_high_pc:
18530 has_high_pc_attr = 1;
18531 highpc = attr.value_as_address ();
18532 if (cu->header.version >= 4 && attr.form_is_constant ())
18533 high_pc_relative = 1;
18534 break;
18535 case DW_AT_location:
18536 /* Support the .debug_loc offsets. */
18537 if (attr.form_is_block ())
18538 {
18539 d.locdesc = DW_BLOCK (&attr);
18540 }
18541 else if (attr.form_is_section_offset ())
18542 {
18543 dwarf2_complex_location_expr_complaint ();
18544 }
18545 else
18546 {
18547 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18548 "partial symbol information");
18549 }
18550 break;
18551 case DW_AT_external:
18552 is_external = DW_UNSND (&attr);
18553 break;
18554 case DW_AT_declaration:
18555 is_declaration = DW_UNSND (&attr);
18556 break;
18557 case DW_AT_type:
18558 has_type = 1;
18559 break;
18560 case DW_AT_abstract_origin:
18561 case DW_AT_specification:
18562 case DW_AT_extension:
18563 has_specification = 1;
18564 spec_offset = attr.get_ref_die_offset ();
18565 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18566 || cu->per_cu->is_dwz);
18567 break;
18568 case DW_AT_sibling:
18569 /* Ignore absolute siblings, they might point outside of
18570 the current compile unit. */
18571 if (attr.form == DW_FORM_ref_addr)
18572 complaint (_("ignoring absolute DW_AT_sibling"));
18573 else
18574 {
18575 const gdb_byte *buffer = reader->buffer;
18576 sect_offset off = attr.get_ref_die_offset ();
18577 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18578
18579 if (sibling_ptr < info_ptr)
18580 complaint (_("DW_AT_sibling points backwards"));
18581 else if (sibling_ptr > reader->buffer_end)
18582 reader->die_section->overflow_complaint ();
18583 else
18584 sibling = sibling_ptr;
18585 }
18586 break;
18587 case DW_AT_byte_size:
18588 has_byte_size = 1;
18589 break;
18590 case DW_AT_const_value:
18591 has_const_value = 1;
18592 break;
18593 case DW_AT_calling_convention:
18594 /* DWARF doesn't provide a way to identify a program's source-level
18595 entry point. DW_AT_calling_convention attributes are only meant
18596 to describe functions' calling conventions.
18597
18598 However, because it's a necessary piece of information in
18599 Fortran, and before DWARF 4 DW_CC_program was the only
18600 piece of debugging information whose definition refers to
18601 a 'main program' at all, several compilers marked Fortran
18602 main programs with DW_CC_program --- even when those
18603 functions use the standard calling conventions.
18604
18605 Although DWARF now specifies a way to provide this
18606 information, we support this practice for backward
18607 compatibility. */
18608 if (DW_UNSND (&attr) == DW_CC_program
18609 && cu->language == language_fortran)
18610 main_subprogram = 1;
18611 break;
18612 case DW_AT_inline:
18613 if (DW_UNSND (&attr) == DW_INL_inlined
18614 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18615 may_be_inlined = 1;
18616 break;
18617
18618 case DW_AT_import:
18619 if (tag == DW_TAG_imported_unit)
18620 {
18621 d.sect_off = attr.get_ref_die_offset ();
18622 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18623 || cu->per_cu->is_dwz);
18624 }
18625 break;
18626
18627 case DW_AT_main_subprogram:
18628 main_subprogram = DW_UNSND (&attr);
18629 break;
18630
18631 case DW_AT_ranges:
18632 {
18633 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18634 but that requires a full DIE, so instead we just
18635 reimplement it. */
18636 int need_ranges_base = tag != DW_TAG_compile_unit;
18637 unsigned int ranges_offset = (DW_UNSND (&attr)
18638 + (need_ranges_base
18639 ? cu->ranges_base
18640 : 0));
18641
18642 /* Value of the DW_AT_ranges attribute is the offset in the
18643 .debug_ranges section. */
18644 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18645 nullptr))
18646 has_pc_info = 1;
18647 }
18648 break;
18649
18650 default:
18651 break;
18652 }
18653 }
18654
18655 /* For Ada, if both the name and the linkage name appear, we prefer
18656 the latter. This lets "catch exception" work better, regardless
18657 of the order in which the name and linkage name were emitted.
18658 Really, though, this is just a workaround for the fact that gdb
18659 doesn't store both the name and the linkage name. */
18660 if (cu->language == language_ada && linkage_name != nullptr)
18661 raw_name = linkage_name;
18662
18663 if (high_pc_relative)
18664 highpc += lowpc;
18665
18666 if (has_low_pc_attr && has_high_pc_attr)
18667 {
18668 /* When using the GNU linker, .gnu.linkonce. sections are used to
18669 eliminate duplicate copies of functions and vtables and such.
18670 The linker will arbitrarily choose one and discard the others.
18671 The AT_*_pc values for such functions refer to local labels in
18672 these sections. If the section from that file was discarded, the
18673 labels are not in the output, so the relocs get a value of 0.
18674 If this is a discarded function, mark the pc bounds as invalid,
18675 so that GDB will ignore it. */
18676 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
18677 {
18678 struct objfile *objfile = per_objfile->objfile;
18679 struct gdbarch *gdbarch = objfile->arch ();
18680
18681 complaint (_("DW_AT_low_pc %s is zero "
18682 "for DIE at %s [in module %s]"),
18683 paddress (gdbarch, lowpc),
18684 sect_offset_str (sect_off),
18685 objfile_name (objfile));
18686 }
18687 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18688 else if (lowpc >= highpc)
18689 {
18690 struct objfile *objfile = per_objfile->objfile;
18691 struct gdbarch *gdbarch = objfile->arch ();
18692
18693 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18694 "for DIE at %s [in module %s]"),
18695 paddress (gdbarch, lowpc),
18696 paddress (gdbarch, highpc),
18697 sect_offset_str (sect_off),
18698 objfile_name (objfile));
18699 }
18700 else
18701 has_pc_info = 1;
18702 }
18703
18704 return info_ptr;
18705 }
18706
18707 /* Find a cached partial DIE at OFFSET in CU. */
18708
18709 struct partial_die_info *
18710 dwarf2_cu::find_partial_die (sect_offset sect_off)
18711 {
18712 struct partial_die_info *lookup_die = NULL;
18713 struct partial_die_info part_die (sect_off);
18714
18715 lookup_die = ((struct partial_die_info *)
18716 htab_find_with_hash (partial_dies, &part_die,
18717 to_underlying (sect_off)));
18718
18719 return lookup_die;
18720 }
18721
18722 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18723 except in the case of .debug_types DIEs which do not reference
18724 outside their CU (they do however referencing other types via
18725 DW_FORM_ref_sig8). */
18726
18727 static const struct cu_partial_die_info
18728 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18729 {
18730 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18731 struct objfile *objfile = per_objfile->objfile;
18732 struct partial_die_info *pd = NULL;
18733
18734 if (offset_in_dwz == cu->per_cu->is_dwz
18735 && cu->header.offset_in_cu_p (sect_off))
18736 {
18737 pd = cu->find_partial_die (sect_off);
18738 if (pd != NULL)
18739 return { cu, pd };
18740 /* We missed recording what we needed.
18741 Load all dies and try again. */
18742 }
18743 else
18744 {
18745 /* TUs don't reference other CUs/TUs (except via type signatures). */
18746 if (cu->per_cu->is_debug_types)
18747 {
18748 error (_("Dwarf Error: Type Unit at offset %s contains"
18749 " external reference to offset %s [in module %s].\n"),
18750 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18751 bfd_get_filename (objfile->obfd));
18752 }
18753 dwarf2_per_cu_data *per_cu
18754 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18755 per_objfile);
18756
18757 cu = per_objfile->get_cu (per_cu);
18758 if (cu == NULL || cu->partial_dies == NULL)
18759 load_partial_comp_unit (per_cu, per_objfile, nullptr);
18760
18761 cu = per_objfile->get_cu (per_cu);
18762
18763 cu->last_used = 0;
18764 pd = cu->find_partial_die (sect_off);
18765 }
18766
18767 /* If we didn't find it, and not all dies have been loaded,
18768 load them all and try again. */
18769
18770 if (pd == NULL && cu->per_cu->load_all_dies == 0)
18771 {
18772 cu->per_cu->load_all_dies = 1;
18773
18774 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18775 THIS_CU->cu may already be in use. So we can't just free it and
18776 replace its DIEs with the ones we read in. Instead, we leave those
18777 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18778 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18779 set. */
18780 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
18781
18782 pd = cu->find_partial_die (sect_off);
18783 }
18784
18785 if (pd == NULL)
18786 internal_error (__FILE__, __LINE__,
18787 _("could not find partial DIE %s "
18788 "in cache [from module %s]\n"),
18789 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18790 return { cu, pd };
18791 }
18792
18793 /* See if we can figure out if the class lives in a namespace. We do
18794 this by looking for a member function; its demangled name will
18795 contain namespace info, if there is any. */
18796
18797 static void
18798 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18799 struct dwarf2_cu *cu)
18800 {
18801 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18802 what template types look like, because the demangler
18803 frequently doesn't give the same name as the debug info. We
18804 could fix this by only using the demangled name to get the
18805 prefix (but see comment in read_structure_type). */
18806
18807 struct partial_die_info *real_pdi;
18808 struct partial_die_info *child_pdi;
18809
18810 /* If this DIE (this DIE's specification, if any) has a parent, then
18811 we should not do this. We'll prepend the parent's fully qualified
18812 name when we create the partial symbol. */
18813
18814 real_pdi = struct_pdi;
18815 while (real_pdi->has_specification)
18816 {
18817 auto res = find_partial_die (real_pdi->spec_offset,
18818 real_pdi->spec_is_dwz, cu);
18819 real_pdi = res.pdi;
18820 cu = res.cu;
18821 }
18822
18823 if (real_pdi->die_parent != NULL)
18824 return;
18825
18826 for (child_pdi = struct_pdi->die_child;
18827 child_pdi != NULL;
18828 child_pdi = child_pdi->die_sibling)
18829 {
18830 if (child_pdi->tag == DW_TAG_subprogram
18831 && child_pdi->linkage_name != NULL)
18832 {
18833 gdb::unique_xmalloc_ptr<char> actual_class_name
18834 (language_class_name_from_physname (cu->language_defn,
18835 child_pdi->linkage_name));
18836 if (actual_class_name != NULL)
18837 {
18838 struct objfile *objfile = cu->per_objfile->objfile;
18839 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
18840 struct_pdi->canonical_name = 1;
18841 }
18842 break;
18843 }
18844 }
18845 }
18846
18847 /* Return true if a DIE with TAG may have the DW_AT_const_value
18848 attribute. */
18849
18850 static bool
18851 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18852 {
18853 switch (tag)
18854 {
18855 case DW_TAG_constant:
18856 case DW_TAG_enumerator:
18857 case DW_TAG_formal_parameter:
18858 case DW_TAG_template_value_param:
18859 case DW_TAG_variable:
18860 return true;
18861 }
18862
18863 return false;
18864 }
18865
18866 void
18867 partial_die_info::fixup (struct dwarf2_cu *cu)
18868 {
18869 /* Once we've fixed up a die, there's no point in doing so again.
18870 This also avoids a memory leak if we were to call
18871 guess_partial_die_structure_name multiple times. */
18872 if (fixup_called)
18873 return;
18874
18875 /* If we found a reference attribute and the DIE has no name, try
18876 to find a name in the referred to DIE. */
18877
18878 if (raw_name == NULL && has_specification)
18879 {
18880 struct partial_die_info *spec_die;
18881
18882 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18883 spec_die = res.pdi;
18884 cu = res.cu;
18885
18886 spec_die->fixup (cu);
18887
18888 if (spec_die->raw_name)
18889 {
18890 raw_name = spec_die->raw_name;
18891 canonical_name = spec_die->canonical_name;
18892
18893 /* Copy DW_AT_external attribute if it is set. */
18894 if (spec_die->is_external)
18895 is_external = spec_die->is_external;
18896 }
18897 }
18898
18899 if (!has_const_value && has_specification
18900 && can_have_DW_AT_const_value_p (tag))
18901 {
18902 struct partial_die_info *spec_die;
18903
18904 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18905 spec_die = res.pdi;
18906 cu = res.cu;
18907
18908 spec_die->fixup (cu);
18909
18910 if (spec_die->has_const_value)
18911 {
18912 /* Copy DW_AT_const_value attribute if it is set. */
18913 has_const_value = spec_die->has_const_value;
18914 }
18915 }
18916
18917 /* Set default names for some unnamed DIEs. */
18918
18919 if (raw_name == NULL && tag == DW_TAG_namespace)
18920 {
18921 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
18922 canonical_name = 1;
18923 }
18924
18925 /* If there is no parent die to provide a namespace, and there are
18926 children, see if we can determine the namespace from their linkage
18927 name. */
18928 if (cu->language == language_cplus
18929 && !cu->per_objfile->per_bfd->types.empty ()
18930 && die_parent == NULL
18931 && has_children
18932 && (tag == DW_TAG_class_type
18933 || tag == DW_TAG_structure_type
18934 || tag == DW_TAG_union_type))
18935 guess_partial_die_structure_name (this, cu);
18936
18937 /* GCC might emit a nameless struct or union that has a linkage
18938 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18939 if (raw_name == NULL
18940 && (tag == DW_TAG_class_type
18941 || tag == DW_TAG_interface_type
18942 || tag == DW_TAG_structure_type
18943 || tag == DW_TAG_union_type)
18944 && linkage_name != NULL)
18945 {
18946 gdb::unique_xmalloc_ptr<char> demangled
18947 (gdb_demangle (linkage_name, DMGL_TYPES));
18948 if (demangled != nullptr)
18949 {
18950 const char *base;
18951
18952 /* Strip any leading namespaces/classes, keep only the base name.
18953 DW_AT_name for named DIEs does not contain the prefixes. */
18954 base = strrchr (demangled.get (), ':');
18955 if (base && base > demangled.get () && base[-1] == ':')
18956 base++;
18957 else
18958 base = demangled.get ();
18959
18960 struct objfile *objfile = cu->per_objfile->objfile;
18961 raw_name = objfile->intern (base);
18962 canonical_name = 1;
18963 }
18964 }
18965
18966 fixup_called = 1;
18967 }
18968
18969 /* Read the .debug_loclists header contents from the given SECTION in the
18970 HEADER. */
18971 static void
18972 read_loclist_header (struct loclist_header *header,
18973 struct dwarf2_section_info *section)
18974 {
18975 unsigned int bytes_read;
18976 bfd *abfd = section->get_bfd_owner ();
18977 const gdb_byte *info_ptr = section->buffer;
18978 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18979 info_ptr += bytes_read;
18980 header->version = read_2_bytes (abfd, info_ptr);
18981 info_ptr += 2;
18982 header->addr_size = read_1_byte (abfd, info_ptr);
18983 info_ptr += 1;
18984 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18985 info_ptr += 1;
18986 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18987 }
18988
18989 /* Return the DW_AT_loclists_base value for the CU. */
18990 static ULONGEST
18991 lookup_loclist_base (struct dwarf2_cu *cu)
18992 {
18993 /* For the .dwo unit, the loclist_base points to the first offset following
18994 the header. The header consists of the following entities-
18995 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18996 bit format)
18997 2. version (2 bytes)
18998 3. address size (1 byte)
18999 4. segment selector size (1 byte)
19000 5. offset entry count (4 bytes)
19001 These sizes are derived as per the DWARFv5 standard. */
19002 if (cu->dwo_unit != nullptr)
19003 {
19004 if (cu->header.initial_length_size == 4)
19005 return LOCLIST_HEADER_SIZE32;
19006 return LOCLIST_HEADER_SIZE64;
19007 }
19008 return cu->loclist_base;
19009 }
19010
19011 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19012 array of offsets in the .debug_loclists section. */
19013 static CORE_ADDR
19014 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19015 {
19016 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19017 struct objfile *objfile = per_objfile->objfile;
19018 bfd *abfd = objfile->obfd;
19019 ULONGEST loclist_base = lookup_loclist_base (cu);
19020 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19021
19022 section->read (objfile);
19023 if (section->buffer == NULL)
19024 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19025 "section [in module %s]"), objfile_name (objfile));
19026 struct loclist_header header;
19027 read_loclist_header (&header, section);
19028 if (loclist_index >= header.offset_entry_count)
19029 complaint (_("DW_FORM_loclistx pointing outside of "
19030 ".debug_loclists offset array [in module %s]"),
19031 objfile_name (objfile));
19032 if (loclist_base + loclist_index * cu->header.offset_size
19033 >= section->size)
19034 complaint (_("DW_FORM_loclistx pointing outside of "
19035 ".debug_loclists section [in module %s]"),
19036 objfile_name (objfile));
19037 const gdb_byte *info_ptr
19038 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19039
19040 if (cu->header.offset_size == 4)
19041 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19042 else
19043 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19044 }
19045
19046 /* Process the attributes that had to be skipped in the first round. These
19047 attributes are the ones that need str_offsets_base or addr_base attributes.
19048 They could not have been processed in the first round, because at the time
19049 the values of str_offsets_base or addr_base may not have been known. */
19050 static void
19051 read_attribute_reprocess (const struct die_reader_specs *reader,
19052 struct attribute *attr)
19053 {
19054 struct dwarf2_cu *cu = reader->cu;
19055 switch (attr->form)
19056 {
19057 case DW_FORM_addrx:
19058 case DW_FORM_GNU_addr_index:
19059 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19060 break;
19061 case DW_FORM_loclistx:
19062 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19063 break;
19064 case DW_FORM_strx:
19065 case DW_FORM_strx1:
19066 case DW_FORM_strx2:
19067 case DW_FORM_strx3:
19068 case DW_FORM_strx4:
19069 case DW_FORM_GNU_str_index:
19070 {
19071 unsigned int str_index = DW_UNSND (attr);
19072 if (reader->dwo_file != NULL)
19073 {
19074 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19075 DW_STRING_IS_CANONICAL (attr) = 0;
19076 }
19077 else
19078 {
19079 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19080 DW_STRING_IS_CANONICAL (attr) = 0;
19081 }
19082 break;
19083 }
19084 default:
19085 gdb_assert_not_reached (_("Unexpected DWARF form."));
19086 }
19087 }
19088
19089 /* Read an attribute value described by an attribute form. */
19090
19091 static const gdb_byte *
19092 read_attribute_value (const struct die_reader_specs *reader,
19093 struct attribute *attr, unsigned form,
19094 LONGEST implicit_const, const gdb_byte *info_ptr,
19095 bool *need_reprocess)
19096 {
19097 struct dwarf2_cu *cu = reader->cu;
19098 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19099 struct objfile *objfile = per_objfile->objfile;
19100 bfd *abfd = reader->abfd;
19101 struct comp_unit_head *cu_header = &cu->header;
19102 unsigned int bytes_read;
19103 struct dwarf_block *blk;
19104 *need_reprocess = false;
19105
19106 attr->form = (enum dwarf_form) form;
19107 switch (form)
19108 {
19109 case DW_FORM_ref_addr:
19110 if (cu->header.version == 2)
19111 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19112 &bytes_read);
19113 else
19114 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19115 &bytes_read);
19116 info_ptr += bytes_read;
19117 break;
19118 case DW_FORM_GNU_ref_alt:
19119 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19120 info_ptr += bytes_read;
19121 break;
19122 case DW_FORM_addr:
19123 {
19124 struct gdbarch *gdbarch = objfile->arch ();
19125 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19126 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19127 info_ptr += bytes_read;
19128 }
19129 break;
19130 case DW_FORM_block2:
19131 blk = dwarf_alloc_block (cu);
19132 blk->size = read_2_bytes (abfd, info_ptr);
19133 info_ptr += 2;
19134 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19135 info_ptr += blk->size;
19136 DW_BLOCK (attr) = blk;
19137 break;
19138 case DW_FORM_block4:
19139 blk = dwarf_alloc_block (cu);
19140 blk->size = read_4_bytes (abfd, info_ptr);
19141 info_ptr += 4;
19142 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19143 info_ptr += blk->size;
19144 DW_BLOCK (attr) = blk;
19145 break;
19146 case DW_FORM_data2:
19147 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19148 info_ptr += 2;
19149 break;
19150 case DW_FORM_data4:
19151 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19152 info_ptr += 4;
19153 break;
19154 case DW_FORM_data8:
19155 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19156 info_ptr += 8;
19157 break;
19158 case DW_FORM_data16:
19159 blk = dwarf_alloc_block (cu);
19160 blk->size = 16;
19161 blk->data = read_n_bytes (abfd, info_ptr, 16);
19162 info_ptr += 16;
19163 DW_BLOCK (attr) = blk;
19164 break;
19165 case DW_FORM_sec_offset:
19166 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19167 info_ptr += bytes_read;
19168 break;
19169 case DW_FORM_loclistx:
19170 {
19171 *need_reprocess = true;
19172 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19173 info_ptr += bytes_read;
19174 }
19175 break;
19176 case DW_FORM_string:
19177 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19178 DW_STRING_IS_CANONICAL (attr) = 0;
19179 info_ptr += bytes_read;
19180 break;
19181 case DW_FORM_strp:
19182 if (!cu->per_cu->is_dwz)
19183 {
19184 DW_STRING (attr) = read_indirect_string (per_objfile,
19185 abfd, info_ptr, cu_header,
19186 &bytes_read);
19187 DW_STRING_IS_CANONICAL (attr) = 0;
19188 info_ptr += bytes_read;
19189 break;
19190 }
19191 /* FALLTHROUGH */
19192 case DW_FORM_line_strp:
19193 if (!cu->per_cu->is_dwz)
19194 {
19195 DW_STRING (attr) = per_objfile->read_line_string (info_ptr, cu_header,
19196 &bytes_read);
19197 DW_STRING_IS_CANONICAL (attr) = 0;
19198 info_ptr += bytes_read;
19199 break;
19200 }
19201 /* FALLTHROUGH */
19202 case DW_FORM_GNU_strp_alt:
19203 {
19204 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
19205 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19206 &bytes_read);
19207
19208 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19209 DW_STRING_IS_CANONICAL (attr) = 0;
19210 info_ptr += bytes_read;
19211 }
19212 break;
19213 case DW_FORM_exprloc:
19214 case DW_FORM_block:
19215 blk = dwarf_alloc_block (cu);
19216 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19217 info_ptr += bytes_read;
19218 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19219 info_ptr += blk->size;
19220 DW_BLOCK (attr) = blk;
19221 break;
19222 case DW_FORM_block1:
19223 blk = dwarf_alloc_block (cu);
19224 blk->size = read_1_byte (abfd, info_ptr);
19225 info_ptr += 1;
19226 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19227 info_ptr += blk->size;
19228 DW_BLOCK (attr) = blk;
19229 break;
19230 case DW_FORM_data1:
19231 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19232 info_ptr += 1;
19233 break;
19234 case DW_FORM_flag:
19235 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19236 info_ptr += 1;
19237 break;
19238 case DW_FORM_flag_present:
19239 DW_UNSND (attr) = 1;
19240 break;
19241 case DW_FORM_sdata:
19242 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19243 info_ptr += bytes_read;
19244 break;
19245 case DW_FORM_udata:
19246 case DW_FORM_rnglistx:
19247 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19248 info_ptr += bytes_read;
19249 break;
19250 case DW_FORM_ref1:
19251 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19252 + read_1_byte (abfd, info_ptr));
19253 info_ptr += 1;
19254 break;
19255 case DW_FORM_ref2:
19256 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19257 + read_2_bytes (abfd, info_ptr));
19258 info_ptr += 2;
19259 break;
19260 case DW_FORM_ref4:
19261 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19262 + read_4_bytes (abfd, info_ptr));
19263 info_ptr += 4;
19264 break;
19265 case DW_FORM_ref8:
19266 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19267 + read_8_bytes (abfd, info_ptr));
19268 info_ptr += 8;
19269 break;
19270 case DW_FORM_ref_sig8:
19271 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19272 info_ptr += 8;
19273 break;
19274 case DW_FORM_ref_udata:
19275 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19276 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19277 info_ptr += bytes_read;
19278 break;
19279 case DW_FORM_indirect:
19280 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19281 info_ptr += bytes_read;
19282 if (form == DW_FORM_implicit_const)
19283 {
19284 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19285 info_ptr += bytes_read;
19286 }
19287 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19288 info_ptr, need_reprocess);
19289 break;
19290 case DW_FORM_implicit_const:
19291 DW_SND (attr) = implicit_const;
19292 break;
19293 case DW_FORM_addrx:
19294 case DW_FORM_GNU_addr_index:
19295 *need_reprocess = true;
19296 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19297 info_ptr += bytes_read;
19298 break;
19299 case DW_FORM_strx:
19300 case DW_FORM_strx1:
19301 case DW_FORM_strx2:
19302 case DW_FORM_strx3:
19303 case DW_FORM_strx4:
19304 case DW_FORM_GNU_str_index:
19305 {
19306 ULONGEST str_index;
19307 if (form == DW_FORM_strx1)
19308 {
19309 str_index = read_1_byte (abfd, info_ptr);
19310 info_ptr += 1;
19311 }
19312 else if (form == DW_FORM_strx2)
19313 {
19314 str_index = read_2_bytes (abfd, info_ptr);
19315 info_ptr += 2;
19316 }
19317 else if (form == DW_FORM_strx3)
19318 {
19319 str_index = read_3_bytes (abfd, info_ptr);
19320 info_ptr += 3;
19321 }
19322 else if (form == DW_FORM_strx4)
19323 {
19324 str_index = read_4_bytes (abfd, info_ptr);
19325 info_ptr += 4;
19326 }
19327 else
19328 {
19329 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19330 info_ptr += bytes_read;
19331 }
19332 *need_reprocess = true;
19333 DW_UNSND (attr) = str_index;
19334 }
19335 break;
19336 default:
19337 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19338 dwarf_form_name (form),
19339 bfd_get_filename (abfd));
19340 }
19341
19342 /* Super hack. */
19343 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19344 attr->form = DW_FORM_GNU_ref_alt;
19345
19346 /* We have seen instances where the compiler tried to emit a byte
19347 size attribute of -1 which ended up being encoded as an unsigned
19348 0xffffffff. Although 0xffffffff is technically a valid size value,
19349 an object of this size seems pretty unlikely so we can relatively
19350 safely treat these cases as if the size attribute was invalid and
19351 treat them as zero by default. */
19352 if (attr->name == DW_AT_byte_size
19353 && form == DW_FORM_data4
19354 && DW_UNSND (attr) >= 0xffffffff)
19355 {
19356 complaint
19357 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19358 hex_string (DW_UNSND (attr)));
19359 DW_UNSND (attr) = 0;
19360 }
19361
19362 return info_ptr;
19363 }
19364
19365 /* Read an attribute described by an abbreviated attribute. */
19366
19367 static const gdb_byte *
19368 read_attribute (const struct die_reader_specs *reader,
19369 struct attribute *attr, struct attr_abbrev *abbrev,
19370 const gdb_byte *info_ptr, bool *need_reprocess)
19371 {
19372 attr->name = abbrev->name;
19373 return read_attribute_value (reader, attr, abbrev->form,
19374 abbrev->implicit_const, info_ptr,
19375 need_reprocess);
19376 }
19377
19378 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19379
19380 static const char *
19381 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19382 LONGEST str_offset)
19383 {
19384 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19385 str_offset, "DW_FORM_strp");
19386 }
19387
19388 /* Return pointer to string at .debug_str offset as read from BUF.
19389 BUF is assumed to be in a compilation unit described by CU_HEADER.
19390 Return *BYTES_READ_PTR count of bytes read from BUF. */
19391
19392 static const char *
19393 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19394 const gdb_byte *buf,
19395 const struct comp_unit_head *cu_header,
19396 unsigned int *bytes_read_ptr)
19397 {
19398 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19399
19400 return read_indirect_string_at_offset (per_objfile, str_offset);
19401 }
19402
19403 /* See read.h. */
19404
19405 const char *
19406 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19407 const struct comp_unit_head *cu_header,
19408 unsigned int *bytes_read_ptr)
19409 {
19410 bfd *abfd = objfile->obfd;
19411 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19412
19413 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19414 }
19415
19416 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19417 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19418 ADDR_SIZE is the size of addresses from the CU header. */
19419
19420 static CORE_ADDR
19421 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19422 gdb::optional<ULONGEST> addr_base, int addr_size)
19423 {
19424 struct objfile *objfile = per_objfile->objfile;
19425 bfd *abfd = objfile->obfd;
19426 const gdb_byte *info_ptr;
19427 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19428
19429 per_objfile->per_bfd->addr.read (objfile);
19430 if (per_objfile->per_bfd->addr.buffer == NULL)
19431 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19432 objfile_name (objfile));
19433 if (addr_base_or_zero + addr_index * addr_size
19434 >= per_objfile->per_bfd->addr.size)
19435 error (_("DW_FORM_addr_index pointing outside of "
19436 ".debug_addr section [in module %s]"),
19437 objfile_name (objfile));
19438 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19439 + addr_index * addr_size);
19440 if (addr_size == 4)
19441 return bfd_get_32 (abfd, info_ptr);
19442 else
19443 return bfd_get_64 (abfd, info_ptr);
19444 }
19445
19446 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19447
19448 static CORE_ADDR
19449 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19450 {
19451 return read_addr_index_1 (cu->per_objfile, addr_index,
19452 cu->addr_base, cu->header.addr_size);
19453 }
19454
19455 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19456
19457 static CORE_ADDR
19458 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19459 unsigned int *bytes_read)
19460 {
19461 bfd *abfd = cu->per_objfile->objfile->obfd;
19462 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19463
19464 return read_addr_index (cu, addr_index);
19465 }
19466
19467 /* See read.h. */
19468
19469 CORE_ADDR
19470 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19471 dwarf2_per_objfile *per_objfile,
19472 unsigned int addr_index)
19473 {
19474 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
19475 gdb::optional<ULONGEST> addr_base;
19476 int addr_size;
19477
19478 /* We need addr_base and addr_size.
19479 If we don't have PER_CU->cu, we have to get it.
19480 Nasty, but the alternative is storing the needed info in PER_CU,
19481 which at this point doesn't seem justified: it's not clear how frequently
19482 it would get used and it would increase the size of every PER_CU.
19483 Entry points like dwarf2_per_cu_addr_size do a similar thing
19484 so we're not in uncharted territory here.
19485 Alas we need to be a bit more complicated as addr_base is contained
19486 in the DIE.
19487
19488 We don't need to read the entire CU(/TU).
19489 We just need the header and top level die.
19490
19491 IWBN to use the aging mechanism to let us lazily later discard the CU.
19492 For now we skip this optimization. */
19493
19494 if (cu != NULL)
19495 {
19496 addr_base = cu->addr_base;
19497 addr_size = cu->header.addr_size;
19498 }
19499 else
19500 {
19501 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
19502 addr_base = reader.cu->addr_base;
19503 addr_size = reader.cu->header.addr_size;
19504 }
19505
19506 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
19507 }
19508
19509 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19510 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19511 DWO file. */
19512
19513 static const char *
19514 read_str_index (struct dwarf2_cu *cu,
19515 struct dwarf2_section_info *str_section,
19516 struct dwarf2_section_info *str_offsets_section,
19517 ULONGEST str_offsets_base, ULONGEST str_index)
19518 {
19519 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19520 struct objfile *objfile = per_objfile->objfile;
19521 const char *objf_name = objfile_name (objfile);
19522 bfd *abfd = objfile->obfd;
19523 const gdb_byte *info_ptr;
19524 ULONGEST str_offset;
19525 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19526
19527 str_section->read (objfile);
19528 str_offsets_section->read (objfile);
19529 if (str_section->buffer == NULL)
19530 error (_("%s used without %s section"
19531 " in CU at offset %s [in module %s]"),
19532 form_name, str_section->get_name (),
19533 sect_offset_str (cu->header.sect_off), objf_name);
19534 if (str_offsets_section->buffer == NULL)
19535 error (_("%s used without %s section"
19536 " in CU at offset %s [in module %s]"),
19537 form_name, str_section->get_name (),
19538 sect_offset_str (cu->header.sect_off), objf_name);
19539 info_ptr = (str_offsets_section->buffer
19540 + str_offsets_base
19541 + str_index * cu->header.offset_size);
19542 if (cu->header.offset_size == 4)
19543 str_offset = bfd_get_32 (abfd, info_ptr);
19544 else
19545 str_offset = bfd_get_64 (abfd, info_ptr);
19546 if (str_offset >= str_section->size)
19547 error (_("Offset from %s pointing outside of"
19548 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19549 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19550 return (const char *) (str_section->buffer + str_offset);
19551 }
19552
19553 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19554
19555 static const char *
19556 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19557 {
19558 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19559 ? reader->cu->header.addr_size : 0;
19560 return read_str_index (reader->cu,
19561 &reader->dwo_file->sections.str,
19562 &reader->dwo_file->sections.str_offsets,
19563 str_offsets_base, str_index);
19564 }
19565
19566 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19567
19568 static const char *
19569 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19570 {
19571 struct objfile *objfile = cu->per_objfile->objfile;
19572 const char *objf_name = objfile_name (objfile);
19573 static const char form_name[] = "DW_FORM_GNU_str_index";
19574 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19575
19576 if (!cu->str_offsets_base.has_value ())
19577 error (_("%s used in Fission stub without %s"
19578 " in CU at offset 0x%lx [in module %s]"),
19579 form_name, str_offsets_attr_name,
19580 (long) cu->header.offset_size, objf_name);
19581
19582 return read_str_index (cu,
19583 &cu->per_objfile->per_bfd->str,
19584 &cu->per_objfile->per_bfd->str_offsets,
19585 *cu->str_offsets_base, str_index);
19586 }
19587
19588 /* Return the length of an LEB128 number in BUF. */
19589
19590 static int
19591 leb128_size (const gdb_byte *buf)
19592 {
19593 const gdb_byte *begin = buf;
19594 gdb_byte byte;
19595
19596 while (1)
19597 {
19598 byte = *buf++;
19599 if ((byte & 128) == 0)
19600 return buf - begin;
19601 }
19602 }
19603
19604 static void
19605 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19606 {
19607 switch (lang)
19608 {
19609 case DW_LANG_C89:
19610 case DW_LANG_C99:
19611 case DW_LANG_C11:
19612 case DW_LANG_C:
19613 case DW_LANG_UPC:
19614 cu->language = language_c;
19615 break;
19616 case DW_LANG_Java:
19617 case DW_LANG_C_plus_plus:
19618 case DW_LANG_C_plus_plus_11:
19619 case DW_LANG_C_plus_plus_14:
19620 cu->language = language_cplus;
19621 break;
19622 case DW_LANG_D:
19623 cu->language = language_d;
19624 break;
19625 case DW_LANG_Fortran77:
19626 case DW_LANG_Fortran90:
19627 case DW_LANG_Fortran95:
19628 case DW_LANG_Fortran03:
19629 case DW_LANG_Fortran08:
19630 cu->language = language_fortran;
19631 break;
19632 case DW_LANG_Go:
19633 cu->language = language_go;
19634 break;
19635 case DW_LANG_Mips_Assembler:
19636 cu->language = language_asm;
19637 break;
19638 case DW_LANG_Ada83:
19639 case DW_LANG_Ada95:
19640 cu->language = language_ada;
19641 break;
19642 case DW_LANG_Modula2:
19643 cu->language = language_m2;
19644 break;
19645 case DW_LANG_Pascal83:
19646 cu->language = language_pascal;
19647 break;
19648 case DW_LANG_ObjC:
19649 cu->language = language_objc;
19650 break;
19651 case DW_LANG_Rust:
19652 case DW_LANG_Rust_old:
19653 cu->language = language_rust;
19654 break;
19655 case DW_LANG_Cobol74:
19656 case DW_LANG_Cobol85:
19657 default:
19658 cu->language = language_minimal;
19659 break;
19660 }
19661 cu->language_defn = language_def (cu->language);
19662 }
19663
19664 /* Return the named attribute or NULL if not there. */
19665
19666 static struct attribute *
19667 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19668 {
19669 for (;;)
19670 {
19671 unsigned int i;
19672 struct attribute *spec = NULL;
19673
19674 for (i = 0; i < die->num_attrs; ++i)
19675 {
19676 if (die->attrs[i].name == name)
19677 return &die->attrs[i];
19678 if (die->attrs[i].name == DW_AT_specification
19679 || die->attrs[i].name == DW_AT_abstract_origin)
19680 spec = &die->attrs[i];
19681 }
19682
19683 if (!spec)
19684 break;
19685
19686 die = follow_die_ref (die, spec, &cu);
19687 }
19688
19689 return NULL;
19690 }
19691
19692 /* Return the string associated with a string-typed attribute, or NULL if it
19693 is either not found or is of an incorrect type. */
19694
19695 static const char *
19696 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19697 {
19698 struct attribute *attr;
19699 const char *str = NULL;
19700
19701 attr = dwarf2_attr (die, name, cu);
19702
19703 if (attr != NULL)
19704 {
19705 str = attr->value_as_string ();
19706 if (str == nullptr)
19707 complaint (_("string type expected for attribute %s for "
19708 "DIE at %s in module %s"),
19709 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19710 objfile_name (cu->per_objfile->objfile));
19711 }
19712
19713 return str;
19714 }
19715
19716 /* Return the dwo name or NULL if not present. If present, it is in either
19717 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19718 static const char *
19719 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19720 {
19721 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19722 if (dwo_name == nullptr)
19723 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19724 return dwo_name;
19725 }
19726
19727 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19728 and holds a non-zero value. This function should only be used for
19729 DW_FORM_flag or DW_FORM_flag_present attributes. */
19730
19731 static int
19732 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19733 {
19734 struct attribute *attr = dwarf2_attr (die, name, cu);
19735
19736 return (attr && DW_UNSND (attr));
19737 }
19738
19739 static int
19740 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19741 {
19742 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19743 which value is non-zero. However, we have to be careful with
19744 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19745 (via dwarf2_flag_true_p) follows this attribute. So we may
19746 end up accidently finding a declaration attribute that belongs
19747 to a different DIE referenced by the specification attribute,
19748 even though the given DIE does not have a declaration attribute. */
19749 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19750 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19751 }
19752
19753 /* Return the die giving the specification for DIE, if there is
19754 one. *SPEC_CU is the CU containing DIE on input, and the CU
19755 containing the return value on output. If there is no
19756 specification, but there is an abstract origin, that is
19757 returned. */
19758
19759 static struct die_info *
19760 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19761 {
19762 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19763 *spec_cu);
19764
19765 if (spec_attr == NULL)
19766 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19767
19768 if (spec_attr == NULL)
19769 return NULL;
19770 else
19771 return follow_die_ref (die, spec_attr, spec_cu);
19772 }
19773
19774 /* Stub for free_line_header to match void * callback types. */
19775
19776 static void
19777 free_line_header_voidp (void *arg)
19778 {
19779 struct line_header *lh = (struct line_header *) arg;
19780
19781 delete lh;
19782 }
19783
19784 /* A convenience function to find the proper .debug_line section for a CU. */
19785
19786 static struct dwarf2_section_info *
19787 get_debug_line_section (struct dwarf2_cu *cu)
19788 {
19789 struct dwarf2_section_info *section;
19790 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19791
19792 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19793 DWO file. */
19794 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19795 section = &cu->dwo_unit->dwo_file->sections.line;
19796 else if (cu->per_cu->is_dwz)
19797 {
19798 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
19799
19800 section = &dwz->line;
19801 }
19802 else
19803 section = &per_objfile->per_bfd->line;
19804
19805 return section;
19806 }
19807
19808 /* Read the statement program header starting at OFFSET in
19809 .debug_line, or .debug_line.dwo. Return a pointer
19810 to a struct line_header, allocated using xmalloc.
19811 Returns NULL if there is a problem reading the header, e.g., if it
19812 has a version we don't understand.
19813
19814 NOTE: the strings in the include directory and file name tables of
19815 the returned object point into the dwarf line section buffer,
19816 and must not be freed. */
19817
19818 static line_header_up
19819 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19820 {
19821 struct dwarf2_section_info *section;
19822 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19823
19824 section = get_debug_line_section (cu);
19825 section->read (per_objfile->objfile);
19826 if (section->buffer == NULL)
19827 {
19828 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19829 complaint (_("missing .debug_line.dwo section"));
19830 else
19831 complaint (_("missing .debug_line section"));
19832 return 0;
19833 }
19834
19835 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19836 per_objfile, section, &cu->header);
19837 }
19838
19839 /* Subroutine of dwarf_decode_lines to simplify it.
19840 Return the file name of the psymtab for the given file_entry.
19841 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19842 If space for the result is malloc'd, *NAME_HOLDER will be set.
19843 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19844
19845 static const char *
19846 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19847 const dwarf2_psymtab *pst,
19848 const char *comp_dir,
19849 gdb::unique_xmalloc_ptr<char> *name_holder)
19850 {
19851 const char *include_name = fe.name;
19852 const char *include_name_to_compare = include_name;
19853 const char *pst_filename;
19854 int file_is_pst;
19855
19856 const char *dir_name = fe.include_dir (lh);
19857
19858 gdb::unique_xmalloc_ptr<char> hold_compare;
19859 if (!IS_ABSOLUTE_PATH (include_name)
19860 && (dir_name != NULL || comp_dir != NULL))
19861 {
19862 /* Avoid creating a duplicate psymtab for PST.
19863 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19864 Before we do the comparison, however, we need to account
19865 for DIR_NAME and COMP_DIR.
19866 First prepend dir_name (if non-NULL). If we still don't
19867 have an absolute path prepend comp_dir (if non-NULL).
19868 However, the directory we record in the include-file's
19869 psymtab does not contain COMP_DIR (to match the
19870 corresponding symtab(s)).
19871
19872 Example:
19873
19874 bash$ cd /tmp
19875 bash$ gcc -g ./hello.c
19876 include_name = "hello.c"
19877 dir_name = "."
19878 DW_AT_comp_dir = comp_dir = "/tmp"
19879 DW_AT_name = "./hello.c"
19880
19881 */
19882
19883 if (dir_name != NULL)
19884 {
19885 name_holder->reset (concat (dir_name, SLASH_STRING,
19886 include_name, (char *) NULL));
19887 include_name = name_holder->get ();
19888 include_name_to_compare = include_name;
19889 }
19890 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19891 {
19892 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19893 include_name, (char *) NULL));
19894 include_name_to_compare = hold_compare.get ();
19895 }
19896 }
19897
19898 pst_filename = pst->filename;
19899 gdb::unique_xmalloc_ptr<char> copied_name;
19900 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19901 {
19902 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19903 pst_filename, (char *) NULL));
19904 pst_filename = copied_name.get ();
19905 }
19906
19907 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19908
19909 if (file_is_pst)
19910 return NULL;
19911 return include_name;
19912 }
19913
19914 /* State machine to track the state of the line number program. */
19915
19916 class lnp_state_machine
19917 {
19918 public:
19919 /* Initialize a machine state for the start of a line number
19920 program. */
19921 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19922 bool record_lines_p);
19923
19924 file_entry *current_file ()
19925 {
19926 /* lh->file_names is 0-based, but the file name numbers in the
19927 statement program are 1-based. */
19928 return m_line_header->file_name_at (m_file);
19929 }
19930
19931 /* Record the line in the state machine. END_SEQUENCE is true if
19932 we're processing the end of a sequence. */
19933 void record_line (bool end_sequence);
19934
19935 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19936 nop-out rest of the lines in this sequence. */
19937 void check_line_address (struct dwarf2_cu *cu,
19938 const gdb_byte *line_ptr,
19939 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19940
19941 void handle_set_discriminator (unsigned int discriminator)
19942 {
19943 m_discriminator = discriminator;
19944 m_line_has_non_zero_discriminator |= discriminator != 0;
19945 }
19946
19947 /* Handle DW_LNE_set_address. */
19948 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19949 {
19950 m_op_index = 0;
19951 address += baseaddr;
19952 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19953 }
19954
19955 /* Handle DW_LNS_advance_pc. */
19956 void handle_advance_pc (CORE_ADDR adjust);
19957
19958 /* Handle a special opcode. */
19959 void handle_special_opcode (unsigned char op_code);
19960
19961 /* Handle DW_LNS_advance_line. */
19962 void handle_advance_line (int line_delta)
19963 {
19964 advance_line (line_delta);
19965 }
19966
19967 /* Handle DW_LNS_set_file. */
19968 void handle_set_file (file_name_index file);
19969
19970 /* Handle DW_LNS_negate_stmt. */
19971 void handle_negate_stmt ()
19972 {
19973 m_is_stmt = !m_is_stmt;
19974 }
19975
19976 /* Handle DW_LNS_const_add_pc. */
19977 void handle_const_add_pc ();
19978
19979 /* Handle DW_LNS_fixed_advance_pc. */
19980 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19981 {
19982 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19983 m_op_index = 0;
19984 }
19985
19986 /* Handle DW_LNS_copy. */
19987 void handle_copy ()
19988 {
19989 record_line (false);
19990 m_discriminator = 0;
19991 }
19992
19993 /* Handle DW_LNE_end_sequence. */
19994 void handle_end_sequence ()
19995 {
19996 m_currently_recording_lines = true;
19997 }
19998
19999 private:
20000 /* Advance the line by LINE_DELTA. */
20001 void advance_line (int line_delta)
20002 {
20003 m_line += line_delta;
20004
20005 if (line_delta != 0)
20006 m_line_has_non_zero_discriminator = m_discriminator != 0;
20007 }
20008
20009 struct dwarf2_cu *m_cu;
20010
20011 gdbarch *m_gdbarch;
20012
20013 /* True if we're recording lines.
20014 Otherwise we're building partial symtabs and are just interested in
20015 finding include files mentioned by the line number program. */
20016 bool m_record_lines_p;
20017
20018 /* The line number header. */
20019 line_header *m_line_header;
20020
20021 /* These are part of the standard DWARF line number state machine,
20022 and initialized according to the DWARF spec. */
20023
20024 unsigned char m_op_index = 0;
20025 /* The line table index of the current file. */
20026 file_name_index m_file = 1;
20027 unsigned int m_line = 1;
20028
20029 /* These are initialized in the constructor. */
20030
20031 CORE_ADDR m_address;
20032 bool m_is_stmt;
20033 unsigned int m_discriminator;
20034
20035 /* Additional bits of state we need to track. */
20036
20037 /* The last file that we called dwarf2_start_subfile for.
20038 This is only used for TLLs. */
20039 unsigned int m_last_file = 0;
20040 /* The last file a line number was recorded for. */
20041 struct subfile *m_last_subfile = NULL;
20042
20043 /* When true, record the lines we decode. */
20044 bool m_currently_recording_lines = false;
20045
20046 /* The last line number that was recorded, used to coalesce
20047 consecutive entries for the same line. This can happen, for
20048 example, when discriminators are present. PR 17276. */
20049 unsigned int m_last_line = 0;
20050 bool m_line_has_non_zero_discriminator = false;
20051 };
20052
20053 void
20054 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20055 {
20056 CORE_ADDR addr_adj = (((m_op_index + adjust)
20057 / m_line_header->maximum_ops_per_instruction)
20058 * m_line_header->minimum_instruction_length);
20059 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20060 m_op_index = ((m_op_index + adjust)
20061 % m_line_header->maximum_ops_per_instruction);
20062 }
20063
20064 void
20065 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20066 {
20067 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20068 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20069 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20070 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20071 / m_line_header->maximum_ops_per_instruction)
20072 * m_line_header->minimum_instruction_length);
20073 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20074 m_op_index = ((m_op_index + adj_opcode_d)
20075 % m_line_header->maximum_ops_per_instruction);
20076
20077 int line_delta = m_line_header->line_base + adj_opcode_r;
20078 advance_line (line_delta);
20079 record_line (false);
20080 m_discriminator = 0;
20081 }
20082
20083 void
20084 lnp_state_machine::handle_set_file (file_name_index file)
20085 {
20086 m_file = file;
20087
20088 const file_entry *fe = current_file ();
20089 if (fe == NULL)
20090 dwarf2_debug_line_missing_file_complaint ();
20091 else if (m_record_lines_p)
20092 {
20093 const char *dir = fe->include_dir (m_line_header);
20094
20095 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20096 m_line_has_non_zero_discriminator = m_discriminator != 0;
20097 dwarf2_start_subfile (m_cu, fe->name, dir);
20098 }
20099 }
20100
20101 void
20102 lnp_state_machine::handle_const_add_pc ()
20103 {
20104 CORE_ADDR adjust
20105 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20106
20107 CORE_ADDR addr_adj
20108 = (((m_op_index + adjust)
20109 / m_line_header->maximum_ops_per_instruction)
20110 * m_line_header->minimum_instruction_length);
20111
20112 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20113 m_op_index = ((m_op_index + adjust)
20114 % m_line_header->maximum_ops_per_instruction);
20115 }
20116
20117 /* Return non-zero if we should add LINE to the line number table.
20118 LINE is the line to add, LAST_LINE is the last line that was added,
20119 LAST_SUBFILE is the subfile for LAST_LINE.
20120 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20121 had a non-zero discriminator.
20122
20123 We have to be careful in the presence of discriminators.
20124 E.g., for this line:
20125
20126 for (i = 0; i < 100000; i++);
20127
20128 clang can emit four line number entries for that one line,
20129 each with a different discriminator.
20130 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20131
20132 However, we want gdb to coalesce all four entries into one.
20133 Otherwise the user could stepi into the middle of the line and
20134 gdb would get confused about whether the pc really was in the
20135 middle of the line.
20136
20137 Things are further complicated by the fact that two consecutive
20138 line number entries for the same line is a heuristic used by gcc
20139 to denote the end of the prologue. So we can't just discard duplicate
20140 entries, we have to be selective about it. The heuristic we use is
20141 that we only collapse consecutive entries for the same line if at least
20142 one of those entries has a non-zero discriminator. PR 17276.
20143
20144 Note: Addresses in the line number state machine can never go backwards
20145 within one sequence, thus this coalescing is ok. */
20146
20147 static int
20148 dwarf_record_line_p (struct dwarf2_cu *cu,
20149 unsigned int line, unsigned int last_line,
20150 int line_has_non_zero_discriminator,
20151 struct subfile *last_subfile)
20152 {
20153 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20154 return 1;
20155 if (line != last_line)
20156 return 1;
20157 /* Same line for the same file that we've seen already.
20158 As a last check, for pr 17276, only record the line if the line
20159 has never had a non-zero discriminator. */
20160 if (!line_has_non_zero_discriminator)
20161 return 1;
20162 return 0;
20163 }
20164
20165 /* Use the CU's builder to record line number LINE beginning at
20166 address ADDRESS in the line table of subfile SUBFILE. */
20167
20168 static void
20169 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20170 unsigned int line, CORE_ADDR address, bool is_stmt,
20171 struct dwarf2_cu *cu)
20172 {
20173 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20174
20175 if (dwarf_line_debug)
20176 {
20177 fprintf_unfiltered (gdb_stdlog,
20178 "Recording line %u, file %s, address %s\n",
20179 line, lbasename (subfile->name),
20180 paddress (gdbarch, address));
20181 }
20182
20183 if (cu != nullptr)
20184 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20185 }
20186
20187 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20188 Mark the end of a set of line number records.
20189 The arguments are the same as for dwarf_record_line_1.
20190 If SUBFILE is NULL the request is ignored. */
20191
20192 static void
20193 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20194 CORE_ADDR address, struct dwarf2_cu *cu)
20195 {
20196 if (subfile == NULL)
20197 return;
20198
20199 if (dwarf_line_debug)
20200 {
20201 fprintf_unfiltered (gdb_stdlog,
20202 "Finishing current line, file %s, address %s\n",
20203 lbasename (subfile->name),
20204 paddress (gdbarch, address));
20205 }
20206
20207 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20208 }
20209
20210 void
20211 lnp_state_machine::record_line (bool end_sequence)
20212 {
20213 if (dwarf_line_debug)
20214 {
20215 fprintf_unfiltered (gdb_stdlog,
20216 "Processing actual line %u: file %u,"
20217 " address %s, is_stmt %u, discrim %u%s\n",
20218 m_line, m_file,
20219 paddress (m_gdbarch, m_address),
20220 m_is_stmt, m_discriminator,
20221 (end_sequence ? "\t(end sequence)" : ""));
20222 }
20223
20224 file_entry *fe = current_file ();
20225
20226 if (fe == NULL)
20227 dwarf2_debug_line_missing_file_complaint ();
20228 /* For now we ignore lines not starting on an instruction boundary.
20229 But not when processing end_sequence for compatibility with the
20230 previous version of the code. */
20231 else if (m_op_index == 0 || end_sequence)
20232 {
20233 fe->included_p = 1;
20234 if (m_record_lines_p)
20235 {
20236 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20237 || end_sequence)
20238 {
20239 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20240 m_currently_recording_lines ? m_cu : nullptr);
20241 }
20242
20243 if (!end_sequence)
20244 {
20245 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20246
20247 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20248 m_line_has_non_zero_discriminator,
20249 m_last_subfile))
20250 {
20251 buildsym_compunit *builder = m_cu->get_builder ();
20252 dwarf_record_line_1 (m_gdbarch,
20253 builder->get_current_subfile (),
20254 m_line, m_address, is_stmt,
20255 m_currently_recording_lines ? m_cu : nullptr);
20256 }
20257 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20258 m_last_line = m_line;
20259 }
20260 }
20261 }
20262 }
20263
20264 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20265 line_header *lh, bool record_lines_p)
20266 {
20267 m_cu = cu;
20268 m_gdbarch = arch;
20269 m_record_lines_p = record_lines_p;
20270 m_line_header = lh;
20271
20272 m_currently_recording_lines = true;
20273
20274 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20275 was a line entry for it so that the backend has a chance to adjust it
20276 and also record it in case it needs it. This is currently used by MIPS
20277 code, cf. `mips_adjust_dwarf2_line'. */
20278 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20279 m_is_stmt = lh->default_is_stmt;
20280 m_discriminator = 0;
20281 }
20282
20283 void
20284 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20285 const gdb_byte *line_ptr,
20286 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20287 {
20288 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20289 the pc range of the CU. However, we restrict the test to only ADDRESS
20290 values of zero to preserve GDB's previous behaviour which is to handle
20291 the specific case of a function being GC'd by the linker. */
20292
20293 if (address == 0 && address < unrelocated_lowpc)
20294 {
20295 /* This line table is for a function which has been
20296 GCd by the linker. Ignore it. PR gdb/12528 */
20297
20298 struct objfile *objfile = cu->per_objfile->objfile;
20299 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20300
20301 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20302 line_offset, objfile_name (objfile));
20303 m_currently_recording_lines = false;
20304 /* Note: m_currently_recording_lines is left as false until we see
20305 DW_LNE_end_sequence. */
20306 }
20307 }
20308
20309 /* Subroutine of dwarf_decode_lines to simplify it.
20310 Process the line number information in LH.
20311 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20312 program in order to set included_p for every referenced header. */
20313
20314 static void
20315 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20316 const int decode_for_pst_p, CORE_ADDR lowpc)
20317 {
20318 const gdb_byte *line_ptr, *extended_end;
20319 const gdb_byte *line_end;
20320 unsigned int bytes_read, extended_len;
20321 unsigned char op_code, extended_op;
20322 CORE_ADDR baseaddr;
20323 struct objfile *objfile = cu->per_objfile->objfile;
20324 bfd *abfd = objfile->obfd;
20325 struct gdbarch *gdbarch = objfile->arch ();
20326 /* True if we're recording line info (as opposed to building partial
20327 symtabs and just interested in finding include files mentioned by
20328 the line number program). */
20329 bool record_lines_p = !decode_for_pst_p;
20330
20331 baseaddr = objfile->text_section_offset ();
20332
20333 line_ptr = lh->statement_program_start;
20334 line_end = lh->statement_program_end;
20335
20336 /* Read the statement sequences until there's nothing left. */
20337 while (line_ptr < line_end)
20338 {
20339 /* The DWARF line number program state machine. Reset the state
20340 machine at the start of each sequence. */
20341 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20342 bool end_sequence = false;
20343
20344 if (record_lines_p)
20345 {
20346 /* Start a subfile for the current file of the state
20347 machine. */
20348 const file_entry *fe = state_machine.current_file ();
20349
20350 if (fe != NULL)
20351 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20352 }
20353
20354 /* Decode the table. */
20355 while (line_ptr < line_end && !end_sequence)
20356 {
20357 op_code = read_1_byte (abfd, line_ptr);
20358 line_ptr += 1;
20359
20360 if (op_code >= lh->opcode_base)
20361 {
20362 /* Special opcode. */
20363 state_machine.handle_special_opcode (op_code);
20364 }
20365 else switch (op_code)
20366 {
20367 case DW_LNS_extended_op:
20368 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20369 &bytes_read);
20370 line_ptr += bytes_read;
20371 extended_end = line_ptr + extended_len;
20372 extended_op = read_1_byte (abfd, line_ptr);
20373 line_ptr += 1;
20374 switch (extended_op)
20375 {
20376 case DW_LNE_end_sequence:
20377 state_machine.handle_end_sequence ();
20378 end_sequence = true;
20379 break;
20380 case DW_LNE_set_address:
20381 {
20382 CORE_ADDR address
20383 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20384 line_ptr += bytes_read;
20385
20386 state_machine.check_line_address (cu, line_ptr,
20387 lowpc - baseaddr, address);
20388 state_machine.handle_set_address (baseaddr, address);
20389 }
20390 break;
20391 case DW_LNE_define_file:
20392 {
20393 const char *cur_file;
20394 unsigned int mod_time, length;
20395 dir_index dindex;
20396
20397 cur_file = read_direct_string (abfd, line_ptr,
20398 &bytes_read);
20399 line_ptr += bytes_read;
20400 dindex = (dir_index)
20401 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20402 line_ptr += bytes_read;
20403 mod_time =
20404 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20405 line_ptr += bytes_read;
20406 length =
20407 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20408 line_ptr += bytes_read;
20409 lh->add_file_name (cur_file, dindex, mod_time, length);
20410 }
20411 break;
20412 case DW_LNE_set_discriminator:
20413 {
20414 /* The discriminator is not interesting to the
20415 debugger; just ignore it. We still need to
20416 check its value though:
20417 if there are consecutive entries for the same
20418 (non-prologue) line we want to coalesce them.
20419 PR 17276. */
20420 unsigned int discr
20421 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20422 line_ptr += bytes_read;
20423
20424 state_machine.handle_set_discriminator (discr);
20425 }
20426 break;
20427 default:
20428 complaint (_("mangled .debug_line section"));
20429 return;
20430 }
20431 /* Make sure that we parsed the extended op correctly. If e.g.
20432 we expected a different address size than the producer used,
20433 we may have read the wrong number of bytes. */
20434 if (line_ptr != extended_end)
20435 {
20436 complaint (_("mangled .debug_line section"));
20437 return;
20438 }
20439 break;
20440 case DW_LNS_copy:
20441 state_machine.handle_copy ();
20442 break;
20443 case DW_LNS_advance_pc:
20444 {
20445 CORE_ADDR adjust
20446 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20447 line_ptr += bytes_read;
20448
20449 state_machine.handle_advance_pc (adjust);
20450 }
20451 break;
20452 case DW_LNS_advance_line:
20453 {
20454 int line_delta
20455 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20456 line_ptr += bytes_read;
20457
20458 state_machine.handle_advance_line (line_delta);
20459 }
20460 break;
20461 case DW_LNS_set_file:
20462 {
20463 file_name_index file
20464 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20465 &bytes_read);
20466 line_ptr += bytes_read;
20467
20468 state_machine.handle_set_file (file);
20469 }
20470 break;
20471 case DW_LNS_set_column:
20472 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20473 line_ptr += bytes_read;
20474 break;
20475 case DW_LNS_negate_stmt:
20476 state_machine.handle_negate_stmt ();
20477 break;
20478 case DW_LNS_set_basic_block:
20479 break;
20480 /* Add to the address register of the state machine the
20481 address increment value corresponding to special opcode
20482 255. I.e., this value is scaled by the minimum
20483 instruction length since special opcode 255 would have
20484 scaled the increment. */
20485 case DW_LNS_const_add_pc:
20486 state_machine.handle_const_add_pc ();
20487 break;
20488 case DW_LNS_fixed_advance_pc:
20489 {
20490 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20491 line_ptr += 2;
20492
20493 state_machine.handle_fixed_advance_pc (addr_adj);
20494 }
20495 break;
20496 default:
20497 {
20498 /* Unknown standard opcode, ignore it. */
20499 int i;
20500
20501 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20502 {
20503 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20504 line_ptr += bytes_read;
20505 }
20506 }
20507 }
20508 }
20509
20510 if (!end_sequence)
20511 dwarf2_debug_line_missing_end_sequence_complaint ();
20512
20513 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20514 in which case we still finish recording the last line). */
20515 state_machine.record_line (true);
20516 }
20517 }
20518
20519 /* Decode the Line Number Program (LNP) for the given line_header
20520 structure and CU. The actual information extracted and the type
20521 of structures created from the LNP depends on the value of PST.
20522
20523 1. If PST is NULL, then this procedure uses the data from the program
20524 to create all necessary symbol tables, and their linetables.
20525
20526 2. If PST is not NULL, this procedure reads the program to determine
20527 the list of files included by the unit represented by PST, and
20528 builds all the associated partial symbol tables.
20529
20530 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20531 It is used for relative paths in the line table.
20532 NOTE: When processing partial symtabs (pst != NULL),
20533 comp_dir == pst->dirname.
20534
20535 NOTE: It is important that psymtabs have the same file name (via strcmp)
20536 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20537 symtab we don't use it in the name of the psymtabs we create.
20538 E.g. expand_line_sal requires this when finding psymtabs to expand.
20539 A good testcase for this is mb-inline.exp.
20540
20541 LOWPC is the lowest address in CU (or 0 if not known).
20542
20543 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20544 for its PC<->lines mapping information. Otherwise only the filename
20545 table is read in. */
20546
20547 static void
20548 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20549 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20550 CORE_ADDR lowpc, int decode_mapping)
20551 {
20552 struct objfile *objfile = cu->per_objfile->objfile;
20553 const int decode_for_pst_p = (pst != NULL);
20554
20555 if (decode_mapping)
20556 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20557
20558 if (decode_for_pst_p)
20559 {
20560 /* Now that we're done scanning the Line Header Program, we can
20561 create the psymtab of each included file. */
20562 for (auto &file_entry : lh->file_names ())
20563 if (file_entry.included_p == 1)
20564 {
20565 gdb::unique_xmalloc_ptr<char> name_holder;
20566 const char *include_name =
20567 psymtab_include_file_name (lh, file_entry, pst,
20568 comp_dir, &name_holder);
20569 if (include_name != NULL)
20570 dwarf2_create_include_psymtab (include_name, pst, objfile);
20571 }
20572 }
20573 else
20574 {
20575 /* Make sure a symtab is created for every file, even files
20576 which contain only variables (i.e. no code with associated
20577 line numbers). */
20578 buildsym_compunit *builder = cu->get_builder ();
20579 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20580
20581 for (auto &fe : lh->file_names ())
20582 {
20583 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20584 if (builder->get_current_subfile ()->symtab == NULL)
20585 {
20586 builder->get_current_subfile ()->symtab
20587 = allocate_symtab (cust,
20588 builder->get_current_subfile ()->name);
20589 }
20590 fe.symtab = builder->get_current_subfile ()->symtab;
20591 }
20592 }
20593 }
20594
20595 /* Start a subfile for DWARF. FILENAME is the name of the file and
20596 DIRNAME the name of the source directory which contains FILENAME
20597 or NULL if not known.
20598 This routine tries to keep line numbers from identical absolute and
20599 relative file names in a common subfile.
20600
20601 Using the `list' example from the GDB testsuite, which resides in
20602 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20603 of /srcdir/list0.c yields the following debugging information for list0.c:
20604
20605 DW_AT_name: /srcdir/list0.c
20606 DW_AT_comp_dir: /compdir
20607 files.files[0].name: list0.h
20608 files.files[0].dir: /srcdir
20609 files.files[1].name: list0.c
20610 files.files[1].dir: /srcdir
20611
20612 The line number information for list0.c has to end up in a single
20613 subfile, so that `break /srcdir/list0.c:1' works as expected.
20614 start_subfile will ensure that this happens provided that we pass the
20615 concatenation of files.files[1].dir and files.files[1].name as the
20616 subfile's name. */
20617
20618 static void
20619 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20620 const char *dirname)
20621 {
20622 gdb::unique_xmalloc_ptr<char> copy;
20623
20624 /* In order not to lose the line information directory,
20625 we concatenate it to the filename when it makes sense.
20626 Note that the Dwarf3 standard says (speaking of filenames in line
20627 information): ``The directory index is ignored for file names
20628 that represent full path names''. Thus ignoring dirname in the
20629 `else' branch below isn't an issue. */
20630
20631 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20632 {
20633 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20634 filename = copy.get ();
20635 }
20636
20637 cu->get_builder ()->start_subfile (filename);
20638 }
20639
20640 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20641 buildsym_compunit constructor. */
20642
20643 struct compunit_symtab *
20644 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20645 CORE_ADDR low_pc)
20646 {
20647 gdb_assert (m_builder == nullptr);
20648
20649 m_builder.reset (new struct buildsym_compunit
20650 (this->per_objfile->objfile,
20651 name, comp_dir, language, low_pc));
20652
20653 list_in_scope = get_builder ()->get_file_symbols ();
20654
20655 get_builder ()->record_debugformat ("DWARF 2");
20656 get_builder ()->record_producer (producer);
20657
20658 processing_has_namespace_info = false;
20659
20660 return get_builder ()->get_compunit_symtab ();
20661 }
20662
20663 static void
20664 var_decode_location (struct attribute *attr, struct symbol *sym,
20665 struct dwarf2_cu *cu)
20666 {
20667 struct objfile *objfile = cu->per_objfile->objfile;
20668 struct comp_unit_head *cu_header = &cu->header;
20669
20670 /* NOTE drow/2003-01-30: There used to be a comment and some special
20671 code here to turn a symbol with DW_AT_external and a
20672 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20673 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20674 with some versions of binutils) where shared libraries could have
20675 relocations against symbols in their debug information - the
20676 minimal symbol would have the right address, but the debug info
20677 would not. It's no longer necessary, because we will explicitly
20678 apply relocations when we read in the debug information now. */
20679
20680 /* A DW_AT_location attribute with no contents indicates that a
20681 variable has been optimized away. */
20682 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20683 {
20684 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20685 return;
20686 }
20687
20688 /* Handle one degenerate form of location expression specially, to
20689 preserve GDB's previous behavior when section offsets are
20690 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20691 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20692
20693 if (attr->form_is_block ()
20694 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20695 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20696 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20697 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20698 && (DW_BLOCK (attr)->size
20699 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20700 {
20701 unsigned int dummy;
20702
20703 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20704 SET_SYMBOL_VALUE_ADDRESS
20705 (sym, cu->header.read_address (objfile->obfd,
20706 DW_BLOCK (attr)->data + 1,
20707 &dummy));
20708 else
20709 SET_SYMBOL_VALUE_ADDRESS
20710 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20711 &dummy));
20712 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20713 fixup_symbol_section (sym, objfile);
20714 SET_SYMBOL_VALUE_ADDRESS
20715 (sym,
20716 SYMBOL_VALUE_ADDRESS (sym)
20717 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20718 return;
20719 }
20720
20721 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20722 expression evaluator, and use LOC_COMPUTED only when necessary
20723 (i.e. when the value of a register or memory location is
20724 referenced, or a thread-local block, etc.). Then again, it might
20725 not be worthwhile. I'm assuming that it isn't unless performance
20726 or memory numbers show me otherwise. */
20727
20728 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20729
20730 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20731 cu->has_loclist = true;
20732 }
20733
20734 /* Given a pointer to a DWARF information entry, figure out if we need
20735 to make a symbol table entry for it, and if so, create a new entry
20736 and return a pointer to it.
20737 If TYPE is NULL, determine symbol type from the die, otherwise
20738 used the passed type.
20739 If SPACE is not NULL, use it to hold the new symbol. If it is
20740 NULL, allocate a new symbol on the objfile's obstack. */
20741
20742 static struct symbol *
20743 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20744 struct symbol *space)
20745 {
20746 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20747 struct objfile *objfile = per_objfile->objfile;
20748 struct gdbarch *gdbarch = objfile->arch ();
20749 struct symbol *sym = NULL;
20750 const char *name;
20751 struct attribute *attr = NULL;
20752 struct attribute *attr2 = NULL;
20753 CORE_ADDR baseaddr;
20754 struct pending **list_to_add = NULL;
20755
20756 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20757
20758 baseaddr = objfile->text_section_offset ();
20759
20760 name = dwarf2_name (die, cu);
20761 if (name)
20762 {
20763 int suppress_add = 0;
20764
20765 if (space)
20766 sym = space;
20767 else
20768 sym = new (&objfile->objfile_obstack) symbol;
20769 OBJSTAT (objfile, n_syms++);
20770
20771 /* Cache this symbol's name and the name's demangled form (if any). */
20772 sym->set_language (cu->language, &objfile->objfile_obstack);
20773 /* Fortran does not have mangling standard and the mangling does differ
20774 between gfortran, iFort etc. */
20775 const char *physname
20776 = (cu->language == language_fortran
20777 ? dwarf2_full_name (name, die, cu)
20778 : dwarf2_physname (name, die, cu));
20779 const char *linkagename = dw2_linkage_name (die, cu);
20780
20781 if (linkagename == nullptr || cu->language == language_ada)
20782 sym->set_linkage_name (physname);
20783 else
20784 {
20785 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20786 sym->set_linkage_name (linkagename);
20787 }
20788
20789 /* Default assumptions.
20790 Use the passed type or decode it from the die. */
20791 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20792 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20793 if (type != NULL)
20794 SYMBOL_TYPE (sym) = type;
20795 else
20796 SYMBOL_TYPE (sym) = die_type (die, cu);
20797 attr = dwarf2_attr (die,
20798 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20799 cu);
20800 if (attr != nullptr)
20801 {
20802 SYMBOL_LINE (sym) = DW_UNSND (attr);
20803 }
20804
20805 attr = dwarf2_attr (die,
20806 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20807 cu);
20808 if (attr != nullptr)
20809 {
20810 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20811 struct file_entry *fe;
20812
20813 if (cu->line_header != NULL)
20814 fe = cu->line_header->file_name_at (file_index);
20815 else
20816 fe = NULL;
20817
20818 if (fe == NULL)
20819 complaint (_("file index out of range"));
20820 else
20821 symbol_set_symtab (sym, fe->symtab);
20822 }
20823
20824 switch (die->tag)
20825 {
20826 case DW_TAG_label:
20827 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20828 if (attr != nullptr)
20829 {
20830 CORE_ADDR addr;
20831
20832 addr = attr->value_as_address ();
20833 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20834 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20835 }
20836 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20837 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20838 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20839 add_symbol_to_list (sym, cu->list_in_scope);
20840 break;
20841 case DW_TAG_subprogram:
20842 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20843 finish_block. */
20844 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20845 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20846 if ((attr2 && (DW_UNSND (attr2) != 0))
20847 || cu->language == language_ada
20848 || cu->language == language_fortran)
20849 {
20850 /* Subprograms marked external are stored as a global symbol.
20851 Ada and Fortran subprograms, whether marked external or
20852 not, are always stored as a global symbol, because we want
20853 to be able to access them globally. For instance, we want
20854 to be able to break on a nested subprogram without having
20855 to specify the context. */
20856 list_to_add = cu->get_builder ()->get_global_symbols ();
20857 }
20858 else
20859 {
20860 list_to_add = cu->list_in_scope;
20861 }
20862 break;
20863 case DW_TAG_inlined_subroutine:
20864 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20865 finish_block. */
20866 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20867 SYMBOL_INLINED (sym) = 1;
20868 list_to_add = cu->list_in_scope;
20869 break;
20870 case DW_TAG_template_value_param:
20871 suppress_add = 1;
20872 /* Fall through. */
20873 case DW_TAG_constant:
20874 case DW_TAG_variable:
20875 case DW_TAG_member:
20876 /* Compilation with minimal debug info may result in
20877 variables with missing type entries. Change the
20878 misleading `void' type to something sensible. */
20879 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20880 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20881
20882 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20883 /* In the case of DW_TAG_member, we should only be called for
20884 static const members. */
20885 if (die->tag == DW_TAG_member)
20886 {
20887 /* dwarf2_add_field uses die_is_declaration,
20888 so we do the same. */
20889 gdb_assert (die_is_declaration (die, cu));
20890 gdb_assert (attr);
20891 }
20892 if (attr != nullptr)
20893 {
20894 dwarf2_const_value (attr, sym, cu);
20895 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20896 if (!suppress_add)
20897 {
20898 if (attr2 && (DW_UNSND (attr2) != 0))
20899 list_to_add = cu->get_builder ()->get_global_symbols ();
20900 else
20901 list_to_add = cu->list_in_scope;
20902 }
20903 break;
20904 }
20905 attr = dwarf2_attr (die, DW_AT_location, cu);
20906 if (attr != nullptr)
20907 {
20908 var_decode_location (attr, sym, cu);
20909 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20910
20911 /* Fortran explicitly imports any global symbols to the local
20912 scope by DW_TAG_common_block. */
20913 if (cu->language == language_fortran && die->parent
20914 && die->parent->tag == DW_TAG_common_block)
20915 attr2 = NULL;
20916
20917 if (SYMBOL_CLASS (sym) == LOC_STATIC
20918 && SYMBOL_VALUE_ADDRESS (sym) == 0
20919 && !per_objfile->per_bfd->has_section_at_zero)
20920 {
20921 /* When a static variable is eliminated by the linker,
20922 the corresponding debug information is not stripped
20923 out, but the variable address is set to null;
20924 do not add such variables into symbol table. */
20925 }
20926 else if (attr2 && (DW_UNSND (attr2) != 0))
20927 {
20928 if (SYMBOL_CLASS (sym) == LOC_STATIC
20929 && (objfile->flags & OBJF_MAINLINE) == 0
20930 && per_objfile->per_bfd->can_copy)
20931 {
20932 /* A global static variable might be subject to
20933 copy relocation. We first check for a local
20934 minsym, though, because maybe the symbol was
20935 marked hidden, in which case this would not
20936 apply. */
20937 bound_minimal_symbol found
20938 = (lookup_minimal_symbol_linkage
20939 (sym->linkage_name (), objfile));
20940 if (found.minsym != nullptr)
20941 sym->maybe_copied = 1;
20942 }
20943
20944 /* A variable with DW_AT_external is never static,
20945 but it may be block-scoped. */
20946 list_to_add
20947 = ((cu->list_in_scope
20948 == cu->get_builder ()->get_file_symbols ())
20949 ? cu->get_builder ()->get_global_symbols ()
20950 : cu->list_in_scope);
20951 }
20952 else
20953 list_to_add = cu->list_in_scope;
20954 }
20955 else
20956 {
20957 /* We do not know the address of this symbol.
20958 If it is an external symbol and we have type information
20959 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20960 The address of the variable will then be determined from
20961 the minimal symbol table whenever the variable is
20962 referenced. */
20963 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20964
20965 /* Fortran explicitly imports any global symbols to the local
20966 scope by DW_TAG_common_block. */
20967 if (cu->language == language_fortran && die->parent
20968 && die->parent->tag == DW_TAG_common_block)
20969 {
20970 /* SYMBOL_CLASS doesn't matter here because
20971 read_common_block is going to reset it. */
20972 if (!suppress_add)
20973 list_to_add = cu->list_in_scope;
20974 }
20975 else if (attr2 && (DW_UNSND (attr2) != 0)
20976 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20977 {
20978 /* A variable with DW_AT_external is never static, but it
20979 may be block-scoped. */
20980 list_to_add
20981 = ((cu->list_in_scope
20982 == cu->get_builder ()->get_file_symbols ())
20983 ? cu->get_builder ()->get_global_symbols ()
20984 : cu->list_in_scope);
20985
20986 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20987 }
20988 else if (!die_is_declaration (die, cu))
20989 {
20990 /* Use the default LOC_OPTIMIZED_OUT class. */
20991 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20992 if (!suppress_add)
20993 list_to_add = cu->list_in_scope;
20994 }
20995 }
20996 break;
20997 case DW_TAG_formal_parameter:
20998 {
20999 /* If we are inside a function, mark this as an argument. If
21000 not, we might be looking at an argument to an inlined function
21001 when we do not have enough information to show inlined frames;
21002 pretend it's a local variable in that case so that the user can
21003 still see it. */
21004 struct context_stack *curr
21005 = cu->get_builder ()->get_current_context_stack ();
21006 if (curr != nullptr && curr->name != nullptr)
21007 SYMBOL_IS_ARGUMENT (sym) = 1;
21008 attr = dwarf2_attr (die, DW_AT_location, cu);
21009 if (attr != nullptr)
21010 {
21011 var_decode_location (attr, sym, cu);
21012 }
21013 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21014 if (attr != nullptr)
21015 {
21016 dwarf2_const_value (attr, sym, cu);
21017 }
21018
21019 list_to_add = cu->list_in_scope;
21020 }
21021 break;
21022 case DW_TAG_unspecified_parameters:
21023 /* From varargs functions; gdb doesn't seem to have any
21024 interest in this information, so just ignore it for now.
21025 (FIXME?) */
21026 break;
21027 case DW_TAG_template_type_param:
21028 suppress_add = 1;
21029 /* Fall through. */
21030 case DW_TAG_class_type:
21031 case DW_TAG_interface_type:
21032 case DW_TAG_structure_type:
21033 case DW_TAG_union_type:
21034 case DW_TAG_set_type:
21035 case DW_TAG_enumeration_type:
21036 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21037 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21038
21039 {
21040 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21041 really ever be static objects: otherwise, if you try
21042 to, say, break of a class's method and you're in a file
21043 which doesn't mention that class, it won't work unless
21044 the check for all static symbols in lookup_symbol_aux
21045 saves you. See the OtherFileClass tests in
21046 gdb.c++/namespace.exp. */
21047
21048 if (!suppress_add)
21049 {
21050 buildsym_compunit *builder = cu->get_builder ();
21051 list_to_add
21052 = (cu->list_in_scope == builder->get_file_symbols ()
21053 && cu->language == language_cplus
21054 ? builder->get_global_symbols ()
21055 : cu->list_in_scope);
21056
21057 /* The semantics of C++ state that "struct foo {
21058 ... }" also defines a typedef for "foo". */
21059 if (cu->language == language_cplus
21060 || cu->language == language_ada
21061 || cu->language == language_d
21062 || cu->language == language_rust)
21063 {
21064 /* The symbol's name is already allocated along
21065 with this objfile, so we don't need to
21066 duplicate it for the type. */
21067 if (SYMBOL_TYPE (sym)->name () == 0)
21068 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21069 }
21070 }
21071 }
21072 break;
21073 case DW_TAG_typedef:
21074 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21075 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21076 list_to_add = cu->list_in_scope;
21077 break;
21078 case DW_TAG_base_type:
21079 case DW_TAG_subrange_type:
21080 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21081 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21082 list_to_add = cu->list_in_scope;
21083 break;
21084 case DW_TAG_enumerator:
21085 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21086 if (attr != nullptr)
21087 {
21088 dwarf2_const_value (attr, sym, cu);
21089 }
21090 {
21091 /* NOTE: carlton/2003-11-10: See comment above in the
21092 DW_TAG_class_type, etc. block. */
21093
21094 list_to_add
21095 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21096 && cu->language == language_cplus
21097 ? cu->get_builder ()->get_global_symbols ()
21098 : cu->list_in_scope);
21099 }
21100 break;
21101 case DW_TAG_imported_declaration:
21102 case DW_TAG_namespace:
21103 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21104 list_to_add = cu->get_builder ()->get_global_symbols ();
21105 break;
21106 case DW_TAG_module:
21107 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21108 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21109 list_to_add = cu->get_builder ()->get_global_symbols ();
21110 break;
21111 case DW_TAG_common_block:
21112 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21113 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21114 add_symbol_to_list (sym, cu->list_in_scope);
21115 break;
21116 default:
21117 /* Not a tag we recognize. Hopefully we aren't processing
21118 trash data, but since we must specifically ignore things
21119 we don't recognize, there is nothing else we should do at
21120 this point. */
21121 complaint (_("unsupported tag: '%s'"),
21122 dwarf_tag_name (die->tag));
21123 break;
21124 }
21125
21126 if (suppress_add)
21127 {
21128 sym->hash_next = objfile->template_symbols;
21129 objfile->template_symbols = sym;
21130 list_to_add = NULL;
21131 }
21132
21133 if (list_to_add != NULL)
21134 add_symbol_to_list (sym, list_to_add);
21135
21136 /* For the benefit of old versions of GCC, check for anonymous
21137 namespaces based on the demangled name. */
21138 if (!cu->processing_has_namespace_info
21139 && cu->language == language_cplus)
21140 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21141 }
21142 return (sym);
21143 }
21144
21145 /* Given an attr with a DW_FORM_dataN value in host byte order,
21146 zero-extend it as appropriate for the symbol's type. The DWARF
21147 standard (v4) is not entirely clear about the meaning of using
21148 DW_FORM_dataN for a constant with a signed type, where the type is
21149 wider than the data. The conclusion of a discussion on the DWARF
21150 list was that this is unspecified. We choose to always zero-extend
21151 because that is the interpretation long in use by GCC. */
21152
21153 static gdb_byte *
21154 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21155 struct dwarf2_cu *cu, LONGEST *value, int bits)
21156 {
21157 struct objfile *objfile = cu->per_objfile->objfile;
21158 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21159 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21160 LONGEST l = DW_UNSND (attr);
21161
21162 if (bits < sizeof (*value) * 8)
21163 {
21164 l &= ((LONGEST) 1 << bits) - 1;
21165 *value = l;
21166 }
21167 else if (bits == sizeof (*value) * 8)
21168 *value = l;
21169 else
21170 {
21171 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21172 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21173 return bytes;
21174 }
21175
21176 return NULL;
21177 }
21178
21179 /* Read a constant value from an attribute. Either set *VALUE, or if
21180 the value does not fit in *VALUE, set *BYTES - either already
21181 allocated on the objfile obstack, or newly allocated on OBSTACK,
21182 or, set *BATON, if we translated the constant to a location
21183 expression. */
21184
21185 static void
21186 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21187 const char *name, struct obstack *obstack,
21188 struct dwarf2_cu *cu,
21189 LONGEST *value, const gdb_byte **bytes,
21190 struct dwarf2_locexpr_baton **baton)
21191 {
21192 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21193 struct objfile *objfile = per_objfile->objfile;
21194 struct comp_unit_head *cu_header = &cu->header;
21195 struct dwarf_block *blk;
21196 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21197 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21198
21199 *value = 0;
21200 *bytes = NULL;
21201 *baton = NULL;
21202
21203 switch (attr->form)
21204 {
21205 case DW_FORM_addr:
21206 case DW_FORM_addrx:
21207 case DW_FORM_GNU_addr_index:
21208 {
21209 gdb_byte *data;
21210
21211 if (TYPE_LENGTH (type) != cu_header->addr_size)
21212 dwarf2_const_value_length_mismatch_complaint (name,
21213 cu_header->addr_size,
21214 TYPE_LENGTH (type));
21215 /* Symbols of this form are reasonably rare, so we just
21216 piggyback on the existing location code rather than writing
21217 a new implementation of symbol_computed_ops. */
21218 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21219 (*baton)->per_objfile = per_objfile;
21220 (*baton)->per_cu = cu->per_cu;
21221 gdb_assert ((*baton)->per_cu);
21222
21223 (*baton)->size = 2 + cu_header->addr_size;
21224 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21225 (*baton)->data = data;
21226
21227 data[0] = DW_OP_addr;
21228 store_unsigned_integer (&data[1], cu_header->addr_size,
21229 byte_order, DW_ADDR (attr));
21230 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21231 }
21232 break;
21233 case DW_FORM_string:
21234 case DW_FORM_strp:
21235 case DW_FORM_strx:
21236 case DW_FORM_GNU_str_index:
21237 case DW_FORM_GNU_strp_alt:
21238 /* DW_STRING is already allocated on the objfile obstack, point
21239 directly to it. */
21240 *bytes = (const gdb_byte *) DW_STRING (attr);
21241 break;
21242 case DW_FORM_block1:
21243 case DW_FORM_block2:
21244 case DW_FORM_block4:
21245 case DW_FORM_block:
21246 case DW_FORM_exprloc:
21247 case DW_FORM_data16:
21248 blk = DW_BLOCK (attr);
21249 if (TYPE_LENGTH (type) != blk->size)
21250 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21251 TYPE_LENGTH (type));
21252 *bytes = blk->data;
21253 break;
21254
21255 /* The DW_AT_const_value attributes are supposed to carry the
21256 symbol's value "represented as it would be on the target
21257 architecture." By the time we get here, it's already been
21258 converted to host endianness, so we just need to sign- or
21259 zero-extend it as appropriate. */
21260 case DW_FORM_data1:
21261 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21262 break;
21263 case DW_FORM_data2:
21264 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21265 break;
21266 case DW_FORM_data4:
21267 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21268 break;
21269 case DW_FORM_data8:
21270 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21271 break;
21272
21273 case DW_FORM_sdata:
21274 case DW_FORM_implicit_const:
21275 *value = DW_SND (attr);
21276 break;
21277
21278 case DW_FORM_udata:
21279 *value = DW_UNSND (attr);
21280 break;
21281
21282 default:
21283 complaint (_("unsupported const value attribute form: '%s'"),
21284 dwarf_form_name (attr->form));
21285 *value = 0;
21286 break;
21287 }
21288 }
21289
21290
21291 /* Copy constant value from an attribute to a symbol. */
21292
21293 static void
21294 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21295 struct dwarf2_cu *cu)
21296 {
21297 struct objfile *objfile = cu->per_objfile->objfile;
21298 LONGEST value;
21299 const gdb_byte *bytes;
21300 struct dwarf2_locexpr_baton *baton;
21301
21302 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21303 sym->print_name (),
21304 &objfile->objfile_obstack, cu,
21305 &value, &bytes, &baton);
21306
21307 if (baton != NULL)
21308 {
21309 SYMBOL_LOCATION_BATON (sym) = baton;
21310 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21311 }
21312 else if (bytes != NULL)
21313 {
21314 SYMBOL_VALUE_BYTES (sym) = bytes;
21315 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21316 }
21317 else
21318 {
21319 SYMBOL_VALUE (sym) = value;
21320 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21321 }
21322 }
21323
21324 /* Return the type of the die in question using its DW_AT_type attribute. */
21325
21326 static struct type *
21327 die_type (struct die_info *die, struct dwarf2_cu *cu)
21328 {
21329 struct attribute *type_attr;
21330
21331 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21332 if (!type_attr)
21333 {
21334 struct objfile *objfile = cu->per_objfile->objfile;
21335 /* A missing DW_AT_type represents a void type. */
21336 return objfile_type (objfile)->builtin_void;
21337 }
21338
21339 return lookup_die_type (die, type_attr, cu);
21340 }
21341
21342 /* True iff CU's producer generates GNAT Ada auxiliary information
21343 that allows to find parallel types through that information instead
21344 of having to do expensive parallel lookups by type name. */
21345
21346 static int
21347 need_gnat_info (struct dwarf2_cu *cu)
21348 {
21349 /* Assume that the Ada compiler was GNAT, which always produces
21350 the auxiliary information. */
21351 return (cu->language == language_ada);
21352 }
21353
21354 /* Return the auxiliary type of the die in question using its
21355 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21356 attribute is not present. */
21357
21358 static struct type *
21359 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21360 {
21361 struct attribute *type_attr;
21362
21363 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21364 if (!type_attr)
21365 return NULL;
21366
21367 return lookup_die_type (die, type_attr, cu);
21368 }
21369
21370 /* If DIE has a descriptive_type attribute, then set the TYPE's
21371 descriptive type accordingly. */
21372
21373 static void
21374 set_descriptive_type (struct type *type, struct die_info *die,
21375 struct dwarf2_cu *cu)
21376 {
21377 struct type *descriptive_type = die_descriptive_type (die, cu);
21378
21379 if (descriptive_type)
21380 {
21381 ALLOCATE_GNAT_AUX_TYPE (type);
21382 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21383 }
21384 }
21385
21386 /* Return the containing type of the die in question using its
21387 DW_AT_containing_type attribute. */
21388
21389 static struct type *
21390 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21391 {
21392 struct attribute *type_attr;
21393 struct objfile *objfile = cu->per_objfile->objfile;
21394
21395 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21396 if (!type_attr)
21397 error (_("Dwarf Error: Problem turning containing type into gdb type "
21398 "[in module %s]"), objfile_name (objfile));
21399
21400 return lookup_die_type (die, type_attr, cu);
21401 }
21402
21403 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21404
21405 static struct type *
21406 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21407 {
21408 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21409 struct objfile *objfile = per_objfile->objfile;
21410 char *saved;
21411
21412 std::string message
21413 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21414 objfile_name (objfile),
21415 sect_offset_str (cu->header.sect_off),
21416 sect_offset_str (die->sect_off));
21417 saved = obstack_strdup (&objfile->objfile_obstack, message);
21418
21419 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21420 }
21421
21422 /* Look up the type of DIE in CU using its type attribute ATTR.
21423 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21424 DW_AT_containing_type.
21425 If there is no type substitute an error marker. */
21426
21427 static struct type *
21428 lookup_die_type (struct die_info *die, const struct attribute *attr,
21429 struct dwarf2_cu *cu)
21430 {
21431 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21432 struct objfile *objfile = per_objfile->objfile;
21433 struct type *this_type;
21434
21435 gdb_assert (attr->name == DW_AT_type
21436 || attr->name == DW_AT_GNAT_descriptive_type
21437 || attr->name == DW_AT_containing_type);
21438
21439 /* First see if we have it cached. */
21440
21441 if (attr->form == DW_FORM_GNU_ref_alt)
21442 {
21443 struct dwarf2_per_cu_data *per_cu;
21444 sect_offset sect_off = attr->get_ref_die_offset ();
21445
21446 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
21447 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
21448 }
21449 else if (attr->form_is_ref ())
21450 {
21451 sect_offset sect_off = attr->get_ref_die_offset ();
21452
21453 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
21454 }
21455 else if (attr->form == DW_FORM_ref_sig8)
21456 {
21457 ULONGEST signature = DW_SIGNATURE (attr);
21458
21459 return get_signatured_type (die, signature, cu);
21460 }
21461 else
21462 {
21463 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21464 " at %s [in module %s]"),
21465 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21466 objfile_name (objfile));
21467 return build_error_marker_type (cu, die);
21468 }
21469
21470 /* If not cached we need to read it in. */
21471
21472 if (this_type == NULL)
21473 {
21474 struct die_info *type_die = NULL;
21475 struct dwarf2_cu *type_cu = cu;
21476
21477 if (attr->form_is_ref ())
21478 type_die = follow_die_ref (die, attr, &type_cu);
21479 if (type_die == NULL)
21480 return build_error_marker_type (cu, die);
21481 /* If we find the type now, it's probably because the type came
21482 from an inter-CU reference and the type's CU got expanded before
21483 ours. */
21484 this_type = read_type_die (type_die, type_cu);
21485 }
21486
21487 /* If we still don't have a type use an error marker. */
21488
21489 if (this_type == NULL)
21490 return build_error_marker_type (cu, die);
21491
21492 return this_type;
21493 }
21494
21495 /* Return the type in DIE, CU.
21496 Returns NULL for invalid types.
21497
21498 This first does a lookup in die_type_hash,
21499 and only reads the die in if necessary.
21500
21501 NOTE: This can be called when reading in partial or full symbols. */
21502
21503 static struct type *
21504 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21505 {
21506 struct type *this_type;
21507
21508 this_type = get_die_type (die, cu);
21509 if (this_type)
21510 return this_type;
21511
21512 return read_type_die_1 (die, cu);
21513 }
21514
21515 /* Read the type in DIE, CU.
21516 Returns NULL for invalid types. */
21517
21518 static struct type *
21519 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21520 {
21521 struct type *this_type = NULL;
21522
21523 switch (die->tag)
21524 {
21525 case DW_TAG_class_type:
21526 case DW_TAG_interface_type:
21527 case DW_TAG_structure_type:
21528 case DW_TAG_union_type:
21529 this_type = read_structure_type (die, cu);
21530 break;
21531 case DW_TAG_enumeration_type:
21532 this_type = read_enumeration_type (die, cu);
21533 break;
21534 case DW_TAG_subprogram:
21535 case DW_TAG_subroutine_type:
21536 case DW_TAG_inlined_subroutine:
21537 this_type = read_subroutine_type (die, cu);
21538 break;
21539 case DW_TAG_array_type:
21540 this_type = read_array_type (die, cu);
21541 break;
21542 case DW_TAG_set_type:
21543 this_type = read_set_type (die, cu);
21544 break;
21545 case DW_TAG_pointer_type:
21546 this_type = read_tag_pointer_type (die, cu);
21547 break;
21548 case DW_TAG_ptr_to_member_type:
21549 this_type = read_tag_ptr_to_member_type (die, cu);
21550 break;
21551 case DW_TAG_reference_type:
21552 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21553 break;
21554 case DW_TAG_rvalue_reference_type:
21555 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21556 break;
21557 case DW_TAG_const_type:
21558 this_type = read_tag_const_type (die, cu);
21559 break;
21560 case DW_TAG_volatile_type:
21561 this_type = read_tag_volatile_type (die, cu);
21562 break;
21563 case DW_TAG_restrict_type:
21564 this_type = read_tag_restrict_type (die, cu);
21565 break;
21566 case DW_TAG_string_type:
21567 this_type = read_tag_string_type (die, cu);
21568 break;
21569 case DW_TAG_typedef:
21570 this_type = read_typedef (die, cu);
21571 break;
21572 case DW_TAG_subrange_type:
21573 this_type = read_subrange_type (die, cu);
21574 break;
21575 case DW_TAG_base_type:
21576 this_type = read_base_type (die, cu);
21577 break;
21578 case DW_TAG_unspecified_type:
21579 this_type = read_unspecified_type (die, cu);
21580 break;
21581 case DW_TAG_namespace:
21582 this_type = read_namespace_type (die, cu);
21583 break;
21584 case DW_TAG_module:
21585 this_type = read_module_type (die, cu);
21586 break;
21587 case DW_TAG_atomic_type:
21588 this_type = read_tag_atomic_type (die, cu);
21589 break;
21590 default:
21591 complaint (_("unexpected tag in read_type_die: '%s'"),
21592 dwarf_tag_name (die->tag));
21593 break;
21594 }
21595
21596 return this_type;
21597 }
21598
21599 /* See if we can figure out if the class lives in a namespace. We do
21600 this by looking for a member function; its demangled name will
21601 contain namespace info, if there is any.
21602 Return the computed name or NULL.
21603 Space for the result is allocated on the objfile's obstack.
21604 This is the full-die version of guess_partial_die_structure_name.
21605 In this case we know DIE has no useful parent. */
21606
21607 static const char *
21608 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21609 {
21610 struct die_info *spec_die;
21611 struct dwarf2_cu *spec_cu;
21612 struct die_info *child;
21613 struct objfile *objfile = cu->per_objfile->objfile;
21614
21615 spec_cu = cu;
21616 spec_die = die_specification (die, &spec_cu);
21617 if (spec_die != NULL)
21618 {
21619 die = spec_die;
21620 cu = spec_cu;
21621 }
21622
21623 for (child = die->child;
21624 child != NULL;
21625 child = child->sibling)
21626 {
21627 if (child->tag == DW_TAG_subprogram)
21628 {
21629 const char *linkage_name = dw2_linkage_name (child, cu);
21630
21631 if (linkage_name != NULL)
21632 {
21633 gdb::unique_xmalloc_ptr<char> actual_name
21634 (language_class_name_from_physname (cu->language_defn,
21635 linkage_name));
21636 const char *name = NULL;
21637
21638 if (actual_name != NULL)
21639 {
21640 const char *die_name = dwarf2_name (die, cu);
21641
21642 if (die_name != NULL
21643 && strcmp (die_name, actual_name.get ()) != 0)
21644 {
21645 /* Strip off the class name from the full name.
21646 We want the prefix. */
21647 int die_name_len = strlen (die_name);
21648 int actual_name_len = strlen (actual_name.get ());
21649 const char *ptr = actual_name.get ();
21650
21651 /* Test for '::' as a sanity check. */
21652 if (actual_name_len > die_name_len + 2
21653 && ptr[actual_name_len - die_name_len - 1] == ':')
21654 name = obstack_strndup (
21655 &objfile->per_bfd->storage_obstack,
21656 ptr, actual_name_len - die_name_len - 2);
21657 }
21658 }
21659 return name;
21660 }
21661 }
21662 }
21663
21664 return NULL;
21665 }
21666
21667 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21668 prefix part in such case. See
21669 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21670
21671 static const char *
21672 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21673 {
21674 struct attribute *attr;
21675 const char *base;
21676
21677 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21678 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21679 return NULL;
21680
21681 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21682 return NULL;
21683
21684 attr = dw2_linkage_name_attr (die, cu);
21685 if (attr == NULL || DW_STRING (attr) == NULL)
21686 return NULL;
21687
21688 /* dwarf2_name had to be already called. */
21689 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21690
21691 /* Strip the base name, keep any leading namespaces/classes. */
21692 base = strrchr (DW_STRING (attr), ':');
21693 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21694 return "";
21695
21696 struct objfile *objfile = cu->per_objfile->objfile;
21697 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21698 DW_STRING (attr),
21699 &base[-1] - DW_STRING (attr));
21700 }
21701
21702 /* Return the name of the namespace/class that DIE is defined within,
21703 or "" if we can't tell. The caller should not xfree the result.
21704
21705 For example, if we're within the method foo() in the following
21706 code:
21707
21708 namespace N {
21709 class C {
21710 void foo () {
21711 }
21712 };
21713 }
21714
21715 then determine_prefix on foo's die will return "N::C". */
21716
21717 static const char *
21718 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21719 {
21720 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21721 struct die_info *parent, *spec_die;
21722 struct dwarf2_cu *spec_cu;
21723 struct type *parent_type;
21724 const char *retval;
21725
21726 if (cu->language != language_cplus
21727 && cu->language != language_fortran && cu->language != language_d
21728 && cu->language != language_rust)
21729 return "";
21730
21731 retval = anonymous_struct_prefix (die, cu);
21732 if (retval)
21733 return retval;
21734
21735 /* We have to be careful in the presence of DW_AT_specification.
21736 For example, with GCC 3.4, given the code
21737
21738 namespace N {
21739 void foo() {
21740 // Definition of N::foo.
21741 }
21742 }
21743
21744 then we'll have a tree of DIEs like this:
21745
21746 1: DW_TAG_compile_unit
21747 2: DW_TAG_namespace // N
21748 3: DW_TAG_subprogram // declaration of N::foo
21749 4: DW_TAG_subprogram // definition of N::foo
21750 DW_AT_specification // refers to die #3
21751
21752 Thus, when processing die #4, we have to pretend that we're in
21753 the context of its DW_AT_specification, namely the contex of die
21754 #3. */
21755 spec_cu = cu;
21756 spec_die = die_specification (die, &spec_cu);
21757 if (spec_die == NULL)
21758 parent = die->parent;
21759 else
21760 {
21761 parent = spec_die->parent;
21762 cu = spec_cu;
21763 }
21764
21765 if (parent == NULL)
21766 return "";
21767 else if (parent->building_fullname)
21768 {
21769 const char *name;
21770 const char *parent_name;
21771
21772 /* It has been seen on RealView 2.2 built binaries,
21773 DW_TAG_template_type_param types actually _defined_ as
21774 children of the parent class:
21775
21776 enum E {};
21777 template class <class Enum> Class{};
21778 Class<enum E> class_e;
21779
21780 1: DW_TAG_class_type (Class)
21781 2: DW_TAG_enumeration_type (E)
21782 3: DW_TAG_enumerator (enum1:0)
21783 3: DW_TAG_enumerator (enum2:1)
21784 ...
21785 2: DW_TAG_template_type_param
21786 DW_AT_type DW_FORM_ref_udata (E)
21787
21788 Besides being broken debug info, it can put GDB into an
21789 infinite loop. Consider:
21790
21791 When we're building the full name for Class<E>, we'll start
21792 at Class, and go look over its template type parameters,
21793 finding E. We'll then try to build the full name of E, and
21794 reach here. We're now trying to build the full name of E,
21795 and look over the parent DIE for containing scope. In the
21796 broken case, if we followed the parent DIE of E, we'd again
21797 find Class, and once again go look at its template type
21798 arguments, etc., etc. Simply don't consider such parent die
21799 as source-level parent of this die (it can't be, the language
21800 doesn't allow it), and break the loop here. */
21801 name = dwarf2_name (die, cu);
21802 parent_name = dwarf2_name (parent, cu);
21803 complaint (_("template param type '%s' defined within parent '%s'"),
21804 name ? name : "<unknown>",
21805 parent_name ? parent_name : "<unknown>");
21806 return "";
21807 }
21808 else
21809 switch (parent->tag)
21810 {
21811 case DW_TAG_namespace:
21812 parent_type = read_type_die (parent, cu);
21813 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21814 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21815 Work around this problem here. */
21816 if (cu->language == language_cplus
21817 && strcmp (parent_type->name (), "::") == 0)
21818 return "";
21819 /* We give a name to even anonymous namespaces. */
21820 return parent_type->name ();
21821 case DW_TAG_class_type:
21822 case DW_TAG_interface_type:
21823 case DW_TAG_structure_type:
21824 case DW_TAG_union_type:
21825 case DW_TAG_module:
21826 parent_type = read_type_die (parent, cu);
21827 if (parent_type->name () != NULL)
21828 return parent_type->name ();
21829 else
21830 /* An anonymous structure is only allowed non-static data
21831 members; no typedefs, no member functions, et cetera.
21832 So it does not need a prefix. */
21833 return "";
21834 case DW_TAG_compile_unit:
21835 case DW_TAG_partial_unit:
21836 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21837 if (cu->language == language_cplus
21838 && !per_objfile->per_bfd->types.empty ()
21839 && die->child != NULL
21840 && (die->tag == DW_TAG_class_type
21841 || die->tag == DW_TAG_structure_type
21842 || die->tag == DW_TAG_union_type))
21843 {
21844 const char *name = guess_full_die_structure_name (die, cu);
21845 if (name != NULL)
21846 return name;
21847 }
21848 return "";
21849 case DW_TAG_subprogram:
21850 /* Nested subroutines in Fortran get a prefix with the name
21851 of the parent's subroutine. */
21852 if (cu->language == language_fortran)
21853 {
21854 if ((die->tag == DW_TAG_subprogram)
21855 && (dwarf2_name (parent, cu) != NULL))
21856 return dwarf2_name (parent, cu);
21857 }
21858 return determine_prefix (parent, cu);
21859 case DW_TAG_enumeration_type:
21860 parent_type = read_type_die (parent, cu);
21861 if (TYPE_DECLARED_CLASS (parent_type))
21862 {
21863 if (parent_type->name () != NULL)
21864 return parent_type->name ();
21865 return "";
21866 }
21867 /* Fall through. */
21868 default:
21869 return determine_prefix (parent, cu);
21870 }
21871 }
21872
21873 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21874 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21875 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21876 an obconcat, otherwise allocate storage for the result. The CU argument is
21877 used to determine the language and hence, the appropriate separator. */
21878
21879 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21880
21881 static char *
21882 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21883 int physname, struct dwarf2_cu *cu)
21884 {
21885 const char *lead = "";
21886 const char *sep;
21887
21888 if (suffix == NULL || suffix[0] == '\0'
21889 || prefix == NULL || prefix[0] == '\0')
21890 sep = "";
21891 else if (cu->language == language_d)
21892 {
21893 /* For D, the 'main' function could be defined in any module, but it
21894 should never be prefixed. */
21895 if (strcmp (suffix, "D main") == 0)
21896 {
21897 prefix = "";
21898 sep = "";
21899 }
21900 else
21901 sep = ".";
21902 }
21903 else if (cu->language == language_fortran && physname)
21904 {
21905 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21906 DW_AT_MIPS_linkage_name is preferred and used instead. */
21907
21908 lead = "__";
21909 sep = "_MOD_";
21910 }
21911 else
21912 sep = "::";
21913
21914 if (prefix == NULL)
21915 prefix = "";
21916 if (suffix == NULL)
21917 suffix = "";
21918
21919 if (obs == NULL)
21920 {
21921 char *retval
21922 = ((char *)
21923 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21924
21925 strcpy (retval, lead);
21926 strcat (retval, prefix);
21927 strcat (retval, sep);
21928 strcat (retval, suffix);
21929 return retval;
21930 }
21931 else
21932 {
21933 /* We have an obstack. */
21934 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21935 }
21936 }
21937
21938 /* Get name of a die, return NULL if not found. */
21939
21940 static const char *
21941 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21942 struct objfile *objfile)
21943 {
21944 if (name && cu->language == language_cplus)
21945 {
21946 gdb::unique_xmalloc_ptr<char> canon_name
21947 = cp_canonicalize_string (name);
21948
21949 if (canon_name != nullptr)
21950 name = objfile->intern (canon_name.get ());
21951 }
21952
21953 return name;
21954 }
21955
21956 /* Get name of a die, return NULL if not found.
21957 Anonymous namespaces are converted to their magic string. */
21958
21959 static const char *
21960 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21961 {
21962 struct attribute *attr;
21963 struct objfile *objfile = cu->per_objfile->objfile;
21964
21965 attr = dwarf2_attr (die, DW_AT_name, cu);
21966 if ((!attr || !DW_STRING (attr))
21967 && die->tag != DW_TAG_namespace
21968 && die->tag != DW_TAG_class_type
21969 && die->tag != DW_TAG_interface_type
21970 && die->tag != DW_TAG_structure_type
21971 && die->tag != DW_TAG_union_type)
21972 return NULL;
21973
21974 switch (die->tag)
21975 {
21976 case DW_TAG_compile_unit:
21977 case DW_TAG_partial_unit:
21978 /* Compilation units have a DW_AT_name that is a filename, not
21979 a source language identifier. */
21980 case DW_TAG_enumeration_type:
21981 case DW_TAG_enumerator:
21982 /* These tags always have simple identifiers already; no need
21983 to canonicalize them. */
21984 return DW_STRING (attr);
21985
21986 case DW_TAG_namespace:
21987 if (attr != NULL && DW_STRING (attr) != NULL)
21988 return DW_STRING (attr);
21989 return CP_ANONYMOUS_NAMESPACE_STR;
21990
21991 case DW_TAG_class_type:
21992 case DW_TAG_interface_type:
21993 case DW_TAG_structure_type:
21994 case DW_TAG_union_type:
21995 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21996 structures or unions. These were of the form "._%d" in GCC 4.1,
21997 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21998 and GCC 4.4. We work around this problem by ignoring these. */
21999 if (attr && DW_STRING (attr)
22000 && (startswith (DW_STRING (attr), "._")
22001 || startswith (DW_STRING (attr), "<anonymous")))
22002 return NULL;
22003
22004 /* GCC might emit a nameless typedef that has a linkage name. See
22005 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22006 if (!attr || DW_STRING (attr) == NULL)
22007 {
22008 attr = dw2_linkage_name_attr (die, cu);
22009 if (attr == NULL || DW_STRING (attr) == NULL)
22010 return NULL;
22011
22012 /* Avoid demangling DW_STRING (attr) the second time on a second
22013 call for the same DIE. */
22014 if (!DW_STRING_IS_CANONICAL (attr))
22015 {
22016 gdb::unique_xmalloc_ptr<char> demangled
22017 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22018 if (demangled == nullptr)
22019 return nullptr;
22020
22021 DW_STRING (attr) = objfile->intern (demangled.get ());
22022 DW_STRING_IS_CANONICAL (attr) = 1;
22023 }
22024
22025 /* Strip any leading namespaces/classes, keep only the base name.
22026 DW_AT_name for named DIEs does not contain the prefixes. */
22027 const char *base = strrchr (DW_STRING (attr), ':');
22028 if (base && base > DW_STRING (attr) && base[-1] == ':')
22029 return &base[1];
22030 else
22031 return DW_STRING (attr);
22032 }
22033 break;
22034
22035 default:
22036 break;
22037 }
22038
22039 if (!DW_STRING_IS_CANONICAL (attr))
22040 {
22041 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22042 objfile);
22043 DW_STRING_IS_CANONICAL (attr) = 1;
22044 }
22045 return DW_STRING (attr);
22046 }
22047
22048 /* Return the die that this die in an extension of, or NULL if there
22049 is none. *EXT_CU is the CU containing DIE on input, and the CU
22050 containing the return value on output. */
22051
22052 static struct die_info *
22053 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22054 {
22055 struct attribute *attr;
22056
22057 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22058 if (attr == NULL)
22059 return NULL;
22060
22061 return follow_die_ref (die, attr, ext_cu);
22062 }
22063
22064 static void
22065 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22066 {
22067 unsigned int i;
22068
22069 print_spaces (indent, f);
22070 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22071 dwarf_tag_name (die->tag), die->abbrev,
22072 sect_offset_str (die->sect_off));
22073
22074 if (die->parent != NULL)
22075 {
22076 print_spaces (indent, f);
22077 fprintf_unfiltered (f, " parent at offset: %s\n",
22078 sect_offset_str (die->parent->sect_off));
22079 }
22080
22081 print_spaces (indent, f);
22082 fprintf_unfiltered (f, " has children: %s\n",
22083 dwarf_bool_name (die->child != NULL));
22084
22085 print_spaces (indent, f);
22086 fprintf_unfiltered (f, " attributes:\n");
22087
22088 for (i = 0; i < die->num_attrs; ++i)
22089 {
22090 print_spaces (indent, f);
22091 fprintf_unfiltered (f, " %s (%s) ",
22092 dwarf_attr_name (die->attrs[i].name),
22093 dwarf_form_name (die->attrs[i].form));
22094
22095 switch (die->attrs[i].form)
22096 {
22097 case DW_FORM_addr:
22098 case DW_FORM_addrx:
22099 case DW_FORM_GNU_addr_index:
22100 fprintf_unfiltered (f, "address: ");
22101 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22102 break;
22103 case DW_FORM_block2:
22104 case DW_FORM_block4:
22105 case DW_FORM_block:
22106 case DW_FORM_block1:
22107 fprintf_unfiltered (f, "block: size %s",
22108 pulongest (DW_BLOCK (&die->attrs[i])->size));
22109 break;
22110 case DW_FORM_exprloc:
22111 fprintf_unfiltered (f, "expression: size %s",
22112 pulongest (DW_BLOCK (&die->attrs[i])->size));
22113 break;
22114 case DW_FORM_data16:
22115 fprintf_unfiltered (f, "constant of 16 bytes");
22116 break;
22117 case DW_FORM_ref_addr:
22118 fprintf_unfiltered (f, "ref address: ");
22119 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22120 break;
22121 case DW_FORM_GNU_ref_alt:
22122 fprintf_unfiltered (f, "alt ref address: ");
22123 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22124 break;
22125 case DW_FORM_ref1:
22126 case DW_FORM_ref2:
22127 case DW_FORM_ref4:
22128 case DW_FORM_ref8:
22129 case DW_FORM_ref_udata:
22130 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22131 (long) (DW_UNSND (&die->attrs[i])));
22132 break;
22133 case DW_FORM_data1:
22134 case DW_FORM_data2:
22135 case DW_FORM_data4:
22136 case DW_FORM_data8:
22137 case DW_FORM_udata:
22138 case DW_FORM_sdata:
22139 fprintf_unfiltered (f, "constant: %s",
22140 pulongest (DW_UNSND (&die->attrs[i])));
22141 break;
22142 case DW_FORM_sec_offset:
22143 fprintf_unfiltered (f, "section offset: %s",
22144 pulongest (DW_UNSND (&die->attrs[i])));
22145 break;
22146 case DW_FORM_ref_sig8:
22147 fprintf_unfiltered (f, "signature: %s",
22148 hex_string (DW_SIGNATURE (&die->attrs[i])));
22149 break;
22150 case DW_FORM_string:
22151 case DW_FORM_strp:
22152 case DW_FORM_line_strp:
22153 case DW_FORM_strx:
22154 case DW_FORM_GNU_str_index:
22155 case DW_FORM_GNU_strp_alt:
22156 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22157 DW_STRING (&die->attrs[i])
22158 ? DW_STRING (&die->attrs[i]) : "",
22159 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22160 break;
22161 case DW_FORM_flag:
22162 if (DW_UNSND (&die->attrs[i]))
22163 fprintf_unfiltered (f, "flag: TRUE");
22164 else
22165 fprintf_unfiltered (f, "flag: FALSE");
22166 break;
22167 case DW_FORM_flag_present:
22168 fprintf_unfiltered (f, "flag: TRUE");
22169 break;
22170 case DW_FORM_indirect:
22171 /* The reader will have reduced the indirect form to
22172 the "base form" so this form should not occur. */
22173 fprintf_unfiltered (f,
22174 "unexpected attribute form: DW_FORM_indirect");
22175 break;
22176 case DW_FORM_implicit_const:
22177 fprintf_unfiltered (f, "constant: %s",
22178 plongest (DW_SND (&die->attrs[i])));
22179 break;
22180 default:
22181 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22182 die->attrs[i].form);
22183 break;
22184 }
22185 fprintf_unfiltered (f, "\n");
22186 }
22187 }
22188
22189 static void
22190 dump_die_for_error (struct die_info *die)
22191 {
22192 dump_die_shallow (gdb_stderr, 0, die);
22193 }
22194
22195 static void
22196 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22197 {
22198 int indent = level * 4;
22199
22200 gdb_assert (die != NULL);
22201
22202 if (level >= max_level)
22203 return;
22204
22205 dump_die_shallow (f, indent, die);
22206
22207 if (die->child != NULL)
22208 {
22209 print_spaces (indent, f);
22210 fprintf_unfiltered (f, " Children:");
22211 if (level + 1 < max_level)
22212 {
22213 fprintf_unfiltered (f, "\n");
22214 dump_die_1 (f, level + 1, max_level, die->child);
22215 }
22216 else
22217 {
22218 fprintf_unfiltered (f,
22219 " [not printed, max nesting level reached]\n");
22220 }
22221 }
22222
22223 if (die->sibling != NULL && level > 0)
22224 {
22225 dump_die_1 (f, level, max_level, die->sibling);
22226 }
22227 }
22228
22229 /* This is called from the pdie macro in gdbinit.in.
22230 It's not static so gcc will keep a copy callable from gdb. */
22231
22232 void
22233 dump_die (struct die_info *die, int max_level)
22234 {
22235 dump_die_1 (gdb_stdlog, 0, max_level, die);
22236 }
22237
22238 static void
22239 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22240 {
22241 void **slot;
22242
22243 slot = htab_find_slot_with_hash (cu->die_hash, die,
22244 to_underlying (die->sect_off),
22245 INSERT);
22246
22247 *slot = die;
22248 }
22249
22250 /* Follow reference or signature attribute ATTR of SRC_DIE.
22251 On entry *REF_CU is the CU of SRC_DIE.
22252 On exit *REF_CU is the CU of the result. */
22253
22254 static struct die_info *
22255 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22256 struct dwarf2_cu **ref_cu)
22257 {
22258 struct die_info *die;
22259
22260 if (attr->form_is_ref ())
22261 die = follow_die_ref (src_die, attr, ref_cu);
22262 else if (attr->form == DW_FORM_ref_sig8)
22263 die = follow_die_sig (src_die, attr, ref_cu);
22264 else
22265 {
22266 dump_die_for_error (src_die);
22267 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22268 objfile_name ((*ref_cu)->per_objfile->objfile));
22269 }
22270
22271 return die;
22272 }
22273
22274 /* Follow reference OFFSET.
22275 On entry *REF_CU is the CU of the source die referencing OFFSET.
22276 On exit *REF_CU is the CU of the result.
22277 Returns NULL if OFFSET is invalid. */
22278
22279 static struct die_info *
22280 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22281 struct dwarf2_cu **ref_cu)
22282 {
22283 struct die_info temp_die;
22284 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22285 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22286
22287 gdb_assert (cu->per_cu != NULL);
22288
22289 target_cu = cu;
22290
22291 if (cu->per_cu->is_debug_types)
22292 {
22293 /* .debug_types CUs cannot reference anything outside their CU.
22294 If they need to, they have to reference a signatured type via
22295 DW_FORM_ref_sig8. */
22296 if (!cu->header.offset_in_cu_p (sect_off))
22297 return NULL;
22298 }
22299 else if (offset_in_dwz != cu->per_cu->is_dwz
22300 || !cu->header.offset_in_cu_p (sect_off))
22301 {
22302 struct dwarf2_per_cu_data *per_cu;
22303
22304 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22305 per_objfile);
22306
22307 /* If necessary, add it to the queue and load its DIEs. */
22308 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22309 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
22310
22311 target_cu = per_objfile->get_cu (per_cu);
22312 }
22313 else if (cu->dies == NULL)
22314 {
22315 /* We're loading full DIEs during partial symbol reading. */
22316 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22317 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
22318 }
22319
22320 *ref_cu = target_cu;
22321 temp_die.sect_off = sect_off;
22322
22323 if (target_cu != cu)
22324 target_cu->ancestor = cu;
22325
22326 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22327 &temp_die,
22328 to_underlying (sect_off));
22329 }
22330
22331 /* Follow reference attribute ATTR of SRC_DIE.
22332 On entry *REF_CU is the CU of SRC_DIE.
22333 On exit *REF_CU is the CU of the result. */
22334
22335 static struct die_info *
22336 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22337 struct dwarf2_cu **ref_cu)
22338 {
22339 sect_offset sect_off = attr->get_ref_die_offset ();
22340 struct dwarf2_cu *cu = *ref_cu;
22341 struct die_info *die;
22342
22343 die = follow_die_offset (sect_off,
22344 (attr->form == DW_FORM_GNU_ref_alt
22345 || cu->per_cu->is_dwz),
22346 ref_cu);
22347 if (!die)
22348 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22349 "at %s [in module %s]"),
22350 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22351 objfile_name (cu->per_objfile->objfile));
22352
22353 return die;
22354 }
22355
22356 /* See read.h. */
22357
22358 struct dwarf2_locexpr_baton
22359 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22360 dwarf2_per_cu_data *per_cu,
22361 dwarf2_per_objfile *per_objfile,
22362 CORE_ADDR (*get_frame_pc) (void *baton),
22363 void *baton, bool resolve_abstract_p)
22364 {
22365 struct die_info *die;
22366 struct attribute *attr;
22367 struct dwarf2_locexpr_baton retval;
22368 struct objfile *objfile = per_objfile->objfile;
22369
22370 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22371 if (cu == nullptr)
22372 cu = load_cu (per_cu, per_objfile, false);
22373
22374 if (cu == nullptr)
22375 {
22376 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22377 Instead just throw an error, not much else we can do. */
22378 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22379 sect_offset_str (sect_off), objfile_name (objfile));
22380 }
22381
22382 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22383 if (!die)
22384 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22385 sect_offset_str (sect_off), objfile_name (objfile));
22386
22387 attr = dwarf2_attr (die, DW_AT_location, cu);
22388 if (!attr && resolve_abstract_p
22389 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22390 != per_objfile->per_bfd->abstract_to_concrete.end ()))
22391 {
22392 CORE_ADDR pc = (*get_frame_pc) (baton);
22393 CORE_ADDR baseaddr = objfile->text_section_offset ();
22394 struct gdbarch *gdbarch = objfile->arch ();
22395
22396 for (const auto &cand_off
22397 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22398 {
22399 struct dwarf2_cu *cand_cu = cu;
22400 struct die_info *cand
22401 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22402 if (!cand
22403 || !cand->parent
22404 || cand->parent->tag != DW_TAG_subprogram)
22405 continue;
22406
22407 CORE_ADDR pc_low, pc_high;
22408 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22409 if (pc_low == ((CORE_ADDR) -1))
22410 continue;
22411 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22412 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22413 if (!(pc_low <= pc && pc < pc_high))
22414 continue;
22415
22416 die = cand;
22417 attr = dwarf2_attr (die, DW_AT_location, cu);
22418 break;
22419 }
22420 }
22421
22422 if (!attr)
22423 {
22424 /* DWARF: "If there is no such attribute, then there is no effect.".
22425 DATA is ignored if SIZE is 0. */
22426
22427 retval.data = NULL;
22428 retval.size = 0;
22429 }
22430 else if (attr->form_is_section_offset ())
22431 {
22432 struct dwarf2_loclist_baton loclist_baton;
22433 CORE_ADDR pc = (*get_frame_pc) (baton);
22434 size_t size;
22435
22436 fill_in_loclist_baton (cu, &loclist_baton, attr);
22437
22438 retval.data = dwarf2_find_location_expression (&loclist_baton,
22439 &size, pc);
22440 retval.size = size;
22441 }
22442 else
22443 {
22444 if (!attr->form_is_block ())
22445 error (_("Dwarf Error: DIE at %s referenced in module %s "
22446 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22447 sect_offset_str (sect_off), objfile_name (objfile));
22448
22449 retval.data = DW_BLOCK (attr)->data;
22450 retval.size = DW_BLOCK (attr)->size;
22451 }
22452 retval.per_objfile = per_objfile;
22453 retval.per_cu = cu->per_cu;
22454
22455 per_objfile->age_comp_units ();
22456
22457 return retval;
22458 }
22459
22460 /* See read.h. */
22461
22462 struct dwarf2_locexpr_baton
22463 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22464 dwarf2_per_cu_data *per_cu,
22465 dwarf2_per_objfile *per_objfile,
22466 CORE_ADDR (*get_frame_pc) (void *baton),
22467 void *baton)
22468 {
22469 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22470
22471 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22472 get_frame_pc, baton);
22473 }
22474
22475 /* Write a constant of a given type as target-ordered bytes into
22476 OBSTACK. */
22477
22478 static const gdb_byte *
22479 write_constant_as_bytes (struct obstack *obstack,
22480 enum bfd_endian byte_order,
22481 struct type *type,
22482 ULONGEST value,
22483 LONGEST *len)
22484 {
22485 gdb_byte *result;
22486
22487 *len = TYPE_LENGTH (type);
22488 result = (gdb_byte *) obstack_alloc (obstack, *len);
22489 store_unsigned_integer (result, *len, byte_order, value);
22490
22491 return result;
22492 }
22493
22494 /* See read.h. */
22495
22496 const gdb_byte *
22497 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22498 dwarf2_per_cu_data *per_cu,
22499 dwarf2_per_objfile *per_objfile,
22500 obstack *obstack,
22501 LONGEST *len)
22502 {
22503 struct die_info *die;
22504 struct attribute *attr;
22505 const gdb_byte *result = NULL;
22506 struct type *type;
22507 LONGEST value;
22508 enum bfd_endian byte_order;
22509 struct objfile *objfile = per_objfile->objfile;
22510
22511 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22512 if (cu == nullptr)
22513 cu = load_cu (per_cu, per_objfile, false);
22514
22515 if (cu == nullptr)
22516 {
22517 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22518 Instead just throw an error, not much else we can do. */
22519 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22520 sect_offset_str (sect_off), objfile_name (objfile));
22521 }
22522
22523 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22524 if (!die)
22525 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22526 sect_offset_str (sect_off), objfile_name (objfile));
22527
22528 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22529 if (attr == NULL)
22530 return NULL;
22531
22532 byte_order = (bfd_big_endian (objfile->obfd)
22533 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22534
22535 switch (attr->form)
22536 {
22537 case DW_FORM_addr:
22538 case DW_FORM_addrx:
22539 case DW_FORM_GNU_addr_index:
22540 {
22541 gdb_byte *tem;
22542
22543 *len = cu->header.addr_size;
22544 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22545 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22546 result = tem;
22547 }
22548 break;
22549 case DW_FORM_string:
22550 case DW_FORM_strp:
22551 case DW_FORM_strx:
22552 case DW_FORM_GNU_str_index:
22553 case DW_FORM_GNU_strp_alt:
22554 /* DW_STRING is already allocated on the objfile obstack, point
22555 directly to it. */
22556 result = (const gdb_byte *) DW_STRING (attr);
22557 *len = strlen (DW_STRING (attr));
22558 break;
22559 case DW_FORM_block1:
22560 case DW_FORM_block2:
22561 case DW_FORM_block4:
22562 case DW_FORM_block:
22563 case DW_FORM_exprloc:
22564 case DW_FORM_data16:
22565 result = DW_BLOCK (attr)->data;
22566 *len = DW_BLOCK (attr)->size;
22567 break;
22568
22569 /* The DW_AT_const_value attributes are supposed to carry the
22570 symbol's value "represented as it would be on the target
22571 architecture." By the time we get here, it's already been
22572 converted to host endianness, so we just need to sign- or
22573 zero-extend it as appropriate. */
22574 case DW_FORM_data1:
22575 type = die_type (die, cu);
22576 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22577 if (result == NULL)
22578 result = write_constant_as_bytes (obstack, byte_order,
22579 type, value, len);
22580 break;
22581 case DW_FORM_data2:
22582 type = die_type (die, cu);
22583 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22584 if (result == NULL)
22585 result = write_constant_as_bytes (obstack, byte_order,
22586 type, value, len);
22587 break;
22588 case DW_FORM_data4:
22589 type = die_type (die, cu);
22590 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22591 if (result == NULL)
22592 result = write_constant_as_bytes (obstack, byte_order,
22593 type, value, len);
22594 break;
22595 case DW_FORM_data8:
22596 type = die_type (die, cu);
22597 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22598 if (result == NULL)
22599 result = write_constant_as_bytes (obstack, byte_order,
22600 type, value, len);
22601 break;
22602
22603 case DW_FORM_sdata:
22604 case DW_FORM_implicit_const:
22605 type = die_type (die, cu);
22606 result = write_constant_as_bytes (obstack, byte_order,
22607 type, DW_SND (attr), len);
22608 break;
22609
22610 case DW_FORM_udata:
22611 type = die_type (die, cu);
22612 result = write_constant_as_bytes (obstack, byte_order,
22613 type, DW_UNSND (attr), len);
22614 break;
22615
22616 default:
22617 complaint (_("unsupported const value attribute form: '%s'"),
22618 dwarf_form_name (attr->form));
22619 break;
22620 }
22621
22622 return result;
22623 }
22624
22625 /* See read.h. */
22626
22627 struct type *
22628 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22629 dwarf2_per_cu_data *per_cu,
22630 dwarf2_per_objfile *per_objfile)
22631 {
22632 struct die_info *die;
22633
22634 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22635 if (cu == nullptr)
22636 cu = load_cu (per_cu, per_objfile, false);
22637
22638 if (cu == nullptr)
22639 return nullptr;
22640
22641 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22642 if (!die)
22643 return NULL;
22644
22645 return die_type (die, cu);
22646 }
22647
22648 /* See read.h. */
22649
22650 struct type *
22651 dwarf2_get_die_type (cu_offset die_offset,
22652 dwarf2_per_cu_data *per_cu,
22653 dwarf2_per_objfile *per_objfile)
22654 {
22655 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22656 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22657 }
22658
22659 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22660 On entry *REF_CU is the CU of SRC_DIE.
22661 On exit *REF_CU is the CU of the result.
22662 Returns NULL if the referenced DIE isn't found. */
22663
22664 static struct die_info *
22665 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22666 struct dwarf2_cu **ref_cu)
22667 {
22668 struct die_info temp_die;
22669 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22670 struct die_info *die;
22671 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
22672
22673
22674 /* While it might be nice to assert sig_type->type == NULL here,
22675 we can get here for DW_AT_imported_declaration where we need
22676 the DIE not the type. */
22677
22678 /* If necessary, add it to the queue and load its DIEs. */
22679
22680 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
22681 language_minimal))
22682 read_signatured_type (sig_type, per_objfile);
22683
22684 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
22685 gdb_assert (sig_cu != NULL);
22686 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22687 temp_die.sect_off = sig_type->type_offset_in_section;
22688 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22689 to_underlying (temp_die.sect_off));
22690 if (die)
22691 {
22692 /* For .gdb_index version 7 keep track of included TUs.
22693 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22694 if (per_objfile->per_bfd->index_table != NULL
22695 && per_objfile->per_bfd->index_table->version <= 7)
22696 {
22697 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22698 }
22699
22700 *ref_cu = sig_cu;
22701 if (sig_cu != cu)
22702 sig_cu->ancestor = cu;
22703
22704 return die;
22705 }
22706
22707 return NULL;
22708 }
22709
22710 /* Follow signatured type referenced by ATTR in SRC_DIE.
22711 On entry *REF_CU is the CU of SRC_DIE.
22712 On exit *REF_CU is the CU of the result.
22713 The result is the DIE of the type.
22714 If the referenced type cannot be found an error is thrown. */
22715
22716 static struct die_info *
22717 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22718 struct dwarf2_cu **ref_cu)
22719 {
22720 ULONGEST signature = DW_SIGNATURE (attr);
22721 struct signatured_type *sig_type;
22722 struct die_info *die;
22723
22724 gdb_assert (attr->form == DW_FORM_ref_sig8);
22725
22726 sig_type = lookup_signatured_type (*ref_cu, signature);
22727 /* sig_type will be NULL if the signatured type is missing from
22728 the debug info. */
22729 if (sig_type == NULL)
22730 {
22731 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22732 " from DIE at %s [in module %s]"),
22733 hex_string (signature), sect_offset_str (src_die->sect_off),
22734 objfile_name ((*ref_cu)->per_objfile->objfile));
22735 }
22736
22737 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22738 if (die == NULL)
22739 {
22740 dump_die_for_error (src_die);
22741 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22742 " from DIE at %s [in module %s]"),
22743 hex_string (signature), sect_offset_str (src_die->sect_off),
22744 objfile_name ((*ref_cu)->per_objfile->objfile));
22745 }
22746
22747 return die;
22748 }
22749
22750 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22751 reading in and processing the type unit if necessary. */
22752
22753 static struct type *
22754 get_signatured_type (struct die_info *die, ULONGEST signature,
22755 struct dwarf2_cu *cu)
22756 {
22757 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22758 struct signatured_type *sig_type;
22759 struct dwarf2_cu *type_cu;
22760 struct die_info *type_die;
22761 struct type *type;
22762
22763 sig_type = lookup_signatured_type (cu, signature);
22764 /* sig_type will be NULL if the signatured type is missing from
22765 the debug info. */
22766 if (sig_type == NULL)
22767 {
22768 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22769 " from DIE at %s [in module %s]"),
22770 hex_string (signature), sect_offset_str (die->sect_off),
22771 objfile_name (per_objfile->objfile));
22772 return build_error_marker_type (cu, die);
22773 }
22774
22775 /* If we already know the type we're done. */
22776 type = per_objfile->get_type_for_signatured_type (sig_type);
22777 if (type != nullptr)
22778 return type;
22779
22780 type_cu = cu;
22781 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22782 if (type_die != NULL)
22783 {
22784 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22785 is created. This is important, for example, because for c++ classes
22786 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22787 type = read_type_die (type_die, type_cu);
22788 if (type == NULL)
22789 {
22790 complaint (_("Dwarf Error: Cannot build signatured type %s"
22791 " referenced from DIE at %s [in module %s]"),
22792 hex_string (signature), sect_offset_str (die->sect_off),
22793 objfile_name (per_objfile->objfile));
22794 type = build_error_marker_type (cu, die);
22795 }
22796 }
22797 else
22798 {
22799 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22800 " from DIE at %s [in module %s]"),
22801 hex_string (signature), sect_offset_str (die->sect_off),
22802 objfile_name (per_objfile->objfile));
22803 type = build_error_marker_type (cu, die);
22804 }
22805
22806 per_objfile->set_type_for_signatured_type (sig_type, type);
22807
22808 return type;
22809 }
22810
22811 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22812 reading in and processing the type unit if necessary. */
22813
22814 static struct type *
22815 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22816 struct dwarf2_cu *cu) /* ARI: editCase function */
22817 {
22818 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22819 if (attr->form_is_ref ())
22820 {
22821 struct dwarf2_cu *type_cu = cu;
22822 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22823
22824 return read_type_die (type_die, type_cu);
22825 }
22826 else if (attr->form == DW_FORM_ref_sig8)
22827 {
22828 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22829 }
22830 else
22831 {
22832 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22833
22834 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22835 " at %s [in module %s]"),
22836 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22837 objfile_name (per_objfile->objfile));
22838 return build_error_marker_type (cu, die);
22839 }
22840 }
22841
22842 /* Load the DIEs associated with type unit PER_CU into memory. */
22843
22844 static void
22845 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22846 dwarf2_per_objfile *per_objfile)
22847 {
22848 struct signatured_type *sig_type;
22849
22850 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22851 gdb_assert (! per_cu->type_unit_group_p ());
22852
22853 /* We have the per_cu, but we need the signatured_type.
22854 Fortunately this is an easy translation. */
22855 gdb_assert (per_cu->is_debug_types);
22856 sig_type = (struct signatured_type *) per_cu;
22857
22858 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22859
22860 read_signatured_type (sig_type, per_objfile);
22861
22862 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22863 }
22864
22865 /* Read in a signatured type and build its CU and DIEs.
22866 If the type is a stub for the real type in a DWO file,
22867 read in the real type from the DWO file as well. */
22868
22869 static void
22870 read_signatured_type (signatured_type *sig_type,
22871 dwarf2_per_objfile *per_objfile)
22872 {
22873 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22874
22875 gdb_assert (per_cu->is_debug_types);
22876 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22877
22878 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
22879
22880 if (!reader.dummy_p)
22881 {
22882 struct dwarf2_cu *cu = reader.cu;
22883 const gdb_byte *info_ptr = reader.info_ptr;
22884
22885 gdb_assert (cu->die_hash == NULL);
22886 cu->die_hash =
22887 htab_create_alloc_ex (cu->header.length / 12,
22888 die_hash,
22889 die_eq,
22890 NULL,
22891 &cu->comp_unit_obstack,
22892 hashtab_obstack_allocate,
22893 dummy_obstack_deallocate);
22894
22895 if (reader.comp_unit_die->has_children)
22896 reader.comp_unit_die->child
22897 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22898 reader.comp_unit_die);
22899 cu->dies = reader.comp_unit_die;
22900 /* comp_unit_die is not stored in die_hash, no need. */
22901
22902 /* We try not to read any attributes in this function, because
22903 not all CUs needed for references have been loaded yet, and
22904 symbol table processing isn't initialized. But we have to
22905 set the CU language, or we won't be able to build types
22906 correctly. Similarly, if we do not read the producer, we can
22907 not apply producer-specific interpretation. */
22908 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22909
22910 reader.keep ();
22911 }
22912
22913 sig_type->per_cu.tu_read = 1;
22914 }
22915
22916 /* Decode simple location descriptions.
22917 Given a pointer to a dwarf block that defines a location, compute
22918 the location and return the value. If COMPUTED is non-null, it is
22919 set to true to indicate that decoding was successful, and false
22920 otherwise. If COMPUTED is null, then this function may emit a
22921 complaint. */
22922
22923 static CORE_ADDR
22924 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22925 {
22926 struct objfile *objfile = cu->per_objfile->objfile;
22927 size_t i;
22928 size_t size = blk->size;
22929 const gdb_byte *data = blk->data;
22930 CORE_ADDR stack[64];
22931 int stacki;
22932 unsigned int bytes_read, unsnd;
22933 gdb_byte op;
22934
22935 if (computed != nullptr)
22936 *computed = false;
22937
22938 i = 0;
22939 stacki = 0;
22940 stack[stacki] = 0;
22941 stack[++stacki] = 0;
22942
22943 while (i < size)
22944 {
22945 op = data[i++];
22946 switch (op)
22947 {
22948 case DW_OP_lit0:
22949 case DW_OP_lit1:
22950 case DW_OP_lit2:
22951 case DW_OP_lit3:
22952 case DW_OP_lit4:
22953 case DW_OP_lit5:
22954 case DW_OP_lit6:
22955 case DW_OP_lit7:
22956 case DW_OP_lit8:
22957 case DW_OP_lit9:
22958 case DW_OP_lit10:
22959 case DW_OP_lit11:
22960 case DW_OP_lit12:
22961 case DW_OP_lit13:
22962 case DW_OP_lit14:
22963 case DW_OP_lit15:
22964 case DW_OP_lit16:
22965 case DW_OP_lit17:
22966 case DW_OP_lit18:
22967 case DW_OP_lit19:
22968 case DW_OP_lit20:
22969 case DW_OP_lit21:
22970 case DW_OP_lit22:
22971 case DW_OP_lit23:
22972 case DW_OP_lit24:
22973 case DW_OP_lit25:
22974 case DW_OP_lit26:
22975 case DW_OP_lit27:
22976 case DW_OP_lit28:
22977 case DW_OP_lit29:
22978 case DW_OP_lit30:
22979 case DW_OP_lit31:
22980 stack[++stacki] = op - DW_OP_lit0;
22981 break;
22982
22983 case DW_OP_reg0:
22984 case DW_OP_reg1:
22985 case DW_OP_reg2:
22986 case DW_OP_reg3:
22987 case DW_OP_reg4:
22988 case DW_OP_reg5:
22989 case DW_OP_reg6:
22990 case DW_OP_reg7:
22991 case DW_OP_reg8:
22992 case DW_OP_reg9:
22993 case DW_OP_reg10:
22994 case DW_OP_reg11:
22995 case DW_OP_reg12:
22996 case DW_OP_reg13:
22997 case DW_OP_reg14:
22998 case DW_OP_reg15:
22999 case DW_OP_reg16:
23000 case DW_OP_reg17:
23001 case DW_OP_reg18:
23002 case DW_OP_reg19:
23003 case DW_OP_reg20:
23004 case DW_OP_reg21:
23005 case DW_OP_reg22:
23006 case DW_OP_reg23:
23007 case DW_OP_reg24:
23008 case DW_OP_reg25:
23009 case DW_OP_reg26:
23010 case DW_OP_reg27:
23011 case DW_OP_reg28:
23012 case DW_OP_reg29:
23013 case DW_OP_reg30:
23014 case DW_OP_reg31:
23015 stack[++stacki] = op - DW_OP_reg0;
23016 if (i < size)
23017 {
23018 if (computed == nullptr)
23019 dwarf2_complex_location_expr_complaint ();
23020 else
23021 return 0;
23022 }
23023 break;
23024
23025 case DW_OP_regx:
23026 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23027 i += bytes_read;
23028 stack[++stacki] = unsnd;
23029 if (i < size)
23030 {
23031 if (computed == nullptr)
23032 dwarf2_complex_location_expr_complaint ();
23033 else
23034 return 0;
23035 }
23036 break;
23037
23038 case DW_OP_addr:
23039 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23040 &bytes_read);
23041 i += bytes_read;
23042 break;
23043
23044 case DW_OP_const1u:
23045 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23046 i += 1;
23047 break;
23048
23049 case DW_OP_const1s:
23050 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23051 i += 1;
23052 break;
23053
23054 case DW_OP_const2u:
23055 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23056 i += 2;
23057 break;
23058
23059 case DW_OP_const2s:
23060 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23061 i += 2;
23062 break;
23063
23064 case DW_OP_const4u:
23065 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23066 i += 4;
23067 break;
23068
23069 case DW_OP_const4s:
23070 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23071 i += 4;
23072 break;
23073
23074 case DW_OP_const8u:
23075 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23076 i += 8;
23077 break;
23078
23079 case DW_OP_constu:
23080 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23081 &bytes_read);
23082 i += bytes_read;
23083 break;
23084
23085 case DW_OP_consts:
23086 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23087 i += bytes_read;
23088 break;
23089
23090 case DW_OP_dup:
23091 stack[stacki + 1] = stack[stacki];
23092 stacki++;
23093 break;
23094
23095 case DW_OP_plus:
23096 stack[stacki - 1] += stack[stacki];
23097 stacki--;
23098 break;
23099
23100 case DW_OP_plus_uconst:
23101 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23102 &bytes_read);
23103 i += bytes_read;
23104 break;
23105
23106 case DW_OP_minus:
23107 stack[stacki - 1] -= stack[stacki];
23108 stacki--;
23109 break;
23110
23111 case DW_OP_deref:
23112 /* If we're not the last op, then we definitely can't encode
23113 this using GDB's address_class enum. This is valid for partial
23114 global symbols, although the variable's address will be bogus
23115 in the psymtab. */
23116 if (i < size)
23117 {
23118 if (computed == nullptr)
23119 dwarf2_complex_location_expr_complaint ();
23120 else
23121 return 0;
23122 }
23123 break;
23124
23125 case DW_OP_GNU_push_tls_address:
23126 case DW_OP_form_tls_address:
23127 /* The top of the stack has the offset from the beginning
23128 of the thread control block at which the variable is located. */
23129 /* Nothing should follow this operator, so the top of stack would
23130 be returned. */
23131 /* This is valid for partial global symbols, but the variable's
23132 address will be bogus in the psymtab. Make it always at least
23133 non-zero to not look as a variable garbage collected by linker
23134 which have DW_OP_addr 0. */
23135 if (i < size)
23136 {
23137 if (computed == nullptr)
23138 dwarf2_complex_location_expr_complaint ();
23139 else
23140 return 0;
23141 }
23142 stack[stacki]++;
23143 break;
23144
23145 case DW_OP_GNU_uninit:
23146 if (computed != nullptr)
23147 return 0;
23148 break;
23149
23150 case DW_OP_addrx:
23151 case DW_OP_GNU_addr_index:
23152 case DW_OP_GNU_const_index:
23153 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23154 &bytes_read);
23155 i += bytes_read;
23156 break;
23157
23158 default:
23159 if (computed == nullptr)
23160 {
23161 const char *name = get_DW_OP_name (op);
23162
23163 if (name)
23164 complaint (_("unsupported stack op: '%s'"),
23165 name);
23166 else
23167 complaint (_("unsupported stack op: '%02x'"),
23168 op);
23169 }
23170
23171 return (stack[stacki]);
23172 }
23173
23174 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23175 outside of the allocated space. Also enforce minimum>0. */
23176 if (stacki >= ARRAY_SIZE (stack) - 1)
23177 {
23178 if (computed == nullptr)
23179 complaint (_("location description stack overflow"));
23180 return 0;
23181 }
23182
23183 if (stacki <= 0)
23184 {
23185 if (computed == nullptr)
23186 complaint (_("location description stack underflow"));
23187 return 0;
23188 }
23189 }
23190
23191 if (computed != nullptr)
23192 *computed = true;
23193 return (stack[stacki]);
23194 }
23195
23196 /* memory allocation interface */
23197
23198 static struct dwarf_block *
23199 dwarf_alloc_block (struct dwarf2_cu *cu)
23200 {
23201 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23202 }
23203
23204 static struct die_info *
23205 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23206 {
23207 struct die_info *die;
23208 size_t size = sizeof (struct die_info);
23209
23210 if (num_attrs > 1)
23211 size += (num_attrs - 1) * sizeof (struct attribute);
23212
23213 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23214 memset (die, 0, sizeof (struct die_info));
23215 return (die);
23216 }
23217
23218 \f
23219
23220 /* Macro support. */
23221
23222 /* An overload of dwarf_decode_macros that finds the correct section
23223 and ensures it is read in before calling the other overload. */
23224
23225 static void
23226 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23227 int section_is_gnu)
23228 {
23229 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23230 struct objfile *objfile = per_objfile->objfile;
23231 const struct line_header *lh = cu->line_header;
23232 unsigned int offset_size = cu->header.offset_size;
23233 struct dwarf2_section_info *section;
23234 const char *section_name;
23235
23236 if (cu->dwo_unit != nullptr)
23237 {
23238 if (section_is_gnu)
23239 {
23240 section = &cu->dwo_unit->dwo_file->sections.macro;
23241 section_name = ".debug_macro.dwo";
23242 }
23243 else
23244 {
23245 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23246 section_name = ".debug_macinfo.dwo";
23247 }
23248 }
23249 else
23250 {
23251 if (section_is_gnu)
23252 {
23253 section = &per_objfile->per_bfd->macro;
23254 section_name = ".debug_macro";
23255 }
23256 else
23257 {
23258 section = &per_objfile->per_bfd->macinfo;
23259 section_name = ".debug_macinfo";
23260 }
23261 }
23262
23263 section->read (objfile);
23264 if (section->buffer == nullptr)
23265 {
23266 complaint (_("missing %s section"), section_name);
23267 return;
23268 }
23269
23270 buildsym_compunit *builder = cu->get_builder ();
23271
23272 dwarf_decode_macros (per_objfile, builder, section, lh,
23273 offset_size, offset, section_is_gnu);
23274 }
23275
23276 /* Return the .debug_loc section to use for CU.
23277 For DWO files use .debug_loc.dwo. */
23278
23279 static struct dwarf2_section_info *
23280 cu_debug_loc_section (struct dwarf2_cu *cu)
23281 {
23282 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23283
23284 if (cu->dwo_unit)
23285 {
23286 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23287
23288 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23289 }
23290 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23291 : &per_objfile->per_bfd->loc);
23292 }
23293
23294 /* A helper function that fills in a dwarf2_loclist_baton. */
23295
23296 static void
23297 fill_in_loclist_baton (struct dwarf2_cu *cu,
23298 struct dwarf2_loclist_baton *baton,
23299 const struct attribute *attr)
23300 {
23301 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23302 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23303
23304 section->read (per_objfile->objfile);
23305
23306 baton->per_objfile = per_objfile;
23307 baton->per_cu = cu->per_cu;
23308 gdb_assert (baton->per_cu);
23309 /* We don't know how long the location list is, but make sure we
23310 don't run off the edge of the section. */
23311 baton->size = section->size - DW_UNSND (attr);
23312 baton->data = section->buffer + DW_UNSND (attr);
23313 if (cu->base_address.has_value ())
23314 baton->base_address = *cu->base_address;
23315 else
23316 baton->base_address = 0;
23317 baton->from_dwo = cu->dwo_unit != NULL;
23318 }
23319
23320 static void
23321 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23322 struct dwarf2_cu *cu, int is_block)
23323 {
23324 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23325 struct objfile *objfile = per_objfile->objfile;
23326 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23327
23328 if (attr->form_is_section_offset ()
23329 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23330 the section. If so, fall through to the complaint in the
23331 other branch. */
23332 && DW_UNSND (attr) < section->get_size (objfile))
23333 {
23334 struct dwarf2_loclist_baton *baton;
23335
23336 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23337
23338 fill_in_loclist_baton (cu, baton, attr);
23339
23340 if (!cu->base_address.has_value ())
23341 complaint (_("Location list used without "
23342 "specifying the CU base address."));
23343
23344 SYMBOL_ACLASS_INDEX (sym) = (is_block
23345 ? dwarf2_loclist_block_index
23346 : dwarf2_loclist_index);
23347 SYMBOL_LOCATION_BATON (sym) = baton;
23348 }
23349 else
23350 {
23351 struct dwarf2_locexpr_baton *baton;
23352
23353 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23354 baton->per_objfile = per_objfile;
23355 baton->per_cu = cu->per_cu;
23356 gdb_assert (baton->per_cu);
23357
23358 if (attr->form_is_block ())
23359 {
23360 /* Note that we're just copying the block's data pointer
23361 here, not the actual data. We're still pointing into the
23362 info_buffer for SYM's objfile; right now we never release
23363 that buffer, but when we do clean up properly this may
23364 need to change. */
23365 baton->size = DW_BLOCK (attr)->size;
23366 baton->data = DW_BLOCK (attr)->data;
23367 }
23368 else
23369 {
23370 dwarf2_invalid_attrib_class_complaint ("location description",
23371 sym->natural_name ());
23372 baton->size = 0;
23373 }
23374
23375 SYMBOL_ACLASS_INDEX (sym) = (is_block
23376 ? dwarf2_locexpr_block_index
23377 : dwarf2_locexpr_index);
23378 SYMBOL_LOCATION_BATON (sym) = baton;
23379 }
23380 }
23381
23382 /* See read.h. */
23383
23384 const comp_unit_head *
23385 dwarf2_per_cu_data::get_header () const
23386 {
23387 if (!m_header_read_in)
23388 {
23389 const gdb_byte *info_ptr
23390 = this->section->buffer + to_underlying (this->sect_off);
23391
23392 memset (&m_header, 0, sizeof (m_header));
23393
23394 read_comp_unit_head (&m_header, info_ptr, this->section,
23395 rcuh_kind::COMPILE);
23396 }
23397
23398 return &m_header;
23399 }
23400
23401 /* See read.h. */
23402
23403 int
23404 dwarf2_per_cu_data::addr_size () const
23405 {
23406 return this->get_header ()->addr_size;
23407 }
23408
23409 /* See read.h. */
23410
23411 int
23412 dwarf2_per_cu_data::offset_size () const
23413 {
23414 return this->get_header ()->offset_size;
23415 }
23416
23417 /* See read.h. */
23418
23419 int
23420 dwarf2_per_cu_data::ref_addr_size () const
23421 {
23422 const comp_unit_head *header = this->get_header ();
23423
23424 if (header->version == 2)
23425 return header->addr_size;
23426 else
23427 return header->offset_size;
23428 }
23429
23430 /* See read.h. */
23431
23432 struct type *
23433 dwarf2_cu::addr_type () const
23434 {
23435 struct objfile *objfile = this->per_objfile->objfile;
23436 struct type *void_type = objfile_type (objfile)->builtin_void;
23437 struct type *addr_type = lookup_pointer_type (void_type);
23438 int addr_size = this->per_cu->addr_size ();
23439
23440 if (TYPE_LENGTH (addr_type) == addr_size)
23441 return addr_type;
23442
23443 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23444 return addr_type;
23445 }
23446
23447 /* A helper function for dwarf2_find_containing_comp_unit that returns
23448 the index of the result, and that searches a vector. It will
23449 return a result even if the offset in question does not actually
23450 occur in any CU. This is separate so that it can be unit
23451 tested. */
23452
23453 static int
23454 dwarf2_find_containing_comp_unit
23455 (sect_offset sect_off,
23456 unsigned int offset_in_dwz,
23457 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23458 {
23459 int low, high;
23460
23461 low = 0;
23462 high = all_comp_units.size () - 1;
23463 while (high > low)
23464 {
23465 struct dwarf2_per_cu_data *mid_cu;
23466 int mid = low + (high - low) / 2;
23467
23468 mid_cu = all_comp_units[mid];
23469 if (mid_cu->is_dwz > offset_in_dwz
23470 || (mid_cu->is_dwz == offset_in_dwz
23471 && mid_cu->sect_off + mid_cu->length > sect_off))
23472 high = mid;
23473 else
23474 low = mid + 1;
23475 }
23476 gdb_assert (low == high);
23477 return low;
23478 }
23479
23480 /* Locate the .debug_info compilation unit from CU's objfile which contains
23481 the DIE at OFFSET. Raises an error on failure. */
23482
23483 static struct dwarf2_per_cu_data *
23484 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23485 unsigned int offset_in_dwz,
23486 dwarf2_per_objfile *per_objfile)
23487 {
23488 int low = dwarf2_find_containing_comp_unit
23489 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
23490 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
23491
23492 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23493 {
23494 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23495 error (_("Dwarf Error: could not find partial DIE containing "
23496 "offset %s [in module %s]"),
23497 sect_offset_str (sect_off),
23498 bfd_get_filename (per_objfile->objfile->obfd));
23499
23500 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23501 <= sect_off);
23502 return per_objfile->per_bfd->all_comp_units[low-1];
23503 }
23504 else
23505 {
23506 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
23507 && sect_off >= this_cu->sect_off + this_cu->length)
23508 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23509 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23510 return this_cu;
23511 }
23512 }
23513
23514 #if GDB_SELF_TEST
23515
23516 namespace selftests {
23517 namespace find_containing_comp_unit {
23518
23519 static void
23520 run_test ()
23521 {
23522 struct dwarf2_per_cu_data one {};
23523 struct dwarf2_per_cu_data two {};
23524 struct dwarf2_per_cu_data three {};
23525 struct dwarf2_per_cu_data four {};
23526
23527 one.length = 5;
23528 two.sect_off = sect_offset (one.length);
23529 two.length = 7;
23530
23531 three.length = 5;
23532 three.is_dwz = 1;
23533 four.sect_off = sect_offset (three.length);
23534 four.length = 7;
23535 four.is_dwz = 1;
23536
23537 std::vector<dwarf2_per_cu_data *> units;
23538 units.push_back (&one);
23539 units.push_back (&two);
23540 units.push_back (&three);
23541 units.push_back (&four);
23542
23543 int result;
23544
23545 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23546 SELF_CHECK (units[result] == &one);
23547 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23548 SELF_CHECK (units[result] == &one);
23549 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23550 SELF_CHECK (units[result] == &two);
23551
23552 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23553 SELF_CHECK (units[result] == &three);
23554 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23555 SELF_CHECK (units[result] == &three);
23556 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23557 SELF_CHECK (units[result] == &four);
23558 }
23559
23560 }
23561 }
23562
23563 #endif /* GDB_SELF_TEST */
23564
23565 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23566
23567 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23568 dwarf2_per_objfile *per_objfile)
23569 : per_cu (per_cu),
23570 per_objfile (per_objfile),
23571 mark (false),
23572 has_loclist (false),
23573 checked_producer (false),
23574 producer_is_gxx_lt_4_6 (false),
23575 producer_is_gcc_lt_4_3 (false),
23576 producer_is_icc (false),
23577 producer_is_icc_lt_14 (false),
23578 producer_is_codewarrior (false),
23579 processing_has_namespace_info (false)
23580 {
23581 }
23582
23583 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23584
23585 static void
23586 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23587 enum language pretend_language)
23588 {
23589 struct attribute *attr;
23590
23591 /* Set the language we're debugging. */
23592 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23593 if (attr != nullptr)
23594 set_cu_language (DW_UNSND (attr), cu);
23595 else
23596 {
23597 cu->language = pretend_language;
23598 cu->language_defn = language_def (cu->language);
23599 }
23600
23601 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23602 }
23603
23604 /* See read.h. */
23605
23606 dwarf2_cu *
23607 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23608 {
23609 auto it = m_dwarf2_cus.find (per_cu);
23610 if (it == m_dwarf2_cus.end ())
23611 return nullptr;
23612
23613 return it->second;
23614 }
23615
23616 /* See read.h. */
23617
23618 void
23619 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23620 {
23621 gdb_assert (this->get_cu (per_cu) == nullptr);
23622
23623 m_dwarf2_cus[per_cu] = cu;
23624 }
23625
23626 /* See read.h. */
23627
23628 void
23629 dwarf2_per_objfile::age_comp_units ()
23630 {
23631 /* Start by clearing all marks. */
23632 for (auto pair : m_dwarf2_cus)
23633 pair.second->mark = false;
23634
23635 /* Traverse all CUs, mark them and their dependencies if used recently
23636 enough. */
23637 for (auto pair : m_dwarf2_cus)
23638 {
23639 dwarf2_cu *cu = pair.second;
23640
23641 cu->last_used++;
23642 if (cu->last_used <= dwarf_max_cache_age)
23643 dwarf2_mark (cu);
23644 }
23645
23646 /* Delete all CUs still not marked. */
23647 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23648 {
23649 dwarf2_cu *cu = it->second;
23650
23651 if (!cu->mark)
23652 {
23653 delete cu;
23654 it = m_dwarf2_cus.erase (it);
23655 }
23656 else
23657 it++;
23658 }
23659 }
23660
23661 /* See read.h. */
23662
23663 void
23664 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23665 {
23666 auto it = m_dwarf2_cus.find (per_cu);
23667 if (it == m_dwarf2_cus.end ())
23668 return;
23669
23670 delete it->second;
23671
23672 m_dwarf2_cus.erase (it);
23673 }
23674
23675 dwarf2_per_objfile::~dwarf2_per_objfile ()
23676 {
23677 remove_all_cus ();
23678 }
23679
23680 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23681 We store these in a hash table separate from the DIEs, and preserve them
23682 when the DIEs are flushed out of cache.
23683
23684 The CU "per_cu" pointer is needed because offset alone is not enough to
23685 uniquely identify the type. A file may have multiple .debug_types sections,
23686 or the type may come from a DWO file. Furthermore, while it's more logical
23687 to use per_cu->section+offset, with Fission the section with the data is in
23688 the DWO file but we don't know that section at the point we need it.
23689 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23690 because we can enter the lookup routine, get_die_type_at_offset, from
23691 outside this file, and thus won't necessarily have PER_CU->cu.
23692 Fortunately, PER_CU is stable for the life of the objfile. */
23693
23694 struct dwarf2_per_cu_offset_and_type
23695 {
23696 const struct dwarf2_per_cu_data *per_cu;
23697 sect_offset sect_off;
23698 struct type *type;
23699 };
23700
23701 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23702
23703 static hashval_t
23704 per_cu_offset_and_type_hash (const void *item)
23705 {
23706 const struct dwarf2_per_cu_offset_and_type *ofs
23707 = (const struct dwarf2_per_cu_offset_and_type *) item;
23708
23709 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23710 }
23711
23712 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23713
23714 static int
23715 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23716 {
23717 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23718 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23719 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23720 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23721
23722 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23723 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23724 }
23725
23726 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23727 table if necessary. For convenience, return TYPE.
23728
23729 The DIEs reading must have careful ordering to:
23730 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23731 reading current DIE.
23732 * Not trying to dereference contents of still incompletely read in types
23733 while reading in other DIEs.
23734 * Enable referencing still incompletely read in types just by a pointer to
23735 the type without accessing its fields.
23736
23737 Therefore caller should follow these rules:
23738 * Try to fetch any prerequisite types we may need to build this DIE type
23739 before building the type and calling set_die_type.
23740 * After building type call set_die_type for current DIE as soon as
23741 possible before fetching more types to complete the current type.
23742 * Make the type as complete as possible before fetching more types. */
23743
23744 static struct type *
23745 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23746 {
23747 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23748 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23749 struct objfile *objfile = per_objfile->objfile;
23750 struct attribute *attr;
23751 struct dynamic_prop prop;
23752
23753 /* For Ada types, make sure that the gnat-specific data is always
23754 initialized (if not already set). There are a few types where
23755 we should not be doing so, because the type-specific area is
23756 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23757 where the type-specific area is used to store the floatformat).
23758 But this is not a problem, because the gnat-specific information
23759 is actually not needed for these types. */
23760 if (need_gnat_info (cu)
23761 && type->code () != TYPE_CODE_FUNC
23762 && type->code () != TYPE_CODE_FLT
23763 && type->code () != TYPE_CODE_METHODPTR
23764 && type->code () != TYPE_CODE_MEMBERPTR
23765 && type->code () != TYPE_CODE_METHOD
23766 && !HAVE_GNAT_AUX_INFO (type))
23767 INIT_GNAT_SPECIFIC (type);
23768
23769 /* Read DW_AT_allocated and set in type. */
23770 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23771 if (attr != NULL && attr->form_is_block ())
23772 {
23773 struct type *prop_type = cu->addr_sized_int_type (false);
23774 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23775 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23776 }
23777 else if (attr != NULL)
23778 {
23779 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23780 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23781 sect_offset_str (die->sect_off));
23782 }
23783
23784 /* Read DW_AT_associated and set in type. */
23785 attr = dwarf2_attr (die, DW_AT_associated, cu);
23786 if (attr != NULL && attr->form_is_block ())
23787 {
23788 struct type *prop_type = cu->addr_sized_int_type (false);
23789 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23790 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23791 }
23792 else if (attr != NULL)
23793 {
23794 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23795 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23796 sect_offset_str (die->sect_off));
23797 }
23798
23799 /* Read DW_AT_data_location and set in type. */
23800 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23801 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23802 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23803
23804 if (per_objfile->die_type_hash == NULL)
23805 per_objfile->die_type_hash
23806 = htab_up (htab_create_alloc (127,
23807 per_cu_offset_and_type_hash,
23808 per_cu_offset_and_type_eq,
23809 NULL, xcalloc, xfree));
23810
23811 ofs.per_cu = cu->per_cu;
23812 ofs.sect_off = die->sect_off;
23813 ofs.type = type;
23814 slot = (struct dwarf2_per_cu_offset_and_type **)
23815 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
23816 if (*slot)
23817 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23818 sect_offset_str (die->sect_off));
23819 *slot = XOBNEW (&objfile->objfile_obstack,
23820 struct dwarf2_per_cu_offset_and_type);
23821 **slot = ofs;
23822 return type;
23823 }
23824
23825 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23826 or return NULL if the die does not have a saved type. */
23827
23828 static struct type *
23829 get_die_type_at_offset (sect_offset sect_off,
23830 dwarf2_per_cu_data *per_cu,
23831 dwarf2_per_objfile *per_objfile)
23832 {
23833 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23834
23835 if (per_objfile->die_type_hash == NULL)
23836 return NULL;
23837
23838 ofs.per_cu = per_cu;
23839 ofs.sect_off = sect_off;
23840 slot = ((struct dwarf2_per_cu_offset_and_type *)
23841 htab_find (per_objfile->die_type_hash.get (), &ofs));
23842 if (slot)
23843 return slot->type;
23844 else
23845 return NULL;
23846 }
23847
23848 /* Look up the type for DIE in CU in die_type_hash,
23849 or return NULL if DIE does not have a saved type. */
23850
23851 static struct type *
23852 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23853 {
23854 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23855 }
23856
23857 /* Add a dependence relationship from CU to REF_PER_CU. */
23858
23859 static void
23860 dwarf2_add_dependence (struct dwarf2_cu *cu,
23861 struct dwarf2_per_cu_data *ref_per_cu)
23862 {
23863 void **slot;
23864
23865 if (cu->dependencies == NULL)
23866 cu->dependencies
23867 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23868 NULL, &cu->comp_unit_obstack,
23869 hashtab_obstack_allocate,
23870 dummy_obstack_deallocate);
23871
23872 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23873 if (*slot == NULL)
23874 *slot = ref_per_cu;
23875 }
23876
23877 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23878 Set the mark field in every compilation unit in the
23879 cache that we must keep because we are keeping CU.
23880
23881 DATA is the dwarf2_per_objfile object in which to look up CUs. */
23882
23883 static int
23884 dwarf2_mark_helper (void **slot, void *data)
23885 {
23886 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23887 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23888 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23889
23890 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23891 reading of the chain. As such dependencies remain valid it is not much
23892 useful to track and undo them during QUIT cleanups. */
23893 if (cu == nullptr)
23894 return 1;
23895
23896 if (cu->mark)
23897 return 1;
23898
23899 cu->mark = true;
23900
23901 if (cu->dependencies != nullptr)
23902 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
23903
23904 return 1;
23905 }
23906
23907 /* Set the mark field in CU and in every other compilation unit in the
23908 cache that we must keep because we are keeping CU. */
23909
23910 static void
23911 dwarf2_mark (struct dwarf2_cu *cu)
23912 {
23913 if (cu->mark)
23914 return;
23915
23916 cu->mark = true;
23917
23918 if (cu->dependencies != nullptr)
23919 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
23920 }
23921
23922 /* Trivial hash function for partial_die_info: the hash value of a DIE
23923 is its offset in .debug_info for this objfile. */
23924
23925 static hashval_t
23926 partial_die_hash (const void *item)
23927 {
23928 const struct partial_die_info *part_die
23929 = (const struct partial_die_info *) item;
23930
23931 return to_underlying (part_die->sect_off);
23932 }
23933
23934 /* Trivial comparison function for partial_die_info structures: two DIEs
23935 are equal if they have the same offset. */
23936
23937 static int
23938 partial_die_eq (const void *item_lhs, const void *item_rhs)
23939 {
23940 const struct partial_die_info *part_die_lhs
23941 = (const struct partial_die_info *) item_lhs;
23942 const struct partial_die_info *part_die_rhs
23943 = (const struct partial_die_info *) item_rhs;
23944
23945 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23946 }
23947
23948 struct cmd_list_element *set_dwarf_cmdlist;
23949 struct cmd_list_element *show_dwarf_cmdlist;
23950
23951 static void
23952 show_check_physname (struct ui_file *file, int from_tty,
23953 struct cmd_list_element *c, const char *value)
23954 {
23955 fprintf_filtered (file,
23956 _("Whether to check \"physname\" is %s.\n"),
23957 value);
23958 }
23959
23960 void _initialize_dwarf2_read ();
23961 void
23962 _initialize_dwarf2_read ()
23963 {
23964 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23965 Set DWARF specific variables.\n\
23966 Configure DWARF variables such as the cache size."),
23967 &set_dwarf_cmdlist, "maintenance set dwarf ",
23968 0/*allow-unknown*/, &maintenance_set_cmdlist);
23969
23970 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23971 Show DWARF specific variables.\n\
23972 Show DWARF variables such as the cache size."),
23973 &show_dwarf_cmdlist, "maintenance show dwarf ",
23974 0/*allow-unknown*/, &maintenance_show_cmdlist);
23975
23976 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23977 &dwarf_max_cache_age, _("\
23978 Set the upper bound on the age of cached DWARF compilation units."), _("\
23979 Show the upper bound on the age of cached DWARF compilation units."), _("\
23980 A higher limit means that cached compilation units will be stored\n\
23981 in memory longer, and more total memory will be used. Zero disables\n\
23982 caching, which can slow down startup."),
23983 NULL,
23984 show_dwarf_max_cache_age,
23985 &set_dwarf_cmdlist,
23986 &show_dwarf_cmdlist);
23987
23988 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23989 Set debugging of the DWARF reader."), _("\
23990 Show debugging of the DWARF reader."), _("\
23991 When enabled (non-zero), debugging messages are printed during DWARF\n\
23992 reading and symtab expansion. A value of 1 (one) provides basic\n\
23993 information. A value greater than 1 provides more verbose information."),
23994 NULL,
23995 NULL,
23996 &setdebuglist, &showdebuglist);
23997
23998 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23999 Set debugging of the DWARF DIE reader."), _("\
24000 Show debugging of the DWARF DIE reader."), _("\
24001 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24002 The value is the maximum depth to print."),
24003 NULL,
24004 NULL,
24005 &setdebuglist, &showdebuglist);
24006
24007 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24008 Set debugging of the dwarf line reader."), _("\
24009 Show debugging of the dwarf line reader."), _("\
24010 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24011 A value of 1 (one) provides basic information.\n\
24012 A value greater than 1 provides more verbose information."),
24013 NULL,
24014 NULL,
24015 &setdebuglist, &showdebuglist);
24016
24017 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24018 Set cross-checking of \"physname\" code against demangler."), _("\
24019 Show cross-checking of \"physname\" code against demangler."), _("\
24020 When enabled, GDB's internal \"physname\" code is checked against\n\
24021 the demangler."),
24022 NULL, show_check_physname,
24023 &setdebuglist, &showdebuglist);
24024
24025 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24026 no_class, &use_deprecated_index_sections, _("\
24027 Set whether to use deprecated gdb_index sections."), _("\
24028 Show whether to use deprecated gdb_index sections."), _("\
24029 When enabled, deprecated .gdb_index sections are used anyway.\n\
24030 Normally they are ignored either because of a missing feature or\n\
24031 performance issue.\n\
24032 Warning: This option must be enabled before gdb reads the file."),
24033 NULL,
24034 NULL,
24035 &setlist, &showlist);
24036
24037 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24038 &dwarf2_locexpr_funcs);
24039 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24040 &dwarf2_loclist_funcs);
24041
24042 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24043 &dwarf2_block_frame_base_locexpr_funcs);
24044 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24045 &dwarf2_block_frame_base_loclist_funcs);
24046
24047 #if GDB_SELF_TEST
24048 selftests::register_test ("dw2_expand_symtabs_matching",
24049 selftests::dw2_expand_symtabs_matching::run_test);
24050 selftests::register_test ("dwarf2_find_containing_comp_unit",
24051 selftests::find_containing_comp_unit::run_test);
24052 #endif
24053 }