]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
294afc9a217cf597ec22c1a1b397d7e56d65ea6d
[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 (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
404 ~dwarf2_cu ();
405
406 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
407
408 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
409 Create the set of symtabs used by this TU, or if this TU is sharing
410 symtabs with another TU and the symtabs have already been created
411 then restore those symtabs in the line header.
412 We don't need the pc/line-number mapping for type units. */
413 void setup_type_unit_groups (struct die_info *die);
414
415 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
416 buildsym_compunit constructor. */
417 struct compunit_symtab *start_symtab (const char *name,
418 const char *comp_dir,
419 CORE_ADDR low_pc);
420
421 /* Reset the builder. */
422 void reset_builder () { m_builder.reset (); }
423
424 /* The header of the compilation unit. */
425 struct comp_unit_head header {};
426
427 /* Base address of this compilation unit. */
428 gdb::optional<CORE_ADDR> base_address;
429
430 /* The language we are debugging. */
431 enum language language = language_unknown;
432 const struct language_defn *language_defn = nullptr;
433
434 const char *producer = nullptr;
435
436 private:
437 /* The symtab builder for this CU. This is only non-NULL when full
438 symbols are being read. */
439 std::unique_ptr<buildsym_compunit> m_builder;
440
441 public:
442 /* The generic symbol table building routines have separate lists for
443 file scope symbols and all all other scopes (local scopes). So
444 we need to select the right one to pass to add_symbol_to_list().
445 We do it by keeping a pointer to the correct list in list_in_scope.
446
447 FIXME: The original dwarf code just treated the file scope as the
448 first local scope, and all other local scopes as nested local
449 scopes, and worked fine. Check to see if we really need to
450 distinguish these in buildsym.c. */
451 struct pending **list_in_scope = nullptr;
452
453 /* Hash table holding all the loaded partial DIEs
454 with partial_die->offset.SECT_OFF as hash. */
455 htab_t partial_dies = nullptr;
456
457 /* Storage for things with the same lifetime as this read-in compilation
458 unit, including partial DIEs. */
459 auto_obstack comp_unit_obstack;
460
461 /* When multiple dwarf2_cu structures are living in memory, this field
462 chains them all together, so that they can be released efficiently.
463 We will probably also want a generation counter so that most-recently-used
464 compilation units are cached... */
465 struct dwarf2_per_cu_data *read_in_chain = nullptr;
466
467 /* Backlink to our per_cu entry. */
468 struct dwarf2_per_cu_data *per_cu;
469
470 /* The dwarf2_per_objfile that owns this. */
471 struct dwarf2_per_objfile *per_objfile;
472
473 /* How many compilation units ago was this CU last referenced? */
474 int last_used = 0;
475
476 /* A hash table of DIE cu_offset for following references with
477 die_info->offset.sect_off as hash. */
478 htab_t die_hash = nullptr;
479
480 /* Full DIEs if read in. */
481 struct die_info *dies = nullptr;
482
483 /* A set of pointers to dwarf2_per_cu_data objects for compilation
484 units referenced by this one. Only set during full symbol processing;
485 partial symbol tables do not have dependencies. */
486 htab_t dependencies = nullptr;
487
488 /* Header data from the line table, during full symbol processing. */
489 struct line_header *line_header = nullptr;
490 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
491 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
492 this is the DW_TAG_compile_unit die for this CU. We'll hold on
493 to the line header as long as this DIE is being processed. See
494 process_die_scope. */
495 die_info *line_header_die_owner = nullptr;
496
497 /* A list of methods which need to have physnames computed
498 after all type information has been read. */
499 std::vector<delayed_method_info> method_list;
500
501 /* To be copied to symtab->call_site_htab. */
502 htab_t call_site_htab = nullptr;
503
504 /* Non-NULL if this CU came from a DWO file.
505 There is an invariant here that is important to remember:
506 Except for attributes copied from the top level DIE in the "main"
507 (or "stub") file in preparation for reading the DWO file
508 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
509 Either there isn't a DWO file (in which case this is NULL and the point
510 is moot), or there is and either we're not going to read it (in which
511 case this is NULL) or there is and we are reading it (in which case this
512 is non-NULL). */
513 struct dwo_unit *dwo_unit = nullptr;
514
515 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE. */
517 gdb::optional<ULONGEST> addr_base;
518
519 /* The DW_AT_rnglists_base attribute if present.
520 Note this value comes from the Fission stub CU/TU's DIE.
521 Also note that the value is zero in the non-DWO case so this value can
522 be used without needing to know whether DWO files are in use or not.
523 N.B. This does not apply to DW_AT_ranges appearing in
524 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
525 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
526 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
527 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
528 ULONGEST ranges_base = 0;
529
530 /* The DW_AT_loclists_base attribute if present. */
531 ULONGEST loclist_base = 0;
532
533 /* When reading debug info generated by older versions of rustc, we
534 have to rewrite some union types to be struct types with a
535 variant part. This rewriting must be done after the CU is fully
536 read in, because otherwise at the point of rewriting some struct
537 type might not have been fully processed. So, we keep a list of
538 all such types here and process them after expansion. */
539 std::vector<struct type *> rust_unions;
540
541 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
542 files, the value is implicitly zero. For DWARF 5 version DWO files, the
543 value is often implicit and is the size of the header of
544 .debug_str_offsets section (8 or 4, depending on the address size). */
545 gdb::optional<ULONGEST> str_offsets_base;
546
547 /* Mark used when releasing cached dies. */
548 bool mark : 1;
549
550 /* This CU references .debug_loc. See the symtab->locations_valid field.
551 This test is imperfect as there may exist optimized debug code not using
552 any location list and still facing inlining issues if handled as
553 unoptimized code. For a future better test see GCC PR other/32998. */
554 bool has_loclist : 1;
555
556 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
557 if all the producer_is_* fields are valid. This information is cached
558 because profiling CU expansion showed excessive time spent in
559 producer_is_gxx_lt_4_6. */
560 bool checked_producer : 1;
561 bool producer_is_gxx_lt_4_6 : 1;
562 bool producer_is_gcc_lt_4_3 : 1;
563 bool producer_is_icc : 1;
564 bool producer_is_icc_lt_14 : 1;
565 bool producer_is_codewarrior : 1;
566
567 /* When true, the file that we're processing is known to have
568 debugging info for C++ namespaces. GCC 3.3.x did not produce
569 this information, but later versions do. */
570
571 bool processing_has_namespace_info : 1;
572
573 struct partial_die_info *find_partial_die (sect_offset sect_off);
574
575 /* If this CU was inherited by another CU (via specification,
576 abstract_origin, etc), this is the ancestor CU. */
577 dwarf2_cu *ancestor;
578
579 /* Get the buildsym_compunit for this CU. */
580 buildsym_compunit *get_builder ()
581 {
582 /* If this CU has a builder associated with it, use that. */
583 if (m_builder != nullptr)
584 return m_builder.get ();
585
586 /* Otherwise, search ancestors for a valid builder. */
587 if (ancestor != nullptr)
588 return ancestor->get_builder ();
589
590 return nullptr;
591 }
592 };
593
594 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
595 This includes type_unit_group and quick_file_names. */
596
597 struct stmt_list_hash
598 {
599 /* The DWO unit this table is from or NULL if there is none. */
600 struct dwo_unit *dwo_unit;
601
602 /* Offset in .debug_line or .debug_line.dwo. */
603 sect_offset line_sect_off;
604 };
605
606 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
607 an object of this type. */
608
609 struct type_unit_group
610 {
611 /* dwarf2read.c's main "handle" on a TU symtab.
612 To simplify things we create an artificial CU that "includes" all the
613 type units using this stmt_list so that the rest of the code still has
614 a "per_cu" handle on the symtab. */
615 struct dwarf2_per_cu_data per_cu;
616
617 /* The TUs that share this DW_AT_stmt_list entry.
618 This is added to while parsing type units to build partial symtabs,
619 and is deleted afterwards and not used again. */
620 std::vector<signatured_type *> *tus;
621
622 /* The compunit symtab.
623 Type units in a group needn't all be defined in the same source file,
624 so we create an essentially anonymous symtab as the compunit symtab. */
625 struct compunit_symtab *compunit_symtab;
626
627 /* The data used to construct the hash key. */
628 struct stmt_list_hash hash;
629
630 /* The symbol tables for this TU (obtained from the files listed in
631 DW_AT_stmt_list).
632 WARNING: The order of entries here must match the order of entries
633 in the line header. After the first TU using this type_unit_group, the
634 line header for the subsequent TUs is recreated from this. This is done
635 because we need to use the same symtabs for each TU using the same
636 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
637 there's no guarantee the line header doesn't have duplicate entries. */
638 struct symtab **symtabs;
639 };
640
641 /* These sections are what may appear in a (real or virtual) DWO file. */
642
643 struct dwo_sections
644 {
645 struct dwarf2_section_info abbrev;
646 struct dwarf2_section_info line;
647 struct dwarf2_section_info loc;
648 struct dwarf2_section_info loclists;
649 struct dwarf2_section_info macinfo;
650 struct dwarf2_section_info macro;
651 struct dwarf2_section_info str;
652 struct dwarf2_section_info str_offsets;
653 /* In the case of a virtual DWO file, these two are unused. */
654 struct dwarf2_section_info info;
655 std::vector<dwarf2_section_info> types;
656 };
657
658 /* CUs/TUs in DWP/DWO files. */
659
660 struct dwo_unit
661 {
662 /* Backlink to the containing struct dwo_file. */
663 struct dwo_file *dwo_file;
664
665 /* The "id" that distinguishes this CU/TU.
666 .debug_info calls this "dwo_id", .debug_types calls this "signature".
667 Since signatures came first, we stick with it for consistency. */
668 ULONGEST signature;
669
670 /* The section this CU/TU lives in, in the DWO file. */
671 struct dwarf2_section_info *section;
672
673 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
674 sect_offset sect_off;
675 unsigned int length;
676
677 /* For types, offset in the type's DIE of the type defined by this TU. */
678 cu_offset type_offset_in_tu;
679 };
680
681 /* include/dwarf2.h defines the DWP section codes.
682 It defines a max value but it doesn't define a min value, which we
683 use for error checking, so provide one. */
684
685 enum dwp_v2_section_ids
686 {
687 DW_SECT_MIN = 1
688 };
689
690 /* Data for one DWO file.
691
692 This includes virtual DWO files (a virtual DWO file is a DWO file as it
693 appears in a DWP file). DWP files don't really have DWO files per se -
694 comdat folding of types "loses" the DWO file they came from, and from
695 a high level view DWP files appear to contain a mass of random types.
696 However, to maintain consistency with the non-DWP case we pretend DWP
697 files contain virtual DWO files, and we assign each TU with one virtual
698 DWO file (generally based on the line and abbrev section offsets -
699 a heuristic that seems to work in practice). */
700
701 struct dwo_file
702 {
703 dwo_file () = default;
704 DISABLE_COPY_AND_ASSIGN (dwo_file);
705
706 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
707 For virtual DWO files the name is constructed from the section offsets
708 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
709 from related CU+TUs. */
710 const char *dwo_name = nullptr;
711
712 /* The DW_AT_comp_dir attribute. */
713 const char *comp_dir = nullptr;
714
715 /* The bfd, when the file is open. Otherwise this is NULL.
716 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
717 gdb_bfd_ref_ptr dbfd;
718
719 /* The sections that make up this DWO file.
720 Remember that for virtual DWO files in DWP V2, these are virtual
721 sections (for lack of a better name). */
722 struct dwo_sections sections {};
723
724 /* The CUs in the file.
725 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
726 an extension to handle LLVM's Link Time Optimization output (where
727 multiple source files may be compiled into a single object/dwo pair). */
728 htab_up cus;
729
730 /* Table of TUs in the file.
731 Each element is a struct dwo_unit. */
732 htab_up tus;
733 };
734
735 /* These sections are what may appear in a DWP file. */
736
737 struct dwp_sections
738 {
739 /* These are used by both DWP version 1 and 2. */
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
743
744 /* These are only used by DWP version 2 files.
745 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
746 sections are referenced by section number, and are not recorded here.
747 In DWP version 2 there is at most one copy of all these sections, each
748 section being (effectively) comprised of the concatenation of all of the
749 individual sections that exist in the version 1 format.
750 To keep the code simple we treat each of these concatenated pieces as a
751 section itself (a virtual section?). */
752 struct dwarf2_section_info abbrev;
753 struct dwarf2_section_info info;
754 struct dwarf2_section_info line;
755 struct dwarf2_section_info loc;
756 struct dwarf2_section_info macinfo;
757 struct dwarf2_section_info macro;
758 struct dwarf2_section_info str_offsets;
759 struct dwarf2_section_info types;
760 };
761
762 /* These sections are what may appear in a virtual DWO file in DWP version 1.
763 A virtual DWO file is a DWO file as it appears in a DWP file. */
764
765 struct virtual_v1_dwo_sections
766 {
767 struct dwarf2_section_info abbrev;
768 struct dwarf2_section_info line;
769 struct dwarf2_section_info loc;
770 struct dwarf2_section_info macinfo;
771 struct dwarf2_section_info macro;
772 struct dwarf2_section_info str_offsets;
773 /* Each DWP hash table entry records one CU or one TU.
774 That is recorded here, and copied to dwo_unit.section. */
775 struct dwarf2_section_info info_or_types;
776 };
777
778 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
779 In version 2, the sections of the DWO files are concatenated together
780 and stored in one section of that name. Thus each ELF section contains
781 several "virtual" sections. */
782
783 struct virtual_v2_dwo_sections
784 {
785 bfd_size_type abbrev_offset;
786 bfd_size_type abbrev_size;
787
788 bfd_size_type line_offset;
789 bfd_size_type line_size;
790
791 bfd_size_type loc_offset;
792 bfd_size_type loc_size;
793
794 bfd_size_type macinfo_offset;
795 bfd_size_type macinfo_size;
796
797 bfd_size_type macro_offset;
798 bfd_size_type macro_size;
799
800 bfd_size_type str_offsets_offset;
801 bfd_size_type str_offsets_size;
802
803 /* Each DWP hash table entry records one CU or one TU.
804 That is recorded here, and copied to dwo_unit.section. */
805 bfd_size_type info_or_types_offset;
806 bfd_size_type info_or_types_size;
807 };
808
809 /* Contents of DWP hash tables. */
810
811 struct dwp_hash_table
812 {
813 uint32_t version, nr_columns;
814 uint32_t nr_units, nr_slots;
815 const gdb_byte *hash_table, *unit_table;
816 union
817 {
818 struct
819 {
820 const gdb_byte *indices;
821 } v1;
822 struct
823 {
824 /* This is indexed by column number and gives the id of the section
825 in that column. */
826 #define MAX_NR_V2_DWO_SECTIONS \
827 (1 /* .debug_info or .debug_types */ \
828 + 1 /* .debug_abbrev */ \
829 + 1 /* .debug_line */ \
830 + 1 /* .debug_loc */ \
831 + 1 /* .debug_str_offsets */ \
832 + 1 /* .debug_macro or .debug_macinfo */)
833 int section_ids[MAX_NR_V2_DWO_SECTIONS];
834 const gdb_byte *offsets;
835 const gdb_byte *sizes;
836 } v2;
837 } section_pool;
838 };
839
840 /* Data for one DWP file. */
841
842 struct dwp_file
843 {
844 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
845 : name (name_),
846 dbfd (std::move (abfd))
847 {
848 }
849
850 /* Name of the file. */
851 const char *name;
852
853 /* File format version. */
854 int version = 0;
855
856 /* The bfd. */
857 gdb_bfd_ref_ptr dbfd;
858
859 /* Section info for this file. */
860 struct dwp_sections sections {};
861
862 /* Table of CUs in the file. */
863 const struct dwp_hash_table *cus = nullptr;
864
865 /* Table of TUs in the file. */
866 const struct dwp_hash_table *tus = nullptr;
867
868 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
869 htab_up loaded_cus;
870 htab_up loaded_tus;
871
872 /* Table to map ELF section numbers to their sections.
873 This is only needed for the DWP V1 file format. */
874 unsigned int num_sections = 0;
875 asection **elf_sections = nullptr;
876 };
877
878 /* Struct used to pass misc. parameters to read_die_and_children, et
879 al. which are used for both .debug_info and .debug_types dies.
880 All parameters here are unchanging for the life of the call. This
881 struct exists to abstract away the constant parameters of die reading. */
882
883 struct die_reader_specs
884 {
885 /* The bfd of die_section. */
886 bfd* abfd;
887
888 /* The CU of the DIE we are parsing. */
889 struct dwarf2_cu *cu;
890
891 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
892 struct dwo_file *dwo_file;
893
894 /* The section the die comes from.
895 This is either .debug_info or .debug_types, or the .dwo variants. */
896 struct dwarf2_section_info *die_section;
897
898 /* die_section->buffer. */
899 const gdb_byte *buffer;
900
901 /* The end of the buffer. */
902 const gdb_byte *buffer_end;
903
904 /* The abbreviation table to use when reading the DIEs. */
905 struct abbrev_table *abbrev_table;
906 };
907
908 /* A subclass of die_reader_specs that holds storage and has complex
909 constructor and destructor behavior. */
910
911 class cutu_reader : public die_reader_specs
912 {
913 public:
914
915 cutu_reader (struct dwarf2_per_cu_data *this_cu,
916 struct abbrev_table *abbrev_table,
917 int use_existing_cu,
918 bool skip_partial);
919
920 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
921 struct dwarf2_cu *parent_cu = nullptr,
922 struct dwo_file *dwo_file = nullptr);
923
924 DISABLE_COPY_AND_ASSIGN (cutu_reader);
925
926 const gdb_byte *info_ptr = nullptr;
927 struct die_info *comp_unit_die = nullptr;
928 bool dummy_p = false;
929
930 /* Release the new CU, putting it on the chain. This cannot be done
931 for dummy CUs. */
932 void keep ();
933
934 private:
935 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
936 dwarf2_per_objfile *per_objfile,
937 int use_existing_cu);
938
939 struct dwarf2_per_cu_data *m_this_cu;
940 std::unique_ptr<dwarf2_cu> m_new_cu;
941
942 /* The ordinary abbreviation table. */
943 abbrev_table_up m_abbrev_table_holder;
944
945 /* The DWO abbreviation table. */
946 abbrev_table_up m_dwo_abbrev_table;
947 };
948
949 /* When we construct a partial symbol table entry we only
950 need this much information. */
951 struct partial_die_info : public allocate_on_obstack
952 {
953 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
954
955 /* Disable assign but still keep copy ctor, which is needed
956 load_partial_dies. */
957 partial_die_info& operator=(const partial_die_info& rhs) = delete;
958
959 /* Adjust the partial die before generating a symbol for it. This
960 function may set the is_external flag or change the DIE's
961 name. */
962 void fixup (struct dwarf2_cu *cu);
963
964 /* Read a minimal amount of information into the minimal die
965 structure. */
966 const gdb_byte *read (const struct die_reader_specs *reader,
967 const struct abbrev_info &abbrev,
968 const gdb_byte *info_ptr);
969
970 /* Offset of this DIE. */
971 const sect_offset sect_off;
972
973 /* DWARF-2 tag for this DIE. */
974 const ENUM_BITFIELD(dwarf_tag) tag : 16;
975
976 /* Assorted flags describing the data found in this DIE. */
977 const unsigned int has_children : 1;
978
979 unsigned int is_external : 1;
980 unsigned int is_declaration : 1;
981 unsigned int has_type : 1;
982 unsigned int has_specification : 1;
983 unsigned int has_pc_info : 1;
984 unsigned int may_be_inlined : 1;
985
986 /* This DIE has been marked DW_AT_main_subprogram. */
987 unsigned int main_subprogram : 1;
988
989 /* Flag set if the SCOPE field of this structure has been
990 computed. */
991 unsigned int scope_set : 1;
992
993 /* Flag set if the DIE has a byte_size attribute. */
994 unsigned int has_byte_size : 1;
995
996 /* Flag set if the DIE has a DW_AT_const_value attribute. */
997 unsigned int has_const_value : 1;
998
999 /* Flag set if any of the DIE's children are template arguments. */
1000 unsigned int has_template_arguments : 1;
1001
1002 /* Flag set if fixup has been called on this die. */
1003 unsigned int fixup_called : 1;
1004
1005 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1006 unsigned int is_dwz : 1;
1007
1008 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1009 unsigned int spec_is_dwz : 1;
1010
1011 /* The name of this DIE. Normally the value of DW_AT_name, but
1012 sometimes a default name for unnamed DIEs. */
1013 const char *name = nullptr;
1014
1015 /* The linkage name, if present. */
1016 const char *linkage_name = nullptr;
1017
1018 /* The scope to prepend to our children. This is generally
1019 allocated on the comp_unit_obstack, so will disappear
1020 when this compilation unit leaves the cache. */
1021 const char *scope = nullptr;
1022
1023 /* Some data associated with the partial DIE. The tag determines
1024 which field is live. */
1025 union
1026 {
1027 /* The location description associated with this DIE, if any. */
1028 struct dwarf_block *locdesc;
1029 /* The offset of an import, for DW_TAG_imported_unit. */
1030 sect_offset sect_off;
1031 } d {};
1032
1033 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1034 CORE_ADDR lowpc = 0;
1035 CORE_ADDR highpc = 0;
1036
1037 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1038 DW_AT_sibling, if any. */
1039 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1040 could return DW_AT_sibling values to its caller load_partial_dies. */
1041 const gdb_byte *sibling = nullptr;
1042
1043 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1044 DW_AT_specification (or DW_AT_abstract_origin or
1045 DW_AT_extension). */
1046 sect_offset spec_offset {};
1047
1048 /* Pointers to this DIE's parent, first child, and next sibling,
1049 if any. */
1050 struct partial_die_info *die_parent = nullptr;
1051 struct partial_die_info *die_child = nullptr;
1052 struct partial_die_info *die_sibling = nullptr;
1053
1054 friend struct partial_die_info *
1055 dwarf2_cu::find_partial_die (sect_offset sect_off);
1056
1057 private:
1058 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1059 partial_die_info (sect_offset sect_off)
1060 : partial_die_info (sect_off, DW_TAG_padding, 0)
1061 {
1062 }
1063
1064 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1065 int has_children_)
1066 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1067 {
1068 is_external = 0;
1069 is_declaration = 0;
1070 has_type = 0;
1071 has_specification = 0;
1072 has_pc_info = 0;
1073 may_be_inlined = 0;
1074 main_subprogram = 0;
1075 scope_set = 0;
1076 has_byte_size = 0;
1077 has_const_value = 0;
1078 has_template_arguments = 0;
1079 fixup_called = 0;
1080 is_dwz = 0;
1081 spec_is_dwz = 0;
1082 }
1083 };
1084
1085 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1086 but this would require a corresponding change in unpack_field_as_long
1087 and friends. */
1088 static int bits_per_byte = 8;
1089
1090 struct variant_part_builder;
1091
1092 /* When reading a variant, we track a bit more information about the
1093 field, and store it in an object of this type. */
1094
1095 struct variant_field
1096 {
1097 int first_field = -1;
1098 int last_field = -1;
1099
1100 /* A variant can contain other variant parts. */
1101 std::vector<variant_part_builder> variant_parts;
1102
1103 /* If we see a DW_TAG_variant, then this will be set if this is the
1104 default branch. */
1105 bool default_branch = false;
1106 /* If we see a DW_AT_discr_value, then this will be the discriminant
1107 value. */
1108 ULONGEST discriminant_value = 0;
1109 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1110 data. */
1111 struct dwarf_block *discr_list_data = nullptr;
1112 };
1113
1114 /* This represents a DW_TAG_variant_part. */
1115
1116 struct variant_part_builder
1117 {
1118 /* The offset of the discriminant field. */
1119 sect_offset discriminant_offset {};
1120
1121 /* Variants that are direct children of this variant part. */
1122 std::vector<variant_field> variants;
1123
1124 /* True if we're currently reading a variant. */
1125 bool processing_variant = false;
1126 };
1127
1128 struct nextfield
1129 {
1130 int accessibility = 0;
1131 int virtuality = 0;
1132 /* Variant parts need to find the discriminant, which is a DIE
1133 reference. We track the section offset of each field to make
1134 this link. */
1135 sect_offset offset;
1136 struct field field {};
1137 };
1138
1139 struct fnfieldlist
1140 {
1141 const char *name = nullptr;
1142 std::vector<struct fn_field> fnfields;
1143 };
1144
1145 /* The routines that read and process dies for a C struct or C++ class
1146 pass lists of data member fields and lists of member function fields
1147 in an instance of a field_info structure, as defined below. */
1148 struct field_info
1149 {
1150 /* List of data member and baseclasses fields. */
1151 std::vector<struct nextfield> fields;
1152 std::vector<struct nextfield> baseclasses;
1153
1154 /* Set if the accessibility of one of the fields is not public. */
1155 int non_public_fields = 0;
1156
1157 /* Member function fieldlist array, contains name of possibly overloaded
1158 member function, number of overloaded member functions and a pointer
1159 to the head of the member function field chain. */
1160 std::vector<struct fnfieldlist> fnfieldlists;
1161
1162 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1163 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1164 std::vector<struct decl_field> typedef_field_list;
1165
1166 /* Nested types defined by this class and the number of elements in this
1167 list. */
1168 std::vector<struct decl_field> nested_types_list;
1169
1170 /* If non-null, this is the variant part we are currently
1171 reading. */
1172 variant_part_builder *current_variant_part = nullptr;
1173 /* This holds all the top-level variant parts attached to the type
1174 we're reading. */
1175 std::vector<variant_part_builder> variant_parts;
1176
1177 /* Return the total number of fields (including baseclasses). */
1178 int nfields () const
1179 {
1180 return fields.size () + baseclasses.size ();
1181 }
1182 };
1183
1184 /* Loaded secondary compilation units are kept in memory until they
1185 have not been referenced for the processing of this many
1186 compilation units. Set this to zero to disable caching. Cache
1187 sizes of up to at least twenty will improve startup time for
1188 typical inter-CU-reference binaries, at an obvious memory cost. */
1189 static int dwarf_max_cache_age = 5;
1190 static void
1191 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1192 struct cmd_list_element *c, const char *value)
1193 {
1194 fprintf_filtered (file, _("The upper bound on the age of cached "
1195 "DWARF compilation units is %s.\n"),
1196 value);
1197 }
1198 \f
1199 /* local function prototypes */
1200
1201 static void dwarf2_find_base_address (struct die_info *die,
1202 struct dwarf2_cu *cu);
1203
1204 static dwarf2_psymtab *create_partial_symtab
1205 (struct dwarf2_per_cu_data *per_cu, const char *name);
1206
1207 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1208 const gdb_byte *info_ptr,
1209 struct die_info *type_unit_die);
1210
1211 static void dwarf2_build_psymtabs_hard
1212 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1213
1214 static void scan_partial_symbols (struct partial_die_info *,
1215 CORE_ADDR *, CORE_ADDR *,
1216 int, struct dwarf2_cu *);
1217
1218 static void add_partial_symbol (struct partial_die_info *,
1219 struct dwarf2_cu *);
1220
1221 static void add_partial_namespace (struct partial_die_info *pdi,
1222 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1223 int set_addrmap, struct dwarf2_cu *cu);
1224
1225 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1226 CORE_ADDR *highpc, int set_addrmap,
1227 struct dwarf2_cu *cu);
1228
1229 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1230 struct dwarf2_cu *cu);
1231
1232 static void add_partial_subprogram (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1234 int need_pc, struct dwarf2_cu *cu);
1235
1236 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1237
1238 static struct partial_die_info *load_partial_dies
1239 (const struct die_reader_specs *, const gdb_byte *, int);
1240
1241 /* A pair of partial_die_info and compilation unit. */
1242 struct cu_partial_die_info
1243 {
1244 /* The compilation unit of the partial_die_info. */
1245 struct dwarf2_cu *cu;
1246 /* A partial_die_info. */
1247 struct partial_die_info *pdi;
1248
1249 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1250 : cu (cu),
1251 pdi (pdi)
1252 { /* Nothing. */ }
1253
1254 private:
1255 cu_partial_die_info () = delete;
1256 };
1257
1258 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1259 struct dwarf2_cu *);
1260
1261 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1262 struct attribute *, struct attr_abbrev *,
1263 const gdb_byte *, bool *need_reprocess);
1264
1265 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1266 struct attribute *attr);
1267
1268 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1269
1270 static sect_offset read_abbrev_offset
1271 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1272 struct dwarf2_section_info *, sect_offset);
1273
1274 static const char *read_indirect_string
1275 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1276 const struct comp_unit_head *, unsigned int *);
1277
1278 static const char *read_indirect_string_at_offset
1279 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1280
1281 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1282 const gdb_byte *,
1283 unsigned int *);
1284
1285 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1286 ULONGEST str_index);
1287
1288 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1289 ULONGEST str_index);
1290
1291 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1292
1293 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1294 struct dwarf2_cu *);
1295
1296 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1297 struct dwarf2_cu *cu);
1298
1299 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1300
1301 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1302 struct dwarf2_cu *cu);
1303
1304 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1305
1306 static struct die_info *die_specification (struct die_info *die,
1307 struct dwarf2_cu **);
1308
1309 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1310 struct dwarf2_cu *cu);
1311
1312 static void dwarf_decode_lines (struct line_header *, const char *,
1313 struct dwarf2_cu *, dwarf2_psymtab *,
1314 CORE_ADDR, int decode_mapping);
1315
1316 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1317 const char *);
1318
1319 static struct symbol *new_symbol (struct die_info *, struct type *,
1320 struct dwarf2_cu *, struct symbol * = NULL);
1321
1322 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1323 struct dwarf2_cu *);
1324
1325 static void dwarf2_const_value_attr (const struct attribute *attr,
1326 struct type *type,
1327 const char *name,
1328 struct obstack *obstack,
1329 struct dwarf2_cu *cu, LONGEST *value,
1330 const gdb_byte **bytes,
1331 struct dwarf2_locexpr_baton **baton);
1332
1333 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1334
1335 static int need_gnat_info (struct dwarf2_cu *);
1336
1337 static struct type *die_descriptive_type (struct die_info *,
1338 struct dwarf2_cu *);
1339
1340 static void set_descriptive_type (struct type *, struct die_info *,
1341 struct dwarf2_cu *);
1342
1343 static struct type *die_containing_type (struct die_info *,
1344 struct dwarf2_cu *);
1345
1346 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1347 struct dwarf2_cu *);
1348
1349 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1350
1351 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1352
1353 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1354
1355 static char *typename_concat (struct obstack *obs, const char *prefix,
1356 const char *suffix, int physname,
1357 struct dwarf2_cu *cu);
1358
1359 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1360
1361 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1362
1363 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1364
1365 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1366
1367 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1368
1369 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1370
1371 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1372 struct dwarf2_cu *, dwarf2_psymtab *);
1373
1374 /* Return the .debug_loclists section to use for cu. */
1375 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1376
1377 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1378 values. Keep the items ordered with increasing constraints compliance. */
1379 enum pc_bounds_kind
1380 {
1381 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1382 PC_BOUNDS_NOT_PRESENT,
1383
1384 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1385 were present but they do not form a valid range of PC addresses. */
1386 PC_BOUNDS_INVALID,
1387
1388 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1389 PC_BOUNDS_RANGES,
1390
1391 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1392 PC_BOUNDS_HIGH_LOW,
1393 };
1394
1395 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *,
1398 dwarf2_psymtab *);
1399
1400 static void get_scope_pc_bounds (struct die_info *,
1401 CORE_ADDR *, CORE_ADDR *,
1402 struct dwarf2_cu *);
1403
1404 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1405 CORE_ADDR, struct dwarf2_cu *);
1406
1407 static void dwarf2_add_field (struct field_info *, struct die_info *,
1408 struct dwarf2_cu *);
1409
1410 static void dwarf2_attach_fields_to_type (struct field_info *,
1411 struct type *, struct dwarf2_cu *);
1412
1413 static void dwarf2_add_member_fn (struct field_info *,
1414 struct die_info *, struct type *,
1415 struct dwarf2_cu *);
1416
1417 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1418 struct type *,
1419 struct dwarf2_cu *);
1420
1421 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1422
1423 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1424
1425 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1426
1427 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1428
1429 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1430
1431 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1432
1433 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1434
1435 static struct type *read_module_type (struct die_info *die,
1436 struct dwarf2_cu *cu);
1437
1438 static const char *namespace_name (struct die_info *die,
1439 int *is_anonymous, struct dwarf2_cu *);
1440
1441 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1442
1443 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1444 bool * = nullptr);
1445
1446 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1447 struct dwarf2_cu *);
1448
1449 static struct die_info *read_die_and_siblings_1
1450 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1451 struct die_info *);
1452
1453 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1454 const gdb_byte *info_ptr,
1455 const gdb_byte **new_info_ptr,
1456 struct die_info *parent);
1457
1458 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1459 struct die_info **, const gdb_byte *,
1460 int);
1461
1462 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1463 struct die_info **, const gdb_byte *);
1464
1465 static void process_die (struct die_info *, struct dwarf2_cu *);
1466
1467 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1468 struct objfile *);
1469
1470 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1471
1472 static const char *dwarf2_full_name (const char *name,
1473 struct die_info *die,
1474 struct dwarf2_cu *cu);
1475
1476 static const char *dwarf2_physname (const char *name, struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
1479 static struct die_info *dwarf2_extension (struct die_info *die,
1480 struct dwarf2_cu **);
1481
1482 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1483
1484 static void dump_die_for_error (struct die_info *);
1485
1486 static void dump_die_1 (struct ui_file *, int level, int max_level,
1487 struct die_info *);
1488
1489 /*static*/ void dump_die (struct die_info *, int max_level);
1490
1491 static void store_in_ref_table (struct die_info *,
1492 struct dwarf2_cu *);
1493
1494 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1495 const struct attribute *,
1496 struct dwarf2_cu **);
1497
1498 static struct die_info *follow_die_ref (struct die_info *,
1499 const struct attribute *,
1500 struct dwarf2_cu **);
1501
1502 static struct die_info *follow_die_sig (struct die_info *,
1503 const struct attribute *,
1504 struct dwarf2_cu **);
1505
1506 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1507 struct dwarf2_cu *);
1508
1509 static struct type *get_DW_AT_signature_type (struct die_info *,
1510 const struct attribute *,
1511 struct dwarf2_cu *);
1512
1513 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1514
1515 static void read_signatured_type (struct signatured_type *);
1516
1517 static int attr_to_dynamic_prop (const struct attribute *attr,
1518 struct die_info *die, struct dwarf2_cu *cu,
1519 struct dynamic_prop *prop, struct type *type);
1520
1521 /* memory allocation interface */
1522
1523 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1524
1525 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1526
1527 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1528
1529 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1530 struct dwarf2_loclist_baton *baton,
1531 const struct attribute *attr);
1532
1533 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1534 struct symbol *sym,
1535 struct dwarf2_cu *cu,
1536 int is_block);
1537
1538 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1539 const gdb_byte *info_ptr,
1540 struct abbrev_info *abbrev);
1541
1542 static hashval_t partial_die_hash (const void *item);
1543
1544 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1545
1546 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1547 (sect_offset sect_off, unsigned int offset_in_dwz,
1548 struct dwarf2_per_objfile *dwarf2_per_objfile);
1549
1550 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1551 struct die_info *comp_unit_die,
1552 enum language pretend_language);
1553
1554 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1555
1556 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1557
1558 static struct type *set_die_type (struct die_info *, struct type *,
1559 struct dwarf2_cu *);
1560
1561 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1562
1563 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1564
1565 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1566 enum language);
1567
1568 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1569 enum language);
1570
1571 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1572 enum language);
1573
1574 static void dwarf2_add_dependence (struct dwarf2_cu *,
1575 struct dwarf2_per_cu_data *);
1576
1577 static void dwarf2_mark (struct dwarf2_cu *);
1578
1579 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1580
1581 static struct type *get_die_type_at_offset (sect_offset,
1582 struct dwarf2_per_cu_data *);
1583
1584 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1585
1586 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1587 enum language pretend_language);
1588
1589 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1590
1591 /* Class, the destructor of which frees all allocated queue entries. This
1592 will only have work to do if an error was thrown while processing the
1593 dwarf. If no error was thrown then the queue entries should have all
1594 been processed, and freed, as we went along. */
1595
1596 class dwarf2_queue_guard
1597 {
1598 public:
1599 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1600 : m_per_objfile (per_objfile)
1601 {
1602 }
1603
1604 /* Free any entries remaining on the queue. There should only be
1605 entries left if we hit an error while processing the dwarf. */
1606 ~dwarf2_queue_guard ()
1607 {
1608 /* Ensure that no memory is allocated by the queue. */
1609 std::queue<dwarf2_queue_item> empty;
1610 std::swap (m_per_objfile->per_bfd->queue, empty);
1611 }
1612
1613 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1614
1615 private:
1616 dwarf2_per_objfile *m_per_objfile;
1617 };
1618
1619 dwarf2_queue_item::~dwarf2_queue_item ()
1620 {
1621 /* Anything still marked queued is likely to be in an
1622 inconsistent state, so discard it. */
1623 if (per_cu->queued)
1624 {
1625 if (per_cu->cu != NULL)
1626 free_one_cached_comp_unit (per_cu);
1627 per_cu->queued = 0;
1628 }
1629 }
1630
1631 /* The return type of find_file_and_directory. Note, the enclosed
1632 string pointers are only valid while this object is valid. */
1633
1634 struct file_and_directory
1635 {
1636 /* The filename. This is never NULL. */
1637 const char *name;
1638
1639 /* The compilation directory. NULL if not known. If we needed to
1640 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1641 points directly to the DW_AT_comp_dir string attribute owned by
1642 the obstack that owns the DIE. */
1643 const char *comp_dir;
1644
1645 /* If we needed to build a new string for comp_dir, this is what
1646 owns the storage. */
1647 std::string comp_dir_storage;
1648 };
1649
1650 static file_and_directory find_file_and_directory (struct die_info *die,
1651 struct dwarf2_cu *cu);
1652
1653 static htab_up allocate_signatured_type_table ();
1654
1655 static htab_up allocate_dwo_unit_table ();
1656
1657 static struct dwo_unit *lookup_dwo_unit_in_dwp
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1659 struct dwp_file *dwp_file, const char *comp_dir,
1660 ULONGEST signature, int is_debug_types);
1661
1662 static struct dwp_file *get_dwp_file
1663 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1664
1665 static struct dwo_unit *lookup_dwo_comp_unit
1666 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1667
1668 static struct dwo_unit *lookup_dwo_type_unit
1669 (struct signatured_type *, const char *, const char *);
1670
1671 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1672
1673 /* A unique pointer to a dwo_file. */
1674
1675 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1676
1677 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1678
1679 static void check_producer (struct dwarf2_cu *cu);
1680
1681 static void free_line_header_voidp (void *arg);
1682 \f
1683 /* Various complaints about symbol reading that don't abort the process. */
1684
1685 static void
1686 dwarf2_debug_line_missing_file_complaint (void)
1687 {
1688 complaint (_(".debug_line section has line data without a file"));
1689 }
1690
1691 static void
1692 dwarf2_debug_line_missing_end_sequence_complaint (void)
1693 {
1694 complaint (_(".debug_line section has line "
1695 "program sequence without an end"));
1696 }
1697
1698 static void
1699 dwarf2_complex_location_expr_complaint (void)
1700 {
1701 complaint (_("location expression too complex"));
1702 }
1703
1704 static void
1705 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1706 int arg3)
1707 {
1708 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1709 arg1, arg2, arg3);
1710 }
1711
1712 static void
1713 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1714 {
1715 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1716 arg1, arg2);
1717 }
1718
1719 /* Hash function for line_header_hash. */
1720
1721 static hashval_t
1722 line_header_hash (const struct line_header *ofs)
1723 {
1724 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1725 }
1726
1727 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1728
1729 static hashval_t
1730 line_header_hash_voidp (const void *item)
1731 {
1732 const struct line_header *ofs = (const struct line_header *) item;
1733
1734 return line_header_hash (ofs);
1735 }
1736
1737 /* Equality function for line_header_hash. */
1738
1739 static int
1740 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1741 {
1742 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1743 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1744
1745 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1746 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1747 }
1748
1749 \f
1750
1751 /* See declaration. */
1752
1753 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1754 bool can_copy_)
1755 : can_copy (can_copy_)
1756 {
1757 if (names == NULL)
1758 names = &dwarf2_elf_names;
1759
1760 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1761 locate_sections (obfd, sec, *names);
1762 }
1763
1764 dwarf2_per_bfd::~dwarf2_per_bfd ()
1765 {
1766 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1767 free_cached_comp_units ();
1768
1769 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1770 per_cu->imported_symtabs_free ();
1771
1772 for (signatured_type *sig_type : all_type_units)
1773 sig_type->per_cu.imported_symtabs_free ();
1774
1775 /* Everything else should be on this->obstack. */
1776 }
1777
1778 /* See declaration. */
1779
1780 void
1781 dwarf2_per_bfd::free_cached_comp_units ()
1782 {
1783 dwarf2_per_cu_data *per_cu = read_in_chain;
1784 dwarf2_per_cu_data **last_chain = &read_in_chain;
1785 while (per_cu != NULL)
1786 {
1787 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1788
1789 delete per_cu->cu;
1790 *last_chain = next_cu;
1791 per_cu = next_cu;
1792 }
1793 }
1794
1795 /* A helper class that calls free_cached_comp_units on
1796 destruction. */
1797
1798 class free_cached_comp_units
1799 {
1800 public:
1801
1802 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1803 : m_per_objfile (per_objfile)
1804 {
1805 }
1806
1807 ~free_cached_comp_units ()
1808 {
1809 m_per_objfile->per_bfd->free_cached_comp_units ();
1810 }
1811
1812 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1813
1814 private:
1815
1816 dwarf2_per_objfile *m_per_objfile;
1817 };
1818
1819 /* See read.h. */
1820
1821 bool
1822 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1823 {
1824 gdb_assert (per_cu->index < this->m_symtabs.size ());
1825
1826 return this->m_symtabs[per_cu->index] != nullptr;
1827 }
1828
1829 /* See read.h. */
1830
1831 compunit_symtab *
1832 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1833 {
1834 gdb_assert (per_cu->index < this->m_symtabs.size ());
1835
1836 return this->m_symtabs[per_cu->index];
1837 }
1838
1839 /* See read.h. */
1840
1841 void
1842 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1843 compunit_symtab *symtab)
1844 {
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1847
1848 this->m_symtabs[per_cu->index] = symtab;
1849 }
1850
1851 /* Try to locate the sections we need for DWARF 2 debugging
1852 information and return true if we have enough to do something.
1853 NAMES points to the dwarf2 section names, or is NULL if the standard
1854 ELF names are used. CAN_COPY is true for formats where symbol
1855 interposition is possible and so symbol values must follow copy
1856 relocation rules. */
1857
1858 int
1859 dwarf2_has_info (struct objfile *objfile,
1860 const struct dwarf2_debug_sections *names,
1861 bool can_copy)
1862 {
1863 if (objfile->flags & OBJF_READNEVER)
1864 return 0;
1865
1866 struct dwarf2_per_objfile *dwarf2_per_objfile
1867 = get_dwarf2_per_objfile (objfile);
1868
1869 if (dwarf2_per_objfile == NULL)
1870 {
1871 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1872 sharing yet). */
1873 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1874
1875 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1876 }
1877
1878 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1879 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1880 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1881 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1882 }
1883
1884 /* When loading sections, we look either for uncompressed section or for
1885 compressed section names. */
1886
1887 static int
1888 section_is_p (const char *section_name,
1889 const struct dwarf2_section_names *names)
1890 {
1891 if (names->normal != NULL
1892 && strcmp (section_name, names->normal) == 0)
1893 return 1;
1894 if (names->compressed != NULL
1895 && strcmp (section_name, names->compressed) == 0)
1896 return 1;
1897 return 0;
1898 }
1899
1900 /* See declaration. */
1901
1902 void
1903 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1904 const dwarf2_debug_sections &names)
1905 {
1906 flagword aflag = bfd_section_flags (sectp);
1907
1908 if ((aflag & SEC_HAS_CONTENTS) == 0)
1909 {
1910 }
1911 else if (elf_section_data (sectp)->this_hdr.sh_size
1912 > bfd_get_file_size (abfd))
1913 {
1914 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1915 warning (_("Discarding section %s which has a section size (%s"
1916 ") larger than the file size [in module %s]"),
1917 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1918 bfd_get_filename (abfd));
1919 }
1920 else if (section_is_p (sectp->name, &names.info))
1921 {
1922 this->info.s.section = sectp;
1923 this->info.size = bfd_section_size (sectp);
1924 }
1925 else if (section_is_p (sectp->name, &names.abbrev))
1926 {
1927 this->abbrev.s.section = sectp;
1928 this->abbrev.size = bfd_section_size (sectp);
1929 }
1930 else if (section_is_p (sectp->name, &names.line))
1931 {
1932 this->line.s.section = sectp;
1933 this->line.size = bfd_section_size (sectp);
1934 }
1935 else if (section_is_p (sectp->name, &names.loc))
1936 {
1937 this->loc.s.section = sectp;
1938 this->loc.size = bfd_section_size (sectp);
1939 }
1940 else if (section_is_p (sectp->name, &names.loclists))
1941 {
1942 this->loclists.s.section = sectp;
1943 this->loclists.size = bfd_section_size (sectp);
1944 }
1945 else if (section_is_p (sectp->name, &names.macinfo))
1946 {
1947 this->macinfo.s.section = sectp;
1948 this->macinfo.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.macro))
1951 {
1952 this->macro.s.section = sectp;
1953 this->macro.size = bfd_section_size (sectp);
1954 }
1955 else if (section_is_p (sectp->name, &names.str))
1956 {
1957 this->str.s.section = sectp;
1958 this->str.size = bfd_section_size (sectp);
1959 }
1960 else if (section_is_p (sectp->name, &names.str_offsets))
1961 {
1962 this->str_offsets.s.section = sectp;
1963 this->str_offsets.size = bfd_section_size (sectp);
1964 }
1965 else if (section_is_p (sectp->name, &names.line_str))
1966 {
1967 this->line_str.s.section = sectp;
1968 this->line_str.size = bfd_section_size (sectp);
1969 }
1970 else if (section_is_p (sectp->name, &names.addr))
1971 {
1972 this->addr.s.section = sectp;
1973 this->addr.size = bfd_section_size (sectp);
1974 }
1975 else if (section_is_p (sectp->name, &names.frame))
1976 {
1977 this->frame.s.section = sectp;
1978 this->frame.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.eh_frame))
1981 {
1982 this->eh_frame.s.section = sectp;
1983 this->eh_frame.size = bfd_section_size (sectp);
1984 }
1985 else if (section_is_p (sectp->name, &names.ranges))
1986 {
1987 this->ranges.s.section = sectp;
1988 this->ranges.size = bfd_section_size (sectp);
1989 }
1990 else if (section_is_p (sectp->name, &names.rnglists))
1991 {
1992 this->rnglists.s.section = sectp;
1993 this->rnglists.size = bfd_section_size (sectp);
1994 }
1995 else if (section_is_p (sectp->name, &names.types))
1996 {
1997 struct dwarf2_section_info type_section;
1998
1999 memset (&type_section, 0, sizeof (type_section));
2000 type_section.s.section = sectp;
2001 type_section.size = bfd_section_size (sectp);
2002
2003 this->types.push_back (type_section);
2004 }
2005 else if (section_is_p (sectp->name, &names.gdb_index))
2006 {
2007 this->gdb_index.s.section = sectp;
2008 this->gdb_index.size = bfd_section_size (sectp);
2009 }
2010 else if (section_is_p (sectp->name, &names.debug_names))
2011 {
2012 this->debug_names.s.section = sectp;
2013 this->debug_names.size = bfd_section_size (sectp);
2014 }
2015 else if (section_is_p (sectp->name, &names.debug_aranges))
2016 {
2017 this->debug_aranges.s.section = sectp;
2018 this->debug_aranges.size = bfd_section_size (sectp);
2019 }
2020
2021 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2022 && bfd_section_vma (sectp) == 0)
2023 this->has_section_at_zero = true;
2024 }
2025
2026 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2027 SECTION_NAME. */
2028
2029 void
2030 dwarf2_get_section_info (struct objfile *objfile,
2031 enum dwarf2_section_enum sect,
2032 asection **sectp, const gdb_byte **bufp,
2033 bfd_size_type *sizep)
2034 {
2035 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2036 struct dwarf2_section_info *info;
2037
2038 /* We may see an objfile without any DWARF, in which case we just
2039 return nothing. */
2040 if (data == NULL)
2041 {
2042 *sectp = NULL;
2043 *bufp = NULL;
2044 *sizep = 0;
2045 return;
2046 }
2047 switch (sect)
2048 {
2049 case DWARF2_DEBUG_FRAME:
2050 info = &data->per_bfd->frame;
2051 break;
2052 case DWARF2_EH_FRAME:
2053 info = &data->per_bfd->eh_frame;
2054 break;
2055 default:
2056 gdb_assert_not_reached ("unexpected section");
2057 }
2058
2059 info->read (objfile);
2060
2061 *sectp = info->get_bfd_section ();
2062 *bufp = info->buffer;
2063 *sizep = info->size;
2064 }
2065
2066 /* A helper function to find the sections for a .dwz file. */
2067
2068 static void
2069 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2070 {
2071 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2072
2073 /* Note that we only support the standard ELF names, because .dwz
2074 is ELF-only (at the time of writing). */
2075 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2076 {
2077 dwz_file->abbrev.s.section = sectp;
2078 dwz_file->abbrev.size = bfd_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2081 {
2082 dwz_file->info.s.section = sectp;
2083 dwz_file->info.size = bfd_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2086 {
2087 dwz_file->str.s.section = sectp;
2088 dwz_file->str.size = bfd_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2091 {
2092 dwz_file->line.s.section = sectp;
2093 dwz_file->line.size = bfd_section_size (sectp);
2094 }
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2096 {
2097 dwz_file->macro.s.section = sectp;
2098 dwz_file->macro.size = bfd_section_size (sectp);
2099 }
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2101 {
2102 dwz_file->gdb_index.s.section = sectp;
2103 dwz_file->gdb_index.size = bfd_section_size (sectp);
2104 }
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2106 {
2107 dwz_file->debug_names.s.section = sectp;
2108 dwz_file->debug_names.size = bfd_section_size (sectp);
2109 }
2110 }
2111
2112 /* See dwarf2read.h. */
2113
2114 struct dwz_file *
2115 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2116 {
2117 const char *filename;
2118 bfd_size_type buildid_len_arg;
2119 size_t buildid_len;
2120 bfd_byte *buildid;
2121
2122 if (dwarf2_per_objfile->per_bfd->dwz_file != NULL)
2123 return dwarf2_per_objfile->per_bfd->dwz_file.get ();
2124
2125 bfd_set_error (bfd_error_no_error);
2126 gdb::unique_xmalloc_ptr<char> data
2127 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2128 &buildid_len_arg, &buildid));
2129 if (data == NULL)
2130 {
2131 if (bfd_get_error () == bfd_error_no_error)
2132 return NULL;
2133 error (_("could not read '.gnu_debugaltlink' section: %s"),
2134 bfd_errmsg (bfd_get_error ()));
2135 }
2136
2137 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2138
2139 buildid_len = (size_t) buildid_len_arg;
2140
2141 filename = data.get ();
2142
2143 std::string abs_storage;
2144 if (!IS_ABSOLUTE_PATH (filename))
2145 {
2146 gdb::unique_xmalloc_ptr<char> abs
2147 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2148
2149 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2150 filename = abs_storage.c_str ();
2151 }
2152
2153 /* First try the file name given in the section. If that doesn't
2154 work, try to use the build-id instead. */
2155 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2156 if (dwz_bfd != NULL)
2157 {
2158 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2159 dwz_bfd.reset (nullptr);
2160 }
2161
2162 if (dwz_bfd == NULL)
2163 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2164
2165 if (dwz_bfd == nullptr)
2166 {
2167 gdb::unique_xmalloc_ptr<char> alt_filename;
2168 const char *origname = dwarf2_per_objfile->objfile->original_name;
2169
2170 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2171 buildid_len,
2172 origname,
2173 &alt_filename));
2174
2175 if (fd.get () >= 0)
2176 {
2177 /* File successfully retrieved from server. */
2178 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2179
2180 if (dwz_bfd == nullptr)
2181 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2182 alt_filename.get ());
2183 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2184 dwz_bfd.reset (nullptr);
2185 }
2186 }
2187
2188 if (dwz_bfd == NULL)
2189 error (_("could not find '.gnu_debugaltlink' file for %s"),
2190 objfile_name (dwarf2_per_objfile->objfile));
2191
2192 std::unique_ptr<struct dwz_file> result
2193 (new struct dwz_file (std::move (dwz_bfd)));
2194
2195 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2196 result.get ());
2197
2198 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2199 result->dwz_bfd.get ());
2200 dwarf2_per_objfile->per_bfd->dwz_file = std::move (result);
2201 return dwarf2_per_objfile->per_bfd->dwz_file.get ();
2202 }
2203 \f
2204 /* DWARF quick_symbols_functions support. */
2205
2206 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2207 unique line tables, so we maintain a separate table of all .debug_line
2208 derived entries to support the sharing.
2209 All the quick functions need is the list of file names. We discard the
2210 line_header when we're done and don't need to record it here. */
2211 struct quick_file_names
2212 {
2213 /* The data used to construct the hash key. */
2214 struct stmt_list_hash hash;
2215
2216 /* The number of entries in file_names, real_names. */
2217 unsigned int num_file_names;
2218
2219 /* The file names from the line table, after being run through
2220 file_full_name. */
2221 const char **file_names;
2222
2223 /* The file names from the line table after being run through
2224 gdb_realpath. These are computed lazily. */
2225 const char **real_names;
2226 };
2227
2228 /* When using the index (and thus not using psymtabs), each CU has an
2229 object of this type. This is used to hold information needed by
2230 the various "quick" methods. */
2231 struct dwarf2_per_cu_quick_data
2232 {
2233 /* The file table. This can be NULL if there was no file table
2234 or it's currently not read in.
2235 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2236 struct quick_file_names *file_names;
2237
2238 /* A temporary mark bit used when iterating over all CUs in
2239 expand_symtabs_matching. */
2240 unsigned int mark : 1;
2241
2242 /* True if we've tried to read the file table and found there isn't one.
2243 There will be no point in trying to read it again next time. */
2244 unsigned int no_file_data : 1;
2245 };
2246
2247 /* Utility hash function for a stmt_list_hash. */
2248
2249 static hashval_t
2250 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2251 {
2252 hashval_t v = 0;
2253
2254 if (stmt_list_hash->dwo_unit != NULL)
2255 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2256 v += to_underlying (stmt_list_hash->line_sect_off);
2257 return v;
2258 }
2259
2260 /* Utility equality function for a stmt_list_hash. */
2261
2262 static int
2263 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2264 const struct stmt_list_hash *rhs)
2265 {
2266 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2267 return 0;
2268 if (lhs->dwo_unit != NULL
2269 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2270 return 0;
2271
2272 return lhs->line_sect_off == rhs->line_sect_off;
2273 }
2274
2275 /* Hash function for a quick_file_names. */
2276
2277 static hashval_t
2278 hash_file_name_entry (const void *e)
2279 {
2280 const struct quick_file_names *file_data
2281 = (const struct quick_file_names *) e;
2282
2283 return hash_stmt_list_entry (&file_data->hash);
2284 }
2285
2286 /* Equality function for a quick_file_names. */
2287
2288 static int
2289 eq_file_name_entry (const void *a, const void *b)
2290 {
2291 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2292 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2293
2294 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2295 }
2296
2297 /* Delete function for a quick_file_names. */
2298
2299 static void
2300 delete_file_name_entry (void *e)
2301 {
2302 struct quick_file_names *file_data = (struct quick_file_names *) e;
2303 int i;
2304
2305 for (i = 0; i < file_data->num_file_names; ++i)
2306 {
2307 xfree ((void*) file_data->file_names[i]);
2308 if (file_data->real_names)
2309 xfree ((void*) file_data->real_names[i]);
2310 }
2311
2312 /* The space for the struct itself lives on the obstack, so we don't
2313 free it here. */
2314 }
2315
2316 /* Create a quick_file_names hash table. */
2317
2318 static htab_up
2319 create_quick_file_names_table (unsigned int nr_initial_entries)
2320 {
2321 return htab_up (htab_create_alloc (nr_initial_entries,
2322 hash_file_name_entry, eq_file_name_entry,
2323 delete_file_name_entry, xcalloc, xfree));
2324 }
2325
2326 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2327 have to be created afterwards. You should call age_cached_comp_units after
2328 processing PER_CU->CU. dw2_setup must have been already called. */
2329
2330 static void
2331 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2332 {
2333 if (per_cu->is_debug_types)
2334 load_full_type_unit (per_cu);
2335 else
2336 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2337
2338 if (per_cu->cu == NULL)
2339 return; /* Dummy CU. */
2340
2341 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2342 }
2343
2344 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2345
2346 static void
2347 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2348 dwarf2_per_objfile *dwarf2_per_objfile,
2349 bool skip_partial)
2350 {
2351 /* Skip type_unit_groups, reading the type units they contain
2352 is handled elsewhere. */
2353 if (per_cu->type_unit_group_p ())
2354 return;
2355
2356 /* The destructor of dwarf2_queue_guard frees any entries left on
2357 the queue. After this point we're guaranteed to leave this function
2358 with the dwarf queue empty. */
2359 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2360
2361 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2362 {
2363 queue_comp_unit (per_cu, language_minimal);
2364 load_cu (per_cu, skip_partial);
2365
2366 /* If we just loaded a CU from a DWO, and we're working with an index
2367 that may badly handle TUs, load all the TUs in that DWO as well.
2368 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2369 if (!per_cu->is_debug_types
2370 && per_cu->cu != NULL
2371 && per_cu->cu->dwo_unit != NULL
2372 && dwarf2_per_objfile->per_bfd->index_table != NULL
2373 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2374 /* DWP files aren't supported yet. */
2375 && get_dwp_file (dwarf2_per_objfile) == NULL)
2376 queue_and_load_all_dwo_tus (per_cu);
2377 }
2378
2379 process_queue (dwarf2_per_objfile);
2380
2381 /* Age the cache, releasing compilation units that have not
2382 been used recently. */
2383 age_cached_comp_units (dwarf2_per_objfile);
2384 }
2385
2386 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2387 the per-objfile for which this symtab is instantiated.
2388
2389 Returns the resulting symbol table. */
2390
2391 static struct compunit_symtab *
2392 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2393 dwarf2_per_objfile *dwarf2_per_objfile,
2394 bool skip_partial)
2395 {
2396 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2397
2398 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2399 {
2400 free_cached_comp_units freer (dwarf2_per_objfile);
2401 scoped_restore decrementer = increment_reading_symtab ();
2402 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2403 process_cu_includes (dwarf2_per_objfile);
2404 }
2405
2406 return dwarf2_per_objfile->get_symtab (per_cu);
2407 }
2408
2409 /* See declaration. */
2410
2411 dwarf2_per_cu_data *
2412 dwarf2_per_bfd::get_cutu (int index)
2413 {
2414 if (index >= this->all_comp_units.size ())
2415 {
2416 index -= this->all_comp_units.size ();
2417 gdb_assert (index < this->all_type_units.size ());
2418 return &this->all_type_units[index]->per_cu;
2419 }
2420
2421 return this->all_comp_units[index];
2422 }
2423
2424 /* See declaration. */
2425
2426 dwarf2_per_cu_data *
2427 dwarf2_per_bfd::get_cu (int index)
2428 {
2429 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2430
2431 return this->all_comp_units[index];
2432 }
2433
2434 /* See declaration. */
2435
2436 signatured_type *
2437 dwarf2_per_bfd::get_tu (int index)
2438 {
2439 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2440
2441 return this->all_type_units[index];
2442 }
2443
2444 /* See read.h. */
2445
2446 dwarf2_per_cu_data *
2447 dwarf2_per_bfd::allocate_per_cu ()
2448 {
2449 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2450 result->index = m_num_psymtabs++;
2451 return result;
2452 }
2453
2454 /* See read.h. */
2455
2456 signatured_type *
2457 dwarf2_per_bfd::allocate_signatured_type ()
2458 {
2459 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2460 result->per_cu.index = m_num_psymtabs++;
2461 return result;
2462 }
2463
2464 /* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2465 obstack, and constructed with the specified field values. */
2466
2467 static dwarf2_per_cu_data *
2468 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2469 struct dwarf2_section_info *section,
2470 int is_dwz,
2471 sect_offset sect_off, ULONGEST length)
2472 {
2473 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2474 the_cu->sect_off = sect_off;
2475 the_cu->length = length;
2476 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2477 the_cu->section = section;
2478 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2479 struct dwarf2_per_cu_quick_data);
2480 the_cu->is_dwz = is_dwz;
2481 return the_cu;
2482 }
2483
2484 /* A helper for create_cus_from_index that handles a given list of
2485 CUs. */
2486
2487 static void
2488 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2489 const gdb_byte *cu_list, offset_type n_elements,
2490 struct dwarf2_section_info *section,
2491 int is_dwz)
2492 {
2493 for (offset_type i = 0; i < n_elements; i += 2)
2494 {
2495 gdb_static_assert (sizeof (ULONGEST) >= 8);
2496
2497 sect_offset sect_off
2498 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2499 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2500 cu_list += 2 * 8;
2501
2502 dwarf2_per_cu_data *per_cu
2503 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2504 sect_off, length);
2505 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2506 }
2507 }
2508
2509 /* Read the CU list from the mapped index, and use it to create all
2510 the CU objects for this objfile. */
2511
2512 static void
2513 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2514 const gdb_byte *cu_list, offset_type cu_list_elements,
2515 const gdb_byte *dwz_list, offset_type dwz_elements)
2516 {
2517 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2518 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2519 ((cu_list_elements + dwz_elements) / 2);
2520
2521 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2522 &dwarf2_per_objfile->per_bfd->info, 0);
2523
2524 if (dwz_elements == 0)
2525 return;
2526
2527 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2528 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2529 &dwz->info, 1);
2530 }
2531
2532 /* Create the signatured type hash table from the index. */
2533
2534 static void
2535 create_signatured_type_table_from_index
2536 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2537 struct dwarf2_section_info *section,
2538 const gdb_byte *bytes,
2539 offset_type elements)
2540 {
2541 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2542 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2543
2544 htab_up sig_types_hash = allocate_signatured_type_table ();
2545
2546 for (offset_type i = 0; i < elements; i += 3)
2547 {
2548 struct signatured_type *sig_type;
2549 ULONGEST signature;
2550 void **slot;
2551 cu_offset type_offset_in_tu;
2552
2553 gdb_static_assert (sizeof (ULONGEST) >= 8);
2554 sect_offset sect_off
2555 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2556 type_offset_in_tu
2557 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2558 BFD_ENDIAN_LITTLE);
2559 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2560 bytes += 3 * 8;
2561
2562 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2563 sig_type->signature = signature;
2564 sig_type->type_offset_in_tu = type_offset_in_tu;
2565 sig_type->per_cu.is_debug_types = 1;
2566 sig_type->per_cu.section = section;
2567 sig_type->per_cu.sect_off = sect_off;
2568 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2569 sig_type->per_cu.v.quick
2570 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2571 struct dwarf2_per_cu_quick_data);
2572
2573 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2574 *slot = sig_type;
2575
2576 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2577 }
2578
2579 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2580 }
2581
2582 /* Create the signatured type hash table from .debug_names. */
2583
2584 static void
2585 create_signatured_type_table_from_debug_names
2586 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2587 const mapped_debug_names &map,
2588 struct dwarf2_section_info *section,
2589 struct dwarf2_section_info *abbrev_section)
2590 {
2591 struct objfile *objfile = dwarf2_per_objfile->objfile;
2592
2593 section->read (objfile);
2594 abbrev_section->read (objfile);
2595
2596 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2597 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2598
2599 htab_up sig_types_hash = allocate_signatured_type_table ();
2600
2601 for (uint32_t i = 0; i < map.tu_count; ++i)
2602 {
2603 struct signatured_type *sig_type;
2604 void **slot;
2605
2606 sect_offset sect_off
2607 = (sect_offset) (extract_unsigned_integer
2608 (map.tu_table_reordered + i * map.offset_size,
2609 map.offset_size,
2610 map.dwarf5_byte_order));
2611
2612 comp_unit_head cu_header;
2613 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2614 abbrev_section,
2615 section->buffer + to_underlying (sect_off),
2616 rcuh_kind::TYPE);
2617
2618 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2619 sig_type->signature = cu_header.signature;
2620 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2621 sig_type->per_cu.is_debug_types = 1;
2622 sig_type->per_cu.section = section;
2623 sig_type->per_cu.sect_off = sect_off;
2624 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2625 sig_type->per_cu.v.quick
2626 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2627 struct dwarf2_per_cu_quick_data);
2628
2629 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2630 *slot = sig_type;
2631
2632 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2633 }
2634
2635 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2636 }
2637
2638 /* Read the address map data from the mapped index, and use it to
2639 populate the objfile's psymtabs_addrmap. */
2640
2641 static void
2642 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2643 struct mapped_index *index)
2644 {
2645 struct objfile *objfile = dwarf2_per_objfile->objfile;
2646 struct gdbarch *gdbarch = objfile->arch ();
2647 const gdb_byte *iter, *end;
2648 struct addrmap *mutable_map;
2649 CORE_ADDR baseaddr;
2650
2651 auto_obstack temp_obstack;
2652
2653 mutable_map = addrmap_create_mutable (&temp_obstack);
2654
2655 iter = index->address_table.data ();
2656 end = iter + index->address_table.size ();
2657
2658 baseaddr = objfile->text_section_offset ();
2659
2660 while (iter < end)
2661 {
2662 ULONGEST hi, lo, cu_index;
2663 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2664 iter += 8;
2665 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2666 iter += 8;
2667 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2668 iter += 4;
2669
2670 if (lo > hi)
2671 {
2672 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2673 hex_string (lo), hex_string (hi));
2674 continue;
2675 }
2676
2677 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2678 {
2679 complaint (_(".gdb_index address table has invalid CU number %u"),
2680 (unsigned) cu_index);
2681 continue;
2682 }
2683
2684 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2685 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2686 addrmap_set_empty (mutable_map, lo, hi - 1,
2687 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2688 }
2689
2690 objfile->partial_symtabs->psymtabs_addrmap
2691 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2692 }
2693
2694 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2695 populate the objfile's psymtabs_addrmap. */
2696
2697 static void
2698 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2699 struct dwarf2_section_info *section)
2700 {
2701 struct objfile *objfile = dwarf2_per_objfile->objfile;
2702 bfd *abfd = objfile->obfd;
2703 struct gdbarch *gdbarch = objfile->arch ();
2704 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2705
2706 auto_obstack temp_obstack;
2707 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2708
2709 std::unordered_map<sect_offset,
2710 dwarf2_per_cu_data *,
2711 gdb::hash_enum<sect_offset>>
2712 debug_info_offset_to_per_cu;
2713 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2714 {
2715 const auto insertpair
2716 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2717 if (!insertpair.second)
2718 {
2719 warning (_("Section .debug_aranges in %s has duplicate "
2720 "debug_info_offset %s, ignoring .debug_aranges."),
2721 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2722 return;
2723 }
2724 }
2725
2726 section->read (objfile);
2727
2728 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2729
2730 const gdb_byte *addr = section->buffer;
2731
2732 while (addr < section->buffer + section->size)
2733 {
2734 const gdb_byte *const entry_addr = addr;
2735 unsigned int bytes_read;
2736
2737 const LONGEST entry_length = read_initial_length (abfd, addr,
2738 &bytes_read);
2739 addr += bytes_read;
2740
2741 const gdb_byte *const entry_end = addr + entry_length;
2742 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2743 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2744 if (addr + entry_length > section->buffer + section->size)
2745 {
2746 warning (_("Section .debug_aranges in %s entry at offset %s "
2747 "length %s exceeds section length %s, "
2748 "ignoring .debug_aranges."),
2749 objfile_name (objfile),
2750 plongest (entry_addr - section->buffer),
2751 plongest (bytes_read + entry_length),
2752 pulongest (section->size));
2753 return;
2754 }
2755
2756 /* The version number. */
2757 const uint16_t version = read_2_bytes (abfd, addr);
2758 addr += 2;
2759 if (version != 2)
2760 {
2761 warning (_("Section .debug_aranges in %s entry at offset %s "
2762 "has unsupported version %d, ignoring .debug_aranges."),
2763 objfile_name (objfile),
2764 plongest (entry_addr - section->buffer), version);
2765 return;
2766 }
2767
2768 const uint64_t debug_info_offset
2769 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2770 addr += offset_size;
2771 const auto per_cu_it
2772 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2773 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2774 {
2775 warning (_("Section .debug_aranges in %s entry at offset %s "
2776 "debug_info_offset %s does not exists, "
2777 "ignoring .debug_aranges."),
2778 objfile_name (objfile),
2779 plongest (entry_addr - section->buffer),
2780 pulongest (debug_info_offset));
2781 return;
2782 }
2783 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2784
2785 const uint8_t address_size = *addr++;
2786 if (address_size < 1 || address_size > 8)
2787 {
2788 warning (_("Section .debug_aranges in %s entry at offset %s "
2789 "address_size %u is invalid, ignoring .debug_aranges."),
2790 objfile_name (objfile),
2791 plongest (entry_addr - section->buffer), address_size);
2792 return;
2793 }
2794
2795 const uint8_t segment_selector_size = *addr++;
2796 if (segment_selector_size != 0)
2797 {
2798 warning (_("Section .debug_aranges in %s entry at offset %s "
2799 "segment_selector_size %u is not supported, "
2800 "ignoring .debug_aranges."),
2801 objfile_name (objfile),
2802 plongest (entry_addr - section->buffer),
2803 segment_selector_size);
2804 return;
2805 }
2806
2807 /* Must pad to an alignment boundary that is twice the address
2808 size. It is undocumented by the DWARF standard but GCC does
2809 use it. */
2810 for (size_t padding = ((-(addr - section->buffer))
2811 & (2 * address_size - 1));
2812 padding > 0; padding--)
2813 if (*addr++ != 0)
2814 {
2815 warning (_("Section .debug_aranges in %s entry at offset %s "
2816 "padding is not zero, ignoring .debug_aranges."),
2817 objfile_name (objfile),
2818 plongest (entry_addr - section->buffer));
2819 return;
2820 }
2821
2822 for (;;)
2823 {
2824 if (addr + 2 * address_size > entry_end)
2825 {
2826 warning (_("Section .debug_aranges in %s entry at offset %s "
2827 "address list is not properly terminated, "
2828 "ignoring .debug_aranges."),
2829 objfile_name (objfile),
2830 plongest (entry_addr - section->buffer));
2831 return;
2832 }
2833 ULONGEST start = extract_unsigned_integer (addr, address_size,
2834 dwarf5_byte_order);
2835 addr += address_size;
2836 ULONGEST length = extract_unsigned_integer (addr, address_size,
2837 dwarf5_byte_order);
2838 addr += address_size;
2839 if (start == 0 && length == 0)
2840 break;
2841 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2842 {
2843 /* Symbol was eliminated due to a COMDAT group. */
2844 continue;
2845 }
2846 ULONGEST end = start + length;
2847 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2848 - baseaddr);
2849 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2850 - baseaddr);
2851 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2852 }
2853 }
2854
2855 objfile->partial_symtabs->psymtabs_addrmap
2856 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2857 }
2858
2859 /* Find a slot in the mapped index INDEX for the object named NAME.
2860 If NAME is found, set *VEC_OUT to point to the CU vector in the
2861 constant pool and return true. If NAME cannot be found, return
2862 false. */
2863
2864 static bool
2865 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2866 offset_type **vec_out)
2867 {
2868 offset_type hash;
2869 offset_type slot, step;
2870 int (*cmp) (const char *, const char *);
2871
2872 gdb::unique_xmalloc_ptr<char> without_params;
2873 if (current_language->la_language == language_cplus
2874 || current_language->la_language == language_fortran
2875 || current_language->la_language == language_d)
2876 {
2877 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2878 not contain any. */
2879
2880 if (strchr (name, '(') != NULL)
2881 {
2882 without_params = cp_remove_params (name);
2883
2884 if (without_params != NULL)
2885 name = without_params.get ();
2886 }
2887 }
2888
2889 /* Index version 4 did not support case insensitive searches. But the
2890 indices for case insensitive languages are built in lowercase, therefore
2891 simulate our NAME being searched is also lowercased. */
2892 hash = mapped_index_string_hash ((index->version == 4
2893 && case_sensitivity == case_sensitive_off
2894 ? 5 : index->version),
2895 name);
2896
2897 slot = hash & (index->symbol_table.size () - 1);
2898 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2899 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2900
2901 for (;;)
2902 {
2903 const char *str;
2904
2905 const auto &bucket = index->symbol_table[slot];
2906 if (bucket.name == 0 && bucket.vec == 0)
2907 return false;
2908
2909 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2910 if (!cmp (name, str))
2911 {
2912 *vec_out = (offset_type *) (index->constant_pool
2913 + MAYBE_SWAP (bucket.vec));
2914 return true;
2915 }
2916
2917 slot = (slot + step) & (index->symbol_table.size () - 1);
2918 }
2919 }
2920
2921 /* A helper function that reads the .gdb_index from BUFFER and fills
2922 in MAP. FILENAME is the name of the file containing the data;
2923 it is used for error reporting. DEPRECATED_OK is true if it is
2924 ok to use deprecated sections.
2925
2926 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2927 out parameters that are filled in with information about the CU and
2928 TU lists in the section.
2929
2930 Returns true if all went well, false otherwise. */
2931
2932 static bool
2933 read_gdb_index_from_buffer (const char *filename,
2934 bool deprecated_ok,
2935 gdb::array_view<const gdb_byte> buffer,
2936 struct mapped_index *map,
2937 const gdb_byte **cu_list,
2938 offset_type *cu_list_elements,
2939 const gdb_byte **types_list,
2940 offset_type *types_list_elements)
2941 {
2942 const gdb_byte *addr = &buffer[0];
2943
2944 /* Version check. */
2945 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2946 /* Versions earlier than 3 emitted every copy of a psymbol. This
2947 causes the index to behave very poorly for certain requests. Version 3
2948 contained incomplete addrmap. So, it seems better to just ignore such
2949 indices. */
2950 if (version < 4)
2951 {
2952 static int warning_printed = 0;
2953 if (!warning_printed)
2954 {
2955 warning (_("Skipping obsolete .gdb_index section in %s."),
2956 filename);
2957 warning_printed = 1;
2958 }
2959 return 0;
2960 }
2961 /* Index version 4 uses a different hash function than index version
2962 5 and later.
2963
2964 Versions earlier than 6 did not emit psymbols for inlined
2965 functions. Using these files will cause GDB not to be able to
2966 set breakpoints on inlined functions by name, so we ignore these
2967 indices unless the user has done
2968 "set use-deprecated-index-sections on". */
2969 if (version < 6 && !deprecated_ok)
2970 {
2971 static int warning_printed = 0;
2972 if (!warning_printed)
2973 {
2974 warning (_("\
2975 Skipping deprecated .gdb_index section in %s.\n\
2976 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2977 to use the section anyway."),
2978 filename);
2979 warning_printed = 1;
2980 }
2981 return 0;
2982 }
2983 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2984 of the TU (for symbols coming from TUs),
2985 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2986 Plus gold-generated indices can have duplicate entries for global symbols,
2987 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2988 These are just performance bugs, and we can't distinguish gdb-generated
2989 indices from gold-generated ones, so issue no warning here. */
2990
2991 /* Indexes with higher version than the one supported by GDB may be no
2992 longer backward compatible. */
2993 if (version > 8)
2994 return 0;
2995
2996 map->version = version;
2997
2998 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2999
3000 int i = 0;
3001 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3002 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3003 / 8);
3004 ++i;
3005
3006 *types_list = addr + MAYBE_SWAP (metadata[i]);
3007 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3008 - MAYBE_SWAP (metadata[i]))
3009 / 8);
3010 ++i;
3011
3012 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3013 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3014 map->address_table
3015 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3016 ++i;
3017
3018 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3019 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3020 map->symbol_table
3021 = gdb::array_view<mapped_index::symbol_table_slot>
3022 ((mapped_index::symbol_table_slot *) symbol_table,
3023 (mapped_index::symbol_table_slot *) symbol_table_end);
3024
3025 ++i;
3026 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3027
3028 return 1;
3029 }
3030
3031 /* Callback types for dwarf2_read_gdb_index. */
3032
3033 typedef gdb::function_view
3034 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3035 get_gdb_index_contents_ftype;
3036 typedef gdb::function_view
3037 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3038 get_gdb_index_contents_dwz_ftype;
3039
3040 /* Read .gdb_index. If everything went ok, initialize the "quick"
3041 elements of all the CUs and return 1. Otherwise, return 0. */
3042
3043 static int
3044 dwarf2_read_gdb_index
3045 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3046 get_gdb_index_contents_ftype get_gdb_index_contents,
3047 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3048 {
3049 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3050 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3051 struct dwz_file *dwz;
3052 struct objfile *objfile = dwarf2_per_objfile->objfile;
3053
3054 gdb::array_view<const gdb_byte> main_index_contents
3055 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3056
3057 if (main_index_contents.empty ())
3058 return 0;
3059
3060 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3061 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3062 use_deprecated_index_sections,
3063 main_index_contents, map.get (), &cu_list,
3064 &cu_list_elements, &types_list,
3065 &types_list_elements))
3066 return 0;
3067
3068 /* Don't use the index if it's empty. */
3069 if (map->symbol_table.empty ())
3070 return 0;
3071
3072 /* If there is a .dwz file, read it so we can get its CU list as
3073 well. */
3074 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3075 if (dwz != NULL)
3076 {
3077 struct mapped_index dwz_map;
3078 const gdb_byte *dwz_types_ignore;
3079 offset_type dwz_types_elements_ignore;
3080
3081 gdb::array_view<const gdb_byte> dwz_index_content
3082 = get_gdb_index_contents_dwz (objfile, dwz);
3083
3084 if (dwz_index_content.empty ())
3085 return 0;
3086
3087 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3088 1, dwz_index_content, &dwz_map,
3089 &dwz_list, &dwz_list_elements,
3090 &dwz_types_ignore,
3091 &dwz_types_elements_ignore))
3092 {
3093 warning (_("could not read '.gdb_index' section from %s; skipping"),
3094 bfd_get_filename (dwz->dwz_bfd.get ()));
3095 return 0;
3096 }
3097 }
3098
3099 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3100 dwz_list, dwz_list_elements);
3101
3102 if (types_list_elements)
3103 {
3104 /* We can only handle a single .debug_types when we have an
3105 index. */
3106 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3107 return 0;
3108
3109 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3110
3111 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3112 types_list, types_list_elements);
3113 }
3114
3115 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3116
3117 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3118 dwarf2_per_objfile->per_bfd->using_index = 1;
3119 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3120 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3121
3122 return 1;
3123 }
3124
3125 /* die_reader_func for dw2_get_file_names. */
3126
3127 static void
3128 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3129 const gdb_byte *info_ptr,
3130 struct die_info *comp_unit_die)
3131 {
3132 struct dwarf2_cu *cu = reader->cu;
3133 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3134 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3135 struct dwarf2_per_cu_data *lh_cu;
3136 struct attribute *attr;
3137 void **slot;
3138 struct quick_file_names *qfn;
3139
3140 gdb_assert (! this_cu->is_debug_types);
3141
3142 /* Our callers never want to match partial units -- instead they
3143 will match the enclosing full CU. */
3144 if (comp_unit_die->tag == DW_TAG_partial_unit)
3145 {
3146 this_cu->v.quick->no_file_data = 1;
3147 return;
3148 }
3149
3150 lh_cu = this_cu;
3151 slot = NULL;
3152
3153 line_header_up lh;
3154 sect_offset line_offset {};
3155
3156 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3157 if (attr != nullptr)
3158 {
3159 struct quick_file_names find_entry;
3160
3161 line_offset = (sect_offset) DW_UNSND (attr);
3162
3163 /* We may have already read in this line header (TU line header sharing).
3164 If we have we're done. */
3165 find_entry.hash.dwo_unit = cu->dwo_unit;
3166 find_entry.hash.line_sect_off = line_offset;
3167 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3168 &find_entry, INSERT);
3169 if (*slot != NULL)
3170 {
3171 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3172 return;
3173 }
3174
3175 lh = dwarf_decode_line_header (line_offset, cu);
3176 }
3177 if (lh == NULL)
3178 {
3179 lh_cu->v.quick->no_file_data = 1;
3180 return;
3181 }
3182
3183 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3184 qfn->hash.dwo_unit = cu->dwo_unit;
3185 qfn->hash.line_sect_off = line_offset;
3186 gdb_assert (slot != NULL);
3187 *slot = qfn;
3188
3189 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3190
3191 int offset = 0;
3192 if (strcmp (fnd.name, "<unknown>") != 0)
3193 ++offset;
3194
3195 qfn->num_file_names = offset + lh->file_names_size ();
3196 qfn->file_names =
3197 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3198 qfn->num_file_names);
3199 if (offset != 0)
3200 qfn->file_names[0] = xstrdup (fnd.name);
3201 for (int i = 0; i < lh->file_names_size (); ++i)
3202 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3203 fnd.comp_dir).release ();
3204 qfn->real_names = NULL;
3205
3206 lh_cu->v.quick->file_names = qfn;
3207 }
3208
3209 /* A helper for the "quick" functions which attempts to read the line
3210 table for THIS_CU. */
3211
3212 static struct quick_file_names *
3213 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3214 {
3215 /* This should never be called for TUs. */
3216 gdb_assert (! this_cu->is_debug_types);
3217 /* Nor type unit groups. */
3218 gdb_assert (! this_cu->type_unit_group_p ());
3219
3220 if (this_cu->v.quick->file_names != NULL)
3221 return this_cu->v.quick->file_names;
3222 /* If we know there is no line data, no point in looking again. */
3223 if (this_cu->v.quick->no_file_data)
3224 return NULL;
3225
3226 cutu_reader reader (this_cu);
3227 if (!reader.dummy_p)
3228 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3229
3230 if (this_cu->v.quick->no_file_data)
3231 return NULL;
3232 return this_cu->v.quick->file_names;
3233 }
3234
3235 /* A helper for the "quick" functions which computes and caches the
3236 real path for a given file name from the line table. */
3237
3238 static const char *
3239 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3240 struct quick_file_names *qfn, int index)
3241 {
3242 if (qfn->real_names == NULL)
3243 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3244 qfn->num_file_names, const char *);
3245
3246 if (qfn->real_names[index] == NULL)
3247 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3248
3249 return qfn->real_names[index];
3250 }
3251
3252 static struct symtab *
3253 dw2_find_last_source_symtab (struct objfile *objfile)
3254 {
3255 struct dwarf2_per_objfile *dwarf2_per_objfile
3256 = get_dwarf2_per_objfile (objfile);
3257 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3258 compunit_symtab *cust
3259 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3260
3261 if (cust == NULL)
3262 return NULL;
3263
3264 return compunit_primary_filetab (cust);
3265 }
3266
3267 /* Traversal function for dw2_forget_cached_source_info. */
3268
3269 static int
3270 dw2_free_cached_file_names (void **slot, void *info)
3271 {
3272 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3273
3274 if (file_data->real_names)
3275 {
3276 int i;
3277
3278 for (i = 0; i < file_data->num_file_names; ++i)
3279 {
3280 xfree ((void*) file_data->real_names[i]);
3281 file_data->real_names[i] = NULL;
3282 }
3283 }
3284
3285 return 1;
3286 }
3287
3288 static void
3289 dw2_forget_cached_source_info (struct objfile *objfile)
3290 {
3291 struct dwarf2_per_objfile *dwarf2_per_objfile
3292 = get_dwarf2_per_objfile (objfile);
3293
3294 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3295 dw2_free_cached_file_names, NULL);
3296 }
3297
3298 /* Helper function for dw2_map_symtabs_matching_filename that expands
3299 the symtabs and calls the iterator. */
3300
3301 static int
3302 dw2_map_expand_apply (struct objfile *objfile,
3303 struct dwarf2_per_cu_data *per_cu,
3304 const char *name, const char *real_path,
3305 gdb::function_view<bool (symtab *)> callback)
3306 {
3307 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3308
3309 /* Don't visit already-expanded CUs. */
3310 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3311 if (per_objfile->symtab_set_p (per_cu))
3312 return 0;
3313
3314 /* This may expand more than one symtab, and we want to iterate over
3315 all of them. */
3316 dw2_instantiate_symtab (per_cu, per_objfile, false);
3317
3318 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3319 last_made, callback);
3320 }
3321
3322 /* Implementation of the map_symtabs_matching_filename method. */
3323
3324 static bool
3325 dw2_map_symtabs_matching_filename
3326 (struct objfile *objfile, const char *name, const char *real_path,
3327 gdb::function_view<bool (symtab *)> callback)
3328 {
3329 const char *name_basename = lbasename (name);
3330 struct dwarf2_per_objfile *dwarf2_per_objfile
3331 = get_dwarf2_per_objfile (objfile);
3332
3333 /* The rule is CUs specify all the files, including those used by
3334 any TU, so there's no need to scan TUs here. */
3335
3336 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3337 {
3338 /* We only need to look at symtabs not already expanded. */
3339 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3340 continue;
3341
3342 quick_file_names *file_data = dw2_get_file_names (per_cu);
3343 if (file_data == NULL)
3344 continue;
3345
3346 for (int j = 0; j < file_data->num_file_names; ++j)
3347 {
3348 const char *this_name = file_data->file_names[j];
3349 const char *this_real_name;
3350
3351 if (compare_filenames_for_search (this_name, name))
3352 {
3353 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3354 callback))
3355 return true;
3356 continue;
3357 }
3358
3359 /* Before we invoke realpath, which can get expensive when many
3360 files are involved, do a quick comparison of the basenames. */
3361 if (! basenames_may_differ
3362 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3363 continue;
3364
3365 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3366 file_data, j);
3367 if (compare_filenames_for_search (this_real_name, name))
3368 {
3369 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3370 callback))
3371 return true;
3372 continue;
3373 }
3374
3375 if (real_path != NULL)
3376 {
3377 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3378 gdb_assert (IS_ABSOLUTE_PATH (name));
3379 if (this_real_name != NULL
3380 && FILENAME_CMP (real_path, this_real_name) == 0)
3381 {
3382 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3383 callback))
3384 return true;
3385 continue;
3386 }
3387 }
3388 }
3389 }
3390
3391 return false;
3392 }
3393
3394 /* Struct used to manage iterating over all CUs looking for a symbol. */
3395
3396 struct dw2_symtab_iterator
3397 {
3398 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3399 struct dwarf2_per_objfile *dwarf2_per_objfile;
3400 /* If set, only look for symbols that match that block. Valid values are
3401 GLOBAL_BLOCK and STATIC_BLOCK. */
3402 gdb::optional<block_enum> block_index;
3403 /* The kind of symbol we're looking for. */
3404 domain_enum domain;
3405 /* The list of CUs from the index entry of the symbol,
3406 or NULL if not found. */
3407 offset_type *vec;
3408 /* The next element in VEC to look at. */
3409 int next;
3410 /* The number of elements in VEC, or zero if there is no match. */
3411 int length;
3412 /* Have we seen a global version of the symbol?
3413 If so we can ignore all further global instances.
3414 This is to work around gold/15646, inefficient gold-generated
3415 indices. */
3416 int global_seen;
3417 };
3418
3419 /* Initialize the index symtab iterator ITER. */
3420
3421 static void
3422 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3423 struct dwarf2_per_objfile *dwarf2_per_objfile,
3424 gdb::optional<block_enum> block_index,
3425 domain_enum domain,
3426 const char *name)
3427 {
3428 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3429 iter->block_index = block_index;
3430 iter->domain = domain;
3431 iter->next = 0;
3432 iter->global_seen = 0;
3433
3434 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3435
3436 /* index is NULL if OBJF_READNOW. */
3437 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3438 iter->length = MAYBE_SWAP (*iter->vec);
3439 else
3440 {
3441 iter->vec = NULL;
3442 iter->length = 0;
3443 }
3444 }
3445
3446 /* Return the next matching CU or NULL if there are no more. */
3447
3448 static struct dwarf2_per_cu_data *
3449 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3450 {
3451 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3452
3453 for ( ; iter->next < iter->length; ++iter->next)
3454 {
3455 offset_type cu_index_and_attrs =
3456 MAYBE_SWAP (iter->vec[iter->next + 1]);
3457 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3458 gdb_index_symbol_kind symbol_kind =
3459 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3460 /* Only check the symbol attributes if they're present.
3461 Indices prior to version 7 don't record them,
3462 and indices >= 7 may elide them for certain symbols
3463 (gold does this). */
3464 int attrs_valid =
3465 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3466 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3467
3468 /* Don't crash on bad data. */
3469 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3470 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3471 {
3472 complaint (_(".gdb_index entry has bad CU index"
3473 " [in module %s]"),
3474 objfile_name (dwarf2_per_objfile->objfile));
3475 continue;
3476 }
3477
3478 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3479
3480 /* Skip if already read in. */
3481 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3482 continue;
3483
3484 /* Check static vs global. */
3485 if (attrs_valid)
3486 {
3487 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3488
3489 if (iter->block_index.has_value ())
3490 {
3491 bool want_static = *iter->block_index == STATIC_BLOCK;
3492
3493 if (is_static != want_static)
3494 continue;
3495 }
3496
3497 /* Work around gold/15646. */
3498 if (!is_static && iter->global_seen)
3499 continue;
3500 if (!is_static)
3501 iter->global_seen = 1;
3502 }
3503
3504 /* Only check the symbol's kind if it has one. */
3505 if (attrs_valid)
3506 {
3507 switch (iter->domain)
3508 {
3509 case VAR_DOMAIN:
3510 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3511 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3512 /* Some types are also in VAR_DOMAIN. */
3513 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3514 continue;
3515 break;
3516 case STRUCT_DOMAIN:
3517 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3518 continue;
3519 break;
3520 case LABEL_DOMAIN:
3521 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3522 continue;
3523 break;
3524 case MODULE_DOMAIN:
3525 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3526 continue;
3527 break;
3528 default:
3529 break;
3530 }
3531 }
3532
3533 ++iter->next;
3534 return per_cu;
3535 }
3536
3537 return NULL;
3538 }
3539
3540 static struct compunit_symtab *
3541 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3542 const char *name, domain_enum domain)
3543 {
3544 struct compunit_symtab *stab_best = NULL;
3545 struct dwarf2_per_objfile *dwarf2_per_objfile
3546 = get_dwarf2_per_objfile (objfile);
3547
3548 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3549
3550 struct dw2_symtab_iterator iter;
3551 struct dwarf2_per_cu_data *per_cu;
3552
3553 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3554
3555 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3556 {
3557 struct symbol *sym, *with_opaque = NULL;
3558 struct compunit_symtab *stab
3559 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3560 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3561 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3562
3563 sym = block_find_symbol (block, name, domain,
3564 block_find_non_opaque_type_preferred,
3565 &with_opaque);
3566
3567 /* Some caution must be observed with overloaded functions
3568 and methods, since the index will not contain any overload
3569 information (but NAME might contain it). */
3570
3571 if (sym != NULL
3572 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3573 return stab;
3574 if (with_opaque != NULL
3575 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3576 stab_best = stab;
3577
3578 /* Keep looking through other CUs. */
3579 }
3580
3581 return stab_best;
3582 }
3583
3584 static void
3585 dw2_print_stats (struct objfile *objfile)
3586 {
3587 struct dwarf2_per_objfile *dwarf2_per_objfile
3588 = get_dwarf2_per_objfile (objfile);
3589 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3590 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3591 int count = 0;
3592
3593 for (int i = 0; i < total; ++i)
3594 {
3595 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3596
3597 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3598 ++count;
3599 }
3600 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3601 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3602 }
3603
3604 /* This dumps minimal information about the index.
3605 It is called via "mt print objfiles".
3606 One use is to verify .gdb_index has been loaded by the
3607 gdb.dwarf2/gdb-index.exp testcase. */
3608
3609 static void
3610 dw2_dump (struct objfile *objfile)
3611 {
3612 struct dwarf2_per_objfile *dwarf2_per_objfile
3613 = get_dwarf2_per_objfile (objfile);
3614
3615 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3616 printf_filtered (".gdb_index:");
3617 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3618 {
3619 printf_filtered (" version %d\n",
3620 dwarf2_per_objfile->per_bfd->index_table->version);
3621 }
3622 else
3623 printf_filtered (" faked for \"readnow\"\n");
3624 printf_filtered ("\n");
3625 }
3626
3627 static void
3628 dw2_expand_symtabs_for_function (struct objfile *objfile,
3629 const char *func_name)
3630 {
3631 struct dwarf2_per_objfile *dwarf2_per_objfile
3632 = get_dwarf2_per_objfile (objfile);
3633
3634 struct dw2_symtab_iterator iter;
3635 struct dwarf2_per_cu_data *per_cu;
3636
3637 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3638
3639 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3640 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3641
3642 }
3643
3644 static void
3645 dw2_expand_all_symtabs (struct objfile *objfile)
3646 {
3647 struct dwarf2_per_objfile *dwarf2_per_objfile
3648 = get_dwarf2_per_objfile (objfile);
3649 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3650 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3651
3652 for (int i = 0; i < total_units; ++i)
3653 {
3654 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3655
3656 /* We don't want to directly expand a partial CU, because if we
3657 read it with the wrong language, then assertion failures can
3658 be triggered later on. See PR symtab/23010. So, tell
3659 dw2_instantiate_symtab to skip partial CUs -- any important
3660 partial CU will be read via DW_TAG_imported_unit anyway. */
3661 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3662 }
3663 }
3664
3665 static void
3666 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3667 const char *fullname)
3668 {
3669 struct dwarf2_per_objfile *dwarf2_per_objfile
3670 = get_dwarf2_per_objfile (objfile);
3671
3672 /* We don't need to consider type units here.
3673 This is only called for examining code, e.g. expand_line_sal.
3674 There can be an order of magnitude (or more) more type units
3675 than comp units, and we avoid them if we can. */
3676
3677 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3678 {
3679 /* We only need to look at symtabs not already expanded. */
3680 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3681 continue;
3682
3683 quick_file_names *file_data = dw2_get_file_names (per_cu);
3684 if (file_data == NULL)
3685 continue;
3686
3687 for (int j = 0; j < file_data->num_file_names; ++j)
3688 {
3689 const char *this_fullname = file_data->file_names[j];
3690
3691 if (filename_cmp (this_fullname, fullname) == 0)
3692 {
3693 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3694 break;
3695 }
3696 }
3697 }
3698 }
3699
3700 static void
3701 dw2_expand_symtabs_matching_symbol
3702 (mapped_index_base &index,
3703 const lookup_name_info &lookup_name_in,
3704 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3705 enum search_domain kind,
3706 gdb::function_view<bool (offset_type)> match_callback);
3707
3708 static void
3709 dw2_expand_symtabs_matching_one
3710 (dwarf2_per_cu_data *per_cu,
3711 dwarf2_per_objfile *per_objfile,
3712 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3713 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3714
3715 static void
3716 dw2_map_matching_symbols
3717 (struct objfile *objfile,
3718 const lookup_name_info &name, domain_enum domain,
3719 int global,
3720 gdb::function_view<symbol_found_callback_ftype> callback,
3721 symbol_compare_ftype *ordered_compare)
3722 {
3723 /* Used for Ada. */
3724 struct dwarf2_per_objfile *dwarf2_per_objfile
3725 = get_dwarf2_per_objfile (objfile);
3726
3727 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3728
3729 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3730 {
3731 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3732 here though if the current language is Ada for a non-Ada objfile
3733 using GNU index. */
3734 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3735
3736 const char *match_name = name.ada ().lookup_name ().c_str ();
3737 auto matcher = [&] (const char *symname)
3738 {
3739 if (ordered_compare == nullptr)
3740 return true;
3741 return ordered_compare (symname, match_name) == 0;
3742 };
3743
3744 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3745 [&] (offset_type namei)
3746 {
3747 struct dw2_symtab_iterator iter;
3748 struct dwarf2_per_cu_data *per_cu;
3749
3750 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3751 match_name);
3752 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3753 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3754 nullptr);
3755 return true;
3756 });
3757 }
3758 else
3759 {
3760 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3761 proceed assuming all symtabs have been read in. */
3762 }
3763
3764 for (compunit_symtab *cust : objfile->compunits ())
3765 {
3766 const struct block *block;
3767
3768 if (cust == NULL)
3769 continue;
3770 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3771 if (!iterate_over_symbols_terminated (block, name,
3772 domain, callback))
3773 return;
3774 }
3775 }
3776
3777 /* Starting from a search name, return the string that finds the upper
3778 bound of all strings that start with SEARCH_NAME in a sorted name
3779 list. Returns the empty string to indicate that the upper bound is
3780 the end of the list. */
3781
3782 static std::string
3783 make_sort_after_prefix_name (const char *search_name)
3784 {
3785 /* When looking to complete "func", we find the upper bound of all
3786 symbols that start with "func" by looking for where we'd insert
3787 the closest string that would follow "func" in lexicographical
3788 order. Usually, that's "func"-with-last-character-incremented,
3789 i.e. "fund". Mind non-ASCII characters, though. Usually those
3790 will be UTF-8 multi-byte sequences, but we can't be certain.
3791 Especially mind the 0xff character, which is a valid character in
3792 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3793 rule out compilers allowing it in identifiers. Note that
3794 conveniently, strcmp/strcasecmp are specified to compare
3795 characters interpreted as unsigned char. So what we do is treat
3796 the whole string as a base 256 number composed of a sequence of
3797 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3798 to 0, and carries 1 to the following more-significant position.
3799 If the very first character in SEARCH_NAME ends up incremented
3800 and carries/overflows, then the upper bound is the end of the
3801 list. The string after the empty string is also the empty
3802 string.
3803
3804 Some examples of this operation:
3805
3806 SEARCH_NAME => "+1" RESULT
3807
3808 "abc" => "abd"
3809 "ab\xff" => "ac"
3810 "\xff" "a" "\xff" => "\xff" "b"
3811 "\xff" => ""
3812 "\xff\xff" => ""
3813 "" => ""
3814
3815 Then, with these symbols for example:
3816
3817 func
3818 func1
3819 fund
3820
3821 completing "func" looks for symbols between "func" and
3822 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3823 which finds "func" and "func1", but not "fund".
3824
3825 And with:
3826
3827 funcÿ (Latin1 'ÿ' [0xff])
3828 funcÿ1
3829 fund
3830
3831 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3832 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3833
3834 And with:
3835
3836 ÿÿ (Latin1 'ÿ' [0xff])
3837 ÿÿ1
3838
3839 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3840 the end of the list.
3841 */
3842 std::string after = search_name;
3843 while (!after.empty () && (unsigned char) after.back () == 0xff)
3844 after.pop_back ();
3845 if (!after.empty ())
3846 after.back () = (unsigned char) after.back () + 1;
3847 return after;
3848 }
3849
3850 /* See declaration. */
3851
3852 std::pair<std::vector<name_component>::const_iterator,
3853 std::vector<name_component>::const_iterator>
3854 mapped_index_base::find_name_components_bounds
3855 (const lookup_name_info &lookup_name_without_params, language lang) const
3856 {
3857 auto *name_cmp
3858 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3859
3860 const char *lang_name
3861 = lookup_name_without_params.language_lookup_name (lang);
3862
3863 /* Comparison function object for lower_bound that matches against a
3864 given symbol name. */
3865 auto lookup_compare_lower = [&] (const name_component &elem,
3866 const char *name)
3867 {
3868 const char *elem_qualified = this->symbol_name_at (elem.idx);
3869 const char *elem_name = elem_qualified + elem.name_offset;
3870 return name_cmp (elem_name, name) < 0;
3871 };
3872
3873 /* Comparison function object for upper_bound that matches against a
3874 given symbol name. */
3875 auto lookup_compare_upper = [&] (const char *name,
3876 const name_component &elem)
3877 {
3878 const char *elem_qualified = this->symbol_name_at (elem.idx);
3879 const char *elem_name = elem_qualified + elem.name_offset;
3880 return name_cmp (name, elem_name) < 0;
3881 };
3882
3883 auto begin = this->name_components.begin ();
3884 auto end = this->name_components.end ();
3885
3886 /* Find the lower bound. */
3887 auto lower = [&] ()
3888 {
3889 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3890 return begin;
3891 else
3892 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3893 } ();
3894
3895 /* Find the upper bound. */
3896 auto upper = [&] ()
3897 {
3898 if (lookup_name_without_params.completion_mode ())
3899 {
3900 /* In completion mode, we want UPPER to point past all
3901 symbols names that have the same prefix. I.e., with
3902 these symbols, and completing "func":
3903
3904 function << lower bound
3905 function1
3906 other_function << upper bound
3907
3908 We find the upper bound by looking for the insertion
3909 point of "func"-with-last-character-incremented,
3910 i.e. "fund". */
3911 std::string after = make_sort_after_prefix_name (lang_name);
3912 if (after.empty ())
3913 return end;
3914 return std::lower_bound (lower, end, after.c_str (),
3915 lookup_compare_lower);
3916 }
3917 else
3918 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3919 } ();
3920
3921 return {lower, upper};
3922 }
3923
3924 /* See declaration. */
3925
3926 void
3927 mapped_index_base::build_name_components ()
3928 {
3929 if (!this->name_components.empty ())
3930 return;
3931
3932 this->name_components_casing = case_sensitivity;
3933 auto *name_cmp
3934 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3935
3936 /* The code below only knows how to break apart components of C++
3937 symbol names (and other languages that use '::' as
3938 namespace/module separator) and Ada symbol names. */
3939 auto count = this->symbol_name_count ();
3940 for (offset_type idx = 0; idx < count; idx++)
3941 {
3942 if (this->symbol_name_slot_invalid (idx))
3943 continue;
3944
3945 const char *name = this->symbol_name_at (idx);
3946
3947 /* Add each name component to the name component table. */
3948 unsigned int previous_len = 0;
3949
3950 if (strstr (name, "::") != nullptr)
3951 {
3952 for (unsigned int current_len = cp_find_first_component (name);
3953 name[current_len] != '\0';
3954 current_len += cp_find_first_component (name + current_len))
3955 {
3956 gdb_assert (name[current_len] == ':');
3957 this->name_components.push_back ({previous_len, idx});
3958 /* Skip the '::'. */
3959 current_len += 2;
3960 previous_len = current_len;
3961 }
3962 }
3963 else
3964 {
3965 /* Handle the Ada encoded (aka mangled) form here. */
3966 for (const char *iter = strstr (name, "__");
3967 iter != nullptr;
3968 iter = strstr (iter, "__"))
3969 {
3970 this->name_components.push_back ({previous_len, idx});
3971 iter += 2;
3972 previous_len = iter - name;
3973 }
3974 }
3975
3976 this->name_components.push_back ({previous_len, idx});
3977 }
3978
3979 /* Sort name_components elements by name. */
3980 auto name_comp_compare = [&] (const name_component &left,
3981 const name_component &right)
3982 {
3983 const char *left_qualified = this->symbol_name_at (left.idx);
3984 const char *right_qualified = this->symbol_name_at (right.idx);
3985
3986 const char *left_name = left_qualified + left.name_offset;
3987 const char *right_name = right_qualified + right.name_offset;
3988
3989 return name_cmp (left_name, right_name) < 0;
3990 };
3991
3992 std::sort (this->name_components.begin (),
3993 this->name_components.end (),
3994 name_comp_compare);
3995 }
3996
3997 /* Helper for dw2_expand_symtabs_matching that works with a
3998 mapped_index_base instead of the containing objfile. This is split
3999 to a separate function in order to be able to unit test the
4000 name_components matching using a mock mapped_index_base. For each
4001 symbol name that matches, calls MATCH_CALLBACK, passing it the
4002 symbol's index in the mapped_index_base symbol table. */
4003
4004 static void
4005 dw2_expand_symtabs_matching_symbol
4006 (mapped_index_base &index,
4007 const lookup_name_info &lookup_name_in,
4008 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4009 enum search_domain kind,
4010 gdb::function_view<bool (offset_type)> match_callback)
4011 {
4012 lookup_name_info lookup_name_without_params
4013 = lookup_name_in.make_ignore_params ();
4014
4015 /* Build the symbol name component sorted vector, if we haven't
4016 yet. */
4017 index.build_name_components ();
4018
4019 /* The same symbol may appear more than once in the range though.
4020 E.g., if we're looking for symbols that complete "w", and we have
4021 a symbol named "w1::w2", we'll find the two name components for
4022 that same symbol in the range. To be sure we only call the
4023 callback once per symbol, we first collect the symbol name
4024 indexes that matched in a temporary vector and ignore
4025 duplicates. */
4026 std::vector<offset_type> matches;
4027
4028 struct name_and_matcher
4029 {
4030 symbol_name_matcher_ftype *matcher;
4031 const char *name;
4032
4033 bool operator== (const name_and_matcher &other) const
4034 {
4035 return matcher == other.matcher && strcmp (name, other.name) == 0;
4036 }
4037 };
4038
4039 /* A vector holding all the different symbol name matchers, for all
4040 languages. */
4041 std::vector<name_and_matcher> matchers;
4042
4043 for (int i = 0; i < nr_languages; i++)
4044 {
4045 enum language lang_e = (enum language) i;
4046
4047 const language_defn *lang = language_def (lang_e);
4048 symbol_name_matcher_ftype *name_matcher
4049 = get_symbol_name_matcher (lang, lookup_name_without_params);
4050
4051 name_and_matcher key {
4052 name_matcher,
4053 lookup_name_without_params.language_lookup_name (lang_e)
4054 };
4055
4056 /* Don't insert the same comparison routine more than once.
4057 Note that we do this linear walk. This is not a problem in
4058 practice because the number of supported languages is
4059 low. */
4060 if (std::find (matchers.begin (), matchers.end (), key)
4061 != matchers.end ())
4062 continue;
4063 matchers.push_back (std::move (key));
4064
4065 auto bounds
4066 = index.find_name_components_bounds (lookup_name_without_params,
4067 lang_e);
4068
4069 /* Now for each symbol name in range, check to see if we have a name
4070 match, and if so, call the MATCH_CALLBACK callback. */
4071
4072 for (; bounds.first != bounds.second; ++bounds.first)
4073 {
4074 const char *qualified = index.symbol_name_at (bounds.first->idx);
4075
4076 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4077 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4078 continue;
4079
4080 matches.push_back (bounds.first->idx);
4081 }
4082 }
4083
4084 std::sort (matches.begin (), matches.end ());
4085
4086 /* Finally call the callback, once per match. */
4087 ULONGEST prev = -1;
4088 for (offset_type idx : matches)
4089 {
4090 if (prev != idx)
4091 {
4092 if (!match_callback (idx))
4093 break;
4094 prev = idx;
4095 }
4096 }
4097
4098 /* Above we use a type wider than idx's for 'prev', since 0 and
4099 (offset_type)-1 are both possible values. */
4100 static_assert (sizeof (prev) > sizeof (offset_type), "");
4101 }
4102
4103 #if GDB_SELF_TEST
4104
4105 namespace selftests { namespace dw2_expand_symtabs_matching {
4106
4107 /* A mock .gdb_index/.debug_names-like name index table, enough to
4108 exercise dw2_expand_symtabs_matching_symbol, which works with the
4109 mapped_index_base interface. Builds an index from the symbol list
4110 passed as parameter to the constructor. */
4111 class mock_mapped_index : public mapped_index_base
4112 {
4113 public:
4114 mock_mapped_index (gdb::array_view<const char *> symbols)
4115 : m_symbol_table (symbols)
4116 {}
4117
4118 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4119
4120 /* Return the number of names in the symbol table. */
4121 size_t symbol_name_count () const override
4122 {
4123 return m_symbol_table.size ();
4124 }
4125
4126 /* Get the name of the symbol at IDX in the symbol table. */
4127 const char *symbol_name_at (offset_type idx) const override
4128 {
4129 return m_symbol_table[idx];
4130 }
4131
4132 private:
4133 gdb::array_view<const char *> m_symbol_table;
4134 };
4135
4136 /* Convenience function that converts a NULL pointer to a "<null>"
4137 string, to pass to print routines. */
4138
4139 static const char *
4140 string_or_null (const char *str)
4141 {
4142 return str != NULL ? str : "<null>";
4143 }
4144
4145 /* Check if a lookup_name_info built from
4146 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4147 index. EXPECTED_LIST is the list of expected matches, in expected
4148 matching order. If no match expected, then an empty list is
4149 specified. Returns true on success. On failure prints a warning
4150 indicating the file:line that failed, and returns false. */
4151
4152 static bool
4153 check_match (const char *file, int line,
4154 mock_mapped_index &mock_index,
4155 const char *name, symbol_name_match_type match_type,
4156 bool completion_mode,
4157 std::initializer_list<const char *> expected_list)
4158 {
4159 lookup_name_info lookup_name (name, match_type, completion_mode);
4160
4161 bool matched = true;
4162
4163 auto mismatch = [&] (const char *expected_str,
4164 const char *got)
4165 {
4166 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4167 "expected=\"%s\", got=\"%s\"\n"),
4168 file, line,
4169 (match_type == symbol_name_match_type::FULL
4170 ? "FULL" : "WILD"),
4171 name, string_or_null (expected_str), string_or_null (got));
4172 matched = false;
4173 };
4174
4175 auto expected_it = expected_list.begin ();
4176 auto expected_end = expected_list.end ();
4177
4178 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4179 NULL, ALL_DOMAIN,
4180 [&] (offset_type idx)
4181 {
4182 const char *matched_name = mock_index.symbol_name_at (idx);
4183 const char *expected_str
4184 = expected_it == expected_end ? NULL : *expected_it++;
4185
4186 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4187 mismatch (expected_str, matched_name);
4188 return true;
4189 });
4190
4191 const char *expected_str
4192 = expected_it == expected_end ? NULL : *expected_it++;
4193 if (expected_str != NULL)
4194 mismatch (expected_str, NULL);
4195
4196 return matched;
4197 }
4198
4199 /* The symbols added to the mock mapped_index for testing (in
4200 canonical form). */
4201 static const char *test_symbols[] = {
4202 "function",
4203 "std::bar",
4204 "std::zfunction",
4205 "std::zfunction2",
4206 "w1::w2",
4207 "ns::foo<char*>",
4208 "ns::foo<int>",
4209 "ns::foo<long>",
4210 "ns2::tmpl<int>::foo2",
4211 "(anonymous namespace)::A::B::C",
4212
4213 /* These are used to check that the increment-last-char in the
4214 matching algorithm for completion doesn't match "t1_fund" when
4215 completing "t1_func". */
4216 "t1_func",
4217 "t1_func1",
4218 "t1_fund",
4219 "t1_fund1",
4220
4221 /* A UTF-8 name with multi-byte sequences to make sure that
4222 cp-name-parser understands this as a single identifier ("função"
4223 is "function" in PT). */
4224 u8"u8função",
4225
4226 /* \377 (0xff) is Latin1 'ÿ'. */
4227 "yfunc\377",
4228
4229 /* \377 (0xff) is Latin1 'ÿ'. */
4230 "\377",
4231 "\377\377123",
4232
4233 /* A name with all sorts of complications. Starts with "z" to make
4234 it easier for the completion tests below. */
4235 #define Z_SYM_NAME \
4236 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4237 "::tuple<(anonymous namespace)::ui*, " \
4238 "std::default_delete<(anonymous namespace)::ui>, void>"
4239
4240 Z_SYM_NAME
4241 };
4242
4243 /* Returns true if the mapped_index_base::find_name_component_bounds
4244 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4245 in completion mode. */
4246
4247 static bool
4248 check_find_bounds_finds (mapped_index_base &index,
4249 const char *search_name,
4250 gdb::array_view<const char *> expected_syms)
4251 {
4252 lookup_name_info lookup_name (search_name,
4253 symbol_name_match_type::FULL, true);
4254
4255 auto bounds = index.find_name_components_bounds (lookup_name,
4256 language_cplus);
4257
4258 size_t distance = std::distance (bounds.first, bounds.second);
4259 if (distance != expected_syms.size ())
4260 return false;
4261
4262 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4263 {
4264 auto nc_elem = bounds.first + exp_elem;
4265 const char *qualified = index.symbol_name_at (nc_elem->idx);
4266 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4267 return false;
4268 }
4269
4270 return true;
4271 }
4272
4273 /* Test the lower-level mapped_index::find_name_component_bounds
4274 method. */
4275
4276 static void
4277 test_mapped_index_find_name_component_bounds ()
4278 {
4279 mock_mapped_index mock_index (test_symbols);
4280
4281 mock_index.build_name_components ();
4282
4283 /* Test the lower-level mapped_index::find_name_component_bounds
4284 method in completion mode. */
4285 {
4286 static const char *expected_syms[] = {
4287 "t1_func",
4288 "t1_func1",
4289 };
4290
4291 SELF_CHECK (check_find_bounds_finds (mock_index,
4292 "t1_func", expected_syms));
4293 }
4294
4295 /* Check that the increment-last-char in the name matching algorithm
4296 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4297 {
4298 static const char *expected_syms1[] = {
4299 "\377",
4300 "\377\377123",
4301 };
4302 SELF_CHECK (check_find_bounds_finds (mock_index,
4303 "\377", expected_syms1));
4304
4305 static const char *expected_syms2[] = {
4306 "\377\377123",
4307 };
4308 SELF_CHECK (check_find_bounds_finds (mock_index,
4309 "\377\377", expected_syms2));
4310 }
4311 }
4312
4313 /* Test dw2_expand_symtabs_matching_symbol. */
4314
4315 static void
4316 test_dw2_expand_symtabs_matching_symbol ()
4317 {
4318 mock_mapped_index mock_index (test_symbols);
4319
4320 /* We let all tests run until the end even if some fails, for debug
4321 convenience. */
4322 bool any_mismatch = false;
4323
4324 /* Create the expected symbols list (an initializer_list). Needed
4325 because lists have commas, and we need to pass them to CHECK,
4326 which is a macro. */
4327 #define EXPECT(...) { __VA_ARGS__ }
4328
4329 /* Wrapper for check_match that passes down the current
4330 __FILE__/__LINE__. */
4331 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4332 any_mismatch |= !check_match (__FILE__, __LINE__, \
4333 mock_index, \
4334 NAME, MATCH_TYPE, COMPLETION_MODE, \
4335 EXPECTED_LIST)
4336
4337 /* Identity checks. */
4338 for (const char *sym : test_symbols)
4339 {
4340 /* Should be able to match all existing symbols. */
4341 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4342 EXPECT (sym));
4343
4344 /* Should be able to match all existing symbols with
4345 parameters. */
4346 std::string with_params = std::string (sym) + "(int)";
4347 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4348 EXPECT (sym));
4349
4350 /* Should be able to match all existing symbols with
4351 parameters and qualifiers. */
4352 with_params = std::string (sym) + " ( int ) const";
4353 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4354 EXPECT (sym));
4355
4356 /* This should really find sym, but cp-name-parser.y doesn't
4357 know about lvalue/rvalue qualifiers yet. */
4358 with_params = std::string (sym) + " ( int ) &&";
4359 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4360 {});
4361 }
4362
4363 /* Check that the name matching algorithm for completion doesn't get
4364 confused with Latin1 'ÿ' / 0xff. */
4365 {
4366 static const char str[] = "\377";
4367 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4368 EXPECT ("\377", "\377\377123"));
4369 }
4370
4371 /* Check that the increment-last-char in the matching algorithm for
4372 completion doesn't match "t1_fund" when completing "t1_func". */
4373 {
4374 static const char str[] = "t1_func";
4375 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4376 EXPECT ("t1_func", "t1_func1"));
4377 }
4378
4379 /* Check that completion mode works at each prefix of the expected
4380 symbol name. */
4381 {
4382 static const char str[] = "function(int)";
4383 size_t len = strlen (str);
4384 std::string lookup;
4385
4386 for (size_t i = 1; i < len; i++)
4387 {
4388 lookup.assign (str, i);
4389 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4390 EXPECT ("function"));
4391 }
4392 }
4393
4394 /* While "w" is a prefix of both components, the match function
4395 should still only be called once. */
4396 {
4397 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4398 EXPECT ("w1::w2"));
4399 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4400 EXPECT ("w1::w2"));
4401 }
4402
4403 /* Same, with a "complicated" symbol. */
4404 {
4405 static const char str[] = Z_SYM_NAME;
4406 size_t len = strlen (str);
4407 std::string lookup;
4408
4409 for (size_t i = 1; i < len; i++)
4410 {
4411 lookup.assign (str, i);
4412 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4413 EXPECT (Z_SYM_NAME));
4414 }
4415 }
4416
4417 /* In FULL mode, an incomplete symbol doesn't match. */
4418 {
4419 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4420 {});
4421 }
4422
4423 /* A complete symbol with parameters matches any overload, since the
4424 index has no overload info. */
4425 {
4426 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4427 EXPECT ("std::zfunction", "std::zfunction2"));
4428 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4429 EXPECT ("std::zfunction", "std::zfunction2"));
4430 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4431 EXPECT ("std::zfunction", "std::zfunction2"));
4432 }
4433
4434 /* Check that whitespace is ignored appropriately. A symbol with a
4435 template argument list. */
4436 {
4437 static const char expected[] = "ns::foo<int>";
4438 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4439 EXPECT (expected));
4440 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4441 EXPECT (expected));
4442 }
4443
4444 /* Check that whitespace is ignored appropriately. A symbol with a
4445 template argument list that includes a pointer. */
4446 {
4447 static const char expected[] = "ns::foo<char*>";
4448 /* Try both completion and non-completion modes. */
4449 static const bool completion_mode[2] = {false, true};
4450 for (size_t i = 0; i < 2; i++)
4451 {
4452 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4453 completion_mode[i], EXPECT (expected));
4454 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4455 completion_mode[i], EXPECT (expected));
4456
4457 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4458 completion_mode[i], EXPECT (expected));
4459 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4460 completion_mode[i], EXPECT (expected));
4461 }
4462 }
4463
4464 {
4465 /* Check method qualifiers are ignored. */
4466 static const char expected[] = "ns::foo<char*>";
4467 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4468 symbol_name_match_type::FULL, true, EXPECT (expected));
4469 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4470 symbol_name_match_type::FULL, true, EXPECT (expected));
4471 CHECK_MATCH ("foo < char * > ( int ) const",
4472 symbol_name_match_type::WILD, true, EXPECT (expected));
4473 CHECK_MATCH ("foo < char * > ( int ) &&",
4474 symbol_name_match_type::WILD, true, EXPECT (expected));
4475 }
4476
4477 /* Test lookup names that don't match anything. */
4478 {
4479 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4480 {});
4481
4482 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4483 {});
4484 }
4485
4486 /* Some wild matching tests, exercising "(anonymous namespace)",
4487 which should not be confused with a parameter list. */
4488 {
4489 static const char *syms[] = {
4490 "A::B::C",
4491 "B::C",
4492 "C",
4493 "A :: B :: C ( int )",
4494 "B :: C ( int )",
4495 "C ( int )",
4496 };
4497
4498 for (const char *s : syms)
4499 {
4500 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4501 EXPECT ("(anonymous namespace)::A::B::C"));
4502 }
4503 }
4504
4505 {
4506 static const char expected[] = "ns2::tmpl<int>::foo2";
4507 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4508 EXPECT (expected));
4509 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4510 EXPECT (expected));
4511 }
4512
4513 SELF_CHECK (!any_mismatch);
4514
4515 #undef EXPECT
4516 #undef CHECK_MATCH
4517 }
4518
4519 static void
4520 run_test ()
4521 {
4522 test_mapped_index_find_name_component_bounds ();
4523 test_dw2_expand_symtabs_matching_symbol ();
4524 }
4525
4526 }} // namespace selftests::dw2_expand_symtabs_matching
4527
4528 #endif /* GDB_SELF_TEST */
4529
4530 /* If FILE_MATCHER is NULL or if PER_CU has
4531 dwarf2_per_cu_quick_data::MARK set (see
4532 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4533 EXPANSION_NOTIFY on it. */
4534
4535 static void
4536 dw2_expand_symtabs_matching_one
4537 (dwarf2_per_cu_data *per_cu,
4538 dwarf2_per_objfile *per_objfile,
4539 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4540 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4541 {
4542 if (file_matcher == NULL || per_cu->v.quick->mark)
4543 {
4544 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4545
4546 compunit_symtab *symtab
4547 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4548 gdb_assert (symtab != nullptr);
4549
4550 if (expansion_notify != NULL && symtab_was_null)
4551 expansion_notify (symtab);
4552 }
4553 }
4554
4555 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4556 matched, to expand corresponding CUs that were marked. IDX is the
4557 index of the symbol name that matched. */
4558
4559 static void
4560 dw2_expand_marked_cus
4561 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4562 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4563 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4564 search_domain kind)
4565 {
4566 offset_type *vec, vec_len, vec_idx;
4567 bool global_seen = false;
4568 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4569
4570 vec = (offset_type *) (index.constant_pool
4571 + MAYBE_SWAP (index.symbol_table[idx].vec));
4572 vec_len = MAYBE_SWAP (vec[0]);
4573 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4574 {
4575 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4576 /* This value is only valid for index versions >= 7. */
4577 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4578 gdb_index_symbol_kind symbol_kind =
4579 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4580 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4581 /* Only check the symbol attributes if they're present.
4582 Indices prior to version 7 don't record them,
4583 and indices >= 7 may elide them for certain symbols
4584 (gold does this). */
4585 int attrs_valid =
4586 (index.version >= 7
4587 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4588
4589 /* Work around gold/15646. */
4590 if (attrs_valid)
4591 {
4592 if (!is_static && global_seen)
4593 continue;
4594 if (!is_static)
4595 global_seen = true;
4596 }
4597
4598 /* Only check the symbol's kind if it has one. */
4599 if (attrs_valid)
4600 {
4601 switch (kind)
4602 {
4603 case VARIABLES_DOMAIN:
4604 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4605 continue;
4606 break;
4607 case FUNCTIONS_DOMAIN:
4608 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4609 continue;
4610 break;
4611 case TYPES_DOMAIN:
4612 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4613 continue;
4614 break;
4615 case MODULES_DOMAIN:
4616 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4617 continue;
4618 break;
4619 default:
4620 break;
4621 }
4622 }
4623
4624 /* Don't crash on bad data. */
4625 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4626 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4627 {
4628 complaint (_(".gdb_index entry has bad CU index"
4629 " [in module %s]"),
4630 objfile_name (dwarf2_per_objfile->objfile));
4631 continue;
4632 }
4633
4634 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4635 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4636 expansion_notify);
4637 }
4638 }
4639
4640 /* If FILE_MATCHER is non-NULL, set all the
4641 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4642 that match FILE_MATCHER. */
4643
4644 static void
4645 dw_expand_symtabs_matching_file_matcher
4646 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4647 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4648 {
4649 if (file_matcher == NULL)
4650 return;
4651
4652 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4653 htab_eq_pointer,
4654 NULL, xcalloc, xfree));
4655 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4656 htab_eq_pointer,
4657 NULL, xcalloc, xfree));
4658
4659 /* The rule is CUs specify all the files, including those used by
4660 any TU, so there's no need to scan TUs here. */
4661
4662 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4663 {
4664 QUIT;
4665
4666 per_cu->v.quick->mark = 0;
4667
4668 /* We only need to look at symtabs not already expanded. */
4669 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4670 continue;
4671
4672 quick_file_names *file_data = dw2_get_file_names (per_cu);
4673 if (file_data == NULL)
4674 continue;
4675
4676 if (htab_find (visited_not_found.get (), file_data) != NULL)
4677 continue;
4678 else if (htab_find (visited_found.get (), file_data) != NULL)
4679 {
4680 per_cu->v.quick->mark = 1;
4681 continue;
4682 }
4683
4684 for (int j = 0; j < file_data->num_file_names; ++j)
4685 {
4686 const char *this_real_name;
4687
4688 if (file_matcher (file_data->file_names[j], false))
4689 {
4690 per_cu->v.quick->mark = 1;
4691 break;
4692 }
4693
4694 /* Before we invoke realpath, which can get expensive when many
4695 files are involved, do a quick comparison of the basenames. */
4696 if (!basenames_may_differ
4697 && !file_matcher (lbasename (file_data->file_names[j]),
4698 true))
4699 continue;
4700
4701 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4702 file_data, j);
4703 if (file_matcher (this_real_name, false))
4704 {
4705 per_cu->v.quick->mark = 1;
4706 break;
4707 }
4708 }
4709
4710 void **slot = htab_find_slot (per_cu->v.quick->mark
4711 ? visited_found.get ()
4712 : visited_not_found.get (),
4713 file_data, INSERT);
4714 *slot = file_data;
4715 }
4716 }
4717
4718 static void
4719 dw2_expand_symtabs_matching
4720 (struct objfile *objfile,
4721 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4722 const lookup_name_info *lookup_name,
4723 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4724 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4725 enum search_domain kind)
4726 {
4727 struct dwarf2_per_objfile *dwarf2_per_objfile
4728 = get_dwarf2_per_objfile (objfile);
4729
4730 /* index_table is NULL if OBJF_READNOW. */
4731 if (!dwarf2_per_objfile->per_bfd->index_table)
4732 return;
4733
4734 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4735
4736 if (symbol_matcher == NULL && lookup_name == NULL)
4737 {
4738 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4739 {
4740 QUIT;
4741
4742 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4743 file_matcher, expansion_notify);
4744 }
4745 return;
4746 }
4747
4748 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4749
4750 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4751 symbol_matcher,
4752 kind, [&] (offset_type idx)
4753 {
4754 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4755 expansion_notify, kind);
4756 return true;
4757 });
4758 }
4759
4760 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4761 symtab. */
4762
4763 static struct compunit_symtab *
4764 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4765 CORE_ADDR pc)
4766 {
4767 int i;
4768
4769 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4770 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4771 return cust;
4772
4773 if (cust->includes == NULL)
4774 return NULL;
4775
4776 for (i = 0; cust->includes[i]; ++i)
4777 {
4778 struct compunit_symtab *s = cust->includes[i];
4779
4780 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4781 if (s != NULL)
4782 return s;
4783 }
4784
4785 return NULL;
4786 }
4787
4788 static struct compunit_symtab *
4789 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4790 struct bound_minimal_symbol msymbol,
4791 CORE_ADDR pc,
4792 struct obj_section *section,
4793 int warn_if_readin)
4794 {
4795 struct dwarf2_per_cu_data *data;
4796 struct compunit_symtab *result;
4797
4798 if (!objfile->partial_symtabs->psymtabs_addrmap)
4799 return NULL;
4800
4801 CORE_ADDR baseaddr = objfile->text_section_offset ();
4802 data = (struct dwarf2_per_cu_data *) addrmap_find
4803 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4804 if (!data)
4805 return NULL;
4806
4807 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4808 if (warn_if_readin && per_objfile->symtab_set_p (data))
4809 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4810 paddress (objfile->arch (), pc));
4811
4812 result = recursively_find_pc_sect_compunit_symtab
4813 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4814
4815 gdb_assert (result != NULL);
4816 return result;
4817 }
4818
4819 static void
4820 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4821 void *data, int need_fullname)
4822 {
4823 struct dwarf2_per_objfile *dwarf2_per_objfile
4824 = get_dwarf2_per_objfile (objfile);
4825
4826 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4827 {
4828 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4829
4830 htab_up visited (htab_create_alloc (10,
4831 htab_hash_pointer, htab_eq_pointer,
4832 NULL, xcalloc, xfree));
4833
4834 /* The rule is CUs specify all the files, including those used
4835 by any TU, so there's no need to scan TUs here. We can
4836 ignore file names coming from already-expanded CUs. */
4837
4838 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4839 {
4840 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4841 {
4842 void **slot = htab_find_slot (visited.get (),
4843 per_cu->v.quick->file_names,
4844 INSERT);
4845
4846 *slot = per_cu->v.quick->file_names;
4847 }
4848 }
4849
4850 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4851 {
4852 /* We only need to look at symtabs not already expanded. */
4853 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4854 continue;
4855
4856 quick_file_names *file_data = dw2_get_file_names (per_cu);
4857 if (file_data == NULL)
4858 continue;
4859
4860 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4861 if (*slot)
4862 {
4863 /* Already visited. */
4864 continue;
4865 }
4866 *slot = file_data;
4867
4868 for (int j = 0; j < file_data->num_file_names; ++j)
4869 {
4870 const char *filename = file_data->file_names[j];
4871 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4872 }
4873 }
4874 }
4875
4876 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4877 {
4878 gdb::unique_xmalloc_ptr<char> this_real_name;
4879
4880 if (need_fullname)
4881 this_real_name = gdb_realpath (filename);
4882 (*fun) (filename, this_real_name.get (), data);
4883 });
4884 }
4885
4886 static int
4887 dw2_has_symbols (struct objfile *objfile)
4888 {
4889 return 1;
4890 }
4891
4892 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4893 {
4894 dw2_has_symbols,
4895 dw2_find_last_source_symtab,
4896 dw2_forget_cached_source_info,
4897 dw2_map_symtabs_matching_filename,
4898 dw2_lookup_symbol,
4899 NULL,
4900 dw2_print_stats,
4901 dw2_dump,
4902 dw2_expand_symtabs_for_function,
4903 dw2_expand_all_symtabs,
4904 dw2_expand_symtabs_with_fullname,
4905 dw2_map_matching_symbols,
4906 dw2_expand_symtabs_matching,
4907 dw2_find_pc_sect_compunit_symtab,
4908 NULL,
4909 dw2_map_symbol_filenames
4910 };
4911
4912 /* DWARF-5 debug_names reader. */
4913
4914 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4915 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4916
4917 /* A helper function that reads the .debug_names section in SECTION
4918 and fills in MAP. FILENAME is the name of the file containing the
4919 section; it is used for error reporting.
4920
4921 Returns true if all went well, false otherwise. */
4922
4923 static bool
4924 read_debug_names_from_section (struct objfile *objfile,
4925 const char *filename,
4926 struct dwarf2_section_info *section,
4927 mapped_debug_names &map)
4928 {
4929 if (section->empty ())
4930 return false;
4931
4932 /* Older elfutils strip versions could keep the section in the main
4933 executable while splitting it for the separate debug info file. */
4934 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4935 return false;
4936
4937 section->read (objfile);
4938
4939 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4940
4941 const gdb_byte *addr = section->buffer;
4942
4943 bfd *const abfd = section->get_bfd_owner ();
4944
4945 unsigned int bytes_read;
4946 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4947 addr += bytes_read;
4948
4949 map.dwarf5_is_dwarf64 = bytes_read != 4;
4950 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4951 if (bytes_read + length != section->size)
4952 {
4953 /* There may be multiple per-CU indices. */
4954 warning (_("Section .debug_names in %s length %s does not match "
4955 "section length %s, ignoring .debug_names."),
4956 filename, plongest (bytes_read + length),
4957 pulongest (section->size));
4958 return false;
4959 }
4960
4961 /* The version number. */
4962 uint16_t version = read_2_bytes (abfd, addr);
4963 addr += 2;
4964 if (version != 5)
4965 {
4966 warning (_("Section .debug_names in %s has unsupported version %d, "
4967 "ignoring .debug_names."),
4968 filename, version);
4969 return false;
4970 }
4971
4972 /* Padding. */
4973 uint16_t padding = read_2_bytes (abfd, addr);
4974 addr += 2;
4975 if (padding != 0)
4976 {
4977 warning (_("Section .debug_names in %s has unsupported padding %d, "
4978 "ignoring .debug_names."),
4979 filename, padding);
4980 return false;
4981 }
4982
4983 /* comp_unit_count - The number of CUs in the CU list. */
4984 map.cu_count = read_4_bytes (abfd, addr);
4985 addr += 4;
4986
4987 /* local_type_unit_count - The number of TUs in the local TU
4988 list. */
4989 map.tu_count = read_4_bytes (abfd, addr);
4990 addr += 4;
4991
4992 /* foreign_type_unit_count - The number of TUs in the foreign TU
4993 list. */
4994 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4995 addr += 4;
4996 if (foreign_tu_count != 0)
4997 {
4998 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4999 "ignoring .debug_names."),
5000 filename, static_cast<unsigned long> (foreign_tu_count));
5001 return false;
5002 }
5003
5004 /* bucket_count - The number of hash buckets in the hash lookup
5005 table. */
5006 map.bucket_count = read_4_bytes (abfd, addr);
5007 addr += 4;
5008
5009 /* name_count - The number of unique names in the index. */
5010 map.name_count = read_4_bytes (abfd, addr);
5011 addr += 4;
5012
5013 /* abbrev_table_size - The size in bytes of the abbreviations
5014 table. */
5015 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5016 addr += 4;
5017
5018 /* augmentation_string_size - The size in bytes of the augmentation
5019 string. This value is rounded up to a multiple of 4. */
5020 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5021 addr += 4;
5022 map.augmentation_is_gdb = ((augmentation_string_size
5023 == sizeof (dwarf5_augmentation))
5024 && memcmp (addr, dwarf5_augmentation,
5025 sizeof (dwarf5_augmentation)) == 0);
5026 augmentation_string_size += (-augmentation_string_size) & 3;
5027 addr += augmentation_string_size;
5028
5029 /* List of CUs */
5030 map.cu_table_reordered = addr;
5031 addr += map.cu_count * map.offset_size;
5032
5033 /* List of Local TUs */
5034 map.tu_table_reordered = addr;
5035 addr += map.tu_count * map.offset_size;
5036
5037 /* Hash Lookup Table */
5038 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5039 addr += map.bucket_count * 4;
5040 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5041 addr += map.name_count * 4;
5042
5043 /* Name Table */
5044 map.name_table_string_offs_reordered = addr;
5045 addr += map.name_count * map.offset_size;
5046 map.name_table_entry_offs_reordered = addr;
5047 addr += map.name_count * map.offset_size;
5048
5049 const gdb_byte *abbrev_table_start = addr;
5050 for (;;)
5051 {
5052 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5053 addr += bytes_read;
5054 if (index_num == 0)
5055 break;
5056
5057 const auto insertpair
5058 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5059 if (!insertpair.second)
5060 {
5061 warning (_("Section .debug_names in %s has duplicate index %s, "
5062 "ignoring .debug_names."),
5063 filename, pulongest (index_num));
5064 return false;
5065 }
5066 mapped_debug_names::index_val &indexval = insertpair.first->second;
5067 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5068 addr += bytes_read;
5069
5070 for (;;)
5071 {
5072 mapped_debug_names::index_val::attr attr;
5073 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5074 addr += bytes_read;
5075 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5076 addr += bytes_read;
5077 if (attr.form == DW_FORM_implicit_const)
5078 {
5079 attr.implicit_const = read_signed_leb128 (abfd, addr,
5080 &bytes_read);
5081 addr += bytes_read;
5082 }
5083 if (attr.dw_idx == 0 && attr.form == 0)
5084 break;
5085 indexval.attr_vec.push_back (std::move (attr));
5086 }
5087 }
5088 if (addr != abbrev_table_start + abbrev_table_size)
5089 {
5090 warning (_("Section .debug_names in %s has abbreviation_table "
5091 "of size %s vs. written as %u, ignoring .debug_names."),
5092 filename, plongest (addr - abbrev_table_start),
5093 abbrev_table_size);
5094 return false;
5095 }
5096 map.entry_pool = addr;
5097
5098 return true;
5099 }
5100
5101 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5102 list. */
5103
5104 static void
5105 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5106 const mapped_debug_names &map,
5107 dwarf2_section_info &section,
5108 bool is_dwz)
5109 {
5110 if (!map.augmentation_is_gdb)
5111 {
5112 for (uint32_t i = 0; i < map.cu_count; ++i)
5113 {
5114 sect_offset sect_off
5115 = (sect_offset) (extract_unsigned_integer
5116 (map.cu_table_reordered + i * map.offset_size,
5117 map.offset_size,
5118 map.dwarf5_byte_order));
5119 /* We don't know the length of the CU, because the CU list in a
5120 .debug_names index can be incomplete, so we can't use the start of
5121 the next CU as end of this CU. We create the CUs here with length 0,
5122 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5123 dwarf2_per_cu_data *per_cu
5124 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5125 sect_off, 0);
5126 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5127 }
5128 }
5129
5130 sect_offset sect_off_prev;
5131 for (uint32_t i = 0; i <= map.cu_count; ++i)
5132 {
5133 sect_offset sect_off_next;
5134 if (i < map.cu_count)
5135 {
5136 sect_off_next
5137 = (sect_offset) (extract_unsigned_integer
5138 (map.cu_table_reordered + i * map.offset_size,
5139 map.offset_size,
5140 map.dwarf5_byte_order));
5141 }
5142 else
5143 sect_off_next = (sect_offset) section.size;
5144 if (i >= 1)
5145 {
5146 const ULONGEST length = sect_off_next - sect_off_prev;
5147 dwarf2_per_cu_data *per_cu
5148 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5149 sect_off_prev, length);
5150 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5151 }
5152 sect_off_prev = sect_off_next;
5153 }
5154 }
5155
5156 /* Read the CU list from the mapped index, and use it to create all
5157 the CU objects for this dwarf2_per_objfile. */
5158
5159 static void
5160 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5161 const mapped_debug_names &map,
5162 const mapped_debug_names &dwz_map)
5163 {
5164 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5165 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5166
5167 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5168 dwarf2_per_objfile->per_bfd->info,
5169 false /* is_dwz */);
5170
5171 if (dwz_map.cu_count == 0)
5172 return;
5173
5174 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5175 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5176 true /* is_dwz */);
5177 }
5178
5179 /* Read .debug_names. If everything went ok, initialize the "quick"
5180 elements of all the CUs and return true. Otherwise, return false. */
5181
5182 static bool
5183 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5184 {
5185 std::unique_ptr<mapped_debug_names> map
5186 (new mapped_debug_names (dwarf2_per_objfile));
5187 mapped_debug_names dwz_map (dwarf2_per_objfile);
5188 struct objfile *objfile = dwarf2_per_objfile->objfile;
5189
5190 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5191 &dwarf2_per_objfile->per_bfd->debug_names,
5192 *map))
5193 return false;
5194
5195 /* Don't use the index if it's empty. */
5196 if (map->name_count == 0)
5197 return false;
5198
5199 /* If there is a .dwz file, read it so we can get its CU list as
5200 well. */
5201 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5202 if (dwz != NULL)
5203 {
5204 if (!read_debug_names_from_section (objfile,
5205 bfd_get_filename (dwz->dwz_bfd.get ()),
5206 &dwz->debug_names, dwz_map))
5207 {
5208 warning (_("could not read '.debug_names' section from %s; skipping"),
5209 bfd_get_filename (dwz->dwz_bfd.get ()));
5210 return false;
5211 }
5212 }
5213
5214 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5215
5216 if (map->tu_count != 0)
5217 {
5218 /* We can only handle a single .debug_types when we have an
5219 index. */
5220 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5221 return false;
5222
5223 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5224
5225 create_signatured_type_table_from_debug_names
5226 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5227 }
5228
5229 create_addrmap_from_aranges (dwarf2_per_objfile,
5230 &dwarf2_per_objfile->per_bfd->debug_aranges);
5231
5232 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5233 dwarf2_per_objfile->per_bfd->using_index = 1;
5234 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5235 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5236
5237 return true;
5238 }
5239
5240 /* Type used to manage iterating over all CUs looking for a symbol for
5241 .debug_names. */
5242
5243 class dw2_debug_names_iterator
5244 {
5245 public:
5246 dw2_debug_names_iterator (const mapped_debug_names &map,
5247 gdb::optional<block_enum> block_index,
5248 domain_enum domain,
5249 const char *name)
5250 : m_map (map), m_block_index (block_index), m_domain (domain),
5251 m_addr (find_vec_in_debug_names (map, name))
5252 {}
5253
5254 dw2_debug_names_iterator (const mapped_debug_names &map,
5255 search_domain search, uint32_t namei)
5256 : m_map (map),
5257 m_search (search),
5258 m_addr (find_vec_in_debug_names (map, namei))
5259 {}
5260
5261 dw2_debug_names_iterator (const mapped_debug_names &map,
5262 block_enum block_index, domain_enum domain,
5263 uint32_t namei)
5264 : m_map (map), m_block_index (block_index), m_domain (domain),
5265 m_addr (find_vec_in_debug_names (map, namei))
5266 {}
5267
5268 /* Return the next matching CU or NULL if there are no more. */
5269 dwarf2_per_cu_data *next ();
5270
5271 private:
5272 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5273 const char *name);
5274 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5275 uint32_t namei);
5276
5277 /* The internalized form of .debug_names. */
5278 const mapped_debug_names &m_map;
5279
5280 /* If set, only look for symbols that match that block. Valid values are
5281 GLOBAL_BLOCK and STATIC_BLOCK. */
5282 const gdb::optional<block_enum> m_block_index;
5283
5284 /* The kind of symbol we're looking for. */
5285 const domain_enum m_domain = UNDEF_DOMAIN;
5286 const search_domain m_search = ALL_DOMAIN;
5287
5288 /* The list of CUs from the index entry of the symbol, or NULL if
5289 not found. */
5290 const gdb_byte *m_addr;
5291 };
5292
5293 const char *
5294 mapped_debug_names::namei_to_name (uint32_t namei) const
5295 {
5296 const ULONGEST namei_string_offs
5297 = extract_unsigned_integer ((name_table_string_offs_reordered
5298 + namei * offset_size),
5299 offset_size,
5300 dwarf5_byte_order);
5301 return read_indirect_string_at_offset (dwarf2_per_objfile,
5302 namei_string_offs);
5303 }
5304
5305 /* Find a slot in .debug_names for the object named NAME. If NAME is
5306 found, return pointer to its pool data. If NAME cannot be found,
5307 return NULL. */
5308
5309 const gdb_byte *
5310 dw2_debug_names_iterator::find_vec_in_debug_names
5311 (const mapped_debug_names &map, const char *name)
5312 {
5313 int (*cmp) (const char *, const char *);
5314
5315 gdb::unique_xmalloc_ptr<char> without_params;
5316 if (current_language->la_language == language_cplus
5317 || current_language->la_language == language_fortran
5318 || current_language->la_language == language_d)
5319 {
5320 /* NAME is already canonical. Drop any qualifiers as
5321 .debug_names does not contain any. */
5322
5323 if (strchr (name, '(') != NULL)
5324 {
5325 without_params = cp_remove_params (name);
5326 if (without_params != NULL)
5327 name = without_params.get ();
5328 }
5329 }
5330
5331 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5332
5333 const uint32_t full_hash = dwarf5_djb_hash (name);
5334 uint32_t namei
5335 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5336 (map.bucket_table_reordered
5337 + (full_hash % map.bucket_count)), 4,
5338 map.dwarf5_byte_order);
5339 if (namei == 0)
5340 return NULL;
5341 --namei;
5342 if (namei >= map.name_count)
5343 {
5344 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5345 "[in module %s]"),
5346 namei, map.name_count,
5347 objfile_name (map.dwarf2_per_objfile->objfile));
5348 return NULL;
5349 }
5350
5351 for (;;)
5352 {
5353 const uint32_t namei_full_hash
5354 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5355 (map.hash_table_reordered + namei), 4,
5356 map.dwarf5_byte_order);
5357 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5358 return NULL;
5359
5360 if (full_hash == namei_full_hash)
5361 {
5362 const char *const namei_string = map.namei_to_name (namei);
5363
5364 #if 0 /* An expensive sanity check. */
5365 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5366 {
5367 complaint (_("Wrong .debug_names hash for string at index %u "
5368 "[in module %s]"),
5369 namei, objfile_name (dwarf2_per_objfile->objfile));
5370 return NULL;
5371 }
5372 #endif
5373
5374 if (cmp (namei_string, name) == 0)
5375 {
5376 const ULONGEST namei_entry_offs
5377 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5378 + namei * map.offset_size),
5379 map.offset_size, map.dwarf5_byte_order);
5380 return map.entry_pool + namei_entry_offs;
5381 }
5382 }
5383
5384 ++namei;
5385 if (namei >= map.name_count)
5386 return NULL;
5387 }
5388 }
5389
5390 const gdb_byte *
5391 dw2_debug_names_iterator::find_vec_in_debug_names
5392 (const mapped_debug_names &map, uint32_t namei)
5393 {
5394 if (namei >= map.name_count)
5395 {
5396 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5397 "[in module %s]"),
5398 namei, map.name_count,
5399 objfile_name (map.dwarf2_per_objfile->objfile));
5400 return NULL;
5401 }
5402
5403 const ULONGEST namei_entry_offs
5404 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5405 + namei * map.offset_size),
5406 map.offset_size, map.dwarf5_byte_order);
5407 return map.entry_pool + namei_entry_offs;
5408 }
5409
5410 /* See dw2_debug_names_iterator. */
5411
5412 dwarf2_per_cu_data *
5413 dw2_debug_names_iterator::next ()
5414 {
5415 if (m_addr == NULL)
5416 return NULL;
5417
5418 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5419 struct objfile *objfile = dwarf2_per_objfile->objfile;
5420 bfd *const abfd = objfile->obfd;
5421
5422 again:
5423
5424 unsigned int bytes_read;
5425 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5426 m_addr += bytes_read;
5427 if (abbrev == 0)
5428 return NULL;
5429
5430 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5431 if (indexval_it == m_map.abbrev_map.cend ())
5432 {
5433 complaint (_("Wrong .debug_names undefined abbrev code %s "
5434 "[in module %s]"),
5435 pulongest (abbrev), objfile_name (objfile));
5436 return NULL;
5437 }
5438 const mapped_debug_names::index_val &indexval = indexval_it->second;
5439 enum class symbol_linkage {
5440 unknown,
5441 static_,
5442 extern_,
5443 } symbol_linkage_ = symbol_linkage::unknown;
5444 dwarf2_per_cu_data *per_cu = NULL;
5445 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5446 {
5447 ULONGEST ull;
5448 switch (attr.form)
5449 {
5450 case DW_FORM_implicit_const:
5451 ull = attr.implicit_const;
5452 break;
5453 case DW_FORM_flag_present:
5454 ull = 1;
5455 break;
5456 case DW_FORM_udata:
5457 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5458 m_addr += bytes_read;
5459 break;
5460 case DW_FORM_ref4:
5461 ull = read_4_bytes (abfd, m_addr);
5462 m_addr += 4;
5463 break;
5464 case DW_FORM_ref8:
5465 ull = read_8_bytes (abfd, m_addr);
5466 m_addr += 8;
5467 break;
5468 case DW_FORM_ref_sig8:
5469 ull = read_8_bytes (abfd, m_addr);
5470 m_addr += 8;
5471 break;
5472 default:
5473 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5474 dwarf_form_name (attr.form),
5475 objfile_name (objfile));
5476 return NULL;
5477 }
5478 switch (attr.dw_idx)
5479 {
5480 case DW_IDX_compile_unit:
5481 /* Don't crash on bad data. */
5482 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5483 {
5484 complaint (_(".debug_names entry has bad CU index %s"
5485 " [in module %s]"),
5486 pulongest (ull),
5487 objfile_name (dwarf2_per_objfile->objfile));
5488 continue;
5489 }
5490 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5491 break;
5492 case DW_IDX_type_unit:
5493 /* Don't crash on bad data. */
5494 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5495 {
5496 complaint (_(".debug_names entry has bad TU index %s"
5497 " [in module %s]"),
5498 pulongest (ull),
5499 objfile_name (dwarf2_per_objfile->objfile));
5500 continue;
5501 }
5502 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5503 break;
5504 case DW_IDX_die_offset:
5505 /* In a per-CU index (as opposed to a per-module index), index
5506 entries without CU attribute implicitly refer to the single CU. */
5507 if (per_cu == NULL)
5508 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5509 break;
5510 case DW_IDX_GNU_internal:
5511 if (!m_map.augmentation_is_gdb)
5512 break;
5513 symbol_linkage_ = symbol_linkage::static_;
5514 break;
5515 case DW_IDX_GNU_external:
5516 if (!m_map.augmentation_is_gdb)
5517 break;
5518 symbol_linkage_ = symbol_linkage::extern_;
5519 break;
5520 }
5521 }
5522
5523 /* Skip if already read in. */
5524 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5525 goto again;
5526
5527 /* Check static vs global. */
5528 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5529 {
5530 const bool want_static = *m_block_index == STATIC_BLOCK;
5531 const bool symbol_is_static =
5532 symbol_linkage_ == symbol_linkage::static_;
5533 if (want_static != symbol_is_static)
5534 goto again;
5535 }
5536
5537 /* Match dw2_symtab_iter_next, symbol_kind
5538 and debug_names::psymbol_tag. */
5539 switch (m_domain)
5540 {
5541 case VAR_DOMAIN:
5542 switch (indexval.dwarf_tag)
5543 {
5544 case DW_TAG_variable:
5545 case DW_TAG_subprogram:
5546 /* Some types are also in VAR_DOMAIN. */
5547 case DW_TAG_typedef:
5548 case DW_TAG_structure_type:
5549 break;
5550 default:
5551 goto again;
5552 }
5553 break;
5554 case STRUCT_DOMAIN:
5555 switch (indexval.dwarf_tag)
5556 {
5557 case DW_TAG_typedef:
5558 case DW_TAG_structure_type:
5559 break;
5560 default:
5561 goto again;
5562 }
5563 break;
5564 case LABEL_DOMAIN:
5565 switch (indexval.dwarf_tag)
5566 {
5567 case 0:
5568 case DW_TAG_variable:
5569 break;
5570 default:
5571 goto again;
5572 }
5573 break;
5574 case MODULE_DOMAIN:
5575 switch (indexval.dwarf_tag)
5576 {
5577 case DW_TAG_module:
5578 break;
5579 default:
5580 goto again;
5581 }
5582 break;
5583 default:
5584 break;
5585 }
5586
5587 /* Match dw2_expand_symtabs_matching, symbol_kind and
5588 debug_names::psymbol_tag. */
5589 switch (m_search)
5590 {
5591 case VARIABLES_DOMAIN:
5592 switch (indexval.dwarf_tag)
5593 {
5594 case DW_TAG_variable:
5595 break;
5596 default:
5597 goto again;
5598 }
5599 break;
5600 case FUNCTIONS_DOMAIN:
5601 switch (indexval.dwarf_tag)
5602 {
5603 case DW_TAG_subprogram:
5604 break;
5605 default:
5606 goto again;
5607 }
5608 break;
5609 case TYPES_DOMAIN:
5610 switch (indexval.dwarf_tag)
5611 {
5612 case DW_TAG_typedef:
5613 case DW_TAG_structure_type:
5614 break;
5615 default:
5616 goto again;
5617 }
5618 break;
5619 case MODULES_DOMAIN:
5620 switch (indexval.dwarf_tag)
5621 {
5622 case DW_TAG_module:
5623 break;
5624 default:
5625 goto again;
5626 }
5627 default:
5628 break;
5629 }
5630
5631 return per_cu;
5632 }
5633
5634 static struct compunit_symtab *
5635 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5636 const char *name, domain_enum domain)
5637 {
5638 struct dwarf2_per_objfile *dwarf2_per_objfile
5639 = get_dwarf2_per_objfile (objfile);
5640
5641 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5642 if (!mapp)
5643 {
5644 /* index is NULL if OBJF_READNOW. */
5645 return NULL;
5646 }
5647 const auto &map = *mapp;
5648
5649 dw2_debug_names_iterator iter (map, block_index, domain, name);
5650
5651 struct compunit_symtab *stab_best = NULL;
5652 struct dwarf2_per_cu_data *per_cu;
5653 while ((per_cu = iter.next ()) != NULL)
5654 {
5655 struct symbol *sym, *with_opaque = NULL;
5656 compunit_symtab *stab
5657 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5658 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5659 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5660
5661 sym = block_find_symbol (block, name, domain,
5662 block_find_non_opaque_type_preferred,
5663 &with_opaque);
5664
5665 /* Some caution must be observed with overloaded functions and
5666 methods, since the index will not contain any overload
5667 information (but NAME might contain it). */
5668
5669 if (sym != NULL
5670 && strcmp_iw (sym->search_name (), name) == 0)
5671 return stab;
5672 if (with_opaque != NULL
5673 && strcmp_iw (with_opaque->search_name (), name) == 0)
5674 stab_best = stab;
5675
5676 /* Keep looking through other CUs. */
5677 }
5678
5679 return stab_best;
5680 }
5681
5682 /* This dumps minimal information about .debug_names. It is called
5683 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5684 uses this to verify that .debug_names has been loaded. */
5685
5686 static void
5687 dw2_debug_names_dump (struct objfile *objfile)
5688 {
5689 struct dwarf2_per_objfile *dwarf2_per_objfile
5690 = get_dwarf2_per_objfile (objfile);
5691
5692 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5693 printf_filtered (".debug_names:");
5694 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5695 printf_filtered (" exists\n");
5696 else
5697 printf_filtered (" faked for \"readnow\"\n");
5698 printf_filtered ("\n");
5699 }
5700
5701 static void
5702 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5703 const char *func_name)
5704 {
5705 struct dwarf2_per_objfile *dwarf2_per_objfile
5706 = get_dwarf2_per_objfile (objfile);
5707
5708 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5709 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5710 {
5711 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5712
5713 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5714
5715 struct dwarf2_per_cu_data *per_cu;
5716 while ((per_cu = iter.next ()) != NULL)
5717 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5718 }
5719 }
5720
5721 static void
5722 dw2_debug_names_map_matching_symbols
5723 (struct objfile *objfile,
5724 const lookup_name_info &name, domain_enum domain,
5725 int global,
5726 gdb::function_view<symbol_found_callback_ftype> callback,
5727 symbol_compare_ftype *ordered_compare)
5728 {
5729 struct dwarf2_per_objfile *dwarf2_per_objfile
5730 = get_dwarf2_per_objfile (objfile);
5731
5732 /* debug_names_table is NULL if OBJF_READNOW. */
5733 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5734 return;
5735
5736 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5737 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5738
5739 const char *match_name = name.ada ().lookup_name ().c_str ();
5740 auto matcher = [&] (const char *symname)
5741 {
5742 if (ordered_compare == nullptr)
5743 return true;
5744 return ordered_compare (symname, match_name) == 0;
5745 };
5746
5747 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5748 [&] (offset_type namei)
5749 {
5750 /* The name was matched, now expand corresponding CUs that were
5751 marked. */
5752 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5753
5754 struct dwarf2_per_cu_data *per_cu;
5755 while ((per_cu = iter.next ()) != NULL)
5756 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5757 nullptr);
5758 return true;
5759 });
5760
5761 /* It's a shame we couldn't do this inside the
5762 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5763 that have already been expanded. Instead, this loop matches what
5764 the psymtab code does. */
5765 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5766 {
5767 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5768 if (symtab != nullptr)
5769 {
5770 const struct block *block
5771 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5772 if (!iterate_over_symbols_terminated (block, name,
5773 domain, callback))
5774 break;
5775 }
5776 }
5777 }
5778
5779 static void
5780 dw2_debug_names_expand_symtabs_matching
5781 (struct objfile *objfile,
5782 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5783 const lookup_name_info *lookup_name,
5784 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5785 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5786 enum search_domain kind)
5787 {
5788 struct dwarf2_per_objfile *dwarf2_per_objfile
5789 = get_dwarf2_per_objfile (objfile);
5790
5791 /* debug_names_table is NULL if OBJF_READNOW. */
5792 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5793 return;
5794
5795 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5796
5797 if (symbol_matcher == NULL && lookup_name == NULL)
5798 {
5799 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5800 {
5801 QUIT;
5802
5803 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5804 file_matcher, expansion_notify);
5805 }
5806 return;
5807 }
5808
5809 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5810
5811 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5812 symbol_matcher,
5813 kind, [&] (offset_type namei)
5814 {
5815 /* The name was matched, now expand corresponding CUs that were
5816 marked. */
5817 dw2_debug_names_iterator iter (map, kind, namei);
5818
5819 struct dwarf2_per_cu_data *per_cu;
5820 while ((per_cu = iter.next ()) != NULL)
5821 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5822 file_matcher, expansion_notify);
5823 return true;
5824 });
5825 }
5826
5827 const struct quick_symbol_functions dwarf2_debug_names_functions =
5828 {
5829 dw2_has_symbols,
5830 dw2_find_last_source_symtab,
5831 dw2_forget_cached_source_info,
5832 dw2_map_symtabs_matching_filename,
5833 dw2_debug_names_lookup_symbol,
5834 NULL,
5835 dw2_print_stats,
5836 dw2_debug_names_dump,
5837 dw2_debug_names_expand_symtabs_for_function,
5838 dw2_expand_all_symtabs,
5839 dw2_expand_symtabs_with_fullname,
5840 dw2_debug_names_map_matching_symbols,
5841 dw2_debug_names_expand_symtabs_matching,
5842 dw2_find_pc_sect_compunit_symtab,
5843 NULL,
5844 dw2_map_symbol_filenames
5845 };
5846
5847 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5848 to either a dwarf2_per_bfd or dwz_file object. */
5849
5850 template <typename T>
5851 static gdb::array_view<const gdb_byte>
5852 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5853 {
5854 dwarf2_section_info *section = &section_owner->gdb_index;
5855
5856 if (section->empty ())
5857 return {};
5858
5859 /* Older elfutils strip versions could keep the section in the main
5860 executable while splitting it for the separate debug info file. */
5861 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5862 return {};
5863
5864 section->read (obj);
5865
5866 /* dwarf2_section_info::size is a bfd_size_type, while
5867 gdb::array_view works with size_t. On 32-bit hosts, with
5868 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5869 is 32-bit. So we need an explicit narrowing conversion here.
5870 This is fine, because it's impossible to allocate or mmap an
5871 array/buffer larger than what size_t can represent. */
5872 return gdb::make_array_view (section->buffer, section->size);
5873 }
5874
5875 /* Lookup the index cache for the contents of the index associated to
5876 DWARF2_OBJ. */
5877
5878 static gdb::array_view<const gdb_byte>
5879 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5880 {
5881 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5882 if (build_id == nullptr)
5883 return {};
5884
5885 return global_index_cache.lookup_gdb_index (build_id,
5886 &dwarf2_per_bfd->index_cache_res);
5887 }
5888
5889 /* Same as the above, but for DWZ. */
5890
5891 static gdb::array_view<const gdb_byte>
5892 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5893 {
5894 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5895 if (build_id == nullptr)
5896 return {};
5897
5898 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5899 }
5900
5901 /* See symfile.h. */
5902
5903 bool
5904 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5905 {
5906 struct dwarf2_per_objfile *dwarf2_per_objfile
5907 = get_dwarf2_per_objfile (objfile);
5908
5909 /* If we're about to read full symbols, don't bother with the
5910 indices. In this case we also don't care if some other debug
5911 format is making psymtabs, because they are all about to be
5912 expanded anyway. */
5913 if ((objfile->flags & OBJF_READNOW))
5914 {
5915 dwarf2_per_objfile->per_bfd->using_index = 1;
5916 create_all_comp_units (dwarf2_per_objfile);
5917 create_all_type_units (dwarf2_per_objfile);
5918 dwarf2_per_objfile->per_bfd->quick_file_names_table
5919 = create_quick_file_names_table
5920 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5921 dwarf2_per_objfile->resize_symtabs ();
5922
5923 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5924 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5925 {
5926 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5927
5928 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5929 struct dwarf2_per_cu_quick_data);
5930 }
5931
5932 /* Return 1 so that gdb sees the "quick" functions. However,
5933 these functions will be no-ops because we will have expanded
5934 all symtabs. */
5935 *index_kind = dw_index_kind::GDB_INDEX;
5936 return true;
5937 }
5938
5939 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5940 {
5941 *index_kind = dw_index_kind::DEBUG_NAMES;
5942 dwarf2_per_objfile->resize_symtabs ();
5943 return true;
5944 }
5945
5946 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5947 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5948 get_gdb_index_contents_from_section<dwz_file>))
5949 {
5950 *index_kind = dw_index_kind::GDB_INDEX;
5951 dwarf2_per_objfile->resize_symtabs ();
5952 return true;
5953 }
5954
5955 /* ... otherwise, try to find the index in the index cache. */
5956 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5957 get_gdb_index_contents_from_cache,
5958 get_gdb_index_contents_from_cache_dwz))
5959 {
5960 global_index_cache.hit ();
5961 *index_kind = dw_index_kind::GDB_INDEX;
5962 dwarf2_per_objfile->resize_symtabs ();
5963 return true;
5964 }
5965
5966 global_index_cache.miss ();
5967 return false;
5968 }
5969
5970 \f
5971
5972 /* Build a partial symbol table. */
5973
5974 void
5975 dwarf2_build_psymtabs (struct objfile *objfile)
5976 {
5977 struct dwarf2_per_objfile *dwarf2_per_objfile
5978 = get_dwarf2_per_objfile (objfile);
5979
5980 init_psymbol_list (objfile, 1024);
5981
5982 try
5983 {
5984 /* This isn't really ideal: all the data we allocate on the
5985 objfile's obstack is still uselessly kept around. However,
5986 freeing it seems unsafe. */
5987 psymtab_discarder psymtabs (objfile);
5988 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5989 psymtabs.keep ();
5990
5991 dwarf2_per_objfile->resize_symtabs ();
5992
5993 /* (maybe) store an index in the cache. */
5994 global_index_cache.store (dwarf2_per_objfile);
5995 }
5996 catch (const gdb_exception_error &except)
5997 {
5998 exception_print (gdb_stderr, except);
5999 }
6000 }
6001
6002 /* Find the base address of the compilation unit for range lists and
6003 location lists. It will normally be specified by DW_AT_low_pc.
6004 In DWARF-3 draft 4, the base address could be overridden by
6005 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6006 compilation units with discontinuous ranges. */
6007
6008 static void
6009 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6010 {
6011 struct attribute *attr;
6012
6013 cu->base_address.reset ();
6014
6015 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6016 if (attr != nullptr)
6017 cu->base_address = attr->value_as_address ();
6018 else
6019 {
6020 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6021 if (attr != nullptr)
6022 cu->base_address = attr->value_as_address ();
6023 }
6024 }
6025
6026 /* Helper function that returns the proper abbrev section for
6027 THIS_CU. */
6028
6029 static struct dwarf2_section_info *
6030 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6031 {
6032 struct dwarf2_section_info *abbrev;
6033 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6034
6035 if (this_cu->is_dwz)
6036 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6037 else
6038 abbrev = &dwarf2_per_objfile->per_bfd->abbrev;
6039
6040 return abbrev;
6041 }
6042
6043 /* Fetch the abbreviation table offset from a comp or type unit header. */
6044
6045 static sect_offset
6046 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6047 struct dwarf2_section_info *section,
6048 sect_offset sect_off)
6049 {
6050 bfd *abfd = section->get_bfd_owner ();
6051 const gdb_byte *info_ptr;
6052 unsigned int initial_length_size, offset_size;
6053 uint16_t version;
6054
6055 section->read (dwarf2_per_objfile->objfile);
6056 info_ptr = section->buffer + to_underlying (sect_off);
6057 read_initial_length (abfd, info_ptr, &initial_length_size);
6058 offset_size = initial_length_size == 4 ? 4 : 8;
6059 info_ptr += initial_length_size;
6060
6061 version = read_2_bytes (abfd, info_ptr);
6062 info_ptr += 2;
6063 if (version >= 5)
6064 {
6065 /* Skip unit type and address size. */
6066 info_ptr += 2;
6067 }
6068
6069 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6070 }
6071
6072 /* A partial symtab that is used only for include files. */
6073 struct dwarf2_include_psymtab : public partial_symtab
6074 {
6075 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6076 : partial_symtab (filename, objfile)
6077 {
6078 }
6079
6080 void read_symtab (struct objfile *objfile) override
6081 {
6082 /* It's an include file, no symbols to read for it.
6083 Everything is in the includer symtab. */
6084
6085 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6086 expansion of the includer psymtab. We use the dependencies[0] field to
6087 model the includer. But if we go the regular route of calling
6088 expand_psymtab here, and having expand_psymtab call expand_dependencies
6089 to expand the includer, we'll only use expand_psymtab on the includer
6090 (making it a non-toplevel psymtab), while if we expand the includer via
6091 another path, we'll use read_symtab (making it a toplevel psymtab).
6092 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6093 psymtab, and trigger read_symtab on the includer here directly. */
6094 includer ()->read_symtab (objfile);
6095 }
6096
6097 void expand_psymtab (struct objfile *objfile) override
6098 {
6099 /* This is not called by read_symtab, and should not be called by any
6100 expand_dependencies. */
6101 gdb_assert (false);
6102 }
6103
6104 bool readin_p (struct objfile *objfile) const override
6105 {
6106 return includer ()->readin_p (objfile);
6107 }
6108
6109 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6110 {
6111 return nullptr;
6112 }
6113
6114 private:
6115 partial_symtab *includer () const
6116 {
6117 /* An include psymtab has exactly one dependency: the psymtab that
6118 includes it. */
6119 gdb_assert (this->number_of_dependencies == 1);
6120 return this->dependencies[0];
6121 }
6122 };
6123
6124 /* Allocate a new partial symtab for file named NAME and mark this new
6125 partial symtab as being an include of PST. */
6126
6127 static void
6128 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6129 struct objfile *objfile)
6130 {
6131 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6132
6133 if (!IS_ABSOLUTE_PATH (subpst->filename))
6134 subpst->dirname = pst->dirname;
6135
6136 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6137 subpst->dependencies[0] = pst;
6138 subpst->number_of_dependencies = 1;
6139 }
6140
6141 /* Read the Line Number Program data and extract the list of files
6142 included by the source file represented by PST. Build an include
6143 partial symtab for each of these included files. */
6144
6145 static void
6146 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6147 struct die_info *die,
6148 dwarf2_psymtab *pst)
6149 {
6150 line_header_up lh;
6151 struct attribute *attr;
6152
6153 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6154 if (attr != nullptr)
6155 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6156 if (lh == NULL)
6157 return; /* No linetable, so no includes. */
6158
6159 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6160 that we pass in the raw text_low here; that is ok because we're
6161 only decoding the line table to make include partial symtabs, and
6162 so the addresses aren't really used. */
6163 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6164 pst->raw_text_low (), 1);
6165 }
6166
6167 static hashval_t
6168 hash_signatured_type (const void *item)
6169 {
6170 const struct signatured_type *sig_type
6171 = (const struct signatured_type *) item;
6172
6173 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6174 return sig_type->signature;
6175 }
6176
6177 static int
6178 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6179 {
6180 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6181 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6182
6183 return lhs->signature == rhs->signature;
6184 }
6185
6186 /* Allocate a hash table for signatured types. */
6187
6188 static htab_up
6189 allocate_signatured_type_table ()
6190 {
6191 return htab_up (htab_create_alloc (41,
6192 hash_signatured_type,
6193 eq_signatured_type,
6194 NULL, xcalloc, xfree));
6195 }
6196
6197 /* A helper function to add a signatured type CU to a table. */
6198
6199 static int
6200 add_signatured_type_cu_to_table (void **slot, void *datum)
6201 {
6202 struct signatured_type *sigt = (struct signatured_type *) *slot;
6203 std::vector<signatured_type *> *all_type_units
6204 = (std::vector<signatured_type *> *) datum;
6205
6206 all_type_units->push_back (sigt);
6207
6208 return 1;
6209 }
6210
6211 /* A helper for create_debug_types_hash_table. Read types from SECTION
6212 and fill them into TYPES_HTAB. It will process only type units,
6213 therefore DW_UT_type. */
6214
6215 static void
6216 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6217 struct dwo_file *dwo_file,
6218 dwarf2_section_info *section, htab_up &types_htab,
6219 rcuh_kind section_kind)
6220 {
6221 struct objfile *objfile = dwarf2_per_objfile->objfile;
6222 struct dwarf2_section_info *abbrev_section;
6223 bfd *abfd;
6224 const gdb_byte *info_ptr, *end_ptr;
6225
6226 abbrev_section = (dwo_file != NULL
6227 ? &dwo_file->sections.abbrev
6228 : &dwarf2_per_objfile->per_bfd->abbrev);
6229
6230 if (dwarf_read_debug)
6231 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6232 section->get_name (),
6233 abbrev_section->get_file_name ());
6234
6235 section->read (objfile);
6236 info_ptr = section->buffer;
6237
6238 if (info_ptr == NULL)
6239 return;
6240
6241 /* We can't set abfd until now because the section may be empty or
6242 not present, in which case the bfd is unknown. */
6243 abfd = section->get_bfd_owner ();
6244
6245 /* We don't use cutu_reader here because we don't need to read
6246 any dies: the signature is in the header. */
6247
6248 end_ptr = info_ptr + section->size;
6249 while (info_ptr < end_ptr)
6250 {
6251 struct signatured_type *sig_type;
6252 struct dwo_unit *dwo_tu;
6253 void **slot;
6254 const gdb_byte *ptr = info_ptr;
6255 struct comp_unit_head header;
6256 unsigned int length;
6257
6258 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6259
6260 /* Initialize it due to a false compiler warning. */
6261 header.signature = -1;
6262 header.type_cu_offset_in_tu = (cu_offset) -1;
6263
6264 /* We need to read the type's signature in order to build the hash
6265 table, but we don't need anything else just yet. */
6266
6267 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6268 abbrev_section, ptr, section_kind);
6269
6270 length = header.get_length ();
6271
6272 /* Skip dummy type units. */
6273 if (ptr >= info_ptr + length
6274 || peek_abbrev_code (abfd, ptr) == 0
6275 || header.unit_type != DW_UT_type)
6276 {
6277 info_ptr += length;
6278 continue;
6279 }
6280
6281 if (types_htab == NULL)
6282 {
6283 if (dwo_file)
6284 types_htab = allocate_dwo_unit_table ();
6285 else
6286 types_htab = allocate_signatured_type_table ();
6287 }
6288
6289 if (dwo_file)
6290 {
6291 sig_type = NULL;
6292 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6293 struct dwo_unit);
6294 dwo_tu->dwo_file = dwo_file;
6295 dwo_tu->signature = header.signature;
6296 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6297 dwo_tu->section = section;
6298 dwo_tu->sect_off = sect_off;
6299 dwo_tu->length = length;
6300 }
6301 else
6302 {
6303 /* N.B.: type_offset is not usable if this type uses a DWO file.
6304 The real type_offset is in the DWO file. */
6305 dwo_tu = NULL;
6306 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6307 sig_type->signature = header.signature;
6308 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6309 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6310 sig_type->per_cu.is_debug_types = 1;
6311 sig_type->per_cu.section = section;
6312 sig_type->per_cu.sect_off = sect_off;
6313 sig_type->per_cu.length = length;
6314 }
6315
6316 slot = htab_find_slot (types_htab.get (),
6317 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6318 INSERT);
6319 gdb_assert (slot != NULL);
6320 if (*slot != NULL)
6321 {
6322 sect_offset dup_sect_off;
6323
6324 if (dwo_file)
6325 {
6326 const struct dwo_unit *dup_tu
6327 = (const struct dwo_unit *) *slot;
6328
6329 dup_sect_off = dup_tu->sect_off;
6330 }
6331 else
6332 {
6333 const struct signatured_type *dup_tu
6334 = (const struct signatured_type *) *slot;
6335
6336 dup_sect_off = dup_tu->per_cu.sect_off;
6337 }
6338
6339 complaint (_("debug type entry at offset %s is duplicate to"
6340 " the entry at offset %s, signature %s"),
6341 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6342 hex_string (header.signature));
6343 }
6344 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6345
6346 if (dwarf_read_debug > 1)
6347 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6348 sect_offset_str (sect_off),
6349 hex_string (header.signature));
6350
6351 info_ptr += length;
6352 }
6353 }
6354
6355 /* Create the hash table of all entries in the .debug_types
6356 (or .debug_types.dwo) section(s).
6357 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6358 otherwise it is NULL.
6359
6360 The result is a pointer to the hash table or NULL if there are no types.
6361
6362 Note: This function processes DWO files only, not DWP files. */
6363
6364 static void
6365 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6366 struct dwo_file *dwo_file,
6367 gdb::array_view<dwarf2_section_info> type_sections,
6368 htab_up &types_htab)
6369 {
6370 for (dwarf2_section_info &section : type_sections)
6371 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6372 types_htab, rcuh_kind::TYPE);
6373 }
6374
6375 /* Create the hash table of all entries in the .debug_types section,
6376 and initialize all_type_units.
6377 The result is zero if there is an error (e.g. missing .debug_types section),
6378 otherwise non-zero. */
6379
6380 static int
6381 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6382 {
6383 htab_up types_htab;
6384
6385 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6386 &dwarf2_per_objfile->per_bfd->info, types_htab,
6387 rcuh_kind::COMPILE);
6388 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6389 dwarf2_per_objfile->per_bfd->types, types_htab);
6390 if (types_htab == NULL)
6391 {
6392 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6393 return 0;
6394 }
6395
6396 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6397
6398 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6399 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6400 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6401
6402 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6403 add_signatured_type_cu_to_table,
6404 &dwarf2_per_objfile->per_bfd->all_type_units);
6405
6406 return 1;
6407 }
6408
6409 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6410 If SLOT is non-NULL, it is the entry to use in the hash table.
6411 Otherwise we find one. */
6412
6413 static struct signatured_type *
6414 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6415 void **slot)
6416 {
6417 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6418 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6419 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6420
6421 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6422
6423 dwarf2_per_objfile->resize_symtabs ();
6424
6425 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6426 sig_type->signature = sig;
6427 sig_type->per_cu.is_debug_types = 1;
6428 if (dwarf2_per_objfile->per_bfd->using_index)
6429 {
6430 sig_type->per_cu.v.quick =
6431 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6432 struct dwarf2_per_cu_quick_data);
6433 }
6434
6435 if (slot == NULL)
6436 {
6437 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6438 sig_type, INSERT);
6439 }
6440 gdb_assert (*slot == NULL);
6441 *slot = sig_type;
6442 /* The rest of sig_type must be filled in by the caller. */
6443 return sig_type;
6444 }
6445
6446 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6447 Fill in SIG_ENTRY with DWO_ENTRY. */
6448
6449 static void
6450 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6451 struct signatured_type *sig_entry,
6452 struct dwo_unit *dwo_entry)
6453 {
6454 /* Make sure we're not clobbering something we don't expect to. */
6455 gdb_assert (! sig_entry->per_cu.queued);
6456 gdb_assert (sig_entry->per_cu.cu == NULL);
6457 if (dwarf2_per_objfile->per_bfd->using_index)
6458 {
6459 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6460 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6461 }
6462 else
6463 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6464 gdb_assert (sig_entry->signature == dwo_entry->signature);
6465 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6466 gdb_assert (sig_entry->type_unit_group == NULL);
6467 gdb_assert (sig_entry->dwo_unit == NULL);
6468
6469 sig_entry->per_cu.section = dwo_entry->section;
6470 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6471 sig_entry->per_cu.length = dwo_entry->length;
6472 sig_entry->per_cu.reading_dwo_directly = 1;
6473 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6474 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6475 sig_entry->dwo_unit = dwo_entry;
6476 }
6477
6478 /* Subroutine of lookup_signatured_type.
6479 If we haven't read the TU yet, create the signatured_type data structure
6480 for a TU to be read in directly from a DWO file, bypassing the stub.
6481 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6482 using .gdb_index, then when reading a CU we want to stay in the DWO file
6483 containing that CU. Otherwise we could end up reading several other DWO
6484 files (due to comdat folding) to process the transitive closure of all the
6485 mentioned TUs, and that can be slow. The current DWO file will have every
6486 type signature that it needs.
6487 We only do this for .gdb_index because in the psymtab case we already have
6488 to read all the DWOs to build the type unit groups. */
6489
6490 static struct signatured_type *
6491 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6492 {
6493 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6494 struct dwo_file *dwo_file;
6495 struct dwo_unit find_dwo_entry, *dwo_entry;
6496 struct signatured_type find_sig_entry, *sig_entry;
6497 void **slot;
6498
6499 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6500
6501 /* If TU skeletons have been removed then we may not have read in any
6502 TUs yet. */
6503 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6504 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6505
6506 /* We only ever need to read in one copy of a signatured type.
6507 Use the global signatured_types array to do our own comdat-folding
6508 of types. If this is the first time we're reading this TU, and
6509 the TU has an entry in .gdb_index, replace the recorded data from
6510 .gdb_index with this TU. */
6511
6512 find_sig_entry.signature = sig;
6513 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6514 &find_sig_entry, INSERT);
6515 sig_entry = (struct signatured_type *) *slot;
6516
6517 /* We can get here with the TU already read, *or* in the process of being
6518 read. Don't reassign the global entry to point to this DWO if that's
6519 the case. Also note that if the TU is already being read, it may not
6520 have come from a DWO, the program may be a mix of Fission-compiled
6521 code and non-Fission-compiled code. */
6522
6523 /* Have we already tried to read this TU?
6524 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6525 needn't exist in the global table yet). */
6526 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6527 return sig_entry;
6528
6529 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6530 dwo_unit of the TU itself. */
6531 dwo_file = cu->dwo_unit->dwo_file;
6532
6533 /* Ok, this is the first time we're reading this TU. */
6534 if (dwo_file->tus == NULL)
6535 return NULL;
6536 find_dwo_entry.signature = sig;
6537 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6538 &find_dwo_entry);
6539 if (dwo_entry == NULL)
6540 return NULL;
6541
6542 /* If the global table doesn't have an entry for this TU, add one. */
6543 if (sig_entry == NULL)
6544 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6545
6546 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6547 sig_entry->per_cu.tu_read = 1;
6548 return sig_entry;
6549 }
6550
6551 /* Subroutine of lookup_signatured_type.
6552 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6553 then try the DWP file. If the TU stub (skeleton) has been removed then
6554 it won't be in .gdb_index. */
6555
6556 static struct signatured_type *
6557 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6558 {
6559 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6560 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6561 struct dwo_unit *dwo_entry;
6562 struct signatured_type find_sig_entry, *sig_entry;
6563 void **slot;
6564
6565 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6566 gdb_assert (dwp_file != NULL);
6567
6568 /* If TU skeletons have been removed then we may not have read in any
6569 TUs yet. */
6570 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6571 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6572
6573 find_sig_entry.signature = sig;
6574 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6575 &find_sig_entry, INSERT);
6576 sig_entry = (struct signatured_type *) *slot;
6577
6578 /* Have we already tried to read this TU?
6579 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6580 needn't exist in the global table yet). */
6581 if (sig_entry != NULL)
6582 return sig_entry;
6583
6584 if (dwp_file->tus == NULL)
6585 return NULL;
6586 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6587 sig, 1 /* is_debug_types */);
6588 if (dwo_entry == NULL)
6589 return NULL;
6590
6591 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6592 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6593
6594 return sig_entry;
6595 }
6596
6597 /* Lookup a signature based type for DW_FORM_ref_sig8.
6598 Returns NULL if signature SIG is not present in the table.
6599 It is up to the caller to complain about this. */
6600
6601 static struct signatured_type *
6602 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6603 {
6604 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6605
6606 if (cu->dwo_unit
6607 && dwarf2_per_objfile->per_bfd->using_index)
6608 {
6609 /* We're in a DWO/DWP file, and we're using .gdb_index.
6610 These cases require special processing. */
6611 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6612 return lookup_dwo_signatured_type (cu, sig);
6613 else
6614 return lookup_dwp_signatured_type (cu, sig);
6615 }
6616 else
6617 {
6618 struct signatured_type find_entry, *entry;
6619
6620 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6621 return NULL;
6622 find_entry.signature = sig;
6623 entry = ((struct signatured_type *)
6624 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6625 &find_entry));
6626 return entry;
6627 }
6628 }
6629
6630 /* Low level DIE reading support. */
6631
6632 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6633
6634 static void
6635 init_cu_die_reader (struct die_reader_specs *reader,
6636 struct dwarf2_cu *cu,
6637 struct dwarf2_section_info *section,
6638 struct dwo_file *dwo_file,
6639 struct abbrev_table *abbrev_table)
6640 {
6641 gdb_assert (section->readin && section->buffer != NULL);
6642 reader->abfd = section->get_bfd_owner ();
6643 reader->cu = cu;
6644 reader->dwo_file = dwo_file;
6645 reader->die_section = section;
6646 reader->buffer = section->buffer;
6647 reader->buffer_end = section->buffer + section->size;
6648 reader->abbrev_table = abbrev_table;
6649 }
6650
6651 /* Subroutine of cutu_reader to simplify it.
6652 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6653 There's just a lot of work to do, and cutu_reader is big enough
6654 already.
6655
6656 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6657 from it to the DIE in the DWO. If NULL we are skipping the stub.
6658 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6659 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6660 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6661 STUB_COMP_DIR may be non-NULL.
6662 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6663 are filled in with the info of the DIE from the DWO file.
6664 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6665 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6666 kept around for at least as long as *RESULT_READER.
6667
6668 The result is non-zero if a valid (non-dummy) DIE was found. */
6669
6670 static int
6671 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6672 struct dwo_unit *dwo_unit,
6673 struct die_info *stub_comp_unit_die,
6674 const char *stub_comp_dir,
6675 struct die_reader_specs *result_reader,
6676 const gdb_byte **result_info_ptr,
6677 struct die_info **result_comp_unit_die,
6678 abbrev_table_up *result_dwo_abbrev_table)
6679 {
6680 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6681 struct objfile *objfile = dwarf2_per_objfile->objfile;
6682 struct dwarf2_cu *cu = this_cu->cu;
6683 bfd *abfd;
6684 const gdb_byte *begin_info_ptr, *info_ptr;
6685 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6686 int i,num_extra_attrs;
6687 struct dwarf2_section_info *dwo_abbrev_section;
6688 struct die_info *comp_unit_die;
6689
6690 /* At most one of these may be provided. */
6691 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6692
6693 /* These attributes aren't processed until later:
6694 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6695 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6696 referenced later. However, these attributes are found in the stub
6697 which we won't have later. In order to not impose this complication
6698 on the rest of the code, we read them here and copy them to the
6699 DWO CU/TU die. */
6700
6701 stmt_list = NULL;
6702 low_pc = NULL;
6703 high_pc = NULL;
6704 ranges = NULL;
6705 comp_dir = NULL;
6706
6707 if (stub_comp_unit_die != NULL)
6708 {
6709 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6710 DWO file. */
6711 if (! this_cu->is_debug_types)
6712 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6713 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6714 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6715 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6716 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6717
6718 cu->addr_base = stub_comp_unit_die->addr_base ();
6719
6720 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6721 here (if needed). We need the value before we can process
6722 DW_AT_ranges. */
6723 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6724 }
6725 else if (stub_comp_dir != NULL)
6726 {
6727 /* Reconstruct the comp_dir attribute to simplify the code below. */
6728 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6729 comp_dir->name = DW_AT_comp_dir;
6730 comp_dir->form = DW_FORM_string;
6731 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6732 DW_STRING (comp_dir) = stub_comp_dir;
6733 }
6734
6735 /* Set up for reading the DWO CU/TU. */
6736 cu->dwo_unit = dwo_unit;
6737 dwarf2_section_info *section = dwo_unit->section;
6738 section->read (objfile);
6739 abfd = section->get_bfd_owner ();
6740 begin_info_ptr = info_ptr = (section->buffer
6741 + to_underlying (dwo_unit->sect_off));
6742 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6743
6744 if (this_cu->is_debug_types)
6745 {
6746 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6747
6748 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6749 &cu->header, section,
6750 dwo_abbrev_section,
6751 info_ptr, rcuh_kind::TYPE);
6752 /* This is not an assert because it can be caused by bad debug info. */
6753 if (sig_type->signature != cu->header.signature)
6754 {
6755 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6756 " TU at offset %s [in module %s]"),
6757 hex_string (sig_type->signature),
6758 hex_string (cu->header.signature),
6759 sect_offset_str (dwo_unit->sect_off),
6760 bfd_get_filename (abfd));
6761 }
6762 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6763 /* For DWOs coming from DWP files, we don't know the CU length
6764 nor the type's offset in the TU until now. */
6765 dwo_unit->length = cu->header.get_length ();
6766 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6767
6768 /* Establish the type offset that can be used to lookup the type.
6769 For DWO files, we don't know it until now. */
6770 sig_type->type_offset_in_section
6771 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6772 }
6773 else
6774 {
6775 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6776 &cu->header, section,
6777 dwo_abbrev_section,
6778 info_ptr, rcuh_kind::COMPILE);
6779 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6780 /* For DWOs coming from DWP files, we don't know the CU length
6781 until now. */
6782 dwo_unit->length = cu->header.get_length ();
6783 }
6784
6785 *result_dwo_abbrev_table
6786 = abbrev_table::read (objfile, dwo_abbrev_section,
6787 cu->header.abbrev_sect_off);
6788 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6789 result_dwo_abbrev_table->get ());
6790
6791 /* Read in the die, but leave space to copy over the attributes
6792 from the stub. This has the benefit of simplifying the rest of
6793 the code - all the work to maintain the illusion of a single
6794 DW_TAG_{compile,type}_unit DIE is done here. */
6795 num_extra_attrs = ((stmt_list != NULL)
6796 + (low_pc != NULL)
6797 + (high_pc != NULL)
6798 + (ranges != NULL)
6799 + (comp_dir != NULL));
6800 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6801 num_extra_attrs);
6802
6803 /* Copy over the attributes from the stub to the DIE we just read in. */
6804 comp_unit_die = *result_comp_unit_die;
6805 i = comp_unit_die->num_attrs;
6806 if (stmt_list != NULL)
6807 comp_unit_die->attrs[i++] = *stmt_list;
6808 if (low_pc != NULL)
6809 comp_unit_die->attrs[i++] = *low_pc;
6810 if (high_pc != NULL)
6811 comp_unit_die->attrs[i++] = *high_pc;
6812 if (ranges != NULL)
6813 comp_unit_die->attrs[i++] = *ranges;
6814 if (comp_dir != NULL)
6815 comp_unit_die->attrs[i++] = *comp_dir;
6816 comp_unit_die->num_attrs += num_extra_attrs;
6817
6818 if (dwarf_die_debug)
6819 {
6820 fprintf_unfiltered (gdb_stdlog,
6821 "Read die from %s@0x%x of %s:\n",
6822 section->get_name (),
6823 (unsigned) (begin_info_ptr - section->buffer),
6824 bfd_get_filename (abfd));
6825 dump_die (comp_unit_die, dwarf_die_debug);
6826 }
6827
6828 /* Skip dummy compilation units. */
6829 if (info_ptr >= begin_info_ptr + dwo_unit->length
6830 || peek_abbrev_code (abfd, info_ptr) == 0)
6831 return 0;
6832
6833 *result_info_ptr = info_ptr;
6834 return 1;
6835 }
6836
6837 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6838 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6839 signature is part of the header. */
6840 static gdb::optional<ULONGEST>
6841 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6842 {
6843 if (cu->header.version >= 5)
6844 return cu->header.signature;
6845 struct attribute *attr;
6846 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6847 if (attr == nullptr)
6848 return gdb::optional<ULONGEST> ();
6849 return DW_UNSND (attr);
6850 }
6851
6852 /* Subroutine of cutu_reader to simplify it.
6853 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6854 Returns NULL if the specified DWO unit cannot be found. */
6855
6856 static struct dwo_unit *
6857 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6858 struct die_info *comp_unit_die,
6859 const char *dwo_name)
6860 {
6861 struct dwarf2_cu *cu = this_cu->cu;
6862 struct dwo_unit *dwo_unit;
6863 const char *comp_dir;
6864
6865 gdb_assert (cu != NULL);
6866
6867 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6868 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6869 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6870
6871 if (this_cu->is_debug_types)
6872 {
6873 struct signatured_type *sig_type;
6874
6875 /* Since this_cu is the first member of struct signatured_type,
6876 we can go from a pointer to one to a pointer to the other. */
6877 sig_type = (struct signatured_type *) this_cu;
6878 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6879 }
6880 else
6881 {
6882 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6883 if (!signature.has_value ())
6884 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6885 " [in module %s]"),
6886 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6887 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6888 *signature);
6889 }
6890
6891 return dwo_unit;
6892 }
6893
6894 /* Subroutine of cutu_reader to simplify it.
6895 See it for a description of the parameters.
6896 Read a TU directly from a DWO file, bypassing the stub. */
6897
6898 void
6899 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6900 dwarf2_per_objfile *per_objfile,
6901 int use_existing_cu)
6902 {
6903 struct signatured_type *sig_type;
6904
6905 /* Verify we can do the following downcast, and that we have the
6906 data we need. */
6907 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6908 sig_type = (struct signatured_type *) this_cu;
6909 gdb_assert (sig_type->dwo_unit != NULL);
6910
6911 if (use_existing_cu && this_cu->cu != NULL)
6912 {
6913 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6914 /* There's no need to do the rereading_dwo_cu handling that
6915 cutu_reader does since we don't read the stub. */
6916 }
6917 else
6918 {
6919 /* If !use_existing_cu, this_cu->cu must be NULL. */
6920 gdb_assert (this_cu->cu == NULL);
6921 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6922 }
6923
6924 /* A future optimization, if needed, would be to use an existing
6925 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6926 could share abbrev tables. */
6927
6928 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6929 NULL /* stub_comp_unit_die */,
6930 sig_type->dwo_unit->dwo_file->comp_dir,
6931 this, &info_ptr,
6932 &comp_unit_die,
6933 &m_dwo_abbrev_table) == 0)
6934 {
6935 /* Dummy die. */
6936 dummy_p = true;
6937 }
6938 }
6939
6940 /* Initialize a CU (or TU) and read its DIEs.
6941 If the CU defers to a DWO file, read the DWO file as well.
6942
6943 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6944 Otherwise the table specified in the comp unit header is read in and used.
6945 This is an optimization for when we already have the abbrev table.
6946
6947 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6948 Otherwise, a new CU is allocated with xmalloc. */
6949
6950 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6951 struct abbrev_table *abbrev_table,
6952 int use_existing_cu,
6953 bool skip_partial)
6954 : die_reader_specs {},
6955 m_this_cu (this_cu)
6956 {
6957 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6958 struct objfile *objfile = dwarf2_per_objfile->objfile;
6959 struct dwarf2_section_info *section = this_cu->section;
6960 bfd *abfd = section->get_bfd_owner ();
6961 struct dwarf2_cu *cu;
6962 const gdb_byte *begin_info_ptr;
6963 struct signatured_type *sig_type = NULL;
6964 struct dwarf2_section_info *abbrev_section;
6965 /* Non-zero if CU currently points to a DWO file and we need to
6966 reread it. When this happens we need to reread the skeleton die
6967 before we can reread the DWO file (this only applies to CUs, not TUs). */
6968 int rereading_dwo_cu = 0;
6969
6970 if (dwarf_die_debug)
6971 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6972 this_cu->is_debug_types ? "type" : "comp",
6973 sect_offset_str (this_cu->sect_off));
6974
6975 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6976 file (instead of going through the stub), short-circuit all of this. */
6977 if (this_cu->reading_dwo_directly)
6978 {
6979 /* Narrow down the scope of possibilities to have to understand. */
6980 gdb_assert (this_cu->is_debug_types);
6981 gdb_assert (abbrev_table == NULL);
6982 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
6983 return;
6984 }
6985
6986 /* This is cheap if the section is already read in. */
6987 section->read (objfile);
6988
6989 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6990
6991 abbrev_section = get_abbrev_section_for_cu (this_cu);
6992
6993 if (use_existing_cu && this_cu->cu != NULL)
6994 {
6995 cu = this_cu->cu;
6996 /* If this CU is from a DWO file we need to start over, we need to
6997 refetch the attributes from the skeleton CU.
6998 This could be optimized by retrieving those attributes from when we
6999 were here the first time: the previous comp_unit_die was stored in
7000 comp_unit_obstack. But there's no data yet that we need this
7001 optimization. */
7002 if (cu->dwo_unit != NULL)
7003 rereading_dwo_cu = 1;
7004 }
7005 else
7006 {
7007 /* If !use_existing_cu, this_cu->cu must be NULL. */
7008 gdb_assert (this_cu->cu == NULL);
7009 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7010 cu = m_new_cu.get ();
7011 }
7012
7013 /* Get the header. */
7014 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7015 {
7016 /* We already have the header, there's no need to read it in again. */
7017 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7018 }
7019 else
7020 {
7021 if (this_cu->is_debug_types)
7022 {
7023 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7024 &cu->header, section,
7025 abbrev_section, info_ptr,
7026 rcuh_kind::TYPE);
7027
7028 /* Since per_cu is the first member of struct signatured_type,
7029 we can go from a pointer to one to a pointer to the other. */
7030 sig_type = (struct signatured_type *) this_cu;
7031 gdb_assert (sig_type->signature == cu->header.signature);
7032 gdb_assert (sig_type->type_offset_in_tu
7033 == cu->header.type_cu_offset_in_tu);
7034 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7035
7036 /* LENGTH has not been set yet for type units if we're
7037 using .gdb_index. */
7038 this_cu->length = cu->header.get_length ();
7039
7040 /* Establish the type offset that can be used to lookup the type. */
7041 sig_type->type_offset_in_section =
7042 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7043
7044 this_cu->dwarf_version = cu->header.version;
7045 }
7046 else
7047 {
7048 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7049 &cu->header, section,
7050 abbrev_section,
7051 info_ptr,
7052 rcuh_kind::COMPILE);
7053
7054 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7055 if (this_cu->length == 0)
7056 this_cu->length = cu->header.get_length ();
7057 else
7058 gdb_assert (this_cu->length == cu->header.get_length ());
7059 this_cu->dwarf_version = cu->header.version;
7060 }
7061 }
7062
7063 /* Skip dummy compilation units. */
7064 if (info_ptr >= begin_info_ptr + this_cu->length
7065 || peek_abbrev_code (abfd, info_ptr) == 0)
7066 {
7067 dummy_p = true;
7068 return;
7069 }
7070
7071 /* If we don't have them yet, read the abbrevs for this compilation unit.
7072 And if we need to read them now, make sure they're freed when we're
7073 done. */
7074 if (abbrev_table != NULL)
7075 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7076 else
7077 {
7078 m_abbrev_table_holder
7079 = abbrev_table::read (objfile, abbrev_section,
7080 cu->header.abbrev_sect_off);
7081 abbrev_table = m_abbrev_table_holder.get ();
7082 }
7083
7084 /* Read the top level CU/TU die. */
7085 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7086 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7087
7088 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7089 {
7090 dummy_p = true;
7091 return;
7092 }
7093
7094 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7095 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7096 table from the DWO file and pass the ownership over to us. It will be
7097 referenced from READER, so we must make sure to free it after we're done
7098 with READER.
7099
7100 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7101 DWO CU, that this test will fail (the attribute will not be present). */
7102 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7103 if (dwo_name != nullptr)
7104 {
7105 struct dwo_unit *dwo_unit;
7106 struct die_info *dwo_comp_unit_die;
7107
7108 if (comp_unit_die->has_children)
7109 {
7110 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7111 " has children (offset %s) [in module %s]"),
7112 sect_offset_str (this_cu->sect_off),
7113 bfd_get_filename (abfd));
7114 }
7115 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7116 if (dwo_unit != NULL)
7117 {
7118 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7119 comp_unit_die, NULL,
7120 this, &info_ptr,
7121 &dwo_comp_unit_die,
7122 &m_dwo_abbrev_table) == 0)
7123 {
7124 /* Dummy die. */
7125 dummy_p = true;
7126 return;
7127 }
7128 comp_unit_die = dwo_comp_unit_die;
7129 }
7130 else
7131 {
7132 /* Yikes, we couldn't find the rest of the DIE, we only have
7133 the stub. A complaint has already been logged. There's
7134 not much more we can do except pass on the stub DIE to
7135 die_reader_func. We don't want to throw an error on bad
7136 debug info. */
7137 }
7138 }
7139 }
7140
7141 void
7142 cutu_reader::keep ()
7143 {
7144 /* Done, clean up. */
7145 gdb_assert (!dummy_p);
7146 if (m_new_cu != NULL)
7147 {
7148 struct dwarf2_per_objfile *dwarf2_per_objfile
7149 = m_this_cu->dwarf2_per_objfile;
7150 /* Link this CU into read_in_chain. */
7151 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7152 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7153 /* The chain owns it now. */
7154 m_new_cu.release ();
7155 }
7156 }
7157
7158 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7159 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7160 assumed to have already done the lookup to find the DWO file).
7161
7162 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7163 THIS_CU->is_debug_types, but nothing else.
7164
7165 We fill in THIS_CU->length.
7166
7167 THIS_CU->cu is always freed when done.
7168 This is done in order to not leave THIS_CU->cu in a state where we have
7169 to care whether it refers to the "main" CU or the DWO CU.
7170
7171 When parent_cu is passed, it is used to provide a default value for
7172 str_offsets_base and addr_base from the parent. */
7173
7174 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7175 struct dwarf2_cu *parent_cu,
7176 struct dwo_file *dwo_file)
7177 : die_reader_specs {},
7178 m_this_cu (this_cu)
7179 {
7180 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7181 struct objfile *objfile = dwarf2_per_objfile->objfile;
7182 struct dwarf2_section_info *section = this_cu->section;
7183 bfd *abfd = section->get_bfd_owner ();
7184 struct dwarf2_section_info *abbrev_section;
7185 const gdb_byte *begin_info_ptr, *info_ptr;
7186
7187 if (dwarf_die_debug)
7188 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7189 this_cu->is_debug_types ? "type" : "comp",
7190 sect_offset_str (this_cu->sect_off));
7191
7192 gdb_assert (this_cu->cu == NULL);
7193
7194 abbrev_section = (dwo_file != NULL
7195 ? &dwo_file->sections.abbrev
7196 : get_abbrev_section_for_cu (this_cu));
7197
7198 /* This is cheap if the section is already read in. */
7199 section->read (objfile);
7200
7201 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7202
7203 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7204 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7205 &m_new_cu->header, section,
7206 abbrev_section, info_ptr,
7207 (this_cu->is_debug_types
7208 ? rcuh_kind::TYPE
7209 : rcuh_kind::COMPILE));
7210
7211 if (parent_cu != nullptr)
7212 {
7213 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7214 m_new_cu->addr_base = parent_cu->addr_base;
7215 }
7216 this_cu->length = m_new_cu->header.get_length ();
7217
7218 /* Skip dummy compilation units. */
7219 if (info_ptr >= begin_info_ptr + this_cu->length
7220 || peek_abbrev_code (abfd, info_ptr) == 0)
7221 {
7222 dummy_p = true;
7223 return;
7224 }
7225
7226 m_abbrev_table_holder
7227 = abbrev_table::read (objfile, abbrev_section,
7228 m_new_cu->header.abbrev_sect_off);
7229
7230 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7231 m_abbrev_table_holder.get ());
7232 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7233 }
7234
7235 \f
7236 /* Type Unit Groups.
7237
7238 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7239 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7240 so that all types coming from the same compilation (.o file) are grouped
7241 together. A future step could be to put the types in the same symtab as
7242 the CU the types ultimately came from. */
7243
7244 static hashval_t
7245 hash_type_unit_group (const void *item)
7246 {
7247 const struct type_unit_group *tu_group
7248 = (const struct type_unit_group *) item;
7249
7250 return hash_stmt_list_entry (&tu_group->hash);
7251 }
7252
7253 static int
7254 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7255 {
7256 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7257 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7258
7259 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7260 }
7261
7262 /* Allocate a hash table for type unit groups. */
7263
7264 static htab_up
7265 allocate_type_unit_groups_table ()
7266 {
7267 return htab_up (htab_create_alloc (3,
7268 hash_type_unit_group,
7269 eq_type_unit_group,
7270 NULL, xcalloc, xfree));
7271 }
7272
7273 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7274 partial symtabs. We combine several TUs per psymtab to not let the size
7275 of any one psymtab grow too big. */
7276 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7277 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7278
7279 /* Helper routine for get_type_unit_group.
7280 Create the type_unit_group object used to hold one or more TUs. */
7281
7282 static struct type_unit_group *
7283 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7284 {
7285 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7286 struct dwarf2_per_cu_data *per_cu;
7287 struct type_unit_group *tu_group;
7288
7289 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7290 struct type_unit_group);
7291 per_cu = &tu_group->per_cu;
7292 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7293
7294 if (dwarf2_per_objfile->per_bfd->using_index)
7295 {
7296 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7297 struct dwarf2_per_cu_quick_data);
7298 }
7299 else
7300 {
7301 unsigned int line_offset = to_underlying (line_offset_struct);
7302 dwarf2_psymtab *pst;
7303 std::string name;
7304
7305 /* Give the symtab a useful name for debug purposes. */
7306 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7307 name = string_printf ("<type_units_%d>",
7308 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7309 else
7310 name = string_printf ("<type_units_at_0x%x>", line_offset);
7311
7312 pst = create_partial_symtab (per_cu, name.c_str ());
7313 pst->anonymous = true;
7314 }
7315
7316 tu_group->hash.dwo_unit = cu->dwo_unit;
7317 tu_group->hash.line_sect_off = line_offset_struct;
7318
7319 return tu_group;
7320 }
7321
7322 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7323 STMT_LIST is a DW_AT_stmt_list attribute. */
7324
7325 static struct type_unit_group *
7326 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7327 {
7328 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7329 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7330 struct type_unit_group *tu_group;
7331 void **slot;
7332 unsigned int line_offset;
7333 struct type_unit_group type_unit_group_for_lookup;
7334
7335 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7336 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7337
7338 /* Do we need to create a new group, or can we use an existing one? */
7339
7340 if (stmt_list)
7341 {
7342 line_offset = DW_UNSND (stmt_list);
7343 ++tu_stats->nr_symtab_sharers;
7344 }
7345 else
7346 {
7347 /* Ugh, no stmt_list. Rare, but we have to handle it.
7348 We can do various things here like create one group per TU or
7349 spread them over multiple groups to split up the expansion work.
7350 To avoid worst case scenarios (too many groups or too large groups)
7351 we, umm, group them in bunches. */
7352 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7353 | (tu_stats->nr_stmt_less_type_units
7354 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7355 ++tu_stats->nr_stmt_less_type_units;
7356 }
7357
7358 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7359 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7360 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7361 &type_unit_group_for_lookup, INSERT);
7362 if (*slot != NULL)
7363 {
7364 tu_group = (struct type_unit_group *) *slot;
7365 gdb_assert (tu_group != NULL);
7366 }
7367 else
7368 {
7369 sect_offset line_offset_struct = (sect_offset) line_offset;
7370 tu_group = create_type_unit_group (cu, line_offset_struct);
7371 *slot = tu_group;
7372 ++tu_stats->nr_symtabs;
7373 }
7374
7375 return tu_group;
7376 }
7377 \f
7378 /* Partial symbol tables. */
7379
7380 /* Create a psymtab named NAME and assign it to PER_CU.
7381
7382 The caller must fill in the following details:
7383 dirname, textlow, texthigh. */
7384
7385 static dwarf2_psymtab *
7386 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7387 {
7388 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7389 dwarf2_psymtab *pst;
7390
7391 pst = new dwarf2_psymtab (name, objfile, per_cu);
7392
7393 pst->psymtabs_addrmap_supported = true;
7394
7395 /* This is the glue that links PST into GDB's symbol API. */
7396 per_cu->v.psymtab = pst;
7397
7398 return pst;
7399 }
7400
7401 /* DIE reader function for process_psymtab_comp_unit. */
7402
7403 static void
7404 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7405 const gdb_byte *info_ptr,
7406 struct die_info *comp_unit_die,
7407 enum language pretend_language)
7408 {
7409 struct dwarf2_cu *cu = reader->cu;
7410 struct objfile *objfile = cu->per_objfile->objfile;
7411 struct gdbarch *gdbarch = objfile->arch ();
7412 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7413 CORE_ADDR baseaddr;
7414 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7415 dwarf2_psymtab *pst;
7416 enum pc_bounds_kind cu_bounds_kind;
7417 const char *filename;
7418
7419 gdb_assert (! per_cu->is_debug_types);
7420
7421 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7422
7423 /* Allocate a new partial symbol table structure. */
7424 gdb::unique_xmalloc_ptr<char> debug_filename;
7425 static const char artificial[] = "<artificial>";
7426 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7427 if (filename == NULL)
7428 filename = "";
7429 else if (strcmp (filename, artificial) == 0)
7430 {
7431 debug_filename.reset (concat (artificial, "@",
7432 sect_offset_str (per_cu->sect_off),
7433 (char *) NULL));
7434 filename = debug_filename.get ();
7435 }
7436
7437 pst = create_partial_symtab (per_cu, filename);
7438
7439 /* This must be done before calling dwarf2_build_include_psymtabs. */
7440 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7441
7442 baseaddr = objfile->text_section_offset ();
7443
7444 dwarf2_find_base_address (comp_unit_die, cu);
7445
7446 /* Possibly set the default values of LOWPC and HIGHPC from
7447 `DW_AT_ranges'. */
7448 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7449 &best_highpc, cu, pst);
7450 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7451 {
7452 CORE_ADDR low
7453 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7454 - baseaddr);
7455 CORE_ADDR high
7456 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7457 - baseaddr - 1);
7458 /* Store the contiguous range if it is not empty; it can be
7459 empty for CUs with no code. */
7460 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7461 low, high, pst);
7462 }
7463
7464 /* Check if comp unit has_children.
7465 If so, read the rest of the partial symbols from this comp unit.
7466 If not, there's no more debug_info for this comp unit. */
7467 if (comp_unit_die->has_children)
7468 {
7469 struct partial_die_info *first_die;
7470 CORE_ADDR lowpc, highpc;
7471
7472 lowpc = ((CORE_ADDR) -1);
7473 highpc = ((CORE_ADDR) 0);
7474
7475 first_die = load_partial_dies (reader, info_ptr, 1);
7476
7477 scan_partial_symbols (first_die, &lowpc, &highpc,
7478 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7479
7480 /* If we didn't find a lowpc, set it to highpc to avoid
7481 complaints from `maint check'. */
7482 if (lowpc == ((CORE_ADDR) -1))
7483 lowpc = highpc;
7484
7485 /* If the compilation unit didn't have an explicit address range,
7486 then use the information extracted from its child dies. */
7487 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7488 {
7489 best_lowpc = lowpc;
7490 best_highpc = highpc;
7491 }
7492 }
7493 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7494 best_lowpc + baseaddr)
7495 - baseaddr);
7496 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7497 best_highpc + baseaddr)
7498 - baseaddr);
7499
7500 end_psymtab_common (objfile, pst);
7501
7502 if (!cu->per_cu->imported_symtabs_empty ())
7503 {
7504 int i;
7505 int len = cu->per_cu->imported_symtabs_size ();
7506
7507 /* Fill in 'dependencies' here; we fill in 'users' in a
7508 post-pass. */
7509 pst->number_of_dependencies = len;
7510 pst->dependencies
7511 = objfile->partial_symtabs->allocate_dependencies (len);
7512 for (i = 0; i < len; ++i)
7513 {
7514 pst->dependencies[i]
7515 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7516 }
7517
7518 cu->per_cu->imported_symtabs_free ();
7519 }
7520
7521 /* Get the list of files included in the current compilation unit,
7522 and build a psymtab for each of them. */
7523 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7524
7525 if (dwarf_read_debug)
7526 fprintf_unfiltered (gdb_stdlog,
7527 "Psymtab for %s unit @%s: %s - %s"
7528 ", %d global, %d static syms\n",
7529 per_cu->is_debug_types ? "type" : "comp",
7530 sect_offset_str (per_cu->sect_off),
7531 paddress (gdbarch, pst->text_low (objfile)),
7532 paddress (gdbarch, pst->text_high (objfile)),
7533 pst->n_global_syms, pst->n_static_syms);
7534 }
7535
7536 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7537 Process compilation unit THIS_CU for a psymtab. */
7538
7539 static void
7540 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7541 bool want_partial_unit,
7542 enum language pretend_language)
7543 {
7544 /* If this compilation unit was already read in, free the
7545 cached copy in order to read it in again. This is
7546 necessary because we skipped some symbols when we first
7547 read in the compilation unit (see load_partial_dies).
7548 This problem could be avoided, but the benefit is unclear. */
7549 if (this_cu->cu != NULL)
7550 free_one_cached_comp_unit (this_cu);
7551
7552 cutu_reader reader (this_cu, NULL, 0, false);
7553
7554 switch (reader.comp_unit_die->tag)
7555 {
7556 case DW_TAG_compile_unit:
7557 this_cu->unit_type = DW_UT_compile;
7558 break;
7559 case DW_TAG_partial_unit:
7560 this_cu->unit_type = DW_UT_partial;
7561 break;
7562 default:
7563 abort ();
7564 }
7565
7566 if (reader.dummy_p)
7567 {
7568 /* Nothing. */
7569 }
7570 else if (this_cu->is_debug_types)
7571 build_type_psymtabs_reader (&reader, reader.info_ptr,
7572 reader.comp_unit_die);
7573 else if (want_partial_unit
7574 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7575 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7576 reader.comp_unit_die,
7577 pretend_language);
7578
7579 this_cu->lang = this_cu->cu->language;
7580
7581 /* Age out any secondary CUs. */
7582 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7583 }
7584
7585 /* Reader function for build_type_psymtabs. */
7586
7587 static void
7588 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7589 const gdb_byte *info_ptr,
7590 struct die_info *type_unit_die)
7591 {
7592 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7593 struct objfile *objfile = dwarf2_per_objfile->objfile;
7594 struct dwarf2_cu *cu = reader->cu;
7595 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7596 struct signatured_type *sig_type;
7597 struct type_unit_group *tu_group;
7598 struct attribute *attr;
7599 struct partial_die_info *first_die;
7600 CORE_ADDR lowpc, highpc;
7601 dwarf2_psymtab *pst;
7602
7603 gdb_assert (per_cu->is_debug_types);
7604 sig_type = (struct signatured_type *) per_cu;
7605
7606 if (! type_unit_die->has_children)
7607 return;
7608
7609 attr = type_unit_die->attr (DW_AT_stmt_list);
7610 tu_group = get_type_unit_group (cu, attr);
7611
7612 if (tu_group->tus == nullptr)
7613 tu_group->tus = new std::vector<signatured_type *>;
7614 tu_group->tus->push_back (sig_type);
7615
7616 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7617 pst = create_partial_symtab (per_cu, "");
7618 pst->anonymous = true;
7619
7620 first_die = load_partial_dies (reader, info_ptr, 1);
7621
7622 lowpc = (CORE_ADDR) -1;
7623 highpc = (CORE_ADDR) 0;
7624 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7625
7626 end_psymtab_common (objfile, pst);
7627 }
7628
7629 /* Struct used to sort TUs by their abbreviation table offset. */
7630
7631 struct tu_abbrev_offset
7632 {
7633 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7634 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7635 {}
7636
7637 signatured_type *sig_type;
7638 sect_offset abbrev_offset;
7639 };
7640
7641 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7642
7643 static bool
7644 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7645 const struct tu_abbrev_offset &b)
7646 {
7647 return a.abbrev_offset < b.abbrev_offset;
7648 }
7649
7650 /* Efficiently read all the type units.
7651 This does the bulk of the work for build_type_psymtabs.
7652
7653 The efficiency is because we sort TUs by the abbrev table they use and
7654 only read each abbrev table once. In one program there are 200K TUs
7655 sharing 8K abbrev tables.
7656
7657 The main purpose of this function is to support building the
7658 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7659 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7660 can collapse the search space by grouping them by stmt_list.
7661 The savings can be significant, in the same program from above the 200K TUs
7662 share 8K stmt_list tables.
7663
7664 FUNC is expected to call get_type_unit_group, which will create the
7665 struct type_unit_group if necessary and add it to
7666 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7667
7668 static void
7669 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7670 {
7671 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7672 abbrev_table_up abbrev_table;
7673 sect_offset abbrev_offset;
7674
7675 /* It's up to the caller to not call us multiple times. */
7676 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7677
7678 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7679 return;
7680
7681 /* TUs typically share abbrev tables, and there can be way more TUs than
7682 abbrev tables. Sort by abbrev table to reduce the number of times we
7683 read each abbrev table in.
7684 Alternatives are to punt or to maintain a cache of abbrev tables.
7685 This is simpler and efficient enough for now.
7686
7687 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7688 symtab to use). Typically TUs with the same abbrev offset have the same
7689 stmt_list value too so in practice this should work well.
7690
7691 The basic algorithm here is:
7692
7693 sort TUs by abbrev table
7694 for each TU with same abbrev table:
7695 read abbrev table if first user
7696 read TU top level DIE
7697 [IWBN if DWO skeletons had DW_AT_stmt_list]
7698 call FUNC */
7699
7700 if (dwarf_read_debug)
7701 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7702
7703 /* Sort in a separate table to maintain the order of all_type_units
7704 for .gdb_index: TU indices directly index all_type_units. */
7705 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7706 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7707
7708 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7709 sorted_by_abbrev.emplace_back
7710 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7711 sig_type->per_cu.section,
7712 sig_type->per_cu.sect_off));
7713
7714 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7715 sort_tu_by_abbrev_offset);
7716
7717 abbrev_offset = (sect_offset) ~(unsigned) 0;
7718
7719 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7720 {
7721 /* Switch to the next abbrev table if necessary. */
7722 if (abbrev_table == NULL
7723 || tu.abbrev_offset != abbrev_offset)
7724 {
7725 abbrev_offset = tu.abbrev_offset;
7726 abbrev_table =
7727 abbrev_table::read (dwarf2_per_objfile->objfile,
7728 &dwarf2_per_objfile->per_bfd->abbrev,
7729 abbrev_offset);
7730 ++tu_stats->nr_uniq_abbrev_tables;
7731 }
7732
7733 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7734 0, false);
7735 if (!reader.dummy_p)
7736 build_type_psymtabs_reader (&reader, reader.info_ptr,
7737 reader.comp_unit_die);
7738 }
7739 }
7740
7741 /* Print collected type unit statistics. */
7742
7743 static void
7744 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7745 {
7746 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7747
7748 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7749 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7750 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7751 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7752 tu_stats->nr_uniq_abbrev_tables);
7753 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7754 tu_stats->nr_symtabs);
7755 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7756 tu_stats->nr_symtab_sharers);
7757 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7758 tu_stats->nr_stmt_less_type_units);
7759 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7760 tu_stats->nr_all_type_units_reallocs);
7761 }
7762
7763 /* Traversal function for build_type_psymtabs. */
7764
7765 static int
7766 build_type_psymtab_dependencies (void **slot, void *info)
7767 {
7768 struct dwarf2_per_objfile *dwarf2_per_objfile
7769 = (struct dwarf2_per_objfile *) info;
7770 struct objfile *objfile = dwarf2_per_objfile->objfile;
7771 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7772 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7773 dwarf2_psymtab *pst = per_cu->v.psymtab;
7774 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7775 int i;
7776
7777 gdb_assert (len > 0);
7778 gdb_assert (per_cu->type_unit_group_p ());
7779
7780 pst->number_of_dependencies = len;
7781 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7782 for (i = 0; i < len; ++i)
7783 {
7784 struct signatured_type *iter = tu_group->tus->at (i);
7785 gdb_assert (iter->per_cu.is_debug_types);
7786 pst->dependencies[i] = iter->per_cu.v.psymtab;
7787 iter->type_unit_group = tu_group;
7788 }
7789
7790 delete tu_group->tus;
7791 tu_group->tus = nullptr;
7792
7793 return 1;
7794 }
7795
7796 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7797 Build partial symbol tables for the .debug_types comp-units. */
7798
7799 static void
7800 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7801 {
7802 if (! create_all_type_units (dwarf2_per_objfile))
7803 return;
7804
7805 build_type_psymtabs_1 (dwarf2_per_objfile);
7806 }
7807
7808 /* Traversal function for process_skeletonless_type_unit.
7809 Read a TU in a DWO file and build partial symbols for it. */
7810
7811 static int
7812 process_skeletonless_type_unit (void **slot, void *info)
7813 {
7814 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7815 struct dwarf2_per_objfile *dwarf2_per_objfile
7816 = (struct dwarf2_per_objfile *) info;
7817 struct signatured_type find_entry, *entry;
7818
7819 /* If this TU doesn't exist in the global table, add it and read it in. */
7820
7821 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7822 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7823
7824 find_entry.signature = dwo_unit->signature;
7825 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7826 &find_entry, INSERT);
7827 /* If we've already seen this type there's nothing to do. What's happening
7828 is we're doing our own version of comdat-folding here. */
7829 if (*slot != NULL)
7830 return 1;
7831
7832 /* This does the job that create_all_type_units would have done for
7833 this TU. */
7834 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7835 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7836 *slot = entry;
7837
7838 /* This does the job that build_type_psymtabs_1 would have done. */
7839 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7840 if (!reader.dummy_p)
7841 build_type_psymtabs_reader (&reader, reader.info_ptr,
7842 reader.comp_unit_die);
7843
7844 return 1;
7845 }
7846
7847 /* Traversal function for process_skeletonless_type_units. */
7848
7849 static int
7850 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7851 {
7852 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7853
7854 if (dwo_file->tus != NULL)
7855 htab_traverse_noresize (dwo_file->tus.get (),
7856 process_skeletonless_type_unit, info);
7857
7858 return 1;
7859 }
7860
7861 /* Scan all TUs of DWO files, verifying we've processed them.
7862 This is needed in case a TU was emitted without its skeleton.
7863 Note: This can't be done until we know what all the DWO files are. */
7864
7865 static void
7866 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7867 {
7868 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7869 if (get_dwp_file (dwarf2_per_objfile) == NULL
7870 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7871 {
7872 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7873 process_dwo_file_for_skeletonless_type_units,
7874 dwarf2_per_objfile);
7875 }
7876 }
7877
7878 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7879
7880 static void
7881 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7882 {
7883 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7884 {
7885 dwarf2_psymtab *pst = per_cu->v.psymtab;
7886
7887 if (pst == NULL)
7888 continue;
7889
7890 for (int j = 0; j < pst->number_of_dependencies; ++j)
7891 {
7892 /* Set the 'user' field only if it is not already set. */
7893 if (pst->dependencies[j]->user == NULL)
7894 pst->dependencies[j]->user = pst;
7895 }
7896 }
7897 }
7898
7899 /* Build the partial symbol table by doing a quick pass through the
7900 .debug_info and .debug_abbrev sections. */
7901
7902 static void
7903 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7904 {
7905 struct objfile *objfile = dwarf2_per_objfile->objfile;
7906
7907 if (dwarf_read_debug)
7908 {
7909 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7910 objfile_name (objfile));
7911 }
7912
7913 scoped_restore restore_reading_psyms
7914 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7915 true);
7916
7917 dwarf2_per_objfile->per_bfd->info.read (objfile);
7918
7919 /* Any cached compilation units will be linked by the per-objfile
7920 read_in_chain. Make sure to free them when we're done. */
7921 free_cached_comp_units freer (dwarf2_per_objfile);
7922
7923 build_type_psymtabs (dwarf2_per_objfile);
7924
7925 create_all_comp_units (dwarf2_per_objfile);
7926
7927 /* Create a temporary address map on a temporary obstack. We later
7928 copy this to the final obstack. */
7929 auto_obstack temp_obstack;
7930
7931 scoped_restore save_psymtabs_addrmap
7932 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7933 addrmap_create_mutable (&temp_obstack));
7934
7935 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7936 {
7937 if (per_cu->v.psymtab != NULL)
7938 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7939 continue;
7940 process_psymtab_comp_unit (per_cu, false, language_minimal);
7941 }
7942
7943 /* This has to wait until we read the CUs, we need the list of DWOs. */
7944 process_skeletonless_type_units (dwarf2_per_objfile);
7945
7946 /* Now that all TUs have been processed we can fill in the dependencies. */
7947 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7948 {
7949 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7950 build_type_psymtab_dependencies, dwarf2_per_objfile);
7951 }
7952
7953 if (dwarf_read_debug)
7954 print_tu_stats (dwarf2_per_objfile);
7955
7956 set_partial_user (dwarf2_per_objfile);
7957
7958 objfile->partial_symtabs->psymtabs_addrmap
7959 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7960 objfile->partial_symtabs->obstack ());
7961 /* At this point we want to keep the address map. */
7962 save_psymtabs_addrmap.release ();
7963
7964 if (dwarf_read_debug)
7965 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7966 objfile_name (objfile));
7967 }
7968
7969 /* Load the partial DIEs for a secondary CU into memory.
7970 This is also used when rereading a primary CU with load_all_dies. */
7971
7972 static void
7973 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7974 {
7975 cutu_reader reader (this_cu, NULL, 1, false);
7976
7977 if (!reader.dummy_p)
7978 {
7979 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7980 language_minimal);
7981
7982 /* Check if comp unit has_children.
7983 If so, read the rest of the partial symbols from this comp unit.
7984 If not, there's no more debug_info for this comp unit. */
7985 if (reader.comp_unit_die->has_children)
7986 load_partial_dies (&reader, reader.info_ptr, 0);
7987
7988 reader.keep ();
7989 }
7990 }
7991
7992 static void
7993 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7994 struct dwarf2_section_info *section,
7995 struct dwarf2_section_info *abbrev_section,
7996 unsigned int is_dwz)
7997 {
7998 const gdb_byte *info_ptr;
7999 struct objfile *objfile = dwarf2_per_objfile->objfile;
8000
8001 if (dwarf_read_debug)
8002 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8003 section->get_name (),
8004 section->get_file_name ());
8005
8006 section->read (objfile);
8007
8008 info_ptr = section->buffer;
8009
8010 while (info_ptr < section->buffer + section->size)
8011 {
8012 struct dwarf2_per_cu_data *this_cu;
8013
8014 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8015
8016 comp_unit_head cu_header;
8017 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8018 abbrev_section, info_ptr,
8019 rcuh_kind::COMPILE);
8020
8021 /* Save the compilation unit for later lookup. */
8022 if (cu_header.unit_type != DW_UT_type)
8023 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8024 else
8025 {
8026 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8027 sig_type->signature = cu_header.signature;
8028 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8029 this_cu = &sig_type->per_cu;
8030 }
8031 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8032 this_cu->sect_off = sect_off;
8033 this_cu->length = cu_header.length + cu_header.initial_length_size;
8034 this_cu->is_dwz = is_dwz;
8035 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8036 this_cu->section = section;
8037
8038 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8039
8040 info_ptr = info_ptr + this_cu->length;
8041 }
8042 }
8043
8044 /* Create a list of all compilation units in OBJFILE.
8045 This is only done for -readnow and building partial symtabs. */
8046
8047 static void
8048 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8049 {
8050 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8051 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8052 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8053
8054 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8055 if (dwz != NULL)
8056 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8057 1);
8058 }
8059
8060 /* Process all loaded DIEs for compilation unit CU, starting at
8061 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8062 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8063 DW_AT_ranges). See the comments of add_partial_subprogram on how
8064 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8065
8066 static void
8067 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8068 CORE_ADDR *highpc, int set_addrmap,
8069 struct dwarf2_cu *cu)
8070 {
8071 struct partial_die_info *pdi;
8072
8073 /* Now, march along the PDI's, descending into ones which have
8074 interesting children but skipping the children of the other ones,
8075 until we reach the end of the compilation unit. */
8076
8077 pdi = first_die;
8078
8079 while (pdi != NULL)
8080 {
8081 pdi->fixup (cu);
8082
8083 /* Anonymous namespaces or modules have no name but have interesting
8084 children, so we need to look at them. Ditto for anonymous
8085 enums. */
8086
8087 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8088 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8089 || pdi->tag == DW_TAG_imported_unit
8090 || pdi->tag == DW_TAG_inlined_subroutine)
8091 {
8092 switch (pdi->tag)
8093 {
8094 case DW_TAG_subprogram:
8095 case DW_TAG_inlined_subroutine:
8096 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8097 break;
8098 case DW_TAG_constant:
8099 case DW_TAG_variable:
8100 case DW_TAG_typedef:
8101 case DW_TAG_union_type:
8102 if (!pdi->is_declaration
8103 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8104 {
8105 add_partial_symbol (pdi, cu);
8106 }
8107 break;
8108 case DW_TAG_class_type:
8109 case DW_TAG_interface_type:
8110 case DW_TAG_structure_type:
8111 if (!pdi->is_declaration)
8112 {
8113 add_partial_symbol (pdi, cu);
8114 }
8115 if ((cu->language == language_rust
8116 || cu->language == language_cplus) && pdi->has_children)
8117 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8118 set_addrmap, cu);
8119 break;
8120 case DW_TAG_enumeration_type:
8121 if (!pdi->is_declaration)
8122 add_partial_enumeration (pdi, cu);
8123 break;
8124 case DW_TAG_base_type:
8125 case DW_TAG_subrange_type:
8126 /* File scope base type definitions are added to the partial
8127 symbol table. */
8128 add_partial_symbol (pdi, cu);
8129 break;
8130 case DW_TAG_namespace:
8131 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8132 break;
8133 case DW_TAG_module:
8134 if (!pdi->is_declaration)
8135 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8136 break;
8137 case DW_TAG_imported_unit:
8138 {
8139 struct dwarf2_per_cu_data *per_cu;
8140
8141 /* For now we don't handle imported units in type units. */
8142 if (cu->per_cu->is_debug_types)
8143 {
8144 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8145 " supported in type units [in module %s]"),
8146 objfile_name (cu->per_objfile->objfile));
8147 }
8148
8149 per_cu = dwarf2_find_containing_comp_unit
8150 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8151
8152 /* Go read the partial unit, if needed. */
8153 if (per_cu->v.psymtab == NULL)
8154 process_psymtab_comp_unit (per_cu, true, cu->language);
8155
8156 cu->per_cu->imported_symtabs_push (per_cu);
8157 }
8158 break;
8159 case DW_TAG_imported_declaration:
8160 add_partial_symbol (pdi, cu);
8161 break;
8162 default:
8163 break;
8164 }
8165 }
8166
8167 /* If the die has a sibling, skip to the sibling. */
8168
8169 pdi = pdi->die_sibling;
8170 }
8171 }
8172
8173 /* Functions used to compute the fully scoped name of a partial DIE.
8174
8175 Normally, this is simple. For C++, the parent DIE's fully scoped
8176 name is concatenated with "::" and the partial DIE's name.
8177 Enumerators are an exception; they use the scope of their parent
8178 enumeration type, i.e. the name of the enumeration type is not
8179 prepended to the enumerator.
8180
8181 There are two complexities. One is DW_AT_specification; in this
8182 case "parent" means the parent of the target of the specification,
8183 instead of the direct parent of the DIE. The other is compilers
8184 which do not emit DW_TAG_namespace; in this case we try to guess
8185 the fully qualified name of structure types from their members'
8186 linkage names. This must be done using the DIE's children rather
8187 than the children of any DW_AT_specification target. We only need
8188 to do this for structures at the top level, i.e. if the target of
8189 any DW_AT_specification (if any; otherwise the DIE itself) does not
8190 have a parent. */
8191
8192 /* Compute the scope prefix associated with PDI's parent, in
8193 compilation unit CU. The result will be allocated on CU's
8194 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8195 field. NULL is returned if no prefix is necessary. */
8196 static const char *
8197 partial_die_parent_scope (struct partial_die_info *pdi,
8198 struct dwarf2_cu *cu)
8199 {
8200 const char *grandparent_scope;
8201 struct partial_die_info *parent, *real_pdi;
8202
8203 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8204 then this means the parent of the specification DIE. */
8205
8206 real_pdi = pdi;
8207 while (real_pdi->has_specification)
8208 {
8209 auto res = find_partial_die (real_pdi->spec_offset,
8210 real_pdi->spec_is_dwz, cu);
8211 real_pdi = res.pdi;
8212 cu = res.cu;
8213 }
8214
8215 parent = real_pdi->die_parent;
8216 if (parent == NULL)
8217 return NULL;
8218
8219 if (parent->scope_set)
8220 return parent->scope;
8221
8222 parent->fixup (cu);
8223
8224 grandparent_scope = partial_die_parent_scope (parent, cu);
8225
8226 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8227 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8228 Work around this problem here. */
8229 if (cu->language == language_cplus
8230 && parent->tag == DW_TAG_namespace
8231 && strcmp (parent->name, "::") == 0
8232 && grandparent_scope == NULL)
8233 {
8234 parent->scope = NULL;
8235 parent->scope_set = 1;
8236 return NULL;
8237 }
8238
8239 /* Nested subroutines in Fortran get a prefix. */
8240 if (pdi->tag == DW_TAG_enumerator)
8241 /* Enumerators should not get the name of the enumeration as a prefix. */
8242 parent->scope = grandparent_scope;
8243 else if (parent->tag == DW_TAG_namespace
8244 || parent->tag == DW_TAG_module
8245 || parent->tag == DW_TAG_structure_type
8246 || parent->tag == DW_TAG_class_type
8247 || parent->tag == DW_TAG_interface_type
8248 || parent->tag == DW_TAG_union_type
8249 || parent->tag == DW_TAG_enumeration_type
8250 || (cu->language == language_fortran
8251 && parent->tag == DW_TAG_subprogram
8252 && pdi->tag == DW_TAG_subprogram))
8253 {
8254 if (grandparent_scope == NULL)
8255 parent->scope = parent->name;
8256 else
8257 parent->scope = typename_concat (&cu->comp_unit_obstack,
8258 grandparent_scope,
8259 parent->name, 0, cu);
8260 }
8261 else
8262 {
8263 /* FIXME drow/2004-04-01: What should we be doing with
8264 function-local names? For partial symbols, we should probably be
8265 ignoring them. */
8266 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8267 dwarf_tag_name (parent->tag),
8268 sect_offset_str (pdi->sect_off));
8269 parent->scope = grandparent_scope;
8270 }
8271
8272 parent->scope_set = 1;
8273 return parent->scope;
8274 }
8275
8276 /* Return the fully scoped name associated with PDI, from compilation unit
8277 CU. The result will be allocated with malloc. */
8278
8279 static gdb::unique_xmalloc_ptr<char>
8280 partial_die_full_name (struct partial_die_info *pdi,
8281 struct dwarf2_cu *cu)
8282 {
8283 const char *parent_scope;
8284
8285 /* If this is a template instantiation, we can not work out the
8286 template arguments from partial DIEs. So, unfortunately, we have
8287 to go through the full DIEs. At least any work we do building
8288 types here will be reused if full symbols are loaded later. */
8289 if (pdi->has_template_arguments)
8290 {
8291 pdi->fixup (cu);
8292
8293 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8294 {
8295 struct die_info *die;
8296 struct attribute attr;
8297 struct dwarf2_cu *ref_cu = cu;
8298
8299 /* DW_FORM_ref_addr is using section offset. */
8300 attr.name = (enum dwarf_attribute) 0;
8301 attr.form = DW_FORM_ref_addr;
8302 attr.u.unsnd = to_underlying (pdi->sect_off);
8303 die = follow_die_ref (NULL, &attr, &ref_cu);
8304
8305 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8306 }
8307 }
8308
8309 parent_scope = partial_die_parent_scope (pdi, cu);
8310 if (parent_scope == NULL)
8311 return NULL;
8312 else
8313 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8314 pdi->name, 0, cu));
8315 }
8316
8317 static void
8318 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8319 {
8320 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8321 struct objfile *objfile = dwarf2_per_objfile->objfile;
8322 struct gdbarch *gdbarch = objfile->arch ();
8323 CORE_ADDR addr = 0;
8324 const char *actual_name = NULL;
8325 CORE_ADDR baseaddr;
8326
8327 baseaddr = objfile->text_section_offset ();
8328
8329 gdb::unique_xmalloc_ptr<char> built_actual_name
8330 = partial_die_full_name (pdi, cu);
8331 if (built_actual_name != NULL)
8332 actual_name = built_actual_name.get ();
8333
8334 if (actual_name == NULL)
8335 actual_name = pdi->name;
8336
8337 partial_symbol psymbol;
8338 memset (&psymbol, 0, sizeof (psymbol));
8339 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8340 psymbol.ginfo.section = -1;
8341
8342 /* The code below indicates that the psymbol should be installed by
8343 setting this. */
8344 gdb::optional<psymbol_placement> where;
8345
8346 switch (pdi->tag)
8347 {
8348 case DW_TAG_inlined_subroutine:
8349 case DW_TAG_subprogram:
8350 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8351 - baseaddr);
8352 if (pdi->is_external
8353 || cu->language == language_ada
8354 || (cu->language == language_fortran
8355 && pdi->die_parent != NULL
8356 && pdi->die_parent->tag == DW_TAG_subprogram))
8357 {
8358 /* Normally, only "external" DIEs are part of the global scope.
8359 But in Ada and Fortran, we want to be able to access nested
8360 procedures globally. So all Ada and Fortran subprograms are
8361 stored in the global scope. */
8362 where = psymbol_placement::GLOBAL;
8363 }
8364 else
8365 where = psymbol_placement::STATIC;
8366
8367 psymbol.domain = VAR_DOMAIN;
8368 psymbol.aclass = LOC_BLOCK;
8369 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8370 psymbol.ginfo.value.address = addr;
8371
8372 if (pdi->main_subprogram && actual_name != NULL)
8373 set_objfile_main_name (objfile, actual_name, cu->language);
8374 break;
8375 case DW_TAG_constant:
8376 psymbol.domain = VAR_DOMAIN;
8377 psymbol.aclass = LOC_STATIC;
8378 where = (pdi->is_external
8379 ? psymbol_placement::GLOBAL
8380 : psymbol_placement::STATIC);
8381 break;
8382 case DW_TAG_variable:
8383 if (pdi->d.locdesc)
8384 addr = decode_locdesc (pdi->d.locdesc, cu);
8385
8386 if (pdi->d.locdesc
8387 && addr == 0
8388 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8389 {
8390 /* A global or static variable may also have been stripped
8391 out by the linker if unused, in which case its address
8392 will be nullified; do not add such variables into partial
8393 symbol table then. */
8394 }
8395 else if (pdi->is_external)
8396 {
8397 /* Global Variable.
8398 Don't enter into the minimal symbol tables as there is
8399 a minimal symbol table entry from the ELF symbols already.
8400 Enter into partial symbol table if it has a location
8401 descriptor or a type.
8402 If the location descriptor is missing, new_symbol will create
8403 a LOC_UNRESOLVED symbol, the address of the variable will then
8404 be determined from the minimal symbol table whenever the variable
8405 is referenced.
8406 The address for the partial symbol table entry is not
8407 used by GDB, but it comes in handy for debugging partial symbol
8408 table building. */
8409
8410 if (pdi->d.locdesc || pdi->has_type)
8411 {
8412 psymbol.domain = VAR_DOMAIN;
8413 psymbol.aclass = LOC_STATIC;
8414 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8415 psymbol.ginfo.value.address = addr;
8416 where = psymbol_placement::GLOBAL;
8417 }
8418 }
8419 else
8420 {
8421 int has_loc = pdi->d.locdesc != NULL;
8422
8423 /* Static Variable. Skip symbols whose value we cannot know (those
8424 without location descriptors or constant values). */
8425 if (!has_loc && !pdi->has_const_value)
8426 return;
8427
8428 psymbol.domain = VAR_DOMAIN;
8429 psymbol.aclass = LOC_STATIC;
8430 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8431 if (has_loc)
8432 psymbol.ginfo.value.address = addr;
8433 where = psymbol_placement::STATIC;
8434 }
8435 break;
8436 case DW_TAG_typedef:
8437 case DW_TAG_base_type:
8438 case DW_TAG_subrange_type:
8439 psymbol.domain = VAR_DOMAIN;
8440 psymbol.aclass = LOC_TYPEDEF;
8441 where = psymbol_placement::STATIC;
8442 break;
8443 case DW_TAG_imported_declaration:
8444 case DW_TAG_namespace:
8445 psymbol.domain = VAR_DOMAIN;
8446 psymbol.aclass = LOC_TYPEDEF;
8447 where = psymbol_placement::GLOBAL;
8448 break;
8449 case DW_TAG_module:
8450 /* With Fortran 77 there might be a "BLOCK DATA" module
8451 available without any name. If so, we skip the module as it
8452 doesn't bring any value. */
8453 if (actual_name != nullptr)
8454 {
8455 psymbol.domain = MODULE_DOMAIN;
8456 psymbol.aclass = LOC_TYPEDEF;
8457 where = psymbol_placement::GLOBAL;
8458 }
8459 break;
8460 case DW_TAG_class_type:
8461 case DW_TAG_interface_type:
8462 case DW_TAG_structure_type:
8463 case DW_TAG_union_type:
8464 case DW_TAG_enumeration_type:
8465 /* Skip external references. The DWARF standard says in the section
8466 about "Structure, Union, and Class Type Entries": "An incomplete
8467 structure, union or class type is represented by a structure,
8468 union or class entry that does not have a byte size attribute
8469 and that has a DW_AT_declaration attribute." */
8470 if (!pdi->has_byte_size && pdi->is_declaration)
8471 return;
8472
8473 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8474 static vs. global. */
8475 psymbol.domain = STRUCT_DOMAIN;
8476 psymbol.aclass = LOC_TYPEDEF;
8477 where = (cu->language == language_cplus
8478 ? psymbol_placement::GLOBAL
8479 : psymbol_placement::STATIC);
8480 break;
8481 case DW_TAG_enumerator:
8482 psymbol.domain = VAR_DOMAIN;
8483 psymbol.aclass = LOC_CONST;
8484 where = (cu->language == language_cplus
8485 ? psymbol_placement::GLOBAL
8486 : psymbol_placement::STATIC);
8487 break;
8488 default:
8489 break;
8490 }
8491
8492 if (where.has_value ())
8493 {
8494 if (built_actual_name != nullptr)
8495 actual_name = objfile->intern (actual_name);
8496 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8497 psymbol.ginfo.set_linkage_name (actual_name);
8498 else
8499 {
8500 psymbol.ginfo.set_demangled_name (actual_name,
8501 &objfile->objfile_obstack);
8502 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8503 }
8504 add_psymbol_to_list (psymbol, *where, objfile);
8505 }
8506 }
8507
8508 /* Read a partial die corresponding to a namespace; also, add a symbol
8509 corresponding to that namespace to the symbol table. NAMESPACE is
8510 the name of the enclosing namespace. */
8511
8512 static void
8513 add_partial_namespace (struct partial_die_info *pdi,
8514 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8515 int set_addrmap, struct dwarf2_cu *cu)
8516 {
8517 /* Add a symbol for the namespace. */
8518
8519 add_partial_symbol (pdi, cu);
8520
8521 /* Now scan partial symbols in that namespace. */
8522
8523 if (pdi->has_children)
8524 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8525 }
8526
8527 /* Read a partial die corresponding to a Fortran module. */
8528
8529 static void
8530 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8531 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8532 {
8533 /* Add a symbol for the namespace. */
8534
8535 add_partial_symbol (pdi, cu);
8536
8537 /* Now scan partial symbols in that module. */
8538
8539 if (pdi->has_children)
8540 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8541 }
8542
8543 /* Read a partial die corresponding to a subprogram or an inlined
8544 subprogram and create a partial symbol for that subprogram.
8545 When the CU language allows it, this routine also defines a partial
8546 symbol for each nested subprogram that this subprogram contains.
8547 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8548 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8549
8550 PDI may also be a lexical block, in which case we simply search
8551 recursively for subprograms defined inside that lexical block.
8552 Again, this is only performed when the CU language allows this
8553 type of definitions. */
8554
8555 static void
8556 add_partial_subprogram (struct partial_die_info *pdi,
8557 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8558 int set_addrmap, struct dwarf2_cu *cu)
8559 {
8560 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8561 {
8562 if (pdi->has_pc_info)
8563 {
8564 if (pdi->lowpc < *lowpc)
8565 *lowpc = pdi->lowpc;
8566 if (pdi->highpc > *highpc)
8567 *highpc = pdi->highpc;
8568 if (set_addrmap)
8569 {
8570 struct objfile *objfile = cu->per_objfile->objfile;
8571 struct gdbarch *gdbarch = objfile->arch ();
8572 CORE_ADDR baseaddr;
8573 CORE_ADDR this_highpc;
8574 CORE_ADDR this_lowpc;
8575
8576 baseaddr = objfile->text_section_offset ();
8577 this_lowpc
8578 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8579 pdi->lowpc + baseaddr)
8580 - baseaddr);
8581 this_highpc
8582 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8583 pdi->highpc + baseaddr)
8584 - baseaddr);
8585 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8586 this_lowpc, this_highpc - 1,
8587 cu->per_cu->v.psymtab);
8588 }
8589 }
8590
8591 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8592 {
8593 if (!pdi->is_declaration)
8594 /* Ignore subprogram DIEs that do not have a name, they are
8595 illegal. Do not emit a complaint at this point, we will
8596 do so when we convert this psymtab into a symtab. */
8597 if (pdi->name)
8598 add_partial_symbol (pdi, cu);
8599 }
8600 }
8601
8602 if (! pdi->has_children)
8603 return;
8604
8605 if (cu->language == language_ada || cu->language == language_fortran)
8606 {
8607 pdi = pdi->die_child;
8608 while (pdi != NULL)
8609 {
8610 pdi->fixup (cu);
8611 if (pdi->tag == DW_TAG_subprogram
8612 || pdi->tag == DW_TAG_inlined_subroutine
8613 || pdi->tag == DW_TAG_lexical_block)
8614 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8615 pdi = pdi->die_sibling;
8616 }
8617 }
8618 }
8619
8620 /* Read a partial die corresponding to an enumeration type. */
8621
8622 static void
8623 add_partial_enumeration (struct partial_die_info *enum_pdi,
8624 struct dwarf2_cu *cu)
8625 {
8626 struct partial_die_info *pdi;
8627
8628 if (enum_pdi->name != NULL)
8629 add_partial_symbol (enum_pdi, cu);
8630
8631 pdi = enum_pdi->die_child;
8632 while (pdi)
8633 {
8634 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8635 complaint (_("malformed enumerator DIE ignored"));
8636 else
8637 add_partial_symbol (pdi, cu);
8638 pdi = pdi->die_sibling;
8639 }
8640 }
8641
8642 /* Return the initial uleb128 in the die at INFO_PTR. */
8643
8644 static unsigned int
8645 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8646 {
8647 unsigned int bytes_read;
8648
8649 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8650 }
8651
8652 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8653 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8654
8655 Return the corresponding abbrev, or NULL if the number is zero (indicating
8656 an empty DIE). In either case *BYTES_READ will be set to the length of
8657 the initial number. */
8658
8659 static struct abbrev_info *
8660 peek_die_abbrev (const die_reader_specs &reader,
8661 const gdb_byte *info_ptr, unsigned int *bytes_read)
8662 {
8663 dwarf2_cu *cu = reader.cu;
8664 bfd *abfd = cu->per_objfile->objfile->obfd;
8665 unsigned int abbrev_number
8666 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8667
8668 if (abbrev_number == 0)
8669 return NULL;
8670
8671 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8672 if (!abbrev)
8673 {
8674 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8675 " at offset %s [in module %s]"),
8676 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8677 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8678 }
8679
8680 return abbrev;
8681 }
8682
8683 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8684 Returns a pointer to the end of a series of DIEs, terminated by an empty
8685 DIE. Any children of the skipped DIEs will also be skipped. */
8686
8687 static const gdb_byte *
8688 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8689 {
8690 while (1)
8691 {
8692 unsigned int bytes_read;
8693 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8694
8695 if (abbrev == NULL)
8696 return info_ptr + bytes_read;
8697 else
8698 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8699 }
8700 }
8701
8702 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8703 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8704 abbrev corresponding to that skipped uleb128 should be passed in
8705 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8706 children. */
8707
8708 static const gdb_byte *
8709 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8710 struct abbrev_info *abbrev)
8711 {
8712 unsigned int bytes_read;
8713 struct attribute attr;
8714 bfd *abfd = reader->abfd;
8715 struct dwarf2_cu *cu = reader->cu;
8716 const gdb_byte *buffer = reader->buffer;
8717 const gdb_byte *buffer_end = reader->buffer_end;
8718 unsigned int form, i;
8719
8720 for (i = 0; i < abbrev->num_attrs; i++)
8721 {
8722 /* The only abbrev we care about is DW_AT_sibling. */
8723 if (abbrev->attrs[i].name == DW_AT_sibling)
8724 {
8725 bool ignored;
8726 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8727 &ignored);
8728 if (attr.form == DW_FORM_ref_addr)
8729 complaint (_("ignoring absolute DW_AT_sibling"));
8730 else
8731 {
8732 sect_offset off = attr.get_ref_die_offset ();
8733 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8734
8735 if (sibling_ptr < info_ptr)
8736 complaint (_("DW_AT_sibling points backwards"));
8737 else if (sibling_ptr > reader->buffer_end)
8738 reader->die_section->overflow_complaint ();
8739 else
8740 return sibling_ptr;
8741 }
8742 }
8743
8744 /* If it isn't DW_AT_sibling, skip this attribute. */
8745 form = abbrev->attrs[i].form;
8746 skip_attribute:
8747 switch (form)
8748 {
8749 case DW_FORM_ref_addr:
8750 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8751 and later it is offset sized. */
8752 if (cu->header.version == 2)
8753 info_ptr += cu->header.addr_size;
8754 else
8755 info_ptr += cu->header.offset_size;
8756 break;
8757 case DW_FORM_GNU_ref_alt:
8758 info_ptr += cu->header.offset_size;
8759 break;
8760 case DW_FORM_addr:
8761 info_ptr += cu->header.addr_size;
8762 break;
8763 case DW_FORM_data1:
8764 case DW_FORM_ref1:
8765 case DW_FORM_flag:
8766 case DW_FORM_strx1:
8767 info_ptr += 1;
8768 break;
8769 case DW_FORM_flag_present:
8770 case DW_FORM_implicit_const:
8771 break;
8772 case DW_FORM_data2:
8773 case DW_FORM_ref2:
8774 case DW_FORM_strx2:
8775 info_ptr += 2;
8776 break;
8777 case DW_FORM_strx3:
8778 info_ptr += 3;
8779 break;
8780 case DW_FORM_data4:
8781 case DW_FORM_ref4:
8782 case DW_FORM_strx4:
8783 info_ptr += 4;
8784 break;
8785 case DW_FORM_data8:
8786 case DW_FORM_ref8:
8787 case DW_FORM_ref_sig8:
8788 info_ptr += 8;
8789 break;
8790 case DW_FORM_data16:
8791 info_ptr += 16;
8792 break;
8793 case DW_FORM_string:
8794 read_direct_string (abfd, info_ptr, &bytes_read);
8795 info_ptr += bytes_read;
8796 break;
8797 case DW_FORM_sec_offset:
8798 case DW_FORM_strp:
8799 case DW_FORM_GNU_strp_alt:
8800 info_ptr += cu->header.offset_size;
8801 break;
8802 case DW_FORM_exprloc:
8803 case DW_FORM_block:
8804 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8805 info_ptr += bytes_read;
8806 break;
8807 case DW_FORM_block1:
8808 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8809 break;
8810 case DW_FORM_block2:
8811 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8812 break;
8813 case DW_FORM_block4:
8814 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8815 break;
8816 case DW_FORM_addrx:
8817 case DW_FORM_strx:
8818 case DW_FORM_sdata:
8819 case DW_FORM_udata:
8820 case DW_FORM_ref_udata:
8821 case DW_FORM_GNU_addr_index:
8822 case DW_FORM_GNU_str_index:
8823 case DW_FORM_rnglistx:
8824 case DW_FORM_loclistx:
8825 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8826 break;
8827 case DW_FORM_indirect:
8828 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8829 info_ptr += bytes_read;
8830 /* We need to continue parsing from here, so just go back to
8831 the top. */
8832 goto skip_attribute;
8833
8834 default:
8835 error (_("Dwarf Error: Cannot handle %s "
8836 "in DWARF reader [in module %s]"),
8837 dwarf_form_name (form),
8838 bfd_get_filename (abfd));
8839 }
8840 }
8841
8842 if (abbrev->has_children)
8843 return skip_children (reader, info_ptr);
8844 else
8845 return info_ptr;
8846 }
8847
8848 /* Locate ORIG_PDI's sibling.
8849 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8850
8851 static const gdb_byte *
8852 locate_pdi_sibling (const struct die_reader_specs *reader,
8853 struct partial_die_info *orig_pdi,
8854 const gdb_byte *info_ptr)
8855 {
8856 /* Do we know the sibling already? */
8857
8858 if (orig_pdi->sibling)
8859 return orig_pdi->sibling;
8860
8861 /* Are there any children to deal with? */
8862
8863 if (!orig_pdi->has_children)
8864 return info_ptr;
8865
8866 /* Skip the children the long way. */
8867
8868 return skip_children (reader, info_ptr);
8869 }
8870
8871 /* Expand this partial symbol table into a full symbol table. SELF is
8872 not NULL. */
8873
8874 void
8875 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8876 {
8877 struct dwarf2_per_objfile *dwarf2_per_objfile
8878 = get_dwarf2_per_objfile (objfile);
8879
8880 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8881
8882 /* If this psymtab is constructed from a debug-only objfile, the
8883 has_section_at_zero flag will not necessarily be correct. We
8884 can get the correct value for this flag by looking at the data
8885 associated with the (presumably stripped) associated objfile. */
8886 if (objfile->separate_debug_objfile_backlink)
8887 {
8888 struct dwarf2_per_objfile *dpo_backlink
8889 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8890
8891 dwarf2_per_objfile->per_bfd->has_section_at_zero
8892 = dpo_backlink->per_bfd->has_section_at_zero;
8893 }
8894
8895 expand_psymtab (objfile);
8896
8897 process_cu_includes (dwarf2_per_objfile);
8898 }
8899 \f
8900 /* Reading in full CUs. */
8901
8902 /* Add PER_CU to the queue. */
8903
8904 static void
8905 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8906 enum language pretend_language)
8907 {
8908 per_cu->queued = 1;
8909 per_cu->dwarf2_per_objfile->per_bfd->queue.emplace (per_cu, pretend_language);
8910 }
8911
8912 /* If PER_CU is not yet queued, add it to the queue.
8913 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8914 dependency.
8915 The result is non-zero if PER_CU was queued, otherwise the result is zero
8916 meaning either PER_CU is already queued or it is already loaded.
8917
8918 N.B. There is an invariant here that if a CU is queued then it is loaded.
8919 The caller is required to load PER_CU if we return non-zero. */
8920
8921 static int
8922 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8923 struct dwarf2_per_cu_data *per_cu,
8924 enum language pretend_language)
8925 {
8926 /* We may arrive here during partial symbol reading, if we need full
8927 DIEs to process an unusual case (e.g. template arguments). Do
8928 not queue PER_CU, just tell our caller to load its DIEs. */
8929 if (per_cu->dwarf2_per_objfile->per_bfd->reading_partial_symbols)
8930 {
8931 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8932 return 1;
8933 return 0;
8934 }
8935
8936 /* Mark the dependence relation so that we don't flush PER_CU
8937 too early. */
8938 if (dependent_cu != NULL)
8939 dwarf2_add_dependence (dependent_cu, per_cu);
8940
8941 /* If it's already on the queue, we have nothing to do. */
8942 if (per_cu->queued)
8943 return 0;
8944
8945 /* If the compilation unit is already loaded, just mark it as
8946 used. */
8947 if (per_cu->cu != NULL)
8948 {
8949 per_cu->cu->last_used = 0;
8950 return 0;
8951 }
8952
8953 /* Add it to the queue. */
8954 queue_comp_unit (per_cu, pretend_language);
8955
8956 return 1;
8957 }
8958
8959 /* Process the queue. */
8960
8961 static void
8962 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8963 {
8964 if (dwarf_read_debug)
8965 {
8966 fprintf_unfiltered (gdb_stdlog,
8967 "Expanding one or more symtabs of objfile %s ...\n",
8968 objfile_name (dwarf2_per_objfile->objfile));
8969 }
8970
8971 /* The queue starts out with one item, but following a DIE reference
8972 may load a new CU, adding it to the end of the queue. */
8973 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
8974 {
8975 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
8976
8977 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
8978 /* Skip dummy CUs. */
8979 && item.per_cu->cu != NULL)
8980 {
8981 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8982 unsigned int debug_print_threshold;
8983 char buf[100];
8984
8985 if (per_cu->is_debug_types)
8986 {
8987 struct signatured_type *sig_type =
8988 (struct signatured_type *) per_cu;
8989
8990 sprintf (buf, "TU %s at offset %s",
8991 hex_string (sig_type->signature),
8992 sect_offset_str (per_cu->sect_off));
8993 /* There can be 100s of TUs.
8994 Only print them in verbose mode. */
8995 debug_print_threshold = 2;
8996 }
8997 else
8998 {
8999 sprintf (buf, "CU at offset %s",
9000 sect_offset_str (per_cu->sect_off));
9001 debug_print_threshold = 1;
9002 }
9003
9004 if (dwarf_read_debug >= debug_print_threshold)
9005 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9006
9007 if (per_cu->is_debug_types)
9008 process_full_type_unit (per_cu, item.pretend_language);
9009 else
9010 process_full_comp_unit (per_cu, item.pretend_language);
9011
9012 if (dwarf_read_debug >= debug_print_threshold)
9013 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9014 }
9015
9016 item.per_cu->queued = 0;
9017 dwarf2_per_objfile->per_bfd->queue.pop ();
9018 }
9019
9020 if (dwarf_read_debug)
9021 {
9022 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9023 objfile_name (dwarf2_per_objfile->objfile));
9024 }
9025 }
9026
9027 /* Read in full symbols for PST, and anything it depends on. */
9028
9029 void
9030 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9031 {
9032 gdb_assert (!readin_p (objfile));
9033
9034 expand_dependencies (objfile);
9035
9036 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9037 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9038 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9039 }
9040
9041 /* See psympriv.h. */
9042
9043 bool
9044 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9045 {
9046 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9047 return per_objfile->symtab_set_p (per_cu_data);
9048 }
9049
9050 /* See psympriv.h. */
9051
9052 compunit_symtab *
9053 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9054 {
9055 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9056 return per_objfile->get_symtab (per_cu_data);
9057 }
9058
9059 /* Trivial hash function for die_info: the hash value of a DIE
9060 is its offset in .debug_info for this objfile. */
9061
9062 static hashval_t
9063 die_hash (const void *item)
9064 {
9065 const struct die_info *die = (const struct die_info *) item;
9066
9067 return to_underlying (die->sect_off);
9068 }
9069
9070 /* Trivial comparison function for die_info structures: two DIEs
9071 are equal if they have the same offset. */
9072
9073 static int
9074 die_eq (const void *item_lhs, const void *item_rhs)
9075 {
9076 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9077 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9078
9079 return die_lhs->sect_off == die_rhs->sect_off;
9080 }
9081
9082 /* Load the DIEs associated with PER_CU into memory. */
9083
9084 static void
9085 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9086 bool skip_partial,
9087 enum language pretend_language)
9088 {
9089 gdb_assert (! this_cu->is_debug_types);
9090
9091 cutu_reader reader (this_cu, NULL, 1, skip_partial);
9092 if (reader.dummy_p)
9093 return;
9094
9095 struct dwarf2_cu *cu = reader.cu;
9096 const gdb_byte *info_ptr = reader.info_ptr;
9097
9098 gdb_assert (cu->die_hash == NULL);
9099 cu->die_hash =
9100 htab_create_alloc_ex (cu->header.length / 12,
9101 die_hash,
9102 die_eq,
9103 NULL,
9104 &cu->comp_unit_obstack,
9105 hashtab_obstack_allocate,
9106 dummy_obstack_deallocate);
9107
9108 if (reader.comp_unit_die->has_children)
9109 reader.comp_unit_die->child
9110 = read_die_and_siblings (&reader, reader.info_ptr,
9111 &info_ptr, reader.comp_unit_die);
9112 cu->dies = reader.comp_unit_die;
9113 /* comp_unit_die is not stored in die_hash, no need. */
9114
9115 /* We try not to read any attributes in this function, because not
9116 all CUs needed for references have been loaded yet, and symbol
9117 table processing isn't initialized. But we have to set the CU language,
9118 or we won't be able to build types correctly.
9119 Similarly, if we do not read the producer, we can not apply
9120 producer-specific interpretation. */
9121 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9122
9123 reader.keep ();
9124 }
9125
9126 /* Add a DIE to the delayed physname list. */
9127
9128 static void
9129 add_to_method_list (struct type *type, int fnfield_index, int index,
9130 const char *name, struct die_info *die,
9131 struct dwarf2_cu *cu)
9132 {
9133 struct delayed_method_info mi;
9134 mi.type = type;
9135 mi.fnfield_index = fnfield_index;
9136 mi.index = index;
9137 mi.name = name;
9138 mi.die = die;
9139 cu->method_list.push_back (mi);
9140 }
9141
9142 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9143 "const" / "volatile". If so, decrements LEN by the length of the
9144 modifier and return true. Otherwise return false. */
9145
9146 template<size_t N>
9147 static bool
9148 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9149 {
9150 size_t mod_len = sizeof (mod) - 1;
9151 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9152 {
9153 len -= mod_len;
9154 return true;
9155 }
9156 return false;
9157 }
9158
9159 /* Compute the physnames of any methods on the CU's method list.
9160
9161 The computation of method physnames is delayed in order to avoid the
9162 (bad) condition that one of the method's formal parameters is of an as yet
9163 incomplete type. */
9164
9165 static void
9166 compute_delayed_physnames (struct dwarf2_cu *cu)
9167 {
9168 /* Only C++ delays computing physnames. */
9169 if (cu->method_list.empty ())
9170 return;
9171 gdb_assert (cu->language == language_cplus);
9172
9173 for (const delayed_method_info &mi : cu->method_list)
9174 {
9175 const char *physname;
9176 struct fn_fieldlist *fn_flp
9177 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9178 physname = dwarf2_physname (mi.name, mi.die, cu);
9179 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9180 = physname ? physname : "";
9181
9182 /* Since there's no tag to indicate whether a method is a
9183 const/volatile overload, extract that information out of the
9184 demangled name. */
9185 if (physname != NULL)
9186 {
9187 size_t len = strlen (physname);
9188
9189 while (1)
9190 {
9191 if (physname[len] == ')') /* shortcut */
9192 break;
9193 else if (check_modifier (physname, len, " const"))
9194 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9195 else if (check_modifier (physname, len, " volatile"))
9196 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9197 else
9198 break;
9199 }
9200 }
9201 }
9202
9203 /* The list is no longer needed. */
9204 cu->method_list.clear ();
9205 }
9206
9207 /* Go objects should be embedded in a DW_TAG_module DIE,
9208 and it's not clear if/how imported objects will appear.
9209 To keep Go support simple until that's worked out,
9210 go back through what we've read and create something usable.
9211 We could do this while processing each DIE, and feels kinda cleaner,
9212 but that way is more invasive.
9213 This is to, for example, allow the user to type "p var" or "b main"
9214 without having to specify the package name, and allow lookups
9215 of module.object to work in contexts that use the expression
9216 parser. */
9217
9218 static void
9219 fixup_go_packaging (struct dwarf2_cu *cu)
9220 {
9221 gdb::unique_xmalloc_ptr<char> package_name;
9222 struct pending *list;
9223 int i;
9224
9225 for (list = *cu->get_builder ()->get_global_symbols ();
9226 list != NULL;
9227 list = list->next)
9228 {
9229 for (i = 0; i < list->nsyms; ++i)
9230 {
9231 struct symbol *sym = list->symbol[i];
9232
9233 if (sym->language () == language_go
9234 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9235 {
9236 gdb::unique_xmalloc_ptr<char> this_package_name
9237 (go_symbol_package_name (sym));
9238
9239 if (this_package_name == NULL)
9240 continue;
9241 if (package_name == NULL)
9242 package_name = std::move (this_package_name);
9243 else
9244 {
9245 struct objfile *objfile = cu->per_objfile->objfile;
9246 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9247 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9248 (symbol_symtab (sym) != NULL
9249 ? symtab_to_filename_for_display
9250 (symbol_symtab (sym))
9251 : objfile_name (objfile)),
9252 this_package_name.get (), package_name.get ());
9253 }
9254 }
9255 }
9256 }
9257
9258 if (package_name != NULL)
9259 {
9260 struct objfile *objfile = cu->per_objfile->objfile;
9261 const char *saved_package_name = objfile->intern (package_name.get ());
9262 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9263 saved_package_name);
9264 struct symbol *sym;
9265
9266 sym = new (&objfile->objfile_obstack) symbol;
9267 sym->set_language (language_go, &objfile->objfile_obstack);
9268 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9269 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9270 e.g., "main" finds the "main" module and not C's main(). */
9271 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9272 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9273 SYMBOL_TYPE (sym) = type;
9274
9275 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9276 }
9277 }
9278
9279 /* Allocate a fully-qualified name consisting of the two parts on the
9280 obstack. */
9281
9282 static const char *
9283 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9284 {
9285 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9286 }
9287
9288 /* A helper that allocates a variant part to attach to a Rust enum
9289 type. OBSTACK is where the results should be allocated. TYPE is
9290 the type we're processing. DISCRIMINANT_INDEX is the index of the
9291 discriminant. It must be the index of one of the fields of TYPE.
9292 DEFAULT_INDEX is the index of the default field; or -1 if there is
9293 no default. RANGES is indexed by "effective" field number (the
9294 field index, but omitting the discriminant and default fields) and
9295 must hold the discriminant values used by the variants. Note that
9296 RANGES must have a lifetime at least as long as OBSTACK -- either
9297 already allocated on it, or static. */
9298
9299 static void
9300 alloc_rust_variant (struct obstack *obstack, struct type *type,
9301 int discriminant_index, int default_index,
9302 gdb::array_view<discriminant_range> ranges)
9303 {
9304 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9305 must be handled by the caller. */
9306 gdb_assert (discriminant_index >= 0
9307 && discriminant_index < type->num_fields ());
9308 gdb_assert (default_index == -1
9309 || (default_index >= 0 && default_index < type->num_fields ()));
9310
9311 /* We have one variant for each non-discriminant field. */
9312 int n_variants = type->num_fields () - 1;
9313
9314 variant *variants = new (obstack) variant[n_variants];
9315 int var_idx = 0;
9316 int range_idx = 0;
9317 for (int i = 0; i < type->num_fields (); ++i)
9318 {
9319 if (i == discriminant_index)
9320 continue;
9321
9322 variants[var_idx].first_field = i;
9323 variants[var_idx].last_field = i + 1;
9324
9325 /* The default field does not need a range, but other fields do.
9326 We skipped the discriminant above. */
9327 if (i != default_index)
9328 {
9329 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9330 ++range_idx;
9331 }
9332
9333 ++var_idx;
9334 }
9335
9336 gdb_assert (range_idx == ranges.size ());
9337 gdb_assert (var_idx == n_variants);
9338
9339 variant_part *part = new (obstack) variant_part;
9340 part->discriminant_index = discriminant_index;
9341 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9342 discriminant_index));
9343 part->variants = gdb::array_view<variant> (variants, n_variants);
9344
9345 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9346 gdb::array_view<variant_part> *prop_value
9347 = new (storage) gdb::array_view<variant_part> (part, 1);
9348
9349 struct dynamic_prop prop;
9350 prop.kind = PROP_VARIANT_PARTS;
9351 prop.data.variant_parts = prop_value;
9352
9353 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9354 }
9355
9356 /* Some versions of rustc emitted enums in an unusual way.
9357
9358 Ordinary enums were emitted as unions. The first element of each
9359 structure in the union was named "RUST$ENUM$DISR". This element
9360 held the discriminant.
9361
9362 These versions of Rust also implemented the "non-zero"
9363 optimization. When the enum had two values, and one is empty and
9364 the other holds a pointer that cannot be zero, the pointer is used
9365 as the discriminant, with a zero value meaning the empty variant.
9366 Here, the union's first member is of the form
9367 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9368 where the fieldnos are the indices of the fields that should be
9369 traversed in order to find the field (which may be several fields deep)
9370 and the variantname is the name of the variant of the case when the
9371 field is zero.
9372
9373 This function recognizes whether TYPE is of one of these forms,
9374 and, if so, smashes it to be a variant type. */
9375
9376 static void
9377 quirk_rust_enum (struct type *type, struct objfile *objfile)
9378 {
9379 gdb_assert (type->code () == TYPE_CODE_UNION);
9380
9381 /* We don't need to deal with empty enums. */
9382 if (type->num_fields () == 0)
9383 return;
9384
9385 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9386 if (type->num_fields () == 1
9387 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9388 {
9389 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9390
9391 /* Decode the field name to find the offset of the
9392 discriminant. */
9393 ULONGEST bit_offset = 0;
9394 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9395 while (name[0] >= '0' && name[0] <= '9')
9396 {
9397 char *tail;
9398 unsigned long index = strtoul (name, &tail, 10);
9399 name = tail;
9400 if (*name != '$'
9401 || index >= field_type->num_fields ()
9402 || (TYPE_FIELD_LOC_KIND (field_type, index)
9403 != FIELD_LOC_KIND_BITPOS))
9404 {
9405 complaint (_("Could not parse Rust enum encoding string \"%s\""
9406 "[in module %s]"),
9407 TYPE_FIELD_NAME (type, 0),
9408 objfile_name (objfile));
9409 return;
9410 }
9411 ++name;
9412
9413 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9414 field_type = TYPE_FIELD_TYPE (field_type, index);
9415 }
9416
9417 /* Smash this type to be a structure type. We have to do this
9418 because the type has already been recorded. */
9419 type->set_code (TYPE_CODE_STRUCT);
9420 type->set_num_fields (3);
9421 /* Save the field we care about. */
9422 struct field saved_field = type->field (0);
9423 type->set_fields
9424 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9425
9426 /* Put the discriminant at index 0. */
9427 TYPE_FIELD_TYPE (type, 0) = field_type;
9428 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9429 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9430 SET_FIELD_BITPOS (type->field (0), bit_offset);
9431
9432 /* The order of fields doesn't really matter, so put the real
9433 field at index 1 and the data-less field at index 2. */
9434 type->field (1) = saved_field;
9435 TYPE_FIELD_NAME (type, 1)
9436 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9437 TYPE_FIELD_TYPE (type, 1)->set_name
9438 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9439 TYPE_FIELD_NAME (type, 1)));
9440
9441 const char *dataless_name
9442 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9443 name);
9444 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9445 dataless_name);
9446 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9447 /* NAME points into the original discriminant name, which
9448 already has the correct lifetime. */
9449 TYPE_FIELD_NAME (type, 2) = name;
9450 SET_FIELD_BITPOS (type->field (2), 0);
9451
9452 /* Indicate that this is a variant type. */
9453 static discriminant_range ranges[1] = { { 0, 0 } };
9454 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9455 }
9456 /* A union with a single anonymous field is probably an old-style
9457 univariant enum. */
9458 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9459 {
9460 /* Smash this type to be a structure type. We have to do this
9461 because the type has already been recorded. */
9462 type->set_code (TYPE_CODE_STRUCT);
9463
9464 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9465 const char *variant_name
9466 = rust_last_path_segment (field_type->name ());
9467 TYPE_FIELD_NAME (type, 0) = variant_name;
9468 field_type->set_name
9469 (rust_fully_qualify (&objfile->objfile_obstack,
9470 type->name (), variant_name));
9471 }
9472 else
9473 {
9474 struct type *disr_type = nullptr;
9475 for (int i = 0; i < type->num_fields (); ++i)
9476 {
9477 disr_type = TYPE_FIELD_TYPE (type, i);
9478
9479 if (disr_type->code () != TYPE_CODE_STRUCT)
9480 {
9481 /* All fields of a true enum will be structs. */
9482 return;
9483 }
9484 else if (disr_type->num_fields () == 0)
9485 {
9486 /* Could be data-less variant, so keep going. */
9487 disr_type = nullptr;
9488 }
9489 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9490 "RUST$ENUM$DISR") != 0)
9491 {
9492 /* Not a Rust enum. */
9493 return;
9494 }
9495 else
9496 {
9497 /* Found one. */
9498 break;
9499 }
9500 }
9501
9502 /* If we got here without a discriminant, then it's probably
9503 just a union. */
9504 if (disr_type == nullptr)
9505 return;
9506
9507 /* Smash this type to be a structure type. We have to do this
9508 because the type has already been recorded. */
9509 type->set_code (TYPE_CODE_STRUCT);
9510
9511 /* Make space for the discriminant field. */
9512 struct field *disr_field = &disr_type->field (0);
9513 field *new_fields
9514 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9515 * sizeof (struct field)));
9516 memcpy (new_fields + 1, type->fields (),
9517 type->num_fields () * sizeof (struct field));
9518 type->set_fields (new_fields);
9519 type->set_num_fields (type->num_fields () + 1);
9520
9521 /* Install the discriminant at index 0 in the union. */
9522 type->field (0) = *disr_field;
9523 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9524 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9525
9526 /* We need a way to find the correct discriminant given a
9527 variant name. For convenience we build a map here. */
9528 struct type *enum_type = FIELD_TYPE (*disr_field);
9529 std::unordered_map<std::string, ULONGEST> discriminant_map;
9530 for (int i = 0; i < enum_type->num_fields (); ++i)
9531 {
9532 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9533 {
9534 const char *name
9535 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9536 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9537 }
9538 }
9539
9540 int n_fields = type->num_fields ();
9541 /* We don't need a range entry for the discriminant, but we do
9542 need one for every other field, as there is no default
9543 variant. */
9544 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9545 discriminant_range,
9546 n_fields - 1);
9547 /* Skip the discriminant here. */
9548 for (int i = 1; i < n_fields; ++i)
9549 {
9550 /* Find the final word in the name of this variant's type.
9551 That name can be used to look up the correct
9552 discriminant. */
9553 const char *variant_name
9554 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9555
9556 auto iter = discriminant_map.find (variant_name);
9557 if (iter != discriminant_map.end ())
9558 {
9559 ranges[i].low = iter->second;
9560 ranges[i].high = iter->second;
9561 }
9562
9563 /* Remove the discriminant field, if it exists. */
9564 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9565 if (sub_type->num_fields () > 0)
9566 {
9567 sub_type->set_num_fields (sub_type->num_fields () - 1);
9568 sub_type->set_fields (sub_type->fields () + 1);
9569 }
9570 TYPE_FIELD_NAME (type, i) = variant_name;
9571 sub_type->set_name
9572 (rust_fully_qualify (&objfile->objfile_obstack,
9573 type->name (), variant_name));
9574 }
9575
9576 /* Indicate that this is a variant type. */
9577 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9578 gdb::array_view<discriminant_range> (ranges,
9579 n_fields - 1));
9580 }
9581 }
9582
9583 /* Rewrite some Rust unions to be structures with variants parts. */
9584
9585 static void
9586 rust_union_quirks (struct dwarf2_cu *cu)
9587 {
9588 gdb_assert (cu->language == language_rust);
9589 for (type *type_ : cu->rust_unions)
9590 quirk_rust_enum (type_, cu->per_objfile->objfile);
9591 /* We don't need this any more. */
9592 cu->rust_unions.clear ();
9593 }
9594
9595 /* A helper function for computing the list of all symbol tables
9596 included by PER_CU. */
9597
9598 static void
9599 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9600 htab_t all_children, htab_t all_type_symtabs,
9601 struct dwarf2_per_cu_data *per_cu,
9602 struct compunit_symtab *immediate_parent)
9603 {
9604 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9605 if (*slot != NULL)
9606 {
9607 /* This inclusion and its children have been processed. */
9608 return;
9609 }
9610
9611 *slot = per_cu;
9612
9613 /* Only add a CU if it has a symbol table. */
9614 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
9615 if (cust != NULL)
9616 {
9617 /* If this is a type unit only add its symbol table if we haven't
9618 seen it yet (type unit per_cu's can share symtabs). */
9619 if (per_cu->is_debug_types)
9620 {
9621 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9622 if (*slot == NULL)
9623 {
9624 *slot = cust;
9625 result->push_back (cust);
9626 if (cust->user == NULL)
9627 cust->user = immediate_parent;
9628 }
9629 }
9630 else
9631 {
9632 result->push_back (cust);
9633 if (cust->user == NULL)
9634 cust->user = immediate_parent;
9635 }
9636 }
9637
9638 if (!per_cu->imported_symtabs_empty ())
9639 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9640 {
9641 recursively_compute_inclusions (result, all_children,
9642 all_type_symtabs, ptr, cust);
9643 }
9644 }
9645
9646 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9647 PER_CU. */
9648
9649 static void
9650 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9651 {
9652 gdb_assert (! per_cu->is_debug_types);
9653
9654 if (!per_cu->imported_symtabs_empty ())
9655 {
9656 int len;
9657 std::vector<compunit_symtab *> result_symtabs;
9658 htab_t all_children, all_type_symtabs;
9659 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
9660
9661 /* If we don't have a symtab, we can just skip this case. */
9662 if (cust == NULL)
9663 return;
9664
9665 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9666 NULL, xcalloc, xfree);
9667 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9668 NULL, xcalloc, xfree);
9669
9670 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9671 {
9672 recursively_compute_inclusions (&result_symtabs, all_children,
9673 all_type_symtabs, ptr, cust);
9674 }
9675
9676 /* Now we have a transitive closure of all the included symtabs. */
9677 len = result_symtabs.size ();
9678 cust->includes
9679 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9680 struct compunit_symtab *, len + 1);
9681 memcpy (cust->includes, result_symtabs.data (),
9682 len * sizeof (compunit_symtab *));
9683 cust->includes[len] = NULL;
9684
9685 htab_delete (all_children);
9686 htab_delete (all_type_symtabs);
9687 }
9688 }
9689
9690 /* Compute the 'includes' field for the symtabs of all the CUs we just
9691 read. */
9692
9693 static void
9694 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9695 {
9696 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9697 {
9698 if (! iter->is_debug_types)
9699 compute_compunit_symtab_includes (iter);
9700 }
9701
9702 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9703 }
9704
9705 /* Generate full symbol information for PER_CU, whose DIEs have
9706 already been loaded into memory. */
9707
9708 static void
9709 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9710 enum language pretend_language)
9711 {
9712 struct dwarf2_cu *cu = per_cu->cu;
9713 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9714 struct objfile *objfile = dwarf2_per_objfile->objfile;
9715 struct gdbarch *gdbarch = objfile->arch ();
9716 CORE_ADDR lowpc, highpc;
9717 struct compunit_symtab *cust;
9718 CORE_ADDR baseaddr;
9719 struct block *static_block;
9720 CORE_ADDR addr;
9721
9722 baseaddr = objfile->text_section_offset ();
9723
9724 /* Clear the list here in case something was left over. */
9725 cu->method_list.clear ();
9726
9727 cu->language = pretend_language;
9728 cu->language_defn = language_def (cu->language);
9729
9730 /* Do line number decoding in read_file_scope () */
9731 process_die (cu->dies, cu);
9732
9733 /* For now fudge the Go package. */
9734 if (cu->language == language_go)
9735 fixup_go_packaging (cu);
9736
9737 /* Now that we have processed all the DIEs in the CU, all the types
9738 should be complete, and it should now be safe to compute all of the
9739 physnames. */
9740 compute_delayed_physnames (cu);
9741
9742 if (cu->language == language_rust)
9743 rust_union_quirks (cu);
9744
9745 /* Some compilers don't define a DW_AT_high_pc attribute for the
9746 compilation unit. If the DW_AT_high_pc is missing, synthesize
9747 it, by scanning the DIE's below the compilation unit. */
9748 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9749
9750 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9751 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9752
9753 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9754 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9755 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9756 addrmap to help ensure it has an accurate map of pc values belonging to
9757 this comp unit. */
9758 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9759
9760 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9761 SECT_OFF_TEXT (objfile),
9762 0);
9763
9764 if (cust != NULL)
9765 {
9766 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9767
9768 /* Set symtab language to language from DW_AT_language. If the
9769 compilation is from a C file generated by language preprocessors, do
9770 not set the language if it was already deduced by start_subfile. */
9771 if (!(cu->language == language_c
9772 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9773 COMPUNIT_FILETABS (cust)->language = cu->language;
9774
9775 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9776 produce DW_AT_location with location lists but it can be possibly
9777 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9778 there were bugs in prologue debug info, fixed later in GCC-4.5
9779 by "unwind info for epilogues" patch (which is not directly related).
9780
9781 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9782 needed, it would be wrong due to missing DW_AT_producer there.
9783
9784 Still one can confuse GDB by using non-standard GCC compilation
9785 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9786 */
9787 if (cu->has_loclist && gcc_4_minor >= 5)
9788 cust->locations_valid = 1;
9789
9790 if (gcc_4_minor >= 5)
9791 cust->epilogue_unwind_valid = 1;
9792
9793 cust->call_site_htab = cu->call_site_htab;
9794 }
9795
9796 dwarf2_per_objfile->set_symtab (per_cu, cust);
9797
9798 /* Push it for inclusion processing later. */
9799 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9800
9801 /* Not needed any more. */
9802 cu->reset_builder ();
9803 }
9804
9805 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9806 already been loaded into memory. */
9807
9808 static void
9809 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9810 enum language pretend_language)
9811 {
9812 struct dwarf2_cu *cu = per_cu->cu;
9813 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9814 struct objfile *objfile = dwarf2_per_objfile->objfile;
9815 struct compunit_symtab *cust;
9816 struct signatured_type *sig_type;
9817
9818 gdb_assert (per_cu->is_debug_types);
9819 sig_type = (struct signatured_type *) per_cu;
9820
9821 /* Clear the list here in case something was left over. */
9822 cu->method_list.clear ();
9823
9824 cu->language = pretend_language;
9825 cu->language_defn = language_def (cu->language);
9826
9827 /* The symbol tables are set up in read_type_unit_scope. */
9828 process_die (cu->dies, cu);
9829
9830 /* For now fudge the Go package. */
9831 if (cu->language == language_go)
9832 fixup_go_packaging (cu);
9833
9834 /* Now that we have processed all the DIEs in the CU, all the types
9835 should be complete, and it should now be safe to compute all of the
9836 physnames. */
9837 compute_delayed_physnames (cu);
9838
9839 if (cu->language == language_rust)
9840 rust_union_quirks (cu);
9841
9842 /* TUs share symbol tables.
9843 If this is the first TU to use this symtab, complete the construction
9844 of it with end_expandable_symtab. Otherwise, complete the addition of
9845 this TU's symbols to the existing symtab. */
9846 if (sig_type->type_unit_group->compunit_symtab == NULL)
9847 {
9848 buildsym_compunit *builder = cu->get_builder ();
9849 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9850 sig_type->type_unit_group->compunit_symtab = cust;
9851
9852 if (cust != NULL)
9853 {
9854 /* Set symtab language to language from DW_AT_language. If the
9855 compilation is from a C file generated by language preprocessors,
9856 do not set the language if it was already deduced by
9857 start_subfile. */
9858 if (!(cu->language == language_c
9859 && COMPUNIT_FILETABS (cust)->language != language_c))
9860 COMPUNIT_FILETABS (cust)->language = cu->language;
9861 }
9862 }
9863 else
9864 {
9865 cu->get_builder ()->augment_type_symtab ();
9866 cust = sig_type->type_unit_group->compunit_symtab;
9867 }
9868
9869 dwarf2_per_objfile->set_symtab (per_cu, cust);
9870
9871 /* Not needed any more. */
9872 cu->reset_builder ();
9873 }
9874
9875 /* Process an imported unit DIE. */
9876
9877 static void
9878 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9879 {
9880 struct attribute *attr;
9881
9882 /* For now we don't handle imported units in type units. */
9883 if (cu->per_cu->is_debug_types)
9884 {
9885 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9886 " supported in type units [in module %s]"),
9887 objfile_name (cu->per_objfile->objfile));
9888 }
9889
9890 attr = dwarf2_attr (die, DW_AT_import, cu);
9891 if (attr != NULL)
9892 {
9893 sect_offset sect_off = attr->get_ref_die_offset ();
9894 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9895 dwarf2_per_cu_data *per_cu
9896 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->per_objfile);
9897
9898 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9899 into another compilation unit, at root level. Regard this as a hint,
9900 and ignore it. */
9901 if (die->parent && die->parent->parent == NULL
9902 && per_cu->unit_type == DW_UT_compile
9903 && per_cu->lang == language_cplus)
9904 return;
9905
9906 /* If necessary, add it to the queue and load its DIEs. */
9907 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9908 load_full_comp_unit (per_cu, false, cu->language);
9909
9910 cu->per_cu->imported_symtabs_push (per_cu);
9911 }
9912 }
9913
9914 /* RAII object that represents a process_die scope: i.e.,
9915 starts/finishes processing a DIE. */
9916 class process_die_scope
9917 {
9918 public:
9919 process_die_scope (die_info *die, dwarf2_cu *cu)
9920 : m_die (die), m_cu (cu)
9921 {
9922 /* We should only be processing DIEs not already in process. */
9923 gdb_assert (!m_die->in_process);
9924 m_die->in_process = true;
9925 }
9926
9927 ~process_die_scope ()
9928 {
9929 m_die->in_process = false;
9930
9931 /* If we're done processing the DIE for the CU that owns the line
9932 header, we don't need the line header anymore. */
9933 if (m_cu->line_header_die_owner == m_die)
9934 {
9935 delete m_cu->line_header;
9936 m_cu->line_header = NULL;
9937 m_cu->line_header_die_owner = NULL;
9938 }
9939 }
9940
9941 private:
9942 die_info *m_die;
9943 dwarf2_cu *m_cu;
9944 };
9945
9946 /* Process a die and its children. */
9947
9948 static void
9949 process_die (struct die_info *die, struct dwarf2_cu *cu)
9950 {
9951 process_die_scope scope (die, cu);
9952
9953 switch (die->tag)
9954 {
9955 case DW_TAG_padding:
9956 break;
9957 case DW_TAG_compile_unit:
9958 case DW_TAG_partial_unit:
9959 read_file_scope (die, cu);
9960 break;
9961 case DW_TAG_type_unit:
9962 read_type_unit_scope (die, cu);
9963 break;
9964 case DW_TAG_subprogram:
9965 /* Nested subprograms in Fortran get a prefix. */
9966 if (cu->language == language_fortran
9967 && die->parent != NULL
9968 && die->parent->tag == DW_TAG_subprogram)
9969 cu->processing_has_namespace_info = true;
9970 /* Fall through. */
9971 case DW_TAG_inlined_subroutine:
9972 read_func_scope (die, cu);
9973 break;
9974 case DW_TAG_lexical_block:
9975 case DW_TAG_try_block:
9976 case DW_TAG_catch_block:
9977 read_lexical_block_scope (die, cu);
9978 break;
9979 case DW_TAG_call_site:
9980 case DW_TAG_GNU_call_site:
9981 read_call_site_scope (die, cu);
9982 break;
9983 case DW_TAG_class_type:
9984 case DW_TAG_interface_type:
9985 case DW_TAG_structure_type:
9986 case DW_TAG_union_type:
9987 process_structure_scope (die, cu);
9988 break;
9989 case DW_TAG_enumeration_type:
9990 process_enumeration_scope (die, cu);
9991 break;
9992
9993 /* These dies have a type, but processing them does not create
9994 a symbol or recurse to process the children. Therefore we can
9995 read them on-demand through read_type_die. */
9996 case DW_TAG_subroutine_type:
9997 case DW_TAG_set_type:
9998 case DW_TAG_array_type:
9999 case DW_TAG_pointer_type:
10000 case DW_TAG_ptr_to_member_type:
10001 case DW_TAG_reference_type:
10002 case DW_TAG_rvalue_reference_type:
10003 case DW_TAG_string_type:
10004 break;
10005
10006 case DW_TAG_base_type:
10007 case DW_TAG_subrange_type:
10008 case DW_TAG_typedef:
10009 /* Add a typedef symbol for the type definition, if it has a
10010 DW_AT_name. */
10011 new_symbol (die, read_type_die (die, cu), cu);
10012 break;
10013 case DW_TAG_common_block:
10014 read_common_block (die, cu);
10015 break;
10016 case DW_TAG_common_inclusion:
10017 break;
10018 case DW_TAG_namespace:
10019 cu->processing_has_namespace_info = true;
10020 read_namespace (die, cu);
10021 break;
10022 case DW_TAG_module:
10023 cu->processing_has_namespace_info = true;
10024 read_module (die, cu);
10025 break;
10026 case DW_TAG_imported_declaration:
10027 cu->processing_has_namespace_info = true;
10028 if (read_namespace_alias (die, cu))
10029 break;
10030 /* The declaration is not a global namespace alias. */
10031 /* Fall through. */
10032 case DW_TAG_imported_module:
10033 cu->processing_has_namespace_info = true;
10034 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10035 || cu->language != language_fortran))
10036 complaint (_("Tag '%s' has unexpected children"),
10037 dwarf_tag_name (die->tag));
10038 read_import_statement (die, cu);
10039 break;
10040
10041 case DW_TAG_imported_unit:
10042 process_imported_unit_die (die, cu);
10043 break;
10044
10045 case DW_TAG_variable:
10046 read_variable (die, cu);
10047 break;
10048
10049 default:
10050 new_symbol (die, NULL, cu);
10051 break;
10052 }
10053 }
10054 \f
10055 /* DWARF name computation. */
10056
10057 /* A helper function for dwarf2_compute_name which determines whether DIE
10058 needs to have the name of the scope prepended to the name listed in the
10059 die. */
10060
10061 static int
10062 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10063 {
10064 struct attribute *attr;
10065
10066 switch (die->tag)
10067 {
10068 case DW_TAG_namespace:
10069 case DW_TAG_typedef:
10070 case DW_TAG_class_type:
10071 case DW_TAG_interface_type:
10072 case DW_TAG_structure_type:
10073 case DW_TAG_union_type:
10074 case DW_TAG_enumeration_type:
10075 case DW_TAG_enumerator:
10076 case DW_TAG_subprogram:
10077 case DW_TAG_inlined_subroutine:
10078 case DW_TAG_member:
10079 case DW_TAG_imported_declaration:
10080 return 1;
10081
10082 case DW_TAG_variable:
10083 case DW_TAG_constant:
10084 /* We only need to prefix "globally" visible variables. These include
10085 any variable marked with DW_AT_external or any variable that
10086 lives in a namespace. [Variables in anonymous namespaces
10087 require prefixing, but they are not DW_AT_external.] */
10088
10089 if (dwarf2_attr (die, DW_AT_specification, cu))
10090 {
10091 struct dwarf2_cu *spec_cu = cu;
10092
10093 return die_needs_namespace (die_specification (die, &spec_cu),
10094 spec_cu);
10095 }
10096
10097 attr = dwarf2_attr (die, DW_AT_external, cu);
10098 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10099 && die->parent->tag != DW_TAG_module)
10100 return 0;
10101 /* A variable in a lexical block of some kind does not need a
10102 namespace, even though in C++ such variables may be external
10103 and have a mangled name. */
10104 if (die->parent->tag == DW_TAG_lexical_block
10105 || die->parent->tag == DW_TAG_try_block
10106 || die->parent->tag == DW_TAG_catch_block
10107 || die->parent->tag == DW_TAG_subprogram)
10108 return 0;
10109 return 1;
10110
10111 default:
10112 return 0;
10113 }
10114 }
10115
10116 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10117 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10118 defined for the given DIE. */
10119
10120 static struct attribute *
10121 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10122 {
10123 struct attribute *attr;
10124
10125 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10126 if (attr == NULL)
10127 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10128
10129 return attr;
10130 }
10131
10132 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10133 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10134 defined for the given DIE. */
10135
10136 static const char *
10137 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10138 {
10139 const char *linkage_name;
10140
10141 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10142 if (linkage_name == NULL)
10143 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10144
10145 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10146 See https://github.com/rust-lang/rust/issues/32925. */
10147 if (cu->language == language_rust && linkage_name != NULL
10148 && strchr (linkage_name, '{') != NULL)
10149 linkage_name = NULL;
10150
10151 return linkage_name;
10152 }
10153
10154 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10155 compute the physname for the object, which include a method's:
10156 - formal parameters (C++),
10157 - receiver type (Go),
10158
10159 The term "physname" is a bit confusing.
10160 For C++, for example, it is the demangled name.
10161 For Go, for example, it's the mangled name.
10162
10163 For Ada, return the DIE's linkage name rather than the fully qualified
10164 name. PHYSNAME is ignored..
10165
10166 The result is allocated on the objfile->per_bfd's obstack and
10167 canonicalized. */
10168
10169 static const char *
10170 dwarf2_compute_name (const char *name,
10171 struct die_info *die, struct dwarf2_cu *cu,
10172 int physname)
10173 {
10174 struct objfile *objfile = cu->per_objfile->objfile;
10175
10176 if (name == NULL)
10177 name = dwarf2_name (die, cu);
10178
10179 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10180 but otherwise compute it by typename_concat inside GDB.
10181 FIXME: Actually this is not really true, or at least not always true.
10182 It's all very confusing. compute_and_set_names doesn't try to demangle
10183 Fortran names because there is no mangling standard. So new_symbol
10184 will set the demangled name to the result of dwarf2_full_name, and it is
10185 the demangled name that GDB uses if it exists. */
10186 if (cu->language == language_ada
10187 || (cu->language == language_fortran && physname))
10188 {
10189 /* For Ada unit, we prefer the linkage name over the name, as
10190 the former contains the exported name, which the user expects
10191 to be able to reference. Ideally, we want the user to be able
10192 to reference this entity using either natural or linkage name,
10193 but we haven't started looking at this enhancement yet. */
10194 const char *linkage_name = dw2_linkage_name (die, cu);
10195
10196 if (linkage_name != NULL)
10197 return linkage_name;
10198 }
10199
10200 /* These are the only languages we know how to qualify names in. */
10201 if (name != NULL
10202 && (cu->language == language_cplus
10203 || cu->language == language_fortran || cu->language == language_d
10204 || cu->language == language_rust))
10205 {
10206 if (die_needs_namespace (die, cu))
10207 {
10208 const char *prefix;
10209 const char *canonical_name = NULL;
10210
10211 string_file buf;
10212
10213 prefix = determine_prefix (die, cu);
10214 if (*prefix != '\0')
10215 {
10216 gdb::unique_xmalloc_ptr<char> prefixed_name
10217 (typename_concat (NULL, prefix, name, physname, cu));
10218
10219 buf.puts (prefixed_name.get ());
10220 }
10221 else
10222 buf.puts (name);
10223
10224 /* Template parameters may be specified in the DIE's DW_AT_name, or
10225 as children with DW_TAG_template_type_param or
10226 DW_TAG_value_type_param. If the latter, add them to the name
10227 here. If the name already has template parameters, then
10228 skip this step; some versions of GCC emit both, and
10229 it is more efficient to use the pre-computed name.
10230
10231 Something to keep in mind about this process: it is very
10232 unlikely, or in some cases downright impossible, to produce
10233 something that will match the mangled name of a function.
10234 If the definition of the function has the same debug info,
10235 we should be able to match up with it anyway. But fallbacks
10236 using the minimal symbol, for instance to find a method
10237 implemented in a stripped copy of libstdc++, will not work.
10238 If we do not have debug info for the definition, we will have to
10239 match them up some other way.
10240
10241 When we do name matching there is a related problem with function
10242 templates; two instantiated function templates are allowed to
10243 differ only by their return types, which we do not add here. */
10244
10245 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10246 {
10247 struct attribute *attr;
10248 struct die_info *child;
10249 int first = 1;
10250
10251 die->building_fullname = 1;
10252
10253 for (child = die->child; child != NULL; child = child->sibling)
10254 {
10255 struct type *type;
10256 LONGEST value;
10257 const gdb_byte *bytes;
10258 struct dwarf2_locexpr_baton *baton;
10259 struct value *v;
10260
10261 if (child->tag != DW_TAG_template_type_param
10262 && child->tag != DW_TAG_template_value_param)
10263 continue;
10264
10265 if (first)
10266 {
10267 buf.puts ("<");
10268 first = 0;
10269 }
10270 else
10271 buf.puts (", ");
10272
10273 attr = dwarf2_attr (child, DW_AT_type, cu);
10274 if (attr == NULL)
10275 {
10276 complaint (_("template parameter missing DW_AT_type"));
10277 buf.puts ("UNKNOWN_TYPE");
10278 continue;
10279 }
10280 type = die_type (child, cu);
10281
10282 if (child->tag == DW_TAG_template_type_param)
10283 {
10284 c_print_type (type, "", &buf, -1, 0, cu->language,
10285 &type_print_raw_options);
10286 continue;
10287 }
10288
10289 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10290 if (attr == NULL)
10291 {
10292 complaint (_("template parameter missing "
10293 "DW_AT_const_value"));
10294 buf.puts ("UNKNOWN_VALUE");
10295 continue;
10296 }
10297
10298 dwarf2_const_value_attr (attr, type, name,
10299 &cu->comp_unit_obstack, cu,
10300 &value, &bytes, &baton);
10301
10302 if (TYPE_NOSIGN (type))
10303 /* GDB prints characters as NUMBER 'CHAR'. If that's
10304 changed, this can use value_print instead. */
10305 c_printchar (value, type, &buf);
10306 else
10307 {
10308 struct value_print_options opts;
10309
10310 if (baton != NULL)
10311 v = dwarf2_evaluate_loc_desc (type, NULL,
10312 baton->data,
10313 baton->size,
10314 baton->per_cu);
10315 else if (bytes != NULL)
10316 {
10317 v = allocate_value (type);
10318 memcpy (value_contents_writeable (v), bytes,
10319 TYPE_LENGTH (type));
10320 }
10321 else
10322 v = value_from_longest (type, value);
10323
10324 /* Specify decimal so that we do not depend on
10325 the radix. */
10326 get_formatted_print_options (&opts, 'd');
10327 opts.raw = 1;
10328 value_print (v, &buf, &opts);
10329 release_value (v);
10330 }
10331 }
10332
10333 die->building_fullname = 0;
10334
10335 if (!first)
10336 {
10337 /* Close the argument list, with a space if necessary
10338 (nested templates). */
10339 if (!buf.empty () && buf.string ().back () == '>')
10340 buf.puts (" >");
10341 else
10342 buf.puts (">");
10343 }
10344 }
10345
10346 /* For C++ methods, append formal parameter type
10347 information, if PHYSNAME. */
10348
10349 if (physname && die->tag == DW_TAG_subprogram
10350 && cu->language == language_cplus)
10351 {
10352 struct type *type = read_type_die (die, cu);
10353
10354 c_type_print_args (type, &buf, 1, cu->language,
10355 &type_print_raw_options);
10356
10357 if (cu->language == language_cplus)
10358 {
10359 /* Assume that an artificial first parameter is
10360 "this", but do not crash if it is not. RealView
10361 marks unnamed (and thus unused) parameters as
10362 artificial; there is no way to differentiate
10363 the two cases. */
10364 if (type->num_fields () > 0
10365 && TYPE_FIELD_ARTIFICIAL (type, 0)
10366 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10367 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10368 0))))
10369 buf.puts (" const");
10370 }
10371 }
10372
10373 const std::string &intermediate_name = buf.string ();
10374
10375 if (cu->language == language_cplus)
10376 canonical_name
10377 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10378 objfile);
10379
10380 /* If we only computed INTERMEDIATE_NAME, or if
10381 INTERMEDIATE_NAME is already canonical, then we need to
10382 intern it. */
10383 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10384 name = objfile->intern (intermediate_name);
10385 else
10386 name = canonical_name;
10387 }
10388 }
10389
10390 return name;
10391 }
10392
10393 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10394 If scope qualifiers are appropriate they will be added. The result
10395 will be allocated on the storage_obstack, or NULL if the DIE does
10396 not have a name. NAME may either be from a previous call to
10397 dwarf2_name or NULL.
10398
10399 The output string will be canonicalized (if C++). */
10400
10401 static const char *
10402 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10403 {
10404 return dwarf2_compute_name (name, die, cu, 0);
10405 }
10406
10407 /* Construct a physname for the given DIE in CU. NAME may either be
10408 from a previous call to dwarf2_name or NULL. The result will be
10409 allocated on the objfile_objstack or NULL if the DIE does not have a
10410 name.
10411
10412 The output string will be canonicalized (if C++). */
10413
10414 static const char *
10415 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10416 {
10417 struct objfile *objfile = cu->per_objfile->objfile;
10418 const char *retval, *mangled = NULL, *canon = NULL;
10419 int need_copy = 1;
10420
10421 /* In this case dwarf2_compute_name is just a shortcut not building anything
10422 on its own. */
10423 if (!die_needs_namespace (die, cu))
10424 return dwarf2_compute_name (name, die, cu, 1);
10425
10426 if (cu->language != language_rust)
10427 mangled = dw2_linkage_name (die, cu);
10428
10429 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10430 has computed. */
10431 gdb::unique_xmalloc_ptr<char> demangled;
10432 if (mangled != NULL)
10433 {
10434
10435 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10436 {
10437 /* Do nothing (do not demangle the symbol name). */
10438 }
10439 else if (cu->language == language_go)
10440 {
10441 /* This is a lie, but we already lie to the caller new_symbol.
10442 new_symbol assumes we return the mangled name.
10443 This just undoes that lie until things are cleaned up. */
10444 }
10445 else
10446 {
10447 /* Use DMGL_RET_DROP for C++ template functions to suppress
10448 their return type. It is easier for GDB users to search
10449 for such functions as `name(params)' than `long name(params)'.
10450 In such case the minimal symbol names do not match the full
10451 symbol names but for template functions there is never a need
10452 to look up their definition from their declaration so
10453 the only disadvantage remains the minimal symbol variant
10454 `long name(params)' does not have the proper inferior type. */
10455 demangled.reset (gdb_demangle (mangled,
10456 (DMGL_PARAMS | DMGL_ANSI
10457 | DMGL_RET_DROP)));
10458 }
10459 if (demangled)
10460 canon = demangled.get ();
10461 else
10462 {
10463 canon = mangled;
10464 need_copy = 0;
10465 }
10466 }
10467
10468 if (canon == NULL || check_physname)
10469 {
10470 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10471
10472 if (canon != NULL && strcmp (physname, canon) != 0)
10473 {
10474 /* It may not mean a bug in GDB. The compiler could also
10475 compute DW_AT_linkage_name incorrectly. But in such case
10476 GDB would need to be bug-to-bug compatible. */
10477
10478 complaint (_("Computed physname <%s> does not match demangled <%s> "
10479 "(from linkage <%s>) - DIE at %s [in module %s]"),
10480 physname, canon, mangled, sect_offset_str (die->sect_off),
10481 objfile_name (objfile));
10482
10483 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10484 is available here - over computed PHYSNAME. It is safer
10485 against both buggy GDB and buggy compilers. */
10486
10487 retval = canon;
10488 }
10489 else
10490 {
10491 retval = physname;
10492 need_copy = 0;
10493 }
10494 }
10495 else
10496 retval = canon;
10497
10498 if (need_copy)
10499 retval = objfile->intern (retval);
10500
10501 return retval;
10502 }
10503
10504 /* Inspect DIE in CU for a namespace alias. If one exists, record
10505 a new symbol for it.
10506
10507 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10508
10509 static int
10510 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10511 {
10512 struct attribute *attr;
10513
10514 /* If the die does not have a name, this is not a namespace
10515 alias. */
10516 attr = dwarf2_attr (die, DW_AT_name, cu);
10517 if (attr != NULL)
10518 {
10519 int num;
10520 struct die_info *d = die;
10521 struct dwarf2_cu *imported_cu = cu;
10522
10523 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10524 keep inspecting DIEs until we hit the underlying import. */
10525 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10526 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10527 {
10528 attr = dwarf2_attr (d, DW_AT_import, cu);
10529 if (attr == NULL)
10530 break;
10531
10532 d = follow_die_ref (d, attr, &imported_cu);
10533 if (d->tag != DW_TAG_imported_declaration)
10534 break;
10535 }
10536
10537 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10538 {
10539 complaint (_("DIE at %s has too many recursively imported "
10540 "declarations"), sect_offset_str (d->sect_off));
10541 return 0;
10542 }
10543
10544 if (attr != NULL)
10545 {
10546 struct type *type;
10547 sect_offset sect_off = attr->get_ref_die_offset ();
10548
10549 type = get_die_type_at_offset (sect_off, cu->per_cu);
10550 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10551 {
10552 /* This declaration is a global namespace alias. Add
10553 a symbol for it whose type is the aliased namespace. */
10554 new_symbol (die, type, cu);
10555 return 1;
10556 }
10557 }
10558 }
10559
10560 return 0;
10561 }
10562
10563 /* Return the using directives repository (global or local?) to use in the
10564 current context for CU.
10565
10566 For Ada, imported declarations can materialize renamings, which *may* be
10567 global. However it is impossible (for now?) in DWARF to distinguish
10568 "external" imported declarations and "static" ones. As all imported
10569 declarations seem to be static in all other languages, make them all CU-wide
10570 global only in Ada. */
10571
10572 static struct using_direct **
10573 using_directives (struct dwarf2_cu *cu)
10574 {
10575 if (cu->language == language_ada
10576 && cu->get_builder ()->outermost_context_p ())
10577 return cu->get_builder ()->get_global_using_directives ();
10578 else
10579 return cu->get_builder ()->get_local_using_directives ();
10580 }
10581
10582 /* Read the import statement specified by the given die and record it. */
10583
10584 static void
10585 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10586 {
10587 struct objfile *objfile = cu->per_objfile->objfile;
10588 struct attribute *import_attr;
10589 struct die_info *imported_die, *child_die;
10590 struct dwarf2_cu *imported_cu;
10591 const char *imported_name;
10592 const char *imported_name_prefix;
10593 const char *canonical_name;
10594 const char *import_alias;
10595 const char *imported_declaration = NULL;
10596 const char *import_prefix;
10597 std::vector<const char *> excludes;
10598
10599 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10600 if (import_attr == NULL)
10601 {
10602 complaint (_("Tag '%s' has no DW_AT_import"),
10603 dwarf_tag_name (die->tag));
10604 return;
10605 }
10606
10607 imported_cu = cu;
10608 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10609 imported_name = dwarf2_name (imported_die, imported_cu);
10610 if (imported_name == NULL)
10611 {
10612 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10613
10614 The import in the following code:
10615 namespace A
10616 {
10617 typedef int B;
10618 }
10619
10620 int main ()
10621 {
10622 using A::B;
10623 B b;
10624 return b;
10625 }
10626
10627 ...
10628 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10629 <52> DW_AT_decl_file : 1
10630 <53> DW_AT_decl_line : 6
10631 <54> DW_AT_import : <0x75>
10632 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10633 <59> DW_AT_name : B
10634 <5b> DW_AT_decl_file : 1
10635 <5c> DW_AT_decl_line : 2
10636 <5d> DW_AT_type : <0x6e>
10637 ...
10638 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10639 <76> DW_AT_byte_size : 4
10640 <77> DW_AT_encoding : 5 (signed)
10641
10642 imports the wrong die ( 0x75 instead of 0x58 ).
10643 This case will be ignored until the gcc bug is fixed. */
10644 return;
10645 }
10646
10647 /* Figure out the local name after import. */
10648 import_alias = dwarf2_name (die, cu);
10649
10650 /* Figure out where the statement is being imported to. */
10651 import_prefix = determine_prefix (die, cu);
10652
10653 /* Figure out what the scope of the imported die is and prepend it
10654 to the name of the imported die. */
10655 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10656
10657 if (imported_die->tag != DW_TAG_namespace
10658 && imported_die->tag != DW_TAG_module)
10659 {
10660 imported_declaration = imported_name;
10661 canonical_name = imported_name_prefix;
10662 }
10663 else if (strlen (imported_name_prefix) > 0)
10664 canonical_name = obconcat (&objfile->objfile_obstack,
10665 imported_name_prefix,
10666 (cu->language == language_d ? "." : "::"),
10667 imported_name, (char *) NULL);
10668 else
10669 canonical_name = imported_name;
10670
10671 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10672 for (child_die = die->child; child_die && child_die->tag;
10673 child_die = child_die->sibling)
10674 {
10675 /* DWARF-4: A Fortran use statement with a “rename list” may be
10676 represented by an imported module entry with an import attribute
10677 referring to the module and owned entries corresponding to those
10678 entities that are renamed as part of being imported. */
10679
10680 if (child_die->tag != DW_TAG_imported_declaration)
10681 {
10682 complaint (_("child DW_TAG_imported_declaration expected "
10683 "- DIE at %s [in module %s]"),
10684 sect_offset_str (child_die->sect_off),
10685 objfile_name (objfile));
10686 continue;
10687 }
10688
10689 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10690 if (import_attr == NULL)
10691 {
10692 complaint (_("Tag '%s' has no DW_AT_import"),
10693 dwarf_tag_name (child_die->tag));
10694 continue;
10695 }
10696
10697 imported_cu = cu;
10698 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10699 &imported_cu);
10700 imported_name = dwarf2_name (imported_die, imported_cu);
10701 if (imported_name == NULL)
10702 {
10703 complaint (_("child DW_TAG_imported_declaration has unknown "
10704 "imported name - DIE at %s [in module %s]"),
10705 sect_offset_str (child_die->sect_off),
10706 objfile_name (objfile));
10707 continue;
10708 }
10709
10710 excludes.push_back (imported_name);
10711
10712 process_die (child_die, cu);
10713 }
10714
10715 add_using_directive (using_directives (cu),
10716 import_prefix,
10717 canonical_name,
10718 import_alias,
10719 imported_declaration,
10720 excludes,
10721 0,
10722 &objfile->objfile_obstack);
10723 }
10724
10725 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10726 types, but gives them a size of zero. Starting with version 14,
10727 ICC is compatible with GCC. */
10728
10729 static bool
10730 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10731 {
10732 if (!cu->checked_producer)
10733 check_producer (cu);
10734
10735 return cu->producer_is_icc_lt_14;
10736 }
10737
10738 /* ICC generates a DW_AT_type for C void functions. This was observed on
10739 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10740 which says that void functions should not have a DW_AT_type. */
10741
10742 static bool
10743 producer_is_icc (struct dwarf2_cu *cu)
10744 {
10745 if (!cu->checked_producer)
10746 check_producer (cu);
10747
10748 return cu->producer_is_icc;
10749 }
10750
10751 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10752 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10753 this, it was first present in GCC release 4.3.0. */
10754
10755 static bool
10756 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10757 {
10758 if (!cu->checked_producer)
10759 check_producer (cu);
10760
10761 return cu->producer_is_gcc_lt_4_3;
10762 }
10763
10764 static file_and_directory
10765 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10766 {
10767 file_and_directory res;
10768
10769 /* Find the filename. Do not use dwarf2_name here, since the filename
10770 is not a source language identifier. */
10771 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10772 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10773
10774 if (res.comp_dir == NULL
10775 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10776 && IS_ABSOLUTE_PATH (res.name))
10777 {
10778 res.comp_dir_storage = ldirname (res.name);
10779 if (!res.comp_dir_storage.empty ())
10780 res.comp_dir = res.comp_dir_storage.c_str ();
10781 }
10782 if (res.comp_dir != NULL)
10783 {
10784 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10785 directory, get rid of it. */
10786 const char *cp = strchr (res.comp_dir, ':');
10787
10788 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10789 res.comp_dir = cp + 1;
10790 }
10791
10792 if (res.name == NULL)
10793 res.name = "<unknown>";
10794
10795 return res;
10796 }
10797
10798 /* Handle DW_AT_stmt_list for a compilation unit.
10799 DIE is the DW_TAG_compile_unit die for CU.
10800 COMP_DIR is the compilation directory. LOWPC is passed to
10801 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10802
10803 static void
10804 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10805 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10806 {
10807 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10808 struct attribute *attr;
10809 struct line_header line_header_local;
10810 hashval_t line_header_local_hash;
10811 void **slot;
10812 int decode_mapping;
10813
10814 gdb_assert (! cu->per_cu->is_debug_types);
10815
10816 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10817 if (attr == NULL)
10818 return;
10819
10820 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10821
10822 /* The line header hash table is only created if needed (it exists to
10823 prevent redundant reading of the line table for partial_units).
10824 If we're given a partial_unit, we'll need it. If we're given a
10825 compile_unit, then use the line header hash table if it's already
10826 created, but don't create one just yet. */
10827
10828 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10829 && die->tag == DW_TAG_partial_unit)
10830 {
10831 dwarf2_per_objfile->per_bfd->line_header_hash
10832 .reset (htab_create_alloc (127, line_header_hash_voidp,
10833 line_header_eq_voidp,
10834 free_line_header_voidp,
10835 xcalloc, xfree));
10836 }
10837
10838 line_header_local.sect_off = line_offset;
10839 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10840 line_header_local_hash = line_header_hash (&line_header_local);
10841 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10842 {
10843 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10844 &line_header_local,
10845 line_header_local_hash, NO_INSERT);
10846
10847 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10848 is not present in *SLOT (since if there is something in *SLOT then
10849 it will be for a partial_unit). */
10850 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10851 {
10852 gdb_assert (*slot != NULL);
10853 cu->line_header = (struct line_header *) *slot;
10854 return;
10855 }
10856 }
10857
10858 /* dwarf_decode_line_header does not yet provide sufficient information.
10859 We always have to call also dwarf_decode_lines for it. */
10860 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10861 if (lh == NULL)
10862 return;
10863
10864 cu->line_header = lh.release ();
10865 cu->line_header_die_owner = die;
10866
10867 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10868 slot = NULL;
10869 else
10870 {
10871 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10872 &line_header_local,
10873 line_header_local_hash, INSERT);
10874 gdb_assert (slot != NULL);
10875 }
10876 if (slot != NULL && *slot == NULL)
10877 {
10878 /* This newly decoded line number information unit will be owned
10879 by line_header_hash hash table. */
10880 *slot = cu->line_header;
10881 cu->line_header_die_owner = NULL;
10882 }
10883 else
10884 {
10885 /* We cannot free any current entry in (*slot) as that struct line_header
10886 may be already used by multiple CUs. Create only temporary decoded
10887 line_header for this CU - it may happen at most once for each line
10888 number information unit. And if we're not using line_header_hash
10889 then this is what we want as well. */
10890 gdb_assert (die->tag != DW_TAG_partial_unit);
10891 }
10892 decode_mapping = (die->tag != DW_TAG_partial_unit);
10893 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10894 decode_mapping);
10895
10896 }
10897
10898 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10899
10900 static void
10901 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10902 {
10903 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10904 struct objfile *objfile = dwarf2_per_objfile->objfile;
10905 struct gdbarch *gdbarch = objfile->arch ();
10906 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10907 CORE_ADDR highpc = ((CORE_ADDR) 0);
10908 struct attribute *attr;
10909 struct die_info *child_die;
10910 CORE_ADDR baseaddr;
10911
10912 prepare_one_comp_unit (cu, die, cu->language);
10913 baseaddr = objfile->text_section_offset ();
10914
10915 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10916
10917 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10918 from finish_block. */
10919 if (lowpc == ((CORE_ADDR) -1))
10920 lowpc = highpc;
10921 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10922
10923 file_and_directory fnd = find_file_and_directory (die, cu);
10924
10925 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10926 standardised yet. As a workaround for the language detection we fall
10927 back to the DW_AT_producer string. */
10928 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10929 cu->language = language_opencl;
10930
10931 /* Similar hack for Go. */
10932 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10933 set_cu_language (DW_LANG_Go, cu);
10934
10935 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10936
10937 /* Decode line number information if present. We do this before
10938 processing child DIEs, so that the line header table is available
10939 for DW_AT_decl_file. */
10940 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10941
10942 /* Process all dies in compilation unit. */
10943 if (die->child != NULL)
10944 {
10945 child_die = die->child;
10946 while (child_die && child_die->tag)
10947 {
10948 process_die (child_die, cu);
10949 child_die = child_die->sibling;
10950 }
10951 }
10952
10953 /* Decode macro information, if present. Dwarf 2 macro information
10954 refers to information in the line number info statement program
10955 header, so we can only read it if we've read the header
10956 successfully. */
10957 attr = dwarf2_attr (die, DW_AT_macros, cu);
10958 if (attr == NULL)
10959 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10960 if (attr && cu->line_header)
10961 {
10962 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10963 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10964
10965 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10966 }
10967 else
10968 {
10969 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10970 if (attr && cu->line_header)
10971 {
10972 unsigned int macro_offset = DW_UNSND (attr);
10973
10974 dwarf_decode_macros (cu, macro_offset, 0);
10975 }
10976 }
10977 }
10978
10979 void
10980 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10981 {
10982 struct type_unit_group *tu_group;
10983 int first_time;
10984 struct attribute *attr;
10985 unsigned int i;
10986 struct signatured_type *sig_type;
10987
10988 gdb_assert (per_cu->is_debug_types);
10989 sig_type = (struct signatured_type *) per_cu;
10990
10991 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10992
10993 /* If we're using .gdb_index (includes -readnow) then
10994 per_cu->type_unit_group may not have been set up yet. */
10995 if (sig_type->type_unit_group == NULL)
10996 sig_type->type_unit_group = get_type_unit_group (this, attr);
10997 tu_group = sig_type->type_unit_group;
10998
10999 /* If we've already processed this stmt_list there's no real need to
11000 do it again, we could fake it and just recreate the part we need
11001 (file name,index -> symtab mapping). If data shows this optimization
11002 is useful we can do it then. */
11003 first_time = tu_group->compunit_symtab == NULL;
11004
11005 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11006 debug info. */
11007 line_header_up lh;
11008 if (attr != NULL)
11009 {
11010 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11011 lh = dwarf_decode_line_header (line_offset, this);
11012 }
11013 if (lh == NULL)
11014 {
11015 if (first_time)
11016 start_symtab ("", NULL, 0);
11017 else
11018 {
11019 gdb_assert (tu_group->symtabs == NULL);
11020 gdb_assert (m_builder == nullptr);
11021 struct compunit_symtab *cust = tu_group->compunit_symtab;
11022 m_builder.reset (new struct buildsym_compunit
11023 (COMPUNIT_OBJFILE (cust), "",
11024 COMPUNIT_DIRNAME (cust),
11025 compunit_language (cust),
11026 0, cust));
11027 list_in_scope = get_builder ()->get_file_symbols ();
11028 }
11029 return;
11030 }
11031
11032 line_header = lh.release ();
11033 line_header_die_owner = die;
11034
11035 if (first_time)
11036 {
11037 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11038
11039 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11040 still initializing it, and our caller (a few levels up)
11041 process_full_type_unit still needs to know if this is the first
11042 time. */
11043
11044 tu_group->symtabs
11045 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11046 struct symtab *, line_header->file_names_size ());
11047
11048 auto &file_names = line_header->file_names ();
11049 for (i = 0; i < file_names.size (); ++i)
11050 {
11051 file_entry &fe = file_names[i];
11052 dwarf2_start_subfile (this, fe.name,
11053 fe.include_dir (line_header));
11054 buildsym_compunit *b = get_builder ();
11055 if (b->get_current_subfile ()->symtab == NULL)
11056 {
11057 /* NOTE: start_subfile will recognize when it's been
11058 passed a file it has already seen. So we can't
11059 assume there's a simple mapping from
11060 cu->line_header->file_names to subfiles, plus
11061 cu->line_header->file_names may contain dups. */
11062 b->get_current_subfile ()->symtab
11063 = allocate_symtab (cust, b->get_current_subfile ()->name);
11064 }
11065
11066 fe.symtab = b->get_current_subfile ()->symtab;
11067 tu_group->symtabs[i] = fe.symtab;
11068 }
11069 }
11070 else
11071 {
11072 gdb_assert (m_builder == nullptr);
11073 struct compunit_symtab *cust = tu_group->compunit_symtab;
11074 m_builder.reset (new struct buildsym_compunit
11075 (COMPUNIT_OBJFILE (cust), "",
11076 COMPUNIT_DIRNAME (cust),
11077 compunit_language (cust),
11078 0, cust));
11079 list_in_scope = get_builder ()->get_file_symbols ();
11080
11081 auto &file_names = line_header->file_names ();
11082 for (i = 0; i < file_names.size (); ++i)
11083 {
11084 file_entry &fe = file_names[i];
11085 fe.symtab = tu_group->symtabs[i];
11086 }
11087 }
11088
11089 /* The main symtab is allocated last. Type units don't have DW_AT_name
11090 so they don't have a "real" (so to speak) symtab anyway.
11091 There is later code that will assign the main symtab to all symbols
11092 that don't have one. We need to handle the case of a symbol with a
11093 missing symtab (DW_AT_decl_file) anyway. */
11094 }
11095
11096 /* Process DW_TAG_type_unit.
11097 For TUs we want to skip the first top level sibling if it's not the
11098 actual type being defined by this TU. In this case the first top
11099 level sibling is there to provide context only. */
11100
11101 static void
11102 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11103 {
11104 struct die_info *child_die;
11105
11106 prepare_one_comp_unit (cu, die, language_minimal);
11107
11108 /* Initialize (or reinitialize) the machinery for building symtabs.
11109 We do this before processing child DIEs, so that the line header table
11110 is available for DW_AT_decl_file. */
11111 cu->setup_type_unit_groups (die);
11112
11113 if (die->child != NULL)
11114 {
11115 child_die = die->child;
11116 while (child_die && child_die->tag)
11117 {
11118 process_die (child_die, cu);
11119 child_die = child_die->sibling;
11120 }
11121 }
11122 }
11123 \f
11124 /* DWO/DWP files.
11125
11126 http://gcc.gnu.org/wiki/DebugFission
11127 http://gcc.gnu.org/wiki/DebugFissionDWP
11128
11129 To simplify handling of both DWO files ("object" files with the DWARF info)
11130 and DWP files (a file with the DWOs packaged up into one file), we treat
11131 DWP files as having a collection of virtual DWO files. */
11132
11133 static hashval_t
11134 hash_dwo_file (const void *item)
11135 {
11136 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11137 hashval_t hash;
11138
11139 hash = htab_hash_string (dwo_file->dwo_name);
11140 if (dwo_file->comp_dir != NULL)
11141 hash += htab_hash_string (dwo_file->comp_dir);
11142 return hash;
11143 }
11144
11145 static int
11146 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11147 {
11148 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11149 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11150
11151 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11152 return 0;
11153 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11154 return lhs->comp_dir == rhs->comp_dir;
11155 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11156 }
11157
11158 /* Allocate a hash table for DWO files. */
11159
11160 static htab_up
11161 allocate_dwo_file_hash_table ()
11162 {
11163 auto delete_dwo_file = [] (void *item)
11164 {
11165 struct dwo_file *dwo_file = (struct dwo_file *) item;
11166
11167 delete dwo_file;
11168 };
11169
11170 return htab_up (htab_create_alloc (41,
11171 hash_dwo_file,
11172 eq_dwo_file,
11173 delete_dwo_file,
11174 xcalloc, xfree));
11175 }
11176
11177 /* Lookup DWO file DWO_NAME. */
11178
11179 static void **
11180 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11181 const char *dwo_name,
11182 const char *comp_dir)
11183 {
11184 struct dwo_file find_entry;
11185 void **slot;
11186
11187 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11188 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11189
11190 find_entry.dwo_name = dwo_name;
11191 find_entry.comp_dir = comp_dir;
11192 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11193 INSERT);
11194
11195 return slot;
11196 }
11197
11198 static hashval_t
11199 hash_dwo_unit (const void *item)
11200 {
11201 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11202
11203 /* This drops the top 32 bits of the id, but is ok for a hash. */
11204 return dwo_unit->signature;
11205 }
11206
11207 static int
11208 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11209 {
11210 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11211 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11212
11213 /* The signature is assumed to be unique within the DWO file.
11214 So while object file CU dwo_id's always have the value zero,
11215 that's OK, assuming each object file DWO file has only one CU,
11216 and that's the rule for now. */
11217 return lhs->signature == rhs->signature;
11218 }
11219
11220 /* Allocate a hash table for DWO CUs,TUs.
11221 There is one of these tables for each of CUs,TUs for each DWO file. */
11222
11223 static htab_up
11224 allocate_dwo_unit_table ()
11225 {
11226 /* Start out with a pretty small number.
11227 Generally DWO files contain only one CU and maybe some TUs. */
11228 return htab_up (htab_create_alloc (3,
11229 hash_dwo_unit,
11230 eq_dwo_unit,
11231 NULL, xcalloc, xfree));
11232 }
11233
11234 /* die_reader_func for create_dwo_cu. */
11235
11236 static void
11237 create_dwo_cu_reader (const struct die_reader_specs *reader,
11238 const gdb_byte *info_ptr,
11239 struct die_info *comp_unit_die,
11240 struct dwo_file *dwo_file,
11241 struct dwo_unit *dwo_unit)
11242 {
11243 struct dwarf2_cu *cu = reader->cu;
11244 sect_offset sect_off = cu->per_cu->sect_off;
11245 struct dwarf2_section_info *section = cu->per_cu->section;
11246
11247 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11248 if (!signature.has_value ())
11249 {
11250 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11251 " its dwo_id [in module %s]"),
11252 sect_offset_str (sect_off), dwo_file->dwo_name);
11253 return;
11254 }
11255
11256 dwo_unit->dwo_file = dwo_file;
11257 dwo_unit->signature = *signature;
11258 dwo_unit->section = section;
11259 dwo_unit->sect_off = sect_off;
11260 dwo_unit->length = cu->per_cu->length;
11261
11262 if (dwarf_read_debug)
11263 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11264 sect_offset_str (sect_off),
11265 hex_string (dwo_unit->signature));
11266 }
11267
11268 /* Create the dwo_units for the CUs in a DWO_FILE.
11269 Note: This function processes DWO files only, not DWP files. */
11270
11271 static void
11272 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11273 dwarf2_cu *cu, struct dwo_file &dwo_file,
11274 dwarf2_section_info &section, htab_up &cus_htab)
11275 {
11276 struct objfile *objfile = dwarf2_per_objfile->objfile;
11277 const gdb_byte *info_ptr, *end_ptr;
11278
11279 section.read (objfile);
11280 info_ptr = section.buffer;
11281
11282 if (info_ptr == NULL)
11283 return;
11284
11285 if (dwarf_read_debug)
11286 {
11287 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11288 section.get_name (),
11289 section.get_file_name ());
11290 }
11291
11292 end_ptr = info_ptr + section.size;
11293 while (info_ptr < end_ptr)
11294 {
11295 struct dwarf2_per_cu_data per_cu;
11296 struct dwo_unit read_unit {};
11297 struct dwo_unit *dwo_unit;
11298 void **slot;
11299 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11300
11301 memset (&per_cu, 0, sizeof (per_cu));
11302 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11303 per_cu.is_debug_types = 0;
11304 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11305 per_cu.section = &section;
11306
11307 cutu_reader reader (&per_cu, cu, &dwo_file);
11308 if (!reader.dummy_p)
11309 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11310 &dwo_file, &read_unit);
11311 info_ptr += per_cu.length;
11312
11313 // If the unit could not be parsed, skip it.
11314 if (read_unit.dwo_file == NULL)
11315 continue;
11316
11317 if (cus_htab == NULL)
11318 cus_htab = allocate_dwo_unit_table ();
11319
11320 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
11321 struct dwo_unit);
11322 *dwo_unit = read_unit;
11323 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11324 gdb_assert (slot != NULL);
11325 if (*slot != NULL)
11326 {
11327 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11328 sect_offset dup_sect_off = dup_cu->sect_off;
11329
11330 complaint (_("debug cu entry at offset %s is duplicate to"
11331 " the entry at offset %s, signature %s"),
11332 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11333 hex_string (dwo_unit->signature));
11334 }
11335 *slot = (void *)dwo_unit;
11336 }
11337 }
11338
11339 /* DWP file .debug_{cu,tu}_index section format:
11340 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11341
11342 DWP Version 1:
11343
11344 Both index sections have the same format, and serve to map a 64-bit
11345 signature to a set of section numbers. Each section begins with a header,
11346 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11347 indexes, and a pool of 32-bit section numbers. The index sections will be
11348 aligned at 8-byte boundaries in the file.
11349
11350 The index section header consists of:
11351
11352 V, 32 bit version number
11353 -, 32 bits unused
11354 N, 32 bit number of compilation units or type units in the index
11355 M, 32 bit number of slots in the hash table
11356
11357 Numbers are recorded using the byte order of the application binary.
11358
11359 The hash table begins at offset 16 in the section, and consists of an array
11360 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11361 order of the application binary). Unused slots in the hash table are 0.
11362 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11363
11364 The parallel table begins immediately after the hash table
11365 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11366 array of 32-bit indexes (using the byte order of the application binary),
11367 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11368 table contains a 32-bit index into the pool of section numbers. For unused
11369 hash table slots, the corresponding entry in the parallel table will be 0.
11370
11371 The pool of section numbers begins immediately following the hash table
11372 (at offset 16 + 12 * M from the beginning of the section). The pool of
11373 section numbers consists of an array of 32-bit words (using the byte order
11374 of the application binary). Each item in the array is indexed starting
11375 from 0. The hash table entry provides the index of the first section
11376 number in the set. Additional section numbers in the set follow, and the
11377 set is terminated by a 0 entry (section number 0 is not used in ELF).
11378
11379 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11380 section must be the first entry in the set, and the .debug_abbrev.dwo must
11381 be the second entry. Other members of the set may follow in any order.
11382
11383 ---
11384
11385 DWP Version 2:
11386
11387 DWP Version 2 combines all the .debug_info, etc. sections into one,
11388 and the entries in the index tables are now offsets into these sections.
11389 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11390 section.
11391
11392 Index Section Contents:
11393 Header
11394 Hash Table of Signatures dwp_hash_table.hash_table
11395 Parallel Table of Indices dwp_hash_table.unit_table
11396 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11397 Table of Section Sizes dwp_hash_table.v2.sizes
11398
11399 The index section header consists of:
11400
11401 V, 32 bit version number
11402 L, 32 bit number of columns in the table of section offsets
11403 N, 32 bit number of compilation units or type units in the index
11404 M, 32 bit number of slots in the hash table
11405
11406 Numbers are recorded using the byte order of the application binary.
11407
11408 The hash table has the same format as version 1.
11409 The parallel table of indices has the same format as version 1,
11410 except that the entries are origin-1 indices into the table of sections
11411 offsets and the table of section sizes.
11412
11413 The table of offsets begins immediately following the parallel table
11414 (at offset 16 + 12 * M from the beginning of the section). The table is
11415 a two-dimensional array of 32-bit words (using the byte order of the
11416 application binary), with L columns and N+1 rows, in row-major order.
11417 Each row in the array is indexed starting from 0. The first row provides
11418 a key to the remaining rows: each column in this row provides an identifier
11419 for a debug section, and the offsets in the same column of subsequent rows
11420 refer to that section. The section identifiers are:
11421
11422 DW_SECT_INFO 1 .debug_info.dwo
11423 DW_SECT_TYPES 2 .debug_types.dwo
11424 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11425 DW_SECT_LINE 4 .debug_line.dwo
11426 DW_SECT_LOC 5 .debug_loc.dwo
11427 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11428 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11429 DW_SECT_MACRO 8 .debug_macro.dwo
11430
11431 The offsets provided by the CU and TU index sections are the base offsets
11432 for the contributions made by each CU or TU to the corresponding section
11433 in the package file. Each CU and TU header contains an abbrev_offset
11434 field, used to find the abbreviations table for that CU or TU within the
11435 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11436 be interpreted as relative to the base offset given in the index section.
11437 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11438 should be interpreted as relative to the base offset for .debug_line.dwo,
11439 and offsets into other debug sections obtained from DWARF attributes should
11440 also be interpreted as relative to the corresponding base offset.
11441
11442 The table of sizes begins immediately following the table of offsets.
11443 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11444 with L columns and N rows, in row-major order. Each row in the array is
11445 indexed starting from 1 (row 0 is shared by the two tables).
11446
11447 ---
11448
11449 Hash table lookup is handled the same in version 1 and 2:
11450
11451 We assume that N and M will not exceed 2^32 - 1.
11452 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11453
11454 Given a 64-bit compilation unit signature or a type signature S, an entry
11455 in the hash table is located as follows:
11456
11457 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11458 the low-order k bits all set to 1.
11459
11460 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11461
11462 3) If the hash table entry at index H matches the signature, use that
11463 entry. If the hash table entry at index H is unused (all zeroes),
11464 terminate the search: the signature is not present in the table.
11465
11466 4) Let H = (H + H') modulo M. Repeat at Step 3.
11467
11468 Because M > N and H' and M are relatively prime, the search is guaranteed
11469 to stop at an unused slot or find the match. */
11470
11471 /* Create a hash table to map DWO IDs to their CU/TU entry in
11472 .debug_{info,types}.dwo in DWP_FILE.
11473 Returns NULL if there isn't one.
11474 Note: This function processes DWP files only, not DWO files. */
11475
11476 static struct dwp_hash_table *
11477 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11478 struct dwp_file *dwp_file, int is_debug_types)
11479 {
11480 struct objfile *objfile = dwarf2_per_objfile->objfile;
11481 bfd *dbfd = dwp_file->dbfd.get ();
11482 const gdb_byte *index_ptr, *index_end;
11483 struct dwarf2_section_info *index;
11484 uint32_t version, nr_columns, nr_units, nr_slots;
11485 struct dwp_hash_table *htab;
11486
11487 if (is_debug_types)
11488 index = &dwp_file->sections.tu_index;
11489 else
11490 index = &dwp_file->sections.cu_index;
11491
11492 if (index->empty ())
11493 return NULL;
11494 index->read (objfile);
11495
11496 index_ptr = index->buffer;
11497 index_end = index_ptr + index->size;
11498
11499 version = read_4_bytes (dbfd, index_ptr);
11500 index_ptr += 4;
11501 if (version == 2)
11502 nr_columns = read_4_bytes (dbfd, index_ptr);
11503 else
11504 nr_columns = 0;
11505 index_ptr += 4;
11506 nr_units = read_4_bytes (dbfd, index_ptr);
11507 index_ptr += 4;
11508 nr_slots = read_4_bytes (dbfd, index_ptr);
11509 index_ptr += 4;
11510
11511 if (version != 1 && version != 2)
11512 {
11513 error (_("Dwarf Error: unsupported DWP file version (%s)"
11514 " [in module %s]"),
11515 pulongest (version), dwp_file->name);
11516 }
11517 if (nr_slots != (nr_slots & -nr_slots))
11518 {
11519 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11520 " is not power of 2 [in module %s]"),
11521 pulongest (nr_slots), dwp_file->name);
11522 }
11523
11524 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11525 htab->version = version;
11526 htab->nr_columns = nr_columns;
11527 htab->nr_units = nr_units;
11528 htab->nr_slots = nr_slots;
11529 htab->hash_table = index_ptr;
11530 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11531
11532 /* Exit early if the table is empty. */
11533 if (nr_slots == 0 || nr_units == 0
11534 || (version == 2 && nr_columns == 0))
11535 {
11536 /* All must be zero. */
11537 if (nr_slots != 0 || nr_units != 0
11538 || (version == 2 && nr_columns != 0))
11539 {
11540 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11541 " all zero [in modules %s]"),
11542 dwp_file->name);
11543 }
11544 return htab;
11545 }
11546
11547 if (version == 1)
11548 {
11549 htab->section_pool.v1.indices =
11550 htab->unit_table + sizeof (uint32_t) * nr_slots;
11551 /* It's harder to decide whether the section is too small in v1.
11552 V1 is deprecated anyway so we punt. */
11553 }
11554 else
11555 {
11556 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11557 int *ids = htab->section_pool.v2.section_ids;
11558 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11559 /* Reverse map for error checking. */
11560 int ids_seen[DW_SECT_MAX + 1];
11561 int i;
11562
11563 if (nr_columns < 2)
11564 {
11565 error (_("Dwarf Error: bad DWP hash table, too few columns"
11566 " in section table [in module %s]"),
11567 dwp_file->name);
11568 }
11569 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11570 {
11571 error (_("Dwarf Error: bad DWP hash table, too many columns"
11572 " in section table [in module %s]"),
11573 dwp_file->name);
11574 }
11575 memset (ids, 255, sizeof_ids);
11576 memset (ids_seen, 255, sizeof (ids_seen));
11577 for (i = 0; i < nr_columns; ++i)
11578 {
11579 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11580
11581 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11582 {
11583 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11584 " in section table [in module %s]"),
11585 id, dwp_file->name);
11586 }
11587 if (ids_seen[id] != -1)
11588 {
11589 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11590 " id %d in section table [in module %s]"),
11591 id, dwp_file->name);
11592 }
11593 ids_seen[id] = i;
11594 ids[i] = id;
11595 }
11596 /* Must have exactly one info or types section. */
11597 if (((ids_seen[DW_SECT_INFO] != -1)
11598 + (ids_seen[DW_SECT_TYPES] != -1))
11599 != 1)
11600 {
11601 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11602 " DWO info/types section [in module %s]"),
11603 dwp_file->name);
11604 }
11605 /* Must have an abbrev section. */
11606 if (ids_seen[DW_SECT_ABBREV] == -1)
11607 {
11608 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11609 " section [in module %s]"),
11610 dwp_file->name);
11611 }
11612 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11613 htab->section_pool.v2.sizes =
11614 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11615 * nr_units * nr_columns);
11616 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11617 * nr_units * nr_columns))
11618 > index_end)
11619 {
11620 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11621 " [in module %s]"),
11622 dwp_file->name);
11623 }
11624 }
11625
11626 return htab;
11627 }
11628
11629 /* Update SECTIONS with the data from SECTP.
11630
11631 This function is like the other "locate" section routines that are
11632 passed to bfd_map_over_sections, but in this context the sections to
11633 read comes from the DWP V1 hash table, not the full ELF section table.
11634
11635 The result is non-zero for success, or zero if an error was found. */
11636
11637 static int
11638 locate_v1_virtual_dwo_sections (asection *sectp,
11639 struct virtual_v1_dwo_sections *sections)
11640 {
11641 const struct dwop_section_names *names = &dwop_section_names;
11642
11643 if (section_is_p (sectp->name, &names->abbrev_dwo))
11644 {
11645 /* There can be only one. */
11646 if (sections->abbrev.s.section != NULL)
11647 return 0;
11648 sections->abbrev.s.section = sectp;
11649 sections->abbrev.size = bfd_section_size (sectp);
11650 }
11651 else if (section_is_p (sectp->name, &names->info_dwo)
11652 || section_is_p (sectp->name, &names->types_dwo))
11653 {
11654 /* There can be only one. */
11655 if (sections->info_or_types.s.section != NULL)
11656 return 0;
11657 sections->info_or_types.s.section = sectp;
11658 sections->info_or_types.size = bfd_section_size (sectp);
11659 }
11660 else if (section_is_p (sectp->name, &names->line_dwo))
11661 {
11662 /* There can be only one. */
11663 if (sections->line.s.section != NULL)
11664 return 0;
11665 sections->line.s.section = sectp;
11666 sections->line.size = bfd_section_size (sectp);
11667 }
11668 else if (section_is_p (sectp->name, &names->loc_dwo))
11669 {
11670 /* There can be only one. */
11671 if (sections->loc.s.section != NULL)
11672 return 0;
11673 sections->loc.s.section = sectp;
11674 sections->loc.size = bfd_section_size (sectp);
11675 }
11676 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11677 {
11678 /* There can be only one. */
11679 if (sections->macinfo.s.section != NULL)
11680 return 0;
11681 sections->macinfo.s.section = sectp;
11682 sections->macinfo.size = bfd_section_size (sectp);
11683 }
11684 else if (section_is_p (sectp->name, &names->macro_dwo))
11685 {
11686 /* There can be only one. */
11687 if (sections->macro.s.section != NULL)
11688 return 0;
11689 sections->macro.s.section = sectp;
11690 sections->macro.size = bfd_section_size (sectp);
11691 }
11692 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11693 {
11694 /* There can be only one. */
11695 if (sections->str_offsets.s.section != NULL)
11696 return 0;
11697 sections->str_offsets.s.section = sectp;
11698 sections->str_offsets.size = bfd_section_size (sectp);
11699 }
11700 else
11701 {
11702 /* No other kind of section is valid. */
11703 return 0;
11704 }
11705
11706 return 1;
11707 }
11708
11709 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11710 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11711 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11712 This is for DWP version 1 files. */
11713
11714 static struct dwo_unit *
11715 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11716 struct dwp_file *dwp_file,
11717 uint32_t unit_index,
11718 const char *comp_dir,
11719 ULONGEST signature, int is_debug_types)
11720 {
11721 const struct dwp_hash_table *dwp_htab =
11722 is_debug_types ? dwp_file->tus : dwp_file->cus;
11723 bfd *dbfd = dwp_file->dbfd.get ();
11724 const char *kind = is_debug_types ? "TU" : "CU";
11725 struct dwo_file *dwo_file;
11726 struct dwo_unit *dwo_unit;
11727 struct virtual_v1_dwo_sections sections;
11728 void **dwo_file_slot;
11729 int i;
11730
11731 gdb_assert (dwp_file->version == 1);
11732
11733 if (dwarf_read_debug)
11734 {
11735 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11736 kind,
11737 pulongest (unit_index), hex_string (signature),
11738 dwp_file->name);
11739 }
11740
11741 /* Fetch the sections of this DWO unit.
11742 Put a limit on the number of sections we look for so that bad data
11743 doesn't cause us to loop forever. */
11744
11745 #define MAX_NR_V1_DWO_SECTIONS \
11746 (1 /* .debug_info or .debug_types */ \
11747 + 1 /* .debug_abbrev */ \
11748 + 1 /* .debug_line */ \
11749 + 1 /* .debug_loc */ \
11750 + 1 /* .debug_str_offsets */ \
11751 + 1 /* .debug_macro or .debug_macinfo */ \
11752 + 1 /* trailing zero */)
11753
11754 memset (&sections, 0, sizeof (sections));
11755
11756 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11757 {
11758 asection *sectp;
11759 uint32_t section_nr =
11760 read_4_bytes (dbfd,
11761 dwp_htab->section_pool.v1.indices
11762 + (unit_index + i) * sizeof (uint32_t));
11763
11764 if (section_nr == 0)
11765 break;
11766 if (section_nr >= dwp_file->num_sections)
11767 {
11768 error (_("Dwarf Error: bad DWP hash table, section number too large"
11769 " [in module %s]"),
11770 dwp_file->name);
11771 }
11772
11773 sectp = dwp_file->elf_sections[section_nr];
11774 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11775 {
11776 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11777 " [in module %s]"),
11778 dwp_file->name);
11779 }
11780 }
11781
11782 if (i < 2
11783 || sections.info_or_types.empty ()
11784 || sections.abbrev.empty ())
11785 {
11786 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11787 " [in module %s]"),
11788 dwp_file->name);
11789 }
11790 if (i == MAX_NR_V1_DWO_SECTIONS)
11791 {
11792 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11793 " [in module %s]"),
11794 dwp_file->name);
11795 }
11796
11797 /* It's easier for the rest of the code if we fake a struct dwo_file and
11798 have dwo_unit "live" in that. At least for now.
11799
11800 The DWP file can be made up of a random collection of CUs and TUs.
11801 However, for each CU + set of TUs that came from the same original DWO
11802 file, we can combine them back into a virtual DWO file to save space
11803 (fewer struct dwo_file objects to allocate). Remember that for really
11804 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11805
11806 std::string virtual_dwo_name =
11807 string_printf ("virtual-dwo/%d-%d-%d-%d",
11808 sections.abbrev.get_id (),
11809 sections.line.get_id (),
11810 sections.loc.get_id (),
11811 sections.str_offsets.get_id ());
11812 /* Can we use an existing virtual DWO file? */
11813 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11814 virtual_dwo_name.c_str (),
11815 comp_dir);
11816 /* Create one if necessary. */
11817 if (*dwo_file_slot == NULL)
11818 {
11819 if (dwarf_read_debug)
11820 {
11821 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11822 virtual_dwo_name.c_str ());
11823 }
11824 dwo_file = new struct dwo_file;
11825 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11826 dwo_file->comp_dir = comp_dir;
11827 dwo_file->sections.abbrev = sections.abbrev;
11828 dwo_file->sections.line = sections.line;
11829 dwo_file->sections.loc = sections.loc;
11830 dwo_file->sections.macinfo = sections.macinfo;
11831 dwo_file->sections.macro = sections.macro;
11832 dwo_file->sections.str_offsets = sections.str_offsets;
11833 /* The "str" section is global to the entire DWP file. */
11834 dwo_file->sections.str = dwp_file->sections.str;
11835 /* The info or types section is assigned below to dwo_unit,
11836 there's no need to record it in dwo_file.
11837 Also, we can't simply record type sections in dwo_file because
11838 we record a pointer into the vector in dwo_unit. As we collect more
11839 types we'll grow the vector and eventually have to reallocate space
11840 for it, invalidating all copies of pointers into the previous
11841 contents. */
11842 *dwo_file_slot = dwo_file;
11843 }
11844 else
11845 {
11846 if (dwarf_read_debug)
11847 {
11848 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11849 virtual_dwo_name.c_str ());
11850 }
11851 dwo_file = (struct dwo_file *) *dwo_file_slot;
11852 }
11853
11854 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11855 dwo_unit->dwo_file = dwo_file;
11856 dwo_unit->signature = signature;
11857 dwo_unit->section =
11858 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11859 *dwo_unit->section = sections.info_or_types;
11860 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11861
11862 return dwo_unit;
11863 }
11864
11865 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11866 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11867 piece within that section used by a TU/CU, return a virtual section
11868 of just that piece. */
11869
11870 static struct dwarf2_section_info
11871 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11872 struct dwarf2_section_info *section,
11873 bfd_size_type offset, bfd_size_type size)
11874 {
11875 struct dwarf2_section_info result;
11876 asection *sectp;
11877
11878 gdb_assert (section != NULL);
11879 gdb_assert (!section->is_virtual);
11880
11881 memset (&result, 0, sizeof (result));
11882 result.s.containing_section = section;
11883 result.is_virtual = true;
11884
11885 if (size == 0)
11886 return result;
11887
11888 sectp = section->get_bfd_section ();
11889
11890 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11891 bounds of the real section. This is a pretty-rare event, so just
11892 flag an error (easier) instead of a warning and trying to cope. */
11893 if (sectp == NULL
11894 || offset + size > bfd_section_size (sectp))
11895 {
11896 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11897 " in section %s [in module %s]"),
11898 sectp ? bfd_section_name (sectp) : "<unknown>",
11899 objfile_name (dwarf2_per_objfile->objfile));
11900 }
11901
11902 result.virtual_offset = offset;
11903 result.size = size;
11904 return result;
11905 }
11906
11907 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11908 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11909 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11910 This is for DWP version 2 files. */
11911
11912 static struct dwo_unit *
11913 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11914 struct dwp_file *dwp_file,
11915 uint32_t unit_index,
11916 const char *comp_dir,
11917 ULONGEST signature, int is_debug_types)
11918 {
11919 const struct dwp_hash_table *dwp_htab =
11920 is_debug_types ? dwp_file->tus : dwp_file->cus;
11921 bfd *dbfd = dwp_file->dbfd.get ();
11922 const char *kind = is_debug_types ? "TU" : "CU";
11923 struct dwo_file *dwo_file;
11924 struct dwo_unit *dwo_unit;
11925 struct virtual_v2_dwo_sections sections;
11926 void **dwo_file_slot;
11927 int i;
11928
11929 gdb_assert (dwp_file->version == 2);
11930
11931 if (dwarf_read_debug)
11932 {
11933 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11934 kind,
11935 pulongest (unit_index), hex_string (signature),
11936 dwp_file->name);
11937 }
11938
11939 /* Fetch the section offsets of this DWO unit. */
11940
11941 memset (&sections, 0, sizeof (sections));
11942
11943 for (i = 0; i < dwp_htab->nr_columns; ++i)
11944 {
11945 uint32_t offset = read_4_bytes (dbfd,
11946 dwp_htab->section_pool.v2.offsets
11947 + (((unit_index - 1) * dwp_htab->nr_columns
11948 + i)
11949 * sizeof (uint32_t)));
11950 uint32_t size = read_4_bytes (dbfd,
11951 dwp_htab->section_pool.v2.sizes
11952 + (((unit_index - 1) * dwp_htab->nr_columns
11953 + i)
11954 * sizeof (uint32_t)));
11955
11956 switch (dwp_htab->section_pool.v2.section_ids[i])
11957 {
11958 case DW_SECT_INFO:
11959 case DW_SECT_TYPES:
11960 sections.info_or_types_offset = offset;
11961 sections.info_or_types_size = size;
11962 break;
11963 case DW_SECT_ABBREV:
11964 sections.abbrev_offset = offset;
11965 sections.abbrev_size = size;
11966 break;
11967 case DW_SECT_LINE:
11968 sections.line_offset = offset;
11969 sections.line_size = size;
11970 break;
11971 case DW_SECT_LOC:
11972 sections.loc_offset = offset;
11973 sections.loc_size = size;
11974 break;
11975 case DW_SECT_STR_OFFSETS:
11976 sections.str_offsets_offset = offset;
11977 sections.str_offsets_size = size;
11978 break;
11979 case DW_SECT_MACINFO:
11980 sections.macinfo_offset = offset;
11981 sections.macinfo_size = size;
11982 break;
11983 case DW_SECT_MACRO:
11984 sections.macro_offset = offset;
11985 sections.macro_size = size;
11986 break;
11987 }
11988 }
11989
11990 /* It's easier for the rest of the code if we fake a struct dwo_file and
11991 have dwo_unit "live" in that. At least for now.
11992
11993 The DWP file can be made up of a random collection of CUs and TUs.
11994 However, for each CU + set of TUs that came from the same original DWO
11995 file, we can combine them back into a virtual DWO file to save space
11996 (fewer struct dwo_file objects to allocate). Remember that for really
11997 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11998
11999 std::string virtual_dwo_name =
12000 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12001 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12002 (long) (sections.line_size ? sections.line_offset : 0),
12003 (long) (sections.loc_size ? sections.loc_offset : 0),
12004 (long) (sections.str_offsets_size
12005 ? sections.str_offsets_offset : 0));
12006 /* Can we use an existing virtual DWO file? */
12007 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12008 virtual_dwo_name.c_str (),
12009 comp_dir);
12010 /* Create one if necessary. */
12011 if (*dwo_file_slot == NULL)
12012 {
12013 if (dwarf_read_debug)
12014 {
12015 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12016 virtual_dwo_name.c_str ());
12017 }
12018 dwo_file = new struct dwo_file;
12019 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12020 dwo_file->comp_dir = comp_dir;
12021 dwo_file->sections.abbrev =
12022 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12023 sections.abbrev_offset, sections.abbrev_size);
12024 dwo_file->sections.line =
12025 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12026 sections.line_offset, sections.line_size);
12027 dwo_file->sections.loc =
12028 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12029 sections.loc_offset, sections.loc_size);
12030 dwo_file->sections.macinfo =
12031 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12032 sections.macinfo_offset, sections.macinfo_size);
12033 dwo_file->sections.macro =
12034 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12035 sections.macro_offset, sections.macro_size);
12036 dwo_file->sections.str_offsets =
12037 create_dwp_v2_section (dwarf2_per_objfile,
12038 &dwp_file->sections.str_offsets,
12039 sections.str_offsets_offset,
12040 sections.str_offsets_size);
12041 /* The "str" section is global to the entire DWP file. */
12042 dwo_file->sections.str = dwp_file->sections.str;
12043 /* The info or types section is assigned below to dwo_unit,
12044 there's no need to record it in dwo_file.
12045 Also, we can't simply record type sections in dwo_file because
12046 we record a pointer into the vector in dwo_unit. As we collect more
12047 types we'll grow the vector and eventually have to reallocate space
12048 for it, invalidating all copies of pointers into the previous
12049 contents. */
12050 *dwo_file_slot = dwo_file;
12051 }
12052 else
12053 {
12054 if (dwarf_read_debug)
12055 {
12056 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12057 virtual_dwo_name.c_str ());
12058 }
12059 dwo_file = (struct dwo_file *) *dwo_file_slot;
12060 }
12061
12062 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12063 dwo_unit->dwo_file = dwo_file;
12064 dwo_unit->signature = signature;
12065 dwo_unit->section =
12066 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12067 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12068 is_debug_types
12069 ? &dwp_file->sections.types
12070 : &dwp_file->sections.info,
12071 sections.info_or_types_offset,
12072 sections.info_or_types_size);
12073 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12074
12075 return dwo_unit;
12076 }
12077
12078 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12079 Returns NULL if the signature isn't found. */
12080
12081 static struct dwo_unit *
12082 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12083 struct dwp_file *dwp_file, const char *comp_dir,
12084 ULONGEST signature, int is_debug_types)
12085 {
12086 const struct dwp_hash_table *dwp_htab =
12087 is_debug_types ? dwp_file->tus : dwp_file->cus;
12088 bfd *dbfd = dwp_file->dbfd.get ();
12089 uint32_t mask = dwp_htab->nr_slots - 1;
12090 uint32_t hash = signature & mask;
12091 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12092 unsigned int i;
12093 void **slot;
12094 struct dwo_unit find_dwo_cu;
12095
12096 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12097 find_dwo_cu.signature = signature;
12098 slot = htab_find_slot (is_debug_types
12099 ? dwp_file->loaded_tus.get ()
12100 : dwp_file->loaded_cus.get (),
12101 &find_dwo_cu, INSERT);
12102
12103 if (*slot != NULL)
12104 return (struct dwo_unit *) *slot;
12105
12106 /* Use a for loop so that we don't loop forever on bad debug info. */
12107 for (i = 0; i < dwp_htab->nr_slots; ++i)
12108 {
12109 ULONGEST signature_in_table;
12110
12111 signature_in_table =
12112 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12113 if (signature_in_table == signature)
12114 {
12115 uint32_t unit_index =
12116 read_4_bytes (dbfd,
12117 dwp_htab->unit_table + hash * sizeof (uint32_t));
12118
12119 if (dwp_file->version == 1)
12120 {
12121 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12122 dwp_file, unit_index,
12123 comp_dir, signature,
12124 is_debug_types);
12125 }
12126 else
12127 {
12128 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12129 dwp_file, unit_index,
12130 comp_dir, signature,
12131 is_debug_types);
12132 }
12133 return (struct dwo_unit *) *slot;
12134 }
12135 if (signature_in_table == 0)
12136 return NULL;
12137 hash = (hash + hash2) & mask;
12138 }
12139
12140 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12141 " [in module %s]"),
12142 dwp_file->name);
12143 }
12144
12145 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12146 Open the file specified by FILE_NAME and hand it off to BFD for
12147 preliminary analysis. Return a newly initialized bfd *, which
12148 includes a canonicalized copy of FILE_NAME.
12149 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12150 SEARCH_CWD is true if the current directory is to be searched.
12151 It will be searched before debug-file-directory.
12152 If successful, the file is added to the bfd include table of the
12153 objfile's bfd (see gdb_bfd_record_inclusion).
12154 If unable to find/open the file, return NULL.
12155 NOTE: This function is derived from symfile_bfd_open. */
12156
12157 static gdb_bfd_ref_ptr
12158 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12159 const char *file_name, int is_dwp, int search_cwd)
12160 {
12161 int desc;
12162 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12163 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12164 to debug_file_directory. */
12165 const char *search_path;
12166 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12167
12168 gdb::unique_xmalloc_ptr<char> search_path_holder;
12169 if (search_cwd)
12170 {
12171 if (*debug_file_directory != '\0')
12172 {
12173 search_path_holder.reset (concat (".", dirname_separator_string,
12174 debug_file_directory,
12175 (char *) NULL));
12176 search_path = search_path_holder.get ();
12177 }
12178 else
12179 search_path = ".";
12180 }
12181 else
12182 search_path = debug_file_directory;
12183
12184 openp_flags flags = OPF_RETURN_REALPATH;
12185 if (is_dwp)
12186 flags |= OPF_SEARCH_IN_PATH;
12187
12188 gdb::unique_xmalloc_ptr<char> absolute_name;
12189 desc = openp (search_path, flags, file_name,
12190 O_RDONLY | O_BINARY, &absolute_name);
12191 if (desc < 0)
12192 return NULL;
12193
12194 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12195 gnutarget, desc));
12196 if (sym_bfd == NULL)
12197 return NULL;
12198 bfd_set_cacheable (sym_bfd.get (), 1);
12199
12200 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12201 return NULL;
12202
12203 /* Success. Record the bfd as having been included by the objfile's bfd.
12204 This is important because things like demangled_names_hash lives in the
12205 objfile's per_bfd space and may have references to things like symbol
12206 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12207 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12208
12209 return sym_bfd;
12210 }
12211
12212 /* Try to open DWO file FILE_NAME.
12213 COMP_DIR is the DW_AT_comp_dir attribute.
12214 The result is the bfd handle of the file.
12215 If there is a problem finding or opening the file, return NULL.
12216 Upon success, the canonicalized path of the file is stored in the bfd,
12217 same as symfile_bfd_open. */
12218
12219 static gdb_bfd_ref_ptr
12220 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12221 const char *file_name, const char *comp_dir)
12222 {
12223 if (IS_ABSOLUTE_PATH (file_name))
12224 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12225 0 /*is_dwp*/, 0 /*search_cwd*/);
12226
12227 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12228
12229 if (comp_dir != NULL)
12230 {
12231 gdb::unique_xmalloc_ptr<char> path_to_try
12232 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12233
12234 /* NOTE: If comp_dir is a relative path, this will also try the
12235 search path, which seems useful. */
12236 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12237 path_to_try.get (),
12238 0 /*is_dwp*/,
12239 1 /*search_cwd*/));
12240 if (abfd != NULL)
12241 return abfd;
12242 }
12243
12244 /* That didn't work, try debug-file-directory, which, despite its name,
12245 is a list of paths. */
12246
12247 if (*debug_file_directory == '\0')
12248 return NULL;
12249
12250 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12251 0 /*is_dwp*/, 1 /*search_cwd*/);
12252 }
12253
12254 /* This function is mapped across the sections and remembers the offset and
12255 size of each of the DWO debugging sections we are interested in. */
12256
12257 static void
12258 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12259 {
12260 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12261 const struct dwop_section_names *names = &dwop_section_names;
12262
12263 if (section_is_p (sectp->name, &names->abbrev_dwo))
12264 {
12265 dwo_sections->abbrev.s.section = sectp;
12266 dwo_sections->abbrev.size = bfd_section_size (sectp);
12267 }
12268 else if (section_is_p (sectp->name, &names->info_dwo))
12269 {
12270 dwo_sections->info.s.section = sectp;
12271 dwo_sections->info.size = bfd_section_size (sectp);
12272 }
12273 else if (section_is_p (sectp->name, &names->line_dwo))
12274 {
12275 dwo_sections->line.s.section = sectp;
12276 dwo_sections->line.size = bfd_section_size (sectp);
12277 }
12278 else if (section_is_p (sectp->name, &names->loc_dwo))
12279 {
12280 dwo_sections->loc.s.section = sectp;
12281 dwo_sections->loc.size = bfd_section_size (sectp);
12282 }
12283 else if (section_is_p (sectp->name, &names->loclists_dwo))
12284 {
12285 dwo_sections->loclists.s.section = sectp;
12286 dwo_sections->loclists.size = bfd_section_size (sectp);
12287 }
12288 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12289 {
12290 dwo_sections->macinfo.s.section = sectp;
12291 dwo_sections->macinfo.size = bfd_section_size (sectp);
12292 }
12293 else if (section_is_p (sectp->name, &names->macro_dwo))
12294 {
12295 dwo_sections->macro.s.section = sectp;
12296 dwo_sections->macro.size = bfd_section_size (sectp);
12297 }
12298 else if (section_is_p (sectp->name, &names->str_dwo))
12299 {
12300 dwo_sections->str.s.section = sectp;
12301 dwo_sections->str.size = bfd_section_size (sectp);
12302 }
12303 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12304 {
12305 dwo_sections->str_offsets.s.section = sectp;
12306 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12307 }
12308 else if (section_is_p (sectp->name, &names->types_dwo))
12309 {
12310 struct dwarf2_section_info type_section;
12311
12312 memset (&type_section, 0, sizeof (type_section));
12313 type_section.s.section = sectp;
12314 type_section.size = bfd_section_size (sectp);
12315 dwo_sections->types.push_back (type_section);
12316 }
12317 }
12318
12319 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12320 by PER_CU. This is for the non-DWP case.
12321 The result is NULL if DWO_NAME can't be found. */
12322
12323 static struct dwo_file *
12324 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12325 const char *dwo_name, const char *comp_dir)
12326 {
12327 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12328
12329 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12330 if (dbfd == NULL)
12331 {
12332 if (dwarf_read_debug)
12333 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12334 return NULL;
12335 }
12336
12337 dwo_file_up dwo_file (new struct dwo_file);
12338 dwo_file->dwo_name = dwo_name;
12339 dwo_file->comp_dir = comp_dir;
12340 dwo_file->dbfd = std::move (dbfd);
12341
12342 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12343 &dwo_file->sections);
12344
12345 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12346 dwo_file->sections.info, dwo_file->cus);
12347
12348 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12349 dwo_file->sections.types, dwo_file->tus);
12350
12351 if (dwarf_read_debug)
12352 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12353
12354 return dwo_file.release ();
12355 }
12356
12357 /* This function is mapped across the sections and remembers the offset and
12358 size of each of the DWP debugging sections common to version 1 and 2 that
12359 we are interested in. */
12360
12361 static void
12362 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12363 void *dwp_file_ptr)
12364 {
12365 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12366 const struct dwop_section_names *names = &dwop_section_names;
12367 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12368
12369 /* Record the ELF section number for later lookup: this is what the
12370 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12371 gdb_assert (elf_section_nr < dwp_file->num_sections);
12372 dwp_file->elf_sections[elf_section_nr] = sectp;
12373
12374 /* Look for specific sections that we need. */
12375 if (section_is_p (sectp->name, &names->str_dwo))
12376 {
12377 dwp_file->sections.str.s.section = sectp;
12378 dwp_file->sections.str.size = bfd_section_size (sectp);
12379 }
12380 else if (section_is_p (sectp->name, &names->cu_index))
12381 {
12382 dwp_file->sections.cu_index.s.section = sectp;
12383 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12384 }
12385 else if (section_is_p (sectp->name, &names->tu_index))
12386 {
12387 dwp_file->sections.tu_index.s.section = sectp;
12388 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12389 }
12390 }
12391
12392 /* This function is mapped across the sections and remembers the offset and
12393 size of each of the DWP version 2 debugging sections that we are interested
12394 in. This is split into a separate function because we don't know if we
12395 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12396
12397 static void
12398 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12399 {
12400 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12401 const struct dwop_section_names *names = &dwop_section_names;
12402 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12403
12404 /* Record the ELF section number for later lookup: this is what the
12405 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12406 gdb_assert (elf_section_nr < dwp_file->num_sections);
12407 dwp_file->elf_sections[elf_section_nr] = sectp;
12408
12409 /* Look for specific sections that we need. */
12410 if (section_is_p (sectp->name, &names->abbrev_dwo))
12411 {
12412 dwp_file->sections.abbrev.s.section = sectp;
12413 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12414 }
12415 else if (section_is_p (sectp->name, &names->info_dwo))
12416 {
12417 dwp_file->sections.info.s.section = sectp;
12418 dwp_file->sections.info.size = bfd_section_size (sectp);
12419 }
12420 else if (section_is_p (sectp->name, &names->line_dwo))
12421 {
12422 dwp_file->sections.line.s.section = sectp;
12423 dwp_file->sections.line.size = bfd_section_size (sectp);
12424 }
12425 else if (section_is_p (sectp->name, &names->loc_dwo))
12426 {
12427 dwp_file->sections.loc.s.section = sectp;
12428 dwp_file->sections.loc.size = bfd_section_size (sectp);
12429 }
12430 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12431 {
12432 dwp_file->sections.macinfo.s.section = sectp;
12433 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12434 }
12435 else if (section_is_p (sectp->name, &names->macro_dwo))
12436 {
12437 dwp_file->sections.macro.s.section = sectp;
12438 dwp_file->sections.macro.size = bfd_section_size (sectp);
12439 }
12440 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12441 {
12442 dwp_file->sections.str_offsets.s.section = sectp;
12443 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12444 }
12445 else if (section_is_p (sectp->name, &names->types_dwo))
12446 {
12447 dwp_file->sections.types.s.section = sectp;
12448 dwp_file->sections.types.size = bfd_section_size (sectp);
12449 }
12450 }
12451
12452 /* Hash function for dwp_file loaded CUs/TUs. */
12453
12454 static hashval_t
12455 hash_dwp_loaded_cutus (const void *item)
12456 {
12457 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12458
12459 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12460 return dwo_unit->signature;
12461 }
12462
12463 /* Equality function for dwp_file loaded CUs/TUs. */
12464
12465 static int
12466 eq_dwp_loaded_cutus (const void *a, const void *b)
12467 {
12468 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12469 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12470
12471 return dua->signature == dub->signature;
12472 }
12473
12474 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12475
12476 static htab_up
12477 allocate_dwp_loaded_cutus_table ()
12478 {
12479 return htab_up (htab_create_alloc (3,
12480 hash_dwp_loaded_cutus,
12481 eq_dwp_loaded_cutus,
12482 NULL, xcalloc, xfree));
12483 }
12484
12485 /* Try to open DWP file FILE_NAME.
12486 The result is the bfd handle of the file.
12487 If there is a problem finding or opening the file, return NULL.
12488 Upon success, the canonicalized path of the file is stored in the bfd,
12489 same as symfile_bfd_open. */
12490
12491 static gdb_bfd_ref_ptr
12492 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12493 const char *file_name)
12494 {
12495 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12496 1 /*is_dwp*/,
12497 1 /*search_cwd*/));
12498 if (abfd != NULL)
12499 return abfd;
12500
12501 /* Work around upstream bug 15652.
12502 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12503 [Whether that's a "bug" is debatable, but it is getting in our way.]
12504 We have no real idea where the dwp file is, because gdb's realpath-ing
12505 of the executable's path may have discarded the needed info.
12506 [IWBN if the dwp file name was recorded in the executable, akin to
12507 .gnu_debuglink, but that doesn't exist yet.]
12508 Strip the directory from FILE_NAME and search again. */
12509 if (*debug_file_directory != '\0')
12510 {
12511 /* Don't implicitly search the current directory here.
12512 If the user wants to search "." to handle this case,
12513 it must be added to debug-file-directory. */
12514 return try_open_dwop_file (dwarf2_per_objfile,
12515 lbasename (file_name), 1 /*is_dwp*/,
12516 0 /*search_cwd*/);
12517 }
12518
12519 return NULL;
12520 }
12521
12522 /* Initialize the use of the DWP file for the current objfile.
12523 By convention the name of the DWP file is ${objfile}.dwp.
12524 The result is NULL if it can't be found. */
12525
12526 static std::unique_ptr<struct dwp_file>
12527 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12528 {
12529 struct objfile *objfile = dwarf2_per_objfile->objfile;
12530
12531 /* Try to find first .dwp for the binary file before any symbolic links
12532 resolving. */
12533
12534 /* If the objfile is a debug file, find the name of the real binary
12535 file and get the name of dwp file from there. */
12536 std::string dwp_name;
12537 if (objfile->separate_debug_objfile_backlink != NULL)
12538 {
12539 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12540 const char *backlink_basename = lbasename (backlink->original_name);
12541
12542 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12543 }
12544 else
12545 dwp_name = objfile->original_name;
12546
12547 dwp_name += ".dwp";
12548
12549 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12550 if (dbfd == NULL
12551 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12552 {
12553 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12554 dwp_name = objfile_name (objfile);
12555 dwp_name += ".dwp";
12556 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12557 }
12558
12559 if (dbfd == NULL)
12560 {
12561 if (dwarf_read_debug)
12562 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12563 return std::unique_ptr<dwp_file> ();
12564 }
12565
12566 const char *name = bfd_get_filename (dbfd.get ());
12567 std::unique_ptr<struct dwp_file> dwp_file
12568 (new struct dwp_file (name, std::move (dbfd)));
12569
12570 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12571 dwp_file->elf_sections =
12572 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12573 dwp_file->num_sections, asection *);
12574
12575 bfd_map_over_sections (dwp_file->dbfd.get (),
12576 dwarf2_locate_common_dwp_sections,
12577 dwp_file.get ());
12578
12579 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12580 0);
12581
12582 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12583 1);
12584
12585 /* The DWP file version is stored in the hash table. Oh well. */
12586 if (dwp_file->cus && dwp_file->tus
12587 && dwp_file->cus->version != dwp_file->tus->version)
12588 {
12589 /* Technically speaking, we should try to limp along, but this is
12590 pretty bizarre. We use pulongest here because that's the established
12591 portability solution (e.g, we cannot use %u for uint32_t). */
12592 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12593 " TU version %s [in DWP file %s]"),
12594 pulongest (dwp_file->cus->version),
12595 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12596 }
12597
12598 if (dwp_file->cus)
12599 dwp_file->version = dwp_file->cus->version;
12600 else if (dwp_file->tus)
12601 dwp_file->version = dwp_file->tus->version;
12602 else
12603 dwp_file->version = 2;
12604
12605 if (dwp_file->version == 2)
12606 bfd_map_over_sections (dwp_file->dbfd.get (),
12607 dwarf2_locate_v2_dwp_sections,
12608 dwp_file.get ());
12609
12610 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12611 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12612
12613 if (dwarf_read_debug)
12614 {
12615 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12616 fprintf_unfiltered (gdb_stdlog,
12617 " %s CUs, %s TUs\n",
12618 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12619 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12620 }
12621
12622 return dwp_file;
12623 }
12624
12625 /* Wrapper around open_and_init_dwp_file, only open it once. */
12626
12627 static struct dwp_file *
12628 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12629 {
12630 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12631 {
12632 dwarf2_per_objfile->per_bfd->dwp_file
12633 = open_and_init_dwp_file (dwarf2_per_objfile);
12634 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12635 }
12636 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12637 }
12638
12639 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12640 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12641 or in the DWP file for the objfile, referenced by THIS_UNIT.
12642 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12643 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12644
12645 This is called, for example, when wanting to read a variable with a
12646 complex location. Therefore we don't want to do file i/o for every call.
12647 Therefore we don't want to look for a DWO file on every call.
12648 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12649 then we check if we've already seen DWO_NAME, and only THEN do we check
12650 for a DWO file.
12651
12652 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12653 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12654
12655 static struct dwo_unit *
12656 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12657 const char *dwo_name, const char *comp_dir,
12658 ULONGEST signature, int is_debug_types)
12659 {
12660 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12661 struct objfile *objfile = dwarf2_per_objfile->objfile;
12662 const char *kind = is_debug_types ? "TU" : "CU";
12663 void **dwo_file_slot;
12664 struct dwo_file *dwo_file;
12665 struct dwp_file *dwp_file;
12666
12667 /* First see if there's a DWP file.
12668 If we have a DWP file but didn't find the DWO inside it, don't
12669 look for the original DWO file. It makes gdb behave differently
12670 depending on whether one is debugging in the build tree. */
12671
12672 dwp_file = get_dwp_file (dwarf2_per_objfile);
12673 if (dwp_file != NULL)
12674 {
12675 const struct dwp_hash_table *dwp_htab =
12676 is_debug_types ? dwp_file->tus : dwp_file->cus;
12677
12678 if (dwp_htab != NULL)
12679 {
12680 struct dwo_unit *dwo_cutu =
12681 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12682 signature, is_debug_types);
12683
12684 if (dwo_cutu != NULL)
12685 {
12686 if (dwarf_read_debug)
12687 {
12688 fprintf_unfiltered (gdb_stdlog,
12689 "Virtual DWO %s %s found: @%s\n",
12690 kind, hex_string (signature),
12691 host_address_to_string (dwo_cutu));
12692 }
12693 return dwo_cutu;
12694 }
12695 }
12696 }
12697 else
12698 {
12699 /* No DWP file, look for the DWO file. */
12700
12701 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12702 dwo_name, comp_dir);
12703 if (*dwo_file_slot == NULL)
12704 {
12705 /* Read in the file and build a table of the CUs/TUs it contains. */
12706 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12707 }
12708 /* NOTE: This will be NULL if unable to open the file. */
12709 dwo_file = (struct dwo_file *) *dwo_file_slot;
12710
12711 if (dwo_file != NULL)
12712 {
12713 struct dwo_unit *dwo_cutu = NULL;
12714
12715 if (is_debug_types && dwo_file->tus)
12716 {
12717 struct dwo_unit find_dwo_cutu;
12718
12719 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12720 find_dwo_cutu.signature = signature;
12721 dwo_cutu
12722 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12723 &find_dwo_cutu);
12724 }
12725 else if (!is_debug_types && dwo_file->cus)
12726 {
12727 struct dwo_unit find_dwo_cutu;
12728
12729 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12730 find_dwo_cutu.signature = signature;
12731 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12732 &find_dwo_cutu);
12733 }
12734
12735 if (dwo_cutu != NULL)
12736 {
12737 if (dwarf_read_debug)
12738 {
12739 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12740 kind, dwo_name, hex_string (signature),
12741 host_address_to_string (dwo_cutu));
12742 }
12743 return dwo_cutu;
12744 }
12745 }
12746 }
12747
12748 /* We didn't find it. This could mean a dwo_id mismatch, or
12749 someone deleted the DWO/DWP file, or the search path isn't set up
12750 correctly to find the file. */
12751
12752 if (dwarf_read_debug)
12753 {
12754 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12755 kind, dwo_name, hex_string (signature));
12756 }
12757
12758 /* This is a warning and not a complaint because it can be caused by
12759 pilot error (e.g., user accidentally deleting the DWO). */
12760 {
12761 /* Print the name of the DWP file if we looked there, helps the user
12762 better diagnose the problem. */
12763 std::string dwp_text;
12764
12765 if (dwp_file != NULL)
12766 dwp_text = string_printf (" [in DWP file %s]",
12767 lbasename (dwp_file->name));
12768
12769 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12770 " [in module %s]"),
12771 kind, dwo_name, hex_string (signature),
12772 dwp_text.c_str (),
12773 this_unit->is_debug_types ? "TU" : "CU",
12774 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12775 }
12776 return NULL;
12777 }
12778
12779 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12780 See lookup_dwo_cutu_unit for details. */
12781
12782 static struct dwo_unit *
12783 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12784 const char *dwo_name, const char *comp_dir,
12785 ULONGEST signature)
12786 {
12787 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12788 }
12789
12790 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12791 See lookup_dwo_cutu_unit for details. */
12792
12793 static struct dwo_unit *
12794 lookup_dwo_type_unit (struct signatured_type *this_tu,
12795 const char *dwo_name, const char *comp_dir)
12796 {
12797 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12798 }
12799
12800 /* Traversal function for queue_and_load_all_dwo_tus. */
12801
12802 static int
12803 queue_and_load_dwo_tu (void **slot, void *info)
12804 {
12805 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12806 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12807 ULONGEST signature = dwo_unit->signature;
12808 struct signatured_type *sig_type =
12809 lookup_dwo_signatured_type (per_cu->cu, signature);
12810
12811 if (sig_type != NULL)
12812 {
12813 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12814
12815 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12816 a real dependency of PER_CU on SIG_TYPE. That is detected later
12817 while processing PER_CU. */
12818 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12819 load_full_type_unit (sig_cu);
12820 per_cu->imported_symtabs_push (sig_cu);
12821 }
12822
12823 return 1;
12824 }
12825
12826 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12827 The DWO may have the only definition of the type, though it may not be
12828 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12829 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12830
12831 static void
12832 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12833 {
12834 struct dwo_unit *dwo_unit;
12835 struct dwo_file *dwo_file;
12836
12837 gdb_assert (!per_cu->is_debug_types);
12838 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12839 gdb_assert (per_cu->cu != NULL);
12840
12841 dwo_unit = per_cu->cu->dwo_unit;
12842 gdb_assert (dwo_unit != NULL);
12843
12844 dwo_file = dwo_unit->dwo_file;
12845 if (dwo_file->tus != NULL)
12846 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12847 per_cu);
12848 }
12849
12850 /* Read in various DIEs. */
12851
12852 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12853 Inherit only the children of the DW_AT_abstract_origin DIE not being
12854 already referenced by DW_AT_abstract_origin from the children of the
12855 current DIE. */
12856
12857 static void
12858 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12859 {
12860 struct die_info *child_die;
12861 sect_offset *offsetp;
12862 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12863 struct die_info *origin_die;
12864 /* Iterator of the ORIGIN_DIE children. */
12865 struct die_info *origin_child_die;
12866 struct attribute *attr;
12867 struct dwarf2_cu *origin_cu;
12868 struct pending **origin_previous_list_in_scope;
12869
12870 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12871 if (!attr)
12872 return;
12873
12874 /* Note that following die references may follow to a die in a
12875 different cu. */
12876
12877 origin_cu = cu;
12878 origin_die = follow_die_ref (die, attr, &origin_cu);
12879
12880 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12881 symbols in. */
12882 origin_previous_list_in_scope = origin_cu->list_in_scope;
12883 origin_cu->list_in_scope = cu->list_in_scope;
12884
12885 if (die->tag != origin_die->tag
12886 && !(die->tag == DW_TAG_inlined_subroutine
12887 && origin_die->tag == DW_TAG_subprogram))
12888 complaint (_("DIE %s and its abstract origin %s have different tags"),
12889 sect_offset_str (die->sect_off),
12890 sect_offset_str (origin_die->sect_off));
12891
12892 std::vector<sect_offset> offsets;
12893
12894 for (child_die = die->child;
12895 child_die && child_die->tag;
12896 child_die = child_die->sibling)
12897 {
12898 struct die_info *child_origin_die;
12899 struct dwarf2_cu *child_origin_cu;
12900
12901 /* We are trying to process concrete instance entries:
12902 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12903 it's not relevant to our analysis here. i.e. detecting DIEs that are
12904 present in the abstract instance but not referenced in the concrete
12905 one. */
12906 if (child_die->tag == DW_TAG_call_site
12907 || child_die->tag == DW_TAG_GNU_call_site)
12908 continue;
12909
12910 /* For each CHILD_DIE, find the corresponding child of
12911 ORIGIN_DIE. If there is more than one layer of
12912 DW_AT_abstract_origin, follow them all; there shouldn't be,
12913 but GCC versions at least through 4.4 generate this (GCC PR
12914 40573). */
12915 child_origin_die = child_die;
12916 child_origin_cu = cu;
12917 while (1)
12918 {
12919 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12920 child_origin_cu);
12921 if (attr == NULL)
12922 break;
12923 child_origin_die = follow_die_ref (child_origin_die, attr,
12924 &child_origin_cu);
12925 }
12926
12927 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12928 counterpart may exist. */
12929 if (child_origin_die != child_die)
12930 {
12931 if (child_die->tag != child_origin_die->tag
12932 && !(child_die->tag == DW_TAG_inlined_subroutine
12933 && child_origin_die->tag == DW_TAG_subprogram))
12934 complaint (_("Child DIE %s and its abstract origin %s have "
12935 "different tags"),
12936 sect_offset_str (child_die->sect_off),
12937 sect_offset_str (child_origin_die->sect_off));
12938 if (child_origin_die->parent != origin_die)
12939 complaint (_("Child DIE %s and its abstract origin %s have "
12940 "different parents"),
12941 sect_offset_str (child_die->sect_off),
12942 sect_offset_str (child_origin_die->sect_off));
12943 else
12944 offsets.push_back (child_origin_die->sect_off);
12945 }
12946 }
12947 std::sort (offsets.begin (), offsets.end ());
12948 sect_offset *offsets_end = offsets.data () + offsets.size ();
12949 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12950 if (offsetp[-1] == *offsetp)
12951 complaint (_("Multiple children of DIE %s refer "
12952 "to DIE %s as their abstract origin"),
12953 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12954
12955 offsetp = offsets.data ();
12956 origin_child_die = origin_die->child;
12957 while (origin_child_die && origin_child_die->tag)
12958 {
12959 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12960 while (offsetp < offsets_end
12961 && *offsetp < origin_child_die->sect_off)
12962 offsetp++;
12963 if (offsetp >= offsets_end
12964 || *offsetp > origin_child_die->sect_off)
12965 {
12966 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12967 Check whether we're already processing ORIGIN_CHILD_DIE.
12968 This can happen with mutually referenced abstract_origins.
12969 PR 16581. */
12970 if (!origin_child_die->in_process)
12971 process_die (origin_child_die, origin_cu);
12972 }
12973 origin_child_die = origin_child_die->sibling;
12974 }
12975 origin_cu->list_in_scope = origin_previous_list_in_scope;
12976
12977 if (cu != origin_cu)
12978 compute_delayed_physnames (origin_cu);
12979 }
12980
12981 static void
12982 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12983 {
12984 struct objfile *objfile = cu->per_objfile->objfile;
12985 struct gdbarch *gdbarch = objfile->arch ();
12986 struct context_stack *newobj;
12987 CORE_ADDR lowpc;
12988 CORE_ADDR highpc;
12989 struct die_info *child_die;
12990 struct attribute *attr, *call_line, *call_file;
12991 const char *name;
12992 CORE_ADDR baseaddr;
12993 struct block *block;
12994 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12995 std::vector<struct symbol *> template_args;
12996 struct template_symbol *templ_func = NULL;
12997
12998 if (inlined_func)
12999 {
13000 /* If we do not have call site information, we can't show the
13001 caller of this inlined function. That's too confusing, so
13002 only use the scope for local variables. */
13003 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13004 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13005 if (call_line == NULL || call_file == NULL)
13006 {
13007 read_lexical_block_scope (die, cu);
13008 return;
13009 }
13010 }
13011
13012 baseaddr = objfile->text_section_offset ();
13013
13014 name = dwarf2_name (die, cu);
13015
13016 /* Ignore functions with missing or empty names. These are actually
13017 illegal according to the DWARF standard. */
13018 if (name == NULL)
13019 {
13020 complaint (_("missing name for subprogram DIE at %s"),
13021 sect_offset_str (die->sect_off));
13022 return;
13023 }
13024
13025 /* Ignore functions with missing or invalid low and high pc attributes. */
13026 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13027 <= PC_BOUNDS_INVALID)
13028 {
13029 attr = dwarf2_attr (die, DW_AT_external, cu);
13030 if (!attr || !DW_UNSND (attr))
13031 complaint (_("cannot get low and high bounds "
13032 "for subprogram DIE at %s"),
13033 sect_offset_str (die->sect_off));
13034 return;
13035 }
13036
13037 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13038 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13039
13040 /* If we have any template arguments, then we must allocate a
13041 different sort of symbol. */
13042 for (child_die = die->child; child_die; child_die = child_die->sibling)
13043 {
13044 if (child_die->tag == DW_TAG_template_type_param
13045 || child_die->tag == DW_TAG_template_value_param)
13046 {
13047 templ_func = new (&objfile->objfile_obstack) template_symbol;
13048 templ_func->subclass = SYMBOL_TEMPLATE;
13049 break;
13050 }
13051 }
13052
13053 newobj = cu->get_builder ()->push_context (0, lowpc);
13054 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13055 (struct symbol *) templ_func);
13056
13057 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13058 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13059 cu->language);
13060
13061 /* If there is a location expression for DW_AT_frame_base, record
13062 it. */
13063 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13064 if (attr != nullptr)
13065 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13066
13067 /* If there is a location for the static link, record it. */
13068 newobj->static_link = NULL;
13069 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13070 if (attr != nullptr)
13071 {
13072 newobj->static_link
13073 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13074 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13075 cu->per_cu->addr_type ());
13076 }
13077
13078 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13079
13080 if (die->child != NULL)
13081 {
13082 child_die = die->child;
13083 while (child_die && child_die->tag)
13084 {
13085 if (child_die->tag == DW_TAG_template_type_param
13086 || child_die->tag == DW_TAG_template_value_param)
13087 {
13088 struct symbol *arg = new_symbol (child_die, NULL, cu);
13089
13090 if (arg != NULL)
13091 template_args.push_back (arg);
13092 }
13093 else
13094 process_die (child_die, cu);
13095 child_die = child_die->sibling;
13096 }
13097 }
13098
13099 inherit_abstract_dies (die, cu);
13100
13101 /* If we have a DW_AT_specification, we might need to import using
13102 directives from the context of the specification DIE. See the
13103 comment in determine_prefix. */
13104 if (cu->language == language_cplus
13105 && dwarf2_attr (die, DW_AT_specification, cu))
13106 {
13107 struct dwarf2_cu *spec_cu = cu;
13108 struct die_info *spec_die = die_specification (die, &spec_cu);
13109
13110 while (spec_die)
13111 {
13112 child_die = spec_die->child;
13113 while (child_die && child_die->tag)
13114 {
13115 if (child_die->tag == DW_TAG_imported_module)
13116 process_die (child_die, spec_cu);
13117 child_die = child_die->sibling;
13118 }
13119
13120 /* In some cases, GCC generates specification DIEs that
13121 themselves contain DW_AT_specification attributes. */
13122 spec_die = die_specification (spec_die, &spec_cu);
13123 }
13124 }
13125
13126 struct context_stack cstk = cu->get_builder ()->pop_context ();
13127 /* Make a block for the local symbols within. */
13128 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13129 cstk.static_link, lowpc, highpc);
13130
13131 /* For C++, set the block's scope. */
13132 if ((cu->language == language_cplus
13133 || cu->language == language_fortran
13134 || cu->language == language_d
13135 || cu->language == language_rust)
13136 && cu->processing_has_namespace_info)
13137 block_set_scope (block, determine_prefix (die, cu),
13138 &objfile->objfile_obstack);
13139
13140 /* If we have address ranges, record them. */
13141 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13142
13143 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13144
13145 /* Attach template arguments to function. */
13146 if (!template_args.empty ())
13147 {
13148 gdb_assert (templ_func != NULL);
13149
13150 templ_func->n_template_arguments = template_args.size ();
13151 templ_func->template_arguments
13152 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13153 templ_func->n_template_arguments);
13154 memcpy (templ_func->template_arguments,
13155 template_args.data (),
13156 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13157
13158 /* Make sure that the symtab is set on the new symbols. Even
13159 though they don't appear in this symtab directly, other parts
13160 of gdb assume that symbols do, and this is reasonably
13161 true. */
13162 for (symbol *sym : template_args)
13163 symbol_set_symtab (sym, symbol_symtab (templ_func));
13164 }
13165
13166 /* In C++, we can have functions nested inside functions (e.g., when
13167 a function declares a class that has methods). This means that
13168 when we finish processing a function scope, we may need to go
13169 back to building a containing block's symbol lists. */
13170 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13171 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13172
13173 /* If we've finished processing a top-level function, subsequent
13174 symbols go in the file symbol list. */
13175 if (cu->get_builder ()->outermost_context_p ())
13176 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13177 }
13178
13179 /* Process all the DIES contained within a lexical block scope. Start
13180 a new scope, process the dies, and then close the scope. */
13181
13182 static void
13183 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13184 {
13185 struct objfile *objfile = cu->per_objfile->objfile;
13186 struct gdbarch *gdbarch = objfile->arch ();
13187 CORE_ADDR lowpc, highpc;
13188 struct die_info *child_die;
13189 CORE_ADDR baseaddr;
13190
13191 baseaddr = objfile->text_section_offset ();
13192
13193 /* Ignore blocks with missing or invalid low and high pc attributes. */
13194 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13195 as multiple lexical blocks? Handling children in a sane way would
13196 be nasty. Might be easier to properly extend generic blocks to
13197 describe ranges. */
13198 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13199 {
13200 case PC_BOUNDS_NOT_PRESENT:
13201 /* DW_TAG_lexical_block has no attributes, process its children as if
13202 there was no wrapping by that DW_TAG_lexical_block.
13203 GCC does no longer produces such DWARF since GCC r224161. */
13204 for (child_die = die->child;
13205 child_die != NULL && child_die->tag;
13206 child_die = child_die->sibling)
13207 {
13208 /* We might already be processing this DIE. This can happen
13209 in an unusual circumstance -- where a subroutine A
13210 appears lexically in another subroutine B, but A actually
13211 inlines B. The recursion is broken here, rather than in
13212 inherit_abstract_dies, because it seems better to simply
13213 drop concrete children here. */
13214 if (!child_die->in_process)
13215 process_die (child_die, cu);
13216 }
13217 return;
13218 case PC_BOUNDS_INVALID:
13219 return;
13220 }
13221 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13222 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13223
13224 cu->get_builder ()->push_context (0, lowpc);
13225 if (die->child != NULL)
13226 {
13227 child_die = die->child;
13228 while (child_die && child_die->tag)
13229 {
13230 process_die (child_die, cu);
13231 child_die = child_die->sibling;
13232 }
13233 }
13234 inherit_abstract_dies (die, cu);
13235 struct context_stack cstk = cu->get_builder ()->pop_context ();
13236
13237 if (*cu->get_builder ()->get_local_symbols () != NULL
13238 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13239 {
13240 struct block *block
13241 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13242 cstk.start_addr, highpc);
13243
13244 /* Note that recording ranges after traversing children, as we
13245 do here, means that recording a parent's ranges entails
13246 walking across all its children's ranges as they appear in
13247 the address map, which is quadratic behavior.
13248
13249 It would be nicer to record the parent's ranges before
13250 traversing its children, simply overriding whatever you find
13251 there. But since we don't even decide whether to create a
13252 block until after we've traversed its children, that's hard
13253 to do. */
13254 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13255 }
13256 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13257 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13258 }
13259
13260 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13261
13262 static void
13263 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13264 {
13265 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13266 struct objfile *objfile = per_objfile->objfile;
13267 struct gdbarch *gdbarch = objfile->arch ();
13268 CORE_ADDR pc, baseaddr;
13269 struct attribute *attr;
13270 struct call_site *call_site, call_site_local;
13271 void **slot;
13272 int nparams;
13273 struct die_info *child_die;
13274
13275 baseaddr = objfile->text_section_offset ();
13276
13277 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13278 if (attr == NULL)
13279 {
13280 /* This was a pre-DWARF-5 GNU extension alias
13281 for DW_AT_call_return_pc. */
13282 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13283 }
13284 if (!attr)
13285 {
13286 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13287 "DIE %s [in module %s]"),
13288 sect_offset_str (die->sect_off), objfile_name (objfile));
13289 return;
13290 }
13291 pc = attr->value_as_address () + baseaddr;
13292 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13293
13294 if (cu->call_site_htab == NULL)
13295 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13296 NULL, &objfile->objfile_obstack,
13297 hashtab_obstack_allocate, NULL);
13298 call_site_local.pc = pc;
13299 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13300 if (*slot != NULL)
13301 {
13302 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13303 "DIE %s [in module %s]"),
13304 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13305 objfile_name (objfile));
13306 return;
13307 }
13308
13309 /* Count parameters at the caller. */
13310
13311 nparams = 0;
13312 for (child_die = die->child; child_die && child_die->tag;
13313 child_die = child_die->sibling)
13314 {
13315 if (child_die->tag != DW_TAG_call_site_parameter
13316 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13317 {
13318 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13319 "DW_TAG_call_site child DIE %s [in module %s]"),
13320 child_die->tag, sect_offset_str (child_die->sect_off),
13321 objfile_name (objfile));
13322 continue;
13323 }
13324
13325 nparams++;
13326 }
13327
13328 call_site
13329 = ((struct call_site *)
13330 obstack_alloc (&objfile->objfile_obstack,
13331 sizeof (*call_site)
13332 + (sizeof (*call_site->parameter) * (nparams - 1))));
13333 *slot = call_site;
13334 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13335 call_site->pc = pc;
13336
13337 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13338 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13339 {
13340 struct die_info *func_die;
13341
13342 /* Skip also over DW_TAG_inlined_subroutine. */
13343 for (func_die = die->parent;
13344 func_die && func_die->tag != DW_TAG_subprogram
13345 && func_die->tag != DW_TAG_subroutine_type;
13346 func_die = func_die->parent);
13347
13348 /* DW_AT_call_all_calls is a superset
13349 of DW_AT_call_all_tail_calls. */
13350 if (func_die
13351 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13352 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13353 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13354 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13355 {
13356 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13357 not complete. But keep CALL_SITE for look ups via call_site_htab,
13358 both the initial caller containing the real return address PC and
13359 the final callee containing the current PC of a chain of tail
13360 calls do not need to have the tail call list complete. But any
13361 function candidate for a virtual tail call frame searched via
13362 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13363 determined unambiguously. */
13364 }
13365 else
13366 {
13367 struct type *func_type = NULL;
13368
13369 if (func_die)
13370 func_type = get_die_type (func_die, cu);
13371 if (func_type != NULL)
13372 {
13373 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13374
13375 /* Enlist this call site to the function. */
13376 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13377 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13378 }
13379 else
13380 complaint (_("Cannot find function owning DW_TAG_call_site "
13381 "DIE %s [in module %s]"),
13382 sect_offset_str (die->sect_off), objfile_name (objfile));
13383 }
13384 }
13385
13386 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13387 if (attr == NULL)
13388 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13389 if (attr == NULL)
13390 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13391 if (attr == NULL)
13392 {
13393 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13394 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13395 }
13396 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13397 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13398 /* Keep NULL DWARF_BLOCK. */;
13399 else if (attr->form_is_block ())
13400 {
13401 struct dwarf2_locexpr_baton *dlbaton;
13402
13403 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13404 dlbaton->data = DW_BLOCK (attr)->data;
13405 dlbaton->size = DW_BLOCK (attr)->size;
13406 dlbaton->per_objfile = per_objfile;
13407 dlbaton->per_cu = cu->per_cu;
13408
13409 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13410 }
13411 else if (attr->form_is_ref ())
13412 {
13413 struct dwarf2_cu *target_cu = cu;
13414 struct die_info *target_die;
13415
13416 target_die = follow_die_ref (die, attr, &target_cu);
13417 gdb_assert (target_cu->per_objfile->objfile == objfile);
13418 if (die_is_declaration (target_die, target_cu))
13419 {
13420 const char *target_physname;
13421
13422 /* Prefer the mangled name; otherwise compute the demangled one. */
13423 target_physname = dw2_linkage_name (target_die, target_cu);
13424 if (target_physname == NULL)
13425 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13426 if (target_physname == NULL)
13427 complaint (_("DW_AT_call_target target DIE has invalid "
13428 "physname, for referencing DIE %s [in module %s]"),
13429 sect_offset_str (die->sect_off), objfile_name (objfile));
13430 else
13431 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13432 }
13433 else
13434 {
13435 CORE_ADDR lowpc;
13436
13437 /* DW_AT_entry_pc should be preferred. */
13438 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13439 <= PC_BOUNDS_INVALID)
13440 complaint (_("DW_AT_call_target target DIE has invalid "
13441 "low pc, for referencing DIE %s [in module %s]"),
13442 sect_offset_str (die->sect_off), objfile_name (objfile));
13443 else
13444 {
13445 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13446 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13447 }
13448 }
13449 }
13450 else
13451 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13452 "block nor reference, for DIE %s [in module %s]"),
13453 sect_offset_str (die->sect_off), objfile_name (objfile));
13454
13455 call_site->per_cu = cu->per_cu;
13456
13457 for (child_die = die->child;
13458 child_die && child_die->tag;
13459 child_die = child_die->sibling)
13460 {
13461 struct call_site_parameter *parameter;
13462 struct attribute *loc, *origin;
13463
13464 if (child_die->tag != DW_TAG_call_site_parameter
13465 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13466 {
13467 /* Already printed the complaint above. */
13468 continue;
13469 }
13470
13471 gdb_assert (call_site->parameter_count < nparams);
13472 parameter = &call_site->parameter[call_site->parameter_count];
13473
13474 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13475 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13476 register is contained in DW_AT_call_value. */
13477
13478 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13479 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13480 if (origin == NULL)
13481 {
13482 /* This was a pre-DWARF-5 GNU extension alias
13483 for DW_AT_call_parameter. */
13484 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13485 }
13486 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13487 {
13488 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13489
13490 sect_offset sect_off = origin->get_ref_die_offset ();
13491 if (!cu->header.offset_in_cu_p (sect_off))
13492 {
13493 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13494 binding can be done only inside one CU. Such referenced DIE
13495 therefore cannot be even moved to DW_TAG_partial_unit. */
13496 complaint (_("DW_AT_call_parameter offset is not in CU for "
13497 "DW_TAG_call_site child DIE %s [in module %s]"),
13498 sect_offset_str (child_die->sect_off),
13499 objfile_name (objfile));
13500 continue;
13501 }
13502 parameter->u.param_cu_off
13503 = (cu_offset) (sect_off - cu->header.sect_off);
13504 }
13505 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13506 {
13507 complaint (_("No DW_FORM_block* DW_AT_location for "
13508 "DW_TAG_call_site child DIE %s [in module %s]"),
13509 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13510 continue;
13511 }
13512 else
13513 {
13514 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13515 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13516 if (parameter->u.dwarf_reg != -1)
13517 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13518 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13519 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13520 &parameter->u.fb_offset))
13521 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13522 else
13523 {
13524 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13525 "for DW_FORM_block* DW_AT_location is supported for "
13526 "DW_TAG_call_site child DIE %s "
13527 "[in module %s]"),
13528 sect_offset_str (child_die->sect_off),
13529 objfile_name (objfile));
13530 continue;
13531 }
13532 }
13533
13534 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13535 if (attr == NULL)
13536 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13537 if (attr == NULL || !attr->form_is_block ())
13538 {
13539 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13540 "DW_TAG_call_site child DIE %s [in module %s]"),
13541 sect_offset_str (child_die->sect_off),
13542 objfile_name (objfile));
13543 continue;
13544 }
13545 parameter->value = DW_BLOCK (attr)->data;
13546 parameter->value_size = DW_BLOCK (attr)->size;
13547
13548 /* Parameters are not pre-cleared by memset above. */
13549 parameter->data_value = NULL;
13550 parameter->data_value_size = 0;
13551 call_site->parameter_count++;
13552
13553 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13554 if (attr == NULL)
13555 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13556 if (attr != nullptr)
13557 {
13558 if (!attr->form_is_block ())
13559 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13560 "DW_TAG_call_site child DIE %s [in module %s]"),
13561 sect_offset_str (child_die->sect_off),
13562 objfile_name (objfile));
13563 else
13564 {
13565 parameter->data_value = DW_BLOCK (attr)->data;
13566 parameter->data_value_size = DW_BLOCK (attr)->size;
13567 }
13568 }
13569 }
13570 }
13571
13572 /* Helper function for read_variable. If DIE represents a virtual
13573 table, then return the type of the concrete object that is
13574 associated with the virtual table. Otherwise, return NULL. */
13575
13576 static struct type *
13577 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13578 {
13579 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13580 if (attr == NULL)
13581 return NULL;
13582
13583 /* Find the type DIE. */
13584 struct die_info *type_die = NULL;
13585 struct dwarf2_cu *type_cu = cu;
13586
13587 if (attr->form_is_ref ())
13588 type_die = follow_die_ref (die, attr, &type_cu);
13589 if (type_die == NULL)
13590 return NULL;
13591
13592 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13593 return NULL;
13594 return die_containing_type (type_die, type_cu);
13595 }
13596
13597 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13598
13599 static void
13600 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13601 {
13602 struct rust_vtable_symbol *storage = NULL;
13603
13604 if (cu->language == language_rust)
13605 {
13606 struct type *containing_type = rust_containing_type (die, cu);
13607
13608 if (containing_type != NULL)
13609 {
13610 struct objfile *objfile = cu->per_objfile->objfile;
13611
13612 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13613 storage->concrete_type = containing_type;
13614 storage->subclass = SYMBOL_RUST_VTABLE;
13615 }
13616 }
13617
13618 struct symbol *res = new_symbol (die, NULL, cu, storage);
13619 struct attribute *abstract_origin
13620 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13621 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13622 if (res == NULL && loc && abstract_origin)
13623 {
13624 /* We have a variable without a name, but with a location and an abstract
13625 origin. This may be a concrete instance of an abstract variable
13626 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13627 later. */
13628 struct dwarf2_cu *origin_cu = cu;
13629 struct die_info *origin_die
13630 = follow_die_ref (die, abstract_origin, &origin_cu);
13631 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13632 per_objfile->per_bfd->abstract_to_concrete
13633 [origin_die->sect_off].push_back (die->sect_off);
13634 }
13635 }
13636
13637 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13638 reading .debug_rnglists.
13639 Callback's type should be:
13640 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13641 Return true if the attributes are present and valid, otherwise,
13642 return false. */
13643
13644 template <typename Callback>
13645 static bool
13646 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13647 Callback &&callback)
13648 {
13649 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13650 struct objfile *objfile = dwarf2_per_objfile->objfile;
13651 bfd *obfd = objfile->obfd;
13652 /* Base address selection entry. */
13653 gdb::optional<CORE_ADDR> base;
13654 const gdb_byte *buffer;
13655 CORE_ADDR baseaddr;
13656 bool overflow = false;
13657
13658 base = cu->base_address;
13659
13660 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13661 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13662 {
13663 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13664 offset);
13665 return false;
13666 }
13667 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13668
13669 baseaddr = objfile->text_section_offset ();
13670
13671 while (1)
13672 {
13673 /* Initialize it due to a false compiler warning. */
13674 CORE_ADDR range_beginning = 0, range_end = 0;
13675 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13676 + dwarf2_per_objfile->per_bfd->rnglists.size);
13677 unsigned int bytes_read;
13678
13679 if (buffer == buf_end)
13680 {
13681 overflow = true;
13682 break;
13683 }
13684 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13685 switch (rlet)
13686 {
13687 case DW_RLE_end_of_list:
13688 break;
13689 case DW_RLE_base_address:
13690 if (buffer + cu->header.addr_size > buf_end)
13691 {
13692 overflow = true;
13693 break;
13694 }
13695 base = cu->header.read_address (obfd, buffer, &bytes_read);
13696 buffer += bytes_read;
13697 break;
13698 case DW_RLE_start_length:
13699 if (buffer + cu->header.addr_size > buf_end)
13700 {
13701 overflow = true;
13702 break;
13703 }
13704 range_beginning = cu->header.read_address (obfd, buffer,
13705 &bytes_read);
13706 buffer += bytes_read;
13707 range_end = (range_beginning
13708 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13709 buffer += bytes_read;
13710 if (buffer > buf_end)
13711 {
13712 overflow = true;
13713 break;
13714 }
13715 break;
13716 case DW_RLE_offset_pair:
13717 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13718 buffer += bytes_read;
13719 if (buffer > buf_end)
13720 {
13721 overflow = true;
13722 break;
13723 }
13724 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13725 buffer += bytes_read;
13726 if (buffer > buf_end)
13727 {
13728 overflow = true;
13729 break;
13730 }
13731 break;
13732 case DW_RLE_start_end:
13733 if (buffer + 2 * cu->header.addr_size > buf_end)
13734 {
13735 overflow = true;
13736 break;
13737 }
13738 range_beginning = cu->header.read_address (obfd, buffer,
13739 &bytes_read);
13740 buffer += bytes_read;
13741 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13742 buffer += bytes_read;
13743 break;
13744 default:
13745 complaint (_("Invalid .debug_rnglists data (no base address)"));
13746 return false;
13747 }
13748 if (rlet == DW_RLE_end_of_list || overflow)
13749 break;
13750 if (rlet == DW_RLE_base_address)
13751 continue;
13752
13753 if (!base.has_value ())
13754 {
13755 /* We have no valid base address for the ranges
13756 data. */
13757 complaint (_("Invalid .debug_rnglists data (no base address)"));
13758 return false;
13759 }
13760
13761 if (range_beginning > range_end)
13762 {
13763 /* Inverted range entries are invalid. */
13764 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13765 return false;
13766 }
13767
13768 /* Empty range entries have no effect. */
13769 if (range_beginning == range_end)
13770 continue;
13771
13772 range_beginning += *base;
13773 range_end += *base;
13774
13775 /* A not-uncommon case of bad debug info.
13776 Don't pollute the addrmap with bad data. */
13777 if (range_beginning + baseaddr == 0
13778 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13779 {
13780 complaint (_(".debug_rnglists entry has start address of zero"
13781 " [in module %s]"), objfile_name (objfile));
13782 continue;
13783 }
13784
13785 callback (range_beginning, range_end);
13786 }
13787
13788 if (overflow)
13789 {
13790 complaint (_("Offset %d is not terminated "
13791 "for DW_AT_ranges attribute"),
13792 offset);
13793 return false;
13794 }
13795
13796 return true;
13797 }
13798
13799 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13800 Callback's type should be:
13801 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13802 Return 1 if the attributes are present and valid, otherwise, return 0. */
13803
13804 template <typename Callback>
13805 static int
13806 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13807 Callback &&callback)
13808 {
13809 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13810 struct objfile *objfile = 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 per_objfile->per_bfd->ranges.read (objfile);
13827 if (offset >= 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 = 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 && !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_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 = cu->per_objfile;
13979 struct attribute *attr;
13980 struct attribute *attr_high;
13981 CORE_ADDR low = 0;
13982 CORE_ADDR high = 0;
13983 enum pc_bounds_kind ret;
13984
13985 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13986 if (attr_high)
13987 {
13988 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13989 if (attr != nullptr)
13990 {
13991 low = attr->value_as_address ();
13992 high = attr_high->value_as_address ();
13993 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13994 high += low;
13995 }
13996 else
13997 /* Found high w/o low attribute. */
13998 return PC_BOUNDS_INVALID;
13999
14000 /* Found consecutive range of addresses. */
14001 ret = PC_BOUNDS_HIGH_LOW;
14002 }
14003 else
14004 {
14005 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14006 if (attr != NULL)
14007 {
14008 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14009 We take advantage of the fact that DW_AT_ranges does not appear
14010 in DW_TAG_compile_unit of DWO files. */
14011 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14012 unsigned int ranges_offset = (DW_UNSND (attr)
14013 + (need_ranges_base
14014 ? cu->ranges_base
14015 : 0));
14016
14017 /* Value of the DW_AT_ranges attribute is the offset in the
14018 .debug_ranges section. */
14019 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14020 return PC_BOUNDS_INVALID;
14021 /* Found discontinuous range of addresses. */
14022 ret = PC_BOUNDS_RANGES;
14023 }
14024 else
14025 return PC_BOUNDS_NOT_PRESENT;
14026 }
14027
14028 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14029 if (high <= low)
14030 return PC_BOUNDS_INVALID;
14031
14032 /* When using the GNU linker, .gnu.linkonce. sections are used to
14033 eliminate duplicate copies of functions and vtables and such.
14034 The linker will arbitrarily choose one and discard the others.
14035 The AT_*_pc values for such functions refer to local labels in
14036 these sections. If the section from that file was discarded, the
14037 labels are not in the output, so the relocs get a value of 0.
14038 If this is a discarded function, mark the pc bounds as invalid,
14039 so that GDB will ignore it. */
14040 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14041 return PC_BOUNDS_INVALID;
14042
14043 *lowpc = low;
14044 if (highpc)
14045 *highpc = high;
14046 return ret;
14047 }
14048
14049 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14050 its low and high PC addresses. Do nothing if these addresses could not
14051 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14052 and HIGHPC to the high address if greater than HIGHPC. */
14053
14054 static void
14055 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14056 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14057 struct dwarf2_cu *cu)
14058 {
14059 CORE_ADDR low, high;
14060 struct die_info *child = die->child;
14061
14062 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14063 {
14064 *lowpc = std::min (*lowpc, low);
14065 *highpc = std::max (*highpc, high);
14066 }
14067
14068 /* If the language does not allow nested subprograms (either inside
14069 subprograms or lexical blocks), we're done. */
14070 if (cu->language != language_ada)
14071 return;
14072
14073 /* Check all the children of the given DIE. If it contains nested
14074 subprograms, then check their pc bounds. Likewise, we need to
14075 check lexical blocks as well, as they may also contain subprogram
14076 definitions. */
14077 while (child && child->tag)
14078 {
14079 if (child->tag == DW_TAG_subprogram
14080 || child->tag == DW_TAG_lexical_block)
14081 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14082 child = child->sibling;
14083 }
14084 }
14085
14086 /* Get the low and high pc's represented by the scope DIE, and store
14087 them in *LOWPC and *HIGHPC. If the correct values can't be
14088 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14089
14090 static void
14091 get_scope_pc_bounds (struct die_info *die,
14092 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14093 struct dwarf2_cu *cu)
14094 {
14095 CORE_ADDR best_low = (CORE_ADDR) -1;
14096 CORE_ADDR best_high = (CORE_ADDR) 0;
14097 CORE_ADDR current_low, current_high;
14098
14099 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14100 >= PC_BOUNDS_RANGES)
14101 {
14102 best_low = current_low;
14103 best_high = current_high;
14104 }
14105 else
14106 {
14107 struct die_info *child = die->child;
14108
14109 while (child && child->tag)
14110 {
14111 switch (child->tag) {
14112 case DW_TAG_subprogram:
14113 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14114 break;
14115 case DW_TAG_namespace:
14116 case DW_TAG_module:
14117 /* FIXME: carlton/2004-01-16: Should we do this for
14118 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14119 that current GCC's always emit the DIEs corresponding
14120 to definitions of methods of classes as children of a
14121 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14122 the DIEs giving the declarations, which could be
14123 anywhere). But I don't see any reason why the
14124 standards says that they have to be there. */
14125 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14126
14127 if (current_low != ((CORE_ADDR) -1))
14128 {
14129 best_low = std::min (best_low, current_low);
14130 best_high = std::max (best_high, current_high);
14131 }
14132 break;
14133 default:
14134 /* Ignore. */
14135 break;
14136 }
14137
14138 child = child->sibling;
14139 }
14140 }
14141
14142 *lowpc = best_low;
14143 *highpc = best_high;
14144 }
14145
14146 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14147 in DIE. */
14148
14149 static void
14150 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14151 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14152 {
14153 struct objfile *objfile = cu->per_objfile->objfile;
14154 struct gdbarch *gdbarch = objfile->arch ();
14155 struct attribute *attr;
14156 struct attribute *attr_high;
14157
14158 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14159 if (attr_high)
14160 {
14161 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14162 if (attr != nullptr)
14163 {
14164 CORE_ADDR low = attr->value_as_address ();
14165 CORE_ADDR high = attr_high->value_as_address ();
14166
14167 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14168 high += low;
14169
14170 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14171 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14172 cu->get_builder ()->record_block_range (block, low, high - 1);
14173 }
14174 }
14175
14176 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14177 if (attr != nullptr)
14178 {
14179 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14180 We take advantage of the fact that DW_AT_ranges does not appear
14181 in DW_TAG_compile_unit of DWO files. */
14182 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14183
14184 /* The value of the DW_AT_ranges attribute is the offset of the
14185 address range list in the .debug_ranges section. */
14186 unsigned long offset = (DW_UNSND (attr)
14187 + (need_ranges_base ? cu->ranges_base : 0));
14188
14189 std::vector<blockrange> blockvec;
14190 dwarf2_ranges_process (offset, cu,
14191 [&] (CORE_ADDR start, CORE_ADDR end)
14192 {
14193 start += baseaddr;
14194 end += baseaddr;
14195 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14196 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14197 cu->get_builder ()->record_block_range (block, start, end - 1);
14198 blockvec.emplace_back (start, end);
14199 });
14200
14201 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14202 }
14203 }
14204
14205 /* Check whether the producer field indicates either of GCC < 4.6, or the
14206 Intel C/C++ compiler, and cache the result in CU. */
14207
14208 static void
14209 check_producer (struct dwarf2_cu *cu)
14210 {
14211 int major, minor;
14212
14213 if (cu->producer == NULL)
14214 {
14215 /* For unknown compilers expect their behavior is DWARF version
14216 compliant.
14217
14218 GCC started to support .debug_types sections by -gdwarf-4 since
14219 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14220 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14221 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14222 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14223 }
14224 else if (producer_is_gcc (cu->producer, &major, &minor))
14225 {
14226 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14227 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14228 }
14229 else if (producer_is_icc (cu->producer, &major, &minor))
14230 {
14231 cu->producer_is_icc = true;
14232 cu->producer_is_icc_lt_14 = major < 14;
14233 }
14234 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14235 cu->producer_is_codewarrior = true;
14236 else
14237 {
14238 /* For other non-GCC compilers, expect their behavior is DWARF version
14239 compliant. */
14240 }
14241
14242 cu->checked_producer = true;
14243 }
14244
14245 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14246 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14247 during 4.6.0 experimental. */
14248
14249 static bool
14250 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14251 {
14252 if (!cu->checked_producer)
14253 check_producer (cu);
14254
14255 return cu->producer_is_gxx_lt_4_6;
14256 }
14257
14258
14259 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14260 with incorrect is_stmt attributes. */
14261
14262 static bool
14263 producer_is_codewarrior (struct dwarf2_cu *cu)
14264 {
14265 if (!cu->checked_producer)
14266 check_producer (cu);
14267
14268 return cu->producer_is_codewarrior;
14269 }
14270
14271 /* Return the default accessibility type if it is not overridden by
14272 DW_AT_accessibility. */
14273
14274 static enum dwarf_access_attribute
14275 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14276 {
14277 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14278 {
14279 /* The default DWARF 2 accessibility for members is public, the default
14280 accessibility for inheritance is private. */
14281
14282 if (die->tag != DW_TAG_inheritance)
14283 return DW_ACCESS_public;
14284 else
14285 return DW_ACCESS_private;
14286 }
14287 else
14288 {
14289 /* DWARF 3+ defines the default accessibility a different way. The same
14290 rules apply now for DW_TAG_inheritance as for the members and it only
14291 depends on the container kind. */
14292
14293 if (die->parent->tag == DW_TAG_class_type)
14294 return DW_ACCESS_private;
14295 else
14296 return DW_ACCESS_public;
14297 }
14298 }
14299
14300 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14301 offset. If the attribute was not found return 0, otherwise return
14302 1. If it was found but could not properly be handled, set *OFFSET
14303 to 0. */
14304
14305 static int
14306 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14307 LONGEST *offset)
14308 {
14309 struct attribute *attr;
14310
14311 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14312 if (attr != NULL)
14313 {
14314 *offset = 0;
14315
14316 /* Note that we do not check for a section offset first here.
14317 This is because DW_AT_data_member_location is new in DWARF 4,
14318 so if we see it, we can assume that a constant form is really
14319 a constant and not a section offset. */
14320 if (attr->form_is_constant ())
14321 *offset = attr->constant_value (0);
14322 else if (attr->form_is_section_offset ())
14323 dwarf2_complex_location_expr_complaint ();
14324 else if (attr->form_is_block ())
14325 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14326 else
14327 dwarf2_complex_location_expr_complaint ();
14328
14329 return 1;
14330 }
14331
14332 return 0;
14333 }
14334
14335 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14336
14337 static void
14338 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14339 struct field *field)
14340 {
14341 struct attribute *attr;
14342
14343 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14344 if (attr != NULL)
14345 {
14346 if (attr->form_is_constant ())
14347 {
14348 LONGEST offset = attr->constant_value (0);
14349 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14350 }
14351 else if (attr->form_is_section_offset ())
14352 dwarf2_complex_location_expr_complaint ();
14353 else if (attr->form_is_block ())
14354 {
14355 bool handled;
14356 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14357 if (handled)
14358 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14359 else
14360 {
14361 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14362 struct objfile *objfile = per_objfile->objfile;
14363 struct dwarf2_locexpr_baton *dlbaton
14364 = XOBNEW (&objfile->objfile_obstack,
14365 struct dwarf2_locexpr_baton);
14366 dlbaton->data = DW_BLOCK (attr)->data;
14367 dlbaton->size = DW_BLOCK (attr)->size;
14368 /* When using this baton, we want to compute the address
14369 of the field, not the value. This is why
14370 is_reference is set to false here. */
14371 dlbaton->is_reference = false;
14372 dlbaton->per_objfile = per_objfile;
14373 dlbaton->per_cu = cu->per_cu;
14374
14375 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14376 }
14377 }
14378 else
14379 dwarf2_complex_location_expr_complaint ();
14380 }
14381 }
14382
14383 /* Add an aggregate field to the field list. */
14384
14385 static void
14386 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14387 struct dwarf2_cu *cu)
14388 {
14389 struct objfile *objfile = cu->per_objfile->objfile;
14390 struct gdbarch *gdbarch = objfile->arch ();
14391 struct nextfield *new_field;
14392 struct attribute *attr;
14393 struct field *fp;
14394 const char *fieldname = "";
14395
14396 if (die->tag == DW_TAG_inheritance)
14397 {
14398 fip->baseclasses.emplace_back ();
14399 new_field = &fip->baseclasses.back ();
14400 }
14401 else
14402 {
14403 fip->fields.emplace_back ();
14404 new_field = &fip->fields.back ();
14405 }
14406
14407 new_field->offset = die->sect_off;
14408
14409 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14410 if (attr != nullptr)
14411 new_field->accessibility = DW_UNSND (attr);
14412 else
14413 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14414 if (new_field->accessibility != DW_ACCESS_public)
14415 fip->non_public_fields = 1;
14416
14417 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14418 if (attr != nullptr)
14419 new_field->virtuality = DW_UNSND (attr);
14420 else
14421 new_field->virtuality = DW_VIRTUALITY_none;
14422
14423 fp = &new_field->field;
14424
14425 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14426 {
14427 /* Data member other than a C++ static data member. */
14428
14429 /* Get type of field. */
14430 fp->type = die_type (die, cu);
14431
14432 SET_FIELD_BITPOS (*fp, 0);
14433
14434 /* Get bit size of field (zero if none). */
14435 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14436 if (attr != nullptr)
14437 {
14438 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14439 }
14440 else
14441 {
14442 FIELD_BITSIZE (*fp) = 0;
14443 }
14444
14445 /* Get bit offset of field. */
14446 handle_data_member_location (die, cu, fp);
14447 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14448 if (attr != nullptr)
14449 {
14450 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14451 {
14452 /* For big endian bits, the DW_AT_bit_offset gives the
14453 additional bit offset from the MSB of the containing
14454 anonymous object to the MSB of the field. We don't
14455 have to do anything special since we don't need to
14456 know the size of the anonymous object. */
14457 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14458 }
14459 else
14460 {
14461 /* For little endian bits, compute the bit offset to the
14462 MSB of the anonymous object, subtract off the number of
14463 bits from the MSB of the field to the MSB of the
14464 object, and then subtract off the number of bits of
14465 the field itself. The result is the bit offset of
14466 the LSB of the field. */
14467 int anonymous_size;
14468 int bit_offset = DW_UNSND (attr);
14469
14470 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14471 if (attr != nullptr)
14472 {
14473 /* The size of the anonymous object containing
14474 the bit field is explicit, so use the
14475 indicated size (in bytes). */
14476 anonymous_size = DW_UNSND (attr);
14477 }
14478 else
14479 {
14480 /* The size of the anonymous object containing
14481 the bit field must be inferred from the type
14482 attribute of the data member containing the
14483 bit field. */
14484 anonymous_size = TYPE_LENGTH (fp->type);
14485 }
14486 SET_FIELD_BITPOS (*fp,
14487 (FIELD_BITPOS (*fp)
14488 + anonymous_size * bits_per_byte
14489 - bit_offset - FIELD_BITSIZE (*fp)));
14490 }
14491 }
14492 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14493 if (attr != NULL)
14494 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14495 + attr->constant_value (0)));
14496
14497 /* Get name of field. */
14498 fieldname = dwarf2_name (die, cu);
14499 if (fieldname == NULL)
14500 fieldname = "";
14501
14502 /* The name is already allocated along with this objfile, so we don't
14503 need to duplicate it for the type. */
14504 fp->name = fieldname;
14505
14506 /* Change accessibility for artificial fields (e.g. virtual table
14507 pointer or virtual base class pointer) to private. */
14508 if (dwarf2_attr (die, DW_AT_artificial, cu))
14509 {
14510 FIELD_ARTIFICIAL (*fp) = 1;
14511 new_field->accessibility = DW_ACCESS_private;
14512 fip->non_public_fields = 1;
14513 }
14514 }
14515 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14516 {
14517 /* C++ static member. */
14518
14519 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14520 is a declaration, but all versions of G++ as of this writing
14521 (so through at least 3.2.1) incorrectly generate
14522 DW_TAG_variable tags. */
14523
14524 const char *physname;
14525
14526 /* Get name of field. */
14527 fieldname = dwarf2_name (die, cu);
14528 if (fieldname == NULL)
14529 return;
14530
14531 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14532 if (attr
14533 /* Only create a symbol if this is an external value.
14534 new_symbol checks this and puts the value in the global symbol
14535 table, which we want. If it is not external, new_symbol
14536 will try to put the value in cu->list_in_scope which is wrong. */
14537 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14538 {
14539 /* A static const member, not much different than an enum as far as
14540 we're concerned, except that we can support more types. */
14541 new_symbol (die, NULL, cu);
14542 }
14543
14544 /* Get physical name. */
14545 physname = dwarf2_physname (fieldname, die, cu);
14546
14547 /* The name is already allocated along with this objfile, so we don't
14548 need to duplicate it for the type. */
14549 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14550 FIELD_TYPE (*fp) = die_type (die, cu);
14551 FIELD_NAME (*fp) = fieldname;
14552 }
14553 else if (die->tag == DW_TAG_inheritance)
14554 {
14555 /* C++ base class field. */
14556 handle_data_member_location (die, cu, fp);
14557 FIELD_BITSIZE (*fp) = 0;
14558 FIELD_TYPE (*fp) = die_type (die, cu);
14559 FIELD_NAME (*fp) = fp->type->name ();
14560 }
14561 else
14562 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14563 }
14564
14565 /* Can the type given by DIE define another type? */
14566
14567 static bool
14568 type_can_define_types (const struct die_info *die)
14569 {
14570 switch (die->tag)
14571 {
14572 case DW_TAG_typedef:
14573 case DW_TAG_class_type:
14574 case DW_TAG_structure_type:
14575 case DW_TAG_union_type:
14576 case DW_TAG_enumeration_type:
14577 return true;
14578
14579 default:
14580 return false;
14581 }
14582 }
14583
14584 /* Add a type definition defined in the scope of the FIP's class. */
14585
14586 static void
14587 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14588 struct dwarf2_cu *cu)
14589 {
14590 struct decl_field fp;
14591 memset (&fp, 0, sizeof (fp));
14592
14593 gdb_assert (type_can_define_types (die));
14594
14595 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14596 fp.name = dwarf2_name (die, cu);
14597 fp.type = read_type_die (die, cu);
14598
14599 /* Save accessibility. */
14600 enum dwarf_access_attribute accessibility;
14601 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14602 if (attr != NULL)
14603 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14604 else
14605 accessibility = dwarf2_default_access_attribute (die, cu);
14606 switch (accessibility)
14607 {
14608 case DW_ACCESS_public:
14609 /* The assumed value if neither private nor protected. */
14610 break;
14611 case DW_ACCESS_private:
14612 fp.is_private = 1;
14613 break;
14614 case DW_ACCESS_protected:
14615 fp.is_protected = 1;
14616 break;
14617 default:
14618 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14619 }
14620
14621 if (die->tag == DW_TAG_typedef)
14622 fip->typedef_field_list.push_back (fp);
14623 else
14624 fip->nested_types_list.push_back (fp);
14625 }
14626
14627 /* A convenience typedef that's used when finding the discriminant
14628 field for a variant part. */
14629 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14630 offset_map_type;
14631
14632 /* Compute the discriminant range for a given variant. OBSTACK is
14633 where the results will be stored. VARIANT is the variant to
14634 process. IS_UNSIGNED indicates whether the discriminant is signed
14635 or unsigned. */
14636
14637 static const gdb::array_view<discriminant_range>
14638 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14639 bool is_unsigned)
14640 {
14641 std::vector<discriminant_range> ranges;
14642
14643 if (variant.default_branch)
14644 return {};
14645
14646 if (variant.discr_list_data == nullptr)
14647 {
14648 discriminant_range r
14649 = {variant.discriminant_value, variant.discriminant_value};
14650 ranges.push_back (r);
14651 }
14652 else
14653 {
14654 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14655 variant.discr_list_data->size);
14656 while (!data.empty ())
14657 {
14658 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14659 {
14660 complaint (_("invalid discriminant marker: %d"), data[0]);
14661 break;
14662 }
14663 bool is_range = data[0] == DW_DSC_range;
14664 data = data.slice (1);
14665
14666 ULONGEST low, high;
14667 unsigned int bytes_read;
14668
14669 if (data.empty ())
14670 {
14671 complaint (_("DW_AT_discr_list missing low value"));
14672 break;
14673 }
14674 if (is_unsigned)
14675 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14676 else
14677 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14678 &bytes_read);
14679 data = data.slice (bytes_read);
14680
14681 if (is_range)
14682 {
14683 if (data.empty ())
14684 {
14685 complaint (_("DW_AT_discr_list missing high value"));
14686 break;
14687 }
14688 if (is_unsigned)
14689 high = read_unsigned_leb128 (nullptr, data.data (),
14690 &bytes_read);
14691 else
14692 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14693 &bytes_read);
14694 data = data.slice (bytes_read);
14695 }
14696 else
14697 high = low;
14698
14699 ranges.push_back ({ low, high });
14700 }
14701 }
14702
14703 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14704 ranges.size ());
14705 std::copy (ranges.begin (), ranges.end (), result);
14706 return gdb::array_view<discriminant_range> (result, ranges.size ());
14707 }
14708
14709 static const gdb::array_view<variant_part> create_variant_parts
14710 (struct obstack *obstack,
14711 const offset_map_type &offset_map,
14712 struct field_info *fi,
14713 const std::vector<variant_part_builder> &variant_parts);
14714
14715 /* Fill in a "struct variant" for a given variant field. RESULT is
14716 the variant to fill in. OBSTACK is where any needed allocations
14717 will be done. OFFSET_MAP holds the mapping from section offsets to
14718 fields for the type. FI describes the fields of the type we're
14719 processing. FIELD is the variant field we're converting. */
14720
14721 static void
14722 create_one_variant (variant &result, struct obstack *obstack,
14723 const offset_map_type &offset_map,
14724 struct field_info *fi, const variant_field &field)
14725 {
14726 result.discriminants = convert_variant_range (obstack, field, false);
14727 result.first_field = field.first_field + fi->baseclasses.size ();
14728 result.last_field = field.last_field + fi->baseclasses.size ();
14729 result.parts = create_variant_parts (obstack, offset_map, fi,
14730 field.variant_parts);
14731 }
14732
14733 /* Fill in a "struct variant_part" for a given variant part. RESULT
14734 is the variant part to fill in. OBSTACK is where any needed
14735 allocations will be done. OFFSET_MAP holds the mapping from
14736 section offsets to fields for the type. FI describes the fields of
14737 the type we're processing. BUILDER is the variant part to be
14738 converted. */
14739
14740 static void
14741 create_one_variant_part (variant_part &result,
14742 struct obstack *obstack,
14743 const offset_map_type &offset_map,
14744 struct field_info *fi,
14745 const variant_part_builder &builder)
14746 {
14747 auto iter = offset_map.find (builder.discriminant_offset);
14748 if (iter == offset_map.end ())
14749 {
14750 result.discriminant_index = -1;
14751 /* Doesn't matter. */
14752 result.is_unsigned = false;
14753 }
14754 else
14755 {
14756 result.discriminant_index = iter->second;
14757 result.is_unsigned
14758 = TYPE_UNSIGNED (FIELD_TYPE
14759 (fi->fields[result.discriminant_index].field));
14760 }
14761
14762 size_t n = builder.variants.size ();
14763 variant *output = new (obstack) variant[n];
14764 for (size_t i = 0; i < n; ++i)
14765 create_one_variant (output[i], obstack, offset_map, fi,
14766 builder.variants[i]);
14767
14768 result.variants = gdb::array_view<variant> (output, n);
14769 }
14770
14771 /* Create a vector of variant parts that can be attached to a type.
14772 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14773 holds the mapping from section offsets to fields for the type. FI
14774 describes the fields of the type we're processing. VARIANT_PARTS
14775 is the vector to convert. */
14776
14777 static const gdb::array_view<variant_part>
14778 create_variant_parts (struct obstack *obstack,
14779 const offset_map_type &offset_map,
14780 struct field_info *fi,
14781 const std::vector<variant_part_builder> &variant_parts)
14782 {
14783 if (variant_parts.empty ())
14784 return {};
14785
14786 size_t n = variant_parts.size ();
14787 variant_part *result = new (obstack) variant_part[n];
14788 for (size_t i = 0; i < n; ++i)
14789 create_one_variant_part (result[i], obstack, offset_map, fi,
14790 variant_parts[i]);
14791
14792 return gdb::array_view<variant_part> (result, n);
14793 }
14794
14795 /* Compute the variant part vector for FIP, attaching it to TYPE when
14796 done. */
14797
14798 static void
14799 add_variant_property (struct field_info *fip, struct type *type,
14800 struct dwarf2_cu *cu)
14801 {
14802 /* Map section offsets of fields to their field index. Note the
14803 field index here does not take the number of baseclasses into
14804 account. */
14805 offset_map_type offset_map;
14806 for (int i = 0; i < fip->fields.size (); ++i)
14807 offset_map[fip->fields[i].offset] = i;
14808
14809 struct objfile *objfile = cu->per_objfile->objfile;
14810 gdb::array_view<variant_part> parts
14811 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14812 fip->variant_parts);
14813
14814 struct dynamic_prop prop;
14815 prop.kind = PROP_VARIANT_PARTS;
14816 prop.data.variant_parts
14817 = ((gdb::array_view<variant_part> *)
14818 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14819
14820 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14821 }
14822
14823 /* Create the vector of fields, and attach it to the type. */
14824
14825 static void
14826 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14827 struct dwarf2_cu *cu)
14828 {
14829 int nfields = fip->nfields ();
14830
14831 /* Record the field count, allocate space for the array of fields,
14832 and create blank accessibility bitfields if necessary. */
14833 type->set_num_fields (nfields);
14834 type->set_fields
14835 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14836
14837 if (fip->non_public_fields && cu->language != language_ada)
14838 {
14839 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14840
14841 TYPE_FIELD_PRIVATE_BITS (type) =
14842 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14843 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14844
14845 TYPE_FIELD_PROTECTED_BITS (type) =
14846 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14847 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14848
14849 TYPE_FIELD_IGNORE_BITS (type) =
14850 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14851 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14852 }
14853
14854 /* If the type has baseclasses, allocate and clear a bit vector for
14855 TYPE_FIELD_VIRTUAL_BITS. */
14856 if (!fip->baseclasses.empty () && cu->language != language_ada)
14857 {
14858 int num_bytes = B_BYTES (fip->baseclasses.size ());
14859 unsigned char *pointer;
14860
14861 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14862 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14863 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14864 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14865 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14866 }
14867
14868 if (!fip->variant_parts.empty ())
14869 add_variant_property (fip, type, cu);
14870
14871 /* Copy the saved-up fields into the field vector. */
14872 for (int i = 0; i < nfields; ++i)
14873 {
14874 struct nextfield &field
14875 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14876 : fip->fields[i - fip->baseclasses.size ()]);
14877
14878 type->field (i) = field.field;
14879 switch (field.accessibility)
14880 {
14881 case DW_ACCESS_private:
14882 if (cu->language != language_ada)
14883 SET_TYPE_FIELD_PRIVATE (type, i);
14884 break;
14885
14886 case DW_ACCESS_protected:
14887 if (cu->language != language_ada)
14888 SET_TYPE_FIELD_PROTECTED (type, i);
14889 break;
14890
14891 case DW_ACCESS_public:
14892 break;
14893
14894 default:
14895 /* Unknown accessibility. Complain and treat it as public. */
14896 {
14897 complaint (_("unsupported accessibility %d"),
14898 field.accessibility);
14899 }
14900 break;
14901 }
14902 if (i < fip->baseclasses.size ())
14903 {
14904 switch (field.virtuality)
14905 {
14906 case DW_VIRTUALITY_virtual:
14907 case DW_VIRTUALITY_pure_virtual:
14908 if (cu->language == language_ada)
14909 error (_("unexpected virtuality in component of Ada type"));
14910 SET_TYPE_FIELD_VIRTUAL (type, i);
14911 break;
14912 }
14913 }
14914 }
14915 }
14916
14917 /* Return true if this member function is a constructor, false
14918 otherwise. */
14919
14920 static int
14921 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14922 {
14923 const char *fieldname;
14924 const char *type_name;
14925 int len;
14926
14927 if (die->parent == NULL)
14928 return 0;
14929
14930 if (die->parent->tag != DW_TAG_structure_type
14931 && die->parent->tag != DW_TAG_union_type
14932 && die->parent->tag != DW_TAG_class_type)
14933 return 0;
14934
14935 fieldname = dwarf2_name (die, cu);
14936 type_name = dwarf2_name (die->parent, cu);
14937 if (fieldname == NULL || type_name == NULL)
14938 return 0;
14939
14940 len = strlen (fieldname);
14941 return (strncmp (fieldname, type_name, len) == 0
14942 && (type_name[len] == '\0' || type_name[len] == '<'));
14943 }
14944
14945 /* Check if the given VALUE is a recognized enum
14946 dwarf_defaulted_attribute constant according to DWARF5 spec,
14947 Table 7.24. */
14948
14949 static bool
14950 is_valid_DW_AT_defaulted (ULONGEST value)
14951 {
14952 switch (value)
14953 {
14954 case DW_DEFAULTED_no:
14955 case DW_DEFAULTED_in_class:
14956 case DW_DEFAULTED_out_of_class:
14957 return true;
14958 }
14959
14960 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14961 return false;
14962 }
14963
14964 /* Add a member function to the proper fieldlist. */
14965
14966 static void
14967 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14968 struct type *type, struct dwarf2_cu *cu)
14969 {
14970 struct objfile *objfile = cu->per_objfile->objfile;
14971 struct attribute *attr;
14972 int i;
14973 struct fnfieldlist *flp = nullptr;
14974 struct fn_field *fnp;
14975 const char *fieldname;
14976 struct type *this_type;
14977 enum dwarf_access_attribute accessibility;
14978
14979 if (cu->language == language_ada)
14980 error (_("unexpected member function in Ada type"));
14981
14982 /* Get name of member function. */
14983 fieldname = dwarf2_name (die, cu);
14984 if (fieldname == NULL)
14985 return;
14986
14987 /* Look up member function name in fieldlist. */
14988 for (i = 0; i < fip->fnfieldlists.size (); i++)
14989 {
14990 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14991 {
14992 flp = &fip->fnfieldlists[i];
14993 break;
14994 }
14995 }
14996
14997 /* Create a new fnfieldlist if necessary. */
14998 if (flp == nullptr)
14999 {
15000 fip->fnfieldlists.emplace_back ();
15001 flp = &fip->fnfieldlists.back ();
15002 flp->name = fieldname;
15003 i = fip->fnfieldlists.size () - 1;
15004 }
15005
15006 /* Create a new member function field and add it to the vector of
15007 fnfieldlists. */
15008 flp->fnfields.emplace_back ();
15009 fnp = &flp->fnfields.back ();
15010
15011 /* Delay processing of the physname until later. */
15012 if (cu->language == language_cplus)
15013 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15014 die, cu);
15015 else
15016 {
15017 const char *physname = dwarf2_physname (fieldname, die, cu);
15018 fnp->physname = physname ? physname : "";
15019 }
15020
15021 fnp->type = alloc_type (objfile);
15022 this_type = read_type_die (die, cu);
15023 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15024 {
15025 int nparams = this_type->num_fields ();
15026
15027 /* TYPE is the domain of this method, and THIS_TYPE is the type
15028 of the method itself (TYPE_CODE_METHOD). */
15029 smash_to_method_type (fnp->type, type,
15030 TYPE_TARGET_TYPE (this_type),
15031 this_type->fields (),
15032 this_type->num_fields (),
15033 TYPE_VARARGS (this_type));
15034
15035 /* Handle static member functions.
15036 Dwarf2 has no clean way to discern C++ static and non-static
15037 member functions. G++ helps GDB by marking the first
15038 parameter for non-static member functions (which is the this
15039 pointer) as artificial. We obtain this information from
15040 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15041 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15042 fnp->voffset = VOFFSET_STATIC;
15043 }
15044 else
15045 complaint (_("member function type missing for '%s'"),
15046 dwarf2_full_name (fieldname, die, cu));
15047
15048 /* Get fcontext from DW_AT_containing_type if present. */
15049 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15050 fnp->fcontext = die_containing_type (die, cu);
15051
15052 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15053 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15054
15055 /* Get accessibility. */
15056 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15057 if (attr != nullptr)
15058 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15059 else
15060 accessibility = dwarf2_default_access_attribute (die, cu);
15061 switch (accessibility)
15062 {
15063 case DW_ACCESS_private:
15064 fnp->is_private = 1;
15065 break;
15066 case DW_ACCESS_protected:
15067 fnp->is_protected = 1;
15068 break;
15069 }
15070
15071 /* Check for artificial methods. */
15072 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15073 if (attr && DW_UNSND (attr) != 0)
15074 fnp->is_artificial = 1;
15075
15076 /* Check for defaulted methods. */
15077 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15078 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15079 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15080
15081 /* Check for deleted methods. */
15082 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15083 if (attr != nullptr && DW_UNSND (attr) != 0)
15084 fnp->is_deleted = 1;
15085
15086 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15087
15088 /* Get index in virtual function table if it is a virtual member
15089 function. For older versions of GCC, this is an offset in the
15090 appropriate virtual table, as specified by DW_AT_containing_type.
15091 For everyone else, it is an expression to be evaluated relative
15092 to the object address. */
15093
15094 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15095 if (attr != nullptr)
15096 {
15097 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15098 {
15099 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15100 {
15101 /* Old-style GCC. */
15102 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15103 }
15104 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15105 || (DW_BLOCK (attr)->size > 1
15106 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15107 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15108 {
15109 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15110 if ((fnp->voffset % cu->header.addr_size) != 0)
15111 dwarf2_complex_location_expr_complaint ();
15112 else
15113 fnp->voffset /= cu->header.addr_size;
15114 fnp->voffset += 2;
15115 }
15116 else
15117 dwarf2_complex_location_expr_complaint ();
15118
15119 if (!fnp->fcontext)
15120 {
15121 /* If there is no `this' field and no DW_AT_containing_type,
15122 we cannot actually find a base class context for the
15123 vtable! */
15124 if (this_type->num_fields () == 0
15125 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15126 {
15127 complaint (_("cannot determine context for virtual member "
15128 "function \"%s\" (offset %s)"),
15129 fieldname, sect_offset_str (die->sect_off));
15130 }
15131 else
15132 {
15133 fnp->fcontext
15134 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15135 }
15136 }
15137 }
15138 else if (attr->form_is_section_offset ())
15139 {
15140 dwarf2_complex_location_expr_complaint ();
15141 }
15142 else
15143 {
15144 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15145 fieldname);
15146 }
15147 }
15148 else
15149 {
15150 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15151 if (attr && DW_UNSND (attr))
15152 {
15153 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15154 complaint (_("Member function \"%s\" (offset %s) is virtual "
15155 "but the vtable offset is not specified"),
15156 fieldname, sect_offset_str (die->sect_off));
15157 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15158 TYPE_CPLUS_DYNAMIC (type) = 1;
15159 }
15160 }
15161 }
15162
15163 /* Create the vector of member function fields, and attach it to the type. */
15164
15165 static void
15166 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15167 struct dwarf2_cu *cu)
15168 {
15169 if (cu->language == language_ada)
15170 error (_("unexpected member functions in Ada type"));
15171
15172 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15173 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15174 TYPE_ALLOC (type,
15175 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15176
15177 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15178 {
15179 struct fnfieldlist &nf = fip->fnfieldlists[i];
15180 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15181
15182 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15183 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15184 fn_flp->fn_fields = (struct fn_field *)
15185 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15186
15187 for (int k = 0; k < nf.fnfields.size (); ++k)
15188 fn_flp->fn_fields[k] = nf.fnfields[k];
15189 }
15190
15191 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15192 }
15193
15194 /* Returns non-zero if NAME is the name of a vtable member in CU's
15195 language, zero otherwise. */
15196 static int
15197 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15198 {
15199 static const char vptr[] = "_vptr";
15200
15201 /* Look for the C++ form of the vtable. */
15202 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15203 return 1;
15204
15205 return 0;
15206 }
15207
15208 /* GCC outputs unnamed structures that are really pointers to member
15209 functions, with the ABI-specified layout. If TYPE describes
15210 such a structure, smash it into a member function type.
15211
15212 GCC shouldn't do this; it should just output pointer to member DIEs.
15213 This is GCC PR debug/28767. */
15214
15215 static void
15216 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15217 {
15218 struct type *pfn_type, *self_type, *new_type;
15219
15220 /* Check for a structure with no name and two children. */
15221 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15222 return;
15223
15224 /* Check for __pfn and __delta members. */
15225 if (TYPE_FIELD_NAME (type, 0) == NULL
15226 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15227 || TYPE_FIELD_NAME (type, 1) == NULL
15228 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15229 return;
15230
15231 /* Find the type of the method. */
15232 pfn_type = TYPE_FIELD_TYPE (type, 0);
15233 if (pfn_type == NULL
15234 || pfn_type->code () != TYPE_CODE_PTR
15235 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15236 return;
15237
15238 /* Look for the "this" argument. */
15239 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15240 if (pfn_type->num_fields () == 0
15241 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15242 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15243 return;
15244
15245 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15246 new_type = alloc_type (objfile);
15247 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15248 pfn_type->fields (), pfn_type->num_fields (),
15249 TYPE_VARARGS (pfn_type));
15250 smash_to_methodptr_type (type, new_type);
15251 }
15252
15253 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15254 appropriate error checking and issuing complaints if there is a
15255 problem. */
15256
15257 static ULONGEST
15258 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15259 {
15260 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15261
15262 if (attr == nullptr)
15263 return 0;
15264
15265 if (!attr->form_is_constant ())
15266 {
15267 complaint (_("DW_AT_alignment must have constant form"
15268 " - DIE at %s [in module %s]"),
15269 sect_offset_str (die->sect_off),
15270 objfile_name (cu->per_objfile->objfile));
15271 return 0;
15272 }
15273
15274 ULONGEST align;
15275 if (attr->form == DW_FORM_sdata)
15276 {
15277 LONGEST val = DW_SND (attr);
15278 if (val < 0)
15279 {
15280 complaint (_("DW_AT_alignment value must not be negative"
15281 " - DIE at %s [in module %s]"),
15282 sect_offset_str (die->sect_off),
15283 objfile_name (cu->per_objfile->objfile));
15284 return 0;
15285 }
15286 align = val;
15287 }
15288 else
15289 align = DW_UNSND (attr);
15290
15291 if (align == 0)
15292 {
15293 complaint (_("DW_AT_alignment value must not be zero"
15294 " - DIE at %s [in module %s]"),
15295 sect_offset_str (die->sect_off),
15296 objfile_name (cu->per_objfile->objfile));
15297 return 0;
15298 }
15299 if ((align & (align - 1)) != 0)
15300 {
15301 complaint (_("DW_AT_alignment value must be a power of 2"
15302 " - DIE at %s [in module %s]"),
15303 sect_offset_str (die->sect_off),
15304 objfile_name (cu->per_objfile->objfile));
15305 return 0;
15306 }
15307
15308 return align;
15309 }
15310
15311 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15312 the alignment for TYPE. */
15313
15314 static void
15315 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15316 struct type *type)
15317 {
15318 if (!set_type_align (type, get_alignment (cu, die)))
15319 complaint (_("DW_AT_alignment value too large"
15320 " - DIE at %s [in module %s]"),
15321 sect_offset_str (die->sect_off),
15322 objfile_name (cu->per_objfile->objfile));
15323 }
15324
15325 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15326 constant for a type, according to DWARF5 spec, Table 5.5. */
15327
15328 static bool
15329 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15330 {
15331 switch (value)
15332 {
15333 case DW_CC_normal:
15334 case DW_CC_pass_by_reference:
15335 case DW_CC_pass_by_value:
15336 return true;
15337
15338 default:
15339 complaint (_("unrecognized DW_AT_calling_convention value "
15340 "(%s) for a type"), pulongest (value));
15341 return false;
15342 }
15343 }
15344
15345 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15346 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15347 also according to GNU-specific values (see include/dwarf2.h). */
15348
15349 static bool
15350 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15351 {
15352 switch (value)
15353 {
15354 case DW_CC_normal:
15355 case DW_CC_program:
15356 case DW_CC_nocall:
15357 return true;
15358
15359 case DW_CC_GNU_renesas_sh:
15360 case DW_CC_GNU_borland_fastcall_i386:
15361 case DW_CC_GDB_IBM_OpenCL:
15362 return true;
15363
15364 default:
15365 complaint (_("unrecognized DW_AT_calling_convention value "
15366 "(%s) for a subroutine"), pulongest (value));
15367 return false;
15368 }
15369 }
15370
15371 /* Called when we find the DIE that starts a structure or union scope
15372 (definition) to create a type for the structure or union. Fill in
15373 the type's name and general properties; the members will not be
15374 processed until process_structure_scope. A symbol table entry for
15375 the type will also not be done until process_structure_scope (assuming
15376 the type has a name).
15377
15378 NOTE: we need to call these functions regardless of whether or not the
15379 DIE has a DW_AT_name attribute, since it might be an anonymous
15380 structure or union. This gets the type entered into our set of
15381 user defined types. */
15382
15383 static struct type *
15384 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15385 {
15386 struct objfile *objfile = cu->per_objfile->objfile;
15387 struct type *type;
15388 struct attribute *attr;
15389 const char *name;
15390
15391 /* If the definition of this type lives in .debug_types, read that type.
15392 Don't follow DW_AT_specification though, that will take us back up
15393 the chain and we want to go down. */
15394 attr = die->attr (DW_AT_signature);
15395 if (attr != nullptr)
15396 {
15397 type = get_DW_AT_signature_type (die, attr, cu);
15398
15399 /* The type's CU may not be the same as CU.
15400 Ensure TYPE is recorded with CU in die_type_hash. */
15401 return set_die_type (die, type, cu);
15402 }
15403
15404 type = alloc_type (objfile);
15405 INIT_CPLUS_SPECIFIC (type);
15406
15407 name = dwarf2_name (die, cu);
15408 if (name != NULL)
15409 {
15410 if (cu->language == language_cplus
15411 || cu->language == language_d
15412 || cu->language == language_rust)
15413 {
15414 const char *full_name = dwarf2_full_name (name, die, cu);
15415
15416 /* dwarf2_full_name might have already finished building the DIE's
15417 type. If so, there is no need to continue. */
15418 if (get_die_type (die, cu) != NULL)
15419 return get_die_type (die, cu);
15420
15421 type->set_name (full_name);
15422 }
15423 else
15424 {
15425 /* The name is already allocated along with this objfile, so
15426 we don't need to duplicate it for the type. */
15427 type->set_name (name);
15428 }
15429 }
15430
15431 if (die->tag == DW_TAG_structure_type)
15432 {
15433 type->set_code (TYPE_CODE_STRUCT);
15434 }
15435 else if (die->tag == DW_TAG_union_type)
15436 {
15437 type->set_code (TYPE_CODE_UNION);
15438 }
15439 else
15440 {
15441 type->set_code (TYPE_CODE_STRUCT);
15442 }
15443
15444 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15445 TYPE_DECLARED_CLASS (type) = 1;
15446
15447 /* Store the calling convention in the type if it's available in
15448 the die. Otherwise the calling convention remains set to
15449 the default value DW_CC_normal. */
15450 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15451 if (attr != nullptr
15452 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15453 {
15454 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15455 TYPE_CPLUS_CALLING_CONVENTION (type)
15456 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15457 }
15458
15459 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15460 if (attr != nullptr)
15461 {
15462 if (attr->form_is_constant ())
15463 TYPE_LENGTH (type) = DW_UNSND (attr);
15464 else
15465 {
15466 struct dynamic_prop prop;
15467 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15468 cu->per_cu->addr_type ()))
15469 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15470 TYPE_LENGTH (type) = 0;
15471 }
15472 }
15473 else
15474 {
15475 TYPE_LENGTH (type) = 0;
15476 }
15477
15478 maybe_set_alignment (cu, die, type);
15479
15480 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15481 {
15482 /* ICC<14 does not output the required DW_AT_declaration on
15483 incomplete types, but gives them a size of zero. */
15484 TYPE_STUB (type) = 1;
15485 }
15486 else
15487 TYPE_STUB_SUPPORTED (type) = 1;
15488
15489 if (die_is_declaration (die, cu))
15490 TYPE_STUB (type) = 1;
15491 else if (attr == NULL && die->child == NULL
15492 && producer_is_realview (cu->producer))
15493 /* RealView does not output the required DW_AT_declaration
15494 on incomplete types. */
15495 TYPE_STUB (type) = 1;
15496
15497 /* We need to add the type field to the die immediately so we don't
15498 infinitely recurse when dealing with pointers to the structure
15499 type within the structure itself. */
15500 set_die_type (die, type, cu);
15501
15502 /* set_die_type should be already done. */
15503 set_descriptive_type (type, die, cu);
15504
15505 return type;
15506 }
15507
15508 static void handle_struct_member_die
15509 (struct die_info *child_die,
15510 struct type *type,
15511 struct field_info *fi,
15512 std::vector<struct symbol *> *template_args,
15513 struct dwarf2_cu *cu);
15514
15515 /* A helper for handle_struct_member_die that handles
15516 DW_TAG_variant_part. */
15517
15518 static void
15519 handle_variant_part (struct die_info *die, struct type *type,
15520 struct field_info *fi,
15521 std::vector<struct symbol *> *template_args,
15522 struct dwarf2_cu *cu)
15523 {
15524 variant_part_builder *new_part;
15525 if (fi->current_variant_part == nullptr)
15526 {
15527 fi->variant_parts.emplace_back ();
15528 new_part = &fi->variant_parts.back ();
15529 }
15530 else if (!fi->current_variant_part->processing_variant)
15531 {
15532 complaint (_("nested DW_TAG_variant_part seen "
15533 "- DIE at %s [in module %s]"),
15534 sect_offset_str (die->sect_off),
15535 objfile_name (cu->per_objfile->objfile));
15536 return;
15537 }
15538 else
15539 {
15540 variant_field &current = fi->current_variant_part->variants.back ();
15541 current.variant_parts.emplace_back ();
15542 new_part = &current.variant_parts.back ();
15543 }
15544
15545 /* When we recurse, we want callees to add to this new variant
15546 part. */
15547 scoped_restore save_current_variant_part
15548 = make_scoped_restore (&fi->current_variant_part, new_part);
15549
15550 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15551 if (discr == NULL)
15552 {
15553 /* It's a univariant form, an extension we support. */
15554 }
15555 else if (discr->form_is_ref ())
15556 {
15557 struct dwarf2_cu *target_cu = cu;
15558 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15559
15560 new_part->discriminant_offset = target_die->sect_off;
15561 }
15562 else
15563 {
15564 complaint (_("DW_AT_discr does not have DIE reference form"
15565 " - DIE at %s [in module %s]"),
15566 sect_offset_str (die->sect_off),
15567 objfile_name (cu->per_objfile->objfile));
15568 }
15569
15570 for (die_info *child_die = die->child;
15571 child_die != NULL;
15572 child_die = child_die->sibling)
15573 handle_struct_member_die (child_die, type, fi, template_args, cu);
15574 }
15575
15576 /* A helper for handle_struct_member_die that handles
15577 DW_TAG_variant. */
15578
15579 static void
15580 handle_variant (struct die_info *die, struct type *type,
15581 struct field_info *fi,
15582 std::vector<struct symbol *> *template_args,
15583 struct dwarf2_cu *cu)
15584 {
15585 if (fi->current_variant_part == nullptr)
15586 {
15587 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15588 "- DIE at %s [in module %s]"),
15589 sect_offset_str (die->sect_off),
15590 objfile_name (cu->per_objfile->objfile));
15591 return;
15592 }
15593 if (fi->current_variant_part->processing_variant)
15594 {
15595 complaint (_("nested DW_TAG_variant seen "
15596 "- DIE at %s [in module %s]"),
15597 sect_offset_str (die->sect_off),
15598 objfile_name (cu->per_objfile->objfile));
15599 return;
15600 }
15601
15602 scoped_restore save_processing_variant
15603 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15604 true);
15605
15606 fi->current_variant_part->variants.emplace_back ();
15607 variant_field &variant = fi->current_variant_part->variants.back ();
15608 variant.first_field = fi->fields.size ();
15609
15610 /* In a variant we want to get the discriminant and also add a
15611 field for our sole member child. */
15612 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15613 if (discr == nullptr)
15614 {
15615 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15616 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15617 variant.default_branch = true;
15618 else
15619 variant.discr_list_data = DW_BLOCK (discr);
15620 }
15621 else
15622 variant.discriminant_value = DW_UNSND (discr);
15623
15624 for (die_info *variant_child = die->child;
15625 variant_child != NULL;
15626 variant_child = variant_child->sibling)
15627 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15628
15629 variant.last_field = fi->fields.size ();
15630 }
15631
15632 /* A helper for process_structure_scope that handles a single member
15633 DIE. */
15634
15635 static void
15636 handle_struct_member_die (struct die_info *child_die, struct type *type,
15637 struct field_info *fi,
15638 std::vector<struct symbol *> *template_args,
15639 struct dwarf2_cu *cu)
15640 {
15641 if (child_die->tag == DW_TAG_member
15642 || child_die->tag == DW_TAG_variable)
15643 {
15644 /* NOTE: carlton/2002-11-05: A C++ static data member
15645 should be a DW_TAG_member that is a declaration, but
15646 all versions of G++ as of this writing (so through at
15647 least 3.2.1) incorrectly generate DW_TAG_variable
15648 tags for them instead. */
15649 dwarf2_add_field (fi, child_die, cu);
15650 }
15651 else if (child_die->tag == DW_TAG_subprogram)
15652 {
15653 /* Rust doesn't have member functions in the C++ sense.
15654 However, it does emit ordinary functions as children
15655 of a struct DIE. */
15656 if (cu->language == language_rust)
15657 read_func_scope (child_die, cu);
15658 else
15659 {
15660 /* C++ member function. */
15661 dwarf2_add_member_fn (fi, child_die, type, cu);
15662 }
15663 }
15664 else if (child_die->tag == DW_TAG_inheritance)
15665 {
15666 /* C++ base class field. */
15667 dwarf2_add_field (fi, child_die, cu);
15668 }
15669 else if (type_can_define_types (child_die))
15670 dwarf2_add_type_defn (fi, child_die, cu);
15671 else if (child_die->tag == DW_TAG_template_type_param
15672 || child_die->tag == DW_TAG_template_value_param)
15673 {
15674 struct symbol *arg = new_symbol (child_die, NULL, cu);
15675
15676 if (arg != NULL)
15677 template_args->push_back (arg);
15678 }
15679 else if (child_die->tag == DW_TAG_variant_part)
15680 handle_variant_part (child_die, type, fi, template_args, cu);
15681 else if (child_die->tag == DW_TAG_variant)
15682 handle_variant (child_die, type, fi, template_args, cu);
15683 }
15684
15685 /* Finish creating a structure or union type, including filling in
15686 its members and creating a symbol for it. */
15687
15688 static void
15689 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15690 {
15691 struct objfile *objfile = cu->per_objfile->objfile;
15692 struct die_info *child_die;
15693 struct type *type;
15694
15695 type = get_die_type (die, cu);
15696 if (type == NULL)
15697 type = read_structure_type (die, cu);
15698
15699 bool has_template_parameters = false;
15700 if (die->child != NULL && ! die_is_declaration (die, cu))
15701 {
15702 struct field_info fi;
15703 std::vector<struct symbol *> template_args;
15704
15705 child_die = die->child;
15706
15707 while (child_die && child_die->tag)
15708 {
15709 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15710 child_die = child_die->sibling;
15711 }
15712
15713 /* Attach template arguments to type. */
15714 if (!template_args.empty ())
15715 {
15716 has_template_parameters = true;
15717 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15718 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15719 TYPE_TEMPLATE_ARGUMENTS (type)
15720 = XOBNEWVEC (&objfile->objfile_obstack,
15721 struct symbol *,
15722 TYPE_N_TEMPLATE_ARGUMENTS (type));
15723 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15724 template_args.data (),
15725 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15726 * sizeof (struct symbol *)));
15727 }
15728
15729 /* Attach fields and member functions to the type. */
15730 if (fi.nfields () > 0)
15731 dwarf2_attach_fields_to_type (&fi, type, cu);
15732 if (!fi.fnfieldlists.empty ())
15733 {
15734 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15735
15736 /* Get the type which refers to the base class (possibly this
15737 class itself) which contains the vtable pointer for the current
15738 class from the DW_AT_containing_type attribute. This use of
15739 DW_AT_containing_type is a GNU extension. */
15740
15741 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15742 {
15743 struct type *t = die_containing_type (die, cu);
15744
15745 set_type_vptr_basetype (type, t);
15746 if (type == t)
15747 {
15748 int i;
15749
15750 /* Our own class provides vtbl ptr. */
15751 for (i = t->num_fields () - 1;
15752 i >= TYPE_N_BASECLASSES (t);
15753 --i)
15754 {
15755 const char *fieldname = TYPE_FIELD_NAME (t, i);
15756
15757 if (is_vtable_name (fieldname, cu))
15758 {
15759 set_type_vptr_fieldno (type, i);
15760 break;
15761 }
15762 }
15763
15764 /* Complain if virtual function table field not found. */
15765 if (i < TYPE_N_BASECLASSES (t))
15766 complaint (_("virtual function table pointer "
15767 "not found when defining class '%s'"),
15768 type->name () ? type->name () : "");
15769 }
15770 else
15771 {
15772 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15773 }
15774 }
15775 else if (cu->producer
15776 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15777 {
15778 /* The IBM XLC compiler does not provide direct indication
15779 of the containing type, but the vtable pointer is
15780 always named __vfp. */
15781
15782 int i;
15783
15784 for (i = type->num_fields () - 1;
15785 i >= TYPE_N_BASECLASSES (type);
15786 --i)
15787 {
15788 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15789 {
15790 set_type_vptr_fieldno (type, i);
15791 set_type_vptr_basetype (type, type);
15792 break;
15793 }
15794 }
15795 }
15796 }
15797
15798 /* Copy fi.typedef_field_list linked list elements content into the
15799 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15800 if (!fi.typedef_field_list.empty ())
15801 {
15802 int count = fi.typedef_field_list.size ();
15803
15804 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15805 TYPE_TYPEDEF_FIELD_ARRAY (type)
15806 = ((struct decl_field *)
15807 TYPE_ALLOC (type,
15808 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15809 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15810
15811 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15812 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15813 }
15814
15815 /* Copy fi.nested_types_list linked list elements content into the
15816 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15817 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15818 {
15819 int count = fi.nested_types_list.size ();
15820
15821 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15822 TYPE_NESTED_TYPES_ARRAY (type)
15823 = ((struct decl_field *)
15824 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15825 TYPE_NESTED_TYPES_COUNT (type) = count;
15826
15827 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15828 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15829 }
15830 }
15831
15832 quirk_gcc_member_function_pointer (type, objfile);
15833 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15834 cu->rust_unions.push_back (type);
15835
15836 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15837 snapshots) has been known to create a die giving a declaration
15838 for a class that has, as a child, a die giving a definition for a
15839 nested class. So we have to process our children even if the
15840 current die is a declaration. Normally, of course, a declaration
15841 won't have any children at all. */
15842
15843 child_die = die->child;
15844
15845 while (child_die != NULL && child_die->tag)
15846 {
15847 if (child_die->tag == DW_TAG_member
15848 || child_die->tag == DW_TAG_variable
15849 || child_die->tag == DW_TAG_inheritance
15850 || child_die->tag == DW_TAG_template_value_param
15851 || child_die->tag == DW_TAG_template_type_param)
15852 {
15853 /* Do nothing. */
15854 }
15855 else
15856 process_die (child_die, cu);
15857
15858 child_die = child_die->sibling;
15859 }
15860
15861 /* Do not consider external references. According to the DWARF standard,
15862 these DIEs are identified by the fact that they have no byte_size
15863 attribute, and a declaration attribute. */
15864 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15865 || !die_is_declaration (die, cu)
15866 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15867 {
15868 struct symbol *sym = new_symbol (die, type, cu);
15869
15870 if (has_template_parameters)
15871 {
15872 struct symtab *symtab;
15873 if (sym != nullptr)
15874 symtab = symbol_symtab (sym);
15875 else if (cu->line_header != nullptr)
15876 {
15877 /* Any related symtab will do. */
15878 symtab
15879 = cu->line_header->file_names ()[0].symtab;
15880 }
15881 else
15882 {
15883 symtab = nullptr;
15884 complaint (_("could not find suitable "
15885 "symtab for template parameter"
15886 " - DIE at %s [in module %s]"),
15887 sect_offset_str (die->sect_off),
15888 objfile_name (objfile));
15889 }
15890
15891 if (symtab != nullptr)
15892 {
15893 /* Make sure that the symtab is set on the new symbols.
15894 Even though they don't appear in this symtab directly,
15895 other parts of gdb assume that symbols do, and this is
15896 reasonably true. */
15897 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15898 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15899 }
15900 }
15901 }
15902 }
15903
15904 /* Assuming DIE is an enumeration type, and TYPE is its associated
15905 type, update TYPE using some information only available in DIE's
15906 children. In particular, the fields are computed. */
15907
15908 static void
15909 update_enumeration_type_from_children (struct die_info *die,
15910 struct type *type,
15911 struct dwarf2_cu *cu)
15912 {
15913 struct die_info *child_die;
15914 int unsigned_enum = 1;
15915 int flag_enum = 1;
15916
15917 auto_obstack obstack;
15918 std::vector<struct field> fields;
15919
15920 for (child_die = die->child;
15921 child_die != NULL && child_die->tag;
15922 child_die = child_die->sibling)
15923 {
15924 struct attribute *attr;
15925 LONGEST value;
15926 const gdb_byte *bytes;
15927 struct dwarf2_locexpr_baton *baton;
15928 const char *name;
15929
15930 if (child_die->tag != DW_TAG_enumerator)
15931 continue;
15932
15933 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15934 if (attr == NULL)
15935 continue;
15936
15937 name = dwarf2_name (child_die, cu);
15938 if (name == NULL)
15939 name = "<anonymous enumerator>";
15940
15941 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15942 &value, &bytes, &baton);
15943 if (value < 0)
15944 {
15945 unsigned_enum = 0;
15946 flag_enum = 0;
15947 }
15948 else
15949 {
15950 if (count_one_bits_ll (value) >= 2)
15951 flag_enum = 0;
15952 }
15953
15954 fields.emplace_back ();
15955 struct field &field = fields.back ();
15956 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15957 SET_FIELD_ENUMVAL (field, value);
15958 }
15959
15960 if (!fields.empty ())
15961 {
15962 type->set_num_fields (fields.size ());
15963 type->set_fields
15964 ((struct field *)
15965 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
15966 memcpy (type->fields (), fields.data (),
15967 sizeof (struct field) * fields.size ());
15968 }
15969
15970 if (unsigned_enum)
15971 TYPE_UNSIGNED (type) = 1;
15972 if (flag_enum)
15973 TYPE_FLAG_ENUM (type) = 1;
15974 }
15975
15976 /* Given a DW_AT_enumeration_type die, set its type. We do not
15977 complete the type's fields yet, or create any symbols. */
15978
15979 static struct type *
15980 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15981 {
15982 struct objfile *objfile = cu->per_objfile->objfile;
15983 struct type *type;
15984 struct attribute *attr;
15985 const char *name;
15986
15987 /* If the definition of this type lives in .debug_types, read that type.
15988 Don't follow DW_AT_specification though, that will take us back up
15989 the chain and we want to go down. */
15990 attr = die->attr (DW_AT_signature);
15991 if (attr != nullptr)
15992 {
15993 type = get_DW_AT_signature_type (die, attr, cu);
15994
15995 /* The type's CU may not be the same as CU.
15996 Ensure TYPE is recorded with CU in die_type_hash. */
15997 return set_die_type (die, type, cu);
15998 }
15999
16000 type = alloc_type (objfile);
16001
16002 type->set_code (TYPE_CODE_ENUM);
16003 name = dwarf2_full_name (NULL, die, cu);
16004 if (name != NULL)
16005 type->set_name (name);
16006
16007 attr = dwarf2_attr (die, DW_AT_type, cu);
16008 if (attr != NULL)
16009 {
16010 struct type *underlying_type = die_type (die, cu);
16011
16012 TYPE_TARGET_TYPE (type) = underlying_type;
16013 }
16014
16015 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16016 if (attr != nullptr)
16017 {
16018 TYPE_LENGTH (type) = DW_UNSND (attr);
16019 }
16020 else
16021 {
16022 TYPE_LENGTH (type) = 0;
16023 }
16024
16025 maybe_set_alignment (cu, die, type);
16026
16027 /* The enumeration DIE can be incomplete. In Ada, any type can be
16028 declared as private in the package spec, and then defined only
16029 inside the package body. Such types are known as Taft Amendment
16030 Types. When another package uses such a type, an incomplete DIE
16031 may be generated by the compiler. */
16032 if (die_is_declaration (die, cu))
16033 TYPE_STUB (type) = 1;
16034
16035 /* If this type has an underlying type that is not a stub, then we
16036 may use its attributes. We always use the "unsigned" attribute
16037 in this situation, because ordinarily we guess whether the type
16038 is unsigned -- but the guess can be wrong and the underlying type
16039 can tell us the reality. However, we defer to a local size
16040 attribute if one exists, because this lets the compiler override
16041 the underlying type if needed. */
16042 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16043 {
16044 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16045 underlying_type = check_typedef (underlying_type);
16046 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16047 if (TYPE_LENGTH (type) == 0)
16048 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16049 if (TYPE_RAW_ALIGN (type) == 0
16050 && TYPE_RAW_ALIGN (underlying_type) != 0)
16051 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16052 }
16053
16054 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16055
16056 set_die_type (die, type, cu);
16057
16058 /* Finish the creation of this type by using the enum's children.
16059 Note that, as usual, this must come after set_die_type to avoid
16060 infinite recursion when trying to compute the names of the
16061 enumerators. */
16062 update_enumeration_type_from_children (die, type, cu);
16063
16064 return type;
16065 }
16066
16067 /* Given a pointer to a die which begins an enumeration, process all
16068 the dies that define the members of the enumeration, and create the
16069 symbol for the enumeration type.
16070
16071 NOTE: We reverse the order of the element list. */
16072
16073 static void
16074 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16075 {
16076 struct type *this_type;
16077
16078 this_type = get_die_type (die, cu);
16079 if (this_type == NULL)
16080 this_type = read_enumeration_type (die, cu);
16081
16082 if (die->child != NULL)
16083 {
16084 struct die_info *child_die;
16085 const char *name;
16086
16087 child_die = die->child;
16088 while (child_die && child_die->tag)
16089 {
16090 if (child_die->tag != DW_TAG_enumerator)
16091 {
16092 process_die (child_die, cu);
16093 }
16094 else
16095 {
16096 name = dwarf2_name (child_die, cu);
16097 if (name)
16098 new_symbol (child_die, this_type, cu);
16099 }
16100
16101 child_die = child_die->sibling;
16102 }
16103 }
16104
16105 /* If we are reading an enum from a .debug_types unit, and the enum
16106 is a declaration, and the enum is not the signatured type in the
16107 unit, then we do not want to add a symbol for it. Adding a
16108 symbol would in some cases obscure the true definition of the
16109 enum, giving users an incomplete type when the definition is
16110 actually available. Note that we do not want to do this for all
16111 enums which are just declarations, because C++0x allows forward
16112 enum declarations. */
16113 if (cu->per_cu->is_debug_types
16114 && die_is_declaration (die, cu))
16115 {
16116 struct signatured_type *sig_type;
16117
16118 sig_type = (struct signatured_type *) cu->per_cu;
16119 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16120 if (sig_type->type_offset_in_section != die->sect_off)
16121 return;
16122 }
16123
16124 new_symbol (die, this_type, cu);
16125 }
16126
16127 /* Extract all information from a DW_TAG_array_type DIE and put it in
16128 the DIE's type field. For now, this only handles one dimensional
16129 arrays. */
16130
16131 static struct type *
16132 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16133 {
16134 struct objfile *objfile = cu->per_objfile->objfile;
16135 struct die_info *child_die;
16136 struct type *type;
16137 struct type *element_type, *range_type, *index_type;
16138 struct attribute *attr;
16139 const char *name;
16140 struct dynamic_prop *byte_stride_prop = NULL;
16141 unsigned int bit_stride = 0;
16142
16143 element_type = die_type (die, cu);
16144
16145 /* The die_type call above may have already set the type for this DIE. */
16146 type = get_die_type (die, cu);
16147 if (type)
16148 return type;
16149
16150 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16151 if (attr != NULL)
16152 {
16153 int stride_ok;
16154 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
16155
16156 byte_stride_prop
16157 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16158 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16159 prop_type);
16160 if (!stride_ok)
16161 {
16162 complaint (_("unable to read array DW_AT_byte_stride "
16163 " - DIE at %s [in module %s]"),
16164 sect_offset_str (die->sect_off),
16165 objfile_name (cu->per_objfile->objfile));
16166 /* Ignore this attribute. We will likely not be able to print
16167 arrays of this type correctly, but there is little we can do
16168 to help if we cannot read the attribute's value. */
16169 byte_stride_prop = NULL;
16170 }
16171 }
16172
16173 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16174 if (attr != NULL)
16175 bit_stride = DW_UNSND (attr);
16176
16177 /* Irix 6.2 native cc creates array types without children for
16178 arrays with unspecified length. */
16179 if (die->child == NULL)
16180 {
16181 index_type = objfile_type (objfile)->builtin_int;
16182 range_type = create_static_range_type (NULL, index_type, 0, -1);
16183 type = create_array_type_with_stride (NULL, element_type, range_type,
16184 byte_stride_prop, bit_stride);
16185 return set_die_type (die, type, cu);
16186 }
16187
16188 std::vector<struct type *> range_types;
16189 child_die = die->child;
16190 while (child_die && child_die->tag)
16191 {
16192 if (child_die->tag == DW_TAG_subrange_type)
16193 {
16194 struct type *child_type = read_type_die (child_die, cu);
16195
16196 if (child_type != NULL)
16197 {
16198 /* The range type was succesfully read. Save it for the
16199 array type creation. */
16200 range_types.push_back (child_type);
16201 }
16202 }
16203 child_die = child_die->sibling;
16204 }
16205
16206 /* Dwarf2 dimensions are output from left to right, create the
16207 necessary array types in backwards order. */
16208
16209 type = element_type;
16210
16211 if (read_array_order (die, cu) == DW_ORD_col_major)
16212 {
16213 int i = 0;
16214
16215 while (i < range_types.size ())
16216 type = create_array_type_with_stride (NULL, type, range_types[i++],
16217 byte_stride_prop, bit_stride);
16218 }
16219 else
16220 {
16221 size_t ndim = range_types.size ();
16222 while (ndim-- > 0)
16223 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16224 byte_stride_prop, bit_stride);
16225 }
16226
16227 /* Understand Dwarf2 support for vector types (like they occur on
16228 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16229 array type. This is not part of the Dwarf2/3 standard yet, but a
16230 custom vendor extension. The main difference between a regular
16231 array and the vector variant is that vectors are passed by value
16232 to functions. */
16233 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16234 if (attr != nullptr)
16235 make_vector_type (type);
16236
16237 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16238 implementation may choose to implement triple vectors using this
16239 attribute. */
16240 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16241 if (attr != nullptr)
16242 {
16243 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16244 TYPE_LENGTH (type) = DW_UNSND (attr);
16245 else
16246 complaint (_("DW_AT_byte_size for array type smaller "
16247 "than the total size of elements"));
16248 }
16249
16250 name = dwarf2_name (die, cu);
16251 if (name)
16252 type->set_name (name);
16253
16254 maybe_set_alignment (cu, die, type);
16255
16256 /* Install the type in the die. */
16257 set_die_type (die, type, cu);
16258
16259 /* set_die_type should be already done. */
16260 set_descriptive_type (type, die, cu);
16261
16262 return type;
16263 }
16264
16265 static enum dwarf_array_dim_ordering
16266 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16267 {
16268 struct attribute *attr;
16269
16270 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16271
16272 if (attr != nullptr)
16273 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16274
16275 /* GNU F77 is a special case, as at 08/2004 array type info is the
16276 opposite order to the dwarf2 specification, but data is still
16277 laid out as per normal fortran.
16278
16279 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16280 version checking. */
16281
16282 if (cu->language == language_fortran
16283 && cu->producer && strstr (cu->producer, "GNU F77"))
16284 {
16285 return DW_ORD_row_major;
16286 }
16287
16288 switch (cu->language_defn->la_array_ordering)
16289 {
16290 case array_column_major:
16291 return DW_ORD_col_major;
16292 case array_row_major:
16293 default:
16294 return DW_ORD_row_major;
16295 };
16296 }
16297
16298 /* Extract all information from a DW_TAG_set_type DIE and put it in
16299 the DIE's type field. */
16300
16301 static struct type *
16302 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16303 {
16304 struct type *domain_type, *set_type;
16305 struct attribute *attr;
16306
16307 domain_type = die_type (die, cu);
16308
16309 /* The die_type call above may have already set the type for this DIE. */
16310 set_type = get_die_type (die, cu);
16311 if (set_type)
16312 return set_type;
16313
16314 set_type = create_set_type (NULL, domain_type);
16315
16316 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16317 if (attr != nullptr)
16318 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16319
16320 maybe_set_alignment (cu, die, set_type);
16321
16322 return set_die_type (die, set_type, cu);
16323 }
16324
16325 /* A helper for read_common_block that creates a locexpr baton.
16326 SYM is the symbol which we are marking as computed.
16327 COMMON_DIE is the DIE for the common block.
16328 COMMON_LOC is the location expression attribute for the common
16329 block itself.
16330 MEMBER_LOC is the location expression attribute for the particular
16331 member of the common block that we are processing.
16332 CU is the CU from which the above come. */
16333
16334 static void
16335 mark_common_block_symbol_computed (struct symbol *sym,
16336 struct die_info *common_die,
16337 struct attribute *common_loc,
16338 struct attribute *member_loc,
16339 struct dwarf2_cu *cu)
16340 {
16341 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16342 struct objfile *objfile = per_objfile->objfile;
16343 struct dwarf2_locexpr_baton *baton;
16344 gdb_byte *ptr;
16345 unsigned int cu_off;
16346 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16347 LONGEST offset = 0;
16348
16349 gdb_assert (common_loc && member_loc);
16350 gdb_assert (common_loc->form_is_block ());
16351 gdb_assert (member_loc->form_is_block ()
16352 || member_loc->form_is_constant ());
16353
16354 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16355 baton->per_objfile = per_objfile;
16356 baton->per_cu = cu->per_cu;
16357 gdb_assert (baton->per_cu);
16358
16359 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16360
16361 if (member_loc->form_is_constant ())
16362 {
16363 offset = member_loc->constant_value (0);
16364 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16365 }
16366 else
16367 baton->size += DW_BLOCK (member_loc)->size;
16368
16369 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16370 baton->data = ptr;
16371
16372 *ptr++ = DW_OP_call4;
16373 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16374 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16375 ptr += 4;
16376
16377 if (member_loc->form_is_constant ())
16378 {
16379 *ptr++ = DW_OP_addr;
16380 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16381 ptr += cu->header.addr_size;
16382 }
16383 else
16384 {
16385 /* We have to copy the data here, because DW_OP_call4 will only
16386 use a DW_AT_location attribute. */
16387 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16388 ptr += DW_BLOCK (member_loc)->size;
16389 }
16390
16391 *ptr++ = DW_OP_plus;
16392 gdb_assert (ptr - baton->data == baton->size);
16393
16394 SYMBOL_LOCATION_BATON (sym) = baton;
16395 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16396 }
16397
16398 /* Create appropriate locally-scoped variables for all the
16399 DW_TAG_common_block entries. Also create a struct common_block
16400 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16401 is used to separate the common blocks name namespace from regular
16402 variable names. */
16403
16404 static void
16405 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16406 {
16407 struct attribute *attr;
16408
16409 attr = dwarf2_attr (die, DW_AT_location, cu);
16410 if (attr != nullptr)
16411 {
16412 /* Support the .debug_loc offsets. */
16413 if (attr->form_is_block ())
16414 {
16415 /* Ok. */
16416 }
16417 else if (attr->form_is_section_offset ())
16418 {
16419 dwarf2_complex_location_expr_complaint ();
16420 attr = NULL;
16421 }
16422 else
16423 {
16424 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16425 "common block member");
16426 attr = NULL;
16427 }
16428 }
16429
16430 if (die->child != NULL)
16431 {
16432 struct objfile *objfile = cu->per_objfile->objfile;
16433 struct die_info *child_die;
16434 size_t n_entries = 0, size;
16435 struct common_block *common_block;
16436 struct symbol *sym;
16437
16438 for (child_die = die->child;
16439 child_die && child_die->tag;
16440 child_die = child_die->sibling)
16441 ++n_entries;
16442
16443 size = (sizeof (struct common_block)
16444 + (n_entries - 1) * sizeof (struct symbol *));
16445 common_block
16446 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16447 size);
16448 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16449 common_block->n_entries = 0;
16450
16451 for (child_die = die->child;
16452 child_die && child_die->tag;
16453 child_die = child_die->sibling)
16454 {
16455 /* Create the symbol in the DW_TAG_common_block block in the current
16456 symbol scope. */
16457 sym = new_symbol (child_die, NULL, cu);
16458 if (sym != NULL)
16459 {
16460 struct attribute *member_loc;
16461
16462 common_block->contents[common_block->n_entries++] = sym;
16463
16464 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16465 cu);
16466 if (member_loc)
16467 {
16468 /* GDB has handled this for a long time, but it is
16469 not specified by DWARF. It seems to have been
16470 emitted by gfortran at least as recently as:
16471 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16472 complaint (_("Variable in common block has "
16473 "DW_AT_data_member_location "
16474 "- DIE at %s [in module %s]"),
16475 sect_offset_str (child_die->sect_off),
16476 objfile_name (objfile));
16477
16478 if (member_loc->form_is_section_offset ())
16479 dwarf2_complex_location_expr_complaint ();
16480 else if (member_loc->form_is_constant ()
16481 || member_loc->form_is_block ())
16482 {
16483 if (attr != nullptr)
16484 mark_common_block_symbol_computed (sym, die, attr,
16485 member_loc, cu);
16486 }
16487 else
16488 dwarf2_complex_location_expr_complaint ();
16489 }
16490 }
16491 }
16492
16493 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16494 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16495 }
16496 }
16497
16498 /* Create a type for a C++ namespace. */
16499
16500 static struct type *
16501 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16502 {
16503 struct objfile *objfile = cu->per_objfile->objfile;
16504 const char *previous_prefix, *name;
16505 int is_anonymous;
16506 struct type *type;
16507
16508 /* For extensions, reuse the type of the original namespace. */
16509 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16510 {
16511 struct die_info *ext_die;
16512 struct dwarf2_cu *ext_cu = cu;
16513
16514 ext_die = dwarf2_extension (die, &ext_cu);
16515 type = read_type_die (ext_die, ext_cu);
16516
16517 /* EXT_CU may not be the same as CU.
16518 Ensure TYPE is recorded with CU in die_type_hash. */
16519 return set_die_type (die, type, cu);
16520 }
16521
16522 name = namespace_name (die, &is_anonymous, cu);
16523
16524 /* Now build the name of the current namespace. */
16525
16526 previous_prefix = determine_prefix (die, cu);
16527 if (previous_prefix[0] != '\0')
16528 name = typename_concat (&objfile->objfile_obstack,
16529 previous_prefix, name, 0, cu);
16530
16531 /* Create the type. */
16532 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16533
16534 return set_die_type (die, type, cu);
16535 }
16536
16537 /* Read a namespace scope. */
16538
16539 static void
16540 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16541 {
16542 struct objfile *objfile = cu->per_objfile->objfile;
16543 int is_anonymous;
16544
16545 /* Add a symbol associated to this if we haven't seen the namespace
16546 before. Also, add a using directive if it's an anonymous
16547 namespace. */
16548
16549 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16550 {
16551 struct type *type;
16552
16553 type = read_type_die (die, cu);
16554 new_symbol (die, type, cu);
16555
16556 namespace_name (die, &is_anonymous, cu);
16557 if (is_anonymous)
16558 {
16559 const char *previous_prefix = determine_prefix (die, cu);
16560
16561 std::vector<const char *> excludes;
16562 add_using_directive (using_directives (cu),
16563 previous_prefix, type->name (), NULL,
16564 NULL, excludes, 0, &objfile->objfile_obstack);
16565 }
16566 }
16567
16568 if (die->child != NULL)
16569 {
16570 struct die_info *child_die = die->child;
16571
16572 while (child_die && child_die->tag)
16573 {
16574 process_die (child_die, cu);
16575 child_die = child_die->sibling;
16576 }
16577 }
16578 }
16579
16580 /* Read a Fortran module as type. This DIE can be only a declaration used for
16581 imported module. Still we need that type as local Fortran "use ... only"
16582 declaration imports depend on the created type in determine_prefix. */
16583
16584 static struct type *
16585 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16586 {
16587 struct objfile *objfile = cu->per_objfile->objfile;
16588 const char *module_name;
16589 struct type *type;
16590
16591 module_name = dwarf2_name (die, cu);
16592 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16593
16594 return set_die_type (die, type, cu);
16595 }
16596
16597 /* Read a Fortran module. */
16598
16599 static void
16600 read_module (struct die_info *die, struct dwarf2_cu *cu)
16601 {
16602 struct die_info *child_die = die->child;
16603 struct type *type;
16604
16605 type = read_type_die (die, cu);
16606 new_symbol (die, type, cu);
16607
16608 while (child_die && child_die->tag)
16609 {
16610 process_die (child_die, cu);
16611 child_die = child_die->sibling;
16612 }
16613 }
16614
16615 /* Return the name of the namespace represented by DIE. Set
16616 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16617 namespace. */
16618
16619 static const char *
16620 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16621 {
16622 struct die_info *current_die;
16623 const char *name = NULL;
16624
16625 /* Loop through the extensions until we find a name. */
16626
16627 for (current_die = die;
16628 current_die != NULL;
16629 current_die = dwarf2_extension (die, &cu))
16630 {
16631 /* We don't use dwarf2_name here so that we can detect the absence
16632 of a name -> anonymous namespace. */
16633 name = dwarf2_string_attr (die, DW_AT_name, cu);
16634
16635 if (name != NULL)
16636 break;
16637 }
16638
16639 /* Is it an anonymous namespace? */
16640
16641 *is_anonymous = (name == NULL);
16642 if (*is_anonymous)
16643 name = CP_ANONYMOUS_NAMESPACE_STR;
16644
16645 return name;
16646 }
16647
16648 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16649 the user defined type vector. */
16650
16651 static struct type *
16652 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16653 {
16654 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16655 struct comp_unit_head *cu_header = &cu->header;
16656 struct type *type;
16657 struct attribute *attr_byte_size;
16658 struct attribute *attr_address_class;
16659 int byte_size, addr_class;
16660 struct type *target_type;
16661
16662 target_type = die_type (die, cu);
16663
16664 /* The die_type call above may have already set the type for this DIE. */
16665 type = get_die_type (die, cu);
16666 if (type)
16667 return type;
16668
16669 type = lookup_pointer_type (target_type);
16670
16671 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16672 if (attr_byte_size)
16673 byte_size = DW_UNSND (attr_byte_size);
16674 else
16675 byte_size = cu_header->addr_size;
16676
16677 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16678 if (attr_address_class)
16679 addr_class = DW_UNSND (attr_address_class);
16680 else
16681 addr_class = DW_ADDR_none;
16682
16683 ULONGEST alignment = get_alignment (cu, die);
16684
16685 /* If the pointer size, alignment, or address class is different
16686 than the default, create a type variant marked as such and set
16687 the length accordingly. */
16688 if (TYPE_LENGTH (type) != byte_size
16689 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16690 && alignment != TYPE_RAW_ALIGN (type))
16691 || addr_class != DW_ADDR_none)
16692 {
16693 if (gdbarch_address_class_type_flags_p (gdbarch))
16694 {
16695 int type_flags;
16696
16697 type_flags = gdbarch_address_class_type_flags
16698 (gdbarch, byte_size, addr_class);
16699 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16700 == 0);
16701 type = make_type_with_address_space (type, type_flags);
16702 }
16703 else if (TYPE_LENGTH (type) != byte_size)
16704 {
16705 complaint (_("invalid pointer size %d"), byte_size);
16706 }
16707 else if (TYPE_RAW_ALIGN (type) != alignment)
16708 {
16709 complaint (_("Invalid DW_AT_alignment"
16710 " - DIE at %s [in module %s]"),
16711 sect_offset_str (die->sect_off),
16712 objfile_name (cu->per_objfile->objfile));
16713 }
16714 else
16715 {
16716 /* Should we also complain about unhandled address classes? */
16717 }
16718 }
16719
16720 TYPE_LENGTH (type) = byte_size;
16721 set_type_align (type, alignment);
16722 return set_die_type (die, type, cu);
16723 }
16724
16725 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16726 the user defined type vector. */
16727
16728 static struct type *
16729 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16730 {
16731 struct type *type;
16732 struct type *to_type;
16733 struct type *domain;
16734
16735 to_type = die_type (die, cu);
16736 domain = die_containing_type (die, cu);
16737
16738 /* The calls above may have already set the type for this DIE. */
16739 type = get_die_type (die, cu);
16740 if (type)
16741 return type;
16742
16743 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16744 type = lookup_methodptr_type (to_type);
16745 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16746 {
16747 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16748
16749 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16750 to_type->fields (), to_type->num_fields (),
16751 TYPE_VARARGS (to_type));
16752 type = lookup_methodptr_type (new_type);
16753 }
16754 else
16755 type = lookup_memberptr_type (to_type, domain);
16756
16757 return set_die_type (die, type, cu);
16758 }
16759
16760 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16761 the user defined type vector. */
16762
16763 static struct type *
16764 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16765 enum type_code refcode)
16766 {
16767 struct comp_unit_head *cu_header = &cu->header;
16768 struct type *type, *target_type;
16769 struct attribute *attr;
16770
16771 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16772
16773 target_type = die_type (die, cu);
16774
16775 /* The die_type call above may have already set the type for this DIE. */
16776 type = get_die_type (die, cu);
16777 if (type)
16778 return type;
16779
16780 type = lookup_reference_type (target_type, refcode);
16781 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16782 if (attr != nullptr)
16783 {
16784 TYPE_LENGTH (type) = DW_UNSND (attr);
16785 }
16786 else
16787 {
16788 TYPE_LENGTH (type) = cu_header->addr_size;
16789 }
16790 maybe_set_alignment (cu, die, type);
16791 return set_die_type (die, type, cu);
16792 }
16793
16794 /* Add the given cv-qualifiers to the element type of the array. GCC
16795 outputs DWARF type qualifiers that apply to an array, not the
16796 element type. But GDB relies on the array element type to carry
16797 the cv-qualifiers. This mimics section 6.7.3 of the C99
16798 specification. */
16799
16800 static struct type *
16801 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16802 struct type *base_type, int cnst, int voltl)
16803 {
16804 struct type *el_type, *inner_array;
16805
16806 base_type = copy_type (base_type);
16807 inner_array = base_type;
16808
16809 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16810 {
16811 TYPE_TARGET_TYPE (inner_array) =
16812 copy_type (TYPE_TARGET_TYPE (inner_array));
16813 inner_array = TYPE_TARGET_TYPE (inner_array);
16814 }
16815
16816 el_type = TYPE_TARGET_TYPE (inner_array);
16817 cnst |= TYPE_CONST (el_type);
16818 voltl |= TYPE_VOLATILE (el_type);
16819 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16820
16821 return set_die_type (die, base_type, cu);
16822 }
16823
16824 static struct type *
16825 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16826 {
16827 struct type *base_type, *cv_type;
16828
16829 base_type = die_type (die, cu);
16830
16831 /* The die_type call above may have already set the type for this DIE. */
16832 cv_type = get_die_type (die, cu);
16833 if (cv_type)
16834 return cv_type;
16835
16836 /* In case the const qualifier is applied to an array type, the element type
16837 is so qualified, not the array type (section 6.7.3 of C99). */
16838 if (base_type->code () == TYPE_CODE_ARRAY)
16839 return add_array_cv_type (die, cu, base_type, 1, 0);
16840
16841 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16842 return set_die_type (die, cv_type, cu);
16843 }
16844
16845 static struct type *
16846 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16847 {
16848 struct type *base_type, *cv_type;
16849
16850 base_type = die_type (die, cu);
16851
16852 /* The die_type call above may have already set the type for this DIE. */
16853 cv_type = get_die_type (die, cu);
16854 if (cv_type)
16855 return cv_type;
16856
16857 /* In case the volatile qualifier is applied to an array type, the
16858 element type is so qualified, not the array type (section 6.7.3
16859 of C99). */
16860 if (base_type->code () == TYPE_CODE_ARRAY)
16861 return add_array_cv_type (die, cu, base_type, 0, 1);
16862
16863 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16864 return set_die_type (die, cv_type, cu);
16865 }
16866
16867 /* Handle DW_TAG_restrict_type. */
16868
16869 static struct type *
16870 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16871 {
16872 struct type *base_type, *cv_type;
16873
16874 base_type = die_type (die, cu);
16875
16876 /* The die_type call above may have already set the type for this DIE. */
16877 cv_type = get_die_type (die, cu);
16878 if (cv_type)
16879 return cv_type;
16880
16881 cv_type = make_restrict_type (base_type);
16882 return set_die_type (die, cv_type, cu);
16883 }
16884
16885 /* Handle DW_TAG_atomic_type. */
16886
16887 static struct type *
16888 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16889 {
16890 struct type *base_type, *cv_type;
16891
16892 base_type = die_type (die, cu);
16893
16894 /* The die_type call above may have already set the type for this DIE. */
16895 cv_type = get_die_type (die, cu);
16896 if (cv_type)
16897 return cv_type;
16898
16899 cv_type = make_atomic_type (base_type);
16900 return set_die_type (die, cv_type, cu);
16901 }
16902
16903 /* Extract all information from a DW_TAG_string_type DIE and add to
16904 the user defined type vector. It isn't really a user defined type,
16905 but it behaves like one, with other DIE's using an AT_user_def_type
16906 attribute to reference it. */
16907
16908 static struct type *
16909 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16910 {
16911 struct objfile *objfile = cu->per_objfile->objfile;
16912 struct gdbarch *gdbarch = objfile->arch ();
16913 struct type *type, *range_type, *index_type, *char_type;
16914 struct attribute *attr;
16915 struct dynamic_prop prop;
16916 bool length_is_constant = true;
16917 LONGEST length;
16918
16919 /* There are a couple of places where bit sizes might be made use of
16920 when parsing a DW_TAG_string_type, however, no producer that we know
16921 of make use of these. Handling bit sizes that are a multiple of the
16922 byte size is easy enough, but what about other bit sizes? Lets deal
16923 with that problem when we have to. Warn about these attributes being
16924 unsupported, then parse the type and ignore them like we always
16925 have. */
16926 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16927 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16928 {
16929 static bool warning_printed = false;
16930 if (!warning_printed)
16931 {
16932 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16933 "currently supported on DW_TAG_string_type."));
16934 warning_printed = true;
16935 }
16936 }
16937
16938 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16939 if (attr != nullptr && !attr->form_is_constant ())
16940 {
16941 /* The string length describes the location at which the length of
16942 the string can be found. The size of the length field can be
16943 specified with one of the attributes below. */
16944 struct type *prop_type;
16945 struct attribute *len
16946 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16947 if (len == nullptr)
16948 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16949 if (len != nullptr && len->form_is_constant ())
16950 {
16951 /* Pass 0 as the default as we know this attribute is constant
16952 and the default value will not be returned. */
16953 LONGEST sz = len->constant_value (0);
16954 prop_type = cu->per_cu->int_type (sz, true);
16955 }
16956 else
16957 {
16958 /* If the size is not specified then we assume it is the size of
16959 an address on this target. */
16960 prop_type = cu->per_cu->addr_sized_int_type (true);
16961 }
16962
16963 /* Convert the attribute into a dynamic property. */
16964 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16965 length = 1;
16966 else
16967 length_is_constant = false;
16968 }
16969 else if (attr != nullptr)
16970 {
16971 /* This DW_AT_string_length just contains the length with no
16972 indirection. There's no need to create a dynamic property in this
16973 case. Pass 0 for the default value as we know it will not be
16974 returned in this case. */
16975 length = attr->constant_value (0);
16976 }
16977 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16978 {
16979 /* We don't currently support non-constant byte sizes for strings. */
16980 length = attr->constant_value (1);
16981 }
16982 else
16983 {
16984 /* Use 1 as a fallback length if we have nothing else. */
16985 length = 1;
16986 }
16987
16988 index_type = objfile_type (objfile)->builtin_int;
16989 if (length_is_constant)
16990 range_type = create_static_range_type (NULL, index_type, 1, length);
16991 else
16992 {
16993 struct dynamic_prop low_bound;
16994
16995 low_bound.kind = PROP_CONST;
16996 low_bound.data.const_val = 1;
16997 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16998 }
16999 char_type = language_string_char_type (cu->language_defn, gdbarch);
17000 type = create_string_type (NULL, char_type, range_type);
17001
17002 return set_die_type (die, type, cu);
17003 }
17004
17005 /* Assuming that DIE corresponds to a function, returns nonzero
17006 if the function is prototyped. */
17007
17008 static int
17009 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17010 {
17011 struct attribute *attr;
17012
17013 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17014 if (attr && (DW_UNSND (attr) != 0))
17015 return 1;
17016
17017 /* The DWARF standard implies that the DW_AT_prototyped attribute
17018 is only meaningful for C, but the concept also extends to other
17019 languages that allow unprototyped functions (Eg: Objective C).
17020 For all other languages, assume that functions are always
17021 prototyped. */
17022 if (cu->language != language_c
17023 && cu->language != language_objc
17024 && cu->language != language_opencl)
17025 return 1;
17026
17027 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17028 prototyped and unprototyped functions; default to prototyped,
17029 since that is more common in modern code (and RealView warns
17030 about unprototyped functions). */
17031 if (producer_is_realview (cu->producer))
17032 return 1;
17033
17034 return 0;
17035 }
17036
17037 /* Handle DIES due to C code like:
17038
17039 struct foo
17040 {
17041 int (*funcp)(int a, long l);
17042 int b;
17043 };
17044
17045 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17046
17047 static struct type *
17048 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17049 {
17050 struct objfile *objfile = cu->per_objfile->objfile;
17051 struct type *type; /* Type that this function returns. */
17052 struct type *ftype; /* Function that returns above type. */
17053 struct attribute *attr;
17054
17055 type = die_type (die, cu);
17056
17057 /* The die_type call above may have already set the type for this DIE. */
17058 ftype = get_die_type (die, cu);
17059 if (ftype)
17060 return ftype;
17061
17062 ftype = lookup_function_type (type);
17063
17064 if (prototyped_function_p (die, cu))
17065 TYPE_PROTOTYPED (ftype) = 1;
17066
17067 /* Store the calling convention in the type if it's available in
17068 the subroutine die. Otherwise set the calling convention to
17069 the default value DW_CC_normal. */
17070 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17071 if (attr != nullptr
17072 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17073 TYPE_CALLING_CONVENTION (ftype)
17074 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17075 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17076 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17077 else
17078 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17079
17080 /* Record whether the function returns normally to its caller or not
17081 if the DWARF producer set that information. */
17082 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17083 if (attr && (DW_UNSND (attr) != 0))
17084 TYPE_NO_RETURN (ftype) = 1;
17085
17086 /* We need to add the subroutine type to the die immediately so
17087 we don't infinitely recurse when dealing with parameters
17088 declared as the same subroutine type. */
17089 set_die_type (die, ftype, cu);
17090
17091 if (die->child != NULL)
17092 {
17093 struct type *void_type = objfile_type (objfile)->builtin_void;
17094 struct die_info *child_die;
17095 int nparams, iparams;
17096
17097 /* Count the number of parameters.
17098 FIXME: GDB currently ignores vararg functions, but knows about
17099 vararg member functions. */
17100 nparams = 0;
17101 child_die = die->child;
17102 while (child_die && child_die->tag)
17103 {
17104 if (child_die->tag == DW_TAG_formal_parameter)
17105 nparams++;
17106 else if (child_die->tag == DW_TAG_unspecified_parameters)
17107 TYPE_VARARGS (ftype) = 1;
17108 child_die = child_die->sibling;
17109 }
17110
17111 /* Allocate storage for parameters and fill them in. */
17112 ftype->set_num_fields (nparams);
17113 ftype->set_fields
17114 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17115
17116 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17117 even if we error out during the parameters reading below. */
17118 for (iparams = 0; iparams < nparams; iparams++)
17119 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17120
17121 iparams = 0;
17122 child_die = die->child;
17123 while (child_die && child_die->tag)
17124 {
17125 if (child_die->tag == DW_TAG_formal_parameter)
17126 {
17127 struct type *arg_type;
17128
17129 /* DWARF version 2 has no clean way to discern C++
17130 static and non-static member functions. G++ helps
17131 GDB by marking the first parameter for non-static
17132 member functions (which is the this pointer) as
17133 artificial. We pass this information to
17134 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17135
17136 DWARF version 3 added DW_AT_object_pointer, which GCC
17137 4.5 does not yet generate. */
17138 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17139 if (attr != nullptr)
17140 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17141 else
17142 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17143 arg_type = die_type (child_die, cu);
17144
17145 /* RealView does not mark THIS as const, which the testsuite
17146 expects. GCC marks THIS as const in method definitions,
17147 but not in the class specifications (GCC PR 43053). */
17148 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17149 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17150 {
17151 int is_this = 0;
17152 struct dwarf2_cu *arg_cu = cu;
17153 const char *name = dwarf2_name (child_die, cu);
17154
17155 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17156 if (attr != nullptr)
17157 {
17158 /* If the compiler emits this, use it. */
17159 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17160 is_this = 1;
17161 }
17162 else if (name && strcmp (name, "this") == 0)
17163 /* Function definitions will have the argument names. */
17164 is_this = 1;
17165 else if (name == NULL && iparams == 0)
17166 /* Declarations may not have the names, so like
17167 elsewhere in GDB, assume an artificial first
17168 argument is "this". */
17169 is_this = 1;
17170
17171 if (is_this)
17172 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17173 arg_type, 0);
17174 }
17175
17176 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17177 iparams++;
17178 }
17179 child_die = child_die->sibling;
17180 }
17181 }
17182
17183 return ftype;
17184 }
17185
17186 static struct type *
17187 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17188 {
17189 struct objfile *objfile = cu->per_objfile->objfile;
17190 const char *name = NULL;
17191 struct type *this_type, *target_type;
17192
17193 name = dwarf2_full_name (NULL, die, cu);
17194 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17195 TYPE_TARGET_STUB (this_type) = 1;
17196 set_die_type (die, this_type, cu);
17197 target_type = die_type (die, cu);
17198 if (target_type != this_type)
17199 TYPE_TARGET_TYPE (this_type) = target_type;
17200 else
17201 {
17202 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17203 spec and cause infinite loops in GDB. */
17204 complaint (_("Self-referential DW_TAG_typedef "
17205 "- DIE at %s [in module %s]"),
17206 sect_offset_str (die->sect_off), objfile_name (objfile));
17207 TYPE_TARGET_TYPE (this_type) = NULL;
17208 }
17209 if (name == NULL)
17210 {
17211 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17212 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17213 Handle these by just returning the target type, rather than
17214 constructing an anonymous typedef type and trying to handle this
17215 elsewhere. */
17216 set_die_type (die, target_type, cu);
17217 return target_type;
17218 }
17219 return this_type;
17220 }
17221
17222 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17223 (which may be different from NAME) to the architecture back-end to allow
17224 it to guess the correct format if necessary. */
17225
17226 static struct type *
17227 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17228 const char *name_hint, enum bfd_endian byte_order)
17229 {
17230 struct gdbarch *gdbarch = objfile->arch ();
17231 const struct floatformat **format;
17232 struct type *type;
17233
17234 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17235 if (format)
17236 type = init_float_type (objfile, bits, name, format, byte_order);
17237 else
17238 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17239
17240 return type;
17241 }
17242
17243 /* Allocate an integer type of size BITS and name NAME. */
17244
17245 static struct type *
17246 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17247 int bits, int unsigned_p, const char *name)
17248 {
17249 struct type *type;
17250
17251 /* Versions of Intel's C Compiler generate an integer type called "void"
17252 instead of using DW_TAG_unspecified_type. This has been seen on
17253 at least versions 14, 17, and 18. */
17254 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17255 && strcmp (name, "void") == 0)
17256 type = objfile_type (objfile)->builtin_void;
17257 else
17258 type = init_integer_type (objfile, bits, unsigned_p, name);
17259
17260 return type;
17261 }
17262
17263 /* Initialise and return a floating point type of size BITS suitable for
17264 use as a component of a complex number. The NAME_HINT is passed through
17265 when initialising the floating point type and is the name of the complex
17266 type.
17267
17268 As DWARF doesn't currently provide an explicit name for the components
17269 of a complex number, but it can be helpful to have these components
17270 named, we try to select a suitable name based on the size of the
17271 component. */
17272 static struct type *
17273 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17274 struct objfile *objfile,
17275 int bits, const char *name_hint,
17276 enum bfd_endian byte_order)
17277 {
17278 gdbarch *gdbarch = objfile->arch ();
17279 struct type *tt = nullptr;
17280
17281 /* Try to find a suitable floating point builtin type of size BITS.
17282 We're going to use the name of this type as the name for the complex
17283 target type that we are about to create. */
17284 switch (cu->language)
17285 {
17286 case language_fortran:
17287 switch (bits)
17288 {
17289 case 32:
17290 tt = builtin_f_type (gdbarch)->builtin_real;
17291 break;
17292 case 64:
17293 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17294 break;
17295 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17296 case 128:
17297 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17298 break;
17299 }
17300 break;
17301 default:
17302 switch (bits)
17303 {
17304 case 32:
17305 tt = builtin_type (gdbarch)->builtin_float;
17306 break;
17307 case 64:
17308 tt = builtin_type (gdbarch)->builtin_double;
17309 break;
17310 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17311 case 128:
17312 tt = builtin_type (gdbarch)->builtin_long_double;
17313 break;
17314 }
17315 break;
17316 }
17317
17318 /* If the type we found doesn't match the size we were looking for, then
17319 pretend we didn't find a type at all, the complex target type we
17320 create will then be nameless. */
17321 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17322 tt = nullptr;
17323
17324 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17325 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17326 }
17327
17328 /* Find a representation of a given base type and install
17329 it in the TYPE field of the die. */
17330
17331 static struct type *
17332 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17333 {
17334 struct objfile *objfile = cu->per_objfile->objfile;
17335 struct type *type;
17336 struct attribute *attr;
17337 int encoding = 0, bits = 0;
17338 const char *name;
17339 gdbarch *arch;
17340
17341 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17342 if (attr != nullptr)
17343 encoding = DW_UNSND (attr);
17344 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17345 if (attr != nullptr)
17346 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17347 name = dwarf2_name (die, cu);
17348 if (!name)
17349 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17350
17351 arch = objfile->arch ();
17352 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17353
17354 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17355 if (attr)
17356 {
17357 int endianity = DW_UNSND (attr);
17358
17359 switch (endianity)
17360 {
17361 case DW_END_big:
17362 byte_order = BFD_ENDIAN_BIG;
17363 break;
17364 case DW_END_little:
17365 byte_order = BFD_ENDIAN_LITTLE;
17366 break;
17367 default:
17368 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17369 break;
17370 }
17371 }
17372
17373 switch (encoding)
17374 {
17375 case DW_ATE_address:
17376 /* Turn DW_ATE_address into a void * pointer. */
17377 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17378 type = init_pointer_type (objfile, bits, name, type);
17379 break;
17380 case DW_ATE_boolean:
17381 type = init_boolean_type (objfile, bits, 1, name);
17382 break;
17383 case DW_ATE_complex_float:
17384 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17385 byte_order);
17386 if (type->code () == TYPE_CODE_ERROR)
17387 {
17388 if (name == nullptr)
17389 {
17390 struct obstack *obstack
17391 = &cu->per_objfile->objfile->objfile_obstack;
17392 name = obconcat (obstack, "_Complex ", type->name (),
17393 nullptr);
17394 }
17395 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17396 }
17397 else
17398 type = init_complex_type (name, type);
17399 break;
17400 case DW_ATE_decimal_float:
17401 type = init_decfloat_type (objfile, bits, name);
17402 break;
17403 case DW_ATE_float:
17404 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17405 break;
17406 case DW_ATE_signed:
17407 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17408 break;
17409 case DW_ATE_unsigned:
17410 if (cu->language == language_fortran
17411 && name
17412 && startswith (name, "character("))
17413 type = init_character_type (objfile, bits, 1, name);
17414 else
17415 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17416 break;
17417 case DW_ATE_signed_char:
17418 if (cu->language == language_ada || cu->language == language_m2
17419 || cu->language == language_pascal
17420 || cu->language == language_fortran)
17421 type = init_character_type (objfile, bits, 0, name);
17422 else
17423 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17424 break;
17425 case DW_ATE_unsigned_char:
17426 if (cu->language == language_ada || cu->language == language_m2
17427 || cu->language == language_pascal
17428 || cu->language == language_fortran
17429 || cu->language == language_rust)
17430 type = init_character_type (objfile, bits, 1, name);
17431 else
17432 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17433 break;
17434 case DW_ATE_UTF:
17435 {
17436 if (bits == 16)
17437 type = builtin_type (arch)->builtin_char16;
17438 else if (bits == 32)
17439 type = builtin_type (arch)->builtin_char32;
17440 else
17441 {
17442 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17443 bits);
17444 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17445 }
17446 return set_die_type (die, type, cu);
17447 }
17448 break;
17449
17450 default:
17451 complaint (_("unsupported DW_AT_encoding: '%s'"),
17452 dwarf_type_encoding_name (encoding));
17453 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17454 break;
17455 }
17456
17457 if (name && strcmp (name, "char") == 0)
17458 TYPE_NOSIGN (type) = 1;
17459
17460 maybe_set_alignment (cu, die, type);
17461
17462 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17463
17464 return set_die_type (die, type, cu);
17465 }
17466
17467 /* Parse dwarf attribute if it's a block, reference or constant and put the
17468 resulting value of the attribute into struct bound_prop.
17469 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17470
17471 static int
17472 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17473 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17474 struct type *default_type)
17475 {
17476 struct dwarf2_property_baton *baton;
17477 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17478 struct objfile *objfile = per_objfile->objfile;
17479 struct obstack *obstack = &objfile->objfile_obstack;
17480
17481 gdb_assert (default_type != NULL);
17482
17483 if (attr == NULL || prop == NULL)
17484 return 0;
17485
17486 if (attr->form_is_block ())
17487 {
17488 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17489 baton->property_type = default_type;
17490 baton->locexpr.per_cu = cu->per_cu;
17491 baton->locexpr.per_objfile = per_objfile;
17492 baton->locexpr.size = DW_BLOCK (attr)->size;
17493 baton->locexpr.data = DW_BLOCK (attr)->data;
17494 switch (attr->name)
17495 {
17496 case DW_AT_string_length:
17497 baton->locexpr.is_reference = true;
17498 break;
17499 default:
17500 baton->locexpr.is_reference = false;
17501 break;
17502 }
17503 prop->data.baton = baton;
17504 prop->kind = PROP_LOCEXPR;
17505 gdb_assert (prop->data.baton != NULL);
17506 }
17507 else if (attr->form_is_ref ())
17508 {
17509 struct dwarf2_cu *target_cu = cu;
17510 struct die_info *target_die;
17511 struct attribute *target_attr;
17512
17513 target_die = follow_die_ref (die, attr, &target_cu);
17514 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17515 if (target_attr == NULL)
17516 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17517 target_cu);
17518 if (target_attr == NULL)
17519 return 0;
17520
17521 switch (target_attr->name)
17522 {
17523 case DW_AT_location:
17524 if (target_attr->form_is_section_offset ())
17525 {
17526 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17527 baton->property_type = die_type (target_die, target_cu);
17528 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17529 prop->data.baton = baton;
17530 prop->kind = PROP_LOCLIST;
17531 gdb_assert (prop->data.baton != NULL);
17532 }
17533 else if (target_attr->form_is_block ())
17534 {
17535 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17536 baton->property_type = die_type (target_die, target_cu);
17537 baton->locexpr.per_cu = cu->per_cu;
17538 baton->locexpr.per_objfile = per_objfile;
17539 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17540 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17541 baton->locexpr.is_reference = true;
17542 prop->data.baton = baton;
17543 prop->kind = PROP_LOCEXPR;
17544 gdb_assert (prop->data.baton != NULL);
17545 }
17546 else
17547 {
17548 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17549 "dynamic property");
17550 return 0;
17551 }
17552 break;
17553 case DW_AT_data_member_location:
17554 {
17555 LONGEST offset;
17556
17557 if (!handle_data_member_location (target_die, target_cu,
17558 &offset))
17559 return 0;
17560
17561 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17562 baton->property_type = read_type_die (target_die->parent,
17563 target_cu);
17564 baton->offset_info.offset = offset;
17565 baton->offset_info.type = die_type (target_die, target_cu);
17566 prop->data.baton = baton;
17567 prop->kind = PROP_ADDR_OFFSET;
17568 break;
17569 }
17570 }
17571 }
17572 else if (attr->form_is_constant ())
17573 {
17574 prop->data.const_val = attr->constant_value (0);
17575 prop->kind = PROP_CONST;
17576 }
17577 else
17578 {
17579 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17580 dwarf2_name (die, cu));
17581 return 0;
17582 }
17583
17584 return 1;
17585 }
17586
17587 /* See read.h. */
17588
17589 struct type *
17590 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17591 {
17592 struct objfile *objfile = dwarf2_per_objfile->objfile;
17593 struct type *int_type;
17594
17595 /* Helper macro to examine the various builtin types. */
17596 #define TRY_TYPE(F) \
17597 int_type = (unsigned_p \
17598 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17599 : objfile_type (objfile)->builtin_ ## F); \
17600 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17601 return int_type
17602
17603 TRY_TYPE (char);
17604 TRY_TYPE (short);
17605 TRY_TYPE (int);
17606 TRY_TYPE (long);
17607 TRY_TYPE (long_long);
17608
17609 #undef TRY_TYPE
17610
17611 gdb_assert_not_reached ("unable to find suitable integer type");
17612 }
17613
17614 /* See read.h. */
17615
17616 struct type *
17617 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17618 {
17619 int addr_size = this->addr_size ();
17620 return int_type (addr_size, unsigned_p);
17621 }
17622
17623 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17624 present (which is valid) then compute the default type based on the
17625 compilation units address size. */
17626
17627 static struct type *
17628 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17629 {
17630 struct type *index_type = die_type (die, cu);
17631
17632 /* Dwarf-2 specifications explicitly allows to create subrange types
17633 without specifying a base type.
17634 In that case, the base type must be set to the type of
17635 the lower bound, upper bound or count, in that order, if any of these
17636 three attributes references an object that has a type.
17637 If no base type is found, the Dwarf-2 specifications say that
17638 a signed integer type of size equal to the size of an address should
17639 be used.
17640 For the following C code: `extern char gdb_int [];'
17641 GCC produces an empty range DIE.
17642 FIXME: muller/2010-05-28: Possible references to object for low bound,
17643 high bound or count are not yet handled by this code. */
17644 if (index_type->code () == TYPE_CODE_VOID)
17645 index_type = cu->per_cu->addr_sized_int_type (false);
17646
17647 return index_type;
17648 }
17649
17650 /* Read the given DW_AT_subrange DIE. */
17651
17652 static struct type *
17653 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17654 {
17655 struct type *base_type, *orig_base_type;
17656 struct type *range_type;
17657 struct attribute *attr;
17658 struct dynamic_prop low, high;
17659 int low_default_is_valid;
17660 int high_bound_is_count = 0;
17661 const char *name;
17662 ULONGEST negative_mask;
17663
17664 orig_base_type = read_subrange_index_type (die, cu);
17665
17666 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17667 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17668 creating the range type, but we use the result of check_typedef
17669 when examining properties of the type. */
17670 base_type = check_typedef (orig_base_type);
17671
17672 /* The die_type call above may have already set the type for this DIE. */
17673 range_type = get_die_type (die, cu);
17674 if (range_type)
17675 return range_type;
17676
17677 low.kind = PROP_CONST;
17678 high.kind = PROP_CONST;
17679 high.data.const_val = 0;
17680
17681 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17682 omitting DW_AT_lower_bound. */
17683 switch (cu->language)
17684 {
17685 case language_c:
17686 case language_cplus:
17687 low.data.const_val = 0;
17688 low_default_is_valid = 1;
17689 break;
17690 case language_fortran:
17691 low.data.const_val = 1;
17692 low_default_is_valid = 1;
17693 break;
17694 case language_d:
17695 case language_objc:
17696 case language_rust:
17697 low.data.const_val = 0;
17698 low_default_is_valid = (cu->header.version >= 4);
17699 break;
17700 case language_ada:
17701 case language_m2:
17702 case language_pascal:
17703 low.data.const_val = 1;
17704 low_default_is_valid = (cu->header.version >= 4);
17705 break;
17706 default:
17707 low.data.const_val = 0;
17708 low_default_is_valid = 0;
17709 break;
17710 }
17711
17712 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17713 if (attr != nullptr)
17714 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17715 else if (!low_default_is_valid)
17716 complaint (_("Missing DW_AT_lower_bound "
17717 "- DIE at %s [in module %s]"),
17718 sect_offset_str (die->sect_off),
17719 objfile_name (cu->per_objfile->objfile));
17720
17721 struct attribute *attr_ub, *attr_count;
17722 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17723 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17724 {
17725 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17726 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17727 {
17728 /* If bounds are constant do the final calculation here. */
17729 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17730 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17731 else
17732 high_bound_is_count = 1;
17733 }
17734 else
17735 {
17736 if (attr_ub != NULL)
17737 complaint (_("Unresolved DW_AT_upper_bound "
17738 "- DIE at %s [in module %s]"),
17739 sect_offset_str (die->sect_off),
17740 objfile_name (cu->per_objfile->objfile));
17741 if (attr_count != NULL)
17742 complaint (_("Unresolved DW_AT_count "
17743 "- DIE at %s [in module %s]"),
17744 sect_offset_str (die->sect_off),
17745 objfile_name (cu->per_objfile->objfile));
17746 }
17747 }
17748
17749 LONGEST bias = 0;
17750 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17751 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17752 bias = bias_attr->constant_value (0);
17753
17754 /* Normally, the DWARF producers are expected to use a signed
17755 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17756 But this is unfortunately not always the case, as witnessed
17757 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17758 is used instead. To work around that ambiguity, we treat
17759 the bounds as signed, and thus sign-extend their values, when
17760 the base type is signed. */
17761 negative_mask =
17762 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17763 if (low.kind == PROP_CONST
17764 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17765 low.data.const_val |= negative_mask;
17766 if (high.kind == PROP_CONST
17767 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17768 high.data.const_val |= negative_mask;
17769
17770 /* Check for bit and byte strides. */
17771 struct dynamic_prop byte_stride_prop;
17772 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17773 if (attr_byte_stride != nullptr)
17774 {
17775 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17776 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17777 prop_type);
17778 }
17779
17780 struct dynamic_prop bit_stride_prop;
17781 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17782 if (attr_bit_stride != nullptr)
17783 {
17784 /* It only makes sense to have either a bit or byte stride. */
17785 if (attr_byte_stride != nullptr)
17786 {
17787 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17788 "- DIE at %s [in module %s]"),
17789 sect_offset_str (die->sect_off),
17790 objfile_name (cu->per_objfile->objfile));
17791 attr_bit_stride = nullptr;
17792 }
17793 else
17794 {
17795 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17796 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17797 prop_type);
17798 }
17799 }
17800
17801 if (attr_byte_stride != nullptr
17802 || attr_bit_stride != nullptr)
17803 {
17804 bool byte_stride_p = (attr_byte_stride != nullptr);
17805 struct dynamic_prop *stride
17806 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17807
17808 range_type
17809 = create_range_type_with_stride (NULL, orig_base_type, &low,
17810 &high, bias, stride, byte_stride_p);
17811 }
17812 else
17813 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17814
17815 if (high_bound_is_count)
17816 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17817
17818 /* Ada expects an empty array on no boundary attributes. */
17819 if (attr == NULL && cu->language != language_ada)
17820 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17821
17822 name = dwarf2_name (die, cu);
17823 if (name)
17824 range_type->set_name (name);
17825
17826 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17827 if (attr != nullptr)
17828 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17829
17830 maybe_set_alignment (cu, die, range_type);
17831
17832 set_die_type (die, range_type, cu);
17833
17834 /* set_die_type should be already done. */
17835 set_descriptive_type (range_type, die, cu);
17836
17837 return range_type;
17838 }
17839
17840 static struct type *
17841 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17842 {
17843 struct type *type;
17844
17845 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17846 type->set_name (dwarf2_name (die, cu));
17847
17848 /* In Ada, an unspecified type is typically used when the description
17849 of the type is deferred to a different unit. When encountering
17850 such a type, we treat it as a stub, and try to resolve it later on,
17851 when needed. */
17852 if (cu->language == language_ada)
17853 TYPE_STUB (type) = 1;
17854
17855 return set_die_type (die, type, cu);
17856 }
17857
17858 /* Read a single die and all its descendents. Set the die's sibling
17859 field to NULL; set other fields in the die correctly, and set all
17860 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17861 location of the info_ptr after reading all of those dies. PARENT
17862 is the parent of the die in question. */
17863
17864 static struct die_info *
17865 read_die_and_children (const struct die_reader_specs *reader,
17866 const gdb_byte *info_ptr,
17867 const gdb_byte **new_info_ptr,
17868 struct die_info *parent)
17869 {
17870 struct die_info *die;
17871 const gdb_byte *cur_ptr;
17872
17873 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17874 if (die == NULL)
17875 {
17876 *new_info_ptr = cur_ptr;
17877 return NULL;
17878 }
17879 store_in_ref_table (die, reader->cu);
17880
17881 if (die->has_children)
17882 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17883 else
17884 {
17885 die->child = NULL;
17886 *new_info_ptr = cur_ptr;
17887 }
17888
17889 die->sibling = NULL;
17890 die->parent = parent;
17891 return die;
17892 }
17893
17894 /* Read a die, all of its descendents, and all of its siblings; set
17895 all of the fields of all of the dies correctly. Arguments are as
17896 in read_die_and_children. */
17897
17898 static struct die_info *
17899 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17900 const gdb_byte *info_ptr,
17901 const gdb_byte **new_info_ptr,
17902 struct die_info *parent)
17903 {
17904 struct die_info *first_die, *last_sibling;
17905 const gdb_byte *cur_ptr;
17906
17907 cur_ptr = info_ptr;
17908 first_die = last_sibling = NULL;
17909
17910 while (1)
17911 {
17912 struct die_info *die
17913 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17914
17915 if (die == NULL)
17916 {
17917 *new_info_ptr = cur_ptr;
17918 return first_die;
17919 }
17920
17921 if (!first_die)
17922 first_die = die;
17923 else
17924 last_sibling->sibling = die;
17925
17926 last_sibling = die;
17927 }
17928 }
17929
17930 /* Read a die, all of its descendents, and all of its siblings; set
17931 all of the fields of all of the dies correctly. Arguments are as
17932 in read_die_and_children.
17933 This the main entry point for reading a DIE and all its children. */
17934
17935 static struct die_info *
17936 read_die_and_siblings (const struct die_reader_specs *reader,
17937 const gdb_byte *info_ptr,
17938 const gdb_byte **new_info_ptr,
17939 struct die_info *parent)
17940 {
17941 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17942 new_info_ptr, parent);
17943
17944 if (dwarf_die_debug)
17945 {
17946 fprintf_unfiltered (gdb_stdlog,
17947 "Read die from %s@0x%x of %s:\n",
17948 reader->die_section->get_name (),
17949 (unsigned) (info_ptr - reader->die_section->buffer),
17950 bfd_get_filename (reader->abfd));
17951 dump_die (die, dwarf_die_debug);
17952 }
17953
17954 return die;
17955 }
17956
17957 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17958 attributes.
17959 The caller is responsible for filling in the extra attributes
17960 and updating (*DIEP)->num_attrs.
17961 Set DIEP to point to a newly allocated die with its information,
17962 except for its child, sibling, and parent fields. */
17963
17964 static const gdb_byte *
17965 read_full_die_1 (const struct die_reader_specs *reader,
17966 struct die_info **diep, const gdb_byte *info_ptr,
17967 int num_extra_attrs)
17968 {
17969 unsigned int abbrev_number, bytes_read, i;
17970 struct abbrev_info *abbrev;
17971 struct die_info *die;
17972 struct dwarf2_cu *cu = reader->cu;
17973 bfd *abfd = reader->abfd;
17974
17975 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17976 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17977 info_ptr += bytes_read;
17978 if (!abbrev_number)
17979 {
17980 *diep = NULL;
17981 return info_ptr;
17982 }
17983
17984 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17985 if (!abbrev)
17986 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17987 abbrev_number,
17988 bfd_get_filename (abfd));
17989
17990 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17991 die->sect_off = sect_off;
17992 die->tag = abbrev->tag;
17993 die->abbrev = abbrev_number;
17994 die->has_children = abbrev->has_children;
17995
17996 /* Make the result usable.
17997 The caller needs to update num_attrs after adding the extra
17998 attributes. */
17999 die->num_attrs = abbrev->num_attrs;
18000
18001 std::vector<int> indexes_that_need_reprocess;
18002 for (i = 0; i < abbrev->num_attrs; ++i)
18003 {
18004 bool need_reprocess;
18005 info_ptr =
18006 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18007 info_ptr, &need_reprocess);
18008 if (need_reprocess)
18009 indexes_that_need_reprocess.push_back (i);
18010 }
18011
18012 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18013 if (attr != nullptr)
18014 cu->str_offsets_base = DW_UNSND (attr);
18015
18016 attr = die->attr (DW_AT_loclists_base);
18017 if (attr != nullptr)
18018 cu->loclist_base = DW_UNSND (attr);
18019
18020 auto maybe_addr_base = die->addr_base ();
18021 if (maybe_addr_base.has_value ())
18022 cu->addr_base = *maybe_addr_base;
18023 for (int index : indexes_that_need_reprocess)
18024 read_attribute_reprocess (reader, &die->attrs[index]);
18025 *diep = die;
18026 return info_ptr;
18027 }
18028
18029 /* Read a die and all its attributes.
18030 Set DIEP to point to a newly allocated die with its information,
18031 except for its child, sibling, and parent fields. */
18032
18033 static const gdb_byte *
18034 read_full_die (const struct die_reader_specs *reader,
18035 struct die_info **diep, const gdb_byte *info_ptr)
18036 {
18037 const gdb_byte *result;
18038
18039 result = read_full_die_1 (reader, diep, info_ptr, 0);
18040
18041 if (dwarf_die_debug)
18042 {
18043 fprintf_unfiltered (gdb_stdlog,
18044 "Read die from %s@0x%x of %s:\n",
18045 reader->die_section->get_name (),
18046 (unsigned) (info_ptr - reader->die_section->buffer),
18047 bfd_get_filename (reader->abfd));
18048 dump_die (*diep, dwarf_die_debug);
18049 }
18050
18051 return result;
18052 }
18053 \f
18054
18055 /* Returns nonzero if TAG represents a type that we might generate a partial
18056 symbol for. */
18057
18058 static int
18059 is_type_tag_for_partial (int tag)
18060 {
18061 switch (tag)
18062 {
18063 #if 0
18064 /* Some types that would be reasonable to generate partial symbols for,
18065 that we don't at present. */
18066 case DW_TAG_array_type:
18067 case DW_TAG_file_type:
18068 case DW_TAG_ptr_to_member_type:
18069 case DW_TAG_set_type:
18070 case DW_TAG_string_type:
18071 case DW_TAG_subroutine_type:
18072 #endif
18073 case DW_TAG_base_type:
18074 case DW_TAG_class_type:
18075 case DW_TAG_interface_type:
18076 case DW_TAG_enumeration_type:
18077 case DW_TAG_structure_type:
18078 case DW_TAG_subrange_type:
18079 case DW_TAG_typedef:
18080 case DW_TAG_union_type:
18081 return 1;
18082 default:
18083 return 0;
18084 }
18085 }
18086
18087 /* Load all DIEs that are interesting for partial symbols into memory. */
18088
18089 static struct partial_die_info *
18090 load_partial_dies (const struct die_reader_specs *reader,
18091 const gdb_byte *info_ptr, int building_psymtab)
18092 {
18093 struct dwarf2_cu *cu = reader->cu;
18094 struct objfile *objfile = cu->per_objfile->objfile;
18095 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18096 unsigned int bytes_read;
18097 unsigned int load_all = 0;
18098 int nesting_level = 1;
18099
18100 parent_die = NULL;
18101 last_die = NULL;
18102
18103 gdb_assert (cu->per_cu != NULL);
18104 if (cu->per_cu->load_all_dies)
18105 load_all = 1;
18106
18107 cu->partial_dies
18108 = htab_create_alloc_ex (cu->header.length / 12,
18109 partial_die_hash,
18110 partial_die_eq,
18111 NULL,
18112 &cu->comp_unit_obstack,
18113 hashtab_obstack_allocate,
18114 dummy_obstack_deallocate);
18115
18116 while (1)
18117 {
18118 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18119
18120 /* A NULL abbrev means the end of a series of children. */
18121 if (abbrev == NULL)
18122 {
18123 if (--nesting_level == 0)
18124 return first_die;
18125
18126 info_ptr += bytes_read;
18127 last_die = parent_die;
18128 parent_die = parent_die->die_parent;
18129 continue;
18130 }
18131
18132 /* Check for template arguments. We never save these; if
18133 they're seen, we just mark the parent, and go on our way. */
18134 if (parent_die != NULL
18135 && cu->language == language_cplus
18136 && (abbrev->tag == DW_TAG_template_type_param
18137 || abbrev->tag == DW_TAG_template_value_param))
18138 {
18139 parent_die->has_template_arguments = 1;
18140
18141 if (!load_all)
18142 {
18143 /* We don't need a partial DIE for the template argument. */
18144 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18145 continue;
18146 }
18147 }
18148
18149 /* We only recurse into c++ subprograms looking for template arguments.
18150 Skip their other children. */
18151 if (!load_all
18152 && cu->language == language_cplus
18153 && parent_die != NULL
18154 && parent_die->tag == DW_TAG_subprogram)
18155 {
18156 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18157 continue;
18158 }
18159
18160 /* Check whether this DIE is interesting enough to save. Normally
18161 we would not be interested in members here, but there may be
18162 later variables referencing them via DW_AT_specification (for
18163 static members). */
18164 if (!load_all
18165 && !is_type_tag_for_partial (abbrev->tag)
18166 && abbrev->tag != DW_TAG_constant
18167 && abbrev->tag != DW_TAG_enumerator
18168 && abbrev->tag != DW_TAG_subprogram
18169 && abbrev->tag != DW_TAG_inlined_subroutine
18170 && abbrev->tag != DW_TAG_lexical_block
18171 && abbrev->tag != DW_TAG_variable
18172 && abbrev->tag != DW_TAG_namespace
18173 && abbrev->tag != DW_TAG_module
18174 && abbrev->tag != DW_TAG_member
18175 && abbrev->tag != DW_TAG_imported_unit
18176 && abbrev->tag != DW_TAG_imported_declaration)
18177 {
18178 /* Otherwise we skip to the next sibling, if any. */
18179 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18180 continue;
18181 }
18182
18183 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18184 abbrev);
18185
18186 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18187
18188 /* This two-pass algorithm for processing partial symbols has a
18189 high cost in cache pressure. Thus, handle some simple cases
18190 here which cover the majority of C partial symbols. DIEs
18191 which neither have specification tags in them, nor could have
18192 specification tags elsewhere pointing at them, can simply be
18193 processed and discarded.
18194
18195 This segment is also optional; scan_partial_symbols and
18196 add_partial_symbol will handle these DIEs if we chain
18197 them in normally. When compilers which do not emit large
18198 quantities of duplicate debug information are more common,
18199 this code can probably be removed. */
18200
18201 /* Any complete simple types at the top level (pretty much all
18202 of them, for a language without namespaces), can be processed
18203 directly. */
18204 if (parent_die == NULL
18205 && pdi.has_specification == 0
18206 && pdi.is_declaration == 0
18207 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18208 || pdi.tag == DW_TAG_base_type
18209 || pdi.tag == DW_TAG_subrange_type))
18210 {
18211 if (building_psymtab && pdi.name != NULL)
18212 add_psymbol_to_list (pdi.name, false,
18213 VAR_DOMAIN, LOC_TYPEDEF, -1,
18214 psymbol_placement::STATIC,
18215 0, cu->language, objfile);
18216 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18217 continue;
18218 }
18219
18220 /* The exception for DW_TAG_typedef with has_children above is
18221 a workaround of GCC PR debug/47510. In the case of this complaint
18222 type_name_or_error will error on such types later.
18223
18224 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18225 it could not find the child DIEs referenced later, this is checked
18226 above. In correct DWARF DW_TAG_typedef should have no children. */
18227
18228 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18229 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18230 "- DIE at %s [in module %s]"),
18231 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18232
18233 /* If we're at the second level, and we're an enumerator, and
18234 our parent has no specification (meaning possibly lives in a
18235 namespace elsewhere), then we can add the partial symbol now
18236 instead of queueing it. */
18237 if (pdi.tag == DW_TAG_enumerator
18238 && parent_die != NULL
18239 && parent_die->die_parent == NULL
18240 && parent_die->tag == DW_TAG_enumeration_type
18241 && parent_die->has_specification == 0)
18242 {
18243 if (pdi.name == NULL)
18244 complaint (_("malformed enumerator DIE ignored"));
18245 else if (building_psymtab)
18246 add_psymbol_to_list (pdi.name, false,
18247 VAR_DOMAIN, LOC_CONST, -1,
18248 cu->language == language_cplus
18249 ? psymbol_placement::GLOBAL
18250 : psymbol_placement::STATIC,
18251 0, cu->language, objfile);
18252
18253 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18254 continue;
18255 }
18256
18257 struct partial_die_info *part_die
18258 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18259
18260 /* We'll save this DIE so link it in. */
18261 part_die->die_parent = parent_die;
18262 part_die->die_sibling = NULL;
18263 part_die->die_child = NULL;
18264
18265 if (last_die && last_die == parent_die)
18266 last_die->die_child = part_die;
18267 else if (last_die)
18268 last_die->die_sibling = part_die;
18269
18270 last_die = part_die;
18271
18272 if (first_die == NULL)
18273 first_die = part_die;
18274
18275 /* Maybe add the DIE to the hash table. Not all DIEs that we
18276 find interesting need to be in the hash table, because we
18277 also have the parent/sibling/child chains; only those that we
18278 might refer to by offset later during partial symbol reading.
18279
18280 For now this means things that might have be the target of a
18281 DW_AT_specification, DW_AT_abstract_origin, or
18282 DW_AT_extension. DW_AT_extension will refer only to
18283 namespaces; DW_AT_abstract_origin refers to functions (and
18284 many things under the function DIE, but we do not recurse
18285 into function DIEs during partial symbol reading) and
18286 possibly variables as well; DW_AT_specification refers to
18287 declarations. Declarations ought to have the DW_AT_declaration
18288 flag. It happens that GCC forgets to put it in sometimes, but
18289 only for functions, not for types.
18290
18291 Adding more things than necessary to the hash table is harmless
18292 except for the performance cost. Adding too few will result in
18293 wasted time in find_partial_die, when we reread the compilation
18294 unit with load_all_dies set. */
18295
18296 if (load_all
18297 || abbrev->tag == DW_TAG_constant
18298 || abbrev->tag == DW_TAG_subprogram
18299 || abbrev->tag == DW_TAG_variable
18300 || abbrev->tag == DW_TAG_namespace
18301 || part_die->is_declaration)
18302 {
18303 void **slot;
18304
18305 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18306 to_underlying (part_die->sect_off),
18307 INSERT);
18308 *slot = part_die;
18309 }
18310
18311 /* For some DIEs we want to follow their children (if any). For C
18312 we have no reason to follow the children of structures; for other
18313 languages we have to, so that we can get at method physnames
18314 to infer fully qualified class names, for DW_AT_specification,
18315 and for C++ template arguments. For C++, we also look one level
18316 inside functions to find template arguments (if the name of the
18317 function does not already contain the template arguments).
18318
18319 For Ada and Fortran, we need to scan the children of subprograms
18320 and lexical blocks as well because these languages allow the
18321 definition of nested entities that could be interesting for the
18322 debugger, such as nested subprograms for instance. */
18323 if (last_die->has_children
18324 && (load_all
18325 || last_die->tag == DW_TAG_namespace
18326 || last_die->tag == DW_TAG_module
18327 || last_die->tag == DW_TAG_enumeration_type
18328 || (cu->language == language_cplus
18329 && last_die->tag == DW_TAG_subprogram
18330 && (last_die->name == NULL
18331 || strchr (last_die->name, '<') == NULL))
18332 || (cu->language != language_c
18333 && (last_die->tag == DW_TAG_class_type
18334 || last_die->tag == DW_TAG_interface_type
18335 || last_die->tag == DW_TAG_structure_type
18336 || last_die->tag == DW_TAG_union_type))
18337 || ((cu->language == language_ada
18338 || cu->language == language_fortran)
18339 && (last_die->tag == DW_TAG_subprogram
18340 || last_die->tag == DW_TAG_lexical_block))))
18341 {
18342 nesting_level++;
18343 parent_die = last_die;
18344 continue;
18345 }
18346
18347 /* Otherwise we skip to the next sibling, if any. */
18348 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18349
18350 /* Back to the top, do it again. */
18351 }
18352 }
18353
18354 partial_die_info::partial_die_info (sect_offset sect_off_,
18355 struct abbrev_info *abbrev)
18356 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18357 {
18358 }
18359
18360 /* Read a minimal amount of information into the minimal die structure.
18361 INFO_PTR should point just after the initial uleb128 of a DIE. */
18362
18363 const gdb_byte *
18364 partial_die_info::read (const struct die_reader_specs *reader,
18365 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18366 {
18367 struct dwarf2_cu *cu = reader->cu;
18368 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18369 unsigned int i;
18370 int has_low_pc_attr = 0;
18371 int has_high_pc_attr = 0;
18372 int high_pc_relative = 0;
18373
18374 for (i = 0; i < abbrev.num_attrs; ++i)
18375 {
18376 attribute attr;
18377 bool need_reprocess;
18378 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18379 info_ptr, &need_reprocess);
18380 /* String and address offsets that need to do the reprocessing have
18381 already been read at this point, so there is no need to wait until
18382 the loop terminates to do the reprocessing. */
18383 if (need_reprocess)
18384 read_attribute_reprocess (reader, &attr);
18385 /* Store the data if it is of an attribute we want to keep in a
18386 partial symbol table. */
18387 switch (attr.name)
18388 {
18389 case DW_AT_name:
18390 switch (tag)
18391 {
18392 case DW_TAG_compile_unit:
18393 case DW_TAG_partial_unit:
18394 case DW_TAG_type_unit:
18395 /* Compilation units have a DW_AT_name that is a filename, not
18396 a source language identifier. */
18397 case DW_TAG_enumeration_type:
18398 case DW_TAG_enumerator:
18399 /* These tags always have simple identifiers already; no need
18400 to canonicalize them. */
18401 name = DW_STRING (&attr);
18402 break;
18403 default:
18404 {
18405 struct objfile *objfile = dwarf2_per_objfile->objfile;
18406
18407 name
18408 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18409 }
18410 break;
18411 }
18412 break;
18413 case DW_AT_linkage_name:
18414 case DW_AT_MIPS_linkage_name:
18415 /* Note that both forms of linkage name might appear. We
18416 assume they will be the same, and we only store the last
18417 one we see. */
18418 linkage_name = attr.value_as_string ();
18419 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18420 See https://github.com/rust-lang/rust/issues/32925. */
18421 if (cu->language == language_rust && linkage_name != NULL
18422 && strchr (linkage_name, '{') != NULL)
18423 linkage_name = NULL;
18424 break;
18425 case DW_AT_low_pc:
18426 has_low_pc_attr = 1;
18427 lowpc = attr.value_as_address ();
18428 break;
18429 case DW_AT_high_pc:
18430 has_high_pc_attr = 1;
18431 highpc = attr.value_as_address ();
18432 if (cu->header.version >= 4 && attr.form_is_constant ())
18433 high_pc_relative = 1;
18434 break;
18435 case DW_AT_location:
18436 /* Support the .debug_loc offsets. */
18437 if (attr.form_is_block ())
18438 {
18439 d.locdesc = DW_BLOCK (&attr);
18440 }
18441 else if (attr.form_is_section_offset ())
18442 {
18443 dwarf2_complex_location_expr_complaint ();
18444 }
18445 else
18446 {
18447 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18448 "partial symbol information");
18449 }
18450 break;
18451 case DW_AT_external:
18452 is_external = DW_UNSND (&attr);
18453 break;
18454 case DW_AT_declaration:
18455 is_declaration = DW_UNSND (&attr);
18456 break;
18457 case DW_AT_type:
18458 has_type = 1;
18459 break;
18460 case DW_AT_abstract_origin:
18461 case DW_AT_specification:
18462 case DW_AT_extension:
18463 has_specification = 1;
18464 spec_offset = attr.get_ref_die_offset ();
18465 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18466 || cu->per_cu->is_dwz);
18467 break;
18468 case DW_AT_sibling:
18469 /* Ignore absolute siblings, they might point outside of
18470 the current compile unit. */
18471 if (attr.form == DW_FORM_ref_addr)
18472 complaint (_("ignoring absolute DW_AT_sibling"));
18473 else
18474 {
18475 const gdb_byte *buffer = reader->buffer;
18476 sect_offset off = attr.get_ref_die_offset ();
18477 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18478
18479 if (sibling_ptr < info_ptr)
18480 complaint (_("DW_AT_sibling points backwards"));
18481 else if (sibling_ptr > reader->buffer_end)
18482 reader->die_section->overflow_complaint ();
18483 else
18484 sibling = sibling_ptr;
18485 }
18486 break;
18487 case DW_AT_byte_size:
18488 has_byte_size = 1;
18489 break;
18490 case DW_AT_const_value:
18491 has_const_value = 1;
18492 break;
18493 case DW_AT_calling_convention:
18494 /* DWARF doesn't provide a way to identify a program's source-level
18495 entry point. DW_AT_calling_convention attributes are only meant
18496 to describe functions' calling conventions.
18497
18498 However, because it's a necessary piece of information in
18499 Fortran, and before DWARF 4 DW_CC_program was the only
18500 piece of debugging information whose definition refers to
18501 a 'main program' at all, several compilers marked Fortran
18502 main programs with DW_CC_program --- even when those
18503 functions use the standard calling conventions.
18504
18505 Although DWARF now specifies a way to provide this
18506 information, we support this practice for backward
18507 compatibility. */
18508 if (DW_UNSND (&attr) == DW_CC_program
18509 && cu->language == language_fortran)
18510 main_subprogram = 1;
18511 break;
18512 case DW_AT_inline:
18513 if (DW_UNSND (&attr) == DW_INL_inlined
18514 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18515 may_be_inlined = 1;
18516 break;
18517
18518 case DW_AT_import:
18519 if (tag == DW_TAG_imported_unit)
18520 {
18521 d.sect_off = attr.get_ref_die_offset ();
18522 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18523 || cu->per_cu->is_dwz);
18524 }
18525 break;
18526
18527 case DW_AT_main_subprogram:
18528 main_subprogram = DW_UNSND (&attr);
18529 break;
18530
18531 case DW_AT_ranges:
18532 {
18533 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18534 but that requires a full DIE, so instead we just
18535 reimplement it. */
18536 int need_ranges_base = tag != DW_TAG_compile_unit;
18537 unsigned int ranges_offset = (DW_UNSND (&attr)
18538 + (need_ranges_base
18539 ? cu->ranges_base
18540 : 0));
18541
18542 /* Value of the DW_AT_ranges attribute is the offset in the
18543 .debug_ranges section. */
18544 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18545 nullptr))
18546 has_pc_info = 1;
18547 }
18548 break;
18549
18550 default:
18551 break;
18552 }
18553 }
18554
18555 /* For Ada, if both the name and the linkage name appear, we prefer
18556 the latter. This lets "catch exception" work better, regardless
18557 of the order in which the name and linkage name were emitted.
18558 Really, though, this is just a workaround for the fact that gdb
18559 doesn't store both the name and the linkage name. */
18560 if (cu->language == language_ada && linkage_name != nullptr)
18561 name = linkage_name;
18562
18563 if (high_pc_relative)
18564 highpc += lowpc;
18565
18566 if (has_low_pc_attr && has_high_pc_attr)
18567 {
18568 /* When using the GNU linker, .gnu.linkonce. sections are used to
18569 eliminate duplicate copies of functions and vtables and such.
18570 The linker will arbitrarily choose one and discard the others.
18571 The AT_*_pc values for such functions refer to local labels in
18572 these sections. If the section from that file was discarded, the
18573 labels are not in the output, so the relocs get a value of 0.
18574 If this is a discarded function, mark the pc bounds as invalid,
18575 so that GDB will ignore it. */
18576 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18577 {
18578 struct objfile *objfile = dwarf2_per_objfile->objfile;
18579 struct gdbarch *gdbarch = objfile->arch ();
18580
18581 complaint (_("DW_AT_low_pc %s is zero "
18582 "for DIE at %s [in module %s]"),
18583 paddress (gdbarch, lowpc),
18584 sect_offset_str (sect_off),
18585 objfile_name (objfile));
18586 }
18587 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18588 else if (lowpc >= highpc)
18589 {
18590 struct objfile *objfile = dwarf2_per_objfile->objfile;
18591 struct gdbarch *gdbarch = objfile->arch ();
18592
18593 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18594 "for DIE at %s [in module %s]"),
18595 paddress (gdbarch, lowpc),
18596 paddress (gdbarch, highpc),
18597 sect_offset_str (sect_off),
18598 objfile_name (objfile));
18599 }
18600 else
18601 has_pc_info = 1;
18602 }
18603
18604 return info_ptr;
18605 }
18606
18607 /* Find a cached partial DIE at OFFSET in CU. */
18608
18609 struct partial_die_info *
18610 dwarf2_cu::find_partial_die (sect_offset sect_off)
18611 {
18612 struct partial_die_info *lookup_die = NULL;
18613 struct partial_die_info part_die (sect_off);
18614
18615 lookup_die = ((struct partial_die_info *)
18616 htab_find_with_hash (partial_dies, &part_die,
18617 to_underlying (sect_off)));
18618
18619 return lookup_die;
18620 }
18621
18622 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18623 except in the case of .debug_types DIEs which do not reference
18624 outside their CU (they do however referencing other types via
18625 DW_FORM_ref_sig8). */
18626
18627 static const struct cu_partial_die_info
18628 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18629 {
18630 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18631 struct objfile *objfile = dwarf2_per_objfile->objfile;
18632 struct dwarf2_per_cu_data *per_cu = NULL;
18633 struct partial_die_info *pd = NULL;
18634
18635 if (offset_in_dwz == cu->per_cu->is_dwz
18636 && cu->header.offset_in_cu_p (sect_off))
18637 {
18638 pd = cu->find_partial_die (sect_off);
18639 if (pd != NULL)
18640 return { cu, pd };
18641 /* We missed recording what we needed.
18642 Load all dies and try again. */
18643 per_cu = cu->per_cu;
18644 }
18645 else
18646 {
18647 /* TUs don't reference other CUs/TUs (except via type signatures). */
18648 if (cu->per_cu->is_debug_types)
18649 {
18650 error (_("Dwarf Error: Type Unit at offset %s contains"
18651 " external reference to offset %s [in module %s].\n"),
18652 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18653 bfd_get_filename (objfile->obfd));
18654 }
18655 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18656 dwarf2_per_objfile);
18657
18658 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18659 load_partial_comp_unit (per_cu);
18660
18661 per_cu->cu->last_used = 0;
18662 pd = per_cu->cu->find_partial_die (sect_off);
18663 }
18664
18665 /* If we didn't find it, and not all dies have been loaded,
18666 load them all and try again. */
18667
18668 if (pd == NULL && per_cu->load_all_dies == 0)
18669 {
18670 per_cu->load_all_dies = 1;
18671
18672 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18673 THIS_CU->cu may already be in use. So we can't just free it and
18674 replace its DIEs with the ones we read in. Instead, we leave those
18675 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18676 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18677 set. */
18678 load_partial_comp_unit (per_cu);
18679
18680 pd = per_cu->cu->find_partial_die (sect_off);
18681 }
18682
18683 if (pd == NULL)
18684 internal_error (__FILE__, __LINE__,
18685 _("could not find partial DIE %s "
18686 "in cache [from module %s]\n"),
18687 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18688 return { per_cu->cu, pd };
18689 }
18690
18691 /* See if we can figure out if the class lives in a namespace. We do
18692 this by looking for a member function; its demangled name will
18693 contain namespace info, if there is any. */
18694
18695 static void
18696 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18697 struct dwarf2_cu *cu)
18698 {
18699 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18700 what template types look like, because the demangler
18701 frequently doesn't give the same name as the debug info. We
18702 could fix this by only using the demangled name to get the
18703 prefix (but see comment in read_structure_type). */
18704
18705 struct partial_die_info *real_pdi;
18706 struct partial_die_info *child_pdi;
18707
18708 /* If this DIE (this DIE's specification, if any) has a parent, then
18709 we should not do this. We'll prepend the parent's fully qualified
18710 name when we create the partial symbol. */
18711
18712 real_pdi = struct_pdi;
18713 while (real_pdi->has_specification)
18714 {
18715 auto res = find_partial_die (real_pdi->spec_offset,
18716 real_pdi->spec_is_dwz, cu);
18717 real_pdi = res.pdi;
18718 cu = res.cu;
18719 }
18720
18721 if (real_pdi->die_parent != NULL)
18722 return;
18723
18724 for (child_pdi = struct_pdi->die_child;
18725 child_pdi != NULL;
18726 child_pdi = child_pdi->die_sibling)
18727 {
18728 if (child_pdi->tag == DW_TAG_subprogram
18729 && child_pdi->linkage_name != NULL)
18730 {
18731 gdb::unique_xmalloc_ptr<char> actual_class_name
18732 (language_class_name_from_physname (cu->language_defn,
18733 child_pdi->linkage_name));
18734 if (actual_class_name != NULL)
18735 {
18736 struct objfile *objfile = cu->per_objfile->objfile;
18737 struct_pdi->name = objfile->intern (actual_class_name.get ());
18738 }
18739 break;
18740 }
18741 }
18742 }
18743
18744 /* Return true if a DIE with TAG may have the DW_AT_const_value
18745 attribute. */
18746
18747 static bool
18748 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18749 {
18750 switch (tag)
18751 {
18752 case DW_TAG_constant:
18753 case DW_TAG_enumerator:
18754 case DW_TAG_formal_parameter:
18755 case DW_TAG_template_value_param:
18756 case DW_TAG_variable:
18757 return true;
18758 }
18759
18760 return false;
18761 }
18762
18763 void
18764 partial_die_info::fixup (struct dwarf2_cu *cu)
18765 {
18766 /* Once we've fixed up a die, there's no point in doing so again.
18767 This also avoids a memory leak if we were to call
18768 guess_partial_die_structure_name multiple times. */
18769 if (fixup_called)
18770 return;
18771
18772 /* If we found a reference attribute and the DIE has no name, try
18773 to find a name in the referred to DIE. */
18774
18775 if (name == NULL && has_specification)
18776 {
18777 struct partial_die_info *spec_die;
18778
18779 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18780 spec_die = res.pdi;
18781 cu = res.cu;
18782
18783 spec_die->fixup (cu);
18784
18785 if (spec_die->name)
18786 {
18787 name = spec_die->name;
18788
18789 /* Copy DW_AT_external attribute if it is set. */
18790 if (spec_die->is_external)
18791 is_external = spec_die->is_external;
18792 }
18793 }
18794
18795 if (!has_const_value && has_specification
18796 && can_have_DW_AT_const_value_p (tag))
18797 {
18798 struct partial_die_info *spec_die;
18799
18800 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18801 spec_die = res.pdi;
18802 cu = res.cu;
18803
18804 spec_die->fixup (cu);
18805
18806 if (spec_die->has_const_value)
18807 {
18808 /* Copy DW_AT_const_value attribute if it is set. */
18809 has_const_value = spec_die->has_const_value;
18810 }
18811 }
18812
18813 /* Set default names for some unnamed DIEs. */
18814
18815 if (name == NULL && tag == DW_TAG_namespace)
18816 name = CP_ANONYMOUS_NAMESPACE_STR;
18817
18818 /* If there is no parent die to provide a namespace, and there are
18819 children, see if we can determine the namespace from their linkage
18820 name. */
18821 if (cu->language == language_cplus
18822 && !cu->per_objfile->per_bfd->types.empty ()
18823 && die_parent == NULL
18824 && has_children
18825 && (tag == DW_TAG_class_type
18826 || tag == DW_TAG_structure_type
18827 || tag == DW_TAG_union_type))
18828 guess_partial_die_structure_name (this, cu);
18829
18830 /* GCC might emit a nameless struct or union that has a linkage
18831 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18832 if (name == NULL
18833 && (tag == DW_TAG_class_type
18834 || tag == DW_TAG_interface_type
18835 || tag == DW_TAG_structure_type
18836 || tag == DW_TAG_union_type)
18837 && linkage_name != NULL)
18838 {
18839 gdb::unique_xmalloc_ptr<char> demangled
18840 (gdb_demangle (linkage_name, DMGL_TYPES));
18841 if (demangled != nullptr)
18842 {
18843 const char *base;
18844
18845 /* Strip any leading namespaces/classes, keep only the base name.
18846 DW_AT_name for named DIEs does not contain the prefixes. */
18847 base = strrchr (demangled.get (), ':');
18848 if (base && base > demangled.get () && base[-1] == ':')
18849 base++;
18850 else
18851 base = demangled.get ();
18852
18853 struct objfile *objfile = cu->per_objfile->objfile;
18854 name = objfile->intern (base);
18855 }
18856 }
18857
18858 fixup_called = 1;
18859 }
18860
18861 /* Read the .debug_loclists header contents from the given SECTION in the
18862 HEADER. */
18863 static void
18864 read_loclist_header (struct loclist_header *header,
18865 struct dwarf2_section_info *section)
18866 {
18867 unsigned int bytes_read;
18868 bfd *abfd = section->get_bfd_owner ();
18869 const gdb_byte *info_ptr = section->buffer;
18870 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18871 info_ptr += bytes_read;
18872 header->version = read_2_bytes (abfd, info_ptr);
18873 info_ptr += 2;
18874 header->addr_size = read_1_byte (abfd, info_ptr);
18875 info_ptr += 1;
18876 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18877 info_ptr += 1;
18878 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18879 }
18880
18881 /* Return the DW_AT_loclists_base value for the CU. */
18882 static ULONGEST
18883 lookup_loclist_base (struct dwarf2_cu *cu)
18884 {
18885 /* For the .dwo unit, the loclist_base points to the first offset following
18886 the header. The header consists of the following entities-
18887 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18888 bit format)
18889 2. version (2 bytes)
18890 3. address size (1 byte)
18891 4. segment selector size (1 byte)
18892 5. offset entry count (4 bytes)
18893 These sizes are derived as per the DWARFv5 standard. */
18894 if (cu->dwo_unit != nullptr)
18895 {
18896 if (cu->header.initial_length_size == 4)
18897 return LOCLIST_HEADER_SIZE32;
18898 return LOCLIST_HEADER_SIZE64;
18899 }
18900 return cu->loclist_base;
18901 }
18902
18903 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18904 array of offsets in the .debug_loclists section. */
18905 static CORE_ADDR
18906 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18907 {
18908 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18909 struct objfile *objfile = dwarf2_per_objfile->objfile;
18910 bfd *abfd = objfile->obfd;
18911 ULONGEST loclist_base = lookup_loclist_base (cu);
18912 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18913
18914 section->read (objfile);
18915 if (section->buffer == NULL)
18916 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18917 "section [in module %s]"), objfile_name (objfile));
18918 struct loclist_header header;
18919 read_loclist_header (&header, section);
18920 if (loclist_index >= header.offset_entry_count)
18921 complaint (_("DW_FORM_loclistx pointing outside of "
18922 ".debug_loclists offset array [in module %s]"),
18923 objfile_name (objfile));
18924 if (loclist_base + loclist_index * cu->header.offset_size
18925 >= section->size)
18926 complaint (_("DW_FORM_loclistx pointing outside of "
18927 ".debug_loclists section [in module %s]"),
18928 objfile_name (objfile));
18929 const gdb_byte *info_ptr
18930 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18931
18932 if (cu->header.offset_size == 4)
18933 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18934 else
18935 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18936 }
18937
18938 /* Process the attributes that had to be skipped in the first round. These
18939 attributes are the ones that need str_offsets_base or addr_base attributes.
18940 They could not have been processed in the first round, because at the time
18941 the values of str_offsets_base or addr_base may not have been known. */
18942 static void
18943 read_attribute_reprocess (const struct die_reader_specs *reader,
18944 struct attribute *attr)
18945 {
18946 struct dwarf2_cu *cu = reader->cu;
18947 switch (attr->form)
18948 {
18949 case DW_FORM_addrx:
18950 case DW_FORM_GNU_addr_index:
18951 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18952 break;
18953 case DW_FORM_loclistx:
18954 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18955 break;
18956 case DW_FORM_strx:
18957 case DW_FORM_strx1:
18958 case DW_FORM_strx2:
18959 case DW_FORM_strx3:
18960 case DW_FORM_strx4:
18961 case DW_FORM_GNU_str_index:
18962 {
18963 unsigned int str_index = DW_UNSND (attr);
18964 if (reader->dwo_file != NULL)
18965 {
18966 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18967 DW_STRING_IS_CANONICAL (attr) = 0;
18968 }
18969 else
18970 {
18971 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18972 DW_STRING_IS_CANONICAL (attr) = 0;
18973 }
18974 break;
18975 }
18976 default:
18977 gdb_assert_not_reached (_("Unexpected DWARF form."));
18978 }
18979 }
18980
18981 /* Read an attribute value described by an attribute form. */
18982
18983 static const gdb_byte *
18984 read_attribute_value (const struct die_reader_specs *reader,
18985 struct attribute *attr, unsigned form,
18986 LONGEST implicit_const, const gdb_byte *info_ptr,
18987 bool *need_reprocess)
18988 {
18989 struct dwarf2_cu *cu = reader->cu;
18990 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18991 struct objfile *objfile = dwarf2_per_objfile->objfile;
18992 bfd *abfd = reader->abfd;
18993 struct comp_unit_head *cu_header = &cu->header;
18994 unsigned int bytes_read;
18995 struct dwarf_block *blk;
18996 *need_reprocess = false;
18997
18998 attr->form = (enum dwarf_form) form;
18999 switch (form)
19000 {
19001 case DW_FORM_ref_addr:
19002 if (cu->header.version == 2)
19003 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19004 &bytes_read);
19005 else
19006 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19007 &bytes_read);
19008 info_ptr += bytes_read;
19009 break;
19010 case DW_FORM_GNU_ref_alt:
19011 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19012 info_ptr += bytes_read;
19013 break;
19014 case DW_FORM_addr:
19015 {
19016 struct gdbarch *gdbarch = objfile->arch ();
19017 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19018 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19019 info_ptr += bytes_read;
19020 }
19021 break;
19022 case DW_FORM_block2:
19023 blk = dwarf_alloc_block (cu);
19024 blk->size = read_2_bytes (abfd, info_ptr);
19025 info_ptr += 2;
19026 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19027 info_ptr += blk->size;
19028 DW_BLOCK (attr) = blk;
19029 break;
19030 case DW_FORM_block4:
19031 blk = dwarf_alloc_block (cu);
19032 blk->size = read_4_bytes (abfd, info_ptr);
19033 info_ptr += 4;
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_data2:
19039 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19040 info_ptr += 2;
19041 break;
19042 case DW_FORM_data4:
19043 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19044 info_ptr += 4;
19045 break;
19046 case DW_FORM_data8:
19047 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19048 info_ptr += 8;
19049 break;
19050 case DW_FORM_data16:
19051 blk = dwarf_alloc_block (cu);
19052 blk->size = 16;
19053 blk->data = read_n_bytes (abfd, info_ptr, 16);
19054 info_ptr += 16;
19055 DW_BLOCK (attr) = blk;
19056 break;
19057 case DW_FORM_sec_offset:
19058 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19059 info_ptr += bytes_read;
19060 break;
19061 case DW_FORM_loclistx:
19062 {
19063 *need_reprocess = true;
19064 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19065 info_ptr += bytes_read;
19066 }
19067 break;
19068 case DW_FORM_string:
19069 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19070 DW_STRING_IS_CANONICAL (attr) = 0;
19071 info_ptr += bytes_read;
19072 break;
19073 case DW_FORM_strp:
19074 if (!cu->per_cu->is_dwz)
19075 {
19076 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19077 abfd, info_ptr, cu_header,
19078 &bytes_read);
19079 DW_STRING_IS_CANONICAL (attr) = 0;
19080 info_ptr += bytes_read;
19081 break;
19082 }
19083 /* FALLTHROUGH */
19084 case DW_FORM_line_strp:
19085 if (!cu->per_cu->is_dwz)
19086 {
19087 DW_STRING (attr)
19088 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19089 &bytes_read);
19090 DW_STRING_IS_CANONICAL (attr) = 0;
19091 info_ptr += bytes_read;
19092 break;
19093 }
19094 /* FALLTHROUGH */
19095 case DW_FORM_GNU_strp_alt:
19096 {
19097 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19098 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19099 &bytes_read);
19100
19101 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19102 DW_STRING_IS_CANONICAL (attr) = 0;
19103 info_ptr += bytes_read;
19104 }
19105 break;
19106 case DW_FORM_exprloc:
19107 case DW_FORM_block:
19108 blk = dwarf_alloc_block (cu);
19109 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19110 info_ptr += bytes_read;
19111 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19112 info_ptr += blk->size;
19113 DW_BLOCK (attr) = blk;
19114 break;
19115 case DW_FORM_block1:
19116 blk = dwarf_alloc_block (cu);
19117 blk->size = read_1_byte (abfd, info_ptr);
19118 info_ptr += 1;
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_data1:
19124 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19125 info_ptr += 1;
19126 break;
19127 case DW_FORM_flag:
19128 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19129 info_ptr += 1;
19130 break;
19131 case DW_FORM_flag_present:
19132 DW_UNSND (attr) = 1;
19133 break;
19134 case DW_FORM_sdata:
19135 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19136 info_ptr += bytes_read;
19137 break;
19138 case DW_FORM_udata:
19139 case DW_FORM_rnglistx:
19140 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19141 info_ptr += bytes_read;
19142 break;
19143 case DW_FORM_ref1:
19144 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19145 + read_1_byte (abfd, info_ptr));
19146 info_ptr += 1;
19147 break;
19148 case DW_FORM_ref2:
19149 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19150 + read_2_bytes (abfd, info_ptr));
19151 info_ptr += 2;
19152 break;
19153 case DW_FORM_ref4:
19154 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19155 + read_4_bytes (abfd, info_ptr));
19156 info_ptr += 4;
19157 break;
19158 case DW_FORM_ref8:
19159 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19160 + read_8_bytes (abfd, info_ptr));
19161 info_ptr += 8;
19162 break;
19163 case DW_FORM_ref_sig8:
19164 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19165 info_ptr += 8;
19166 break;
19167 case DW_FORM_ref_udata:
19168 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19169 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19170 info_ptr += bytes_read;
19171 break;
19172 case DW_FORM_indirect:
19173 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19174 info_ptr += bytes_read;
19175 if (form == DW_FORM_implicit_const)
19176 {
19177 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19178 info_ptr += bytes_read;
19179 }
19180 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19181 info_ptr, need_reprocess);
19182 break;
19183 case DW_FORM_implicit_const:
19184 DW_SND (attr) = implicit_const;
19185 break;
19186 case DW_FORM_addrx:
19187 case DW_FORM_GNU_addr_index:
19188 *need_reprocess = true;
19189 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19190 info_ptr += bytes_read;
19191 break;
19192 case DW_FORM_strx:
19193 case DW_FORM_strx1:
19194 case DW_FORM_strx2:
19195 case DW_FORM_strx3:
19196 case DW_FORM_strx4:
19197 case DW_FORM_GNU_str_index:
19198 {
19199 ULONGEST str_index;
19200 if (form == DW_FORM_strx1)
19201 {
19202 str_index = read_1_byte (abfd, info_ptr);
19203 info_ptr += 1;
19204 }
19205 else if (form == DW_FORM_strx2)
19206 {
19207 str_index = read_2_bytes (abfd, info_ptr);
19208 info_ptr += 2;
19209 }
19210 else if (form == DW_FORM_strx3)
19211 {
19212 str_index = read_3_bytes (abfd, info_ptr);
19213 info_ptr += 3;
19214 }
19215 else if (form == DW_FORM_strx4)
19216 {
19217 str_index = read_4_bytes (abfd, info_ptr);
19218 info_ptr += 4;
19219 }
19220 else
19221 {
19222 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19223 info_ptr += bytes_read;
19224 }
19225 *need_reprocess = true;
19226 DW_UNSND (attr) = str_index;
19227 }
19228 break;
19229 default:
19230 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19231 dwarf_form_name (form),
19232 bfd_get_filename (abfd));
19233 }
19234
19235 /* Super hack. */
19236 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19237 attr->form = DW_FORM_GNU_ref_alt;
19238
19239 /* We have seen instances where the compiler tried to emit a byte
19240 size attribute of -1 which ended up being encoded as an unsigned
19241 0xffffffff. Although 0xffffffff is technically a valid size value,
19242 an object of this size seems pretty unlikely so we can relatively
19243 safely treat these cases as if the size attribute was invalid and
19244 treat them as zero by default. */
19245 if (attr->name == DW_AT_byte_size
19246 && form == DW_FORM_data4
19247 && DW_UNSND (attr) >= 0xffffffff)
19248 {
19249 complaint
19250 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19251 hex_string (DW_UNSND (attr)));
19252 DW_UNSND (attr) = 0;
19253 }
19254
19255 return info_ptr;
19256 }
19257
19258 /* Read an attribute described by an abbreviated attribute. */
19259
19260 static const gdb_byte *
19261 read_attribute (const struct die_reader_specs *reader,
19262 struct attribute *attr, struct attr_abbrev *abbrev,
19263 const gdb_byte *info_ptr, bool *need_reprocess)
19264 {
19265 attr->name = abbrev->name;
19266 return read_attribute_value (reader, attr, abbrev->form,
19267 abbrev->implicit_const, info_ptr,
19268 need_reprocess);
19269 }
19270
19271 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19272
19273 static const char *
19274 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19275 LONGEST str_offset)
19276 {
19277 return dwarf2_per_objfile->per_bfd->str.read_string
19278 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19279 }
19280
19281 /* Return pointer to string at .debug_str offset as read from BUF.
19282 BUF is assumed to be in a compilation unit described by CU_HEADER.
19283 Return *BYTES_READ_PTR count of bytes read from BUF. */
19284
19285 static const char *
19286 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19287 const gdb_byte *buf,
19288 const struct comp_unit_head *cu_header,
19289 unsigned int *bytes_read_ptr)
19290 {
19291 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19292
19293 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19294 }
19295
19296 /* See read.h. */
19297
19298 const char *
19299 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19300 const struct comp_unit_head *cu_header,
19301 unsigned int *bytes_read_ptr)
19302 {
19303 bfd *abfd = objfile->obfd;
19304 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19305
19306 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19307 }
19308
19309 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19310 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19311 ADDR_SIZE is the size of addresses from the CU header. */
19312
19313 static CORE_ADDR
19314 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19315 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19316 int addr_size)
19317 {
19318 struct objfile *objfile = dwarf2_per_objfile->objfile;
19319 bfd *abfd = objfile->obfd;
19320 const gdb_byte *info_ptr;
19321 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19322
19323 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19324 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19325 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19326 objfile_name (objfile));
19327 if (addr_base_or_zero + addr_index * addr_size
19328 >= dwarf2_per_objfile->per_bfd->addr.size)
19329 error (_("DW_FORM_addr_index pointing outside of "
19330 ".debug_addr section [in module %s]"),
19331 objfile_name (objfile));
19332 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19333 + addr_base_or_zero + addr_index * addr_size);
19334 if (addr_size == 4)
19335 return bfd_get_32 (abfd, info_ptr);
19336 else
19337 return bfd_get_64 (abfd, info_ptr);
19338 }
19339
19340 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19341
19342 static CORE_ADDR
19343 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19344 {
19345 return read_addr_index_1 (cu->per_objfile, addr_index,
19346 cu->addr_base, cu->header.addr_size);
19347 }
19348
19349 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19350
19351 static CORE_ADDR
19352 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19353 unsigned int *bytes_read)
19354 {
19355 bfd *abfd = cu->per_objfile->objfile->obfd;
19356 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19357
19358 return read_addr_index (cu, addr_index);
19359 }
19360
19361 /* See read.h. */
19362
19363 CORE_ADDR
19364 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
19365 {
19366 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19367 struct dwarf2_cu *cu = per_cu->cu;
19368 gdb::optional<ULONGEST> addr_base;
19369 int addr_size;
19370
19371 /* We need addr_base and addr_size.
19372 If we don't have PER_CU->cu, we have to get it.
19373 Nasty, but the alternative is storing the needed info in PER_CU,
19374 which at this point doesn't seem justified: it's not clear how frequently
19375 it would get used and it would increase the size of every PER_CU.
19376 Entry points like dwarf2_per_cu_addr_size do a similar thing
19377 so we're not in uncharted territory here.
19378 Alas we need to be a bit more complicated as addr_base is contained
19379 in the DIE.
19380
19381 We don't need to read the entire CU(/TU).
19382 We just need the header and top level die.
19383
19384 IWBN to use the aging mechanism to let us lazily later discard the CU.
19385 For now we skip this optimization. */
19386
19387 if (cu != NULL)
19388 {
19389 addr_base = cu->addr_base;
19390 addr_size = cu->header.addr_size;
19391 }
19392 else
19393 {
19394 cutu_reader reader (per_cu, NULL, 0, false);
19395 addr_base = reader.cu->addr_base;
19396 addr_size = reader.cu->header.addr_size;
19397 }
19398
19399 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19400 addr_size);
19401 }
19402
19403 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19404 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19405 DWO file. */
19406
19407 static const char *
19408 read_str_index (struct dwarf2_cu *cu,
19409 struct dwarf2_section_info *str_section,
19410 struct dwarf2_section_info *str_offsets_section,
19411 ULONGEST str_offsets_base, ULONGEST str_index)
19412 {
19413 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19414 struct objfile *objfile = dwarf2_per_objfile->objfile;
19415 const char *objf_name = objfile_name (objfile);
19416 bfd *abfd = objfile->obfd;
19417 const gdb_byte *info_ptr;
19418 ULONGEST str_offset;
19419 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19420
19421 str_section->read (objfile);
19422 str_offsets_section->read (objfile);
19423 if (str_section->buffer == NULL)
19424 error (_("%s used without %s section"
19425 " in CU at offset %s [in module %s]"),
19426 form_name, str_section->get_name (),
19427 sect_offset_str (cu->header.sect_off), objf_name);
19428 if (str_offsets_section->buffer == NULL)
19429 error (_("%s used without %s section"
19430 " in CU at offset %s [in module %s]"),
19431 form_name, str_section->get_name (),
19432 sect_offset_str (cu->header.sect_off), objf_name);
19433 info_ptr = (str_offsets_section->buffer
19434 + str_offsets_base
19435 + str_index * cu->header.offset_size);
19436 if (cu->header.offset_size == 4)
19437 str_offset = bfd_get_32 (abfd, info_ptr);
19438 else
19439 str_offset = bfd_get_64 (abfd, info_ptr);
19440 if (str_offset >= str_section->size)
19441 error (_("Offset from %s pointing outside of"
19442 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19443 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19444 return (const char *) (str_section->buffer + str_offset);
19445 }
19446
19447 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19448
19449 static const char *
19450 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19451 {
19452 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19453 ? reader->cu->header.addr_size : 0;
19454 return read_str_index (reader->cu,
19455 &reader->dwo_file->sections.str,
19456 &reader->dwo_file->sections.str_offsets,
19457 str_offsets_base, str_index);
19458 }
19459
19460 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19461
19462 static const char *
19463 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19464 {
19465 struct objfile *objfile = cu->per_objfile->objfile;
19466 const char *objf_name = objfile_name (objfile);
19467 static const char form_name[] = "DW_FORM_GNU_str_index";
19468 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19469
19470 if (!cu->str_offsets_base.has_value ())
19471 error (_("%s used in Fission stub without %s"
19472 " in CU at offset 0x%lx [in module %s]"),
19473 form_name, str_offsets_attr_name,
19474 (long) cu->header.offset_size, objf_name);
19475
19476 return read_str_index (cu,
19477 &cu->per_objfile->per_bfd->str,
19478 &cu->per_objfile->per_bfd->str_offsets,
19479 *cu->str_offsets_base, str_index);
19480 }
19481
19482 /* Return the length of an LEB128 number in BUF. */
19483
19484 static int
19485 leb128_size (const gdb_byte *buf)
19486 {
19487 const gdb_byte *begin = buf;
19488 gdb_byte byte;
19489
19490 while (1)
19491 {
19492 byte = *buf++;
19493 if ((byte & 128) == 0)
19494 return buf - begin;
19495 }
19496 }
19497
19498 static void
19499 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19500 {
19501 switch (lang)
19502 {
19503 case DW_LANG_C89:
19504 case DW_LANG_C99:
19505 case DW_LANG_C11:
19506 case DW_LANG_C:
19507 case DW_LANG_UPC:
19508 cu->language = language_c;
19509 break;
19510 case DW_LANG_Java:
19511 case DW_LANG_C_plus_plus:
19512 case DW_LANG_C_plus_plus_11:
19513 case DW_LANG_C_plus_plus_14:
19514 cu->language = language_cplus;
19515 break;
19516 case DW_LANG_D:
19517 cu->language = language_d;
19518 break;
19519 case DW_LANG_Fortran77:
19520 case DW_LANG_Fortran90:
19521 case DW_LANG_Fortran95:
19522 case DW_LANG_Fortran03:
19523 case DW_LANG_Fortran08:
19524 cu->language = language_fortran;
19525 break;
19526 case DW_LANG_Go:
19527 cu->language = language_go;
19528 break;
19529 case DW_LANG_Mips_Assembler:
19530 cu->language = language_asm;
19531 break;
19532 case DW_LANG_Ada83:
19533 case DW_LANG_Ada95:
19534 cu->language = language_ada;
19535 break;
19536 case DW_LANG_Modula2:
19537 cu->language = language_m2;
19538 break;
19539 case DW_LANG_Pascal83:
19540 cu->language = language_pascal;
19541 break;
19542 case DW_LANG_ObjC:
19543 cu->language = language_objc;
19544 break;
19545 case DW_LANG_Rust:
19546 case DW_LANG_Rust_old:
19547 cu->language = language_rust;
19548 break;
19549 case DW_LANG_Cobol74:
19550 case DW_LANG_Cobol85:
19551 default:
19552 cu->language = language_minimal;
19553 break;
19554 }
19555 cu->language_defn = language_def (cu->language);
19556 }
19557
19558 /* Return the named attribute or NULL if not there. */
19559
19560 static struct attribute *
19561 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19562 {
19563 for (;;)
19564 {
19565 unsigned int i;
19566 struct attribute *spec = NULL;
19567
19568 for (i = 0; i < die->num_attrs; ++i)
19569 {
19570 if (die->attrs[i].name == name)
19571 return &die->attrs[i];
19572 if (die->attrs[i].name == DW_AT_specification
19573 || die->attrs[i].name == DW_AT_abstract_origin)
19574 spec = &die->attrs[i];
19575 }
19576
19577 if (!spec)
19578 break;
19579
19580 die = follow_die_ref (die, spec, &cu);
19581 }
19582
19583 return NULL;
19584 }
19585
19586 /* Return the string associated with a string-typed attribute, or NULL if it
19587 is either not found or is of an incorrect type. */
19588
19589 static const char *
19590 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19591 {
19592 struct attribute *attr;
19593 const char *str = NULL;
19594
19595 attr = dwarf2_attr (die, name, cu);
19596
19597 if (attr != NULL)
19598 {
19599 str = attr->value_as_string ();
19600 if (str == nullptr)
19601 complaint (_("string type expected for attribute %s for "
19602 "DIE at %s in module %s"),
19603 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19604 objfile_name (cu->per_objfile->objfile));
19605 }
19606
19607 return str;
19608 }
19609
19610 /* Return the dwo name or NULL if not present. If present, it is in either
19611 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19612 static const char *
19613 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19614 {
19615 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19616 if (dwo_name == nullptr)
19617 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19618 return dwo_name;
19619 }
19620
19621 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19622 and holds a non-zero value. This function should only be used for
19623 DW_FORM_flag or DW_FORM_flag_present attributes. */
19624
19625 static int
19626 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19627 {
19628 struct attribute *attr = dwarf2_attr (die, name, cu);
19629
19630 return (attr && DW_UNSND (attr));
19631 }
19632
19633 static int
19634 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19635 {
19636 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19637 which value is non-zero. However, we have to be careful with
19638 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19639 (via dwarf2_flag_true_p) follows this attribute. So we may
19640 end up accidently finding a declaration attribute that belongs
19641 to a different DIE referenced by the specification attribute,
19642 even though the given DIE does not have a declaration attribute. */
19643 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19644 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19645 }
19646
19647 /* Return the die giving the specification for DIE, if there is
19648 one. *SPEC_CU is the CU containing DIE on input, and the CU
19649 containing the return value on output. If there is no
19650 specification, but there is an abstract origin, that is
19651 returned. */
19652
19653 static struct die_info *
19654 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19655 {
19656 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19657 *spec_cu);
19658
19659 if (spec_attr == NULL)
19660 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19661
19662 if (spec_attr == NULL)
19663 return NULL;
19664 else
19665 return follow_die_ref (die, spec_attr, spec_cu);
19666 }
19667
19668 /* Stub for free_line_header to match void * callback types. */
19669
19670 static void
19671 free_line_header_voidp (void *arg)
19672 {
19673 struct line_header *lh = (struct line_header *) arg;
19674
19675 delete lh;
19676 }
19677
19678 /* A convenience function to find the proper .debug_line section for a CU. */
19679
19680 static struct dwarf2_section_info *
19681 get_debug_line_section (struct dwarf2_cu *cu)
19682 {
19683 struct dwarf2_section_info *section;
19684 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19685
19686 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19687 DWO file. */
19688 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19689 section = &cu->dwo_unit->dwo_file->sections.line;
19690 else if (cu->per_cu->is_dwz)
19691 {
19692 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19693
19694 section = &dwz->line;
19695 }
19696 else
19697 section = &dwarf2_per_objfile->per_bfd->line;
19698
19699 return section;
19700 }
19701
19702 /* Read the statement program header starting at OFFSET in
19703 .debug_line, or .debug_line.dwo. Return a pointer
19704 to a struct line_header, allocated using xmalloc.
19705 Returns NULL if there is a problem reading the header, e.g., if it
19706 has a version we don't understand.
19707
19708 NOTE: the strings in the include directory and file name tables of
19709 the returned object point into the dwarf line section buffer,
19710 and must not be freed. */
19711
19712 static line_header_up
19713 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19714 {
19715 struct dwarf2_section_info *section;
19716 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19717
19718 section = get_debug_line_section (cu);
19719 section->read (dwarf2_per_objfile->objfile);
19720 if (section->buffer == NULL)
19721 {
19722 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19723 complaint (_("missing .debug_line.dwo section"));
19724 else
19725 complaint (_("missing .debug_line section"));
19726 return 0;
19727 }
19728
19729 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19730 dwarf2_per_objfile, section,
19731 &cu->header);
19732 }
19733
19734 /* Subroutine of dwarf_decode_lines to simplify it.
19735 Return the file name of the psymtab for the given file_entry.
19736 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19737 If space for the result is malloc'd, *NAME_HOLDER will be set.
19738 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19739
19740 static const char *
19741 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19742 const dwarf2_psymtab *pst,
19743 const char *comp_dir,
19744 gdb::unique_xmalloc_ptr<char> *name_holder)
19745 {
19746 const char *include_name = fe.name;
19747 const char *include_name_to_compare = include_name;
19748 const char *pst_filename;
19749 int file_is_pst;
19750
19751 const char *dir_name = fe.include_dir (lh);
19752
19753 gdb::unique_xmalloc_ptr<char> hold_compare;
19754 if (!IS_ABSOLUTE_PATH (include_name)
19755 && (dir_name != NULL || comp_dir != NULL))
19756 {
19757 /* Avoid creating a duplicate psymtab for PST.
19758 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19759 Before we do the comparison, however, we need to account
19760 for DIR_NAME and COMP_DIR.
19761 First prepend dir_name (if non-NULL). If we still don't
19762 have an absolute path prepend comp_dir (if non-NULL).
19763 However, the directory we record in the include-file's
19764 psymtab does not contain COMP_DIR (to match the
19765 corresponding symtab(s)).
19766
19767 Example:
19768
19769 bash$ cd /tmp
19770 bash$ gcc -g ./hello.c
19771 include_name = "hello.c"
19772 dir_name = "."
19773 DW_AT_comp_dir = comp_dir = "/tmp"
19774 DW_AT_name = "./hello.c"
19775
19776 */
19777
19778 if (dir_name != NULL)
19779 {
19780 name_holder->reset (concat (dir_name, SLASH_STRING,
19781 include_name, (char *) NULL));
19782 include_name = name_holder->get ();
19783 include_name_to_compare = include_name;
19784 }
19785 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19786 {
19787 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19788 include_name, (char *) NULL));
19789 include_name_to_compare = hold_compare.get ();
19790 }
19791 }
19792
19793 pst_filename = pst->filename;
19794 gdb::unique_xmalloc_ptr<char> copied_name;
19795 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19796 {
19797 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19798 pst_filename, (char *) NULL));
19799 pst_filename = copied_name.get ();
19800 }
19801
19802 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19803
19804 if (file_is_pst)
19805 return NULL;
19806 return include_name;
19807 }
19808
19809 /* State machine to track the state of the line number program. */
19810
19811 class lnp_state_machine
19812 {
19813 public:
19814 /* Initialize a machine state for the start of a line number
19815 program. */
19816 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19817 bool record_lines_p);
19818
19819 file_entry *current_file ()
19820 {
19821 /* lh->file_names is 0-based, but the file name numbers in the
19822 statement program are 1-based. */
19823 return m_line_header->file_name_at (m_file);
19824 }
19825
19826 /* Record the line in the state machine. END_SEQUENCE is true if
19827 we're processing the end of a sequence. */
19828 void record_line (bool end_sequence);
19829
19830 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19831 nop-out rest of the lines in this sequence. */
19832 void check_line_address (struct dwarf2_cu *cu,
19833 const gdb_byte *line_ptr,
19834 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19835
19836 void handle_set_discriminator (unsigned int discriminator)
19837 {
19838 m_discriminator = discriminator;
19839 m_line_has_non_zero_discriminator |= discriminator != 0;
19840 }
19841
19842 /* Handle DW_LNE_set_address. */
19843 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19844 {
19845 m_op_index = 0;
19846 address += baseaddr;
19847 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19848 }
19849
19850 /* Handle DW_LNS_advance_pc. */
19851 void handle_advance_pc (CORE_ADDR adjust);
19852
19853 /* Handle a special opcode. */
19854 void handle_special_opcode (unsigned char op_code);
19855
19856 /* Handle DW_LNS_advance_line. */
19857 void handle_advance_line (int line_delta)
19858 {
19859 advance_line (line_delta);
19860 }
19861
19862 /* Handle DW_LNS_set_file. */
19863 void handle_set_file (file_name_index file);
19864
19865 /* Handle DW_LNS_negate_stmt. */
19866 void handle_negate_stmt ()
19867 {
19868 m_is_stmt = !m_is_stmt;
19869 }
19870
19871 /* Handle DW_LNS_const_add_pc. */
19872 void handle_const_add_pc ();
19873
19874 /* Handle DW_LNS_fixed_advance_pc. */
19875 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19876 {
19877 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19878 m_op_index = 0;
19879 }
19880
19881 /* Handle DW_LNS_copy. */
19882 void handle_copy ()
19883 {
19884 record_line (false);
19885 m_discriminator = 0;
19886 }
19887
19888 /* Handle DW_LNE_end_sequence. */
19889 void handle_end_sequence ()
19890 {
19891 m_currently_recording_lines = true;
19892 }
19893
19894 private:
19895 /* Advance the line by LINE_DELTA. */
19896 void advance_line (int line_delta)
19897 {
19898 m_line += line_delta;
19899
19900 if (line_delta != 0)
19901 m_line_has_non_zero_discriminator = m_discriminator != 0;
19902 }
19903
19904 struct dwarf2_cu *m_cu;
19905
19906 gdbarch *m_gdbarch;
19907
19908 /* True if we're recording lines.
19909 Otherwise we're building partial symtabs and are just interested in
19910 finding include files mentioned by the line number program. */
19911 bool m_record_lines_p;
19912
19913 /* The line number header. */
19914 line_header *m_line_header;
19915
19916 /* These are part of the standard DWARF line number state machine,
19917 and initialized according to the DWARF spec. */
19918
19919 unsigned char m_op_index = 0;
19920 /* The line table index of the current file. */
19921 file_name_index m_file = 1;
19922 unsigned int m_line = 1;
19923
19924 /* These are initialized in the constructor. */
19925
19926 CORE_ADDR m_address;
19927 bool m_is_stmt;
19928 unsigned int m_discriminator;
19929
19930 /* Additional bits of state we need to track. */
19931
19932 /* The last file that we called dwarf2_start_subfile for.
19933 This is only used for TLLs. */
19934 unsigned int m_last_file = 0;
19935 /* The last file a line number was recorded for. */
19936 struct subfile *m_last_subfile = NULL;
19937
19938 /* When true, record the lines we decode. */
19939 bool m_currently_recording_lines = false;
19940
19941 /* The last line number that was recorded, used to coalesce
19942 consecutive entries for the same line. This can happen, for
19943 example, when discriminators are present. PR 17276. */
19944 unsigned int m_last_line = 0;
19945 bool m_line_has_non_zero_discriminator = false;
19946 };
19947
19948 void
19949 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19950 {
19951 CORE_ADDR addr_adj = (((m_op_index + adjust)
19952 / m_line_header->maximum_ops_per_instruction)
19953 * m_line_header->minimum_instruction_length);
19954 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19955 m_op_index = ((m_op_index + adjust)
19956 % m_line_header->maximum_ops_per_instruction);
19957 }
19958
19959 void
19960 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19961 {
19962 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19963 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19964 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19965 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19966 / m_line_header->maximum_ops_per_instruction)
19967 * m_line_header->minimum_instruction_length);
19968 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19969 m_op_index = ((m_op_index + adj_opcode_d)
19970 % m_line_header->maximum_ops_per_instruction);
19971
19972 int line_delta = m_line_header->line_base + adj_opcode_r;
19973 advance_line (line_delta);
19974 record_line (false);
19975 m_discriminator = 0;
19976 }
19977
19978 void
19979 lnp_state_machine::handle_set_file (file_name_index file)
19980 {
19981 m_file = file;
19982
19983 const file_entry *fe = current_file ();
19984 if (fe == NULL)
19985 dwarf2_debug_line_missing_file_complaint ();
19986 else if (m_record_lines_p)
19987 {
19988 const char *dir = fe->include_dir (m_line_header);
19989
19990 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19991 m_line_has_non_zero_discriminator = m_discriminator != 0;
19992 dwarf2_start_subfile (m_cu, fe->name, dir);
19993 }
19994 }
19995
19996 void
19997 lnp_state_machine::handle_const_add_pc ()
19998 {
19999 CORE_ADDR adjust
20000 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20001
20002 CORE_ADDR addr_adj
20003 = (((m_op_index + adjust)
20004 / m_line_header->maximum_ops_per_instruction)
20005 * m_line_header->minimum_instruction_length);
20006
20007 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20008 m_op_index = ((m_op_index + adjust)
20009 % m_line_header->maximum_ops_per_instruction);
20010 }
20011
20012 /* Return non-zero if we should add LINE to the line number table.
20013 LINE is the line to add, LAST_LINE is the last line that was added,
20014 LAST_SUBFILE is the subfile for LAST_LINE.
20015 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20016 had a non-zero discriminator.
20017
20018 We have to be careful in the presence of discriminators.
20019 E.g., for this line:
20020
20021 for (i = 0; i < 100000; i++);
20022
20023 clang can emit four line number entries for that one line,
20024 each with a different discriminator.
20025 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20026
20027 However, we want gdb to coalesce all four entries into one.
20028 Otherwise the user could stepi into the middle of the line and
20029 gdb would get confused about whether the pc really was in the
20030 middle of the line.
20031
20032 Things are further complicated by the fact that two consecutive
20033 line number entries for the same line is a heuristic used by gcc
20034 to denote the end of the prologue. So we can't just discard duplicate
20035 entries, we have to be selective about it. The heuristic we use is
20036 that we only collapse consecutive entries for the same line if at least
20037 one of those entries has a non-zero discriminator. PR 17276.
20038
20039 Note: Addresses in the line number state machine can never go backwards
20040 within one sequence, thus this coalescing is ok. */
20041
20042 static int
20043 dwarf_record_line_p (struct dwarf2_cu *cu,
20044 unsigned int line, unsigned int last_line,
20045 int line_has_non_zero_discriminator,
20046 struct subfile *last_subfile)
20047 {
20048 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20049 return 1;
20050 if (line != last_line)
20051 return 1;
20052 /* Same line for the same file that we've seen already.
20053 As a last check, for pr 17276, only record the line if the line
20054 has never had a non-zero discriminator. */
20055 if (!line_has_non_zero_discriminator)
20056 return 1;
20057 return 0;
20058 }
20059
20060 /* Use the CU's builder to record line number LINE beginning at
20061 address ADDRESS in the line table of subfile SUBFILE. */
20062
20063 static void
20064 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20065 unsigned int line, CORE_ADDR address, bool is_stmt,
20066 struct dwarf2_cu *cu)
20067 {
20068 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20069
20070 if (dwarf_line_debug)
20071 {
20072 fprintf_unfiltered (gdb_stdlog,
20073 "Recording line %u, file %s, address %s\n",
20074 line, lbasename (subfile->name),
20075 paddress (gdbarch, address));
20076 }
20077
20078 if (cu != nullptr)
20079 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20080 }
20081
20082 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20083 Mark the end of a set of line number records.
20084 The arguments are the same as for dwarf_record_line_1.
20085 If SUBFILE is NULL the request is ignored. */
20086
20087 static void
20088 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20089 CORE_ADDR address, struct dwarf2_cu *cu)
20090 {
20091 if (subfile == NULL)
20092 return;
20093
20094 if (dwarf_line_debug)
20095 {
20096 fprintf_unfiltered (gdb_stdlog,
20097 "Finishing current line, file %s, address %s\n",
20098 lbasename (subfile->name),
20099 paddress (gdbarch, address));
20100 }
20101
20102 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20103 }
20104
20105 void
20106 lnp_state_machine::record_line (bool end_sequence)
20107 {
20108 if (dwarf_line_debug)
20109 {
20110 fprintf_unfiltered (gdb_stdlog,
20111 "Processing actual line %u: file %u,"
20112 " address %s, is_stmt %u, discrim %u%s\n",
20113 m_line, m_file,
20114 paddress (m_gdbarch, m_address),
20115 m_is_stmt, m_discriminator,
20116 (end_sequence ? "\t(end sequence)" : ""));
20117 }
20118
20119 file_entry *fe = current_file ();
20120
20121 if (fe == NULL)
20122 dwarf2_debug_line_missing_file_complaint ();
20123 /* For now we ignore lines not starting on an instruction boundary.
20124 But not when processing end_sequence for compatibility with the
20125 previous version of the code. */
20126 else if (m_op_index == 0 || end_sequence)
20127 {
20128 fe->included_p = 1;
20129 if (m_record_lines_p)
20130 {
20131 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20132 || end_sequence)
20133 {
20134 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20135 m_currently_recording_lines ? m_cu : nullptr);
20136 }
20137
20138 if (!end_sequence)
20139 {
20140 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20141
20142 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20143 m_line_has_non_zero_discriminator,
20144 m_last_subfile))
20145 {
20146 buildsym_compunit *builder = m_cu->get_builder ();
20147 dwarf_record_line_1 (m_gdbarch,
20148 builder->get_current_subfile (),
20149 m_line, m_address, is_stmt,
20150 m_currently_recording_lines ? m_cu : nullptr);
20151 }
20152 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20153 m_last_line = m_line;
20154 }
20155 }
20156 }
20157 }
20158
20159 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20160 line_header *lh, bool record_lines_p)
20161 {
20162 m_cu = cu;
20163 m_gdbarch = arch;
20164 m_record_lines_p = record_lines_p;
20165 m_line_header = lh;
20166
20167 m_currently_recording_lines = true;
20168
20169 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20170 was a line entry for it so that the backend has a chance to adjust it
20171 and also record it in case it needs it. This is currently used by MIPS
20172 code, cf. `mips_adjust_dwarf2_line'. */
20173 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20174 m_is_stmt = lh->default_is_stmt;
20175 m_discriminator = 0;
20176 }
20177
20178 void
20179 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20180 const gdb_byte *line_ptr,
20181 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20182 {
20183 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20184 the pc range of the CU. However, we restrict the test to only ADDRESS
20185 values of zero to preserve GDB's previous behaviour which is to handle
20186 the specific case of a function being GC'd by the linker. */
20187
20188 if (address == 0 && address < unrelocated_lowpc)
20189 {
20190 /* This line table is for a function which has been
20191 GCd by the linker. Ignore it. PR gdb/12528 */
20192
20193 struct objfile *objfile = cu->per_objfile->objfile;
20194 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20195
20196 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20197 line_offset, objfile_name (objfile));
20198 m_currently_recording_lines = false;
20199 /* Note: m_currently_recording_lines is left as false until we see
20200 DW_LNE_end_sequence. */
20201 }
20202 }
20203
20204 /* Subroutine of dwarf_decode_lines to simplify it.
20205 Process the line number information in LH.
20206 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20207 program in order to set included_p for every referenced header. */
20208
20209 static void
20210 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20211 const int decode_for_pst_p, CORE_ADDR lowpc)
20212 {
20213 const gdb_byte *line_ptr, *extended_end;
20214 const gdb_byte *line_end;
20215 unsigned int bytes_read, extended_len;
20216 unsigned char op_code, extended_op;
20217 CORE_ADDR baseaddr;
20218 struct objfile *objfile = cu->per_objfile->objfile;
20219 bfd *abfd = objfile->obfd;
20220 struct gdbarch *gdbarch = objfile->arch ();
20221 /* True if we're recording line info (as opposed to building partial
20222 symtabs and just interested in finding include files mentioned by
20223 the line number program). */
20224 bool record_lines_p = !decode_for_pst_p;
20225
20226 baseaddr = objfile->text_section_offset ();
20227
20228 line_ptr = lh->statement_program_start;
20229 line_end = lh->statement_program_end;
20230
20231 /* Read the statement sequences until there's nothing left. */
20232 while (line_ptr < line_end)
20233 {
20234 /* The DWARF line number program state machine. Reset the state
20235 machine at the start of each sequence. */
20236 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20237 bool end_sequence = false;
20238
20239 if (record_lines_p)
20240 {
20241 /* Start a subfile for the current file of the state
20242 machine. */
20243 const file_entry *fe = state_machine.current_file ();
20244
20245 if (fe != NULL)
20246 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20247 }
20248
20249 /* Decode the table. */
20250 while (line_ptr < line_end && !end_sequence)
20251 {
20252 op_code = read_1_byte (abfd, line_ptr);
20253 line_ptr += 1;
20254
20255 if (op_code >= lh->opcode_base)
20256 {
20257 /* Special opcode. */
20258 state_machine.handle_special_opcode (op_code);
20259 }
20260 else switch (op_code)
20261 {
20262 case DW_LNS_extended_op:
20263 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20264 &bytes_read);
20265 line_ptr += bytes_read;
20266 extended_end = line_ptr + extended_len;
20267 extended_op = read_1_byte (abfd, line_ptr);
20268 line_ptr += 1;
20269 switch (extended_op)
20270 {
20271 case DW_LNE_end_sequence:
20272 state_machine.handle_end_sequence ();
20273 end_sequence = true;
20274 break;
20275 case DW_LNE_set_address:
20276 {
20277 CORE_ADDR address
20278 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20279 line_ptr += bytes_read;
20280
20281 state_machine.check_line_address (cu, line_ptr,
20282 lowpc - baseaddr, address);
20283 state_machine.handle_set_address (baseaddr, address);
20284 }
20285 break;
20286 case DW_LNE_define_file:
20287 {
20288 const char *cur_file;
20289 unsigned int mod_time, length;
20290 dir_index dindex;
20291
20292 cur_file = read_direct_string (abfd, line_ptr,
20293 &bytes_read);
20294 line_ptr += bytes_read;
20295 dindex = (dir_index)
20296 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20297 line_ptr += bytes_read;
20298 mod_time =
20299 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20300 line_ptr += bytes_read;
20301 length =
20302 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20303 line_ptr += bytes_read;
20304 lh->add_file_name (cur_file, dindex, mod_time, length);
20305 }
20306 break;
20307 case DW_LNE_set_discriminator:
20308 {
20309 /* The discriminator is not interesting to the
20310 debugger; just ignore it. We still need to
20311 check its value though:
20312 if there are consecutive entries for the same
20313 (non-prologue) line we want to coalesce them.
20314 PR 17276. */
20315 unsigned int discr
20316 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20317 line_ptr += bytes_read;
20318
20319 state_machine.handle_set_discriminator (discr);
20320 }
20321 break;
20322 default:
20323 complaint (_("mangled .debug_line section"));
20324 return;
20325 }
20326 /* Make sure that we parsed the extended op correctly. If e.g.
20327 we expected a different address size than the producer used,
20328 we may have read the wrong number of bytes. */
20329 if (line_ptr != extended_end)
20330 {
20331 complaint (_("mangled .debug_line section"));
20332 return;
20333 }
20334 break;
20335 case DW_LNS_copy:
20336 state_machine.handle_copy ();
20337 break;
20338 case DW_LNS_advance_pc:
20339 {
20340 CORE_ADDR adjust
20341 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20342 line_ptr += bytes_read;
20343
20344 state_machine.handle_advance_pc (adjust);
20345 }
20346 break;
20347 case DW_LNS_advance_line:
20348 {
20349 int line_delta
20350 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20351 line_ptr += bytes_read;
20352
20353 state_machine.handle_advance_line (line_delta);
20354 }
20355 break;
20356 case DW_LNS_set_file:
20357 {
20358 file_name_index file
20359 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20360 &bytes_read);
20361 line_ptr += bytes_read;
20362
20363 state_machine.handle_set_file (file);
20364 }
20365 break;
20366 case DW_LNS_set_column:
20367 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20368 line_ptr += bytes_read;
20369 break;
20370 case DW_LNS_negate_stmt:
20371 state_machine.handle_negate_stmt ();
20372 break;
20373 case DW_LNS_set_basic_block:
20374 break;
20375 /* Add to the address register of the state machine the
20376 address increment value corresponding to special opcode
20377 255. I.e., this value is scaled by the minimum
20378 instruction length since special opcode 255 would have
20379 scaled the increment. */
20380 case DW_LNS_const_add_pc:
20381 state_machine.handle_const_add_pc ();
20382 break;
20383 case DW_LNS_fixed_advance_pc:
20384 {
20385 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20386 line_ptr += 2;
20387
20388 state_machine.handle_fixed_advance_pc (addr_adj);
20389 }
20390 break;
20391 default:
20392 {
20393 /* Unknown standard opcode, ignore it. */
20394 int i;
20395
20396 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20397 {
20398 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20399 line_ptr += bytes_read;
20400 }
20401 }
20402 }
20403 }
20404
20405 if (!end_sequence)
20406 dwarf2_debug_line_missing_end_sequence_complaint ();
20407
20408 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20409 in which case we still finish recording the last line). */
20410 state_machine.record_line (true);
20411 }
20412 }
20413
20414 /* Decode the Line Number Program (LNP) for the given line_header
20415 structure and CU. The actual information extracted and the type
20416 of structures created from the LNP depends on the value of PST.
20417
20418 1. If PST is NULL, then this procedure uses the data from the program
20419 to create all necessary symbol tables, and their linetables.
20420
20421 2. If PST is not NULL, this procedure reads the program to determine
20422 the list of files included by the unit represented by PST, and
20423 builds all the associated partial symbol tables.
20424
20425 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20426 It is used for relative paths in the line table.
20427 NOTE: When processing partial symtabs (pst != NULL),
20428 comp_dir == pst->dirname.
20429
20430 NOTE: It is important that psymtabs have the same file name (via strcmp)
20431 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20432 symtab we don't use it in the name of the psymtabs we create.
20433 E.g. expand_line_sal requires this when finding psymtabs to expand.
20434 A good testcase for this is mb-inline.exp.
20435
20436 LOWPC is the lowest address in CU (or 0 if not known).
20437
20438 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20439 for its PC<->lines mapping information. Otherwise only the filename
20440 table is read in. */
20441
20442 static void
20443 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20444 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20445 CORE_ADDR lowpc, int decode_mapping)
20446 {
20447 struct objfile *objfile = cu->per_objfile->objfile;
20448 const int decode_for_pst_p = (pst != NULL);
20449
20450 if (decode_mapping)
20451 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20452
20453 if (decode_for_pst_p)
20454 {
20455 /* Now that we're done scanning the Line Header Program, we can
20456 create the psymtab of each included file. */
20457 for (auto &file_entry : lh->file_names ())
20458 if (file_entry.included_p == 1)
20459 {
20460 gdb::unique_xmalloc_ptr<char> name_holder;
20461 const char *include_name =
20462 psymtab_include_file_name (lh, file_entry, pst,
20463 comp_dir, &name_holder);
20464 if (include_name != NULL)
20465 dwarf2_create_include_psymtab (include_name, pst, objfile);
20466 }
20467 }
20468 else
20469 {
20470 /* Make sure a symtab is created for every file, even files
20471 which contain only variables (i.e. no code with associated
20472 line numbers). */
20473 buildsym_compunit *builder = cu->get_builder ();
20474 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20475
20476 for (auto &fe : lh->file_names ())
20477 {
20478 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20479 if (builder->get_current_subfile ()->symtab == NULL)
20480 {
20481 builder->get_current_subfile ()->symtab
20482 = allocate_symtab (cust,
20483 builder->get_current_subfile ()->name);
20484 }
20485 fe.symtab = builder->get_current_subfile ()->symtab;
20486 }
20487 }
20488 }
20489
20490 /* Start a subfile for DWARF. FILENAME is the name of the file and
20491 DIRNAME the name of the source directory which contains FILENAME
20492 or NULL if not known.
20493 This routine tries to keep line numbers from identical absolute and
20494 relative file names in a common subfile.
20495
20496 Using the `list' example from the GDB testsuite, which resides in
20497 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20498 of /srcdir/list0.c yields the following debugging information for list0.c:
20499
20500 DW_AT_name: /srcdir/list0.c
20501 DW_AT_comp_dir: /compdir
20502 files.files[0].name: list0.h
20503 files.files[0].dir: /srcdir
20504 files.files[1].name: list0.c
20505 files.files[1].dir: /srcdir
20506
20507 The line number information for list0.c has to end up in a single
20508 subfile, so that `break /srcdir/list0.c:1' works as expected.
20509 start_subfile will ensure that this happens provided that we pass the
20510 concatenation of files.files[1].dir and files.files[1].name as the
20511 subfile's name. */
20512
20513 static void
20514 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20515 const char *dirname)
20516 {
20517 gdb::unique_xmalloc_ptr<char> copy;
20518
20519 /* In order not to lose the line information directory,
20520 we concatenate it to the filename when it makes sense.
20521 Note that the Dwarf3 standard says (speaking of filenames in line
20522 information): ``The directory index is ignored for file names
20523 that represent full path names''. Thus ignoring dirname in the
20524 `else' branch below isn't an issue. */
20525
20526 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20527 {
20528 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20529 filename = copy.get ();
20530 }
20531
20532 cu->get_builder ()->start_subfile (filename);
20533 }
20534
20535 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20536 buildsym_compunit constructor. */
20537
20538 struct compunit_symtab *
20539 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20540 CORE_ADDR low_pc)
20541 {
20542 gdb_assert (m_builder == nullptr);
20543
20544 m_builder.reset (new struct buildsym_compunit
20545 (per_cu->dwarf2_per_objfile->objfile,
20546 name, comp_dir, language, low_pc));
20547
20548 list_in_scope = get_builder ()->get_file_symbols ();
20549
20550 get_builder ()->record_debugformat ("DWARF 2");
20551 get_builder ()->record_producer (producer);
20552
20553 processing_has_namespace_info = false;
20554
20555 return get_builder ()->get_compunit_symtab ();
20556 }
20557
20558 static void
20559 var_decode_location (struct attribute *attr, struct symbol *sym,
20560 struct dwarf2_cu *cu)
20561 {
20562 struct objfile *objfile = cu->per_objfile->objfile;
20563 struct comp_unit_head *cu_header = &cu->header;
20564
20565 /* NOTE drow/2003-01-30: There used to be a comment and some special
20566 code here to turn a symbol with DW_AT_external and a
20567 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20568 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20569 with some versions of binutils) where shared libraries could have
20570 relocations against symbols in their debug information - the
20571 minimal symbol would have the right address, but the debug info
20572 would not. It's no longer necessary, because we will explicitly
20573 apply relocations when we read in the debug information now. */
20574
20575 /* A DW_AT_location attribute with no contents indicates that a
20576 variable has been optimized away. */
20577 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20578 {
20579 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20580 return;
20581 }
20582
20583 /* Handle one degenerate form of location expression specially, to
20584 preserve GDB's previous behavior when section offsets are
20585 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20586 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20587
20588 if (attr->form_is_block ()
20589 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20590 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20591 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20592 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20593 && (DW_BLOCK (attr)->size
20594 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20595 {
20596 unsigned int dummy;
20597
20598 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20599 SET_SYMBOL_VALUE_ADDRESS
20600 (sym, cu->header.read_address (objfile->obfd,
20601 DW_BLOCK (attr)->data + 1,
20602 &dummy));
20603 else
20604 SET_SYMBOL_VALUE_ADDRESS
20605 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20606 &dummy));
20607 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20608 fixup_symbol_section (sym, objfile);
20609 SET_SYMBOL_VALUE_ADDRESS
20610 (sym,
20611 SYMBOL_VALUE_ADDRESS (sym)
20612 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20613 return;
20614 }
20615
20616 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20617 expression evaluator, and use LOC_COMPUTED only when necessary
20618 (i.e. when the value of a register or memory location is
20619 referenced, or a thread-local block, etc.). Then again, it might
20620 not be worthwhile. I'm assuming that it isn't unless performance
20621 or memory numbers show me otherwise. */
20622
20623 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20624
20625 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20626 cu->has_loclist = true;
20627 }
20628
20629 /* Given a pointer to a DWARF information entry, figure out if we need
20630 to make a symbol table entry for it, and if so, create a new entry
20631 and return a pointer to it.
20632 If TYPE is NULL, determine symbol type from the die, otherwise
20633 used the passed type.
20634 If SPACE is not NULL, use it to hold the new symbol. If it is
20635 NULL, allocate a new symbol on the objfile's obstack. */
20636
20637 static struct symbol *
20638 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20639 struct symbol *space)
20640 {
20641 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20642 struct objfile *objfile = dwarf2_per_objfile->objfile;
20643 struct gdbarch *gdbarch = objfile->arch ();
20644 struct symbol *sym = NULL;
20645 const char *name;
20646 struct attribute *attr = NULL;
20647 struct attribute *attr2 = NULL;
20648 CORE_ADDR baseaddr;
20649 struct pending **list_to_add = NULL;
20650
20651 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20652
20653 baseaddr = objfile->text_section_offset ();
20654
20655 name = dwarf2_name (die, cu);
20656 if (name)
20657 {
20658 int suppress_add = 0;
20659
20660 if (space)
20661 sym = space;
20662 else
20663 sym = new (&objfile->objfile_obstack) symbol;
20664 OBJSTAT (objfile, n_syms++);
20665
20666 /* Cache this symbol's name and the name's demangled form (if any). */
20667 sym->set_language (cu->language, &objfile->objfile_obstack);
20668 /* Fortran does not have mangling standard and the mangling does differ
20669 between gfortran, iFort etc. */
20670 const char *physname
20671 = (cu->language == language_fortran
20672 ? dwarf2_full_name (name, die, cu)
20673 : dwarf2_physname (name, die, cu));
20674 const char *linkagename = dw2_linkage_name (die, cu);
20675
20676 if (linkagename == nullptr || cu->language == language_ada)
20677 sym->set_linkage_name (physname);
20678 else
20679 {
20680 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20681 sym->set_linkage_name (linkagename);
20682 }
20683
20684 /* Default assumptions.
20685 Use the passed type or decode it from the die. */
20686 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20687 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20688 if (type != NULL)
20689 SYMBOL_TYPE (sym) = type;
20690 else
20691 SYMBOL_TYPE (sym) = die_type (die, cu);
20692 attr = dwarf2_attr (die,
20693 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20694 cu);
20695 if (attr != nullptr)
20696 {
20697 SYMBOL_LINE (sym) = DW_UNSND (attr);
20698 }
20699
20700 attr = dwarf2_attr (die,
20701 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20702 cu);
20703 if (attr != nullptr)
20704 {
20705 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20706 struct file_entry *fe;
20707
20708 if (cu->line_header != NULL)
20709 fe = cu->line_header->file_name_at (file_index);
20710 else
20711 fe = NULL;
20712
20713 if (fe == NULL)
20714 complaint (_("file index out of range"));
20715 else
20716 symbol_set_symtab (sym, fe->symtab);
20717 }
20718
20719 switch (die->tag)
20720 {
20721 case DW_TAG_label:
20722 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20723 if (attr != nullptr)
20724 {
20725 CORE_ADDR addr;
20726
20727 addr = attr->value_as_address ();
20728 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20729 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20730 }
20731 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20732 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20733 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20734 add_symbol_to_list (sym, cu->list_in_scope);
20735 break;
20736 case DW_TAG_subprogram:
20737 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20738 finish_block. */
20739 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20740 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20741 if ((attr2 && (DW_UNSND (attr2) != 0))
20742 || cu->language == language_ada
20743 || cu->language == language_fortran)
20744 {
20745 /* Subprograms marked external are stored as a global symbol.
20746 Ada and Fortran subprograms, whether marked external or
20747 not, are always stored as a global symbol, because we want
20748 to be able to access them globally. For instance, we want
20749 to be able to break on a nested subprogram without having
20750 to specify the context. */
20751 list_to_add = cu->get_builder ()->get_global_symbols ();
20752 }
20753 else
20754 {
20755 list_to_add = cu->list_in_scope;
20756 }
20757 break;
20758 case DW_TAG_inlined_subroutine:
20759 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20760 finish_block. */
20761 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20762 SYMBOL_INLINED (sym) = 1;
20763 list_to_add = cu->list_in_scope;
20764 break;
20765 case DW_TAG_template_value_param:
20766 suppress_add = 1;
20767 /* Fall through. */
20768 case DW_TAG_constant:
20769 case DW_TAG_variable:
20770 case DW_TAG_member:
20771 /* Compilation with minimal debug info may result in
20772 variables with missing type entries. Change the
20773 misleading `void' type to something sensible. */
20774 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20775 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20776
20777 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20778 /* In the case of DW_TAG_member, we should only be called for
20779 static const members. */
20780 if (die->tag == DW_TAG_member)
20781 {
20782 /* dwarf2_add_field uses die_is_declaration,
20783 so we do the same. */
20784 gdb_assert (die_is_declaration (die, cu));
20785 gdb_assert (attr);
20786 }
20787 if (attr != nullptr)
20788 {
20789 dwarf2_const_value (attr, sym, cu);
20790 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20791 if (!suppress_add)
20792 {
20793 if (attr2 && (DW_UNSND (attr2) != 0))
20794 list_to_add = cu->get_builder ()->get_global_symbols ();
20795 else
20796 list_to_add = cu->list_in_scope;
20797 }
20798 break;
20799 }
20800 attr = dwarf2_attr (die, DW_AT_location, cu);
20801 if (attr != nullptr)
20802 {
20803 var_decode_location (attr, sym, cu);
20804 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20805
20806 /* Fortran explicitly imports any global symbols to the local
20807 scope by DW_TAG_common_block. */
20808 if (cu->language == language_fortran && die->parent
20809 && die->parent->tag == DW_TAG_common_block)
20810 attr2 = NULL;
20811
20812 if (SYMBOL_CLASS (sym) == LOC_STATIC
20813 && SYMBOL_VALUE_ADDRESS (sym) == 0
20814 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20815 {
20816 /* When a static variable is eliminated by the linker,
20817 the corresponding debug information is not stripped
20818 out, but the variable address is set to null;
20819 do not add such variables into symbol table. */
20820 }
20821 else if (attr2 && (DW_UNSND (attr2) != 0))
20822 {
20823 if (SYMBOL_CLASS (sym) == LOC_STATIC
20824 && (objfile->flags & OBJF_MAINLINE) == 0
20825 && dwarf2_per_objfile->per_bfd->can_copy)
20826 {
20827 /* A global static variable might be subject to
20828 copy relocation. We first check for a local
20829 minsym, though, because maybe the symbol was
20830 marked hidden, in which case this would not
20831 apply. */
20832 bound_minimal_symbol found
20833 = (lookup_minimal_symbol_linkage
20834 (sym->linkage_name (), objfile));
20835 if (found.minsym != nullptr)
20836 sym->maybe_copied = 1;
20837 }
20838
20839 /* A variable with DW_AT_external is never static,
20840 but it may be block-scoped. */
20841 list_to_add
20842 = ((cu->list_in_scope
20843 == cu->get_builder ()->get_file_symbols ())
20844 ? cu->get_builder ()->get_global_symbols ()
20845 : cu->list_in_scope);
20846 }
20847 else
20848 list_to_add = cu->list_in_scope;
20849 }
20850 else
20851 {
20852 /* We do not know the address of this symbol.
20853 If it is an external symbol and we have type information
20854 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20855 The address of the variable will then be determined from
20856 the minimal symbol table whenever the variable is
20857 referenced. */
20858 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20859
20860 /* Fortran explicitly imports any global symbols to the local
20861 scope by DW_TAG_common_block. */
20862 if (cu->language == language_fortran && die->parent
20863 && die->parent->tag == DW_TAG_common_block)
20864 {
20865 /* SYMBOL_CLASS doesn't matter here because
20866 read_common_block is going to reset it. */
20867 if (!suppress_add)
20868 list_to_add = cu->list_in_scope;
20869 }
20870 else if (attr2 && (DW_UNSND (attr2) != 0)
20871 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20872 {
20873 /* A variable with DW_AT_external is never static, but it
20874 may be block-scoped. */
20875 list_to_add
20876 = ((cu->list_in_scope
20877 == cu->get_builder ()->get_file_symbols ())
20878 ? cu->get_builder ()->get_global_symbols ()
20879 : cu->list_in_scope);
20880
20881 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20882 }
20883 else if (!die_is_declaration (die, cu))
20884 {
20885 /* Use the default LOC_OPTIMIZED_OUT class. */
20886 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20887 if (!suppress_add)
20888 list_to_add = cu->list_in_scope;
20889 }
20890 }
20891 break;
20892 case DW_TAG_formal_parameter:
20893 {
20894 /* If we are inside a function, mark this as an argument. If
20895 not, we might be looking at an argument to an inlined function
20896 when we do not have enough information to show inlined frames;
20897 pretend it's a local variable in that case so that the user can
20898 still see it. */
20899 struct context_stack *curr
20900 = cu->get_builder ()->get_current_context_stack ();
20901 if (curr != nullptr && curr->name != nullptr)
20902 SYMBOL_IS_ARGUMENT (sym) = 1;
20903 attr = dwarf2_attr (die, DW_AT_location, cu);
20904 if (attr != nullptr)
20905 {
20906 var_decode_location (attr, sym, cu);
20907 }
20908 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20909 if (attr != nullptr)
20910 {
20911 dwarf2_const_value (attr, sym, cu);
20912 }
20913
20914 list_to_add = cu->list_in_scope;
20915 }
20916 break;
20917 case DW_TAG_unspecified_parameters:
20918 /* From varargs functions; gdb doesn't seem to have any
20919 interest in this information, so just ignore it for now.
20920 (FIXME?) */
20921 break;
20922 case DW_TAG_template_type_param:
20923 suppress_add = 1;
20924 /* Fall through. */
20925 case DW_TAG_class_type:
20926 case DW_TAG_interface_type:
20927 case DW_TAG_structure_type:
20928 case DW_TAG_union_type:
20929 case DW_TAG_set_type:
20930 case DW_TAG_enumeration_type:
20931 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20932 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20933
20934 {
20935 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20936 really ever be static objects: otherwise, if you try
20937 to, say, break of a class's method and you're in a file
20938 which doesn't mention that class, it won't work unless
20939 the check for all static symbols in lookup_symbol_aux
20940 saves you. See the OtherFileClass tests in
20941 gdb.c++/namespace.exp. */
20942
20943 if (!suppress_add)
20944 {
20945 buildsym_compunit *builder = cu->get_builder ();
20946 list_to_add
20947 = (cu->list_in_scope == builder->get_file_symbols ()
20948 && cu->language == language_cplus
20949 ? builder->get_global_symbols ()
20950 : cu->list_in_scope);
20951
20952 /* The semantics of C++ state that "struct foo {
20953 ... }" also defines a typedef for "foo". */
20954 if (cu->language == language_cplus
20955 || cu->language == language_ada
20956 || cu->language == language_d
20957 || cu->language == language_rust)
20958 {
20959 /* The symbol's name is already allocated along
20960 with this objfile, so we don't need to
20961 duplicate it for the type. */
20962 if (SYMBOL_TYPE (sym)->name () == 0)
20963 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
20964 }
20965 }
20966 }
20967 break;
20968 case DW_TAG_typedef:
20969 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20970 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20971 list_to_add = cu->list_in_scope;
20972 break;
20973 case DW_TAG_base_type:
20974 case DW_TAG_subrange_type:
20975 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20976 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20977 list_to_add = cu->list_in_scope;
20978 break;
20979 case DW_TAG_enumerator:
20980 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20981 if (attr != nullptr)
20982 {
20983 dwarf2_const_value (attr, sym, cu);
20984 }
20985 {
20986 /* NOTE: carlton/2003-11-10: See comment above in the
20987 DW_TAG_class_type, etc. block. */
20988
20989 list_to_add
20990 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20991 && cu->language == language_cplus
20992 ? cu->get_builder ()->get_global_symbols ()
20993 : cu->list_in_scope);
20994 }
20995 break;
20996 case DW_TAG_imported_declaration:
20997 case DW_TAG_namespace:
20998 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20999 list_to_add = cu->get_builder ()->get_global_symbols ();
21000 break;
21001 case DW_TAG_module:
21002 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21003 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21004 list_to_add = cu->get_builder ()->get_global_symbols ();
21005 break;
21006 case DW_TAG_common_block:
21007 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21008 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21009 add_symbol_to_list (sym, cu->list_in_scope);
21010 break;
21011 default:
21012 /* Not a tag we recognize. Hopefully we aren't processing
21013 trash data, but since we must specifically ignore things
21014 we don't recognize, there is nothing else we should do at
21015 this point. */
21016 complaint (_("unsupported tag: '%s'"),
21017 dwarf_tag_name (die->tag));
21018 break;
21019 }
21020
21021 if (suppress_add)
21022 {
21023 sym->hash_next = objfile->template_symbols;
21024 objfile->template_symbols = sym;
21025 list_to_add = NULL;
21026 }
21027
21028 if (list_to_add != NULL)
21029 add_symbol_to_list (sym, list_to_add);
21030
21031 /* For the benefit of old versions of GCC, check for anonymous
21032 namespaces based on the demangled name. */
21033 if (!cu->processing_has_namespace_info
21034 && cu->language == language_cplus)
21035 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21036 }
21037 return (sym);
21038 }
21039
21040 /* Given an attr with a DW_FORM_dataN value in host byte order,
21041 zero-extend it as appropriate for the symbol's type. The DWARF
21042 standard (v4) is not entirely clear about the meaning of using
21043 DW_FORM_dataN for a constant with a signed type, where the type is
21044 wider than the data. The conclusion of a discussion on the DWARF
21045 list was that this is unspecified. We choose to always zero-extend
21046 because that is the interpretation long in use by GCC. */
21047
21048 static gdb_byte *
21049 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21050 struct dwarf2_cu *cu, LONGEST *value, int bits)
21051 {
21052 struct objfile *objfile = cu->per_objfile->objfile;
21053 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21054 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21055 LONGEST l = DW_UNSND (attr);
21056
21057 if (bits < sizeof (*value) * 8)
21058 {
21059 l &= ((LONGEST) 1 << bits) - 1;
21060 *value = l;
21061 }
21062 else if (bits == sizeof (*value) * 8)
21063 *value = l;
21064 else
21065 {
21066 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21067 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21068 return bytes;
21069 }
21070
21071 return NULL;
21072 }
21073
21074 /* Read a constant value from an attribute. Either set *VALUE, or if
21075 the value does not fit in *VALUE, set *BYTES - either already
21076 allocated on the objfile obstack, or newly allocated on OBSTACK,
21077 or, set *BATON, if we translated the constant to a location
21078 expression. */
21079
21080 static void
21081 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21082 const char *name, struct obstack *obstack,
21083 struct dwarf2_cu *cu,
21084 LONGEST *value, const gdb_byte **bytes,
21085 struct dwarf2_locexpr_baton **baton)
21086 {
21087 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21088 struct objfile *objfile = per_objfile->objfile;
21089 struct comp_unit_head *cu_header = &cu->header;
21090 struct dwarf_block *blk;
21091 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21092 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21093
21094 *value = 0;
21095 *bytes = NULL;
21096 *baton = NULL;
21097
21098 switch (attr->form)
21099 {
21100 case DW_FORM_addr:
21101 case DW_FORM_addrx:
21102 case DW_FORM_GNU_addr_index:
21103 {
21104 gdb_byte *data;
21105
21106 if (TYPE_LENGTH (type) != cu_header->addr_size)
21107 dwarf2_const_value_length_mismatch_complaint (name,
21108 cu_header->addr_size,
21109 TYPE_LENGTH (type));
21110 /* Symbols of this form are reasonably rare, so we just
21111 piggyback on the existing location code rather than writing
21112 a new implementation of symbol_computed_ops. */
21113 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21114 (*baton)->per_objfile = per_objfile;
21115 (*baton)->per_cu = cu->per_cu;
21116 gdb_assert ((*baton)->per_cu);
21117
21118 (*baton)->size = 2 + cu_header->addr_size;
21119 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21120 (*baton)->data = data;
21121
21122 data[0] = DW_OP_addr;
21123 store_unsigned_integer (&data[1], cu_header->addr_size,
21124 byte_order, DW_ADDR (attr));
21125 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21126 }
21127 break;
21128 case DW_FORM_string:
21129 case DW_FORM_strp:
21130 case DW_FORM_strx:
21131 case DW_FORM_GNU_str_index:
21132 case DW_FORM_GNU_strp_alt:
21133 /* DW_STRING is already allocated on the objfile obstack, point
21134 directly to it. */
21135 *bytes = (const gdb_byte *) DW_STRING (attr);
21136 break;
21137 case DW_FORM_block1:
21138 case DW_FORM_block2:
21139 case DW_FORM_block4:
21140 case DW_FORM_block:
21141 case DW_FORM_exprloc:
21142 case DW_FORM_data16:
21143 blk = DW_BLOCK (attr);
21144 if (TYPE_LENGTH (type) != blk->size)
21145 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21146 TYPE_LENGTH (type));
21147 *bytes = blk->data;
21148 break;
21149
21150 /* The DW_AT_const_value attributes are supposed to carry the
21151 symbol's value "represented as it would be on the target
21152 architecture." By the time we get here, it's already been
21153 converted to host endianness, so we just need to sign- or
21154 zero-extend it as appropriate. */
21155 case DW_FORM_data1:
21156 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21157 break;
21158 case DW_FORM_data2:
21159 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21160 break;
21161 case DW_FORM_data4:
21162 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21163 break;
21164 case DW_FORM_data8:
21165 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21166 break;
21167
21168 case DW_FORM_sdata:
21169 case DW_FORM_implicit_const:
21170 *value = DW_SND (attr);
21171 break;
21172
21173 case DW_FORM_udata:
21174 *value = DW_UNSND (attr);
21175 break;
21176
21177 default:
21178 complaint (_("unsupported const value attribute form: '%s'"),
21179 dwarf_form_name (attr->form));
21180 *value = 0;
21181 break;
21182 }
21183 }
21184
21185
21186 /* Copy constant value from an attribute to a symbol. */
21187
21188 static void
21189 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21190 struct dwarf2_cu *cu)
21191 {
21192 struct objfile *objfile = cu->per_objfile->objfile;
21193 LONGEST value;
21194 const gdb_byte *bytes;
21195 struct dwarf2_locexpr_baton *baton;
21196
21197 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21198 sym->print_name (),
21199 &objfile->objfile_obstack, cu,
21200 &value, &bytes, &baton);
21201
21202 if (baton != NULL)
21203 {
21204 SYMBOL_LOCATION_BATON (sym) = baton;
21205 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21206 }
21207 else if (bytes != NULL)
21208 {
21209 SYMBOL_VALUE_BYTES (sym) = bytes;
21210 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21211 }
21212 else
21213 {
21214 SYMBOL_VALUE (sym) = value;
21215 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21216 }
21217 }
21218
21219 /* Return the type of the die in question using its DW_AT_type attribute. */
21220
21221 static struct type *
21222 die_type (struct die_info *die, struct dwarf2_cu *cu)
21223 {
21224 struct attribute *type_attr;
21225
21226 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21227 if (!type_attr)
21228 {
21229 struct objfile *objfile = cu->per_objfile->objfile;
21230 /* A missing DW_AT_type represents a void type. */
21231 return objfile_type (objfile)->builtin_void;
21232 }
21233
21234 return lookup_die_type (die, type_attr, cu);
21235 }
21236
21237 /* True iff CU's producer generates GNAT Ada auxiliary information
21238 that allows to find parallel types through that information instead
21239 of having to do expensive parallel lookups by type name. */
21240
21241 static int
21242 need_gnat_info (struct dwarf2_cu *cu)
21243 {
21244 /* Assume that the Ada compiler was GNAT, which always produces
21245 the auxiliary information. */
21246 return (cu->language == language_ada);
21247 }
21248
21249 /* Return the auxiliary type of the die in question using its
21250 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21251 attribute is not present. */
21252
21253 static struct type *
21254 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21255 {
21256 struct attribute *type_attr;
21257
21258 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21259 if (!type_attr)
21260 return NULL;
21261
21262 return lookup_die_type (die, type_attr, cu);
21263 }
21264
21265 /* If DIE has a descriptive_type attribute, then set the TYPE's
21266 descriptive type accordingly. */
21267
21268 static void
21269 set_descriptive_type (struct type *type, struct die_info *die,
21270 struct dwarf2_cu *cu)
21271 {
21272 struct type *descriptive_type = die_descriptive_type (die, cu);
21273
21274 if (descriptive_type)
21275 {
21276 ALLOCATE_GNAT_AUX_TYPE (type);
21277 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21278 }
21279 }
21280
21281 /* Return the containing type of the die in question using its
21282 DW_AT_containing_type attribute. */
21283
21284 static struct type *
21285 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21286 {
21287 struct attribute *type_attr;
21288 struct objfile *objfile = cu->per_objfile->objfile;
21289
21290 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21291 if (!type_attr)
21292 error (_("Dwarf Error: Problem turning containing type into gdb type "
21293 "[in module %s]"), objfile_name (objfile));
21294
21295 return lookup_die_type (die, type_attr, cu);
21296 }
21297
21298 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21299
21300 static struct type *
21301 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21302 {
21303 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21304 struct objfile *objfile = dwarf2_per_objfile->objfile;
21305 char *saved;
21306
21307 std::string message
21308 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21309 objfile_name (objfile),
21310 sect_offset_str (cu->header.sect_off),
21311 sect_offset_str (die->sect_off));
21312 saved = obstack_strdup (&objfile->objfile_obstack, message);
21313
21314 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21315 }
21316
21317 /* Look up the type of DIE in CU using its type attribute ATTR.
21318 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21319 DW_AT_containing_type.
21320 If there is no type substitute an error marker. */
21321
21322 static struct type *
21323 lookup_die_type (struct die_info *die, const struct attribute *attr,
21324 struct dwarf2_cu *cu)
21325 {
21326 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21327 struct objfile *objfile = dwarf2_per_objfile->objfile;
21328 struct type *this_type;
21329
21330 gdb_assert (attr->name == DW_AT_type
21331 || attr->name == DW_AT_GNAT_descriptive_type
21332 || attr->name == DW_AT_containing_type);
21333
21334 /* First see if we have it cached. */
21335
21336 if (attr->form == DW_FORM_GNU_ref_alt)
21337 {
21338 struct dwarf2_per_cu_data *per_cu;
21339 sect_offset sect_off = attr->get_ref_die_offset ();
21340
21341 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21342 dwarf2_per_objfile);
21343 this_type = get_die_type_at_offset (sect_off, per_cu);
21344 }
21345 else if (attr->form_is_ref ())
21346 {
21347 sect_offset sect_off = attr->get_ref_die_offset ();
21348
21349 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21350 }
21351 else if (attr->form == DW_FORM_ref_sig8)
21352 {
21353 ULONGEST signature = DW_SIGNATURE (attr);
21354
21355 return get_signatured_type (die, signature, cu);
21356 }
21357 else
21358 {
21359 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21360 " at %s [in module %s]"),
21361 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21362 objfile_name (objfile));
21363 return build_error_marker_type (cu, die);
21364 }
21365
21366 /* If not cached we need to read it in. */
21367
21368 if (this_type == NULL)
21369 {
21370 struct die_info *type_die = NULL;
21371 struct dwarf2_cu *type_cu = cu;
21372
21373 if (attr->form_is_ref ())
21374 type_die = follow_die_ref (die, attr, &type_cu);
21375 if (type_die == NULL)
21376 return build_error_marker_type (cu, die);
21377 /* If we find the type now, it's probably because the type came
21378 from an inter-CU reference and the type's CU got expanded before
21379 ours. */
21380 this_type = read_type_die (type_die, type_cu);
21381 }
21382
21383 /* If we still don't have a type use an error marker. */
21384
21385 if (this_type == NULL)
21386 return build_error_marker_type (cu, die);
21387
21388 return this_type;
21389 }
21390
21391 /* Return the type in DIE, CU.
21392 Returns NULL for invalid types.
21393
21394 This first does a lookup in die_type_hash,
21395 and only reads the die in if necessary.
21396
21397 NOTE: This can be called when reading in partial or full symbols. */
21398
21399 static struct type *
21400 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21401 {
21402 struct type *this_type;
21403
21404 this_type = get_die_type (die, cu);
21405 if (this_type)
21406 return this_type;
21407
21408 return read_type_die_1 (die, cu);
21409 }
21410
21411 /* Read the type in DIE, CU.
21412 Returns NULL for invalid types. */
21413
21414 static struct type *
21415 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21416 {
21417 struct type *this_type = NULL;
21418
21419 switch (die->tag)
21420 {
21421 case DW_TAG_class_type:
21422 case DW_TAG_interface_type:
21423 case DW_TAG_structure_type:
21424 case DW_TAG_union_type:
21425 this_type = read_structure_type (die, cu);
21426 break;
21427 case DW_TAG_enumeration_type:
21428 this_type = read_enumeration_type (die, cu);
21429 break;
21430 case DW_TAG_subprogram:
21431 case DW_TAG_subroutine_type:
21432 case DW_TAG_inlined_subroutine:
21433 this_type = read_subroutine_type (die, cu);
21434 break;
21435 case DW_TAG_array_type:
21436 this_type = read_array_type (die, cu);
21437 break;
21438 case DW_TAG_set_type:
21439 this_type = read_set_type (die, cu);
21440 break;
21441 case DW_TAG_pointer_type:
21442 this_type = read_tag_pointer_type (die, cu);
21443 break;
21444 case DW_TAG_ptr_to_member_type:
21445 this_type = read_tag_ptr_to_member_type (die, cu);
21446 break;
21447 case DW_TAG_reference_type:
21448 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21449 break;
21450 case DW_TAG_rvalue_reference_type:
21451 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21452 break;
21453 case DW_TAG_const_type:
21454 this_type = read_tag_const_type (die, cu);
21455 break;
21456 case DW_TAG_volatile_type:
21457 this_type = read_tag_volatile_type (die, cu);
21458 break;
21459 case DW_TAG_restrict_type:
21460 this_type = read_tag_restrict_type (die, cu);
21461 break;
21462 case DW_TAG_string_type:
21463 this_type = read_tag_string_type (die, cu);
21464 break;
21465 case DW_TAG_typedef:
21466 this_type = read_typedef (die, cu);
21467 break;
21468 case DW_TAG_subrange_type:
21469 this_type = read_subrange_type (die, cu);
21470 break;
21471 case DW_TAG_base_type:
21472 this_type = read_base_type (die, cu);
21473 break;
21474 case DW_TAG_unspecified_type:
21475 this_type = read_unspecified_type (die, cu);
21476 break;
21477 case DW_TAG_namespace:
21478 this_type = read_namespace_type (die, cu);
21479 break;
21480 case DW_TAG_module:
21481 this_type = read_module_type (die, cu);
21482 break;
21483 case DW_TAG_atomic_type:
21484 this_type = read_tag_atomic_type (die, cu);
21485 break;
21486 default:
21487 complaint (_("unexpected tag in read_type_die: '%s'"),
21488 dwarf_tag_name (die->tag));
21489 break;
21490 }
21491
21492 return this_type;
21493 }
21494
21495 /* See if we can figure out if the class lives in a namespace. We do
21496 this by looking for a member function; its demangled name will
21497 contain namespace info, if there is any.
21498 Return the computed name or NULL.
21499 Space for the result is allocated on the objfile's obstack.
21500 This is the full-die version of guess_partial_die_structure_name.
21501 In this case we know DIE has no useful parent. */
21502
21503 static const char *
21504 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21505 {
21506 struct die_info *spec_die;
21507 struct dwarf2_cu *spec_cu;
21508 struct die_info *child;
21509 struct objfile *objfile = cu->per_objfile->objfile;
21510
21511 spec_cu = cu;
21512 spec_die = die_specification (die, &spec_cu);
21513 if (spec_die != NULL)
21514 {
21515 die = spec_die;
21516 cu = spec_cu;
21517 }
21518
21519 for (child = die->child;
21520 child != NULL;
21521 child = child->sibling)
21522 {
21523 if (child->tag == DW_TAG_subprogram)
21524 {
21525 const char *linkage_name = dw2_linkage_name (child, cu);
21526
21527 if (linkage_name != NULL)
21528 {
21529 gdb::unique_xmalloc_ptr<char> actual_name
21530 (language_class_name_from_physname (cu->language_defn,
21531 linkage_name));
21532 const char *name = NULL;
21533
21534 if (actual_name != NULL)
21535 {
21536 const char *die_name = dwarf2_name (die, cu);
21537
21538 if (die_name != NULL
21539 && strcmp (die_name, actual_name.get ()) != 0)
21540 {
21541 /* Strip off the class name from the full name.
21542 We want the prefix. */
21543 int die_name_len = strlen (die_name);
21544 int actual_name_len = strlen (actual_name.get ());
21545 const char *ptr = actual_name.get ();
21546
21547 /* Test for '::' as a sanity check. */
21548 if (actual_name_len > die_name_len + 2
21549 && ptr[actual_name_len - die_name_len - 1] == ':')
21550 name = obstack_strndup (
21551 &objfile->per_bfd->storage_obstack,
21552 ptr, actual_name_len - die_name_len - 2);
21553 }
21554 }
21555 return name;
21556 }
21557 }
21558 }
21559
21560 return NULL;
21561 }
21562
21563 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21564 prefix part in such case. See
21565 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21566
21567 static const char *
21568 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21569 {
21570 struct attribute *attr;
21571 const char *base;
21572
21573 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21574 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21575 return NULL;
21576
21577 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21578 return NULL;
21579
21580 attr = dw2_linkage_name_attr (die, cu);
21581 if (attr == NULL || DW_STRING (attr) == NULL)
21582 return NULL;
21583
21584 /* dwarf2_name had to be already called. */
21585 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21586
21587 /* Strip the base name, keep any leading namespaces/classes. */
21588 base = strrchr (DW_STRING (attr), ':');
21589 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21590 return "";
21591
21592 struct objfile *objfile = cu->per_objfile->objfile;
21593 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21594 DW_STRING (attr),
21595 &base[-1] - DW_STRING (attr));
21596 }
21597
21598 /* Return the name of the namespace/class that DIE is defined within,
21599 or "" if we can't tell. The caller should not xfree the result.
21600
21601 For example, if we're within the method foo() in the following
21602 code:
21603
21604 namespace N {
21605 class C {
21606 void foo () {
21607 }
21608 };
21609 }
21610
21611 then determine_prefix on foo's die will return "N::C". */
21612
21613 static const char *
21614 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21615 {
21616 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21617 struct die_info *parent, *spec_die;
21618 struct dwarf2_cu *spec_cu;
21619 struct type *parent_type;
21620 const char *retval;
21621
21622 if (cu->language != language_cplus
21623 && cu->language != language_fortran && cu->language != language_d
21624 && cu->language != language_rust)
21625 return "";
21626
21627 retval = anonymous_struct_prefix (die, cu);
21628 if (retval)
21629 return retval;
21630
21631 /* We have to be careful in the presence of DW_AT_specification.
21632 For example, with GCC 3.4, given the code
21633
21634 namespace N {
21635 void foo() {
21636 // Definition of N::foo.
21637 }
21638 }
21639
21640 then we'll have a tree of DIEs like this:
21641
21642 1: DW_TAG_compile_unit
21643 2: DW_TAG_namespace // N
21644 3: DW_TAG_subprogram // declaration of N::foo
21645 4: DW_TAG_subprogram // definition of N::foo
21646 DW_AT_specification // refers to die #3
21647
21648 Thus, when processing die #4, we have to pretend that we're in
21649 the context of its DW_AT_specification, namely the contex of die
21650 #3. */
21651 spec_cu = cu;
21652 spec_die = die_specification (die, &spec_cu);
21653 if (spec_die == NULL)
21654 parent = die->parent;
21655 else
21656 {
21657 parent = spec_die->parent;
21658 cu = spec_cu;
21659 }
21660
21661 if (parent == NULL)
21662 return "";
21663 else if (parent->building_fullname)
21664 {
21665 const char *name;
21666 const char *parent_name;
21667
21668 /* It has been seen on RealView 2.2 built binaries,
21669 DW_TAG_template_type_param types actually _defined_ as
21670 children of the parent class:
21671
21672 enum E {};
21673 template class <class Enum> Class{};
21674 Class<enum E> class_e;
21675
21676 1: DW_TAG_class_type (Class)
21677 2: DW_TAG_enumeration_type (E)
21678 3: DW_TAG_enumerator (enum1:0)
21679 3: DW_TAG_enumerator (enum2:1)
21680 ...
21681 2: DW_TAG_template_type_param
21682 DW_AT_type DW_FORM_ref_udata (E)
21683
21684 Besides being broken debug info, it can put GDB into an
21685 infinite loop. Consider:
21686
21687 When we're building the full name for Class<E>, we'll start
21688 at Class, and go look over its template type parameters,
21689 finding E. We'll then try to build the full name of E, and
21690 reach here. We're now trying to build the full name of E,
21691 and look over the parent DIE for containing scope. In the
21692 broken case, if we followed the parent DIE of E, we'd again
21693 find Class, and once again go look at its template type
21694 arguments, etc., etc. Simply don't consider such parent die
21695 as source-level parent of this die (it can't be, the language
21696 doesn't allow it), and break the loop here. */
21697 name = dwarf2_name (die, cu);
21698 parent_name = dwarf2_name (parent, cu);
21699 complaint (_("template param type '%s' defined within parent '%s'"),
21700 name ? name : "<unknown>",
21701 parent_name ? parent_name : "<unknown>");
21702 return "";
21703 }
21704 else
21705 switch (parent->tag)
21706 {
21707 case DW_TAG_namespace:
21708 parent_type = read_type_die (parent, cu);
21709 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21710 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21711 Work around this problem here. */
21712 if (cu->language == language_cplus
21713 && strcmp (parent_type->name (), "::") == 0)
21714 return "";
21715 /* We give a name to even anonymous namespaces. */
21716 return parent_type->name ();
21717 case DW_TAG_class_type:
21718 case DW_TAG_interface_type:
21719 case DW_TAG_structure_type:
21720 case DW_TAG_union_type:
21721 case DW_TAG_module:
21722 parent_type = read_type_die (parent, cu);
21723 if (parent_type->name () != NULL)
21724 return parent_type->name ();
21725 else
21726 /* An anonymous structure is only allowed non-static data
21727 members; no typedefs, no member functions, et cetera.
21728 So it does not need a prefix. */
21729 return "";
21730 case DW_TAG_compile_unit:
21731 case DW_TAG_partial_unit:
21732 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21733 if (cu->language == language_cplus
21734 && !dwarf2_per_objfile->per_bfd->types.empty ()
21735 && die->child != NULL
21736 && (die->tag == DW_TAG_class_type
21737 || die->tag == DW_TAG_structure_type
21738 || die->tag == DW_TAG_union_type))
21739 {
21740 const char *name = guess_full_die_structure_name (die, cu);
21741 if (name != NULL)
21742 return name;
21743 }
21744 return "";
21745 case DW_TAG_subprogram:
21746 /* Nested subroutines in Fortran get a prefix with the name
21747 of the parent's subroutine. */
21748 if (cu->language == language_fortran)
21749 {
21750 if ((die->tag == DW_TAG_subprogram)
21751 && (dwarf2_name (parent, cu) != NULL))
21752 return dwarf2_name (parent, cu);
21753 }
21754 return determine_prefix (parent, cu);
21755 case DW_TAG_enumeration_type:
21756 parent_type = read_type_die (parent, cu);
21757 if (TYPE_DECLARED_CLASS (parent_type))
21758 {
21759 if (parent_type->name () != NULL)
21760 return parent_type->name ();
21761 return "";
21762 }
21763 /* Fall through. */
21764 default:
21765 return determine_prefix (parent, cu);
21766 }
21767 }
21768
21769 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21770 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21771 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21772 an obconcat, otherwise allocate storage for the result. The CU argument is
21773 used to determine the language and hence, the appropriate separator. */
21774
21775 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21776
21777 static char *
21778 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21779 int physname, struct dwarf2_cu *cu)
21780 {
21781 const char *lead = "";
21782 const char *sep;
21783
21784 if (suffix == NULL || suffix[0] == '\0'
21785 || prefix == NULL || prefix[0] == '\0')
21786 sep = "";
21787 else if (cu->language == language_d)
21788 {
21789 /* For D, the 'main' function could be defined in any module, but it
21790 should never be prefixed. */
21791 if (strcmp (suffix, "D main") == 0)
21792 {
21793 prefix = "";
21794 sep = "";
21795 }
21796 else
21797 sep = ".";
21798 }
21799 else if (cu->language == language_fortran && physname)
21800 {
21801 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21802 DW_AT_MIPS_linkage_name is preferred and used instead. */
21803
21804 lead = "__";
21805 sep = "_MOD_";
21806 }
21807 else
21808 sep = "::";
21809
21810 if (prefix == NULL)
21811 prefix = "";
21812 if (suffix == NULL)
21813 suffix = "";
21814
21815 if (obs == NULL)
21816 {
21817 char *retval
21818 = ((char *)
21819 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21820
21821 strcpy (retval, lead);
21822 strcat (retval, prefix);
21823 strcat (retval, sep);
21824 strcat (retval, suffix);
21825 return retval;
21826 }
21827 else
21828 {
21829 /* We have an obstack. */
21830 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21831 }
21832 }
21833
21834 /* Get name of a die, return NULL if not found. */
21835
21836 static const char *
21837 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21838 struct objfile *objfile)
21839 {
21840 if (name && cu->language == language_cplus)
21841 {
21842 gdb::unique_xmalloc_ptr<char> canon_name
21843 = cp_canonicalize_string (name);
21844
21845 if (canon_name != nullptr)
21846 name = objfile->intern (canon_name.get ());
21847 }
21848
21849 return name;
21850 }
21851
21852 /* Get name of a die, return NULL if not found.
21853 Anonymous namespaces are converted to their magic string. */
21854
21855 static const char *
21856 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21857 {
21858 struct attribute *attr;
21859 struct objfile *objfile = cu->per_objfile->objfile;
21860
21861 attr = dwarf2_attr (die, DW_AT_name, cu);
21862 if ((!attr || !DW_STRING (attr))
21863 && die->tag != DW_TAG_namespace
21864 && die->tag != DW_TAG_class_type
21865 && die->tag != DW_TAG_interface_type
21866 && die->tag != DW_TAG_structure_type
21867 && die->tag != DW_TAG_union_type)
21868 return NULL;
21869
21870 switch (die->tag)
21871 {
21872 case DW_TAG_compile_unit:
21873 case DW_TAG_partial_unit:
21874 /* Compilation units have a DW_AT_name that is a filename, not
21875 a source language identifier. */
21876 case DW_TAG_enumeration_type:
21877 case DW_TAG_enumerator:
21878 /* These tags always have simple identifiers already; no need
21879 to canonicalize them. */
21880 return DW_STRING (attr);
21881
21882 case DW_TAG_namespace:
21883 if (attr != NULL && DW_STRING (attr) != NULL)
21884 return DW_STRING (attr);
21885 return CP_ANONYMOUS_NAMESPACE_STR;
21886
21887 case DW_TAG_class_type:
21888 case DW_TAG_interface_type:
21889 case DW_TAG_structure_type:
21890 case DW_TAG_union_type:
21891 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21892 structures or unions. These were of the form "._%d" in GCC 4.1,
21893 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21894 and GCC 4.4. We work around this problem by ignoring these. */
21895 if (attr && DW_STRING (attr)
21896 && (startswith (DW_STRING (attr), "._")
21897 || startswith (DW_STRING (attr), "<anonymous")))
21898 return NULL;
21899
21900 /* GCC might emit a nameless typedef that has a linkage name. See
21901 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21902 if (!attr || DW_STRING (attr) == NULL)
21903 {
21904 attr = dw2_linkage_name_attr (die, cu);
21905 if (attr == NULL || DW_STRING (attr) == NULL)
21906 return NULL;
21907
21908 /* Avoid demangling DW_STRING (attr) the second time on a second
21909 call for the same DIE. */
21910 if (!DW_STRING_IS_CANONICAL (attr))
21911 {
21912 gdb::unique_xmalloc_ptr<char> demangled
21913 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21914 if (demangled == nullptr)
21915 return nullptr;
21916
21917 DW_STRING (attr) = objfile->intern (demangled.get ());
21918 DW_STRING_IS_CANONICAL (attr) = 1;
21919 }
21920
21921 /* Strip any leading namespaces/classes, keep only the base name.
21922 DW_AT_name for named DIEs does not contain the prefixes. */
21923 const char *base = strrchr (DW_STRING (attr), ':');
21924 if (base && base > DW_STRING (attr) && base[-1] == ':')
21925 return &base[1];
21926 else
21927 return DW_STRING (attr);
21928 }
21929 break;
21930
21931 default:
21932 break;
21933 }
21934
21935 if (!DW_STRING_IS_CANONICAL (attr))
21936 {
21937 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21938 objfile);
21939 DW_STRING_IS_CANONICAL (attr) = 1;
21940 }
21941 return DW_STRING (attr);
21942 }
21943
21944 /* Return the die that this die in an extension of, or NULL if there
21945 is none. *EXT_CU is the CU containing DIE on input, and the CU
21946 containing the return value on output. */
21947
21948 static struct die_info *
21949 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21950 {
21951 struct attribute *attr;
21952
21953 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21954 if (attr == NULL)
21955 return NULL;
21956
21957 return follow_die_ref (die, attr, ext_cu);
21958 }
21959
21960 static void
21961 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21962 {
21963 unsigned int i;
21964
21965 print_spaces (indent, f);
21966 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21967 dwarf_tag_name (die->tag), die->abbrev,
21968 sect_offset_str (die->sect_off));
21969
21970 if (die->parent != NULL)
21971 {
21972 print_spaces (indent, f);
21973 fprintf_unfiltered (f, " parent at offset: %s\n",
21974 sect_offset_str (die->parent->sect_off));
21975 }
21976
21977 print_spaces (indent, f);
21978 fprintf_unfiltered (f, " has children: %s\n",
21979 dwarf_bool_name (die->child != NULL));
21980
21981 print_spaces (indent, f);
21982 fprintf_unfiltered (f, " attributes:\n");
21983
21984 for (i = 0; i < die->num_attrs; ++i)
21985 {
21986 print_spaces (indent, f);
21987 fprintf_unfiltered (f, " %s (%s) ",
21988 dwarf_attr_name (die->attrs[i].name),
21989 dwarf_form_name (die->attrs[i].form));
21990
21991 switch (die->attrs[i].form)
21992 {
21993 case DW_FORM_addr:
21994 case DW_FORM_addrx:
21995 case DW_FORM_GNU_addr_index:
21996 fprintf_unfiltered (f, "address: ");
21997 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21998 break;
21999 case DW_FORM_block2:
22000 case DW_FORM_block4:
22001 case DW_FORM_block:
22002 case DW_FORM_block1:
22003 fprintf_unfiltered (f, "block: size %s",
22004 pulongest (DW_BLOCK (&die->attrs[i])->size));
22005 break;
22006 case DW_FORM_exprloc:
22007 fprintf_unfiltered (f, "expression: size %s",
22008 pulongest (DW_BLOCK (&die->attrs[i])->size));
22009 break;
22010 case DW_FORM_data16:
22011 fprintf_unfiltered (f, "constant of 16 bytes");
22012 break;
22013 case DW_FORM_ref_addr:
22014 fprintf_unfiltered (f, "ref address: ");
22015 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22016 break;
22017 case DW_FORM_GNU_ref_alt:
22018 fprintf_unfiltered (f, "alt ref address: ");
22019 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22020 break;
22021 case DW_FORM_ref1:
22022 case DW_FORM_ref2:
22023 case DW_FORM_ref4:
22024 case DW_FORM_ref8:
22025 case DW_FORM_ref_udata:
22026 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22027 (long) (DW_UNSND (&die->attrs[i])));
22028 break;
22029 case DW_FORM_data1:
22030 case DW_FORM_data2:
22031 case DW_FORM_data4:
22032 case DW_FORM_data8:
22033 case DW_FORM_udata:
22034 case DW_FORM_sdata:
22035 fprintf_unfiltered (f, "constant: %s",
22036 pulongest (DW_UNSND (&die->attrs[i])));
22037 break;
22038 case DW_FORM_sec_offset:
22039 fprintf_unfiltered (f, "section offset: %s",
22040 pulongest (DW_UNSND (&die->attrs[i])));
22041 break;
22042 case DW_FORM_ref_sig8:
22043 fprintf_unfiltered (f, "signature: %s",
22044 hex_string (DW_SIGNATURE (&die->attrs[i])));
22045 break;
22046 case DW_FORM_string:
22047 case DW_FORM_strp:
22048 case DW_FORM_line_strp:
22049 case DW_FORM_strx:
22050 case DW_FORM_GNU_str_index:
22051 case DW_FORM_GNU_strp_alt:
22052 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22053 DW_STRING (&die->attrs[i])
22054 ? DW_STRING (&die->attrs[i]) : "",
22055 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22056 break;
22057 case DW_FORM_flag:
22058 if (DW_UNSND (&die->attrs[i]))
22059 fprintf_unfiltered (f, "flag: TRUE");
22060 else
22061 fprintf_unfiltered (f, "flag: FALSE");
22062 break;
22063 case DW_FORM_flag_present:
22064 fprintf_unfiltered (f, "flag: TRUE");
22065 break;
22066 case DW_FORM_indirect:
22067 /* The reader will have reduced the indirect form to
22068 the "base form" so this form should not occur. */
22069 fprintf_unfiltered (f,
22070 "unexpected attribute form: DW_FORM_indirect");
22071 break;
22072 case DW_FORM_implicit_const:
22073 fprintf_unfiltered (f, "constant: %s",
22074 plongest (DW_SND (&die->attrs[i])));
22075 break;
22076 default:
22077 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22078 die->attrs[i].form);
22079 break;
22080 }
22081 fprintf_unfiltered (f, "\n");
22082 }
22083 }
22084
22085 static void
22086 dump_die_for_error (struct die_info *die)
22087 {
22088 dump_die_shallow (gdb_stderr, 0, die);
22089 }
22090
22091 static void
22092 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22093 {
22094 int indent = level * 4;
22095
22096 gdb_assert (die != NULL);
22097
22098 if (level >= max_level)
22099 return;
22100
22101 dump_die_shallow (f, indent, die);
22102
22103 if (die->child != NULL)
22104 {
22105 print_spaces (indent, f);
22106 fprintf_unfiltered (f, " Children:");
22107 if (level + 1 < max_level)
22108 {
22109 fprintf_unfiltered (f, "\n");
22110 dump_die_1 (f, level + 1, max_level, die->child);
22111 }
22112 else
22113 {
22114 fprintf_unfiltered (f,
22115 " [not printed, max nesting level reached]\n");
22116 }
22117 }
22118
22119 if (die->sibling != NULL && level > 0)
22120 {
22121 dump_die_1 (f, level, max_level, die->sibling);
22122 }
22123 }
22124
22125 /* This is called from the pdie macro in gdbinit.in.
22126 It's not static so gcc will keep a copy callable from gdb. */
22127
22128 void
22129 dump_die (struct die_info *die, int max_level)
22130 {
22131 dump_die_1 (gdb_stdlog, 0, max_level, die);
22132 }
22133
22134 static void
22135 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22136 {
22137 void **slot;
22138
22139 slot = htab_find_slot_with_hash (cu->die_hash, die,
22140 to_underlying (die->sect_off),
22141 INSERT);
22142
22143 *slot = die;
22144 }
22145
22146 /* Follow reference or signature attribute ATTR of SRC_DIE.
22147 On entry *REF_CU is the CU of SRC_DIE.
22148 On exit *REF_CU is the CU of the result. */
22149
22150 static struct die_info *
22151 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22152 struct dwarf2_cu **ref_cu)
22153 {
22154 struct die_info *die;
22155
22156 if (attr->form_is_ref ())
22157 die = follow_die_ref (src_die, attr, ref_cu);
22158 else if (attr->form == DW_FORM_ref_sig8)
22159 die = follow_die_sig (src_die, attr, ref_cu);
22160 else
22161 {
22162 dump_die_for_error (src_die);
22163 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22164 objfile_name ((*ref_cu)->per_objfile->objfile));
22165 }
22166
22167 return die;
22168 }
22169
22170 /* Follow reference OFFSET.
22171 On entry *REF_CU is the CU of the source die referencing OFFSET.
22172 On exit *REF_CU is the CU of the result.
22173 Returns NULL if OFFSET is invalid. */
22174
22175 static struct die_info *
22176 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22177 struct dwarf2_cu **ref_cu)
22178 {
22179 struct die_info temp_die;
22180 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22181 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22182
22183 gdb_assert (cu->per_cu != NULL);
22184
22185 target_cu = cu;
22186
22187 if (cu->per_cu->is_debug_types)
22188 {
22189 /* .debug_types CUs cannot reference anything outside their CU.
22190 If they need to, they have to reference a signatured type via
22191 DW_FORM_ref_sig8. */
22192 if (!cu->header.offset_in_cu_p (sect_off))
22193 return NULL;
22194 }
22195 else if (offset_in_dwz != cu->per_cu->is_dwz
22196 || !cu->header.offset_in_cu_p (sect_off))
22197 {
22198 struct dwarf2_per_cu_data *per_cu;
22199
22200 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22201 dwarf2_per_objfile);
22202
22203 /* If necessary, add it to the queue and load its DIEs. */
22204 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22205 load_full_comp_unit (per_cu, false, cu->language);
22206
22207 target_cu = per_cu->cu;
22208 }
22209 else if (cu->dies == NULL)
22210 {
22211 /* We're loading full DIEs during partial symbol reading. */
22212 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22213 load_full_comp_unit (cu->per_cu, false, language_minimal);
22214 }
22215
22216 *ref_cu = target_cu;
22217 temp_die.sect_off = sect_off;
22218
22219 if (target_cu != cu)
22220 target_cu->ancestor = cu;
22221
22222 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22223 &temp_die,
22224 to_underlying (sect_off));
22225 }
22226
22227 /* Follow reference attribute ATTR of SRC_DIE.
22228 On entry *REF_CU is the CU of SRC_DIE.
22229 On exit *REF_CU is the CU of the result. */
22230
22231 static struct die_info *
22232 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22233 struct dwarf2_cu **ref_cu)
22234 {
22235 sect_offset sect_off = attr->get_ref_die_offset ();
22236 struct dwarf2_cu *cu = *ref_cu;
22237 struct die_info *die;
22238
22239 die = follow_die_offset (sect_off,
22240 (attr->form == DW_FORM_GNU_ref_alt
22241 || cu->per_cu->is_dwz),
22242 ref_cu);
22243 if (!die)
22244 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22245 "at %s [in module %s]"),
22246 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22247 objfile_name (cu->per_objfile->objfile));
22248
22249 return die;
22250 }
22251
22252 /* See read.h. */
22253
22254 struct dwarf2_locexpr_baton
22255 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22256 dwarf2_per_cu_data *per_cu,
22257 CORE_ADDR (*get_frame_pc) (void *baton),
22258 void *baton, bool resolve_abstract_p)
22259 {
22260 struct dwarf2_cu *cu;
22261 struct die_info *die;
22262 struct attribute *attr;
22263 struct dwarf2_locexpr_baton retval;
22264 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22265 struct objfile *objfile = dwarf2_per_objfile->objfile;
22266
22267 if (per_cu->cu == NULL)
22268 load_cu (per_cu, false);
22269 cu = per_cu->cu;
22270 if (cu == NULL)
22271 {
22272 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22273 Instead just throw an error, not much else we can do. */
22274 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22275 sect_offset_str (sect_off), objfile_name (objfile));
22276 }
22277
22278 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22279 if (!die)
22280 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22281 sect_offset_str (sect_off), objfile_name (objfile));
22282
22283 attr = dwarf2_attr (die, DW_AT_location, cu);
22284 if (!attr && resolve_abstract_p
22285 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22286 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22287 {
22288 CORE_ADDR pc = (*get_frame_pc) (baton);
22289 CORE_ADDR baseaddr = objfile->text_section_offset ();
22290 struct gdbarch *gdbarch = objfile->arch ();
22291
22292 for (const auto &cand_off
22293 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22294 {
22295 struct dwarf2_cu *cand_cu = cu;
22296 struct die_info *cand
22297 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22298 if (!cand
22299 || !cand->parent
22300 || cand->parent->tag != DW_TAG_subprogram)
22301 continue;
22302
22303 CORE_ADDR pc_low, pc_high;
22304 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22305 if (pc_low == ((CORE_ADDR) -1))
22306 continue;
22307 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22308 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22309 if (!(pc_low <= pc && pc < pc_high))
22310 continue;
22311
22312 die = cand;
22313 attr = dwarf2_attr (die, DW_AT_location, cu);
22314 break;
22315 }
22316 }
22317
22318 if (!attr)
22319 {
22320 /* DWARF: "If there is no such attribute, then there is no effect.".
22321 DATA is ignored if SIZE is 0. */
22322
22323 retval.data = NULL;
22324 retval.size = 0;
22325 }
22326 else if (attr->form_is_section_offset ())
22327 {
22328 struct dwarf2_loclist_baton loclist_baton;
22329 CORE_ADDR pc = (*get_frame_pc) (baton);
22330 size_t size;
22331
22332 fill_in_loclist_baton (cu, &loclist_baton, attr);
22333
22334 retval.data = dwarf2_find_location_expression (&loclist_baton,
22335 &size, pc);
22336 retval.size = size;
22337 }
22338 else
22339 {
22340 if (!attr->form_is_block ())
22341 error (_("Dwarf Error: DIE at %s referenced in module %s "
22342 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22343 sect_offset_str (sect_off), objfile_name (objfile));
22344
22345 retval.data = DW_BLOCK (attr)->data;
22346 retval.size = DW_BLOCK (attr)->size;
22347 }
22348 retval.per_objfile = dwarf2_per_objfile;
22349 retval.per_cu = cu->per_cu;
22350
22351 age_cached_comp_units (dwarf2_per_objfile);
22352
22353 return retval;
22354 }
22355
22356 /* See read.h. */
22357
22358 struct dwarf2_locexpr_baton
22359 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22360 dwarf2_per_cu_data *per_cu,
22361 CORE_ADDR (*get_frame_pc) (void *baton),
22362 void *baton)
22363 {
22364 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22365
22366 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22367 }
22368
22369 /* Write a constant of a given type as target-ordered bytes into
22370 OBSTACK. */
22371
22372 static const gdb_byte *
22373 write_constant_as_bytes (struct obstack *obstack,
22374 enum bfd_endian byte_order,
22375 struct type *type,
22376 ULONGEST value,
22377 LONGEST *len)
22378 {
22379 gdb_byte *result;
22380
22381 *len = TYPE_LENGTH (type);
22382 result = (gdb_byte *) obstack_alloc (obstack, *len);
22383 store_unsigned_integer (result, *len, byte_order, value);
22384
22385 return result;
22386 }
22387
22388 /* See read.h. */
22389
22390 const gdb_byte *
22391 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22392 dwarf2_per_cu_data *per_cu,
22393 obstack *obstack,
22394 LONGEST *len)
22395 {
22396 struct dwarf2_cu *cu;
22397 struct die_info *die;
22398 struct attribute *attr;
22399 const gdb_byte *result = NULL;
22400 struct type *type;
22401 LONGEST value;
22402 enum bfd_endian byte_order;
22403 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22404
22405 if (per_cu->cu == NULL)
22406 load_cu (per_cu, false);
22407 cu = per_cu->cu;
22408 if (cu == NULL)
22409 {
22410 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22411 Instead just throw an error, not much else we can do. */
22412 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22413 sect_offset_str (sect_off), objfile_name (objfile));
22414 }
22415
22416 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22417 if (!die)
22418 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22419 sect_offset_str (sect_off), objfile_name (objfile));
22420
22421 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22422 if (attr == NULL)
22423 return NULL;
22424
22425 byte_order = (bfd_big_endian (objfile->obfd)
22426 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22427
22428 switch (attr->form)
22429 {
22430 case DW_FORM_addr:
22431 case DW_FORM_addrx:
22432 case DW_FORM_GNU_addr_index:
22433 {
22434 gdb_byte *tem;
22435
22436 *len = cu->header.addr_size;
22437 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22438 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22439 result = tem;
22440 }
22441 break;
22442 case DW_FORM_string:
22443 case DW_FORM_strp:
22444 case DW_FORM_strx:
22445 case DW_FORM_GNU_str_index:
22446 case DW_FORM_GNU_strp_alt:
22447 /* DW_STRING is already allocated on the objfile obstack, point
22448 directly to it. */
22449 result = (const gdb_byte *) DW_STRING (attr);
22450 *len = strlen (DW_STRING (attr));
22451 break;
22452 case DW_FORM_block1:
22453 case DW_FORM_block2:
22454 case DW_FORM_block4:
22455 case DW_FORM_block:
22456 case DW_FORM_exprloc:
22457 case DW_FORM_data16:
22458 result = DW_BLOCK (attr)->data;
22459 *len = DW_BLOCK (attr)->size;
22460 break;
22461
22462 /* The DW_AT_const_value attributes are supposed to carry the
22463 symbol's value "represented as it would be on the target
22464 architecture." By the time we get here, it's already been
22465 converted to host endianness, so we just need to sign- or
22466 zero-extend it as appropriate. */
22467 case DW_FORM_data1:
22468 type = die_type (die, cu);
22469 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22470 if (result == NULL)
22471 result = write_constant_as_bytes (obstack, byte_order,
22472 type, value, len);
22473 break;
22474 case DW_FORM_data2:
22475 type = die_type (die, cu);
22476 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22477 if (result == NULL)
22478 result = write_constant_as_bytes (obstack, byte_order,
22479 type, value, len);
22480 break;
22481 case DW_FORM_data4:
22482 type = die_type (die, cu);
22483 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22484 if (result == NULL)
22485 result = write_constant_as_bytes (obstack, byte_order,
22486 type, value, len);
22487 break;
22488 case DW_FORM_data8:
22489 type = die_type (die, cu);
22490 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22491 if (result == NULL)
22492 result = write_constant_as_bytes (obstack, byte_order,
22493 type, value, len);
22494 break;
22495
22496 case DW_FORM_sdata:
22497 case DW_FORM_implicit_const:
22498 type = die_type (die, cu);
22499 result = write_constant_as_bytes (obstack, byte_order,
22500 type, DW_SND (attr), len);
22501 break;
22502
22503 case DW_FORM_udata:
22504 type = die_type (die, cu);
22505 result = write_constant_as_bytes (obstack, byte_order,
22506 type, DW_UNSND (attr), len);
22507 break;
22508
22509 default:
22510 complaint (_("unsupported const value attribute form: '%s'"),
22511 dwarf_form_name (attr->form));
22512 break;
22513 }
22514
22515 return result;
22516 }
22517
22518 /* See read.h. */
22519
22520 struct type *
22521 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22522 dwarf2_per_cu_data *per_cu)
22523 {
22524 struct dwarf2_cu *cu;
22525 struct die_info *die;
22526
22527 if (per_cu->cu == NULL)
22528 load_cu (per_cu, false);
22529 cu = per_cu->cu;
22530 if (!cu)
22531 return NULL;
22532
22533 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22534 if (!die)
22535 return NULL;
22536
22537 return die_type (die, cu);
22538 }
22539
22540 /* See read.h. */
22541
22542 struct type *
22543 dwarf2_get_die_type (cu_offset die_offset,
22544 struct dwarf2_per_cu_data *per_cu)
22545 {
22546 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22547 return get_die_type_at_offset (die_offset_sect, per_cu);
22548 }
22549
22550 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22551 On entry *REF_CU is the CU of SRC_DIE.
22552 On exit *REF_CU is the CU of the result.
22553 Returns NULL if the referenced DIE isn't found. */
22554
22555 static struct die_info *
22556 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22557 struct dwarf2_cu **ref_cu)
22558 {
22559 struct die_info temp_die;
22560 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22561 struct die_info *die;
22562
22563 /* While it might be nice to assert sig_type->type == NULL here,
22564 we can get here for DW_AT_imported_declaration where we need
22565 the DIE not the type. */
22566
22567 /* If necessary, add it to the queue and load its DIEs. */
22568
22569 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22570 read_signatured_type (sig_type);
22571
22572 sig_cu = sig_type->per_cu.cu;
22573 gdb_assert (sig_cu != NULL);
22574 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22575 temp_die.sect_off = sig_type->type_offset_in_section;
22576 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22577 to_underlying (temp_die.sect_off));
22578 if (die)
22579 {
22580 struct dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22581
22582 /* For .gdb_index version 7 keep track of included TUs.
22583 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22584 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22585 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22586 {
22587 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22588 }
22589
22590 *ref_cu = sig_cu;
22591 if (sig_cu != cu)
22592 sig_cu->ancestor = cu;
22593
22594 return die;
22595 }
22596
22597 return NULL;
22598 }
22599
22600 /* Follow signatured type referenced by ATTR in SRC_DIE.
22601 On entry *REF_CU is the CU of SRC_DIE.
22602 On exit *REF_CU is the CU of the result.
22603 The result is the DIE of the type.
22604 If the referenced type cannot be found an error is thrown. */
22605
22606 static struct die_info *
22607 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22608 struct dwarf2_cu **ref_cu)
22609 {
22610 ULONGEST signature = DW_SIGNATURE (attr);
22611 struct signatured_type *sig_type;
22612 struct die_info *die;
22613
22614 gdb_assert (attr->form == DW_FORM_ref_sig8);
22615
22616 sig_type = lookup_signatured_type (*ref_cu, signature);
22617 /* sig_type will be NULL if the signatured type is missing from
22618 the debug info. */
22619 if (sig_type == NULL)
22620 {
22621 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22622 " from DIE at %s [in module %s]"),
22623 hex_string (signature), sect_offset_str (src_die->sect_off),
22624 objfile_name ((*ref_cu)->per_objfile->objfile));
22625 }
22626
22627 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22628 if (die == NULL)
22629 {
22630 dump_die_for_error (src_die);
22631 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22632 " from DIE at %s [in module %s]"),
22633 hex_string (signature), sect_offset_str (src_die->sect_off),
22634 objfile_name ((*ref_cu)->per_objfile->objfile));
22635 }
22636
22637 return die;
22638 }
22639
22640 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22641 reading in and processing the type unit if necessary. */
22642
22643 static struct type *
22644 get_signatured_type (struct die_info *die, ULONGEST signature,
22645 struct dwarf2_cu *cu)
22646 {
22647 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22648 struct signatured_type *sig_type;
22649 struct dwarf2_cu *type_cu;
22650 struct die_info *type_die;
22651 struct type *type;
22652
22653 sig_type = lookup_signatured_type (cu, signature);
22654 /* sig_type will be NULL if the signatured type is missing from
22655 the debug info. */
22656 if (sig_type == NULL)
22657 {
22658 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22659 " from DIE at %s [in module %s]"),
22660 hex_string (signature), sect_offset_str (die->sect_off),
22661 objfile_name (dwarf2_per_objfile->objfile));
22662 return build_error_marker_type (cu, die);
22663 }
22664
22665 /* If we already know the type we're done. */
22666 if (sig_type->type != NULL)
22667 return sig_type->type;
22668
22669 type_cu = cu;
22670 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22671 if (type_die != NULL)
22672 {
22673 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22674 is created. This is important, for example, because for c++ classes
22675 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22676 type = read_type_die (type_die, type_cu);
22677 if (type == NULL)
22678 {
22679 complaint (_("Dwarf Error: Cannot build signatured type %s"
22680 " referenced from DIE at %s [in module %s]"),
22681 hex_string (signature), sect_offset_str (die->sect_off),
22682 objfile_name (dwarf2_per_objfile->objfile));
22683 type = build_error_marker_type (cu, die);
22684 }
22685 }
22686 else
22687 {
22688 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22689 " from DIE at %s [in module %s]"),
22690 hex_string (signature), sect_offset_str (die->sect_off),
22691 objfile_name (dwarf2_per_objfile->objfile));
22692 type = build_error_marker_type (cu, die);
22693 }
22694 sig_type->type = type;
22695
22696 return type;
22697 }
22698
22699 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22700 reading in and processing the type unit if necessary. */
22701
22702 static struct type *
22703 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22704 struct dwarf2_cu *cu) /* ARI: editCase function */
22705 {
22706 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22707 if (attr->form_is_ref ())
22708 {
22709 struct dwarf2_cu *type_cu = cu;
22710 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22711
22712 return read_type_die (type_die, type_cu);
22713 }
22714 else if (attr->form == DW_FORM_ref_sig8)
22715 {
22716 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22717 }
22718 else
22719 {
22720 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22721
22722 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22723 " at %s [in module %s]"),
22724 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22725 objfile_name (dwarf2_per_objfile->objfile));
22726 return build_error_marker_type (cu, die);
22727 }
22728 }
22729
22730 /* Load the DIEs associated with type unit PER_CU into memory. */
22731
22732 static void
22733 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22734 {
22735 struct signatured_type *sig_type;
22736
22737 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22738 gdb_assert (! per_cu->type_unit_group_p ());
22739
22740 /* We have the per_cu, but we need the signatured_type.
22741 Fortunately this is an easy translation. */
22742 gdb_assert (per_cu->is_debug_types);
22743 sig_type = (struct signatured_type *) per_cu;
22744
22745 gdb_assert (per_cu->cu == NULL);
22746
22747 read_signatured_type (sig_type);
22748
22749 gdb_assert (per_cu->cu != NULL);
22750 }
22751
22752 /* Read in a signatured type and build its CU and DIEs.
22753 If the type is a stub for the real type in a DWO file,
22754 read in the real type from the DWO file as well. */
22755
22756 static void
22757 read_signatured_type (struct signatured_type *sig_type)
22758 {
22759 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22760
22761 gdb_assert (per_cu->is_debug_types);
22762 gdb_assert (per_cu->cu == NULL);
22763
22764 cutu_reader reader (per_cu, NULL, 0, false);
22765
22766 if (!reader.dummy_p)
22767 {
22768 struct dwarf2_cu *cu = reader.cu;
22769 const gdb_byte *info_ptr = reader.info_ptr;
22770
22771 gdb_assert (cu->die_hash == NULL);
22772 cu->die_hash =
22773 htab_create_alloc_ex (cu->header.length / 12,
22774 die_hash,
22775 die_eq,
22776 NULL,
22777 &cu->comp_unit_obstack,
22778 hashtab_obstack_allocate,
22779 dummy_obstack_deallocate);
22780
22781 if (reader.comp_unit_die->has_children)
22782 reader.comp_unit_die->child
22783 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22784 reader.comp_unit_die);
22785 cu->dies = reader.comp_unit_die;
22786 /* comp_unit_die is not stored in die_hash, no need. */
22787
22788 /* We try not to read any attributes in this function, because
22789 not all CUs needed for references have been loaded yet, and
22790 symbol table processing isn't initialized. But we have to
22791 set the CU language, or we won't be able to build types
22792 correctly. Similarly, if we do not read the producer, we can
22793 not apply producer-specific interpretation. */
22794 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22795
22796 reader.keep ();
22797 }
22798
22799 sig_type->per_cu.tu_read = 1;
22800 }
22801
22802 /* Decode simple location descriptions.
22803 Given a pointer to a dwarf block that defines a location, compute
22804 the location and return the value. If COMPUTED is non-null, it is
22805 set to true to indicate that decoding was successful, and false
22806 otherwise. If COMPUTED is null, then this function may emit a
22807 complaint. */
22808
22809 static CORE_ADDR
22810 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22811 {
22812 struct objfile *objfile = cu->per_objfile->objfile;
22813 size_t i;
22814 size_t size = blk->size;
22815 const gdb_byte *data = blk->data;
22816 CORE_ADDR stack[64];
22817 int stacki;
22818 unsigned int bytes_read, unsnd;
22819 gdb_byte op;
22820
22821 if (computed != nullptr)
22822 *computed = false;
22823
22824 i = 0;
22825 stacki = 0;
22826 stack[stacki] = 0;
22827 stack[++stacki] = 0;
22828
22829 while (i < size)
22830 {
22831 op = data[i++];
22832 switch (op)
22833 {
22834 case DW_OP_lit0:
22835 case DW_OP_lit1:
22836 case DW_OP_lit2:
22837 case DW_OP_lit3:
22838 case DW_OP_lit4:
22839 case DW_OP_lit5:
22840 case DW_OP_lit6:
22841 case DW_OP_lit7:
22842 case DW_OP_lit8:
22843 case DW_OP_lit9:
22844 case DW_OP_lit10:
22845 case DW_OP_lit11:
22846 case DW_OP_lit12:
22847 case DW_OP_lit13:
22848 case DW_OP_lit14:
22849 case DW_OP_lit15:
22850 case DW_OP_lit16:
22851 case DW_OP_lit17:
22852 case DW_OP_lit18:
22853 case DW_OP_lit19:
22854 case DW_OP_lit20:
22855 case DW_OP_lit21:
22856 case DW_OP_lit22:
22857 case DW_OP_lit23:
22858 case DW_OP_lit24:
22859 case DW_OP_lit25:
22860 case DW_OP_lit26:
22861 case DW_OP_lit27:
22862 case DW_OP_lit28:
22863 case DW_OP_lit29:
22864 case DW_OP_lit30:
22865 case DW_OP_lit31:
22866 stack[++stacki] = op - DW_OP_lit0;
22867 break;
22868
22869 case DW_OP_reg0:
22870 case DW_OP_reg1:
22871 case DW_OP_reg2:
22872 case DW_OP_reg3:
22873 case DW_OP_reg4:
22874 case DW_OP_reg5:
22875 case DW_OP_reg6:
22876 case DW_OP_reg7:
22877 case DW_OP_reg8:
22878 case DW_OP_reg9:
22879 case DW_OP_reg10:
22880 case DW_OP_reg11:
22881 case DW_OP_reg12:
22882 case DW_OP_reg13:
22883 case DW_OP_reg14:
22884 case DW_OP_reg15:
22885 case DW_OP_reg16:
22886 case DW_OP_reg17:
22887 case DW_OP_reg18:
22888 case DW_OP_reg19:
22889 case DW_OP_reg20:
22890 case DW_OP_reg21:
22891 case DW_OP_reg22:
22892 case DW_OP_reg23:
22893 case DW_OP_reg24:
22894 case DW_OP_reg25:
22895 case DW_OP_reg26:
22896 case DW_OP_reg27:
22897 case DW_OP_reg28:
22898 case DW_OP_reg29:
22899 case DW_OP_reg30:
22900 case DW_OP_reg31:
22901 stack[++stacki] = op - DW_OP_reg0;
22902 if (i < size)
22903 {
22904 if (computed == nullptr)
22905 dwarf2_complex_location_expr_complaint ();
22906 else
22907 return 0;
22908 }
22909 break;
22910
22911 case DW_OP_regx:
22912 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22913 i += bytes_read;
22914 stack[++stacki] = unsnd;
22915 if (i < size)
22916 {
22917 if (computed == nullptr)
22918 dwarf2_complex_location_expr_complaint ();
22919 else
22920 return 0;
22921 }
22922 break;
22923
22924 case DW_OP_addr:
22925 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22926 &bytes_read);
22927 i += bytes_read;
22928 break;
22929
22930 case DW_OP_const1u:
22931 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22932 i += 1;
22933 break;
22934
22935 case DW_OP_const1s:
22936 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22937 i += 1;
22938 break;
22939
22940 case DW_OP_const2u:
22941 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22942 i += 2;
22943 break;
22944
22945 case DW_OP_const2s:
22946 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22947 i += 2;
22948 break;
22949
22950 case DW_OP_const4u:
22951 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22952 i += 4;
22953 break;
22954
22955 case DW_OP_const4s:
22956 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22957 i += 4;
22958 break;
22959
22960 case DW_OP_const8u:
22961 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22962 i += 8;
22963 break;
22964
22965 case DW_OP_constu:
22966 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22967 &bytes_read);
22968 i += bytes_read;
22969 break;
22970
22971 case DW_OP_consts:
22972 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22973 i += bytes_read;
22974 break;
22975
22976 case DW_OP_dup:
22977 stack[stacki + 1] = stack[stacki];
22978 stacki++;
22979 break;
22980
22981 case DW_OP_plus:
22982 stack[stacki - 1] += stack[stacki];
22983 stacki--;
22984 break;
22985
22986 case DW_OP_plus_uconst:
22987 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22988 &bytes_read);
22989 i += bytes_read;
22990 break;
22991
22992 case DW_OP_minus:
22993 stack[stacki - 1] -= stack[stacki];
22994 stacki--;
22995 break;
22996
22997 case DW_OP_deref:
22998 /* If we're not the last op, then we definitely can't encode
22999 this using GDB's address_class enum. This is valid for partial
23000 global symbols, although the variable's address will be bogus
23001 in the psymtab. */
23002 if (i < size)
23003 {
23004 if (computed == nullptr)
23005 dwarf2_complex_location_expr_complaint ();
23006 else
23007 return 0;
23008 }
23009 break;
23010
23011 case DW_OP_GNU_push_tls_address:
23012 case DW_OP_form_tls_address:
23013 /* The top of the stack has the offset from the beginning
23014 of the thread control block at which the variable is located. */
23015 /* Nothing should follow this operator, so the top of stack would
23016 be returned. */
23017 /* This is valid for partial global symbols, but the variable's
23018 address will be bogus in the psymtab. Make it always at least
23019 non-zero to not look as a variable garbage collected by linker
23020 which have DW_OP_addr 0. */
23021 if (i < size)
23022 {
23023 if (computed == nullptr)
23024 dwarf2_complex_location_expr_complaint ();
23025 else
23026 return 0;
23027 }
23028 stack[stacki]++;
23029 break;
23030
23031 case DW_OP_GNU_uninit:
23032 if (computed != nullptr)
23033 return 0;
23034 break;
23035
23036 case DW_OP_addrx:
23037 case DW_OP_GNU_addr_index:
23038 case DW_OP_GNU_const_index:
23039 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23040 &bytes_read);
23041 i += bytes_read;
23042 break;
23043
23044 default:
23045 if (computed == nullptr)
23046 {
23047 const char *name = get_DW_OP_name (op);
23048
23049 if (name)
23050 complaint (_("unsupported stack op: '%s'"),
23051 name);
23052 else
23053 complaint (_("unsupported stack op: '%02x'"),
23054 op);
23055 }
23056
23057 return (stack[stacki]);
23058 }
23059
23060 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23061 outside of the allocated space. Also enforce minimum>0. */
23062 if (stacki >= ARRAY_SIZE (stack) - 1)
23063 {
23064 if (computed == nullptr)
23065 complaint (_("location description stack overflow"));
23066 return 0;
23067 }
23068
23069 if (stacki <= 0)
23070 {
23071 if (computed == nullptr)
23072 complaint (_("location description stack underflow"));
23073 return 0;
23074 }
23075 }
23076
23077 if (computed != nullptr)
23078 *computed = true;
23079 return (stack[stacki]);
23080 }
23081
23082 /* memory allocation interface */
23083
23084 static struct dwarf_block *
23085 dwarf_alloc_block (struct dwarf2_cu *cu)
23086 {
23087 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23088 }
23089
23090 static struct die_info *
23091 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23092 {
23093 struct die_info *die;
23094 size_t size = sizeof (struct die_info);
23095
23096 if (num_attrs > 1)
23097 size += (num_attrs - 1) * sizeof (struct attribute);
23098
23099 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23100 memset (die, 0, sizeof (struct die_info));
23101 return (die);
23102 }
23103
23104 \f
23105
23106 /* Macro support. */
23107
23108 /* An overload of dwarf_decode_macros that finds the correct section
23109 and ensures it is read in before calling the other overload. */
23110
23111 static void
23112 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23113 int section_is_gnu)
23114 {
23115 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23116 struct objfile *objfile = dwarf2_per_objfile->objfile;
23117 const struct line_header *lh = cu->line_header;
23118 unsigned int offset_size = cu->header.offset_size;
23119 struct dwarf2_section_info *section;
23120 const char *section_name;
23121
23122 if (cu->dwo_unit != nullptr)
23123 {
23124 if (section_is_gnu)
23125 {
23126 section = &cu->dwo_unit->dwo_file->sections.macro;
23127 section_name = ".debug_macro.dwo";
23128 }
23129 else
23130 {
23131 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23132 section_name = ".debug_macinfo.dwo";
23133 }
23134 }
23135 else
23136 {
23137 if (section_is_gnu)
23138 {
23139 section = &dwarf2_per_objfile->per_bfd->macro;
23140 section_name = ".debug_macro";
23141 }
23142 else
23143 {
23144 section = &dwarf2_per_objfile->per_bfd->macinfo;
23145 section_name = ".debug_macinfo";
23146 }
23147 }
23148
23149 section->read (objfile);
23150 if (section->buffer == nullptr)
23151 {
23152 complaint (_("missing %s section"), section_name);
23153 return;
23154 }
23155
23156 buildsym_compunit *builder = cu->get_builder ();
23157
23158 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23159 offset_size, offset, section_is_gnu);
23160 }
23161
23162 /* Return the .debug_loc section to use for CU.
23163 For DWO files use .debug_loc.dwo. */
23164
23165 static struct dwarf2_section_info *
23166 cu_debug_loc_section (struct dwarf2_cu *cu)
23167 {
23168 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23169
23170 if (cu->dwo_unit)
23171 {
23172 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23173
23174 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23175 }
23176 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23177 : &dwarf2_per_objfile->per_bfd->loc);
23178 }
23179
23180 /* A helper function that fills in a dwarf2_loclist_baton. */
23181
23182 static void
23183 fill_in_loclist_baton (struct dwarf2_cu *cu,
23184 struct dwarf2_loclist_baton *baton,
23185 const struct attribute *attr)
23186 {
23187 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23188 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23189
23190 section->read (dwarf2_per_objfile->objfile);
23191
23192 baton->per_objfile = dwarf2_per_objfile;
23193 baton->per_cu = cu->per_cu;
23194 gdb_assert (baton->per_cu);
23195 /* We don't know how long the location list is, but make sure we
23196 don't run off the edge of the section. */
23197 baton->size = section->size - DW_UNSND (attr);
23198 baton->data = section->buffer + DW_UNSND (attr);
23199 if (cu->base_address.has_value ())
23200 baton->base_address = *cu->base_address;
23201 else
23202 baton->base_address = 0;
23203 baton->from_dwo = cu->dwo_unit != NULL;
23204 }
23205
23206 static void
23207 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23208 struct dwarf2_cu *cu, int is_block)
23209 {
23210 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23211 struct objfile *objfile = dwarf2_per_objfile->objfile;
23212 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23213
23214 if (attr->form_is_section_offset ()
23215 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23216 the section. If so, fall through to the complaint in the
23217 other branch. */
23218 && DW_UNSND (attr) < section->get_size (objfile))
23219 {
23220 struct dwarf2_loclist_baton *baton;
23221
23222 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23223
23224 fill_in_loclist_baton (cu, baton, attr);
23225
23226 if (!cu->base_address.has_value ())
23227 complaint (_("Location list used without "
23228 "specifying the CU base address."));
23229
23230 SYMBOL_ACLASS_INDEX (sym) = (is_block
23231 ? dwarf2_loclist_block_index
23232 : dwarf2_loclist_index);
23233 SYMBOL_LOCATION_BATON (sym) = baton;
23234 }
23235 else
23236 {
23237 struct dwarf2_locexpr_baton *baton;
23238
23239 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23240 baton->per_objfile = dwarf2_per_objfile;
23241 baton->per_cu = cu->per_cu;
23242 gdb_assert (baton->per_cu);
23243
23244 if (attr->form_is_block ())
23245 {
23246 /* Note that we're just copying the block's data pointer
23247 here, not the actual data. We're still pointing into the
23248 info_buffer for SYM's objfile; right now we never release
23249 that buffer, but when we do clean up properly this may
23250 need to change. */
23251 baton->size = DW_BLOCK (attr)->size;
23252 baton->data = DW_BLOCK (attr)->data;
23253 }
23254 else
23255 {
23256 dwarf2_invalid_attrib_class_complaint ("location description",
23257 sym->natural_name ());
23258 baton->size = 0;
23259 }
23260
23261 SYMBOL_ACLASS_INDEX (sym) = (is_block
23262 ? dwarf2_locexpr_block_index
23263 : dwarf2_locexpr_index);
23264 SYMBOL_LOCATION_BATON (sym) = baton;
23265 }
23266 }
23267
23268 /* See read.h. */
23269
23270 struct objfile *
23271 dwarf2_per_cu_data::objfile () const
23272 {
23273 struct objfile *objfile = dwarf2_per_objfile->objfile;
23274
23275 /* Return the master objfile, so that we can report and look up the
23276 correct file containing this variable. */
23277 if (objfile->separate_debug_objfile_backlink)
23278 objfile = objfile->separate_debug_objfile_backlink;
23279
23280 return objfile;
23281 }
23282
23283 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23284 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23285 CU_HEADERP first. */
23286
23287 static const struct comp_unit_head *
23288 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23289 const struct dwarf2_per_cu_data *per_cu)
23290 {
23291 const gdb_byte *info_ptr;
23292
23293 if (per_cu->cu)
23294 return &per_cu->cu->header;
23295
23296 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23297
23298 memset (cu_headerp, 0, sizeof (*cu_headerp));
23299 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23300 rcuh_kind::COMPILE);
23301
23302 return cu_headerp;
23303 }
23304
23305 /* See read.h. */
23306
23307 int
23308 dwarf2_per_cu_data::addr_size () const
23309 {
23310 struct comp_unit_head cu_header_local;
23311 const struct comp_unit_head *cu_headerp;
23312
23313 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23314
23315 return cu_headerp->addr_size;
23316 }
23317
23318 /* See read.h. */
23319
23320 int
23321 dwarf2_per_cu_data::offset_size () const
23322 {
23323 struct comp_unit_head cu_header_local;
23324 const struct comp_unit_head *cu_headerp;
23325
23326 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23327
23328 return cu_headerp->offset_size;
23329 }
23330
23331 /* See read.h. */
23332
23333 int
23334 dwarf2_per_cu_data::ref_addr_size () const
23335 {
23336 struct comp_unit_head cu_header_local;
23337 const struct comp_unit_head *cu_headerp;
23338
23339 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23340
23341 if (cu_headerp->version == 2)
23342 return cu_headerp->addr_size;
23343 else
23344 return cu_headerp->offset_size;
23345 }
23346
23347 /* See read.h. */
23348
23349 CORE_ADDR
23350 dwarf2_per_cu_data::text_offset () const
23351 {
23352 struct objfile *objfile = dwarf2_per_objfile->objfile;
23353
23354 return objfile->text_section_offset ();
23355 }
23356
23357 /* See read.h. */
23358
23359 struct type *
23360 dwarf2_per_cu_data::addr_type () const
23361 {
23362 struct objfile *objfile = dwarf2_per_objfile->objfile;
23363 struct type *void_type = objfile_type (objfile)->builtin_void;
23364 struct type *addr_type = lookup_pointer_type (void_type);
23365 int addr_size = this->addr_size ();
23366
23367 if (TYPE_LENGTH (addr_type) == addr_size)
23368 return addr_type;
23369
23370 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23371 return addr_type;
23372 }
23373
23374 /* A helper function for dwarf2_find_containing_comp_unit that returns
23375 the index of the result, and that searches a vector. It will
23376 return a result even if the offset in question does not actually
23377 occur in any CU. This is separate so that it can be unit
23378 tested. */
23379
23380 static int
23381 dwarf2_find_containing_comp_unit
23382 (sect_offset sect_off,
23383 unsigned int offset_in_dwz,
23384 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23385 {
23386 int low, high;
23387
23388 low = 0;
23389 high = all_comp_units.size () - 1;
23390 while (high > low)
23391 {
23392 struct dwarf2_per_cu_data *mid_cu;
23393 int mid = low + (high - low) / 2;
23394
23395 mid_cu = all_comp_units[mid];
23396 if (mid_cu->is_dwz > offset_in_dwz
23397 || (mid_cu->is_dwz == offset_in_dwz
23398 && mid_cu->sect_off + mid_cu->length > sect_off))
23399 high = mid;
23400 else
23401 low = mid + 1;
23402 }
23403 gdb_assert (low == high);
23404 return low;
23405 }
23406
23407 /* Locate the .debug_info compilation unit from CU's objfile which contains
23408 the DIE at OFFSET. Raises an error on failure. */
23409
23410 static struct dwarf2_per_cu_data *
23411 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23412 unsigned int offset_in_dwz,
23413 struct dwarf2_per_objfile *dwarf2_per_objfile)
23414 {
23415 int low
23416 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23417 dwarf2_per_objfile->per_bfd->all_comp_units);
23418 struct dwarf2_per_cu_data *this_cu
23419 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23420
23421 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23422 {
23423 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23424 error (_("Dwarf Error: could not find partial DIE containing "
23425 "offset %s [in module %s]"),
23426 sect_offset_str (sect_off),
23427 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23428
23429 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23430 <= sect_off);
23431 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23432 }
23433 else
23434 {
23435 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23436 && sect_off >= this_cu->sect_off + this_cu->length)
23437 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23438 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23439 return this_cu;
23440 }
23441 }
23442
23443 #if GDB_SELF_TEST
23444
23445 namespace selftests {
23446 namespace find_containing_comp_unit {
23447
23448 static void
23449 run_test ()
23450 {
23451 struct dwarf2_per_cu_data one {};
23452 struct dwarf2_per_cu_data two {};
23453 struct dwarf2_per_cu_data three {};
23454 struct dwarf2_per_cu_data four {};
23455
23456 one.length = 5;
23457 two.sect_off = sect_offset (one.length);
23458 two.length = 7;
23459
23460 three.length = 5;
23461 three.is_dwz = 1;
23462 four.sect_off = sect_offset (three.length);
23463 four.length = 7;
23464 four.is_dwz = 1;
23465
23466 std::vector<dwarf2_per_cu_data *> units;
23467 units.push_back (&one);
23468 units.push_back (&two);
23469 units.push_back (&three);
23470 units.push_back (&four);
23471
23472 int result;
23473
23474 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23475 SELF_CHECK (units[result] == &one);
23476 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23477 SELF_CHECK (units[result] == &one);
23478 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23479 SELF_CHECK (units[result] == &two);
23480
23481 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23482 SELF_CHECK (units[result] == &three);
23483 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23484 SELF_CHECK (units[result] == &three);
23485 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23486 SELF_CHECK (units[result] == &four);
23487 }
23488
23489 }
23490 }
23491
23492 #endif /* GDB_SELF_TEST */
23493
23494 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23495
23496 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23497 dwarf2_per_objfile *per_objfile)
23498 : per_cu (per_cu),
23499 per_objfile (per_objfile),
23500 mark (false),
23501 has_loclist (false),
23502 checked_producer (false),
23503 producer_is_gxx_lt_4_6 (false),
23504 producer_is_gcc_lt_4_3 (false),
23505 producer_is_icc (false),
23506 producer_is_icc_lt_14 (false),
23507 producer_is_codewarrior (false),
23508 processing_has_namespace_info (false)
23509 {
23510 per_cu->cu = this;
23511 }
23512
23513 /* Destroy a dwarf2_cu. */
23514
23515 dwarf2_cu::~dwarf2_cu ()
23516 {
23517 per_cu->cu = NULL;
23518 }
23519
23520 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23521
23522 static void
23523 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23524 enum language pretend_language)
23525 {
23526 struct attribute *attr;
23527
23528 /* Set the language we're debugging. */
23529 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23530 if (attr != nullptr)
23531 set_cu_language (DW_UNSND (attr), cu);
23532 else
23533 {
23534 cu->language = pretend_language;
23535 cu->language_defn = language_def (cu->language);
23536 }
23537
23538 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23539 }
23540
23541 /* Increase the age counter on each cached compilation unit, and free
23542 any that are too old. */
23543
23544 static void
23545 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23546 {
23547 struct dwarf2_per_cu_data *per_cu, **last_chain;
23548
23549 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23550 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23551 while (per_cu != NULL)
23552 {
23553 per_cu->cu->last_used ++;
23554 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23555 dwarf2_mark (per_cu->cu);
23556 per_cu = per_cu->cu->read_in_chain;
23557 }
23558
23559 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23560 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23561 while (per_cu != NULL)
23562 {
23563 struct dwarf2_per_cu_data *next_cu;
23564
23565 next_cu = per_cu->cu->read_in_chain;
23566
23567 if (!per_cu->cu->mark)
23568 {
23569 delete per_cu->cu;
23570 *last_chain = next_cu;
23571 }
23572 else
23573 last_chain = &per_cu->cu->read_in_chain;
23574
23575 per_cu = next_cu;
23576 }
23577 }
23578
23579 /* Remove a single compilation unit from the cache. */
23580
23581 static void
23582 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23583 {
23584 struct dwarf2_per_cu_data *per_cu, **last_chain;
23585 struct dwarf2_per_objfile *dwarf2_per_objfile
23586 = target_per_cu->dwarf2_per_objfile;
23587
23588 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23589 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23590 while (per_cu != NULL)
23591 {
23592 struct dwarf2_per_cu_data *next_cu;
23593
23594 next_cu = per_cu->cu->read_in_chain;
23595
23596 if (per_cu == target_per_cu)
23597 {
23598 delete per_cu->cu;
23599 per_cu->cu = NULL;
23600 *last_chain = next_cu;
23601 break;
23602 }
23603 else
23604 last_chain = &per_cu->cu->read_in_chain;
23605
23606 per_cu = next_cu;
23607 }
23608 }
23609
23610 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23611 We store these in a hash table separate from the DIEs, and preserve them
23612 when the DIEs are flushed out of cache.
23613
23614 The CU "per_cu" pointer is needed because offset alone is not enough to
23615 uniquely identify the type. A file may have multiple .debug_types sections,
23616 or the type may come from a DWO file. Furthermore, while it's more logical
23617 to use per_cu->section+offset, with Fission the section with the data is in
23618 the DWO file but we don't know that section at the point we need it.
23619 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23620 because we can enter the lookup routine, get_die_type_at_offset, from
23621 outside this file, and thus won't necessarily have PER_CU->cu.
23622 Fortunately, PER_CU is stable for the life of the objfile. */
23623
23624 struct dwarf2_per_cu_offset_and_type
23625 {
23626 const struct dwarf2_per_cu_data *per_cu;
23627 sect_offset sect_off;
23628 struct type *type;
23629 };
23630
23631 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23632
23633 static hashval_t
23634 per_cu_offset_and_type_hash (const void *item)
23635 {
23636 const struct dwarf2_per_cu_offset_and_type *ofs
23637 = (const struct dwarf2_per_cu_offset_and_type *) item;
23638
23639 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23640 }
23641
23642 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23643
23644 static int
23645 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23646 {
23647 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23648 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23649 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23650 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23651
23652 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23653 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23654 }
23655
23656 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23657 table if necessary. For convenience, return TYPE.
23658
23659 The DIEs reading must have careful ordering to:
23660 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23661 reading current DIE.
23662 * Not trying to dereference contents of still incompletely read in types
23663 while reading in other DIEs.
23664 * Enable referencing still incompletely read in types just by a pointer to
23665 the type without accessing its fields.
23666
23667 Therefore caller should follow these rules:
23668 * Try to fetch any prerequisite types we may need to build this DIE type
23669 before building the type and calling set_die_type.
23670 * After building type call set_die_type for current DIE as soon as
23671 possible before fetching more types to complete the current type.
23672 * Make the type as complete as possible before fetching more types. */
23673
23674 static struct type *
23675 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23676 {
23677 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23678 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23679 struct objfile *objfile = dwarf2_per_objfile->objfile;
23680 struct attribute *attr;
23681 struct dynamic_prop prop;
23682
23683 /* For Ada types, make sure that the gnat-specific data is always
23684 initialized (if not already set). There are a few types where
23685 we should not be doing so, because the type-specific area is
23686 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23687 where the type-specific area is used to store the floatformat).
23688 But this is not a problem, because the gnat-specific information
23689 is actually not needed for these types. */
23690 if (need_gnat_info (cu)
23691 && type->code () != TYPE_CODE_FUNC
23692 && type->code () != TYPE_CODE_FLT
23693 && type->code () != TYPE_CODE_METHODPTR
23694 && type->code () != TYPE_CODE_MEMBERPTR
23695 && type->code () != TYPE_CODE_METHOD
23696 && !HAVE_GNAT_AUX_INFO (type))
23697 INIT_GNAT_SPECIFIC (type);
23698
23699 /* Read DW_AT_allocated and set in type. */
23700 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23701 if (attr != NULL && attr->form_is_block ())
23702 {
23703 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23704 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23705 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23706 }
23707 else if (attr != NULL)
23708 {
23709 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23710 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23711 sect_offset_str (die->sect_off));
23712 }
23713
23714 /* Read DW_AT_associated and set in type. */
23715 attr = dwarf2_attr (die, DW_AT_associated, cu);
23716 if (attr != NULL && attr->form_is_block ())
23717 {
23718 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23719 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23720 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23721 }
23722 else if (attr != NULL)
23723 {
23724 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23725 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23726 sect_offset_str (die->sect_off));
23727 }
23728
23729 /* Read DW_AT_data_location and set in type. */
23730 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23731 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23732 cu->per_cu->addr_type ()))
23733 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23734
23735 if (dwarf2_per_objfile->die_type_hash == NULL)
23736 dwarf2_per_objfile->die_type_hash
23737 = htab_up (htab_create_alloc (127,
23738 per_cu_offset_and_type_hash,
23739 per_cu_offset_and_type_eq,
23740 NULL, xcalloc, xfree));
23741
23742 ofs.per_cu = cu->per_cu;
23743 ofs.sect_off = die->sect_off;
23744 ofs.type = type;
23745 slot = (struct dwarf2_per_cu_offset_and_type **)
23746 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23747 if (*slot)
23748 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23749 sect_offset_str (die->sect_off));
23750 *slot = XOBNEW (&objfile->objfile_obstack,
23751 struct dwarf2_per_cu_offset_and_type);
23752 **slot = ofs;
23753 return type;
23754 }
23755
23756 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23757 or return NULL if the die does not have a saved type. */
23758
23759 static struct type *
23760 get_die_type_at_offset (sect_offset sect_off,
23761 struct dwarf2_per_cu_data *per_cu)
23762 {
23763 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23764 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23765
23766 if (dwarf2_per_objfile->die_type_hash == NULL)
23767 return NULL;
23768
23769 ofs.per_cu = per_cu;
23770 ofs.sect_off = sect_off;
23771 slot = ((struct dwarf2_per_cu_offset_and_type *)
23772 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23773 if (slot)
23774 return slot->type;
23775 else
23776 return NULL;
23777 }
23778
23779 /* Look up the type for DIE in CU in die_type_hash,
23780 or return NULL if DIE does not have a saved type. */
23781
23782 static struct type *
23783 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23784 {
23785 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23786 }
23787
23788 /* Add a dependence relationship from CU to REF_PER_CU. */
23789
23790 static void
23791 dwarf2_add_dependence (struct dwarf2_cu *cu,
23792 struct dwarf2_per_cu_data *ref_per_cu)
23793 {
23794 void **slot;
23795
23796 if (cu->dependencies == NULL)
23797 cu->dependencies
23798 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23799 NULL, &cu->comp_unit_obstack,
23800 hashtab_obstack_allocate,
23801 dummy_obstack_deallocate);
23802
23803 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23804 if (*slot == NULL)
23805 *slot = ref_per_cu;
23806 }
23807
23808 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23809 Set the mark field in every compilation unit in the
23810 cache that we must keep because we are keeping CU. */
23811
23812 static int
23813 dwarf2_mark_helper (void **slot, void *data)
23814 {
23815 struct dwarf2_per_cu_data *per_cu;
23816
23817 per_cu = (struct dwarf2_per_cu_data *) *slot;
23818
23819 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23820 reading of the chain. As such dependencies remain valid it is not much
23821 useful to track and undo them during QUIT cleanups. */
23822 if (per_cu->cu == NULL)
23823 return 1;
23824
23825 if (per_cu->cu->mark)
23826 return 1;
23827 per_cu->cu->mark = true;
23828
23829 if (per_cu->cu->dependencies != NULL)
23830 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23831
23832 return 1;
23833 }
23834
23835 /* Set the mark field in CU and in every other compilation unit in the
23836 cache that we must keep because we are keeping CU. */
23837
23838 static void
23839 dwarf2_mark (struct dwarf2_cu *cu)
23840 {
23841 if (cu->mark)
23842 return;
23843 cu->mark = true;
23844 if (cu->dependencies != NULL)
23845 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23846 }
23847
23848 static void
23849 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23850 {
23851 while (per_cu)
23852 {
23853 per_cu->cu->mark = false;
23854 per_cu = per_cu->cu->read_in_chain;
23855 }
23856 }
23857
23858 /* Trivial hash function for partial_die_info: the hash value of a DIE
23859 is its offset in .debug_info for this objfile. */
23860
23861 static hashval_t
23862 partial_die_hash (const void *item)
23863 {
23864 const struct partial_die_info *part_die
23865 = (const struct partial_die_info *) item;
23866
23867 return to_underlying (part_die->sect_off);
23868 }
23869
23870 /* Trivial comparison function for partial_die_info structures: two DIEs
23871 are equal if they have the same offset. */
23872
23873 static int
23874 partial_die_eq (const void *item_lhs, const void *item_rhs)
23875 {
23876 const struct partial_die_info *part_die_lhs
23877 = (const struct partial_die_info *) item_lhs;
23878 const struct partial_die_info *part_die_rhs
23879 = (const struct partial_die_info *) item_rhs;
23880
23881 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23882 }
23883
23884 struct cmd_list_element *set_dwarf_cmdlist;
23885 struct cmd_list_element *show_dwarf_cmdlist;
23886
23887 static void
23888 show_check_physname (struct ui_file *file, int from_tty,
23889 struct cmd_list_element *c, const char *value)
23890 {
23891 fprintf_filtered (file,
23892 _("Whether to check \"physname\" is %s.\n"),
23893 value);
23894 }
23895
23896 void _initialize_dwarf2_read ();
23897 void
23898 _initialize_dwarf2_read ()
23899 {
23900 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23901 Set DWARF specific variables.\n\
23902 Configure DWARF variables such as the cache size."),
23903 &set_dwarf_cmdlist, "maintenance set dwarf ",
23904 0/*allow-unknown*/, &maintenance_set_cmdlist);
23905
23906 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23907 Show DWARF specific variables.\n\
23908 Show DWARF variables such as the cache size."),
23909 &show_dwarf_cmdlist, "maintenance show dwarf ",
23910 0/*allow-unknown*/, &maintenance_show_cmdlist);
23911
23912 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23913 &dwarf_max_cache_age, _("\
23914 Set the upper bound on the age of cached DWARF compilation units."), _("\
23915 Show the upper bound on the age of cached DWARF compilation units."), _("\
23916 A higher limit means that cached compilation units will be stored\n\
23917 in memory longer, and more total memory will be used. Zero disables\n\
23918 caching, which can slow down startup."),
23919 NULL,
23920 show_dwarf_max_cache_age,
23921 &set_dwarf_cmdlist,
23922 &show_dwarf_cmdlist);
23923
23924 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23925 Set debugging of the DWARF reader."), _("\
23926 Show debugging of the DWARF reader."), _("\
23927 When enabled (non-zero), debugging messages are printed during DWARF\n\
23928 reading and symtab expansion. A value of 1 (one) provides basic\n\
23929 information. A value greater than 1 provides more verbose information."),
23930 NULL,
23931 NULL,
23932 &setdebuglist, &showdebuglist);
23933
23934 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23935 Set debugging of the DWARF DIE reader."), _("\
23936 Show debugging of the DWARF DIE reader."), _("\
23937 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23938 The value is the maximum depth to print."),
23939 NULL,
23940 NULL,
23941 &setdebuglist, &showdebuglist);
23942
23943 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23944 Set debugging of the dwarf line reader."), _("\
23945 Show debugging of the dwarf line reader."), _("\
23946 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23947 A value of 1 (one) provides basic information.\n\
23948 A value greater than 1 provides more verbose information."),
23949 NULL,
23950 NULL,
23951 &setdebuglist, &showdebuglist);
23952
23953 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23954 Set cross-checking of \"physname\" code against demangler."), _("\
23955 Show cross-checking of \"physname\" code against demangler."), _("\
23956 When enabled, GDB's internal \"physname\" code is checked against\n\
23957 the demangler."),
23958 NULL, show_check_physname,
23959 &setdebuglist, &showdebuglist);
23960
23961 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23962 no_class, &use_deprecated_index_sections, _("\
23963 Set whether to use deprecated gdb_index sections."), _("\
23964 Show whether to use deprecated gdb_index sections."), _("\
23965 When enabled, deprecated .gdb_index sections are used anyway.\n\
23966 Normally they are ignored either because of a missing feature or\n\
23967 performance issue.\n\
23968 Warning: This option must be enabled before gdb reads the file."),
23969 NULL,
23970 NULL,
23971 &setlist, &showlist);
23972
23973 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23974 &dwarf2_locexpr_funcs);
23975 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23976 &dwarf2_loclist_funcs);
23977
23978 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23979 &dwarf2_block_frame_base_locexpr_funcs);
23980 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23981 &dwarf2_block_frame_base_loclist_funcs);
23982
23983 #if GDB_SELF_TEST
23984 selftests::register_test ("dw2_expand_symtabs_matching",
23985 selftests::dw2_expand_symtabs_matching::run_test);
23986 selftests::register_test ("dwarf2_find_containing_comp_unit",
23987 selftests::find_containing_comp_unit::run_test);
23988 #endif
23989 }