]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Move die_type_hash to dwarf2_per_objfile
[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 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* Size of .debug_loclists section header for 32-bit DWARF format. */
118 #define LOCLIST_HEADER_SIZE32 12
119
120 /* Size of .debug_loclists section header for 64-bit DWARF format. */
121 #define LOCLIST_HEADER_SIZE64 20
122
123 /* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136 struct name_component
137 {
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146 };
147
148 /* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151 struct mapped_index_base
152 {
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
187
188 /* Prevent deleting/destroying via a base class pointer. */
189 protected:
190 ~mapped_index_base() = default;
191 };
192
193 /* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
195 struct mapped_index final : public mapped_index_base
196 {
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
204 /* Index data format version. */
205 int version = 0;
206
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
209
210 /* The symbol table, implemented as a hash table. */
211 gdb::array_view<symbol_table_slot> symbol_table;
212
213 /* A pointer to the constant pool. */
214 const char *constant_pool = nullptr;
215
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
219 return bucket.name == 0 && bucket.vec == 0;
220 }
221
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
224 const char *symbol_name_at (offset_type idx) const override
225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
226
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
229 };
230
231 /* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
233 struct mapped_debug_names final : public mapped_index_base
234 {
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
281 };
282
283 /* See dwarf2read.h. */
284
285 dwarf2_per_objfile *
286 get_dwarf2_per_objfile (struct objfile *objfile)
287 {
288 return dwarf2_objfile_data_key.get (objfile);
289 }
290
291 /* Default names of the debugging sections. */
292
293 /* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
296 static const struct dwarf2_debug_sections dwarf2_elf_names =
297 {
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
302 { ".debug_loclists", ".zdebug_loclists" },
303 { ".debug_macinfo", ".zdebug_macinfo" },
304 { ".debug_macro", ".zdebug_macro" },
305 { ".debug_str", ".zdebug_str" },
306 { ".debug_str_offsets", ".zdebug_str_offsets" },
307 { ".debug_line_str", ".zdebug_line_str" },
308 { ".debug_ranges", ".zdebug_ranges" },
309 { ".debug_rnglists", ".zdebug_rnglists" },
310 { ".debug_types", ".zdebug_types" },
311 { ".debug_addr", ".zdebug_addr" },
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
314 { ".gdb_index", ".zgdb_index" },
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
317 23
318 };
319
320 /* List of DWO/DWP sections. */
321
322 static const struct dwop_section_names
323 {
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
328 struct dwarf2_section_names loclists_dwo;
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
336 }
337 dwop_section_names =
338 {
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
351 };
352
353 /* local data types */
354
355 /* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357 struct loclist_header
358 {
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
425
426 /* Base address of this compilation unit. */
427 gdb::optional<CORE_ADDR> base_address;
428
429 /* The language we are debugging. */
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
432
433 const char *producer = nullptr;
434
435 private:
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
438 std::unique_ptr<buildsym_compunit> m_builder;
439
440 public:
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
450 struct pending **list_in_scope = nullptr;
451
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
454 htab_t partial_dies = nullptr;
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
458 auto_obstack comp_unit_obstack;
459
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
465
466 /* Backlink to our per_cu entry. */
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
470 int last_used = 0;
471
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
474 htab_t die_hash = nullptr;
475
476 /* Full DIEs if read in. */
477 struct die_info *dies = nullptr;
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
482 htab_t dependencies = nullptr;
483
484 /* Header data from the line table, during full symbol processing. */
485 struct line_header *line_header = nullptr;
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
491 die_info *line_header_die_owner = nullptr;
492
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
495 std::vector<delayed_method_info> method_list;
496
497 /* To be copied to symtab->call_site_htab. */
498 htab_t call_site_htab = nullptr;
499
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
509 struct dwo_unit *dwo_unit = nullptr;
510
511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
512 Note this value comes from the Fission stub CU/TU's DIE. */
513 gdb::optional<ULONGEST> addr_base;
514
515 /* The DW_AT_rnglists_base attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE.
517 Also note that the value is zero in the non-DWO case so this value can
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
524 ULONGEST ranges_base = 0;
525
526 /* The DW_AT_loclists_base attribute if present. */
527 ULONGEST loclist_base = 0;
528
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
543 /* Mark used when releasing cached dies. */
544 bool mark : 1;
545
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
550 bool has_loclist : 1;
551
552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
559 bool producer_is_icc : 1;
560 bool producer_is_icc_lt_14 : 1;
561 bool producer_is_codewarrior : 1;
562
563 /* When true, the file that we're processing is known to have
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
567 bool processing_has_namespace_info : 1;
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
588 };
589
590 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593 struct stmt_list_hash
594 {
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
599 sect_offset line_sect_off;
600 };
601
602 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
603 an object of this type. */
604
605 struct type_unit_group
606 {
607 /* dwarf2read.c's main "handle" on a TU symtab.
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
610 a "per_cu" handle on the symtab. */
611 struct dwarf2_per_cu_data per_cu;
612
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
616 std::vector<signatured_type *> *tus;
617
618 /* The compunit symtab.
619 Type units in a group needn't all be defined in the same source file,
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
622
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
625
626 /* The symbol tables for this TU (obtained from the files listed in
627 DW_AT_stmt_list).
628 WARNING: The order of entries here must match the order of entries
629 in the line header. After the first TU using this type_unit_group, the
630 line header for the subsequent TUs is recreated from this. This is done
631 because we need to use the same symtabs for each TU using the same
632 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
633 there's no guarantee the line header doesn't have duplicate entries. */
634 struct symtab **symtabs;
635 };
636
637 /* These sections are what may appear in a (real or virtual) DWO file. */
638
639 struct dwo_sections
640 {
641 struct dwarf2_section_info abbrev;
642 struct dwarf2_section_info line;
643 struct dwarf2_section_info loc;
644 struct dwarf2_section_info loclists;
645 struct dwarf2_section_info macinfo;
646 struct dwarf2_section_info macro;
647 struct dwarf2_section_info str;
648 struct dwarf2_section_info str_offsets;
649 /* In the case of a virtual DWO file, these two are unused. */
650 struct dwarf2_section_info info;
651 std::vector<dwarf2_section_info> types;
652 };
653
654 /* CUs/TUs in DWP/DWO files. */
655
656 struct dwo_unit
657 {
658 /* Backlink to the containing struct dwo_file. */
659 struct dwo_file *dwo_file;
660
661 /* The "id" that distinguishes this CU/TU.
662 .debug_info calls this "dwo_id", .debug_types calls this "signature".
663 Since signatures came first, we stick with it for consistency. */
664 ULONGEST signature;
665
666 /* The section this CU/TU lives in, in the DWO file. */
667 struct dwarf2_section_info *section;
668
669 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
670 sect_offset sect_off;
671 unsigned int length;
672
673 /* For types, offset in the type's DIE of the type defined by this TU. */
674 cu_offset type_offset_in_tu;
675 };
676
677 /* include/dwarf2.h defines the DWP section codes.
678 It defines a max value but it doesn't define a min value, which we
679 use for error checking, so provide one. */
680
681 enum dwp_v2_section_ids
682 {
683 DW_SECT_MIN = 1
684 };
685
686 /* Data for one DWO file.
687
688 This includes virtual DWO files (a virtual DWO file is a DWO file as it
689 appears in a DWP file). DWP files don't really have DWO files per se -
690 comdat folding of types "loses" the DWO file they came from, and from
691 a high level view DWP files appear to contain a mass of random types.
692 However, to maintain consistency with the non-DWP case we pretend DWP
693 files contain virtual DWO files, and we assign each TU with one virtual
694 DWO file (generally based on the line and abbrev section offsets -
695 a heuristic that seems to work in practice). */
696
697 struct dwo_file
698 {
699 dwo_file () = default;
700 DISABLE_COPY_AND_ASSIGN (dwo_file);
701
702 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
703 For virtual DWO files the name is constructed from the section offsets
704 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
705 from related CU+TUs. */
706 const char *dwo_name = nullptr;
707
708 /* The DW_AT_comp_dir attribute. */
709 const char *comp_dir = nullptr;
710
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
713 gdb_bfd_ref_ptr dbfd;
714
715 /* The sections that make up this DWO file.
716 Remember that for virtual DWO files in DWP V2, these are virtual
717 sections (for lack of a better name). */
718 struct dwo_sections sections {};
719
720 /* The CUs in the file.
721 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
722 an extension to handle LLVM's Link Time Optimization output (where
723 multiple source files may be compiled into a single object/dwo pair). */
724 htab_up cus;
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
728 htab_up tus;
729 };
730
731 /* These sections are what may appear in a DWP file. */
732
733 struct dwp_sections
734 {
735 /* These are used by both DWP version 1 and 2. */
736 struct dwarf2_section_info str;
737 struct dwarf2_section_info cu_index;
738 struct dwarf2_section_info tu_index;
739
740 /* These are only used by DWP version 2 files.
741 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
742 sections are referenced by section number, and are not recorded here.
743 In DWP version 2 there is at most one copy of all these sections, each
744 section being (effectively) comprised of the concatenation of all of the
745 individual sections that exist in the version 1 format.
746 To keep the code simple we treat each of these concatenated pieces as a
747 section itself (a virtual section?). */
748 struct dwarf2_section_info abbrev;
749 struct dwarf2_section_info info;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str_offsets;
755 struct dwarf2_section_info types;
756 };
757
758 /* These sections are what may appear in a virtual DWO file in DWP version 1.
759 A virtual DWO file is a DWO file as it appears in a DWP file. */
760
761 struct virtual_v1_dwo_sections
762 {
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 /* Each DWP hash table entry records one CU or one TU.
770 That is recorded here, and copied to dwo_unit.section. */
771 struct dwarf2_section_info info_or_types;
772 };
773
774 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
775 In version 2, the sections of the DWO files are concatenated together
776 and stored in one section of that name. Thus each ELF section contains
777 several "virtual" sections. */
778
779 struct virtual_v2_dwo_sections
780 {
781 bfd_size_type abbrev_offset;
782 bfd_size_type abbrev_size;
783
784 bfd_size_type line_offset;
785 bfd_size_type line_size;
786
787 bfd_size_type loc_offset;
788 bfd_size_type loc_size;
789
790 bfd_size_type macinfo_offset;
791 bfd_size_type macinfo_size;
792
793 bfd_size_type macro_offset;
794 bfd_size_type macro_size;
795
796 bfd_size_type str_offsets_offset;
797 bfd_size_type str_offsets_size;
798
799 /* Each DWP hash table entry records one CU or one TU.
800 That is recorded here, and copied to dwo_unit.section. */
801 bfd_size_type info_or_types_offset;
802 bfd_size_type info_or_types_size;
803 };
804
805 /* Contents of DWP hash tables. */
806
807 struct dwp_hash_table
808 {
809 uint32_t version, nr_columns;
810 uint32_t nr_units, nr_slots;
811 const gdb_byte *hash_table, *unit_table;
812 union
813 {
814 struct
815 {
816 const gdb_byte *indices;
817 } v1;
818 struct
819 {
820 /* This is indexed by column number and gives the id of the section
821 in that column. */
822 #define MAX_NR_V2_DWO_SECTIONS \
823 (1 /* .debug_info or .debug_types */ \
824 + 1 /* .debug_abbrev */ \
825 + 1 /* .debug_line */ \
826 + 1 /* .debug_loc */ \
827 + 1 /* .debug_str_offsets */ \
828 + 1 /* .debug_macro or .debug_macinfo */)
829 int section_ids[MAX_NR_V2_DWO_SECTIONS];
830 const gdb_byte *offsets;
831 const gdb_byte *sizes;
832 } v2;
833 } section_pool;
834 };
835
836 /* Data for one DWP file. */
837
838 struct dwp_file
839 {
840 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
841 : name (name_),
842 dbfd (std::move (abfd))
843 {
844 }
845
846 /* Name of the file. */
847 const char *name;
848
849 /* File format version. */
850 int version = 0;
851
852 /* The bfd. */
853 gdb_bfd_ref_ptr dbfd;
854
855 /* Section info for this file. */
856 struct dwp_sections sections {};
857
858 /* Table of CUs in the file. */
859 const struct dwp_hash_table *cus = nullptr;
860
861 /* Table of TUs in the file. */
862 const struct dwp_hash_table *tus = nullptr;
863
864 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
865 htab_up loaded_cus;
866 htab_up loaded_tus;
867
868 /* Table to map ELF section numbers to their sections.
869 This is only needed for the DWP V1 file format. */
870 unsigned int num_sections = 0;
871 asection **elf_sections = nullptr;
872 };
873
874 /* Struct used to pass misc. parameters to read_die_and_children, et
875 al. which are used for both .debug_info and .debug_types dies.
876 All parameters here are unchanging for the life of the call. This
877 struct exists to abstract away the constant parameters of die reading. */
878
879 struct die_reader_specs
880 {
881 /* The bfd of die_section. */
882 bfd* abfd;
883
884 /* The CU of the DIE we are parsing. */
885 struct dwarf2_cu *cu;
886
887 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
888 struct dwo_file *dwo_file;
889
890 /* The section the die comes from.
891 This is either .debug_info or .debug_types, or the .dwo variants. */
892 struct dwarf2_section_info *die_section;
893
894 /* die_section->buffer. */
895 const gdb_byte *buffer;
896
897 /* The end of the buffer. */
898 const gdb_byte *buffer_end;
899
900 /* The abbreviation table to use when reading the DIEs. */
901 struct abbrev_table *abbrev_table;
902 };
903
904 /* A subclass of die_reader_specs that holds storage and has complex
905 constructor and destructor behavior. */
906
907 class cutu_reader : public die_reader_specs
908 {
909 public:
910
911 cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 struct abbrev_table *abbrev_table,
913 int use_existing_cu,
914 bool skip_partial);
915
916 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
917 struct dwarf2_cu *parent_cu = nullptr,
918 struct dwo_file *dwo_file = nullptr);
919
920 DISABLE_COPY_AND_ASSIGN (cutu_reader);
921
922 const gdb_byte *info_ptr = nullptr;
923 struct die_info *comp_unit_die = nullptr;
924 bool dummy_p = false;
925
926 /* Release the new CU, putting it on the chain. This cannot be done
927 for dummy CUs. */
928 void keep ();
929
930 private:
931 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
932 int use_existing_cu);
933
934 struct dwarf2_per_cu_data *m_this_cu;
935 std::unique_ptr<dwarf2_cu> m_new_cu;
936
937 /* The ordinary abbreviation table. */
938 abbrev_table_up m_abbrev_table_holder;
939
940 /* The DWO abbreviation table. */
941 abbrev_table_up m_dwo_abbrev_table;
942 };
943
944 /* When we construct a partial symbol table entry we only
945 need this much information. */
946 struct partial_die_info : public allocate_on_obstack
947 {
948 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
949
950 /* Disable assign but still keep copy ctor, which is needed
951 load_partial_dies. */
952 partial_die_info& operator=(const partial_die_info& rhs) = delete;
953
954 /* Adjust the partial die before generating a symbol for it. This
955 function may set the is_external flag or change the DIE's
956 name. */
957 void fixup (struct dwarf2_cu *cu);
958
959 /* Read a minimal amount of information into the minimal die
960 structure. */
961 const gdb_byte *read (const struct die_reader_specs *reader,
962 const struct abbrev_info &abbrev,
963 const gdb_byte *info_ptr);
964
965 /* Offset of this DIE. */
966 const sect_offset sect_off;
967
968 /* DWARF-2 tag for this DIE. */
969 const ENUM_BITFIELD(dwarf_tag) tag : 16;
970
971 /* Assorted flags describing the data found in this DIE. */
972 const unsigned int has_children : 1;
973
974 unsigned int is_external : 1;
975 unsigned int is_declaration : 1;
976 unsigned int has_type : 1;
977 unsigned int has_specification : 1;
978 unsigned int has_pc_info : 1;
979 unsigned int may_be_inlined : 1;
980
981 /* This DIE has been marked DW_AT_main_subprogram. */
982 unsigned int main_subprogram : 1;
983
984 /* Flag set if the SCOPE field of this structure has been
985 computed. */
986 unsigned int scope_set : 1;
987
988 /* Flag set if the DIE has a byte_size attribute. */
989 unsigned int has_byte_size : 1;
990
991 /* Flag set if the DIE has a DW_AT_const_value attribute. */
992 unsigned int has_const_value : 1;
993
994 /* Flag set if any of the DIE's children are template arguments. */
995 unsigned int has_template_arguments : 1;
996
997 /* Flag set if fixup has been called on this die. */
998 unsigned int fixup_called : 1;
999
1000 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1001 unsigned int is_dwz : 1;
1002
1003 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1004 unsigned int spec_is_dwz : 1;
1005
1006 /* The name of this DIE. Normally the value of DW_AT_name, but
1007 sometimes a default name for unnamed DIEs. */
1008 const char *name = nullptr;
1009
1010 /* The linkage name, if present. */
1011 const char *linkage_name = nullptr;
1012
1013 /* The scope to prepend to our children. This is generally
1014 allocated on the comp_unit_obstack, so will disappear
1015 when this compilation unit leaves the cache. */
1016 const char *scope = nullptr;
1017
1018 /* Some data associated with the partial DIE. The tag determines
1019 which field is live. */
1020 union
1021 {
1022 /* The location description associated with this DIE, if any. */
1023 struct dwarf_block *locdesc;
1024 /* The offset of an import, for DW_TAG_imported_unit. */
1025 sect_offset sect_off;
1026 } d {};
1027
1028 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1029 CORE_ADDR lowpc = 0;
1030 CORE_ADDR highpc = 0;
1031
1032 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1033 DW_AT_sibling, if any. */
1034 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1035 could return DW_AT_sibling values to its caller load_partial_dies. */
1036 const gdb_byte *sibling = nullptr;
1037
1038 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1039 DW_AT_specification (or DW_AT_abstract_origin or
1040 DW_AT_extension). */
1041 sect_offset spec_offset {};
1042
1043 /* Pointers to this DIE's parent, first child, and next sibling,
1044 if any. */
1045 struct partial_die_info *die_parent = nullptr;
1046 struct partial_die_info *die_child = nullptr;
1047 struct partial_die_info *die_sibling = nullptr;
1048
1049 friend struct partial_die_info *
1050 dwarf2_cu::find_partial_die (sect_offset sect_off);
1051
1052 private:
1053 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1054 partial_die_info (sect_offset sect_off)
1055 : partial_die_info (sect_off, DW_TAG_padding, 0)
1056 {
1057 }
1058
1059 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1060 int has_children_)
1061 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1062 {
1063 is_external = 0;
1064 is_declaration = 0;
1065 has_type = 0;
1066 has_specification = 0;
1067 has_pc_info = 0;
1068 may_be_inlined = 0;
1069 main_subprogram = 0;
1070 scope_set = 0;
1071 has_byte_size = 0;
1072 has_const_value = 0;
1073 has_template_arguments = 0;
1074 fixup_called = 0;
1075 is_dwz = 0;
1076 spec_is_dwz = 0;
1077 }
1078 };
1079
1080 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1081 but this would require a corresponding change in unpack_field_as_long
1082 and friends. */
1083 static int bits_per_byte = 8;
1084
1085 struct variant_part_builder;
1086
1087 /* When reading a variant, we track a bit more information about the
1088 field, and store it in an object of this type. */
1089
1090 struct variant_field
1091 {
1092 int first_field = -1;
1093 int last_field = -1;
1094
1095 /* A variant can contain other variant parts. */
1096 std::vector<variant_part_builder> variant_parts;
1097
1098 /* If we see a DW_TAG_variant, then this will be set if this is the
1099 default branch. */
1100 bool default_branch = false;
1101 /* If we see a DW_AT_discr_value, then this will be the discriminant
1102 value. */
1103 ULONGEST discriminant_value = 0;
1104 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1105 data. */
1106 struct dwarf_block *discr_list_data = nullptr;
1107 };
1108
1109 /* This represents a DW_TAG_variant_part. */
1110
1111 struct variant_part_builder
1112 {
1113 /* The offset of the discriminant field. */
1114 sect_offset discriminant_offset {};
1115
1116 /* Variants that are direct children of this variant part. */
1117 std::vector<variant_field> variants;
1118
1119 /* True if we're currently reading a variant. */
1120 bool processing_variant = false;
1121 };
1122
1123 struct nextfield
1124 {
1125 int accessibility = 0;
1126 int virtuality = 0;
1127 /* Variant parts need to find the discriminant, which is a DIE
1128 reference. We track the section offset of each field to make
1129 this link. */
1130 sect_offset offset;
1131 struct field field {};
1132 };
1133
1134 struct fnfieldlist
1135 {
1136 const char *name = nullptr;
1137 std::vector<struct fn_field> fnfields;
1138 };
1139
1140 /* The routines that read and process dies for a C struct or C++ class
1141 pass lists of data member fields and lists of member function fields
1142 in an instance of a field_info structure, as defined below. */
1143 struct field_info
1144 {
1145 /* List of data member and baseclasses fields. */
1146 std::vector<struct nextfield> fields;
1147 std::vector<struct nextfield> baseclasses;
1148
1149 /* Set if the accessibility of one of the fields is not public. */
1150 int non_public_fields = 0;
1151
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
1155 std::vector<struct fnfieldlist> fnfieldlists;
1156
1157 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1158 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1159 std::vector<struct decl_field> typedef_field_list;
1160
1161 /* Nested types defined by this class and the number of elements in this
1162 list. */
1163 std::vector<struct decl_field> nested_types_list;
1164
1165 /* If non-null, this is the variant part we are currently
1166 reading. */
1167 variant_part_builder *current_variant_part = nullptr;
1168 /* This holds all the top-level variant parts attached to the type
1169 we're reading. */
1170 std::vector<variant_part_builder> variant_parts;
1171
1172 /* Return the total number of fields (including baseclasses). */
1173 int nfields () const
1174 {
1175 return fields.size () + baseclasses.size ();
1176 }
1177 };
1178
1179 /* Loaded secondary compilation units are kept in memory until they
1180 have not been referenced for the processing of this many
1181 compilation units. Set this to zero to disable caching. Cache
1182 sizes of up to at least twenty will improve startup time for
1183 typical inter-CU-reference binaries, at an obvious memory cost. */
1184 static int dwarf_max_cache_age = 5;
1185 static void
1186 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1187 struct cmd_list_element *c, const char *value)
1188 {
1189 fprintf_filtered (file, _("The upper bound on the age of cached "
1190 "DWARF compilation units is %s.\n"),
1191 value);
1192 }
1193 \f
1194 /* local function prototypes */
1195
1196 static void dwarf2_find_base_address (struct die_info *die,
1197 struct dwarf2_cu *cu);
1198
1199 static dwarf2_psymtab *create_partial_symtab
1200 (struct dwarf2_per_cu_data *per_cu, const char *name);
1201
1202 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1203 const gdb_byte *info_ptr,
1204 struct die_info *type_unit_die);
1205
1206 static void dwarf2_build_psymtabs_hard
1207 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1208
1209 static void scan_partial_symbols (struct partial_die_info *,
1210 CORE_ADDR *, CORE_ADDR *,
1211 int, struct dwarf2_cu *);
1212
1213 static void add_partial_symbol (struct partial_die_info *,
1214 struct dwarf2_cu *);
1215
1216 static void add_partial_namespace (struct partial_die_info *pdi,
1217 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1218 int set_addrmap, struct dwarf2_cu *cu);
1219
1220 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1221 CORE_ADDR *highpc, int set_addrmap,
1222 struct dwarf2_cu *cu);
1223
1224 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1225 struct dwarf2_cu *cu);
1226
1227 static void add_partial_subprogram (struct partial_die_info *pdi,
1228 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1229 int need_pc, struct dwarf2_cu *cu);
1230
1231 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1232
1233 static struct partial_die_info *load_partial_dies
1234 (const struct die_reader_specs *, const gdb_byte *, int);
1235
1236 /* A pair of partial_die_info and compilation unit. */
1237 struct cu_partial_die_info
1238 {
1239 /* The compilation unit of the partial_die_info. */
1240 struct dwarf2_cu *cu;
1241 /* A partial_die_info. */
1242 struct partial_die_info *pdi;
1243
1244 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1245 : cu (cu),
1246 pdi (pdi)
1247 { /* Nothing. */ }
1248
1249 private:
1250 cu_partial_die_info () = delete;
1251 };
1252
1253 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1254 struct dwarf2_cu *);
1255
1256 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1257 struct attribute *, struct attr_abbrev *,
1258 const gdb_byte *, bool *need_reprocess);
1259
1260 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1261 struct attribute *attr);
1262
1263 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1264
1265 static sect_offset read_abbrev_offset
1266 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1267 struct dwarf2_section_info *, sect_offset);
1268
1269 static const char *read_indirect_string
1270 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1271 const struct comp_unit_head *, unsigned int *);
1272
1273 static const char *read_indirect_string_at_offset
1274 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1275
1276 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1277 const gdb_byte *,
1278 unsigned int *);
1279
1280 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1281 ULONGEST str_index);
1282
1283 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1284 ULONGEST str_index);
1285
1286 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1287
1288 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1289 struct dwarf2_cu *);
1290
1291 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1292 struct dwarf2_cu *cu);
1293
1294 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1295
1296 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1297 struct dwarf2_cu *cu);
1298
1299 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1300
1301 static struct die_info *die_specification (struct die_info *die,
1302 struct dwarf2_cu **);
1303
1304 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1305 struct dwarf2_cu *cu);
1306
1307 static void dwarf_decode_lines (struct line_header *, const char *,
1308 struct dwarf2_cu *, dwarf2_psymtab *,
1309 CORE_ADDR, int decode_mapping);
1310
1311 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1312 const char *);
1313
1314 static struct symbol *new_symbol (struct die_info *, struct type *,
1315 struct dwarf2_cu *, struct symbol * = NULL);
1316
1317 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1318 struct dwarf2_cu *);
1319
1320 static void dwarf2_const_value_attr (const struct attribute *attr,
1321 struct type *type,
1322 const char *name,
1323 struct obstack *obstack,
1324 struct dwarf2_cu *cu, LONGEST *value,
1325 const gdb_byte **bytes,
1326 struct dwarf2_locexpr_baton **baton);
1327
1328 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1329
1330 static int need_gnat_info (struct dwarf2_cu *);
1331
1332 static struct type *die_descriptive_type (struct die_info *,
1333 struct dwarf2_cu *);
1334
1335 static void set_descriptive_type (struct type *, struct die_info *,
1336 struct dwarf2_cu *);
1337
1338 static struct type *die_containing_type (struct die_info *,
1339 struct dwarf2_cu *);
1340
1341 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1342 struct dwarf2_cu *);
1343
1344 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1345
1346 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1347
1348 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1349
1350 static char *typename_concat (struct obstack *obs, const char *prefix,
1351 const char *suffix, int physname,
1352 struct dwarf2_cu *cu);
1353
1354 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1355
1356 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1357
1358 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1359
1360 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1361
1362 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1363
1364 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1365
1366 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1367 struct dwarf2_cu *, dwarf2_psymtab *);
1368
1369 /* Return the .debug_loclists section to use for cu. */
1370 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1371
1372 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1373 values. Keep the items ordered with increasing constraints compliance. */
1374 enum pc_bounds_kind
1375 {
1376 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1377 PC_BOUNDS_NOT_PRESENT,
1378
1379 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1380 were present but they do not form a valid range of PC addresses. */
1381 PC_BOUNDS_INVALID,
1382
1383 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1384 PC_BOUNDS_RANGES,
1385
1386 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1387 PC_BOUNDS_HIGH_LOW,
1388 };
1389
1390 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1391 CORE_ADDR *, CORE_ADDR *,
1392 struct dwarf2_cu *,
1393 dwarf2_psymtab *);
1394
1395 static void get_scope_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *);
1398
1399 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1400 CORE_ADDR, struct dwarf2_cu *);
1401
1402 static void dwarf2_add_field (struct field_info *, struct die_info *,
1403 struct dwarf2_cu *);
1404
1405 static void dwarf2_attach_fields_to_type (struct field_info *,
1406 struct type *, struct dwarf2_cu *);
1407
1408 static void dwarf2_add_member_fn (struct field_info *,
1409 struct die_info *, struct type *,
1410 struct dwarf2_cu *);
1411
1412 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1413 struct type *,
1414 struct dwarf2_cu *);
1415
1416 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1417
1418 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1419
1420 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1421
1422 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1423
1424 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1425
1426 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1427
1428 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1429
1430 static struct type *read_module_type (struct die_info *die,
1431 struct dwarf2_cu *cu);
1432
1433 static const char *namespace_name (struct die_info *die,
1434 int *is_anonymous, struct dwarf2_cu *);
1435
1436 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1437
1438 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1439 bool * = nullptr);
1440
1441 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1442 struct dwarf2_cu *);
1443
1444 static struct die_info *read_die_and_siblings_1
1445 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1446 struct die_info *);
1447
1448 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1449 const gdb_byte *info_ptr,
1450 const gdb_byte **new_info_ptr,
1451 struct die_info *parent);
1452
1453 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1454 struct die_info **, const gdb_byte *,
1455 int);
1456
1457 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1458 struct die_info **, const gdb_byte *);
1459
1460 static void process_die (struct die_info *, struct dwarf2_cu *);
1461
1462 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1463 struct objfile *);
1464
1465 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1466
1467 static const char *dwarf2_full_name (const char *name,
1468 struct die_info *die,
1469 struct dwarf2_cu *cu);
1470
1471 static const char *dwarf2_physname (const char *name, struct die_info *die,
1472 struct dwarf2_cu *cu);
1473
1474 static struct die_info *dwarf2_extension (struct die_info *die,
1475 struct dwarf2_cu **);
1476
1477 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1478
1479 static void dump_die_for_error (struct die_info *);
1480
1481 static void dump_die_1 (struct ui_file *, int level, int max_level,
1482 struct die_info *);
1483
1484 /*static*/ void dump_die (struct die_info *, int max_level);
1485
1486 static void store_in_ref_table (struct die_info *,
1487 struct dwarf2_cu *);
1488
1489 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1490 const struct attribute *,
1491 struct dwarf2_cu **);
1492
1493 static struct die_info *follow_die_ref (struct die_info *,
1494 const struct attribute *,
1495 struct dwarf2_cu **);
1496
1497 static struct die_info *follow_die_sig (struct die_info *,
1498 const struct attribute *,
1499 struct dwarf2_cu **);
1500
1501 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1502 struct dwarf2_cu *);
1503
1504 static struct type *get_DW_AT_signature_type (struct die_info *,
1505 const struct attribute *,
1506 struct dwarf2_cu *);
1507
1508 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1509
1510 static void read_signatured_type (struct signatured_type *);
1511
1512 static int attr_to_dynamic_prop (const struct attribute *attr,
1513 struct die_info *die, struct dwarf2_cu *cu,
1514 struct dynamic_prop *prop, struct type *type);
1515
1516 /* memory allocation interface */
1517
1518 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1519
1520 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1521
1522 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1523
1524 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
1526 const struct attribute *attr);
1527
1528 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1529 struct symbol *sym,
1530 struct dwarf2_cu *cu,
1531 int is_block);
1532
1533 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1534 const gdb_byte *info_ptr,
1535 struct abbrev_info *abbrev);
1536
1537 static hashval_t partial_die_hash (const void *item);
1538
1539 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
1541 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1542 (sect_offset sect_off, unsigned int offset_in_dwz,
1543 struct dwarf2_per_objfile *dwarf2_per_objfile);
1544
1545 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1546 struct die_info *comp_unit_die,
1547 enum language pretend_language);
1548
1549 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1550
1551 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1552
1553 static struct type *set_die_type (struct die_info *, struct type *,
1554 struct dwarf2_cu *);
1555
1556 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1557
1558 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1559
1560 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1561 enum language);
1562
1563 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1564 enum language);
1565
1566 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
1569 static void dwarf2_add_dependence (struct dwarf2_cu *,
1570 struct dwarf2_per_cu_data *);
1571
1572 static void dwarf2_mark (struct dwarf2_cu *);
1573
1574 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1575
1576 static struct type *get_die_type_at_offset (sect_offset,
1577 struct dwarf2_per_cu_data *);
1578
1579 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1580
1581 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1582 enum language pretend_language);
1583
1584 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1585
1586 /* Class, the destructor of which frees all allocated queue entries. This
1587 will only have work to do if an error was thrown while processing the
1588 dwarf. If no error was thrown then the queue entries should have all
1589 been processed, and freed, as we went along. */
1590
1591 class dwarf2_queue_guard
1592 {
1593 public:
1594 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1595 : m_per_objfile (per_objfile)
1596 {
1597 }
1598
1599 /* Free any entries remaining on the queue. There should only be
1600 entries left if we hit an error while processing the dwarf. */
1601 ~dwarf2_queue_guard ()
1602 {
1603 /* Ensure that no memory is allocated by the queue. */
1604 std::queue<dwarf2_queue_item> empty;
1605 std::swap (m_per_objfile->per_bfd->queue, empty);
1606 }
1607
1608 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1609
1610 private:
1611 dwarf2_per_objfile *m_per_objfile;
1612 };
1613
1614 dwarf2_queue_item::~dwarf2_queue_item ()
1615 {
1616 /* Anything still marked queued is likely to be in an
1617 inconsistent state, so discard it. */
1618 if (per_cu->queued)
1619 {
1620 if (per_cu->cu != NULL)
1621 free_one_cached_comp_unit (per_cu);
1622 per_cu->queued = 0;
1623 }
1624 }
1625
1626 /* The return type of find_file_and_directory. Note, the enclosed
1627 string pointers are only valid while this object is valid. */
1628
1629 struct file_and_directory
1630 {
1631 /* The filename. This is never NULL. */
1632 const char *name;
1633
1634 /* The compilation directory. NULL if not known. If we needed to
1635 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1636 points directly to the DW_AT_comp_dir string attribute owned by
1637 the obstack that owns the DIE. */
1638 const char *comp_dir;
1639
1640 /* If we needed to build a new string for comp_dir, this is what
1641 owns the storage. */
1642 std::string comp_dir_storage;
1643 };
1644
1645 static file_and_directory find_file_and_directory (struct die_info *die,
1646 struct dwarf2_cu *cu);
1647
1648 static htab_up allocate_signatured_type_table ();
1649
1650 static htab_up allocate_dwo_unit_table ();
1651
1652 static struct dwo_unit *lookup_dwo_unit_in_dwp
1653 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1654 struct dwp_file *dwp_file, const char *comp_dir,
1655 ULONGEST signature, int is_debug_types);
1656
1657 static struct dwp_file *get_dwp_file
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1659
1660 static struct dwo_unit *lookup_dwo_comp_unit
1661 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1662
1663 static struct dwo_unit *lookup_dwo_type_unit
1664 (struct signatured_type *, const char *, const char *);
1665
1666 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1667
1668 /* A unique pointer to a dwo_file. */
1669
1670 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1671
1672 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1673
1674 static void check_producer (struct dwarf2_cu *cu);
1675
1676 static void free_line_header_voidp (void *arg);
1677 \f
1678 /* Various complaints about symbol reading that don't abort the process. */
1679
1680 static void
1681 dwarf2_debug_line_missing_file_complaint (void)
1682 {
1683 complaint (_(".debug_line section has line data without a file"));
1684 }
1685
1686 static void
1687 dwarf2_debug_line_missing_end_sequence_complaint (void)
1688 {
1689 complaint (_(".debug_line section has line "
1690 "program sequence without an end"));
1691 }
1692
1693 static void
1694 dwarf2_complex_location_expr_complaint (void)
1695 {
1696 complaint (_("location expression too complex"));
1697 }
1698
1699 static void
1700 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1701 int arg3)
1702 {
1703 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1704 arg1, arg2, arg3);
1705 }
1706
1707 static void
1708 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1709 {
1710 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1711 arg1, arg2);
1712 }
1713
1714 /* Hash function for line_header_hash. */
1715
1716 static hashval_t
1717 line_header_hash (const struct line_header *ofs)
1718 {
1719 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1720 }
1721
1722 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1723
1724 static hashval_t
1725 line_header_hash_voidp (const void *item)
1726 {
1727 const struct line_header *ofs = (const struct line_header *) item;
1728
1729 return line_header_hash (ofs);
1730 }
1731
1732 /* Equality function for line_header_hash. */
1733
1734 static int
1735 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1736 {
1737 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1738 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1739
1740 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1741 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1742 }
1743
1744 \f
1745
1746 /* See declaration. */
1747
1748 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1749 bool can_copy_)
1750 : can_copy (can_copy_)
1751 {
1752 if (names == NULL)
1753 names = &dwarf2_elf_names;
1754
1755 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1756 locate_sections (obfd, sec, *names);
1757 }
1758
1759 dwarf2_per_bfd::~dwarf2_per_bfd ()
1760 {
1761 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1762 free_cached_comp_units ();
1763
1764 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1765 per_cu->imported_symtabs_free ();
1766
1767 for (signatured_type *sig_type : all_type_units)
1768 sig_type->per_cu.imported_symtabs_free ();
1769
1770 /* Everything else should be on this->obstack. */
1771 }
1772
1773 /* See declaration. */
1774
1775 void
1776 dwarf2_per_bfd::free_cached_comp_units ()
1777 {
1778 dwarf2_per_cu_data *per_cu = read_in_chain;
1779 dwarf2_per_cu_data **last_chain = &read_in_chain;
1780 while (per_cu != NULL)
1781 {
1782 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1783
1784 delete per_cu->cu;
1785 *last_chain = next_cu;
1786 per_cu = next_cu;
1787 }
1788 }
1789
1790 /* A helper class that calls free_cached_comp_units on
1791 destruction. */
1792
1793 class free_cached_comp_units
1794 {
1795 public:
1796
1797 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1798 : m_per_objfile (per_objfile)
1799 {
1800 }
1801
1802 ~free_cached_comp_units ()
1803 {
1804 m_per_objfile->per_bfd->free_cached_comp_units ();
1805 }
1806
1807 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1808
1809 private:
1810
1811 dwarf2_per_objfile *m_per_objfile;
1812 };
1813
1814 /* See read.h. */
1815
1816 bool
1817 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1818 {
1819 gdb_assert (per_cu->index < this->m_symtabs.size ());
1820
1821 return this->m_symtabs[per_cu->index] != nullptr;
1822 }
1823
1824 /* See read.h. */
1825
1826 compunit_symtab *
1827 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1828 {
1829 gdb_assert (per_cu->index < this->m_symtabs.size ());
1830
1831 return this->m_symtabs[per_cu->index];
1832 }
1833
1834 /* See read.h. */
1835
1836 void
1837 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1838 compunit_symtab *symtab)
1839 {
1840 gdb_assert (per_cu->index < this->m_symtabs.size ());
1841 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1842
1843 this->m_symtabs[per_cu->index] = symtab;
1844 }
1845
1846 /* Try to locate the sections we need for DWARF 2 debugging
1847 information and return true if we have enough to do something.
1848 NAMES points to the dwarf2 section names, or is NULL if the standard
1849 ELF names are used. CAN_COPY is true for formats where symbol
1850 interposition is possible and so symbol values must follow copy
1851 relocation rules. */
1852
1853 int
1854 dwarf2_has_info (struct objfile *objfile,
1855 const struct dwarf2_debug_sections *names,
1856 bool can_copy)
1857 {
1858 if (objfile->flags & OBJF_READNEVER)
1859 return 0;
1860
1861 struct dwarf2_per_objfile *dwarf2_per_objfile
1862 = get_dwarf2_per_objfile (objfile);
1863
1864 if (dwarf2_per_objfile == NULL)
1865 {
1866 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1867 sharing yet). */
1868 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1869
1870 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1871 }
1872
1873 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1874 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1875 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1876 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1877 }
1878
1879 /* When loading sections, we look either for uncompressed section or for
1880 compressed section names. */
1881
1882 static int
1883 section_is_p (const char *section_name,
1884 const struct dwarf2_section_names *names)
1885 {
1886 if (names->normal != NULL
1887 && strcmp (section_name, names->normal) == 0)
1888 return 1;
1889 if (names->compressed != NULL
1890 && strcmp (section_name, names->compressed) == 0)
1891 return 1;
1892 return 0;
1893 }
1894
1895 /* See declaration. */
1896
1897 void
1898 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1899 const dwarf2_debug_sections &names)
1900 {
1901 flagword aflag = bfd_section_flags (sectp);
1902
1903 if ((aflag & SEC_HAS_CONTENTS) == 0)
1904 {
1905 }
1906 else if (elf_section_data (sectp)->this_hdr.sh_size
1907 > bfd_get_file_size (abfd))
1908 {
1909 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1910 warning (_("Discarding section %s which has a section size (%s"
1911 ") larger than the file size [in module %s]"),
1912 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1913 bfd_get_filename (abfd));
1914 }
1915 else if (section_is_p (sectp->name, &names.info))
1916 {
1917 this->info.s.section = sectp;
1918 this->info.size = bfd_section_size (sectp);
1919 }
1920 else if (section_is_p (sectp->name, &names.abbrev))
1921 {
1922 this->abbrev.s.section = sectp;
1923 this->abbrev.size = bfd_section_size (sectp);
1924 }
1925 else if (section_is_p (sectp->name, &names.line))
1926 {
1927 this->line.s.section = sectp;
1928 this->line.size = bfd_section_size (sectp);
1929 }
1930 else if (section_is_p (sectp->name, &names.loc))
1931 {
1932 this->loc.s.section = sectp;
1933 this->loc.size = bfd_section_size (sectp);
1934 }
1935 else if (section_is_p (sectp->name, &names.loclists))
1936 {
1937 this->loclists.s.section = sectp;
1938 this->loclists.size = bfd_section_size (sectp);
1939 }
1940 else if (section_is_p (sectp->name, &names.macinfo))
1941 {
1942 this->macinfo.s.section = sectp;
1943 this->macinfo.size = bfd_section_size (sectp);
1944 }
1945 else if (section_is_p (sectp->name, &names.macro))
1946 {
1947 this->macro.s.section = sectp;
1948 this->macro.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.str))
1951 {
1952 this->str.s.section = sectp;
1953 this->str.size = bfd_section_size (sectp);
1954 }
1955 else if (section_is_p (sectp->name, &names.str_offsets))
1956 {
1957 this->str_offsets.s.section = sectp;
1958 this->str_offsets.size = bfd_section_size (sectp);
1959 }
1960 else if (section_is_p (sectp->name, &names.line_str))
1961 {
1962 this->line_str.s.section = sectp;
1963 this->line_str.size = bfd_section_size (sectp);
1964 }
1965 else if (section_is_p (sectp->name, &names.addr))
1966 {
1967 this->addr.s.section = sectp;
1968 this->addr.size = bfd_section_size (sectp);
1969 }
1970 else if (section_is_p (sectp->name, &names.frame))
1971 {
1972 this->frame.s.section = sectp;
1973 this->frame.size = bfd_section_size (sectp);
1974 }
1975 else if (section_is_p (sectp->name, &names.eh_frame))
1976 {
1977 this->eh_frame.s.section = sectp;
1978 this->eh_frame.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.ranges))
1981 {
1982 this->ranges.s.section = sectp;
1983 this->ranges.size = bfd_section_size (sectp);
1984 }
1985 else if (section_is_p (sectp->name, &names.rnglists))
1986 {
1987 this->rnglists.s.section = sectp;
1988 this->rnglists.size = bfd_section_size (sectp);
1989 }
1990 else if (section_is_p (sectp->name, &names.types))
1991 {
1992 struct dwarf2_section_info type_section;
1993
1994 memset (&type_section, 0, sizeof (type_section));
1995 type_section.s.section = sectp;
1996 type_section.size = bfd_section_size (sectp);
1997
1998 this->types.push_back (type_section);
1999 }
2000 else if (section_is_p (sectp->name, &names.gdb_index))
2001 {
2002 this->gdb_index.s.section = sectp;
2003 this->gdb_index.size = bfd_section_size (sectp);
2004 }
2005 else if (section_is_p (sectp->name, &names.debug_names))
2006 {
2007 this->debug_names.s.section = sectp;
2008 this->debug_names.size = bfd_section_size (sectp);
2009 }
2010 else if (section_is_p (sectp->name, &names.debug_aranges))
2011 {
2012 this->debug_aranges.s.section = sectp;
2013 this->debug_aranges.size = bfd_section_size (sectp);
2014 }
2015
2016 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2017 && bfd_section_vma (sectp) == 0)
2018 this->has_section_at_zero = true;
2019 }
2020
2021 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2022 SECTION_NAME. */
2023
2024 void
2025 dwarf2_get_section_info (struct objfile *objfile,
2026 enum dwarf2_section_enum sect,
2027 asection **sectp, const gdb_byte **bufp,
2028 bfd_size_type *sizep)
2029 {
2030 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2031 struct dwarf2_section_info *info;
2032
2033 /* We may see an objfile without any DWARF, in which case we just
2034 return nothing. */
2035 if (data == NULL)
2036 {
2037 *sectp = NULL;
2038 *bufp = NULL;
2039 *sizep = 0;
2040 return;
2041 }
2042 switch (sect)
2043 {
2044 case DWARF2_DEBUG_FRAME:
2045 info = &data->per_bfd->frame;
2046 break;
2047 case DWARF2_EH_FRAME:
2048 info = &data->per_bfd->eh_frame;
2049 break;
2050 default:
2051 gdb_assert_not_reached ("unexpected section");
2052 }
2053
2054 info->read (objfile);
2055
2056 *sectp = info->get_bfd_section ();
2057 *bufp = info->buffer;
2058 *sizep = info->size;
2059 }
2060
2061 /* A helper function to find the sections for a .dwz file. */
2062
2063 static void
2064 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2065 {
2066 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2067
2068 /* Note that we only support the standard ELF names, because .dwz
2069 is ELF-only (at the time of writing). */
2070 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2071 {
2072 dwz_file->abbrev.s.section = sectp;
2073 dwz_file->abbrev.size = bfd_section_size (sectp);
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2076 {
2077 dwz_file->info.s.section = sectp;
2078 dwz_file->info.size = bfd_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2081 {
2082 dwz_file->str.s.section = sectp;
2083 dwz_file->str.size = bfd_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2086 {
2087 dwz_file->line.s.section = sectp;
2088 dwz_file->line.size = bfd_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2091 {
2092 dwz_file->macro.s.section = sectp;
2093 dwz_file->macro.size = bfd_section_size (sectp);
2094 }
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2096 {
2097 dwz_file->gdb_index.s.section = sectp;
2098 dwz_file->gdb_index.size = bfd_section_size (sectp);
2099 }
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2101 {
2102 dwz_file->debug_names.s.section = sectp;
2103 dwz_file->debug_names.size = bfd_section_size (sectp);
2104 }
2105 }
2106
2107 /* See dwarf2read.h. */
2108
2109 struct dwz_file *
2110 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2111 {
2112 const char *filename;
2113 bfd_size_type buildid_len_arg;
2114 size_t buildid_len;
2115 bfd_byte *buildid;
2116
2117 if (dwarf2_per_objfile->per_bfd->dwz_file != NULL)
2118 return dwarf2_per_objfile->per_bfd->dwz_file.get ();
2119
2120 bfd_set_error (bfd_error_no_error);
2121 gdb::unique_xmalloc_ptr<char> data
2122 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2123 &buildid_len_arg, &buildid));
2124 if (data == NULL)
2125 {
2126 if (bfd_get_error () == bfd_error_no_error)
2127 return NULL;
2128 error (_("could not read '.gnu_debugaltlink' section: %s"),
2129 bfd_errmsg (bfd_get_error ()));
2130 }
2131
2132 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2133
2134 buildid_len = (size_t) buildid_len_arg;
2135
2136 filename = data.get ();
2137
2138 std::string abs_storage;
2139 if (!IS_ABSOLUTE_PATH (filename))
2140 {
2141 gdb::unique_xmalloc_ptr<char> abs
2142 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2143
2144 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2145 filename = abs_storage.c_str ();
2146 }
2147
2148 /* First try the file name given in the section. If that doesn't
2149 work, try to use the build-id instead. */
2150 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2151 if (dwz_bfd != NULL)
2152 {
2153 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2154 dwz_bfd.reset (nullptr);
2155 }
2156
2157 if (dwz_bfd == NULL)
2158 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2159
2160 if (dwz_bfd == nullptr)
2161 {
2162 gdb::unique_xmalloc_ptr<char> alt_filename;
2163 const char *origname = dwarf2_per_objfile->objfile->original_name;
2164
2165 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2166 buildid_len,
2167 origname,
2168 &alt_filename));
2169
2170 if (fd.get () >= 0)
2171 {
2172 /* File successfully retrieved from server. */
2173 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2174
2175 if (dwz_bfd == nullptr)
2176 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2177 alt_filename.get ());
2178 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2179 dwz_bfd.reset (nullptr);
2180 }
2181 }
2182
2183 if (dwz_bfd == NULL)
2184 error (_("could not find '.gnu_debugaltlink' file for %s"),
2185 objfile_name (dwarf2_per_objfile->objfile));
2186
2187 std::unique_ptr<struct dwz_file> result
2188 (new struct dwz_file (std::move (dwz_bfd)));
2189
2190 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2191 result.get ());
2192
2193 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2194 result->dwz_bfd.get ());
2195 dwarf2_per_objfile->per_bfd->dwz_file = std::move (result);
2196 return dwarf2_per_objfile->per_bfd->dwz_file.get ();
2197 }
2198 \f
2199 /* DWARF quick_symbols_functions support. */
2200
2201 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2202 unique line tables, so we maintain a separate table of all .debug_line
2203 derived entries to support the sharing.
2204 All the quick functions need is the list of file names. We discard the
2205 line_header when we're done and don't need to record it here. */
2206 struct quick_file_names
2207 {
2208 /* The data used to construct the hash key. */
2209 struct stmt_list_hash hash;
2210
2211 /* The number of entries in file_names, real_names. */
2212 unsigned int num_file_names;
2213
2214 /* The file names from the line table, after being run through
2215 file_full_name. */
2216 const char **file_names;
2217
2218 /* The file names from the line table after being run through
2219 gdb_realpath. These are computed lazily. */
2220 const char **real_names;
2221 };
2222
2223 /* When using the index (and thus not using psymtabs), each CU has an
2224 object of this type. This is used to hold information needed by
2225 the various "quick" methods. */
2226 struct dwarf2_per_cu_quick_data
2227 {
2228 /* The file table. This can be NULL if there was no file table
2229 or it's currently not read in.
2230 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2231 struct quick_file_names *file_names;
2232
2233 /* A temporary mark bit used when iterating over all CUs in
2234 expand_symtabs_matching. */
2235 unsigned int mark : 1;
2236
2237 /* True if we've tried to read the file table and found there isn't one.
2238 There will be no point in trying to read it again next time. */
2239 unsigned int no_file_data : 1;
2240 };
2241
2242 /* Utility hash function for a stmt_list_hash. */
2243
2244 static hashval_t
2245 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2246 {
2247 hashval_t v = 0;
2248
2249 if (stmt_list_hash->dwo_unit != NULL)
2250 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2251 v += to_underlying (stmt_list_hash->line_sect_off);
2252 return v;
2253 }
2254
2255 /* Utility equality function for a stmt_list_hash. */
2256
2257 static int
2258 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2259 const struct stmt_list_hash *rhs)
2260 {
2261 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2262 return 0;
2263 if (lhs->dwo_unit != NULL
2264 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2265 return 0;
2266
2267 return lhs->line_sect_off == rhs->line_sect_off;
2268 }
2269
2270 /* Hash function for a quick_file_names. */
2271
2272 static hashval_t
2273 hash_file_name_entry (const void *e)
2274 {
2275 const struct quick_file_names *file_data
2276 = (const struct quick_file_names *) e;
2277
2278 return hash_stmt_list_entry (&file_data->hash);
2279 }
2280
2281 /* Equality function for a quick_file_names. */
2282
2283 static int
2284 eq_file_name_entry (const void *a, const void *b)
2285 {
2286 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2287 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2288
2289 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2290 }
2291
2292 /* Delete function for a quick_file_names. */
2293
2294 static void
2295 delete_file_name_entry (void *e)
2296 {
2297 struct quick_file_names *file_data = (struct quick_file_names *) e;
2298 int i;
2299
2300 for (i = 0; i < file_data->num_file_names; ++i)
2301 {
2302 xfree ((void*) file_data->file_names[i]);
2303 if (file_data->real_names)
2304 xfree ((void*) file_data->real_names[i]);
2305 }
2306
2307 /* The space for the struct itself lives on the obstack, so we don't
2308 free it here. */
2309 }
2310
2311 /* Create a quick_file_names hash table. */
2312
2313 static htab_up
2314 create_quick_file_names_table (unsigned int nr_initial_entries)
2315 {
2316 return htab_up (htab_create_alloc (nr_initial_entries,
2317 hash_file_name_entry, eq_file_name_entry,
2318 delete_file_name_entry, xcalloc, xfree));
2319 }
2320
2321 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2322 have to be created afterwards. You should call age_cached_comp_units after
2323 processing PER_CU->CU. dw2_setup must have been already called. */
2324
2325 static void
2326 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2327 {
2328 if (per_cu->is_debug_types)
2329 load_full_type_unit (per_cu);
2330 else
2331 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2332
2333 if (per_cu->cu == NULL)
2334 return; /* Dummy CU. */
2335
2336 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2337 }
2338
2339 /* Read in the symbols for PER_CU. */
2340
2341 static void
2342 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2343 {
2344 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2345
2346 /* Skip type_unit_groups, reading the type units they contain
2347 is handled elsewhere. */
2348 if (per_cu->type_unit_group_p ())
2349 return;
2350
2351 /* The destructor of dwarf2_queue_guard frees any entries left on
2352 the queue. After this point we're guaranteed to leave this function
2353 with the dwarf queue empty. */
2354 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2355
2356 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2357 {
2358 queue_comp_unit (per_cu, language_minimal);
2359 load_cu (per_cu, skip_partial);
2360
2361 /* If we just loaded a CU from a DWO, and we're working with an index
2362 that may badly handle TUs, load all the TUs in that DWO as well.
2363 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2364 if (!per_cu->is_debug_types
2365 && per_cu->cu != NULL
2366 && per_cu->cu->dwo_unit != NULL
2367 && dwarf2_per_objfile->per_bfd->index_table != NULL
2368 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2369 /* DWP files aren't supported yet. */
2370 && get_dwp_file (dwarf2_per_objfile) == NULL)
2371 queue_and_load_all_dwo_tus (per_cu);
2372 }
2373
2374 process_queue (dwarf2_per_objfile);
2375
2376 /* Age the cache, releasing compilation units that have not
2377 been used recently. */
2378 age_cached_comp_units (dwarf2_per_objfile);
2379 }
2380
2381 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2382 the objfile from which this CU came. Returns the resulting symbol
2383 table. */
2384
2385 static struct compunit_symtab *
2386 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2387 {
2388 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2389
2390 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2391
2392 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2393 {
2394 free_cached_comp_units freer (dwarf2_per_objfile);
2395 scoped_restore decrementer = increment_reading_symtab ();
2396 dw2_do_instantiate_symtab (per_cu, skip_partial);
2397 process_cu_includes (dwarf2_per_objfile);
2398 }
2399
2400 return dwarf2_per_objfile->get_symtab (per_cu);
2401 }
2402
2403 /* See declaration. */
2404
2405 dwarf2_per_cu_data *
2406 dwarf2_per_bfd::get_cutu (int index)
2407 {
2408 if (index >= this->all_comp_units.size ())
2409 {
2410 index -= this->all_comp_units.size ();
2411 gdb_assert (index < this->all_type_units.size ());
2412 return &this->all_type_units[index]->per_cu;
2413 }
2414
2415 return this->all_comp_units[index];
2416 }
2417
2418 /* See declaration. */
2419
2420 dwarf2_per_cu_data *
2421 dwarf2_per_bfd::get_cu (int index)
2422 {
2423 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2424
2425 return this->all_comp_units[index];
2426 }
2427
2428 /* See declaration. */
2429
2430 signatured_type *
2431 dwarf2_per_bfd::get_tu (int index)
2432 {
2433 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2434
2435 return this->all_type_units[index];
2436 }
2437
2438 /* See read.h. */
2439
2440 dwarf2_per_cu_data *
2441 dwarf2_per_bfd::allocate_per_cu ()
2442 {
2443 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2444 result->index = m_num_psymtabs++;
2445 return result;
2446 }
2447
2448 /* See read.h. */
2449
2450 signatured_type *
2451 dwarf2_per_bfd::allocate_signatured_type ()
2452 {
2453 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2454 result->per_cu.index = m_num_psymtabs++;
2455 return result;
2456 }
2457
2458 /* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2459 obstack, and constructed with the specified field values. */
2460
2461 static dwarf2_per_cu_data *
2462 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2463 struct dwarf2_section_info *section,
2464 int is_dwz,
2465 sect_offset sect_off, ULONGEST length)
2466 {
2467 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2468 the_cu->sect_off = sect_off;
2469 the_cu->length = length;
2470 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2471 the_cu->section = section;
2472 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2473 struct dwarf2_per_cu_quick_data);
2474 the_cu->is_dwz = is_dwz;
2475 return the_cu;
2476 }
2477
2478 /* A helper for create_cus_from_index that handles a given list of
2479 CUs. */
2480
2481 static void
2482 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2483 const gdb_byte *cu_list, offset_type n_elements,
2484 struct dwarf2_section_info *section,
2485 int is_dwz)
2486 {
2487 for (offset_type i = 0; i < n_elements; i += 2)
2488 {
2489 gdb_static_assert (sizeof (ULONGEST) >= 8);
2490
2491 sect_offset sect_off
2492 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2493 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2494 cu_list += 2 * 8;
2495
2496 dwarf2_per_cu_data *per_cu
2497 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2498 sect_off, length);
2499 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2500 }
2501 }
2502
2503 /* Read the CU list from the mapped index, and use it to create all
2504 the CU objects for this objfile. */
2505
2506 static void
2507 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2508 const gdb_byte *cu_list, offset_type cu_list_elements,
2509 const gdb_byte *dwz_list, offset_type dwz_elements)
2510 {
2511 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2512 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2513 ((cu_list_elements + dwz_elements) / 2);
2514
2515 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2516 &dwarf2_per_objfile->per_bfd->info, 0);
2517
2518 if (dwz_elements == 0)
2519 return;
2520
2521 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2522 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2523 &dwz->info, 1);
2524 }
2525
2526 /* Create the signatured type hash table from the index. */
2527
2528 static void
2529 create_signatured_type_table_from_index
2530 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2531 struct dwarf2_section_info *section,
2532 const gdb_byte *bytes,
2533 offset_type elements)
2534 {
2535 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2536 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2537
2538 htab_up sig_types_hash = allocate_signatured_type_table ();
2539
2540 for (offset_type i = 0; i < elements; i += 3)
2541 {
2542 struct signatured_type *sig_type;
2543 ULONGEST signature;
2544 void **slot;
2545 cu_offset type_offset_in_tu;
2546
2547 gdb_static_assert (sizeof (ULONGEST) >= 8);
2548 sect_offset sect_off
2549 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2550 type_offset_in_tu
2551 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2552 BFD_ENDIAN_LITTLE);
2553 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2554 bytes += 3 * 8;
2555
2556 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2557 sig_type->signature = signature;
2558 sig_type->type_offset_in_tu = type_offset_in_tu;
2559 sig_type->per_cu.is_debug_types = 1;
2560 sig_type->per_cu.section = section;
2561 sig_type->per_cu.sect_off = sect_off;
2562 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2563 sig_type->per_cu.v.quick
2564 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2565 struct dwarf2_per_cu_quick_data);
2566
2567 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2568 *slot = sig_type;
2569
2570 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2571 }
2572
2573 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2574 }
2575
2576 /* Create the signatured type hash table from .debug_names. */
2577
2578 static void
2579 create_signatured_type_table_from_debug_names
2580 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2581 const mapped_debug_names &map,
2582 struct dwarf2_section_info *section,
2583 struct dwarf2_section_info *abbrev_section)
2584 {
2585 struct objfile *objfile = dwarf2_per_objfile->objfile;
2586
2587 section->read (objfile);
2588 abbrev_section->read (objfile);
2589
2590 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2591 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2592
2593 htab_up sig_types_hash = allocate_signatured_type_table ();
2594
2595 for (uint32_t i = 0; i < map.tu_count; ++i)
2596 {
2597 struct signatured_type *sig_type;
2598 void **slot;
2599
2600 sect_offset sect_off
2601 = (sect_offset) (extract_unsigned_integer
2602 (map.tu_table_reordered + i * map.offset_size,
2603 map.offset_size,
2604 map.dwarf5_byte_order));
2605
2606 comp_unit_head cu_header;
2607 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2608 abbrev_section,
2609 section->buffer + to_underlying (sect_off),
2610 rcuh_kind::TYPE);
2611
2612 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2613 sig_type->signature = cu_header.signature;
2614 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2615 sig_type->per_cu.is_debug_types = 1;
2616 sig_type->per_cu.section = section;
2617 sig_type->per_cu.sect_off = sect_off;
2618 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2619 sig_type->per_cu.v.quick
2620 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2621 struct dwarf2_per_cu_quick_data);
2622
2623 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2624 *slot = sig_type;
2625
2626 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2627 }
2628
2629 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2630 }
2631
2632 /* Read the address map data from the mapped index, and use it to
2633 populate the objfile's psymtabs_addrmap. */
2634
2635 static void
2636 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2637 struct mapped_index *index)
2638 {
2639 struct objfile *objfile = dwarf2_per_objfile->objfile;
2640 struct gdbarch *gdbarch = objfile->arch ();
2641 const gdb_byte *iter, *end;
2642 struct addrmap *mutable_map;
2643 CORE_ADDR baseaddr;
2644
2645 auto_obstack temp_obstack;
2646
2647 mutable_map = addrmap_create_mutable (&temp_obstack);
2648
2649 iter = index->address_table.data ();
2650 end = iter + index->address_table.size ();
2651
2652 baseaddr = objfile->text_section_offset ();
2653
2654 while (iter < end)
2655 {
2656 ULONGEST hi, lo, cu_index;
2657 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2658 iter += 8;
2659 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2660 iter += 8;
2661 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2662 iter += 4;
2663
2664 if (lo > hi)
2665 {
2666 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2667 hex_string (lo), hex_string (hi));
2668 continue;
2669 }
2670
2671 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2672 {
2673 complaint (_(".gdb_index address table has invalid CU number %u"),
2674 (unsigned) cu_index);
2675 continue;
2676 }
2677
2678 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2679 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2680 addrmap_set_empty (mutable_map, lo, hi - 1,
2681 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2682 }
2683
2684 objfile->partial_symtabs->psymtabs_addrmap
2685 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2686 }
2687
2688 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2689 populate the objfile's psymtabs_addrmap. */
2690
2691 static void
2692 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2693 struct dwarf2_section_info *section)
2694 {
2695 struct objfile *objfile = dwarf2_per_objfile->objfile;
2696 bfd *abfd = objfile->obfd;
2697 struct gdbarch *gdbarch = objfile->arch ();
2698 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2699
2700 auto_obstack temp_obstack;
2701 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2702
2703 std::unordered_map<sect_offset,
2704 dwarf2_per_cu_data *,
2705 gdb::hash_enum<sect_offset>>
2706 debug_info_offset_to_per_cu;
2707 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2708 {
2709 const auto insertpair
2710 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2711 if (!insertpair.second)
2712 {
2713 warning (_("Section .debug_aranges in %s has duplicate "
2714 "debug_info_offset %s, ignoring .debug_aranges."),
2715 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2716 return;
2717 }
2718 }
2719
2720 section->read (objfile);
2721
2722 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2723
2724 const gdb_byte *addr = section->buffer;
2725
2726 while (addr < section->buffer + section->size)
2727 {
2728 const gdb_byte *const entry_addr = addr;
2729 unsigned int bytes_read;
2730
2731 const LONGEST entry_length = read_initial_length (abfd, addr,
2732 &bytes_read);
2733 addr += bytes_read;
2734
2735 const gdb_byte *const entry_end = addr + entry_length;
2736 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2737 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2738 if (addr + entry_length > section->buffer + section->size)
2739 {
2740 warning (_("Section .debug_aranges in %s entry at offset %s "
2741 "length %s exceeds section length %s, "
2742 "ignoring .debug_aranges."),
2743 objfile_name (objfile),
2744 plongest (entry_addr - section->buffer),
2745 plongest (bytes_read + entry_length),
2746 pulongest (section->size));
2747 return;
2748 }
2749
2750 /* The version number. */
2751 const uint16_t version = read_2_bytes (abfd, addr);
2752 addr += 2;
2753 if (version != 2)
2754 {
2755 warning (_("Section .debug_aranges in %s entry at offset %s "
2756 "has unsupported version %d, ignoring .debug_aranges."),
2757 objfile_name (objfile),
2758 plongest (entry_addr - section->buffer), version);
2759 return;
2760 }
2761
2762 const uint64_t debug_info_offset
2763 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2764 addr += offset_size;
2765 const auto per_cu_it
2766 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2767 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2768 {
2769 warning (_("Section .debug_aranges in %s entry at offset %s "
2770 "debug_info_offset %s does not exists, "
2771 "ignoring .debug_aranges."),
2772 objfile_name (objfile),
2773 plongest (entry_addr - section->buffer),
2774 pulongest (debug_info_offset));
2775 return;
2776 }
2777 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2778
2779 const uint8_t address_size = *addr++;
2780 if (address_size < 1 || address_size > 8)
2781 {
2782 warning (_("Section .debug_aranges in %s entry at offset %s "
2783 "address_size %u is invalid, ignoring .debug_aranges."),
2784 objfile_name (objfile),
2785 plongest (entry_addr - section->buffer), address_size);
2786 return;
2787 }
2788
2789 const uint8_t segment_selector_size = *addr++;
2790 if (segment_selector_size != 0)
2791 {
2792 warning (_("Section .debug_aranges in %s entry at offset %s "
2793 "segment_selector_size %u is not supported, "
2794 "ignoring .debug_aranges."),
2795 objfile_name (objfile),
2796 plongest (entry_addr - section->buffer),
2797 segment_selector_size);
2798 return;
2799 }
2800
2801 /* Must pad to an alignment boundary that is twice the address
2802 size. It is undocumented by the DWARF standard but GCC does
2803 use it. */
2804 for (size_t padding = ((-(addr - section->buffer))
2805 & (2 * address_size - 1));
2806 padding > 0; padding--)
2807 if (*addr++ != 0)
2808 {
2809 warning (_("Section .debug_aranges in %s entry at offset %s "
2810 "padding is not zero, ignoring .debug_aranges."),
2811 objfile_name (objfile),
2812 plongest (entry_addr - section->buffer));
2813 return;
2814 }
2815
2816 for (;;)
2817 {
2818 if (addr + 2 * address_size > entry_end)
2819 {
2820 warning (_("Section .debug_aranges in %s entry at offset %s "
2821 "address list is not properly terminated, "
2822 "ignoring .debug_aranges."),
2823 objfile_name (objfile),
2824 plongest (entry_addr - section->buffer));
2825 return;
2826 }
2827 ULONGEST start = extract_unsigned_integer (addr, address_size,
2828 dwarf5_byte_order);
2829 addr += address_size;
2830 ULONGEST length = extract_unsigned_integer (addr, address_size,
2831 dwarf5_byte_order);
2832 addr += address_size;
2833 if (start == 0 && length == 0)
2834 break;
2835 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2836 {
2837 /* Symbol was eliminated due to a COMDAT group. */
2838 continue;
2839 }
2840 ULONGEST end = start + length;
2841 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2842 - baseaddr);
2843 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2844 - baseaddr);
2845 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2846 }
2847 }
2848
2849 objfile->partial_symtabs->psymtabs_addrmap
2850 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2851 }
2852
2853 /* Find a slot in the mapped index INDEX for the object named NAME.
2854 If NAME is found, set *VEC_OUT to point to the CU vector in the
2855 constant pool and return true. If NAME cannot be found, return
2856 false. */
2857
2858 static bool
2859 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2860 offset_type **vec_out)
2861 {
2862 offset_type hash;
2863 offset_type slot, step;
2864 int (*cmp) (const char *, const char *);
2865
2866 gdb::unique_xmalloc_ptr<char> without_params;
2867 if (current_language->la_language == language_cplus
2868 || current_language->la_language == language_fortran
2869 || current_language->la_language == language_d)
2870 {
2871 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2872 not contain any. */
2873
2874 if (strchr (name, '(') != NULL)
2875 {
2876 without_params = cp_remove_params (name);
2877
2878 if (without_params != NULL)
2879 name = without_params.get ();
2880 }
2881 }
2882
2883 /* Index version 4 did not support case insensitive searches. But the
2884 indices for case insensitive languages are built in lowercase, therefore
2885 simulate our NAME being searched is also lowercased. */
2886 hash = mapped_index_string_hash ((index->version == 4
2887 && case_sensitivity == case_sensitive_off
2888 ? 5 : index->version),
2889 name);
2890
2891 slot = hash & (index->symbol_table.size () - 1);
2892 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2893 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2894
2895 for (;;)
2896 {
2897 const char *str;
2898
2899 const auto &bucket = index->symbol_table[slot];
2900 if (bucket.name == 0 && bucket.vec == 0)
2901 return false;
2902
2903 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2904 if (!cmp (name, str))
2905 {
2906 *vec_out = (offset_type *) (index->constant_pool
2907 + MAYBE_SWAP (bucket.vec));
2908 return true;
2909 }
2910
2911 slot = (slot + step) & (index->symbol_table.size () - 1);
2912 }
2913 }
2914
2915 /* A helper function that reads the .gdb_index from BUFFER and fills
2916 in MAP. FILENAME is the name of the file containing the data;
2917 it is used for error reporting. DEPRECATED_OK is true if it is
2918 ok to use deprecated sections.
2919
2920 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2921 out parameters that are filled in with information about the CU and
2922 TU lists in the section.
2923
2924 Returns true if all went well, false otherwise. */
2925
2926 static bool
2927 read_gdb_index_from_buffer (const char *filename,
2928 bool deprecated_ok,
2929 gdb::array_view<const gdb_byte> buffer,
2930 struct mapped_index *map,
2931 const gdb_byte **cu_list,
2932 offset_type *cu_list_elements,
2933 const gdb_byte **types_list,
2934 offset_type *types_list_elements)
2935 {
2936 const gdb_byte *addr = &buffer[0];
2937
2938 /* Version check. */
2939 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2940 /* Versions earlier than 3 emitted every copy of a psymbol. This
2941 causes the index to behave very poorly for certain requests. Version 3
2942 contained incomplete addrmap. So, it seems better to just ignore such
2943 indices. */
2944 if (version < 4)
2945 {
2946 static int warning_printed = 0;
2947 if (!warning_printed)
2948 {
2949 warning (_("Skipping obsolete .gdb_index section in %s."),
2950 filename);
2951 warning_printed = 1;
2952 }
2953 return 0;
2954 }
2955 /* Index version 4 uses a different hash function than index version
2956 5 and later.
2957
2958 Versions earlier than 6 did not emit psymbols for inlined
2959 functions. Using these files will cause GDB not to be able to
2960 set breakpoints on inlined functions by name, so we ignore these
2961 indices unless the user has done
2962 "set use-deprecated-index-sections on". */
2963 if (version < 6 && !deprecated_ok)
2964 {
2965 static int warning_printed = 0;
2966 if (!warning_printed)
2967 {
2968 warning (_("\
2969 Skipping deprecated .gdb_index section in %s.\n\
2970 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2971 to use the section anyway."),
2972 filename);
2973 warning_printed = 1;
2974 }
2975 return 0;
2976 }
2977 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2978 of the TU (for symbols coming from TUs),
2979 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2980 Plus gold-generated indices can have duplicate entries for global symbols,
2981 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2982 These are just performance bugs, and we can't distinguish gdb-generated
2983 indices from gold-generated ones, so issue no warning here. */
2984
2985 /* Indexes with higher version than the one supported by GDB may be no
2986 longer backward compatible. */
2987 if (version > 8)
2988 return 0;
2989
2990 map->version = version;
2991
2992 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2993
2994 int i = 0;
2995 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2996 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2997 / 8);
2998 ++i;
2999
3000 *types_list = addr + MAYBE_SWAP (metadata[i]);
3001 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3002 - MAYBE_SWAP (metadata[i]))
3003 / 8);
3004 ++i;
3005
3006 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3007 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3008 map->address_table
3009 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3010 ++i;
3011
3012 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3013 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3014 map->symbol_table
3015 = gdb::array_view<mapped_index::symbol_table_slot>
3016 ((mapped_index::symbol_table_slot *) symbol_table,
3017 (mapped_index::symbol_table_slot *) symbol_table_end);
3018
3019 ++i;
3020 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3021
3022 return 1;
3023 }
3024
3025 /* Callback types for dwarf2_read_gdb_index. */
3026
3027 typedef gdb::function_view
3028 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3029 get_gdb_index_contents_ftype;
3030 typedef gdb::function_view
3031 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3032 get_gdb_index_contents_dwz_ftype;
3033
3034 /* Read .gdb_index. If everything went ok, initialize the "quick"
3035 elements of all the CUs and return 1. Otherwise, return 0. */
3036
3037 static int
3038 dwarf2_read_gdb_index
3039 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3040 get_gdb_index_contents_ftype get_gdb_index_contents,
3041 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3042 {
3043 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3044 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3045 struct dwz_file *dwz;
3046 struct objfile *objfile = dwarf2_per_objfile->objfile;
3047
3048 gdb::array_view<const gdb_byte> main_index_contents
3049 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3050
3051 if (main_index_contents.empty ())
3052 return 0;
3053
3054 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3055 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3056 use_deprecated_index_sections,
3057 main_index_contents, map.get (), &cu_list,
3058 &cu_list_elements, &types_list,
3059 &types_list_elements))
3060 return 0;
3061
3062 /* Don't use the index if it's empty. */
3063 if (map->symbol_table.empty ())
3064 return 0;
3065
3066 /* If there is a .dwz file, read it so we can get its CU list as
3067 well. */
3068 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3069 if (dwz != NULL)
3070 {
3071 struct mapped_index dwz_map;
3072 const gdb_byte *dwz_types_ignore;
3073 offset_type dwz_types_elements_ignore;
3074
3075 gdb::array_view<const gdb_byte> dwz_index_content
3076 = get_gdb_index_contents_dwz (objfile, dwz);
3077
3078 if (dwz_index_content.empty ())
3079 return 0;
3080
3081 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3082 1, dwz_index_content, &dwz_map,
3083 &dwz_list, &dwz_list_elements,
3084 &dwz_types_ignore,
3085 &dwz_types_elements_ignore))
3086 {
3087 warning (_("could not read '.gdb_index' section from %s; skipping"),
3088 bfd_get_filename (dwz->dwz_bfd.get ()));
3089 return 0;
3090 }
3091 }
3092
3093 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3094 dwz_list, dwz_list_elements);
3095
3096 if (types_list_elements)
3097 {
3098 /* We can only handle a single .debug_types when we have an
3099 index. */
3100 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3101 return 0;
3102
3103 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3104
3105 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3106 types_list, types_list_elements);
3107 }
3108
3109 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3110
3111 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3112 dwarf2_per_objfile->per_bfd->using_index = 1;
3113 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3114 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3115
3116 return 1;
3117 }
3118
3119 /* die_reader_func for dw2_get_file_names. */
3120
3121 static void
3122 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3123 const gdb_byte *info_ptr,
3124 struct die_info *comp_unit_die)
3125 {
3126 struct dwarf2_cu *cu = reader->cu;
3127 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3128 struct dwarf2_per_objfile *dwarf2_per_objfile
3129 = cu->per_cu->dwarf2_per_objfile;
3130 struct dwarf2_per_cu_data *lh_cu;
3131 struct attribute *attr;
3132 void **slot;
3133 struct quick_file_names *qfn;
3134
3135 gdb_assert (! this_cu->is_debug_types);
3136
3137 /* Our callers never want to match partial units -- instead they
3138 will match the enclosing full CU. */
3139 if (comp_unit_die->tag == DW_TAG_partial_unit)
3140 {
3141 this_cu->v.quick->no_file_data = 1;
3142 return;
3143 }
3144
3145 lh_cu = this_cu;
3146 slot = NULL;
3147
3148 line_header_up lh;
3149 sect_offset line_offset {};
3150
3151 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3152 if (attr != nullptr)
3153 {
3154 struct quick_file_names find_entry;
3155
3156 line_offset = (sect_offset) DW_UNSND (attr);
3157
3158 /* We may have already read in this line header (TU line header sharing).
3159 If we have we're done. */
3160 find_entry.hash.dwo_unit = cu->dwo_unit;
3161 find_entry.hash.line_sect_off = line_offset;
3162 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3163 &find_entry, INSERT);
3164 if (*slot != NULL)
3165 {
3166 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3167 return;
3168 }
3169
3170 lh = dwarf_decode_line_header (line_offset, cu);
3171 }
3172 if (lh == NULL)
3173 {
3174 lh_cu->v.quick->no_file_data = 1;
3175 return;
3176 }
3177
3178 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3179 qfn->hash.dwo_unit = cu->dwo_unit;
3180 qfn->hash.line_sect_off = line_offset;
3181 gdb_assert (slot != NULL);
3182 *slot = qfn;
3183
3184 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3185
3186 int offset = 0;
3187 if (strcmp (fnd.name, "<unknown>") != 0)
3188 ++offset;
3189
3190 qfn->num_file_names = offset + lh->file_names_size ();
3191 qfn->file_names =
3192 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3193 qfn->num_file_names);
3194 if (offset != 0)
3195 qfn->file_names[0] = xstrdup (fnd.name);
3196 for (int i = 0; i < lh->file_names_size (); ++i)
3197 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3198 fnd.comp_dir).release ();
3199 qfn->real_names = NULL;
3200
3201 lh_cu->v.quick->file_names = qfn;
3202 }
3203
3204 /* A helper for the "quick" functions which attempts to read the line
3205 table for THIS_CU. */
3206
3207 static struct quick_file_names *
3208 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3209 {
3210 /* This should never be called for TUs. */
3211 gdb_assert (! this_cu->is_debug_types);
3212 /* Nor type unit groups. */
3213 gdb_assert (! this_cu->type_unit_group_p ());
3214
3215 if (this_cu->v.quick->file_names != NULL)
3216 return this_cu->v.quick->file_names;
3217 /* If we know there is no line data, no point in looking again. */
3218 if (this_cu->v.quick->no_file_data)
3219 return NULL;
3220
3221 cutu_reader reader (this_cu);
3222 if (!reader.dummy_p)
3223 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3224
3225 if (this_cu->v.quick->no_file_data)
3226 return NULL;
3227 return this_cu->v.quick->file_names;
3228 }
3229
3230 /* A helper for the "quick" functions which computes and caches the
3231 real path for a given file name from the line table. */
3232
3233 static const char *
3234 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3235 struct quick_file_names *qfn, int index)
3236 {
3237 if (qfn->real_names == NULL)
3238 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3239 qfn->num_file_names, const char *);
3240
3241 if (qfn->real_names[index] == NULL)
3242 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3243
3244 return qfn->real_names[index];
3245 }
3246
3247 static struct symtab *
3248 dw2_find_last_source_symtab (struct objfile *objfile)
3249 {
3250 struct dwarf2_per_objfile *dwarf2_per_objfile
3251 = get_dwarf2_per_objfile (objfile);
3252 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3253 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3254
3255 if (cust == NULL)
3256 return NULL;
3257
3258 return compunit_primary_filetab (cust);
3259 }
3260
3261 /* Traversal function for dw2_forget_cached_source_info. */
3262
3263 static int
3264 dw2_free_cached_file_names (void **slot, void *info)
3265 {
3266 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3267
3268 if (file_data->real_names)
3269 {
3270 int i;
3271
3272 for (i = 0; i < file_data->num_file_names; ++i)
3273 {
3274 xfree ((void*) file_data->real_names[i]);
3275 file_data->real_names[i] = NULL;
3276 }
3277 }
3278
3279 return 1;
3280 }
3281
3282 static void
3283 dw2_forget_cached_source_info (struct objfile *objfile)
3284 {
3285 struct dwarf2_per_objfile *dwarf2_per_objfile
3286 = get_dwarf2_per_objfile (objfile);
3287
3288 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3289 dw2_free_cached_file_names, NULL);
3290 }
3291
3292 /* Helper function for dw2_map_symtabs_matching_filename that expands
3293 the symtabs and calls the iterator. */
3294
3295 static int
3296 dw2_map_expand_apply (struct objfile *objfile,
3297 struct dwarf2_per_cu_data *per_cu,
3298 const char *name, const char *real_path,
3299 gdb::function_view<bool (symtab *)> callback)
3300 {
3301 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3302
3303 /* Don't visit already-expanded CUs. */
3304 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3305 if (per_objfile->symtab_set_p (per_cu))
3306 return 0;
3307
3308 /* This may expand more than one symtab, and we want to iterate over
3309 all of them. */
3310 dw2_instantiate_symtab (per_cu, false);
3311
3312 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3313 last_made, callback);
3314 }
3315
3316 /* Implementation of the map_symtabs_matching_filename method. */
3317
3318 static bool
3319 dw2_map_symtabs_matching_filename
3320 (struct objfile *objfile, const char *name, const char *real_path,
3321 gdb::function_view<bool (symtab *)> callback)
3322 {
3323 const char *name_basename = lbasename (name);
3324 struct dwarf2_per_objfile *dwarf2_per_objfile
3325 = get_dwarf2_per_objfile (objfile);
3326
3327 /* The rule is CUs specify all the files, including those used by
3328 any TU, so there's no need to scan TUs here. */
3329
3330 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3331 {
3332 /* We only need to look at symtabs not already expanded. */
3333 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3334 continue;
3335
3336 quick_file_names *file_data = dw2_get_file_names (per_cu);
3337 if (file_data == NULL)
3338 continue;
3339
3340 for (int j = 0; j < file_data->num_file_names; ++j)
3341 {
3342 const char *this_name = file_data->file_names[j];
3343 const char *this_real_name;
3344
3345 if (compare_filenames_for_search (this_name, name))
3346 {
3347 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3348 callback))
3349 return true;
3350 continue;
3351 }
3352
3353 /* Before we invoke realpath, which can get expensive when many
3354 files are involved, do a quick comparison of the basenames. */
3355 if (! basenames_may_differ
3356 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3357 continue;
3358
3359 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3360 file_data, j);
3361 if (compare_filenames_for_search (this_real_name, name))
3362 {
3363 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3364 callback))
3365 return true;
3366 continue;
3367 }
3368
3369 if (real_path != NULL)
3370 {
3371 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3372 gdb_assert (IS_ABSOLUTE_PATH (name));
3373 if (this_real_name != NULL
3374 && FILENAME_CMP (real_path, this_real_name) == 0)
3375 {
3376 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3377 callback))
3378 return true;
3379 continue;
3380 }
3381 }
3382 }
3383 }
3384
3385 return false;
3386 }
3387
3388 /* Struct used to manage iterating over all CUs looking for a symbol. */
3389
3390 struct dw2_symtab_iterator
3391 {
3392 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3393 struct dwarf2_per_objfile *dwarf2_per_objfile;
3394 /* If set, only look for symbols that match that block. Valid values are
3395 GLOBAL_BLOCK and STATIC_BLOCK. */
3396 gdb::optional<block_enum> block_index;
3397 /* The kind of symbol we're looking for. */
3398 domain_enum domain;
3399 /* The list of CUs from the index entry of the symbol,
3400 or NULL if not found. */
3401 offset_type *vec;
3402 /* The next element in VEC to look at. */
3403 int next;
3404 /* The number of elements in VEC, or zero if there is no match. */
3405 int length;
3406 /* Have we seen a global version of the symbol?
3407 If so we can ignore all further global instances.
3408 This is to work around gold/15646, inefficient gold-generated
3409 indices. */
3410 int global_seen;
3411 };
3412
3413 /* Initialize the index symtab iterator ITER. */
3414
3415 static void
3416 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3417 struct dwarf2_per_objfile *dwarf2_per_objfile,
3418 gdb::optional<block_enum> block_index,
3419 domain_enum domain,
3420 const char *name)
3421 {
3422 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3423 iter->block_index = block_index;
3424 iter->domain = domain;
3425 iter->next = 0;
3426 iter->global_seen = 0;
3427
3428 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3429
3430 /* index is NULL if OBJF_READNOW. */
3431 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3432 iter->length = MAYBE_SWAP (*iter->vec);
3433 else
3434 {
3435 iter->vec = NULL;
3436 iter->length = 0;
3437 }
3438 }
3439
3440 /* Return the next matching CU or NULL if there are no more. */
3441
3442 static struct dwarf2_per_cu_data *
3443 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3444 {
3445 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3446
3447 for ( ; iter->next < iter->length; ++iter->next)
3448 {
3449 offset_type cu_index_and_attrs =
3450 MAYBE_SWAP (iter->vec[iter->next + 1]);
3451 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3452 gdb_index_symbol_kind symbol_kind =
3453 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3454 /* Only check the symbol attributes if they're present.
3455 Indices prior to version 7 don't record them,
3456 and indices >= 7 may elide them for certain symbols
3457 (gold does this). */
3458 int attrs_valid =
3459 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3460 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3461
3462 /* Don't crash on bad data. */
3463 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3464 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3465 {
3466 complaint (_(".gdb_index entry has bad CU index"
3467 " [in module %s]"),
3468 objfile_name (dwarf2_per_objfile->objfile));
3469 continue;
3470 }
3471
3472 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3473
3474 /* Skip if already read in. */
3475 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3476 continue;
3477
3478 /* Check static vs global. */
3479 if (attrs_valid)
3480 {
3481 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3482
3483 if (iter->block_index.has_value ())
3484 {
3485 bool want_static = *iter->block_index == STATIC_BLOCK;
3486
3487 if (is_static != want_static)
3488 continue;
3489 }
3490
3491 /* Work around gold/15646. */
3492 if (!is_static && iter->global_seen)
3493 continue;
3494 if (!is_static)
3495 iter->global_seen = 1;
3496 }
3497
3498 /* Only check the symbol's kind if it has one. */
3499 if (attrs_valid)
3500 {
3501 switch (iter->domain)
3502 {
3503 case VAR_DOMAIN:
3504 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3505 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3506 /* Some types are also in VAR_DOMAIN. */
3507 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3508 continue;
3509 break;
3510 case STRUCT_DOMAIN:
3511 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3512 continue;
3513 break;
3514 case LABEL_DOMAIN:
3515 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3516 continue;
3517 break;
3518 case MODULE_DOMAIN:
3519 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3520 continue;
3521 break;
3522 default:
3523 break;
3524 }
3525 }
3526
3527 ++iter->next;
3528 return per_cu;
3529 }
3530
3531 return NULL;
3532 }
3533
3534 static struct compunit_symtab *
3535 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3536 const char *name, domain_enum domain)
3537 {
3538 struct compunit_symtab *stab_best = NULL;
3539 struct dwarf2_per_objfile *dwarf2_per_objfile
3540 = get_dwarf2_per_objfile (objfile);
3541
3542 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3543
3544 struct dw2_symtab_iterator iter;
3545 struct dwarf2_per_cu_data *per_cu;
3546
3547 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3548
3549 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3550 {
3551 struct symbol *sym, *with_opaque = NULL;
3552 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3553 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3554 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3555
3556 sym = block_find_symbol (block, name, domain,
3557 block_find_non_opaque_type_preferred,
3558 &with_opaque);
3559
3560 /* Some caution must be observed with overloaded functions
3561 and methods, since the index will not contain any overload
3562 information (but NAME might contain it). */
3563
3564 if (sym != NULL
3565 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3566 return stab;
3567 if (with_opaque != NULL
3568 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3569 stab_best = stab;
3570
3571 /* Keep looking through other CUs. */
3572 }
3573
3574 return stab_best;
3575 }
3576
3577 static void
3578 dw2_print_stats (struct objfile *objfile)
3579 {
3580 struct dwarf2_per_objfile *dwarf2_per_objfile
3581 = get_dwarf2_per_objfile (objfile);
3582 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3583 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3584 int count = 0;
3585
3586 for (int i = 0; i < total; ++i)
3587 {
3588 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3589
3590 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3591 ++count;
3592 }
3593 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3594 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3595 }
3596
3597 /* This dumps minimal information about the index.
3598 It is called via "mt print objfiles".
3599 One use is to verify .gdb_index has been loaded by the
3600 gdb.dwarf2/gdb-index.exp testcase. */
3601
3602 static void
3603 dw2_dump (struct objfile *objfile)
3604 {
3605 struct dwarf2_per_objfile *dwarf2_per_objfile
3606 = get_dwarf2_per_objfile (objfile);
3607
3608 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3609 printf_filtered (".gdb_index:");
3610 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3611 {
3612 printf_filtered (" version %d\n",
3613 dwarf2_per_objfile->per_bfd->index_table->version);
3614 }
3615 else
3616 printf_filtered (" faked for \"readnow\"\n");
3617 printf_filtered ("\n");
3618 }
3619
3620 static void
3621 dw2_expand_symtabs_for_function (struct objfile *objfile,
3622 const char *func_name)
3623 {
3624 struct dwarf2_per_objfile *dwarf2_per_objfile
3625 = get_dwarf2_per_objfile (objfile);
3626
3627 struct dw2_symtab_iterator iter;
3628 struct dwarf2_per_cu_data *per_cu;
3629
3630 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3631
3632 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3633 dw2_instantiate_symtab (per_cu, false);
3634
3635 }
3636
3637 static void
3638 dw2_expand_all_symtabs (struct objfile *objfile)
3639 {
3640 struct dwarf2_per_objfile *dwarf2_per_objfile
3641 = get_dwarf2_per_objfile (objfile);
3642 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3643 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3644
3645 for (int i = 0; i < total_units; ++i)
3646 {
3647 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3648
3649 /* We don't want to directly expand a partial CU, because if we
3650 read it with the wrong language, then assertion failures can
3651 be triggered later on. See PR symtab/23010. So, tell
3652 dw2_instantiate_symtab to skip partial CUs -- any important
3653 partial CU will be read via DW_TAG_imported_unit anyway. */
3654 dw2_instantiate_symtab (per_cu, true);
3655 }
3656 }
3657
3658 static void
3659 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3660 const char *fullname)
3661 {
3662 struct dwarf2_per_objfile *dwarf2_per_objfile
3663 = get_dwarf2_per_objfile (objfile);
3664
3665 /* We don't need to consider type units here.
3666 This is only called for examining code, e.g. expand_line_sal.
3667 There can be an order of magnitude (or more) more type units
3668 than comp units, and we avoid them if we can. */
3669
3670 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3671 {
3672 /* We only need to look at symtabs not already expanded. */
3673 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3674 continue;
3675
3676 quick_file_names *file_data = dw2_get_file_names (per_cu);
3677 if (file_data == NULL)
3678 continue;
3679
3680 for (int j = 0; j < file_data->num_file_names; ++j)
3681 {
3682 const char *this_fullname = file_data->file_names[j];
3683
3684 if (filename_cmp (this_fullname, fullname) == 0)
3685 {
3686 dw2_instantiate_symtab (per_cu, false);
3687 break;
3688 }
3689 }
3690 }
3691 }
3692
3693 static void
3694 dw2_expand_symtabs_matching_symbol
3695 (mapped_index_base &index,
3696 const lookup_name_info &lookup_name_in,
3697 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3698 enum search_domain kind,
3699 gdb::function_view<bool (offset_type)> match_callback);
3700
3701 static void
3702 dw2_expand_symtabs_matching_one
3703 (struct dwarf2_per_cu_data *per_cu,
3704 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3705 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3706
3707 static void
3708 dw2_map_matching_symbols
3709 (struct objfile *objfile,
3710 const lookup_name_info &name, domain_enum domain,
3711 int global,
3712 gdb::function_view<symbol_found_callback_ftype> callback,
3713 symbol_compare_ftype *ordered_compare)
3714 {
3715 /* Used for Ada. */
3716 struct dwarf2_per_objfile *dwarf2_per_objfile
3717 = get_dwarf2_per_objfile (objfile);
3718
3719 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3720
3721 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3722 {
3723 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3724 here though if the current language is Ada for a non-Ada objfile
3725 using GNU index. */
3726 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3727
3728 const char *match_name = name.ada ().lookup_name ().c_str ();
3729 auto matcher = [&] (const char *symname)
3730 {
3731 if (ordered_compare == nullptr)
3732 return true;
3733 return ordered_compare (symname, match_name) == 0;
3734 };
3735
3736 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3737 [&] (offset_type namei)
3738 {
3739 struct dw2_symtab_iterator iter;
3740 struct dwarf2_per_cu_data *per_cu;
3741
3742 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3743 match_name);
3744 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3745 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
3746 return true;
3747 });
3748 }
3749 else
3750 {
3751 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3752 proceed assuming all symtabs have been read in. */
3753 }
3754
3755 for (compunit_symtab *cust : objfile->compunits ())
3756 {
3757 const struct block *block;
3758
3759 if (cust == NULL)
3760 continue;
3761 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3762 if (!iterate_over_symbols_terminated (block, name,
3763 domain, callback))
3764 return;
3765 }
3766 }
3767
3768 /* Starting from a search name, return the string that finds the upper
3769 bound of all strings that start with SEARCH_NAME in a sorted name
3770 list. Returns the empty string to indicate that the upper bound is
3771 the end of the list. */
3772
3773 static std::string
3774 make_sort_after_prefix_name (const char *search_name)
3775 {
3776 /* When looking to complete "func", we find the upper bound of all
3777 symbols that start with "func" by looking for where we'd insert
3778 the closest string that would follow "func" in lexicographical
3779 order. Usually, that's "func"-with-last-character-incremented,
3780 i.e. "fund". Mind non-ASCII characters, though. Usually those
3781 will be UTF-8 multi-byte sequences, but we can't be certain.
3782 Especially mind the 0xff character, which is a valid character in
3783 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3784 rule out compilers allowing it in identifiers. Note that
3785 conveniently, strcmp/strcasecmp are specified to compare
3786 characters interpreted as unsigned char. So what we do is treat
3787 the whole string as a base 256 number composed of a sequence of
3788 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3789 to 0, and carries 1 to the following more-significant position.
3790 If the very first character in SEARCH_NAME ends up incremented
3791 and carries/overflows, then the upper bound is the end of the
3792 list. The string after the empty string is also the empty
3793 string.
3794
3795 Some examples of this operation:
3796
3797 SEARCH_NAME => "+1" RESULT
3798
3799 "abc" => "abd"
3800 "ab\xff" => "ac"
3801 "\xff" "a" "\xff" => "\xff" "b"
3802 "\xff" => ""
3803 "\xff\xff" => ""
3804 "" => ""
3805
3806 Then, with these symbols for example:
3807
3808 func
3809 func1
3810 fund
3811
3812 completing "func" looks for symbols between "func" and
3813 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3814 which finds "func" and "func1", but not "fund".
3815
3816 And with:
3817
3818 funcÿ (Latin1 'ÿ' [0xff])
3819 funcÿ1
3820 fund
3821
3822 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3823 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3824
3825 And with:
3826
3827 ÿÿ (Latin1 'ÿ' [0xff])
3828 ÿÿ1
3829
3830 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3831 the end of the list.
3832 */
3833 std::string after = search_name;
3834 while (!after.empty () && (unsigned char) after.back () == 0xff)
3835 after.pop_back ();
3836 if (!after.empty ())
3837 after.back () = (unsigned char) after.back () + 1;
3838 return after;
3839 }
3840
3841 /* See declaration. */
3842
3843 std::pair<std::vector<name_component>::const_iterator,
3844 std::vector<name_component>::const_iterator>
3845 mapped_index_base::find_name_components_bounds
3846 (const lookup_name_info &lookup_name_without_params, language lang) const
3847 {
3848 auto *name_cmp
3849 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3850
3851 const char *lang_name
3852 = lookup_name_without_params.language_lookup_name (lang);
3853
3854 /* Comparison function object for lower_bound that matches against a
3855 given symbol name. */
3856 auto lookup_compare_lower = [&] (const name_component &elem,
3857 const char *name)
3858 {
3859 const char *elem_qualified = this->symbol_name_at (elem.idx);
3860 const char *elem_name = elem_qualified + elem.name_offset;
3861 return name_cmp (elem_name, name) < 0;
3862 };
3863
3864 /* Comparison function object for upper_bound that matches against a
3865 given symbol name. */
3866 auto lookup_compare_upper = [&] (const char *name,
3867 const name_component &elem)
3868 {
3869 const char *elem_qualified = this->symbol_name_at (elem.idx);
3870 const char *elem_name = elem_qualified + elem.name_offset;
3871 return name_cmp (name, elem_name) < 0;
3872 };
3873
3874 auto begin = this->name_components.begin ();
3875 auto end = this->name_components.end ();
3876
3877 /* Find the lower bound. */
3878 auto lower = [&] ()
3879 {
3880 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3881 return begin;
3882 else
3883 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3884 } ();
3885
3886 /* Find the upper bound. */
3887 auto upper = [&] ()
3888 {
3889 if (lookup_name_without_params.completion_mode ())
3890 {
3891 /* In completion mode, we want UPPER to point past all
3892 symbols names that have the same prefix. I.e., with
3893 these symbols, and completing "func":
3894
3895 function << lower bound
3896 function1
3897 other_function << upper bound
3898
3899 We find the upper bound by looking for the insertion
3900 point of "func"-with-last-character-incremented,
3901 i.e. "fund". */
3902 std::string after = make_sort_after_prefix_name (lang_name);
3903 if (after.empty ())
3904 return end;
3905 return std::lower_bound (lower, end, after.c_str (),
3906 lookup_compare_lower);
3907 }
3908 else
3909 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3910 } ();
3911
3912 return {lower, upper};
3913 }
3914
3915 /* See declaration. */
3916
3917 void
3918 mapped_index_base::build_name_components ()
3919 {
3920 if (!this->name_components.empty ())
3921 return;
3922
3923 this->name_components_casing = case_sensitivity;
3924 auto *name_cmp
3925 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3926
3927 /* The code below only knows how to break apart components of C++
3928 symbol names (and other languages that use '::' as
3929 namespace/module separator) and Ada symbol names. */
3930 auto count = this->symbol_name_count ();
3931 for (offset_type idx = 0; idx < count; idx++)
3932 {
3933 if (this->symbol_name_slot_invalid (idx))
3934 continue;
3935
3936 const char *name = this->symbol_name_at (idx);
3937
3938 /* Add each name component to the name component table. */
3939 unsigned int previous_len = 0;
3940
3941 if (strstr (name, "::") != nullptr)
3942 {
3943 for (unsigned int current_len = cp_find_first_component (name);
3944 name[current_len] != '\0';
3945 current_len += cp_find_first_component (name + current_len))
3946 {
3947 gdb_assert (name[current_len] == ':');
3948 this->name_components.push_back ({previous_len, idx});
3949 /* Skip the '::'. */
3950 current_len += 2;
3951 previous_len = current_len;
3952 }
3953 }
3954 else
3955 {
3956 /* Handle the Ada encoded (aka mangled) form here. */
3957 for (const char *iter = strstr (name, "__");
3958 iter != nullptr;
3959 iter = strstr (iter, "__"))
3960 {
3961 this->name_components.push_back ({previous_len, idx});
3962 iter += 2;
3963 previous_len = iter - name;
3964 }
3965 }
3966
3967 this->name_components.push_back ({previous_len, idx});
3968 }
3969
3970 /* Sort name_components elements by name. */
3971 auto name_comp_compare = [&] (const name_component &left,
3972 const name_component &right)
3973 {
3974 const char *left_qualified = this->symbol_name_at (left.idx);
3975 const char *right_qualified = this->symbol_name_at (right.idx);
3976
3977 const char *left_name = left_qualified + left.name_offset;
3978 const char *right_name = right_qualified + right.name_offset;
3979
3980 return name_cmp (left_name, right_name) < 0;
3981 };
3982
3983 std::sort (this->name_components.begin (),
3984 this->name_components.end (),
3985 name_comp_compare);
3986 }
3987
3988 /* Helper for dw2_expand_symtabs_matching that works with a
3989 mapped_index_base instead of the containing objfile. This is split
3990 to a separate function in order to be able to unit test the
3991 name_components matching using a mock mapped_index_base. For each
3992 symbol name that matches, calls MATCH_CALLBACK, passing it the
3993 symbol's index in the mapped_index_base symbol table. */
3994
3995 static void
3996 dw2_expand_symtabs_matching_symbol
3997 (mapped_index_base &index,
3998 const lookup_name_info &lookup_name_in,
3999 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4000 enum search_domain kind,
4001 gdb::function_view<bool (offset_type)> match_callback)
4002 {
4003 lookup_name_info lookup_name_without_params
4004 = lookup_name_in.make_ignore_params ();
4005
4006 /* Build the symbol name component sorted vector, if we haven't
4007 yet. */
4008 index.build_name_components ();
4009
4010 /* The same symbol may appear more than once in the range though.
4011 E.g., if we're looking for symbols that complete "w", and we have
4012 a symbol named "w1::w2", we'll find the two name components for
4013 that same symbol in the range. To be sure we only call the
4014 callback once per symbol, we first collect the symbol name
4015 indexes that matched in a temporary vector and ignore
4016 duplicates. */
4017 std::vector<offset_type> matches;
4018
4019 struct name_and_matcher
4020 {
4021 symbol_name_matcher_ftype *matcher;
4022 const char *name;
4023
4024 bool operator== (const name_and_matcher &other) const
4025 {
4026 return matcher == other.matcher && strcmp (name, other.name) == 0;
4027 }
4028 };
4029
4030 /* A vector holding all the different symbol name matchers, for all
4031 languages. */
4032 std::vector<name_and_matcher> matchers;
4033
4034 for (int i = 0; i < nr_languages; i++)
4035 {
4036 enum language lang_e = (enum language) i;
4037
4038 const language_defn *lang = language_def (lang_e);
4039 symbol_name_matcher_ftype *name_matcher
4040 = get_symbol_name_matcher (lang, lookup_name_without_params);
4041
4042 name_and_matcher key {
4043 name_matcher,
4044 lookup_name_without_params.language_lookup_name (lang_e)
4045 };
4046
4047 /* Don't insert the same comparison routine more than once.
4048 Note that we do this linear walk. This is not a problem in
4049 practice because the number of supported languages is
4050 low. */
4051 if (std::find (matchers.begin (), matchers.end (), key)
4052 != matchers.end ())
4053 continue;
4054 matchers.push_back (std::move (key));
4055
4056 auto bounds
4057 = index.find_name_components_bounds (lookup_name_without_params,
4058 lang_e);
4059
4060 /* Now for each symbol name in range, check to see if we have a name
4061 match, and if so, call the MATCH_CALLBACK callback. */
4062
4063 for (; bounds.first != bounds.second; ++bounds.first)
4064 {
4065 const char *qualified = index.symbol_name_at (bounds.first->idx);
4066
4067 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4068 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4069 continue;
4070
4071 matches.push_back (bounds.first->idx);
4072 }
4073 }
4074
4075 std::sort (matches.begin (), matches.end ());
4076
4077 /* Finally call the callback, once per match. */
4078 ULONGEST prev = -1;
4079 for (offset_type idx : matches)
4080 {
4081 if (prev != idx)
4082 {
4083 if (!match_callback (idx))
4084 break;
4085 prev = idx;
4086 }
4087 }
4088
4089 /* Above we use a type wider than idx's for 'prev', since 0 and
4090 (offset_type)-1 are both possible values. */
4091 static_assert (sizeof (prev) > sizeof (offset_type), "");
4092 }
4093
4094 #if GDB_SELF_TEST
4095
4096 namespace selftests { namespace dw2_expand_symtabs_matching {
4097
4098 /* A mock .gdb_index/.debug_names-like name index table, enough to
4099 exercise dw2_expand_symtabs_matching_symbol, which works with the
4100 mapped_index_base interface. Builds an index from the symbol list
4101 passed as parameter to the constructor. */
4102 class mock_mapped_index : public mapped_index_base
4103 {
4104 public:
4105 mock_mapped_index (gdb::array_view<const char *> symbols)
4106 : m_symbol_table (symbols)
4107 {}
4108
4109 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4110
4111 /* Return the number of names in the symbol table. */
4112 size_t symbol_name_count () const override
4113 {
4114 return m_symbol_table.size ();
4115 }
4116
4117 /* Get the name of the symbol at IDX in the symbol table. */
4118 const char *symbol_name_at (offset_type idx) const override
4119 {
4120 return m_symbol_table[idx];
4121 }
4122
4123 private:
4124 gdb::array_view<const char *> m_symbol_table;
4125 };
4126
4127 /* Convenience function that converts a NULL pointer to a "<null>"
4128 string, to pass to print routines. */
4129
4130 static const char *
4131 string_or_null (const char *str)
4132 {
4133 return str != NULL ? str : "<null>";
4134 }
4135
4136 /* Check if a lookup_name_info built from
4137 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4138 index. EXPECTED_LIST is the list of expected matches, in expected
4139 matching order. If no match expected, then an empty list is
4140 specified. Returns true on success. On failure prints a warning
4141 indicating the file:line that failed, and returns false. */
4142
4143 static bool
4144 check_match (const char *file, int line,
4145 mock_mapped_index &mock_index,
4146 const char *name, symbol_name_match_type match_type,
4147 bool completion_mode,
4148 std::initializer_list<const char *> expected_list)
4149 {
4150 lookup_name_info lookup_name (name, match_type, completion_mode);
4151
4152 bool matched = true;
4153
4154 auto mismatch = [&] (const char *expected_str,
4155 const char *got)
4156 {
4157 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4158 "expected=\"%s\", got=\"%s\"\n"),
4159 file, line,
4160 (match_type == symbol_name_match_type::FULL
4161 ? "FULL" : "WILD"),
4162 name, string_or_null (expected_str), string_or_null (got));
4163 matched = false;
4164 };
4165
4166 auto expected_it = expected_list.begin ();
4167 auto expected_end = expected_list.end ();
4168
4169 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4170 NULL, ALL_DOMAIN,
4171 [&] (offset_type idx)
4172 {
4173 const char *matched_name = mock_index.symbol_name_at (idx);
4174 const char *expected_str
4175 = expected_it == expected_end ? NULL : *expected_it++;
4176
4177 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4178 mismatch (expected_str, matched_name);
4179 return true;
4180 });
4181
4182 const char *expected_str
4183 = expected_it == expected_end ? NULL : *expected_it++;
4184 if (expected_str != NULL)
4185 mismatch (expected_str, NULL);
4186
4187 return matched;
4188 }
4189
4190 /* The symbols added to the mock mapped_index for testing (in
4191 canonical form). */
4192 static const char *test_symbols[] = {
4193 "function",
4194 "std::bar",
4195 "std::zfunction",
4196 "std::zfunction2",
4197 "w1::w2",
4198 "ns::foo<char*>",
4199 "ns::foo<int>",
4200 "ns::foo<long>",
4201 "ns2::tmpl<int>::foo2",
4202 "(anonymous namespace)::A::B::C",
4203
4204 /* These are used to check that the increment-last-char in the
4205 matching algorithm for completion doesn't match "t1_fund" when
4206 completing "t1_func". */
4207 "t1_func",
4208 "t1_func1",
4209 "t1_fund",
4210 "t1_fund1",
4211
4212 /* A UTF-8 name with multi-byte sequences to make sure that
4213 cp-name-parser understands this as a single identifier ("função"
4214 is "function" in PT). */
4215 u8"u8função",
4216
4217 /* \377 (0xff) is Latin1 'ÿ'. */
4218 "yfunc\377",
4219
4220 /* \377 (0xff) is Latin1 'ÿ'. */
4221 "\377",
4222 "\377\377123",
4223
4224 /* A name with all sorts of complications. Starts with "z" to make
4225 it easier for the completion tests below. */
4226 #define Z_SYM_NAME \
4227 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4228 "::tuple<(anonymous namespace)::ui*, " \
4229 "std::default_delete<(anonymous namespace)::ui>, void>"
4230
4231 Z_SYM_NAME
4232 };
4233
4234 /* Returns true if the mapped_index_base::find_name_component_bounds
4235 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4236 in completion mode. */
4237
4238 static bool
4239 check_find_bounds_finds (mapped_index_base &index,
4240 const char *search_name,
4241 gdb::array_view<const char *> expected_syms)
4242 {
4243 lookup_name_info lookup_name (search_name,
4244 symbol_name_match_type::FULL, true);
4245
4246 auto bounds = index.find_name_components_bounds (lookup_name,
4247 language_cplus);
4248
4249 size_t distance = std::distance (bounds.first, bounds.second);
4250 if (distance != expected_syms.size ())
4251 return false;
4252
4253 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4254 {
4255 auto nc_elem = bounds.first + exp_elem;
4256 const char *qualified = index.symbol_name_at (nc_elem->idx);
4257 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4258 return false;
4259 }
4260
4261 return true;
4262 }
4263
4264 /* Test the lower-level mapped_index::find_name_component_bounds
4265 method. */
4266
4267 static void
4268 test_mapped_index_find_name_component_bounds ()
4269 {
4270 mock_mapped_index mock_index (test_symbols);
4271
4272 mock_index.build_name_components ();
4273
4274 /* Test the lower-level mapped_index::find_name_component_bounds
4275 method in completion mode. */
4276 {
4277 static const char *expected_syms[] = {
4278 "t1_func",
4279 "t1_func1",
4280 };
4281
4282 SELF_CHECK (check_find_bounds_finds (mock_index,
4283 "t1_func", expected_syms));
4284 }
4285
4286 /* Check that the increment-last-char in the name matching algorithm
4287 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4288 {
4289 static const char *expected_syms1[] = {
4290 "\377",
4291 "\377\377123",
4292 };
4293 SELF_CHECK (check_find_bounds_finds (mock_index,
4294 "\377", expected_syms1));
4295
4296 static const char *expected_syms2[] = {
4297 "\377\377123",
4298 };
4299 SELF_CHECK (check_find_bounds_finds (mock_index,
4300 "\377\377", expected_syms2));
4301 }
4302 }
4303
4304 /* Test dw2_expand_symtabs_matching_symbol. */
4305
4306 static void
4307 test_dw2_expand_symtabs_matching_symbol ()
4308 {
4309 mock_mapped_index mock_index (test_symbols);
4310
4311 /* We let all tests run until the end even if some fails, for debug
4312 convenience. */
4313 bool any_mismatch = false;
4314
4315 /* Create the expected symbols list (an initializer_list). Needed
4316 because lists have commas, and we need to pass them to CHECK,
4317 which is a macro. */
4318 #define EXPECT(...) { __VA_ARGS__ }
4319
4320 /* Wrapper for check_match that passes down the current
4321 __FILE__/__LINE__. */
4322 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4323 any_mismatch |= !check_match (__FILE__, __LINE__, \
4324 mock_index, \
4325 NAME, MATCH_TYPE, COMPLETION_MODE, \
4326 EXPECTED_LIST)
4327
4328 /* Identity checks. */
4329 for (const char *sym : test_symbols)
4330 {
4331 /* Should be able to match all existing symbols. */
4332 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4333 EXPECT (sym));
4334
4335 /* Should be able to match all existing symbols with
4336 parameters. */
4337 std::string with_params = std::string (sym) + "(int)";
4338 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4339 EXPECT (sym));
4340
4341 /* Should be able to match all existing symbols with
4342 parameters and qualifiers. */
4343 with_params = std::string (sym) + " ( int ) const";
4344 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4345 EXPECT (sym));
4346
4347 /* This should really find sym, but cp-name-parser.y doesn't
4348 know about lvalue/rvalue qualifiers yet. */
4349 with_params = std::string (sym) + " ( int ) &&";
4350 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4351 {});
4352 }
4353
4354 /* Check that the name matching algorithm for completion doesn't get
4355 confused with Latin1 'ÿ' / 0xff. */
4356 {
4357 static const char str[] = "\377";
4358 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4359 EXPECT ("\377", "\377\377123"));
4360 }
4361
4362 /* Check that the increment-last-char in the matching algorithm for
4363 completion doesn't match "t1_fund" when completing "t1_func". */
4364 {
4365 static const char str[] = "t1_func";
4366 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4367 EXPECT ("t1_func", "t1_func1"));
4368 }
4369
4370 /* Check that completion mode works at each prefix of the expected
4371 symbol name. */
4372 {
4373 static const char str[] = "function(int)";
4374 size_t len = strlen (str);
4375 std::string lookup;
4376
4377 for (size_t i = 1; i < len; i++)
4378 {
4379 lookup.assign (str, i);
4380 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4381 EXPECT ("function"));
4382 }
4383 }
4384
4385 /* While "w" is a prefix of both components, the match function
4386 should still only be called once. */
4387 {
4388 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4389 EXPECT ("w1::w2"));
4390 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4391 EXPECT ("w1::w2"));
4392 }
4393
4394 /* Same, with a "complicated" symbol. */
4395 {
4396 static const char str[] = Z_SYM_NAME;
4397 size_t len = strlen (str);
4398 std::string lookup;
4399
4400 for (size_t i = 1; i < len; i++)
4401 {
4402 lookup.assign (str, i);
4403 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4404 EXPECT (Z_SYM_NAME));
4405 }
4406 }
4407
4408 /* In FULL mode, an incomplete symbol doesn't match. */
4409 {
4410 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4411 {});
4412 }
4413
4414 /* A complete symbol with parameters matches any overload, since the
4415 index has no overload info. */
4416 {
4417 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4418 EXPECT ("std::zfunction", "std::zfunction2"));
4419 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4420 EXPECT ("std::zfunction", "std::zfunction2"));
4421 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4422 EXPECT ("std::zfunction", "std::zfunction2"));
4423 }
4424
4425 /* Check that whitespace is ignored appropriately. A symbol with a
4426 template argument list. */
4427 {
4428 static const char expected[] = "ns::foo<int>";
4429 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4430 EXPECT (expected));
4431 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4432 EXPECT (expected));
4433 }
4434
4435 /* Check that whitespace is ignored appropriately. A symbol with a
4436 template argument list that includes a pointer. */
4437 {
4438 static const char expected[] = "ns::foo<char*>";
4439 /* Try both completion and non-completion modes. */
4440 static const bool completion_mode[2] = {false, true};
4441 for (size_t i = 0; i < 2; i++)
4442 {
4443 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4444 completion_mode[i], EXPECT (expected));
4445 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4446 completion_mode[i], EXPECT (expected));
4447
4448 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4449 completion_mode[i], EXPECT (expected));
4450 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4451 completion_mode[i], EXPECT (expected));
4452 }
4453 }
4454
4455 {
4456 /* Check method qualifiers are ignored. */
4457 static const char expected[] = "ns::foo<char*>";
4458 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4459 symbol_name_match_type::FULL, true, EXPECT (expected));
4460 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4461 symbol_name_match_type::FULL, true, EXPECT (expected));
4462 CHECK_MATCH ("foo < char * > ( int ) const",
4463 symbol_name_match_type::WILD, true, EXPECT (expected));
4464 CHECK_MATCH ("foo < char * > ( int ) &&",
4465 symbol_name_match_type::WILD, true, EXPECT (expected));
4466 }
4467
4468 /* Test lookup names that don't match anything. */
4469 {
4470 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4471 {});
4472
4473 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4474 {});
4475 }
4476
4477 /* Some wild matching tests, exercising "(anonymous namespace)",
4478 which should not be confused with a parameter list. */
4479 {
4480 static const char *syms[] = {
4481 "A::B::C",
4482 "B::C",
4483 "C",
4484 "A :: B :: C ( int )",
4485 "B :: C ( int )",
4486 "C ( int )",
4487 };
4488
4489 for (const char *s : syms)
4490 {
4491 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4492 EXPECT ("(anonymous namespace)::A::B::C"));
4493 }
4494 }
4495
4496 {
4497 static const char expected[] = "ns2::tmpl<int>::foo2";
4498 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4499 EXPECT (expected));
4500 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4501 EXPECT (expected));
4502 }
4503
4504 SELF_CHECK (!any_mismatch);
4505
4506 #undef EXPECT
4507 #undef CHECK_MATCH
4508 }
4509
4510 static void
4511 run_test ()
4512 {
4513 test_mapped_index_find_name_component_bounds ();
4514 test_dw2_expand_symtabs_matching_symbol ();
4515 }
4516
4517 }} // namespace selftests::dw2_expand_symtabs_matching
4518
4519 #endif /* GDB_SELF_TEST */
4520
4521 /* If FILE_MATCHER is NULL or if PER_CU has
4522 dwarf2_per_cu_quick_data::MARK set (see
4523 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4524 EXPANSION_NOTIFY on it. */
4525
4526 static void
4527 dw2_expand_symtabs_matching_one
4528 (struct dwarf2_per_cu_data *per_cu,
4529 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4530 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4531 {
4532 if (file_matcher == NULL || per_cu->v.quick->mark)
4533 {
4534 dwarf2_per_objfile *per_objfile = per_cu->dwarf2_per_objfile;
4535 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4536
4537 compunit_symtab *symtab = dw2_instantiate_symtab (per_cu, false);
4538 gdb_assert (symtab != nullptr);
4539
4540 if (expansion_notify != NULL && symtab_was_null)
4541 expansion_notify (symtab);
4542 }
4543 }
4544
4545 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4546 matched, to expand corresponding CUs that were marked. IDX is the
4547 index of the symbol name that matched. */
4548
4549 static void
4550 dw2_expand_marked_cus
4551 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4552 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4553 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4554 search_domain kind)
4555 {
4556 offset_type *vec, vec_len, vec_idx;
4557 bool global_seen = false;
4558 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4559
4560 vec = (offset_type *) (index.constant_pool
4561 + MAYBE_SWAP (index.symbol_table[idx].vec));
4562 vec_len = MAYBE_SWAP (vec[0]);
4563 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4564 {
4565 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4566 /* This value is only valid for index versions >= 7. */
4567 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4568 gdb_index_symbol_kind symbol_kind =
4569 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4570 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4571 /* Only check the symbol attributes if they're present.
4572 Indices prior to version 7 don't record them,
4573 and indices >= 7 may elide them for certain symbols
4574 (gold does this). */
4575 int attrs_valid =
4576 (index.version >= 7
4577 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4578
4579 /* Work around gold/15646. */
4580 if (attrs_valid)
4581 {
4582 if (!is_static && global_seen)
4583 continue;
4584 if (!is_static)
4585 global_seen = true;
4586 }
4587
4588 /* Only check the symbol's kind if it has one. */
4589 if (attrs_valid)
4590 {
4591 switch (kind)
4592 {
4593 case VARIABLES_DOMAIN:
4594 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4595 continue;
4596 break;
4597 case FUNCTIONS_DOMAIN:
4598 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4599 continue;
4600 break;
4601 case TYPES_DOMAIN:
4602 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4603 continue;
4604 break;
4605 case MODULES_DOMAIN:
4606 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4607 continue;
4608 break;
4609 default:
4610 break;
4611 }
4612 }
4613
4614 /* Don't crash on bad data. */
4615 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4616 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4617 {
4618 complaint (_(".gdb_index entry has bad CU index"
4619 " [in module %s]"),
4620 objfile_name (dwarf2_per_objfile->objfile));
4621 continue;
4622 }
4623
4624 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4625 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4626 expansion_notify);
4627 }
4628 }
4629
4630 /* If FILE_MATCHER is non-NULL, set all the
4631 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4632 that match FILE_MATCHER. */
4633
4634 static void
4635 dw_expand_symtabs_matching_file_matcher
4636 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4637 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4638 {
4639 if (file_matcher == NULL)
4640 return;
4641
4642 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4643 htab_eq_pointer,
4644 NULL, xcalloc, xfree));
4645 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4646 htab_eq_pointer,
4647 NULL, xcalloc, xfree));
4648
4649 /* The rule is CUs specify all the files, including those used by
4650 any TU, so there's no need to scan TUs here. */
4651
4652 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4653 {
4654 QUIT;
4655
4656 per_cu->v.quick->mark = 0;
4657
4658 /* We only need to look at symtabs not already expanded. */
4659 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4660 continue;
4661
4662 quick_file_names *file_data = dw2_get_file_names (per_cu);
4663 if (file_data == NULL)
4664 continue;
4665
4666 if (htab_find (visited_not_found.get (), file_data) != NULL)
4667 continue;
4668 else if (htab_find (visited_found.get (), file_data) != NULL)
4669 {
4670 per_cu->v.quick->mark = 1;
4671 continue;
4672 }
4673
4674 for (int j = 0; j < file_data->num_file_names; ++j)
4675 {
4676 const char *this_real_name;
4677
4678 if (file_matcher (file_data->file_names[j], false))
4679 {
4680 per_cu->v.quick->mark = 1;
4681 break;
4682 }
4683
4684 /* Before we invoke realpath, which can get expensive when many
4685 files are involved, do a quick comparison of the basenames. */
4686 if (!basenames_may_differ
4687 && !file_matcher (lbasename (file_data->file_names[j]),
4688 true))
4689 continue;
4690
4691 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4692 file_data, j);
4693 if (file_matcher (this_real_name, false))
4694 {
4695 per_cu->v.quick->mark = 1;
4696 break;
4697 }
4698 }
4699
4700 void **slot = htab_find_slot (per_cu->v.quick->mark
4701 ? visited_found.get ()
4702 : visited_not_found.get (),
4703 file_data, INSERT);
4704 *slot = file_data;
4705 }
4706 }
4707
4708 static void
4709 dw2_expand_symtabs_matching
4710 (struct objfile *objfile,
4711 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4712 const lookup_name_info *lookup_name,
4713 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4714 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4715 enum search_domain kind)
4716 {
4717 struct dwarf2_per_objfile *dwarf2_per_objfile
4718 = get_dwarf2_per_objfile (objfile);
4719
4720 /* index_table is NULL if OBJF_READNOW. */
4721 if (!dwarf2_per_objfile->per_bfd->index_table)
4722 return;
4723
4724 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4725
4726 if (symbol_matcher == NULL && lookup_name == NULL)
4727 {
4728 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4729 {
4730 QUIT;
4731
4732 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4733 expansion_notify);
4734 }
4735 return;
4736 }
4737
4738 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4739
4740 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4741 symbol_matcher,
4742 kind, [&] (offset_type idx)
4743 {
4744 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4745 expansion_notify, kind);
4746 return true;
4747 });
4748 }
4749
4750 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4751 symtab. */
4752
4753 static struct compunit_symtab *
4754 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4755 CORE_ADDR pc)
4756 {
4757 int i;
4758
4759 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4760 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4761 return cust;
4762
4763 if (cust->includes == NULL)
4764 return NULL;
4765
4766 for (i = 0; cust->includes[i]; ++i)
4767 {
4768 struct compunit_symtab *s = cust->includes[i];
4769
4770 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4771 if (s != NULL)
4772 return s;
4773 }
4774
4775 return NULL;
4776 }
4777
4778 static struct compunit_symtab *
4779 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4780 struct bound_minimal_symbol msymbol,
4781 CORE_ADDR pc,
4782 struct obj_section *section,
4783 int warn_if_readin)
4784 {
4785 struct dwarf2_per_cu_data *data;
4786 struct compunit_symtab *result;
4787
4788 if (!objfile->partial_symtabs->psymtabs_addrmap)
4789 return NULL;
4790
4791 CORE_ADDR baseaddr = objfile->text_section_offset ();
4792 data = (struct dwarf2_per_cu_data *) addrmap_find
4793 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4794 if (!data)
4795 return NULL;
4796
4797 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4798 if (warn_if_readin && per_objfile->symtab_set_p (data))
4799 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4800 paddress (objfile->arch (), pc));
4801
4802 result
4803 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4804 false),
4805 pc);
4806 gdb_assert (result != NULL);
4807 return result;
4808 }
4809
4810 static void
4811 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4812 void *data, int need_fullname)
4813 {
4814 struct dwarf2_per_objfile *dwarf2_per_objfile
4815 = get_dwarf2_per_objfile (objfile);
4816
4817 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4818 {
4819 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4820
4821 htab_up visited (htab_create_alloc (10,
4822 htab_hash_pointer, htab_eq_pointer,
4823 NULL, xcalloc, xfree));
4824
4825 /* The rule is CUs specify all the files, including those used
4826 by any TU, so there's no need to scan TUs here. We can
4827 ignore file names coming from already-expanded CUs. */
4828
4829 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4830 {
4831 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4832 {
4833 void **slot = htab_find_slot (visited.get (),
4834 per_cu->v.quick->file_names,
4835 INSERT);
4836
4837 *slot = per_cu->v.quick->file_names;
4838 }
4839 }
4840
4841 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4842 {
4843 /* We only need to look at symtabs not already expanded. */
4844 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4845 continue;
4846
4847 quick_file_names *file_data = dw2_get_file_names (per_cu);
4848 if (file_data == NULL)
4849 continue;
4850
4851 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4852 if (*slot)
4853 {
4854 /* Already visited. */
4855 continue;
4856 }
4857 *slot = file_data;
4858
4859 for (int j = 0; j < file_data->num_file_names; ++j)
4860 {
4861 const char *filename = file_data->file_names[j];
4862 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4863 }
4864 }
4865 }
4866
4867 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4868 {
4869 gdb::unique_xmalloc_ptr<char> this_real_name;
4870
4871 if (need_fullname)
4872 this_real_name = gdb_realpath (filename);
4873 (*fun) (filename, this_real_name.get (), data);
4874 });
4875 }
4876
4877 static int
4878 dw2_has_symbols (struct objfile *objfile)
4879 {
4880 return 1;
4881 }
4882
4883 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4884 {
4885 dw2_has_symbols,
4886 dw2_find_last_source_symtab,
4887 dw2_forget_cached_source_info,
4888 dw2_map_symtabs_matching_filename,
4889 dw2_lookup_symbol,
4890 NULL,
4891 dw2_print_stats,
4892 dw2_dump,
4893 dw2_expand_symtabs_for_function,
4894 dw2_expand_all_symtabs,
4895 dw2_expand_symtabs_with_fullname,
4896 dw2_map_matching_symbols,
4897 dw2_expand_symtabs_matching,
4898 dw2_find_pc_sect_compunit_symtab,
4899 NULL,
4900 dw2_map_symbol_filenames
4901 };
4902
4903 /* DWARF-5 debug_names reader. */
4904
4905 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4906 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4907
4908 /* A helper function that reads the .debug_names section in SECTION
4909 and fills in MAP. FILENAME is the name of the file containing the
4910 section; it is used for error reporting.
4911
4912 Returns true if all went well, false otherwise. */
4913
4914 static bool
4915 read_debug_names_from_section (struct objfile *objfile,
4916 const char *filename,
4917 struct dwarf2_section_info *section,
4918 mapped_debug_names &map)
4919 {
4920 if (section->empty ())
4921 return false;
4922
4923 /* Older elfutils strip versions could keep the section in the main
4924 executable while splitting it for the separate debug info file. */
4925 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4926 return false;
4927
4928 section->read (objfile);
4929
4930 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4931
4932 const gdb_byte *addr = section->buffer;
4933
4934 bfd *const abfd = section->get_bfd_owner ();
4935
4936 unsigned int bytes_read;
4937 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4938 addr += bytes_read;
4939
4940 map.dwarf5_is_dwarf64 = bytes_read != 4;
4941 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4942 if (bytes_read + length != section->size)
4943 {
4944 /* There may be multiple per-CU indices. */
4945 warning (_("Section .debug_names in %s length %s does not match "
4946 "section length %s, ignoring .debug_names."),
4947 filename, plongest (bytes_read + length),
4948 pulongest (section->size));
4949 return false;
4950 }
4951
4952 /* The version number. */
4953 uint16_t version = read_2_bytes (abfd, addr);
4954 addr += 2;
4955 if (version != 5)
4956 {
4957 warning (_("Section .debug_names in %s has unsupported version %d, "
4958 "ignoring .debug_names."),
4959 filename, version);
4960 return false;
4961 }
4962
4963 /* Padding. */
4964 uint16_t padding = read_2_bytes (abfd, addr);
4965 addr += 2;
4966 if (padding != 0)
4967 {
4968 warning (_("Section .debug_names in %s has unsupported padding %d, "
4969 "ignoring .debug_names."),
4970 filename, padding);
4971 return false;
4972 }
4973
4974 /* comp_unit_count - The number of CUs in the CU list. */
4975 map.cu_count = read_4_bytes (abfd, addr);
4976 addr += 4;
4977
4978 /* local_type_unit_count - The number of TUs in the local TU
4979 list. */
4980 map.tu_count = read_4_bytes (abfd, addr);
4981 addr += 4;
4982
4983 /* foreign_type_unit_count - The number of TUs in the foreign TU
4984 list. */
4985 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4986 addr += 4;
4987 if (foreign_tu_count != 0)
4988 {
4989 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4990 "ignoring .debug_names."),
4991 filename, static_cast<unsigned long> (foreign_tu_count));
4992 return false;
4993 }
4994
4995 /* bucket_count - The number of hash buckets in the hash lookup
4996 table. */
4997 map.bucket_count = read_4_bytes (abfd, addr);
4998 addr += 4;
4999
5000 /* name_count - The number of unique names in the index. */
5001 map.name_count = read_4_bytes (abfd, addr);
5002 addr += 4;
5003
5004 /* abbrev_table_size - The size in bytes of the abbreviations
5005 table. */
5006 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5007 addr += 4;
5008
5009 /* augmentation_string_size - The size in bytes of the augmentation
5010 string. This value is rounded up to a multiple of 4. */
5011 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5012 addr += 4;
5013 map.augmentation_is_gdb = ((augmentation_string_size
5014 == sizeof (dwarf5_augmentation))
5015 && memcmp (addr, dwarf5_augmentation,
5016 sizeof (dwarf5_augmentation)) == 0);
5017 augmentation_string_size += (-augmentation_string_size) & 3;
5018 addr += augmentation_string_size;
5019
5020 /* List of CUs */
5021 map.cu_table_reordered = addr;
5022 addr += map.cu_count * map.offset_size;
5023
5024 /* List of Local TUs */
5025 map.tu_table_reordered = addr;
5026 addr += map.tu_count * map.offset_size;
5027
5028 /* Hash Lookup Table */
5029 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5030 addr += map.bucket_count * 4;
5031 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5032 addr += map.name_count * 4;
5033
5034 /* Name Table */
5035 map.name_table_string_offs_reordered = addr;
5036 addr += map.name_count * map.offset_size;
5037 map.name_table_entry_offs_reordered = addr;
5038 addr += map.name_count * map.offset_size;
5039
5040 const gdb_byte *abbrev_table_start = addr;
5041 for (;;)
5042 {
5043 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5044 addr += bytes_read;
5045 if (index_num == 0)
5046 break;
5047
5048 const auto insertpair
5049 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5050 if (!insertpair.second)
5051 {
5052 warning (_("Section .debug_names in %s has duplicate index %s, "
5053 "ignoring .debug_names."),
5054 filename, pulongest (index_num));
5055 return false;
5056 }
5057 mapped_debug_names::index_val &indexval = insertpair.first->second;
5058 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5059 addr += bytes_read;
5060
5061 for (;;)
5062 {
5063 mapped_debug_names::index_val::attr attr;
5064 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5065 addr += bytes_read;
5066 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5067 addr += bytes_read;
5068 if (attr.form == DW_FORM_implicit_const)
5069 {
5070 attr.implicit_const = read_signed_leb128 (abfd, addr,
5071 &bytes_read);
5072 addr += bytes_read;
5073 }
5074 if (attr.dw_idx == 0 && attr.form == 0)
5075 break;
5076 indexval.attr_vec.push_back (std::move (attr));
5077 }
5078 }
5079 if (addr != abbrev_table_start + abbrev_table_size)
5080 {
5081 warning (_("Section .debug_names in %s has abbreviation_table "
5082 "of size %s vs. written as %u, ignoring .debug_names."),
5083 filename, plongest (addr - abbrev_table_start),
5084 abbrev_table_size);
5085 return false;
5086 }
5087 map.entry_pool = addr;
5088
5089 return true;
5090 }
5091
5092 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5093 list. */
5094
5095 static void
5096 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5097 const mapped_debug_names &map,
5098 dwarf2_section_info &section,
5099 bool is_dwz)
5100 {
5101 if (!map.augmentation_is_gdb)
5102 {
5103 for (uint32_t i = 0; i < map.cu_count; ++i)
5104 {
5105 sect_offset sect_off
5106 = (sect_offset) (extract_unsigned_integer
5107 (map.cu_table_reordered + i * map.offset_size,
5108 map.offset_size,
5109 map.dwarf5_byte_order));
5110 /* We don't know the length of the CU, because the CU list in a
5111 .debug_names index can be incomplete, so we can't use the start of
5112 the next CU as end of this CU. We create the CUs here with length 0,
5113 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5114 dwarf2_per_cu_data *per_cu
5115 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5116 sect_off, 0);
5117 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5118 }
5119 }
5120
5121 sect_offset sect_off_prev;
5122 for (uint32_t i = 0; i <= map.cu_count; ++i)
5123 {
5124 sect_offset sect_off_next;
5125 if (i < map.cu_count)
5126 {
5127 sect_off_next
5128 = (sect_offset) (extract_unsigned_integer
5129 (map.cu_table_reordered + i * map.offset_size,
5130 map.offset_size,
5131 map.dwarf5_byte_order));
5132 }
5133 else
5134 sect_off_next = (sect_offset) section.size;
5135 if (i >= 1)
5136 {
5137 const ULONGEST length = sect_off_next - sect_off_prev;
5138 dwarf2_per_cu_data *per_cu
5139 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5140 sect_off_prev, length);
5141 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5142 }
5143 sect_off_prev = sect_off_next;
5144 }
5145 }
5146
5147 /* Read the CU list from the mapped index, and use it to create all
5148 the CU objects for this dwarf2_per_objfile. */
5149
5150 static void
5151 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5152 const mapped_debug_names &map,
5153 const mapped_debug_names &dwz_map)
5154 {
5155 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5156 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5157
5158 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5159 dwarf2_per_objfile->per_bfd->info,
5160 false /* is_dwz */);
5161
5162 if (dwz_map.cu_count == 0)
5163 return;
5164
5165 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5166 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5167 true /* is_dwz */);
5168 }
5169
5170 /* Read .debug_names. If everything went ok, initialize the "quick"
5171 elements of all the CUs and return true. Otherwise, return false. */
5172
5173 static bool
5174 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5175 {
5176 std::unique_ptr<mapped_debug_names> map
5177 (new mapped_debug_names (dwarf2_per_objfile));
5178 mapped_debug_names dwz_map (dwarf2_per_objfile);
5179 struct objfile *objfile = dwarf2_per_objfile->objfile;
5180
5181 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5182 &dwarf2_per_objfile->per_bfd->debug_names,
5183 *map))
5184 return false;
5185
5186 /* Don't use the index if it's empty. */
5187 if (map->name_count == 0)
5188 return false;
5189
5190 /* If there is a .dwz file, read it so we can get its CU list as
5191 well. */
5192 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5193 if (dwz != NULL)
5194 {
5195 if (!read_debug_names_from_section (objfile,
5196 bfd_get_filename (dwz->dwz_bfd.get ()),
5197 &dwz->debug_names, dwz_map))
5198 {
5199 warning (_("could not read '.debug_names' section from %s; skipping"),
5200 bfd_get_filename (dwz->dwz_bfd.get ()));
5201 return false;
5202 }
5203 }
5204
5205 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5206
5207 if (map->tu_count != 0)
5208 {
5209 /* We can only handle a single .debug_types when we have an
5210 index. */
5211 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5212 return false;
5213
5214 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5215
5216 create_signatured_type_table_from_debug_names
5217 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5218 }
5219
5220 create_addrmap_from_aranges (dwarf2_per_objfile,
5221 &dwarf2_per_objfile->per_bfd->debug_aranges);
5222
5223 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5224 dwarf2_per_objfile->per_bfd->using_index = 1;
5225 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5226 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5227
5228 return true;
5229 }
5230
5231 /* Type used to manage iterating over all CUs looking for a symbol for
5232 .debug_names. */
5233
5234 class dw2_debug_names_iterator
5235 {
5236 public:
5237 dw2_debug_names_iterator (const mapped_debug_names &map,
5238 gdb::optional<block_enum> block_index,
5239 domain_enum domain,
5240 const char *name)
5241 : m_map (map), m_block_index (block_index), m_domain (domain),
5242 m_addr (find_vec_in_debug_names (map, name))
5243 {}
5244
5245 dw2_debug_names_iterator (const mapped_debug_names &map,
5246 search_domain search, uint32_t namei)
5247 : m_map (map),
5248 m_search (search),
5249 m_addr (find_vec_in_debug_names (map, namei))
5250 {}
5251
5252 dw2_debug_names_iterator (const mapped_debug_names &map,
5253 block_enum block_index, domain_enum domain,
5254 uint32_t namei)
5255 : m_map (map), m_block_index (block_index), m_domain (domain),
5256 m_addr (find_vec_in_debug_names (map, namei))
5257 {}
5258
5259 /* Return the next matching CU or NULL if there are no more. */
5260 dwarf2_per_cu_data *next ();
5261
5262 private:
5263 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5264 const char *name);
5265 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5266 uint32_t namei);
5267
5268 /* The internalized form of .debug_names. */
5269 const mapped_debug_names &m_map;
5270
5271 /* If set, only look for symbols that match that block. Valid values are
5272 GLOBAL_BLOCK and STATIC_BLOCK. */
5273 const gdb::optional<block_enum> m_block_index;
5274
5275 /* The kind of symbol we're looking for. */
5276 const domain_enum m_domain = UNDEF_DOMAIN;
5277 const search_domain m_search = ALL_DOMAIN;
5278
5279 /* The list of CUs from the index entry of the symbol, or NULL if
5280 not found. */
5281 const gdb_byte *m_addr;
5282 };
5283
5284 const char *
5285 mapped_debug_names::namei_to_name (uint32_t namei) const
5286 {
5287 const ULONGEST namei_string_offs
5288 = extract_unsigned_integer ((name_table_string_offs_reordered
5289 + namei * offset_size),
5290 offset_size,
5291 dwarf5_byte_order);
5292 return read_indirect_string_at_offset (dwarf2_per_objfile,
5293 namei_string_offs);
5294 }
5295
5296 /* Find a slot in .debug_names for the object named NAME. If NAME is
5297 found, return pointer to its pool data. If NAME cannot be found,
5298 return NULL. */
5299
5300 const gdb_byte *
5301 dw2_debug_names_iterator::find_vec_in_debug_names
5302 (const mapped_debug_names &map, const char *name)
5303 {
5304 int (*cmp) (const char *, const char *);
5305
5306 gdb::unique_xmalloc_ptr<char> without_params;
5307 if (current_language->la_language == language_cplus
5308 || current_language->la_language == language_fortran
5309 || current_language->la_language == language_d)
5310 {
5311 /* NAME is already canonical. Drop any qualifiers as
5312 .debug_names does not contain any. */
5313
5314 if (strchr (name, '(') != NULL)
5315 {
5316 without_params = cp_remove_params (name);
5317 if (without_params != NULL)
5318 name = without_params.get ();
5319 }
5320 }
5321
5322 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5323
5324 const uint32_t full_hash = dwarf5_djb_hash (name);
5325 uint32_t namei
5326 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5327 (map.bucket_table_reordered
5328 + (full_hash % map.bucket_count)), 4,
5329 map.dwarf5_byte_order);
5330 if (namei == 0)
5331 return NULL;
5332 --namei;
5333 if (namei >= map.name_count)
5334 {
5335 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5336 "[in module %s]"),
5337 namei, map.name_count,
5338 objfile_name (map.dwarf2_per_objfile->objfile));
5339 return NULL;
5340 }
5341
5342 for (;;)
5343 {
5344 const uint32_t namei_full_hash
5345 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5346 (map.hash_table_reordered + namei), 4,
5347 map.dwarf5_byte_order);
5348 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5349 return NULL;
5350
5351 if (full_hash == namei_full_hash)
5352 {
5353 const char *const namei_string = map.namei_to_name (namei);
5354
5355 #if 0 /* An expensive sanity check. */
5356 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5357 {
5358 complaint (_("Wrong .debug_names hash for string at index %u "
5359 "[in module %s]"),
5360 namei, objfile_name (dwarf2_per_objfile->objfile));
5361 return NULL;
5362 }
5363 #endif
5364
5365 if (cmp (namei_string, name) == 0)
5366 {
5367 const ULONGEST namei_entry_offs
5368 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5369 + namei * map.offset_size),
5370 map.offset_size, map.dwarf5_byte_order);
5371 return map.entry_pool + namei_entry_offs;
5372 }
5373 }
5374
5375 ++namei;
5376 if (namei >= map.name_count)
5377 return NULL;
5378 }
5379 }
5380
5381 const gdb_byte *
5382 dw2_debug_names_iterator::find_vec_in_debug_names
5383 (const mapped_debug_names &map, uint32_t namei)
5384 {
5385 if (namei >= map.name_count)
5386 {
5387 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5388 "[in module %s]"),
5389 namei, map.name_count,
5390 objfile_name (map.dwarf2_per_objfile->objfile));
5391 return NULL;
5392 }
5393
5394 const ULONGEST namei_entry_offs
5395 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5396 + namei * map.offset_size),
5397 map.offset_size, map.dwarf5_byte_order);
5398 return map.entry_pool + namei_entry_offs;
5399 }
5400
5401 /* See dw2_debug_names_iterator. */
5402
5403 dwarf2_per_cu_data *
5404 dw2_debug_names_iterator::next ()
5405 {
5406 if (m_addr == NULL)
5407 return NULL;
5408
5409 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5410 struct objfile *objfile = dwarf2_per_objfile->objfile;
5411 bfd *const abfd = objfile->obfd;
5412
5413 again:
5414
5415 unsigned int bytes_read;
5416 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5417 m_addr += bytes_read;
5418 if (abbrev == 0)
5419 return NULL;
5420
5421 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5422 if (indexval_it == m_map.abbrev_map.cend ())
5423 {
5424 complaint (_("Wrong .debug_names undefined abbrev code %s "
5425 "[in module %s]"),
5426 pulongest (abbrev), objfile_name (objfile));
5427 return NULL;
5428 }
5429 const mapped_debug_names::index_val &indexval = indexval_it->second;
5430 enum class symbol_linkage {
5431 unknown,
5432 static_,
5433 extern_,
5434 } symbol_linkage_ = symbol_linkage::unknown;
5435 dwarf2_per_cu_data *per_cu = NULL;
5436 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5437 {
5438 ULONGEST ull;
5439 switch (attr.form)
5440 {
5441 case DW_FORM_implicit_const:
5442 ull = attr.implicit_const;
5443 break;
5444 case DW_FORM_flag_present:
5445 ull = 1;
5446 break;
5447 case DW_FORM_udata:
5448 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5449 m_addr += bytes_read;
5450 break;
5451 case DW_FORM_ref4:
5452 ull = read_4_bytes (abfd, m_addr);
5453 m_addr += 4;
5454 break;
5455 case DW_FORM_ref8:
5456 ull = read_8_bytes (abfd, m_addr);
5457 m_addr += 8;
5458 break;
5459 case DW_FORM_ref_sig8:
5460 ull = read_8_bytes (abfd, m_addr);
5461 m_addr += 8;
5462 break;
5463 default:
5464 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5465 dwarf_form_name (attr.form),
5466 objfile_name (objfile));
5467 return NULL;
5468 }
5469 switch (attr.dw_idx)
5470 {
5471 case DW_IDX_compile_unit:
5472 /* Don't crash on bad data. */
5473 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5474 {
5475 complaint (_(".debug_names entry has bad CU index %s"
5476 " [in module %s]"),
5477 pulongest (ull),
5478 objfile_name (dwarf2_per_objfile->objfile));
5479 continue;
5480 }
5481 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5482 break;
5483 case DW_IDX_type_unit:
5484 /* Don't crash on bad data. */
5485 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5486 {
5487 complaint (_(".debug_names entry has bad TU index %s"
5488 " [in module %s]"),
5489 pulongest (ull),
5490 objfile_name (dwarf2_per_objfile->objfile));
5491 continue;
5492 }
5493 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5494 break;
5495 case DW_IDX_die_offset:
5496 /* In a per-CU index (as opposed to a per-module index), index
5497 entries without CU attribute implicitly refer to the single CU. */
5498 if (per_cu == NULL)
5499 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5500 break;
5501 case DW_IDX_GNU_internal:
5502 if (!m_map.augmentation_is_gdb)
5503 break;
5504 symbol_linkage_ = symbol_linkage::static_;
5505 break;
5506 case DW_IDX_GNU_external:
5507 if (!m_map.augmentation_is_gdb)
5508 break;
5509 symbol_linkage_ = symbol_linkage::extern_;
5510 break;
5511 }
5512 }
5513
5514 /* Skip if already read in. */
5515 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5516 goto again;
5517
5518 /* Check static vs global. */
5519 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5520 {
5521 const bool want_static = *m_block_index == STATIC_BLOCK;
5522 const bool symbol_is_static =
5523 symbol_linkage_ == symbol_linkage::static_;
5524 if (want_static != symbol_is_static)
5525 goto again;
5526 }
5527
5528 /* Match dw2_symtab_iter_next, symbol_kind
5529 and debug_names::psymbol_tag. */
5530 switch (m_domain)
5531 {
5532 case VAR_DOMAIN:
5533 switch (indexval.dwarf_tag)
5534 {
5535 case DW_TAG_variable:
5536 case DW_TAG_subprogram:
5537 /* Some types are also in VAR_DOMAIN. */
5538 case DW_TAG_typedef:
5539 case DW_TAG_structure_type:
5540 break;
5541 default:
5542 goto again;
5543 }
5544 break;
5545 case STRUCT_DOMAIN:
5546 switch (indexval.dwarf_tag)
5547 {
5548 case DW_TAG_typedef:
5549 case DW_TAG_structure_type:
5550 break;
5551 default:
5552 goto again;
5553 }
5554 break;
5555 case LABEL_DOMAIN:
5556 switch (indexval.dwarf_tag)
5557 {
5558 case 0:
5559 case DW_TAG_variable:
5560 break;
5561 default:
5562 goto again;
5563 }
5564 break;
5565 case MODULE_DOMAIN:
5566 switch (indexval.dwarf_tag)
5567 {
5568 case DW_TAG_module:
5569 break;
5570 default:
5571 goto again;
5572 }
5573 break;
5574 default:
5575 break;
5576 }
5577
5578 /* Match dw2_expand_symtabs_matching, symbol_kind and
5579 debug_names::psymbol_tag. */
5580 switch (m_search)
5581 {
5582 case VARIABLES_DOMAIN:
5583 switch (indexval.dwarf_tag)
5584 {
5585 case DW_TAG_variable:
5586 break;
5587 default:
5588 goto again;
5589 }
5590 break;
5591 case FUNCTIONS_DOMAIN:
5592 switch (indexval.dwarf_tag)
5593 {
5594 case DW_TAG_subprogram:
5595 break;
5596 default:
5597 goto again;
5598 }
5599 break;
5600 case TYPES_DOMAIN:
5601 switch (indexval.dwarf_tag)
5602 {
5603 case DW_TAG_typedef:
5604 case DW_TAG_structure_type:
5605 break;
5606 default:
5607 goto again;
5608 }
5609 break;
5610 case MODULES_DOMAIN:
5611 switch (indexval.dwarf_tag)
5612 {
5613 case DW_TAG_module:
5614 break;
5615 default:
5616 goto again;
5617 }
5618 default:
5619 break;
5620 }
5621
5622 return per_cu;
5623 }
5624
5625 static struct compunit_symtab *
5626 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5627 const char *name, domain_enum domain)
5628 {
5629 struct dwarf2_per_objfile *dwarf2_per_objfile
5630 = get_dwarf2_per_objfile (objfile);
5631
5632 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5633 if (!mapp)
5634 {
5635 /* index is NULL if OBJF_READNOW. */
5636 return NULL;
5637 }
5638 const auto &map = *mapp;
5639
5640 dw2_debug_names_iterator iter (map, block_index, domain, name);
5641
5642 struct compunit_symtab *stab_best = NULL;
5643 struct dwarf2_per_cu_data *per_cu;
5644 while ((per_cu = iter.next ()) != NULL)
5645 {
5646 struct symbol *sym, *with_opaque = NULL;
5647 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5648 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5649 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5650
5651 sym = block_find_symbol (block, name, domain,
5652 block_find_non_opaque_type_preferred,
5653 &with_opaque);
5654
5655 /* Some caution must be observed with overloaded functions and
5656 methods, since the index will not contain any overload
5657 information (but NAME might contain it). */
5658
5659 if (sym != NULL
5660 && strcmp_iw (sym->search_name (), name) == 0)
5661 return stab;
5662 if (with_opaque != NULL
5663 && strcmp_iw (with_opaque->search_name (), name) == 0)
5664 stab_best = stab;
5665
5666 /* Keep looking through other CUs. */
5667 }
5668
5669 return stab_best;
5670 }
5671
5672 /* This dumps minimal information about .debug_names. It is called
5673 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5674 uses this to verify that .debug_names has been loaded. */
5675
5676 static void
5677 dw2_debug_names_dump (struct objfile *objfile)
5678 {
5679 struct dwarf2_per_objfile *dwarf2_per_objfile
5680 = get_dwarf2_per_objfile (objfile);
5681
5682 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5683 printf_filtered (".debug_names:");
5684 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5685 printf_filtered (" exists\n");
5686 else
5687 printf_filtered (" faked for \"readnow\"\n");
5688 printf_filtered ("\n");
5689 }
5690
5691 static void
5692 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5693 const char *func_name)
5694 {
5695 struct dwarf2_per_objfile *dwarf2_per_objfile
5696 = get_dwarf2_per_objfile (objfile);
5697
5698 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5699 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5700 {
5701 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5702
5703 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5704
5705 struct dwarf2_per_cu_data *per_cu;
5706 while ((per_cu = iter.next ()) != NULL)
5707 dw2_instantiate_symtab (per_cu, false);
5708 }
5709 }
5710
5711 static void
5712 dw2_debug_names_map_matching_symbols
5713 (struct objfile *objfile,
5714 const lookup_name_info &name, domain_enum domain,
5715 int global,
5716 gdb::function_view<symbol_found_callback_ftype> callback,
5717 symbol_compare_ftype *ordered_compare)
5718 {
5719 struct dwarf2_per_objfile *dwarf2_per_objfile
5720 = get_dwarf2_per_objfile (objfile);
5721
5722 /* debug_names_table is NULL if OBJF_READNOW. */
5723 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5724 return;
5725
5726 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5727 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5728
5729 const char *match_name = name.ada ().lookup_name ().c_str ();
5730 auto matcher = [&] (const char *symname)
5731 {
5732 if (ordered_compare == nullptr)
5733 return true;
5734 return ordered_compare (symname, match_name) == 0;
5735 };
5736
5737 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5738 [&] (offset_type namei)
5739 {
5740 /* The name was matched, now expand corresponding CUs that were
5741 marked. */
5742 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5743
5744 struct dwarf2_per_cu_data *per_cu;
5745 while ((per_cu = iter.next ()) != NULL)
5746 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5747 return true;
5748 });
5749
5750 /* It's a shame we couldn't do this inside the
5751 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5752 that have already been expanded. Instead, this loop matches what
5753 the psymtab code does. */
5754 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5755 {
5756 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5757 if (symtab != nullptr)
5758 {
5759 const struct block *block
5760 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5761 if (!iterate_over_symbols_terminated (block, name,
5762 domain, callback))
5763 break;
5764 }
5765 }
5766 }
5767
5768 static void
5769 dw2_debug_names_expand_symtabs_matching
5770 (struct objfile *objfile,
5771 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5772 const lookup_name_info *lookup_name,
5773 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5774 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5775 enum search_domain kind)
5776 {
5777 struct dwarf2_per_objfile *dwarf2_per_objfile
5778 = get_dwarf2_per_objfile (objfile);
5779
5780 /* debug_names_table is NULL if OBJF_READNOW. */
5781 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5782 return;
5783
5784 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5785
5786 if (symbol_matcher == NULL && lookup_name == NULL)
5787 {
5788 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5789 {
5790 QUIT;
5791
5792 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5793 expansion_notify);
5794 }
5795 return;
5796 }
5797
5798 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5799
5800 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5801 symbol_matcher,
5802 kind, [&] (offset_type namei)
5803 {
5804 /* The name was matched, now expand corresponding CUs that were
5805 marked. */
5806 dw2_debug_names_iterator iter (map, kind, namei);
5807
5808 struct dwarf2_per_cu_data *per_cu;
5809 while ((per_cu = iter.next ()) != NULL)
5810 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5811 expansion_notify);
5812 return true;
5813 });
5814 }
5815
5816 const struct quick_symbol_functions dwarf2_debug_names_functions =
5817 {
5818 dw2_has_symbols,
5819 dw2_find_last_source_symtab,
5820 dw2_forget_cached_source_info,
5821 dw2_map_symtabs_matching_filename,
5822 dw2_debug_names_lookup_symbol,
5823 NULL,
5824 dw2_print_stats,
5825 dw2_debug_names_dump,
5826 dw2_debug_names_expand_symtabs_for_function,
5827 dw2_expand_all_symtabs,
5828 dw2_expand_symtabs_with_fullname,
5829 dw2_debug_names_map_matching_symbols,
5830 dw2_debug_names_expand_symtabs_matching,
5831 dw2_find_pc_sect_compunit_symtab,
5832 NULL,
5833 dw2_map_symbol_filenames
5834 };
5835
5836 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5837 to either a dwarf2_per_bfd or dwz_file object. */
5838
5839 template <typename T>
5840 static gdb::array_view<const gdb_byte>
5841 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5842 {
5843 dwarf2_section_info *section = &section_owner->gdb_index;
5844
5845 if (section->empty ())
5846 return {};
5847
5848 /* Older elfutils strip versions could keep the section in the main
5849 executable while splitting it for the separate debug info file. */
5850 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5851 return {};
5852
5853 section->read (obj);
5854
5855 /* dwarf2_section_info::size is a bfd_size_type, while
5856 gdb::array_view works with size_t. On 32-bit hosts, with
5857 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5858 is 32-bit. So we need an explicit narrowing conversion here.
5859 This is fine, because it's impossible to allocate or mmap an
5860 array/buffer larger than what size_t can represent. */
5861 return gdb::make_array_view (section->buffer, section->size);
5862 }
5863
5864 /* Lookup the index cache for the contents of the index associated to
5865 DWARF2_OBJ. */
5866
5867 static gdb::array_view<const gdb_byte>
5868 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5869 {
5870 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5871 if (build_id == nullptr)
5872 return {};
5873
5874 return global_index_cache.lookup_gdb_index (build_id,
5875 &dwarf2_per_bfd->index_cache_res);
5876 }
5877
5878 /* Same as the above, but for DWZ. */
5879
5880 static gdb::array_view<const gdb_byte>
5881 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5882 {
5883 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5884 if (build_id == nullptr)
5885 return {};
5886
5887 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5888 }
5889
5890 /* See symfile.h. */
5891
5892 bool
5893 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5894 {
5895 struct dwarf2_per_objfile *dwarf2_per_objfile
5896 = get_dwarf2_per_objfile (objfile);
5897
5898 /* If we're about to read full symbols, don't bother with the
5899 indices. In this case we also don't care if some other debug
5900 format is making psymtabs, because they are all about to be
5901 expanded anyway. */
5902 if ((objfile->flags & OBJF_READNOW))
5903 {
5904 dwarf2_per_objfile->per_bfd->using_index = 1;
5905 create_all_comp_units (dwarf2_per_objfile);
5906 create_all_type_units (dwarf2_per_objfile);
5907 dwarf2_per_objfile->per_bfd->quick_file_names_table
5908 = create_quick_file_names_table
5909 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5910 dwarf2_per_objfile->resize_symtabs ();
5911
5912 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5913 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5914 {
5915 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5916
5917 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5918 struct dwarf2_per_cu_quick_data);
5919 }
5920
5921 /* Return 1 so that gdb sees the "quick" functions. However,
5922 these functions will be no-ops because we will have expanded
5923 all symtabs. */
5924 *index_kind = dw_index_kind::GDB_INDEX;
5925 return true;
5926 }
5927
5928 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5929 {
5930 *index_kind = dw_index_kind::DEBUG_NAMES;
5931 dwarf2_per_objfile->resize_symtabs ();
5932 return true;
5933 }
5934
5935 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5936 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5937 get_gdb_index_contents_from_section<dwz_file>))
5938 {
5939 *index_kind = dw_index_kind::GDB_INDEX;
5940 dwarf2_per_objfile->resize_symtabs ();
5941 return true;
5942 }
5943
5944 /* ... otherwise, try to find the index in the index cache. */
5945 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5946 get_gdb_index_contents_from_cache,
5947 get_gdb_index_contents_from_cache_dwz))
5948 {
5949 global_index_cache.hit ();
5950 *index_kind = dw_index_kind::GDB_INDEX;
5951 dwarf2_per_objfile->resize_symtabs ();
5952 return true;
5953 }
5954
5955 global_index_cache.miss ();
5956 return false;
5957 }
5958
5959 \f
5960
5961 /* Build a partial symbol table. */
5962
5963 void
5964 dwarf2_build_psymtabs (struct objfile *objfile)
5965 {
5966 struct dwarf2_per_objfile *dwarf2_per_objfile
5967 = get_dwarf2_per_objfile (objfile);
5968
5969 init_psymbol_list (objfile, 1024);
5970
5971 try
5972 {
5973 /* This isn't really ideal: all the data we allocate on the
5974 objfile's obstack is still uselessly kept around. However,
5975 freeing it seems unsafe. */
5976 psymtab_discarder psymtabs (objfile);
5977 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5978 psymtabs.keep ();
5979
5980 dwarf2_per_objfile->resize_symtabs ();
5981
5982 /* (maybe) store an index in the cache. */
5983 global_index_cache.store (dwarf2_per_objfile);
5984 }
5985 catch (const gdb_exception_error &except)
5986 {
5987 exception_print (gdb_stderr, except);
5988 }
5989 }
5990
5991 /* Find the base address of the compilation unit for range lists and
5992 location lists. It will normally be specified by DW_AT_low_pc.
5993 In DWARF-3 draft 4, the base address could be overridden by
5994 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5995 compilation units with discontinuous ranges. */
5996
5997 static void
5998 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5999 {
6000 struct attribute *attr;
6001
6002 cu->base_address.reset ();
6003
6004 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6005 if (attr != nullptr)
6006 cu->base_address = attr->value_as_address ();
6007 else
6008 {
6009 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6010 if (attr != nullptr)
6011 cu->base_address = attr->value_as_address ();
6012 }
6013 }
6014
6015 /* Helper function that returns the proper abbrev section for
6016 THIS_CU. */
6017
6018 static struct dwarf2_section_info *
6019 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6020 {
6021 struct dwarf2_section_info *abbrev;
6022 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6023
6024 if (this_cu->is_dwz)
6025 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6026 else
6027 abbrev = &dwarf2_per_objfile->per_bfd->abbrev;
6028
6029 return abbrev;
6030 }
6031
6032 /* Fetch the abbreviation table offset from a comp or type unit header. */
6033
6034 static sect_offset
6035 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6036 struct dwarf2_section_info *section,
6037 sect_offset sect_off)
6038 {
6039 bfd *abfd = section->get_bfd_owner ();
6040 const gdb_byte *info_ptr;
6041 unsigned int initial_length_size, offset_size;
6042 uint16_t version;
6043
6044 section->read (dwarf2_per_objfile->objfile);
6045 info_ptr = section->buffer + to_underlying (sect_off);
6046 read_initial_length (abfd, info_ptr, &initial_length_size);
6047 offset_size = initial_length_size == 4 ? 4 : 8;
6048 info_ptr += initial_length_size;
6049
6050 version = read_2_bytes (abfd, info_ptr);
6051 info_ptr += 2;
6052 if (version >= 5)
6053 {
6054 /* Skip unit type and address size. */
6055 info_ptr += 2;
6056 }
6057
6058 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6059 }
6060
6061 /* A partial symtab that is used only for include files. */
6062 struct dwarf2_include_psymtab : public partial_symtab
6063 {
6064 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6065 : partial_symtab (filename, objfile)
6066 {
6067 }
6068
6069 void read_symtab (struct objfile *objfile) override
6070 {
6071 /* It's an include file, no symbols to read for it.
6072 Everything is in the includer symtab. */
6073
6074 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6075 expansion of the includer psymtab. We use the dependencies[0] field to
6076 model the includer. But if we go the regular route of calling
6077 expand_psymtab here, and having expand_psymtab call expand_dependencies
6078 to expand the includer, we'll only use expand_psymtab on the includer
6079 (making it a non-toplevel psymtab), while if we expand the includer via
6080 another path, we'll use read_symtab (making it a toplevel psymtab).
6081 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6082 psymtab, and trigger read_symtab on the includer here directly. */
6083 includer ()->read_symtab (objfile);
6084 }
6085
6086 void expand_psymtab (struct objfile *objfile) override
6087 {
6088 /* This is not called by read_symtab, and should not be called by any
6089 expand_dependencies. */
6090 gdb_assert (false);
6091 }
6092
6093 bool readin_p (struct objfile *objfile) const override
6094 {
6095 return includer ()->readin_p (objfile);
6096 }
6097
6098 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6099 {
6100 return nullptr;
6101 }
6102
6103 private:
6104 partial_symtab *includer () const
6105 {
6106 /* An include psymtab has exactly one dependency: the psymtab that
6107 includes it. */
6108 gdb_assert (this->number_of_dependencies == 1);
6109 return this->dependencies[0];
6110 }
6111 };
6112
6113 /* Allocate a new partial symtab for file named NAME and mark this new
6114 partial symtab as being an include of PST. */
6115
6116 static void
6117 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6118 struct objfile *objfile)
6119 {
6120 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6121
6122 if (!IS_ABSOLUTE_PATH (subpst->filename))
6123 subpst->dirname = pst->dirname;
6124
6125 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6126 subpst->dependencies[0] = pst;
6127 subpst->number_of_dependencies = 1;
6128 }
6129
6130 /* Read the Line Number Program data and extract the list of files
6131 included by the source file represented by PST. Build an include
6132 partial symtab for each of these included files. */
6133
6134 static void
6135 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6136 struct die_info *die,
6137 dwarf2_psymtab *pst)
6138 {
6139 line_header_up lh;
6140 struct attribute *attr;
6141
6142 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6143 if (attr != nullptr)
6144 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6145 if (lh == NULL)
6146 return; /* No linetable, so no includes. */
6147
6148 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6149 that we pass in the raw text_low here; that is ok because we're
6150 only decoding the line table to make include partial symtabs, and
6151 so the addresses aren't really used. */
6152 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6153 pst->raw_text_low (), 1);
6154 }
6155
6156 static hashval_t
6157 hash_signatured_type (const void *item)
6158 {
6159 const struct signatured_type *sig_type
6160 = (const struct signatured_type *) item;
6161
6162 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6163 return sig_type->signature;
6164 }
6165
6166 static int
6167 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6168 {
6169 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6170 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6171
6172 return lhs->signature == rhs->signature;
6173 }
6174
6175 /* Allocate a hash table for signatured types. */
6176
6177 static htab_up
6178 allocate_signatured_type_table ()
6179 {
6180 return htab_up (htab_create_alloc (41,
6181 hash_signatured_type,
6182 eq_signatured_type,
6183 NULL, xcalloc, xfree));
6184 }
6185
6186 /* A helper function to add a signatured type CU to a table. */
6187
6188 static int
6189 add_signatured_type_cu_to_table (void **slot, void *datum)
6190 {
6191 struct signatured_type *sigt = (struct signatured_type *) *slot;
6192 std::vector<signatured_type *> *all_type_units
6193 = (std::vector<signatured_type *> *) datum;
6194
6195 all_type_units->push_back (sigt);
6196
6197 return 1;
6198 }
6199
6200 /* A helper for create_debug_types_hash_table. Read types from SECTION
6201 and fill them into TYPES_HTAB. It will process only type units,
6202 therefore DW_UT_type. */
6203
6204 static void
6205 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6206 struct dwo_file *dwo_file,
6207 dwarf2_section_info *section, htab_up &types_htab,
6208 rcuh_kind section_kind)
6209 {
6210 struct objfile *objfile = dwarf2_per_objfile->objfile;
6211 struct dwarf2_section_info *abbrev_section;
6212 bfd *abfd;
6213 const gdb_byte *info_ptr, *end_ptr;
6214
6215 abbrev_section = (dwo_file != NULL
6216 ? &dwo_file->sections.abbrev
6217 : &dwarf2_per_objfile->per_bfd->abbrev);
6218
6219 if (dwarf_read_debug)
6220 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6221 section->get_name (),
6222 abbrev_section->get_file_name ());
6223
6224 section->read (objfile);
6225 info_ptr = section->buffer;
6226
6227 if (info_ptr == NULL)
6228 return;
6229
6230 /* We can't set abfd until now because the section may be empty or
6231 not present, in which case the bfd is unknown. */
6232 abfd = section->get_bfd_owner ();
6233
6234 /* We don't use cutu_reader here because we don't need to read
6235 any dies: the signature is in the header. */
6236
6237 end_ptr = info_ptr + section->size;
6238 while (info_ptr < end_ptr)
6239 {
6240 struct signatured_type *sig_type;
6241 struct dwo_unit *dwo_tu;
6242 void **slot;
6243 const gdb_byte *ptr = info_ptr;
6244 struct comp_unit_head header;
6245 unsigned int length;
6246
6247 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6248
6249 /* Initialize it due to a false compiler warning. */
6250 header.signature = -1;
6251 header.type_cu_offset_in_tu = (cu_offset) -1;
6252
6253 /* We need to read the type's signature in order to build the hash
6254 table, but we don't need anything else just yet. */
6255
6256 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6257 abbrev_section, ptr, section_kind);
6258
6259 length = header.get_length ();
6260
6261 /* Skip dummy type units. */
6262 if (ptr >= info_ptr + length
6263 || peek_abbrev_code (abfd, ptr) == 0
6264 || header.unit_type != DW_UT_type)
6265 {
6266 info_ptr += length;
6267 continue;
6268 }
6269
6270 if (types_htab == NULL)
6271 {
6272 if (dwo_file)
6273 types_htab = allocate_dwo_unit_table ();
6274 else
6275 types_htab = allocate_signatured_type_table ();
6276 }
6277
6278 if (dwo_file)
6279 {
6280 sig_type = NULL;
6281 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6282 struct dwo_unit);
6283 dwo_tu->dwo_file = dwo_file;
6284 dwo_tu->signature = header.signature;
6285 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6286 dwo_tu->section = section;
6287 dwo_tu->sect_off = sect_off;
6288 dwo_tu->length = length;
6289 }
6290 else
6291 {
6292 /* N.B.: type_offset is not usable if this type uses a DWO file.
6293 The real type_offset is in the DWO file. */
6294 dwo_tu = NULL;
6295 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6296 sig_type->signature = header.signature;
6297 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6298 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6299 sig_type->per_cu.is_debug_types = 1;
6300 sig_type->per_cu.section = section;
6301 sig_type->per_cu.sect_off = sect_off;
6302 sig_type->per_cu.length = length;
6303 }
6304
6305 slot = htab_find_slot (types_htab.get (),
6306 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6307 INSERT);
6308 gdb_assert (slot != NULL);
6309 if (*slot != NULL)
6310 {
6311 sect_offset dup_sect_off;
6312
6313 if (dwo_file)
6314 {
6315 const struct dwo_unit *dup_tu
6316 = (const struct dwo_unit *) *slot;
6317
6318 dup_sect_off = dup_tu->sect_off;
6319 }
6320 else
6321 {
6322 const struct signatured_type *dup_tu
6323 = (const struct signatured_type *) *slot;
6324
6325 dup_sect_off = dup_tu->per_cu.sect_off;
6326 }
6327
6328 complaint (_("debug type entry at offset %s is duplicate to"
6329 " the entry at offset %s, signature %s"),
6330 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6331 hex_string (header.signature));
6332 }
6333 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6334
6335 if (dwarf_read_debug > 1)
6336 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6337 sect_offset_str (sect_off),
6338 hex_string (header.signature));
6339
6340 info_ptr += length;
6341 }
6342 }
6343
6344 /* Create the hash table of all entries in the .debug_types
6345 (or .debug_types.dwo) section(s).
6346 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6347 otherwise it is NULL.
6348
6349 The result is a pointer to the hash table or NULL if there are no types.
6350
6351 Note: This function processes DWO files only, not DWP files. */
6352
6353 static void
6354 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6355 struct dwo_file *dwo_file,
6356 gdb::array_view<dwarf2_section_info> type_sections,
6357 htab_up &types_htab)
6358 {
6359 for (dwarf2_section_info &section : type_sections)
6360 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6361 types_htab, rcuh_kind::TYPE);
6362 }
6363
6364 /* Create the hash table of all entries in the .debug_types section,
6365 and initialize all_type_units.
6366 The result is zero if there is an error (e.g. missing .debug_types section),
6367 otherwise non-zero. */
6368
6369 static int
6370 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6371 {
6372 htab_up types_htab;
6373
6374 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6375 &dwarf2_per_objfile->per_bfd->info, types_htab,
6376 rcuh_kind::COMPILE);
6377 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6378 dwarf2_per_objfile->per_bfd->types, types_htab);
6379 if (types_htab == NULL)
6380 {
6381 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6382 return 0;
6383 }
6384
6385 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6386
6387 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6388 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6389 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6390
6391 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6392 add_signatured_type_cu_to_table,
6393 &dwarf2_per_objfile->per_bfd->all_type_units);
6394
6395 return 1;
6396 }
6397
6398 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6399 If SLOT is non-NULL, it is the entry to use in the hash table.
6400 Otherwise we find one. */
6401
6402 static struct signatured_type *
6403 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6404 void **slot)
6405 {
6406 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6407 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6408 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6409
6410 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6411
6412 dwarf2_per_objfile->resize_symtabs ();
6413
6414 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6415 sig_type->signature = sig;
6416 sig_type->per_cu.is_debug_types = 1;
6417 if (dwarf2_per_objfile->per_bfd->using_index)
6418 {
6419 sig_type->per_cu.v.quick =
6420 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6421 struct dwarf2_per_cu_quick_data);
6422 }
6423
6424 if (slot == NULL)
6425 {
6426 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6427 sig_type, INSERT);
6428 }
6429 gdb_assert (*slot == NULL);
6430 *slot = sig_type;
6431 /* The rest of sig_type must be filled in by the caller. */
6432 return sig_type;
6433 }
6434
6435 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6436 Fill in SIG_ENTRY with DWO_ENTRY. */
6437
6438 static void
6439 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6440 struct signatured_type *sig_entry,
6441 struct dwo_unit *dwo_entry)
6442 {
6443 /* Make sure we're not clobbering something we don't expect to. */
6444 gdb_assert (! sig_entry->per_cu.queued);
6445 gdb_assert (sig_entry->per_cu.cu == NULL);
6446 if (dwarf2_per_objfile->per_bfd->using_index)
6447 {
6448 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6449 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6450 }
6451 else
6452 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6453 gdb_assert (sig_entry->signature == dwo_entry->signature);
6454 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6455 gdb_assert (sig_entry->type_unit_group == NULL);
6456 gdb_assert (sig_entry->dwo_unit == NULL);
6457
6458 sig_entry->per_cu.section = dwo_entry->section;
6459 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6460 sig_entry->per_cu.length = dwo_entry->length;
6461 sig_entry->per_cu.reading_dwo_directly = 1;
6462 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6463 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6464 sig_entry->dwo_unit = dwo_entry;
6465 }
6466
6467 /* Subroutine of lookup_signatured_type.
6468 If we haven't read the TU yet, create the signatured_type data structure
6469 for a TU to be read in directly from a DWO file, bypassing the stub.
6470 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6471 using .gdb_index, then when reading a CU we want to stay in the DWO file
6472 containing that CU. Otherwise we could end up reading several other DWO
6473 files (due to comdat folding) to process the transitive closure of all the
6474 mentioned TUs, and that can be slow. The current DWO file will have every
6475 type signature that it needs.
6476 We only do this for .gdb_index because in the psymtab case we already have
6477 to read all the DWOs to build the type unit groups. */
6478
6479 static struct signatured_type *
6480 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6481 {
6482 struct dwarf2_per_objfile *dwarf2_per_objfile
6483 = cu->per_cu->dwarf2_per_objfile;
6484 struct dwo_file *dwo_file;
6485 struct dwo_unit find_dwo_entry, *dwo_entry;
6486 struct signatured_type find_sig_entry, *sig_entry;
6487 void **slot;
6488
6489 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6490
6491 /* If TU skeletons have been removed then we may not have read in any
6492 TUs yet. */
6493 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6494 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6495
6496 /* We only ever need to read in one copy of a signatured type.
6497 Use the global signatured_types array to do our own comdat-folding
6498 of types. If this is the first time we're reading this TU, and
6499 the TU has an entry in .gdb_index, replace the recorded data from
6500 .gdb_index with this TU. */
6501
6502 find_sig_entry.signature = sig;
6503 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6504 &find_sig_entry, INSERT);
6505 sig_entry = (struct signatured_type *) *slot;
6506
6507 /* We can get here with the TU already read, *or* in the process of being
6508 read. Don't reassign the global entry to point to this DWO if that's
6509 the case. Also note that if the TU is already being read, it may not
6510 have come from a DWO, the program may be a mix of Fission-compiled
6511 code and non-Fission-compiled code. */
6512
6513 /* Have we already tried to read this TU?
6514 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6515 needn't exist in the global table yet). */
6516 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6517 return sig_entry;
6518
6519 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6520 dwo_unit of the TU itself. */
6521 dwo_file = cu->dwo_unit->dwo_file;
6522
6523 /* Ok, this is the first time we're reading this TU. */
6524 if (dwo_file->tus == NULL)
6525 return NULL;
6526 find_dwo_entry.signature = sig;
6527 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6528 &find_dwo_entry);
6529 if (dwo_entry == NULL)
6530 return NULL;
6531
6532 /* If the global table doesn't have an entry for this TU, add one. */
6533 if (sig_entry == NULL)
6534 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6535
6536 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6537 sig_entry->per_cu.tu_read = 1;
6538 return sig_entry;
6539 }
6540
6541 /* Subroutine of lookup_signatured_type.
6542 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6543 then try the DWP file. If the TU stub (skeleton) has been removed then
6544 it won't be in .gdb_index. */
6545
6546 static struct signatured_type *
6547 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6548 {
6549 struct dwarf2_per_objfile *dwarf2_per_objfile
6550 = cu->per_cu->dwarf2_per_objfile;
6551 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6552 struct dwo_unit *dwo_entry;
6553 struct signatured_type find_sig_entry, *sig_entry;
6554 void **slot;
6555
6556 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6557 gdb_assert (dwp_file != NULL);
6558
6559 /* If TU skeletons have been removed then we may not have read in any
6560 TUs yet. */
6561 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6562 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6563
6564 find_sig_entry.signature = sig;
6565 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6566 &find_sig_entry, INSERT);
6567 sig_entry = (struct signatured_type *) *slot;
6568
6569 /* Have we already tried to read this TU?
6570 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6571 needn't exist in the global table yet). */
6572 if (sig_entry != NULL)
6573 return sig_entry;
6574
6575 if (dwp_file->tus == NULL)
6576 return NULL;
6577 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6578 sig, 1 /* is_debug_types */);
6579 if (dwo_entry == NULL)
6580 return NULL;
6581
6582 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6583 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6584
6585 return sig_entry;
6586 }
6587
6588 /* Lookup a signature based type for DW_FORM_ref_sig8.
6589 Returns NULL if signature SIG is not present in the table.
6590 It is up to the caller to complain about this. */
6591
6592 static struct signatured_type *
6593 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6594 {
6595 struct dwarf2_per_objfile *dwarf2_per_objfile
6596 = cu->per_cu->dwarf2_per_objfile;
6597
6598 if (cu->dwo_unit
6599 && dwarf2_per_objfile->per_bfd->using_index)
6600 {
6601 /* We're in a DWO/DWP file, and we're using .gdb_index.
6602 These cases require special processing. */
6603 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6604 return lookup_dwo_signatured_type (cu, sig);
6605 else
6606 return lookup_dwp_signatured_type (cu, sig);
6607 }
6608 else
6609 {
6610 struct signatured_type find_entry, *entry;
6611
6612 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6613 return NULL;
6614 find_entry.signature = sig;
6615 entry = ((struct signatured_type *)
6616 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6617 &find_entry));
6618 return entry;
6619 }
6620 }
6621
6622 /* Low level DIE reading support. */
6623
6624 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6625
6626 static void
6627 init_cu_die_reader (struct die_reader_specs *reader,
6628 struct dwarf2_cu *cu,
6629 struct dwarf2_section_info *section,
6630 struct dwo_file *dwo_file,
6631 struct abbrev_table *abbrev_table)
6632 {
6633 gdb_assert (section->readin && section->buffer != NULL);
6634 reader->abfd = section->get_bfd_owner ();
6635 reader->cu = cu;
6636 reader->dwo_file = dwo_file;
6637 reader->die_section = section;
6638 reader->buffer = section->buffer;
6639 reader->buffer_end = section->buffer + section->size;
6640 reader->abbrev_table = abbrev_table;
6641 }
6642
6643 /* Subroutine of cutu_reader to simplify it.
6644 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6645 There's just a lot of work to do, and cutu_reader is big enough
6646 already.
6647
6648 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6649 from it to the DIE in the DWO. If NULL we are skipping the stub.
6650 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6651 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6652 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6653 STUB_COMP_DIR may be non-NULL.
6654 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6655 are filled in with the info of the DIE from the DWO file.
6656 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6657 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6658 kept around for at least as long as *RESULT_READER.
6659
6660 The result is non-zero if a valid (non-dummy) DIE was found. */
6661
6662 static int
6663 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6664 struct dwo_unit *dwo_unit,
6665 struct die_info *stub_comp_unit_die,
6666 const char *stub_comp_dir,
6667 struct die_reader_specs *result_reader,
6668 const gdb_byte **result_info_ptr,
6669 struct die_info **result_comp_unit_die,
6670 abbrev_table_up *result_dwo_abbrev_table)
6671 {
6672 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6673 struct objfile *objfile = dwarf2_per_objfile->objfile;
6674 struct dwarf2_cu *cu = this_cu->cu;
6675 bfd *abfd;
6676 const gdb_byte *begin_info_ptr, *info_ptr;
6677 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6678 int i,num_extra_attrs;
6679 struct dwarf2_section_info *dwo_abbrev_section;
6680 struct die_info *comp_unit_die;
6681
6682 /* At most one of these may be provided. */
6683 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6684
6685 /* These attributes aren't processed until later:
6686 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6687 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6688 referenced later. However, these attributes are found in the stub
6689 which we won't have later. In order to not impose this complication
6690 on the rest of the code, we read them here and copy them to the
6691 DWO CU/TU die. */
6692
6693 stmt_list = NULL;
6694 low_pc = NULL;
6695 high_pc = NULL;
6696 ranges = NULL;
6697 comp_dir = NULL;
6698
6699 if (stub_comp_unit_die != NULL)
6700 {
6701 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6702 DWO file. */
6703 if (! this_cu->is_debug_types)
6704 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6705 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6706 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6707 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6708 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6709
6710 cu->addr_base = stub_comp_unit_die->addr_base ();
6711
6712 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6713 here (if needed). We need the value before we can process
6714 DW_AT_ranges. */
6715 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6716 }
6717 else if (stub_comp_dir != NULL)
6718 {
6719 /* Reconstruct the comp_dir attribute to simplify the code below. */
6720 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6721 comp_dir->name = DW_AT_comp_dir;
6722 comp_dir->form = DW_FORM_string;
6723 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6724 DW_STRING (comp_dir) = stub_comp_dir;
6725 }
6726
6727 /* Set up for reading the DWO CU/TU. */
6728 cu->dwo_unit = dwo_unit;
6729 dwarf2_section_info *section = dwo_unit->section;
6730 section->read (objfile);
6731 abfd = section->get_bfd_owner ();
6732 begin_info_ptr = info_ptr = (section->buffer
6733 + to_underlying (dwo_unit->sect_off));
6734 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6735
6736 if (this_cu->is_debug_types)
6737 {
6738 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6739
6740 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6741 &cu->header, section,
6742 dwo_abbrev_section,
6743 info_ptr, rcuh_kind::TYPE);
6744 /* This is not an assert because it can be caused by bad debug info. */
6745 if (sig_type->signature != cu->header.signature)
6746 {
6747 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6748 " TU at offset %s [in module %s]"),
6749 hex_string (sig_type->signature),
6750 hex_string (cu->header.signature),
6751 sect_offset_str (dwo_unit->sect_off),
6752 bfd_get_filename (abfd));
6753 }
6754 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6755 /* For DWOs coming from DWP files, we don't know the CU length
6756 nor the type's offset in the TU until now. */
6757 dwo_unit->length = cu->header.get_length ();
6758 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6759
6760 /* Establish the type offset that can be used to lookup the type.
6761 For DWO files, we don't know it until now. */
6762 sig_type->type_offset_in_section
6763 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6764 }
6765 else
6766 {
6767 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6768 &cu->header, section,
6769 dwo_abbrev_section,
6770 info_ptr, rcuh_kind::COMPILE);
6771 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6772 /* For DWOs coming from DWP files, we don't know the CU length
6773 until now. */
6774 dwo_unit->length = cu->header.get_length ();
6775 }
6776
6777 *result_dwo_abbrev_table
6778 = abbrev_table::read (objfile, dwo_abbrev_section,
6779 cu->header.abbrev_sect_off);
6780 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6781 result_dwo_abbrev_table->get ());
6782
6783 /* Read in the die, but leave space to copy over the attributes
6784 from the stub. This has the benefit of simplifying the rest of
6785 the code - all the work to maintain the illusion of a single
6786 DW_TAG_{compile,type}_unit DIE is done here. */
6787 num_extra_attrs = ((stmt_list != NULL)
6788 + (low_pc != NULL)
6789 + (high_pc != NULL)
6790 + (ranges != NULL)
6791 + (comp_dir != NULL));
6792 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6793 num_extra_attrs);
6794
6795 /* Copy over the attributes from the stub to the DIE we just read in. */
6796 comp_unit_die = *result_comp_unit_die;
6797 i = comp_unit_die->num_attrs;
6798 if (stmt_list != NULL)
6799 comp_unit_die->attrs[i++] = *stmt_list;
6800 if (low_pc != NULL)
6801 comp_unit_die->attrs[i++] = *low_pc;
6802 if (high_pc != NULL)
6803 comp_unit_die->attrs[i++] = *high_pc;
6804 if (ranges != NULL)
6805 comp_unit_die->attrs[i++] = *ranges;
6806 if (comp_dir != NULL)
6807 comp_unit_die->attrs[i++] = *comp_dir;
6808 comp_unit_die->num_attrs += num_extra_attrs;
6809
6810 if (dwarf_die_debug)
6811 {
6812 fprintf_unfiltered (gdb_stdlog,
6813 "Read die from %s@0x%x of %s:\n",
6814 section->get_name (),
6815 (unsigned) (begin_info_ptr - section->buffer),
6816 bfd_get_filename (abfd));
6817 dump_die (comp_unit_die, dwarf_die_debug);
6818 }
6819
6820 /* Skip dummy compilation units. */
6821 if (info_ptr >= begin_info_ptr + dwo_unit->length
6822 || peek_abbrev_code (abfd, info_ptr) == 0)
6823 return 0;
6824
6825 *result_info_ptr = info_ptr;
6826 return 1;
6827 }
6828
6829 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6830 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6831 signature is part of the header. */
6832 static gdb::optional<ULONGEST>
6833 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6834 {
6835 if (cu->header.version >= 5)
6836 return cu->header.signature;
6837 struct attribute *attr;
6838 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6839 if (attr == nullptr)
6840 return gdb::optional<ULONGEST> ();
6841 return DW_UNSND (attr);
6842 }
6843
6844 /* Subroutine of cutu_reader to simplify it.
6845 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6846 Returns NULL if the specified DWO unit cannot be found. */
6847
6848 static struct dwo_unit *
6849 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6850 struct die_info *comp_unit_die,
6851 const char *dwo_name)
6852 {
6853 struct dwarf2_cu *cu = this_cu->cu;
6854 struct dwo_unit *dwo_unit;
6855 const char *comp_dir;
6856
6857 gdb_assert (cu != NULL);
6858
6859 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6860 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6861 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6862
6863 if (this_cu->is_debug_types)
6864 {
6865 struct signatured_type *sig_type;
6866
6867 /* Since this_cu is the first member of struct signatured_type,
6868 we can go from a pointer to one to a pointer to the other. */
6869 sig_type = (struct signatured_type *) this_cu;
6870 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6871 }
6872 else
6873 {
6874 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6875 if (!signature.has_value ())
6876 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6877 " [in module %s]"),
6878 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6879 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6880 *signature);
6881 }
6882
6883 return dwo_unit;
6884 }
6885
6886 /* Subroutine of cutu_reader to simplify it.
6887 See it for a description of the parameters.
6888 Read a TU directly from a DWO file, bypassing the stub. */
6889
6890 void
6891 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6892 int use_existing_cu)
6893 {
6894 struct signatured_type *sig_type;
6895
6896 /* Verify we can do the following downcast, and that we have the
6897 data we need. */
6898 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6899 sig_type = (struct signatured_type *) this_cu;
6900 gdb_assert (sig_type->dwo_unit != NULL);
6901
6902 if (use_existing_cu && this_cu->cu != NULL)
6903 {
6904 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6905 /* There's no need to do the rereading_dwo_cu handling that
6906 cutu_reader does since we don't read the stub. */
6907 }
6908 else
6909 {
6910 /* If !use_existing_cu, this_cu->cu must be NULL. */
6911 gdb_assert (this_cu->cu == NULL);
6912 m_new_cu.reset (new dwarf2_cu (this_cu));
6913 }
6914
6915 /* A future optimization, if needed, would be to use an existing
6916 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6917 could share abbrev tables. */
6918
6919 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6920 NULL /* stub_comp_unit_die */,
6921 sig_type->dwo_unit->dwo_file->comp_dir,
6922 this, &info_ptr,
6923 &comp_unit_die,
6924 &m_dwo_abbrev_table) == 0)
6925 {
6926 /* Dummy die. */
6927 dummy_p = true;
6928 }
6929 }
6930
6931 /* Initialize a CU (or TU) and read its DIEs.
6932 If the CU defers to a DWO file, read the DWO file as well.
6933
6934 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6935 Otherwise the table specified in the comp unit header is read in and used.
6936 This is an optimization for when we already have the abbrev table.
6937
6938 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6939 Otherwise, a new CU is allocated with xmalloc. */
6940
6941 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6942 struct abbrev_table *abbrev_table,
6943 int use_existing_cu,
6944 bool skip_partial)
6945 : die_reader_specs {},
6946 m_this_cu (this_cu)
6947 {
6948 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6949 struct objfile *objfile = dwarf2_per_objfile->objfile;
6950 struct dwarf2_section_info *section = this_cu->section;
6951 bfd *abfd = section->get_bfd_owner ();
6952 struct dwarf2_cu *cu;
6953 const gdb_byte *begin_info_ptr;
6954 struct signatured_type *sig_type = NULL;
6955 struct dwarf2_section_info *abbrev_section;
6956 /* Non-zero if CU currently points to a DWO file and we need to
6957 reread it. When this happens we need to reread the skeleton die
6958 before we can reread the DWO file (this only applies to CUs, not TUs). */
6959 int rereading_dwo_cu = 0;
6960
6961 if (dwarf_die_debug)
6962 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6963 this_cu->is_debug_types ? "type" : "comp",
6964 sect_offset_str (this_cu->sect_off));
6965
6966 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6967 file (instead of going through the stub), short-circuit all of this. */
6968 if (this_cu->reading_dwo_directly)
6969 {
6970 /* Narrow down the scope of possibilities to have to understand. */
6971 gdb_assert (this_cu->is_debug_types);
6972 gdb_assert (abbrev_table == NULL);
6973 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6974 return;
6975 }
6976
6977 /* This is cheap if the section is already read in. */
6978 section->read (objfile);
6979
6980 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6981
6982 abbrev_section = get_abbrev_section_for_cu (this_cu);
6983
6984 if (use_existing_cu && this_cu->cu != NULL)
6985 {
6986 cu = this_cu->cu;
6987 /* If this CU is from a DWO file we need to start over, we need to
6988 refetch the attributes from the skeleton CU.
6989 This could be optimized by retrieving those attributes from when we
6990 were here the first time: the previous comp_unit_die was stored in
6991 comp_unit_obstack. But there's no data yet that we need this
6992 optimization. */
6993 if (cu->dwo_unit != NULL)
6994 rereading_dwo_cu = 1;
6995 }
6996 else
6997 {
6998 /* If !use_existing_cu, this_cu->cu must be NULL. */
6999 gdb_assert (this_cu->cu == NULL);
7000 m_new_cu.reset (new dwarf2_cu (this_cu));
7001 cu = m_new_cu.get ();
7002 }
7003
7004 /* Get the header. */
7005 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7006 {
7007 /* We already have the header, there's no need to read it in again. */
7008 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7009 }
7010 else
7011 {
7012 if (this_cu->is_debug_types)
7013 {
7014 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7015 &cu->header, section,
7016 abbrev_section, info_ptr,
7017 rcuh_kind::TYPE);
7018
7019 /* Since per_cu is the first member of struct signatured_type,
7020 we can go from a pointer to one to a pointer to the other. */
7021 sig_type = (struct signatured_type *) this_cu;
7022 gdb_assert (sig_type->signature == cu->header.signature);
7023 gdb_assert (sig_type->type_offset_in_tu
7024 == cu->header.type_cu_offset_in_tu);
7025 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7026
7027 /* LENGTH has not been set yet for type units if we're
7028 using .gdb_index. */
7029 this_cu->length = cu->header.get_length ();
7030
7031 /* Establish the type offset that can be used to lookup the type. */
7032 sig_type->type_offset_in_section =
7033 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7034
7035 this_cu->dwarf_version = cu->header.version;
7036 }
7037 else
7038 {
7039 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7040 &cu->header, section,
7041 abbrev_section,
7042 info_ptr,
7043 rcuh_kind::COMPILE);
7044
7045 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7046 if (this_cu->length == 0)
7047 this_cu->length = cu->header.get_length ();
7048 else
7049 gdb_assert (this_cu->length == cu->header.get_length ());
7050 this_cu->dwarf_version = cu->header.version;
7051 }
7052 }
7053
7054 /* Skip dummy compilation units. */
7055 if (info_ptr >= begin_info_ptr + this_cu->length
7056 || peek_abbrev_code (abfd, info_ptr) == 0)
7057 {
7058 dummy_p = true;
7059 return;
7060 }
7061
7062 /* If we don't have them yet, read the abbrevs for this compilation unit.
7063 And if we need to read them now, make sure they're freed when we're
7064 done. */
7065 if (abbrev_table != NULL)
7066 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7067 else
7068 {
7069 m_abbrev_table_holder
7070 = abbrev_table::read (objfile, abbrev_section,
7071 cu->header.abbrev_sect_off);
7072 abbrev_table = m_abbrev_table_holder.get ();
7073 }
7074
7075 /* Read the top level CU/TU die. */
7076 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7077 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7078
7079 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7080 {
7081 dummy_p = true;
7082 return;
7083 }
7084
7085 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7086 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7087 table from the DWO file and pass the ownership over to us. It will be
7088 referenced from READER, so we must make sure to free it after we're done
7089 with READER.
7090
7091 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7092 DWO CU, that this test will fail (the attribute will not be present). */
7093 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7094 if (dwo_name != nullptr)
7095 {
7096 struct dwo_unit *dwo_unit;
7097 struct die_info *dwo_comp_unit_die;
7098
7099 if (comp_unit_die->has_children)
7100 {
7101 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7102 " has children (offset %s) [in module %s]"),
7103 sect_offset_str (this_cu->sect_off),
7104 bfd_get_filename (abfd));
7105 }
7106 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7107 if (dwo_unit != NULL)
7108 {
7109 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7110 comp_unit_die, NULL,
7111 this, &info_ptr,
7112 &dwo_comp_unit_die,
7113 &m_dwo_abbrev_table) == 0)
7114 {
7115 /* Dummy die. */
7116 dummy_p = true;
7117 return;
7118 }
7119 comp_unit_die = dwo_comp_unit_die;
7120 }
7121 else
7122 {
7123 /* Yikes, we couldn't find the rest of the DIE, we only have
7124 the stub. A complaint has already been logged. There's
7125 not much more we can do except pass on the stub DIE to
7126 die_reader_func. We don't want to throw an error on bad
7127 debug info. */
7128 }
7129 }
7130 }
7131
7132 void
7133 cutu_reader::keep ()
7134 {
7135 /* Done, clean up. */
7136 gdb_assert (!dummy_p);
7137 if (m_new_cu != NULL)
7138 {
7139 struct dwarf2_per_objfile *dwarf2_per_objfile
7140 = m_this_cu->dwarf2_per_objfile;
7141 /* Link this CU into read_in_chain. */
7142 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7143 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7144 /* The chain owns it now. */
7145 m_new_cu.release ();
7146 }
7147 }
7148
7149 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7150 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7151 assumed to have already done the lookup to find the DWO file).
7152
7153 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7154 THIS_CU->is_debug_types, but nothing else.
7155
7156 We fill in THIS_CU->length.
7157
7158 THIS_CU->cu is always freed when done.
7159 This is done in order to not leave THIS_CU->cu in a state where we have
7160 to care whether it refers to the "main" CU or the DWO CU.
7161
7162 When parent_cu is passed, it is used to provide a default value for
7163 str_offsets_base and addr_base from the parent. */
7164
7165 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7166 struct dwarf2_cu *parent_cu,
7167 struct dwo_file *dwo_file)
7168 : die_reader_specs {},
7169 m_this_cu (this_cu)
7170 {
7171 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7172 struct objfile *objfile = dwarf2_per_objfile->objfile;
7173 struct dwarf2_section_info *section = this_cu->section;
7174 bfd *abfd = section->get_bfd_owner ();
7175 struct dwarf2_section_info *abbrev_section;
7176 const gdb_byte *begin_info_ptr, *info_ptr;
7177
7178 if (dwarf_die_debug)
7179 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7180 this_cu->is_debug_types ? "type" : "comp",
7181 sect_offset_str (this_cu->sect_off));
7182
7183 gdb_assert (this_cu->cu == NULL);
7184
7185 abbrev_section = (dwo_file != NULL
7186 ? &dwo_file->sections.abbrev
7187 : get_abbrev_section_for_cu (this_cu));
7188
7189 /* This is cheap if the section is already read in. */
7190 section->read (objfile);
7191
7192 m_new_cu.reset (new dwarf2_cu (this_cu));
7193
7194 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7195 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7196 &m_new_cu->header, section,
7197 abbrev_section, info_ptr,
7198 (this_cu->is_debug_types
7199 ? rcuh_kind::TYPE
7200 : rcuh_kind::COMPILE));
7201
7202 if (parent_cu != nullptr)
7203 {
7204 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7205 m_new_cu->addr_base = parent_cu->addr_base;
7206 }
7207 this_cu->length = m_new_cu->header.get_length ();
7208
7209 /* Skip dummy compilation units. */
7210 if (info_ptr >= begin_info_ptr + this_cu->length
7211 || peek_abbrev_code (abfd, info_ptr) == 0)
7212 {
7213 dummy_p = true;
7214 return;
7215 }
7216
7217 m_abbrev_table_holder
7218 = abbrev_table::read (objfile, abbrev_section,
7219 m_new_cu->header.abbrev_sect_off);
7220
7221 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7222 m_abbrev_table_holder.get ());
7223 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7224 }
7225
7226 \f
7227 /* Type Unit Groups.
7228
7229 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7230 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7231 so that all types coming from the same compilation (.o file) are grouped
7232 together. A future step could be to put the types in the same symtab as
7233 the CU the types ultimately came from. */
7234
7235 static hashval_t
7236 hash_type_unit_group (const void *item)
7237 {
7238 const struct type_unit_group *tu_group
7239 = (const struct type_unit_group *) item;
7240
7241 return hash_stmt_list_entry (&tu_group->hash);
7242 }
7243
7244 static int
7245 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7246 {
7247 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7248 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7249
7250 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7251 }
7252
7253 /* Allocate a hash table for type unit groups. */
7254
7255 static htab_up
7256 allocate_type_unit_groups_table ()
7257 {
7258 return htab_up (htab_create_alloc (3,
7259 hash_type_unit_group,
7260 eq_type_unit_group,
7261 NULL, xcalloc, xfree));
7262 }
7263
7264 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7265 partial symtabs. We combine several TUs per psymtab to not let the size
7266 of any one psymtab grow too big. */
7267 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7268 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7269
7270 /* Helper routine for get_type_unit_group.
7271 Create the type_unit_group object used to hold one or more TUs. */
7272
7273 static struct type_unit_group *
7274 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7275 {
7276 struct dwarf2_per_objfile *dwarf2_per_objfile
7277 = cu->per_cu->dwarf2_per_objfile;
7278 struct dwarf2_per_cu_data *per_cu;
7279 struct type_unit_group *tu_group;
7280
7281 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7282 struct type_unit_group);
7283 per_cu = &tu_group->per_cu;
7284 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7285
7286 if (dwarf2_per_objfile->per_bfd->using_index)
7287 {
7288 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7289 struct dwarf2_per_cu_quick_data);
7290 }
7291 else
7292 {
7293 unsigned int line_offset = to_underlying (line_offset_struct);
7294 dwarf2_psymtab *pst;
7295 std::string name;
7296
7297 /* Give the symtab a useful name for debug purposes. */
7298 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7299 name = string_printf ("<type_units_%d>",
7300 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7301 else
7302 name = string_printf ("<type_units_at_0x%x>", line_offset);
7303
7304 pst = create_partial_symtab (per_cu, name.c_str ());
7305 pst->anonymous = true;
7306 }
7307
7308 tu_group->hash.dwo_unit = cu->dwo_unit;
7309 tu_group->hash.line_sect_off = line_offset_struct;
7310
7311 return tu_group;
7312 }
7313
7314 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7315 STMT_LIST is a DW_AT_stmt_list attribute. */
7316
7317 static struct type_unit_group *
7318 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7319 {
7320 struct dwarf2_per_objfile *dwarf2_per_objfile
7321 = cu->per_cu->dwarf2_per_objfile;
7322 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7323 struct type_unit_group *tu_group;
7324 void **slot;
7325 unsigned int line_offset;
7326 struct type_unit_group type_unit_group_for_lookup;
7327
7328 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7329 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7330
7331 /* Do we need to create a new group, or can we use an existing one? */
7332
7333 if (stmt_list)
7334 {
7335 line_offset = DW_UNSND (stmt_list);
7336 ++tu_stats->nr_symtab_sharers;
7337 }
7338 else
7339 {
7340 /* Ugh, no stmt_list. Rare, but we have to handle it.
7341 We can do various things here like create one group per TU or
7342 spread them over multiple groups to split up the expansion work.
7343 To avoid worst case scenarios (too many groups or too large groups)
7344 we, umm, group them in bunches. */
7345 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7346 | (tu_stats->nr_stmt_less_type_units
7347 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7348 ++tu_stats->nr_stmt_less_type_units;
7349 }
7350
7351 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7352 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7353 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7354 &type_unit_group_for_lookup, INSERT);
7355 if (*slot != NULL)
7356 {
7357 tu_group = (struct type_unit_group *) *slot;
7358 gdb_assert (tu_group != NULL);
7359 }
7360 else
7361 {
7362 sect_offset line_offset_struct = (sect_offset) line_offset;
7363 tu_group = create_type_unit_group (cu, line_offset_struct);
7364 *slot = tu_group;
7365 ++tu_stats->nr_symtabs;
7366 }
7367
7368 return tu_group;
7369 }
7370 \f
7371 /* Partial symbol tables. */
7372
7373 /* Create a psymtab named NAME and assign it to PER_CU.
7374
7375 The caller must fill in the following details:
7376 dirname, textlow, texthigh. */
7377
7378 static dwarf2_psymtab *
7379 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7380 {
7381 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7382 dwarf2_psymtab *pst;
7383
7384 pst = new dwarf2_psymtab (name, objfile, per_cu);
7385
7386 pst->psymtabs_addrmap_supported = true;
7387
7388 /* This is the glue that links PST into GDB's symbol API. */
7389 per_cu->v.psymtab = pst;
7390
7391 return pst;
7392 }
7393
7394 /* DIE reader function for process_psymtab_comp_unit. */
7395
7396 static void
7397 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7398 const gdb_byte *info_ptr,
7399 struct die_info *comp_unit_die,
7400 enum language pretend_language)
7401 {
7402 struct dwarf2_cu *cu = reader->cu;
7403 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7404 struct gdbarch *gdbarch = objfile->arch ();
7405 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7406 CORE_ADDR baseaddr;
7407 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7408 dwarf2_psymtab *pst;
7409 enum pc_bounds_kind cu_bounds_kind;
7410 const char *filename;
7411
7412 gdb_assert (! per_cu->is_debug_types);
7413
7414 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7415
7416 /* Allocate a new partial symbol table structure. */
7417 gdb::unique_xmalloc_ptr<char> debug_filename;
7418 static const char artificial[] = "<artificial>";
7419 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7420 if (filename == NULL)
7421 filename = "";
7422 else if (strcmp (filename, artificial) == 0)
7423 {
7424 debug_filename.reset (concat (artificial, "@",
7425 sect_offset_str (per_cu->sect_off),
7426 (char *) NULL));
7427 filename = debug_filename.get ();
7428 }
7429
7430 pst = create_partial_symtab (per_cu, filename);
7431
7432 /* This must be done before calling dwarf2_build_include_psymtabs. */
7433 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7434
7435 baseaddr = objfile->text_section_offset ();
7436
7437 dwarf2_find_base_address (comp_unit_die, cu);
7438
7439 /* Possibly set the default values of LOWPC and HIGHPC from
7440 `DW_AT_ranges'. */
7441 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7442 &best_highpc, cu, pst);
7443 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7444 {
7445 CORE_ADDR low
7446 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7447 - baseaddr);
7448 CORE_ADDR high
7449 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7450 - baseaddr - 1);
7451 /* Store the contiguous range if it is not empty; it can be
7452 empty for CUs with no code. */
7453 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7454 low, high, pst);
7455 }
7456
7457 /* Check if comp unit has_children.
7458 If so, read the rest of the partial symbols from this comp unit.
7459 If not, there's no more debug_info for this comp unit. */
7460 if (comp_unit_die->has_children)
7461 {
7462 struct partial_die_info *first_die;
7463 CORE_ADDR lowpc, highpc;
7464
7465 lowpc = ((CORE_ADDR) -1);
7466 highpc = ((CORE_ADDR) 0);
7467
7468 first_die = load_partial_dies (reader, info_ptr, 1);
7469
7470 scan_partial_symbols (first_die, &lowpc, &highpc,
7471 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7472
7473 /* If we didn't find a lowpc, set it to highpc to avoid
7474 complaints from `maint check'. */
7475 if (lowpc == ((CORE_ADDR) -1))
7476 lowpc = highpc;
7477
7478 /* If the compilation unit didn't have an explicit address range,
7479 then use the information extracted from its child dies. */
7480 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7481 {
7482 best_lowpc = lowpc;
7483 best_highpc = highpc;
7484 }
7485 }
7486 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7487 best_lowpc + baseaddr)
7488 - baseaddr);
7489 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7490 best_highpc + baseaddr)
7491 - baseaddr);
7492
7493 end_psymtab_common (objfile, pst);
7494
7495 if (!cu->per_cu->imported_symtabs_empty ())
7496 {
7497 int i;
7498 int len = cu->per_cu->imported_symtabs_size ();
7499
7500 /* Fill in 'dependencies' here; we fill in 'users' in a
7501 post-pass. */
7502 pst->number_of_dependencies = len;
7503 pst->dependencies
7504 = objfile->partial_symtabs->allocate_dependencies (len);
7505 for (i = 0; i < len; ++i)
7506 {
7507 pst->dependencies[i]
7508 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7509 }
7510
7511 cu->per_cu->imported_symtabs_free ();
7512 }
7513
7514 /* Get the list of files included in the current compilation unit,
7515 and build a psymtab for each of them. */
7516 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7517
7518 if (dwarf_read_debug)
7519 fprintf_unfiltered (gdb_stdlog,
7520 "Psymtab for %s unit @%s: %s - %s"
7521 ", %d global, %d static syms\n",
7522 per_cu->is_debug_types ? "type" : "comp",
7523 sect_offset_str (per_cu->sect_off),
7524 paddress (gdbarch, pst->text_low (objfile)),
7525 paddress (gdbarch, pst->text_high (objfile)),
7526 pst->n_global_syms, pst->n_static_syms);
7527 }
7528
7529 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7530 Process compilation unit THIS_CU for a psymtab. */
7531
7532 static void
7533 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7534 bool want_partial_unit,
7535 enum language pretend_language)
7536 {
7537 /* If this compilation unit was already read in, free the
7538 cached copy in order to read it in again. This is
7539 necessary because we skipped some symbols when we first
7540 read in the compilation unit (see load_partial_dies).
7541 This problem could be avoided, but the benefit is unclear. */
7542 if (this_cu->cu != NULL)
7543 free_one_cached_comp_unit (this_cu);
7544
7545 cutu_reader reader (this_cu, NULL, 0, false);
7546
7547 switch (reader.comp_unit_die->tag)
7548 {
7549 case DW_TAG_compile_unit:
7550 this_cu->unit_type = DW_UT_compile;
7551 break;
7552 case DW_TAG_partial_unit:
7553 this_cu->unit_type = DW_UT_partial;
7554 break;
7555 default:
7556 abort ();
7557 }
7558
7559 if (reader.dummy_p)
7560 {
7561 /* Nothing. */
7562 }
7563 else if (this_cu->is_debug_types)
7564 build_type_psymtabs_reader (&reader, reader.info_ptr,
7565 reader.comp_unit_die);
7566 else if (want_partial_unit
7567 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7568 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7569 reader.comp_unit_die,
7570 pretend_language);
7571
7572 this_cu->lang = this_cu->cu->language;
7573
7574 /* Age out any secondary CUs. */
7575 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7576 }
7577
7578 /* Reader function for build_type_psymtabs. */
7579
7580 static void
7581 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7582 const gdb_byte *info_ptr,
7583 struct die_info *type_unit_die)
7584 {
7585 struct dwarf2_per_objfile *dwarf2_per_objfile
7586 = reader->cu->per_cu->dwarf2_per_objfile;
7587 struct objfile *objfile = dwarf2_per_objfile->objfile;
7588 struct dwarf2_cu *cu = reader->cu;
7589 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7590 struct signatured_type *sig_type;
7591 struct type_unit_group *tu_group;
7592 struct attribute *attr;
7593 struct partial_die_info *first_die;
7594 CORE_ADDR lowpc, highpc;
7595 dwarf2_psymtab *pst;
7596
7597 gdb_assert (per_cu->is_debug_types);
7598 sig_type = (struct signatured_type *) per_cu;
7599
7600 if (! type_unit_die->has_children)
7601 return;
7602
7603 attr = type_unit_die->attr (DW_AT_stmt_list);
7604 tu_group = get_type_unit_group (cu, attr);
7605
7606 if (tu_group->tus == nullptr)
7607 tu_group->tus = new std::vector<signatured_type *>;
7608 tu_group->tus->push_back (sig_type);
7609
7610 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7611 pst = create_partial_symtab (per_cu, "");
7612 pst->anonymous = true;
7613
7614 first_die = load_partial_dies (reader, info_ptr, 1);
7615
7616 lowpc = (CORE_ADDR) -1;
7617 highpc = (CORE_ADDR) 0;
7618 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7619
7620 end_psymtab_common (objfile, pst);
7621 }
7622
7623 /* Struct used to sort TUs by their abbreviation table offset. */
7624
7625 struct tu_abbrev_offset
7626 {
7627 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7628 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7629 {}
7630
7631 signatured_type *sig_type;
7632 sect_offset abbrev_offset;
7633 };
7634
7635 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7636
7637 static bool
7638 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7639 const struct tu_abbrev_offset &b)
7640 {
7641 return a.abbrev_offset < b.abbrev_offset;
7642 }
7643
7644 /* Efficiently read all the type units.
7645 This does the bulk of the work for build_type_psymtabs.
7646
7647 The efficiency is because we sort TUs by the abbrev table they use and
7648 only read each abbrev table once. In one program there are 200K TUs
7649 sharing 8K abbrev tables.
7650
7651 The main purpose of this function is to support building the
7652 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7653 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7654 can collapse the search space by grouping them by stmt_list.
7655 The savings can be significant, in the same program from above the 200K TUs
7656 share 8K stmt_list tables.
7657
7658 FUNC is expected to call get_type_unit_group, which will create the
7659 struct type_unit_group if necessary and add it to
7660 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7661
7662 static void
7663 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7664 {
7665 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7666 abbrev_table_up abbrev_table;
7667 sect_offset abbrev_offset;
7668
7669 /* It's up to the caller to not call us multiple times. */
7670 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7671
7672 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7673 return;
7674
7675 /* TUs typically share abbrev tables, and there can be way more TUs than
7676 abbrev tables. Sort by abbrev table to reduce the number of times we
7677 read each abbrev table in.
7678 Alternatives are to punt or to maintain a cache of abbrev tables.
7679 This is simpler and efficient enough for now.
7680
7681 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7682 symtab to use). Typically TUs with the same abbrev offset have the same
7683 stmt_list value too so in practice this should work well.
7684
7685 The basic algorithm here is:
7686
7687 sort TUs by abbrev table
7688 for each TU with same abbrev table:
7689 read abbrev table if first user
7690 read TU top level DIE
7691 [IWBN if DWO skeletons had DW_AT_stmt_list]
7692 call FUNC */
7693
7694 if (dwarf_read_debug)
7695 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7696
7697 /* Sort in a separate table to maintain the order of all_type_units
7698 for .gdb_index: TU indices directly index all_type_units. */
7699 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7700 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7701
7702 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7703 sorted_by_abbrev.emplace_back
7704 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7705 sig_type->per_cu.section,
7706 sig_type->per_cu.sect_off));
7707
7708 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7709 sort_tu_by_abbrev_offset);
7710
7711 abbrev_offset = (sect_offset) ~(unsigned) 0;
7712
7713 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7714 {
7715 /* Switch to the next abbrev table if necessary. */
7716 if (abbrev_table == NULL
7717 || tu.abbrev_offset != abbrev_offset)
7718 {
7719 abbrev_offset = tu.abbrev_offset;
7720 abbrev_table =
7721 abbrev_table::read (dwarf2_per_objfile->objfile,
7722 &dwarf2_per_objfile->per_bfd->abbrev,
7723 abbrev_offset);
7724 ++tu_stats->nr_uniq_abbrev_tables;
7725 }
7726
7727 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7728 0, false);
7729 if (!reader.dummy_p)
7730 build_type_psymtabs_reader (&reader, reader.info_ptr,
7731 reader.comp_unit_die);
7732 }
7733 }
7734
7735 /* Print collected type unit statistics. */
7736
7737 static void
7738 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7739 {
7740 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7741
7742 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7743 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7744 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7745 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7746 tu_stats->nr_uniq_abbrev_tables);
7747 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7748 tu_stats->nr_symtabs);
7749 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7750 tu_stats->nr_symtab_sharers);
7751 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7752 tu_stats->nr_stmt_less_type_units);
7753 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7754 tu_stats->nr_all_type_units_reallocs);
7755 }
7756
7757 /* Traversal function for build_type_psymtabs. */
7758
7759 static int
7760 build_type_psymtab_dependencies (void **slot, void *info)
7761 {
7762 struct dwarf2_per_objfile *dwarf2_per_objfile
7763 = (struct dwarf2_per_objfile *) info;
7764 struct objfile *objfile = dwarf2_per_objfile->objfile;
7765 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7766 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7767 dwarf2_psymtab *pst = per_cu->v.psymtab;
7768 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7769 int i;
7770
7771 gdb_assert (len > 0);
7772 gdb_assert (per_cu->type_unit_group_p ());
7773
7774 pst->number_of_dependencies = len;
7775 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7776 for (i = 0; i < len; ++i)
7777 {
7778 struct signatured_type *iter = tu_group->tus->at (i);
7779 gdb_assert (iter->per_cu.is_debug_types);
7780 pst->dependencies[i] = iter->per_cu.v.psymtab;
7781 iter->type_unit_group = tu_group;
7782 }
7783
7784 delete tu_group->tus;
7785 tu_group->tus = nullptr;
7786
7787 return 1;
7788 }
7789
7790 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7791 Build partial symbol tables for the .debug_types comp-units. */
7792
7793 static void
7794 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7795 {
7796 if (! create_all_type_units (dwarf2_per_objfile))
7797 return;
7798
7799 build_type_psymtabs_1 (dwarf2_per_objfile);
7800 }
7801
7802 /* Traversal function for process_skeletonless_type_unit.
7803 Read a TU in a DWO file and build partial symbols for it. */
7804
7805 static int
7806 process_skeletonless_type_unit (void **slot, void *info)
7807 {
7808 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7809 struct dwarf2_per_objfile *dwarf2_per_objfile
7810 = (struct dwarf2_per_objfile *) info;
7811 struct signatured_type find_entry, *entry;
7812
7813 /* If this TU doesn't exist in the global table, add it and read it in. */
7814
7815 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7816 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7817
7818 find_entry.signature = dwo_unit->signature;
7819 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7820 &find_entry, INSERT);
7821 /* If we've already seen this type there's nothing to do. What's happening
7822 is we're doing our own version of comdat-folding here. */
7823 if (*slot != NULL)
7824 return 1;
7825
7826 /* This does the job that create_all_type_units would have done for
7827 this TU. */
7828 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7829 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7830 *slot = entry;
7831
7832 /* This does the job that build_type_psymtabs_1 would have done. */
7833 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7834 if (!reader.dummy_p)
7835 build_type_psymtabs_reader (&reader, reader.info_ptr,
7836 reader.comp_unit_die);
7837
7838 return 1;
7839 }
7840
7841 /* Traversal function for process_skeletonless_type_units. */
7842
7843 static int
7844 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7845 {
7846 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7847
7848 if (dwo_file->tus != NULL)
7849 htab_traverse_noresize (dwo_file->tus.get (),
7850 process_skeletonless_type_unit, info);
7851
7852 return 1;
7853 }
7854
7855 /* Scan all TUs of DWO files, verifying we've processed them.
7856 This is needed in case a TU was emitted without its skeleton.
7857 Note: This can't be done until we know what all the DWO files are. */
7858
7859 static void
7860 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7861 {
7862 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7863 if (get_dwp_file (dwarf2_per_objfile) == NULL
7864 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7865 {
7866 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7867 process_dwo_file_for_skeletonless_type_units,
7868 dwarf2_per_objfile);
7869 }
7870 }
7871
7872 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7873
7874 static void
7875 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7876 {
7877 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7878 {
7879 dwarf2_psymtab *pst = per_cu->v.psymtab;
7880
7881 if (pst == NULL)
7882 continue;
7883
7884 for (int j = 0; j < pst->number_of_dependencies; ++j)
7885 {
7886 /* Set the 'user' field only if it is not already set. */
7887 if (pst->dependencies[j]->user == NULL)
7888 pst->dependencies[j]->user = pst;
7889 }
7890 }
7891 }
7892
7893 /* Build the partial symbol table by doing a quick pass through the
7894 .debug_info and .debug_abbrev sections. */
7895
7896 static void
7897 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7898 {
7899 struct objfile *objfile = dwarf2_per_objfile->objfile;
7900
7901 if (dwarf_read_debug)
7902 {
7903 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7904 objfile_name (objfile));
7905 }
7906
7907 scoped_restore restore_reading_psyms
7908 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7909 true);
7910
7911 dwarf2_per_objfile->per_bfd->info.read (objfile);
7912
7913 /* Any cached compilation units will be linked by the per-objfile
7914 read_in_chain. Make sure to free them when we're done. */
7915 free_cached_comp_units freer (dwarf2_per_objfile);
7916
7917 build_type_psymtabs (dwarf2_per_objfile);
7918
7919 create_all_comp_units (dwarf2_per_objfile);
7920
7921 /* Create a temporary address map on a temporary obstack. We later
7922 copy this to the final obstack. */
7923 auto_obstack temp_obstack;
7924
7925 scoped_restore save_psymtabs_addrmap
7926 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7927 addrmap_create_mutable (&temp_obstack));
7928
7929 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7930 {
7931 if (per_cu->v.psymtab != NULL)
7932 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7933 continue;
7934 process_psymtab_comp_unit (per_cu, false, language_minimal);
7935 }
7936
7937 /* This has to wait until we read the CUs, we need the list of DWOs. */
7938 process_skeletonless_type_units (dwarf2_per_objfile);
7939
7940 /* Now that all TUs have been processed we can fill in the dependencies. */
7941 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7942 {
7943 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7944 build_type_psymtab_dependencies, dwarf2_per_objfile);
7945 }
7946
7947 if (dwarf_read_debug)
7948 print_tu_stats (dwarf2_per_objfile);
7949
7950 set_partial_user (dwarf2_per_objfile);
7951
7952 objfile->partial_symtabs->psymtabs_addrmap
7953 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7954 objfile->partial_symtabs->obstack ());
7955 /* At this point we want to keep the address map. */
7956 save_psymtabs_addrmap.release ();
7957
7958 if (dwarf_read_debug)
7959 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7960 objfile_name (objfile));
7961 }
7962
7963 /* Load the partial DIEs for a secondary CU into memory.
7964 This is also used when rereading a primary CU with load_all_dies. */
7965
7966 static void
7967 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7968 {
7969 cutu_reader reader (this_cu, NULL, 1, false);
7970
7971 if (!reader.dummy_p)
7972 {
7973 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7974 language_minimal);
7975
7976 /* Check if comp unit has_children.
7977 If so, read the rest of the partial symbols from this comp unit.
7978 If not, there's no more debug_info for this comp unit. */
7979 if (reader.comp_unit_die->has_children)
7980 load_partial_dies (&reader, reader.info_ptr, 0);
7981
7982 reader.keep ();
7983 }
7984 }
7985
7986 static void
7987 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7988 struct dwarf2_section_info *section,
7989 struct dwarf2_section_info *abbrev_section,
7990 unsigned int is_dwz)
7991 {
7992 const gdb_byte *info_ptr;
7993 struct objfile *objfile = dwarf2_per_objfile->objfile;
7994
7995 if (dwarf_read_debug)
7996 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7997 section->get_name (),
7998 section->get_file_name ());
7999
8000 section->read (objfile);
8001
8002 info_ptr = section->buffer;
8003
8004 while (info_ptr < section->buffer + section->size)
8005 {
8006 struct dwarf2_per_cu_data *this_cu;
8007
8008 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8009
8010 comp_unit_head cu_header;
8011 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8012 abbrev_section, info_ptr,
8013 rcuh_kind::COMPILE);
8014
8015 /* Save the compilation unit for later lookup. */
8016 if (cu_header.unit_type != DW_UT_type)
8017 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8018 else
8019 {
8020 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8021 sig_type->signature = cu_header.signature;
8022 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8023 this_cu = &sig_type->per_cu;
8024 }
8025 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8026 this_cu->sect_off = sect_off;
8027 this_cu->length = cu_header.length + cu_header.initial_length_size;
8028 this_cu->is_dwz = is_dwz;
8029 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8030 this_cu->section = section;
8031
8032 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8033
8034 info_ptr = info_ptr + this_cu->length;
8035 }
8036 }
8037
8038 /* Create a list of all compilation units in OBJFILE.
8039 This is only done for -readnow and building partial symtabs. */
8040
8041 static void
8042 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8043 {
8044 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8045 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8046 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8047
8048 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8049 if (dwz != NULL)
8050 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8051 1);
8052 }
8053
8054 /* Process all loaded DIEs for compilation unit CU, starting at
8055 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8056 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8057 DW_AT_ranges). See the comments of add_partial_subprogram on how
8058 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8059
8060 static void
8061 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8062 CORE_ADDR *highpc, int set_addrmap,
8063 struct dwarf2_cu *cu)
8064 {
8065 struct partial_die_info *pdi;
8066
8067 /* Now, march along the PDI's, descending into ones which have
8068 interesting children but skipping the children of the other ones,
8069 until we reach the end of the compilation unit. */
8070
8071 pdi = first_die;
8072
8073 while (pdi != NULL)
8074 {
8075 pdi->fixup (cu);
8076
8077 /* Anonymous namespaces or modules have no name but have interesting
8078 children, so we need to look at them. Ditto for anonymous
8079 enums. */
8080
8081 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8082 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8083 || pdi->tag == DW_TAG_imported_unit
8084 || pdi->tag == DW_TAG_inlined_subroutine)
8085 {
8086 switch (pdi->tag)
8087 {
8088 case DW_TAG_subprogram:
8089 case DW_TAG_inlined_subroutine:
8090 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8091 break;
8092 case DW_TAG_constant:
8093 case DW_TAG_variable:
8094 case DW_TAG_typedef:
8095 case DW_TAG_union_type:
8096 if (!pdi->is_declaration
8097 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8098 {
8099 add_partial_symbol (pdi, cu);
8100 }
8101 break;
8102 case DW_TAG_class_type:
8103 case DW_TAG_interface_type:
8104 case DW_TAG_structure_type:
8105 if (!pdi->is_declaration)
8106 {
8107 add_partial_symbol (pdi, cu);
8108 }
8109 if ((cu->language == language_rust
8110 || cu->language == language_cplus) && pdi->has_children)
8111 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8112 set_addrmap, cu);
8113 break;
8114 case DW_TAG_enumeration_type:
8115 if (!pdi->is_declaration)
8116 add_partial_enumeration (pdi, cu);
8117 break;
8118 case DW_TAG_base_type:
8119 case DW_TAG_subrange_type:
8120 /* File scope base type definitions are added to the partial
8121 symbol table. */
8122 add_partial_symbol (pdi, cu);
8123 break;
8124 case DW_TAG_namespace:
8125 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8126 break;
8127 case DW_TAG_module:
8128 if (!pdi->is_declaration)
8129 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8130 break;
8131 case DW_TAG_imported_unit:
8132 {
8133 struct dwarf2_per_cu_data *per_cu;
8134
8135 /* For now we don't handle imported units in type units. */
8136 if (cu->per_cu->is_debug_types)
8137 {
8138 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8139 " supported in type units [in module %s]"),
8140 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8141 }
8142
8143 per_cu = dwarf2_find_containing_comp_unit
8144 (pdi->d.sect_off, pdi->is_dwz,
8145 cu->per_cu->dwarf2_per_objfile);
8146
8147 /* Go read the partial unit, if needed. */
8148 if (per_cu->v.psymtab == NULL)
8149 process_psymtab_comp_unit (per_cu, true, cu->language);
8150
8151 cu->per_cu->imported_symtabs_push (per_cu);
8152 }
8153 break;
8154 case DW_TAG_imported_declaration:
8155 add_partial_symbol (pdi, cu);
8156 break;
8157 default:
8158 break;
8159 }
8160 }
8161
8162 /* If the die has a sibling, skip to the sibling. */
8163
8164 pdi = pdi->die_sibling;
8165 }
8166 }
8167
8168 /* Functions used to compute the fully scoped name of a partial DIE.
8169
8170 Normally, this is simple. For C++, the parent DIE's fully scoped
8171 name is concatenated with "::" and the partial DIE's name.
8172 Enumerators are an exception; they use the scope of their parent
8173 enumeration type, i.e. the name of the enumeration type is not
8174 prepended to the enumerator.
8175
8176 There are two complexities. One is DW_AT_specification; in this
8177 case "parent" means the parent of the target of the specification,
8178 instead of the direct parent of the DIE. The other is compilers
8179 which do not emit DW_TAG_namespace; in this case we try to guess
8180 the fully qualified name of structure types from their members'
8181 linkage names. This must be done using the DIE's children rather
8182 than the children of any DW_AT_specification target. We only need
8183 to do this for structures at the top level, i.e. if the target of
8184 any DW_AT_specification (if any; otherwise the DIE itself) does not
8185 have a parent. */
8186
8187 /* Compute the scope prefix associated with PDI's parent, in
8188 compilation unit CU. The result will be allocated on CU's
8189 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8190 field. NULL is returned if no prefix is necessary. */
8191 static const char *
8192 partial_die_parent_scope (struct partial_die_info *pdi,
8193 struct dwarf2_cu *cu)
8194 {
8195 const char *grandparent_scope;
8196 struct partial_die_info *parent, *real_pdi;
8197
8198 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8199 then this means the parent of the specification DIE. */
8200
8201 real_pdi = pdi;
8202 while (real_pdi->has_specification)
8203 {
8204 auto res = find_partial_die (real_pdi->spec_offset,
8205 real_pdi->spec_is_dwz, cu);
8206 real_pdi = res.pdi;
8207 cu = res.cu;
8208 }
8209
8210 parent = real_pdi->die_parent;
8211 if (parent == NULL)
8212 return NULL;
8213
8214 if (parent->scope_set)
8215 return parent->scope;
8216
8217 parent->fixup (cu);
8218
8219 grandparent_scope = partial_die_parent_scope (parent, cu);
8220
8221 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8222 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8223 Work around this problem here. */
8224 if (cu->language == language_cplus
8225 && parent->tag == DW_TAG_namespace
8226 && strcmp (parent->name, "::") == 0
8227 && grandparent_scope == NULL)
8228 {
8229 parent->scope = NULL;
8230 parent->scope_set = 1;
8231 return NULL;
8232 }
8233
8234 /* Nested subroutines in Fortran get a prefix. */
8235 if (pdi->tag == DW_TAG_enumerator)
8236 /* Enumerators should not get the name of the enumeration as a prefix. */
8237 parent->scope = grandparent_scope;
8238 else if (parent->tag == DW_TAG_namespace
8239 || parent->tag == DW_TAG_module
8240 || parent->tag == DW_TAG_structure_type
8241 || parent->tag == DW_TAG_class_type
8242 || parent->tag == DW_TAG_interface_type
8243 || parent->tag == DW_TAG_union_type
8244 || parent->tag == DW_TAG_enumeration_type
8245 || (cu->language == language_fortran
8246 && parent->tag == DW_TAG_subprogram
8247 && pdi->tag == DW_TAG_subprogram))
8248 {
8249 if (grandparent_scope == NULL)
8250 parent->scope = parent->name;
8251 else
8252 parent->scope = typename_concat (&cu->comp_unit_obstack,
8253 grandparent_scope,
8254 parent->name, 0, cu);
8255 }
8256 else
8257 {
8258 /* FIXME drow/2004-04-01: What should we be doing with
8259 function-local names? For partial symbols, we should probably be
8260 ignoring them. */
8261 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8262 dwarf_tag_name (parent->tag),
8263 sect_offset_str (pdi->sect_off));
8264 parent->scope = grandparent_scope;
8265 }
8266
8267 parent->scope_set = 1;
8268 return parent->scope;
8269 }
8270
8271 /* Return the fully scoped name associated with PDI, from compilation unit
8272 CU. The result will be allocated with malloc. */
8273
8274 static gdb::unique_xmalloc_ptr<char>
8275 partial_die_full_name (struct partial_die_info *pdi,
8276 struct dwarf2_cu *cu)
8277 {
8278 const char *parent_scope;
8279
8280 /* If this is a template instantiation, we can not work out the
8281 template arguments from partial DIEs. So, unfortunately, we have
8282 to go through the full DIEs. At least any work we do building
8283 types here will be reused if full symbols are loaded later. */
8284 if (pdi->has_template_arguments)
8285 {
8286 pdi->fixup (cu);
8287
8288 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8289 {
8290 struct die_info *die;
8291 struct attribute attr;
8292 struct dwarf2_cu *ref_cu = cu;
8293
8294 /* DW_FORM_ref_addr is using section offset. */
8295 attr.name = (enum dwarf_attribute) 0;
8296 attr.form = DW_FORM_ref_addr;
8297 attr.u.unsnd = to_underlying (pdi->sect_off);
8298 die = follow_die_ref (NULL, &attr, &ref_cu);
8299
8300 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8301 }
8302 }
8303
8304 parent_scope = partial_die_parent_scope (pdi, cu);
8305 if (parent_scope == NULL)
8306 return NULL;
8307 else
8308 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8309 pdi->name, 0, cu));
8310 }
8311
8312 static void
8313 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8314 {
8315 struct dwarf2_per_objfile *dwarf2_per_objfile
8316 = cu->per_cu->dwarf2_per_objfile;
8317 struct objfile *objfile = dwarf2_per_objfile->objfile;
8318 struct gdbarch *gdbarch = objfile->arch ();
8319 CORE_ADDR addr = 0;
8320 const char *actual_name = NULL;
8321 CORE_ADDR baseaddr;
8322
8323 baseaddr = objfile->text_section_offset ();
8324
8325 gdb::unique_xmalloc_ptr<char> built_actual_name
8326 = partial_die_full_name (pdi, cu);
8327 if (built_actual_name != NULL)
8328 actual_name = built_actual_name.get ();
8329
8330 if (actual_name == NULL)
8331 actual_name = pdi->name;
8332
8333 partial_symbol psymbol;
8334 memset (&psymbol, 0, sizeof (psymbol));
8335 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8336 psymbol.ginfo.section = -1;
8337
8338 /* The code below indicates that the psymbol should be installed by
8339 setting this. */
8340 gdb::optional<psymbol_placement> where;
8341
8342 switch (pdi->tag)
8343 {
8344 case DW_TAG_inlined_subroutine:
8345 case DW_TAG_subprogram:
8346 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8347 - baseaddr);
8348 if (pdi->is_external
8349 || cu->language == language_ada
8350 || (cu->language == language_fortran
8351 && pdi->die_parent != NULL
8352 && pdi->die_parent->tag == DW_TAG_subprogram))
8353 {
8354 /* Normally, only "external" DIEs are part of the global scope.
8355 But in Ada and Fortran, we want to be able to access nested
8356 procedures globally. So all Ada and Fortran subprograms are
8357 stored in the global scope. */
8358 where = psymbol_placement::GLOBAL;
8359 }
8360 else
8361 where = psymbol_placement::STATIC;
8362
8363 psymbol.domain = VAR_DOMAIN;
8364 psymbol.aclass = LOC_BLOCK;
8365 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8366 psymbol.ginfo.value.address = addr;
8367
8368 if (pdi->main_subprogram && actual_name != NULL)
8369 set_objfile_main_name (objfile, actual_name, cu->language);
8370 break;
8371 case DW_TAG_constant:
8372 psymbol.domain = VAR_DOMAIN;
8373 psymbol.aclass = LOC_STATIC;
8374 where = (pdi->is_external
8375 ? psymbol_placement::GLOBAL
8376 : psymbol_placement::STATIC);
8377 break;
8378 case DW_TAG_variable:
8379 if (pdi->d.locdesc)
8380 addr = decode_locdesc (pdi->d.locdesc, cu);
8381
8382 if (pdi->d.locdesc
8383 && addr == 0
8384 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8385 {
8386 /* A global or static variable may also have been stripped
8387 out by the linker if unused, in which case its address
8388 will be nullified; do not add such variables into partial
8389 symbol table then. */
8390 }
8391 else if (pdi->is_external)
8392 {
8393 /* Global Variable.
8394 Don't enter into the minimal symbol tables as there is
8395 a minimal symbol table entry from the ELF symbols already.
8396 Enter into partial symbol table if it has a location
8397 descriptor or a type.
8398 If the location descriptor is missing, new_symbol will create
8399 a LOC_UNRESOLVED symbol, the address of the variable will then
8400 be determined from the minimal symbol table whenever the variable
8401 is referenced.
8402 The address for the partial symbol table entry is not
8403 used by GDB, but it comes in handy for debugging partial symbol
8404 table building. */
8405
8406 if (pdi->d.locdesc || pdi->has_type)
8407 {
8408 psymbol.domain = VAR_DOMAIN;
8409 psymbol.aclass = LOC_STATIC;
8410 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8411 psymbol.ginfo.value.address = addr;
8412 where = psymbol_placement::GLOBAL;
8413 }
8414 }
8415 else
8416 {
8417 int has_loc = pdi->d.locdesc != NULL;
8418
8419 /* Static Variable. Skip symbols whose value we cannot know (those
8420 without location descriptors or constant values). */
8421 if (!has_loc && !pdi->has_const_value)
8422 return;
8423
8424 psymbol.domain = VAR_DOMAIN;
8425 psymbol.aclass = LOC_STATIC;
8426 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8427 if (has_loc)
8428 psymbol.ginfo.value.address = addr;
8429 where = psymbol_placement::STATIC;
8430 }
8431 break;
8432 case DW_TAG_typedef:
8433 case DW_TAG_base_type:
8434 case DW_TAG_subrange_type:
8435 psymbol.domain = VAR_DOMAIN;
8436 psymbol.aclass = LOC_TYPEDEF;
8437 where = psymbol_placement::STATIC;
8438 break;
8439 case DW_TAG_imported_declaration:
8440 case DW_TAG_namespace:
8441 psymbol.domain = VAR_DOMAIN;
8442 psymbol.aclass = LOC_TYPEDEF;
8443 where = psymbol_placement::GLOBAL;
8444 break;
8445 case DW_TAG_module:
8446 /* With Fortran 77 there might be a "BLOCK DATA" module
8447 available without any name. If so, we skip the module as it
8448 doesn't bring any value. */
8449 if (actual_name != nullptr)
8450 {
8451 psymbol.domain = MODULE_DOMAIN;
8452 psymbol.aclass = LOC_TYPEDEF;
8453 where = psymbol_placement::GLOBAL;
8454 }
8455 break;
8456 case DW_TAG_class_type:
8457 case DW_TAG_interface_type:
8458 case DW_TAG_structure_type:
8459 case DW_TAG_union_type:
8460 case DW_TAG_enumeration_type:
8461 /* Skip external references. The DWARF standard says in the section
8462 about "Structure, Union, and Class Type Entries": "An incomplete
8463 structure, union or class type is represented by a structure,
8464 union or class entry that does not have a byte size attribute
8465 and that has a DW_AT_declaration attribute." */
8466 if (!pdi->has_byte_size && pdi->is_declaration)
8467 return;
8468
8469 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8470 static vs. global. */
8471 psymbol.domain = STRUCT_DOMAIN;
8472 psymbol.aclass = LOC_TYPEDEF;
8473 where = (cu->language == language_cplus
8474 ? psymbol_placement::GLOBAL
8475 : psymbol_placement::STATIC);
8476 break;
8477 case DW_TAG_enumerator:
8478 psymbol.domain = VAR_DOMAIN;
8479 psymbol.aclass = LOC_CONST;
8480 where = (cu->language == language_cplus
8481 ? psymbol_placement::GLOBAL
8482 : psymbol_placement::STATIC);
8483 break;
8484 default:
8485 break;
8486 }
8487
8488 if (where.has_value ())
8489 {
8490 if (built_actual_name != nullptr)
8491 actual_name = objfile->intern (actual_name);
8492 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8493 psymbol.ginfo.set_linkage_name (actual_name);
8494 else
8495 {
8496 psymbol.ginfo.set_demangled_name (actual_name,
8497 &objfile->objfile_obstack);
8498 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8499 }
8500 add_psymbol_to_list (psymbol, *where, objfile);
8501 }
8502 }
8503
8504 /* Read a partial die corresponding to a namespace; also, add a symbol
8505 corresponding to that namespace to the symbol table. NAMESPACE is
8506 the name of the enclosing namespace. */
8507
8508 static void
8509 add_partial_namespace (struct partial_die_info *pdi,
8510 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8511 int set_addrmap, struct dwarf2_cu *cu)
8512 {
8513 /* Add a symbol for the namespace. */
8514
8515 add_partial_symbol (pdi, cu);
8516
8517 /* Now scan partial symbols in that namespace. */
8518
8519 if (pdi->has_children)
8520 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8521 }
8522
8523 /* Read a partial die corresponding to a Fortran module. */
8524
8525 static void
8526 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8527 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8528 {
8529 /* Add a symbol for the namespace. */
8530
8531 add_partial_symbol (pdi, cu);
8532
8533 /* Now scan partial symbols in that module. */
8534
8535 if (pdi->has_children)
8536 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8537 }
8538
8539 /* Read a partial die corresponding to a subprogram or an inlined
8540 subprogram and create a partial symbol for that subprogram.
8541 When the CU language allows it, this routine also defines a partial
8542 symbol for each nested subprogram that this subprogram contains.
8543 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8544 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8545
8546 PDI may also be a lexical block, in which case we simply search
8547 recursively for subprograms defined inside that lexical block.
8548 Again, this is only performed when the CU language allows this
8549 type of definitions. */
8550
8551 static void
8552 add_partial_subprogram (struct partial_die_info *pdi,
8553 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8554 int set_addrmap, struct dwarf2_cu *cu)
8555 {
8556 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8557 {
8558 if (pdi->has_pc_info)
8559 {
8560 if (pdi->lowpc < *lowpc)
8561 *lowpc = pdi->lowpc;
8562 if (pdi->highpc > *highpc)
8563 *highpc = pdi->highpc;
8564 if (set_addrmap)
8565 {
8566 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8567 struct gdbarch *gdbarch = objfile->arch ();
8568 CORE_ADDR baseaddr;
8569 CORE_ADDR this_highpc;
8570 CORE_ADDR this_lowpc;
8571
8572 baseaddr = objfile->text_section_offset ();
8573 this_lowpc
8574 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8575 pdi->lowpc + baseaddr)
8576 - baseaddr);
8577 this_highpc
8578 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8579 pdi->highpc + baseaddr)
8580 - baseaddr);
8581 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8582 this_lowpc, this_highpc - 1,
8583 cu->per_cu->v.psymtab);
8584 }
8585 }
8586
8587 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8588 {
8589 if (!pdi->is_declaration)
8590 /* Ignore subprogram DIEs that do not have a name, they are
8591 illegal. Do not emit a complaint at this point, we will
8592 do so when we convert this psymtab into a symtab. */
8593 if (pdi->name)
8594 add_partial_symbol (pdi, cu);
8595 }
8596 }
8597
8598 if (! pdi->has_children)
8599 return;
8600
8601 if (cu->language == language_ada || cu->language == language_fortran)
8602 {
8603 pdi = pdi->die_child;
8604 while (pdi != NULL)
8605 {
8606 pdi->fixup (cu);
8607 if (pdi->tag == DW_TAG_subprogram
8608 || pdi->tag == DW_TAG_inlined_subroutine
8609 || pdi->tag == DW_TAG_lexical_block)
8610 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8611 pdi = pdi->die_sibling;
8612 }
8613 }
8614 }
8615
8616 /* Read a partial die corresponding to an enumeration type. */
8617
8618 static void
8619 add_partial_enumeration (struct partial_die_info *enum_pdi,
8620 struct dwarf2_cu *cu)
8621 {
8622 struct partial_die_info *pdi;
8623
8624 if (enum_pdi->name != NULL)
8625 add_partial_symbol (enum_pdi, cu);
8626
8627 pdi = enum_pdi->die_child;
8628 while (pdi)
8629 {
8630 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8631 complaint (_("malformed enumerator DIE ignored"));
8632 else
8633 add_partial_symbol (pdi, cu);
8634 pdi = pdi->die_sibling;
8635 }
8636 }
8637
8638 /* Return the initial uleb128 in the die at INFO_PTR. */
8639
8640 static unsigned int
8641 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8642 {
8643 unsigned int bytes_read;
8644
8645 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8646 }
8647
8648 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8649 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8650
8651 Return the corresponding abbrev, or NULL if the number is zero (indicating
8652 an empty DIE). In either case *BYTES_READ will be set to the length of
8653 the initial number. */
8654
8655 static struct abbrev_info *
8656 peek_die_abbrev (const die_reader_specs &reader,
8657 const gdb_byte *info_ptr, unsigned int *bytes_read)
8658 {
8659 dwarf2_cu *cu = reader.cu;
8660 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8661 unsigned int abbrev_number
8662 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8663
8664 if (abbrev_number == 0)
8665 return NULL;
8666
8667 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8668 if (!abbrev)
8669 {
8670 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8671 " at offset %s [in module %s]"),
8672 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8673 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8674 }
8675
8676 return abbrev;
8677 }
8678
8679 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8680 Returns a pointer to the end of a series of DIEs, terminated by an empty
8681 DIE. Any children of the skipped DIEs will also be skipped. */
8682
8683 static const gdb_byte *
8684 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8685 {
8686 while (1)
8687 {
8688 unsigned int bytes_read;
8689 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8690
8691 if (abbrev == NULL)
8692 return info_ptr + bytes_read;
8693 else
8694 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8695 }
8696 }
8697
8698 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8699 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8700 abbrev corresponding to that skipped uleb128 should be passed in
8701 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8702 children. */
8703
8704 static const gdb_byte *
8705 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8706 struct abbrev_info *abbrev)
8707 {
8708 unsigned int bytes_read;
8709 struct attribute attr;
8710 bfd *abfd = reader->abfd;
8711 struct dwarf2_cu *cu = reader->cu;
8712 const gdb_byte *buffer = reader->buffer;
8713 const gdb_byte *buffer_end = reader->buffer_end;
8714 unsigned int form, i;
8715
8716 for (i = 0; i < abbrev->num_attrs; i++)
8717 {
8718 /* The only abbrev we care about is DW_AT_sibling. */
8719 if (abbrev->attrs[i].name == DW_AT_sibling)
8720 {
8721 bool ignored;
8722 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8723 &ignored);
8724 if (attr.form == DW_FORM_ref_addr)
8725 complaint (_("ignoring absolute DW_AT_sibling"));
8726 else
8727 {
8728 sect_offset off = attr.get_ref_die_offset ();
8729 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8730
8731 if (sibling_ptr < info_ptr)
8732 complaint (_("DW_AT_sibling points backwards"));
8733 else if (sibling_ptr > reader->buffer_end)
8734 reader->die_section->overflow_complaint ();
8735 else
8736 return sibling_ptr;
8737 }
8738 }
8739
8740 /* If it isn't DW_AT_sibling, skip this attribute. */
8741 form = abbrev->attrs[i].form;
8742 skip_attribute:
8743 switch (form)
8744 {
8745 case DW_FORM_ref_addr:
8746 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8747 and later it is offset sized. */
8748 if (cu->header.version == 2)
8749 info_ptr += cu->header.addr_size;
8750 else
8751 info_ptr += cu->header.offset_size;
8752 break;
8753 case DW_FORM_GNU_ref_alt:
8754 info_ptr += cu->header.offset_size;
8755 break;
8756 case DW_FORM_addr:
8757 info_ptr += cu->header.addr_size;
8758 break;
8759 case DW_FORM_data1:
8760 case DW_FORM_ref1:
8761 case DW_FORM_flag:
8762 case DW_FORM_strx1:
8763 info_ptr += 1;
8764 break;
8765 case DW_FORM_flag_present:
8766 case DW_FORM_implicit_const:
8767 break;
8768 case DW_FORM_data2:
8769 case DW_FORM_ref2:
8770 case DW_FORM_strx2:
8771 info_ptr += 2;
8772 break;
8773 case DW_FORM_strx3:
8774 info_ptr += 3;
8775 break;
8776 case DW_FORM_data4:
8777 case DW_FORM_ref4:
8778 case DW_FORM_strx4:
8779 info_ptr += 4;
8780 break;
8781 case DW_FORM_data8:
8782 case DW_FORM_ref8:
8783 case DW_FORM_ref_sig8:
8784 info_ptr += 8;
8785 break;
8786 case DW_FORM_data16:
8787 info_ptr += 16;
8788 break;
8789 case DW_FORM_string:
8790 read_direct_string (abfd, info_ptr, &bytes_read);
8791 info_ptr += bytes_read;
8792 break;
8793 case DW_FORM_sec_offset:
8794 case DW_FORM_strp:
8795 case DW_FORM_GNU_strp_alt:
8796 info_ptr += cu->header.offset_size;
8797 break;
8798 case DW_FORM_exprloc:
8799 case DW_FORM_block:
8800 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8801 info_ptr += bytes_read;
8802 break;
8803 case DW_FORM_block1:
8804 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8805 break;
8806 case DW_FORM_block2:
8807 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8808 break;
8809 case DW_FORM_block4:
8810 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8811 break;
8812 case DW_FORM_addrx:
8813 case DW_FORM_strx:
8814 case DW_FORM_sdata:
8815 case DW_FORM_udata:
8816 case DW_FORM_ref_udata:
8817 case DW_FORM_GNU_addr_index:
8818 case DW_FORM_GNU_str_index:
8819 case DW_FORM_rnglistx:
8820 case DW_FORM_loclistx:
8821 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8822 break;
8823 case DW_FORM_indirect:
8824 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8825 info_ptr += bytes_read;
8826 /* We need to continue parsing from here, so just go back to
8827 the top. */
8828 goto skip_attribute;
8829
8830 default:
8831 error (_("Dwarf Error: Cannot handle %s "
8832 "in DWARF reader [in module %s]"),
8833 dwarf_form_name (form),
8834 bfd_get_filename (abfd));
8835 }
8836 }
8837
8838 if (abbrev->has_children)
8839 return skip_children (reader, info_ptr);
8840 else
8841 return info_ptr;
8842 }
8843
8844 /* Locate ORIG_PDI's sibling.
8845 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8846
8847 static const gdb_byte *
8848 locate_pdi_sibling (const struct die_reader_specs *reader,
8849 struct partial_die_info *orig_pdi,
8850 const gdb_byte *info_ptr)
8851 {
8852 /* Do we know the sibling already? */
8853
8854 if (orig_pdi->sibling)
8855 return orig_pdi->sibling;
8856
8857 /* Are there any children to deal with? */
8858
8859 if (!orig_pdi->has_children)
8860 return info_ptr;
8861
8862 /* Skip the children the long way. */
8863
8864 return skip_children (reader, info_ptr);
8865 }
8866
8867 /* Expand this partial symbol table into a full symbol table. SELF is
8868 not NULL. */
8869
8870 void
8871 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8872 {
8873 struct dwarf2_per_objfile *dwarf2_per_objfile
8874 = get_dwarf2_per_objfile (objfile);
8875
8876 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8877
8878 /* If this psymtab is constructed from a debug-only objfile, the
8879 has_section_at_zero flag will not necessarily be correct. We
8880 can get the correct value for this flag by looking at the data
8881 associated with the (presumably stripped) associated objfile. */
8882 if (objfile->separate_debug_objfile_backlink)
8883 {
8884 struct dwarf2_per_objfile *dpo_backlink
8885 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8886
8887 dwarf2_per_objfile->per_bfd->has_section_at_zero
8888 = dpo_backlink->per_bfd->has_section_at_zero;
8889 }
8890
8891 expand_psymtab (objfile);
8892
8893 process_cu_includes (dwarf2_per_objfile);
8894 }
8895 \f
8896 /* Reading in full CUs. */
8897
8898 /* Add PER_CU to the queue. */
8899
8900 static void
8901 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8902 enum language pretend_language)
8903 {
8904 per_cu->queued = 1;
8905 per_cu->dwarf2_per_objfile->per_bfd->queue.emplace (per_cu, pretend_language);
8906 }
8907
8908 /* If PER_CU is not yet queued, add it to the queue.
8909 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8910 dependency.
8911 The result is non-zero if PER_CU was queued, otherwise the result is zero
8912 meaning either PER_CU is already queued or it is already loaded.
8913
8914 N.B. There is an invariant here that if a CU is queued then it is loaded.
8915 The caller is required to load PER_CU if we return non-zero. */
8916
8917 static int
8918 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8919 struct dwarf2_per_cu_data *per_cu,
8920 enum language pretend_language)
8921 {
8922 /* We may arrive here during partial symbol reading, if we need full
8923 DIEs to process an unusual case (e.g. template arguments). Do
8924 not queue PER_CU, just tell our caller to load its DIEs. */
8925 if (per_cu->dwarf2_per_objfile->per_bfd->reading_partial_symbols)
8926 {
8927 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8928 return 1;
8929 return 0;
8930 }
8931
8932 /* Mark the dependence relation so that we don't flush PER_CU
8933 too early. */
8934 if (dependent_cu != NULL)
8935 dwarf2_add_dependence (dependent_cu, per_cu);
8936
8937 /* If it's already on the queue, we have nothing to do. */
8938 if (per_cu->queued)
8939 return 0;
8940
8941 /* If the compilation unit is already loaded, just mark it as
8942 used. */
8943 if (per_cu->cu != NULL)
8944 {
8945 per_cu->cu->last_used = 0;
8946 return 0;
8947 }
8948
8949 /* Add it to the queue. */
8950 queue_comp_unit (per_cu, pretend_language);
8951
8952 return 1;
8953 }
8954
8955 /* Process the queue. */
8956
8957 static void
8958 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8959 {
8960 if (dwarf_read_debug)
8961 {
8962 fprintf_unfiltered (gdb_stdlog,
8963 "Expanding one or more symtabs of objfile %s ...\n",
8964 objfile_name (dwarf2_per_objfile->objfile));
8965 }
8966
8967 /* The queue starts out with one item, but following a DIE reference
8968 may load a new CU, adding it to the end of the queue. */
8969 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
8970 {
8971 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
8972
8973 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
8974 /* Skip dummy CUs. */
8975 && item.per_cu->cu != NULL)
8976 {
8977 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8978 unsigned int debug_print_threshold;
8979 char buf[100];
8980
8981 if (per_cu->is_debug_types)
8982 {
8983 struct signatured_type *sig_type =
8984 (struct signatured_type *) per_cu;
8985
8986 sprintf (buf, "TU %s at offset %s",
8987 hex_string (sig_type->signature),
8988 sect_offset_str (per_cu->sect_off));
8989 /* There can be 100s of TUs.
8990 Only print them in verbose mode. */
8991 debug_print_threshold = 2;
8992 }
8993 else
8994 {
8995 sprintf (buf, "CU at offset %s",
8996 sect_offset_str (per_cu->sect_off));
8997 debug_print_threshold = 1;
8998 }
8999
9000 if (dwarf_read_debug >= debug_print_threshold)
9001 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9002
9003 if (per_cu->is_debug_types)
9004 process_full_type_unit (per_cu, item.pretend_language);
9005 else
9006 process_full_comp_unit (per_cu, item.pretend_language);
9007
9008 if (dwarf_read_debug >= debug_print_threshold)
9009 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9010 }
9011
9012 item.per_cu->queued = 0;
9013 dwarf2_per_objfile->per_bfd->queue.pop ();
9014 }
9015
9016 if (dwarf_read_debug)
9017 {
9018 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9019 objfile_name (dwarf2_per_objfile->objfile));
9020 }
9021 }
9022
9023 /* Read in full symbols for PST, and anything it depends on. */
9024
9025 void
9026 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9027 {
9028 gdb_assert (!readin_p (objfile));
9029
9030 expand_dependencies (objfile);
9031
9032 dw2_do_instantiate_symtab (per_cu_data, false);
9033 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9034 }
9035
9036 /* See psympriv.h. */
9037
9038 bool
9039 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9040 {
9041 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9042 return per_objfile->symtab_set_p (per_cu_data);
9043 }
9044
9045 /* See psympriv.h. */
9046
9047 compunit_symtab *
9048 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9049 {
9050 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9051 return per_objfile->get_symtab (per_cu_data);
9052 }
9053
9054 /* Trivial hash function for die_info: the hash value of a DIE
9055 is its offset in .debug_info for this objfile. */
9056
9057 static hashval_t
9058 die_hash (const void *item)
9059 {
9060 const struct die_info *die = (const struct die_info *) item;
9061
9062 return to_underlying (die->sect_off);
9063 }
9064
9065 /* Trivial comparison function for die_info structures: two DIEs
9066 are equal if they have the same offset. */
9067
9068 static int
9069 die_eq (const void *item_lhs, const void *item_rhs)
9070 {
9071 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9072 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9073
9074 return die_lhs->sect_off == die_rhs->sect_off;
9075 }
9076
9077 /* Load the DIEs associated with PER_CU into memory. */
9078
9079 static void
9080 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9081 bool skip_partial,
9082 enum language pretend_language)
9083 {
9084 gdb_assert (! this_cu->is_debug_types);
9085
9086 cutu_reader reader (this_cu, NULL, 1, skip_partial);
9087 if (reader.dummy_p)
9088 return;
9089
9090 struct dwarf2_cu *cu = reader.cu;
9091 const gdb_byte *info_ptr = reader.info_ptr;
9092
9093 gdb_assert (cu->die_hash == NULL);
9094 cu->die_hash =
9095 htab_create_alloc_ex (cu->header.length / 12,
9096 die_hash,
9097 die_eq,
9098 NULL,
9099 &cu->comp_unit_obstack,
9100 hashtab_obstack_allocate,
9101 dummy_obstack_deallocate);
9102
9103 if (reader.comp_unit_die->has_children)
9104 reader.comp_unit_die->child
9105 = read_die_and_siblings (&reader, reader.info_ptr,
9106 &info_ptr, reader.comp_unit_die);
9107 cu->dies = reader.comp_unit_die;
9108 /* comp_unit_die is not stored in die_hash, no need. */
9109
9110 /* We try not to read any attributes in this function, because not
9111 all CUs needed for references have been loaded yet, and symbol
9112 table processing isn't initialized. But we have to set the CU language,
9113 or we won't be able to build types correctly.
9114 Similarly, if we do not read the producer, we can not apply
9115 producer-specific interpretation. */
9116 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9117
9118 reader.keep ();
9119 }
9120
9121 /* Add a DIE to the delayed physname list. */
9122
9123 static void
9124 add_to_method_list (struct type *type, int fnfield_index, int index,
9125 const char *name, struct die_info *die,
9126 struct dwarf2_cu *cu)
9127 {
9128 struct delayed_method_info mi;
9129 mi.type = type;
9130 mi.fnfield_index = fnfield_index;
9131 mi.index = index;
9132 mi.name = name;
9133 mi.die = die;
9134 cu->method_list.push_back (mi);
9135 }
9136
9137 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9138 "const" / "volatile". If so, decrements LEN by the length of the
9139 modifier and return true. Otherwise return false. */
9140
9141 template<size_t N>
9142 static bool
9143 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9144 {
9145 size_t mod_len = sizeof (mod) - 1;
9146 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9147 {
9148 len -= mod_len;
9149 return true;
9150 }
9151 return false;
9152 }
9153
9154 /* Compute the physnames of any methods on the CU's method list.
9155
9156 The computation of method physnames is delayed in order to avoid the
9157 (bad) condition that one of the method's formal parameters is of an as yet
9158 incomplete type. */
9159
9160 static void
9161 compute_delayed_physnames (struct dwarf2_cu *cu)
9162 {
9163 /* Only C++ delays computing physnames. */
9164 if (cu->method_list.empty ())
9165 return;
9166 gdb_assert (cu->language == language_cplus);
9167
9168 for (const delayed_method_info &mi : cu->method_list)
9169 {
9170 const char *physname;
9171 struct fn_fieldlist *fn_flp
9172 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9173 physname = dwarf2_physname (mi.name, mi.die, cu);
9174 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9175 = physname ? physname : "";
9176
9177 /* Since there's no tag to indicate whether a method is a
9178 const/volatile overload, extract that information out of the
9179 demangled name. */
9180 if (physname != NULL)
9181 {
9182 size_t len = strlen (physname);
9183
9184 while (1)
9185 {
9186 if (physname[len] == ')') /* shortcut */
9187 break;
9188 else if (check_modifier (physname, len, " const"))
9189 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9190 else if (check_modifier (physname, len, " volatile"))
9191 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9192 else
9193 break;
9194 }
9195 }
9196 }
9197
9198 /* The list is no longer needed. */
9199 cu->method_list.clear ();
9200 }
9201
9202 /* Go objects should be embedded in a DW_TAG_module DIE,
9203 and it's not clear if/how imported objects will appear.
9204 To keep Go support simple until that's worked out,
9205 go back through what we've read and create something usable.
9206 We could do this while processing each DIE, and feels kinda cleaner,
9207 but that way is more invasive.
9208 This is to, for example, allow the user to type "p var" or "b main"
9209 without having to specify the package name, and allow lookups
9210 of module.object to work in contexts that use the expression
9211 parser. */
9212
9213 static void
9214 fixup_go_packaging (struct dwarf2_cu *cu)
9215 {
9216 gdb::unique_xmalloc_ptr<char> package_name;
9217 struct pending *list;
9218 int i;
9219
9220 for (list = *cu->get_builder ()->get_global_symbols ();
9221 list != NULL;
9222 list = list->next)
9223 {
9224 for (i = 0; i < list->nsyms; ++i)
9225 {
9226 struct symbol *sym = list->symbol[i];
9227
9228 if (sym->language () == language_go
9229 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9230 {
9231 gdb::unique_xmalloc_ptr<char> this_package_name
9232 (go_symbol_package_name (sym));
9233
9234 if (this_package_name == NULL)
9235 continue;
9236 if (package_name == NULL)
9237 package_name = std::move (this_package_name);
9238 else
9239 {
9240 struct objfile *objfile
9241 = cu->per_cu->dwarf2_per_objfile->objfile;
9242 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9243 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9244 (symbol_symtab (sym) != NULL
9245 ? symtab_to_filename_for_display
9246 (symbol_symtab (sym))
9247 : objfile_name (objfile)),
9248 this_package_name.get (), package_name.get ());
9249 }
9250 }
9251 }
9252 }
9253
9254 if (package_name != NULL)
9255 {
9256 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9257 const char *saved_package_name = objfile->intern (package_name.get ());
9258 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9259 saved_package_name);
9260 struct symbol *sym;
9261
9262 sym = new (&objfile->objfile_obstack) symbol;
9263 sym->set_language (language_go, &objfile->objfile_obstack);
9264 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9265 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9266 e.g., "main" finds the "main" module and not C's main(). */
9267 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9268 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9269 SYMBOL_TYPE (sym) = type;
9270
9271 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9272 }
9273 }
9274
9275 /* Allocate a fully-qualified name consisting of the two parts on the
9276 obstack. */
9277
9278 static const char *
9279 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9280 {
9281 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9282 }
9283
9284 /* A helper that allocates a variant part to attach to a Rust enum
9285 type. OBSTACK is where the results should be allocated. TYPE is
9286 the type we're processing. DISCRIMINANT_INDEX is the index of the
9287 discriminant. It must be the index of one of the fields of TYPE.
9288 DEFAULT_INDEX is the index of the default field; or -1 if there is
9289 no default. RANGES is indexed by "effective" field number (the
9290 field index, but omitting the discriminant and default fields) and
9291 must hold the discriminant values used by the variants. Note that
9292 RANGES must have a lifetime at least as long as OBSTACK -- either
9293 already allocated on it, or static. */
9294
9295 static void
9296 alloc_rust_variant (struct obstack *obstack, struct type *type,
9297 int discriminant_index, int default_index,
9298 gdb::array_view<discriminant_range> ranges)
9299 {
9300 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9301 must be handled by the caller. */
9302 gdb_assert (discriminant_index >= 0
9303 && discriminant_index < type->num_fields ());
9304 gdb_assert (default_index == -1
9305 || (default_index >= 0 && default_index < type->num_fields ()));
9306
9307 /* We have one variant for each non-discriminant field. */
9308 int n_variants = type->num_fields () - 1;
9309
9310 variant *variants = new (obstack) variant[n_variants];
9311 int var_idx = 0;
9312 int range_idx = 0;
9313 for (int i = 0; i < type->num_fields (); ++i)
9314 {
9315 if (i == discriminant_index)
9316 continue;
9317
9318 variants[var_idx].first_field = i;
9319 variants[var_idx].last_field = i + 1;
9320
9321 /* The default field does not need a range, but other fields do.
9322 We skipped the discriminant above. */
9323 if (i != default_index)
9324 {
9325 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9326 ++range_idx;
9327 }
9328
9329 ++var_idx;
9330 }
9331
9332 gdb_assert (range_idx == ranges.size ());
9333 gdb_assert (var_idx == n_variants);
9334
9335 variant_part *part = new (obstack) variant_part;
9336 part->discriminant_index = discriminant_index;
9337 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9338 discriminant_index));
9339 part->variants = gdb::array_view<variant> (variants, n_variants);
9340
9341 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9342 gdb::array_view<variant_part> *prop_value
9343 = new (storage) gdb::array_view<variant_part> (part, 1);
9344
9345 struct dynamic_prop prop;
9346 prop.kind = PROP_VARIANT_PARTS;
9347 prop.data.variant_parts = prop_value;
9348
9349 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9350 }
9351
9352 /* Some versions of rustc emitted enums in an unusual way.
9353
9354 Ordinary enums were emitted as unions. The first element of each
9355 structure in the union was named "RUST$ENUM$DISR". This element
9356 held the discriminant.
9357
9358 These versions of Rust also implemented the "non-zero"
9359 optimization. When the enum had two values, and one is empty and
9360 the other holds a pointer that cannot be zero, the pointer is used
9361 as the discriminant, with a zero value meaning the empty variant.
9362 Here, the union's first member is of the form
9363 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9364 where the fieldnos are the indices of the fields that should be
9365 traversed in order to find the field (which may be several fields deep)
9366 and the variantname is the name of the variant of the case when the
9367 field is zero.
9368
9369 This function recognizes whether TYPE is of one of these forms,
9370 and, if so, smashes it to be a variant type. */
9371
9372 static void
9373 quirk_rust_enum (struct type *type, struct objfile *objfile)
9374 {
9375 gdb_assert (type->code () == TYPE_CODE_UNION);
9376
9377 /* We don't need to deal with empty enums. */
9378 if (type->num_fields () == 0)
9379 return;
9380
9381 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9382 if (type->num_fields () == 1
9383 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9384 {
9385 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9386
9387 /* Decode the field name to find the offset of the
9388 discriminant. */
9389 ULONGEST bit_offset = 0;
9390 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9391 while (name[0] >= '0' && name[0] <= '9')
9392 {
9393 char *tail;
9394 unsigned long index = strtoul (name, &tail, 10);
9395 name = tail;
9396 if (*name != '$'
9397 || index >= field_type->num_fields ()
9398 || (TYPE_FIELD_LOC_KIND (field_type, index)
9399 != FIELD_LOC_KIND_BITPOS))
9400 {
9401 complaint (_("Could not parse Rust enum encoding string \"%s\""
9402 "[in module %s]"),
9403 TYPE_FIELD_NAME (type, 0),
9404 objfile_name (objfile));
9405 return;
9406 }
9407 ++name;
9408
9409 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9410 field_type = TYPE_FIELD_TYPE (field_type, index);
9411 }
9412
9413 /* Smash this type to be a structure type. We have to do this
9414 because the type has already been recorded. */
9415 type->set_code (TYPE_CODE_STRUCT);
9416 type->set_num_fields (3);
9417 /* Save the field we care about. */
9418 struct field saved_field = type->field (0);
9419 type->set_fields
9420 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9421
9422 /* Put the discriminant at index 0. */
9423 TYPE_FIELD_TYPE (type, 0) = field_type;
9424 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9425 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9426 SET_FIELD_BITPOS (type->field (0), bit_offset);
9427
9428 /* The order of fields doesn't really matter, so put the real
9429 field at index 1 and the data-less field at index 2. */
9430 type->field (1) = saved_field;
9431 TYPE_FIELD_NAME (type, 1)
9432 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9433 TYPE_FIELD_TYPE (type, 1)->set_name
9434 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9435 TYPE_FIELD_NAME (type, 1)));
9436
9437 const char *dataless_name
9438 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9439 name);
9440 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9441 dataless_name);
9442 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9443 /* NAME points into the original discriminant name, which
9444 already has the correct lifetime. */
9445 TYPE_FIELD_NAME (type, 2) = name;
9446 SET_FIELD_BITPOS (type->field (2), 0);
9447
9448 /* Indicate that this is a variant type. */
9449 static discriminant_range ranges[1] = { { 0, 0 } };
9450 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9451 }
9452 /* A union with a single anonymous field is probably an old-style
9453 univariant enum. */
9454 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9455 {
9456 /* Smash this type to be a structure type. We have to do this
9457 because the type has already been recorded. */
9458 type->set_code (TYPE_CODE_STRUCT);
9459
9460 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9461 const char *variant_name
9462 = rust_last_path_segment (field_type->name ());
9463 TYPE_FIELD_NAME (type, 0) = variant_name;
9464 field_type->set_name
9465 (rust_fully_qualify (&objfile->objfile_obstack,
9466 type->name (), variant_name));
9467 }
9468 else
9469 {
9470 struct type *disr_type = nullptr;
9471 for (int i = 0; i < type->num_fields (); ++i)
9472 {
9473 disr_type = TYPE_FIELD_TYPE (type, i);
9474
9475 if (disr_type->code () != TYPE_CODE_STRUCT)
9476 {
9477 /* All fields of a true enum will be structs. */
9478 return;
9479 }
9480 else if (disr_type->num_fields () == 0)
9481 {
9482 /* Could be data-less variant, so keep going. */
9483 disr_type = nullptr;
9484 }
9485 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9486 "RUST$ENUM$DISR") != 0)
9487 {
9488 /* Not a Rust enum. */
9489 return;
9490 }
9491 else
9492 {
9493 /* Found one. */
9494 break;
9495 }
9496 }
9497
9498 /* If we got here without a discriminant, then it's probably
9499 just a union. */
9500 if (disr_type == nullptr)
9501 return;
9502
9503 /* Smash this type to be a structure type. We have to do this
9504 because the type has already been recorded. */
9505 type->set_code (TYPE_CODE_STRUCT);
9506
9507 /* Make space for the discriminant field. */
9508 struct field *disr_field = &disr_type->field (0);
9509 field *new_fields
9510 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9511 * sizeof (struct field)));
9512 memcpy (new_fields + 1, type->fields (),
9513 type->num_fields () * sizeof (struct field));
9514 type->set_fields (new_fields);
9515 type->set_num_fields (type->num_fields () + 1);
9516
9517 /* Install the discriminant at index 0 in the union. */
9518 type->field (0) = *disr_field;
9519 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9520 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9521
9522 /* We need a way to find the correct discriminant given a
9523 variant name. For convenience we build a map here. */
9524 struct type *enum_type = FIELD_TYPE (*disr_field);
9525 std::unordered_map<std::string, ULONGEST> discriminant_map;
9526 for (int i = 0; i < enum_type->num_fields (); ++i)
9527 {
9528 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9529 {
9530 const char *name
9531 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9532 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9533 }
9534 }
9535
9536 int n_fields = type->num_fields ();
9537 /* We don't need a range entry for the discriminant, but we do
9538 need one for every other field, as there is no default
9539 variant. */
9540 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9541 discriminant_range,
9542 n_fields - 1);
9543 /* Skip the discriminant here. */
9544 for (int i = 1; i < n_fields; ++i)
9545 {
9546 /* Find the final word in the name of this variant's type.
9547 That name can be used to look up the correct
9548 discriminant. */
9549 const char *variant_name
9550 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9551
9552 auto iter = discriminant_map.find (variant_name);
9553 if (iter != discriminant_map.end ())
9554 {
9555 ranges[i].low = iter->second;
9556 ranges[i].high = iter->second;
9557 }
9558
9559 /* Remove the discriminant field, if it exists. */
9560 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9561 if (sub_type->num_fields () > 0)
9562 {
9563 sub_type->set_num_fields (sub_type->num_fields () - 1);
9564 sub_type->set_fields (sub_type->fields () + 1);
9565 }
9566 TYPE_FIELD_NAME (type, i) = variant_name;
9567 sub_type->set_name
9568 (rust_fully_qualify (&objfile->objfile_obstack,
9569 type->name (), variant_name));
9570 }
9571
9572 /* Indicate that this is a variant type. */
9573 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9574 gdb::array_view<discriminant_range> (ranges,
9575 n_fields - 1));
9576 }
9577 }
9578
9579 /* Rewrite some Rust unions to be structures with variants parts. */
9580
9581 static void
9582 rust_union_quirks (struct dwarf2_cu *cu)
9583 {
9584 gdb_assert (cu->language == language_rust);
9585 for (type *type_ : cu->rust_unions)
9586 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9587 /* We don't need this any more. */
9588 cu->rust_unions.clear ();
9589 }
9590
9591 /* A helper function for computing the list of all symbol tables
9592 included by PER_CU. */
9593
9594 static void
9595 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9596 htab_t all_children, htab_t all_type_symtabs,
9597 struct dwarf2_per_cu_data *per_cu,
9598 struct compunit_symtab *immediate_parent)
9599 {
9600 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9601 if (*slot != NULL)
9602 {
9603 /* This inclusion and its children have been processed. */
9604 return;
9605 }
9606
9607 *slot = per_cu;
9608
9609 /* Only add a CU if it has a symbol table. */
9610 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
9611 if (cust != NULL)
9612 {
9613 /* If this is a type unit only add its symbol table if we haven't
9614 seen it yet (type unit per_cu's can share symtabs). */
9615 if (per_cu->is_debug_types)
9616 {
9617 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9618 if (*slot == NULL)
9619 {
9620 *slot = cust;
9621 result->push_back (cust);
9622 if (cust->user == NULL)
9623 cust->user = immediate_parent;
9624 }
9625 }
9626 else
9627 {
9628 result->push_back (cust);
9629 if (cust->user == NULL)
9630 cust->user = immediate_parent;
9631 }
9632 }
9633
9634 if (!per_cu->imported_symtabs_empty ())
9635 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9636 {
9637 recursively_compute_inclusions (result, all_children,
9638 all_type_symtabs, ptr, cust);
9639 }
9640 }
9641
9642 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9643 PER_CU. */
9644
9645 static void
9646 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9647 {
9648 gdb_assert (! per_cu->is_debug_types);
9649
9650 if (!per_cu->imported_symtabs_empty ())
9651 {
9652 int len;
9653 std::vector<compunit_symtab *> result_symtabs;
9654 htab_t all_children, all_type_symtabs;
9655 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
9656
9657 /* If we don't have a symtab, we can just skip this case. */
9658 if (cust == NULL)
9659 return;
9660
9661 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9662 NULL, xcalloc, xfree);
9663 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9664 NULL, xcalloc, xfree);
9665
9666 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9667 {
9668 recursively_compute_inclusions (&result_symtabs, all_children,
9669 all_type_symtabs, ptr, cust);
9670 }
9671
9672 /* Now we have a transitive closure of all the included symtabs. */
9673 len = result_symtabs.size ();
9674 cust->includes
9675 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9676 struct compunit_symtab *, len + 1);
9677 memcpy (cust->includes, result_symtabs.data (),
9678 len * sizeof (compunit_symtab *));
9679 cust->includes[len] = NULL;
9680
9681 htab_delete (all_children);
9682 htab_delete (all_type_symtabs);
9683 }
9684 }
9685
9686 /* Compute the 'includes' field for the symtabs of all the CUs we just
9687 read. */
9688
9689 static void
9690 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9691 {
9692 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9693 {
9694 if (! iter->is_debug_types)
9695 compute_compunit_symtab_includes (iter);
9696 }
9697
9698 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9699 }
9700
9701 /* Generate full symbol information for PER_CU, whose DIEs have
9702 already been loaded into memory. */
9703
9704 static void
9705 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9706 enum language pretend_language)
9707 {
9708 struct dwarf2_cu *cu = per_cu->cu;
9709 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9710 struct objfile *objfile = dwarf2_per_objfile->objfile;
9711 struct gdbarch *gdbarch = objfile->arch ();
9712 CORE_ADDR lowpc, highpc;
9713 struct compunit_symtab *cust;
9714 CORE_ADDR baseaddr;
9715 struct block *static_block;
9716 CORE_ADDR addr;
9717
9718 baseaddr = objfile->text_section_offset ();
9719
9720 /* Clear the list here in case something was left over. */
9721 cu->method_list.clear ();
9722
9723 cu->language = pretend_language;
9724 cu->language_defn = language_def (cu->language);
9725
9726 /* Do line number decoding in read_file_scope () */
9727 process_die (cu->dies, cu);
9728
9729 /* For now fudge the Go package. */
9730 if (cu->language == language_go)
9731 fixup_go_packaging (cu);
9732
9733 /* Now that we have processed all the DIEs in the CU, all the types
9734 should be complete, and it should now be safe to compute all of the
9735 physnames. */
9736 compute_delayed_physnames (cu);
9737
9738 if (cu->language == language_rust)
9739 rust_union_quirks (cu);
9740
9741 /* Some compilers don't define a DW_AT_high_pc attribute for the
9742 compilation unit. If the DW_AT_high_pc is missing, synthesize
9743 it, by scanning the DIE's below the compilation unit. */
9744 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9745
9746 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9747 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9748
9749 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9750 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9751 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9752 addrmap to help ensure it has an accurate map of pc values belonging to
9753 this comp unit. */
9754 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9755
9756 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9757 SECT_OFF_TEXT (objfile),
9758 0);
9759
9760 if (cust != NULL)
9761 {
9762 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9763
9764 /* Set symtab language to language from DW_AT_language. If the
9765 compilation is from a C file generated by language preprocessors, do
9766 not set the language if it was already deduced by start_subfile. */
9767 if (!(cu->language == language_c
9768 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9769 COMPUNIT_FILETABS (cust)->language = cu->language;
9770
9771 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9772 produce DW_AT_location with location lists but it can be possibly
9773 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9774 there were bugs in prologue debug info, fixed later in GCC-4.5
9775 by "unwind info for epilogues" patch (which is not directly related).
9776
9777 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9778 needed, it would be wrong due to missing DW_AT_producer there.
9779
9780 Still one can confuse GDB by using non-standard GCC compilation
9781 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9782 */
9783 if (cu->has_loclist && gcc_4_minor >= 5)
9784 cust->locations_valid = 1;
9785
9786 if (gcc_4_minor >= 5)
9787 cust->epilogue_unwind_valid = 1;
9788
9789 cust->call_site_htab = cu->call_site_htab;
9790 }
9791
9792 dwarf2_per_objfile->set_symtab (per_cu, cust);
9793
9794 /* Push it for inclusion processing later. */
9795 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9796
9797 /* Not needed any more. */
9798 cu->reset_builder ();
9799 }
9800
9801 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9802 already been loaded into memory. */
9803
9804 static void
9805 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9806 enum language pretend_language)
9807 {
9808 struct dwarf2_cu *cu = per_cu->cu;
9809 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9810 struct objfile *objfile = dwarf2_per_objfile->objfile;
9811 struct compunit_symtab *cust;
9812 struct signatured_type *sig_type;
9813
9814 gdb_assert (per_cu->is_debug_types);
9815 sig_type = (struct signatured_type *) per_cu;
9816
9817 /* Clear the list here in case something was left over. */
9818 cu->method_list.clear ();
9819
9820 cu->language = pretend_language;
9821 cu->language_defn = language_def (cu->language);
9822
9823 /* The symbol tables are set up in read_type_unit_scope. */
9824 process_die (cu->dies, cu);
9825
9826 /* For now fudge the Go package. */
9827 if (cu->language == language_go)
9828 fixup_go_packaging (cu);
9829
9830 /* Now that we have processed all the DIEs in the CU, all the types
9831 should be complete, and it should now be safe to compute all of the
9832 physnames. */
9833 compute_delayed_physnames (cu);
9834
9835 if (cu->language == language_rust)
9836 rust_union_quirks (cu);
9837
9838 /* TUs share symbol tables.
9839 If this is the first TU to use this symtab, complete the construction
9840 of it with end_expandable_symtab. Otherwise, complete the addition of
9841 this TU's symbols to the existing symtab. */
9842 if (sig_type->type_unit_group->compunit_symtab == NULL)
9843 {
9844 buildsym_compunit *builder = cu->get_builder ();
9845 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9846 sig_type->type_unit_group->compunit_symtab = cust;
9847
9848 if (cust != NULL)
9849 {
9850 /* Set symtab language to language from DW_AT_language. If the
9851 compilation is from a C file generated by language preprocessors,
9852 do not set the language if it was already deduced by
9853 start_subfile. */
9854 if (!(cu->language == language_c
9855 && COMPUNIT_FILETABS (cust)->language != language_c))
9856 COMPUNIT_FILETABS (cust)->language = cu->language;
9857 }
9858 }
9859 else
9860 {
9861 cu->get_builder ()->augment_type_symtab ();
9862 cust = sig_type->type_unit_group->compunit_symtab;
9863 }
9864
9865 dwarf2_per_objfile->set_symtab (per_cu, cust);
9866
9867 /* Not needed any more. */
9868 cu->reset_builder ();
9869 }
9870
9871 /* Process an imported unit DIE. */
9872
9873 static void
9874 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9875 {
9876 struct attribute *attr;
9877
9878 /* For now we don't handle imported units in type units. */
9879 if (cu->per_cu->is_debug_types)
9880 {
9881 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9882 " supported in type units [in module %s]"),
9883 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9884 }
9885
9886 attr = dwarf2_attr (die, DW_AT_import, cu);
9887 if (attr != NULL)
9888 {
9889 sect_offset sect_off = attr->get_ref_die_offset ();
9890 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9891 dwarf2_per_cu_data *per_cu
9892 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9893 cu->per_cu->dwarf2_per_objfile);
9894
9895 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9896 into another compilation unit, at root level. Regard this as a hint,
9897 and ignore it. */
9898 if (die->parent && die->parent->parent == NULL
9899 && per_cu->unit_type == DW_UT_compile
9900 && per_cu->lang == language_cplus)
9901 return;
9902
9903 /* If necessary, add it to the queue and load its DIEs. */
9904 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9905 load_full_comp_unit (per_cu, false, cu->language);
9906
9907 cu->per_cu->imported_symtabs_push (per_cu);
9908 }
9909 }
9910
9911 /* RAII object that represents a process_die scope: i.e.,
9912 starts/finishes processing a DIE. */
9913 class process_die_scope
9914 {
9915 public:
9916 process_die_scope (die_info *die, dwarf2_cu *cu)
9917 : m_die (die), m_cu (cu)
9918 {
9919 /* We should only be processing DIEs not already in process. */
9920 gdb_assert (!m_die->in_process);
9921 m_die->in_process = true;
9922 }
9923
9924 ~process_die_scope ()
9925 {
9926 m_die->in_process = false;
9927
9928 /* If we're done processing the DIE for the CU that owns the line
9929 header, we don't need the line header anymore. */
9930 if (m_cu->line_header_die_owner == m_die)
9931 {
9932 delete m_cu->line_header;
9933 m_cu->line_header = NULL;
9934 m_cu->line_header_die_owner = NULL;
9935 }
9936 }
9937
9938 private:
9939 die_info *m_die;
9940 dwarf2_cu *m_cu;
9941 };
9942
9943 /* Process a die and its children. */
9944
9945 static void
9946 process_die (struct die_info *die, struct dwarf2_cu *cu)
9947 {
9948 process_die_scope scope (die, cu);
9949
9950 switch (die->tag)
9951 {
9952 case DW_TAG_padding:
9953 break;
9954 case DW_TAG_compile_unit:
9955 case DW_TAG_partial_unit:
9956 read_file_scope (die, cu);
9957 break;
9958 case DW_TAG_type_unit:
9959 read_type_unit_scope (die, cu);
9960 break;
9961 case DW_TAG_subprogram:
9962 /* Nested subprograms in Fortran get a prefix. */
9963 if (cu->language == language_fortran
9964 && die->parent != NULL
9965 && die->parent->tag == DW_TAG_subprogram)
9966 cu->processing_has_namespace_info = true;
9967 /* Fall through. */
9968 case DW_TAG_inlined_subroutine:
9969 read_func_scope (die, cu);
9970 break;
9971 case DW_TAG_lexical_block:
9972 case DW_TAG_try_block:
9973 case DW_TAG_catch_block:
9974 read_lexical_block_scope (die, cu);
9975 break;
9976 case DW_TAG_call_site:
9977 case DW_TAG_GNU_call_site:
9978 read_call_site_scope (die, cu);
9979 break;
9980 case DW_TAG_class_type:
9981 case DW_TAG_interface_type:
9982 case DW_TAG_structure_type:
9983 case DW_TAG_union_type:
9984 process_structure_scope (die, cu);
9985 break;
9986 case DW_TAG_enumeration_type:
9987 process_enumeration_scope (die, cu);
9988 break;
9989
9990 /* These dies have a type, but processing them does not create
9991 a symbol or recurse to process the children. Therefore we can
9992 read them on-demand through read_type_die. */
9993 case DW_TAG_subroutine_type:
9994 case DW_TAG_set_type:
9995 case DW_TAG_array_type:
9996 case DW_TAG_pointer_type:
9997 case DW_TAG_ptr_to_member_type:
9998 case DW_TAG_reference_type:
9999 case DW_TAG_rvalue_reference_type:
10000 case DW_TAG_string_type:
10001 break;
10002
10003 case DW_TAG_base_type:
10004 case DW_TAG_subrange_type:
10005 case DW_TAG_typedef:
10006 /* Add a typedef symbol for the type definition, if it has a
10007 DW_AT_name. */
10008 new_symbol (die, read_type_die (die, cu), cu);
10009 break;
10010 case DW_TAG_common_block:
10011 read_common_block (die, cu);
10012 break;
10013 case DW_TAG_common_inclusion:
10014 break;
10015 case DW_TAG_namespace:
10016 cu->processing_has_namespace_info = true;
10017 read_namespace (die, cu);
10018 break;
10019 case DW_TAG_module:
10020 cu->processing_has_namespace_info = true;
10021 read_module (die, cu);
10022 break;
10023 case DW_TAG_imported_declaration:
10024 cu->processing_has_namespace_info = true;
10025 if (read_namespace_alias (die, cu))
10026 break;
10027 /* The declaration is not a global namespace alias. */
10028 /* Fall through. */
10029 case DW_TAG_imported_module:
10030 cu->processing_has_namespace_info = true;
10031 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10032 || cu->language != language_fortran))
10033 complaint (_("Tag '%s' has unexpected children"),
10034 dwarf_tag_name (die->tag));
10035 read_import_statement (die, cu);
10036 break;
10037
10038 case DW_TAG_imported_unit:
10039 process_imported_unit_die (die, cu);
10040 break;
10041
10042 case DW_TAG_variable:
10043 read_variable (die, cu);
10044 break;
10045
10046 default:
10047 new_symbol (die, NULL, cu);
10048 break;
10049 }
10050 }
10051 \f
10052 /* DWARF name computation. */
10053
10054 /* A helper function for dwarf2_compute_name which determines whether DIE
10055 needs to have the name of the scope prepended to the name listed in the
10056 die. */
10057
10058 static int
10059 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10060 {
10061 struct attribute *attr;
10062
10063 switch (die->tag)
10064 {
10065 case DW_TAG_namespace:
10066 case DW_TAG_typedef:
10067 case DW_TAG_class_type:
10068 case DW_TAG_interface_type:
10069 case DW_TAG_structure_type:
10070 case DW_TAG_union_type:
10071 case DW_TAG_enumeration_type:
10072 case DW_TAG_enumerator:
10073 case DW_TAG_subprogram:
10074 case DW_TAG_inlined_subroutine:
10075 case DW_TAG_member:
10076 case DW_TAG_imported_declaration:
10077 return 1;
10078
10079 case DW_TAG_variable:
10080 case DW_TAG_constant:
10081 /* We only need to prefix "globally" visible variables. These include
10082 any variable marked with DW_AT_external or any variable that
10083 lives in a namespace. [Variables in anonymous namespaces
10084 require prefixing, but they are not DW_AT_external.] */
10085
10086 if (dwarf2_attr (die, DW_AT_specification, cu))
10087 {
10088 struct dwarf2_cu *spec_cu = cu;
10089
10090 return die_needs_namespace (die_specification (die, &spec_cu),
10091 spec_cu);
10092 }
10093
10094 attr = dwarf2_attr (die, DW_AT_external, cu);
10095 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10096 && die->parent->tag != DW_TAG_module)
10097 return 0;
10098 /* A variable in a lexical block of some kind does not need a
10099 namespace, even though in C++ such variables may be external
10100 and have a mangled name. */
10101 if (die->parent->tag == DW_TAG_lexical_block
10102 || die->parent->tag == DW_TAG_try_block
10103 || die->parent->tag == DW_TAG_catch_block
10104 || die->parent->tag == DW_TAG_subprogram)
10105 return 0;
10106 return 1;
10107
10108 default:
10109 return 0;
10110 }
10111 }
10112
10113 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10114 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10115 defined for the given DIE. */
10116
10117 static struct attribute *
10118 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10119 {
10120 struct attribute *attr;
10121
10122 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10123 if (attr == NULL)
10124 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10125
10126 return attr;
10127 }
10128
10129 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10130 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10131 defined for the given DIE. */
10132
10133 static const char *
10134 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10135 {
10136 const char *linkage_name;
10137
10138 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10139 if (linkage_name == NULL)
10140 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10141
10142 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10143 See https://github.com/rust-lang/rust/issues/32925. */
10144 if (cu->language == language_rust && linkage_name != NULL
10145 && strchr (linkage_name, '{') != NULL)
10146 linkage_name = NULL;
10147
10148 return linkage_name;
10149 }
10150
10151 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10152 compute the physname for the object, which include a method's:
10153 - formal parameters (C++),
10154 - receiver type (Go),
10155
10156 The term "physname" is a bit confusing.
10157 For C++, for example, it is the demangled name.
10158 For Go, for example, it's the mangled name.
10159
10160 For Ada, return the DIE's linkage name rather than the fully qualified
10161 name. PHYSNAME is ignored..
10162
10163 The result is allocated on the objfile->per_bfd's obstack and
10164 canonicalized. */
10165
10166 static const char *
10167 dwarf2_compute_name (const char *name,
10168 struct die_info *die, struct dwarf2_cu *cu,
10169 int physname)
10170 {
10171 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10172
10173 if (name == NULL)
10174 name = dwarf2_name (die, cu);
10175
10176 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10177 but otherwise compute it by typename_concat inside GDB.
10178 FIXME: Actually this is not really true, or at least not always true.
10179 It's all very confusing. compute_and_set_names doesn't try to demangle
10180 Fortran names because there is no mangling standard. So new_symbol
10181 will set the demangled name to the result of dwarf2_full_name, and it is
10182 the demangled name that GDB uses if it exists. */
10183 if (cu->language == language_ada
10184 || (cu->language == language_fortran && physname))
10185 {
10186 /* For Ada unit, we prefer the linkage name over the name, as
10187 the former contains the exported name, which the user expects
10188 to be able to reference. Ideally, we want the user to be able
10189 to reference this entity using either natural or linkage name,
10190 but we haven't started looking at this enhancement yet. */
10191 const char *linkage_name = dw2_linkage_name (die, cu);
10192
10193 if (linkage_name != NULL)
10194 return linkage_name;
10195 }
10196
10197 /* These are the only languages we know how to qualify names in. */
10198 if (name != NULL
10199 && (cu->language == language_cplus
10200 || cu->language == language_fortran || cu->language == language_d
10201 || cu->language == language_rust))
10202 {
10203 if (die_needs_namespace (die, cu))
10204 {
10205 const char *prefix;
10206 const char *canonical_name = NULL;
10207
10208 string_file buf;
10209
10210 prefix = determine_prefix (die, cu);
10211 if (*prefix != '\0')
10212 {
10213 gdb::unique_xmalloc_ptr<char> prefixed_name
10214 (typename_concat (NULL, prefix, name, physname, cu));
10215
10216 buf.puts (prefixed_name.get ());
10217 }
10218 else
10219 buf.puts (name);
10220
10221 /* Template parameters may be specified in the DIE's DW_AT_name, or
10222 as children with DW_TAG_template_type_param or
10223 DW_TAG_value_type_param. If the latter, add them to the name
10224 here. If the name already has template parameters, then
10225 skip this step; some versions of GCC emit both, and
10226 it is more efficient to use the pre-computed name.
10227
10228 Something to keep in mind about this process: it is very
10229 unlikely, or in some cases downright impossible, to produce
10230 something that will match the mangled name of a function.
10231 If the definition of the function has the same debug info,
10232 we should be able to match up with it anyway. But fallbacks
10233 using the minimal symbol, for instance to find a method
10234 implemented in a stripped copy of libstdc++, will not work.
10235 If we do not have debug info for the definition, we will have to
10236 match them up some other way.
10237
10238 When we do name matching there is a related problem with function
10239 templates; two instantiated function templates are allowed to
10240 differ only by their return types, which we do not add here. */
10241
10242 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10243 {
10244 struct attribute *attr;
10245 struct die_info *child;
10246 int first = 1;
10247
10248 die->building_fullname = 1;
10249
10250 for (child = die->child; child != NULL; child = child->sibling)
10251 {
10252 struct type *type;
10253 LONGEST value;
10254 const gdb_byte *bytes;
10255 struct dwarf2_locexpr_baton *baton;
10256 struct value *v;
10257
10258 if (child->tag != DW_TAG_template_type_param
10259 && child->tag != DW_TAG_template_value_param)
10260 continue;
10261
10262 if (first)
10263 {
10264 buf.puts ("<");
10265 first = 0;
10266 }
10267 else
10268 buf.puts (", ");
10269
10270 attr = dwarf2_attr (child, DW_AT_type, cu);
10271 if (attr == NULL)
10272 {
10273 complaint (_("template parameter missing DW_AT_type"));
10274 buf.puts ("UNKNOWN_TYPE");
10275 continue;
10276 }
10277 type = die_type (child, cu);
10278
10279 if (child->tag == DW_TAG_template_type_param)
10280 {
10281 c_print_type (type, "", &buf, -1, 0, cu->language,
10282 &type_print_raw_options);
10283 continue;
10284 }
10285
10286 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10287 if (attr == NULL)
10288 {
10289 complaint (_("template parameter missing "
10290 "DW_AT_const_value"));
10291 buf.puts ("UNKNOWN_VALUE");
10292 continue;
10293 }
10294
10295 dwarf2_const_value_attr (attr, type, name,
10296 &cu->comp_unit_obstack, cu,
10297 &value, &bytes, &baton);
10298
10299 if (TYPE_NOSIGN (type))
10300 /* GDB prints characters as NUMBER 'CHAR'. If that's
10301 changed, this can use value_print instead. */
10302 c_printchar (value, type, &buf);
10303 else
10304 {
10305 struct value_print_options opts;
10306
10307 if (baton != NULL)
10308 v = dwarf2_evaluate_loc_desc (type, NULL,
10309 baton->data,
10310 baton->size,
10311 baton->per_cu);
10312 else if (bytes != NULL)
10313 {
10314 v = allocate_value (type);
10315 memcpy (value_contents_writeable (v), bytes,
10316 TYPE_LENGTH (type));
10317 }
10318 else
10319 v = value_from_longest (type, value);
10320
10321 /* Specify decimal so that we do not depend on
10322 the radix. */
10323 get_formatted_print_options (&opts, 'd');
10324 opts.raw = 1;
10325 value_print (v, &buf, &opts);
10326 release_value (v);
10327 }
10328 }
10329
10330 die->building_fullname = 0;
10331
10332 if (!first)
10333 {
10334 /* Close the argument list, with a space if necessary
10335 (nested templates). */
10336 if (!buf.empty () && buf.string ().back () == '>')
10337 buf.puts (" >");
10338 else
10339 buf.puts (">");
10340 }
10341 }
10342
10343 /* For C++ methods, append formal parameter type
10344 information, if PHYSNAME. */
10345
10346 if (physname && die->tag == DW_TAG_subprogram
10347 && cu->language == language_cplus)
10348 {
10349 struct type *type = read_type_die (die, cu);
10350
10351 c_type_print_args (type, &buf, 1, cu->language,
10352 &type_print_raw_options);
10353
10354 if (cu->language == language_cplus)
10355 {
10356 /* Assume that an artificial first parameter is
10357 "this", but do not crash if it is not. RealView
10358 marks unnamed (and thus unused) parameters as
10359 artificial; there is no way to differentiate
10360 the two cases. */
10361 if (type->num_fields () > 0
10362 && TYPE_FIELD_ARTIFICIAL (type, 0)
10363 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10364 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10365 0))))
10366 buf.puts (" const");
10367 }
10368 }
10369
10370 const std::string &intermediate_name = buf.string ();
10371
10372 if (cu->language == language_cplus)
10373 canonical_name
10374 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10375 objfile);
10376
10377 /* If we only computed INTERMEDIATE_NAME, or if
10378 INTERMEDIATE_NAME is already canonical, then we need to
10379 intern it. */
10380 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10381 name = objfile->intern (intermediate_name);
10382 else
10383 name = canonical_name;
10384 }
10385 }
10386
10387 return name;
10388 }
10389
10390 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10391 If scope qualifiers are appropriate they will be added. The result
10392 will be allocated on the storage_obstack, or NULL if the DIE does
10393 not have a name. NAME may either be from a previous call to
10394 dwarf2_name or NULL.
10395
10396 The output string will be canonicalized (if C++). */
10397
10398 static const char *
10399 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10400 {
10401 return dwarf2_compute_name (name, die, cu, 0);
10402 }
10403
10404 /* Construct a physname for the given DIE in CU. NAME may either be
10405 from a previous call to dwarf2_name or NULL. The result will be
10406 allocated on the objfile_objstack or NULL if the DIE does not have a
10407 name.
10408
10409 The output string will be canonicalized (if C++). */
10410
10411 static const char *
10412 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10413 {
10414 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10415 const char *retval, *mangled = NULL, *canon = NULL;
10416 int need_copy = 1;
10417
10418 /* In this case dwarf2_compute_name is just a shortcut not building anything
10419 on its own. */
10420 if (!die_needs_namespace (die, cu))
10421 return dwarf2_compute_name (name, die, cu, 1);
10422
10423 if (cu->language != language_rust)
10424 mangled = dw2_linkage_name (die, cu);
10425
10426 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10427 has computed. */
10428 gdb::unique_xmalloc_ptr<char> demangled;
10429 if (mangled != NULL)
10430 {
10431
10432 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10433 {
10434 /* Do nothing (do not demangle the symbol name). */
10435 }
10436 else if (cu->language == language_go)
10437 {
10438 /* This is a lie, but we already lie to the caller new_symbol.
10439 new_symbol assumes we return the mangled name.
10440 This just undoes that lie until things are cleaned up. */
10441 }
10442 else
10443 {
10444 /* Use DMGL_RET_DROP for C++ template functions to suppress
10445 their return type. It is easier for GDB users to search
10446 for such functions as `name(params)' than `long name(params)'.
10447 In such case the minimal symbol names do not match the full
10448 symbol names but for template functions there is never a need
10449 to look up their definition from their declaration so
10450 the only disadvantage remains the minimal symbol variant
10451 `long name(params)' does not have the proper inferior type. */
10452 demangled.reset (gdb_demangle (mangled,
10453 (DMGL_PARAMS | DMGL_ANSI
10454 | DMGL_RET_DROP)));
10455 }
10456 if (demangled)
10457 canon = demangled.get ();
10458 else
10459 {
10460 canon = mangled;
10461 need_copy = 0;
10462 }
10463 }
10464
10465 if (canon == NULL || check_physname)
10466 {
10467 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10468
10469 if (canon != NULL && strcmp (physname, canon) != 0)
10470 {
10471 /* It may not mean a bug in GDB. The compiler could also
10472 compute DW_AT_linkage_name incorrectly. But in such case
10473 GDB would need to be bug-to-bug compatible. */
10474
10475 complaint (_("Computed physname <%s> does not match demangled <%s> "
10476 "(from linkage <%s>) - DIE at %s [in module %s]"),
10477 physname, canon, mangled, sect_offset_str (die->sect_off),
10478 objfile_name (objfile));
10479
10480 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10481 is available here - over computed PHYSNAME. It is safer
10482 against both buggy GDB and buggy compilers. */
10483
10484 retval = canon;
10485 }
10486 else
10487 {
10488 retval = physname;
10489 need_copy = 0;
10490 }
10491 }
10492 else
10493 retval = canon;
10494
10495 if (need_copy)
10496 retval = objfile->intern (retval);
10497
10498 return retval;
10499 }
10500
10501 /* Inspect DIE in CU for a namespace alias. If one exists, record
10502 a new symbol for it.
10503
10504 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10505
10506 static int
10507 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10508 {
10509 struct attribute *attr;
10510
10511 /* If the die does not have a name, this is not a namespace
10512 alias. */
10513 attr = dwarf2_attr (die, DW_AT_name, cu);
10514 if (attr != NULL)
10515 {
10516 int num;
10517 struct die_info *d = die;
10518 struct dwarf2_cu *imported_cu = cu;
10519
10520 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10521 keep inspecting DIEs until we hit the underlying import. */
10522 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10523 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10524 {
10525 attr = dwarf2_attr (d, DW_AT_import, cu);
10526 if (attr == NULL)
10527 break;
10528
10529 d = follow_die_ref (d, attr, &imported_cu);
10530 if (d->tag != DW_TAG_imported_declaration)
10531 break;
10532 }
10533
10534 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10535 {
10536 complaint (_("DIE at %s has too many recursively imported "
10537 "declarations"), sect_offset_str (d->sect_off));
10538 return 0;
10539 }
10540
10541 if (attr != NULL)
10542 {
10543 struct type *type;
10544 sect_offset sect_off = attr->get_ref_die_offset ();
10545
10546 type = get_die_type_at_offset (sect_off, cu->per_cu);
10547 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10548 {
10549 /* This declaration is a global namespace alias. Add
10550 a symbol for it whose type is the aliased namespace. */
10551 new_symbol (die, type, cu);
10552 return 1;
10553 }
10554 }
10555 }
10556
10557 return 0;
10558 }
10559
10560 /* Return the using directives repository (global or local?) to use in the
10561 current context for CU.
10562
10563 For Ada, imported declarations can materialize renamings, which *may* be
10564 global. However it is impossible (for now?) in DWARF to distinguish
10565 "external" imported declarations and "static" ones. As all imported
10566 declarations seem to be static in all other languages, make them all CU-wide
10567 global only in Ada. */
10568
10569 static struct using_direct **
10570 using_directives (struct dwarf2_cu *cu)
10571 {
10572 if (cu->language == language_ada
10573 && cu->get_builder ()->outermost_context_p ())
10574 return cu->get_builder ()->get_global_using_directives ();
10575 else
10576 return cu->get_builder ()->get_local_using_directives ();
10577 }
10578
10579 /* Read the import statement specified by the given die and record it. */
10580
10581 static void
10582 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10583 {
10584 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10585 struct attribute *import_attr;
10586 struct die_info *imported_die, *child_die;
10587 struct dwarf2_cu *imported_cu;
10588 const char *imported_name;
10589 const char *imported_name_prefix;
10590 const char *canonical_name;
10591 const char *import_alias;
10592 const char *imported_declaration = NULL;
10593 const char *import_prefix;
10594 std::vector<const char *> excludes;
10595
10596 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10597 if (import_attr == NULL)
10598 {
10599 complaint (_("Tag '%s' has no DW_AT_import"),
10600 dwarf_tag_name (die->tag));
10601 return;
10602 }
10603
10604 imported_cu = cu;
10605 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10606 imported_name = dwarf2_name (imported_die, imported_cu);
10607 if (imported_name == NULL)
10608 {
10609 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10610
10611 The import in the following code:
10612 namespace A
10613 {
10614 typedef int B;
10615 }
10616
10617 int main ()
10618 {
10619 using A::B;
10620 B b;
10621 return b;
10622 }
10623
10624 ...
10625 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10626 <52> DW_AT_decl_file : 1
10627 <53> DW_AT_decl_line : 6
10628 <54> DW_AT_import : <0x75>
10629 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10630 <59> DW_AT_name : B
10631 <5b> DW_AT_decl_file : 1
10632 <5c> DW_AT_decl_line : 2
10633 <5d> DW_AT_type : <0x6e>
10634 ...
10635 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10636 <76> DW_AT_byte_size : 4
10637 <77> DW_AT_encoding : 5 (signed)
10638
10639 imports the wrong die ( 0x75 instead of 0x58 ).
10640 This case will be ignored until the gcc bug is fixed. */
10641 return;
10642 }
10643
10644 /* Figure out the local name after import. */
10645 import_alias = dwarf2_name (die, cu);
10646
10647 /* Figure out where the statement is being imported to. */
10648 import_prefix = determine_prefix (die, cu);
10649
10650 /* Figure out what the scope of the imported die is and prepend it
10651 to the name of the imported die. */
10652 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10653
10654 if (imported_die->tag != DW_TAG_namespace
10655 && imported_die->tag != DW_TAG_module)
10656 {
10657 imported_declaration = imported_name;
10658 canonical_name = imported_name_prefix;
10659 }
10660 else if (strlen (imported_name_prefix) > 0)
10661 canonical_name = obconcat (&objfile->objfile_obstack,
10662 imported_name_prefix,
10663 (cu->language == language_d ? "." : "::"),
10664 imported_name, (char *) NULL);
10665 else
10666 canonical_name = imported_name;
10667
10668 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10669 for (child_die = die->child; child_die && child_die->tag;
10670 child_die = child_die->sibling)
10671 {
10672 /* DWARF-4: A Fortran use statement with a “rename list” may be
10673 represented by an imported module entry with an import attribute
10674 referring to the module and owned entries corresponding to those
10675 entities that are renamed as part of being imported. */
10676
10677 if (child_die->tag != DW_TAG_imported_declaration)
10678 {
10679 complaint (_("child DW_TAG_imported_declaration expected "
10680 "- DIE at %s [in module %s]"),
10681 sect_offset_str (child_die->sect_off),
10682 objfile_name (objfile));
10683 continue;
10684 }
10685
10686 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10687 if (import_attr == NULL)
10688 {
10689 complaint (_("Tag '%s' has no DW_AT_import"),
10690 dwarf_tag_name (child_die->tag));
10691 continue;
10692 }
10693
10694 imported_cu = cu;
10695 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10696 &imported_cu);
10697 imported_name = dwarf2_name (imported_die, imported_cu);
10698 if (imported_name == NULL)
10699 {
10700 complaint (_("child DW_TAG_imported_declaration has unknown "
10701 "imported name - DIE at %s [in module %s]"),
10702 sect_offset_str (child_die->sect_off),
10703 objfile_name (objfile));
10704 continue;
10705 }
10706
10707 excludes.push_back (imported_name);
10708
10709 process_die (child_die, cu);
10710 }
10711
10712 add_using_directive (using_directives (cu),
10713 import_prefix,
10714 canonical_name,
10715 import_alias,
10716 imported_declaration,
10717 excludes,
10718 0,
10719 &objfile->objfile_obstack);
10720 }
10721
10722 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10723 types, but gives them a size of zero. Starting with version 14,
10724 ICC is compatible with GCC. */
10725
10726 static bool
10727 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10728 {
10729 if (!cu->checked_producer)
10730 check_producer (cu);
10731
10732 return cu->producer_is_icc_lt_14;
10733 }
10734
10735 /* ICC generates a DW_AT_type for C void functions. This was observed on
10736 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10737 which says that void functions should not have a DW_AT_type. */
10738
10739 static bool
10740 producer_is_icc (struct dwarf2_cu *cu)
10741 {
10742 if (!cu->checked_producer)
10743 check_producer (cu);
10744
10745 return cu->producer_is_icc;
10746 }
10747
10748 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10749 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10750 this, it was first present in GCC release 4.3.0. */
10751
10752 static bool
10753 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10754 {
10755 if (!cu->checked_producer)
10756 check_producer (cu);
10757
10758 return cu->producer_is_gcc_lt_4_3;
10759 }
10760
10761 static file_and_directory
10762 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10763 {
10764 file_and_directory res;
10765
10766 /* Find the filename. Do not use dwarf2_name here, since the filename
10767 is not a source language identifier. */
10768 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10769 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10770
10771 if (res.comp_dir == NULL
10772 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10773 && IS_ABSOLUTE_PATH (res.name))
10774 {
10775 res.comp_dir_storage = ldirname (res.name);
10776 if (!res.comp_dir_storage.empty ())
10777 res.comp_dir = res.comp_dir_storage.c_str ();
10778 }
10779 if (res.comp_dir != NULL)
10780 {
10781 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10782 directory, get rid of it. */
10783 const char *cp = strchr (res.comp_dir, ':');
10784
10785 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10786 res.comp_dir = cp + 1;
10787 }
10788
10789 if (res.name == NULL)
10790 res.name = "<unknown>";
10791
10792 return res;
10793 }
10794
10795 /* Handle DW_AT_stmt_list for a compilation unit.
10796 DIE is the DW_TAG_compile_unit die for CU.
10797 COMP_DIR is the compilation directory. LOWPC is passed to
10798 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10799
10800 static void
10801 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10802 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10803 {
10804 struct dwarf2_per_objfile *dwarf2_per_objfile
10805 = cu->per_cu->dwarf2_per_objfile;
10806 struct attribute *attr;
10807 struct line_header line_header_local;
10808 hashval_t line_header_local_hash;
10809 void **slot;
10810 int decode_mapping;
10811
10812 gdb_assert (! cu->per_cu->is_debug_types);
10813
10814 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10815 if (attr == NULL)
10816 return;
10817
10818 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10819
10820 /* The line header hash table is only created if needed (it exists to
10821 prevent redundant reading of the line table for partial_units).
10822 If we're given a partial_unit, we'll need it. If we're given a
10823 compile_unit, then use the line header hash table if it's already
10824 created, but don't create one just yet. */
10825
10826 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10827 && die->tag == DW_TAG_partial_unit)
10828 {
10829 dwarf2_per_objfile->per_bfd->line_header_hash
10830 .reset (htab_create_alloc (127, line_header_hash_voidp,
10831 line_header_eq_voidp,
10832 free_line_header_voidp,
10833 xcalloc, xfree));
10834 }
10835
10836 line_header_local.sect_off = line_offset;
10837 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10838 line_header_local_hash = line_header_hash (&line_header_local);
10839 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10840 {
10841 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10842 &line_header_local,
10843 line_header_local_hash, NO_INSERT);
10844
10845 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10846 is not present in *SLOT (since if there is something in *SLOT then
10847 it will be for a partial_unit). */
10848 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10849 {
10850 gdb_assert (*slot != NULL);
10851 cu->line_header = (struct line_header *) *slot;
10852 return;
10853 }
10854 }
10855
10856 /* dwarf_decode_line_header does not yet provide sufficient information.
10857 We always have to call also dwarf_decode_lines for it. */
10858 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10859 if (lh == NULL)
10860 return;
10861
10862 cu->line_header = lh.release ();
10863 cu->line_header_die_owner = die;
10864
10865 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10866 slot = NULL;
10867 else
10868 {
10869 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10870 &line_header_local,
10871 line_header_local_hash, INSERT);
10872 gdb_assert (slot != NULL);
10873 }
10874 if (slot != NULL && *slot == NULL)
10875 {
10876 /* This newly decoded line number information unit will be owned
10877 by line_header_hash hash table. */
10878 *slot = cu->line_header;
10879 cu->line_header_die_owner = NULL;
10880 }
10881 else
10882 {
10883 /* We cannot free any current entry in (*slot) as that struct line_header
10884 may be already used by multiple CUs. Create only temporary decoded
10885 line_header for this CU - it may happen at most once for each line
10886 number information unit. And if we're not using line_header_hash
10887 then this is what we want as well. */
10888 gdb_assert (die->tag != DW_TAG_partial_unit);
10889 }
10890 decode_mapping = (die->tag != DW_TAG_partial_unit);
10891 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10892 decode_mapping);
10893
10894 }
10895
10896 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10897
10898 static void
10899 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10900 {
10901 struct dwarf2_per_objfile *dwarf2_per_objfile
10902 = cu->per_cu->dwarf2_per_objfile;
10903 struct objfile *objfile = dwarf2_per_objfile->objfile;
10904 struct gdbarch *gdbarch = objfile->arch ();
10905 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10906 CORE_ADDR highpc = ((CORE_ADDR) 0);
10907 struct attribute *attr;
10908 struct die_info *child_die;
10909 CORE_ADDR baseaddr;
10910
10911 prepare_one_comp_unit (cu, die, cu->language);
10912 baseaddr = objfile->text_section_offset ();
10913
10914 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10915
10916 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10917 from finish_block. */
10918 if (lowpc == ((CORE_ADDR) -1))
10919 lowpc = highpc;
10920 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10921
10922 file_and_directory fnd = find_file_and_directory (die, cu);
10923
10924 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10925 standardised yet. As a workaround for the language detection we fall
10926 back to the DW_AT_producer string. */
10927 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10928 cu->language = language_opencl;
10929
10930 /* Similar hack for Go. */
10931 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10932 set_cu_language (DW_LANG_Go, cu);
10933
10934 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10935
10936 /* Decode line number information if present. We do this before
10937 processing child DIEs, so that the line header table is available
10938 for DW_AT_decl_file. */
10939 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10940
10941 /* Process all dies in compilation unit. */
10942 if (die->child != NULL)
10943 {
10944 child_die = die->child;
10945 while (child_die && child_die->tag)
10946 {
10947 process_die (child_die, cu);
10948 child_die = child_die->sibling;
10949 }
10950 }
10951
10952 /* Decode macro information, if present. Dwarf 2 macro information
10953 refers to information in the line number info statement program
10954 header, so we can only read it if we've read the header
10955 successfully. */
10956 attr = dwarf2_attr (die, DW_AT_macros, cu);
10957 if (attr == NULL)
10958 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10959 if (attr && cu->line_header)
10960 {
10961 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10962 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10963
10964 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10965 }
10966 else
10967 {
10968 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10969 if (attr && cu->line_header)
10970 {
10971 unsigned int macro_offset = DW_UNSND (attr);
10972
10973 dwarf_decode_macros (cu, macro_offset, 0);
10974 }
10975 }
10976 }
10977
10978 void
10979 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10980 {
10981 struct type_unit_group *tu_group;
10982 int first_time;
10983 struct attribute *attr;
10984 unsigned int i;
10985 struct signatured_type *sig_type;
10986
10987 gdb_assert (per_cu->is_debug_types);
10988 sig_type = (struct signatured_type *) per_cu;
10989
10990 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10991
10992 /* If we're using .gdb_index (includes -readnow) then
10993 per_cu->type_unit_group may not have been set up yet. */
10994 if (sig_type->type_unit_group == NULL)
10995 sig_type->type_unit_group = get_type_unit_group (this, attr);
10996 tu_group = sig_type->type_unit_group;
10997
10998 /* If we've already processed this stmt_list there's no real need to
10999 do it again, we could fake it and just recreate the part we need
11000 (file name,index -> symtab mapping). If data shows this optimization
11001 is useful we can do it then. */
11002 first_time = tu_group->compunit_symtab == NULL;
11003
11004 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11005 debug info. */
11006 line_header_up lh;
11007 if (attr != NULL)
11008 {
11009 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11010 lh = dwarf_decode_line_header (line_offset, this);
11011 }
11012 if (lh == NULL)
11013 {
11014 if (first_time)
11015 start_symtab ("", NULL, 0);
11016 else
11017 {
11018 gdb_assert (tu_group->symtabs == NULL);
11019 gdb_assert (m_builder == nullptr);
11020 struct compunit_symtab *cust = tu_group->compunit_symtab;
11021 m_builder.reset (new struct buildsym_compunit
11022 (COMPUNIT_OBJFILE (cust), "",
11023 COMPUNIT_DIRNAME (cust),
11024 compunit_language (cust),
11025 0, cust));
11026 list_in_scope = get_builder ()->get_file_symbols ();
11027 }
11028 return;
11029 }
11030
11031 line_header = lh.release ();
11032 line_header_die_owner = die;
11033
11034 if (first_time)
11035 {
11036 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11037
11038 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11039 still initializing it, and our caller (a few levels up)
11040 process_full_type_unit still needs to know if this is the first
11041 time. */
11042
11043 tu_group->symtabs
11044 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11045 struct symtab *, line_header->file_names_size ());
11046
11047 auto &file_names = line_header->file_names ();
11048 for (i = 0; i < file_names.size (); ++i)
11049 {
11050 file_entry &fe = file_names[i];
11051 dwarf2_start_subfile (this, fe.name,
11052 fe.include_dir (line_header));
11053 buildsym_compunit *b = get_builder ();
11054 if (b->get_current_subfile ()->symtab == NULL)
11055 {
11056 /* NOTE: start_subfile will recognize when it's been
11057 passed a file it has already seen. So we can't
11058 assume there's a simple mapping from
11059 cu->line_header->file_names to subfiles, plus
11060 cu->line_header->file_names may contain dups. */
11061 b->get_current_subfile ()->symtab
11062 = allocate_symtab (cust, b->get_current_subfile ()->name);
11063 }
11064
11065 fe.symtab = b->get_current_subfile ()->symtab;
11066 tu_group->symtabs[i] = fe.symtab;
11067 }
11068 }
11069 else
11070 {
11071 gdb_assert (m_builder == nullptr);
11072 struct compunit_symtab *cust = tu_group->compunit_symtab;
11073 m_builder.reset (new struct buildsym_compunit
11074 (COMPUNIT_OBJFILE (cust), "",
11075 COMPUNIT_DIRNAME (cust),
11076 compunit_language (cust),
11077 0, cust));
11078 list_in_scope = get_builder ()->get_file_symbols ();
11079
11080 auto &file_names = line_header->file_names ();
11081 for (i = 0; i < file_names.size (); ++i)
11082 {
11083 file_entry &fe = file_names[i];
11084 fe.symtab = tu_group->symtabs[i];
11085 }
11086 }
11087
11088 /* The main symtab is allocated last. Type units don't have DW_AT_name
11089 so they don't have a "real" (so to speak) symtab anyway.
11090 There is later code that will assign the main symtab to all symbols
11091 that don't have one. We need to handle the case of a symbol with a
11092 missing symtab (DW_AT_decl_file) anyway. */
11093 }
11094
11095 /* Process DW_TAG_type_unit.
11096 For TUs we want to skip the first top level sibling if it's not the
11097 actual type being defined by this TU. In this case the first top
11098 level sibling is there to provide context only. */
11099
11100 static void
11101 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11102 {
11103 struct die_info *child_die;
11104
11105 prepare_one_comp_unit (cu, die, language_minimal);
11106
11107 /* Initialize (or reinitialize) the machinery for building symtabs.
11108 We do this before processing child DIEs, so that the line header table
11109 is available for DW_AT_decl_file. */
11110 cu->setup_type_unit_groups (die);
11111
11112 if (die->child != NULL)
11113 {
11114 child_die = die->child;
11115 while (child_die && child_die->tag)
11116 {
11117 process_die (child_die, cu);
11118 child_die = child_die->sibling;
11119 }
11120 }
11121 }
11122 \f
11123 /* DWO/DWP files.
11124
11125 http://gcc.gnu.org/wiki/DebugFission
11126 http://gcc.gnu.org/wiki/DebugFissionDWP
11127
11128 To simplify handling of both DWO files ("object" files with the DWARF info)
11129 and DWP files (a file with the DWOs packaged up into one file), we treat
11130 DWP files as having a collection of virtual DWO files. */
11131
11132 static hashval_t
11133 hash_dwo_file (const void *item)
11134 {
11135 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11136 hashval_t hash;
11137
11138 hash = htab_hash_string (dwo_file->dwo_name);
11139 if (dwo_file->comp_dir != NULL)
11140 hash += htab_hash_string (dwo_file->comp_dir);
11141 return hash;
11142 }
11143
11144 static int
11145 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11146 {
11147 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11148 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11149
11150 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11151 return 0;
11152 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11153 return lhs->comp_dir == rhs->comp_dir;
11154 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11155 }
11156
11157 /* Allocate a hash table for DWO files. */
11158
11159 static htab_up
11160 allocate_dwo_file_hash_table ()
11161 {
11162 auto delete_dwo_file = [] (void *item)
11163 {
11164 struct dwo_file *dwo_file = (struct dwo_file *) item;
11165
11166 delete dwo_file;
11167 };
11168
11169 return htab_up (htab_create_alloc (41,
11170 hash_dwo_file,
11171 eq_dwo_file,
11172 delete_dwo_file,
11173 xcalloc, xfree));
11174 }
11175
11176 /* Lookup DWO file DWO_NAME. */
11177
11178 static void **
11179 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11180 const char *dwo_name,
11181 const char *comp_dir)
11182 {
11183 struct dwo_file find_entry;
11184 void **slot;
11185
11186 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11187 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11188
11189 find_entry.dwo_name = dwo_name;
11190 find_entry.comp_dir = comp_dir;
11191 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11192 INSERT);
11193
11194 return slot;
11195 }
11196
11197 static hashval_t
11198 hash_dwo_unit (const void *item)
11199 {
11200 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11201
11202 /* This drops the top 32 bits of the id, but is ok for a hash. */
11203 return dwo_unit->signature;
11204 }
11205
11206 static int
11207 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11208 {
11209 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11210 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11211
11212 /* The signature is assumed to be unique within the DWO file.
11213 So while object file CU dwo_id's always have the value zero,
11214 that's OK, assuming each object file DWO file has only one CU,
11215 and that's the rule for now. */
11216 return lhs->signature == rhs->signature;
11217 }
11218
11219 /* Allocate a hash table for DWO CUs,TUs.
11220 There is one of these tables for each of CUs,TUs for each DWO file. */
11221
11222 static htab_up
11223 allocate_dwo_unit_table ()
11224 {
11225 /* Start out with a pretty small number.
11226 Generally DWO files contain only one CU and maybe some TUs. */
11227 return htab_up (htab_create_alloc (3,
11228 hash_dwo_unit,
11229 eq_dwo_unit,
11230 NULL, xcalloc, xfree));
11231 }
11232
11233 /* die_reader_func for create_dwo_cu. */
11234
11235 static void
11236 create_dwo_cu_reader (const struct die_reader_specs *reader,
11237 const gdb_byte *info_ptr,
11238 struct die_info *comp_unit_die,
11239 struct dwo_file *dwo_file,
11240 struct dwo_unit *dwo_unit)
11241 {
11242 struct dwarf2_cu *cu = reader->cu;
11243 sect_offset sect_off = cu->per_cu->sect_off;
11244 struct dwarf2_section_info *section = cu->per_cu->section;
11245
11246 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11247 if (!signature.has_value ())
11248 {
11249 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11250 " its dwo_id [in module %s]"),
11251 sect_offset_str (sect_off), dwo_file->dwo_name);
11252 return;
11253 }
11254
11255 dwo_unit->dwo_file = dwo_file;
11256 dwo_unit->signature = *signature;
11257 dwo_unit->section = section;
11258 dwo_unit->sect_off = sect_off;
11259 dwo_unit->length = cu->per_cu->length;
11260
11261 if (dwarf_read_debug)
11262 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11263 sect_offset_str (sect_off),
11264 hex_string (dwo_unit->signature));
11265 }
11266
11267 /* Create the dwo_units for the CUs in a DWO_FILE.
11268 Note: This function processes DWO files only, not DWP files. */
11269
11270 static void
11271 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11272 dwarf2_cu *cu, struct dwo_file &dwo_file,
11273 dwarf2_section_info &section, htab_up &cus_htab)
11274 {
11275 struct objfile *objfile = dwarf2_per_objfile->objfile;
11276 const gdb_byte *info_ptr, *end_ptr;
11277
11278 section.read (objfile);
11279 info_ptr = section.buffer;
11280
11281 if (info_ptr == NULL)
11282 return;
11283
11284 if (dwarf_read_debug)
11285 {
11286 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11287 section.get_name (),
11288 section.get_file_name ());
11289 }
11290
11291 end_ptr = info_ptr + section.size;
11292 while (info_ptr < end_ptr)
11293 {
11294 struct dwarf2_per_cu_data per_cu;
11295 struct dwo_unit read_unit {};
11296 struct dwo_unit *dwo_unit;
11297 void **slot;
11298 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11299
11300 memset (&per_cu, 0, sizeof (per_cu));
11301 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11302 per_cu.is_debug_types = 0;
11303 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11304 per_cu.section = &section;
11305
11306 cutu_reader reader (&per_cu, cu, &dwo_file);
11307 if (!reader.dummy_p)
11308 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11309 &dwo_file, &read_unit);
11310 info_ptr += per_cu.length;
11311
11312 // If the unit could not be parsed, skip it.
11313 if (read_unit.dwo_file == NULL)
11314 continue;
11315
11316 if (cus_htab == NULL)
11317 cus_htab = allocate_dwo_unit_table ();
11318
11319 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
11320 struct dwo_unit);
11321 *dwo_unit = read_unit;
11322 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11323 gdb_assert (slot != NULL);
11324 if (*slot != NULL)
11325 {
11326 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11327 sect_offset dup_sect_off = dup_cu->sect_off;
11328
11329 complaint (_("debug cu entry at offset %s is duplicate to"
11330 " the entry at offset %s, signature %s"),
11331 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11332 hex_string (dwo_unit->signature));
11333 }
11334 *slot = (void *)dwo_unit;
11335 }
11336 }
11337
11338 /* DWP file .debug_{cu,tu}_index section format:
11339 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11340
11341 DWP Version 1:
11342
11343 Both index sections have the same format, and serve to map a 64-bit
11344 signature to a set of section numbers. Each section begins with a header,
11345 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11346 indexes, and a pool of 32-bit section numbers. The index sections will be
11347 aligned at 8-byte boundaries in the file.
11348
11349 The index section header consists of:
11350
11351 V, 32 bit version number
11352 -, 32 bits unused
11353 N, 32 bit number of compilation units or type units in the index
11354 M, 32 bit number of slots in the hash table
11355
11356 Numbers are recorded using the byte order of the application binary.
11357
11358 The hash table begins at offset 16 in the section, and consists of an array
11359 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11360 order of the application binary). Unused slots in the hash table are 0.
11361 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11362
11363 The parallel table begins immediately after the hash table
11364 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11365 array of 32-bit indexes (using the byte order of the application binary),
11366 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11367 table contains a 32-bit index into the pool of section numbers. For unused
11368 hash table slots, the corresponding entry in the parallel table will be 0.
11369
11370 The pool of section numbers begins immediately following the hash table
11371 (at offset 16 + 12 * M from the beginning of the section). The pool of
11372 section numbers consists of an array of 32-bit words (using the byte order
11373 of the application binary). Each item in the array is indexed starting
11374 from 0. The hash table entry provides the index of the first section
11375 number in the set. Additional section numbers in the set follow, and the
11376 set is terminated by a 0 entry (section number 0 is not used in ELF).
11377
11378 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11379 section must be the first entry in the set, and the .debug_abbrev.dwo must
11380 be the second entry. Other members of the set may follow in any order.
11381
11382 ---
11383
11384 DWP Version 2:
11385
11386 DWP Version 2 combines all the .debug_info, etc. sections into one,
11387 and the entries in the index tables are now offsets into these sections.
11388 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11389 section.
11390
11391 Index Section Contents:
11392 Header
11393 Hash Table of Signatures dwp_hash_table.hash_table
11394 Parallel Table of Indices dwp_hash_table.unit_table
11395 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11396 Table of Section Sizes dwp_hash_table.v2.sizes
11397
11398 The index section header consists of:
11399
11400 V, 32 bit version number
11401 L, 32 bit number of columns in the table of section offsets
11402 N, 32 bit number of compilation units or type units in the index
11403 M, 32 bit number of slots in the hash table
11404
11405 Numbers are recorded using the byte order of the application binary.
11406
11407 The hash table has the same format as version 1.
11408 The parallel table of indices has the same format as version 1,
11409 except that the entries are origin-1 indices into the table of sections
11410 offsets and the table of section sizes.
11411
11412 The table of offsets begins immediately following the parallel table
11413 (at offset 16 + 12 * M from the beginning of the section). The table is
11414 a two-dimensional array of 32-bit words (using the byte order of the
11415 application binary), with L columns and N+1 rows, in row-major order.
11416 Each row in the array is indexed starting from 0. The first row provides
11417 a key to the remaining rows: each column in this row provides an identifier
11418 for a debug section, and the offsets in the same column of subsequent rows
11419 refer to that section. The section identifiers are:
11420
11421 DW_SECT_INFO 1 .debug_info.dwo
11422 DW_SECT_TYPES 2 .debug_types.dwo
11423 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11424 DW_SECT_LINE 4 .debug_line.dwo
11425 DW_SECT_LOC 5 .debug_loc.dwo
11426 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11427 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11428 DW_SECT_MACRO 8 .debug_macro.dwo
11429
11430 The offsets provided by the CU and TU index sections are the base offsets
11431 for the contributions made by each CU or TU to the corresponding section
11432 in the package file. Each CU and TU header contains an abbrev_offset
11433 field, used to find the abbreviations table for that CU or TU within the
11434 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11435 be interpreted as relative to the base offset given in the index section.
11436 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11437 should be interpreted as relative to the base offset for .debug_line.dwo,
11438 and offsets into other debug sections obtained from DWARF attributes should
11439 also be interpreted as relative to the corresponding base offset.
11440
11441 The table of sizes begins immediately following the table of offsets.
11442 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11443 with L columns and N rows, in row-major order. Each row in the array is
11444 indexed starting from 1 (row 0 is shared by the two tables).
11445
11446 ---
11447
11448 Hash table lookup is handled the same in version 1 and 2:
11449
11450 We assume that N and M will not exceed 2^32 - 1.
11451 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11452
11453 Given a 64-bit compilation unit signature or a type signature S, an entry
11454 in the hash table is located as follows:
11455
11456 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11457 the low-order k bits all set to 1.
11458
11459 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11460
11461 3) If the hash table entry at index H matches the signature, use that
11462 entry. If the hash table entry at index H is unused (all zeroes),
11463 terminate the search: the signature is not present in the table.
11464
11465 4) Let H = (H + H') modulo M. Repeat at Step 3.
11466
11467 Because M > N and H' and M are relatively prime, the search is guaranteed
11468 to stop at an unused slot or find the match. */
11469
11470 /* Create a hash table to map DWO IDs to their CU/TU entry in
11471 .debug_{info,types}.dwo in DWP_FILE.
11472 Returns NULL if there isn't one.
11473 Note: This function processes DWP files only, not DWO files. */
11474
11475 static struct dwp_hash_table *
11476 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11477 struct dwp_file *dwp_file, int is_debug_types)
11478 {
11479 struct objfile *objfile = dwarf2_per_objfile->objfile;
11480 bfd *dbfd = dwp_file->dbfd.get ();
11481 const gdb_byte *index_ptr, *index_end;
11482 struct dwarf2_section_info *index;
11483 uint32_t version, nr_columns, nr_units, nr_slots;
11484 struct dwp_hash_table *htab;
11485
11486 if (is_debug_types)
11487 index = &dwp_file->sections.tu_index;
11488 else
11489 index = &dwp_file->sections.cu_index;
11490
11491 if (index->empty ())
11492 return NULL;
11493 index->read (objfile);
11494
11495 index_ptr = index->buffer;
11496 index_end = index_ptr + index->size;
11497
11498 version = read_4_bytes (dbfd, index_ptr);
11499 index_ptr += 4;
11500 if (version == 2)
11501 nr_columns = read_4_bytes (dbfd, index_ptr);
11502 else
11503 nr_columns = 0;
11504 index_ptr += 4;
11505 nr_units = read_4_bytes (dbfd, index_ptr);
11506 index_ptr += 4;
11507 nr_slots = read_4_bytes (dbfd, index_ptr);
11508 index_ptr += 4;
11509
11510 if (version != 1 && version != 2)
11511 {
11512 error (_("Dwarf Error: unsupported DWP file version (%s)"
11513 " [in module %s]"),
11514 pulongest (version), dwp_file->name);
11515 }
11516 if (nr_slots != (nr_slots & -nr_slots))
11517 {
11518 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11519 " is not power of 2 [in module %s]"),
11520 pulongest (nr_slots), dwp_file->name);
11521 }
11522
11523 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11524 htab->version = version;
11525 htab->nr_columns = nr_columns;
11526 htab->nr_units = nr_units;
11527 htab->nr_slots = nr_slots;
11528 htab->hash_table = index_ptr;
11529 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11530
11531 /* Exit early if the table is empty. */
11532 if (nr_slots == 0 || nr_units == 0
11533 || (version == 2 && nr_columns == 0))
11534 {
11535 /* All must be zero. */
11536 if (nr_slots != 0 || nr_units != 0
11537 || (version == 2 && nr_columns != 0))
11538 {
11539 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11540 " all zero [in modules %s]"),
11541 dwp_file->name);
11542 }
11543 return htab;
11544 }
11545
11546 if (version == 1)
11547 {
11548 htab->section_pool.v1.indices =
11549 htab->unit_table + sizeof (uint32_t) * nr_slots;
11550 /* It's harder to decide whether the section is too small in v1.
11551 V1 is deprecated anyway so we punt. */
11552 }
11553 else
11554 {
11555 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11556 int *ids = htab->section_pool.v2.section_ids;
11557 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11558 /* Reverse map for error checking. */
11559 int ids_seen[DW_SECT_MAX + 1];
11560 int i;
11561
11562 if (nr_columns < 2)
11563 {
11564 error (_("Dwarf Error: bad DWP hash table, too few columns"
11565 " in section table [in module %s]"),
11566 dwp_file->name);
11567 }
11568 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11569 {
11570 error (_("Dwarf Error: bad DWP hash table, too many columns"
11571 " in section table [in module %s]"),
11572 dwp_file->name);
11573 }
11574 memset (ids, 255, sizeof_ids);
11575 memset (ids_seen, 255, sizeof (ids_seen));
11576 for (i = 0; i < nr_columns; ++i)
11577 {
11578 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11579
11580 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11581 {
11582 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11583 " in section table [in module %s]"),
11584 id, dwp_file->name);
11585 }
11586 if (ids_seen[id] != -1)
11587 {
11588 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11589 " id %d in section table [in module %s]"),
11590 id, dwp_file->name);
11591 }
11592 ids_seen[id] = i;
11593 ids[i] = id;
11594 }
11595 /* Must have exactly one info or types section. */
11596 if (((ids_seen[DW_SECT_INFO] != -1)
11597 + (ids_seen[DW_SECT_TYPES] != -1))
11598 != 1)
11599 {
11600 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11601 " DWO info/types section [in module %s]"),
11602 dwp_file->name);
11603 }
11604 /* Must have an abbrev section. */
11605 if (ids_seen[DW_SECT_ABBREV] == -1)
11606 {
11607 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11608 " section [in module %s]"),
11609 dwp_file->name);
11610 }
11611 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11612 htab->section_pool.v2.sizes =
11613 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11614 * nr_units * nr_columns);
11615 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11616 * nr_units * nr_columns))
11617 > index_end)
11618 {
11619 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11620 " [in module %s]"),
11621 dwp_file->name);
11622 }
11623 }
11624
11625 return htab;
11626 }
11627
11628 /* Update SECTIONS with the data from SECTP.
11629
11630 This function is like the other "locate" section routines that are
11631 passed to bfd_map_over_sections, but in this context the sections to
11632 read comes from the DWP V1 hash table, not the full ELF section table.
11633
11634 The result is non-zero for success, or zero if an error was found. */
11635
11636 static int
11637 locate_v1_virtual_dwo_sections (asection *sectp,
11638 struct virtual_v1_dwo_sections *sections)
11639 {
11640 const struct dwop_section_names *names = &dwop_section_names;
11641
11642 if (section_is_p (sectp->name, &names->abbrev_dwo))
11643 {
11644 /* There can be only one. */
11645 if (sections->abbrev.s.section != NULL)
11646 return 0;
11647 sections->abbrev.s.section = sectp;
11648 sections->abbrev.size = bfd_section_size (sectp);
11649 }
11650 else if (section_is_p (sectp->name, &names->info_dwo)
11651 || section_is_p (sectp->name, &names->types_dwo))
11652 {
11653 /* There can be only one. */
11654 if (sections->info_or_types.s.section != NULL)
11655 return 0;
11656 sections->info_or_types.s.section = sectp;
11657 sections->info_or_types.size = bfd_section_size (sectp);
11658 }
11659 else if (section_is_p (sectp->name, &names->line_dwo))
11660 {
11661 /* There can be only one. */
11662 if (sections->line.s.section != NULL)
11663 return 0;
11664 sections->line.s.section = sectp;
11665 sections->line.size = bfd_section_size (sectp);
11666 }
11667 else if (section_is_p (sectp->name, &names->loc_dwo))
11668 {
11669 /* There can be only one. */
11670 if (sections->loc.s.section != NULL)
11671 return 0;
11672 sections->loc.s.section = sectp;
11673 sections->loc.size = bfd_section_size (sectp);
11674 }
11675 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11676 {
11677 /* There can be only one. */
11678 if (sections->macinfo.s.section != NULL)
11679 return 0;
11680 sections->macinfo.s.section = sectp;
11681 sections->macinfo.size = bfd_section_size (sectp);
11682 }
11683 else if (section_is_p (sectp->name, &names->macro_dwo))
11684 {
11685 /* There can be only one. */
11686 if (sections->macro.s.section != NULL)
11687 return 0;
11688 sections->macro.s.section = sectp;
11689 sections->macro.size = bfd_section_size (sectp);
11690 }
11691 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11692 {
11693 /* There can be only one. */
11694 if (sections->str_offsets.s.section != NULL)
11695 return 0;
11696 sections->str_offsets.s.section = sectp;
11697 sections->str_offsets.size = bfd_section_size (sectp);
11698 }
11699 else
11700 {
11701 /* No other kind of section is valid. */
11702 return 0;
11703 }
11704
11705 return 1;
11706 }
11707
11708 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11709 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11710 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11711 This is for DWP version 1 files. */
11712
11713 static struct dwo_unit *
11714 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11715 struct dwp_file *dwp_file,
11716 uint32_t unit_index,
11717 const char *comp_dir,
11718 ULONGEST signature, int is_debug_types)
11719 {
11720 const struct dwp_hash_table *dwp_htab =
11721 is_debug_types ? dwp_file->tus : dwp_file->cus;
11722 bfd *dbfd = dwp_file->dbfd.get ();
11723 const char *kind = is_debug_types ? "TU" : "CU";
11724 struct dwo_file *dwo_file;
11725 struct dwo_unit *dwo_unit;
11726 struct virtual_v1_dwo_sections sections;
11727 void **dwo_file_slot;
11728 int i;
11729
11730 gdb_assert (dwp_file->version == 1);
11731
11732 if (dwarf_read_debug)
11733 {
11734 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11735 kind,
11736 pulongest (unit_index), hex_string (signature),
11737 dwp_file->name);
11738 }
11739
11740 /* Fetch the sections of this DWO unit.
11741 Put a limit on the number of sections we look for so that bad data
11742 doesn't cause us to loop forever. */
11743
11744 #define MAX_NR_V1_DWO_SECTIONS \
11745 (1 /* .debug_info or .debug_types */ \
11746 + 1 /* .debug_abbrev */ \
11747 + 1 /* .debug_line */ \
11748 + 1 /* .debug_loc */ \
11749 + 1 /* .debug_str_offsets */ \
11750 + 1 /* .debug_macro or .debug_macinfo */ \
11751 + 1 /* trailing zero */)
11752
11753 memset (&sections, 0, sizeof (sections));
11754
11755 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11756 {
11757 asection *sectp;
11758 uint32_t section_nr =
11759 read_4_bytes (dbfd,
11760 dwp_htab->section_pool.v1.indices
11761 + (unit_index + i) * sizeof (uint32_t));
11762
11763 if (section_nr == 0)
11764 break;
11765 if (section_nr >= dwp_file->num_sections)
11766 {
11767 error (_("Dwarf Error: bad DWP hash table, section number too large"
11768 " [in module %s]"),
11769 dwp_file->name);
11770 }
11771
11772 sectp = dwp_file->elf_sections[section_nr];
11773 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11774 {
11775 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11776 " [in module %s]"),
11777 dwp_file->name);
11778 }
11779 }
11780
11781 if (i < 2
11782 || sections.info_or_types.empty ()
11783 || sections.abbrev.empty ())
11784 {
11785 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11786 " [in module %s]"),
11787 dwp_file->name);
11788 }
11789 if (i == MAX_NR_V1_DWO_SECTIONS)
11790 {
11791 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11792 " [in module %s]"),
11793 dwp_file->name);
11794 }
11795
11796 /* It's easier for the rest of the code if we fake a struct dwo_file and
11797 have dwo_unit "live" in that. At least for now.
11798
11799 The DWP file can be made up of a random collection of CUs and TUs.
11800 However, for each CU + set of TUs that came from the same original DWO
11801 file, we can combine them back into a virtual DWO file to save space
11802 (fewer struct dwo_file objects to allocate). Remember that for really
11803 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11804
11805 std::string virtual_dwo_name =
11806 string_printf ("virtual-dwo/%d-%d-%d-%d",
11807 sections.abbrev.get_id (),
11808 sections.line.get_id (),
11809 sections.loc.get_id (),
11810 sections.str_offsets.get_id ());
11811 /* Can we use an existing virtual DWO file? */
11812 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11813 virtual_dwo_name.c_str (),
11814 comp_dir);
11815 /* Create one if necessary. */
11816 if (*dwo_file_slot == NULL)
11817 {
11818 if (dwarf_read_debug)
11819 {
11820 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11821 virtual_dwo_name.c_str ());
11822 }
11823 dwo_file = new struct dwo_file;
11824 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11825 dwo_file->comp_dir = comp_dir;
11826 dwo_file->sections.abbrev = sections.abbrev;
11827 dwo_file->sections.line = sections.line;
11828 dwo_file->sections.loc = sections.loc;
11829 dwo_file->sections.macinfo = sections.macinfo;
11830 dwo_file->sections.macro = sections.macro;
11831 dwo_file->sections.str_offsets = sections.str_offsets;
11832 /* The "str" section is global to the entire DWP file. */
11833 dwo_file->sections.str = dwp_file->sections.str;
11834 /* The info or types section is assigned below to dwo_unit,
11835 there's no need to record it in dwo_file.
11836 Also, we can't simply record type sections in dwo_file because
11837 we record a pointer into the vector in dwo_unit. As we collect more
11838 types we'll grow the vector and eventually have to reallocate space
11839 for it, invalidating all copies of pointers into the previous
11840 contents. */
11841 *dwo_file_slot = dwo_file;
11842 }
11843 else
11844 {
11845 if (dwarf_read_debug)
11846 {
11847 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11848 virtual_dwo_name.c_str ());
11849 }
11850 dwo_file = (struct dwo_file *) *dwo_file_slot;
11851 }
11852
11853 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11854 dwo_unit->dwo_file = dwo_file;
11855 dwo_unit->signature = signature;
11856 dwo_unit->section =
11857 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11858 *dwo_unit->section = sections.info_or_types;
11859 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11860
11861 return dwo_unit;
11862 }
11863
11864 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11865 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11866 piece within that section used by a TU/CU, return a virtual section
11867 of just that piece. */
11868
11869 static struct dwarf2_section_info
11870 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11871 struct dwarf2_section_info *section,
11872 bfd_size_type offset, bfd_size_type size)
11873 {
11874 struct dwarf2_section_info result;
11875 asection *sectp;
11876
11877 gdb_assert (section != NULL);
11878 gdb_assert (!section->is_virtual);
11879
11880 memset (&result, 0, sizeof (result));
11881 result.s.containing_section = section;
11882 result.is_virtual = true;
11883
11884 if (size == 0)
11885 return result;
11886
11887 sectp = section->get_bfd_section ();
11888
11889 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11890 bounds of the real section. This is a pretty-rare event, so just
11891 flag an error (easier) instead of a warning and trying to cope. */
11892 if (sectp == NULL
11893 || offset + size > bfd_section_size (sectp))
11894 {
11895 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11896 " in section %s [in module %s]"),
11897 sectp ? bfd_section_name (sectp) : "<unknown>",
11898 objfile_name (dwarf2_per_objfile->objfile));
11899 }
11900
11901 result.virtual_offset = offset;
11902 result.size = size;
11903 return result;
11904 }
11905
11906 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11907 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11908 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11909 This is for DWP version 2 files. */
11910
11911 static struct dwo_unit *
11912 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11913 struct dwp_file *dwp_file,
11914 uint32_t unit_index,
11915 const char *comp_dir,
11916 ULONGEST signature, int is_debug_types)
11917 {
11918 const struct dwp_hash_table *dwp_htab =
11919 is_debug_types ? dwp_file->tus : dwp_file->cus;
11920 bfd *dbfd = dwp_file->dbfd.get ();
11921 const char *kind = is_debug_types ? "TU" : "CU";
11922 struct dwo_file *dwo_file;
11923 struct dwo_unit *dwo_unit;
11924 struct virtual_v2_dwo_sections sections;
11925 void **dwo_file_slot;
11926 int i;
11927
11928 gdb_assert (dwp_file->version == 2);
11929
11930 if (dwarf_read_debug)
11931 {
11932 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11933 kind,
11934 pulongest (unit_index), hex_string (signature),
11935 dwp_file->name);
11936 }
11937
11938 /* Fetch the section offsets of this DWO unit. */
11939
11940 memset (&sections, 0, sizeof (sections));
11941
11942 for (i = 0; i < dwp_htab->nr_columns; ++i)
11943 {
11944 uint32_t offset = read_4_bytes (dbfd,
11945 dwp_htab->section_pool.v2.offsets
11946 + (((unit_index - 1) * dwp_htab->nr_columns
11947 + i)
11948 * sizeof (uint32_t)));
11949 uint32_t size = read_4_bytes (dbfd,
11950 dwp_htab->section_pool.v2.sizes
11951 + (((unit_index - 1) * dwp_htab->nr_columns
11952 + i)
11953 * sizeof (uint32_t)));
11954
11955 switch (dwp_htab->section_pool.v2.section_ids[i])
11956 {
11957 case DW_SECT_INFO:
11958 case DW_SECT_TYPES:
11959 sections.info_or_types_offset = offset;
11960 sections.info_or_types_size = size;
11961 break;
11962 case DW_SECT_ABBREV:
11963 sections.abbrev_offset = offset;
11964 sections.abbrev_size = size;
11965 break;
11966 case DW_SECT_LINE:
11967 sections.line_offset = offset;
11968 sections.line_size = size;
11969 break;
11970 case DW_SECT_LOC:
11971 sections.loc_offset = offset;
11972 sections.loc_size = size;
11973 break;
11974 case DW_SECT_STR_OFFSETS:
11975 sections.str_offsets_offset = offset;
11976 sections.str_offsets_size = size;
11977 break;
11978 case DW_SECT_MACINFO:
11979 sections.macinfo_offset = offset;
11980 sections.macinfo_size = size;
11981 break;
11982 case DW_SECT_MACRO:
11983 sections.macro_offset = offset;
11984 sections.macro_size = size;
11985 break;
11986 }
11987 }
11988
11989 /* It's easier for the rest of the code if we fake a struct dwo_file and
11990 have dwo_unit "live" in that. At least for now.
11991
11992 The DWP file can be made up of a random collection of CUs and TUs.
11993 However, for each CU + set of TUs that came from the same original DWO
11994 file, we can combine them back into a virtual DWO file to save space
11995 (fewer struct dwo_file objects to allocate). Remember that for really
11996 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11997
11998 std::string virtual_dwo_name =
11999 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12000 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12001 (long) (sections.line_size ? sections.line_offset : 0),
12002 (long) (sections.loc_size ? sections.loc_offset : 0),
12003 (long) (sections.str_offsets_size
12004 ? sections.str_offsets_offset : 0));
12005 /* Can we use an existing virtual DWO file? */
12006 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12007 virtual_dwo_name.c_str (),
12008 comp_dir);
12009 /* Create one if necessary. */
12010 if (*dwo_file_slot == NULL)
12011 {
12012 if (dwarf_read_debug)
12013 {
12014 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12015 virtual_dwo_name.c_str ());
12016 }
12017 dwo_file = new struct dwo_file;
12018 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12019 dwo_file->comp_dir = comp_dir;
12020 dwo_file->sections.abbrev =
12021 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12022 sections.abbrev_offset, sections.abbrev_size);
12023 dwo_file->sections.line =
12024 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12025 sections.line_offset, sections.line_size);
12026 dwo_file->sections.loc =
12027 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12028 sections.loc_offset, sections.loc_size);
12029 dwo_file->sections.macinfo =
12030 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12031 sections.macinfo_offset, sections.macinfo_size);
12032 dwo_file->sections.macro =
12033 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12034 sections.macro_offset, sections.macro_size);
12035 dwo_file->sections.str_offsets =
12036 create_dwp_v2_section (dwarf2_per_objfile,
12037 &dwp_file->sections.str_offsets,
12038 sections.str_offsets_offset,
12039 sections.str_offsets_size);
12040 /* The "str" section is global to the entire DWP file. */
12041 dwo_file->sections.str = dwp_file->sections.str;
12042 /* The info or types section is assigned below to dwo_unit,
12043 there's no need to record it in dwo_file.
12044 Also, we can't simply record type sections in dwo_file because
12045 we record a pointer into the vector in dwo_unit. As we collect more
12046 types we'll grow the vector and eventually have to reallocate space
12047 for it, invalidating all copies of pointers into the previous
12048 contents. */
12049 *dwo_file_slot = dwo_file;
12050 }
12051 else
12052 {
12053 if (dwarf_read_debug)
12054 {
12055 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12056 virtual_dwo_name.c_str ());
12057 }
12058 dwo_file = (struct dwo_file *) *dwo_file_slot;
12059 }
12060
12061 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12062 dwo_unit->dwo_file = dwo_file;
12063 dwo_unit->signature = signature;
12064 dwo_unit->section =
12065 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12066 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12067 is_debug_types
12068 ? &dwp_file->sections.types
12069 : &dwp_file->sections.info,
12070 sections.info_or_types_offset,
12071 sections.info_or_types_size);
12072 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12073
12074 return dwo_unit;
12075 }
12076
12077 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12078 Returns NULL if the signature isn't found. */
12079
12080 static struct dwo_unit *
12081 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12082 struct dwp_file *dwp_file, const char *comp_dir,
12083 ULONGEST signature, int is_debug_types)
12084 {
12085 const struct dwp_hash_table *dwp_htab =
12086 is_debug_types ? dwp_file->tus : dwp_file->cus;
12087 bfd *dbfd = dwp_file->dbfd.get ();
12088 uint32_t mask = dwp_htab->nr_slots - 1;
12089 uint32_t hash = signature & mask;
12090 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12091 unsigned int i;
12092 void **slot;
12093 struct dwo_unit find_dwo_cu;
12094
12095 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12096 find_dwo_cu.signature = signature;
12097 slot = htab_find_slot (is_debug_types
12098 ? dwp_file->loaded_tus.get ()
12099 : dwp_file->loaded_cus.get (),
12100 &find_dwo_cu, INSERT);
12101
12102 if (*slot != NULL)
12103 return (struct dwo_unit *) *slot;
12104
12105 /* Use a for loop so that we don't loop forever on bad debug info. */
12106 for (i = 0; i < dwp_htab->nr_slots; ++i)
12107 {
12108 ULONGEST signature_in_table;
12109
12110 signature_in_table =
12111 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12112 if (signature_in_table == signature)
12113 {
12114 uint32_t unit_index =
12115 read_4_bytes (dbfd,
12116 dwp_htab->unit_table + hash * sizeof (uint32_t));
12117
12118 if (dwp_file->version == 1)
12119 {
12120 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12121 dwp_file, unit_index,
12122 comp_dir, signature,
12123 is_debug_types);
12124 }
12125 else
12126 {
12127 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12128 dwp_file, unit_index,
12129 comp_dir, signature,
12130 is_debug_types);
12131 }
12132 return (struct dwo_unit *) *slot;
12133 }
12134 if (signature_in_table == 0)
12135 return NULL;
12136 hash = (hash + hash2) & mask;
12137 }
12138
12139 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12140 " [in module %s]"),
12141 dwp_file->name);
12142 }
12143
12144 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12145 Open the file specified by FILE_NAME and hand it off to BFD for
12146 preliminary analysis. Return a newly initialized bfd *, which
12147 includes a canonicalized copy of FILE_NAME.
12148 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12149 SEARCH_CWD is true if the current directory is to be searched.
12150 It will be searched before debug-file-directory.
12151 If successful, the file is added to the bfd include table of the
12152 objfile's bfd (see gdb_bfd_record_inclusion).
12153 If unable to find/open the file, return NULL.
12154 NOTE: This function is derived from symfile_bfd_open. */
12155
12156 static gdb_bfd_ref_ptr
12157 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12158 const char *file_name, int is_dwp, int search_cwd)
12159 {
12160 int desc;
12161 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12162 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12163 to debug_file_directory. */
12164 const char *search_path;
12165 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12166
12167 gdb::unique_xmalloc_ptr<char> search_path_holder;
12168 if (search_cwd)
12169 {
12170 if (*debug_file_directory != '\0')
12171 {
12172 search_path_holder.reset (concat (".", dirname_separator_string,
12173 debug_file_directory,
12174 (char *) NULL));
12175 search_path = search_path_holder.get ();
12176 }
12177 else
12178 search_path = ".";
12179 }
12180 else
12181 search_path = debug_file_directory;
12182
12183 openp_flags flags = OPF_RETURN_REALPATH;
12184 if (is_dwp)
12185 flags |= OPF_SEARCH_IN_PATH;
12186
12187 gdb::unique_xmalloc_ptr<char> absolute_name;
12188 desc = openp (search_path, flags, file_name,
12189 O_RDONLY | O_BINARY, &absolute_name);
12190 if (desc < 0)
12191 return NULL;
12192
12193 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12194 gnutarget, desc));
12195 if (sym_bfd == NULL)
12196 return NULL;
12197 bfd_set_cacheable (sym_bfd.get (), 1);
12198
12199 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12200 return NULL;
12201
12202 /* Success. Record the bfd as having been included by the objfile's bfd.
12203 This is important because things like demangled_names_hash lives in the
12204 objfile's per_bfd space and may have references to things like symbol
12205 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12206 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12207
12208 return sym_bfd;
12209 }
12210
12211 /* Try to open DWO file FILE_NAME.
12212 COMP_DIR is the DW_AT_comp_dir attribute.
12213 The result is the bfd handle of the file.
12214 If there is a problem finding or opening the file, return NULL.
12215 Upon success, the canonicalized path of the file is stored in the bfd,
12216 same as symfile_bfd_open. */
12217
12218 static gdb_bfd_ref_ptr
12219 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12220 const char *file_name, const char *comp_dir)
12221 {
12222 if (IS_ABSOLUTE_PATH (file_name))
12223 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12224 0 /*is_dwp*/, 0 /*search_cwd*/);
12225
12226 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12227
12228 if (comp_dir != NULL)
12229 {
12230 gdb::unique_xmalloc_ptr<char> path_to_try
12231 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12232
12233 /* NOTE: If comp_dir is a relative path, this will also try the
12234 search path, which seems useful. */
12235 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12236 path_to_try.get (),
12237 0 /*is_dwp*/,
12238 1 /*search_cwd*/));
12239 if (abfd != NULL)
12240 return abfd;
12241 }
12242
12243 /* That didn't work, try debug-file-directory, which, despite its name,
12244 is a list of paths. */
12245
12246 if (*debug_file_directory == '\0')
12247 return NULL;
12248
12249 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12250 0 /*is_dwp*/, 1 /*search_cwd*/);
12251 }
12252
12253 /* This function is mapped across the sections and remembers the offset and
12254 size of each of the DWO debugging sections we are interested in. */
12255
12256 static void
12257 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12258 {
12259 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12260 const struct dwop_section_names *names = &dwop_section_names;
12261
12262 if (section_is_p (sectp->name, &names->abbrev_dwo))
12263 {
12264 dwo_sections->abbrev.s.section = sectp;
12265 dwo_sections->abbrev.size = bfd_section_size (sectp);
12266 }
12267 else if (section_is_p (sectp->name, &names->info_dwo))
12268 {
12269 dwo_sections->info.s.section = sectp;
12270 dwo_sections->info.size = bfd_section_size (sectp);
12271 }
12272 else if (section_is_p (sectp->name, &names->line_dwo))
12273 {
12274 dwo_sections->line.s.section = sectp;
12275 dwo_sections->line.size = bfd_section_size (sectp);
12276 }
12277 else if (section_is_p (sectp->name, &names->loc_dwo))
12278 {
12279 dwo_sections->loc.s.section = sectp;
12280 dwo_sections->loc.size = bfd_section_size (sectp);
12281 }
12282 else if (section_is_p (sectp->name, &names->loclists_dwo))
12283 {
12284 dwo_sections->loclists.s.section = sectp;
12285 dwo_sections->loclists.size = bfd_section_size (sectp);
12286 }
12287 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12288 {
12289 dwo_sections->macinfo.s.section = sectp;
12290 dwo_sections->macinfo.size = bfd_section_size (sectp);
12291 }
12292 else if (section_is_p (sectp->name, &names->macro_dwo))
12293 {
12294 dwo_sections->macro.s.section = sectp;
12295 dwo_sections->macro.size = bfd_section_size (sectp);
12296 }
12297 else if (section_is_p (sectp->name, &names->str_dwo))
12298 {
12299 dwo_sections->str.s.section = sectp;
12300 dwo_sections->str.size = bfd_section_size (sectp);
12301 }
12302 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12303 {
12304 dwo_sections->str_offsets.s.section = sectp;
12305 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12306 }
12307 else if (section_is_p (sectp->name, &names->types_dwo))
12308 {
12309 struct dwarf2_section_info type_section;
12310
12311 memset (&type_section, 0, sizeof (type_section));
12312 type_section.s.section = sectp;
12313 type_section.size = bfd_section_size (sectp);
12314 dwo_sections->types.push_back (type_section);
12315 }
12316 }
12317
12318 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12319 by PER_CU. This is for the non-DWP case.
12320 The result is NULL if DWO_NAME can't be found. */
12321
12322 static struct dwo_file *
12323 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12324 const char *dwo_name, const char *comp_dir)
12325 {
12326 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12327
12328 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12329 if (dbfd == NULL)
12330 {
12331 if (dwarf_read_debug)
12332 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12333 return NULL;
12334 }
12335
12336 dwo_file_up dwo_file (new struct dwo_file);
12337 dwo_file->dwo_name = dwo_name;
12338 dwo_file->comp_dir = comp_dir;
12339 dwo_file->dbfd = std::move (dbfd);
12340
12341 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12342 &dwo_file->sections);
12343
12344 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12345 dwo_file->sections.info, dwo_file->cus);
12346
12347 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12348 dwo_file->sections.types, dwo_file->tus);
12349
12350 if (dwarf_read_debug)
12351 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12352
12353 return dwo_file.release ();
12354 }
12355
12356 /* This function is mapped across the sections and remembers the offset and
12357 size of each of the DWP debugging sections common to version 1 and 2 that
12358 we are interested in. */
12359
12360 static void
12361 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12362 void *dwp_file_ptr)
12363 {
12364 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12365 const struct dwop_section_names *names = &dwop_section_names;
12366 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12367
12368 /* Record the ELF section number for later lookup: this is what the
12369 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12370 gdb_assert (elf_section_nr < dwp_file->num_sections);
12371 dwp_file->elf_sections[elf_section_nr] = sectp;
12372
12373 /* Look for specific sections that we need. */
12374 if (section_is_p (sectp->name, &names->str_dwo))
12375 {
12376 dwp_file->sections.str.s.section = sectp;
12377 dwp_file->sections.str.size = bfd_section_size (sectp);
12378 }
12379 else if (section_is_p (sectp->name, &names->cu_index))
12380 {
12381 dwp_file->sections.cu_index.s.section = sectp;
12382 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12383 }
12384 else if (section_is_p (sectp->name, &names->tu_index))
12385 {
12386 dwp_file->sections.tu_index.s.section = sectp;
12387 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12388 }
12389 }
12390
12391 /* This function is mapped across the sections and remembers the offset and
12392 size of each of the DWP version 2 debugging sections that we are interested
12393 in. This is split into a separate function because we don't know if we
12394 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12395
12396 static void
12397 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12398 {
12399 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12400 const struct dwop_section_names *names = &dwop_section_names;
12401 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12402
12403 /* Record the ELF section number for later lookup: this is what the
12404 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12405 gdb_assert (elf_section_nr < dwp_file->num_sections);
12406 dwp_file->elf_sections[elf_section_nr] = sectp;
12407
12408 /* Look for specific sections that we need. */
12409 if (section_is_p (sectp->name, &names->abbrev_dwo))
12410 {
12411 dwp_file->sections.abbrev.s.section = sectp;
12412 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12413 }
12414 else if (section_is_p (sectp->name, &names->info_dwo))
12415 {
12416 dwp_file->sections.info.s.section = sectp;
12417 dwp_file->sections.info.size = bfd_section_size (sectp);
12418 }
12419 else if (section_is_p (sectp->name, &names->line_dwo))
12420 {
12421 dwp_file->sections.line.s.section = sectp;
12422 dwp_file->sections.line.size = bfd_section_size (sectp);
12423 }
12424 else if (section_is_p (sectp->name, &names->loc_dwo))
12425 {
12426 dwp_file->sections.loc.s.section = sectp;
12427 dwp_file->sections.loc.size = bfd_section_size (sectp);
12428 }
12429 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12430 {
12431 dwp_file->sections.macinfo.s.section = sectp;
12432 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12433 }
12434 else if (section_is_p (sectp->name, &names->macro_dwo))
12435 {
12436 dwp_file->sections.macro.s.section = sectp;
12437 dwp_file->sections.macro.size = bfd_section_size (sectp);
12438 }
12439 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12440 {
12441 dwp_file->sections.str_offsets.s.section = sectp;
12442 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12443 }
12444 else if (section_is_p (sectp->name, &names->types_dwo))
12445 {
12446 dwp_file->sections.types.s.section = sectp;
12447 dwp_file->sections.types.size = bfd_section_size (sectp);
12448 }
12449 }
12450
12451 /* Hash function for dwp_file loaded CUs/TUs. */
12452
12453 static hashval_t
12454 hash_dwp_loaded_cutus (const void *item)
12455 {
12456 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12457
12458 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12459 return dwo_unit->signature;
12460 }
12461
12462 /* Equality function for dwp_file loaded CUs/TUs. */
12463
12464 static int
12465 eq_dwp_loaded_cutus (const void *a, const void *b)
12466 {
12467 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12468 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12469
12470 return dua->signature == dub->signature;
12471 }
12472
12473 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12474
12475 static htab_up
12476 allocate_dwp_loaded_cutus_table ()
12477 {
12478 return htab_up (htab_create_alloc (3,
12479 hash_dwp_loaded_cutus,
12480 eq_dwp_loaded_cutus,
12481 NULL, xcalloc, xfree));
12482 }
12483
12484 /* Try to open DWP file FILE_NAME.
12485 The result is the bfd handle of the file.
12486 If there is a problem finding or opening the file, return NULL.
12487 Upon success, the canonicalized path of the file is stored in the bfd,
12488 same as symfile_bfd_open. */
12489
12490 static gdb_bfd_ref_ptr
12491 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12492 const char *file_name)
12493 {
12494 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12495 1 /*is_dwp*/,
12496 1 /*search_cwd*/));
12497 if (abfd != NULL)
12498 return abfd;
12499
12500 /* Work around upstream bug 15652.
12501 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12502 [Whether that's a "bug" is debatable, but it is getting in our way.]
12503 We have no real idea where the dwp file is, because gdb's realpath-ing
12504 of the executable's path may have discarded the needed info.
12505 [IWBN if the dwp file name was recorded in the executable, akin to
12506 .gnu_debuglink, but that doesn't exist yet.]
12507 Strip the directory from FILE_NAME and search again. */
12508 if (*debug_file_directory != '\0')
12509 {
12510 /* Don't implicitly search the current directory here.
12511 If the user wants to search "." to handle this case,
12512 it must be added to debug-file-directory. */
12513 return try_open_dwop_file (dwarf2_per_objfile,
12514 lbasename (file_name), 1 /*is_dwp*/,
12515 0 /*search_cwd*/);
12516 }
12517
12518 return NULL;
12519 }
12520
12521 /* Initialize the use of the DWP file for the current objfile.
12522 By convention the name of the DWP file is ${objfile}.dwp.
12523 The result is NULL if it can't be found. */
12524
12525 static std::unique_ptr<struct dwp_file>
12526 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12527 {
12528 struct objfile *objfile = dwarf2_per_objfile->objfile;
12529
12530 /* Try to find first .dwp for the binary file before any symbolic links
12531 resolving. */
12532
12533 /* If the objfile is a debug file, find the name of the real binary
12534 file and get the name of dwp file from there. */
12535 std::string dwp_name;
12536 if (objfile->separate_debug_objfile_backlink != NULL)
12537 {
12538 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12539 const char *backlink_basename = lbasename (backlink->original_name);
12540
12541 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12542 }
12543 else
12544 dwp_name = objfile->original_name;
12545
12546 dwp_name += ".dwp";
12547
12548 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12549 if (dbfd == NULL
12550 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12551 {
12552 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12553 dwp_name = objfile_name (objfile);
12554 dwp_name += ".dwp";
12555 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12556 }
12557
12558 if (dbfd == NULL)
12559 {
12560 if (dwarf_read_debug)
12561 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12562 return std::unique_ptr<dwp_file> ();
12563 }
12564
12565 const char *name = bfd_get_filename (dbfd.get ());
12566 std::unique_ptr<struct dwp_file> dwp_file
12567 (new struct dwp_file (name, std::move (dbfd)));
12568
12569 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12570 dwp_file->elf_sections =
12571 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12572 dwp_file->num_sections, asection *);
12573
12574 bfd_map_over_sections (dwp_file->dbfd.get (),
12575 dwarf2_locate_common_dwp_sections,
12576 dwp_file.get ());
12577
12578 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12579 0);
12580
12581 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12582 1);
12583
12584 /* The DWP file version is stored in the hash table. Oh well. */
12585 if (dwp_file->cus && dwp_file->tus
12586 && dwp_file->cus->version != dwp_file->tus->version)
12587 {
12588 /* Technically speaking, we should try to limp along, but this is
12589 pretty bizarre. We use pulongest here because that's the established
12590 portability solution (e.g, we cannot use %u for uint32_t). */
12591 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12592 " TU version %s [in DWP file %s]"),
12593 pulongest (dwp_file->cus->version),
12594 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12595 }
12596
12597 if (dwp_file->cus)
12598 dwp_file->version = dwp_file->cus->version;
12599 else if (dwp_file->tus)
12600 dwp_file->version = dwp_file->tus->version;
12601 else
12602 dwp_file->version = 2;
12603
12604 if (dwp_file->version == 2)
12605 bfd_map_over_sections (dwp_file->dbfd.get (),
12606 dwarf2_locate_v2_dwp_sections,
12607 dwp_file.get ());
12608
12609 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12610 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12611
12612 if (dwarf_read_debug)
12613 {
12614 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12615 fprintf_unfiltered (gdb_stdlog,
12616 " %s CUs, %s TUs\n",
12617 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12618 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12619 }
12620
12621 return dwp_file;
12622 }
12623
12624 /* Wrapper around open_and_init_dwp_file, only open it once. */
12625
12626 static struct dwp_file *
12627 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12628 {
12629 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12630 {
12631 dwarf2_per_objfile->per_bfd->dwp_file
12632 = open_and_init_dwp_file (dwarf2_per_objfile);
12633 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12634 }
12635 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12636 }
12637
12638 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12639 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12640 or in the DWP file for the objfile, referenced by THIS_UNIT.
12641 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12642 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12643
12644 This is called, for example, when wanting to read a variable with a
12645 complex location. Therefore we don't want to do file i/o for every call.
12646 Therefore we don't want to look for a DWO file on every call.
12647 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12648 then we check if we've already seen DWO_NAME, and only THEN do we check
12649 for a DWO file.
12650
12651 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12652 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12653
12654 static struct dwo_unit *
12655 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12656 const char *dwo_name, const char *comp_dir,
12657 ULONGEST signature, int is_debug_types)
12658 {
12659 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12660 struct objfile *objfile = dwarf2_per_objfile->objfile;
12661 const char *kind = is_debug_types ? "TU" : "CU";
12662 void **dwo_file_slot;
12663 struct dwo_file *dwo_file;
12664 struct dwp_file *dwp_file;
12665
12666 /* First see if there's a DWP file.
12667 If we have a DWP file but didn't find the DWO inside it, don't
12668 look for the original DWO file. It makes gdb behave differently
12669 depending on whether one is debugging in the build tree. */
12670
12671 dwp_file = get_dwp_file (dwarf2_per_objfile);
12672 if (dwp_file != NULL)
12673 {
12674 const struct dwp_hash_table *dwp_htab =
12675 is_debug_types ? dwp_file->tus : dwp_file->cus;
12676
12677 if (dwp_htab != NULL)
12678 {
12679 struct dwo_unit *dwo_cutu =
12680 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12681 signature, is_debug_types);
12682
12683 if (dwo_cutu != NULL)
12684 {
12685 if (dwarf_read_debug)
12686 {
12687 fprintf_unfiltered (gdb_stdlog,
12688 "Virtual DWO %s %s found: @%s\n",
12689 kind, hex_string (signature),
12690 host_address_to_string (dwo_cutu));
12691 }
12692 return dwo_cutu;
12693 }
12694 }
12695 }
12696 else
12697 {
12698 /* No DWP file, look for the DWO file. */
12699
12700 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12701 dwo_name, comp_dir);
12702 if (*dwo_file_slot == NULL)
12703 {
12704 /* Read in the file and build a table of the CUs/TUs it contains. */
12705 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12706 }
12707 /* NOTE: This will be NULL if unable to open the file. */
12708 dwo_file = (struct dwo_file *) *dwo_file_slot;
12709
12710 if (dwo_file != NULL)
12711 {
12712 struct dwo_unit *dwo_cutu = NULL;
12713
12714 if (is_debug_types && dwo_file->tus)
12715 {
12716 struct dwo_unit find_dwo_cutu;
12717
12718 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12719 find_dwo_cutu.signature = signature;
12720 dwo_cutu
12721 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12722 &find_dwo_cutu);
12723 }
12724 else if (!is_debug_types && dwo_file->cus)
12725 {
12726 struct dwo_unit find_dwo_cutu;
12727
12728 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12729 find_dwo_cutu.signature = signature;
12730 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12731 &find_dwo_cutu);
12732 }
12733
12734 if (dwo_cutu != NULL)
12735 {
12736 if (dwarf_read_debug)
12737 {
12738 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12739 kind, dwo_name, hex_string (signature),
12740 host_address_to_string (dwo_cutu));
12741 }
12742 return dwo_cutu;
12743 }
12744 }
12745 }
12746
12747 /* We didn't find it. This could mean a dwo_id mismatch, or
12748 someone deleted the DWO/DWP file, or the search path isn't set up
12749 correctly to find the file. */
12750
12751 if (dwarf_read_debug)
12752 {
12753 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12754 kind, dwo_name, hex_string (signature));
12755 }
12756
12757 /* This is a warning and not a complaint because it can be caused by
12758 pilot error (e.g., user accidentally deleting the DWO). */
12759 {
12760 /* Print the name of the DWP file if we looked there, helps the user
12761 better diagnose the problem. */
12762 std::string dwp_text;
12763
12764 if (dwp_file != NULL)
12765 dwp_text = string_printf (" [in DWP file %s]",
12766 lbasename (dwp_file->name));
12767
12768 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12769 " [in module %s]"),
12770 kind, dwo_name, hex_string (signature),
12771 dwp_text.c_str (),
12772 this_unit->is_debug_types ? "TU" : "CU",
12773 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12774 }
12775 return NULL;
12776 }
12777
12778 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12779 See lookup_dwo_cutu_unit for details. */
12780
12781 static struct dwo_unit *
12782 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12783 const char *dwo_name, const char *comp_dir,
12784 ULONGEST signature)
12785 {
12786 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12787 }
12788
12789 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12790 See lookup_dwo_cutu_unit for details. */
12791
12792 static struct dwo_unit *
12793 lookup_dwo_type_unit (struct signatured_type *this_tu,
12794 const char *dwo_name, const char *comp_dir)
12795 {
12796 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12797 }
12798
12799 /* Traversal function for queue_and_load_all_dwo_tus. */
12800
12801 static int
12802 queue_and_load_dwo_tu (void **slot, void *info)
12803 {
12804 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12805 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12806 ULONGEST signature = dwo_unit->signature;
12807 struct signatured_type *sig_type =
12808 lookup_dwo_signatured_type (per_cu->cu, signature);
12809
12810 if (sig_type != NULL)
12811 {
12812 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12813
12814 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12815 a real dependency of PER_CU on SIG_TYPE. That is detected later
12816 while processing PER_CU. */
12817 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12818 load_full_type_unit (sig_cu);
12819 per_cu->imported_symtabs_push (sig_cu);
12820 }
12821
12822 return 1;
12823 }
12824
12825 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12826 The DWO may have the only definition of the type, though it may not be
12827 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12828 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12829
12830 static void
12831 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12832 {
12833 struct dwo_unit *dwo_unit;
12834 struct dwo_file *dwo_file;
12835
12836 gdb_assert (!per_cu->is_debug_types);
12837 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12838 gdb_assert (per_cu->cu != NULL);
12839
12840 dwo_unit = per_cu->cu->dwo_unit;
12841 gdb_assert (dwo_unit != NULL);
12842
12843 dwo_file = dwo_unit->dwo_file;
12844 if (dwo_file->tus != NULL)
12845 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12846 per_cu);
12847 }
12848
12849 /* Read in various DIEs. */
12850
12851 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12852 Inherit only the children of the DW_AT_abstract_origin DIE not being
12853 already referenced by DW_AT_abstract_origin from the children of the
12854 current DIE. */
12855
12856 static void
12857 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12858 {
12859 struct die_info *child_die;
12860 sect_offset *offsetp;
12861 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12862 struct die_info *origin_die;
12863 /* Iterator of the ORIGIN_DIE children. */
12864 struct die_info *origin_child_die;
12865 struct attribute *attr;
12866 struct dwarf2_cu *origin_cu;
12867 struct pending **origin_previous_list_in_scope;
12868
12869 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12870 if (!attr)
12871 return;
12872
12873 /* Note that following die references may follow to a die in a
12874 different cu. */
12875
12876 origin_cu = cu;
12877 origin_die = follow_die_ref (die, attr, &origin_cu);
12878
12879 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12880 symbols in. */
12881 origin_previous_list_in_scope = origin_cu->list_in_scope;
12882 origin_cu->list_in_scope = cu->list_in_scope;
12883
12884 if (die->tag != origin_die->tag
12885 && !(die->tag == DW_TAG_inlined_subroutine
12886 && origin_die->tag == DW_TAG_subprogram))
12887 complaint (_("DIE %s and its abstract origin %s have different tags"),
12888 sect_offset_str (die->sect_off),
12889 sect_offset_str (origin_die->sect_off));
12890
12891 std::vector<sect_offset> offsets;
12892
12893 for (child_die = die->child;
12894 child_die && child_die->tag;
12895 child_die = child_die->sibling)
12896 {
12897 struct die_info *child_origin_die;
12898 struct dwarf2_cu *child_origin_cu;
12899
12900 /* We are trying to process concrete instance entries:
12901 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12902 it's not relevant to our analysis here. i.e. detecting DIEs that are
12903 present in the abstract instance but not referenced in the concrete
12904 one. */
12905 if (child_die->tag == DW_TAG_call_site
12906 || child_die->tag == DW_TAG_GNU_call_site)
12907 continue;
12908
12909 /* For each CHILD_DIE, find the corresponding child of
12910 ORIGIN_DIE. If there is more than one layer of
12911 DW_AT_abstract_origin, follow them all; there shouldn't be,
12912 but GCC versions at least through 4.4 generate this (GCC PR
12913 40573). */
12914 child_origin_die = child_die;
12915 child_origin_cu = cu;
12916 while (1)
12917 {
12918 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12919 child_origin_cu);
12920 if (attr == NULL)
12921 break;
12922 child_origin_die = follow_die_ref (child_origin_die, attr,
12923 &child_origin_cu);
12924 }
12925
12926 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12927 counterpart may exist. */
12928 if (child_origin_die != child_die)
12929 {
12930 if (child_die->tag != child_origin_die->tag
12931 && !(child_die->tag == DW_TAG_inlined_subroutine
12932 && child_origin_die->tag == DW_TAG_subprogram))
12933 complaint (_("Child DIE %s and its abstract origin %s have "
12934 "different tags"),
12935 sect_offset_str (child_die->sect_off),
12936 sect_offset_str (child_origin_die->sect_off));
12937 if (child_origin_die->parent != origin_die)
12938 complaint (_("Child DIE %s and its abstract origin %s have "
12939 "different parents"),
12940 sect_offset_str (child_die->sect_off),
12941 sect_offset_str (child_origin_die->sect_off));
12942 else
12943 offsets.push_back (child_origin_die->sect_off);
12944 }
12945 }
12946 std::sort (offsets.begin (), offsets.end ());
12947 sect_offset *offsets_end = offsets.data () + offsets.size ();
12948 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12949 if (offsetp[-1] == *offsetp)
12950 complaint (_("Multiple children of DIE %s refer "
12951 "to DIE %s as their abstract origin"),
12952 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12953
12954 offsetp = offsets.data ();
12955 origin_child_die = origin_die->child;
12956 while (origin_child_die && origin_child_die->tag)
12957 {
12958 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12959 while (offsetp < offsets_end
12960 && *offsetp < origin_child_die->sect_off)
12961 offsetp++;
12962 if (offsetp >= offsets_end
12963 || *offsetp > origin_child_die->sect_off)
12964 {
12965 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12966 Check whether we're already processing ORIGIN_CHILD_DIE.
12967 This can happen with mutually referenced abstract_origins.
12968 PR 16581. */
12969 if (!origin_child_die->in_process)
12970 process_die (origin_child_die, origin_cu);
12971 }
12972 origin_child_die = origin_child_die->sibling;
12973 }
12974 origin_cu->list_in_scope = origin_previous_list_in_scope;
12975
12976 if (cu != origin_cu)
12977 compute_delayed_physnames (origin_cu);
12978 }
12979
12980 static void
12981 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12982 {
12983 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12984 struct gdbarch *gdbarch = objfile->arch ();
12985 struct context_stack *newobj;
12986 CORE_ADDR lowpc;
12987 CORE_ADDR highpc;
12988 struct die_info *child_die;
12989 struct attribute *attr, *call_line, *call_file;
12990 const char *name;
12991 CORE_ADDR baseaddr;
12992 struct block *block;
12993 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12994 std::vector<struct symbol *> template_args;
12995 struct template_symbol *templ_func = NULL;
12996
12997 if (inlined_func)
12998 {
12999 /* If we do not have call site information, we can't show the
13000 caller of this inlined function. That's too confusing, so
13001 only use the scope for local variables. */
13002 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13003 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13004 if (call_line == NULL || call_file == NULL)
13005 {
13006 read_lexical_block_scope (die, cu);
13007 return;
13008 }
13009 }
13010
13011 baseaddr = objfile->text_section_offset ();
13012
13013 name = dwarf2_name (die, cu);
13014
13015 /* Ignore functions with missing or empty names. These are actually
13016 illegal according to the DWARF standard. */
13017 if (name == NULL)
13018 {
13019 complaint (_("missing name for subprogram DIE at %s"),
13020 sect_offset_str (die->sect_off));
13021 return;
13022 }
13023
13024 /* Ignore functions with missing or invalid low and high pc attributes. */
13025 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13026 <= PC_BOUNDS_INVALID)
13027 {
13028 attr = dwarf2_attr (die, DW_AT_external, cu);
13029 if (!attr || !DW_UNSND (attr))
13030 complaint (_("cannot get low and high bounds "
13031 "for subprogram DIE at %s"),
13032 sect_offset_str (die->sect_off));
13033 return;
13034 }
13035
13036 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13037 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13038
13039 /* If we have any template arguments, then we must allocate a
13040 different sort of symbol. */
13041 for (child_die = die->child; child_die; child_die = child_die->sibling)
13042 {
13043 if (child_die->tag == DW_TAG_template_type_param
13044 || child_die->tag == DW_TAG_template_value_param)
13045 {
13046 templ_func = new (&objfile->objfile_obstack) template_symbol;
13047 templ_func->subclass = SYMBOL_TEMPLATE;
13048 break;
13049 }
13050 }
13051
13052 newobj = cu->get_builder ()->push_context (0, lowpc);
13053 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13054 (struct symbol *) templ_func);
13055
13056 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13057 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13058 cu->language);
13059
13060 /* If there is a location expression for DW_AT_frame_base, record
13061 it. */
13062 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13063 if (attr != nullptr)
13064 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13065
13066 /* If there is a location for the static link, record it. */
13067 newobj->static_link = NULL;
13068 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13069 if (attr != nullptr)
13070 {
13071 newobj->static_link
13072 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13073 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13074 cu->per_cu->addr_type ());
13075 }
13076
13077 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13078
13079 if (die->child != NULL)
13080 {
13081 child_die = die->child;
13082 while (child_die && child_die->tag)
13083 {
13084 if (child_die->tag == DW_TAG_template_type_param
13085 || child_die->tag == DW_TAG_template_value_param)
13086 {
13087 struct symbol *arg = new_symbol (child_die, NULL, cu);
13088
13089 if (arg != NULL)
13090 template_args.push_back (arg);
13091 }
13092 else
13093 process_die (child_die, cu);
13094 child_die = child_die->sibling;
13095 }
13096 }
13097
13098 inherit_abstract_dies (die, cu);
13099
13100 /* If we have a DW_AT_specification, we might need to import using
13101 directives from the context of the specification DIE. See the
13102 comment in determine_prefix. */
13103 if (cu->language == language_cplus
13104 && dwarf2_attr (die, DW_AT_specification, cu))
13105 {
13106 struct dwarf2_cu *spec_cu = cu;
13107 struct die_info *spec_die = die_specification (die, &spec_cu);
13108
13109 while (spec_die)
13110 {
13111 child_die = spec_die->child;
13112 while (child_die && child_die->tag)
13113 {
13114 if (child_die->tag == DW_TAG_imported_module)
13115 process_die (child_die, spec_cu);
13116 child_die = child_die->sibling;
13117 }
13118
13119 /* In some cases, GCC generates specification DIEs that
13120 themselves contain DW_AT_specification attributes. */
13121 spec_die = die_specification (spec_die, &spec_cu);
13122 }
13123 }
13124
13125 struct context_stack cstk = cu->get_builder ()->pop_context ();
13126 /* Make a block for the local symbols within. */
13127 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13128 cstk.static_link, lowpc, highpc);
13129
13130 /* For C++, set the block's scope. */
13131 if ((cu->language == language_cplus
13132 || cu->language == language_fortran
13133 || cu->language == language_d
13134 || cu->language == language_rust)
13135 && cu->processing_has_namespace_info)
13136 block_set_scope (block, determine_prefix (die, cu),
13137 &objfile->objfile_obstack);
13138
13139 /* If we have address ranges, record them. */
13140 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13141
13142 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13143
13144 /* Attach template arguments to function. */
13145 if (!template_args.empty ())
13146 {
13147 gdb_assert (templ_func != NULL);
13148
13149 templ_func->n_template_arguments = template_args.size ();
13150 templ_func->template_arguments
13151 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13152 templ_func->n_template_arguments);
13153 memcpy (templ_func->template_arguments,
13154 template_args.data (),
13155 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13156
13157 /* Make sure that the symtab is set on the new symbols. Even
13158 though they don't appear in this symtab directly, other parts
13159 of gdb assume that symbols do, and this is reasonably
13160 true. */
13161 for (symbol *sym : template_args)
13162 symbol_set_symtab (sym, symbol_symtab (templ_func));
13163 }
13164
13165 /* In C++, we can have functions nested inside functions (e.g., when
13166 a function declares a class that has methods). This means that
13167 when we finish processing a function scope, we may need to go
13168 back to building a containing block's symbol lists. */
13169 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13170 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13171
13172 /* If we've finished processing a top-level function, subsequent
13173 symbols go in the file symbol list. */
13174 if (cu->get_builder ()->outermost_context_p ())
13175 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13176 }
13177
13178 /* Process all the DIES contained within a lexical block scope. Start
13179 a new scope, process the dies, and then close the scope. */
13180
13181 static void
13182 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13183 {
13184 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13185 struct gdbarch *gdbarch = objfile->arch ();
13186 CORE_ADDR lowpc, highpc;
13187 struct die_info *child_die;
13188 CORE_ADDR baseaddr;
13189
13190 baseaddr = objfile->text_section_offset ();
13191
13192 /* Ignore blocks with missing or invalid low and high pc attributes. */
13193 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13194 as multiple lexical blocks? Handling children in a sane way would
13195 be nasty. Might be easier to properly extend generic blocks to
13196 describe ranges. */
13197 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13198 {
13199 case PC_BOUNDS_NOT_PRESENT:
13200 /* DW_TAG_lexical_block has no attributes, process its children as if
13201 there was no wrapping by that DW_TAG_lexical_block.
13202 GCC does no longer produces such DWARF since GCC r224161. */
13203 for (child_die = die->child;
13204 child_die != NULL && child_die->tag;
13205 child_die = child_die->sibling)
13206 {
13207 /* We might already be processing this DIE. This can happen
13208 in an unusual circumstance -- where a subroutine A
13209 appears lexically in another subroutine B, but A actually
13210 inlines B. The recursion is broken here, rather than in
13211 inherit_abstract_dies, because it seems better to simply
13212 drop concrete children here. */
13213 if (!child_die->in_process)
13214 process_die (child_die, cu);
13215 }
13216 return;
13217 case PC_BOUNDS_INVALID:
13218 return;
13219 }
13220 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13221 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13222
13223 cu->get_builder ()->push_context (0, lowpc);
13224 if (die->child != NULL)
13225 {
13226 child_die = die->child;
13227 while (child_die && child_die->tag)
13228 {
13229 process_die (child_die, cu);
13230 child_die = child_die->sibling;
13231 }
13232 }
13233 inherit_abstract_dies (die, cu);
13234 struct context_stack cstk = cu->get_builder ()->pop_context ();
13235
13236 if (*cu->get_builder ()->get_local_symbols () != NULL
13237 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13238 {
13239 struct block *block
13240 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13241 cstk.start_addr, highpc);
13242
13243 /* Note that recording ranges after traversing children, as we
13244 do here, means that recording a parent's ranges entails
13245 walking across all its children's ranges as they appear in
13246 the address map, which is quadratic behavior.
13247
13248 It would be nicer to record the parent's ranges before
13249 traversing its children, simply overriding whatever you find
13250 there. But since we don't even decide whether to create a
13251 block until after we've traversed its children, that's hard
13252 to do. */
13253 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13254 }
13255 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13256 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13257 }
13258
13259 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13260
13261 static void
13262 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13263 {
13264 dwarf2_per_objfile *per_objfile = cu->per_cu->dwarf2_per_objfile;
13265 struct objfile *objfile = per_objfile->objfile;
13266 struct gdbarch *gdbarch = objfile->arch ();
13267 CORE_ADDR pc, baseaddr;
13268 struct attribute *attr;
13269 struct call_site *call_site, call_site_local;
13270 void **slot;
13271 int nparams;
13272 struct die_info *child_die;
13273
13274 baseaddr = objfile->text_section_offset ();
13275
13276 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13277 if (attr == NULL)
13278 {
13279 /* This was a pre-DWARF-5 GNU extension alias
13280 for DW_AT_call_return_pc. */
13281 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13282 }
13283 if (!attr)
13284 {
13285 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13286 "DIE %s [in module %s]"),
13287 sect_offset_str (die->sect_off), objfile_name (objfile));
13288 return;
13289 }
13290 pc = attr->value_as_address () + baseaddr;
13291 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13292
13293 if (cu->call_site_htab == NULL)
13294 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13295 NULL, &objfile->objfile_obstack,
13296 hashtab_obstack_allocate, NULL);
13297 call_site_local.pc = pc;
13298 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13299 if (*slot != NULL)
13300 {
13301 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13302 "DIE %s [in module %s]"),
13303 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13304 objfile_name (objfile));
13305 return;
13306 }
13307
13308 /* Count parameters at the caller. */
13309
13310 nparams = 0;
13311 for (child_die = die->child; child_die && child_die->tag;
13312 child_die = child_die->sibling)
13313 {
13314 if (child_die->tag != DW_TAG_call_site_parameter
13315 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13316 {
13317 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13318 "DW_TAG_call_site child DIE %s [in module %s]"),
13319 child_die->tag, sect_offset_str (child_die->sect_off),
13320 objfile_name (objfile));
13321 continue;
13322 }
13323
13324 nparams++;
13325 }
13326
13327 call_site
13328 = ((struct call_site *)
13329 obstack_alloc (&objfile->objfile_obstack,
13330 sizeof (*call_site)
13331 + (sizeof (*call_site->parameter) * (nparams - 1))));
13332 *slot = call_site;
13333 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13334 call_site->pc = pc;
13335
13336 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13337 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13338 {
13339 struct die_info *func_die;
13340
13341 /* Skip also over DW_TAG_inlined_subroutine. */
13342 for (func_die = die->parent;
13343 func_die && func_die->tag != DW_TAG_subprogram
13344 && func_die->tag != DW_TAG_subroutine_type;
13345 func_die = func_die->parent);
13346
13347 /* DW_AT_call_all_calls is a superset
13348 of DW_AT_call_all_tail_calls. */
13349 if (func_die
13350 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13351 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13352 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13353 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13354 {
13355 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13356 not complete. But keep CALL_SITE for look ups via call_site_htab,
13357 both the initial caller containing the real return address PC and
13358 the final callee containing the current PC of a chain of tail
13359 calls do not need to have the tail call list complete. But any
13360 function candidate for a virtual tail call frame searched via
13361 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13362 determined unambiguously. */
13363 }
13364 else
13365 {
13366 struct type *func_type = NULL;
13367
13368 if (func_die)
13369 func_type = get_die_type (func_die, cu);
13370 if (func_type != NULL)
13371 {
13372 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13373
13374 /* Enlist this call site to the function. */
13375 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13376 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13377 }
13378 else
13379 complaint (_("Cannot find function owning DW_TAG_call_site "
13380 "DIE %s [in module %s]"),
13381 sect_offset_str (die->sect_off), objfile_name (objfile));
13382 }
13383 }
13384
13385 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13386 if (attr == NULL)
13387 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13388 if (attr == NULL)
13389 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13390 if (attr == NULL)
13391 {
13392 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13393 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13394 }
13395 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13396 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13397 /* Keep NULL DWARF_BLOCK. */;
13398 else if (attr->form_is_block ())
13399 {
13400 struct dwarf2_locexpr_baton *dlbaton;
13401
13402 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13403 dlbaton->data = DW_BLOCK (attr)->data;
13404 dlbaton->size = DW_BLOCK (attr)->size;
13405 dlbaton->per_objfile = per_objfile;
13406 dlbaton->per_cu = cu->per_cu;
13407
13408 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13409 }
13410 else if (attr->form_is_ref ())
13411 {
13412 struct dwarf2_cu *target_cu = cu;
13413 struct die_info *target_die;
13414
13415 target_die = follow_die_ref (die, attr, &target_cu);
13416 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13417 if (die_is_declaration (target_die, target_cu))
13418 {
13419 const char *target_physname;
13420
13421 /* Prefer the mangled name; otherwise compute the demangled one. */
13422 target_physname = dw2_linkage_name (target_die, target_cu);
13423 if (target_physname == NULL)
13424 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13425 if (target_physname == NULL)
13426 complaint (_("DW_AT_call_target target DIE has invalid "
13427 "physname, for referencing DIE %s [in module %s]"),
13428 sect_offset_str (die->sect_off), objfile_name (objfile));
13429 else
13430 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13431 }
13432 else
13433 {
13434 CORE_ADDR lowpc;
13435
13436 /* DW_AT_entry_pc should be preferred. */
13437 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13438 <= PC_BOUNDS_INVALID)
13439 complaint (_("DW_AT_call_target target DIE has invalid "
13440 "low pc, for referencing DIE %s [in module %s]"),
13441 sect_offset_str (die->sect_off), objfile_name (objfile));
13442 else
13443 {
13444 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13445 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13446 }
13447 }
13448 }
13449 else
13450 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13451 "block nor reference, for DIE %s [in module %s]"),
13452 sect_offset_str (die->sect_off), objfile_name (objfile));
13453
13454 call_site->per_cu = cu->per_cu;
13455
13456 for (child_die = die->child;
13457 child_die && child_die->tag;
13458 child_die = child_die->sibling)
13459 {
13460 struct call_site_parameter *parameter;
13461 struct attribute *loc, *origin;
13462
13463 if (child_die->tag != DW_TAG_call_site_parameter
13464 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13465 {
13466 /* Already printed the complaint above. */
13467 continue;
13468 }
13469
13470 gdb_assert (call_site->parameter_count < nparams);
13471 parameter = &call_site->parameter[call_site->parameter_count];
13472
13473 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13474 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13475 register is contained in DW_AT_call_value. */
13476
13477 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13478 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13479 if (origin == NULL)
13480 {
13481 /* This was a pre-DWARF-5 GNU extension alias
13482 for DW_AT_call_parameter. */
13483 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13484 }
13485 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13486 {
13487 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13488
13489 sect_offset sect_off = origin->get_ref_die_offset ();
13490 if (!cu->header.offset_in_cu_p (sect_off))
13491 {
13492 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13493 binding can be done only inside one CU. Such referenced DIE
13494 therefore cannot be even moved to DW_TAG_partial_unit. */
13495 complaint (_("DW_AT_call_parameter offset is not in CU for "
13496 "DW_TAG_call_site child DIE %s [in module %s]"),
13497 sect_offset_str (child_die->sect_off),
13498 objfile_name (objfile));
13499 continue;
13500 }
13501 parameter->u.param_cu_off
13502 = (cu_offset) (sect_off - cu->header.sect_off);
13503 }
13504 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13505 {
13506 complaint (_("No DW_FORM_block* DW_AT_location for "
13507 "DW_TAG_call_site child DIE %s [in module %s]"),
13508 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13509 continue;
13510 }
13511 else
13512 {
13513 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13514 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13515 if (parameter->u.dwarf_reg != -1)
13516 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13517 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13518 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13519 &parameter->u.fb_offset))
13520 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13521 else
13522 {
13523 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13524 "for DW_FORM_block* DW_AT_location is supported for "
13525 "DW_TAG_call_site child DIE %s "
13526 "[in module %s]"),
13527 sect_offset_str (child_die->sect_off),
13528 objfile_name (objfile));
13529 continue;
13530 }
13531 }
13532
13533 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13534 if (attr == NULL)
13535 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13536 if (attr == NULL || !attr->form_is_block ())
13537 {
13538 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13539 "DW_TAG_call_site child DIE %s [in module %s]"),
13540 sect_offset_str (child_die->sect_off),
13541 objfile_name (objfile));
13542 continue;
13543 }
13544 parameter->value = DW_BLOCK (attr)->data;
13545 parameter->value_size = DW_BLOCK (attr)->size;
13546
13547 /* Parameters are not pre-cleared by memset above. */
13548 parameter->data_value = NULL;
13549 parameter->data_value_size = 0;
13550 call_site->parameter_count++;
13551
13552 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13553 if (attr == NULL)
13554 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13555 if (attr != nullptr)
13556 {
13557 if (!attr->form_is_block ())
13558 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13559 "DW_TAG_call_site child DIE %s [in module %s]"),
13560 sect_offset_str (child_die->sect_off),
13561 objfile_name (objfile));
13562 else
13563 {
13564 parameter->data_value = DW_BLOCK (attr)->data;
13565 parameter->data_value_size = DW_BLOCK (attr)->size;
13566 }
13567 }
13568 }
13569 }
13570
13571 /* Helper function for read_variable. If DIE represents a virtual
13572 table, then return the type of the concrete object that is
13573 associated with the virtual table. Otherwise, return NULL. */
13574
13575 static struct type *
13576 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13577 {
13578 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13579 if (attr == NULL)
13580 return NULL;
13581
13582 /* Find the type DIE. */
13583 struct die_info *type_die = NULL;
13584 struct dwarf2_cu *type_cu = cu;
13585
13586 if (attr->form_is_ref ())
13587 type_die = follow_die_ref (die, attr, &type_cu);
13588 if (type_die == NULL)
13589 return NULL;
13590
13591 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13592 return NULL;
13593 return die_containing_type (type_die, type_cu);
13594 }
13595
13596 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13597
13598 static void
13599 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13600 {
13601 struct rust_vtable_symbol *storage = NULL;
13602
13603 if (cu->language == language_rust)
13604 {
13605 struct type *containing_type = rust_containing_type (die, cu);
13606
13607 if (containing_type != NULL)
13608 {
13609 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13610
13611 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13612 storage->concrete_type = containing_type;
13613 storage->subclass = SYMBOL_RUST_VTABLE;
13614 }
13615 }
13616
13617 struct symbol *res = new_symbol (die, NULL, cu, storage);
13618 struct attribute *abstract_origin
13619 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13620 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13621 if (res == NULL && loc && abstract_origin)
13622 {
13623 /* We have a variable without a name, but with a location and an abstract
13624 origin. This may be a concrete instance of an abstract variable
13625 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13626 later. */
13627 struct dwarf2_cu *origin_cu = cu;
13628 struct die_info *origin_die
13629 = follow_die_ref (die, abstract_origin, &origin_cu);
13630 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13631 dpo->per_bfd->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13632 }
13633 }
13634
13635 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13636 reading .debug_rnglists.
13637 Callback's type should be:
13638 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13639 Return true if the attributes are present and valid, otherwise,
13640 return false. */
13641
13642 template <typename Callback>
13643 static bool
13644 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13645 Callback &&callback)
13646 {
13647 struct dwarf2_per_objfile *dwarf2_per_objfile
13648 = cu->per_cu->dwarf2_per_objfile;
13649 struct objfile *objfile = dwarf2_per_objfile->objfile;
13650 bfd *obfd = objfile->obfd;
13651 /* Base address selection entry. */
13652 gdb::optional<CORE_ADDR> base;
13653 const gdb_byte *buffer;
13654 CORE_ADDR baseaddr;
13655 bool overflow = false;
13656
13657 base = cu->base_address;
13658
13659 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13660 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13661 {
13662 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13663 offset);
13664 return false;
13665 }
13666 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13667
13668 baseaddr = objfile->text_section_offset ();
13669
13670 while (1)
13671 {
13672 /* Initialize it due to a false compiler warning. */
13673 CORE_ADDR range_beginning = 0, range_end = 0;
13674 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13675 + dwarf2_per_objfile->per_bfd->rnglists.size);
13676 unsigned int bytes_read;
13677
13678 if (buffer == buf_end)
13679 {
13680 overflow = true;
13681 break;
13682 }
13683 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13684 switch (rlet)
13685 {
13686 case DW_RLE_end_of_list:
13687 break;
13688 case DW_RLE_base_address:
13689 if (buffer + cu->header.addr_size > buf_end)
13690 {
13691 overflow = true;
13692 break;
13693 }
13694 base = cu->header.read_address (obfd, buffer, &bytes_read);
13695 buffer += bytes_read;
13696 break;
13697 case DW_RLE_start_length:
13698 if (buffer + cu->header.addr_size > buf_end)
13699 {
13700 overflow = true;
13701 break;
13702 }
13703 range_beginning = cu->header.read_address (obfd, buffer,
13704 &bytes_read);
13705 buffer += bytes_read;
13706 range_end = (range_beginning
13707 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13708 buffer += bytes_read;
13709 if (buffer > buf_end)
13710 {
13711 overflow = true;
13712 break;
13713 }
13714 break;
13715 case DW_RLE_offset_pair:
13716 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13717 buffer += bytes_read;
13718 if (buffer > buf_end)
13719 {
13720 overflow = true;
13721 break;
13722 }
13723 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13724 buffer += bytes_read;
13725 if (buffer > buf_end)
13726 {
13727 overflow = true;
13728 break;
13729 }
13730 break;
13731 case DW_RLE_start_end:
13732 if (buffer + 2 * cu->header.addr_size > buf_end)
13733 {
13734 overflow = true;
13735 break;
13736 }
13737 range_beginning = cu->header.read_address (obfd, buffer,
13738 &bytes_read);
13739 buffer += bytes_read;
13740 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13741 buffer += bytes_read;
13742 break;
13743 default:
13744 complaint (_("Invalid .debug_rnglists data (no base address)"));
13745 return false;
13746 }
13747 if (rlet == DW_RLE_end_of_list || overflow)
13748 break;
13749 if (rlet == DW_RLE_base_address)
13750 continue;
13751
13752 if (!base.has_value ())
13753 {
13754 /* We have no valid base address for the ranges
13755 data. */
13756 complaint (_("Invalid .debug_rnglists data (no base address)"));
13757 return false;
13758 }
13759
13760 if (range_beginning > range_end)
13761 {
13762 /* Inverted range entries are invalid. */
13763 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13764 return false;
13765 }
13766
13767 /* Empty range entries have no effect. */
13768 if (range_beginning == range_end)
13769 continue;
13770
13771 range_beginning += *base;
13772 range_end += *base;
13773
13774 /* A not-uncommon case of bad debug info.
13775 Don't pollute the addrmap with bad data. */
13776 if (range_beginning + baseaddr == 0
13777 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13778 {
13779 complaint (_(".debug_rnglists entry has start address of zero"
13780 " [in module %s]"), objfile_name (objfile));
13781 continue;
13782 }
13783
13784 callback (range_beginning, range_end);
13785 }
13786
13787 if (overflow)
13788 {
13789 complaint (_("Offset %d is not terminated "
13790 "for DW_AT_ranges attribute"),
13791 offset);
13792 return false;
13793 }
13794
13795 return true;
13796 }
13797
13798 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13799 Callback's type should be:
13800 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13801 Return 1 if the attributes are present and valid, otherwise, return 0. */
13802
13803 template <typename Callback>
13804 static int
13805 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13806 Callback &&callback)
13807 {
13808 struct dwarf2_per_objfile *dwarf2_per_objfile
13809 = cu->per_cu->dwarf2_per_objfile;
13810 struct objfile *objfile = dwarf2_per_objfile->objfile;
13811 struct comp_unit_head *cu_header = &cu->header;
13812 bfd *obfd = objfile->obfd;
13813 unsigned int addr_size = cu_header->addr_size;
13814 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13815 /* Base address selection entry. */
13816 gdb::optional<CORE_ADDR> base;
13817 unsigned int dummy;
13818 const gdb_byte *buffer;
13819 CORE_ADDR baseaddr;
13820
13821 if (cu_header->version >= 5)
13822 return dwarf2_rnglists_process (offset, cu, callback);
13823
13824 base = cu->base_address;
13825
13826 dwarf2_per_objfile->per_bfd->ranges.read (objfile);
13827 if (offset >= dwarf2_per_objfile->per_bfd->ranges.size)
13828 {
13829 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13830 offset);
13831 return 0;
13832 }
13833 buffer = dwarf2_per_objfile->per_bfd->ranges.buffer + offset;
13834
13835 baseaddr = objfile->text_section_offset ();
13836
13837 while (1)
13838 {
13839 CORE_ADDR range_beginning, range_end;
13840
13841 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13842 buffer += addr_size;
13843 range_end = cu->header.read_address (obfd, buffer, &dummy);
13844 buffer += addr_size;
13845 offset += 2 * addr_size;
13846
13847 /* An end of list marker is a pair of zero addresses. */
13848 if (range_beginning == 0 && range_end == 0)
13849 /* Found the end of list entry. */
13850 break;
13851
13852 /* Each base address selection entry is a pair of 2 values.
13853 The first is the largest possible address, the second is
13854 the base address. Check for a base address here. */
13855 if ((range_beginning & mask) == mask)
13856 {
13857 /* If we found the largest possible address, then we already
13858 have the base address in range_end. */
13859 base = range_end;
13860 continue;
13861 }
13862
13863 if (!base.has_value ())
13864 {
13865 /* We have no valid base address for the ranges
13866 data. */
13867 complaint (_("Invalid .debug_ranges data (no base address)"));
13868 return 0;
13869 }
13870
13871 if (range_beginning > range_end)
13872 {
13873 /* Inverted range entries are invalid. */
13874 complaint (_("Invalid .debug_ranges data (inverted range)"));
13875 return 0;
13876 }
13877
13878 /* Empty range entries have no effect. */
13879 if (range_beginning == range_end)
13880 continue;
13881
13882 range_beginning += *base;
13883 range_end += *base;
13884
13885 /* A not-uncommon case of bad debug info.
13886 Don't pollute the addrmap with bad data. */
13887 if (range_beginning + baseaddr == 0
13888 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13889 {
13890 complaint (_(".debug_ranges entry has start address of zero"
13891 " [in module %s]"), objfile_name (objfile));
13892 continue;
13893 }
13894
13895 callback (range_beginning, range_end);
13896 }
13897
13898 return 1;
13899 }
13900
13901 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13902 Return 1 if the attributes are present and valid, otherwise, return 0.
13903 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13904
13905 static int
13906 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13907 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13908 dwarf2_psymtab *ranges_pst)
13909 {
13910 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13911 struct gdbarch *gdbarch = objfile->arch ();
13912 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13913 int low_set = 0;
13914 CORE_ADDR low = 0;
13915 CORE_ADDR high = 0;
13916 int retval;
13917
13918 retval = dwarf2_ranges_process (offset, cu,
13919 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13920 {
13921 if (ranges_pst != NULL)
13922 {
13923 CORE_ADDR lowpc;
13924 CORE_ADDR highpc;
13925
13926 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13927 range_beginning + baseaddr)
13928 - baseaddr);
13929 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13930 range_end + baseaddr)
13931 - baseaddr);
13932 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13933 lowpc, highpc - 1, ranges_pst);
13934 }
13935
13936 /* FIXME: This is recording everything as a low-high
13937 segment of consecutive addresses. We should have a
13938 data structure for discontiguous block ranges
13939 instead. */
13940 if (! low_set)
13941 {
13942 low = range_beginning;
13943 high = range_end;
13944 low_set = 1;
13945 }
13946 else
13947 {
13948 if (range_beginning < low)
13949 low = range_beginning;
13950 if (range_end > high)
13951 high = range_end;
13952 }
13953 });
13954 if (!retval)
13955 return 0;
13956
13957 if (! low_set)
13958 /* If the first entry is an end-of-list marker, the range
13959 describes an empty scope, i.e. no instructions. */
13960 return 0;
13961
13962 if (low_return)
13963 *low_return = low;
13964 if (high_return)
13965 *high_return = high;
13966 return 1;
13967 }
13968
13969 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13970 definition for the return value. *LOWPC and *HIGHPC are set iff
13971 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13972
13973 static enum pc_bounds_kind
13974 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13975 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13976 dwarf2_psymtab *pst)
13977 {
13978 struct dwarf2_per_objfile *dwarf2_per_objfile
13979 = cu->per_cu->dwarf2_per_objfile;
13980 struct attribute *attr;
13981 struct attribute *attr_high;
13982 CORE_ADDR low = 0;
13983 CORE_ADDR high = 0;
13984 enum pc_bounds_kind ret;
13985
13986 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13987 if (attr_high)
13988 {
13989 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13990 if (attr != nullptr)
13991 {
13992 low = attr->value_as_address ();
13993 high = attr_high->value_as_address ();
13994 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13995 high += low;
13996 }
13997 else
13998 /* Found high w/o low attribute. */
13999 return PC_BOUNDS_INVALID;
14000
14001 /* Found consecutive range of addresses. */
14002 ret = PC_BOUNDS_HIGH_LOW;
14003 }
14004 else
14005 {
14006 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14007 if (attr != NULL)
14008 {
14009 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14010 We take advantage of the fact that DW_AT_ranges does not appear
14011 in DW_TAG_compile_unit of DWO files. */
14012 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14013 unsigned int ranges_offset = (DW_UNSND (attr)
14014 + (need_ranges_base
14015 ? cu->ranges_base
14016 : 0));
14017
14018 /* Value of the DW_AT_ranges attribute is the offset in the
14019 .debug_ranges section. */
14020 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14021 return PC_BOUNDS_INVALID;
14022 /* Found discontinuous range of addresses. */
14023 ret = PC_BOUNDS_RANGES;
14024 }
14025 else
14026 return PC_BOUNDS_NOT_PRESENT;
14027 }
14028
14029 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14030 if (high <= low)
14031 return PC_BOUNDS_INVALID;
14032
14033 /* When using the GNU linker, .gnu.linkonce. sections are used to
14034 eliminate duplicate copies of functions and vtables and such.
14035 The linker will arbitrarily choose one and discard the others.
14036 The AT_*_pc values for such functions refer to local labels in
14037 these sections. If the section from that file was discarded, the
14038 labels are not in the output, so the relocs get a value of 0.
14039 If this is a discarded function, mark the pc bounds as invalid,
14040 so that GDB will ignore it. */
14041 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14042 return PC_BOUNDS_INVALID;
14043
14044 *lowpc = low;
14045 if (highpc)
14046 *highpc = high;
14047 return ret;
14048 }
14049
14050 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14051 its low and high PC addresses. Do nothing if these addresses could not
14052 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14053 and HIGHPC to the high address if greater than HIGHPC. */
14054
14055 static void
14056 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14057 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14058 struct dwarf2_cu *cu)
14059 {
14060 CORE_ADDR low, high;
14061 struct die_info *child = die->child;
14062
14063 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14064 {
14065 *lowpc = std::min (*lowpc, low);
14066 *highpc = std::max (*highpc, high);
14067 }
14068
14069 /* If the language does not allow nested subprograms (either inside
14070 subprograms or lexical blocks), we're done. */
14071 if (cu->language != language_ada)
14072 return;
14073
14074 /* Check all the children of the given DIE. If it contains nested
14075 subprograms, then check their pc bounds. Likewise, we need to
14076 check lexical blocks as well, as they may also contain subprogram
14077 definitions. */
14078 while (child && child->tag)
14079 {
14080 if (child->tag == DW_TAG_subprogram
14081 || child->tag == DW_TAG_lexical_block)
14082 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14083 child = child->sibling;
14084 }
14085 }
14086
14087 /* Get the low and high pc's represented by the scope DIE, and store
14088 them in *LOWPC and *HIGHPC. If the correct values can't be
14089 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14090
14091 static void
14092 get_scope_pc_bounds (struct die_info *die,
14093 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14094 struct dwarf2_cu *cu)
14095 {
14096 CORE_ADDR best_low = (CORE_ADDR) -1;
14097 CORE_ADDR best_high = (CORE_ADDR) 0;
14098 CORE_ADDR current_low, current_high;
14099
14100 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14101 >= PC_BOUNDS_RANGES)
14102 {
14103 best_low = current_low;
14104 best_high = current_high;
14105 }
14106 else
14107 {
14108 struct die_info *child = die->child;
14109
14110 while (child && child->tag)
14111 {
14112 switch (child->tag) {
14113 case DW_TAG_subprogram:
14114 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14115 break;
14116 case DW_TAG_namespace:
14117 case DW_TAG_module:
14118 /* FIXME: carlton/2004-01-16: Should we do this for
14119 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14120 that current GCC's always emit the DIEs corresponding
14121 to definitions of methods of classes as children of a
14122 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14123 the DIEs giving the declarations, which could be
14124 anywhere). But I don't see any reason why the
14125 standards says that they have to be there. */
14126 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14127
14128 if (current_low != ((CORE_ADDR) -1))
14129 {
14130 best_low = std::min (best_low, current_low);
14131 best_high = std::max (best_high, current_high);
14132 }
14133 break;
14134 default:
14135 /* Ignore. */
14136 break;
14137 }
14138
14139 child = child->sibling;
14140 }
14141 }
14142
14143 *lowpc = best_low;
14144 *highpc = best_high;
14145 }
14146
14147 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14148 in DIE. */
14149
14150 static void
14151 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14152 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14153 {
14154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14155 struct gdbarch *gdbarch = objfile->arch ();
14156 struct attribute *attr;
14157 struct attribute *attr_high;
14158
14159 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14160 if (attr_high)
14161 {
14162 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14163 if (attr != nullptr)
14164 {
14165 CORE_ADDR low = attr->value_as_address ();
14166 CORE_ADDR high = attr_high->value_as_address ();
14167
14168 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14169 high += low;
14170
14171 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14172 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14173 cu->get_builder ()->record_block_range (block, low, high - 1);
14174 }
14175 }
14176
14177 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14178 if (attr != nullptr)
14179 {
14180 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14181 We take advantage of the fact that DW_AT_ranges does not appear
14182 in DW_TAG_compile_unit of DWO files. */
14183 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14184
14185 /* The value of the DW_AT_ranges attribute is the offset of the
14186 address range list in the .debug_ranges section. */
14187 unsigned long offset = (DW_UNSND (attr)
14188 + (need_ranges_base ? cu->ranges_base : 0));
14189
14190 std::vector<blockrange> blockvec;
14191 dwarf2_ranges_process (offset, cu,
14192 [&] (CORE_ADDR start, CORE_ADDR end)
14193 {
14194 start += baseaddr;
14195 end += baseaddr;
14196 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14197 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14198 cu->get_builder ()->record_block_range (block, start, end - 1);
14199 blockvec.emplace_back (start, end);
14200 });
14201
14202 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14203 }
14204 }
14205
14206 /* Check whether the producer field indicates either of GCC < 4.6, or the
14207 Intel C/C++ compiler, and cache the result in CU. */
14208
14209 static void
14210 check_producer (struct dwarf2_cu *cu)
14211 {
14212 int major, minor;
14213
14214 if (cu->producer == NULL)
14215 {
14216 /* For unknown compilers expect their behavior is DWARF version
14217 compliant.
14218
14219 GCC started to support .debug_types sections by -gdwarf-4 since
14220 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14221 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14222 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14223 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14224 }
14225 else if (producer_is_gcc (cu->producer, &major, &minor))
14226 {
14227 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14228 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14229 }
14230 else if (producer_is_icc (cu->producer, &major, &minor))
14231 {
14232 cu->producer_is_icc = true;
14233 cu->producer_is_icc_lt_14 = major < 14;
14234 }
14235 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14236 cu->producer_is_codewarrior = true;
14237 else
14238 {
14239 /* For other non-GCC compilers, expect their behavior is DWARF version
14240 compliant. */
14241 }
14242
14243 cu->checked_producer = true;
14244 }
14245
14246 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14247 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14248 during 4.6.0 experimental. */
14249
14250 static bool
14251 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14252 {
14253 if (!cu->checked_producer)
14254 check_producer (cu);
14255
14256 return cu->producer_is_gxx_lt_4_6;
14257 }
14258
14259
14260 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14261 with incorrect is_stmt attributes. */
14262
14263 static bool
14264 producer_is_codewarrior (struct dwarf2_cu *cu)
14265 {
14266 if (!cu->checked_producer)
14267 check_producer (cu);
14268
14269 return cu->producer_is_codewarrior;
14270 }
14271
14272 /* Return the default accessibility type if it is not overridden by
14273 DW_AT_accessibility. */
14274
14275 static enum dwarf_access_attribute
14276 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14277 {
14278 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14279 {
14280 /* The default DWARF 2 accessibility for members is public, the default
14281 accessibility for inheritance is private. */
14282
14283 if (die->tag != DW_TAG_inheritance)
14284 return DW_ACCESS_public;
14285 else
14286 return DW_ACCESS_private;
14287 }
14288 else
14289 {
14290 /* DWARF 3+ defines the default accessibility a different way. The same
14291 rules apply now for DW_TAG_inheritance as for the members and it only
14292 depends on the container kind. */
14293
14294 if (die->parent->tag == DW_TAG_class_type)
14295 return DW_ACCESS_private;
14296 else
14297 return DW_ACCESS_public;
14298 }
14299 }
14300
14301 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14302 offset. If the attribute was not found return 0, otherwise return
14303 1. If it was found but could not properly be handled, set *OFFSET
14304 to 0. */
14305
14306 static int
14307 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14308 LONGEST *offset)
14309 {
14310 struct attribute *attr;
14311
14312 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14313 if (attr != NULL)
14314 {
14315 *offset = 0;
14316
14317 /* Note that we do not check for a section offset first here.
14318 This is because DW_AT_data_member_location is new in DWARF 4,
14319 so if we see it, we can assume that a constant form is really
14320 a constant and not a section offset. */
14321 if (attr->form_is_constant ())
14322 *offset = attr->constant_value (0);
14323 else if (attr->form_is_section_offset ())
14324 dwarf2_complex_location_expr_complaint ();
14325 else if (attr->form_is_block ())
14326 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14327 else
14328 dwarf2_complex_location_expr_complaint ();
14329
14330 return 1;
14331 }
14332
14333 return 0;
14334 }
14335
14336 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14337
14338 static void
14339 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14340 struct field *field)
14341 {
14342 struct attribute *attr;
14343
14344 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14345 if (attr != NULL)
14346 {
14347 if (attr->form_is_constant ())
14348 {
14349 LONGEST offset = attr->constant_value (0);
14350 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14351 }
14352 else if (attr->form_is_section_offset ())
14353 dwarf2_complex_location_expr_complaint ();
14354 else if (attr->form_is_block ())
14355 {
14356 bool handled;
14357 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14358 if (handled)
14359 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14360 else
14361 {
14362 struct objfile *objfile
14363 = cu->per_cu->dwarf2_per_objfile->objfile;
14364 struct dwarf2_locexpr_baton *dlbaton
14365 = XOBNEW (&objfile->objfile_obstack,
14366 struct dwarf2_locexpr_baton);
14367 dlbaton->data = DW_BLOCK (attr)->data;
14368 dlbaton->size = DW_BLOCK (attr)->size;
14369 /* When using this baton, we want to compute the address
14370 of the field, not the value. This is why
14371 is_reference is set to false here. */
14372 dlbaton->is_reference = false;
14373 dlbaton->per_objfile = cu->per_cu->dwarf2_per_objfile;
14374 dlbaton->per_cu = cu->per_cu;
14375
14376 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14377 }
14378 }
14379 else
14380 dwarf2_complex_location_expr_complaint ();
14381 }
14382 }
14383
14384 /* Add an aggregate field to the field list. */
14385
14386 static void
14387 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14388 struct dwarf2_cu *cu)
14389 {
14390 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14391 struct gdbarch *gdbarch = objfile->arch ();
14392 struct nextfield *new_field;
14393 struct attribute *attr;
14394 struct field *fp;
14395 const char *fieldname = "";
14396
14397 if (die->tag == DW_TAG_inheritance)
14398 {
14399 fip->baseclasses.emplace_back ();
14400 new_field = &fip->baseclasses.back ();
14401 }
14402 else
14403 {
14404 fip->fields.emplace_back ();
14405 new_field = &fip->fields.back ();
14406 }
14407
14408 new_field->offset = die->sect_off;
14409
14410 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14411 if (attr != nullptr)
14412 new_field->accessibility = DW_UNSND (attr);
14413 else
14414 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14415 if (new_field->accessibility != DW_ACCESS_public)
14416 fip->non_public_fields = 1;
14417
14418 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14419 if (attr != nullptr)
14420 new_field->virtuality = DW_UNSND (attr);
14421 else
14422 new_field->virtuality = DW_VIRTUALITY_none;
14423
14424 fp = &new_field->field;
14425
14426 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14427 {
14428 /* Data member other than a C++ static data member. */
14429
14430 /* Get type of field. */
14431 fp->type = die_type (die, cu);
14432
14433 SET_FIELD_BITPOS (*fp, 0);
14434
14435 /* Get bit size of field (zero if none). */
14436 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14437 if (attr != nullptr)
14438 {
14439 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14440 }
14441 else
14442 {
14443 FIELD_BITSIZE (*fp) = 0;
14444 }
14445
14446 /* Get bit offset of field. */
14447 handle_data_member_location (die, cu, fp);
14448 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14449 if (attr != nullptr)
14450 {
14451 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14452 {
14453 /* For big endian bits, the DW_AT_bit_offset gives the
14454 additional bit offset from the MSB of the containing
14455 anonymous object to the MSB of the field. We don't
14456 have to do anything special since we don't need to
14457 know the size of the anonymous object. */
14458 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14459 }
14460 else
14461 {
14462 /* For little endian bits, compute the bit offset to the
14463 MSB of the anonymous object, subtract off the number of
14464 bits from the MSB of the field to the MSB of the
14465 object, and then subtract off the number of bits of
14466 the field itself. The result is the bit offset of
14467 the LSB of the field. */
14468 int anonymous_size;
14469 int bit_offset = DW_UNSND (attr);
14470
14471 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14472 if (attr != nullptr)
14473 {
14474 /* The size of the anonymous object containing
14475 the bit field is explicit, so use the
14476 indicated size (in bytes). */
14477 anonymous_size = DW_UNSND (attr);
14478 }
14479 else
14480 {
14481 /* The size of the anonymous object containing
14482 the bit field must be inferred from the type
14483 attribute of the data member containing the
14484 bit field. */
14485 anonymous_size = TYPE_LENGTH (fp->type);
14486 }
14487 SET_FIELD_BITPOS (*fp,
14488 (FIELD_BITPOS (*fp)
14489 + anonymous_size * bits_per_byte
14490 - bit_offset - FIELD_BITSIZE (*fp)));
14491 }
14492 }
14493 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14494 if (attr != NULL)
14495 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14496 + attr->constant_value (0)));
14497
14498 /* Get name of field. */
14499 fieldname = dwarf2_name (die, cu);
14500 if (fieldname == NULL)
14501 fieldname = "";
14502
14503 /* The name is already allocated along with this objfile, so we don't
14504 need to duplicate it for the type. */
14505 fp->name = fieldname;
14506
14507 /* Change accessibility for artificial fields (e.g. virtual table
14508 pointer or virtual base class pointer) to private. */
14509 if (dwarf2_attr (die, DW_AT_artificial, cu))
14510 {
14511 FIELD_ARTIFICIAL (*fp) = 1;
14512 new_field->accessibility = DW_ACCESS_private;
14513 fip->non_public_fields = 1;
14514 }
14515 }
14516 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14517 {
14518 /* C++ static member. */
14519
14520 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14521 is a declaration, but all versions of G++ as of this writing
14522 (so through at least 3.2.1) incorrectly generate
14523 DW_TAG_variable tags. */
14524
14525 const char *physname;
14526
14527 /* Get name of field. */
14528 fieldname = dwarf2_name (die, cu);
14529 if (fieldname == NULL)
14530 return;
14531
14532 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14533 if (attr
14534 /* Only create a symbol if this is an external value.
14535 new_symbol checks this and puts the value in the global symbol
14536 table, which we want. If it is not external, new_symbol
14537 will try to put the value in cu->list_in_scope which is wrong. */
14538 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14539 {
14540 /* A static const member, not much different than an enum as far as
14541 we're concerned, except that we can support more types. */
14542 new_symbol (die, NULL, cu);
14543 }
14544
14545 /* Get physical name. */
14546 physname = dwarf2_physname (fieldname, die, cu);
14547
14548 /* The name is already allocated along with this objfile, so we don't
14549 need to duplicate it for the type. */
14550 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14551 FIELD_TYPE (*fp) = die_type (die, cu);
14552 FIELD_NAME (*fp) = fieldname;
14553 }
14554 else if (die->tag == DW_TAG_inheritance)
14555 {
14556 /* C++ base class field. */
14557 handle_data_member_location (die, cu, fp);
14558 FIELD_BITSIZE (*fp) = 0;
14559 FIELD_TYPE (*fp) = die_type (die, cu);
14560 FIELD_NAME (*fp) = fp->type->name ();
14561 }
14562 else
14563 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14564 }
14565
14566 /* Can the type given by DIE define another type? */
14567
14568 static bool
14569 type_can_define_types (const struct die_info *die)
14570 {
14571 switch (die->tag)
14572 {
14573 case DW_TAG_typedef:
14574 case DW_TAG_class_type:
14575 case DW_TAG_structure_type:
14576 case DW_TAG_union_type:
14577 case DW_TAG_enumeration_type:
14578 return true;
14579
14580 default:
14581 return false;
14582 }
14583 }
14584
14585 /* Add a type definition defined in the scope of the FIP's class. */
14586
14587 static void
14588 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14589 struct dwarf2_cu *cu)
14590 {
14591 struct decl_field fp;
14592 memset (&fp, 0, sizeof (fp));
14593
14594 gdb_assert (type_can_define_types (die));
14595
14596 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14597 fp.name = dwarf2_name (die, cu);
14598 fp.type = read_type_die (die, cu);
14599
14600 /* Save accessibility. */
14601 enum dwarf_access_attribute accessibility;
14602 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14603 if (attr != NULL)
14604 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14605 else
14606 accessibility = dwarf2_default_access_attribute (die, cu);
14607 switch (accessibility)
14608 {
14609 case DW_ACCESS_public:
14610 /* The assumed value if neither private nor protected. */
14611 break;
14612 case DW_ACCESS_private:
14613 fp.is_private = 1;
14614 break;
14615 case DW_ACCESS_protected:
14616 fp.is_protected = 1;
14617 break;
14618 default:
14619 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14620 }
14621
14622 if (die->tag == DW_TAG_typedef)
14623 fip->typedef_field_list.push_back (fp);
14624 else
14625 fip->nested_types_list.push_back (fp);
14626 }
14627
14628 /* A convenience typedef that's used when finding the discriminant
14629 field for a variant part. */
14630 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14631 offset_map_type;
14632
14633 /* Compute the discriminant range for a given variant. OBSTACK is
14634 where the results will be stored. VARIANT is the variant to
14635 process. IS_UNSIGNED indicates whether the discriminant is signed
14636 or unsigned. */
14637
14638 static const gdb::array_view<discriminant_range>
14639 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14640 bool is_unsigned)
14641 {
14642 std::vector<discriminant_range> ranges;
14643
14644 if (variant.default_branch)
14645 return {};
14646
14647 if (variant.discr_list_data == nullptr)
14648 {
14649 discriminant_range r
14650 = {variant.discriminant_value, variant.discriminant_value};
14651 ranges.push_back (r);
14652 }
14653 else
14654 {
14655 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14656 variant.discr_list_data->size);
14657 while (!data.empty ())
14658 {
14659 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14660 {
14661 complaint (_("invalid discriminant marker: %d"), data[0]);
14662 break;
14663 }
14664 bool is_range = data[0] == DW_DSC_range;
14665 data = data.slice (1);
14666
14667 ULONGEST low, high;
14668 unsigned int bytes_read;
14669
14670 if (data.empty ())
14671 {
14672 complaint (_("DW_AT_discr_list missing low value"));
14673 break;
14674 }
14675 if (is_unsigned)
14676 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14677 else
14678 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14679 &bytes_read);
14680 data = data.slice (bytes_read);
14681
14682 if (is_range)
14683 {
14684 if (data.empty ())
14685 {
14686 complaint (_("DW_AT_discr_list missing high value"));
14687 break;
14688 }
14689 if (is_unsigned)
14690 high = read_unsigned_leb128 (nullptr, data.data (),
14691 &bytes_read);
14692 else
14693 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14694 &bytes_read);
14695 data = data.slice (bytes_read);
14696 }
14697 else
14698 high = low;
14699
14700 ranges.push_back ({ low, high });
14701 }
14702 }
14703
14704 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14705 ranges.size ());
14706 std::copy (ranges.begin (), ranges.end (), result);
14707 return gdb::array_view<discriminant_range> (result, ranges.size ());
14708 }
14709
14710 static const gdb::array_view<variant_part> create_variant_parts
14711 (struct obstack *obstack,
14712 const offset_map_type &offset_map,
14713 struct field_info *fi,
14714 const std::vector<variant_part_builder> &variant_parts);
14715
14716 /* Fill in a "struct variant" for a given variant field. RESULT is
14717 the variant to fill in. OBSTACK is where any needed allocations
14718 will be done. OFFSET_MAP holds the mapping from section offsets to
14719 fields for the type. FI describes the fields of the type we're
14720 processing. FIELD is the variant field we're converting. */
14721
14722 static void
14723 create_one_variant (variant &result, struct obstack *obstack,
14724 const offset_map_type &offset_map,
14725 struct field_info *fi, const variant_field &field)
14726 {
14727 result.discriminants = convert_variant_range (obstack, field, false);
14728 result.first_field = field.first_field + fi->baseclasses.size ();
14729 result.last_field = field.last_field + fi->baseclasses.size ();
14730 result.parts = create_variant_parts (obstack, offset_map, fi,
14731 field.variant_parts);
14732 }
14733
14734 /* Fill in a "struct variant_part" for a given variant part. RESULT
14735 is the variant part to fill in. OBSTACK is where any needed
14736 allocations will be done. OFFSET_MAP holds the mapping from
14737 section offsets to fields for the type. FI describes the fields of
14738 the type we're processing. BUILDER is the variant part to be
14739 converted. */
14740
14741 static void
14742 create_one_variant_part (variant_part &result,
14743 struct obstack *obstack,
14744 const offset_map_type &offset_map,
14745 struct field_info *fi,
14746 const variant_part_builder &builder)
14747 {
14748 auto iter = offset_map.find (builder.discriminant_offset);
14749 if (iter == offset_map.end ())
14750 {
14751 result.discriminant_index = -1;
14752 /* Doesn't matter. */
14753 result.is_unsigned = false;
14754 }
14755 else
14756 {
14757 result.discriminant_index = iter->second;
14758 result.is_unsigned
14759 = TYPE_UNSIGNED (FIELD_TYPE
14760 (fi->fields[result.discriminant_index].field));
14761 }
14762
14763 size_t n = builder.variants.size ();
14764 variant *output = new (obstack) variant[n];
14765 for (size_t i = 0; i < n; ++i)
14766 create_one_variant (output[i], obstack, offset_map, fi,
14767 builder.variants[i]);
14768
14769 result.variants = gdb::array_view<variant> (output, n);
14770 }
14771
14772 /* Create a vector of variant parts that can be attached to a type.
14773 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14774 holds the mapping from section offsets to fields for the type. FI
14775 describes the fields of the type we're processing. VARIANT_PARTS
14776 is the vector to convert. */
14777
14778 static const gdb::array_view<variant_part>
14779 create_variant_parts (struct obstack *obstack,
14780 const offset_map_type &offset_map,
14781 struct field_info *fi,
14782 const std::vector<variant_part_builder> &variant_parts)
14783 {
14784 if (variant_parts.empty ())
14785 return {};
14786
14787 size_t n = variant_parts.size ();
14788 variant_part *result = new (obstack) variant_part[n];
14789 for (size_t i = 0; i < n; ++i)
14790 create_one_variant_part (result[i], obstack, offset_map, fi,
14791 variant_parts[i]);
14792
14793 return gdb::array_view<variant_part> (result, n);
14794 }
14795
14796 /* Compute the variant part vector for FIP, attaching it to TYPE when
14797 done. */
14798
14799 static void
14800 add_variant_property (struct field_info *fip, struct type *type,
14801 struct dwarf2_cu *cu)
14802 {
14803 /* Map section offsets of fields to their field index. Note the
14804 field index here does not take the number of baseclasses into
14805 account. */
14806 offset_map_type offset_map;
14807 for (int i = 0; i < fip->fields.size (); ++i)
14808 offset_map[fip->fields[i].offset] = i;
14809
14810 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14811 gdb::array_view<variant_part> parts
14812 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14813 fip->variant_parts);
14814
14815 struct dynamic_prop prop;
14816 prop.kind = PROP_VARIANT_PARTS;
14817 prop.data.variant_parts
14818 = ((gdb::array_view<variant_part> *)
14819 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14820
14821 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14822 }
14823
14824 /* Create the vector of fields, and attach it to the type. */
14825
14826 static void
14827 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14828 struct dwarf2_cu *cu)
14829 {
14830 int nfields = fip->nfields ();
14831
14832 /* Record the field count, allocate space for the array of fields,
14833 and create blank accessibility bitfields if necessary. */
14834 type->set_num_fields (nfields);
14835 type->set_fields
14836 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14837
14838 if (fip->non_public_fields && cu->language != language_ada)
14839 {
14840 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14841
14842 TYPE_FIELD_PRIVATE_BITS (type) =
14843 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14844 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14845
14846 TYPE_FIELD_PROTECTED_BITS (type) =
14847 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14848 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14849
14850 TYPE_FIELD_IGNORE_BITS (type) =
14851 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14852 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14853 }
14854
14855 /* If the type has baseclasses, allocate and clear a bit vector for
14856 TYPE_FIELD_VIRTUAL_BITS. */
14857 if (!fip->baseclasses.empty () && cu->language != language_ada)
14858 {
14859 int num_bytes = B_BYTES (fip->baseclasses.size ());
14860 unsigned char *pointer;
14861
14862 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14863 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14864 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14865 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14866 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14867 }
14868
14869 if (!fip->variant_parts.empty ())
14870 add_variant_property (fip, type, cu);
14871
14872 /* Copy the saved-up fields into the field vector. */
14873 for (int i = 0; i < nfields; ++i)
14874 {
14875 struct nextfield &field
14876 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14877 : fip->fields[i - fip->baseclasses.size ()]);
14878
14879 type->field (i) = field.field;
14880 switch (field.accessibility)
14881 {
14882 case DW_ACCESS_private:
14883 if (cu->language != language_ada)
14884 SET_TYPE_FIELD_PRIVATE (type, i);
14885 break;
14886
14887 case DW_ACCESS_protected:
14888 if (cu->language != language_ada)
14889 SET_TYPE_FIELD_PROTECTED (type, i);
14890 break;
14891
14892 case DW_ACCESS_public:
14893 break;
14894
14895 default:
14896 /* Unknown accessibility. Complain and treat it as public. */
14897 {
14898 complaint (_("unsupported accessibility %d"),
14899 field.accessibility);
14900 }
14901 break;
14902 }
14903 if (i < fip->baseclasses.size ())
14904 {
14905 switch (field.virtuality)
14906 {
14907 case DW_VIRTUALITY_virtual:
14908 case DW_VIRTUALITY_pure_virtual:
14909 if (cu->language == language_ada)
14910 error (_("unexpected virtuality in component of Ada type"));
14911 SET_TYPE_FIELD_VIRTUAL (type, i);
14912 break;
14913 }
14914 }
14915 }
14916 }
14917
14918 /* Return true if this member function is a constructor, false
14919 otherwise. */
14920
14921 static int
14922 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14923 {
14924 const char *fieldname;
14925 const char *type_name;
14926 int len;
14927
14928 if (die->parent == NULL)
14929 return 0;
14930
14931 if (die->parent->tag != DW_TAG_structure_type
14932 && die->parent->tag != DW_TAG_union_type
14933 && die->parent->tag != DW_TAG_class_type)
14934 return 0;
14935
14936 fieldname = dwarf2_name (die, cu);
14937 type_name = dwarf2_name (die->parent, cu);
14938 if (fieldname == NULL || type_name == NULL)
14939 return 0;
14940
14941 len = strlen (fieldname);
14942 return (strncmp (fieldname, type_name, len) == 0
14943 && (type_name[len] == '\0' || type_name[len] == '<'));
14944 }
14945
14946 /* Check if the given VALUE is a recognized enum
14947 dwarf_defaulted_attribute constant according to DWARF5 spec,
14948 Table 7.24. */
14949
14950 static bool
14951 is_valid_DW_AT_defaulted (ULONGEST value)
14952 {
14953 switch (value)
14954 {
14955 case DW_DEFAULTED_no:
14956 case DW_DEFAULTED_in_class:
14957 case DW_DEFAULTED_out_of_class:
14958 return true;
14959 }
14960
14961 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14962 return false;
14963 }
14964
14965 /* Add a member function to the proper fieldlist. */
14966
14967 static void
14968 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14969 struct type *type, struct dwarf2_cu *cu)
14970 {
14971 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14972 struct attribute *attr;
14973 int i;
14974 struct fnfieldlist *flp = nullptr;
14975 struct fn_field *fnp;
14976 const char *fieldname;
14977 struct type *this_type;
14978 enum dwarf_access_attribute accessibility;
14979
14980 if (cu->language == language_ada)
14981 error (_("unexpected member function in Ada type"));
14982
14983 /* Get name of member function. */
14984 fieldname = dwarf2_name (die, cu);
14985 if (fieldname == NULL)
14986 return;
14987
14988 /* Look up member function name in fieldlist. */
14989 for (i = 0; i < fip->fnfieldlists.size (); i++)
14990 {
14991 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14992 {
14993 flp = &fip->fnfieldlists[i];
14994 break;
14995 }
14996 }
14997
14998 /* Create a new fnfieldlist if necessary. */
14999 if (flp == nullptr)
15000 {
15001 fip->fnfieldlists.emplace_back ();
15002 flp = &fip->fnfieldlists.back ();
15003 flp->name = fieldname;
15004 i = fip->fnfieldlists.size () - 1;
15005 }
15006
15007 /* Create a new member function field and add it to the vector of
15008 fnfieldlists. */
15009 flp->fnfields.emplace_back ();
15010 fnp = &flp->fnfields.back ();
15011
15012 /* Delay processing of the physname until later. */
15013 if (cu->language == language_cplus)
15014 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15015 die, cu);
15016 else
15017 {
15018 const char *physname = dwarf2_physname (fieldname, die, cu);
15019 fnp->physname = physname ? physname : "";
15020 }
15021
15022 fnp->type = alloc_type (objfile);
15023 this_type = read_type_die (die, cu);
15024 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15025 {
15026 int nparams = this_type->num_fields ();
15027
15028 /* TYPE is the domain of this method, and THIS_TYPE is the type
15029 of the method itself (TYPE_CODE_METHOD). */
15030 smash_to_method_type (fnp->type, type,
15031 TYPE_TARGET_TYPE (this_type),
15032 this_type->fields (),
15033 this_type->num_fields (),
15034 TYPE_VARARGS (this_type));
15035
15036 /* Handle static member functions.
15037 Dwarf2 has no clean way to discern C++ static and non-static
15038 member functions. G++ helps GDB by marking the first
15039 parameter for non-static member functions (which is the this
15040 pointer) as artificial. We obtain this information from
15041 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15042 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15043 fnp->voffset = VOFFSET_STATIC;
15044 }
15045 else
15046 complaint (_("member function type missing for '%s'"),
15047 dwarf2_full_name (fieldname, die, cu));
15048
15049 /* Get fcontext from DW_AT_containing_type if present. */
15050 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15051 fnp->fcontext = die_containing_type (die, cu);
15052
15053 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15054 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15055
15056 /* Get accessibility. */
15057 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15058 if (attr != nullptr)
15059 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15060 else
15061 accessibility = dwarf2_default_access_attribute (die, cu);
15062 switch (accessibility)
15063 {
15064 case DW_ACCESS_private:
15065 fnp->is_private = 1;
15066 break;
15067 case DW_ACCESS_protected:
15068 fnp->is_protected = 1;
15069 break;
15070 }
15071
15072 /* Check for artificial methods. */
15073 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15074 if (attr && DW_UNSND (attr) != 0)
15075 fnp->is_artificial = 1;
15076
15077 /* Check for defaulted methods. */
15078 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15079 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15080 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15081
15082 /* Check for deleted methods. */
15083 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15084 if (attr != nullptr && DW_UNSND (attr) != 0)
15085 fnp->is_deleted = 1;
15086
15087 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15088
15089 /* Get index in virtual function table if it is a virtual member
15090 function. For older versions of GCC, this is an offset in the
15091 appropriate virtual table, as specified by DW_AT_containing_type.
15092 For everyone else, it is an expression to be evaluated relative
15093 to the object address. */
15094
15095 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15096 if (attr != nullptr)
15097 {
15098 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15099 {
15100 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15101 {
15102 /* Old-style GCC. */
15103 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15104 }
15105 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15106 || (DW_BLOCK (attr)->size > 1
15107 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15108 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15109 {
15110 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15111 if ((fnp->voffset % cu->header.addr_size) != 0)
15112 dwarf2_complex_location_expr_complaint ();
15113 else
15114 fnp->voffset /= cu->header.addr_size;
15115 fnp->voffset += 2;
15116 }
15117 else
15118 dwarf2_complex_location_expr_complaint ();
15119
15120 if (!fnp->fcontext)
15121 {
15122 /* If there is no `this' field and no DW_AT_containing_type,
15123 we cannot actually find a base class context for the
15124 vtable! */
15125 if (this_type->num_fields () == 0
15126 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15127 {
15128 complaint (_("cannot determine context for virtual member "
15129 "function \"%s\" (offset %s)"),
15130 fieldname, sect_offset_str (die->sect_off));
15131 }
15132 else
15133 {
15134 fnp->fcontext
15135 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15136 }
15137 }
15138 }
15139 else if (attr->form_is_section_offset ())
15140 {
15141 dwarf2_complex_location_expr_complaint ();
15142 }
15143 else
15144 {
15145 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15146 fieldname);
15147 }
15148 }
15149 else
15150 {
15151 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15152 if (attr && DW_UNSND (attr))
15153 {
15154 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15155 complaint (_("Member function \"%s\" (offset %s) is virtual "
15156 "but the vtable offset is not specified"),
15157 fieldname, sect_offset_str (die->sect_off));
15158 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15159 TYPE_CPLUS_DYNAMIC (type) = 1;
15160 }
15161 }
15162 }
15163
15164 /* Create the vector of member function fields, and attach it to the type. */
15165
15166 static void
15167 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15168 struct dwarf2_cu *cu)
15169 {
15170 if (cu->language == language_ada)
15171 error (_("unexpected member functions in Ada type"));
15172
15173 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15174 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15175 TYPE_ALLOC (type,
15176 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15177
15178 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15179 {
15180 struct fnfieldlist &nf = fip->fnfieldlists[i];
15181 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15182
15183 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15184 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15185 fn_flp->fn_fields = (struct fn_field *)
15186 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15187
15188 for (int k = 0; k < nf.fnfields.size (); ++k)
15189 fn_flp->fn_fields[k] = nf.fnfields[k];
15190 }
15191
15192 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15193 }
15194
15195 /* Returns non-zero if NAME is the name of a vtable member in CU's
15196 language, zero otherwise. */
15197 static int
15198 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15199 {
15200 static const char vptr[] = "_vptr";
15201
15202 /* Look for the C++ form of the vtable. */
15203 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15204 return 1;
15205
15206 return 0;
15207 }
15208
15209 /* GCC outputs unnamed structures that are really pointers to member
15210 functions, with the ABI-specified layout. If TYPE describes
15211 such a structure, smash it into a member function type.
15212
15213 GCC shouldn't do this; it should just output pointer to member DIEs.
15214 This is GCC PR debug/28767. */
15215
15216 static void
15217 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15218 {
15219 struct type *pfn_type, *self_type, *new_type;
15220
15221 /* Check for a structure with no name and two children. */
15222 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15223 return;
15224
15225 /* Check for __pfn and __delta members. */
15226 if (TYPE_FIELD_NAME (type, 0) == NULL
15227 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15228 || TYPE_FIELD_NAME (type, 1) == NULL
15229 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15230 return;
15231
15232 /* Find the type of the method. */
15233 pfn_type = TYPE_FIELD_TYPE (type, 0);
15234 if (pfn_type == NULL
15235 || pfn_type->code () != TYPE_CODE_PTR
15236 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15237 return;
15238
15239 /* Look for the "this" argument. */
15240 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15241 if (pfn_type->num_fields () == 0
15242 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15243 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15244 return;
15245
15246 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15247 new_type = alloc_type (objfile);
15248 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15249 pfn_type->fields (), pfn_type->num_fields (),
15250 TYPE_VARARGS (pfn_type));
15251 smash_to_methodptr_type (type, new_type);
15252 }
15253
15254 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15255 appropriate error checking and issuing complaints if there is a
15256 problem. */
15257
15258 static ULONGEST
15259 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15260 {
15261 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15262
15263 if (attr == nullptr)
15264 return 0;
15265
15266 if (!attr->form_is_constant ())
15267 {
15268 complaint (_("DW_AT_alignment must have constant form"
15269 " - DIE at %s [in module %s]"),
15270 sect_offset_str (die->sect_off),
15271 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15272 return 0;
15273 }
15274
15275 ULONGEST align;
15276 if (attr->form == DW_FORM_sdata)
15277 {
15278 LONGEST val = DW_SND (attr);
15279 if (val < 0)
15280 {
15281 complaint (_("DW_AT_alignment value must not be negative"
15282 " - DIE at %s [in module %s]"),
15283 sect_offset_str (die->sect_off),
15284 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15285 return 0;
15286 }
15287 align = val;
15288 }
15289 else
15290 align = DW_UNSND (attr);
15291
15292 if (align == 0)
15293 {
15294 complaint (_("DW_AT_alignment value must not be zero"
15295 " - DIE at %s [in module %s]"),
15296 sect_offset_str (die->sect_off),
15297 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15298 return 0;
15299 }
15300 if ((align & (align - 1)) != 0)
15301 {
15302 complaint (_("DW_AT_alignment value must be a power of 2"
15303 " - DIE at %s [in module %s]"),
15304 sect_offset_str (die->sect_off),
15305 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15306 return 0;
15307 }
15308
15309 return align;
15310 }
15311
15312 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15313 the alignment for TYPE. */
15314
15315 static void
15316 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15317 struct type *type)
15318 {
15319 if (!set_type_align (type, get_alignment (cu, die)))
15320 complaint (_("DW_AT_alignment value too large"
15321 " - DIE at %s [in module %s]"),
15322 sect_offset_str (die->sect_off),
15323 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15324 }
15325
15326 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15327 constant for a type, according to DWARF5 spec, Table 5.5. */
15328
15329 static bool
15330 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15331 {
15332 switch (value)
15333 {
15334 case DW_CC_normal:
15335 case DW_CC_pass_by_reference:
15336 case DW_CC_pass_by_value:
15337 return true;
15338
15339 default:
15340 complaint (_("unrecognized DW_AT_calling_convention value "
15341 "(%s) for a type"), pulongest (value));
15342 return false;
15343 }
15344 }
15345
15346 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15347 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15348 also according to GNU-specific values (see include/dwarf2.h). */
15349
15350 static bool
15351 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15352 {
15353 switch (value)
15354 {
15355 case DW_CC_normal:
15356 case DW_CC_program:
15357 case DW_CC_nocall:
15358 return true;
15359
15360 case DW_CC_GNU_renesas_sh:
15361 case DW_CC_GNU_borland_fastcall_i386:
15362 case DW_CC_GDB_IBM_OpenCL:
15363 return true;
15364
15365 default:
15366 complaint (_("unrecognized DW_AT_calling_convention value "
15367 "(%s) for a subroutine"), pulongest (value));
15368 return false;
15369 }
15370 }
15371
15372 /* Called when we find the DIE that starts a structure or union scope
15373 (definition) to create a type for the structure or union. Fill in
15374 the type's name and general properties; the members will not be
15375 processed until process_structure_scope. A symbol table entry for
15376 the type will also not be done until process_structure_scope (assuming
15377 the type has a name).
15378
15379 NOTE: we need to call these functions regardless of whether or not the
15380 DIE has a DW_AT_name attribute, since it might be an anonymous
15381 structure or union. This gets the type entered into our set of
15382 user defined types. */
15383
15384 static struct type *
15385 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15386 {
15387 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15388 struct type *type;
15389 struct attribute *attr;
15390 const char *name;
15391
15392 /* If the definition of this type lives in .debug_types, read that type.
15393 Don't follow DW_AT_specification though, that will take us back up
15394 the chain and we want to go down. */
15395 attr = die->attr (DW_AT_signature);
15396 if (attr != nullptr)
15397 {
15398 type = get_DW_AT_signature_type (die, attr, cu);
15399
15400 /* The type's CU may not be the same as CU.
15401 Ensure TYPE is recorded with CU in die_type_hash. */
15402 return set_die_type (die, type, cu);
15403 }
15404
15405 type = alloc_type (objfile);
15406 INIT_CPLUS_SPECIFIC (type);
15407
15408 name = dwarf2_name (die, cu);
15409 if (name != NULL)
15410 {
15411 if (cu->language == language_cplus
15412 || cu->language == language_d
15413 || cu->language == language_rust)
15414 {
15415 const char *full_name = dwarf2_full_name (name, die, cu);
15416
15417 /* dwarf2_full_name might have already finished building the DIE's
15418 type. If so, there is no need to continue. */
15419 if (get_die_type (die, cu) != NULL)
15420 return get_die_type (die, cu);
15421
15422 type->set_name (full_name);
15423 }
15424 else
15425 {
15426 /* The name is already allocated along with this objfile, so
15427 we don't need to duplicate it for the type. */
15428 type->set_name (name);
15429 }
15430 }
15431
15432 if (die->tag == DW_TAG_structure_type)
15433 {
15434 type->set_code (TYPE_CODE_STRUCT);
15435 }
15436 else if (die->tag == DW_TAG_union_type)
15437 {
15438 type->set_code (TYPE_CODE_UNION);
15439 }
15440 else
15441 {
15442 type->set_code (TYPE_CODE_STRUCT);
15443 }
15444
15445 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15446 TYPE_DECLARED_CLASS (type) = 1;
15447
15448 /* Store the calling convention in the type if it's available in
15449 the die. Otherwise the calling convention remains set to
15450 the default value DW_CC_normal. */
15451 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15452 if (attr != nullptr
15453 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15454 {
15455 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15456 TYPE_CPLUS_CALLING_CONVENTION (type)
15457 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15458 }
15459
15460 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15461 if (attr != nullptr)
15462 {
15463 if (attr->form_is_constant ())
15464 TYPE_LENGTH (type) = DW_UNSND (attr);
15465 else
15466 {
15467 struct dynamic_prop prop;
15468 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15469 cu->per_cu->addr_type ()))
15470 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15471 TYPE_LENGTH (type) = 0;
15472 }
15473 }
15474 else
15475 {
15476 TYPE_LENGTH (type) = 0;
15477 }
15478
15479 maybe_set_alignment (cu, die, type);
15480
15481 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15482 {
15483 /* ICC<14 does not output the required DW_AT_declaration on
15484 incomplete types, but gives them a size of zero. */
15485 TYPE_STUB (type) = 1;
15486 }
15487 else
15488 TYPE_STUB_SUPPORTED (type) = 1;
15489
15490 if (die_is_declaration (die, cu))
15491 TYPE_STUB (type) = 1;
15492 else if (attr == NULL && die->child == NULL
15493 && producer_is_realview (cu->producer))
15494 /* RealView does not output the required DW_AT_declaration
15495 on incomplete types. */
15496 TYPE_STUB (type) = 1;
15497
15498 /* We need to add the type field to the die immediately so we don't
15499 infinitely recurse when dealing with pointers to the structure
15500 type within the structure itself. */
15501 set_die_type (die, type, cu);
15502
15503 /* set_die_type should be already done. */
15504 set_descriptive_type (type, die, cu);
15505
15506 return type;
15507 }
15508
15509 static void handle_struct_member_die
15510 (struct die_info *child_die,
15511 struct type *type,
15512 struct field_info *fi,
15513 std::vector<struct symbol *> *template_args,
15514 struct dwarf2_cu *cu);
15515
15516 /* A helper for handle_struct_member_die that handles
15517 DW_TAG_variant_part. */
15518
15519 static void
15520 handle_variant_part (struct die_info *die, struct type *type,
15521 struct field_info *fi,
15522 std::vector<struct symbol *> *template_args,
15523 struct dwarf2_cu *cu)
15524 {
15525 variant_part_builder *new_part;
15526 if (fi->current_variant_part == nullptr)
15527 {
15528 fi->variant_parts.emplace_back ();
15529 new_part = &fi->variant_parts.back ();
15530 }
15531 else if (!fi->current_variant_part->processing_variant)
15532 {
15533 complaint (_("nested DW_TAG_variant_part seen "
15534 "- DIE at %s [in module %s]"),
15535 sect_offset_str (die->sect_off),
15536 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15537 return;
15538 }
15539 else
15540 {
15541 variant_field &current = fi->current_variant_part->variants.back ();
15542 current.variant_parts.emplace_back ();
15543 new_part = &current.variant_parts.back ();
15544 }
15545
15546 /* When we recurse, we want callees to add to this new variant
15547 part. */
15548 scoped_restore save_current_variant_part
15549 = make_scoped_restore (&fi->current_variant_part, new_part);
15550
15551 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15552 if (discr == NULL)
15553 {
15554 /* It's a univariant form, an extension we support. */
15555 }
15556 else if (discr->form_is_ref ())
15557 {
15558 struct dwarf2_cu *target_cu = cu;
15559 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15560
15561 new_part->discriminant_offset = target_die->sect_off;
15562 }
15563 else
15564 {
15565 complaint (_("DW_AT_discr does not have DIE reference form"
15566 " - DIE at %s [in module %s]"),
15567 sect_offset_str (die->sect_off),
15568 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15569 }
15570
15571 for (die_info *child_die = die->child;
15572 child_die != NULL;
15573 child_die = child_die->sibling)
15574 handle_struct_member_die (child_die, type, fi, template_args, cu);
15575 }
15576
15577 /* A helper for handle_struct_member_die that handles
15578 DW_TAG_variant. */
15579
15580 static void
15581 handle_variant (struct die_info *die, struct type *type,
15582 struct field_info *fi,
15583 std::vector<struct symbol *> *template_args,
15584 struct dwarf2_cu *cu)
15585 {
15586 if (fi->current_variant_part == nullptr)
15587 {
15588 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15589 "- DIE at %s [in module %s]"),
15590 sect_offset_str (die->sect_off),
15591 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15592 return;
15593 }
15594 if (fi->current_variant_part->processing_variant)
15595 {
15596 complaint (_("nested DW_TAG_variant seen "
15597 "- DIE at %s [in module %s]"),
15598 sect_offset_str (die->sect_off),
15599 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15600 return;
15601 }
15602
15603 scoped_restore save_processing_variant
15604 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15605 true);
15606
15607 fi->current_variant_part->variants.emplace_back ();
15608 variant_field &variant = fi->current_variant_part->variants.back ();
15609 variant.first_field = fi->fields.size ();
15610
15611 /* In a variant we want to get the discriminant and also add a
15612 field for our sole member child. */
15613 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15614 if (discr == nullptr)
15615 {
15616 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15617 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15618 variant.default_branch = true;
15619 else
15620 variant.discr_list_data = DW_BLOCK (discr);
15621 }
15622 else
15623 variant.discriminant_value = DW_UNSND (discr);
15624
15625 for (die_info *variant_child = die->child;
15626 variant_child != NULL;
15627 variant_child = variant_child->sibling)
15628 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15629
15630 variant.last_field = fi->fields.size ();
15631 }
15632
15633 /* A helper for process_structure_scope that handles a single member
15634 DIE. */
15635
15636 static void
15637 handle_struct_member_die (struct die_info *child_die, struct type *type,
15638 struct field_info *fi,
15639 std::vector<struct symbol *> *template_args,
15640 struct dwarf2_cu *cu)
15641 {
15642 if (child_die->tag == DW_TAG_member
15643 || child_die->tag == DW_TAG_variable)
15644 {
15645 /* NOTE: carlton/2002-11-05: A C++ static data member
15646 should be a DW_TAG_member that is a declaration, but
15647 all versions of G++ as of this writing (so through at
15648 least 3.2.1) incorrectly generate DW_TAG_variable
15649 tags for them instead. */
15650 dwarf2_add_field (fi, child_die, cu);
15651 }
15652 else if (child_die->tag == DW_TAG_subprogram)
15653 {
15654 /* Rust doesn't have member functions in the C++ sense.
15655 However, it does emit ordinary functions as children
15656 of a struct DIE. */
15657 if (cu->language == language_rust)
15658 read_func_scope (child_die, cu);
15659 else
15660 {
15661 /* C++ member function. */
15662 dwarf2_add_member_fn (fi, child_die, type, cu);
15663 }
15664 }
15665 else if (child_die->tag == DW_TAG_inheritance)
15666 {
15667 /* C++ base class field. */
15668 dwarf2_add_field (fi, child_die, cu);
15669 }
15670 else if (type_can_define_types (child_die))
15671 dwarf2_add_type_defn (fi, child_die, cu);
15672 else if (child_die->tag == DW_TAG_template_type_param
15673 || child_die->tag == DW_TAG_template_value_param)
15674 {
15675 struct symbol *arg = new_symbol (child_die, NULL, cu);
15676
15677 if (arg != NULL)
15678 template_args->push_back (arg);
15679 }
15680 else if (child_die->tag == DW_TAG_variant_part)
15681 handle_variant_part (child_die, type, fi, template_args, cu);
15682 else if (child_die->tag == DW_TAG_variant)
15683 handle_variant (child_die, type, fi, template_args, cu);
15684 }
15685
15686 /* Finish creating a structure or union type, including filling in
15687 its members and creating a symbol for it. */
15688
15689 static void
15690 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15691 {
15692 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15693 struct die_info *child_die;
15694 struct type *type;
15695
15696 type = get_die_type (die, cu);
15697 if (type == NULL)
15698 type = read_structure_type (die, cu);
15699
15700 bool has_template_parameters = false;
15701 if (die->child != NULL && ! die_is_declaration (die, cu))
15702 {
15703 struct field_info fi;
15704 std::vector<struct symbol *> template_args;
15705
15706 child_die = die->child;
15707
15708 while (child_die && child_die->tag)
15709 {
15710 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15711 child_die = child_die->sibling;
15712 }
15713
15714 /* Attach template arguments to type. */
15715 if (!template_args.empty ())
15716 {
15717 has_template_parameters = true;
15718 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15719 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15720 TYPE_TEMPLATE_ARGUMENTS (type)
15721 = XOBNEWVEC (&objfile->objfile_obstack,
15722 struct symbol *,
15723 TYPE_N_TEMPLATE_ARGUMENTS (type));
15724 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15725 template_args.data (),
15726 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15727 * sizeof (struct symbol *)));
15728 }
15729
15730 /* Attach fields and member functions to the type. */
15731 if (fi.nfields () > 0)
15732 dwarf2_attach_fields_to_type (&fi, type, cu);
15733 if (!fi.fnfieldlists.empty ())
15734 {
15735 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15736
15737 /* Get the type which refers to the base class (possibly this
15738 class itself) which contains the vtable pointer for the current
15739 class from the DW_AT_containing_type attribute. This use of
15740 DW_AT_containing_type is a GNU extension. */
15741
15742 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15743 {
15744 struct type *t = die_containing_type (die, cu);
15745
15746 set_type_vptr_basetype (type, t);
15747 if (type == t)
15748 {
15749 int i;
15750
15751 /* Our own class provides vtbl ptr. */
15752 for (i = t->num_fields () - 1;
15753 i >= TYPE_N_BASECLASSES (t);
15754 --i)
15755 {
15756 const char *fieldname = TYPE_FIELD_NAME (t, i);
15757
15758 if (is_vtable_name (fieldname, cu))
15759 {
15760 set_type_vptr_fieldno (type, i);
15761 break;
15762 }
15763 }
15764
15765 /* Complain if virtual function table field not found. */
15766 if (i < TYPE_N_BASECLASSES (t))
15767 complaint (_("virtual function table pointer "
15768 "not found when defining class '%s'"),
15769 type->name () ? type->name () : "");
15770 }
15771 else
15772 {
15773 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15774 }
15775 }
15776 else if (cu->producer
15777 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15778 {
15779 /* The IBM XLC compiler does not provide direct indication
15780 of the containing type, but the vtable pointer is
15781 always named __vfp. */
15782
15783 int i;
15784
15785 for (i = type->num_fields () - 1;
15786 i >= TYPE_N_BASECLASSES (type);
15787 --i)
15788 {
15789 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15790 {
15791 set_type_vptr_fieldno (type, i);
15792 set_type_vptr_basetype (type, type);
15793 break;
15794 }
15795 }
15796 }
15797 }
15798
15799 /* Copy fi.typedef_field_list linked list elements content into the
15800 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15801 if (!fi.typedef_field_list.empty ())
15802 {
15803 int count = fi.typedef_field_list.size ();
15804
15805 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15806 TYPE_TYPEDEF_FIELD_ARRAY (type)
15807 = ((struct decl_field *)
15808 TYPE_ALLOC (type,
15809 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15810 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15811
15812 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15813 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15814 }
15815
15816 /* Copy fi.nested_types_list linked list elements content into the
15817 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15818 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15819 {
15820 int count = fi.nested_types_list.size ();
15821
15822 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15823 TYPE_NESTED_TYPES_ARRAY (type)
15824 = ((struct decl_field *)
15825 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15826 TYPE_NESTED_TYPES_COUNT (type) = count;
15827
15828 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15829 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15830 }
15831 }
15832
15833 quirk_gcc_member_function_pointer (type, objfile);
15834 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15835 cu->rust_unions.push_back (type);
15836
15837 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15838 snapshots) has been known to create a die giving a declaration
15839 for a class that has, as a child, a die giving a definition for a
15840 nested class. So we have to process our children even if the
15841 current die is a declaration. Normally, of course, a declaration
15842 won't have any children at all. */
15843
15844 child_die = die->child;
15845
15846 while (child_die != NULL && child_die->tag)
15847 {
15848 if (child_die->tag == DW_TAG_member
15849 || child_die->tag == DW_TAG_variable
15850 || child_die->tag == DW_TAG_inheritance
15851 || child_die->tag == DW_TAG_template_value_param
15852 || child_die->tag == DW_TAG_template_type_param)
15853 {
15854 /* Do nothing. */
15855 }
15856 else
15857 process_die (child_die, cu);
15858
15859 child_die = child_die->sibling;
15860 }
15861
15862 /* Do not consider external references. According to the DWARF standard,
15863 these DIEs are identified by the fact that they have no byte_size
15864 attribute, and a declaration attribute. */
15865 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15866 || !die_is_declaration (die, cu)
15867 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15868 {
15869 struct symbol *sym = new_symbol (die, type, cu);
15870
15871 if (has_template_parameters)
15872 {
15873 struct symtab *symtab;
15874 if (sym != nullptr)
15875 symtab = symbol_symtab (sym);
15876 else if (cu->line_header != nullptr)
15877 {
15878 /* Any related symtab will do. */
15879 symtab
15880 = cu->line_header->file_names ()[0].symtab;
15881 }
15882 else
15883 {
15884 symtab = nullptr;
15885 complaint (_("could not find suitable "
15886 "symtab for template parameter"
15887 " - DIE at %s [in module %s]"),
15888 sect_offset_str (die->sect_off),
15889 objfile_name (objfile));
15890 }
15891
15892 if (symtab != nullptr)
15893 {
15894 /* Make sure that the symtab is set on the new symbols.
15895 Even though they don't appear in this symtab directly,
15896 other parts of gdb assume that symbols do, and this is
15897 reasonably true. */
15898 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15899 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15900 }
15901 }
15902 }
15903 }
15904
15905 /* Assuming DIE is an enumeration type, and TYPE is its associated
15906 type, update TYPE using some information only available in DIE's
15907 children. In particular, the fields are computed. */
15908
15909 static void
15910 update_enumeration_type_from_children (struct die_info *die,
15911 struct type *type,
15912 struct dwarf2_cu *cu)
15913 {
15914 struct die_info *child_die;
15915 int unsigned_enum = 1;
15916 int flag_enum = 1;
15917
15918 auto_obstack obstack;
15919 std::vector<struct field> fields;
15920
15921 for (child_die = die->child;
15922 child_die != NULL && child_die->tag;
15923 child_die = child_die->sibling)
15924 {
15925 struct attribute *attr;
15926 LONGEST value;
15927 const gdb_byte *bytes;
15928 struct dwarf2_locexpr_baton *baton;
15929 const char *name;
15930
15931 if (child_die->tag != DW_TAG_enumerator)
15932 continue;
15933
15934 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15935 if (attr == NULL)
15936 continue;
15937
15938 name = dwarf2_name (child_die, cu);
15939 if (name == NULL)
15940 name = "<anonymous enumerator>";
15941
15942 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15943 &value, &bytes, &baton);
15944 if (value < 0)
15945 {
15946 unsigned_enum = 0;
15947 flag_enum = 0;
15948 }
15949 else
15950 {
15951 if (count_one_bits_ll (value) >= 2)
15952 flag_enum = 0;
15953 }
15954
15955 fields.emplace_back ();
15956 struct field &field = fields.back ();
15957 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15958 SET_FIELD_ENUMVAL (field, value);
15959 }
15960
15961 if (!fields.empty ())
15962 {
15963 type->set_num_fields (fields.size ());
15964 type->set_fields
15965 ((struct field *)
15966 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
15967 memcpy (type->fields (), fields.data (),
15968 sizeof (struct field) * fields.size ());
15969 }
15970
15971 if (unsigned_enum)
15972 TYPE_UNSIGNED (type) = 1;
15973 if (flag_enum)
15974 TYPE_FLAG_ENUM (type) = 1;
15975 }
15976
15977 /* Given a DW_AT_enumeration_type die, set its type. We do not
15978 complete the type's fields yet, or create any symbols. */
15979
15980 static struct type *
15981 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15982 {
15983 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15984 struct type *type;
15985 struct attribute *attr;
15986 const char *name;
15987
15988 /* If the definition of this type lives in .debug_types, read that type.
15989 Don't follow DW_AT_specification though, that will take us back up
15990 the chain and we want to go down. */
15991 attr = die->attr (DW_AT_signature);
15992 if (attr != nullptr)
15993 {
15994 type = get_DW_AT_signature_type (die, attr, cu);
15995
15996 /* The type's CU may not be the same as CU.
15997 Ensure TYPE is recorded with CU in die_type_hash. */
15998 return set_die_type (die, type, cu);
15999 }
16000
16001 type = alloc_type (objfile);
16002
16003 type->set_code (TYPE_CODE_ENUM);
16004 name = dwarf2_full_name (NULL, die, cu);
16005 if (name != NULL)
16006 type->set_name (name);
16007
16008 attr = dwarf2_attr (die, DW_AT_type, cu);
16009 if (attr != NULL)
16010 {
16011 struct type *underlying_type = die_type (die, cu);
16012
16013 TYPE_TARGET_TYPE (type) = underlying_type;
16014 }
16015
16016 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16017 if (attr != nullptr)
16018 {
16019 TYPE_LENGTH (type) = DW_UNSND (attr);
16020 }
16021 else
16022 {
16023 TYPE_LENGTH (type) = 0;
16024 }
16025
16026 maybe_set_alignment (cu, die, type);
16027
16028 /* The enumeration DIE can be incomplete. In Ada, any type can be
16029 declared as private in the package spec, and then defined only
16030 inside the package body. Such types are known as Taft Amendment
16031 Types. When another package uses such a type, an incomplete DIE
16032 may be generated by the compiler. */
16033 if (die_is_declaration (die, cu))
16034 TYPE_STUB (type) = 1;
16035
16036 /* If this type has an underlying type that is not a stub, then we
16037 may use its attributes. We always use the "unsigned" attribute
16038 in this situation, because ordinarily we guess whether the type
16039 is unsigned -- but the guess can be wrong and the underlying type
16040 can tell us the reality. However, we defer to a local size
16041 attribute if one exists, because this lets the compiler override
16042 the underlying type if needed. */
16043 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16044 {
16045 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16046 underlying_type = check_typedef (underlying_type);
16047 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16048 if (TYPE_LENGTH (type) == 0)
16049 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16050 if (TYPE_RAW_ALIGN (type) == 0
16051 && TYPE_RAW_ALIGN (underlying_type) != 0)
16052 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16053 }
16054
16055 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16056
16057 set_die_type (die, type, cu);
16058
16059 /* Finish the creation of this type by using the enum's children.
16060 Note that, as usual, this must come after set_die_type to avoid
16061 infinite recursion when trying to compute the names of the
16062 enumerators. */
16063 update_enumeration_type_from_children (die, type, cu);
16064
16065 return type;
16066 }
16067
16068 /* Given a pointer to a die which begins an enumeration, process all
16069 the dies that define the members of the enumeration, and create the
16070 symbol for the enumeration type.
16071
16072 NOTE: We reverse the order of the element list. */
16073
16074 static void
16075 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16076 {
16077 struct type *this_type;
16078
16079 this_type = get_die_type (die, cu);
16080 if (this_type == NULL)
16081 this_type = read_enumeration_type (die, cu);
16082
16083 if (die->child != NULL)
16084 {
16085 struct die_info *child_die;
16086 const char *name;
16087
16088 child_die = die->child;
16089 while (child_die && child_die->tag)
16090 {
16091 if (child_die->tag != DW_TAG_enumerator)
16092 {
16093 process_die (child_die, cu);
16094 }
16095 else
16096 {
16097 name = dwarf2_name (child_die, cu);
16098 if (name)
16099 new_symbol (child_die, this_type, cu);
16100 }
16101
16102 child_die = child_die->sibling;
16103 }
16104 }
16105
16106 /* If we are reading an enum from a .debug_types unit, and the enum
16107 is a declaration, and the enum is not the signatured type in the
16108 unit, then we do not want to add a symbol for it. Adding a
16109 symbol would in some cases obscure the true definition of the
16110 enum, giving users an incomplete type when the definition is
16111 actually available. Note that we do not want to do this for all
16112 enums which are just declarations, because C++0x allows forward
16113 enum declarations. */
16114 if (cu->per_cu->is_debug_types
16115 && die_is_declaration (die, cu))
16116 {
16117 struct signatured_type *sig_type;
16118
16119 sig_type = (struct signatured_type *) cu->per_cu;
16120 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16121 if (sig_type->type_offset_in_section != die->sect_off)
16122 return;
16123 }
16124
16125 new_symbol (die, this_type, cu);
16126 }
16127
16128 /* Extract all information from a DW_TAG_array_type DIE and put it in
16129 the DIE's type field. For now, this only handles one dimensional
16130 arrays. */
16131
16132 static struct type *
16133 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16134 {
16135 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16136 struct die_info *child_die;
16137 struct type *type;
16138 struct type *element_type, *range_type, *index_type;
16139 struct attribute *attr;
16140 const char *name;
16141 struct dynamic_prop *byte_stride_prop = NULL;
16142 unsigned int bit_stride = 0;
16143
16144 element_type = die_type (die, cu);
16145
16146 /* The die_type call above may have already set the type for this DIE. */
16147 type = get_die_type (die, cu);
16148 if (type)
16149 return type;
16150
16151 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16152 if (attr != NULL)
16153 {
16154 int stride_ok;
16155 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
16156
16157 byte_stride_prop
16158 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16159 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16160 prop_type);
16161 if (!stride_ok)
16162 {
16163 complaint (_("unable to read array DW_AT_byte_stride "
16164 " - DIE at %s [in module %s]"),
16165 sect_offset_str (die->sect_off),
16166 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16167 /* Ignore this attribute. We will likely not be able to print
16168 arrays of this type correctly, but there is little we can do
16169 to help if we cannot read the attribute's value. */
16170 byte_stride_prop = NULL;
16171 }
16172 }
16173
16174 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16175 if (attr != NULL)
16176 bit_stride = DW_UNSND (attr);
16177
16178 /* Irix 6.2 native cc creates array types without children for
16179 arrays with unspecified length. */
16180 if (die->child == NULL)
16181 {
16182 index_type = objfile_type (objfile)->builtin_int;
16183 range_type = create_static_range_type (NULL, index_type, 0, -1);
16184 type = create_array_type_with_stride (NULL, element_type, range_type,
16185 byte_stride_prop, bit_stride);
16186 return set_die_type (die, type, cu);
16187 }
16188
16189 std::vector<struct type *> range_types;
16190 child_die = die->child;
16191 while (child_die && child_die->tag)
16192 {
16193 if (child_die->tag == DW_TAG_subrange_type)
16194 {
16195 struct type *child_type = read_type_die (child_die, cu);
16196
16197 if (child_type != NULL)
16198 {
16199 /* The range type was succesfully read. Save it for the
16200 array type creation. */
16201 range_types.push_back (child_type);
16202 }
16203 }
16204 child_die = child_die->sibling;
16205 }
16206
16207 /* Dwarf2 dimensions are output from left to right, create the
16208 necessary array types in backwards order. */
16209
16210 type = element_type;
16211
16212 if (read_array_order (die, cu) == DW_ORD_col_major)
16213 {
16214 int i = 0;
16215
16216 while (i < range_types.size ())
16217 type = create_array_type_with_stride (NULL, type, range_types[i++],
16218 byte_stride_prop, bit_stride);
16219 }
16220 else
16221 {
16222 size_t ndim = range_types.size ();
16223 while (ndim-- > 0)
16224 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16225 byte_stride_prop, bit_stride);
16226 }
16227
16228 /* Understand Dwarf2 support for vector types (like they occur on
16229 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16230 array type. This is not part of the Dwarf2/3 standard yet, but a
16231 custom vendor extension. The main difference between a regular
16232 array and the vector variant is that vectors are passed by value
16233 to functions. */
16234 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16235 if (attr != nullptr)
16236 make_vector_type (type);
16237
16238 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16239 implementation may choose to implement triple vectors using this
16240 attribute. */
16241 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16242 if (attr != nullptr)
16243 {
16244 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16245 TYPE_LENGTH (type) = DW_UNSND (attr);
16246 else
16247 complaint (_("DW_AT_byte_size for array type smaller "
16248 "than the total size of elements"));
16249 }
16250
16251 name = dwarf2_name (die, cu);
16252 if (name)
16253 type->set_name (name);
16254
16255 maybe_set_alignment (cu, die, type);
16256
16257 /* Install the type in the die. */
16258 set_die_type (die, type, cu);
16259
16260 /* set_die_type should be already done. */
16261 set_descriptive_type (type, die, cu);
16262
16263 return type;
16264 }
16265
16266 static enum dwarf_array_dim_ordering
16267 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16268 {
16269 struct attribute *attr;
16270
16271 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16272
16273 if (attr != nullptr)
16274 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16275
16276 /* GNU F77 is a special case, as at 08/2004 array type info is the
16277 opposite order to the dwarf2 specification, but data is still
16278 laid out as per normal fortran.
16279
16280 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16281 version checking. */
16282
16283 if (cu->language == language_fortran
16284 && cu->producer && strstr (cu->producer, "GNU F77"))
16285 {
16286 return DW_ORD_row_major;
16287 }
16288
16289 switch (cu->language_defn->la_array_ordering)
16290 {
16291 case array_column_major:
16292 return DW_ORD_col_major;
16293 case array_row_major:
16294 default:
16295 return DW_ORD_row_major;
16296 };
16297 }
16298
16299 /* Extract all information from a DW_TAG_set_type DIE and put it in
16300 the DIE's type field. */
16301
16302 static struct type *
16303 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16304 {
16305 struct type *domain_type, *set_type;
16306 struct attribute *attr;
16307
16308 domain_type = die_type (die, cu);
16309
16310 /* The die_type call above may have already set the type for this DIE. */
16311 set_type = get_die_type (die, cu);
16312 if (set_type)
16313 return set_type;
16314
16315 set_type = create_set_type (NULL, domain_type);
16316
16317 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16318 if (attr != nullptr)
16319 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16320
16321 maybe_set_alignment (cu, die, set_type);
16322
16323 return set_die_type (die, set_type, cu);
16324 }
16325
16326 /* A helper for read_common_block that creates a locexpr baton.
16327 SYM is the symbol which we are marking as computed.
16328 COMMON_DIE is the DIE for the common block.
16329 COMMON_LOC is the location expression attribute for the common
16330 block itself.
16331 MEMBER_LOC is the location expression attribute for the particular
16332 member of the common block that we are processing.
16333 CU is the CU from which the above come. */
16334
16335 static void
16336 mark_common_block_symbol_computed (struct symbol *sym,
16337 struct die_info *common_die,
16338 struct attribute *common_loc,
16339 struct attribute *member_loc,
16340 struct dwarf2_cu *cu)
16341 {
16342 dwarf2_per_objfile *per_objfile = cu->per_cu->dwarf2_per_objfile;
16343 struct objfile *objfile = per_objfile->objfile;
16344 struct dwarf2_locexpr_baton *baton;
16345 gdb_byte *ptr;
16346 unsigned int cu_off;
16347 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16348 LONGEST offset = 0;
16349
16350 gdb_assert (common_loc && member_loc);
16351 gdb_assert (common_loc->form_is_block ());
16352 gdb_assert (member_loc->form_is_block ()
16353 || member_loc->form_is_constant ());
16354
16355 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16356 baton->per_objfile = per_objfile;
16357 baton->per_cu = cu->per_cu;
16358 gdb_assert (baton->per_cu);
16359
16360 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16361
16362 if (member_loc->form_is_constant ())
16363 {
16364 offset = member_loc->constant_value (0);
16365 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16366 }
16367 else
16368 baton->size += DW_BLOCK (member_loc)->size;
16369
16370 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16371 baton->data = ptr;
16372
16373 *ptr++ = DW_OP_call4;
16374 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16375 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16376 ptr += 4;
16377
16378 if (member_loc->form_is_constant ())
16379 {
16380 *ptr++ = DW_OP_addr;
16381 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16382 ptr += cu->header.addr_size;
16383 }
16384 else
16385 {
16386 /* We have to copy the data here, because DW_OP_call4 will only
16387 use a DW_AT_location attribute. */
16388 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16389 ptr += DW_BLOCK (member_loc)->size;
16390 }
16391
16392 *ptr++ = DW_OP_plus;
16393 gdb_assert (ptr - baton->data == baton->size);
16394
16395 SYMBOL_LOCATION_BATON (sym) = baton;
16396 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16397 }
16398
16399 /* Create appropriate locally-scoped variables for all the
16400 DW_TAG_common_block entries. Also create a struct common_block
16401 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16402 is used to separate the common blocks name namespace from regular
16403 variable names. */
16404
16405 static void
16406 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16407 {
16408 struct attribute *attr;
16409
16410 attr = dwarf2_attr (die, DW_AT_location, cu);
16411 if (attr != nullptr)
16412 {
16413 /* Support the .debug_loc offsets. */
16414 if (attr->form_is_block ())
16415 {
16416 /* Ok. */
16417 }
16418 else if (attr->form_is_section_offset ())
16419 {
16420 dwarf2_complex_location_expr_complaint ();
16421 attr = NULL;
16422 }
16423 else
16424 {
16425 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16426 "common block member");
16427 attr = NULL;
16428 }
16429 }
16430
16431 if (die->child != NULL)
16432 {
16433 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16434 struct die_info *child_die;
16435 size_t n_entries = 0, size;
16436 struct common_block *common_block;
16437 struct symbol *sym;
16438
16439 for (child_die = die->child;
16440 child_die && child_die->tag;
16441 child_die = child_die->sibling)
16442 ++n_entries;
16443
16444 size = (sizeof (struct common_block)
16445 + (n_entries - 1) * sizeof (struct symbol *));
16446 common_block
16447 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16448 size);
16449 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16450 common_block->n_entries = 0;
16451
16452 for (child_die = die->child;
16453 child_die && child_die->tag;
16454 child_die = child_die->sibling)
16455 {
16456 /* Create the symbol in the DW_TAG_common_block block in the current
16457 symbol scope. */
16458 sym = new_symbol (child_die, NULL, cu);
16459 if (sym != NULL)
16460 {
16461 struct attribute *member_loc;
16462
16463 common_block->contents[common_block->n_entries++] = sym;
16464
16465 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16466 cu);
16467 if (member_loc)
16468 {
16469 /* GDB has handled this for a long time, but it is
16470 not specified by DWARF. It seems to have been
16471 emitted by gfortran at least as recently as:
16472 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16473 complaint (_("Variable in common block has "
16474 "DW_AT_data_member_location "
16475 "- DIE at %s [in module %s]"),
16476 sect_offset_str (child_die->sect_off),
16477 objfile_name (objfile));
16478
16479 if (member_loc->form_is_section_offset ())
16480 dwarf2_complex_location_expr_complaint ();
16481 else if (member_loc->form_is_constant ()
16482 || member_loc->form_is_block ())
16483 {
16484 if (attr != nullptr)
16485 mark_common_block_symbol_computed (sym, die, attr,
16486 member_loc, cu);
16487 }
16488 else
16489 dwarf2_complex_location_expr_complaint ();
16490 }
16491 }
16492 }
16493
16494 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16495 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16496 }
16497 }
16498
16499 /* Create a type for a C++ namespace. */
16500
16501 static struct type *
16502 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16503 {
16504 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16505 const char *previous_prefix, *name;
16506 int is_anonymous;
16507 struct type *type;
16508
16509 /* For extensions, reuse the type of the original namespace. */
16510 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16511 {
16512 struct die_info *ext_die;
16513 struct dwarf2_cu *ext_cu = cu;
16514
16515 ext_die = dwarf2_extension (die, &ext_cu);
16516 type = read_type_die (ext_die, ext_cu);
16517
16518 /* EXT_CU may not be the same as CU.
16519 Ensure TYPE is recorded with CU in die_type_hash. */
16520 return set_die_type (die, type, cu);
16521 }
16522
16523 name = namespace_name (die, &is_anonymous, cu);
16524
16525 /* Now build the name of the current namespace. */
16526
16527 previous_prefix = determine_prefix (die, cu);
16528 if (previous_prefix[0] != '\0')
16529 name = typename_concat (&objfile->objfile_obstack,
16530 previous_prefix, name, 0, cu);
16531
16532 /* Create the type. */
16533 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16534
16535 return set_die_type (die, type, cu);
16536 }
16537
16538 /* Read a namespace scope. */
16539
16540 static void
16541 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16542 {
16543 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16544 int is_anonymous;
16545
16546 /* Add a symbol associated to this if we haven't seen the namespace
16547 before. Also, add a using directive if it's an anonymous
16548 namespace. */
16549
16550 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16551 {
16552 struct type *type;
16553
16554 type = read_type_die (die, cu);
16555 new_symbol (die, type, cu);
16556
16557 namespace_name (die, &is_anonymous, cu);
16558 if (is_anonymous)
16559 {
16560 const char *previous_prefix = determine_prefix (die, cu);
16561
16562 std::vector<const char *> excludes;
16563 add_using_directive (using_directives (cu),
16564 previous_prefix, type->name (), NULL,
16565 NULL, excludes, 0, &objfile->objfile_obstack);
16566 }
16567 }
16568
16569 if (die->child != NULL)
16570 {
16571 struct die_info *child_die = die->child;
16572
16573 while (child_die && child_die->tag)
16574 {
16575 process_die (child_die, cu);
16576 child_die = child_die->sibling;
16577 }
16578 }
16579 }
16580
16581 /* Read a Fortran module as type. This DIE can be only a declaration used for
16582 imported module. Still we need that type as local Fortran "use ... only"
16583 declaration imports depend on the created type in determine_prefix. */
16584
16585 static struct type *
16586 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16587 {
16588 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16589 const char *module_name;
16590 struct type *type;
16591
16592 module_name = dwarf2_name (die, cu);
16593 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16594
16595 return set_die_type (die, type, cu);
16596 }
16597
16598 /* Read a Fortran module. */
16599
16600 static void
16601 read_module (struct die_info *die, struct dwarf2_cu *cu)
16602 {
16603 struct die_info *child_die = die->child;
16604 struct type *type;
16605
16606 type = read_type_die (die, cu);
16607 new_symbol (die, type, cu);
16608
16609 while (child_die && child_die->tag)
16610 {
16611 process_die (child_die, cu);
16612 child_die = child_die->sibling;
16613 }
16614 }
16615
16616 /* Return the name of the namespace represented by DIE. Set
16617 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16618 namespace. */
16619
16620 static const char *
16621 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16622 {
16623 struct die_info *current_die;
16624 const char *name = NULL;
16625
16626 /* Loop through the extensions until we find a name. */
16627
16628 for (current_die = die;
16629 current_die != NULL;
16630 current_die = dwarf2_extension (die, &cu))
16631 {
16632 /* We don't use dwarf2_name here so that we can detect the absence
16633 of a name -> anonymous namespace. */
16634 name = dwarf2_string_attr (die, DW_AT_name, cu);
16635
16636 if (name != NULL)
16637 break;
16638 }
16639
16640 /* Is it an anonymous namespace? */
16641
16642 *is_anonymous = (name == NULL);
16643 if (*is_anonymous)
16644 name = CP_ANONYMOUS_NAMESPACE_STR;
16645
16646 return name;
16647 }
16648
16649 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16650 the user defined type vector. */
16651
16652 static struct type *
16653 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16654 {
16655 struct gdbarch *gdbarch
16656 = cu->per_cu->dwarf2_per_objfile->objfile->arch ();
16657 struct comp_unit_head *cu_header = &cu->header;
16658 struct type *type;
16659 struct attribute *attr_byte_size;
16660 struct attribute *attr_address_class;
16661 int byte_size, addr_class;
16662 struct type *target_type;
16663
16664 target_type = die_type (die, cu);
16665
16666 /* The die_type call above may have already set the type for this DIE. */
16667 type = get_die_type (die, cu);
16668 if (type)
16669 return type;
16670
16671 type = lookup_pointer_type (target_type);
16672
16673 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16674 if (attr_byte_size)
16675 byte_size = DW_UNSND (attr_byte_size);
16676 else
16677 byte_size = cu_header->addr_size;
16678
16679 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16680 if (attr_address_class)
16681 addr_class = DW_UNSND (attr_address_class);
16682 else
16683 addr_class = DW_ADDR_none;
16684
16685 ULONGEST alignment = get_alignment (cu, die);
16686
16687 /* If the pointer size, alignment, or address class is different
16688 than the default, create a type variant marked as such and set
16689 the length accordingly. */
16690 if (TYPE_LENGTH (type) != byte_size
16691 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16692 && alignment != TYPE_RAW_ALIGN (type))
16693 || addr_class != DW_ADDR_none)
16694 {
16695 if (gdbarch_address_class_type_flags_p (gdbarch))
16696 {
16697 int type_flags;
16698
16699 type_flags = gdbarch_address_class_type_flags
16700 (gdbarch, byte_size, addr_class);
16701 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16702 == 0);
16703 type = make_type_with_address_space (type, type_flags);
16704 }
16705 else if (TYPE_LENGTH (type) != byte_size)
16706 {
16707 complaint (_("invalid pointer size %d"), byte_size);
16708 }
16709 else if (TYPE_RAW_ALIGN (type) != alignment)
16710 {
16711 complaint (_("Invalid DW_AT_alignment"
16712 " - DIE at %s [in module %s]"),
16713 sect_offset_str (die->sect_off),
16714 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16715 }
16716 else
16717 {
16718 /* Should we also complain about unhandled address classes? */
16719 }
16720 }
16721
16722 TYPE_LENGTH (type) = byte_size;
16723 set_type_align (type, alignment);
16724 return set_die_type (die, type, cu);
16725 }
16726
16727 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16728 the user defined type vector. */
16729
16730 static struct type *
16731 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16732 {
16733 struct type *type;
16734 struct type *to_type;
16735 struct type *domain;
16736
16737 to_type = die_type (die, cu);
16738 domain = die_containing_type (die, cu);
16739
16740 /* The calls above may have already set the type for this DIE. */
16741 type = get_die_type (die, cu);
16742 if (type)
16743 return type;
16744
16745 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16746 type = lookup_methodptr_type (to_type);
16747 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16748 {
16749 struct type *new_type
16750 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16751
16752 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16753 to_type->fields (), to_type->num_fields (),
16754 TYPE_VARARGS (to_type));
16755 type = lookup_methodptr_type (new_type);
16756 }
16757 else
16758 type = lookup_memberptr_type (to_type, domain);
16759
16760 return set_die_type (die, type, cu);
16761 }
16762
16763 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16764 the user defined type vector. */
16765
16766 static struct type *
16767 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16768 enum type_code refcode)
16769 {
16770 struct comp_unit_head *cu_header = &cu->header;
16771 struct type *type, *target_type;
16772 struct attribute *attr;
16773
16774 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16775
16776 target_type = die_type (die, cu);
16777
16778 /* The die_type call above may have already set the type for this DIE. */
16779 type = get_die_type (die, cu);
16780 if (type)
16781 return type;
16782
16783 type = lookup_reference_type (target_type, refcode);
16784 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16785 if (attr != nullptr)
16786 {
16787 TYPE_LENGTH (type) = DW_UNSND (attr);
16788 }
16789 else
16790 {
16791 TYPE_LENGTH (type) = cu_header->addr_size;
16792 }
16793 maybe_set_alignment (cu, die, type);
16794 return set_die_type (die, type, cu);
16795 }
16796
16797 /* Add the given cv-qualifiers to the element type of the array. GCC
16798 outputs DWARF type qualifiers that apply to an array, not the
16799 element type. But GDB relies on the array element type to carry
16800 the cv-qualifiers. This mimics section 6.7.3 of the C99
16801 specification. */
16802
16803 static struct type *
16804 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16805 struct type *base_type, int cnst, int voltl)
16806 {
16807 struct type *el_type, *inner_array;
16808
16809 base_type = copy_type (base_type);
16810 inner_array = base_type;
16811
16812 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16813 {
16814 TYPE_TARGET_TYPE (inner_array) =
16815 copy_type (TYPE_TARGET_TYPE (inner_array));
16816 inner_array = TYPE_TARGET_TYPE (inner_array);
16817 }
16818
16819 el_type = TYPE_TARGET_TYPE (inner_array);
16820 cnst |= TYPE_CONST (el_type);
16821 voltl |= TYPE_VOLATILE (el_type);
16822 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16823
16824 return set_die_type (die, base_type, cu);
16825 }
16826
16827 static struct type *
16828 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16829 {
16830 struct type *base_type, *cv_type;
16831
16832 base_type = die_type (die, cu);
16833
16834 /* The die_type call above may have already set the type for this DIE. */
16835 cv_type = get_die_type (die, cu);
16836 if (cv_type)
16837 return cv_type;
16838
16839 /* In case the const qualifier is applied to an array type, the element type
16840 is so qualified, not the array type (section 6.7.3 of C99). */
16841 if (base_type->code () == TYPE_CODE_ARRAY)
16842 return add_array_cv_type (die, cu, base_type, 1, 0);
16843
16844 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16845 return set_die_type (die, cv_type, cu);
16846 }
16847
16848 static struct type *
16849 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16850 {
16851 struct type *base_type, *cv_type;
16852
16853 base_type = die_type (die, cu);
16854
16855 /* The die_type call above may have already set the type for this DIE. */
16856 cv_type = get_die_type (die, cu);
16857 if (cv_type)
16858 return cv_type;
16859
16860 /* In case the volatile qualifier is applied to an array type, the
16861 element type is so qualified, not the array type (section 6.7.3
16862 of C99). */
16863 if (base_type->code () == TYPE_CODE_ARRAY)
16864 return add_array_cv_type (die, cu, base_type, 0, 1);
16865
16866 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16867 return set_die_type (die, cv_type, cu);
16868 }
16869
16870 /* Handle DW_TAG_restrict_type. */
16871
16872 static struct type *
16873 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16874 {
16875 struct type *base_type, *cv_type;
16876
16877 base_type = die_type (die, cu);
16878
16879 /* The die_type call above may have already set the type for this DIE. */
16880 cv_type = get_die_type (die, cu);
16881 if (cv_type)
16882 return cv_type;
16883
16884 cv_type = make_restrict_type (base_type);
16885 return set_die_type (die, cv_type, cu);
16886 }
16887
16888 /* Handle DW_TAG_atomic_type. */
16889
16890 static struct type *
16891 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16892 {
16893 struct type *base_type, *cv_type;
16894
16895 base_type = die_type (die, cu);
16896
16897 /* The die_type call above may have already set the type for this DIE. */
16898 cv_type = get_die_type (die, cu);
16899 if (cv_type)
16900 return cv_type;
16901
16902 cv_type = make_atomic_type (base_type);
16903 return set_die_type (die, cv_type, cu);
16904 }
16905
16906 /* Extract all information from a DW_TAG_string_type DIE and add to
16907 the user defined type vector. It isn't really a user defined type,
16908 but it behaves like one, with other DIE's using an AT_user_def_type
16909 attribute to reference it. */
16910
16911 static struct type *
16912 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16913 {
16914 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16915 struct gdbarch *gdbarch = objfile->arch ();
16916 struct type *type, *range_type, *index_type, *char_type;
16917 struct attribute *attr;
16918 struct dynamic_prop prop;
16919 bool length_is_constant = true;
16920 LONGEST length;
16921
16922 /* There are a couple of places where bit sizes might be made use of
16923 when parsing a DW_TAG_string_type, however, no producer that we know
16924 of make use of these. Handling bit sizes that are a multiple of the
16925 byte size is easy enough, but what about other bit sizes? Lets deal
16926 with that problem when we have to. Warn about these attributes being
16927 unsupported, then parse the type and ignore them like we always
16928 have. */
16929 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16930 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16931 {
16932 static bool warning_printed = false;
16933 if (!warning_printed)
16934 {
16935 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16936 "currently supported on DW_TAG_string_type."));
16937 warning_printed = true;
16938 }
16939 }
16940
16941 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16942 if (attr != nullptr && !attr->form_is_constant ())
16943 {
16944 /* The string length describes the location at which the length of
16945 the string can be found. The size of the length field can be
16946 specified with one of the attributes below. */
16947 struct type *prop_type;
16948 struct attribute *len
16949 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16950 if (len == nullptr)
16951 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16952 if (len != nullptr && len->form_is_constant ())
16953 {
16954 /* Pass 0 as the default as we know this attribute is constant
16955 and the default value will not be returned. */
16956 LONGEST sz = len->constant_value (0);
16957 prop_type = cu->per_cu->int_type (sz, true);
16958 }
16959 else
16960 {
16961 /* If the size is not specified then we assume it is the size of
16962 an address on this target. */
16963 prop_type = cu->per_cu->addr_sized_int_type (true);
16964 }
16965
16966 /* Convert the attribute into a dynamic property. */
16967 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16968 length = 1;
16969 else
16970 length_is_constant = false;
16971 }
16972 else if (attr != nullptr)
16973 {
16974 /* This DW_AT_string_length just contains the length with no
16975 indirection. There's no need to create a dynamic property in this
16976 case. Pass 0 for the default value as we know it will not be
16977 returned in this case. */
16978 length = attr->constant_value (0);
16979 }
16980 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16981 {
16982 /* We don't currently support non-constant byte sizes for strings. */
16983 length = attr->constant_value (1);
16984 }
16985 else
16986 {
16987 /* Use 1 as a fallback length if we have nothing else. */
16988 length = 1;
16989 }
16990
16991 index_type = objfile_type (objfile)->builtin_int;
16992 if (length_is_constant)
16993 range_type = create_static_range_type (NULL, index_type, 1, length);
16994 else
16995 {
16996 struct dynamic_prop low_bound;
16997
16998 low_bound.kind = PROP_CONST;
16999 low_bound.data.const_val = 1;
17000 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17001 }
17002 char_type = language_string_char_type (cu->language_defn, gdbarch);
17003 type = create_string_type (NULL, char_type, range_type);
17004
17005 return set_die_type (die, type, cu);
17006 }
17007
17008 /* Assuming that DIE corresponds to a function, returns nonzero
17009 if the function is prototyped. */
17010
17011 static int
17012 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17013 {
17014 struct attribute *attr;
17015
17016 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17017 if (attr && (DW_UNSND (attr) != 0))
17018 return 1;
17019
17020 /* The DWARF standard implies that the DW_AT_prototyped attribute
17021 is only meaningful for C, but the concept also extends to other
17022 languages that allow unprototyped functions (Eg: Objective C).
17023 For all other languages, assume that functions are always
17024 prototyped. */
17025 if (cu->language != language_c
17026 && cu->language != language_objc
17027 && cu->language != language_opencl)
17028 return 1;
17029
17030 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17031 prototyped and unprototyped functions; default to prototyped,
17032 since that is more common in modern code (and RealView warns
17033 about unprototyped functions). */
17034 if (producer_is_realview (cu->producer))
17035 return 1;
17036
17037 return 0;
17038 }
17039
17040 /* Handle DIES due to C code like:
17041
17042 struct foo
17043 {
17044 int (*funcp)(int a, long l);
17045 int b;
17046 };
17047
17048 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17049
17050 static struct type *
17051 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17052 {
17053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17054 struct type *type; /* Type that this function returns. */
17055 struct type *ftype; /* Function that returns above type. */
17056 struct attribute *attr;
17057
17058 type = die_type (die, cu);
17059
17060 /* The die_type call above may have already set the type for this DIE. */
17061 ftype = get_die_type (die, cu);
17062 if (ftype)
17063 return ftype;
17064
17065 ftype = lookup_function_type (type);
17066
17067 if (prototyped_function_p (die, cu))
17068 TYPE_PROTOTYPED (ftype) = 1;
17069
17070 /* Store the calling convention in the type if it's available in
17071 the subroutine die. Otherwise set the calling convention to
17072 the default value DW_CC_normal. */
17073 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17074 if (attr != nullptr
17075 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17076 TYPE_CALLING_CONVENTION (ftype)
17077 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17078 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17079 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17080 else
17081 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17082
17083 /* Record whether the function returns normally to its caller or not
17084 if the DWARF producer set that information. */
17085 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17086 if (attr && (DW_UNSND (attr) != 0))
17087 TYPE_NO_RETURN (ftype) = 1;
17088
17089 /* We need to add the subroutine type to the die immediately so
17090 we don't infinitely recurse when dealing with parameters
17091 declared as the same subroutine type. */
17092 set_die_type (die, ftype, cu);
17093
17094 if (die->child != NULL)
17095 {
17096 struct type *void_type = objfile_type (objfile)->builtin_void;
17097 struct die_info *child_die;
17098 int nparams, iparams;
17099
17100 /* Count the number of parameters.
17101 FIXME: GDB currently ignores vararg functions, but knows about
17102 vararg member functions. */
17103 nparams = 0;
17104 child_die = die->child;
17105 while (child_die && child_die->tag)
17106 {
17107 if (child_die->tag == DW_TAG_formal_parameter)
17108 nparams++;
17109 else if (child_die->tag == DW_TAG_unspecified_parameters)
17110 TYPE_VARARGS (ftype) = 1;
17111 child_die = child_die->sibling;
17112 }
17113
17114 /* Allocate storage for parameters and fill them in. */
17115 ftype->set_num_fields (nparams);
17116 ftype->set_fields
17117 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17118
17119 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17120 even if we error out during the parameters reading below. */
17121 for (iparams = 0; iparams < nparams; iparams++)
17122 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17123
17124 iparams = 0;
17125 child_die = die->child;
17126 while (child_die && child_die->tag)
17127 {
17128 if (child_die->tag == DW_TAG_formal_parameter)
17129 {
17130 struct type *arg_type;
17131
17132 /* DWARF version 2 has no clean way to discern C++
17133 static and non-static member functions. G++ helps
17134 GDB by marking the first parameter for non-static
17135 member functions (which is the this pointer) as
17136 artificial. We pass this information to
17137 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17138
17139 DWARF version 3 added DW_AT_object_pointer, which GCC
17140 4.5 does not yet generate. */
17141 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17142 if (attr != nullptr)
17143 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17144 else
17145 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17146 arg_type = die_type (child_die, cu);
17147
17148 /* RealView does not mark THIS as const, which the testsuite
17149 expects. GCC marks THIS as const in method definitions,
17150 but not in the class specifications (GCC PR 43053). */
17151 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17152 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17153 {
17154 int is_this = 0;
17155 struct dwarf2_cu *arg_cu = cu;
17156 const char *name = dwarf2_name (child_die, cu);
17157
17158 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17159 if (attr != nullptr)
17160 {
17161 /* If the compiler emits this, use it. */
17162 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17163 is_this = 1;
17164 }
17165 else if (name && strcmp (name, "this") == 0)
17166 /* Function definitions will have the argument names. */
17167 is_this = 1;
17168 else if (name == NULL && iparams == 0)
17169 /* Declarations may not have the names, so like
17170 elsewhere in GDB, assume an artificial first
17171 argument is "this". */
17172 is_this = 1;
17173
17174 if (is_this)
17175 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17176 arg_type, 0);
17177 }
17178
17179 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17180 iparams++;
17181 }
17182 child_die = child_die->sibling;
17183 }
17184 }
17185
17186 return ftype;
17187 }
17188
17189 static struct type *
17190 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17191 {
17192 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17193 const char *name = NULL;
17194 struct type *this_type, *target_type;
17195
17196 name = dwarf2_full_name (NULL, die, cu);
17197 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17198 TYPE_TARGET_STUB (this_type) = 1;
17199 set_die_type (die, this_type, cu);
17200 target_type = die_type (die, cu);
17201 if (target_type != this_type)
17202 TYPE_TARGET_TYPE (this_type) = target_type;
17203 else
17204 {
17205 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17206 spec and cause infinite loops in GDB. */
17207 complaint (_("Self-referential DW_TAG_typedef "
17208 "- DIE at %s [in module %s]"),
17209 sect_offset_str (die->sect_off), objfile_name (objfile));
17210 TYPE_TARGET_TYPE (this_type) = NULL;
17211 }
17212 if (name == NULL)
17213 {
17214 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17215 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17216 Handle these by just returning the target type, rather than
17217 constructing an anonymous typedef type and trying to handle this
17218 elsewhere. */
17219 set_die_type (die, target_type, cu);
17220 return target_type;
17221 }
17222 return this_type;
17223 }
17224
17225 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17226 (which may be different from NAME) to the architecture back-end to allow
17227 it to guess the correct format if necessary. */
17228
17229 static struct type *
17230 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17231 const char *name_hint, enum bfd_endian byte_order)
17232 {
17233 struct gdbarch *gdbarch = objfile->arch ();
17234 const struct floatformat **format;
17235 struct type *type;
17236
17237 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17238 if (format)
17239 type = init_float_type (objfile, bits, name, format, byte_order);
17240 else
17241 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17242
17243 return type;
17244 }
17245
17246 /* Allocate an integer type of size BITS and name NAME. */
17247
17248 static struct type *
17249 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17250 int bits, int unsigned_p, const char *name)
17251 {
17252 struct type *type;
17253
17254 /* Versions of Intel's C Compiler generate an integer type called "void"
17255 instead of using DW_TAG_unspecified_type. This has been seen on
17256 at least versions 14, 17, and 18. */
17257 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17258 && strcmp (name, "void") == 0)
17259 type = objfile_type (objfile)->builtin_void;
17260 else
17261 type = init_integer_type (objfile, bits, unsigned_p, name);
17262
17263 return type;
17264 }
17265
17266 /* Initialise and return a floating point type of size BITS suitable for
17267 use as a component of a complex number. The NAME_HINT is passed through
17268 when initialising the floating point type and is the name of the complex
17269 type.
17270
17271 As DWARF doesn't currently provide an explicit name for the components
17272 of a complex number, but it can be helpful to have these components
17273 named, we try to select a suitable name based on the size of the
17274 component. */
17275 static struct type *
17276 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17277 struct objfile *objfile,
17278 int bits, const char *name_hint,
17279 enum bfd_endian byte_order)
17280 {
17281 gdbarch *gdbarch = objfile->arch ();
17282 struct type *tt = nullptr;
17283
17284 /* Try to find a suitable floating point builtin type of size BITS.
17285 We're going to use the name of this type as the name for the complex
17286 target type that we are about to create. */
17287 switch (cu->language)
17288 {
17289 case language_fortran:
17290 switch (bits)
17291 {
17292 case 32:
17293 tt = builtin_f_type (gdbarch)->builtin_real;
17294 break;
17295 case 64:
17296 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17297 break;
17298 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17299 case 128:
17300 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17301 break;
17302 }
17303 break;
17304 default:
17305 switch (bits)
17306 {
17307 case 32:
17308 tt = builtin_type (gdbarch)->builtin_float;
17309 break;
17310 case 64:
17311 tt = builtin_type (gdbarch)->builtin_double;
17312 break;
17313 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17314 case 128:
17315 tt = builtin_type (gdbarch)->builtin_long_double;
17316 break;
17317 }
17318 break;
17319 }
17320
17321 /* If the type we found doesn't match the size we were looking for, then
17322 pretend we didn't find a type at all, the complex target type we
17323 create will then be nameless. */
17324 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17325 tt = nullptr;
17326
17327 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17328 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17329 }
17330
17331 /* Find a representation of a given base type and install
17332 it in the TYPE field of the die. */
17333
17334 static struct type *
17335 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17336 {
17337 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17338 struct type *type;
17339 struct attribute *attr;
17340 int encoding = 0, bits = 0;
17341 const char *name;
17342 gdbarch *arch;
17343
17344 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17345 if (attr != nullptr)
17346 encoding = DW_UNSND (attr);
17347 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17348 if (attr != nullptr)
17349 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17350 name = dwarf2_name (die, cu);
17351 if (!name)
17352 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17353
17354 arch = objfile->arch ();
17355 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17356
17357 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17358 if (attr)
17359 {
17360 int endianity = DW_UNSND (attr);
17361
17362 switch (endianity)
17363 {
17364 case DW_END_big:
17365 byte_order = BFD_ENDIAN_BIG;
17366 break;
17367 case DW_END_little:
17368 byte_order = BFD_ENDIAN_LITTLE;
17369 break;
17370 default:
17371 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17372 break;
17373 }
17374 }
17375
17376 switch (encoding)
17377 {
17378 case DW_ATE_address:
17379 /* Turn DW_ATE_address into a void * pointer. */
17380 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17381 type = init_pointer_type (objfile, bits, name, type);
17382 break;
17383 case DW_ATE_boolean:
17384 type = init_boolean_type (objfile, bits, 1, name);
17385 break;
17386 case DW_ATE_complex_float:
17387 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17388 byte_order);
17389 if (type->code () == TYPE_CODE_ERROR)
17390 {
17391 if (name == nullptr)
17392 {
17393 struct obstack *obstack
17394 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17395 name = obconcat (obstack, "_Complex ", type->name (),
17396 nullptr);
17397 }
17398 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17399 }
17400 else
17401 type = init_complex_type (name, type);
17402 break;
17403 case DW_ATE_decimal_float:
17404 type = init_decfloat_type (objfile, bits, name);
17405 break;
17406 case DW_ATE_float:
17407 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17408 break;
17409 case DW_ATE_signed:
17410 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17411 break;
17412 case DW_ATE_unsigned:
17413 if (cu->language == language_fortran
17414 && name
17415 && startswith (name, "character("))
17416 type = init_character_type (objfile, bits, 1, name);
17417 else
17418 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17419 break;
17420 case DW_ATE_signed_char:
17421 if (cu->language == language_ada || cu->language == language_m2
17422 || cu->language == language_pascal
17423 || cu->language == language_fortran)
17424 type = init_character_type (objfile, bits, 0, name);
17425 else
17426 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17427 break;
17428 case DW_ATE_unsigned_char:
17429 if (cu->language == language_ada || cu->language == language_m2
17430 || cu->language == language_pascal
17431 || cu->language == language_fortran
17432 || cu->language == language_rust)
17433 type = init_character_type (objfile, bits, 1, name);
17434 else
17435 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17436 break;
17437 case DW_ATE_UTF:
17438 {
17439 if (bits == 16)
17440 type = builtin_type (arch)->builtin_char16;
17441 else if (bits == 32)
17442 type = builtin_type (arch)->builtin_char32;
17443 else
17444 {
17445 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17446 bits);
17447 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17448 }
17449 return set_die_type (die, type, cu);
17450 }
17451 break;
17452
17453 default:
17454 complaint (_("unsupported DW_AT_encoding: '%s'"),
17455 dwarf_type_encoding_name (encoding));
17456 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17457 break;
17458 }
17459
17460 if (name && strcmp (name, "char") == 0)
17461 TYPE_NOSIGN (type) = 1;
17462
17463 maybe_set_alignment (cu, die, type);
17464
17465 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17466
17467 return set_die_type (die, type, cu);
17468 }
17469
17470 /* Parse dwarf attribute if it's a block, reference or constant and put the
17471 resulting value of the attribute into struct bound_prop.
17472 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17473
17474 static int
17475 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17476 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17477 struct type *default_type)
17478 {
17479 struct dwarf2_property_baton *baton;
17480 dwarf2_per_objfile *per_objfile = cu->per_cu->dwarf2_per_objfile;
17481 struct objfile *objfile = per_objfile->objfile;
17482 struct obstack *obstack = &objfile->objfile_obstack;
17483
17484 gdb_assert (default_type != NULL);
17485
17486 if (attr == NULL || prop == NULL)
17487 return 0;
17488
17489 if (attr->form_is_block ())
17490 {
17491 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17492 baton->property_type = default_type;
17493 baton->locexpr.per_cu = cu->per_cu;
17494 baton->locexpr.per_objfile = per_objfile;
17495 baton->locexpr.size = DW_BLOCK (attr)->size;
17496 baton->locexpr.data = DW_BLOCK (attr)->data;
17497 switch (attr->name)
17498 {
17499 case DW_AT_string_length:
17500 baton->locexpr.is_reference = true;
17501 break;
17502 default:
17503 baton->locexpr.is_reference = false;
17504 break;
17505 }
17506 prop->data.baton = baton;
17507 prop->kind = PROP_LOCEXPR;
17508 gdb_assert (prop->data.baton != NULL);
17509 }
17510 else if (attr->form_is_ref ())
17511 {
17512 struct dwarf2_cu *target_cu = cu;
17513 struct die_info *target_die;
17514 struct attribute *target_attr;
17515
17516 target_die = follow_die_ref (die, attr, &target_cu);
17517 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17518 if (target_attr == NULL)
17519 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17520 target_cu);
17521 if (target_attr == NULL)
17522 return 0;
17523
17524 switch (target_attr->name)
17525 {
17526 case DW_AT_location:
17527 if (target_attr->form_is_section_offset ())
17528 {
17529 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17530 baton->property_type = die_type (target_die, target_cu);
17531 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17532 prop->data.baton = baton;
17533 prop->kind = PROP_LOCLIST;
17534 gdb_assert (prop->data.baton != NULL);
17535 }
17536 else if (target_attr->form_is_block ())
17537 {
17538 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17539 baton->property_type = die_type (target_die, target_cu);
17540 baton->locexpr.per_cu = cu->per_cu;
17541 baton->locexpr.per_objfile = per_objfile;
17542 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17543 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17544 baton->locexpr.is_reference = true;
17545 prop->data.baton = baton;
17546 prop->kind = PROP_LOCEXPR;
17547 gdb_assert (prop->data.baton != NULL);
17548 }
17549 else
17550 {
17551 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17552 "dynamic property");
17553 return 0;
17554 }
17555 break;
17556 case DW_AT_data_member_location:
17557 {
17558 LONGEST offset;
17559
17560 if (!handle_data_member_location (target_die, target_cu,
17561 &offset))
17562 return 0;
17563
17564 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17565 baton->property_type = read_type_die (target_die->parent,
17566 target_cu);
17567 baton->offset_info.offset = offset;
17568 baton->offset_info.type = die_type (target_die, target_cu);
17569 prop->data.baton = baton;
17570 prop->kind = PROP_ADDR_OFFSET;
17571 break;
17572 }
17573 }
17574 }
17575 else if (attr->form_is_constant ())
17576 {
17577 prop->data.const_val = attr->constant_value (0);
17578 prop->kind = PROP_CONST;
17579 }
17580 else
17581 {
17582 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17583 dwarf2_name (die, cu));
17584 return 0;
17585 }
17586
17587 return 1;
17588 }
17589
17590 /* See read.h. */
17591
17592 struct type *
17593 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17594 {
17595 struct objfile *objfile = dwarf2_per_objfile->objfile;
17596 struct type *int_type;
17597
17598 /* Helper macro to examine the various builtin types. */
17599 #define TRY_TYPE(F) \
17600 int_type = (unsigned_p \
17601 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17602 : objfile_type (objfile)->builtin_ ## F); \
17603 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17604 return int_type
17605
17606 TRY_TYPE (char);
17607 TRY_TYPE (short);
17608 TRY_TYPE (int);
17609 TRY_TYPE (long);
17610 TRY_TYPE (long_long);
17611
17612 #undef TRY_TYPE
17613
17614 gdb_assert_not_reached ("unable to find suitable integer type");
17615 }
17616
17617 /* See read.h. */
17618
17619 struct type *
17620 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17621 {
17622 int addr_size = this->addr_size ();
17623 return int_type (addr_size, unsigned_p);
17624 }
17625
17626 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17627 present (which is valid) then compute the default type based on the
17628 compilation units address size. */
17629
17630 static struct type *
17631 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17632 {
17633 struct type *index_type = die_type (die, cu);
17634
17635 /* Dwarf-2 specifications explicitly allows to create subrange types
17636 without specifying a base type.
17637 In that case, the base type must be set to the type of
17638 the lower bound, upper bound or count, in that order, if any of these
17639 three attributes references an object that has a type.
17640 If no base type is found, the Dwarf-2 specifications say that
17641 a signed integer type of size equal to the size of an address should
17642 be used.
17643 For the following C code: `extern char gdb_int [];'
17644 GCC produces an empty range DIE.
17645 FIXME: muller/2010-05-28: Possible references to object for low bound,
17646 high bound or count are not yet handled by this code. */
17647 if (index_type->code () == TYPE_CODE_VOID)
17648 index_type = cu->per_cu->addr_sized_int_type (false);
17649
17650 return index_type;
17651 }
17652
17653 /* Read the given DW_AT_subrange DIE. */
17654
17655 static struct type *
17656 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17657 {
17658 struct type *base_type, *orig_base_type;
17659 struct type *range_type;
17660 struct attribute *attr;
17661 struct dynamic_prop low, high;
17662 int low_default_is_valid;
17663 int high_bound_is_count = 0;
17664 const char *name;
17665 ULONGEST negative_mask;
17666
17667 orig_base_type = read_subrange_index_type (die, cu);
17668
17669 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17670 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17671 creating the range type, but we use the result of check_typedef
17672 when examining properties of the type. */
17673 base_type = check_typedef (orig_base_type);
17674
17675 /* The die_type call above may have already set the type for this DIE. */
17676 range_type = get_die_type (die, cu);
17677 if (range_type)
17678 return range_type;
17679
17680 low.kind = PROP_CONST;
17681 high.kind = PROP_CONST;
17682 high.data.const_val = 0;
17683
17684 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17685 omitting DW_AT_lower_bound. */
17686 switch (cu->language)
17687 {
17688 case language_c:
17689 case language_cplus:
17690 low.data.const_val = 0;
17691 low_default_is_valid = 1;
17692 break;
17693 case language_fortran:
17694 low.data.const_val = 1;
17695 low_default_is_valid = 1;
17696 break;
17697 case language_d:
17698 case language_objc:
17699 case language_rust:
17700 low.data.const_val = 0;
17701 low_default_is_valid = (cu->header.version >= 4);
17702 break;
17703 case language_ada:
17704 case language_m2:
17705 case language_pascal:
17706 low.data.const_val = 1;
17707 low_default_is_valid = (cu->header.version >= 4);
17708 break;
17709 default:
17710 low.data.const_val = 0;
17711 low_default_is_valid = 0;
17712 break;
17713 }
17714
17715 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17716 if (attr != nullptr)
17717 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17718 else if (!low_default_is_valid)
17719 complaint (_("Missing DW_AT_lower_bound "
17720 "- DIE at %s [in module %s]"),
17721 sect_offset_str (die->sect_off),
17722 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17723
17724 struct attribute *attr_ub, *attr_count;
17725 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17726 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17727 {
17728 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17729 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17730 {
17731 /* If bounds are constant do the final calculation here. */
17732 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17733 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17734 else
17735 high_bound_is_count = 1;
17736 }
17737 else
17738 {
17739 if (attr_ub != NULL)
17740 complaint (_("Unresolved DW_AT_upper_bound "
17741 "- DIE at %s [in module %s]"),
17742 sect_offset_str (die->sect_off),
17743 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17744 if (attr_count != NULL)
17745 complaint (_("Unresolved DW_AT_count "
17746 "- DIE at %s [in module %s]"),
17747 sect_offset_str (die->sect_off),
17748 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17749 }
17750 }
17751
17752 LONGEST bias = 0;
17753 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17754 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17755 bias = bias_attr->constant_value (0);
17756
17757 /* Normally, the DWARF producers are expected to use a signed
17758 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17759 But this is unfortunately not always the case, as witnessed
17760 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17761 is used instead. To work around that ambiguity, we treat
17762 the bounds as signed, and thus sign-extend their values, when
17763 the base type is signed. */
17764 negative_mask =
17765 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17766 if (low.kind == PROP_CONST
17767 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17768 low.data.const_val |= negative_mask;
17769 if (high.kind == PROP_CONST
17770 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17771 high.data.const_val |= negative_mask;
17772
17773 /* Check for bit and byte strides. */
17774 struct dynamic_prop byte_stride_prop;
17775 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17776 if (attr_byte_stride != nullptr)
17777 {
17778 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17779 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17780 prop_type);
17781 }
17782
17783 struct dynamic_prop bit_stride_prop;
17784 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17785 if (attr_bit_stride != nullptr)
17786 {
17787 /* It only makes sense to have either a bit or byte stride. */
17788 if (attr_byte_stride != nullptr)
17789 {
17790 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17791 "- DIE at %s [in module %s]"),
17792 sect_offset_str (die->sect_off),
17793 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17794 attr_bit_stride = nullptr;
17795 }
17796 else
17797 {
17798 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17799 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17800 prop_type);
17801 }
17802 }
17803
17804 if (attr_byte_stride != nullptr
17805 || attr_bit_stride != nullptr)
17806 {
17807 bool byte_stride_p = (attr_byte_stride != nullptr);
17808 struct dynamic_prop *stride
17809 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17810
17811 range_type
17812 = create_range_type_with_stride (NULL, orig_base_type, &low,
17813 &high, bias, stride, byte_stride_p);
17814 }
17815 else
17816 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17817
17818 if (high_bound_is_count)
17819 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17820
17821 /* Ada expects an empty array on no boundary attributes. */
17822 if (attr == NULL && cu->language != language_ada)
17823 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17824
17825 name = dwarf2_name (die, cu);
17826 if (name)
17827 range_type->set_name (name);
17828
17829 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17830 if (attr != nullptr)
17831 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17832
17833 maybe_set_alignment (cu, die, range_type);
17834
17835 set_die_type (die, range_type, cu);
17836
17837 /* set_die_type should be already done. */
17838 set_descriptive_type (range_type, die, cu);
17839
17840 return range_type;
17841 }
17842
17843 static struct type *
17844 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17845 {
17846 struct type *type;
17847
17848 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17849 NULL);
17850 type->set_name (dwarf2_name (die, cu));
17851
17852 /* In Ada, an unspecified type is typically used when the description
17853 of the type is deferred to a different unit. When encountering
17854 such a type, we treat it as a stub, and try to resolve it later on,
17855 when needed. */
17856 if (cu->language == language_ada)
17857 TYPE_STUB (type) = 1;
17858
17859 return set_die_type (die, type, cu);
17860 }
17861
17862 /* Read a single die and all its descendents. Set the die's sibling
17863 field to NULL; set other fields in the die correctly, and set all
17864 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17865 location of the info_ptr after reading all of those dies. PARENT
17866 is the parent of the die in question. */
17867
17868 static struct die_info *
17869 read_die_and_children (const struct die_reader_specs *reader,
17870 const gdb_byte *info_ptr,
17871 const gdb_byte **new_info_ptr,
17872 struct die_info *parent)
17873 {
17874 struct die_info *die;
17875 const gdb_byte *cur_ptr;
17876
17877 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17878 if (die == NULL)
17879 {
17880 *new_info_ptr = cur_ptr;
17881 return NULL;
17882 }
17883 store_in_ref_table (die, reader->cu);
17884
17885 if (die->has_children)
17886 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17887 else
17888 {
17889 die->child = NULL;
17890 *new_info_ptr = cur_ptr;
17891 }
17892
17893 die->sibling = NULL;
17894 die->parent = parent;
17895 return die;
17896 }
17897
17898 /* Read a die, all of its descendents, and all of its siblings; set
17899 all of the fields of all of the dies correctly. Arguments are as
17900 in read_die_and_children. */
17901
17902 static struct die_info *
17903 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17904 const gdb_byte *info_ptr,
17905 const gdb_byte **new_info_ptr,
17906 struct die_info *parent)
17907 {
17908 struct die_info *first_die, *last_sibling;
17909 const gdb_byte *cur_ptr;
17910
17911 cur_ptr = info_ptr;
17912 first_die = last_sibling = NULL;
17913
17914 while (1)
17915 {
17916 struct die_info *die
17917 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17918
17919 if (die == NULL)
17920 {
17921 *new_info_ptr = cur_ptr;
17922 return first_die;
17923 }
17924
17925 if (!first_die)
17926 first_die = die;
17927 else
17928 last_sibling->sibling = die;
17929
17930 last_sibling = die;
17931 }
17932 }
17933
17934 /* Read a die, all of its descendents, and all of its siblings; set
17935 all of the fields of all of the dies correctly. Arguments are as
17936 in read_die_and_children.
17937 This the main entry point for reading a DIE and all its children. */
17938
17939 static struct die_info *
17940 read_die_and_siblings (const struct die_reader_specs *reader,
17941 const gdb_byte *info_ptr,
17942 const gdb_byte **new_info_ptr,
17943 struct die_info *parent)
17944 {
17945 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17946 new_info_ptr, parent);
17947
17948 if (dwarf_die_debug)
17949 {
17950 fprintf_unfiltered (gdb_stdlog,
17951 "Read die from %s@0x%x of %s:\n",
17952 reader->die_section->get_name (),
17953 (unsigned) (info_ptr - reader->die_section->buffer),
17954 bfd_get_filename (reader->abfd));
17955 dump_die (die, dwarf_die_debug);
17956 }
17957
17958 return die;
17959 }
17960
17961 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17962 attributes.
17963 The caller is responsible for filling in the extra attributes
17964 and updating (*DIEP)->num_attrs.
17965 Set DIEP to point to a newly allocated die with its information,
17966 except for its child, sibling, and parent fields. */
17967
17968 static const gdb_byte *
17969 read_full_die_1 (const struct die_reader_specs *reader,
17970 struct die_info **diep, const gdb_byte *info_ptr,
17971 int num_extra_attrs)
17972 {
17973 unsigned int abbrev_number, bytes_read, i;
17974 struct abbrev_info *abbrev;
17975 struct die_info *die;
17976 struct dwarf2_cu *cu = reader->cu;
17977 bfd *abfd = reader->abfd;
17978
17979 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17980 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17981 info_ptr += bytes_read;
17982 if (!abbrev_number)
17983 {
17984 *diep = NULL;
17985 return info_ptr;
17986 }
17987
17988 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17989 if (!abbrev)
17990 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17991 abbrev_number,
17992 bfd_get_filename (abfd));
17993
17994 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17995 die->sect_off = sect_off;
17996 die->tag = abbrev->tag;
17997 die->abbrev = abbrev_number;
17998 die->has_children = abbrev->has_children;
17999
18000 /* Make the result usable.
18001 The caller needs to update num_attrs after adding the extra
18002 attributes. */
18003 die->num_attrs = abbrev->num_attrs;
18004
18005 std::vector<int> indexes_that_need_reprocess;
18006 for (i = 0; i < abbrev->num_attrs; ++i)
18007 {
18008 bool need_reprocess;
18009 info_ptr =
18010 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18011 info_ptr, &need_reprocess);
18012 if (need_reprocess)
18013 indexes_that_need_reprocess.push_back (i);
18014 }
18015
18016 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18017 if (attr != nullptr)
18018 cu->str_offsets_base = DW_UNSND (attr);
18019
18020 attr = die->attr (DW_AT_loclists_base);
18021 if (attr != nullptr)
18022 cu->loclist_base = DW_UNSND (attr);
18023
18024 auto maybe_addr_base = die->addr_base ();
18025 if (maybe_addr_base.has_value ())
18026 cu->addr_base = *maybe_addr_base;
18027 for (int index : indexes_that_need_reprocess)
18028 read_attribute_reprocess (reader, &die->attrs[index]);
18029 *diep = die;
18030 return info_ptr;
18031 }
18032
18033 /* Read a die and all its attributes.
18034 Set DIEP to point to a newly allocated die with its information,
18035 except for its child, sibling, and parent fields. */
18036
18037 static const gdb_byte *
18038 read_full_die (const struct die_reader_specs *reader,
18039 struct die_info **diep, const gdb_byte *info_ptr)
18040 {
18041 const gdb_byte *result;
18042
18043 result = read_full_die_1 (reader, diep, info_ptr, 0);
18044
18045 if (dwarf_die_debug)
18046 {
18047 fprintf_unfiltered (gdb_stdlog,
18048 "Read die from %s@0x%x of %s:\n",
18049 reader->die_section->get_name (),
18050 (unsigned) (info_ptr - reader->die_section->buffer),
18051 bfd_get_filename (reader->abfd));
18052 dump_die (*diep, dwarf_die_debug);
18053 }
18054
18055 return result;
18056 }
18057 \f
18058
18059 /* Returns nonzero if TAG represents a type that we might generate a partial
18060 symbol for. */
18061
18062 static int
18063 is_type_tag_for_partial (int tag)
18064 {
18065 switch (tag)
18066 {
18067 #if 0
18068 /* Some types that would be reasonable to generate partial symbols for,
18069 that we don't at present. */
18070 case DW_TAG_array_type:
18071 case DW_TAG_file_type:
18072 case DW_TAG_ptr_to_member_type:
18073 case DW_TAG_set_type:
18074 case DW_TAG_string_type:
18075 case DW_TAG_subroutine_type:
18076 #endif
18077 case DW_TAG_base_type:
18078 case DW_TAG_class_type:
18079 case DW_TAG_interface_type:
18080 case DW_TAG_enumeration_type:
18081 case DW_TAG_structure_type:
18082 case DW_TAG_subrange_type:
18083 case DW_TAG_typedef:
18084 case DW_TAG_union_type:
18085 return 1;
18086 default:
18087 return 0;
18088 }
18089 }
18090
18091 /* Load all DIEs that are interesting for partial symbols into memory. */
18092
18093 static struct partial_die_info *
18094 load_partial_dies (const struct die_reader_specs *reader,
18095 const gdb_byte *info_ptr, int building_psymtab)
18096 {
18097 struct dwarf2_cu *cu = reader->cu;
18098 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18099 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18100 unsigned int bytes_read;
18101 unsigned int load_all = 0;
18102 int nesting_level = 1;
18103
18104 parent_die = NULL;
18105 last_die = NULL;
18106
18107 gdb_assert (cu->per_cu != NULL);
18108 if (cu->per_cu->load_all_dies)
18109 load_all = 1;
18110
18111 cu->partial_dies
18112 = htab_create_alloc_ex (cu->header.length / 12,
18113 partial_die_hash,
18114 partial_die_eq,
18115 NULL,
18116 &cu->comp_unit_obstack,
18117 hashtab_obstack_allocate,
18118 dummy_obstack_deallocate);
18119
18120 while (1)
18121 {
18122 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18123
18124 /* A NULL abbrev means the end of a series of children. */
18125 if (abbrev == NULL)
18126 {
18127 if (--nesting_level == 0)
18128 return first_die;
18129
18130 info_ptr += bytes_read;
18131 last_die = parent_die;
18132 parent_die = parent_die->die_parent;
18133 continue;
18134 }
18135
18136 /* Check for template arguments. We never save these; if
18137 they're seen, we just mark the parent, and go on our way. */
18138 if (parent_die != NULL
18139 && cu->language == language_cplus
18140 && (abbrev->tag == DW_TAG_template_type_param
18141 || abbrev->tag == DW_TAG_template_value_param))
18142 {
18143 parent_die->has_template_arguments = 1;
18144
18145 if (!load_all)
18146 {
18147 /* We don't need a partial DIE for the template argument. */
18148 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18149 continue;
18150 }
18151 }
18152
18153 /* We only recurse into c++ subprograms looking for template arguments.
18154 Skip their other children. */
18155 if (!load_all
18156 && cu->language == language_cplus
18157 && parent_die != NULL
18158 && parent_die->tag == DW_TAG_subprogram)
18159 {
18160 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18161 continue;
18162 }
18163
18164 /* Check whether this DIE is interesting enough to save. Normally
18165 we would not be interested in members here, but there may be
18166 later variables referencing them via DW_AT_specification (for
18167 static members). */
18168 if (!load_all
18169 && !is_type_tag_for_partial (abbrev->tag)
18170 && abbrev->tag != DW_TAG_constant
18171 && abbrev->tag != DW_TAG_enumerator
18172 && abbrev->tag != DW_TAG_subprogram
18173 && abbrev->tag != DW_TAG_inlined_subroutine
18174 && abbrev->tag != DW_TAG_lexical_block
18175 && abbrev->tag != DW_TAG_variable
18176 && abbrev->tag != DW_TAG_namespace
18177 && abbrev->tag != DW_TAG_module
18178 && abbrev->tag != DW_TAG_member
18179 && abbrev->tag != DW_TAG_imported_unit
18180 && abbrev->tag != DW_TAG_imported_declaration)
18181 {
18182 /* Otherwise we skip to the next sibling, if any. */
18183 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18184 continue;
18185 }
18186
18187 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18188 abbrev);
18189
18190 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18191
18192 /* This two-pass algorithm for processing partial symbols has a
18193 high cost in cache pressure. Thus, handle some simple cases
18194 here which cover the majority of C partial symbols. DIEs
18195 which neither have specification tags in them, nor could have
18196 specification tags elsewhere pointing at them, can simply be
18197 processed and discarded.
18198
18199 This segment is also optional; scan_partial_symbols and
18200 add_partial_symbol will handle these DIEs if we chain
18201 them in normally. When compilers which do not emit large
18202 quantities of duplicate debug information are more common,
18203 this code can probably be removed. */
18204
18205 /* Any complete simple types at the top level (pretty much all
18206 of them, for a language without namespaces), can be processed
18207 directly. */
18208 if (parent_die == NULL
18209 && pdi.has_specification == 0
18210 && pdi.is_declaration == 0
18211 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18212 || pdi.tag == DW_TAG_base_type
18213 || pdi.tag == DW_TAG_subrange_type))
18214 {
18215 if (building_psymtab && pdi.name != NULL)
18216 add_psymbol_to_list (pdi.name, false,
18217 VAR_DOMAIN, LOC_TYPEDEF, -1,
18218 psymbol_placement::STATIC,
18219 0, cu->language, objfile);
18220 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18221 continue;
18222 }
18223
18224 /* The exception for DW_TAG_typedef with has_children above is
18225 a workaround of GCC PR debug/47510. In the case of this complaint
18226 type_name_or_error will error on such types later.
18227
18228 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18229 it could not find the child DIEs referenced later, this is checked
18230 above. In correct DWARF DW_TAG_typedef should have no children. */
18231
18232 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18233 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18234 "- DIE at %s [in module %s]"),
18235 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18236
18237 /* If we're at the second level, and we're an enumerator, and
18238 our parent has no specification (meaning possibly lives in a
18239 namespace elsewhere), then we can add the partial symbol now
18240 instead of queueing it. */
18241 if (pdi.tag == DW_TAG_enumerator
18242 && parent_die != NULL
18243 && parent_die->die_parent == NULL
18244 && parent_die->tag == DW_TAG_enumeration_type
18245 && parent_die->has_specification == 0)
18246 {
18247 if (pdi.name == NULL)
18248 complaint (_("malformed enumerator DIE ignored"));
18249 else if (building_psymtab)
18250 add_psymbol_to_list (pdi.name, false,
18251 VAR_DOMAIN, LOC_CONST, -1,
18252 cu->language == language_cplus
18253 ? psymbol_placement::GLOBAL
18254 : psymbol_placement::STATIC,
18255 0, cu->language, objfile);
18256
18257 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18258 continue;
18259 }
18260
18261 struct partial_die_info *part_die
18262 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18263
18264 /* We'll save this DIE so link it in. */
18265 part_die->die_parent = parent_die;
18266 part_die->die_sibling = NULL;
18267 part_die->die_child = NULL;
18268
18269 if (last_die && last_die == parent_die)
18270 last_die->die_child = part_die;
18271 else if (last_die)
18272 last_die->die_sibling = part_die;
18273
18274 last_die = part_die;
18275
18276 if (first_die == NULL)
18277 first_die = part_die;
18278
18279 /* Maybe add the DIE to the hash table. Not all DIEs that we
18280 find interesting need to be in the hash table, because we
18281 also have the parent/sibling/child chains; only those that we
18282 might refer to by offset later during partial symbol reading.
18283
18284 For now this means things that might have be the target of a
18285 DW_AT_specification, DW_AT_abstract_origin, or
18286 DW_AT_extension. DW_AT_extension will refer only to
18287 namespaces; DW_AT_abstract_origin refers to functions (and
18288 many things under the function DIE, but we do not recurse
18289 into function DIEs during partial symbol reading) and
18290 possibly variables as well; DW_AT_specification refers to
18291 declarations. Declarations ought to have the DW_AT_declaration
18292 flag. It happens that GCC forgets to put it in sometimes, but
18293 only for functions, not for types.
18294
18295 Adding more things than necessary to the hash table is harmless
18296 except for the performance cost. Adding too few will result in
18297 wasted time in find_partial_die, when we reread the compilation
18298 unit with load_all_dies set. */
18299
18300 if (load_all
18301 || abbrev->tag == DW_TAG_constant
18302 || abbrev->tag == DW_TAG_subprogram
18303 || abbrev->tag == DW_TAG_variable
18304 || abbrev->tag == DW_TAG_namespace
18305 || part_die->is_declaration)
18306 {
18307 void **slot;
18308
18309 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18310 to_underlying (part_die->sect_off),
18311 INSERT);
18312 *slot = part_die;
18313 }
18314
18315 /* For some DIEs we want to follow their children (if any). For C
18316 we have no reason to follow the children of structures; for other
18317 languages we have to, so that we can get at method physnames
18318 to infer fully qualified class names, for DW_AT_specification,
18319 and for C++ template arguments. For C++, we also look one level
18320 inside functions to find template arguments (if the name of the
18321 function does not already contain the template arguments).
18322
18323 For Ada and Fortran, we need to scan the children of subprograms
18324 and lexical blocks as well because these languages allow the
18325 definition of nested entities that could be interesting for the
18326 debugger, such as nested subprograms for instance. */
18327 if (last_die->has_children
18328 && (load_all
18329 || last_die->tag == DW_TAG_namespace
18330 || last_die->tag == DW_TAG_module
18331 || last_die->tag == DW_TAG_enumeration_type
18332 || (cu->language == language_cplus
18333 && last_die->tag == DW_TAG_subprogram
18334 && (last_die->name == NULL
18335 || strchr (last_die->name, '<') == NULL))
18336 || (cu->language != language_c
18337 && (last_die->tag == DW_TAG_class_type
18338 || last_die->tag == DW_TAG_interface_type
18339 || last_die->tag == DW_TAG_structure_type
18340 || last_die->tag == DW_TAG_union_type))
18341 || ((cu->language == language_ada
18342 || cu->language == language_fortran)
18343 && (last_die->tag == DW_TAG_subprogram
18344 || last_die->tag == DW_TAG_lexical_block))))
18345 {
18346 nesting_level++;
18347 parent_die = last_die;
18348 continue;
18349 }
18350
18351 /* Otherwise we skip to the next sibling, if any. */
18352 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18353
18354 /* Back to the top, do it again. */
18355 }
18356 }
18357
18358 partial_die_info::partial_die_info (sect_offset sect_off_,
18359 struct abbrev_info *abbrev)
18360 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18361 {
18362 }
18363
18364 /* Read a minimal amount of information into the minimal die structure.
18365 INFO_PTR should point just after the initial uleb128 of a DIE. */
18366
18367 const gdb_byte *
18368 partial_die_info::read (const struct die_reader_specs *reader,
18369 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18370 {
18371 struct dwarf2_cu *cu = reader->cu;
18372 struct dwarf2_per_objfile *dwarf2_per_objfile
18373 = cu->per_cu->dwarf2_per_objfile;
18374 unsigned int i;
18375 int has_low_pc_attr = 0;
18376 int has_high_pc_attr = 0;
18377 int high_pc_relative = 0;
18378
18379 for (i = 0; i < abbrev.num_attrs; ++i)
18380 {
18381 attribute attr;
18382 bool need_reprocess;
18383 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18384 info_ptr, &need_reprocess);
18385 /* String and address offsets that need to do the reprocessing have
18386 already been read at this point, so there is no need to wait until
18387 the loop terminates to do the reprocessing. */
18388 if (need_reprocess)
18389 read_attribute_reprocess (reader, &attr);
18390 /* Store the data if it is of an attribute we want to keep in a
18391 partial symbol table. */
18392 switch (attr.name)
18393 {
18394 case DW_AT_name:
18395 switch (tag)
18396 {
18397 case DW_TAG_compile_unit:
18398 case DW_TAG_partial_unit:
18399 case DW_TAG_type_unit:
18400 /* Compilation units have a DW_AT_name that is a filename, not
18401 a source language identifier. */
18402 case DW_TAG_enumeration_type:
18403 case DW_TAG_enumerator:
18404 /* These tags always have simple identifiers already; no need
18405 to canonicalize them. */
18406 name = DW_STRING (&attr);
18407 break;
18408 default:
18409 {
18410 struct objfile *objfile = dwarf2_per_objfile->objfile;
18411
18412 name
18413 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18414 }
18415 break;
18416 }
18417 break;
18418 case DW_AT_linkage_name:
18419 case DW_AT_MIPS_linkage_name:
18420 /* Note that both forms of linkage name might appear. We
18421 assume they will be the same, and we only store the last
18422 one we see. */
18423 linkage_name = attr.value_as_string ();
18424 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18425 See https://github.com/rust-lang/rust/issues/32925. */
18426 if (cu->language == language_rust && linkage_name != NULL
18427 && strchr (linkage_name, '{') != NULL)
18428 linkage_name = NULL;
18429 break;
18430 case DW_AT_low_pc:
18431 has_low_pc_attr = 1;
18432 lowpc = attr.value_as_address ();
18433 break;
18434 case DW_AT_high_pc:
18435 has_high_pc_attr = 1;
18436 highpc = attr.value_as_address ();
18437 if (cu->header.version >= 4 && attr.form_is_constant ())
18438 high_pc_relative = 1;
18439 break;
18440 case DW_AT_location:
18441 /* Support the .debug_loc offsets. */
18442 if (attr.form_is_block ())
18443 {
18444 d.locdesc = DW_BLOCK (&attr);
18445 }
18446 else if (attr.form_is_section_offset ())
18447 {
18448 dwarf2_complex_location_expr_complaint ();
18449 }
18450 else
18451 {
18452 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18453 "partial symbol information");
18454 }
18455 break;
18456 case DW_AT_external:
18457 is_external = DW_UNSND (&attr);
18458 break;
18459 case DW_AT_declaration:
18460 is_declaration = DW_UNSND (&attr);
18461 break;
18462 case DW_AT_type:
18463 has_type = 1;
18464 break;
18465 case DW_AT_abstract_origin:
18466 case DW_AT_specification:
18467 case DW_AT_extension:
18468 has_specification = 1;
18469 spec_offset = attr.get_ref_die_offset ();
18470 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18471 || cu->per_cu->is_dwz);
18472 break;
18473 case DW_AT_sibling:
18474 /* Ignore absolute siblings, they might point outside of
18475 the current compile unit. */
18476 if (attr.form == DW_FORM_ref_addr)
18477 complaint (_("ignoring absolute DW_AT_sibling"));
18478 else
18479 {
18480 const gdb_byte *buffer = reader->buffer;
18481 sect_offset off = attr.get_ref_die_offset ();
18482 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18483
18484 if (sibling_ptr < info_ptr)
18485 complaint (_("DW_AT_sibling points backwards"));
18486 else if (sibling_ptr > reader->buffer_end)
18487 reader->die_section->overflow_complaint ();
18488 else
18489 sibling = sibling_ptr;
18490 }
18491 break;
18492 case DW_AT_byte_size:
18493 has_byte_size = 1;
18494 break;
18495 case DW_AT_const_value:
18496 has_const_value = 1;
18497 break;
18498 case DW_AT_calling_convention:
18499 /* DWARF doesn't provide a way to identify a program's source-level
18500 entry point. DW_AT_calling_convention attributes are only meant
18501 to describe functions' calling conventions.
18502
18503 However, because it's a necessary piece of information in
18504 Fortran, and before DWARF 4 DW_CC_program was the only
18505 piece of debugging information whose definition refers to
18506 a 'main program' at all, several compilers marked Fortran
18507 main programs with DW_CC_program --- even when those
18508 functions use the standard calling conventions.
18509
18510 Although DWARF now specifies a way to provide this
18511 information, we support this practice for backward
18512 compatibility. */
18513 if (DW_UNSND (&attr) == DW_CC_program
18514 && cu->language == language_fortran)
18515 main_subprogram = 1;
18516 break;
18517 case DW_AT_inline:
18518 if (DW_UNSND (&attr) == DW_INL_inlined
18519 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18520 may_be_inlined = 1;
18521 break;
18522
18523 case DW_AT_import:
18524 if (tag == DW_TAG_imported_unit)
18525 {
18526 d.sect_off = attr.get_ref_die_offset ();
18527 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18528 || cu->per_cu->is_dwz);
18529 }
18530 break;
18531
18532 case DW_AT_main_subprogram:
18533 main_subprogram = DW_UNSND (&attr);
18534 break;
18535
18536 case DW_AT_ranges:
18537 {
18538 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18539 but that requires a full DIE, so instead we just
18540 reimplement it. */
18541 int need_ranges_base = tag != DW_TAG_compile_unit;
18542 unsigned int ranges_offset = (DW_UNSND (&attr)
18543 + (need_ranges_base
18544 ? cu->ranges_base
18545 : 0));
18546
18547 /* Value of the DW_AT_ranges attribute is the offset in the
18548 .debug_ranges section. */
18549 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18550 nullptr))
18551 has_pc_info = 1;
18552 }
18553 break;
18554
18555 default:
18556 break;
18557 }
18558 }
18559
18560 /* For Ada, if both the name and the linkage name appear, we prefer
18561 the latter. This lets "catch exception" work better, regardless
18562 of the order in which the name and linkage name were emitted.
18563 Really, though, this is just a workaround for the fact that gdb
18564 doesn't store both the name and the linkage name. */
18565 if (cu->language == language_ada && linkage_name != nullptr)
18566 name = linkage_name;
18567
18568 if (high_pc_relative)
18569 highpc += lowpc;
18570
18571 if (has_low_pc_attr && has_high_pc_attr)
18572 {
18573 /* When using the GNU linker, .gnu.linkonce. sections are used to
18574 eliminate duplicate copies of functions and vtables and such.
18575 The linker will arbitrarily choose one and discard the others.
18576 The AT_*_pc values for such functions refer to local labels in
18577 these sections. If the section from that file was discarded, the
18578 labels are not in the output, so the relocs get a value of 0.
18579 If this is a discarded function, mark the pc bounds as invalid,
18580 so that GDB will ignore it. */
18581 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18582 {
18583 struct objfile *objfile = dwarf2_per_objfile->objfile;
18584 struct gdbarch *gdbarch = objfile->arch ();
18585
18586 complaint (_("DW_AT_low_pc %s is zero "
18587 "for DIE at %s [in module %s]"),
18588 paddress (gdbarch, lowpc),
18589 sect_offset_str (sect_off),
18590 objfile_name (objfile));
18591 }
18592 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18593 else if (lowpc >= highpc)
18594 {
18595 struct objfile *objfile = dwarf2_per_objfile->objfile;
18596 struct gdbarch *gdbarch = objfile->arch ();
18597
18598 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18599 "for DIE at %s [in module %s]"),
18600 paddress (gdbarch, lowpc),
18601 paddress (gdbarch, highpc),
18602 sect_offset_str (sect_off),
18603 objfile_name (objfile));
18604 }
18605 else
18606 has_pc_info = 1;
18607 }
18608
18609 return info_ptr;
18610 }
18611
18612 /* Find a cached partial DIE at OFFSET in CU. */
18613
18614 struct partial_die_info *
18615 dwarf2_cu::find_partial_die (sect_offset sect_off)
18616 {
18617 struct partial_die_info *lookup_die = NULL;
18618 struct partial_die_info part_die (sect_off);
18619
18620 lookup_die = ((struct partial_die_info *)
18621 htab_find_with_hash (partial_dies, &part_die,
18622 to_underlying (sect_off)));
18623
18624 return lookup_die;
18625 }
18626
18627 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18628 except in the case of .debug_types DIEs which do not reference
18629 outside their CU (they do however referencing other types via
18630 DW_FORM_ref_sig8). */
18631
18632 static const struct cu_partial_die_info
18633 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18634 {
18635 struct dwarf2_per_objfile *dwarf2_per_objfile
18636 = cu->per_cu->dwarf2_per_objfile;
18637 struct objfile *objfile = dwarf2_per_objfile->objfile;
18638 struct dwarf2_per_cu_data *per_cu = NULL;
18639 struct partial_die_info *pd = NULL;
18640
18641 if (offset_in_dwz == cu->per_cu->is_dwz
18642 && cu->header.offset_in_cu_p (sect_off))
18643 {
18644 pd = cu->find_partial_die (sect_off);
18645 if (pd != NULL)
18646 return { cu, pd };
18647 /* We missed recording what we needed.
18648 Load all dies and try again. */
18649 per_cu = cu->per_cu;
18650 }
18651 else
18652 {
18653 /* TUs don't reference other CUs/TUs (except via type signatures). */
18654 if (cu->per_cu->is_debug_types)
18655 {
18656 error (_("Dwarf Error: Type Unit at offset %s contains"
18657 " external reference to offset %s [in module %s].\n"),
18658 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18659 bfd_get_filename (objfile->obfd));
18660 }
18661 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18662 dwarf2_per_objfile);
18663
18664 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18665 load_partial_comp_unit (per_cu);
18666
18667 per_cu->cu->last_used = 0;
18668 pd = per_cu->cu->find_partial_die (sect_off);
18669 }
18670
18671 /* If we didn't find it, and not all dies have been loaded,
18672 load them all and try again. */
18673
18674 if (pd == NULL && per_cu->load_all_dies == 0)
18675 {
18676 per_cu->load_all_dies = 1;
18677
18678 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18679 THIS_CU->cu may already be in use. So we can't just free it and
18680 replace its DIEs with the ones we read in. Instead, we leave those
18681 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18682 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18683 set. */
18684 load_partial_comp_unit (per_cu);
18685
18686 pd = per_cu->cu->find_partial_die (sect_off);
18687 }
18688
18689 if (pd == NULL)
18690 internal_error (__FILE__, __LINE__,
18691 _("could not find partial DIE %s "
18692 "in cache [from module %s]\n"),
18693 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18694 return { per_cu->cu, pd };
18695 }
18696
18697 /* See if we can figure out if the class lives in a namespace. We do
18698 this by looking for a member function; its demangled name will
18699 contain namespace info, if there is any. */
18700
18701 static void
18702 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18703 struct dwarf2_cu *cu)
18704 {
18705 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18706 what template types look like, because the demangler
18707 frequently doesn't give the same name as the debug info. We
18708 could fix this by only using the demangled name to get the
18709 prefix (but see comment in read_structure_type). */
18710
18711 struct partial_die_info *real_pdi;
18712 struct partial_die_info *child_pdi;
18713
18714 /* If this DIE (this DIE's specification, if any) has a parent, then
18715 we should not do this. We'll prepend the parent's fully qualified
18716 name when we create the partial symbol. */
18717
18718 real_pdi = struct_pdi;
18719 while (real_pdi->has_specification)
18720 {
18721 auto res = find_partial_die (real_pdi->spec_offset,
18722 real_pdi->spec_is_dwz, cu);
18723 real_pdi = res.pdi;
18724 cu = res.cu;
18725 }
18726
18727 if (real_pdi->die_parent != NULL)
18728 return;
18729
18730 for (child_pdi = struct_pdi->die_child;
18731 child_pdi != NULL;
18732 child_pdi = child_pdi->die_sibling)
18733 {
18734 if (child_pdi->tag == DW_TAG_subprogram
18735 && child_pdi->linkage_name != NULL)
18736 {
18737 gdb::unique_xmalloc_ptr<char> actual_class_name
18738 (language_class_name_from_physname (cu->language_defn,
18739 child_pdi->linkage_name));
18740 if (actual_class_name != NULL)
18741 {
18742 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18743 struct_pdi->name = objfile->intern (actual_class_name.get ());
18744 }
18745 break;
18746 }
18747 }
18748 }
18749
18750 /* Return true if a DIE with TAG may have the DW_AT_const_value
18751 attribute. */
18752
18753 static bool
18754 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18755 {
18756 switch (tag)
18757 {
18758 case DW_TAG_constant:
18759 case DW_TAG_enumerator:
18760 case DW_TAG_formal_parameter:
18761 case DW_TAG_template_value_param:
18762 case DW_TAG_variable:
18763 return true;
18764 }
18765
18766 return false;
18767 }
18768
18769 void
18770 partial_die_info::fixup (struct dwarf2_cu *cu)
18771 {
18772 /* Once we've fixed up a die, there's no point in doing so again.
18773 This also avoids a memory leak if we were to call
18774 guess_partial_die_structure_name multiple times. */
18775 if (fixup_called)
18776 return;
18777
18778 /* If we found a reference attribute and the DIE has no name, try
18779 to find a name in the referred to DIE. */
18780
18781 if (name == NULL && has_specification)
18782 {
18783 struct partial_die_info *spec_die;
18784
18785 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18786 spec_die = res.pdi;
18787 cu = res.cu;
18788
18789 spec_die->fixup (cu);
18790
18791 if (spec_die->name)
18792 {
18793 name = spec_die->name;
18794
18795 /* Copy DW_AT_external attribute if it is set. */
18796 if (spec_die->is_external)
18797 is_external = spec_die->is_external;
18798 }
18799 }
18800
18801 if (!has_const_value && has_specification
18802 && can_have_DW_AT_const_value_p (tag))
18803 {
18804 struct partial_die_info *spec_die;
18805
18806 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18807 spec_die = res.pdi;
18808 cu = res.cu;
18809
18810 spec_die->fixup (cu);
18811
18812 if (spec_die->has_const_value)
18813 {
18814 /* Copy DW_AT_const_value attribute if it is set. */
18815 has_const_value = spec_die->has_const_value;
18816 }
18817 }
18818
18819 /* Set default names for some unnamed DIEs. */
18820
18821 if (name == NULL && tag == DW_TAG_namespace)
18822 name = CP_ANONYMOUS_NAMESPACE_STR;
18823
18824 /* If there is no parent die to provide a namespace, and there are
18825 children, see if we can determine the namespace from their linkage
18826 name. */
18827 if (cu->language == language_cplus
18828 && !cu->per_cu->dwarf2_per_objfile->per_bfd->types.empty ()
18829 && die_parent == NULL
18830 && has_children
18831 && (tag == DW_TAG_class_type
18832 || tag == DW_TAG_structure_type
18833 || tag == DW_TAG_union_type))
18834 guess_partial_die_structure_name (this, cu);
18835
18836 /* GCC might emit a nameless struct or union that has a linkage
18837 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18838 if (name == NULL
18839 && (tag == DW_TAG_class_type
18840 || tag == DW_TAG_interface_type
18841 || tag == DW_TAG_structure_type
18842 || tag == DW_TAG_union_type)
18843 && linkage_name != NULL)
18844 {
18845 gdb::unique_xmalloc_ptr<char> demangled
18846 (gdb_demangle (linkage_name, DMGL_TYPES));
18847 if (demangled != nullptr)
18848 {
18849 const char *base;
18850
18851 /* Strip any leading namespaces/classes, keep only the base name.
18852 DW_AT_name for named DIEs does not contain the prefixes. */
18853 base = strrchr (demangled.get (), ':');
18854 if (base && base > demangled.get () && base[-1] == ':')
18855 base++;
18856 else
18857 base = demangled.get ();
18858
18859 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18860 name = objfile->intern (base);
18861 }
18862 }
18863
18864 fixup_called = 1;
18865 }
18866
18867 /* Read the .debug_loclists header contents from the given SECTION in the
18868 HEADER. */
18869 static void
18870 read_loclist_header (struct loclist_header *header,
18871 struct dwarf2_section_info *section)
18872 {
18873 unsigned int bytes_read;
18874 bfd *abfd = section->get_bfd_owner ();
18875 const gdb_byte *info_ptr = section->buffer;
18876 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18877 info_ptr += bytes_read;
18878 header->version = read_2_bytes (abfd, info_ptr);
18879 info_ptr += 2;
18880 header->addr_size = read_1_byte (abfd, info_ptr);
18881 info_ptr += 1;
18882 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18883 info_ptr += 1;
18884 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18885 }
18886
18887 /* Return the DW_AT_loclists_base value for the CU. */
18888 static ULONGEST
18889 lookup_loclist_base (struct dwarf2_cu *cu)
18890 {
18891 /* For the .dwo unit, the loclist_base points to the first offset following
18892 the header. The header consists of the following entities-
18893 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18894 bit format)
18895 2. version (2 bytes)
18896 3. address size (1 byte)
18897 4. segment selector size (1 byte)
18898 5. offset entry count (4 bytes)
18899 These sizes are derived as per the DWARFv5 standard. */
18900 if (cu->dwo_unit != nullptr)
18901 {
18902 if (cu->header.initial_length_size == 4)
18903 return LOCLIST_HEADER_SIZE32;
18904 return LOCLIST_HEADER_SIZE64;
18905 }
18906 return cu->loclist_base;
18907 }
18908
18909 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18910 array of offsets in the .debug_loclists section. */
18911 static CORE_ADDR
18912 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18913 {
18914 struct dwarf2_per_objfile *dwarf2_per_objfile
18915 = cu->per_cu->dwarf2_per_objfile;
18916 struct objfile *objfile = dwarf2_per_objfile->objfile;
18917 bfd *abfd = objfile->obfd;
18918 ULONGEST loclist_base = lookup_loclist_base (cu);
18919 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18920
18921 section->read (objfile);
18922 if (section->buffer == NULL)
18923 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18924 "section [in module %s]"), objfile_name (objfile));
18925 struct loclist_header header;
18926 read_loclist_header (&header, section);
18927 if (loclist_index >= header.offset_entry_count)
18928 complaint (_("DW_FORM_loclistx pointing outside of "
18929 ".debug_loclists offset array [in module %s]"),
18930 objfile_name (objfile));
18931 if (loclist_base + loclist_index * cu->header.offset_size
18932 >= section->size)
18933 complaint (_("DW_FORM_loclistx pointing outside of "
18934 ".debug_loclists section [in module %s]"),
18935 objfile_name (objfile));
18936 const gdb_byte *info_ptr
18937 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18938
18939 if (cu->header.offset_size == 4)
18940 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18941 else
18942 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18943 }
18944
18945 /* Process the attributes that had to be skipped in the first round. These
18946 attributes are the ones that need str_offsets_base or addr_base attributes.
18947 They could not have been processed in the first round, because at the time
18948 the values of str_offsets_base or addr_base may not have been known. */
18949 static void
18950 read_attribute_reprocess (const struct die_reader_specs *reader,
18951 struct attribute *attr)
18952 {
18953 struct dwarf2_cu *cu = reader->cu;
18954 switch (attr->form)
18955 {
18956 case DW_FORM_addrx:
18957 case DW_FORM_GNU_addr_index:
18958 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18959 break;
18960 case DW_FORM_loclistx:
18961 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18962 break;
18963 case DW_FORM_strx:
18964 case DW_FORM_strx1:
18965 case DW_FORM_strx2:
18966 case DW_FORM_strx3:
18967 case DW_FORM_strx4:
18968 case DW_FORM_GNU_str_index:
18969 {
18970 unsigned int str_index = DW_UNSND (attr);
18971 if (reader->dwo_file != NULL)
18972 {
18973 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18974 DW_STRING_IS_CANONICAL (attr) = 0;
18975 }
18976 else
18977 {
18978 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18979 DW_STRING_IS_CANONICAL (attr) = 0;
18980 }
18981 break;
18982 }
18983 default:
18984 gdb_assert_not_reached (_("Unexpected DWARF form."));
18985 }
18986 }
18987
18988 /* Read an attribute value described by an attribute form. */
18989
18990 static const gdb_byte *
18991 read_attribute_value (const struct die_reader_specs *reader,
18992 struct attribute *attr, unsigned form,
18993 LONGEST implicit_const, const gdb_byte *info_ptr,
18994 bool *need_reprocess)
18995 {
18996 struct dwarf2_cu *cu = reader->cu;
18997 struct dwarf2_per_objfile *dwarf2_per_objfile
18998 = cu->per_cu->dwarf2_per_objfile;
18999 struct objfile *objfile = dwarf2_per_objfile->objfile;
19000 bfd *abfd = reader->abfd;
19001 struct comp_unit_head *cu_header = &cu->header;
19002 unsigned int bytes_read;
19003 struct dwarf_block *blk;
19004 *need_reprocess = false;
19005
19006 attr->form = (enum dwarf_form) form;
19007 switch (form)
19008 {
19009 case DW_FORM_ref_addr:
19010 if (cu->header.version == 2)
19011 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19012 &bytes_read);
19013 else
19014 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19015 &bytes_read);
19016 info_ptr += bytes_read;
19017 break;
19018 case DW_FORM_GNU_ref_alt:
19019 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19020 info_ptr += bytes_read;
19021 break;
19022 case DW_FORM_addr:
19023 {
19024 struct gdbarch *gdbarch = objfile->arch ();
19025 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19026 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19027 info_ptr += bytes_read;
19028 }
19029 break;
19030 case DW_FORM_block2:
19031 blk = dwarf_alloc_block (cu);
19032 blk->size = read_2_bytes (abfd, info_ptr);
19033 info_ptr += 2;
19034 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19035 info_ptr += blk->size;
19036 DW_BLOCK (attr) = blk;
19037 break;
19038 case DW_FORM_block4:
19039 blk = dwarf_alloc_block (cu);
19040 blk->size = read_4_bytes (abfd, info_ptr);
19041 info_ptr += 4;
19042 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19043 info_ptr += blk->size;
19044 DW_BLOCK (attr) = blk;
19045 break;
19046 case DW_FORM_data2:
19047 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19048 info_ptr += 2;
19049 break;
19050 case DW_FORM_data4:
19051 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19052 info_ptr += 4;
19053 break;
19054 case DW_FORM_data8:
19055 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19056 info_ptr += 8;
19057 break;
19058 case DW_FORM_data16:
19059 blk = dwarf_alloc_block (cu);
19060 blk->size = 16;
19061 blk->data = read_n_bytes (abfd, info_ptr, 16);
19062 info_ptr += 16;
19063 DW_BLOCK (attr) = blk;
19064 break;
19065 case DW_FORM_sec_offset:
19066 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19067 info_ptr += bytes_read;
19068 break;
19069 case DW_FORM_loclistx:
19070 {
19071 *need_reprocess = true;
19072 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19073 info_ptr += bytes_read;
19074 }
19075 break;
19076 case DW_FORM_string:
19077 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19078 DW_STRING_IS_CANONICAL (attr) = 0;
19079 info_ptr += bytes_read;
19080 break;
19081 case DW_FORM_strp:
19082 if (!cu->per_cu->is_dwz)
19083 {
19084 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19085 abfd, info_ptr, cu_header,
19086 &bytes_read);
19087 DW_STRING_IS_CANONICAL (attr) = 0;
19088 info_ptr += bytes_read;
19089 break;
19090 }
19091 /* FALLTHROUGH */
19092 case DW_FORM_line_strp:
19093 if (!cu->per_cu->is_dwz)
19094 {
19095 DW_STRING (attr)
19096 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19097 &bytes_read);
19098 DW_STRING_IS_CANONICAL (attr) = 0;
19099 info_ptr += bytes_read;
19100 break;
19101 }
19102 /* FALLTHROUGH */
19103 case DW_FORM_GNU_strp_alt:
19104 {
19105 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19106 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19107 &bytes_read);
19108
19109 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19110 DW_STRING_IS_CANONICAL (attr) = 0;
19111 info_ptr += bytes_read;
19112 }
19113 break;
19114 case DW_FORM_exprloc:
19115 case DW_FORM_block:
19116 blk = dwarf_alloc_block (cu);
19117 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19118 info_ptr += bytes_read;
19119 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19120 info_ptr += blk->size;
19121 DW_BLOCK (attr) = blk;
19122 break;
19123 case DW_FORM_block1:
19124 blk = dwarf_alloc_block (cu);
19125 blk->size = read_1_byte (abfd, info_ptr);
19126 info_ptr += 1;
19127 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19128 info_ptr += blk->size;
19129 DW_BLOCK (attr) = blk;
19130 break;
19131 case DW_FORM_data1:
19132 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19133 info_ptr += 1;
19134 break;
19135 case DW_FORM_flag:
19136 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19137 info_ptr += 1;
19138 break;
19139 case DW_FORM_flag_present:
19140 DW_UNSND (attr) = 1;
19141 break;
19142 case DW_FORM_sdata:
19143 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19144 info_ptr += bytes_read;
19145 break;
19146 case DW_FORM_udata:
19147 case DW_FORM_rnglistx:
19148 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19149 info_ptr += bytes_read;
19150 break;
19151 case DW_FORM_ref1:
19152 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19153 + read_1_byte (abfd, info_ptr));
19154 info_ptr += 1;
19155 break;
19156 case DW_FORM_ref2:
19157 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19158 + read_2_bytes (abfd, info_ptr));
19159 info_ptr += 2;
19160 break;
19161 case DW_FORM_ref4:
19162 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19163 + read_4_bytes (abfd, info_ptr));
19164 info_ptr += 4;
19165 break;
19166 case DW_FORM_ref8:
19167 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19168 + read_8_bytes (abfd, info_ptr));
19169 info_ptr += 8;
19170 break;
19171 case DW_FORM_ref_sig8:
19172 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19173 info_ptr += 8;
19174 break;
19175 case DW_FORM_ref_udata:
19176 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19177 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19178 info_ptr += bytes_read;
19179 break;
19180 case DW_FORM_indirect:
19181 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19182 info_ptr += bytes_read;
19183 if (form == DW_FORM_implicit_const)
19184 {
19185 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19186 info_ptr += bytes_read;
19187 }
19188 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19189 info_ptr, need_reprocess);
19190 break;
19191 case DW_FORM_implicit_const:
19192 DW_SND (attr) = implicit_const;
19193 break;
19194 case DW_FORM_addrx:
19195 case DW_FORM_GNU_addr_index:
19196 *need_reprocess = true;
19197 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19198 info_ptr += bytes_read;
19199 break;
19200 case DW_FORM_strx:
19201 case DW_FORM_strx1:
19202 case DW_FORM_strx2:
19203 case DW_FORM_strx3:
19204 case DW_FORM_strx4:
19205 case DW_FORM_GNU_str_index:
19206 {
19207 ULONGEST str_index;
19208 if (form == DW_FORM_strx1)
19209 {
19210 str_index = read_1_byte (abfd, info_ptr);
19211 info_ptr += 1;
19212 }
19213 else if (form == DW_FORM_strx2)
19214 {
19215 str_index = read_2_bytes (abfd, info_ptr);
19216 info_ptr += 2;
19217 }
19218 else if (form == DW_FORM_strx3)
19219 {
19220 str_index = read_3_bytes (abfd, info_ptr);
19221 info_ptr += 3;
19222 }
19223 else if (form == DW_FORM_strx4)
19224 {
19225 str_index = read_4_bytes (abfd, info_ptr);
19226 info_ptr += 4;
19227 }
19228 else
19229 {
19230 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19231 info_ptr += bytes_read;
19232 }
19233 *need_reprocess = true;
19234 DW_UNSND (attr) = str_index;
19235 }
19236 break;
19237 default:
19238 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19239 dwarf_form_name (form),
19240 bfd_get_filename (abfd));
19241 }
19242
19243 /* Super hack. */
19244 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19245 attr->form = DW_FORM_GNU_ref_alt;
19246
19247 /* We have seen instances where the compiler tried to emit a byte
19248 size attribute of -1 which ended up being encoded as an unsigned
19249 0xffffffff. Although 0xffffffff is technically a valid size value,
19250 an object of this size seems pretty unlikely so we can relatively
19251 safely treat these cases as if the size attribute was invalid and
19252 treat them as zero by default. */
19253 if (attr->name == DW_AT_byte_size
19254 && form == DW_FORM_data4
19255 && DW_UNSND (attr) >= 0xffffffff)
19256 {
19257 complaint
19258 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19259 hex_string (DW_UNSND (attr)));
19260 DW_UNSND (attr) = 0;
19261 }
19262
19263 return info_ptr;
19264 }
19265
19266 /* Read an attribute described by an abbreviated attribute. */
19267
19268 static const gdb_byte *
19269 read_attribute (const struct die_reader_specs *reader,
19270 struct attribute *attr, struct attr_abbrev *abbrev,
19271 const gdb_byte *info_ptr, bool *need_reprocess)
19272 {
19273 attr->name = abbrev->name;
19274 return read_attribute_value (reader, attr, abbrev->form,
19275 abbrev->implicit_const, info_ptr,
19276 need_reprocess);
19277 }
19278
19279 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19280
19281 static const char *
19282 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19283 LONGEST str_offset)
19284 {
19285 return dwarf2_per_objfile->per_bfd->str.read_string
19286 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19287 }
19288
19289 /* Return pointer to string at .debug_str offset as read from BUF.
19290 BUF is assumed to be in a compilation unit described by CU_HEADER.
19291 Return *BYTES_READ_PTR count of bytes read from BUF. */
19292
19293 static const char *
19294 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19295 const gdb_byte *buf,
19296 const struct comp_unit_head *cu_header,
19297 unsigned int *bytes_read_ptr)
19298 {
19299 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19300
19301 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19302 }
19303
19304 /* See read.h. */
19305
19306 const char *
19307 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19308 const struct comp_unit_head *cu_header,
19309 unsigned int *bytes_read_ptr)
19310 {
19311 bfd *abfd = objfile->obfd;
19312 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19313
19314 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19315 }
19316
19317 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19318 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19319 ADDR_SIZE is the size of addresses from the CU header. */
19320
19321 static CORE_ADDR
19322 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19323 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19324 int addr_size)
19325 {
19326 struct objfile *objfile = dwarf2_per_objfile->objfile;
19327 bfd *abfd = objfile->obfd;
19328 const gdb_byte *info_ptr;
19329 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19330
19331 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19332 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19333 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19334 objfile_name (objfile));
19335 if (addr_base_or_zero + addr_index * addr_size
19336 >= dwarf2_per_objfile->per_bfd->addr.size)
19337 error (_("DW_FORM_addr_index pointing outside of "
19338 ".debug_addr section [in module %s]"),
19339 objfile_name (objfile));
19340 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19341 + addr_base_or_zero + addr_index * addr_size);
19342 if (addr_size == 4)
19343 return bfd_get_32 (abfd, info_ptr);
19344 else
19345 return bfd_get_64 (abfd, info_ptr);
19346 }
19347
19348 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19349
19350 static CORE_ADDR
19351 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19352 {
19353 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19354 cu->addr_base, cu->header.addr_size);
19355 }
19356
19357 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19358
19359 static CORE_ADDR
19360 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19361 unsigned int *bytes_read)
19362 {
19363 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19364 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19365
19366 return read_addr_index (cu, addr_index);
19367 }
19368
19369 /* See read.h. */
19370
19371 CORE_ADDR
19372 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
19373 {
19374 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19375 struct dwarf2_cu *cu = per_cu->cu;
19376 gdb::optional<ULONGEST> addr_base;
19377 int addr_size;
19378
19379 /* We need addr_base and addr_size.
19380 If we don't have PER_CU->cu, we have to get it.
19381 Nasty, but the alternative is storing the needed info in PER_CU,
19382 which at this point doesn't seem justified: it's not clear how frequently
19383 it would get used and it would increase the size of every PER_CU.
19384 Entry points like dwarf2_per_cu_addr_size do a similar thing
19385 so we're not in uncharted territory here.
19386 Alas we need to be a bit more complicated as addr_base is contained
19387 in the DIE.
19388
19389 We don't need to read the entire CU(/TU).
19390 We just need the header and top level die.
19391
19392 IWBN to use the aging mechanism to let us lazily later discard the CU.
19393 For now we skip this optimization. */
19394
19395 if (cu != NULL)
19396 {
19397 addr_base = cu->addr_base;
19398 addr_size = cu->header.addr_size;
19399 }
19400 else
19401 {
19402 cutu_reader reader (per_cu, NULL, 0, false);
19403 addr_base = reader.cu->addr_base;
19404 addr_size = reader.cu->header.addr_size;
19405 }
19406
19407 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19408 addr_size);
19409 }
19410
19411 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19412 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19413 DWO file. */
19414
19415 static const char *
19416 read_str_index (struct dwarf2_cu *cu,
19417 struct dwarf2_section_info *str_section,
19418 struct dwarf2_section_info *str_offsets_section,
19419 ULONGEST str_offsets_base, ULONGEST str_index)
19420 {
19421 struct dwarf2_per_objfile *dwarf2_per_objfile
19422 = cu->per_cu->dwarf2_per_objfile;
19423 struct objfile *objfile = dwarf2_per_objfile->objfile;
19424 const char *objf_name = objfile_name (objfile);
19425 bfd *abfd = objfile->obfd;
19426 const gdb_byte *info_ptr;
19427 ULONGEST str_offset;
19428 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19429
19430 str_section->read (objfile);
19431 str_offsets_section->read (objfile);
19432 if (str_section->buffer == NULL)
19433 error (_("%s used without %s section"
19434 " in CU at offset %s [in module %s]"),
19435 form_name, str_section->get_name (),
19436 sect_offset_str (cu->header.sect_off), objf_name);
19437 if (str_offsets_section->buffer == NULL)
19438 error (_("%s used without %s section"
19439 " in CU at offset %s [in module %s]"),
19440 form_name, str_section->get_name (),
19441 sect_offset_str (cu->header.sect_off), objf_name);
19442 info_ptr = (str_offsets_section->buffer
19443 + str_offsets_base
19444 + str_index * cu->header.offset_size);
19445 if (cu->header.offset_size == 4)
19446 str_offset = bfd_get_32 (abfd, info_ptr);
19447 else
19448 str_offset = bfd_get_64 (abfd, info_ptr);
19449 if (str_offset >= str_section->size)
19450 error (_("Offset from %s pointing outside of"
19451 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19452 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19453 return (const char *) (str_section->buffer + str_offset);
19454 }
19455
19456 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19457
19458 static const char *
19459 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19460 {
19461 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19462 ? reader->cu->header.addr_size : 0;
19463 return read_str_index (reader->cu,
19464 &reader->dwo_file->sections.str,
19465 &reader->dwo_file->sections.str_offsets,
19466 str_offsets_base, str_index);
19467 }
19468
19469 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19470
19471 static const char *
19472 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19473 {
19474 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19475 const char *objf_name = objfile_name (objfile);
19476 static const char form_name[] = "DW_FORM_GNU_str_index";
19477 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19478
19479 if (!cu->str_offsets_base.has_value ())
19480 error (_("%s used in Fission stub without %s"
19481 " in CU at offset 0x%lx [in module %s]"),
19482 form_name, str_offsets_attr_name,
19483 (long) cu->header.offset_size, objf_name);
19484
19485 return read_str_index (cu,
19486 &cu->per_cu->dwarf2_per_objfile->per_bfd->str,
19487 &cu->per_cu->dwarf2_per_objfile->per_bfd->str_offsets,
19488 *cu->str_offsets_base, str_index);
19489 }
19490
19491 /* Return the length of an LEB128 number in BUF. */
19492
19493 static int
19494 leb128_size (const gdb_byte *buf)
19495 {
19496 const gdb_byte *begin = buf;
19497 gdb_byte byte;
19498
19499 while (1)
19500 {
19501 byte = *buf++;
19502 if ((byte & 128) == 0)
19503 return buf - begin;
19504 }
19505 }
19506
19507 static void
19508 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19509 {
19510 switch (lang)
19511 {
19512 case DW_LANG_C89:
19513 case DW_LANG_C99:
19514 case DW_LANG_C11:
19515 case DW_LANG_C:
19516 case DW_LANG_UPC:
19517 cu->language = language_c;
19518 break;
19519 case DW_LANG_Java:
19520 case DW_LANG_C_plus_plus:
19521 case DW_LANG_C_plus_plus_11:
19522 case DW_LANG_C_plus_plus_14:
19523 cu->language = language_cplus;
19524 break;
19525 case DW_LANG_D:
19526 cu->language = language_d;
19527 break;
19528 case DW_LANG_Fortran77:
19529 case DW_LANG_Fortran90:
19530 case DW_LANG_Fortran95:
19531 case DW_LANG_Fortran03:
19532 case DW_LANG_Fortran08:
19533 cu->language = language_fortran;
19534 break;
19535 case DW_LANG_Go:
19536 cu->language = language_go;
19537 break;
19538 case DW_LANG_Mips_Assembler:
19539 cu->language = language_asm;
19540 break;
19541 case DW_LANG_Ada83:
19542 case DW_LANG_Ada95:
19543 cu->language = language_ada;
19544 break;
19545 case DW_LANG_Modula2:
19546 cu->language = language_m2;
19547 break;
19548 case DW_LANG_Pascal83:
19549 cu->language = language_pascal;
19550 break;
19551 case DW_LANG_ObjC:
19552 cu->language = language_objc;
19553 break;
19554 case DW_LANG_Rust:
19555 case DW_LANG_Rust_old:
19556 cu->language = language_rust;
19557 break;
19558 case DW_LANG_Cobol74:
19559 case DW_LANG_Cobol85:
19560 default:
19561 cu->language = language_minimal;
19562 break;
19563 }
19564 cu->language_defn = language_def (cu->language);
19565 }
19566
19567 /* Return the named attribute or NULL if not there. */
19568
19569 static struct attribute *
19570 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19571 {
19572 for (;;)
19573 {
19574 unsigned int i;
19575 struct attribute *spec = NULL;
19576
19577 for (i = 0; i < die->num_attrs; ++i)
19578 {
19579 if (die->attrs[i].name == name)
19580 return &die->attrs[i];
19581 if (die->attrs[i].name == DW_AT_specification
19582 || die->attrs[i].name == DW_AT_abstract_origin)
19583 spec = &die->attrs[i];
19584 }
19585
19586 if (!spec)
19587 break;
19588
19589 die = follow_die_ref (die, spec, &cu);
19590 }
19591
19592 return NULL;
19593 }
19594
19595 /* Return the string associated with a string-typed attribute, or NULL if it
19596 is either not found or is of an incorrect type. */
19597
19598 static const char *
19599 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19600 {
19601 struct attribute *attr;
19602 const char *str = NULL;
19603
19604 attr = dwarf2_attr (die, name, cu);
19605
19606 if (attr != NULL)
19607 {
19608 str = attr->value_as_string ();
19609 if (str == nullptr)
19610 complaint (_("string type expected for attribute %s for "
19611 "DIE at %s in module %s"),
19612 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19613 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19614 }
19615
19616 return str;
19617 }
19618
19619 /* Return the dwo name or NULL if not present. If present, it is in either
19620 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19621 static const char *
19622 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19623 {
19624 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19625 if (dwo_name == nullptr)
19626 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19627 return dwo_name;
19628 }
19629
19630 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19631 and holds a non-zero value. This function should only be used for
19632 DW_FORM_flag or DW_FORM_flag_present attributes. */
19633
19634 static int
19635 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19636 {
19637 struct attribute *attr = dwarf2_attr (die, name, cu);
19638
19639 return (attr && DW_UNSND (attr));
19640 }
19641
19642 static int
19643 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19644 {
19645 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19646 which value is non-zero. However, we have to be careful with
19647 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19648 (via dwarf2_flag_true_p) follows this attribute. So we may
19649 end up accidently finding a declaration attribute that belongs
19650 to a different DIE referenced by the specification attribute,
19651 even though the given DIE does not have a declaration attribute. */
19652 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19653 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19654 }
19655
19656 /* Return the die giving the specification for DIE, if there is
19657 one. *SPEC_CU is the CU containing DIE on input, and the CU
19658 containing the return value on output. If there is no
19659 specification, but there is an abstract origin, that is
19660 returned. */
19661
19662 static struct die_info *
19663 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19664 {
19665 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19666 *spec_cu);
19667
19668 if (spec_attr == NULL)
19669 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19670
19671 if (spec_attr == NULL)
19672 return NULL;
19673 else
19674 return follow_die_ref (die, spec_attr, spec_cu);
19675 }
19676
19677 /* Stub for free_line_header to match void * callback types. */
19678
19679 static void
19680 free_line_header_voidp (void *arg)
19681 {
19682 struct line_header *lh = (struct line_header *) arg;
19683
19684 delete lh;
19685 }
19686
19687 /* A convenience function to find the proper .debug_line section for a CU. */
19688
19689 static struct dwarf2_section_info *
19690 get_debug_line_section (struct dwarf2_cu *cu)
19691 {
19692 struct dwarf2_section_info *section;
19693 struct dwarf2_per_objfile *dwarf2_per_objfile
19694 = cu->per_cu->dwarf2_per_objfile;
19695
19696 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19697 DWO file. */
19698 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19699 section = &cu->dwo_unit->dwo_file->sections.line;
19700 else if (cu->per_cu->is_dwz)
19701 {
19702 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19703
19704 section = &dwz->line;
19705 }
19706 else
19707 section = &dwarf2_per_objfile->per_bfd->line;
19708
19709 return section;
19710 }
19711
19712 /* Read the statement program header starting at OFFSET in
19713 .debug_line, or .debug_line.dwo. Return a pointer
19714 to a struct line_header, allocated using xmalloc.
19715 Returns NULL if there is a problem reading the header, e.g., if it
19716 has a version we don't understand.
19717
19718 NOTE: the strings in the include directory and file name tables of
19719 the returned object point into the dwarf line section buffer,
19720 and must not be freed. */
19721
19722 static line_header_up
19723 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19724 {
19725 struct dwarf2_section_info *section;
19726 struct dwarf2_per_objfile *dwarf2_per_objfile
19727 = cu->per_cu->dwarf2_per_objfile;
19728
19729 section = get_debug_line_section (cu);
19730 section->read (dwarf2_per_objfile->objfile);
19731 if (section->buffer == NULL)
19732 {
19733 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19734 complaint (_("missing .debug_line.dwo section"));
19735 else
19736 complaint (_("missing .debug_line section"));
19737 return 0;
19738 }
19739
19740 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19741 dwarf2_per_objfile, section,
19742 &cu->header);
19743 }
19744
19745 /* Subroutine of dwarf_decode_lines to simplify it.
19746 Return the file name of the psymtab for the given file_entry.
19747 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19748 If space for the result is malloc'd, *NAME_HOLDER will be set.
19749 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19750
19751 static const char *
19752 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19753 const dwarf2_psymtab *pst,
19754 const char *comp_dir,
19755 gdb::unique_xmalloc_ptr<char> *name_holder)
19756 {
19757 const char *include_name = fe.name;
19758 const char *include_name_to_compare = include_name;
19759 const char *pst_filename;
19760 int file_is_pst;
19761
19762 const char *dir_name = fe.include_dir (lh);
19763
19764 gdb::unique_xmalloc_ptr<char> hold_compare;
19765 if (!IS_ABSOLUTE_PATH (include_name)
19766 && (dir_name != NULL || comp_dir != NULL))
19767 {
19768 /* Avoid creating a duplicate psymtab for PST.
19769 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19770 Before we do the comparison, however, we need to account
19771 for DIR_NAME and COMP_DIR.
19772 First prepend dir_name (if non-NULL). If we still don't
19773 have an absolute path prepend comp_dir (if non-NULL).
19774 However, the directory we record in the include-file's
19775 psymtab does not contain COMP_DIR (to match the
19776 corresponding symtab(s)).
19777
19778 Example:
19779
19780 bash$ cd /tmp
19781 bash$ gcc -g ./hello.c
19782 include_name = "hello.c"
19783 dir_name = "."
19784 DW_AT_comp_dir = comp_dir = "/tmp"
19785 DW_AT_name = "./hello.c"
19786
19787 */
19788
19789 if (dir_name != NULL)
19790 {
19791 name_holder->reset (concat (dir_name, SLASH_STRING,
19792 include_name, (char *) NULL));
19793 include_name = name_holder->get ();
19794 include_name_to_compare = include_name;
19795 }
19796 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19797 {
19798 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19799 include_name, (char *) NULL));
19800 include_name_to_compare = hold_compare.get ();
19801 }
19802 }
19803
19804 pst_filename = pst->filename;
19805 gdb::unique_xmalloc_ptr<char> copied_name;
19806 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19807 {
19808 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19809 pst_filename, (char *) NULL));
19810 pst_filename = copied_name.get ();
19811 }
19812
19813 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19814
19815 if (file_is_pst)
19816 return NULL;
19817 return include_name;
19818 }
19819
19820 /* State machine to track the state of the line number program. */
19821
19822 class lnp_state_machine
19823 {
19824 public:
19825 /* Initialize a machine state for the start of a line number
19826 program. */
19827 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19828 bool record_lines_p);
19829
19830 file_entry *current_file ()
19831 {
19832 /* lh->file_names is 0-based, but the file name numbers in the
19833 statement program are 1-based. */
19834 return m_line_header->file_name_at (m_file);
19835 }
19836
19837 /* Record the line in the state machine. END_SEQUENCE is true if
19838 we're processing the end of a sequence. */
19839 void record_line (bool end_sequence);
19840
19841 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19842 nop-out rest of the lines in this sequence. */
19843 void check_line_address (struct dwarf2_cu *cu,
19844 const gdb_byte *line_ptr,
19845 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19846
19847 void handle_set_discriminator (unsigned int discriminator)
19848 {
19849 m_discriminator = discriminator;
19850 m_line_has_non_zero_discriminator |= discriminator != 0;
19851 }
19852
19853 /* Handle DW_LNE_set_address. */
19854 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19855 {
19856 m_op_index = 0;
19857 address += baseaddr;
19858 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19859 }
19860
19861 /* Handle DW_LNS_advance_pc. */
19862 void handle_advance_pc (CORE_ADDR adjust);
19863
19864 /* Handle a special opcode. */
19865 void handle_special_opcode (unsigned char op_code);
19866
19867 /* Handle DW_LNS_advance_line. */
19868 void handle_advance_line (int line_delta)
19869 {
19870 advance_line (line_delta);
19871 }
19872
19873 /* Handle DW_LNS_set_file. */
19874 void handle_set_file (file_name_index file);
19875
19876 /* Handle DW_LNS_negate_stmt. */
19877 void handle_negate_stmt ()
19878 {
19879 m_is_stmt = !m_is_stmt;
19880 }
19881
19882 /* Handle DW_LNS_const_add_pc. */
19883 void handle_const_add_pc ();
19884
19885 /* Handle DW_LNS_fixed_advance_pc. */
19886 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19887 {
19888 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19889 m_op_index = 0;
19890 }
19891
19892 /* Handle DW_LNS_copy. */
19893 void handle_copy ()
19894 {
19895 record_line (false);
19896 m_discriminator = 0;
19897 }
19898
19899 /* Handle DW_LNE_end_sequence. */
19900 void handle_end_sequence ()
19901 {
19902 m_currently_recording_lines = true;
19903 }
19904
19905 private:
19906 /* Advance the line by LINE_DELTA. */
19907 void advance_line (int line_delta)
19908 {
19909 m_line += line_delta;
19910
19911 if (line_delta != 0)
19912 m_line_has_non_zero_discriminator = m_discriminator != 0;
19913 }
19914
19915 struct dwarf2_cu *m_cu;
19916
19917 gdbarch *m_gdbarch;
19918
19919 /* True if we're recording lines.
19920 Otherwise we're building partial symtabs and are just interested in
19921 finding include files mentioned by the line number program. */
19922 bool m_record_lines_p;
19923
19924 /* The line number header. */
19925 line_header *m_line_header;
19926
19927 /* These are part of the standard DWARF line number state machine,
19928 and initialized according to the DWARF spec. */
19929
19930 unsigned char m_op_index = 0;
19931 /* The line table index of the current file. */
19932 file_name_index m_file = 1;
19933 unsigned int m_line = 1;
19934
19935 /* These are initialized in the constructor. */
19936
19937 CORE_ADDR m_address;
19938 bool m_is_stmt;
19939 unsigned int m_discriminator;
19940
19941 /* Additional bits of state we need to track. */
19942
19943 /* The last file that we called dwarf2_start_subfile for.
19944 This is only used for TLLs. */
19945 unsigned int m_last_file = 0;
19946 /* The last file a line number was recorded for. */
19947 struct subfile *m_last_subfile = NULL;
19948
19949 /* When true, record the lines we decode. */
19950 bool m_currently_recording_lines = false;
19951
19952 /* The last line number that was recorded, used to coalesce
19953 consecutive entries for the same line. This can happen, for
19954 example, when discriminators are present. PR 17276. */
19955 unsigned int m_last_line = 0;
19956 bool m_line_has_non_zero_discriminator = false;
19957 };
19958
19959 void
19960 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19961 {
19962 CORE_ADDR addr_adj = (((m_op_index + adjust)
19963 / m_line_header->maximum_ops_per_instruction)
19964 * m_line_header->minimum_instruction_length);
19965 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19966 m_op_index = ((m_op_index + adjust)
19967 % m_line_header->maximum_ops_per_instruction);
19968 }
19969
19970 void
19971 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19972 {
19973 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19974 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19975 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19976 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19977 / m_line_header->maximum_ops_per_instruction)
19978 * m_line_header->minimum_instruction_length);
19979 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19980 m_op_index = ((m_op_index + adj_opcode_d)
19981 % m_line_header->maximum_ops_per_instruction);
19982
19983 int line_delta = m_line_header->line_base + adj_opcode_r;
19984 advance_line (line_delta);
19985 record_line (false);
19986 m_discriminator = 0;
19987 }
19988
19989 void
19990 lnp_state_machine::handle_set_file (file_name_index file)
19991 {
19992 m_file = file;
19993
19994 const file_entry *fe = current_file ();
19995 if (fe == NULL)
19996 dwarf2_debug_line_missing_file_complaint ();
19997 else if (m_record_lines_p)
19998 {
19999 const char *dir = fe->include_dir (m_line_header);
20000
20001 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20002 m_line_has_non_zero_discriminator = m_discriminator != 0;
20003 dwarf2_start_subfile (m_cu, fe->name, dir);
20004 }
20005 }
20006
20007 void
20008 lnp_state_machine::handle_const_add_pc ()
20009 {
20010 CORE_ADDR adjust
20011 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20012
20013 CORE_ADDR addr_adj
20014 = (((m_op_index + adjust)
20015 / m_line_header->maximum_ops_per_instruction)
20016 * m_line_header->minimum_instruction_length);
20017
20018 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20019 m_op_index = ((m_op_index + adjust)
20020 % m_line_header->maximum_ops_per_instruction);
20021 }
20022
20023 /* Return non-zero if we should add LINE to the line number table.
20024 LINE is the line to add, LAST_LINE is the last line that was added,
20025 LAST_SUBFILE is the subfile for LAST_LINE.
20026 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20027 had a non-zero discriminator.
20028
20029 We have to be careful in the presence of discriminators.
20030 E.g., for this line:
20031
20032 for (i = 0; i < 100000; i++);
20033
20034 clang can emit four line number entries for that one line,
20035 each with a different discriminator.
20036 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20037
20038 However, we want gdb to coalesce all four entries into one.
20039 Otherwise the user could stepi into the middle of the line and
20040 gdb would get confused about whether the pc really was in the
20041 middle of the line.
20042
20043 Things are further complicated by the fact that two consecutive
20044 line number entries for the same line is a heuristic used by gcc
20045 to denote the end of the prologue. So we can't just discard duplicate
20046 entries, we have to be selective about it. The heuristic we use is
20047 that we only collapse consecutive entries for the same line if at least
20048 one of those entries has a non-zero discriminator. PR 17276.
20049
20050 Note: Addresses in the line number state machine can never go backwards
20051 within one sequence, thus this coalescing is ok. */
20052
20053 static int
20054 dwarf_record_line_p (struct dwarf2_cu *cu,
20055 unsigned int line, unsigned int last_line,
20056 int line_has_non_zero_discriminator,
20057 struct subfile *last_subfile)
20058 {
20059 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20060 return 1;
20061 if (line != last_line)
20062 return 1;
20063 /* Same line for the same file that we've seen already.
20064 As a last check, for pr 17276, only record the line if the line
20065 has never had a non-zero discriminator. */
20066 if (!line_has_non_zero_discriminator)
20067 return 1;
20068 return 0;
20069 }
20070
20071 /* Use the CU's builder to record line number LINE beginning at
20072 address ADDRESS in the line table of subfile SUBFILE. */
20073
20074 static void
20075 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20076 unsigned int line, CORE_ADDR address, bool is_stmt,
20077 struct dwarf2_cu *cu)
20078 {
20079 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20080
20081 if (dwarf_line_debug)
20082 {
20083 fprintf_unfiltered (gdb_stdlog,
20084 "Recording line %u, file %s, address %s\n",
20085 line, lbasename (subfile->name),
20086 paddress (gdbarch, address));
20087 }
20088
20089 if (cu != nullptr)
20090 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20091 }
20092
20093 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20094 Mark the end of a set of line number records.
20095 The arguments are the same as for dwarf_record_line_1.
20096 If SUBFILE is NULL the request is ignored. */
20097
20098 static void
20099 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20100 CORE_ADDR address, struct dwarf2_cu *cu)
20101 {
20102 if (subfile == NULL)
20103 return;
20104
20105 if (dwarf_line_debug)
20106 {
20107 fprintf_unfiltered (gdb_stdlog,
20108 "Finishing current line, file %s, address %s\n",
20109 lbasename (subfile->name),
20110 paddress (gdbarch, address));
20111 }
20112
20113 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20114 }
20115
20116 void
20117 lnp_state_machine::record_line (bool end_sequence)
20118 {
20119 if (dwarf_line_debug)
20120 {
20121 fprintf_unfiltered (gdb_stdlog,
20122 "Processing actual line %u: file %u,"
20123 " address %s, is_stmt %u, discrim %u%s\n",
20124 m_line, m_file,
20125 paddress (m_gdbarch, m_address),
20126 m_is_stmt, m_discriminator,
20127 (end_sequence ? "\t(end sequence)" : ""));
20128 }
20129
20130 file_entry *fe = current_file ();
20131
20132 if (fe == NULL)
20133 dwarf2_debug_line_missing_file_complaint ();
20134 /* For now we ignore lines not starting on an instruction boundary.
20135 But not when processing end_sequence for compatibility with the
20136 previous version of the code. */
20137 else if (m_op_index == 0 || end_sequence)
20138 {
20139 fe->included_p = 1;
20140 if (m_record_lines_p)
20141 {
20142 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20143 || end_sequence)
20144 {
20145 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20146 m_currently_recording_lines ? m_cu : nullptr);
20147 }
20148
20149 if (!end_sequence)
20150 {
20151 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20152
20153 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20154 m_line_has_non_zero_discriminator,
20155 m_last_subfile))
20156 {
20157 buildsym_compunit *builder = m_cu->get_builder ();
20158 dwarf_record_line_1 (m_gdbarch,
20159 builder->get_current_subfile (),
20160 m_line, m_address, is_stmt,
20161 m_currently_recording_lines ? m_cu : nullptr);
20162 }
20163 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20164 m_last_line = m_line;
20165 }
20166 }
20167 }
20168 }
20169
20170 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20171 line_header *lh, bool record_lines_p)
20172 {
20173 m_cu = cu;
20174 m_gdbarch = arch;
20175 m_record_lines_p = record_lines_p;
20176 m_line_header = lh;
20177
20178 m_currently_recording_lines = true;
20179
20180 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20181 was a line entry for it so that the backend has a chance to adjust it
20182 and also record it in case it needs it. This is currently used by MIPS
20183 code, cf. `mips_adjust_dwarf2_line'. */
20184 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20185 m_is_stmt = lh->default_is_stmt;
20186 m_discriminator = 0;
20187 }
20188
20189 void
20190 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20191 const gdb_byte *line_ptr,
20192 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20193 {
20194 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20195 the pc range of the CU. However, we restrict the test to only ADDRESS
20196 values of zero to preserve GDB's previous behaviour which is to handle
20197 the specific case of a function being GC'd by the linker. */
20198
20199 if (address == 0 && address < unrelocated_lowpc)
20200 {
20201 /* This line table is for a function which has been
20202 GCd by the linker. Ignore it. PR gdb/12528 */
20203
20204 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20205 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20206
20207 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20208 line_offset, objfile_name (objfile));
20209 m_currently_recording_lines = false;
20210 /* Note: m_currently_recording_lines is left as false until we see
20211 DW_LNE_end_sequence. */
20212 }
20213 }
20214
20215 /* Subroutine of dwarf_decode_lines to simplify it.
20216 Process the line number information in LH.
20217 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20218 program in order to set included_p for every referenced header. */
20219
20220 static void
20221 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20222 const int decode_for_pst_p, CORE_ADDR lowpc)
20223 {
20224 const gdb_byte *line_ptr, *extended_end;
20225 const gdb_byte *line_end;
20226 unsigned int bytes_read, extended_len;
20227 unsigned char op_code, extended_op;
20228 CORE_ADDR baseaddr;
20229 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20230 bfd *abfd = objfile->obfd;
20231 struct gdbarch *gdbarch = objfile->arch ();
20232 /* True if we're recording line info (as opposed to building partial
20233 symtabs and just interested in finding include files mentioned by
20234 the line number program). */
20235 bool record_lines_p = !decode_for_pst_p;
20236
20237 baseaddr = objfile->text_section_offset ();
20238
20239 line_ptr = lh->statement_program_start;
20240 line_end = lh->statement_program_end;
20241
20242 /* Read the statement sequences until there's nothing left. */
20243 while (line_ptr < line_end)
20244 {
20245 /* The DWARF line number program state machine. Reset the state
20246 machine at the start of each sequence. */
20247 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20248 bool end_sequence = false;
20249
20250 if (record_lines_p)
20251 {
20252 /* Start a subfile for the current file of the state
20253 machine. */
20254 const file_entry *fe = state_machine.current_file ();
20255
20256 if (fe != NULL)
20257 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20258 }
20259
20260 /* Decode the table. */
20261 while (line_ptr < line_end && !end_sequence)
20262 {
20263 op_code = read_1_byte (abfd, line_ptr);
20264 line_ptr += 1;
20265
20266 if (op_code >= lh->opcode_base)
20267 {
20268 /* Special opcode. */
20269 state_machine.handle_special_opcode (op_code);
20270 }
20271 else switch (op_code)
20272 {
20273 case DW_LNS_extended_op:
20274 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20275 &bytes_read);
20276 line_ptr += bytes_read;
20277 extended_end = line_ptr + extended_len;
20278 extended_op = read_1_byte (abfd, line_ptr);
20279 line_ptr += 1;
20280 switch (extended_op)
20281 {
20282 case DW_LNE_end_sequence:
20283 state_machine.handle_end_sequence ();
20284 end_sequence = true;
20285 break;
20286 case DW_LNE_set_address:
20287 {
20288 CORE_ADDR address
20289 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20290 line_ptr += bytes_read;
20291
20292 state_machine.check_line_address (cu, line_ptr,
20293 lowpc - baseaddr, address);
20294 state_machine.handle_set_address (baseaddr, address);
20295 }
20296 break;
20297 case DW_LNE_define_file:
20298 {
20299 const char *cur_file;
20300 unsigned int mod_time, length;
20301 dir_index dindex;
20302
20303 cur_file = read_direct_string (abfd, line_ptr,
20304 &bytes_read);
20305 line_ptr += bytes_read;
20306 dindex = (dir_index)
20307 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20308 line_ptr += bytes_read;
20309 mod_time =
20310 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20311 line_ptr += bytes_read;
20312 length =
20313 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20314 line_ptr += bytes_read;
20315 lh->add_file_name (cur_file, dindex, mod_time, length);
20316 }
20317 break;
20318 case DW_LNE_set_discriminator:
20319 {
20320 /* The discriminator is not interesting to the
20321 debugger; just ignore it. We still need to
20322 check its value though:
20323 if there are consecutive entries for the same
20324 (non-prologue) line we want to coalesce them.
20325 PR 17276. */
20326 unsigned int discr
20327 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20328 line_ptr += bytes_read;
20329
20330 state_machine.handle_set_discriminator (discr);
20331 }
20332 break;
20333 default:
20334 complaint (_("mangled .debug_line section"));
20335 return;
20336 }
20337 /* Make sure that we parsed the extended op correctly. If e.g.
20338 we expected a different address size than the producer used,
20339 we may have read the wrong number of bytes. */
20340 if (line_ptr != extended_end)
20341 {
20342 complaint (_("mangled .debug_line section"));
20343 return;
20344 }
20345 break;
20346 case DW_LNS_copy:
20347 state_machine.handle_copy ();
20348 break;
20349 case DW_LNS_advance_pc:
20350 {
20351 CORE_ADDR adjust
20352 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20353 line_ptr += bytes_read;
20354
20355 state_machine.handle_advance_pc (adjust);
20356 }
20357 break;
20358 case DW_LNS_advance_line:
20359 {
20360 int line_delta
20361 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20362 line_ptr += bytes_read;
20363
20364 state_machine.handle_advance_line (line_delta);
20365 }
20366 break;
20367 case DW_LNS_set_file:
20368 {
20369 file_name_index file
20370 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20371 &bytes_read);
20372 line_ptr += bytes_read;
20373
20374 state_machine.handle_set_file (file);
20375 }
20376 break;
20377 case DW_LNS_set_column:
20378 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20379 line_ptr += bytes_read;
20380 break;
20381 case DW_LNS_negate_stmt:
20382 state_machine.handle_negate_stmt ();
20383 break;
20384 case DW_LNS_set_basic_block:
20385 break;
20386 /* Add to the address register of the state machine the
20387 address increment value corresponding to special opcode
20388 255. I.e., this value is scaled by the minimum
20389 instruction length since special opcode 255 would have
20390 scaled the increment. */
20391 case DW_LNS_const_add_pc:
20392 state_machine.handle_const_add_pc ();
20393 break;
20394 case DW_LNS_fixed_advance_pc:
20395 {
20396 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20397 line_ptr += 2;
20398
20399 state_machine.handle_fixed_advance_pc (addr_adj);
20400 }
20401 break;
20402 default:
20403 {
20404 /* Unknown standard opcode, ignore it. */
20405 int i;
20406
20407 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20408 {
20409 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20410 line_ptr += bytes_read;
20411 }
20412 }
20413 }
20414 }
20415
20416 if (!end_sequence)
20417 dwarf2_debug_line_missing_end_sequence_complaint ();
20418
20419 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20420 in which case we still finish recording the last line). */
20421 state_machine.record_line (true);
20422 }
20423 }
20424
20425 /* Decode the Line Number Program (LNP) for the given line_header
20426 structure and CU. The actual information extracted and the type
20427 of structures created from the LNP depends on the value of PST.
20428
20429 1. If PST is NULL, then this procedure uses the data from the program
20430 to create all necessary symbol tables, and their linetables.
20431
20432 2. If PST is not NULL, this procedure reads the program to determine
20433 the list of files included by the unit represented by PST, and
20434 builds all the associated partial symbol tables.
20435
20436 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20437 It is used for relative paths in the line table.
20438 NOTE: When processing partial symtabs (pst != NULL),
20439 comp_dir == pst->dirname.
20440
20441 NOTE: It is important that psymtabs have the same file name (via strcmp)
20442 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20443 symtab we don't use it in the name of the psymtabs we create.
20444 E.g. expand_line_sal requires this when finding psymtabs to expand.
20445 A good testcase for this is mb-inline.exp.
20446
20447 LOWPC is the lowest address in CU (or 0 if not known).
20448
20449 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20450 for its PC<->lines mapping information. Otherwise only the filename
20451 table is read in. */
20452
20453 static void
20454 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20455 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20456 CORE_ADDR lowpc, int decode_mapping)
20457 {
20458 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20459 const int decode_for_pst_p = (pst != NULL);
20460
20461 if (decode_mapping)
20462 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20463
20464 if (decode_for_pst_p)
20465 {
20466 /* Now that we're done scanning the Line Header Program, we can
20467 create the psymtab of each included file. */
20468 for (auto &file_entry : lh->file_names ())
20469 if (file_entry.included_p == 1)
20470 {
20471 gdb::unique_xmalloc_ptr<char> name_holder;
20472 const char *include_name =
20473 psymtab_include_file_name (lh, file_entry, pst,
20474 comp_dir, &name_holder);
20475 if (include_name != NULL)
20476 dwarf2_create_include_psymtab (include_name, pst, objfile);
20477 }
20478 }
20479 else
20480 {
20481 /* Make sure a symtab is created for every file, even files
20482 which contain only variables (i.e. no code with associated
20483 line numbers). */
20484 buildsym_compunit *builder = cu->get_builder ();
20485 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20486
20487 for (auto &fe : lh->file_names ())
20488 {
20489 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20490 if (builder->get_current_subfile ()->symtab == NULL)
20491 {
20492 builder->get_current_subfile ()->symtab
20493 = allocate_symtab (cust,
20494 builder->get_current_subfile ()->name);
20495 }
20496 fe.symtab = builder->get_current_subfile ()->symtab;
20497 }
20498 }
20499 }
20500
20501 /* Start a subfile for DWARF. FILENAME is the name of the file and
20502 DIRNAME the name of the source directory which contains FILENAME
20503 or NULL if not known.
20504 This routine tries to keep line numbers from identical absolute and
20505 relative file names in a common subfile.
20506
20507 Using the `list' example from the GDB testsuite, which resides in
20508 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20509 of /srcdir/list0.c yields the following debugging information for list0.c:
20510
20511 DW_AT_name: /srcdir/list0.c
20512 DW_AT_comp_dir: /compdir
20513 files.files[0].name: list0.h
20514 files.files[0].dir: /srcdir
20515 files.files[1].name: list0.c
20516 files.files[1].dir: /srcdir
20517
20518 The line number information for list0.c has to end up in a single
20519 subfile, so that `break /srcdir/list0.c:1' works as expected.
20520 start_subfile will ensure that this happens provided that we pass the
20521 concatenation of files.files[1].dir and files.files[1].name as the
20522 subfile's name. */
20523
20524 static void
20525 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20526 const char *dirname)
20527 {
20528 gdb::unique_xmalloc_ptr<char> copy;
20529
20530 /* In order not to lose the line information directory,
20531 we concatenate it to the filename when it makes sense.
20532 Note that the Dwarf3 standard says (speaking of filenames in line
20533 information): ``The directory index is ignored for file names
20534 that represent full path names''. Thus ignoring dirname in the
20535 `else' branch below isn't an issue. */
20536
20537 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20538 {
20539 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20540 filename = copy.get ();
20541 }
20542
20543 cu->get_builder ()->start_subfile (filename);
20544 }
20545
20546 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20547 buildsym_compunit constructor. */
20548
20549 struct compunit_symtab *
20550 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20551 CORE_ADDR low_pc)
20552 {
20553 gdb_assert (m_builder == nullptr);
20554
20555 m_builder.reset (new struct buildsym_compunit
20556 (per_cu->dwarf2_per_objfile->objfile,
20557 name, comp_dir, language, low_pc));
20558
20559 list_in_scope = get_builder ()->get_file_symbols ();
20560
20561 get_builder ()->record_debugformat ("DWARF 2");
20562 get_builder ()->record_producer (producer);
20563
20564 processing_has_namespace_info = false;
20565
20566 return get_builder ()->get_compunit_symtab ();
20567 }
20568
20569 static void
20570 var_decode_location (struct attribute *attr, struct symbol *sym,
20571 struct dwarf2_cu *cu)
20572 {
20573 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20574 struct comp_unit_head *cu_header = &cu->header;
20575
20576 /* NOTE drow/2003-01-30: There used to be a comment and some special
20577 code here to turn a symbol with DW_AT_external and a
20578 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20579 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20580 with some versions of binutils) where shared libraries could have
20581 relocations against symbols in their debug information - the
20582 minimal symbol would have the right address, but the debug info
20583 would not. It's no longer necessary, because we will explicitly
20584 apply relocations when we read in the debug information now. */
20585
20586 /* A DW_AT_location attribute with no contents indicates that a
20587 variable has been optimized away. */
20588 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20589 {
20590 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20591 return;
20592 }
20593
20594 /* Handle one degenerate form of location expression specially, to
20595 preserve GDB's previous behavior when section offsets are
20596 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20597 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20598
20599 if (attr->form_is_block ()
20600 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20601 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20602 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20603 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20604 && (DW_BLOCK (attr)->size
20605 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20606 {
20607 unsigned int dummy;
20608
20609 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20610 SET_SYMBOL_VALUE_ADDRESS
20611 (sym, cu->header.read_address (objfile->obfd,
20612 DW_BLOCK (attr)->data + 1,
20613 &dummy));
20614 else
20615 SET_SYMBOL_VALUE_ADDRESS
20616 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20617 &dummy));
20618 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20619 fixup_symbol_section (sym, objfile);
20620 SET_SYMBOL_VALUE_ADDRESS
20621 (sym,
20622 SYMBOL_VALUE_ADDRESS (sym)
20623 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20624 return;
20625 }
20626
20627 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20628 expression evaluator, and use LOC_COMPUTED only when necessary
20629 (i.e. when the value of a register or memory location is
20630 referenced, or a thread-local block, etc.). Then again, it might
20631 not be worthwhile. I'm assuming that it isn't unless performance
20632 or memory numbers show me otherwise. */
20633
20634 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20635
20636 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20637 cu->has_loclist = true;
20638 }
20639
20640 /* Given a pointer to a DWARF information entry, figure out if we need
20641 to make a symbol table entry for it, and if so, create a new entry
20642 and return a pointer to it.
20643 If TYPE is NULL, determine symbol type from the die, otherwise
20644 used the passed type.
20645 If SPACE is not NULL, use it to hold the new symbol. If it is
20646 NULL, allocate a new symbol on the objfile's obstack. */
20647
20648 static struct symbol *
20649 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20650 struct symbol *space)
20651 {
20652 struct dwarf2_per_objfile *dwarf2_per_objfile
20653 = cu->per_cu->dwarf2_per_objfile;
20654 struct objfile *objfile = dwarf2_per_objfile->objfile;
20655 struct gdbarch *gdbarch = objfile->arch ();
20656 struct symbol *sym = NULL;
20657 const char *name;
20658 struct attribute *attr = NULL;
20659 struct attribute *attr2 = NULL;
20660 CORE_ADDR baseaddr;
20661 struct pending **list_to_add = NULL;
20662
20663 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20664
20665 baseaddr = objfile->text_section_offset ();
20666
20667 name = dwarf2_name (die, cu);
20668 if (name)
20669 {
20670 int suppress_add = 0;
20671
20672 if (space)
20673 sym = space;
20674 else
20675 sym = new (&objfile->objfile_obstack) symbol;
20676 OBJSTAT (objfile, n_syms++);
20677
20678 /* Cache this symbol's name and the name's demangled form (if any). */
20679 sym->set_language (cu->language, &objfile->objfile_obstack);
20680 /* Fortran does not have mangling standard and the mangling does differ
20681 between gfortran, iFort etc. */
20682 const char *physname
20683 = (cu->language == language_fortran
20684 ? dwarf2_full_name (name, die, cu)
20685 : dwarf2_physname (name, die, cu));
20686 const char *linkagename = dw2_linkage_name (die, cu);
20687
20688 if (linkagename == nullptr || cu->language == language_ada)
20689 sym->set_linkage_name (physname);
20690 else
20691 {
20692 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20693 sym->set_linkage_name (linkagename);
20694 }
20695
20696 /* Default assumptions.
20697 Use the passed type or decode it from the die. */
20698 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20699 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20700 if (type != NULL)
20701 SYMBOL_TYPE (sym) = type;
20702 else
20703 SYMBOL_TYPE (sym) = die_type (die, cu);
20704 attr = dwarf2_attr (die,
20705 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20706 cu);
20707 if (attr != nullptr)
20708 {
20709 SYMBOL_LINE (sym) = DW_UNSND (attr);
20710 }
20711
20712 attr = dwarf2_attr (die,
20713 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20714 cu);
20715 if (attr != nullptr)
20716 {
20717 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20718 struct file_entry *fe;
20719
20720 if (cu->line_header != NULL)
20721 fe = cu->line_header->file_name_at (file_index);
20722 else
20723 fe = NULL;
20724
20725 if (fe == NULL)
20726 complaint (_("file index out of range"));
20727 else
20728 symbol_set_symtab (sym, fe->symtab);
20729 }
20730
20731 switch (die->tag)
20732 {
20733 case DW_TAG_label:
20734 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20735 if (attr != nullptr)
20736 {
20737 CORE_ADDR addr;
20738
20739 addr = attr->value_as_address ();
20740 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20741 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20742 }
20743 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20744 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20745 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20746 add_symbol_to_list (sym, cu->list_in_scope);
20747 break;
20748 case DW_TAG_subprogram:
20749 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20750 finish_block. */
20751 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20752 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20753 if ((attr2 && (DW_UNSND (attr2) != 0))
20754 || cu->language == language_ada
20755 || cu->language == language_fortran)
20756 {
20757 /* Subprograms marked external are stored as a global symbol.
20758 Ada and Fortran subprograms, whether marked external or
20759 not, are always stored as a global symbol, because we want
20760 to be able to access them globally. For instance, we want
20761 to be able to break on a nested subprogram without having
20762 to specify the context. */
20763 list_to_add = cu->get_builder ()->get_global_symbols ();
20764 }
20765 else
20766 {
20767 list_to_add = cu->list_in_scope;
20768 }
20769 break;
20770 case DW_TAG_inlined_subroutine:
20771 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20772 finish_block. */
20773 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20774 SYMBOL_INLINED (sym) = 1;
20775 list_to_add = cu->list_in_scope;
20776 break;
20777 case DW_TAG_template_value_param:
20778 suppress_add = 1;
20779 /* Fall through. */
20780 case DW_TAG_constant:
20781 case DW_TAG_variable:
20782 case DW_TAG_member:
20783 /* Compilation with minimal debug info may result in
20784 variables with missing type entries. Change the
20785 misleading `void' type to something sensible. */
20786 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20787 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20788
20789 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20790 /* In the case of DW_TAG_member, we should only be called for
20791 static const members. */
20792 if (die->tag == DW_TAG_member)
20793 {
20794 /* dwarf2_add_field uses die_is_declaration,
20795 so we do the same. */
20796 gdb_assert (die_is_declaration (die, cu));
20797 gdb_assert (attr);
20798 }
20799 if (attr != nullptr)
20800 {
20801 dwarf2_const_value (attr, sym, cu);
20802 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20803 if (!suppress_add)
20804 {
20805 if (attr2 && (DW_UNSND (attr2) != 0))
20806 list_to_add = cu->get_builder ()->get_global_symbols ();
20807 else
20808 list_to_add = cu->list_in_scope;
20809 }
20810 break;
20811 }
20812 attr = dwarf2_attr (die, DW_AT_location, cu);
20813 if (attr != nullptr)
20814 {
20815 var_decode_location (attr, sym, cu);
20816 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20817
20818 /* Fortran explicitly imports any global symbols to the local
20819 scope by DW_TAG_common_block. */
20820 if (cu->language == language_fortran && die->parent
20821 && die->parent->tag == DW_TAG_common_block)
20822 attr2 = NULL;
20823
20824 if (SYMBOL_CLASS (sym) == LOC_STATIC
20825 && SYMBOL_VALUE_ADDRESS (sym) == 0
20826 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20827 {
20828 /* When a static variable is eliminated by the linker,
20829 the corresponding debug information is not stripped
20830 out, but the variable address is set to null;
20831 do not add such variables into symbol table. */
20832 }
20833 else if (attr2 && (DW_UNSND (attr2) != 0))
20834 {
20835 if (SYMBOL_CLASS (sym) == LOC_STATIC
20836 && (objfile->flags & OBJF_MAINLINE) == 0
20837 && dwarf2_per_objfile->per_bfd->can_copy)
20838 {
20839 /* A global static variable might be subject to
20840 copy relocation. We first check for a local
20841 minsym, though, because maybe the symbol was
20842 marked hidden, in which case this would not
20843 apply. */
20844 bound_minimal_symbol found
20845 = (lookup_minimal_symbol_linkage
20846 (sym->linkage_name (), objfile));
20847 if (found.minsym != nullptr)
20848 sym->maybe_copied = 1;
20849 }
20850
20851 /* A variable with DW_AT_external is never static,
20852 but it may be block-scoped. */
20853 list_to_add
20854 = ((cu->list_in_scope
20855 == cu->get_builder ()->get_file_symbols ())
20856 ? cu->get_builder ()->get_global_symbols ()
20857 : cu->list_in_scope);
20858 }
20859 else
20860 list_to_add = cu->list_in_scope;
20861 }
20862 else
20863 {
20864 /* We do not know the address of this symbol.
20865 If it is an external symbol and we have type information
20866 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20867 The address of the variable will then be determined from
20868 the minimal symbol table whenever the variable is
20869 referenced. */
20870 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20871
20872 /* Fortran explicitly imports any global symbols to the local
20873 scope by DW_TAG_common_block. */
20874 if (cu->language == language_fortran && die->parent
20875 && die->parent->tag == DW_TAG_common_block)
20876 {
20877 /* SYMBOL_CLASS doesn't matter here because
20878 read_common_block is going to reset it. */
20879 if (!suppress_add)
20880 list_to_add = cu->list_in_scope;
20881 }
20882 else if (attr2 && (DW_UNSND (attr2) != 0)
20883 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20884 {
20885 /* A variable with DW_AT_external is never static, but it
20886 may be block-scoped. */
20887 list_to_add
20888 = ((cu->list_in_scope
20889 == cu->get_builder ()->get_file_symbols ())
20890 ? cu->get_builder ()->get_global_symbols ()
20891 : cu->list_in_scope);
20892
20893 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20894 }
20895 else if (!die_is_declaration (die, cu))
20896 {
20897 /* Use the default LOC_OPTIMIZED_OUT class. */
20898 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20899 if (!suppress_add)
20900 list_to_add = cu->list_in_scope;
20901 }
20902 }
20903 break;
20904 case DW_TAG_formal_parameter:
20905 {
20906 /* If we are inside a function, mark this as an argument. If
20907 not, we might be looking at an argument to an inlined function
20908 when we do not have enough information to show inlined frames;
20909 pretend it's a local variable in that case so that the user can
20910 still see it. */
20911 struct context_stack *curr
20912 = cu->get_builder ()->get_current_context_stack ();
20913 if (curr != nullptr && curr->name != nullptr)
20914 SYMBOL_IS_ARGUMENT (sym) = 1;
20915 attr = dwarf2_attr (die, DW_AT_location, cu);
20916 if (attr != nullptr)
20917 {
20918 var_decode_location (attr, sym, cu);
20919 }
20920 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20921 if (attr != nullptr)
20922 {
20923 dwarf2_const_value (attr, sym, cu);
20924 }
20925
20926 list_to_add = cu->list_in_scope;
20927 }
20928 break;
20929 case DW_TAG_unspecified_parameters:
20930 /* From varargs functions; gdb doesn't seem to have any
20931 interest in this information, so just ignore it for now.
20932 (FIXME?) */
20933 break;
20934 case DW_TAG_template_type_param:
20935 suppress_add = 1;
20936 /* Fall through. */
20937 case DW_TAG_class_type:
20938 case DW_TAG_interface_type:
20939 case DW_TAG_structure_type:
20940 case DW_TAG_union_type:
20941 case DW_TAG_set_type:
20942 case DW_TAG_enumeration_type:
20943 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20944 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20945
20946 {
20947 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20948 really ever be static objects: otherwise, if you try
20949 to, say, break of a class's method and you're in a file
20950 which doesn't mention that class, it won't work unless
20951 the check for all static symbols in lookup_symbol_aux
20952 saves you. See the OtherFileClass tests in
20953 gdb.c++/namespace.exp. */
20954
20955 if (!suppress_add)
20956 {
20957 buildsym_compunit *builder = cu->get_builder ();
20958 list_to_add
20959 = (cu->list_in_scope == builder->get_file_symbols ()
20960 && cu->language == language_cplus
20961 ? builder->get_global_symbols ()
20962 : cu->list_in_scope);
20963
20964 /* The semantics of C++ state that "struct foo {
20965 ... }" also defines a typedef for "foo". */
20966 if (cu->language == language_cplus
20967 || cu->language == language_ada
20968 || cu->language == language_d
20969 || cu->language == language_rust)
20970 {
20971 /* The symbol's name is already allocated along
20972 with this objfile, so we don't need to
20973 duplicate it for the type. */
20974 if (SYMBOL_TYPE (sym)->name () == 0)
20975 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
20976 }
20977 }
20978 }
20979 break;
20980 case DW_TAG_typedef:
20981 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20982 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20983 list_to_add = cu->list_in_scope;
20984 break;
20985 case DW_TAG_base_type:
20986 case DW_TAG_subrange_type:
20987 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20988 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20989 list_to_add = cu->list_in_scope;
20990 break;
20991 case DW_TAG_enumerator:
20992 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20993 if (attr != nullptr)
20994 {
20995 dwarf2_const_value (attr, sym, cu);
20996 }
20997 {
20998 /* NOTE: carlton/2003-11-10: See comment above in the
20999 DW_TAG_class_type, etc. block. */
21000
21001 list_to_add
21002 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21003 && cu->language == language_cplus
21004 ? cu->get_builder ()->get_global_symbols ()
21005 : cu->list_in_scope);
21006 }
21007 break;
21008 case DW_TAG_imported_declaration:
21009 case DW_TAG_namespace:
21010 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21011 list_to_add = cu->get_builder ()->get_global_symbols ();
21012 break;
21013 case DW_TAG_module:
21014 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21015 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21016 list_to_add = cu->get_builder ()->get_global_symbols ();
21017 break;
21018 case DW_TAG_common_block:
21019 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21020 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21021 add_symbol_to_list (sym, cu->list_in_scope);
21022 break;
21023 default:
21024 /* Not a tag we recognize. Hopefully we aren't processing
21025 trash data, but since we must specifically ignore things
21026 we don't recognize, there is nothing else we should do at
21027 this point. */
21028 complaint (_("unsupported tag: '%s'"),
21029 dwarf_tag_name (die->tag));
21030 break;
21031 }
21032
21033 if (suppress_add)
21034 {
21035 sym->hash_next = objfile->template_symbols;
21036 objfile->template_symbols = sym;
21037 list_to_add = NULL;
21038 }
21039
21040 if (list_to_add != NULL)
21041 add_symbol_to_list (sym, list_to_add);
21042
21043 /* For the benefit of old versions of GCC, check for anonymous
21044 namespaces based on the demangled name. */
21045 if (!cu->processing_has_namespace_info
21046 && cu->language == language_cplus)
21047 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21048 }
21049 return (sym);
21050 }
21051
21052 /* Given an attr with a DW_FORM_dataN value in host byte order,
21053 zero-extend it as appropriate for the symbol's type. The DWARF
21054 standard (v4) is not entirely clear about the meaning of using
21055 DW_FORM_dataN for a constant with a signed type, where the type is
21056 wider than the data. The conclusion of a discussion on the DWARF
21057 list was that this is unspecified. We choose to always zero-extend
21058 because that is the interpretation long in use by GCC. */
21059
21060 static gdb_byte *
21061 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21062 struct dwarf2_cu *cu, LONGEST *value, int bits)
21063 {
21064 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21065 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21066 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21067 LONGEST l = DW_UNSND (attr);
21068
21069 if (bits < sizeof (*value) * 8)
21070 {
21071 l &= ((LONGEST) 1 << bits) - 1;
21072 *value = l;
21073 }
21074 else if (bits == sizeof (*value) * 8)
21075 *value = l;
21076 else
21077 {
21078 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21079 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21080 return bytes;
21081 }
21082
21083 return NULL;
21084 }
21085
21086 /* Read a constant value from an attribute. Either set *VALUE, or if
21087 the value does not fit in *VALUE, set *BYTES - either already
21088 allocated on the objfile obstack, or newly allocated on OBSTACK,
21089 or, set *BATON, if we translated the constant to a location
21090 expression. */
21091
21092 static void
21093 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21094 const char *name, struct obstack *obstack,
21095 struct dwarf2_cu *cu,
21096 LONGEST *value, const gdb_byte **bytes,
21097 struct dwarf2_locexpr_baton **baton)
21098 {
21099 dwarf2_per_objfile *per_objfile = cu->per_cu->dwarf2_per_objfile;
21100 struct objfile *objfile = per_objfile->objfile;
21101 struct comp_unit_head *cu_header = &cu->header;
21102 struct dwarf_block *blk;
21103 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21104 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21105
21106 *value = 0;
21107 *bytes = NULL;
21108 *baton = NULL;
21109
21110 switch (attr->form)
21111 {
21112 case DW_FORM_addr:
21113 case DW_FORM_addrx:
21114 case DW_FORM_GNU_addr_index:
21115 {
21116 gdb_byte *data;
21117
21118 if (TYPE_LENGTH (type) != cu_header->addr_size)
21119 dwarf2_const_value_length_mismatch_complaint (name,
21120 cu_header->addr_size,
21121 TYPE_LENGTH (type));
21122 /* Symbols of this form are reasonably rare, so we just
21123 piggyback on the existing location code rather than writing
21124 a new implementation of symbol_computed_ops. */
21125 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21126 (*baton)->per_objfile = per_objfile;
21127 (*baton)->per_cu = cu->per_cu;
21128 gdb_assert ((*baton)->per_cu);
21129
21130 (*baton)->size = 2 + cu_header->addr_size;
21131 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21132 (*baton)->data = data;
21133
21134 data[0] = DW_OP_addr;
21135 store_unsigned_integer (&data[1], cu_header->addr_size,
21136 byte_order, DW_ADDR (attr));
21137 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21138 }
21139 break;
21140 case DW_FORM_string:
21141 case DW_FORM_strp:
21142 case DW_FORM_strx:
21143 case DW_FORM_GNU_str_index:
21144 case DW_FORM_GNU_strp_alt:
21145 /* DW_STRING is already allocated on the objfile obstack, point
21146 directly to it. */
21147 *bytes = (const gdb_byte *) DW_STRING (attr);
21148 break;
21149 case DW_FORM_block1:
21150 case DW_FORM_block2:
21151 case DW_FORM_block4:
21152 case DW_FORM_block:
21153 case DW_FORM_exprloc:
21154 case DW_FORM_data16:
21155 blk = DW_BLOCK (attr);
21156 if (TYPE_LENGTH (type) != blk->size)
21157 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21158 TYPE_LENGTH (type));
21159 *bytes = blk->data;
21160 break;
21161
21162 /* The DW_AT_const_value attributes are supposed to carry the
21163 symbol's value "represented as it would be on the target
21164 architecture." By the time we get here, it's already been
21165 converted to host endianness, so we just need to sign- or
21166 zero-extend it as appropriate. */
21167 case DW_FORM_data1:
21168 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21169 break;
21170 case DW_FORM_data2:
21171 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21172 break;
21173 case DW_FORM_data4:
21174 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21175 break;
21176 case DW_FORM_data8:
21177 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21178 break;
21179
21180 case DW_FORM_sdata:
21181 case DW_FORM_implicit_const:
21182 *value = DW_SND (attr);
21183 break;
21184
21185 case DW_FORM_udata:
21186 *value = DW_UNSND (attr);
21187 break;
21188
21189 default:
21190 complaint (_("unsupported const value attribute form: '%s'"),
21191 dwarf_form_name (attr->form));
21192 *value = 0;
21193 break;
21194 }
21195 }
21196
21197
21198 /* Copy constant value from an attribute to a symbol. */
21199
21200 static void
21201 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21202 struct dwarf2_cu *cu)
21203 {
21204 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21205 LONGEST value;
21206 const gdb_byte *bytes;
21207 struct dwarf2_locexpr_baton *baton;
21208
21209 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21210 sym->print_name (),
21211 &objfile->objfile_obstack, cu,
21212 &value, &bytes, &baton);
21213
21214 if (baton != NULL)
21215 {
21216 SYMBOL_LOCATION_BATON (sym) = baton;
21217 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21218 }
21219 else if (bytes != NULL)
21220 {
21221 SYMBOL_VALUE_BYTES (sym) = bytes;
21222 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21223 }
21224 else
21225 {
21226 SYMBOL_VALUE (sym) = value;
21227 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21228 }
21229 }
21230
21231 /* Return the type of the die in question using its DW_AT_type attribute. */
21232
21233 static struct type *
21234 die_type (struct die_info *die, struct dwarf2_cu *cu)
21235 {
21236 struct attribute *type_attr;
21237
21238 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21239 if (!type_attr)
21240 {
21241 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21242 /* A missing DW_AT_type represents a void type. */
21243 return objfile_type (objfile)->builtin_void;
21244 }
21245
21246 return lookup_die_type (die, type_attr, cu);
21247 }
21248
21249 /* True iff CU's producer generates GNAT Ada auxiliary information
21250 that allows to find parallel types through that information instead
21251 of having to do expensive parallel lookups by type name. */
21252
21253 static int
21254 need_gnat_info (struct dwarf2_cu *cu)
21255 {
21256 /* Assume that the Ada compiler was GNAT, which always produces
21257 the auxiliary information. */
21258 return (cu->language == language_ada);
21259 }
21260
21261 /* Return the auxiliary type of the die in question using its
21262 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21263 attribute is not present. */
21264
21265 static struct type *
21266 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21267 {
21268 struct attribute *type_attr;
21269
21270 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21271 if (!type_attr)
21272 return NULL;
21273
21274 return lookup_die_type (die, type_attr, cu);
21275 }
21276
21277 /* If DIE has a descriptive_type attribute, then set the TYPE's
21278 descriptive type accordingly. */
21279
21280 static void
21281 set_descriptive_type (struct type *type, struct die_info *die,
21282 struct dwarf2_cu *cu)
21283 {
21284 struct type *descriptive_type = die_descriptive_type (die, cu);
21285
21286 if (descriptive_type)
21287 {
21288 ALLOCATE_GNAT_AUX_TYPE (type);
21289 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21290 }
21291 }
21292
21293 /* Return the containing type of the die in question using its
21294 DW_AT_containing_type attribute. */
21295
21296 static struct type *
21297 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21298 {
21299 struct attribute *type_attr;
21300 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21301
21302 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21303 if (!type_attr)
21304 error (_("Dwarf Error: Problem turning containing type into gdb type "
21305 "[in module %s]"), objfile_name (objfile));
21306
21307 return lookup_die_type (die, type_attr, cu);
21308 }
21309
21310 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21311
21312 static struct type *
21313 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21314 {
21315 struct dwarf2_per_objfile *dwarf2_per_objfile
21316 = cu->per_cu->dwarf2_per_objfile;
21317 struct objfile *objfile = dwarf2_per_objfile->objfile;
21318 char *saved;
21319
21320 std::string message
21321 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21322 objfile_name (objfile),
21323 sect_offset_str (cu->header.sect_off),
21324 sect_offset_str (die->sect_off));
21325 saved = obstack_strdup (&objfile->objfile_obstack, message);
21326
21327 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21328 }
21329
21330 /* Look up the type of DIE in CU using its type attribute ATTR.
21331 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21332 DW_AT_containing_type.
21333 If there is no type substitute an error marker. */
21334
21335 static struct type *
21336 lookup_die_type (struct die_info *die, const struct attribute *attr,
21337 struct dwarf2_cu *cu)
21338 {
21339 struct dwarf2_per_objfile *dwarf2_per_objfile
21340 = cu->per_cu->dwarf2_per_objfile;
21341 struct objfile *objfile = dwarf2_per_objfile->objfile;
21342 struct type *this_type;
21343
21344 gdb_assert (attr->name == DW_AT_type
21345 || attr->name == DW_AT_GNAT_descriptive_type
21346 || attr->name == DW_AT_containing_type);
21347
21348 /* First see if we have it cached. */
21349
21350 if (attr->form == DW_FORM_GNU_ref_alt)
21351 {
21352 struct dwarf2_per_cu_data *per_cu;
21353 sect_offset sect_off = attr->get_ref_die_offset ();
21354
21355 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21356 dwarf2_per_objfile);
21357 this_type = get_die_type_at_offset (sect_off, per_cu);
21358 }
21359 else if (attr->form_is_ref ())
21360 {
21361 sect_offset sect_off = attr->get_ref_die_offset ();
21362
21363 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21364 }
21365 else if (attr->form == DW_FORM_ref_sig8)
21366 {
21367 ULONGEST signature = DW_SIGNATURE (attr);
21368
21369 return get_signatured_type (die, signature, cu);
21370 }
21371 else
21372 {
21373 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21374 " at %s [in module %s]"),
21375 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21376 objfile_name (objfile));
21377 return build_error_marker_type (cu, die);
21378 }
21379
21380 /* If not cached we need to read it in. */
21381
21382 if (this_type == NULL)
21383 {
21384 struct die_info *type_die = NULL;
21385 struct dwarf2_cu *type_cu = cu;
21386
21387 if (attr->form_is_ref ())
21388 type_die = follow_die_ref (die, attr, &type_cu);
21389 if (type_die == NULL)
21390 return build_error_marker_type (cu, die);
21391 /* If we find the type now, it's probably because the type came
21392 from an inter-CU reference and the type's CU got expanded before
21393 ours. */
21394 this_type = read_type_die (type_die, type_cu);
21395 }
21396
21397 /* If we still don't have a type use an error marker. */
21398
21399 if (this_type == NULL)
21400 return build_error_marker_type (cu, die);
21401
21402 return this_type;
21403 }
21404
21405 /* Return the type in DIE, CU.
21406 Returns NULL for invalid types.
21407
21408 This first does a lookup in die_type_hash,
21409 and only reads the die in if necessary.
21410
21411 NOTE: This can be called when reading in partial or full symbols. */
21412
21413 static struct type *
21414 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21415 {
21416 struct type *this_type;
21417
21418 this_type = get_die_type (die, cu);
21419 if (this_type)
21420 return this_type;
21421
21422 return read_type_die_1 (die, cu);
21423 }
21424
21425 /* Read the type in DIE, CU.
21426 Returns NULL for invalid types. */
21427
21428 static struct type *
21429 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21430 {
21431 struct type *this_type = NULL;
21432
21433 switch (die->tag)
21434 {
21435 case DW_TAG_class_type:
21436 case DW_TAG_interface_type:
21437 case DW_TAG_structure_type:
21438 case DW_TAG_union_type:
21439 this_type = read_structure_type (die, cu);
21440 break;
21441 case DW_TAG_enumeration_type:
21442 this_type = read_enumeration_type (die, cu);
21443 break;
21444 case DW_TAG_subprogram:
21445 case DW_TAG_subroutine_type:
21446 case DW_TAG_inlined_subroutine:
21447 this_type = read_subroutine_type (die, cu);
21448 break;
21449 case DW_TAG_array_type:
21450 this_type = read_array_type (die, cu);
21451 break;
21452 case DW_TAG_set_type:
21453 this_type = read_set_type (die, cu);
21454 break;
21455 case DW_TAG_pointer_type:
21456 this_type = read_tag_pointer_type (die, cu);
21457 break;
21458 case DW_TAG_ptr_to_member_type:
21459 this_type = read_tag_ptr_to_member_type (die, cu);
21460 break;
21461 case DW_TAG_reference_type:
21462 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21463 break;
21464 case DW_TAG_rvalue_reference_type:
21465 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21466 break;
21467 case DW_TAG_const_type:
21468 this_type = read_tag_const_type (die, cu);
21469 break;
21470 case DW_TAG_volatile_type:
21471 this_type = read_tag_volatile_type (die, cu);
21472 break;
21473 case DW_TAG_restrict_type:
21474 this_type = read_tag_restrict_type (die, cu);
21475 break;
21476 case DW_TAG_string_type:
21477 this_type = read_tag_string_type (die, cu);
21478 break;
21479 case DW_TAG_typedef:
21480 this_type = read_typedef (die, cu);
21481 break;
21482 case DW_TAG_subrange_type:
21483 this_type = read_subrange_type (die, cu);
21484 break;
21485 case DW_TAG_base_type:
21486 this_type = read_base_type (die, cu);
21487 break;
21488 case DW_TAG_unspecified_type:
21489 this_type = read_unspecified_type (die, cu);
21490 break;
21491 case DW_TAG_namespace:
21492 this_type = read_namespace_type (die, cu);
21493 break;
21494 case DW_TAG_module:
21495 this_type = read_module_type (die, cu);
21496 break;
21497 case DW_TAG_atomic_type:
21498 this_type = read_tag_atomic_type (die, cu);
21499 break;
21500 default:
21501 complaint (_("unexpected tag in read_type_die: '%s'"),
21502 dwarf_tag_name (die->tag));
21503 break;
21504 }
21505
21506 return this_type;
21507 }
21508
21509 /* See if we can figure out if the class lives in a namespace. We do
21510 this by looking for a member function; its demangled name will
21511 contain namespace info, if there is any.
21512 Return the computed name or NULL.
21513 Space for the result is allocated on the objfile's obstack.
21514 This is the full-die version of guess_partial_die_structure_name.
21515 In this case we know DIE has no useful parent. */
21516
21517 static const char *
21518 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21519 {
21520 struct die_info *spec_die;
21521 struct dwarf2_cu *spec_cu;
21522 struct die_info *child;
21523 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21524
21525 spec_cu = cu;
21526 spec_die = die_specification (die, &spec_cu);
21527 if (spec_die != NULL)
21528 {
21529 die = spec_die;
21530 cu = spec_cu;
21531 }
21532
21533 for (child = die->child;
21534 child != NULL;
21535 child = child->sibling)
21536 {
21537 if (child->tag == DW_TAG_subprogram)
21538 {
21539 const char *linkage_name = dw2_linkage_name (child, cu);
21540
21541 if (linkage_name != NULL)
21542 {
21543 gdb::unique_xmalloc_ptr<char> actual_name
21544 (language_class_name_from_physname (cu->language_defn,
21545 linkage_name));
21546 const char *name = NULL;
21547
21548 if (actual_name != NULL)
21549 {
21550 const char *die_name = dwarf2_name (die, cu);
21551
21552 if (die_name != NULL
21553 && strcmp (die_name, actual_name.get ()) != 0)
21554 {
21555 /* Strip off the class name from the full name.
21556 We want the prefix. */
21557 int die_name_len = strlen (die_name);
21558 int actual_name_len = strlen (actual_name.get ());
21559 const char *ptr = actual_name.get ();
21560
21561 /* Test for '::' as a sanity check. */
21562 if (actual_name_len > die_name_len + 2
21563 && ptr[actual_name_len - die_name_len - 1] == ':')
21564 name = obstack_strndup (
21565 &objfile->per_bfd->storage_obstack,
21566 ptr, actual_name_len - die_name_len - 2);
21567 }
21568 }
21569 return name;
21570 }
21571 }
21572 }
21573
21574 return NULL;
21575 }
21576
21577 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21578 prefix part in such case. See
21579 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21580
21581 static const char *
21582 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21583 {
21584 struct attribute *attr;
21585 const char *base;
21586
21587 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21588 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21589 return NULL;
21590
21591 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21592 return NULL;
21593
21594 attr = dw2_linkage_name_attr (die, cu);
21595 if (attr == NULL || DW_STRING (attr) == NULL)
21596 return NULL;
21597
21598 /* dwarf2_name had to be already called. */
21599 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21600
21601 /* Strip the base name, keep any leading namespaces/classes. */
21602 base = strrchr (DW_STRING (attr), ':');
21603 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21604 return "";
21605
21606 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21607 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21608 DW_STRING (attr),
21609 &base[-1] - DW_STRING (attr));
21610 }
21611
21612 /* Return the name of the namespace/class that DIE is defined within,
21613 or "" if we can't tell. The caller should not xfree the result.
21614
21615 For example, if we're within the method foo() in the following
21616 code:
21617
21618 namespace N {
21619 class C {
21620 void foo () {
21621 }
21622 };
21623 }
21624
21625 then determine_prefix on foo's die will return "N::C". */
21626
21627 static const char *
21628 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21629 {
21630 struct dwarf2_per_objfile *dwarf2_per_objfile
21631 = cu->per_cu->dwarf2_per_objfile;
21632 struct die_info *parent, *spec_die;
21633 struct dwarf2_cu *spec_cu;
21634 struct type *parent_type;
21635 const char *retval;
21636
21637 if (cu->language != language_cplus
21638 && cu->language != language_fortran && cu->language != language_d
21639 && cu->language != language_rust)
21640 return "";
21641
21642 retval = anonymous_struct_prefix (die, cu);
21643 if (retval)
21644 return retval;
21645
21646 /* We have to be careful in the presence of DW_AT_specification.
21647 For example, with GCC 3.4, given the code
21648
21649 namespace N {
21650 void foo() {
21651 // Definition of N::foo.
21652 }
21653 }
21654
21655 then we'll have a tree of DIEs like this:
21656
21657 1: DW_TAG_compile_unit
21658 2: DW_TAG_namespace // N
21659 3: DW_TAG_subprogram // declaration of N::foo
21660 4: DW_TAG_subprogram // definition of N::foo
21661 DW_AT_specification // refers to die #3
21662
21663 Thus, when processing die #4, we have to pretend that we're in
21664 the context of its DW_AT_specification, namely the contex of die
21665 #3. */
21666 spec_cu = cu;
21667 spec_die = die_specification (die, &spec_cu);
21668 if (spec_die == NULL)
21669 parent = die->parent;
21670 else
21671 {
21672 parent = spec_die->parent;
21673 cu = spec_cu;
21674 }
21675
21676 if (parent == NULL)
21677 return "";
21678 else if (parent->building_fullname)
21679 {
21680 const char *name;
21681 const char *parent_name;
21682
21683 /* It has been seen on RealView 2.2 built binaries,
21684 DW_TAG_template_type_param types actually _defined_ as
21685 children of the parent class:
21686
21687 enum E {};
21688 template class <class Enum> Class{};
21689 Class<enum E> class_e;
21690
21691 1: DW_TAG_class_type (Class)
21692 2: DW_TAG_enumeration_type (E)
21693 3: DW_TAG_enumerator (enum1:0)
21694 3: DW_TAG_enumerator (enum2:1)
21695 ...
21696 2: DW_TAG_template_type_param
21697 DW_AT_type DW_FORM_ref_udata (E)
21698
21699 Besides being broken debug info, it can put GDB into an
21700 infinite loop. Consider:
21701
21702 When we're building the full name for Class<E>, we'll start
21703 at Class, and go look over its template type parameters,
21704 finding E. We'll then try to build the full name of E, and
21705 reach here. We're now trying to build the full name of E,
21706 and look over the parent DIE for containing scope. In the
21707 broken case, if we followed the parent DIE of E, we'd again
21708 find Class, and once again go look at its template type
21709 arguments, etc., etc. Simply don't consider such parent die
21710 as source-level parent of this die (it can't be, the language
21711 doesn't allow it), and break the loop here. */
21712 name = dwarf2_name (die, cu);
21713 parent_name = dwarf2_name (parent, cu);
21714 complaint (_("template param type '%s' defined within parent '%s'"),
21715 name ? name : "<unknown>",
21716 parent_name ? parent_name : "<unknown>");
21717 return "";
21718 }
21719 else
21720 switch (parent->tag)
21721 {
21722 case DW_TAG_namespace:
21723 parent_type = read_type_die (parent, cu);
21724 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21725 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21726 Work around this problem here. */
21727 if (cu->language == language_cplus
21728 && strcmp (parent_type->name (), "::") == 0)
21729 return "";
21730 /* We give a name to even anonymous namespaces. */
21731 return parent_type->name ();
21732 case DW_TAG_class_type:
21733 case DW_TAG_interface_type:
21734 case DW_TAG_structure_type:
21735 case DW_TAG_union_type:
21736 case DW_TAG_module:
21737 parent_type = read_type_die (parent, cu);
21738 if (parent_type->name () != NULL)
21739 return parent_type->name ();
21740 else
21741 /* An anonymous structure is only allowed non-static data
21742 members; no typedefs, no member functions, et cetera.
21743 So it does not need a prefix. */
21744 return "";
21745 case DW_TAG_compile_unit:
21746 case DW_TAG_partial_unit:
21747 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21748 if (cu->language == language_cplus
21749 && !dwarf2_per_objfile->per_bfd->types.empty ()
21750 && die->child != NULL
21751 && (die->tag == DW_TAG_class_type
21752 || die->tag == DW_TAG_structure_type
21753 || die->tag == DW_TAG_union_type))
21754 {
21755 const char *name = guess_full_die_structure_name (die, cu);
21756 if (name != NULL)
21757 return name;
21758 }
21759 return "";
21760 case DW_TAG_subprogram:
21761 /* Nested subroutines in Fortran get a prefix with the name
21762 of the parent's subroutine. */
21763 if (cu->language == language_fortran)
21764 {
21765 if ((die->tag == DW_TAG_subprogram)
21766 && (dwarf2_name (parent, cu) != NULL))
21767 return dwarf2_name (parent, cu);
21768 }
21769 return determine_prefix (parent, cu);
21770 case DW_TAG_enumeration_type:
21771 parent_type = read_type_die (parent, cu);
21772 if (TYPE_DECLARED_CLASS (parent_type))
21773 {
21774 if (parent_type->name () != NULL)
21775 return parent_type->name ();
21776 return "";
21777 }
21778 /* Fall through. */
21779 default:
21780 return determine_prefix (parent, cu);
21781 }
21782 }
21783
21784 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21785 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21786 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21787 an obconcat, otherwise allocate storage for the result. The CU argument is
21788 used to determine the language and hence, the appropriate separator. */
21789
21790 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21791
21792 static char *
21793 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21794 int physname, struct dwarf2_cu *cu)
21795 {
21796 const char *lead = "";
21797 const char *sep;
21798
21799 if (suffix == NULL || suffix[0] == '\0'
21800 || prefix == NULL || prefix[0] == '\0')
21801 sep = "";
21802 else if (cu->language == language_d)
21803 {
21804 /* For D, the 'main' function could be defined in any module, but it
21805 should never be prefixed. */
21806 if (strcmp (suffix, "D main") == 0)
21807 {
21808 prefix = "";
21809 sep = "";
21810 }
21811 else
21812 sep = ".";
21813 }
21814 else if (cu->language == language_fortran && physname)
21815 {
21816 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21817 DW_AT_MIPS_linkage_name is preferred and used instead. */
21818
21819 lead = "__";
21820 sep = "_MOD_";
21821 }
21822 else
21823 sep = "::";
21824
21825 if (prefix == NULL)
21826 prefix = "";
21827 if (suffix == NULL)
21828 suffix = "";
21829
21830 if (obs == NULL)
21831 {
21832 char *retval
21833 = ((char *)
21834 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21835
21836 strcpy (retval, lead);
21837 strcat (retval, prefix);
21838 strcat (retval, sep);
21839 strcat (retval, suffix);
21840 return retval;
21841 }
21842 else
21843 {
21844 /* We have an obstack. */
21845 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21846 }
21847 }
21848
21849 /* Get name of a die, return NULL if not found. */
21850
21851 static const char *
21852 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21853 struct objfile *objfile)
21854 {
21855 if (name && cu->language == language_cplus)
21856 {
21857 gdb::unique_xmalloc_ptr<char> canon_name
21858 = cp_canonicalize_string (name);
21859
21860 if (canon_name != nullptr)
21861 name = objfile->intern (canon_name.get ());
21862 }
21863
21864 return name;
21865 }
21866
21867 /* Get name of a die, return NULL if not found.
21868 Anonymous namespaces are converted to their magic string. */
21869
21870 static const char *
21871 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21872 {
21873 struct attribute *attr;
21874 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21875
21876 attr = dwarf2_attr (die, DW_AT_name, cu);
21877 if ((!attr || !DW_STRING (attr))
21878 && die->tag != DW_TAG_namespace
21879 && die->tag != DW_TAG_class_type
21880 && die->tag != DW_TAG_interface_type
21881 && die->tag != DW_TAG_structure_type
21882 && die->tag != DW_TAG_union_type)
21883 return NULL;
21884
21885 switch (die->tag)
21886 {
21887 case DW_TAG_compile_unit:
21888 case DW_TAG_partial_unit:
21889 /* Compilation units have a DW_AT_name that is a filename, not
21890 a source language identifier. */
21891 case DW_TAG_enumeration_type:
21892 case DW_TAG_enumerator:
21893 /* These tags always have simple identifiers already; no need
21894 to canonicalize them. */
21895 return DW_STRING (attr);
21896
21897 case DW_TAG_namespace:
21898 if (attr != NULL && DW_STRING (attr) != NULL)
21899 return DW_STRING (attr);
21900 return CP_ANONYMOUS_NAMESPACE_STR;
21901
21902 case DW_TAG_class_type:
21903 case DW_TAG_interface_type:
21904 case DW_TAG_structure_type:
21905 case DW_TAG_union_type:
21906 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21907 structures or unions. These were of the form "._%d" in GCC 4.1,
21908 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21909 and GCC 4.4. We work around this problem by ignoring these. */
21910 if (attr && DW_STRING (attr)
21911 && (startswith (DW_STRING (attr), "._")
21912 || startswith (DW_STRING (attr), "<anonymous")))
21913 return NULL;
21914
21915 /* GCC might emit a nameless typedef that has a linkage name. See
21916 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21917 if (!attr || DW_STRING (attr) == NULL)
21918 {
21919 attr = dw2_linkage_name_attr (die, cu);
21920 if (attr == NULL || DW_STRING (attr) == NULL)
21921 return NULL;
21922
21923 /* Avoid demangling DW_STRING (attr) the second time on a second
21924 call for the same DIE. */
21925 if (!DW_STRING_IS_CANONICAL (attr))
21926 {
21927 gdb::unique_xmalloc_ptr<char> demangled
21928 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21929 if (demangled == nullptr)
21930 return nullptr;
21931
21932 DW_STRING (attr) = objfile->intern (demangled.get ());
21933 DW_STRING_IS_CANONICAL (attr) = 1;
21934 }
21935
21936 /* Strip any leading namespaces/classes, keep only the base name.
21937 DW_AT_name for named DIEs does not contain the prefixes. */
21938 const char *base = strrchr (DW_STRING (attr), ':');
21939 if (base && base > DW_STRING (attr) && base[-1] == ':')
21940 return &base[1];
21941 else
21942 return DW_STRING (attr);
21943 }
21944 break;
21945
21946 default:
21947 break;
21948 }
21949
21950 if (!DW_STRING_IS_CANONICAL (attr))
21951 {
21952 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21953 objfile);
21954 DW_STRING_IS_CANONICAL (attr) = 1;
21955 }
21956 return DW_STRING (attr);
21957 }
21958
21959 /* Return the die that this die in an extension of, or NULL if there
21960 is none. *EXT_CU is the CU containing DIE on input, and the CU
21961 containing the return value on output. */
21962
21963 static struct die_info *
21964 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21965 {
21966 struct attribute *attr;
21967
21968 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21969 if (attr == NULL)
21970 return NULL;
21971
21972 return follow_die_ref (die, attr, ext_cu);
21973 }
21974
21975 static void
21976 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21977 {
21978 unsigned int i;
21979
21980 print_spaces (indent, f);
21981 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21982 dwarf_tag_name (die->tag), die->abbrev,
21983 sect_offset_str (die->sect_off));
21984
21985 if (die->parent != NULL)
21986 {
21987 print_spaces (indent, f);
21988 fprintf_unfiltered (f, " parent at offset: %s\n",
21989 sect_offset_str (die->parent->sect_off));
21990 }
21991
21992 print_spaces (indent, f);
21993 fprintf_unfiltered (f, " has children: %s\n",
21994 dwarf_bool_name (die->child != NULL));
21995
21996 print_spaces (indent, f);
21997 fprintf_unfiltered (f, " attributes:\n");
21998
21999 for (i = 0; i < die->num_attrs; ++i)
22000 {
22001 print_spaces (indent, f);
22002 fprintf_unfiltered (f, " %s (%s) ",
22003 dwarf_attr_name (die->attrs[i].name),
22004 dwarf_form_name (die->attrs[i].form));
22005
22006 switch (die->attrs[i].form)
22007 {
22008 case DW_FORM_addr:
22009 case DW_FORM_addrx:
22010 case DW_FORM_GNU_addr_index:
22011 fprintf_unfiltered (f, "address: ");
22012 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22013 break;
22014 case DW_FORM_block2:
22015 case DW_FORM_block4:
22016 case DW_FORM_block:
22017 case DW_FORM_block1:
22018 fprintf_unfiltered (f, "block: size %s",
22019 pulongest (DW_BLOCK (&die->attrs[i])->size));
22020 break;
22021 case DW_FORM_exprloc:
22022 fprintf_unfiltered (f, "expression: size %s",
22023 pulongest (DW_BLOCK (&die->attrs[i])->size));
22024 break;
22025 case DW_FORM_data16:
22026 fprintf_unfiltered (f, "constant of 16 bytes");
22027 break;
22028 case DW_FORM_ref_addr:
22029 fprintf_unfiltered (f, "ref address: ");
22030 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22031 break;
22032 case DW_FORM_GNU_ref_alt:
22033 fprintf_unfiltered (f, "alt ref address: ");
22034 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22035 break;
22036 case DW_FORM_ref1:
22037 case DW_FORM_ref2:
22038 case DW_FORM_ref4:
22039 case DW_FORM_ref8:
22040 case DW_FORM_ref_udata:
22041 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22042 (long) (DW_UNSND (&die->attrs[i])));
22043 break;
22044 case DW_FORM_data1:
22045 case DW_FORM_data2:
22046 case DW_FORM_data4:
22047 case DW_FORM_data8:
22048 case DW_FORM_udata:
22049 case DW_FORM_sdata:
22050 fprintf_unfiltered (f, "constant: %s",
22051 pulongest (DW_UNSND (&die->attrs[i])));
22052 break;
22053 case DW_FORM_sec_offset:
22054 fprintf_unfiltered (f, "section offset: %s",
22055 pulongest (DW_UNSND (&die->attrs[i])));
22056 break;
22057 case DW_FORM_ref_sig8:
22058 fprintf_unfiltered (f, "signature: %s",
22059 hex_string (DW_SIGNATURE (&die->attrs[i])));
22060 break;
22061 case DW_FORM_string:
22062 case DW_FORM_strp:
22063 case DW_FORM_line_strp:
22064 case DW_FORM_strx:
22065 case DW_FORM_GNU_str_index:
22066 case DW_FORM_GNU_strp_alt:
22067 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22068 DW_STRING (&die->attrs[i])
22069 ? DW_STRING (&die->attrs[i]) : "",
22070 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22071 break;
22072 case DW_FORM_flag:
22073 if (DW_UNSND (&die->attrs[i]))
22074 fprintf_unfiltered (f, "flag: TRUE");
22075 else
22076 fprintf_unfiltered (f, "flag: FALSE");
22077 break;
22078 case DW_FORM_flag_present:
22079 fprintf_unfiltered (f, "flag: TRUE");
22080 break;
22081 case DW_FORM_indirect:
22082 /* The reader will have reduced the indirect form to
22083 the "base form" so this form should not occur. */
22084 fprintf_unfiltered (f,
22085 "unexpected attribute form: DW_FORM_indirect");
22086 break;
22087 case DW_FORM_implicit_const:
22088 fprintf_unfiltered (f, "constant: %s",
22089 plongest (DW_SND (&die->attrs[i])));
22090 break;
22091 default:
22092 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22093 die->attrs[i].form);
22094 break;
22095 }
22096 fprintf_unfiltered (f, "\n");
22097 }
22098 }
22099
22100 static void
22101 dump_die_for_error (struct die_info *die)
22102 {
22103 dump_die_shallow (gdb_stderr, 0, die);
22104 }
22105
22106 static void
22107 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22108 {
22109 int indent = level * 4;
22110
22111 gdb_assert (die != NULL);
22112
22113 if (level >= max_level)
22114 return;
22115
22116 dump_die_shallow (f, indent, die);
22117
22118 if (die->child != NULL)
22119 {
22120 print_spaces (indent, f);
22121 fprintf_unfiltered (f, " Children:");
22122 if (level + 1 < max_level)
22123 {
22124 fprintf_unfiltered (f, "\n");
22125 dump_die_1 (f, level + 1, max_level, die->child);
22126 }
22127 else
22128 {
22129 fprintf_unfiltered (f,
22130 " [not printed, max nesting level reached]\n");
22131 }
22132 }
22133
22134 if (die->sibling != NULL && level > 0)
22135 {
22136 dump_die_1 (f, level, max_level, die->sibling);
22137 }
22138 }
22139
22140 /* This is called from the pdie macro in gdbinit.in.
22141 It's not static so gcc will keep a copy callable from gdb. */
22142
22143 void
22144 dump_die (struct die_info *die, int max_level)
22145 {
22146 dump_die_1 (gdb_stdlog, 0, max_level, die);
22147 }
22148
22149 static void
22150 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22151 {
22152 void **slot;
22153
22154 slot = htab_find_slot_with_hash (cu->die_hash, die,
22155 to_underlying (die->sect_off),
22156 INSERT);
22157
22158 *slot = die;
22159 }
22160
22161 /* Follow reference or signature attribute ATTR of SRC_DIE.
22162 On entry *REF_CU is the CU of SRC_DIE.
22163 On exit *REF_CU is the CU of the result. */
22164
22165 static struct die_info *
22166 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22167 struct dwarf2_cu **ref_cu)
22168 {
22169 struct die_info *die;
22170
22171 if (attr->form_is_ref ())
22172 die = follow_die_ref (src_die, attr, ref_cu);
22173 else if (attr->form == DW_FORM_ref_sig8)
22174 die = follow_die_sig (src_die, attr, ref_cu);
22175 else
22176 {
22177 dump_die_for_error (src_die);
22178 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22179 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22180 }
22181
22182 return die;
22183 }
22184
22185 /* Follow reference OFFSET.
22186 On entry *REF_CU is the CU of the source die referencing OFFSET.
22187 On exit *REF_CU is the CU of the result.
22188 Returns NULL if OFFSET is invalid. */
22189
22190 static struct die_info *
22191 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22192 struct dwarf2_cu **ref_cu)
22193 {
22194 struct die_info temp_die;
22195 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22196 struct dwarf2_per_objfile *dwarf2_per_objfile
22197 = cu->per_cu->dwarf2_per_objfile;
22198
22199 gdb_assert (cu->per_cu != NULL);
22200
22201 target_cu = cu;
22202
22203 if (cu->per_cu->is_debug_types)
22204 {
22205 /* .debug_types CUs cannot reference anything outside their CU.
22206 If they need to, they have to reference a signatured type via
22207 DW_FORM_ref_sig8. */
22208 if (!cu->header.offset_in_cu_p (sect_off))
22209 return NULL;
22210 }
22211 else if (offset_in_dwz != cu->per_cu->is_dwz
22212 || !cu->header.offset_in_cu_p (sect_off))
22213 {
22214 struct dwarf2_per_cu_data *per_cu;
22215
22216 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22217 dwarf2_per_objfile);
22218
22219 /* If necessary, add it to the queue and load its DIEs. */
22220 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22221 load_full_comp_unit (per_cu, false, cu->language);
22222
22223 target_cu = per_cu->cu;
22224 }
22225 else if (cu->dies == NULL)
22226 {
22227 /* We're loading full DIEs during partial symbol reading. */
22228 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22229 load_full_comp_unit (cu->per_cu, false, language_minimal);
22230 }
22231
22232 *ref_cu = target_cu;
22233 temp_die.sect_off = sect_off;
22234
22235 if (target_cu != cu)
22236 target_cu->ancestor = cu;
22237
22238 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22239 &temp_die,
22240 to_underlying (sect_off));
22241 }
22242
22243 /* Follow reference attribute ATTR of SRC_DIE.
22244 On entry *REF_CU is the CU of SRC_DIE.
22245 On exit *REF_CU is the CU of the result. */
22246
22247 static struct die_info *
22248 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22249 struct dwarf2_cu **ref_cu)
22250 {
22251 sect_offset sect_off = attr->get_ref_die_offset ();
22252 struct dwarf2_cu *cu = *ref_cu;
22253 struct die_info *die;
22254
22255 die = follow_die_offset (sect_off,
22256 (attr->form == DW_FORM_GNU_ref_alt
22257 || cu->per_cu->is_dwz),
22258 ref_cu);
22259 if (!die)
22260 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22261 "at %s [in module %s]"),
22262 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22263 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22264
22265 return die;
22266 }
22267
22268 /* See read.h. */
22269
22270 struct dwarf2_locexpr_baton
22271 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22272 dwarf2_per_cu_data *per_cu,
22273 CORE_ADDR (*get_frame_pc) (void *baton),
22274 void *baton, bool resolve_abstract_p)
22275 {
22276 struct dwarf2_cu *cu;
22277 struct die_info *die;
22278 struct attribute *attr;
22279 struct dwarf2_locexpr_baton retval;
22280 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22281 struct objfile *objfile = dwarf2_per_objfile->objfile;
22282
22283 if (per_cu->cu == NULL)
22284 load_cu (per_cu, false);
22285 cu = per_cu->cu;
22286 if (cu == NULL)
22287 {
22288 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22289 Instead just throw an error, not much else we can do. */
22290 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22291 sect_offset_str (sect_off), objfile_name (objfile));
22292 }
22293
22294 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22295 if (!die)
22296 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22297 sect_offset_str (sect_off), objfile_name (objfile));
22298
22299 attr = dwarf2_attr (die, DW_AT_location, cu);
22300 if (!attr && resolve_abstract_p
22301 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22302 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22303 {
22304 CORE_ADDR pc = (*get_frame_pc) (baton);
22305 CORE_ADDR baseaddr = objfile->text_section_offset ();
22306 struct gdbarch *gdbarch = objfile->arch ();
22307
22308 for (const auto &cand_off
22309 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22310 {
22311 struct dwarf2_cu *cand_cu = cu;
22312 struct die_info *cand
22313 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22314 if (!cand
22315 || !cand->parent
22316 || cand->parent->tag != DW_TAG_subprogram)
22317 continue;
22318
22319 CORE_ADDR pc_low, pc_high;
22320 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22321 if (pc_low == ((CORE_ADDR) -1))
22322 continue;
22323 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22324 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22325 if (!(pc_low <= pc && pc < pc_high))
22326 continue;
22327
22328 die = cand;
22329 attr = dwarf2_attr (die, DW_AT_location, cu);
22330 break;
22331 }
22332 }
22333
22334 if (!attr)
22335 {
22336 /* DWARF: "If there is no such attribute, then there is no effect.".
22337 DATA is ignored if SIZE is 0. */
22338
22339 retval.data = NULL;
22340 retval.size = 0;
22341 }
22342 else if (attr->form_is_section_offset ())
22343 {
22344 struct dwarf2_loclist_baton loclist_baton;
22345 CORE_ADDR pc = (*get_frame_pc) (baton);
22346 size_t size;
22347
22348 fill_in_loclist_baton (cu, &loclist_baton, attr);
22349
22350 retval.data = dwarf2_find_location_expression (&loclist_baton,
22351 &size, pc);
22352 retval.size = size;
22353 }
22354 else
22355 {
22356 if (!attr->form_is_block ())
22357 error (_("Dwarf Error: DIE at %s referenced in module %s "
22358 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22359 sect_offset_str (sect_off), objfile_name (objfile));
22360
22361 retval.data = DW_BLOCK (attr)->data;
22362 retval.size = DW_BLOCK (attr)->size;
22363 }
22364 retval.per_objfile = dwarf2_per_objfile;
22365 retval.per_cu = cu->per_cu;
22366
22367 age_cached_comp_units (dwarf2_per_objfile);
22368
22369 return retval;
22370 }
22371
22372 /* See read.h. */
22373
22374 struct dwarf2_locexpr_baton
22375 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22376 dwarf2_per_cu_data *per_cu,
22377 CORE_ADDR (*get_frame_pc) (void *baton),
22378 void *baton)
22379 {
22380 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22381
22382 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22383 }
22384
22385 /* Write a constant of a given type as target-ordered bytes into
22386 OBSTACK. */
22387
22388 static const gdb_byte *
22389 write_constant_as_bytes (struct obstack *obstack,
22390 enum bfd_endian byte_order,
22391 struct type *type,
22392 ULONGEST value,
22393 LONGEST *len)
22394 {
22395 gdb_byte *result;
22396
22397 *len = TYPE_LENGTH (type);
22398 result = (gdb_byte *) obstack_alloc (obstack, *len);
22399 store_unsigned_integer (result, *len, byte_order, value);
22400
22401 return result;
22402 }
22403
22404 /* See read.h. */
22405
22406 const gdb_byte *
22407 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22408 dwarf2_per_cu_data *per_cu,
22409 obstack *obstack,
22410 LONGEST *len)
22411 {
22412 struct dwarf2_cu *cu;
22413 struct die_info *die;
22414 struct attribute *attr;
22415 const gdb_byte *result = NULL;
22416 struct type *type;
22417 LONGEST value;
22418 enum bfd_endian byte_order;
22419 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22420
22421 if (per_cu->cu == NULL)
22422 load_cu (per_cu, false);
22423 cu = per_cu->cu;
22424 if (cu == NULL)
22425 {
22426 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22427 Instead just throw an error, not much else we can do. */
22428 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22429 sect_offset_str (sect_off), objfile_name (objfile));
22430 }
22431
22432 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22433 if (!die)
22434 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22435 sect_offset_str (sect_off), objfile_name (objfile));
22436
22437 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22438 if (attr == NULL)
22439 return NULL;
22440
22441 byte_order = (bfd_big_endian (objfile->obfd)
22442 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22443
22444 switch (attr->form)
22445 {
22446 case DW_FORM_addr:
22447 case DW_FORM_addrx:
22448 case DW_FORM_GNU_addr_index:
22449 {
22450 gdb_byte *tem;
22451
22452 *len = cu->header.addr_size;
22453 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22454 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22455 result = tem;
22456 }
22457 break;
22458 case DW_FORM_string:
22459 case DW_FORM_strp:
22460 case DW_FORM_strx:
22461 case DW_FORM_GNU_str_index:
22462 case DW_FORM_GNU_strp_alt:
22463 /* DW_STRING is already allocated on the objfile obstack, point
22464 directly to it. */
22465 result = (const gdb_byte *) DW_STRING (attr);
22466 *len = strlen (DW_STRING (attr));
22467 break;
22468 case DW_FORM_block1:
22469 case DW_FORM_block2:
22470 case DW_FORM_block4:
22471 case DW_FORM_block:
22472 case DW_FORM_exprloc:
22473 case DW_FORM_data16:
22474 result = DW_BLOCK (attr)->data;
22475 *len = DW_BLOCK (attr)->size;
22476 break;
22477
22478 /* The DW_AT_const_value attributes are supposed to carry the
22479 symbol's value "represented as it would be on the target
22480 architecture." By the time we get here, it's already been
22481 converted to host endianness, so we just need to sign- or
22482 zero-extend it as appropriate. */
22483 case DW_FORM_data1:
22484 type = die_type (die, cu);
22485 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22486 if (result == NULL)
22487 result = write_constant_as_bytes (obstack, byte_order,
22488 type, value, len);
22489 break;
22490 case DW_FORM_data2:
22491 type = die_type (die, cu);
22492 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22493 if (result == NULL)
22494 result = write_constant_as_bytes (obstack, byte_order,
22495 type, value, len);
22496 break;
22497 case DW_FORM_data4:
22498 type = die_type (die, cu);
22499 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22500 if (result == NULL)
22501 result = write_constant_as_bytes (obstack, byte_order,
22502 type, value, len);
22503 break;
22504 case DW_FORM_data8:
22505 type = die_type (die, cu);
22506 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22507 if (result == NULL)
22508 result = write_constant_as_bytes (obstack, byte_order,
22509 type, value, len);
22510 break;
22511
22512 case DW_FORM_sdata:
22513 case DW_FORM_implicit_const:
22514 type = die_type (die, cu);
22515 result = write_constant_as_bytes (obstack, byte_order,
22516 type, DW_SND (attr), len);
22517 break;
22518
22519 case DW_FORM_udata:
22520 type = die_type (die, cu);
22521 result = write_constant_as_bytes (obstack, byte_order,
22522 type, DW_UNSND (attr), len);
22523 break;
22524
22525 default:
22526 complaint (_("unsupported const value attribute form: '%s'"),
22527 dwarf_form_name (attr->form));
22528 break;
22529 }
22530
22531 return result;
22532 }
22533
22534 /* See read.h. */
22535
22536 struct type *
22537 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22538 dwarf2_per_cu_data *per_cu)
22539 {
22540 struct dwarf2_cu *cu;
22541 struct die_info *die;
22542
22543 if (per_cu->cu == NULL)
22544 load_cu (per_cu, false);
22545 cu = per_cu->cu;
22546 if (!cu)
22547 return NULL;
22548
22549 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22550 if (!die)
22551 return NULL;
22552
22553 return die_type (die, cu);
22554 }
22555
22556 /* See read.h. */
22557
22558 struct type *
22559 dwarf2_get_die_type (cu_offset die_offset,
22560 struct dwarf2_per_cu_data *per_cu)
22561 {
22562 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22563 return get_die_type_at_offset (die_offset_sect, per_cu);
22564 }
22565
22566 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22567 On entry *REF_CU is the CU of SRC_DIE.
22568 On exit *REF_CU is the CU of the result.
22569 Returns NULL if the referenced DIE isn't found. */
22570
22571 static struct die_info *
22572 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22573 struct dwarf2_cu **ref_cu)
22574 {
22575 struct die_info temp_die;
22576 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22577 struct die_info *die;
22578
22579 /* While it might be nice to assert sig_type->type == NULL here,
22580 we can get here for DW_AT_imported_declaration where we need
22581 the DIE not the type. */
22582
22583 /* If necessary, add it to the queue and load its DIEs. */
22584
22585 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22586 read_signatured_type (sig_type);
22587
22588 sig_cu = sig_type->per_cu.cu;
22589 gdb_assert (sig_cu != NULL);
22590 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22591 temp_die.sect_off = sig_type->type_offset_in_section;
22592 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22593 to_underlying (temp_die.sect_off));
22594 if (die)
22595 {
22596 struct dwarf2_per_objfile *dwarf2_per_objfile
22597 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22598
22599 /* For .gdb_index version 7 keep track of included TUs.
22600 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22601 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22602 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22603 {
22604 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22605 }
22606
22607 *ref_cu = sig_cu;
22608 if (sig_cu != cu)
22609 sig_cu->ancestor = cu;
22610
22611 return die;
22612 }
22613
22614 return NULL;
22615 }
22616
22617 /* Follow signatured type referenced by ATTR in SRC_DIE.
22618 On entry *REF_CU is the CU of SRC_DIE.
22619 On exit *REF_CU is the CU of the result.
22620 The result is the DIE of the type.
22621 If the referenced type cannot be found an error is thrown. */
22622
22623 static struct die_info *
22624 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22625 struct dwarf2_cu **ref_cu)
22626 {
22627 ULONGEST signature = DW_SIGNATURE (attr);
22628 struct signatured_type *sig_type;
22629 struct die_info *die;
22630
22631 gdb_assert (attr->form == DW_FORM_ref_sig8);
22632
22633 sig_type = lookup_signatured_type (*ref_cu, signature);
22634 /* sig_type will be NULL if the signatured type is missing from
22635 the debug info. */
22636 if (sig_type == NULL)
22637 {
22638 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22639 " from DIE at %s [in module %s]"),
22640 hex_string (signature), sect_offset_str (src_die->sect_off),
22641 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22642 }
22643
22644 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22645 if (die == NULL)
22646 {
22647 dump_die_for_error (src_die);
22648 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22649 " from DIE at %s [in module %s]"),
22650 hex_string (signature), sect_offset_str (src_die->sect_off),
22651 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22652 }
22653
22654 return die;
22655 }
22656
22657 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22658 reading in and processing the type unit if necessary. */
22659
22660 static struct type *
22661 get_signatured_type (struct die_info *die, ULONGEST signature,
22662 struct dwarf2_cu *cu)
22663 {
22664 struct dwarf2_per_objfile *dwarf2_per_objfile
22665 = cu->per_cu->dwarf2_per_objfile;
22666 struct signatured_type *sig_type;
22667 struct dwarf2_cu *type_cu;
22668 struct die_info *type_die;
22669 struct type *type;
22670
22671 sig_type = lookup_signatured_type (cu, signature);
22672 /* sig_type will be NULL if the signatured type is missing from
22673 the debug info. */
22674 if (sig_type == NULL)
22675 {
22676 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22677 " from DIE at %s [in module %s]"),
22678 hex_string (signature), sect_offset_str (die->sect_off),
22679 objfile_name (dwarf2_per_objfile->objfile));
22680 return build_error_marker_type (cu, die);
22681 }
22682
22683 /* If we already know the type we're done. */
22684 if (sig_type->type != NULL)
22685 return sig_type->type;
22686
22687 type_cu = cu;
22688 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22689 if (type_die != NULL)
22690 {
22691 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22692 is created. This is important, for example, because for c++ classes
22693 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22694 type = read_type_die (type_die, type_cu);
22695 if (type == NULL)
22696 {
22697 complaint (_("Dwarf Error: Cannot build signatured type %s"
22698 " referenced from DIE at %s [in module %s]"),
22699 hex_string (signature), sect_offset_str (die->sect_off),
22700 objfile_name (dwarf2_per_objfile->objfile));
22701 type = build_error_marker_type (cu, die);
22702 }
22703 }
22704 else
22705 {
22706 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22707 " from DIE at %s [in module %s]"),
22708 hex_string (signature), sect_offset_str (die->sect_off),
22709 objfile_name (dwarf2_per_objfile->objfile));
22710 type = build_error_marker_type (cu, die);
22711 }
22712 sig_type->type = type;
22713
22714 return type;
22715 }
22716
22717 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22718 reading in and processing the type unit if necessary. */
22719
22720 static struct type *
22721 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22722 struct dwarf2_cu *cu) /* ARI: editCase function */
22723 {
22724 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22725 if (attr->form_is_ref ())
22726 {
22727 struct dwarf2_cu *type_cu = cu;
22728 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22729
22730 return read_type_die (type_die, type_cu);
22731 }
22732 else if (attr->form == DW_FORM_ref_sig8)
22733 {
22734 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22735 }
22736 else
22737 {
22738 struct dwarf2_per_objfile *dwarf2_per_objfile
22739 = cu->per_cu->dwarf2_per_objfile;
22740
22741 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22742 " at %s [in module %s]"),
22743 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22744 objfile_name (dwarf2_per_objfile->objfile));
22745 return build_error_marker_type (cu, die);
22746 }
22747 }
22748
22749 /* Load the DIEs associated with type unit PER_CU into memory. */
22750
22751 static void
22752 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22753 {
22754 struct signatured_type *sig_type;
22755
22756 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22757 gdb_assert (! per_cu->type_unit_group_p ());
22758
22759 /* We have the per_cu, but we need the signatured_type.
22760 Fortunately this is an easy translation. */
22761 gdb_assert (per_cu->is_debug_types);
22762 sig_type = (struct signatured_type *) per_cu;
22763
22764 gdb_assert (per_cu->cu == NULL);
22765
22766 read_signatured_type (sig_type);
22767
22768 gdb_assert (per_cu->cu != NULL);
22769 }
22770
22771 /* Read in a signatured type and build its CU and DIEs.
22772 If the type is a stub for the real type in a DWO file,
22773 read in the real type from the DWO file as well. */
22774
22775 static void
22776 read_signatured_type (struct signatured_type *sig_type)
22777 {
22778 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22779
22780 gdb_assert (per_cu->is_debug_types);
22781 gdb_assert (per_cu->cu == NULL);
22782
22783 cutu_reader reader (per_cu, NULL, 0, false);
22784
22785 if (!reader.dummy_p)
22786 {
22787 struct dwarf2_cu *cu = reader.cu;
22788 const gdb_byte *info_ptr = reader.info_ptr;
22789
22790 gdb_assert (cu->die_hash == NULL);
22791 cu->die_hash =
22792 htab_create_alloc_ex (cu->header.length / 12,
22793 die_hash,
22794 die_eq,
22795 NULL,
22796 &cu->comp_unit_obstack,
22797 hashtab_obstack_allocate,
22798 dummy_obstack_deallocate);
22799
22800 if (reader.comp_unit_die->has_children)
22801 reader.comp_unit_die->child
22802 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22803 reader.comp_unit_die);
22804 cu->dies = reader.comp_unit_die;
22805 /* comp_unit_die is not stored in die_hash, no need. */
22806
22807 /* We try not to read any attributes in this function, because
22808 not all CUs needed for references have been loaded yet, and
22809 symbol table processing isn't initialized. But we have to
22810 set the CU language, or we won't be able to build types
22811 correctly. Similarly, if we do not read the producer, we can
22812 not apply producer-specific interpretation. */
22813 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22814
22815 reader.keep ();
22816 }
22817
22818 sig_type->per_cu.tu_read = 1;
22819 }
22820
22821 /* Decode simple location descriptions.
22822 Given a pointer to a dwarf block that defines a location, compute
22823 the location and return the value. If COMPUTED is non-null, it is
22824 set to true to indicate that decoding was successful, and false
22825 otherwise. If COMPUTED is null, then this function may emit a
22826 complaint. */
22827
22828 static CORE_ADDR
22829 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22830 {
22831 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22832 size_t i;
22833 size_t size = blk->size;
22834 const gdb_byte *data = blk->data;
22835 CORE_ADDR stack[64];
22836 int stacki;
22837 unsigned int bytes_read, unsnd;
22838 gdb_byte op;
22839
22840 if (computed != nullptr)
22841 *computed = false;
22842
22843 i = 0;
22844 stacki = 0;
22845 stack[stacki] = 0;
22846 stack[++stacki] = 0;
22847
22848 while (i < size)
22849 {
22850 op = data[i++];
22851 switch (op)
22852 {
22853 case DW_OP_lit0:
22854 case DW_OP_lit1:
22855 case DW_OP_lit2:
22856 case DW_OP_lit3:
22857 case DW_OP_lit4:
22858 case DW_OP_lit5:
22859 case DW_OP_lit6:
22860 case DW_OP_lit7:
22861 case DW_OP_lit8:
22862 case DW_OP_lit9:
22863 case DW_OP_lit10:
22864 case DW_OP_lit11:
22865 case DW_OP_lit12:
22866 case DW_OP_lit13:
22867 case DW_OP_lit14:
22868 case DW_OP_lit15:
22869 case DW_OP_lit16:
22870 case DW_OP_lit17:
22871 case DW_OP_lit18:
22872 case DW_OP_lit19:
22873 case DW_OP_lit20:
22874 case DW_OP_lit21:
22875 case DW_OP_lit22:
22876 case DW_OP_lit23:
22877 case DW_OP_lit24:
22878 case DW_OP_lit25:
22879 case DW_OP_lit26:
22880 case DW_OP_lit27:
22881 case DW_OP_lit28:
22882 case DW_OP_lit29:
22883 case DW_OP_lit30:
22884 case DW_OP_lit31:
22885 stack[++stacki] = op - DW_OP_lit0;
22886 break;
22887
22888 case DW_OP_reg0:
22889 case DW_OP_reg1:
22890 case DW_OP_reg2:
22891 case DW_OP_reg3:
22892 case DW_OP_reg4:
22893 case DW_OP_reg5:
22894 case DW_OP_reg6:
22895 case DW_OP_reg7:
22896 case DW_OP_reg8:
22897 case DW_OP_reg9:
22898 case DW_OP_reg10:
22899 case DW_OP_reg11:
22900 case DW_OP_reg12:
22901 case DW_OP_reg13:
22902 case DW_OP_reg14:
22903 case DW_OP_reg15:
22904 case DW_OP_reg16:
22905 case DW_OP_reg17:
22906 case DW_OP_reg18:
22907 case DW_OP_reg19:
22908 case DW_OP_reg20:
22909 case DW_OP_reg21:
22910 case DW_OP_reg22:
22911 case DW_OP_reg23:
22912 case DW_OP_reg24:
22913 case DW_OP_reg25:
22914 case DW_OP_reg26:
22915 case DW_OP_reg27:
22916 case DW_OP_reg28:
22917 case DW_OP_reg29:
22918 case DW_OP_reg30:
22919 case DW_OP_reg31:
22920 stack[++stacki] = op - DW_OP_reg0;
22921 if (i < size)
22922 {
22923 if (computed == nullptr)
22924 dwarf2_complex_location_expr_complaint ();
22925 else
22926 return 0;
22927 }
22928 break;
22929
22930 case DW_OP_regx:
22931 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22932 i += bytes_read;
22933 stack[++stacki] = unsnd;
22934 if (i < size)
22935 {
22936 if (computed == nullptr)
22937 dwarf2_complex_location_expr_complaint ();
22938 else
22939 return 0;
22940 }
22941 break;
22942
22943 case DW_OP_addr:
22944 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22945 &bytes_read);
22946 i += bytes_read;
22947 break;
22948
22949 case DW_OP_const1u:
22950 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22951 i += 1;
22952 break;
22953
22954 case DW_OP_const1s:
22955 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22956 i += 1;
22957 break;
22958
22959 case DW_OP_const2u:
22960 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22961 i += 2;
22962 break;
22963
22964 case DW_OP_const2s:
22965 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22966 i += 2;
22967 break;
22968
22969 case DW_OP_const4u:
22970 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22971 i += 4;
22972 break;
22973
22974 case DW_OP_const4s:
22975 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22976 i += 4;
22977 break;
22978
22979 case DW_OP_const8u:
22980 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22981 i += 8;
22982 break;
22983
22984 case DW_OP_constu:
22985 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22986 &bytes_read);
22987 i += bytes_read;
22988 break;
22989
22990 case DW_OP_consts:
22991 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22992 i += bytes_read;
22993 break;
22994
22995 case DW_OP_dup:
22996 stack[stacki + 1] = stack[stacki];
22997 stacki++;
22998 break;
22999
23000 case DW_OP_plus:
23001 stack[stacki - 1] += stack[stacki];
23002 stacki--;
23003 break;
23004
23005 case DW_OP_plus_uconst:
23006 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23007 &bytes_read);
23008 i += bytes_read;
23009 break;
23010
23011 case DW_OP_minus:
23012 stack[stacki - 1] -= stack[stacki];
23013 stacki--;
23014 break;
23015
23016 case DW_OP_deref:
23017 /* If we're not the last op, then we definitely can't encode
23018 this using GDB's address_class enum. This is valid for partial
23019 global symbols, although the variable's address will be bogus
23020 in the psymtab. */
23021 if (i < size)
23022 {
23023 if (computed == nullptr)
23024 dwarf2_complex_location_expr_complaint ();
23025 else
23026 return 0;
23027 }
23028 break;
23029
23030 case DW_OP_GNU_push_tls_address:
23031 case DW_OP_form_tls_address:
23032 /* The top of the stack has the offset from the beginning
23033 of the thread control block at which the variable is located. */
23034 /* Nothing should follow this operator, so the top of stack would
23035 be returned. */
23036 /* This is valid for partial global symbols, but the variable's
23037 address will be bogus in the psymtab. Make it always at least
23038 non-zero to not look as a variable garbage collected by linker
23039 which have DW_OP_addr 0. */
23040 if (i < size)
23041 {
23042 if (computed == nullptr)
23043 dwarf2_complex_location_expr_complaint ();
23044 else
23045 return 0;
23046 }
23047 stack[stacki]++;
23048 break;
23049
23050 case DW_OP_GNU_uninit:
23051 if (computed != nullptr)
23052 return 0;
23053 break;
23054
23055 case DW_OP_addrx:
23056 case DW_OP_GNU_addr_index:
23057 case DW_OP_GNU_const_index:
23058 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23059 &bytes_read);
23060 i += bytes_read;
23061 break;
23062
23063 default:
23064 if (computed == nullptr)
23065 {
23066 const char *name = get_DW_OP_name (op);
23067
23068 if (name)
23069 complaint (_("unsupported stack op: '%s'"),
23070 name);
23071 else
23072 complaint (_("unsupported stack op: '%02x'"),
23073 op);
23074 }
23075
23076 return (stack[stacki]);
23077 }
23078
23079 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23080 outside of the allocated space. Also enforce minimum>0. */
23081 if (stacki >= ARRAY_SIZE (stack) - 1)
23082 {
23083 if (computed == nullptr)
23084 complaint (_("location description stack overflow"));
23085 return 0;
23086 }
23087
23088 if (stacki <= 0)
23089 {
23090 if (computed == nullptr)
23091 complaint (_("location description stack underflow"));
23092 return 0;
23093 }
23094 }
23095
23096 if (computed != nullptr)
23097 *computed = true;
23098 return (stack[stacki]);
23099 }
23100
23101 /* memory allocation interface */
23102
23103 static struct dwarf_block *
23104 dwarf_alloc_block (struct dwarf2_cu *cu)
23105 {
23106 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23107 }
23108
23109 static struct die_info *
23110 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23111 {
23112 struct die_info *die;
23113 size_t size = sizeof (struct die_info);
23114
23115 if (num_attrs > 1)
23116 size += (num_attrs - 1) * sizeof (struct attribute);
23117
23118 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23119 memset (die, 0, sizeof (struct die_info));
23120 return (die);
23121 }
23122
23123 \f
23124
23125 /* Macro support. */
23126
23127 /* An overload of dwarf_decode_macros that finds the correct section
23128 and ensures it is read in before calling the other overload. */
23129
23130 static void
23131 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23132 int section_is_gnu)
23133 {
23134 struct dwarf2_per_objfile *dwarf2_per_objfile
23135 = cu->per_cu->dwarf2_per_objfile;
23136 struct objfile *objfile = dwarf2_per_objfile->objfile;
23137 const struct line_header *lh = cu->line_header;
23138 unsigned int offset_size = cu->header.offset_size;
23139 struct dwarf2_section_info *section;
23140 const char *section_name;
23141
23142 if (cu->dwo_unit != nullptr)
23143 {
23144 if (section_is_gnu)
23145 {
23146 section = &cu->dwo_unit->dwo_file->sections.macro;
23147 section_name = ".debug_macro.dwo";
23148 }
23149 else
23150 {
23151 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23152 section_name = ".debug_macinfo.dwo";
23153 }
23154 }
23155 else
23156 {
23157 if (section_is_gnu)
23158 {
23159 section = &dwarf2_per_objfile->per_bfd->macro;
23160 section_name = ".debug_macro";
23161 }
23162 else
23163 {
23164 section = &dwarf2_per_objfile->per_bfd->macinfo;
23165 section_name = ".debug_macinfo";
23166 }
23167 }
23168
23169 section->read (objfile);
23170 if (section->buffer == nullptr)
23171 {
23172 complaint (_("missing %s section"), section_name);
23173 return;
23174 }
23175
23176 buildsym_compunit *builder = cu->get_builder ();
23177
23178 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23179 offset_size, offset, section_is_gnu);
23180 }
23181
23182 /* Return the .debug_loc section to use for CU.
23183 For DWO files use .debug_loc.dwo. */
23184
23185 static struct dwarf2_section_info *
23186 cu_debug_loc_section (struct dwarf2_cu *cu)
23187 {
23188 struct dwarf2_per_objfile *dwarf2_per_objfile
23189 = cu->per_cu->dwarf2_per_objfile;
23190
23191 if (cu->dwo_unit)
23192 {
23193 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23194
23195 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23196 }
23197 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23198 : &dwarf2_per_objfile->per_bfd->loc);
23199 }
23200
23201 /* A helper function that fills in a dwarf2_loclist_baton. */
23202
23203 static void
23204 fill_in_loclist_baton (struct dwarf2_cu *cu,
23205 struct dwarf2_loclist_baton *baton,
23206 const struct attribute *attr)
23207 {
23208 struct dwarf2_per_objfile *dwarf2_per_objfile
23209 = cu->per_cu->dwarf2_per_objfile;
23210 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23211
23212 section->read (dwarf2_per_objfile->objfile);
23213
23214 baton->per_objfile = dwarf2_per_objfile;
23215 baton->per_cu = cu->per_cu;
23216 gdb_assert (baton->per_cu);
23217 /* We don't know how long the location list is, but make sure we
23218 don't run off the edge of the section. */
23219 baton->size = section->size - DW_UNSND (attr);
23220 baton->data = section->buffer + DW_UNSND (attr);
23221 if (cu->base_address.has_value ())
23222 baton->base_address = *cu->base_address;
23223 else
23224 baton->base_address = 0;
23225 baton->from_dwo = cu->dwo_unit != NULL;
23226 }
23227
23228 static void
23229 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23230 struct dwarf2_cu *cu, int is_block)
23231 {
23232 struct dwarf2_per_objfile *dwarf2_per_objfile
23233 = cu->per_cu->dwarf2_per_objfile;
23234 struct objfile *objfile = dwarf2_per_objfile->objfile;
23235 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23236
23237 if (attr->form_is_section_offset ()
23238 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23239 the section. If so, fall through to the complaint in the
23240 other branch. */
23241 && DW_UNSND (attr) < section->get_size (objfile))
23242 {
23243 struct dwarf2_loclist_baton *baton;
23244
23245 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23246
23247 fill_in_loclist_baton (cu, baton, attr);
23248
23249 if (!cu->base_address.has_value ())
23250 complaint (_("Location list used without "
23251 "specifying the CU base address."));
23252
23253 SYMBOL_ACLASS_INDEX (sym) = (is_block
23254 ? dwarf2_loclist_block_index
23255 : dwarf2_loclist_index);
23256 SYMBOL_LOCATION_BATON (sym) = baton;
23257 }
23258 else
23259 {
23260 struct dwarf2_locexpr_baton *baton;
23261
23262 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23263 baton->per_objfile = dwarf2_per_objfile;
23264 baton->per_cu = cu->per_cu;
23265 gdb_assert (baton->per_cu);
23266
23267 if (attr->form_is_block ())
23268 {
23269 /* Note that we're just copying the block's data pointer
23270 here, not the actual data. We're still pointing into the
23271 info_buffer for SYM's objfile; right now we never release
23272 that buffer, but when we do clean up properly this may
23273 need to change. */
23274 baton->size = DW_BLOCK (attr)->size;
23275 baton->data = DW_BLOCK (attr)->data;
23276 }
23277 else
23278 {
23279 dwarf2_invalid_attrib_class_complaint ("location description",
23280 sym->natural_name ());
23281 baton->size = 0;
23282 }
23283
23284 SYMBOL_ACLASS_INDEX (sym) = (is_block
23285 ? dwarf2_locexpr_block_index
23286 : dwarf2_locexpr_index);
23287 SYMBOL_LOCATION_BATON (sym) = baton;
23288 }
23289 }
23290
23291 /* See read.h. */
23292
23293 struct objfile *
23294 dwarf2_per_cu_data::objfile () const
23295 {
23296 struct objfile *objfile = dwarf2_per_objfile->objfile;
23297
23298 /* Return the master objfile, so that we can report and look up the
23299 correct file containing this variable. */
23300 if (objfile->separate_debug_objfile_backlink)
23301 objfile = objfile->separate_debug_objfile_backlink;
23302
23303 return objfile;
23304 }
23305
23306 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23307 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23308 CU_HEADERP first. */
23309
23310 static const struct comp_unit_head *
23311 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23312 const struct dwarf2_per_cu_data *per_cu)
23313 {
23314 const gdb_byte *info_ptr;
23315
23316 if (per_cu->cu)
23317 return &per_cu->cu->header;
23318
23319 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23320
23321 memset (cu_headerp, 0, sizeof (*cu_headerp));
23322 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23323 rcuh_kind::COMPILE);
23324
23325 return cu_headerp;
23326 }
23327
23328 /* See read.h. */
23329
23330 int
23331 dwarf2_per_cu_data::addr_size () const
23332 {
23333 struct comp_unit_head cu_header_local;
23334 const struct comp_unit_head *cu_headerp;
23335
23336 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23337
23338 return cu_headerp->addr_size;
23339 }
23340
23341 /* See read.h. */
23342
23343 int
23344 dwarf2_per_cu_data::offset_size () const
23345 {
23346 struct comp_unit_head cu_header_local;
23347 const struct comp_unit_head *cu_headerp;
23348
23349 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23350
23351 return cu_headerp->offset_size;
23352 }
23353
23354 /* See read.h. */
23355
23356 int
23357 dwarf2_per_cu_data::ref_addr_size () const
23358 {
23359 struct comp_unit_head cu_header_local;
23360 const struct comp_unit_head *cu_headerp;
23361
23362 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23363
23364 if (cu_headerp->version == 2)
23365 return cu_headerp->addr_size;
23366 else
23367 return cu_headerp->offset_size;
23368 }
23369
23370 /* See read.h. */
23371
23372 CORE_ADDR
23373 dwarf2_per_cu_data::text_offset () const
23374 {
23375 struct objfile *objfile = dwarf2_per_objfile->objfile;
23376
23377 return objfile->text_section_offset ();
23378 }
23379
23380 /* See read.h. */
23381
23382 struct type *
23383 dwarf2_per_cu_data::addr_type () const
23384 {
23385 struct objfile *objfile = dwarf2_per_objfile->objfile;
23386 struct type *void_type = objfile_type (objfile)->builtin_void;
23387 struct type *addr_type = lookup_pointer_type (void_type);
23388 int addr_size = this->addr_size ();
23389
23390 if (TYPE_LENGTH (addr_type) == addr_size)
23391 return addr_type;
23392
23393 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23394 return addr_type;
23395 }
23396
23397 /* A helper function for dwarf2_find_containing_comp_unit that returns
23398 the index of the result, and that searches a vector. It will
23399 return a result even if the offset in question does not actually
23400 occur in any CU. This is separate so that it can be unit
23401 tested. */
23402
23403 static int
23404 dwarf2_find_containing_comp_unit
23405 (sect_offset sect_off,
23406 unsigned int offset_in_dwz,
23407 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23408 {
23409 int low, high;
23410
23411 low = 0;
23412 high = all_comp_units.size () - 1;
23413 while (high > low)
23414 {
23415 struct dwarf2_per_cu_data *mid_cu;
23416 int mid = low + (high - low) / 2;
23417
23418 mid_cu = all_comp_units[mid];
23419 if (mid_cu->is_dwz > offset_in_dwz
23420 || (mid_cu->is_dwz == offset_in_dwz
23421 && mid_cu->sect_off + mid_cu->length > sect_off))
23422 high = mid;
23423 else
23424 low = mid + 1;
23425 }
23426 gdb_assert (low == high);
23427 return low;
23428 }
23429
23430 /* Locate the .debug_info compilation unit from CU's objfile which contains
23431 the DIE at OFFSET. Raises an error on failure. */
23432
23433 static struct dwarf2_per_cu_data *
23434 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23435 unsigned int offset_in_dwz,
23436 struct dwarf2_per_objfile *dwarf2_per_objfile)
23437 {
23438 int low
23439 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23440 dwarf2_per_objfile->per_bfd->all_comp_units);
23441 struct dwarf2_per_cu_data *this_cu
23442 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23443
23444 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23445 {
23446 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23447 error (_("Dwarf Error: could not find partial DIE containing "
23448 "offset %s [in module %s]"),
23449 sect_offset_str (sect_off),
23450 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23451
23452 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23453 <= sect_off);
23454 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23455 }
23456 else
23457 {
23458 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23459 && sect_off >= this_cu->sect_off + this_cu->length)
23460 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23461 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23462 return this_cu;
23463 }
23464 }
23465
23466 #if GDB_SELF_TEST
23467
23468 namespace selftests {
23469 namespace find_containing_comp_unit {
23470
23471 static void
23472 run_test ()
23473 {
23474 struct dwarf2_per_cu_data one {};
23475 struct dwarf2_per_cu_data two {};
23476 struct dwarf2_per_cu_data three {};
23477 struct dwarf2_per_cu_data four {};
23478
23479 one.length = 5;
23480 two.sect_off = sect_offset (one.length);
23481 two.length = 7;
23482
23483 three.length = 5;
23484 three.is_dwz = 1;
23485 four.sect_off = sect_offset (three.length);
23486 four.length = 7;
23487 four.is_dwz = 1;
23488
23489 std::vector<dwarf2_per_cu_data *> units;
23490 units.push_back (&one);
23491 units.push_back (&two);
23492 units.push_back (&three);
23493 units.push_back (&four);
23494
23495 int result;
23496
23497 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23498 SELF_CHECK (units[result] == &one);
23499 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23500 SELF_CHECK (units[result] == &one);
23501 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23502 SELF_CHECK (units[result] == &two);
23503
23504 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23505 SELF_CHECK (units[result] == &three);
23506 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23507 SELF_CHECK (units[result] == &three);
23508 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23509 SELF_CHECK (units[result] == &four);
23510 }
23511
23512 }
23513 }
23514
23515 #endif /* GDB_SELF_TEST */
23516
23517 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23518
23519 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23520 : per_cu (per_cu_),
23521 mark (false),
23522 has_loclist (false),
23523 checked_producer (false),
23524 producer_is_gxx_lt_4_6 (false),
23525 producer_is_gcc_lt_4_3 (false),
23526 producer_is_icc (false),
23527 producer_is_icc_lt_14 (false),
23528 producer_is_codewarrior (false),
23529 processing_has_namespace_info (false)
23530 {
23531 per_cu->cu = this;
23532 }
23533
23534 /* Destroy a dwarf2_cu. */
23535
23536 dwarf2_cu::~dwarf2_cu ()
23537 {
23538 per_cu->cu = NULL;
23539 }
23540
23541 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23542
23543 static void
23544 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23545 enum language pretend_language)
23546 {
23547 struct attribute *attr;
23548
23549 /* Set the language we're debugging. */
23550 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23551 if (attr != nullptr)
23552 set_cu_language (DW_UNSND (attr), cu);
23553 else
23554 {
23555 cu->language = pretend_language;
23556 cu->language_defn = language_def (cu->language);
23557 }
23558
23559 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23560 }
23561
23562 /* Increase the age counter on each cached compilation unit, and free
23563 any that are too old. */
23564
23565 static void
23566 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23567 {
23568 struct dwarf2_per_cu_data *per_cu, **last_chain;
23569
23570 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23571 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23572 while (per_cu != NULL)
23573 {
23574 per_cu->cu->last_used ++;
23575 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23576 dwarf2_mark (per_cu->cu);
23577 per_cu = per_cu->cu->read_in_chain;
23578 }
23579
23580 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23581 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23582 while (per_cu != NULL)
23583 {
23584 struct dwarf2_per_cu_data *next_cu;
23585
23586 next_cu = per_cu->cu->read_in_chain;
23587
23588 if (!per_cu->cu->mark)
23589 {
23590 delete per_cu->cu;
23591 *last_chain = next_cu;
23592 }
23593 else
23594 last_chain = &per_cu->cu->read_in_chain;
23595
23596 per_cu = next_cu;
23597 }
23598 }
23599
23600 /* Remove a single compilation unit from the cache. */
23601
23602 static void
23603 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23604 {
23605 struct dwarf2_per_cu_data *per_cu, **last_chain;
23606 struct dwarf2_per_objfile *dwarf2_per_objfile
23607 = target_per_cu->dwarf2_per_objfile;
23608
23609 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23610 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23611 while (per_cu != NULL)
23612 {
23613 struct dwarf2_per_cu_data *next_cu;
23614
23615 next_cu = per_cu->cu->read_in_chain;
23616
23617 if (per_cu == target_per_cu)
23618 {
23619 delete per_cu->cu;
23620 per_cu->cu = NULL;
23621 *last_chain = next_cu;
23622 break;
23623 }
23624 else
23625 last_chain = &per_cu->cu->read_in_chain;
23626
23627 per_cu = next_cu;
23628 }
23629 }
23630
23631 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23632 We store these in a hash table separate from the DIEs, and preserve them
23633 when the DIEs are flushed out of cache.
23634
23635 The CU "per_cu" pointer is needed because offset alone is not enough to
23636 uniquely identify the type. A file may have multiple .debug_types sections,
23637 or the type may come from a DWO file. Furthermore, while it's more logical
23638 to use per_cu->section+offset, with Fission the section with the data is in
23639 the DWO file but we don't know that section at the point we need it.
23640 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23641 because we can enter the lookup routine, get_die_type_at_offset, from
23642 outside this file, and thus won't necessarily have PER_CU->cu.
23643 Fortunately, PER_CU is stable for the life of the objfile. */
23644
23645 struct dwarf2_per_cu_offset_and_type
23646 {
23647 const struct dwarf2_per_cu_data *per_cu;
23648 sect_offset sect_off;
23649 struct type *type;
23650 };
23651
23652 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23653
23654 static hashval_t
23655 per_cu_offset_and_type_hash (const void *item)
23656 {
23657 const struct dwarf2_per_cu_offset_and_type *ofs
23658 = (const struct dwarf2_per_cu_offset_and_type *) item;
23659
23660 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23661 }
23662
23663 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23664
23665 static int
23666 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23667 {
23668 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23669 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23670 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23671 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23672
23673 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23674 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23675 }
23676
23677 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23678 table if necessary. For convenience, return TYPE.
23679
23680 The DIEs reading must have careful ordering to:
23681 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23682 reading current DIE.
23683 * Not trying to dereference contents of still incompletely read in types
23684 while reading in other DIEs.
23685 * Enable referencing still incompletely read in types just by a pointer to
23686 the type without accessing its fields.
23687
23688 Therefore caller should follow these rules:
23689 * Try to fetch any prerequisite types we may need to build this DIE type
23690 before building the type and calling set_die_type.
23691 * After building type call set_die_type for current DIE as soon as
23692 possible before fetching more types to complete the current type.
23693 * Make the type as complete as possible before fetching more types. */
23694
23695 static struct type *
23696 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23697 {
23698 struct dwarf2_per_objfile *dwarf2_per_objfile
23699 = cu->per_cu->dwarf2_per_objfile;
23700 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23701 struct objfile *objfile = dwarf2_per_objfile->objfile;
23702 struct attribute *attr;
23703 struct dynamic_prop prop;
23704
23705 /* For Ada types, make sure that the gnat-specific data is always
23706 initialized (if not already set). There are a few types where
23707 we should not be doing so, because the type-specific area is
23708 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23709 where the type-specific area is used to store the floatformat).
23710 But this is not a problem, because the gnat-specific information
23711 is actually not needed for these types. */
23712 if (need_gnat_info (cu)
23713 && type->code () != TYPE_CODE_FUNC
23714 && type->code () != TYPE_CODE_FLT
23715 && type->code () != TYPE_CODE_METHODPTR
23716 && type->code () != TYPE_CODE_MEMBERPTR
23717 && type->code () != TYPE_CODE_METHOD
23718 && !HAVE_GNAT_AUX_INFO (type))
23719 INIT_GNAT_SPECIFIC (type);
23720
23721 /* Read DW_AT_allocated and set in type. */
23722 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23723 if (attr != NULL && attr->form_is_block ())
23724 {
23725 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23726 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23727 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23728 }
23729 else if (attr != NULL)
23730 {
23731 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23732 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23733 sect_offset_str (die->sect_off));
23734 }
23735
23736 /* Read DW_AT_associated and set in type. */
23737 attr = dwarf2_attr (die, DW_AT_associated, cu);
23738 if (attr != NULL && attr->form_is_block ())
23739 {
23740 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23741 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23742 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23743 }
23744 else if (attr != NULL)
23745 {
23746 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23747 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23748 sect_offset_str (die->sect_off));
23749 }
23750
23751 /* Read DW_AT_data_location and set in type. */
23752 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23753 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23754 cu->per_cu->addr_type ()))
23755 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23756
23757 if (dwarf2_per_objfile->die_type_hash == NULL)
23758 dwarf2_per_objfile->die_type_hash
23759 = htab_up (htab_create_alloc (127,
23760 per_cu_offset_and_type_hash,
23761 per_cu_offset_and_type_eq,
23762 NULL, xcalloc, xfree));
23763
23764 ofs.per_cu = cu->per_cu;
23765 ofs.sect_off = die->sect_off;
23766 ofs.type = type;
23767 slot = (struct dwarf2_per_cu_offset_and_type **)
23768 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23769 if (*slot)
23770 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23771 sect_offset_str (die->sect_off));
23772 *slot = XOBNEW (&objfile->objfile_obstack,
23773 struct dwarf2_per_cu_offset_and_type);
23774 **slot = ofs;
23775 return type;
23776 }
23777
23778 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23779 or return NULL if the die does not have a saved type. */
23780
23781 static struct type *
23782 get_die_type_at_offset (sect_offset sect_off,
23783 struct dwarf2_per_cu_data *per_cu)
23784 {
23785 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23786 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23787
23788 if (dwarf2_per_objfile->die_type_hash == NULL)
23789 return NULL;
23790
23791 ofs.per_cu = per_cu;
23792 ofs.sect_off = sect_off;
23793 slot = ((struct dwarf2_per_cu_offset_and_type *)
23794 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23795 if (slot)
23796 return slot->type;
23797 else
23798 return NULL;
23799 }
23800
23801 /* Look up the type for DIE in CU in die_type_hash,
23802 or return NULL if DIE does not have a saved type. */
23803
23804 static struct type *
23805 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23806 {
23807 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23808 }
23809
23810 /* Add a dependence relationship from CU to REF_PER_CU. */
23811
23812 static void
23813 dwarf2_add_dependence (struct dwarf2_cu *cu,
23814 struct dwarf2_per_cu_data *ref_per_cu)
23815 {
23816 void **slot;
23817
23818 if (cu->dependencies == NULL)
23819 cu->dependencies
23820 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23821 NULL, &cu->comp_unit_obstack,
23822 hashtab_obstack_allocate,
23823 dummy_obstack_deallocate);
23824
23825 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23826 if (*slot == NULL)
23827 *slot = ref_per_cu;
23828 }
23829
23830 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23831 Set the mark field in every compilation unit in the
23832 cache that we must keep because we are keeping CU. */
23833
23834 static int
23835 dwarf2_mark_helper (void **slot, void *data)
23836 {
23837 struct dwarf2_per_cu_data *per_cu;
23838
23839 per_cu = (struct dwarf2_per_cu_data *) *slot;
23840
23841 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23842 reading of the chain. As such dependencies remain valid it is not much
23843 useful to track and undo them during QUIT cleanups. */
23844 if (per_cu->cu == NULL)
23845 return 1;
23846
23847 if (per_cu->cu->mark)
23848 return 1;
23849 per_cu->cu->mark = true;
23850
23851 if (per_cu->cu->dependencies != NULL)
23852 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23853
23854 return 1;
23855 }
23856
23857 /* Set the mark field in CU and in every other compilation unit in the
23858 cache that we must keep because we are keeping CU. */
23859
23860 static void
23861 dwarf2_mark (struct dwarf2_cu *cu)
23862 {
23863 if (cu->mark)
23864 return;
23865 cu->mark = true;
23866 if (cu->dependencies != NULL)
23867 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23868 }
23869
23870 static void
23871 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23872 {
23873 while (per_cu)
23874 {
23875 per_cu->cu->mark = false;
23876 per_cu = per_cu->cu->read_in_chain;
23877 }
23878 }
23879
23880 /* Trivial hash function for partial_die_info: the hash value of a DIE
23881 is its offset in .debug_info for this objfile. */
23882
23883 static hashval_t
23884 partial_die_hash (const void *item)
23885 {
23886 const struct partial_die_info *part_die
23887 = (const struct partial_die_info *) item;
23888
23889 return to_underlying (part_die->sect_off);
23890 }
23891
23892 /* Trivial comparison function for partial_die_info structures: two DIEs
23893 are equal if they have the same offset. */
23894
23895 static int
23896 partial_die_eq (const void *item_lhs, const void *item_rhs)
23897 {
23898 const struct partial_die_info *part_die_lhs
23899 = (const struct partial_die_info *) item_lhs;
23900 const struct partial_die_info *part_die_rhs
23901 = (const struct partial_die_info *) item_rhs;
23902
23903 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23904 }
23905
23906 struct cmd_list_element *set_dwarf_cmdlist;
23907 struct cmd_list_element *show_dwarf_cmdlist;
23908
23909 static void
23910 show_check_physname (struct ui_file *file, int from_tty,
23911 struct cmd_list_element *c, const char *value)
23912 {
23913 fprintf_filtered (file,
23914 _("Whether to check \"physname\" is %s.\n"),
23915 value);
23916 }
23917
23918 void _initialize_dwarf2_read ();
23919 void
23920 _initialize_dwarf2_read ()
23921 {
23922 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23923 Set DWARF specific variables.\n\
23924 Configure DWARF variables such as the cache size."),
23925 &set_dwarf_cmdlist, "maintenance set dwarf ",
23926 0/*allow-unknown*/, &maintenance_set_cmdlist);
23927
23928 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23929 Show DWARF specific variables.\n\
23930 Show DWARF variables such as the cache size."),
23931 &show_dwarf_cmdlist, "maintenance show dwarf ",
23932 0/*allow-unknown*/, &maintenance_show_cmdlist);
23933
23934 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23935 &dwarf_max_cache_age, _("\
23936 Set the upper bound on the age of cached DWARF compilation units."), _("\
23937 Show the upper bound on the age of cached DWARF compilation units."), _("\
23938 A higher limit means that cached compilation units will be stored\n\
23939 in memory longer, and more total memory will be used. Zero disables\n\
23940 caching, which can slow down startup."),
23941 NULL,
23942 show_dwarf_max_cache_age,
23943 &set_dwarf_cmdlist,
23944 &show_dwarf_cmdlist);
23945
23946 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23947 Set debugging of the DWARF reader."), _("\
23948 Show debugging of the DWARF reader."), _("\
23949 When enabled (non-zero), debugging messages are printed during DWARF\n\
23950 reading and symtab expansion. A value of 1 (one) provides basic\n\
23951 information. A value greater than 1 provides more verbose information."),
23952 NULL,
23953 NULL,
23954 &setdebuglist, &showdebuglist);
23955
23956 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23957 Set debugging of the DWARF DIE reader."), _("\
23958 Show debugging of the DWARF DIE reader."), _("\
23959 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23960 The value is the maximum depth to print."),
23961 NULL,
23962 NULL,
23963 &setdebuglist, &showdebuglist);
23964
23965 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23966 Set debugging of the dwarf line reader."), _("\
23967 Show debugging of the dwarf line reader."), _("\
23968 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23969 A value of 1 (one) provides basic information.\n\
23970 A value greater than 1 provides more verbose information."),
23971 NULL,
23972 NULL,
23973 &setdebuglist, &showdebuglist);
23974
23975 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23976 Set cross-checking of \"physname\" code against demangler."), _("\
23977 Show cross-checking of \"physname\" code against demangler."), _("\
23978 When enabled, GDB's internal \"physname\" code is checked against\n\
23979 the demangler."),
23980 NULL, show_check_physname,
23981 &setdebuglist, &showdebuglist);
23982
23983 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23984 no_class, &use_deprecated_index_sections, _("\
23985 Set whether to use deprecated gdb_index sections."), _("\
23986 Show whether to use deprecated gdb_index sections."), _("\
23987 When enabled, deprecated .gdb_index sections are used anyway.\n\
23988 Normally they are ignored either because of a missing feature or\n\
23989 performance issue.\n\
23990 Warning: This option must be enabled before gdb reads the file."),
23991 NULL,
23992 NULL,
23993 &setlist, &showlist);
23994
23995 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23996 &dwarf2_locexpr_funcs);
23997 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23998 &dwarf2_loclist_funcs);
23999
24000 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24001 &dwarf2_block_frame_base_locexpr_funcs);
24002 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24003 &dwarf2_block_frame_base_loclist_funcs);
24004
24005 #if GDB_SELF_TEST
24006 selftests::register_test ("dw2_expand_symtabs_matching",
24007 selftests::dw2_expand_symtabs_matching::run_test);
24008 selftests::register_test ("dwarf2_find_containing_comp_unit",
24009 selftests::find_containing_comp_unit::run_test);
24010 #endif
24011 }