Add dwarf2_per_objfile parameter to create_partial_symtab
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 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 (dwarf2_per_cu_data *this_cu,
916 dwarf2_per_objfile *per_objfile,
917 struct abbrev_table *abbrev_table,
918 int use_existing_cu,
919 bool skip_partial);
920
921 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
922 dwarf2_per_objfile *per_objfile,
923 struct dwarf2_cu *parent_cu = nullptr,
924 struct dwo_file *dwo_file = nullptr);
925
926 DISABLE_COPY_AND_ASSIGN (cutu_reader);
927
928 const gdb_byte *info_ptr = nullptr;
929 struct die_info *comp_unit_die = nullptr;
930 bool dummy_p = false;
931
932 /* Release the new CU, putting it on the chain. This cannot be done
933 for dummy CUs. */
934 void keep ();
935
936 private:
937 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
938 dwarf2_per_objfile *per_objfile,
939 int use_existing_cu);
940
941 struct dwarf2_per_cu_data *m_this_cu;
942 std::unique_ptr<dwarf2_cu> m_new_cu;
943
944 /* The ordinary abbreviation table. */
945 abbrev_table_up m_abbrev_table_holder;
946
947 /* The DWO abbreviation table. */
948 abbrev_table_up m_dwo_abbrev_table;
949 };
950
951 /* When we construct a partial symbol table entry we only
952 need this much information. */
953 struct partial_die_info : public allocate_on_obstack
954 {
955 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
956
957 /* Disable assign but still keep copy ctor, which is needed
958 load_partial_dies. */
959 partial_die_info& operator=(const partial_die_info& rhs) = delete;
960
961 /* Adjust the partial die before generating a symbol for it. This
962 function may set the is_external flag or change the DIE's
963 name. */
964 void fixup (struct dwarf2_cu *cu);
965
966 /* Read a minimal amount of information into the minimal die
967 structure. */
968 const gdb_byte *read (const struct die_reader_specs *reader,
969 const struct abbrev_info &abbrev,
970 const gdb_byte *info_ptr);
971
972 /* Offset of this DIE. */
973 const sect_offset sect_off;
974
975 /* DWARF-2 tag for this DIE. */
976 const ENUM_BITFIELD(dwarf_tag) tag : 16;
977
978 /* Assorted flags describing the data found in this DIE. */
979 const unsigned int has_children : 1;
980
981 unsigned int is_external : 1;
982 unsigned int is_declaration : 1;
983 unsigned int has_type : 1;
984 unsigned int has_specification : 1;
985 unsigned int has_pc_info : 1;
986 unsigned int may_be_inlined : 1;
987
988 /* This DIE has been marked DW_AT_main_subprogram. */
989 unsigned int main_subprogram : 1;
990
991 /* Flag set if the SCOPE field of this structure has been
992 computed. */
993 unsigned int scope_set : 1;
994
995 /* Flag set if the DIE has a byte_size attribute. */
996 unsigned int has_byte_size : 1;
997
998 /* Flag set if the DIE has a DW_AT_const_value attribute. */
999 unsigned int has_const_value : 1;
1000
1001 /* Flag set if any of the DIE's children are template arguments. */
1002 unsigned int has_template_arguments : 1;
1003
1004 /* Flag set if fixup has been called on this die. */
1005 unsigned int fixup_called : 1;
1006
1007 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1008 unsigned int is_dwz : 1;
1009
1010 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1011 unsigned int spec_is_dwz : 1;
1012
1013 /* The name of this DIE. Normally the value of DW_AT_name, but
1014 sometimes a default name for unnamed DIEs. */
1015 const char *name = nullptr;
1016
1017 /* The linkage name, if present. */
1018 const char *linkage_name = nullptr;
1019
1020 /* The scope to prepend to our children. This is generally
1021 allocated on the comp_unit_obstack, so will disappear
1022 when this compilation unit leaves the cache. */
1023 const char *scope = nullptr;
1024
1025 /* Some data associated with the partial DIE. The tag determines
1026 which field is live. */
1027 union
1028 {
1029 /* The location description associated with this DIE, if any. */
1030 struct dwarf_block *locdesc;
1031 /* The offset of an import, for DW_TAG_imported_unit. */
1032 sect_offset sect_off;
1033 } d {};
1034
1035 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1036 CORE_ADDR lowpc = 0;
1037 CORE_ADDR highpc = 0;
1038
1039 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1040 DW_AT_sibling, if any. */
1041 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1042 could return DW_AT_sibling values to its caller load_partial_dies. */
1043 const gdb_byte *sibling = nullptr;
1044
1045 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1046 DW_AT_specification (or DW_AT_abstract_origin or
1047 DW_AT_extension). */
1048 sect_offset spec_offset {};
1049
1050 /* Pointers to this DIE's parent, first child, and next sibling,
1051 if any. */
1052 struct partial_die_info *die_parent = nullptr;
1053 struct partial_die_info *die_child = nullptr;
1054 struct partial_die_info *die_sibling = nullptr;
1055
1056 friend struct partial_die_info *
1057 dwarf2_cu::find_partial_die (sect_offset sect_off);
1058
1059 private:
1060 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1061 partial_die_info (sect_offset sect_off)
1062 : partial_die_info (sect_off, DW_TAG_padding, 0)
1063 {
1064 }
1065
1066 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1067 int has_children_)
1068 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1069 {
1070 is_external = 0;
1071 is_declaration = 0;
1072 has_type = 0;
1073 has_specification = 0;
1074 has_pc_info = 0;
1075 may_be_inlined = 0;
1076 main_subprogram = 0;
1077 scope_set = 0;
1078 has_byte_size = 0;
1079 has_const_value = 0;
1080 has_template_arguments = 0;
1081 fixup_called = 0;
1082 is_dwz = 0;
1083 spec_is_dwz = 0;
1084 }
1085 };
1086
1087 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090 static int bits_per_byte = 8;
1091
1092 struct variant_part_builder;
1093
1094 /* When reading a variant, we track a bit more information about the
1095 field, and store it in an object of this type. */
1096
1097 struct variant_field
1098 {
1099 int first_field = -1;
1100 int last_field = -1;
1101
1102 /* A variant can contain other variant parts. */
1103 std::vector<variant_part_builder> variant_parts;
1104
1105 /* If we see a DW_TAG_variant, then this will be set if this is the
1106 default branch. */
1107 bool default_branch = false;
1108 /* If we see a DW_AT_discr_value, then this will be the discriminant
1109 value. */
1110 ULONGEST discriminant_value = 0;
1111 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1112 data. */
1113 struct dwarf_block *discr_list_data = nullptr;
1114 };
1115
1116 /* This represents a DW_TAG_variant_part. */
1117
1118 struct variant_part_builder
1119 {
1120 /* The offset of the discriminant field. */
1121 sect_offset discriminant_offset {};
1122
1123 /* Variants that are direct children of this variant part. */
1124 std::vector<variant_field> variants;
1125
1126 /* True if we're currently reading a variant. */
1127 bool processing_variant = false;
1128 };
1129
1130 struct nextfield
1131 {
1132 int accessibility = 0;
1133 int virtuality = 0;
1134 /* Variant parts need to find the discriminant, which is a DIE
1135 reference. We track the section offset of each field to make
1136 this link. */
1137 sect_offset offset;
1138 struct field field {};
1139 };
1140
1141 struct fnfieldlist
1142 {
1143 const char *name = nullptr;
1144 std::vector<struct fn_field> fnfields;
1145 };
1146
1147 /* The routines that read and process dies for a C struct or C++ class
1148 pass lists of data member fields and lists of member function fields
1149 in an instance of a field_info structure, as defined below. */
1150 struct field_info
1151 {
1152 /* List of data member and baseclasses fields. */
1153 std::vector<struct nextfield> fields;
1154 std::vector<struct nextfield> baseclasses;
1155
1156 /* Set if the accessibility of one of the fields is not public. */
1157 int non_public_fields = 0;
1158
1159 /* Member function fieldlist array, contains name of possibly overloaded
1160 member function, number of overloaded member functions and a pointer
1161 to the head of the member function field chain. */
1162 std::vector<struct fnfieldlist> fnfieldlists;
1163
1164 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1165 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1166 std::vector<struct decl_field> typedef_field_list;
1167
1168 /* Nested types defined by this class and the number of elements in this
1169 list. */
1170 std::vector<struct decl_field> nested_types_list;
1171
1172 /* If non-null, this is the variant part we are currently
1173 reading. */
1174 variant_part_builder *current_variant_part = nullptr;
1175 /* This holds all the top-level variant parts attached to the type
1176 we're reading. */
1177 std::vector<variant_part_builder> variant_parts;
1178
1179 /* Return the total number of fields (including baseclasses). */
1180 int nfields () const
1181 {
1182 return fields.size () + baseclasses.size ();
1183 }
1184 };
1185
1186 /* Loaded secondary compilation units are kept in memory until they
1187 have not been referenced for the processing of this many
1188 compilation units. Set this to zero to disable caching. Cache
1189 sizes of up to at least twenty will improve startup time for
1190 typical inter-CU-reference binaries, at an obvious memory cost. */
1191 static int dwarf_max_cache_age = 5;
1192 static void
1193 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1194 struct cmd_list_element *c, const char *value)
1195 {
1196 fprintf_filtered (file, _("The upper bound on the age of cached "
1197 "DWARF compilation units is %s.\n"),
1198 value);
1199 }
1200 \f
1201 /* local function prototypes */
1202
1203 static void dwarf2_find_base_address (struct die_info *die,
1204 struct dwarf2_cu *cu);
1205
1206 static dwarf2_psymtab *create_partial_symtab
1207 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1208 const char *name);
1209
1210 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1211 const gdb_byte *info_ptr,
1212 struct die_info *type_unit_die);
1213
1214 static void dwarf2_build_psymtabs_hard
1215 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1216
1217 static void scan_partial_symbols (struct partial_die_info *,
1218 CORE_ADDR *, CORE_ADDR *,
1219 int, struct dwarf2_cu *);
1220
1221 static void add_partial_symbol (struct partial_die_info *,
1222 struct dwarf2_cu *);
1223
1224 static void add_partial_namespace (struct partial_die_info *pdi,
1225 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1226 int set_addrmap, struct dwarf2_cu *cu);
1227
1228 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1229 CORE_ADDR *highpc, int set_addrmap,
1230 struct dwarf2_cu *cu);
1231
1232 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1233 struct dwarf2_cu *cu);
1234
1235 static void add_partial_subprogram (struct partial_die_info *pdi,
1236 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1237 int need_pc, struct dwarf2_cu *cu);
1238
1239 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1240
1241 static struct partial_die_info *load_partial_dies
1242 (const struct die_reader_specs *, const gdb_byte *, int);
1243
1244 /* A pair of partial_die_info and compilation unit. */
1245 struct cu_partial_die_info
1246 {
1247 /* The compilation unit of the partial_die_info. */
1248 struct dwarf2_cu *cu;
1249 /* A partial_die_info. */
1250 struct partial_die_info *pdi;
1251
1252 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1253 : cu (cu),
1254 pdi (pdi)
1255 { /* Nothing. */ }
1256
1257 private:
1258 cu_partial_die_info () = delete;
1259 };
1260
1261 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1262 struct dwarf2_cu *);
1263
1264 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1265 struct attribute *, struct attr_abbrev *,
1266 const gdb_byte *, bool *need_reprocess);
1267
1268 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1269 struct attribute *attr);
1270
1271 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1272
1273 static sect_offset read_abbrev_offset
1274 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1275 struct dwarf2_section_info *, sect_offset);
1276
1277 static const char *read_indirect_string
1278 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1279 const struct comp_unit_head *, unsigned int *);
1280
1281 static const char *read_indirect_string_at_offset
1282 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1283
1284 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1285 const gdb_byte *,
1286 unsigned int *);
1287
1288 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1289 ULONGEST str_index);
1290
1291 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1292 ULONGEST str_index);
1293
1294 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1295
1296 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1297 struct dwarf2_cu *);
1298
1299 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1300 struct dwarf2_cu *cu);
1301
1302 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1303
1304 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1305 struct dwarf2_cu *cu);
1306
1307 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1308
1309 static struct die_info *die_specification (struct die_info *die,
1310 struct dwarf2_cu **);
1311
1312 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1313 struct dwarf2_cu *cu);
1314
1315 static void dwarf_decode_lines (struct line_header *, const char *,
1316 struct dwarf2_cu *, dwarf2_psymtab *,
1317 CORE_ADDR, int decode_mapping);
1318
1319 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1320 const char *);
1321
1322 static struct symbol *new_symbol (struct die_info *, struct type *,
1323 struct dwarf2_cu *, struct symbol * = NULL);
1324
1325 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1326 struct dwarf2_cu *);
1327
1328 static void dwarf2_const_value_attr (const struct attribute *attr,
1329 struct type *type,
1330 const char *name,
1331 struct obstack *obstack,
1332 struct dwarf2_cu *cu, LONGEST *value,
1333 const gdb_byte **bytes,
1334 struct dwarf2_locexpr_baton **baton);
1335
1336 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1337
1338 static int need_gnat_info (struct dwarf2_cu *);
1339
1340 static struct type *die_descriptive_type (struct die_info *,
1341 struct dwarf2_cu *);
1342
1343 static void set_descriptive_type (struct type *, struct die_info *,
1344 struct dwarf2_cu *);
1345
1346 static struct type *die_containing_type (struct die_info *,
1347 struct dwarf2_cu *);
1348
1349 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1350 struct dwarf2_cu *);
1351
1352 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1353
1354 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1355
1356 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1357
1358 static char *typename_concat (struct obstack *obs, const char *prefix,
1359 const char *suffix, int physname,
1360 struct dwarf2_cu *cu);
1361
1362 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1363
1364 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1365
1366 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1367
1368 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1369
1370 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1371
1372 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1373
1374 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1375 struct dwarf2_cu *, dwarf2_psymtab *);
1376
1377 /* Return the .debug_loclists section to use for cu. */
1378 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1379
1380 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1381 values. Keep the items ordered with increasing constraints compliance. */
1382 enum pc_bounds_kind
1383 {
1384 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1385 PC_BOUNDS_NOT_PRESENT,
1386
1387 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1388 were present but they do not form a valid range of PC addresses. */
1389 PC_BOUNDS_INVALID,
1390
1391 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1392 PC_BOUNDS_RANGES,
1393
1394 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1395 PC_BOUNDS_HIGH_LOW,
1396 };
1397
1398 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1399 CORE_ADDR *, CORE_ADDR *,
1400 struct dwarf2_cu *,
1401 dwarf2_psymtab *);
1402
1403 static void get_scope_pc_bounds (struct die_info *,
1404 CORE_ADDR *, CORE_ADDR *,
1405 struct dwarf2_cu *);
1406
1407 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1408 CORE_ADDR, struct dwarf2_cu *);
1409
1410 static void dwarf2_add_field (struct field_info *, struct die_info *,
1411 struct dwarf2_cu *);
1412
1413 static void dwarf2_attach_fields_to_type (struct field_info *,
1414 struct type *, struct dwarf2_cu *);
1415
1416 static void dwarf2_add_member_fn (struct field_info *,
1417 struct die_info *, struct type *,
1418 struct dwarf2_cu *);
1419
1420 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1421 struct type *,
1422 struct dwarf2_cu *);
1423
1424 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1425
1426 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1427
1428 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1429
1430 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1431
1432 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1433
1434 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1435
1436 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1437
1438 static struct type *read_module_type (struct die_info *die,
1439 struct dwarf2_cu *cu);
1440
1441 static const char *namespace_name (struct die_info *die,
1442 int *is_anonymous, struct dwarf2_cu *);
1443
1444 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1445
1446 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1447 bool * = nullptr);
1448
1449 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1450 struct dwarf2_cu *);
1451
1452 static struct die_info *read_die_and_siblings_1
1453 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1454 struct die_info *);
1455
1456 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1457 const gdb_byte *info_ptr,
1458 const gdb_byte **new_info_ptr,
1459 struct die_info *parent);
1460
1461 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1462 struct die_info **, const gdb_byte *,
1463 int);
1464
1465 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1466 struct die_info **, const gdb_byte *);
1467
1468 static void process_die (struct die_info *, struct dwarf2_cu *);
1469
1470 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1471 struct objfile *);
1472
1473 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1474
1475 static const char *dwarf2_full_name (const char *name,
1476 struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
1479 static const char *dwarf2_physname (const char *name, struct die_info *die,
1480 struct dwarf2_cu *cu);
1481
1482 static struct die_info *dwarf2_extension (struct die_info *die,
1483 struct dwarf2_cu **);
1484
1485 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1486
1487 static void dump_die_for_error (struct die_info *);
1488
1489 static void dump_die_1 (struct ui_file *, int level, int max_level,
1490 struct die_info *);
1491
1492 /*static*/ void dump_die (struct die_info *, int max_level);
1493
1494 static void store_in_ref_table (struct die_info *,
1495 struct dwarf2_cu *);
1496
1497 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1498 const struct attribute *,
1499 struct dwarf2_cu **);
1500
1501 static struct die_info *follow_die_ref (struct die_info *,
1502 const struct attribute *,
1503 struct dwarf2_cu **);
1504
1505 static struct die_info *follow_die_sig (struct die_info *,
1506 const struct attribute *,
1507 struct dwarf2_cu **);
1508
1509 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1510 struct dwarf2_cu *);
1511
1512 static struct type *get_DW_AT_signature_type (struct die_info *,
1513 const struct attribute *,
1514 struct dwarf2_cu *);
1515
1516 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1517 dwarf2_per_objfile *per_objfile);
1518
1519 static void read_signatured_type (signatured_type *sig_type,
1520 dwarf2_per_objfile *per_objfile);
1521
1522 static int attr_to_dynamic_prop (const struct attribute *attr,
1523 struct die_info *die, struct dwarf2_cu *cu,
1524 struct dynamic_prop *prop, struct type *type);
1525
1526 /* memory allocation interface */
1527
1528 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1529
1530 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1531
1532 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1533
1534 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1535 struct dwarf2_loclist_baton *baton,
1536 const struct attribute *attr);
1537
1538 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1539 struct symbol *sym,
1540 struct dwarf2_cu *cu,
1541 int is_block);
1542
1543 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1544 const gdb_byte *info_ptr,
1545 struct abbrev_info *abbrev);
1546
1547 static hashval_t partial_die_hash (const void *item);
1548
1549 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1550
1551 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1552 (sect_offset sect_off, unsigned int offset_in_dwz,
1553 struct dwarf2_per_objfile *dwarf2_per_objfile);
1554
1555 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1556 struct die_info *comp_unit_die,
1557 enum language pretend_language);
1558
1559 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1560
1561 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1562
1563 static struct type *set_die_type (struct die_info *, struct type *,
1564 struct dwarf2_cu *);
1565
1566 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1567
1568 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1569
1570 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1571 dwarf2_per_objfile *per_objfile,
1572 bool skip_partial,
1573 enum language pretend_language);
1574
1575 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1576 enum language);
1577
1578 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1579 enum language);
1580
1581 static void dwarf2_add_dependence (struct dwarf2_cu *,
1582 struct dwarf2_per_cu_data *);
1583
1584 static void dwarf2_mark (struct dwarf2_cu *);
1585
1586 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1587
1588 static struct type *get_die_type_at_offset (sect_offset,
1589 struct dwarf2_per_cu_data *);
1590
1591 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1592
1593 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1594 enum language pretend_language);
1595
1596 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1597
1598 /* Class, the destructor of which frees all allocated queue entries. This
1599 will only have work to do if an error was thrown while processing the
1600 dwarf. If no error was thrown then the queue entries should have all
1601 been processed, and freed, as we went along. */
1602
1603 class dwarf2_queue_guard
1604 {
1605 public:
1606 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1607 : m_per_objfile (per_objfile)
1608 {
1609 }
1610
1611 /* Free any entries remaining on the queue. There should only be
1612 entries left if we hit an error while processing the dwarf. */
1613 ~dwarf2_queue_guard ()
1614 {
1615 /* Ensure that no memory is allocated by the queue. */
1616 std::queue<dwarf2_queue_item> empty;
1617 std::swap (m_per_objfile->per_bfd->queue, empty);
1618 }
1619
1620 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1621
1622 private:
1623 dwarf2_per_objfile *m_per_objfile;
1624 };
1625
1626 dwarf2_queue_item::~dwarf2_queue_item ()
1627 {
1628 /* Anything still marked queued is likely to be in an
1629 inconsistent state, so discard it. */
1630 if (per_cu->queued)
1631 {
1632 if (per_cu->cu != NULL)
1633 free_one_cached_comp_unit (per_cu);
1634 per_cu->queued = 0;
1635 }
1636 }
1637
1638 /* The return type of find_file_and_directory. Note, the enclosed
1639 string pointers are only valid while this object is valid. */
1640
1641 struct file_and_directory
1642 {
1643 /* The filename. This is never NULL. */
1644 const char *name;
1645
1646 /* The compilation directory. NULL if not known. If we needed to
1647 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1648 points directly to the DW_AT_comp_dir string attribute owned by
1649 the obstack that owns the DIE. */
1650 const char *comp_dir;
1651
1652 /* If we needed to build a new string for comp_dir, this is what
1653 owns the storage. */
1654 std::string comp_dir_storage;
1655 };
1656
1657 static file_and_directory find_file_and_directory (struct die_info *die,
1658 struct dwarf2_cu *cu);
1659
1660 static htab_up allocate_signatured_type_table ();
1661
1662 static htab_up allocate_dwo_unit_table ();
1663
1664 static struct dwo_unit *lookup_dwo_unit_in_dwp
1665 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1666 struct dwp_file *dwp_file, const char *comp_dir,
1667 ULONGEST signature, int is_debug_types);
1668
1669 static struct dwp_file *get_dwp_file
1670 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1671
1672 static struct dwo_unit *lookup_dwo_comp_unit
1673 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1674
1675 static struct dwo_unit *lookup_dwo_type_unit
1676 (struct signatured_type *, const char *, const char *);
1677
1678 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1679
1680 /* A unique pointer to a dwo_file. */
1681
1682 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1683
1684 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1685
1686 static void check_producer (struct dwarf2_cu *cu);
1687
1688 static void free_line_header_voidp (void *arg);
1689 \f
1690 /* Various complaints about symbol reading that don't abort the process. */
1691
1692 static void
1693 dwarf2_debug_line_missing_file_complaint (void)
1694 {
1695 complaint (_(".debug_line section has line data without a file"));
1696 }
1697
1698 static void
1699 dwarf2_debug_line_missing_end_sequence_complaint (void)
1700 {
1701 complaint (_(".debug_line section has line "
1702 "program sequence without an end"));
1703 }
1704
1705 static void
1706 dwarf2_complex_location_expr_complaint (void)
1707 {
1708 complaint (_("location expression too complex"));
1709 }
1710
1711 static void
1712 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1713 int arg3)
1714 {
1715 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1716 arg1, arg2, arg3);
1717 }
1718
1719 static void
1720 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1721 {
1722 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1723 arg1, arg2);
1724 }
1725
1726 /* Hash function for line_header_hash. */
1727
1728 static hashval_t
1729 line_header_hash (const struct line_header *ofs)
1730 {
1731 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1732 }
1733
1734 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1735
1736 static hashval_t
1737 line_header_hash_voidp (const void *item)
1738 {
1739 const struct line_header *ofs = (const struct line_header *) item;
1740
1741 return line_header_hash (ofs);
1742 }
1743
1744 /* Equality function for line_header_hash. */
1745
1746 static int
1747 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1748 {
1749 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1750 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1751
1752 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1753 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1754 }
1755
1756 \f
1757
1758 /* See declaration. */
1759
1760 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1761 bool can_copy_)
1762 : obfd (obfd),
1763 can_copy (can_copy_)
1764 {
1765 if (names == NULL)
1766 names = &dwarf2_elf_names;
1767
1768 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1769 locate_sections (obfd, sec, *names);
1770 }
1771
1772 dwarf2_per_bfd::~dwarf2_per_bfd ()
1773 {
1774 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1775 free_cached_comp_units ();
1776
1777 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1778 per_cu->imported_symtabs_free ();
1779
1780 for (signatured_type *sig_type : all_type_units)
1781 sig_type->per_cu.imported_symtabs_free ();
1782
1783 /* Everything else should be on this->obstack. */
1784 }
1785
1786 /* See declaration. */
1787
1788 void
1789 dwarf2_per_bfd::free_cached_comp_units ()
1790 {
1791 dwarf2_per_cu_data *per_cu = read_in_chain;
1792 dwarf2_per_cu_data **last_chain = &read_in_chain;
1793 while (per_cu != NULL)
1794 {
1795 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1796
1797 delete per_cu->cu;
1798 *last_chain = next_cu;
1799 per_cu = next_cu;
1800 }
1801 }
1802
1803 /* A helper class that calls free_cached_comp_units on
1804 destruction. */
1805
1806 class free_cached_comp_units
1807 {
1808 public:
1809
1810 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1811 : m_per_objfile (per_objfile)
1812 {
1813 }
1814
1815 ~free_cached_comp_units ()
1816 {
1817 m_per_objfile->per_bfd->free_cached_comp_units ();
1818 }
1819
1820 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1821
1822 private:
1823
1824 dwarf2_per_objfile *m_per_objfile;
1825 };
1826
1827 /* See read.h. */
1828
1829 bool
1830 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1831 {
1832 gdb_assert (per_cu->index < this->m_symtabs.size ());
1833
1834 return this->m_symtabs[per_cu->index] != nullptr;
1835 }
1836
1837 /* See read.h. */
1838
1839 compunit_symtab *
1840 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1841 {
1842 gdb_assert (per_cu->index < this->m_symtabs.size ());
1843
1844 return this->m_symtabs[per_cu->index];
1845 }
1846
1847 /* See read.h. */
1848
1849 void
1850 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1851 compunit_symtab *symtab)
1852 {
1853 gdb_assert (per_cu->index < this->m_symtabs.size ());
1854 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1855
1856 this->m_symtabs[per_cu->index] = symtab;
1857 }
1858
1859 /* Try to locate the sections we need for DWARF 2 debugging
1860 information and return true if we have enough to do something.
1861 NAMES points to the dwarf2 section names, or is NULL if the standard
1862 ELF names are used. CAN_COPY is true for formats where symbol
1863 interposition is possible and so symbol values must follow copy
1864 relocation rules. */
1865
1866 int
1867 dwarf2_has_info (struct objfile *objfile,
1868 const struct dwarf2_debug_sections *names,
1869 bool can_copy)
1870 {
1871 if (objfile->flags & OBJF_READNEVER)
1872 return 0;
1873
1874 struct dwarf2_per_objfile *dwarf2_per_objfile
1875 = get_dwarf2_per_objfile (objfile);
1876
1877 if (dwarf2_per_objfile == NULL)
1878 {
1879 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1880 sharing yet). */
1881 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1882
1883 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1884 }
1885
1886 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1887 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1888 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1889 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1890 }
1891
1892 /* When loading sections, we look either for uncompressed section or for
1893 compressed section names. */
1894
1895 static int
1896 section_is_p (const char *section_name,
1897 const struct dwarf2_section_names *names)
1898 {
1899 if (names->normal != NULL
1900 && strcmp (section_name, names->normal) == 0)
1901 return 1;
1902 if (names->compressed != NULL
1903 && strcmp (section_name, names->compressed) == 0)
1904 return 1;
1905 return 0;
1906 }
1907
1908 /* See declaration. */
1909
1910 void
1911 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1912 const dwarf2_debug_sections &names)
1913 {
1914 flagword aflag = bfd_section_flags (sectp);
1915
1916 if ((aflag & SEC_HAS_CONTENTS) == 0)
1917 {
1918 }
1919 else if (elf_section_data (sectp)->this_hdr.sh_size
1920 > bfd_get_file_size (abfd))
1921 {
1922 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1923 warning (_("Discarding section %s which has a section size (%s"
1924 ") larger than the file size [in module %s]"),
1925 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1926 bfd_get_filename (abfd));
1927 }
1928 else if (section_is_p (sectp->name, &names.info))
1929 {
1930 this->info.s.section = sectp;
1931 this->info.size = bfd_section_size (sectp);
1932 }
1933 else if (section_is_p (sectp->name, &names.abbrev))
1934 {
1935 this->abbrev.s.section = sectp;
1936 this->abbrev.size = bfd_section_size (sectp);
1937 }
1938 else if (section_is_p (sectp->name, &names.line))
1939 {
1940 this->line.s.section = sectp;
1941 this->line.size = bfd_section_size (sectp);
1942 }
1943 else if (section_is_p (sectp->name, &names.loc))
1944 {
1945 this->loc.s.section = sectp;
1946 this->loc.size = bfd_section_size (sectp);
1947 }
1948 else if (section_is_p (sectp->name, &names.loclists))
1949 {
1950 this->loclists.s.section = sectp;
1951 this->loclists.size = bfd_section_size (sectp);
1952 }
1953 else if (section_is_p (sectp->name, &names.macinfo))
1954 {
1955 this->macinfo.s.section = sectp;
1956 this->macinfo.size = bfd_section_size (sectp);
1957 }
1958 else if (section_is_p (sectp->name, &names.macro))
1959 {
1960 this->macro.s.section = sectp;
1961 this->macro.size = bfd_section_size (sectp);
1962 }
1963 else if (section_is_p (sectp->name, &names.str))
1964 {
1965 this->str.s.section = sectp;
1966 this->str.size = bfd_section_size (sectp);
1967 }
1968 else if (section_is_p (sectp->name, &names.str_offsets))
1969 {
1970 this->str_offsets.s.section = sectp;
1971 this->str_offsets.size = bfd_section_size (sectp);
1972 }
1973 else if (section_is_p (sectp->name, &names.line_str))
1974 {
1975 this->line_str.s.section = sectp;
1976 this->line_str.size = bfd_section_size (sectp);
1977 }
1978 else if (section_is_p (sectp->name, &names.addr))
1979 {
1980 this->addr.s.section = sectp;
1981 this->addr.size = bfd_section_size (sectp);
1982 }
1983 else if (section_is_p (sectp->name, &names.frame))
1984 {
1985 this->frame.s.section = sectp;
1986 this->frame.size = bfd_section_size (sectp);
1987 }
1988 else if (section_is_p (sectp->name, &names.eh_frame))
1989 {
1990 this->eh_frame.s.section = sectp;
1991 this->eh_frame.size = bfd_section_size (sectp);
1992 }
1993 else if (section_is_p (sectp->name, &names.ranges))
1994 {
1995 this->ranges.s.section = sectp;
1996 this->ranges.size = bfd_section_size (sectp);
1997 }
1998 else if (section_is_p (sectp->name, &names.rnglists))
1999 {
2000 this->rnglists.s.section = sectp;
2001 this->rnglists.size = bfd_section_size (sectp);
2002 }
2003 else if (section_is_p (sectp->name, &names.types))
2004 {
2005 struct dwarf2_section_info type_section;
2006
2007 memset (&type_section, 0, sizeof (type_section));
2008 type_section.s.section = sectp;
2009 type_section.size = bfd_section_size (sectp);
2010
2011 this->types.push_back (type_section);
2012 }
2013 else if (section_is_p (sectp->name, &names.gdb_index))
2014 {
2015 this->gdb_index.s.section = sectp;
2016 this->gdb_index.size = bfd_section_size (sectp);
2017 }
2018 else if (section_is_p (sectp->name, &names.debug_names))
2019 {
2020 this->debug_names.s.section = sectp;
2021 this->debug_names.size = bfd_section_size (sectp);
2022 }
2023 else if (section_is_p (sectp->name, &names.debug_aranges))
2024 {
2025 this->debug_aranges.s.section = sectp;
2026 this->debug_aranges.size = bfd_section_size (sectp);
2027 }
2028
2029 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2030 && bfd_section_vma (sectp) == 0)
2031 this->has_section_at_zero = true;
2032 }
2033
2034 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2035 SECTION_NAME. */
2036
2037 void
2038 dwarf2_get_section_info (struct objfile *objfile,
2039 enum dwarf2_section_enum sect,
2040 asection **sectp, const gdb_byte **bufp,
2041 bfd_size_type *sizep)
2042 {
2043 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2044 struct dwarf2_section_info *info;
2045
2046 /* We may see an objfile without any DWARF, in which case we just
2047 return nothing. */
2048 if (data == NULL)
2049 {
2050 *sectp = NULL;
2051 *bufp = NULL;
2052 *sizep = 0;
2053 return;
2054 }
2055 switch (sect)
2056 {
2057 case DWARF2_DEBUG_FRAME:
2058 info = &data->per_bfd->frame;
2059 break;
2060 case DWARF2_EH_FRAME:
2061 info = &data->per_bfd->eh_frame;
2062 break;
2063 default:
2064 gdb_assert_not_reached ("unexpected section");
2065 }
2066
2067 info->read (objfile);
2068
2069 *sectp = info->get_bfd_section ();
2070 *bufp = info->buffer;
2071 *sizep = info->size;
2072 }
2073
2074 /* A helper function to find the sections for a .dwz file. */
2075
2076 static void
2077 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2078 {
2079 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2080
2081 /* Note that we only support the standard ELF names, because .dwz
2082 is ELF-only (at the time of writing). */
2083 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2084 {
2085 dwz_file->abbrev.s.section = sectp;
2086 dwz_file->abbrev.size = bfd_section_size (sectp);
2087 }
2088 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2089 {
2090 dwz_file->info.s.section = sectp;
2091 dwz_file->info.size = bfd_section_size (sectp);
2092 }
2093 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2094 {
2095 dwz_file->str.s.section = sectp;
2096 dwz_file->str.size = bfd_section_size (sectp);
2097 }
2098 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2099 {
2100 dwz_file->line.s.section = sectp;
2101 dwz_file->line.size = bfd_section_size (sectp);
2102 }
2103 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2104 {
2105 dwz_file->macro.s.section = sectp;
2106 dwz_file->macro.size = bfd_section_size (sectp);
2107 }
2108 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2109 {
2110 dwz_file->gdb_index.s.section = sectp;
2111 dwz_file->gdb_index.size = bfd_section_size (sectp);
2112 }
2113 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2114 {
2115 dwz_file->debug_names.s.section = sectp;
2116 dwz_file->debug_names.size = bfd_section_size (sectp);
2117 }
2118 }
2119
2120 /* See dwarf2read.h. */
2121
2122 struct dwz_file *
2123 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2124 {
2125 const char *filename;
2126 bfd_size_type buildid_len_arg;
2127 size_t buildid_len;
2128 bfd_byte *buildid;
2129
2130 if (per_bfd->dwz_file != NULL)
2131 return per_bfd->dwz_file.get ();
2132
2133 bfd_set_error (bfd_error_no_error);
2134 gdb::unique_xmalloc_ptr<char> data
2135 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2136 &buildid_len_arg, &buildid));
2137 if (data == NULL)
2138 {
2139 if (bfd_get_error () == bfd_error_no_error)
2140 return NULL;
2141 error (_("could not read '.gnu_debugaltlink' section: %s"),
2142 bfd_errmsg (bfd_get_error ()));
2143 }
2144
2145 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2146
2147 buildid_len = (size_t) buildid_len_arg;
2148
2149 filename = data.get ();
2150
2151 std::string abs_storage;
2152 if (!IS_ABSOLUTE_PATH (filename))
2153 {
2154 gdb::unique_xmalloc_ptr<char> abs
2155 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2156
2157 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2158 filename = abs_storage.c_str ();
2159 }
2160
2161 /* First try the file name given in the section. If that doesn't
2162 work, try to use the build-id instead. */
2163 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2164 if (dwz_bfd != NULL)
2165 {
2166 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2167 dwz_bfd.reset (nullptr);
2168 }
2169
2170 if (dwz_bfd == NULL)
2171 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2172
2173 if (dwz_bfd == nullptr)
2174 {
2175 gdb::unique_xmalloc_ptr<char> alt_filename;
2176 const char *origname = bfd_get_filename (per_bfd->obfd);
2177
2178 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2179 buildid_len,
2180 origname,
2181 &alt_filename));
2182
2183 if (fd.get () >= 0)
2184 {
2185 /* File successfully retrieved from server. */
2186 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2187
2188 if (dwz_bfd == nullptr)
2189 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2190 alt_filename.get ());
2191 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2192 dwz_bfd.reset (nullptr);
2193 }
2194 }
2195
2196 if (dwz_bfd == NULL)
2197 error (_("could not find '.gnu_debugaltlink' file for %s"),
2198 bfd_get_filename (per_bfd->obfd));
2199
2200 std::unique_ptr<struct dwz_file> result
2201 (new struct dwz_file (std::move (dwz_bfd)));
2202
2203 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2204 result.get ());
2205
2206 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2207 per_bfd->dwz_file = std::move (result);
2208 return per_bfd->dwz_file.get ();
2209 }
2210 \f
2211 /* DWARF quick_symbols_functions support. */
2212
2213 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2214 unique line tables, so we maintain a separate table of all .debug_line
2215 derived entries to support the sharing.
2216 All the quick functions need is the list of file names. We discard the
2217 line_header when we're done and don't need to record it here. */
2218 struct quick_file_names
2219 {
2220 /* The data used to construct the hash key. */
2221 struct stmt_list_hash hash;
2222
2223 /* The number of entries in file_names, real_names. */
2224 unsigned int num_file_names;
2225
2226 /* The file names from the line table, after being run through
2227 file_full_name. */
2228 const char **file_names;
2229
2230 /* The file names from the line table after being run through
2231 gdb_realpath. These are computed lazily. */
2232 const char **real_names;
2233 };
2234
2235 /* When using the index (and thus not using psymtabs), each CU has an
2236 object of this type. This is used to hold information needed by
2237 the various "quick" methods. */
2238 struct dwarf2_per_cu_quick_data
2239 {
2240 /* The file table. This can be NULL if there was no file table
2241 or it's currently not read in.
2242 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2243 struct quick_file_names *file_names;
2244
2245 /* A temporary mark bit used when iterating over all CUs in
2246 expand_symtabs_matching. */
2247 unsigned int mark : 1;
2248
2249 /* True if we've tried to read the file table and found there isn't one.
2250 There will be no point in trying to read it again next time. */
2251 unsigned int no_file_data : 1;
2252 };
2253
2254 /* Utility hash function for a stmt_list_hash. */
2255
2256 static hashval_t
2257 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2258 {
2259 hashval_t v = 0;
2260
2261 if (stmt_list_hash->dwo_unit != NULL)
2262 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2263 v += to_underlying (stmt_list_hash->line_sect_off);
2264 return v;
2265 }
2266
2267 /* Utility equality function for a stmt_list_hash. */
2268
2269 static int
2270 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2271 const struct stmt_list_hash *rhs)
2272 {
2273 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2274 return 0;
2275 if (lhs->dwo_unit != NULL
2276 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2277 return 0;
2278
2279 return lhs->line_sect_off == rhs->line_sect_off;
2280 }
2281
2282 /* Hash function for a quick_file_names. */
2283
2284 static hashval_t
2285 hash_file_name_entry (const void *e)
2286 {
2287 const struct quick_file_names *file_data
2288 = (const struct quick_file_names *) e;
2289
2290 return hash_stmt_list_entry (&file_data->hash);
2291 }
2292
2293 /* Equality function for a quick_file_names. */
2294
2295 static int
2296 eq_file_name_entry (const void *a, const void *b)
2297 {
2298 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2299 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2300
2301 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2302 }
2303
2304 /* Delete function for a quick_file_names. */
2305
2306 static void
2307 delete_file_name_entry (void *e)
2308 {
2309 struct quick_file_names *file_data = (struct quick_file_names *) e;
2310 int i;
2311
2312 for (i = 0; i < file_data->num_file_names; ++i)
2313 {
2314 xfree ((void*) file_data->file_names[i]);
2315 if (file_data->real_names)
2316 xfree ((void*) file_data->real_names[i]);
2317 }
2318
2319 /* The space for the struct itself lives on the obstack, so we don't
2320 free it here. */
2321 }
2322
2323 /* Create a quick_file_names hash table. */
2324
2325 static htab_up
2326 create_quick_file_names_table (unsigned int nr_initial_entries)
2327 {
2328 return htab_up (htab_create_alloc (nr_initial_entries,
2329 hash_file_name_entry, eq_file_name_entry,
2330 delete_file_name_entry, xcalloc, xfree));
2331 }
2332
2333 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2334 function is unrelated to symtabs, symtab would have to be created afterwards.
2335 You should call age_cached_comp_units after processing the CU. */
2336
2337 static void
2338 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2339 bool skip_partial)
2340 {
2341 if (per_cu->is_debug_types)
2342 load_full_type_unit (per_cu, per_objfile);
2343 else
2344 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2345
2346 if (per_cu->cu == NULL)
2347 return; /* Dummy CU. */
2348
2349 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2350 }
2351
2352 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2353
2354 static void
2355 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2356 dwarf2_per_objfile *dwarf2_per_objfile,
2357 bool skip_partial)
2358 {
2359 /* Skip type_unit_groups, reading the type units they contain
2360 is handled elsewhere. */
2361 if (per_cu->type_unit_group_p ())
2362 return;
2363
2364 /* The destructor of dwarf2_queue_guard frees any entries left on
2365 the queue. After this point we're guaranteed to leave this function
2366 with the dwarf queue empty. */
2367 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2368
2369 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2370 {
2371 queue_comp_unit (per_cu, language_minimal);
2372 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2373
2374 /* If we just loaded a CU from a DWO, and we're working with an index
2375 that may badly handle TUs, load all the TUs in that DWO as well.
2376 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2377 if (!per_cu->is_debug_types
2378 && per_cu->cu != NULL
2379 && per_cu->cu->dwo_unit != NULL
2380 && dwarf2_per_objfile->per_bfd->index_table != NULL
2381 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2382 /* DWP files aren't supported yet. */
2383 && get_dwp_file (dwarf2_per_objfile) == NULL)
2384 queue_and_load_all_dwo_tus (per_cu);
2385 }
2386
2387 process_queue (dwarf2_per_objfile);
2388
2389 /* Age the cache, releasing compilation units that have not
2390 been used recently. */
2391 age_cached_comp_units (dwarf2_per_objfile);
2392 }
2393
2394 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2395 the per-objfile for which this symtab is instantiated.
2396
2397 Returns the resulting symbol table. */
2398
2399 static struct compunit_symtab *
2400 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2401 dwarf2_per_objfile *dwarf2_per_objfile,
2402 bool skip_partial)
2403 {
2404 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2405
2406 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2407 {
2408 free_cached_comp_units freer (dwarf2_per_objfile);
2409 scoped_restore decrementer = increment_reading_symtab ();
2410 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2411 process_cu_includes (dwarf2_per_objfile);
2412 }
2413
2414 return dwarf2_per_objfile->get_symtab (per_cu);
2415 }
2416
2417 /* See declaration. */
2418
2419 dwarf2_per_cu_data *
2420 dwarf2_per_bfd::get_cutu (int index)
2421 {
2422 if (index >= this->all_comp_units.size ())
2423 {
2424 index -= this->all_comp_units.size ();
2425 gdb_assert (index < this->all_type_units.size ());
2426 return &this->all_type_units[index]->per_cu;
2427 }
2428
2429 return this->all_comp_units[index];
2430 }
2431
2432 /* See declaration. */
2433
2434 dwarf2_per_cu_data *
2435 dwarf2_per_bfd::get_cu (int index)
2436 {
2437 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2438
2439 return this->all_comp_units[index];
2440 }
2441
2442 /* See declaration. */
2443
2444 signatured_type *
2445 dwarf2_per_bfd::get_tu (int index)
2446 {
2447 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2448
2449 return this->all_type_units[index];
2450 }
2451
2452 /* See read.h. */
2453
2454 dwarf2_per_cu_data *
2455 dwarf2_per_bfd::allocate_per_cu ()
2456 {
2457 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2458 result->per_bfd = this;
2459 result->index = m_num_psymtabs++;
2460 return result;
2461 }
2462
2463 /* See read.h. */
2464
2465 signatured_type *
2466 dwarf2_per_bfd::allocate_signatured_type ()
2467 {
2468 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2469 result->per_cu.per_bfd = this;
2470 result->per_cu.index = m_num_psymtabs++;
2471 return result;
2472 }
2473
2474 /* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2475 obstack, and constructed with the specified field values. */
2476
2477 static dwarf2_per_cu_data *
2478 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2479 struct dwarf2_section_info *section,
2480 int is_dwz,
2481 sect_offset sect_off, ULONGEST length)
2482 {
2483 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2484 the_cu->sect_off = sect_off;
2485 the_cu->length = length;
2486 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2487 the_cu->section = section;
2488 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2489 struct dwarf2_per_cu_quick_data);
2490 the_cu->is_dwz = is_dwz;
2491 return the_cu;
2492 }
2493
2494 /* A helper for create_cus_from_index that handles a given list of
2495 CUs. */
2496
2497 static void
2498 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2499 const gdb_byte *cu_list, offset_type n_elements,
2500 struct dwarf2_section_info *section,
2501 int is_dwz)
2502 {
2503 for (offset_type i = 0; i < n_elements; i += 2)
2504 {
2505 gdb_static_assert (sizeof (ULONGEST) >= 8);
2506
2507 sect_offset sect_off
2508 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2509 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2510 cu_list += 2 * 8;
2511
2512 dwarf2_per_cu_data *per_cu
2513 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2514 sect_off, length);
2515 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2516 }
2517 }
2518
2519 /* Read the CU list from the mapped index, and use it to create all
2520 the CU objects for this objfile. */
2521
2522 static void
2523 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2524 const gdb_byte *cu_list, offset_type cu_list_elements,
2525 const gdb_byte *dwz_list, offset_type dwz_elements)
2526 {
2527 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2528 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2529 ((cu_list_elements + dwz_elements) / 2);
2530
2531 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2532 &dwarf2_per_objfile->per_bfd->info, 0);
2533
2534 if (dwz_elements == 0)
2535 return;
2536
2537 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
2538 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2539 &dwz->info, 1);
2540 }
2541
2542 /* Create the signatured type hash table from the index. */
2543
2544 static void
2545 create_signatured_type_table_from_index
2546 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2547 struct dwarf2_section_info *section,
2548 const gdb_byte *bytes,
2549 offset_type elements)
2550 {
2551 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2552 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2553
2554 htab_up sig_types_hash = allocate_signatured_type_table ();
2555
2556 for (offset_type i = 0; i < elements; i += 3)
2557 {
2558 struct signatured_type *sig_type;
2559 ULONGEST signature;
2560 void **slot;
2561 cu_offset type_offset_in_tu;
2562
2563 gdb_static_assert (sizeof (ULONGEST) >= 8);
2564 sect_offset sect_off
2565 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2566 type_offset_in_tu
2567 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2568 BFD_ENDIAN_LITTLE);
2569 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2570 bytes += 3 * 8;
2571
2572 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2573 sig_type->signature = signature;
2574 sig_type->type_offset_in_tu = type_offset_in_tu;
2575 sig_type->per_cu.is_debug_types = 1;
2576 sig_type->per_cu.section = section;
2577 sig_type->per_cu.sect_off = sect_off;
2578 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2579 sig_type->per_cu.v.quick
2580 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2581 struct dwarf2_per_cu_quick_data);
2582
2583 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2584 *slot = sig_type;
2585
2586 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2587 }
2588
2589 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2590 }
2591
2592 /* Create the signatured type hash table from .debug_names. */
2593
2594 static void
2595 create_signatured_type_table_from_debug_names
2596 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2597 const mapped_debug_names &map,
2598 struct dwarf2_section_info *section,
2599 struct dwarf2_section_info *abbrev_section)
2600 {
2601 struct objfile *objfile = dwarf2_per_objfile->objfile;
2602
2603 section->read (objfile);
2604 abbrev_section->read (objfile);
2605
2606 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2607 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2608
2609 htab_up sig_types_hash = allocate_signatured_type_table ();
2610
2611 for (uint32_t i = 0; i < map.tu_count; ++i)
2612 {
2613 struct signatured_type *sig_type;
2614 void **slot;
2615
2616 sect_offset sect_off
2617 = (sect_offset) (extract_unsigned_integer
2618 (map.tu_table_reordered + i * map.offset_size,
2619 map.offset_size,
2620 map.dwarf5_byte_order));
2621
2622 comp_unit_head cu_header;
2623 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2624 abbrev_section,
2625 section->buffer + to_underlying (sect_off),
2626 rcuh_kind::TYPE);
2627
2628 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2629 sig_type->signature = cu_header.signature;
2630 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2631 sig_type->per_cu.is_debug_types = 1;
2632 sig_type->per_cu.section = section;
2633 sig_type->per_cu.sect_off = sect_off;
2634 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2635 sig_type->per_cu.v.quick
2636 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2637 struct dwarf2_per_cu_quick_data);
2638
2639 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2640 *slot = sig_type;
2641
2642 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2643 }
2644
2645 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2646 }
2647
2648 /* Read the address map data from the mapped index, and use it to
2649 populate the objfile's psymtabs_addrmap. */
2650
2651 static void
2652 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2653 struct mapped_index *index)
2654 {
2655 struct objfile *objfile = dwarf2_per_objfile->objfile;
2656 struct gdbarch *gdbarch = objfile->arch ();
2657 const gdb_byte *iter, *end;
2658 struct addrmap *mutable_map;
2659 CORE_ADDR baseaddr;
2660
2661 auto_obstack temp_obstack;
2662
2663 mutable_map = addrmap_create_mutable (&temp_obstack);
2664
2665 iter = index->address_table.data ();
2666 end = iter + index->address_table.size ();
2667
2668 baseaddr = objfile->text_section_offset ();
2669
2670 while (iter < end)
2671 {
2672 ULONGEST hi, lo, cu_index;
2673 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2674 iter += 8;
2675 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2676 iter += 8;
2677 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2678 iter += 4;
2679
2680 if (lo > hi)
2681 {
2682 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2683 hex_string (lo), hex_string (hi));
2684 continue;
2685 }
2686
2687 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2688 {
2689 complaint (_(".gdb_index address table has invalid CU number %u"),
2690 (unsigned) cu_index);
2691 continue;
2692 }
2693
2694 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2695 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2696 addrmap_set_empty (mutable_map, lo, hi - 1,
2697 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2698 }
2699
2700 objfile->partial_symtabs->psymtabs_addrmap
2701 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2702 }
2703
2704 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2705 populate the objfile's psymtabs_addrmap. */
2706
2707 static void
2708 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2709 struct dwarf2_section_info *section)
2710 {
2711 struct objfile *objfile = dwarf2_per_objfile->objfile;
2712 bfd *abfd = objfile->obfd;
2713 struct gdbarch *gdbarch = objfile->arch ();
2714 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2715
2716 auto_obstack temp_obstack;
2717 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2718
2719 std::unordered_map<sect_offset,
2720 dwarf2_per_cu_data *,
2721 gdb::hash_enum<sect_offset>>
2722 debug_info_offset_to_per_cu;
2723 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2724 {
2725 const auto insertpair
2726 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2727 if (!insertpair.second)
2728 {
2729 warning (_("Section .debug_aranges in %s has duplicate "
2730 "debug_info_offset %s, ignoring .debug_aranges."),
2731 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2732 return;
2733 }
2734 }
2735
2736 section->read (objfile);
2737
2738 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2739
2740 const gdb_byte *addr = section->buffer;
2741
2742 while (addr < section->buffer + section->size)
2743 {
2744 const gdb_byte *const entry_addr = addr;
2745 unsigned int bytes_read;
2746
2747 const LONGEST entry_length = read_initial_length (abfd, addr,
2748 &bytes_read);
2749 addr += bytes_read;
2750
2751 const gdb_byte *const entry_end = addr + entry_length;
2752 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2753 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2754 if (addr + entry_length > section->buffer + section->size)
2755 {
2756 warning (_("Section .debug_aranges in %s entry at offset %s "
2757 "length %s exceeds section length %s, "
2758 "ignoring .debug_aranges."),
2759 objfile_name (objfile),
2760 plongest (entry_addr - section->buffer),
2761 plongest (bytes_read + entry_length),
2762 pulongest (section->size));
2763 return;
2764 }
2765
2766 /* The version number. */
2767 const uint16_t version = read_2_bytes (abfd, addr);
2768 addr += 2;
2769 if (version != 2)
2770 {
2771 warning (_("Section .debug_aranges in %s entry at offset %s "
2772 "has unsupported version %d, ignoring .debug_aranges."),
2773 objfile_name (objfile),
2774 plongest (entry_addr - section->buffer), version);
2775 return;
2776 }
2777
2778 const uint64_t debug_info_offset
2779 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2780 addr += offset_size;
2781 const auto per_cu_it
2782 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2783 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2784 {
2785 warning (_("Section .debug_aranges in %s entry at offset %s "
2786 "debug_info_offset %s does not exists, "
2787 "ignoring .debug_aranges."),
2788 objfile_name (objfile),
2789 plongest (entry_addr - section->buffer),
2790 pulongest (debug_info_offset));
2791 return;
2792 }
2793 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2794
2795 const uint8_t address_size = *addr++;
2796 if (address_size < 1 || address_size > 8)
2797 {
2798 warning (_("Section .debug_aranges in %s entry at offset %s "
2799 "address_size %u is invalid, ignoring .debug_aranges."),
2800 objfile_name (objfile),
2801 plongest (entry_addr - section->buffer), address_size);
2802 return;
2803 }
2804
2805 const uint8_t segment_selector_size = *addr++;
2806 if (segment_selector_size != 0)
2807 {
2808 warning (_("Section .debug_aranges in %s entry at offset %s "
2809 "segment_selector_size %u is not supported, "
2810 "ignoring .debug_aranges."),
2811 objfile_name (objfile),
2812 plongest (entry_addr - section->buffer),
2813 segment_selector_size);
2814 return;
2815 }
2816
2817 /* Must pad to an alignment boundary that is twice the address
2818 size. It is undocumented by the DWARF standard but GCC does
2819 use it. */
2820 for (size_t padding = ((-(addr - section->buffer))
2821 & (2 * address_size - 1));
2822 padding > 0; padding--)
2823 if (*addr++ != 0)
2824 {
2825 warning (_("Section .debug_aranges in %s entry at offset %s "
2826 "padding is not zero, ignoring .debug_aranges."),
2827 objfile_name (objfile),
2828 plongest (entry_addr - section->buffer));
2829 return;
2830 }
2831
2832 for (;;)
2833 {
2834 if (addr + 2 * address_size > entry_end)
2835 {
2836 warning (_("Section .debug_aranges in %s entry at offset %s "
2837 "address list is not properly terminated, "
2838 "ignoring .debug_aranges."),
2839 objfile_name (objfile),
2840 plongest (entry_addr - section->buffer));
2841 return;
2842 }
2843 ULONGEST start = extract_unsigned_integer (addr, address_size,
2844 dwarf5_byte_order);
2845 addr += address_size;
2846 ULONGEST length = extract_unsigned_integer (addr, address_size,
2847 dwarf5_byte_order);
2848 addr += address_size;
2849 if (start == 0 && length == 0)
2850 break;
2851 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2852 {
2853 /* Symbol was eliminated due to a COMDAT group. */
2854 continue;
2855 }
2856 ULONGEST end = start + length;
2857 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2858 - baseaddr);
2859 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2860 - baseaddr);
2861 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2862 }
2863 }
2864
2865 objfile->partial_symtabs->psymtabs_addrmap
2866 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2867 }
2868
2869 /* Find a slot in the mapped index INDEX for the object named NAME.
2870 If NAME is found, set *VEC_OUT to point to the CU vector in the
2871 constant pool and return true. If NAME cannot be found, return
2872 false. */
2873
2874 static bool
2875 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2876 offset_type **vec_out)
2877 {
2878 offset_type hash;
2879 offset_type slot, step;
2880 int (*cmp) (const char *, const char *);
2881
2882 gdb::unique_xmalloc_ptr<char> without_params;
2883 if (current_language->la_language == language_cplus
2884 || current_language->la_language == language_fortran
2885 || current_language->la_language == language_d)
2886 {
2887 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2888 not contain any. */
2889
2890 if (strchr (name, '(') != NULL)
2891 {
2892 without_params = cp_remove_params (name);
2893
2894 if (without_params != NULL)
2895 name = without_params.get ();
2896 }
2897 }
2898
2899 /* Index version 4 did not support case insensitive searches. But the
2900 indices for case insensitive languages are built in lowercase, therefore
2901 simulate our NAME being searched is also lowercased. */
2902 hash = mapped_index_string_hash ((index->version == 4
2903 && case_sensitivity == case_sensitive_off
2904 ? 5 : index->version),
2905 name);
2906
2907 slot = hash & (index->symbol_table.size () - 1);
2908 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2909 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2910
2911 for (;;)
2912 {
2913 const char *str;
2914
2915 const auto &bucket = index->symbol_table[slot];
2916 if (bucket.name == 0 && bucket.vec == 0)
2917 return false;
2918
2919 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2920 if (!cmp (name, str))
2921 {
2922 *vec_out = (offset_type *) (index->constant_pool
2923 + MAYBE_SWAP (bucket.vec));
2924 return true;
2925 }
2926
2927 slot = (slot + step) & (index->symbol_table.size () - 1);
2928 }
2929 }
2930
2931 /* A helper function that reads the .gdb_index from BUFFER and fills
2932 in MAP. FILENAME is the name of the file containing the data;
2933 it is used for error reporting. DEPRECATED_OK is true if it is
2934 ok to use deprecated sections.
2935
2936 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2937 out parameters that are filled in with information about the CU and
2938 TU lists in the section.
2939
2940 Returns true if all went well, false otherwise. */
2941
2942 static bool
2943 read_gdb_index_from_buffer (const char *filename,
2944 bool deprecated_ok,
2945 gdb::array_view<const gdb_byte> buffer,
2946 struct mapped_index *map,
2947 const gdb_byte **cu_list,
2948 offset_type *cu_list_elements,
2949 const gdb_byte **types_list,
2950 offset_type *types_list_elements)
2951 {
2952 const gdb_byte *addr = &buffer[0];
2953
2954 /* Version check. */
2955 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2956 /* Versions earlier than 3 emitted every copy of a psymbol. This
2957 causes the index to behave very poorly for certain requests. Version 3
2958 contained incomplete addrmap. So, it seems better to just ignore such
2959 indices. */
2960 if (version < 4)
2961 {
2962 static int warning_printed = 0;
2963 if (!warning_printed)
2964 {
2965 warning (_("Skipping obsolete .gdb_index section in %s."),
2966 filename);
2967 warning_printed = 1;
2968 }
2969 return 0;
2970 }
2971 /* Index version 4 uses a different hash function than index version
2972 5 and later.
2973
2974 Versions earlier than 6 did not emit psymbols for inlined
2975 functions. Using these files will cause GDB not to be able to
2976 set breakpoints on inlined functions by name, so we ignore these
2977 indices unless the user has done
2978 "set use-deprecated-index-sections on". */
2979 if (version < 6 && !deprecated_ok)
2980 {
2981 static int warning_printed = 0;
2982 if (!warning_printed)
2983 {
2984 warning (_("\
2985 Skipping deprecated .gdb_index section in %s.\n\
2986 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2987 to use the section anyway."),
2988 filename);
2989 warning_printed = 1;
2990 }
2991 return 0;
2992 }
2993 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2994 of the TU (for symbols coming from TUs),
2995 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2996 Plus gold-generated indices can have duplicate entries for global symbols,
2997 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2998 These are just performance bugs, and we can't distinguish gdb-generated
2999 indices from gold-generated ones, so issue no warning here. */
3000
3001 /* Indexes with higher version than the one supported by GDB may be no
3002 longer backward compatible. */
3003 if (version > 8)
3004 return 0;
3005
3006 map->version = version;
3007
3008 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3009
3010 int i = 0;
3011 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3012 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3013 / 8);
3014 ++i;
3015
3016 *types_list = addr + MAYBE_SWAP (metadata[i]);
3017 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3018 - MAYBE_SWAP (metadata[i]))
3019 / 8);
3020 ++i;
3021
3022 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3023 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3024 map->address_table
3025 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3026 ++i;
3027
3028 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3029 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3030 map->symbol_table
3031 = gdb::array_view<mapped_index::symbol_table_slot>
3032 ((mapped_index::symbol_table_slot *) symbol_table,
3033 (mapped_index::symbol_table_slot *) symbol_table_end);
3034
3035 ++i;
3036 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3037
3038 return 1;
3039 }
3040
3041 /* Callback types for dwarf2_read_gdb_index. */
3042
3043 typedef gdb::function_view
3044 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3045 get_gdb_index_contents_ftype;
3046 typedef gdb::function_view
3047 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3048 get_gdb_index_contents_dwz_ftype;
3049
3050 /* Read .gdb_index. If everything went ok, initialize the "quick"
3051 elements of all the CUs and return 1. Otherwise, return 0. */
3052
3053 static int
3054 dwarf2_read_gdb_index
3055 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3056 get_gdb_index_contents_ftype get_gdb_index_contents,
3057 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3058 {
3059 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3060 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3061 struct dwz_file *dwz;
3062 struct objfile *objfile = dwarf2_per_objfile->objfile;
3063
3064 gdb::array_view<const gdb_byte> main_index_contents
3065 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3066
3067 if (main_index_contents.empty ())
3068 return 0;
3069
3070 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3071 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3072 use_deprecated_index_sections,
3073 main_index_contents, map.get (), &cu_list,
3074 &cu_list_elements, &types_list,
3075 &types_list_elements))
3076 return 0;
3077
3078 /* Don't use the index if it's empty. */
3079 if (map->symbol_table.empty ())
3080 return 0;
3081
3082 /* If there is a .dwz file, read it so we can get its CU list as
3083 well. */
3084 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3085 if (dwz != NULL)
3086 {
3087 struct mapped_index dwz_map;
3088 const gdb_byte *dwz_types_ignore;
3089 offset_type dwz_types_elements_ignore;
3090
3091 gdb::array_view<const gdb_byte> dwz_index_content
3092 = get_gdb_index_contents_dwz (objfile, dwz);
3093
3094 if (dwz_index_content.empty ())
3095 return 0;
3096
3097 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3098 1, dwz_index_content, &dwz_map,
3099 &dwz_list, &dwz_list_elements,
3100 &dwz_types_ignore,
3101 &dwz_types_elements_ignore))
3102 {
3103 warning (_("could not read '.gdb_index' section from %s; skipping"),
3104 bfd_get_filename (dwz->dwz_bfd.get ()));
3105 return 0;
3106 }
3107 }
3108
3109 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3110 dwz_list, dwz_list_elements);
3111
3112 if (types_list_elements)
3113 {
3114 /* We can only handle a single .debug_types when we have an
3115 index. */
3116 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3117 return 0;
3118
3119 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3120
3121 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3122 types_list, types_list_elements);
3123 }
3124
3125 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3126
3127 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3128 dwarf2_per_objfile->per_bfd->using_index = 1;
3129 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3130 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3131
3132 return 1;
3133 }
3134
3135 /* die_reader_func for dw2_get_file_names. */
3136
3137 static void
3138 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3139 const gdb_byte *info_ptr,
3140 struct die_info *comp_unit_die)
3141 {
3142 struct dwarf2_cu *cu = reader->cu;
3143 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3144 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3145 struct dwarf2_per_cu_data *lh_cu;
3146 struct attribute *attr;
3147 void **slot;
3148 struct quick_file_names *qfn;
3149
3150 gdb_assert (! this_cu->is_debug_types);
3151
3152 /* Our callers never want to match partial units -- instead they
3153 will match the enclosing full CU. */
3154 if (comp_unit_die->tag == DW_TAG_partial_unit)
3155 {
3156 this_cu->v.quick->no_file_data = 1;
3157 return;
3158 }
3159
3160 lh_cu = this_cu;
3161 slot = NULL;
3162
3163 line_header_up lh;
3164 sect_offset line_offset {};
3165
3166 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3167 if (attr != nullptr)
3168 {
3169 struct quick_file_names find_entry;
3170
3171 line_offset = (sect_offset) DW_UNSND (attr);
3172
3173 /* We may have already read in this line header (TU line header sharing).
3174 If we have we're done. */
3175 find_entry.hash.dwo_unit = cu->dwo_unit;
3176 find_entry.hash.line_sect_off = line_offset;
3177 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3178 &find_entry, INSERT);
3179 if (*slot != NULL)
3180 {
3181 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3182 return;
3183 }
3184
3185 lh = dwarf_decode_line_header (line_offset, cu);
3186 }
3187 if (lh == NULL)
3188 {
3189 lh_cu->v.quick->no_file_data = 1;
3190 return;
3191 }
3192
3193 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3194 qfn->hash.dwo_unit = cu->dwo_unit;
3195 qfn->hash.line_sect_off = line_offset;
3196 gdb_assert (slot != NULL);
3197 *slot = qfn;
3198
3199 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3200
3201 int offset = 0;
3202 if (strcmp (fnd.name, "<unknown>") != 0)
3203 ++offset;
3204
3205 qfn->num_file_names = offset + lh->file_names_size ();
3206 qfn->file_names =
3207 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3208 qfn->num_file_names);
3209 if (offset != 0)
3210 qfn->file_names[0] = xstrdup (fnd.name);
3211 for (int i = 0; i < lh->file_names_size (); ++i)
3212 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3213 fnd.comp_dir).release ();
3214 qfn->real_names = NULL;
3215
3216 lh_cu->v.quick->file_names = qfn;
3217 }
3218
3219 /* A helper for the "quick" functions which attempts to read the line
3220 table for THIS_CU. */
3221
3222 static struct quick_file_names *
3223 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3224 dwarf2_per_objfile *per_objfile)
3225 {
3226 /* This should never be called for TUs. */
3227 gdb_assert (! this_cu->is_debug_types);
3228 /* Nor type unit groups. */
3229 gdb_assert (! this_cu->type_unit_group_p ());
3230
3231 if (this_cu->v.quick->file_names != NULL)
3232 return this_cu->v.quick->file_names;
3233 /* If we know there is no line data, no point in looking again. */
3234 if (this_cu->v.quick->no_file_data)
3235 return NULL;
3236
3237 cutu_reader reader (this_cu, per_objfile);
3238 if (!reader.dummy_p)
3239 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3240
3241 if (this_cu->v.quick->no_file_data)
3242 return NULL;
3243 return this_cu->v.quick->file_names;
3244 }
3245
3246 /* A helper for the "quick" functions which computes and caches the
3247 real path for a given file name from the line table. */
3248
3249 static const char *
3250 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3251 struct quick_file_names *qfn, int index)
3252 {
3253 if (qfn->real_names == NULL)
3254 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3255 qfn->num_file_names, const char *);
3256
3257 if (qfn->real_names[index] == NULL)
3258 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3259
3260 return qfn->real_names[index];
3261 }
3262
3263 static struct symtab *
3264 dw2_find_last_source_symtab (struct objfile *objfile)
3265 {
3266 struct dwarf2_per_objfile *dwarf2_per_objfile
3267 = get_dwarf2_per_objfile (objfile);
3268 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3269 compunit_symtab *cust
3270 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3271
3272 if (cust == NULL)
3273 return NULL;
3274
3275 return compunit_primary_filetab (cust);
3276 }
3277
3278 /* Traversal function for dw2_forget_cached_source_info. */
3279
3280 static int
3281 dw2_free_cached_file_names (void **slot, void *info)
3282 {
3283 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3284
3285 if (file_data->real_names)
3286 {
3287 int i;
3288
3289 for (i = 0; i < file_data->num_file_names; ++i)
3290 {
3291 xfree ((void*) file_data->real_names[i]);
3292 file_data->real_names[i] = NULL;
3293 }
3294 }
3295
3296 return 1;
3297 }
3298
3299 static void
3300 dw2_forget_cached_source_info (struct objfile *objfile)
3301 {
3302 struct dwarf2_per_objfile *dwarf2_per_objfile
3303 = get_dwarf2_per_objfile (objfile);
3304
3305 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3306 dw2_free_cached_file_names, NULL);
3307 }
3308
3309 /* Helper function for dw2_map_symtabs_matching_filename that expands
3310 the symtabs and calls the iterator. */
3311
3312 static int
3313 dw2_map_expand_apply (struct objfile *objfile,
3314 struct dwarf2_per_cu_data *per_cu,
3315 const char *name, const char *real_path,
3316 gdb::function_view<bool (symtab *)> callback)
3317 {
3318 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3319
3320 /* Don't visit already-expanded CUs. */
3321 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3322 if (per_objfile->symtab_set_p (per_cu))
3323 return 0;
3324
3325 /* This may expand more than one symtab, and we want to iterate over
3326 all of them. */
3327 dw2_instantiate_symtab (per_cu, per_objfile, false);
3328
3329 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3330 last_made, callback);
3331 }
3332
3333 /* Implementation of the map_symtabs_matching_filename method. */
3334
3335 static bool
3336 dw2_map_symtabs_matching_filename
3337 (struct objfile *objfile, const char *name, const char *real_path,
3338 gdb::function_view<bool (symtab *)> callback)
3339 {
3340 const char *name_basename = lbasename (name);
3341 struct dwarf2_per_objfile *dwarf2_per_objfile
3342 = get_dwarf2_per_objfile (objfile);
3343
3344 /* The rule is CUs specify all the files, including those used by
3345 any TU, so there's no need to scan TUs here. */
3346
3347 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3348 {
3349 /* We only need to look at symtabs not already expanded. */
3350 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3351 continue;
3352
3353 quick_file_names *file_data
3354 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3355 if (file_data == NULL)
3356 continue;
3357
3358 for (int j = 0; j < file_data->num_file_names; ++j)
3359 {
3360 const char *this_name = file_data->file_names[j];
3361 const char *this_real_name;
3362
3363 if (compare_filenames_for_search (this_name, name))
3364 {
3365 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3366 callback))
3367 return true;
3368 continue;
3369 }
3370
3371 /* Before we invoke realpath, which can get expensive when many
3372 files are involved, do a quick comparison of the basenames. */
3373 if (! basenames_may_differ
3374 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3375 continue;
3376
3377 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3378 file_data, j);
3379 if (compare_filenames_for_search (this_real_name, name))
3380 {
3381 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3382 callback))
3383 return true;
3384 continue;
3385 }
3386
3387 if (real_path != NULL)
3388 {
3389 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3390 gdb_assert (IS_ABSOLUTE_PATH (name));
3391 if (this_real_name != NULL
3392 && FILENAME_CMP (real_path, this_real_name) == 0)
3393 {
3394 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3395 callback))
3396 return true;
3397 continue;
3398 }
3399 }
3400 }
3401 }
3402
3403 return false;
3404 }
3405
3406 /* Struct used to manage iterating over all CUs looking for a symbol. */
3407
3408 struct dw2_symtab_iterator
3409 {
3410 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3411 struct dwarf2_per_objfile *dwarf2_per_objfile;
3412 /* If set, only look for symbols that match that block. Valid values are
3413 GLOBAL_BLOCK and STATIC_BLOCK. */
3414 gdb::optional<block_enum> block_index;
3415 /* The kind of symbol we're looking for. */
3416 domain_enum domain;
3417 /* The list of CUs from the index entry of the symbol,
3418 or NULL if not found. */
3419 offset_type *vec;
3420 /* The next element in VEC to look at. */
3421 int next;
3422 /* The number of elements in VEC, or zero if there is no match. */
3423 int length;
3424 /* Have we seen a global version of the symbol?
3425 If so we can ignore all further global instances.
3426 This is to work around gold/15646, inefficient gold-generated
3427 indices. */
3428 int global_seen;
3429 };
3430
3431 /* Initialize the index symtab iterator ITER. */
3432
3433 static void
3434 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3435 struct dwarf2_per_objfile *dwarf2_per_objfile,
3436 gdb::optional<block_enum> block_index,
3437 domain_enum domain,
3438 const char *name)
3439 {
3440 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3441 iter->block_index = block_index;
3442 iter->domain = domain;
3443 iter->next = 0;
3444 iter->global_seen = 0;
3445
3446 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3447
3448 /* index is NULL if OBJF_READNOW. */
3449 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3450 iter->length = MAYBE_SWAP (*iter->vec);
3451 else
3452 {
3453 iter->vec = NULL;
3454 iter->length = 0;
3455 }
3456 }
3457
3458 /* Return the next matching CU or NULL if there are no more. */
3459
3460 static struct dwarf2_per_cu_data *
3461 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3462 {
3463 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3464
3465 for ( ; iter->next < iter->length; ++iter->next)
3466 {
3467 offset_type cu_index_and_attrs =
3468 MAYBE_SWAP (iter->vec[iter->next + 1]);
3469 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3470 gdb_index_symbol_kind symbol_kind =
3471 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3472 /* Only check the symbol attributes if they're present.
3473 Indices prior to version 7 don't record them,
3474 and indices >= 7 may elide them for certain symbols
3475 (gold does this). */
3476 int attrs_valid =
3477 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3478 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3479
3480 /* Don't crash on bad data. */
3481 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3482 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3483 {
3484 complaint (_(".gdb_index entry has bad CU index"
3485 " [in module %s]"),
3486 objfile_name (dwarf2_per_objfile->objfile));
3487 continue;
3488 }
3489
3490 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3491
3492 /* Skip if already read in. */
3493 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3494 continue;
3495
3496 /* Check static vs global. */
3497 if (attrs_valid)
3498 {
3499 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3500
3501 if (iter->block_index.has_value ())
3502 {
3503 bool want_static = *iter->block_index == STATIC_BLOCK;
3504
3505 if (is_static != want_static)
3506 continue;
3507 }
3508
3509 /* Work around gold/15646. */
3510 if (!is_static && iter->global_seen)
3511 continue;
3512 if (!is_static)
3513 iter->global_seen = 1;
3514 }
3515
3516 /* Only check the symbol's kind if it has one. */
3517 if (attrs_valid)
3518 {
3519 switch (iter->domain)
3520 {
3521 case VAR_DOMAIN:
3522 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3523 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3524 /* Some types are also in VAR_DOMAIN. */
3525 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3526 continue;
3527 break;
3528 case STRUCT_DOMAIN:
3529 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3530 continue;
3531 break;
3532 case LABEL_DOMAIN:
3533 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3534 continue;
3535 break;
3536 case MODULE_DOMAIN:
3537 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3538 continue;
3539 break;
3540 default:
3541 break;
3542 }
3543 }
3544
3545 ++iter->next;
3546 return per_cu;
3547 }
3548
3549 return NULL;
3550 }
3551
3552 static struct compunit_symtab *
3553 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3554 const char *name, domain_enum domain)
3555 {
3556 struct compunit_symtab *stab_best = NULL;
3557 struct dwarf2_per_objfile *dwarf2_per_objfile
3558 = get_dwarf2_per_objfile (objfile);
3559
3560 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3561
3562 struct dw2_symtab_iterator iter;
3563 struct dwarf2_per_cu_data *per_cu;
3564
3565 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3566
3567 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3568 {
3569 struct symbol *sym, *with_opaque = NULL;
3570 struct compunit_symtab *stab
3571 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3572 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3573 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3574
3575 sym = block_find_symbol (block, name, domain,
3576 block_find_non_opaque_type_preferred,
3577 &with_opaque);
3578
3579 /* Some caution must be observed with overloaded functions
3580 and methods, since the index will not contain any overload
3581 information (but NAME might contain it). */
3582
3583 if (sym != NULL
3584 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3585 return stab;
3586 if (with_opaque != NULL
3587 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3588 stab_best = stab;
3589
3590 /* Keep looking through other CUs. */
3591 }
3592
3593 return stab_best;
3594 }
3595
3596 static void
3597 dw2_print_stats (struct objfile *objfile)
3598 {
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
3601 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3602 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3603 int count = 0;
3604
3605 for (int i = 0; i < total; ++i)
3606 {
3607 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3608
3609 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3610 ++count;
3611 }
3612 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3613 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3614 }
3615
3616 /* This dumps minimal information about the index.
3617 It is called via "mt print objfiles".
3618 One use is to verify .gdb_index has been loaded by the
3619 gdb.dwarf2/gdb-index.exp testcase. */
3620
3621 static void
3622 dw2_dump (struct objfile *objfile)
3623 {
3624 struct dwarf2_per_objfile *dwarf2_per_objfile
3625 = get_dwarf2_per_objfile (objfile);
3626
3627 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3628 printf_filtered (".gdb_index:");
3629 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3630 {
3631 printf_filtered (" version %d\n",
3632 dwarf2_per_objfile->per_bfd->index_table->version);
3633 }
3634 else
3635 printf_filtered (" faked for \"readnow\"\n");
3636 printf_filtered ("\n");
3637 }
3638
3639 static void
3640 dw2_expand_symtabs_for_function (struct objfile *objfile,
3641 const char *func_name)
3642 {
3643 struct dwarf2_per_objfile *dwarf2_per_objfile
3644 = get_dwarf2_per_objfile (objfile);
3645
3646 struct dw2_symtab_iterator iter;
3647 struct dwarf2_per_cu_data *per_cu;
3648
3649 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3650
3651 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3652 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3653
3654 }
3655
3656 static void
3657 dw2_expand_all_symtabs (struct objfile *objfile)
3658 {
3659 struct dwarf2_per_objfile *dwarf2_per_objfile
3660 = get_dwarf2_per_objfile (objfile);
3661 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3662 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3663
3664 for (int i = 0; i < total_units; ++i)
3665 {
3666 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3667
3668 /* We don't want to directly expand a partial CU, because if we
3669 read it with the wrong language, then assertion failures can
3670 be triggered later on. See PR symtab/23010. So, tell
3671 dw2_instantiate_symtab to skip partial CUs -- any important
3672 partial CU will be read via DW_TAG_imported_unit anyway. */
3673 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3674 }
3675 }
3676
3677 static void
3678 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3679 const char *fullname)
3680 {
3681 struct dwarf2_per_objfile *dwarf2_per_objfile
3682 = get_dwarf2_per_objfile (objfile);
3683
3684 /* We don't need to consider type units here.
3685 This is only called for examining code, e.g. expand_line_sal.
3686 There can be an order of magnitude (or more) more type units
3687 than comp units, and we avoid them if we can. */
3688
3689 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3690 {
3691 /* We only need to look at symtabs not already expanded. */
3692 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3693 continue;
3694
3695 quick_file_names *file_data
3696 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3697 if (file_data == NULL)
3698 continue;
3699
3700 for (int j = 0; j < file_data->num_file_names; ++j)
3701 {
3702 const char *this_fullname = file_data->file_names[j];
3703
3704 if (filename_cmp (this_fullname, fullname) == 0)
3705 {
3706 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3707 break;
3708 }
3709 }
3710 }
3711 }
3712
3713 static void
3714 dw2_expand_symtabs_matching_symbol
3715 (mapped_index_base &index,
3716 const lookup_name_info &lookup_name_in,
3717 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3718 enum search_domain kind,
3719 gdb::function_view<bool (offset_type)> match_callback);
3720
3721 static void
3722 dw2_expand_symtabs_matching_one
3723 (dwarf2_per_cu_data *per_cu,
3724 dwarf2_per_objfile *per_objfile,
3725 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3726 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3727
3728 static void
3729 dw2_map_matching_symbols
3730 (struct objfile *objfile,
3731 const lookup_name_info &name, domain_enum domain,
3732 int global,
3733 gdb::function_view<symbol_found_callback_ftype> callback,
3734 symbol_compare_ftype *ordered_compare)
3735 {
3736 /* Used for Ada. */
3737 struct dwarf2_per_objfile *dwarf2_per_objfile
3738 = get_dwarf2_per_objfile (objfile);
3739
3740 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3741
3742 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3743 {
3744 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3745 here though if the current language is Ada for a non-Ada objfile
3746 using GNU index. */
3747 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3748
3749 const char *match_name = name.ada ().lookup_name ().c_str ();
3750 auto matcher = [&] (const char *symname)
3751 {
3752 if (ordered_compare == nullptr)
3753 return true;
3754 return ordered_compare (symname, match_name) == 0;
3755 };
3756
3757 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3758 [&] (offset_type namei)
3759 {
3760 struct dw2_symtab_iterator iter;
3761 struct dwarf2_per_cu_data *per_cu;
3762
3763 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3764 match_name);
3765 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3766 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3767 nullptr);
3768 return true;
3769 });
3770 }
3771 else
3772 {
3773 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3774 proceed assuming all symtabs have been read in. */
3775 }
3776
3777 for (compunit_symtab *cust : objfile->compunits ())
3778 {
3779 const struct block *block;
3780
3781 if (cust == NULL)
3782 continue;
3783 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3784 if (!iterate_over_symbols_terminated (block, name,
3785 domain, callback))
3786 return;
3787 }
3788 }
3789
3790 /* Starting from a search name, return the string that finds the upper
3791 bound of all strings that start with SEARCH_NAME in a sorted name
3792 list. Returns the empty string to indicate that the upper bound is
3793 the end of the list. */
3794
3795 static std::string
3796 make_sort_after_prefix_name (const char *search_name)
3797 {
3798 /* When looking to complete "func", we find the upper bound of all
3799 symbols that start with "func" by looking for where we'd insert
3800 the closest string that would follow "func" in lexicographical
3801 order. Usually, that's "func"-with-last-character-incremented,
3802 i.e. "fund". Mind non-ASCII characters, though. Usually those
3803 will be UTF-8 multi-byte sequences, but we can't be certain.
3804 Especially mind the 0xff character, which is a valid character in
3805 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3806 rule out compilers allowing it in identifiers. Note that
3807 conveniently, strcmp/strcasecmp are specified to compare
3808 characters interpreted as unsigned char. So what we do is treat
3809 the whole string as a base 256 number composed of a sequence of
3810 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3811 to 0, and carries 1 to the following more-significant position.
3812 If the very first character in SEARCH_NAME ends up incremented
3813 and carries/overflows, then the upper bound is the end of the
3814 list. The string after the empty string is also the empty
3815 string.
3816
3817 Some examples of this operation:
3818
3819 SEARCH_NAME => "+1" RESULT
3820
3821 "abc" => "abd"
3822 "ab\xff" => "ac"
3823 "\xff" "a" "\xff" => "\xff" "b"
3824 "\xff" => ""
3825 "\xff\xff" => ""
3826 "" => ""
3827
3828 Then, with these symbols for example:
3829
3830 func
3831 func1
3832 fund
3833
3834 completing "func" looks for symbols between "func" and
3835 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3836 which finds "func" and "func1", but not "fund".
3837
3838 And with:
3839
3840 funcÿ (Latin1 'ÿ' [0xff])
3841 funcÿ1
3842 fund
3843
3844 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3845 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3846
3847 And with:
3848
3849 ÿÿ (Latin1 'ÿ' [0xff])
3850 ÿÿ1
3851
3852 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3853 the end of the list.
3854 */
3855 std::string after = search_name;
3856 while (!after.empty () && (unsigned char) after.back () == 0xff)
3857 after.pop_back ();
3858 if (!after.empty ())
3859 after.back () = (unsigned char) after.back () + 1;
3860 return after;
3861 }
3862
3863 /* See declaration. */
3864
3865 std::pair<std::vector<name_component>::const_iterator,
3866 std::vector<name_component>::const_iterator>
3867 mapped_index_base::find_name_components_bounds
3868 (const lookup_name_info &lookup_name_without_params, language lang) const
3869 {
3870 auto *name_cmp
3871 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3872
3873 const char *lang_name
3874 = lookup_name_without_params.language_lookup_name (lang);
3875
3876 /* Comparison function object for lower_bound that matches against a
3877 given symbol name. */
3878 auto lookup_compare_lower = [&] (const name_component &elem,
3879 const char *name)
3880 {
3881 const char *elem_qualified = this->symbol_name_at (elem.idx);
3882 const char *elem_name = elem_qualified + elem.name_offset;
3883 return name_cmp (elem_name, name) < 0;
3884 };
3885
3886 /* Comparison function object for upper_bound that matches against a
3887 given symbol name. */
3888 auto lookup_compare_upper = [&] (const char *name,
3889 const name_component &elem)
3890 {
3891 const char *elem_qualified = this->symbol_name_at (elem.idx);
3892 const char *elem_name = elem_qualified + elem.name_offset;
3893 return name_cmp (name, elem_name) < 0;
3894 };
3895
3896 auto begin = this->name_components.begin ();
3897 auto end = this->name_components.end ();
3898
3899 /* Find the lower bound. */
3900 auto lower = [&] ()
3901 {
3902 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3903 return begin;
3904 else
3905 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3906 } ();
3907
3908 /* Find the upper bound. */
3909 auto upper = [&] ()
3910 {
3911 if (lookup_name_without_params.completion_mode ())
3912 {
3913 /* In completion mode, we want UPPER to point past all
3914 symbols names that have the same prefix. I.e., with
3915 these symbols, and completing "func":
3916
3917 function << lower bound
3918 function1
3919 other_function << upper bound
3920
3921 We find the upper bound by looking for the insertion
3922 point of "func"-with-last-character-incremented,
3923 i.e. "fund". */
3924 std::string after = make_sort_after_prefix_name (lang_name);
3925 if (after.empty ())
3926 return end;
3927 return std::lower_bound (lower, end, after.c_str (),
3928 lookup_compare_lower);
3929 }
3930 else
3931 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3932 } ();
3933
3934 return {lower, upper};
3935 }
3936
3937 /* See declaration. */
3938
3939 void
3940 mapped_index_base::build_name_components ()
3941 {
3942 if (!this->name_components.empty ())
3943 return;
3944
3945 this->name_components_casing = case_sensitivity;
3946 auto *name_cmp
3947 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3948
3949 /* The code below only knows how to break apart components of C++
3950 symbol names (and other languages that use '::' as
3951 namespace/module separator) and Ada symbol names. */
3952 auto count = this->symbol_name_count ();
3953 for (offset_type idx = 0; idx < count; idx++)
3954 {
3955 if (this->symbol_name_slot_invalid (idx))
3956 continue;
3957
3958 const char *name = this->symbol_name_at (idx);
3959
3960 /* Add each name component to the name component table. */
3961 unsigned int previous_len = 0;
3962
3963 if (strstr (name, "::") != nullptr)
3964 {
3965 for (unsigned int current_len = cp_find_first_component (name);
3966 name[current_len] != '\0';
3967 current_len += cp_find_first_component (name + current_len))
3968 {
3969 gdb_assert (name[current_len] == ':');
3970 this->name_components.push_back ({previous_len, idx});
3971 /* Skip the '::'. */
3972 current_len += 2;
3973 previous_len = current_len;
3974 }
3975 }
3976 else
3977 {
3978 /* Handle the Ada encoded (aka mangled) form here. */
3979 for (const char *iter = strstr (name, "__");
3980 iter != nullptr;
3981 iter = strstr (iter, "__"))
3982 {
3983 this->name_components.push_back ({previous_len, idx});
3984 iter += 2;
3985 previous_len = iter - name;
3986 }
3987 }
3988
3989 this->name_components.push_back ({previous_len, idx});
3990 }
3991
3992 /* Sort name_components elements by name. */
3993 auto name_comp_compare = [&] (const name_component &left,
3994 const name_component &right)
3995 {
3996 const char *left_qualified = this->symbol_name_at (left.idx);
3997 const char *right_qualified = this->symbol_name_at (right.idx);
3998
3999 const char *left_name = left_qualified + left.name_offset;
4000 const char *right_name = right_qualified + right.name_offset;
4001
4002 return name_cmp (left_name, right_name) < 0;
4003 };
4004
4005 std::sort (this->name_components.begin (),
4006 this->name_components.end (),
4007 name_comp_compare);
4008 }
4009
4010 /* Helper for dw2_expand_symtabs_matching that works with a
4011 mapped_index_base instead of the containing objfile. This is split
4012 to a separate function in order to be able to unit test the
4013 name_components matching using a mock mapped_index_base. For each
4014 symbol name that matches, calls MATCH_CALLBACK, passing it the
4015 symbol's index in the mapped_index_base symbol table. */
4016
4017 static void
4018 dw2_expand_symtabs_matching_symbol
4019 (mapped_index_base &index,
4020 const lookup_name_info &lookup_name_in,
4021 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4022 enum search_domain kind,
4023 gdb::function_view<bool (offset_type)> match_callback)
4024 {
4025 lookup_name_info lookup_name_without_params
4026 = lookup_name_in.make_ignore_params ();
4027
4028 /* Build the symbol name component sorted vector, if we haven't
4029 yet. */
4030 index.build_name_components ();
4031
4032 /* The same symbol may appear more than once in the range though.
4033 E.g., if we're looking for symbols that complete "w", and we have
4034 a symbol named "w1::w2", we'll find the two name components for
4035 that same symbol in the range. To be sure we only call the
4036 callback once per symbol, we first collect the symbol name
4037 indexes that matched in a temporary vector and ignore
4038 duplicates. */
4039 std::vector<offset_type> matches;
4040
4041 struct name_and_matcher
4042 {
4043 symbol_name_matcher_ftype *matcher;
4044 const char *name;
4045
4046 bool operator== (const name_and_matcher &other) const
4047 {
4048 return matcher == other.matcher && strcmp (name, other.name) == 0;
4049 }
4050 };
4051
4052 /* A vector holding all the different symbol name matchers, for all
4053 languages. */
4054 std::vector<name_and_matcher> matchers;
4055
4056 for (int i = 0; i < nr_languages; i++)
4057 {
4058 enum language lang_e = (enum language) i;
4059
4060 const language_defn *lang = language_def (lang_e);
4061 symbol_name_matcher_ftype *name_matcher
4062 = get_symbol_name_matcher (lang, lookup_name_without_params);
4063
4064 name_and_matcher key {
4065 name_matcher,
4066 lookup_name_without_params.language_lookup_name (lang_e)
4067 };
4068
4069 /* Don't insert the same comparison routine more than once.
4070 Note that we do this linear walk. This is not a problem in
4071 practice because the number of supported languages is
4072 low. */
4073 if (std::find (matchers.begin (), matchers.end (), key)
4074 != matchers.end ())
4075 continue;
4076 matchers.push_back (std::move (key));
4077
4078 auto bounds
4079 = index.find_name_components_bounds (lookup_name_without_params,
4080 lang_e);
4081
4082 /* Now for each symbol name in range, check to see if we have a name
4083 match, and if so, call the MATCH_CALLBACK callback. */
4084
4085 for (; bounds.first != bounds.second; ++bounds.first)
4086 {
4087 const char *qualified = index.symbol_name_at (bounds.first->idx);
4088
4089 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4090 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4091 continue;
4092
4093 matches.push_back (bounds.first->idx);
4094 }
4095 }
4096
4097 std::sort (matches.begin (), matches.end ());
4098
4099 /* Finally call the callback, once per match. */
4100 ULONGEST prev = -1;
4101 for (offset_type idx : matches)
4102 {
4103 if (prev != idx)
4104 {
4105 if (!match_callback (idx))
4106 break;
4107 prev = idx;
4108 }
4109 }
4110
4111 /* Above we use a type wider than idx's for 'prev', since 0 and
4112 (offset_type)-1 are both possible values. */
4113 static_assert (sizeof (prev) > sizeof (offset_type), "");
4114 }
4115
4116 #if GDB_SELF_TEST
4117
4118 namespace selftests { namespace dw2_expand_symtabs_matching {
4119
4120 /* A mock .gdb_index/.debug_names-like name index table, enough to
4121 exercise dw2_expand_symtabs_matching_symbol, which works with the
4122 mapped_index_base interface. Builds an index from the symbol list
4123 passed as parameter to the constructor. */
4124 class mock_mapped_index : public mapped_index_base
4125 {
4126 public:
4127 mock_mapped_index (gdb::array_view<const char *> symbols)
4128 : m_symbol_table (symbols)
4129 {}
4130
4131 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4132
4133 /* Return the number of names in the symbol table. */
4134 size_t symbol_name_count () const override
4135 {
4136 return m_symbol_table.size ();
4137 }
4138
4139 /* Get the name of the symbol at IDX in the symbol table. */
4140 const char *symbol_name_at (offset_type idx) const override
4141 {
4142 return m_symbol_table[idx];
4143 }
4144
4145 private:
4146 gdb::array_view<const char *> m_symbol_table;
4147 };
4148
4149 /* Convenience function that converts a NULL pointer to a "<null>"
4150 string, to pass to print routines. */
4151
4152 static const char *
4153 string_or_null (const char *str)
4154 {
4155 return str != NULL ? str : "<null>";
4156 }
4157
4158 /* Check if a lookup_name_info built from
4159 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4160 index. EXPECTED_LIST is the list of expected matches, in expected
4161 matching order. If no match expected, then an empty list is
4162 specified. Returns true on success. On failure prints a warning
4163 indicating the file:line that failed, and returns false. */
4164
4165 static bool
4166 check_match (const char *file, int line,
4167 mock_mapped_index &mock_index,
4168 const char *name, symbol_name_match_type match_type,
4169 bool completion_mode,
4170 std::initializer_list<const char *> expected_list)
4171 {
4172 lookup_name_info lookup_name (name, match_type, completion_mode);
4173
4174 bool matched = true;
4175
4176 auto mismatch = [&] (const char *expected_str,
4177 const char *got)
4178 {
4179 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4180 "expected=\"%s\", got=\"%s\"\n"),
4181 file, line,
4182 (match_type == symbol_name_match_type::FULL
4183 ? "FULL" : "WILD"),
4184 name, string_or_null (expected_str), string_or_null (got));
4185 matched = false;
4186 };
4187
4188 auto expected_it = expected_list.begin ();
4189 auto expected_end = expected_list.end ();
4190
4191 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4192 NULL, ALL_DOMAIN,
4193 [&] (offset_type idx)
4194 {
4195 const char *matched_name = mock_index.symbol_name_at (idx);
4196 const char *expected_str
4197 = expected_it == expected_end ? NULL : *expected_it++;
4198
4199 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4200 mismatch (expected_str, matched_name);
4201 return true;
4202 });
4203
4204 const char *expected_str
4205 = expected_it == expected_end ? NULL : *expected_it++;
4206 if (expected_str != NULL)
4207 mismatch (expected_str, NULL);
4208
4209 return matched;
4210 }
4211
4212 /* The symbols added to the mock mapped_index for testing (in
4213 canonical form). */
4214 static const char *test_symbols[] = {
4215 "function",
4216 "std::bar",
4217 "std::zfunction",
4218 "std::zfunction2",
4219 "w1::w2",
4220 "ns::foo<char*>",
4221 "ns::foo<int>",
4222 "ns::foo<long>",
4223 "ns2::tmpl<int>::foo2",
4224 "(anonymous namespace)::A::B::C",
4225
4226 /* These are used to check that the increment-last-char in the
4227 matching algorithm for completion doesn't match "t1_fund" when
4228 completing "t1_func". */
4229 "t1_func",
4230 "t1_func1",
4231 "t1_fund",
4232 "t1_fund1",
4233
4234 /* A UTF-8 name with multi-byte sequences to make sure that
4235 cp-name-parser understands this as a single identifier ("função"
4236 is "function" in PT). */
4237 u8"u8função",
4238
4239 /* \377 (0xff) is Latin1 'ÿ'. */
4240 "yfunc\377",
4241
4242 /* \377 (0xff) is Latin1 'ÿ'. */
4243 "\377",
4244 "\377\377123",
4245
4246 /* A name with all sorts of complications. Starts with "z" to make
4247 it easier for the completion tests below. */
4248 #define Z_SYM_NAME \
4249 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4250 "::tuple<(anonymous namespace)::ui*, " \
4251 "std::default_delete<(anonymous namespace)::ui>, void>"
4252
4253 Z_SYM_NAME
4254 };
4255
4256 /* Returns true if the mapped_index_base::find_name_component_bounds
4257 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4258 in completion mode. */
4259
4260 static bool
4261 check_find_bounds_finds (mapped_index_base &index,
4262 const char *search_name,
4263 gdb::array_view<const char *> expected_syms)
4264 {
4265 lookup_name_info lookup_name (search_name,
4266 symbol_name_match_type::FULL, true);
4267
4268 auto bounds = index.find_name_components_bounds (lookup_name,
4269 language_cplus);
4270
4271 size_t distance = std::distance (bounds.first, bounds.second);
4272 if (distance != expected_syms.size ())
4273 return false;
4274
4275 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4276 {
4277 auto nc_elem = bounds.first + exp_elem;
4278 const char *qualified = index.symbol_name_at (nc_elem->idx);
4279 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4280 return false;
4281 }
4282
4283 return true;
4284 }
4285
4286 /* Test the lower-level mapped_index::find_name_component_bounds
4287 method. */
4288
4289 static void
4290 test_mapped_index_find_name_component_bounds ()
4291 {
4292 mock_mapped_index mock_index (test_symbols);
4293
4294 mock_index.build_name_components ();
4295
4296 /* Test the lower-level mapped_index::find_name_component_bounds
4297 method in completion mode. */
4298 {
4299 static const char *expected_syms[] = {
4300 "t1_func",
4301 "t1_func1",
4302 };
4303
4304 SELF_CHECK (check_find_bounds_finds (mock_index,
4305 "t1_func", expected_syms));
4306 }
4307
4308 /* Check that the increment-last-char in the name matching algorithm
4309 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4310 {
4311 static const char *expected_syms1[] = {
4312 "\377",
4313 "\377\377123",
4314 };
4315 SELF_CHECK (check_find_bounds_finds (mock_index,
4316 "\377", expected_syms1));
4317
4318 static const char *expected_syms2[] = {
4319 "\377\377123",
4320 };
4321 SELF_CHECK (check_find_bounds_finds (mock_index,
4322 "\377\377", expected_syms2));
4323 }
4324 }
4325
4326 /* Test dw2_expand_symtabs_matching_symbol. */
4327
4328 static void
4329 test_dw2_expand_symtabs_matching_symbol ()
4330 {
4331 mock_mapped_index mock_index (test_symbols);
4332
4333 /* We let all tests run until the end even if some fails, for debug
4334 convenience. */
4335 bool any_mismatch = false;
4336
4337 /* Create the expected symbols list (an initializer_list). Needed
4338 because lists have commas, and we need to pass them to CHECK,
4339 which is a macro. */
4340 #define EXPECT(...) { __VA_ARGS__ }
4341
4342 /* Wrapper for check_match that passes down the current
4343 __FILE__/__LINE__. */
4344 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4345 any_mismatch |= !check_match (__FILE__, __LINE__, \
4346 mock_index, \
4347 NAME, MATCH_TYPE, COMPLETION_MODE, \
4348 EXPECTED_LIST)
4349
4350 /* Identity checks. */
4351 for (const char *sym : test_symbols)
4352 {
4353 /* Should be able to match all existing symbols. */
4354 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4355 EXPECT (sym));
4356
4357 /* Should be able to match all existing symbols with
4358 parameters. */
4359 std::string with_params = std::string (sym) + "(int)";
4360 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4361 EXPECT (sym));
4362
4363 /* Should be able to match all existing symbols with
4364 parameters and qualifiers. */
4365 with_params = std::string (sym) + " ( int ) const";
4366 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4367 EXPECT (sym));
4368
4369 /* This should really find sym, but cp-name-parser.y doesn't
4370 know about lvalue/rvalue qualifiers yet. */
4371 with_params = std::string (sym) + " ( int ) &&";
4372 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4373 {});
4374 }
4375
4376 /* Check that the name matching algorithm for completion doesn't get
4377 confused with Latin1 'ÿ' / 0xff. */
4378 {
4379 static const char str[] = "\377";
4380 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4381 EXPECT ("\377", "\377\377123"));
4382 }
4383
4384 /* Check that the increment-last-char in the matching algorithm for
4385 completion doesn't match "t1_fund" when completing "t1_func". */
4386 {
4387 static const char str[] = "t1_func";
4388 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4389 EXPECT ("t1_func", "t1_func1"));
4390 }
4391
4392 /* Check that completion mode works at each prefix of the expected
4393 symbol name. */
4394 {
4395 static const char str[] = "function(int)";
4396 size_t len = strlen (str);
4397 std::string lookup;
4398
4399 for (size_t i = 1; i < len; i++)
4400 {
4401 lookup.assign (str, i);
4402 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4403 EXPECT ("function"));
4404 }
4405 }
4406
4407 /* While "w" is a prefix of both components, the match function
4408 should still only be called once. */
4409 {
4410 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4411 EXPECT ("w1::w2"));
4412 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4413 EXPECT ("w1::w2"));
4414 }
4415
4416 /* Same, with a "complicated" symbol. */
4417 {
4418 static const char str[] = Z_SYM_NAME;
4419 size_t len = strlen (str);
4420 std::string lookup;
4421
4422 for (size_t i = 1; i < len; i++)
4423 {
4424 lookup.assign (str, i);
4425 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4426 EXPECT (Z_SYM_NAME));
4427 }
4428 }
4429
4430 /* In FULL mode, an incomplete symbol doesn't match. */
4431 {
4432 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4433 {});
4434 }
4435
4436 /* A complete symbol with parameters matches any overload, since the
4437 index has no overload info. */
4438 {
4439 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4440 EXPECT ("std::zfunction", "std::zfunction2"));
4441 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4442 EXPECT ("std::zfunction", "std::zfunction2"));
4443 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4444 EXPECT ("std::zfunction", "std::zfunction2"));
4445 }
4446
4447 /* Check that whitespace is ignored appropriately. A symbol with a
4448 template argument list. */
4449 {
4450 static const char expected[] = "ns::foo<int>";
4451 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4452 EXPECT (expected));
4453 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4454 EXPECT (expected));
4455 }
4456
4457 /* Check that whitespace is ignored appropriately. A symbol with a
4458 template argument list that includes a pointer. */
4459 {
4460 static const char expected[] = "ns::foo<char*>";
4461 /* Try both completion and non-completion modes. */
4462 static const bool completion_mode[2] = {false, true};
4463 for (size_t i = 0; i < 2; i++)
4464 {
4465 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4466 completion_mode[i], EXPECT (expected));
4467 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4468 completion_mode[i], EXPECT (expected));
4469
4470 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4471 completion_mode[i], EXPECT (expected));
4472 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4473 completion_mode[i], EXPECT (expected));
4474 }
4475 }
4476
4477 {
4478 /* Check method qualifiers are ignored. */
4479 static const char expected[] = "ns::foo<char*>";
4480 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4481 symbol_name_match_type::FULL, true, EXPECT (expected));
4482 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4483 symbol_name_match_type::FULL, true, EXPECT (expected));
4484 CHECK_MATCH ("foo < char * > ( int ) const",
4485 symbol_name_match_type::WILD, true, EXPECT (expected));
4486 CHECK_MATCH ("foo < char * > ( int ) &&",
4487 symbol_name_match_type::WILD, true, EXPECT (expected));
4488 }
4489
4490 /* Test lookup names that don't match anything. */
4491 {
4492 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4493 {});
4494
4495 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4496 {});
4497 }
4498
4499 /* Some wild matching tests, exercising "(anonymous namespace)",
4500 which should not be confused with a parameter list. */
4501 {
4502 static const char *syms[] = {
4503 "A::B::C",
4504 "B::C",
4505 "C",
4506 "A :: B :: C ( int )",
4507 "B :: C ( int )",
4508 "C ( int )",
4509 };
4510
4511 for (const char *s : syms)
4512 {
4513 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4514 EXPECT ("(anonymous namespace)::A::B::C"));
4515 }
4516 }
4517
4518 {
4519 static const char expected[] = "ns2::tmpl<int>::foo2";
4520 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4521 EXPECT (expected));
4522 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4523 EXPECT (expected));
4524 }
4525
4526 SELF_CHECK (!any_mismatch);
4527
4528 #undef EXPECT
4529 #undef CHECK_MATCH
4530 }
4531
4532 static void
4533 run_test ()
4534 {
4535 test_mapped_index_find_name_component_bounds ();
4536 test_dw2_expand_symtabs_matching_symbol ();
4537 }
4538
4539 }} // namespace selftests::dw2_expand_symtabs_matching
4540
4541 #endif /* GDB_SELF_TEST */
4542
4543 /* If FILE_MATCHER is NULL or if PER_CU has
4544 dwarf2_per_cu_quick_data::MARK set (see
4545 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4546 EXPANSION_NOTIFY on it. */
4547
4548 static void
4549 dw2_expand_symtabs_matching_one
4550 (dwarf2_per_cu_data *per_cu,
4551 dwarf2_per_objfile *per_objfile,
4552 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4553 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4554 {
4555 if (file_matcher == NULL || per_cu->v.quick->mark)
4556 {
4557 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4558
4559 compunit_symtab *symtab
4560 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4561 gdb_assert (symtab != nullptr);
4562
4563 if (expansion_notify != NULL && symtab_was_null)
4564 expansion_notify (symtab);
4565 }
4566 }
4567
4568 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4569 matched, to expand corresponding CUs that were marked. IDX is the
4570 index of the symbol name that matched. */
4571
4572 static void
4573 dw2_expand_marked_cus
4574 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4575 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4576 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4577 search_domain kind)
4578 {
4579 offset_type *vec, vec_len, vec_idx;
4580 bool global_seen = false;
4581 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4582
4583 vec = (offset_type *) (index.constant_pool
4584 + MAYBE_SWAP (index.symbol_table[idx].vec));
4585 vec_len = MAYBE_SWAP (vec[0]);
4586 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4587 {
4588 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4589 /* This value is only valid for index versions >= 7. */
4590 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4591 gdb_index_symbol_kind symbol_kind =
4592 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4593 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4594 /* Only check the symbol attributes if they're present.
4595 Indices prior to version 7 don't record them,
4596 and indices >= 7 may elide them for certain symbols
4597 (gold does this). */
4598 int attrs_valid =
4599 (index.version >= 7
4600 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4601
4602 /* Work around gold/15646. */
4603 if (attrs_valid)
4604 {
4605 if (!is_static && global_seen)
4606 continue;
4607 if (!is_static)
4608 global_seen = true;
4609 }
4610
4611 /* Only check the symbol's kind if it has one. */
4612 if (attrs_valid)
4613 {
4614 switch (kind)
4615 {
4616 case VARIABLES_DOMAIN:
4617 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4618 continue;
4619 break;
4620 case FUNCTIONS_DOMAIN:
4621 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4622 continue;
4623 break;
4624 case TYPES_DOMAIN:
4625 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4626 continue;
4627 break;
4628 case MODULES_DOMAIN:
4629 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4630 continue;
4631 break;
4632 default:
4633 break;
4634 }
4635 }
4636
4637 /* Don't crash on bad data. */
4638 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4639 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4640 {
4641 complaint (_(".gdb_index entry has bad CU index"
4642 " [in module %s]"),
4643 objfile_name (dwarf2_per_objfile->objfile));
4644 continue;
4645 }
4646
4647 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4648 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4649 expansion_notify);
4650 }
4651 }
4652
4653 /* If FILE_MATCHER is non-NULL, set all the
4654 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4655 that match FILE_MATCHER. */
4656
4657 static void
4658 dw_expand_symtabs_matching_file_matcher
4659 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4660 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4661 {
4662 if (file_matcher == NULL)
4663 return;
4664
4665 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4666 htab_eq_pointer,
4667 NULL, xcalloc, xfree));
4668 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4669 htab_eq_pointer,
4670 NULL, xcalloc, xfree));
4671
4672 /* The rule is CUs specify all the files, including those used by
4673 any TU, so there's no need to scan TUs here. */
4674
4675 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4676 {
4677 QUIT;
4678
4679 per_cu->v.quick->mark = 0;
4680
4681 /* We only need to look at symtabs not already expanded. */
4682 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4683 continue;
4684
4685 quick_file_names *file_data
4686 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4687 if (file_data == NULL)
4688 continue;
4689
4690 if (htab_find (visited_not_found.get (), file_data) != NULL)
4691 continue;
4692 else if (htab_find (visited_found.get (), file_data) != NULL)
4693 {
4694 per_cu->v.quick->mark = 1;
4695 continue;
4696 }
4697
4698 for (int j = 0; j < file_data->num_file_names; ++j)
4699 {
4700 const char *this_real_name;
4701
4702 if (file_matcher (file_data->file_names[j], false))
4703 {
4704 per_cu->v.quick->mark = 1;
4705 break;
4706 }
4707
4708 /* Before we invoke realpath, which can get expensive when many
4709 files are involved, do a quick comparison of the basenames. */
4710 if (!basenames_may_differ
4711 && !file_matcher (lbasename (file_data->file_names[j]),
4712 true))
4713 continue;
4714
4715 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4716 file_data, j);
4717 if (file_matcher (this_real_name, false))
4718 {
4719 per_cu->v.quick->mark = 1;
4720 break;
4721 }
4722 }
4723
4724 void **slot = htab_find_slot (per_cu->v.quick->mark
4725 ? visited_found.get ()
4726 : visited_not_found.get (),
4727 file_data, INSERT);
4728 *slot = file_data;
4729 }
4730 }
4731
4732 static void
4733 dw2_expand_symtabs_matching
4734 (struct objfile *objfile,
4735 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4736 const lookup_name_info *lookup_name,
4737 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4738 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4739 enum search_domain kind)
4740 {
4741 struct dwarf2_per_objfile *dwarf2_per_objfile
4742 = get_dwarf2_per_objfile (objfile);
4743
4744 /* index_table is NULL if OBJF_READNOW. */
4745 if (!dwarf2_per_objfile->per_bfd->index_table)
4746 return;
4747
4748 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4749
4750 if (symbol_matcher == NULL && lookup_name == NULL)
4751 {
4752 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4753 {
4754 QUIT;
4755
4756 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4757 file_matcher, expansion_notify);
4758 }
4759 return;
4760 }
4761
4762 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4763
4764 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4765 symbol_matcher,
4766 kind, [&] (offset_type idx)
4767 {
4768 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4769 expansion_notify, kind);
4770 return true;
4771 });
4772 }
4773
4774 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4775 symtab. */
4776
4777 static struct compunit_symtab *
4778 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4779 CORE_ADDR pc)
4780 {
4781 int i;
4782
4783 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4784 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4785 return cust;
4786
4787 if (cust->includes == NULL)
4788 return NULL;
4789
4790 for (i = 0; cust->includes[i]; ++i)
4791 {
4792 struct compunit_symtab *s = cust->includes[i];
4793
4794 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4795 if (s != NULL)
4796 return s;
4797 }
4798
4799 return NULL;
4800 }
4801
4802 static struct compunit_symtab *
4803 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4804 struct bound_minimal_symbol msymbol,
4805 CORE_ADDR pc,
4806 struct obj_section *section,
4807 int warn_if_readin)
4808 {
4809 struct dwarf2_per_cu_data *data;
4810 struct compunit_symtab *result;
4811
4812 if (!objfile->partial_symtabs->psymtabs_addrmap)
4813 return NULL;
4814
4815 CORE_ADDR baseaddr = objfile->text_section_offset ();
4816 data = (struct dwarf2_per_cu_data *) addrmap_find
4817 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4818 if (!data)
4819 return NULL;
4820
4821 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4822 if (warn_if_readin && per_objfile->symtab_set_p (data))
4823 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4824 paddress (objfile->arch (), pc));
4825
4826 result = recursively_find_pc_sect_compunit_symtab
4827 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4828
4829 gdb_assert (result != NULL);
4830 return result;
4831 }
4832
4833 static void
4834 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4835 void *data, int need_fullname)
4836 {
4837 struct dwarf2_per_objfile *dwarf2_per_objfile
4838 = get_dwarf2_per_objfile (objfile);
4839
4840 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4841 {
4842 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4843
4844 htab_up visited (htab_create_alloc (10,
4845 htab_hash_pointer, htab_eq_pointer,
4846 NULL, xcalloc, xfree));
4847
4848 /* The rule is CUs specify all the files, including those used
4849 by any TU, so there's no need to scan TUs here. We can
4850 ignore file names coming from already-expanded CUs. */
4851
4852 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4853 {
4854 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4855 {
4856 void **slot = htab_find_slot (visited.get (),
4857 per_cu->v.quick->file_names,
4858 INSERT);
4859
4860 *slot = per_cu->v.quick->file_names;
4861 }
4862 }
4863
4864 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4865 {
4866 /* We only need to look at symtabs not already expanded. */
4867 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4868 continue;
4869
4870 quick_file_names *file_data
4871 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4872 if (file_data == NULL)
4873 continue;
4874
4875 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4876 if (*slot)
4877 {
4878 /* Already visited. */
4879 continue;
4880 }
4881 *slot = file_data;
4882
4883 for (int j = 0; j < file_data->num_file_names; ++j)
4884 {
4885 const char *filename = file_data->file_names[j];
4886 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4887 }
4888 }
4889 }
4890
4891 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4892 {
4893 gdb::unique_xmalloc_ptr<char> this_real_name;
4894
4895 if (need_fullname)
4896 this_real_name = gdb_realpath (filename);
4897 (*fun) (filename, this_real_name.get (), data);
4898 });
4899 }
4900
4901 static int
4902 dw2_has_symbols (struct objfile *objfile)
4903 {
4904 return 1;
4905 }
4906
4907 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4908 {
4909 dw2_has_symbols,
4910 dw2_find_last_source_symtab,
4911 dw2_forget_cached_source_info,
4912 dw2_map_symtabs_matching_filename,
4913 dw2_lookup_symbol,
4914 NULL,
4915 dw2_print_stats,
4916 dw2_dump,
4917 dw2_expand_symtabs_for_function,
4918 dw2_expand_all_symtabs,
4919 dw2_expand_symtabs_with_fullname,
4920 dw2_map_matching_symbols,
4921 dw2_expand_symtabs_matching,
4922 dw2_find_pc_sect_compunit_symtab,
4923 NULL,
4924 dw2_map_symbol_filenames
4925 };
4926
4927 /* DWARF-5 debug_names reader. */
4928
4929 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4930 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4931
4932 /* A helper function that reads the .debug_names section in SECTION
4933 and fills in MAP. FILENAME is the name of the file containing the
4934 section; it is used for error reporting.
4935
4936 Returns true if all went well, false otherwise. */
4937
4938 static bool
4939 read_debug_names_from_section (struct objfile *objfile,
4940 const char *filename,
4941 struct dwarf2_section_info *section,
4942 mapped_debug_names &map)
4943 {
4944 if (section->empty ())
4945 return false;
4946
4947 /* Older elfutils strip versions could keep the section in the main
4948 executable while splitting it for the separate debug info file. */
4949 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4950 return false;
4951
4952 section->read (objfile);
4953
4954 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4955
4956 const gdb_byte *addr = section->buffer;
4957
4958 bfd *const abfd = section->get_bfd_owner ();
4959
4960 unsigned int bytes_read;
4961 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4962 addr += bytes_read;
4963
4964 map.dwarf5_is_dwarf64 = bytes_read != 4;
4965 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4966 if (bytes_read + length != section->size)
4967 {
4968 /* There may be multiple per-CU indices. */
4969 warning (_("Section .debug_names in %s length %s does not match "
4970 "section length %s, ignoring .debug_names."),
4971 filename, plongest (bytes_read + length),
4972 pulongest (section->size));
4973 return false;
4974 }
4975
4976 /* The version number. */
4977 uint16_t version = read_2_bytes (abfd, addr);
4978 addr += 2;
4979 if (version != 5)
4980 {
4981 warning (_("Section .debug_names in %s has unsupported version %d, "
4982 "ignoring .debug_names."),
4983 filename, version);
4984 return false;
4985 }
4986
4987 /* Padding. */
4988 uint16_t padding = read_2_bytes (abfd, addr);
4989 addr += 2;
4990 if (padding != 0)
4991 {
4992 warning (_("Section .debug_names in %s has unsupported padding %d, "
4993 "ignoring .debug_names."),
4994 filename, padding);
4995 return false;
4996 }
4997
4998 /* comp_unit_count - The number of CUs in the CU list. */
4999 map.cu_count = read_4_bytes (abfd, addr);
5000 addr += 4;
5001
5002 /* local_type_unit_count - The number of TUs in the local TU
5003 list. */
5004 map.tu_count = read_4_bytes (abfd, addr);
5005 addr += 4;
5006
5007 /* foreign_type_unit_count - The number of TUs in the foreign TU
5008 list. */
5009 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5010 addr += 4;
5011 if (foreign_tu_count != 0)
5012 {
5013 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5014 "ignoring .debug_names."),
5015 filename, static_cast<unsigned long> (foreign_tu_count));
5016 return false;
5017 }
5018
5019 /* bucket_count - The number of hash buckets in the hash lookup
5020 table. */
5021 map.bucket_count = read_4_bytes (abfd, addr);
5022 addr += 4;
5023
5024 /* name_count - The number of unique names in the index. */
5025 map.name_count = read_4_bytes (abfd, addr);
5026 addr += 4;
5027
5028 /* abbrev_table_size - The size in bytes of the abbreviations
5029 table. */
5030 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5031 addr += 4;
5032
5033 /* augmentation_string_size - The size in bytes of the augmentation
5034 string. This value is rounded up to a multiple of 4. */
5035 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5036 addr += 4;
5037 map.augmentation_is_gdb = ((augmentation_string_size
5038 == sizeof (dwarf5_augmentation))
5039 && memcmp (addr, dwarf5_augmentation,
5040 sizeof (dwarf5_augmentation)) == 0);
5041 augmentation_string_size += (-augmentation_string_size) & 3;
5042 addr += augmentation_string_size;
5043
5044 /* List of CUs */
5045 map.cu_table_reordered = addr;
5046 addr += map.cu_count * map.offset_size;
5047
5048 /* List of Local TUs */
5049 map.tu_table_reordered = addr;
5050 addr += map.tu_count * map.offset_size;
5051
5052 /* Hash Lookup Table */
5053 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5054 addr += map.bucket_count * 4;
5055 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5056 addr += map.name_count * 4;
5057
5058 /* Name Table */
5059 map.name_table_string_offs_reordered = addr;
5060 addr += map.name_count * map.offset_size;
5061 map.name_table_entry_offs_reordered = addr;
5062 addr += map.name_count * map.offset_size;
5063
5064 const gdb_byte *abbrev_table_start = addr;
5065 for (;;)
5066 {
5067 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5068 addr += bytes_read;
5069 if (index_num == 0)
5070 break;
5071
5072 const auto insertpair
5073 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5074 if (!insertpair.second)
5075 {
5076 warning (_("Section .debug_names in %s has duplicate index %s, "
5077 "ignoring .debug_names."),
5078 filename, pulongest (index_num));
5079 return false;
5080 }
5081 mapped_debug_names::index_val &indexval = insertpair.first->second;
5082 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5083 addr += bytes_read;
5084
5085 for (;;)
5086 {
5087 mapped_debug_names::index_val::attr attr;
5088 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5089 addr += bytes_read;
5090 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5091 addr += bytes_read;
5092 if (attr.form == DW_FORM_implicit_const)
5093 {
5094 attr.implicit_const = read_signed_leb128 (abfd, addr,
5095 &bytes_read);
5096 addr += bytes_read;
5097 }
5098 if (attr.dw_idx == 0 && attr.form == 0)
5099 break;
5100 indexval.attr_vec.push_back (std::move (attr));
5101 }
5102 }
5103 if (addr != abbrev_table_start + abbrev_table_size)
5104 {
5105 warning (_("Section .debug_names in %s has abbreviation_table "
5106 "of size %s vs. written as %u, ignoring .debug_names."),
5107 filename, plongest (addr - abbrev_table_start),
5108 abbrev_table_size);
5109 return false;
5110 }
5111 map.entry_pool = addr;
5112
5113 return true;
5114 }
5115
5116 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5117 list. */
5118
5119 static void
5120 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5121 const mapped_debug_names &map,
5122 dwarf2_section_info &section,
5123 bool is_dwz)
5124 {
5125 if (!map.augmentation_is_gdb)
5126 {
5127 for (uint32_t i = 0; i < map.cu_count; ++i)
5128 {
5129 sect_offset sect_off
5130 = (sect_offset) (extract_unsigned_integer
5131 (map.cu_table_reordered + i * map.offset_size,
5132 map.offset_size,
5133 map.dwarf5_byte_order));
5134 /* We don't know the length of the CU, because the CU list in a
5135 .debug_names index can be incomplete, so we can't use the start of
5136 the next CU as end of this CU. We create the CUs here with length 0,
5137 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5138 dwarf2_per_cu_data *per_cu
5139 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5140 sect_off, 0);
5141 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5142 }
5143 }
5144
5145 sect_offset sect_off_prev;
5146 for (uint32_t i = 0; i <= map.cu_count; ++i)
5147 {
5148 sect_offset sect_off_next;
5149 if (i < map.cu_count)
5150 {
5151 sect_off_next
5152 = (sect_offset) (extract_unsigned_integer
5153 (map.cu_table_reordered + i * map.offset_size,
5154 map.offset_size,
5155 map.dwarf5_byte_order));
5156 }
5157 else
5158 sect_off_next = (sect_offset) section.size;
5159 if (i >= 1)
5160 {
5161 const ULONGEST length = sect_off_next - sect_off_prev;
5162 dwarf2_per_cu_data *per_cu
5163 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5164 sect_off_prev, length);
5165 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5166 }
5167 sect_off_prev = sect_off_next;
5168 }
5169 }
5170
5171 /* Read the CU list from the mapped index, and use it to create all
5172 the CU objects for this dwarf2_per_objfile. */
5173
5174 static void
5175 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5176 const mapped_debug_names &map,
5177 const mapped_debug_names &dwz_map)
5178 {
5179 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5180 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5181
5182 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5183 dwarf2_per_objfile->per_bfd->info,
5184 false /* is_dwz */);
5185
5186 if (dwz_map.cu_count == 0)
5187 return;
5188
5189 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5190 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5191 true /* is_dwz */);
5192 }
5193
5194 /* Read .debug_names. If everything went ok, initialize the "quick"
5195 elements of all the CUs and return true. Otherwise, return false. */
5196
5197 static bool
5198 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5199 {
5200 std::unique_ptr<mapped_debug_names> map
5201 (new mapped_debug_names (dwarf2_per_objfile));
5202 mapped_debug_names dwz_map (dwarf2_per_objfile);
5203 struct objfile *objfile = dwarf2_per_objfile->objfile;
5204
5205 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5206 &dwarf2_per_objfile->per_bfd->debug_names,
5207 *map))
5208 return false;
5209
5210 /* Don't use the index if it's empty. */
5211 if (map->name_count == 0)
5212 return false;
5213
5214 /* If there is a .dwz file, read it so we can get its CU list as
5215 well. */
5216 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5217 if (dwz != NULL)
5218 {
5219 if (!read_debug_names_from_section (objfile,
5220 bfd_get_filename (dwz->dwz_bfd.get ()),
5221 &dwz->debug_names, dwz_map))
5222 {
5223 warning (_("could not read '.debug_names' section from %s; skipping"),
5224 bfd_get_filename (dwz->dwz_bfd.get ()));
5225 return false;
5226 }
5227 }
5228
5229 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5230
5231 if (map->tu_count != 0)
5232 {
5233 /* We can only handle a single .debug_types when we have an
5234 index. */
5235 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5236 return false;
5237
5238 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5239
5240 create_signatured_type_table_from_debug_names
5241 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5242 }
5243
5244 create_addrmap_from_aranges (dwarf2_per_objfile,
5245 &dwarf2_per_objfile->per_bfd->debug_aranges);
5246
5247 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5248 dwarf2_per_objfile->per_bfd->using_index = 1;
5249 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5250 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5251
5252 return true;
5253 }
5254
5255 /* Type used to manage iterating over all CUs looking for a symbol for
5256 .debug_names. */
5257
5258 class dw2_debug_names_iterator
5259 {
5260 public:
5261 dw2_debug_names_iterator (const mapped_debug_names &map,
5262 gdb::optional<block_enum> block_index,
5263 domain_enum domain,
5264 const char *name)
5265 : m_map (map), m_block_index (block_index), m_domain (domain),
5266 m_addr (find_vec_in_debug_names (map, name))
5267 {}
5268
5269 dw2_debug_names_iterator (const mapped_debug_names &map,
5270 search_domain search, uint32_t namei)
5271 : m_map (map),
5272 m_search (search),
5273 m_addr (find_vec_in_debug_names (map, namei))
5274 {}
5275
5276 dw2_debug_names_iterator (const mapped_debug_names &map,
5277 block_enum block_index, domain_enum domain,
5278 uint32_t namei)
5279 : m_map (map), m_block_index (block_index), m_domain (domain),
5280 m_addr (find_vec_in_debug_names (map, namei))
5281 {}
5282
5283 /* Return the next matching CU or NULL if there are no more. */
5284 dwarf2_per_cu_data *next ();
5285
5286 private:
5287 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5288 const char *name);
5289 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5290 uint32_t namei);
5291
5292 /* The internalized form of .debug_names. */
5293 const mapped_debug_names &m_map;
5294
5295 /* If set, only look for symbols that match that block. Valid values are
5296 GLOBAL_BLOCK and STATIC_BLOCK. */
5297 const gdb::optional<block_enum> m_block_index;
5298
5299 /* The kind of symbol we're looking for. */
5300 const domain_enum m_domain = UNDEF_DOMAIN;
5301 const search_domain m_search = ALL_DOMAIN;
5302
5303 /* The list of CUs from the index entry of the symbol, or NULL if
5304 not found. */
5305 const gdb_byte *m_addr;
5306 };
5307
5308 const char *
5309 mapped_debug_names::namei_to_name (uint32_t namei) const
5310 {
5311 const ULONGEST namei_string_offs
5312 = extract_unsigned_integer ((name_table_string_offs_reordered
5313 + namei * offset_size),
5314 offset_size,
5315 dwarf5_byte_order);
5316 return read_indirect_string_at_offset (dwarf2_per_objfile,
5317 namei_string_offs);
5318 }
5319
5320 /* Find a slot in .debug_names for the object named NAME. If NAME is
5321 found, return pointer to its pool data. If NAME cannot be found,
5322 return NULL. */
5323
5324 const gdb_byte *
5325 dw2_debug_names_iterator::find_vec_in_debug_names
5326 (const mapped_debug_names &map, const char *name)
5327 {
5328 int (*cmp) (const char *, const char *);
5329
5330 gdb::unique_xmalloc_ptr<char> without_params;
5331 if (current_language->la_language == language_cplus
5332 || current_language->la_language == language_fortran
5333 || current_language->la_language == language_d)
5334 {
5335 /* NAME is already canonical. Drop any qualifiers as
5336 .debug_names does not contain any. */
5337
5338 if (strchr (name, '(') != NULL)
5339 {
5340 without_params = cp_remove_params (name);
5341 if (without_params != NULL)
5342 name = without_params.get ();
5343 }
5344 }
5345
5346 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5347
5348 const uint32_t full_hash = dwarf5_djb_hash (name);
5349 uint32_t namei
5350 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5351 (map.bucket_table_reordered
5352 + (full_hash % map.bucket_count)), 4,
5353 map.dwarf5_byte_order);
5354 if (namei == 0)
5355 return NULL;
5356 --namei;
5357 if (namei >= map.name_count)
5358 {
5359 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5360 "[in module %s]"),
5361 namei, map.name_count,
5362 objfile_name (map.dwarf2_per_objfile->objfile));
5363 return NULL;
5364 }
5365
5366 for (;;)
5367 {
5368 const uint32_t namei_full_hash
5369 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5370 (map.hash_table_reordered + namei), 4,
5371 map.dwarf5_byte_order);
5372 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5373 return NULL;
5374
5375 if (full_hash == namei_full_hash)
5376 {
5377 const char *const namei_string = map.namei_to_name (namei);
5378
5379 #if 0 /* An expensive sanity check. */
5380 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5381 {
5382 complaint (_("Wrong .debug_names hash for string at index %u "
5383 "[in module %s]"),
5384 namei, objfile_name (dwarf2_per_objfile->objfile));
5385 return NULL;
5386 }
5387 #endif
5388
5389 if (cmp (namei_string, name) == 0)
5390 {
5391 const ULONGEST namei_entry_offs
5392 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5393 + namei * map.offset_size),
5394 map.offset_size, map.dwarf5_byte_order);
5395 return map.entry_pool + namei_entry_offs;
5396 }
5397 }
5398
5399 ++namei;
5400 if (namei >= map.name_count)
5401 return NULL;
5402 }
5403 }
5404
5405 const gdb_byte *
5406 dw2_debug_names_iterator::find_vec_in_debug_names
5407 (const mapped_debug_names &map, uint32_t namei)
5408 {
5409 if (namei >= map.name_count)
5410 {
5411 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5412 "[in module %s]"),
5413 namei, map.name_count,
5414 objfile_name (map.dwarf2_per_objfile->objfile));
5415 return NULL;
5416 }
5417
5418 const ULONGEST namei_entry_offs
5419 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5420 + namei * map.offset_size),
5421 map.offset_size, map.dwarf5_byte_order);
5422 return map.entry_pool + namei_entry_offs;
5423 }
5424
5425 /* See dw2_debug_names_iterator. */
5426
5427 dwarf2_per_cu_data *
5428 dw2_debug_names_iterator::next ()
5429 {
5430 if (m_addr == NULL)
5431 return NULL;
5432
5433 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5434 struct objfile *objfile = dwarf2_per_objfile->objfile;
5435 bfd *const abfd = objfile->obfd;
5436
5437 again:
5438
5439 unsigned int bytes_read;
5440 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5441 m_addr += bytes_read;
5442 if (abbrev == 0)
5443 return NULL;
5444
5445 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5446 if (indexval_it == m_map.abbrev_map.cend ())
5447 {
5448 complaint (_("Wrong .debug_names undefined abbrev code %s "
5449 "[in module %s]"),
5450 pulongest (abbrev), objfile_name (objfile));
5451 return NULL;
5452 }
5453 const mapped_debug_names::index_val &indexval = indexval_it->second;
5454 enum class symbol_linkage {
5455 unknown,
5456 static_,
5457 extern_,
5458 } symbol_linkage_ = symbol_linkage::unknown;
5459 dwarf2_per_cu_data *per_cu = NULL;
5460 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5461 {
5462 ULONGEST ull;
5463 switch (attr.form)
5464 {
5465 case DW_FORM_implicit_const:
5466 ull = attr.implicit_const;
5467 break;
5468 case DW_FORM_flag_present:
5469 ull = 1;
5470 break;
5471 case DW_FORM_udata:
5472 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5473 m_addr += bytes_read;
5474 break;
5475 case DW_FORM_ref4:
5476 ull = read_4_bytes (abfd, m_addr);
5477 m_addr += 4;
5478 break;
5479 case DW_FORM_ref8:
5480 ull = read_8_bytes (abfd, m_addr);
5481 m_addr += 8;
5482 break;
5483 case DW_FORM_ref_sig8:
5484 ull = read_8_bytes (abfd, m_addr);
5485 m_addr += 8;
5486 break;
5487 default:
5488 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5489 dwarf_form_name (attr.form),
5490 objfile_name (objfile));
5491 return NULL;
5492 }
5493 switch (attr.dw_idx)
5494 {
5495 case DW_IDX_compile_unit:
5496 /* Don't crash on bad data. */
5497 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5498 {
5499 complaint (_(".debug_names entry has bad CU index %s"
5500 " [in module %s]"),
5501 pulongest (ull),
5502 objfile_name (dwarf2_per_objfile->objfile));
5503 continue;
5504 }
5505 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5506 break;
5507 case DW_IDX_type_unit:
5508 /* Don't crash on bad data. */
5509 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5510 {
5511 complaint (_(".debug_names entry has bad TU index %s"
5512 " [in module %s]"),
5513 pulongest (ull),
5514 objfile_name (dwarf2_per_objfile->objfile));
5515 continue;
5516 }
5517 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5518 break;
5519 case DW_IDX_die_offset:
5520 /* In a per-CU index (as opposed to a per-module index), index
5521 entries without CU attribute implicitly refer to the single CU. */
5522 if (per_cu == NULL)
5523 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5524 break;
5525 case DW_IDX_GNU_internal:
5526 if (!m_map.augmentation_is_gdb)
5527 break;
5528 symbol_linkage_ = symbol_linkage::static_;
5529 break;
5530 case DW_IDX_GNU_external:
5531 if (!m_map.augmentation_is_gdb)
5532 break;
5533 symbol_linkage_ = symbol_linkage::extern_;
5534 break;
5535 }
5536 }
5537
5538 /* Skip if already read in. */
5539 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5540 goto again;
5541
5542 /* Check static vs global. */
5543 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5544 {
5545 const bool want_static = *m_block_index == STATIC_BLOCK;
5546 const bool symbol_is_static =
5547 symbol_linkage_ == symbol_linkage::static_;
5548 if (want_static != symbol_is_static)
5549 goto again;
5550 }
5551
5552 /* Match dw2_symtab_iter_next, symbol_kind
5553 and debug_names::psymbol_tag. */
5554 switch (m_domain)
5555 {
5556 case VAR_DOMAIN:
5557 switch (indexval.dwarf_tag)
5558 {
5559 case DW_TAG_variable:
5560 case DW_TAG_subprogram:
5561 /* Some types are also in VAR_DOMAIN. */
5562 case DW_TAG_typedef:
5563 case DW_TAG_structure_type:
5564 break;
5565 default:
5566 goto again;
5567 }
5568 break;
5569 case STRUCT_DOMAIN:
5570 switch (indexval.dwarf_tag)
5571 {
5572 case DW_TAG_typedef:
5573 case DW_TAG_structure_type:
5574 break;
5575 default:
5576 goto again;
5577 }
5578 break;
5579 case LABEL_DOMAIN:
5580 switch (indexval.dwarf_tag)
5581 {
5582 case 0:
5583 case DW_TAG_variable:
5584 break;
5585 default:
5586 goto again;
5587 }
5588 break;
5589 case MODULE_DOMAIN:
5590 switch (indexval.dwarf_tag)
5591 {
5592 case DW_TAG_module:
5593 break;
5594 default:
5595 goto again;
5596 }
5597 break;
5598 default:
5599 break;
5600 }
5601
5602 /* Match dw2_expand_symtabs_matching, symbol_kind and
5603 debug_names::psymbol_tag. */
5604 switch (m_search)
5605 {
5606 case VARIABLES_DOMAIN:
5607 switch (indexval.dwarf_tag)
5608 {
5609 case DW_TAG_variable:
5610 break;
5611 default:
5612 goto again;
5613 }
5614 break;
5615 case FUNCTIONS_DOMAIN:
5616 switch (indexval.dwarf_tag)
5617 {
5618 case DW_TAG_subprogram:
5619 break;
5620 default:
5621 goto again;
5622 }
5623 break;
5624 case TYPES_DOMAIN:
5625 switch (indexval.dwarf_tag)
5626 {
5627 case DW_TAG_typedef:
5628 case DW_TAG_structure_type:
5629 break;
5630 default:
5631 goto again;
5632 }
5633 break;
5634 case MODULES_DOMAIN:
5635 switch (indexval.dwarf_tag)
5636 {
5637 case DW_TAG_module:
5638 break;
5639 default:
5640 goto again;
5641 }
5642 default:
5643 break;
5644 }
5645
5646 return per_cu;
5647 }
5648
5649 static struct compunit_symtab *
5650 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5651 const char *name, domain_enum domain)
5652 {
5653 struct dwarf2_per_objfile *dwarf2_per_objfile
5654 = get_dwarf2_per_objfile (objfile);
5655
5656 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5657 if (!mapp)
5658 {
5659 /* index is NULL if OBJF_READNOW. */
5660 return NULL;
5661 }
5662 const auto &map = *mapp;
5663
5664 dw2_debug_names_iterator iter (map, block_index, domain, name);
5665
5666 struct compunit_symtab *stab_best = NULL;
5667 struct dwarf2_per_cu_data *per_cu;
5668 while ((per_cu = iter.next ()) != NULL)
5669 {
5670 struct symbol *sym, *with_opaque = NULL;
5671 compunit_symtab *stab
5672 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5673 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5674 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5675
5676 sym = block_find_symbol (block, name, domain,
5677 block_find_non_opaque_type_preferred,
5678 &with_opaque);
5679
5680 /* Some caution must be observed with overloaded functions and
5681 methods, since the index will not contain any overload
5682 information (but NAME might contain it). */
5683
5684 if (sym != NULL
5685 && strcmp_iw (sym->search_name (), name) == 0)
5686 return stab;
5687 if (with_opaque != NULL
5688 && strcmp_iw (with_opaque->search_name (), name) == 0)
5689 stab_best = stab;
5690
5691 /* Keep looking through other CUs. */
5692 }
5693
5694 return stab_best;
5695 }
5696
5697 /* This dumps minimal information about .debug_names. It is called
5698 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5699 uses this to verify that .debug_names has been loaded. */
5700
5701 static void
5702 dw2_debug_names_dump (struct objfile *objfile)
5703 {
5704 struct dwarf2_per_objfile *dwarf2_per_objfile
5705 = get_dwarf2_per_objfile (objfile);
5706
5707 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5708 printf_filtered (".debug_names:");
5709 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5710 printf_filtered (" exists\n");
5711 else
5712 printf_filtered (" faked for \"readnow\"\n");
5713 printf_filtered ("\n");
5714 }
5715
5716 static void
5717 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5718 const char *func_name)
5719 {
5720 struct dwarf2_per_objfile *dwarf2_per_objfile
5721 = get_dwarf2_per_objfile (objfile);
5722
5723 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5724 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5725 {
5726 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5727
5728 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5729
5730 struct dwarf2_per_cu_data *per_cu;
5731 while ((per_cu = iter.next ()) != NULL)
5732 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5733 }
5734 }
5735
5736 static void
5737 dw2_debug_names_map_matching_symbols
5738 (struct objfile *objfile,
5739 const lookup_name_info &name, domain_enum domain,
5740 int global,
5741 gdb::function_view<symbol_found_callback_ftype> callback,
5742 symbol_compare_ftype *ordered_compare)
5743 {
5744 struct dwarf2_per_objfile *dwarf2_per_objfile
5745 = get_dwarf2_per_objfile (objfile);
5746
5747 /* debug_names_table is NULL if OBJF_READNOW. */
5748 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5749 return;
5750
5751 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5752 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5753
5754 const char *match_name = name.ada ().lookup_name ().c_str ();
5755 auto matcher = [&] (const char *symname)
5756 {
5757 if (ordered_compare == nullptr)
5758 return true;
5759 return ordered_compare (symname, match_name) == 0;
5760 };
5761
5762 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5763 [&] (offset_type namei)
5764 {
5765 /* The name was matched, now expand corresponding CUs that were
5766 marked. */
5767 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5768
5769 struct dwarf2_per_cu_data *per_cu;
5770 while ((per_cu = iter.next ()) != NULL)
5771 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5772 nullptr);
5773 return true;
5774 });
5775
5776 /* It's a shame we couldn't do this inside the
5777 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5778 that have already been expanded. Instead, this loop matches what
5779 the psymtab code does. */
5780 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5781 {
5782 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5783 if (symtab != nullptr)
5784 {
5785 const struct block *block
5786 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5787 if (!iterate_over_symbols_terminated (block, name,
5788 domain, callback))
5789 break;
5790 }
5791 }
5792 }
5793
5794 static void
5795 dw2_debug_names_expand_symtabs_matching
5796 (struct objfile *objfile,
5797 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5798 const lookup_name_info *lookup_name,
5799 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5800 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5801 enum search_domain kind)
5802 {
5803 struct dwarf2_per_objfile *dwarf2_per_objfile
5804 = get_dwarf2_per_objfile (objfile);
5805
5806 /* debug_names_table is NULL if OBJF_READNOW. */
5807 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5808 return;
5809
5810 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5811
5812 if (symbol_matcher == NULL && lookup_name == NULL)
5813 {
5814 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5815 {
5816 QUIT;
5817
5818 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5819 file_matcher, expansion_notify);
5820 }
5821 return;
5822 }
5823
5824 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5825
5826 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5827 symbol_matcher,
5828 kind, [&] (offset_type namei)
5829 {
5830 /* The name was matched, now expand corresponding CUs that were
5831 marked. */
5832 dw2_debug_names_iterator iter (map, kind, namei);
5833
5834 struct dwarf2_per_cu_data *per_cu;
5835 while ((per_cu = iter.next ()) != NULL)
5836 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5837 file_matcher, expansion_notify);
5838 return true;
5839 });
5840 }
5841
5842 const struct quick_symbol_functions dwarf2_debug_names_functions =
5843 {
5844 dw2_has_symbols,
5845 dw2_find_last_source_symtab,
5846 dw2_forget_cached_source_info,
5847 dw2_map_symtabs_matching_filename,
5848 dw2_debug_names_lookup_symbol,
5849 NULL,
5850 dw2_print_stats,
5851 dw2_debug_names_dump,
5852 dw2_debug_names_expand_symtabs_for_function,
5853 dw2_expand_all_symtabs,
5854 dw2_expand_symtabs_with_fullname,
5855 dw2_debug_names_map_matching_symbols,
5856 dw2_debug_names_expand_symtabs_matching,
5857 dw2_find_pc_sect_compunit_symtab,
5858 NULL,
5859 dw2_map_symbol_filenames
5860 };
5861
5862 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5863 to either a dwarf2_per_bfd or dwz_file object. */
5864
5865 template <typename T>
5866 static gdb::array_view<const gdb_byte>
5867 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5868 {
5869 dwarf2_section_info *section = &section_owner->gdb_index;
5870
5871 if (section->empty ())
5872 return {};
5873
5874 /* Older elfutils strip versions could keep the section in the main
5875 executable while splitting it for the separate debug info file. */
5876 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5877 return {};
5878
5879 section->read (obj);
5880
5881 /* dwarf2_section_info::size is a bfd_size_type, while
5882 gdb::array_view works with size_t. On 32-bit hosts, with
5883 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5884 is 32-bit. So we need an explicit narrowing conversion here.
5885 This is fine, because it's impossible to allocate or mmap an
5886 array/buffer larger than what size_t can represent. */
5887 return gdb::make_array_view (section->buffer, section->size);
5888 }
5889
5890 /* Lookup the index cache for the contents of the index associated to
5891 DWARF2_OBJ. */
5892
5893 static gdb::array_view<const gdb_byte>
5894 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5895 {
5896 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5897 if (build_id == nullptr)
5898 return {};
5899
5900 return global_index_cache.lookup_gdb_index (build_id,
5901 &dwarf2_per_bfd->index_cache_res);
5902 }
5903
5904 /* Same as the above, but for DWZ. */
5905
5906 static gdb::array_view<const gdb_byte>
5907 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5908 {
5909 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5910 if (build_id == nullptr)
5911 return {};
5912
5913 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5914 }
5915
5916 /* See symfile.h. */
5917
5918 bool
5919 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5920 {
5921 struct dwarf2_per_objfile *dwarf2_per_objfile
5922 = get_dwarf2_per_objfile (objfile);
5923
5924 /* If we're about to read full symbols, don't bother with the
5925 indices. In this case we also don't care if some other debug
5926 format is making psymtabs, because they are all about to be
5927 expanded anyway. */
5928 if ((objfile->flags & OBJF_READNOW))
5929 {
5930 dwarf2_per_objfile->per_bfd->using_index = 1;
5931 create_all_comp_units (dwarf2_per_objfile);
5932 create_all_type_units (dwarf2_per_objfile);
5933 dwarf2_per_objfile->per_bfd->quick_file_names_table
5934 = create_quick_file_names_table
5935 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5936 dwarf2_per_objfile->resize_symtabs ();
5937
5938 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5939 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5940 {
5941 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5942
5943 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5944 struct dwarf2_per_cu_quick_data);
5945 }
5946
5947 /* Return 1 so that gdb sees the "quick" functions. However,
5948 these functions will be no-ops because we will have expanded
5949 all symtabs. */
5950 *index_kind = dw_index_kind::GDB_INDEX;
5951 return true;
5952 }
5953
5954 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5955 {
5956 *index_kind = dw_index_kind::DEBUG_NAMES;
5957 dwarf2_per_objfile->resize_symtabs ();
5958 return true;
5959 }
5960
5961 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5962 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5963 get_gdb_index_contents_from_section<dwz_file>))
5964 {
5965 *index_kind = dw_index_kind::GDB_INDEX;
5966 dwarf2_per_objfile->resize_symtabs ();
5967 return true;
5968 }
5969
5970 /* ... otherwise, try to find the index in the index cache. */
5971 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5972 get_gdb_index_contents_from_cache,
5973 get_gdb_index_contents_from_cache_dwz))
5974 {
5975 global_index_cache.hit ();
5976 *index_kind = dw_index_kind::GDB_INDEX;
5977 dwarf2_per_objfile->resize_symtabs ();
5978 return true;
5979 }
5980
5981 global_index_cache.miss ();
5982 return false;
5983 }
5984
5985 \f
5986
5987 /* Build a partial symbol table. */
5988
5989 void
5990 dwarf2_build_psymtabs (struct objfile *objfile)
5991 {
5992 struct dwarf2_per_objfile *dwarf2_per_objfile
5993 = get_dwarf2_per_objfile (objfile);
5994
5995 init_psymbol_list (objfile, 1024);
5996
5997 try
5998 {
5999 /* This isn't really ideal: all the data we allocate on the
6000 objfile's obstack is still uselessly kept around. However,
6001 freeing it seems unsafe. */
6002 psymtab_discarder psymtabs (objfile);
6003 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6004 psymtabs.keep ();
6005
6006 dwarf2_per_objfile->resize_symtabs ();
6007
6008 /* (maybe) store an index in the cache. */
6009 global_index_cache.store (dwarf2_per_objfile);
6010 }
6011 catch (const gdb_exception_error &except)
6012 {
6013 exception_print (gdb_stderr, except);
6014 }
6015 }
6016
6017 /* Find the base address of the compilation unit for range lists and
6018 location lists. It will normally be specified by DW_AT_low_pc.
6019 In DWARF-3 draft 4, the base address could be overridden by
6020 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6021 compilation units with discontinuous ranges. */
6022
6023 static void
6024 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6025 {
6026 struct attribute *attr;
6027
6028 cu->base_address.reset ();
6029
6030 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6031 if (attr != nullptr)
6032 cu->base_address = attr->value_as_address ();
6033 else
6034 {
6035 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6036 if (attr != nullptr)
6037 cu->base_address = attr->value_as_address ();
6038 }
6039 }
6040
6041 /* Helper function that returns the proper abbrev section for
6042 THIS_CU. */
6043
6044 static struct dwarf2_section_info *
6045 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6046 {
6047 struct dwarf2_section_info *abbrev;
6048 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6049
6050 if (this_cu->is_dwz)
6051 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6052 else
6053 abbrev = &per_bfd->abbrev;
6054
6055 return abbrev;
6056 }
6057
6058 /* Fetch the abbreviation table offset from a comp or type unit header. */
6059
6060 static sect_offset
6061 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6062 struct dwarf2_section_info *section,
6063 sect_offset sect_off)
6064 {
6065 bfd *abfd = section->get_bfd_owner ();
6066 const gdb_byte *info_ptr;
6067 unsigned int initial_length_size, offset_size;
6068 uint16_t version;
6069
6070 section->read (dwarf2_per_objfile->objfile);
6071 info_ptr = section->buffer + to_underlying (sect_off);
6072 read_initial_length (abfd, info_ptr, &initial_length_size);
6073 offset_size = initial_length_size == 4 ? 4 : 8;
6074 info_ptr += initial_length_size;
6075
6076 version = read_2_bytes (abfd, info_ptr);
6077 info_ptr += 2;
6078 if (version >= 5)
6079 {
6080 /* Skip unit type and address size. */
6081 info_ptr += 2;
6082 }
6083
6084 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6085 }
6086
6087 /* A partial symtab that is used only for include files. */
6088 struct dwarf2_include_psymtab : public partial_symtab
6089 {
6090 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6091 : partial_symtab (filename, objfile)
6092 {
6093 }
6094
6095 void read_symtab (struct objfile *objfile) override
6096 {
6097 /* It's an include file, no symbols to read for it.
6098 Everything is in the includer symtab. */
6099
6100 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6101 expansion of the includer psymtab. We use the dependencies[0] field to
6102 model the includer. But if we go the regular route of calling
6103 expand_psymtab here, and having expand_psymtab call expand_dependencies
6104 to expand the includer, we'll only use expand_psymtab on the includer
6105 (making it a non-toplevel psymtab), while if we expand the includer via
6106 another path, we'll use read_symtab (making it a toplevel psymtab).
6107 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6108 psymtab, and trigger read_symtab on the includer here directly. */
6109 includer ()->read_symtab (objfile);
6110 }
6111
6112 void expand_psymtab (struct objfile *objfile) override
6113 {
6114 /* This is not called by read_symtab, and should not be called by any
6115 expand_dependencies. */
6116 gdb_assert (false);
6117 }
6118
6119 bool readin_p (struct objfile *objfile) const override
6120 {
6121 return includer ()->readin_p (objfile);
6122 }
6123
6124 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6125 {
6126 return nullptr;
6127 }
6128
6129 private:
6130 partial_symtab *includer () const
6131 {
6132 /* An include psymtab has exactly one dependency: the psymtab that
6133 includes it. */
6134 gdb_assert (this->number_of_dependencies == 1);
6135 return this->dependencies[0];
6136 }
6137 };
6138
6139 /* Allocate a new partial symtab for file named NAME and mark this new
6140 partial symtab as being an include of PST. */
6141
6142 static void
6143 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6144 struct objfile *objfile)
6145 {
6146 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6147
6148 if (!IS_ABSOLUTE_PATH (subpst->filename))
6149 subpst->dirname = pst->dirname;
6150
6151 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6152 subpst->dependencies[0] = pst;
6153 subpst->number_of_dependencies = 1;
6154 }
6155
6156 /* Read the Line Number Program data and extract the list of files
6157 included by the source file represented by PST. Build an include
6158 partial symtab for each of these included files. */
6159
6160 static void
6161 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6162 struct die_info *die,
6163 dwarf2_psymtab *pst)
6164 {
6165 line_header_up lh;
6166 struct attribute *attr;
6167
6168 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6169 if (attr != nullptr)
6170 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6171 if (lh == NULL)
6172 return; /* No linetable, so no includes. */
6173
6174 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6175 that we pass in the raw text_low here; that is ok because we're
6176 only decoding the line table to make include partial symtabs, and
6177 so the addresses aren't really used. */
6178 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6179 pst->raw_text_low (), 1);
6180 }
6181
6182 static hashval_t
6183 hash_signatured_type (const void *item)
6184 {
6185 const struct signatured_type *sig_type
6186 = (const struct signatured_type *) item;
6187
6188 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6189 return sig_type->signature;
6190 }
6191
6192 static int
6193 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6194 {
6195 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6196 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6197
6198 return lhs->signature == rhs->signature;
6199 }
6200
6201 /* Allocate a hash table for signatured types. */
6202
6203 static htab_up
6204 allocate_signatured_type_table ()
6205 {
6206 return htab_up (htab_create_alloc (41,
6207 hash_signatured_type,
6208 eq_signatured_type,
6209 NULL, xcalloc, xfree));
6210 }
6211
6212 /* A helper function to add a signatured type CU to a table. */
6213
6214 static int
6215 add_signatured_type_cu_to_table (void **slot, void *datum)
6216 {
6217 struct signatured_type *sigt = (struct signatured_type *) *slot;
6218 std::vector<signatured_type *> *all_type_units
6219 = (std::vector<signatured_type *> *) datum;
6220
6221 all_type_units->push_back (sigt);
6222
6223 return 1;
6224 }
6225
6226 /* A helper for create_debug_types_hash_table. Read types from SECTION
6227 and fill them into TYPES_HTAB. It will process only type units,
6228 therefore DW_UT_type. */
6229
6230 static void
6231 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6232 struct dwo_file *dwo_file,
6233 dwarf2_section_info *section, htab_up &types_htab,
6234 rcuh_kind section_kind)
6235 {
6236 struct objfile *objfile = dwarf2_per_objfile->objfile;
6237 struct dwarf2_section_info *abbrev_section;
6238 bfd *abfd;
6239 const gdb_byte *info_ptr, *end_ptr;
6240
6241 abbrev_section = (dwo_file != NULL
6242 ? &dwo_file->sections.abbrev
6243 : &dwarf2_per_objfile->per_bfd->abbrev);
6244
6245 if (dwarf_read_debug)
6246 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6247 section->get_name (),
6248 abbrev_section->get_file_name ());
6249
6250 section->read (objfile);
6251 info_ptr = section->buffer;
6252
6253 if (info_ptr == NULL)
6254 return;
6255
6256 /* We can't set abfd until now because the section may be empty or
6257 not present, in which case the bfd is unknown. */
6258 abfd = section->get_bfd_owner ();
6259
6260 /* We don't use cutu_reader here because we don't need to read
6261 any dies: the signature is in the header. */
6262
6263 end_ptr = info_ptr + section->size;
6264 while (info_ptr < end_ptr)
6265 {
6266 struct signatured_type *sig_type;
6267 struct dwo_unit *dwo_tu;
6268 void **slot;
6269 const gdb_byte *ptr = info_ptr;
6270 struct comp_unit_head header;
6271 unsigned int length;
6272
6273 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6274
6275 /* Initialize it due to a false compiler warning. */
6276 header.signature = -1;
6277 header.type_cu_offset_in_tu = (cu_offset) -1;
6278
6279 /* We need to read the type's signature in order to build the hash
6280 table, but we don't need anything else just yet. */
6281
6282 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6283 abbrev_section, ptr, section_kind);
6284
6285 length = header.get_length ();
6286
6287 /* Skip dummy type units. */
6288 if (ptr >= info_ptr + length
6289 || peek_abbrev_code (abfd, ptr) == 0
6290 || header.unit_type != DW_UT_type)
6291 {
6292 info_ptr += length;
6293 continue;
6294 }
6295
6296 if (types_htab == NULL)
6297 {
6298 if (dwo_file)
6299 types_htab = allocate_dwo_unit_table ();
6300 else
6301 types_htab = allocate_signatured_type_table ();
6302 }
6303
6304 if (dwo_file)
6305 {
6306 sig_type = NULL;
6307 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6308 struct dwo_unit);
6309 dwo_tu->dwo_file = dwo_file;
6310 dwo_tu->signature = header.signature;
6311 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6312 dwo_tu->section = section;
6313 dwo_tu->sect_off = sect_off;
6314 dwo_tu->length = length;
6315 }
6316 else
6317 {
6318 /* N.B.: type_offset is not usable if this type uses a DWO file.
6319 The real type_offset is in the DWO file. */
6320 dwo_tu = NULL;
6321 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6322 sig_type->signature = header.signature;
6323 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6324 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6325 sig_type->per_cu.is_debug_types = 1;
6326 sig_type->per_cu.section = section;
6327 sig_type->per_cu.sect_off = sect_off;
6328 sig_type->per_cu.length = length;
6329 }
6330
6331 slot = htab_find_slot (types_htab.get (),
6332 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6333 INSERT);
6334 gdb_assert (slot != NULL);
6335 if (*slot != NULL)
6336 {
6337 sect_offset dup_sect_off;
6338
6339 if (dwo_file)
6340 {
6341 const struct dwo_unit *dup_tu
6342 = (const struct dwo_unit *) *slot;
6343
6344 dup_sect_off = dup_tu->sect_off;
6345 }
6346 else
6347 {
6348 const struct signatured_type *dup_tu
6349 = (const struct signatured_type *) *slot;
6350
6351 dup_sect_off = dup_tu->per_cu.sect_off;
6352 }
6353
6354 complaint (_("debug type entry at offset %s is duplicate to"
6355 " the entry at offset %s, signature %s"),
6356 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6357 hex_string (header.signature));
6358 }
6359 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6360
6361 if (dwarf_read_debug > 1)
6362 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6363 sect_offset_str (sect_off),
6364 hex_string (header.signature));
6365
6366 info_ptr += length;
6367 }
6368 }
6369
6370 /* Create the hash table of all entries in the .debug_types
6371 (or .debug_types.dwo) section(s).
6372 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6373 otherwise it is NULL.
6374
6375 The result is a pointer to the hash table or NULL if there are no types.
6376
6377 Note: This function processes DWO files only, not DWP files. */
6378
6379 static void
6380 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6381 struct dwo_file *dwo_file,
6382 gdb::array_view<dwarf2_section_info> type_sections,
6383 htab_up &types_htab)
6384 {
6385 for (dwarf2_section_info &section : type_sections)
6386 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6387 types_htab, rcuh_kind::TYPE);
6388 }
6389
6390 /* Create the hash table of all entries in the .debug_types section,
6391 and initialize all_type_units.
6392 The result is zero if there is an error (e.g. missing .debug_types section),
6393 otherwise non-zero. */
6394
6395 static int
6396 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6397 {
6398 htab_up types_htab;
6399
6400 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6401 &dwarf2_per_objfile->per_bfd->info, types_htab,
6402 rcuh_kind::COMPILE);
6403 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6404 dwarf2_per_objfile->per_bfd->types, types_htab);
6405 if (types_htab == NULL)
6406 {
6407 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6408 return 0;
6409 }
6410
6411 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6412
6413 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6414 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6415 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6416
6417 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6418 add_signatured_type_cu_to_table,
6419 &dwarf2_per_objfile->per_bfd->all_type_units);
6420
6421 return 1;
6422 }
6423
6424 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6425 If SLOT is non-NULL, it is the entry to use in the hash table.
6426 Otherwise we find one. */
6427
6428 static struct signatured_type *
6429 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6430 void **slot)
6431 {
6432 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6433 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6434 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6435
6436 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6437
6438 dwarf2_per_objfile->resize_symtabs ();
6439
6440 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6441 sig_type->signature = sig;
6442 sig_type->per_cu.is_debug_types = 1;
6443 if (dwarf2_per_objfile->per_bfd->using_index)
6444 {
6445 sig_type->per_cu.v.quick =
6446 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6447 struct dwarf2_per_cu_quick_data);
6448 }
6449
6450 if (slot == NULL)
6451 {
6452 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6453 sig_type, INSERT);
6454 }
6455 gdb_assert (*slot == NULL);
6456 *slot = sig_type;
6457 /* The rest of sig_type must be filled in by the caller. */
6458 return sig_type;
6459 }
6460
6461 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6462 Fill in SIG_ENTRY with DWO_ENTRY. */
6463
6464 static void
6465 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6466 struct signatured_type *sig_entry,
6467 struct dwo_unit *dwo_entry)
6468 {
6469 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6470
6471 /* Make sure we're not clobbering something we don't expect to. */
6472 gdb_assert (! sig_entry->per_cu.queued);
6473 gdb_assert (sig_entry->per_cu.cu == NULL);
6474 if (per_bfd->using_index)
6475 {
6476 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6477 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6478 }
6479 else
6480 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6481 gdb_assert (sig_entry->signature == dwo_entry->signature);
6482 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6483 gdb_assert (sig_entry->type_unit_group == NULL);
6484 gdb_assert (sig_entry->dwo_unit == NULL);
6485
6486 sig_entry->per_cu.section = dwo_entry->section;
6487 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6488 sig_entry->per_cu.length = dwo_entry->length;
6489 sig_entry->per_cu.reading_dwo_directly = 1;
6490 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6491 sig_entry->per_cu.per_bfd = per_bfd;
6492 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6493 sig_entry->dwo_unit = dwo_entry;
6494 }
6495
6496 /* Subroutine of lookup_signatured_type.
6497 If we haven't read the TU yet, create the signatured_type data structure
6498 for a TU to be read in directly from a DWO file, bypassing the stub.
6499 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6500 using .gdb_index, then when reading a CU we want to stay in the DWO file
6501 containing that CU. Otherwise we could end up reading several other DWO
6502 files (due to comdat folding) to process the transitive closure of all the
6503 mentioned TUs, and that can be slow. The current DWO file will have every
6504 type signature that it needs.
6505 We only do this for .gdb_index because in the psymtab case we already have
6506 to read all the DWOs to build the type unit groups. */
6507
6508 static struct signatured_type *
6509 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6510 {
6511 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6512 struct dwo_file *dwo_file;
6513 struct dwo_unit find_dwo_entry, *dwo_entry;
6514 struct signatured_type find_sig_entry, *sig_entry;
6515 void **slot;
6516
6517 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6518
6519 /* If TU skeletons have been removed then we may not have read in any
6520 TUs yet. */
6521 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6522 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6523
6524 /* We only ever need to read in one copy of a signatured type.
6525 Use the global signatured_types array to do our own comdat-folding
6526 of types. If this is the first time we're reading this TU, and
6527 the TU has an entry in .gdb_index, replace the recorded data from
6528 .gdb_index with this TU. */
6529
6530 find_sig_entry.signature = sig;
6531 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6532 &find_sig_entry, INSERT);
6533 sig_entry = (struct signatured_type *) *slot;
6534
6535 /* We can get here with the TU already read, *or* in the process of being
6536 read. Don't reassign the global entry to point to this DWO if that's
6537 the case. Also note that if the TU is already being read, it may not
6538 have come from a DWO, the program may be a mix of Fission-compiled
6539 code and non-Fission-compiled code. */
6540
6541 /* Have we already tried to read this TU?
6542 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6543 needn't exist in the global table yet). */
6544 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6545 return sig_entry;
6546
6547 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6548 dwo_unit of the TU itself. */
6549 dwo_file = cu->dwo_unit->dwo_file;
6550
6551 /* Ok, this is the first time we're reading this TU. */
6552 if (dwo_file->tus == NULL)
6553 return NULL;
6554 find_dwo_entry.signature = sig;
6555 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6556 &find_dwo_entry);
6557 if (dwo_entry == NULL)
6558 return NULL;
6559
6560 /* If the global table doesn't have an entry for this TU, add one. */
6561 if (sig_entry == NULL)
6562 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6563
6564 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6565 sig_entry->per_cu.tu_read = 1;
6566 return sig_entry;
6567 }
6568
6569 /* Subroutine of lookup_signatured_type.
6570 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6571 then try the DWP file. If the TU stub (skeleton) has been removed then
6572 it won't be in .gdb_index. */
6573
6574 static struct signatured_type *
6575 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6576 {
6577 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6578 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6579 struct dwo_unit *dwo_entry;
6580 struct signatured_type find_sig_entry, *sig_entry;
6581 void **slot;
6582
6583 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6584 gdb_assert (dwp_file != NULL);
6585
6586 /* If TU skeletons have been removed then we may not have read in any
6587 TUs yet. */
6588 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6589 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6590
6591 find_sig_entry.signature = sig;
6592 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6593 &find_sig_entry, INSERT);
6594 sig_entry = (struct signatured_type *) *slot;
6595
6596 /* Have we already tried to read this TU?
6597 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6598 needn't exist in the global table yet). */
6599 if (sig_entry != NULL)
6600 return sig_entry;
6601
6602 if (dwp_file->tus == NULL)
6603 return NULL;
6604 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6605 sig, 1 /* is_debug_types */);
6606 if (dwo_entry == NULL)
6607 return NULL;
6608
6609 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6610 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6611
6612 return sig_entry;
6613 }
6614
6615 /* Lookup a signature based type for DW_FORM_ref_sig8.
6616 Returns NULL if signature SIG is not present in the table.
6617 It is up to the caller to complain about this. */
6618
6619 static struct signatured_type *
6620 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6621 {
6622 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6623
6624 if (cu->dwo_unit
6625 && dwarf2_per_objfile->per_bfd->using_index)
6626 {
6627 /* We're in a DWO/DWP file, and we're using .gdb_index.
6628 These cases require special processing. */
6629 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6630 return lookup_dwo_signatured_type (cu, sig);
6631 else
6632 return lookup_dwp_signatured_type (cu, sig);
6633 }
6634 else
6635 {
6636 struct signatured_type find_entry, *entry;
6637
6638 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6639 return NULL;
6640 find_entry.signature = sig;
6641 entry = ((struct signatured_type *)
6642 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6643 &find_entry));
6644 return entry;
6645 }
6646 }
6647
6648 /* Low level DIE reading support. */
6649
6650 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6651
6652 static void
6653 init_cu_die_reader (struct die_reader_specs *reader,
6654 struct dwarf2_cu *cu,
6655 struct dwarf2_section_info *section,
6656 struct dwo_file *dwo_file,
6657 struct abbrev_table *abbrev_table)
6658 {
6659 gdb_assert (section->readin && section->buffer != NULL);
6660 reader->abfd = section->get_bfd_owner ();
6661 reader->cu = cu;
6662 reader->dwo_file = dwo_file;
6663 reader->die_section = section;
6664 reader->buffer = section->buffer;
6665 reader->buffer_end = section->buffer + section->size;
6666 reader->abbrev_table = abbrev_table;
6667 }
6668
6669 /* Subroutine of cutu_reader to simplify it.
6670 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6671 There's just a lot of work to do, and cutu_reader is big enough
6672 already.
6673
6674 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6675 from it to the DIE in the DWO. If NULL we are skipping the stub.
6676 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6677 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6678 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6679 STUB_COMP_DIR may be non-NULL.
6680 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6681 are filled in with the info of the DIE from the DWO file.
6682 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6683 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6684 kept around for at least as long as *RESULT_READER.
6685
6686 The result is non-zero if a valid (non-dummy) DIE was found. */
6687
6688 static int
6689 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6690 struct dwo_unit *dwo_unit,
6691 struct die_info *stub_comp_unit_die,
6692 const char *stub_comp_dir,
6693 struct die_reader_specs *result_reader,
6694 const gdb_byte **result_info_ptr,
6695 struct die_info **result_comp_unit_die,
6696 abbrev_table_up *result_dwo_abbrev_table)
6697 {
6698 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6699 struct objfile *objfile = dwarf2_per_objfile->objfile;
6700 struct dwarf2_cu *cu = this_cu->cu;
6701 bfd *abfd;
6702 const gdb_byte *begin_info_ptr, *info_ptr;
6703 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6704 int i,num_extra_attrs;
6705 struct dwarf2_section_info *dwo_abbrev_section;
6706 struct die_info *comp_unit_die;
6707
6708 /* At most one of these may be provided. */
6709 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6710
6711 /* These attributes aren't processed until later:
6712 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6713 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6714 referenced later. However, these attributes are found in the stub
6715 which we won't have later. In order to not impose this complication
6716 on the rest of the code, we read them here and copy them to the
6717 DWO CU/TU die. */
6718
6719 stmt_list = NULL;
6720 low_pc = NULL;
6721 high_pc = NULL;
6722 ranges = NULL;
6723 comp_dir = NULL;
6724
6725 if (stub_comp_unit_die != NULL)
6726 {
6727 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6728 DWO file. */
6729 if (! this_cu->is_debug_types)
6730 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6731 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6732 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6733 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6734 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6735
6736 cu->addr_base = stub_comp_unit_die->addr_base ();
6737
6738 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6739 here (if needed). We need the value before we can process
6740 DW_AT_ranges. */
6741 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6742 }
6743 else if (stub_comp_dir != NULL)
6744 {
6745 /* Reconstruct the comp_dir attribute to simplify the code below. */
6746 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6747 comp_dir->name = DW_AT_comp_dir;
6748 comp_dir->form = DW_FORM_string;
6749 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6750 DW_STRING (comp_dir) = stub_comp_dir;
6751 }
6752
6753 /* Set up for reading the DWO CU/TU. */
6754 cu->dwo_unit = dwo_unit;
6755 dwarf2_section_info *section = dwo_unit->section;
6756 section->read (objfile);
6757 abfd = section->get_bfd_owner ();
6758 begin_info_ptr = info_ptr = (section->buffer
6759 + to_underlying (dwo_unit->sect_off));
6760 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6761
6762 if (this_cu->is_debug_types)
6763 {
6764 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6765
6766 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6767 &cu->header, section,
6768 dwo_abbrev_section,
6769 info_ptr, rcuh_kind::TYPE);
6770 /* This is not an assert because it can be caused by bad debug info. */
6771 if (sig_type->signature != cu->header.signature)
6772 {
6773 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6774 " TU at offset %s [in module %s]"),
6775 hex_string (sig_type->signature),
6776 hex_string (cu->header.signature),
6777 sect_offset_str (dwo_unit->sect_off),
6778 bfd_get_filename (abfd));
6779 }
6780 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6781 /* For DWOs coming from DWP files, we don't know the CU length
6782 nor the type's offset in the TU until now. */
6783 dwo_unit->length = cu->header.get_length ();
6784 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6785
6786 /* Establish the type offset that can be used to lookup the type.
6787 For DWO files, we don't know it until now. */
6788 sig_type->type_offset_in_section
6789 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6790 }
6791 else
6792 {
6793 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6794 &cu->header, section,
6795 dwo_abbrev_section,
6796 info_ptr, rcuh_kind::COMPILE);
6797 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6798 /* For DWOs coming from DWP files, we don't know the CU length
6799 until now. */
6800 dwo_unit->length = cu->header.get_length ();
6801 }
6802
6803 *result_dwo_abbrev_table
6804 = abbrev_table::read (objfile, dwo_abbrev_section,
6805 cu->header.abbrev_sect_off);
6806 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6807 result_dwo_abbrev_table->get ());
6808
6809 /* Read in the die, but leave space to copy over the attributes
6810 from the stub. This has the benefit of simplifying the rest of
6811 the code - all the work to maintain the illusion of a single
6812 DW_TAG_{compile,type}_unit DIE is done here. */
6813 num_extra_attrs = ((stmt_list != NULL)
6814 + (low_pc != NULL)
6815 + (high_pc != NULL)
6816 + (ranges != NULL)
6817 + (comp_dir != NULL));
6818 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6819 num_extra_attrs);
6820
6821 /* Copy over the attributes from the stub to the DIE we just read in. */
6822 comp_unit_die = *result_comp_unit_die;
6823 i = comp_unit_die->num_attrs;
6824 if (stmt_list != NULL)
6825 comp_unit_die->attrs[i++] = *stmt_list;
6826 if (low_pc != NULL)
6827 comp_unit_die->attrs[i++] = *low_pc;
6828 if (high_pc != NULL)
6829 comp_unit_die->attrs[i++] = *high_pc;
6830 if (ranges != NULL)
6831 comp_unit_die->attrs[i++] = *ranges;
6832 if (comp_dir != NULL)
6833 comp_unit_die->attrs[i++] = *comp_dir;
6834 comp_unit_die->num_attrs += num_extra_attrs;
6835
6836 if (dwarf_die_debug)
6837 {
6838 fprintf_unfiltered (gdb_stdlog,
6839 "Read die from %s@0x%x of %s:\n",
6840 section->get_name (),
6841 (unsigned) (begin_info_ptr - section->buffer),
6842 bfd_get_filename (abfd));
6843 dump_die (comp_unit_die, dwarf_die_debug);
6844 }
6845
6846 /* Skip dummy compilation units. */
6847 if (info_ptr >= begin_info_ptr + dwo_unit->length
6848 || peek_abbrev_code (abfd, info_ptr) == 0)
6849 return 0;
6850
6851 *result_info_ptr = info_ptr;
6852 return 1;
6853 }
6854
6855 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6856 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6857 signature is part of the header. */
6858 static gdb::optional<ULONGEST>
6859 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6860 {
6861 if (cu->header.version >= 5)
6862 return cu->header.signature;
6863 struct attribute *attr;
6864 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6865 if (attr == nullptr)
6866 return gdb::optional<ULONGEST> ();
6867 return DW_UNSND (attr);
6868 }
6869
6870 /* Subroutine of cutu_reader to simplify it.
6871 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6872 Returns NULL if the specified DWO unit cannot be found. */
6873
6874 static struct dwo_unit *
6875 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6876 struct die_info *comp_unit_die,
6877 const char *dwo_name)
6878 {
6879 struct dwarf2_cu *cu = this_cu->cu;
6880 struct dwo_unit *dwo_unit;
6881 const char *comp_dir;
6882
6883 gdb_assert (cu != NULL);
6884
6885 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6886 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6887 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6888
6889 if (this_cu->is_debug_types)
6890 {
6891 struct signatured_type *sig_type;
6892
6893 /* Since this_cu is the first member of struct signatured_type,
6894 we can go from a pointer to one to a pointer to the other. */
6895 sig_type = (struct signatured_type *) this_cu;
6896 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6897 }
6898 else
6899 {
6900 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6901 if (!signature.has_value ())
6902 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6903 " [in module %s]"),
6904 dwo_name, bfd_get_filename (this_cu->per_bfd->obfd));
6905 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6906 *signature);
6907 }
6908
6909 return dwo_unit;
6910 }
6911
6912 /* Subroutine of cutu_reader to simplify it.
6913 See it for a description of the parameters.
6914 Read a TU directly from a DWO file, bypassing the stub. */
6915
6916 void
6917 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6918 dwarf2_per_objfile *per_objfile,
6919 int use_existing_cu)
6920 {
6921 struct signatured_type *sig_type;
6922
6923 /* Verify we can do the following downcast, and that we have the
6924 data we need. */
6925 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6926 sig_type = (struct signatured_type *) this_cu;
6927 gdb_assert (sig_type->dwo_unit != NULL);
6928
6929 if (use_existing_cu && this_cu->cu != NULL)
6930 {
6931 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6932 /* There's no need to do the rereading_dwo_cu handling that
6933 cutu_reader does since we don't read the stub. */
6934 }
6935 else
6936 {
6937 /* If !use_existing_cu, this_cu->cu must be NULL. */
6938 gdb_assert (this_cu->cu == NULL);
6939 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6940 }
6941
6942 /* A future optimization, if needed, would be to use an existing
6943 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6944 could share abbrev tables. */
6945
6946 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6947 NULL /* stub_comp_unit_die */,
6948 sig_type->dwo_unit->dwo_file->comp_dir,
6949 this, &info_ptr,
6950 &comp_unit_die,
6951 &m_dwo_abbrev_table) == 0)
6952 {
6953 /* Dummy die. */
6954 dummy_p = true;
6955 }
6956 }
6957
6958 /* Initialize a CU (or TU) and read its DIEs.
6959 If the CU defers to a DWO file, read the DWO file as well.
6960
6961 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6962 Otherwise the table specified in the comp unit header is read in and used.
6963 This is an optimization for when we already have the abbrev table.
6964
6965 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6966 Otherwise, a new CU is allocated with xmalloc. */
6967
6968 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6969 dwarf2_per_objfile *dwarf2_per_objfile,
6970 struct abbrev_table *abbrev_table,
6971 int use_existing_cu,
6972 bool skip_partial)
6973 : die_reader_specs {},
6974 m_this_cu (this_cu)
6975 {
6976 struct objfile *objfile = dwarf2_per_objfile->objfile;
6977 struct dwarf2_section_info *section = this_cu->section;
6978 bfd *abfd = section->get_bfd_owner ();
6979 struct dwarf2_cu *cu;
6980 const gdb_byte *begin_info_ptr;
6981 struct signatured_type *sig_type = NULL;
6982 struct dwarf2_section_info *abbrev_section;
6983 /* Non-zero if CU currently points to a DWO file and we need to
6984 reread it. When this happens we need to reread the skeleton die
6985 before we can reread the DWO file (this only applies to CUs, not TUs). */
6986 int rereading_dwo_cu = 0;
6987
6988 if (dwarf_die_debug)
6989 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6990 this_cu->is_debug_types ? "type" : "comp",
6991 sect_offset_str (this_cu->sect_off));
6992
6993 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6994 file (instead of going through the stub), short-circuit all of this. */
6995 if (this_cu->reading_dwo_directly)
6996 {
6997 /* Narrow down the scope of possibilities to have to understand. */
6998 gdb_assert (this_cu->is_debug_types);
6999 gdb_assert (abbrev_table == NULL);
7000 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
7001 return;
7002 }
7003
7004 /* This is cheap if the section is already read in. */
7005 section->read (objfile);
7006
7007 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7008
7009 abbrev_section = get_abbrev_section_for_cu (this_cu);
7010
7011 if (use_existing_cu && this_cu->cu != NULL)
7012 {
7013 cu = this_cu->cu;
7014 /* If this CU is from a DWO file we need to start over, we need to
7015 refetch the attributes from the skeleton CU.
7016 This could be optimized by retrieving those attributes from when we
7017 were here the first time: the previous comp_unit_die was stored in
7018 comp_unit_obstack. But there's no data yet that we need this
7019 optimization. */
7020 if (cu->dwo_unit != NULL)
7021 rereading_dwo_cu = 1;
7022 }
7023 else
7024 {
7025 /* If !use_existing_cu, this_cu->cu must be NULL. */
7026 gdb_assert (this_cu->cu == NULL);
7027 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7028 cu = m_new_cu.get ();
7029 }
7030
7031 /* Get the header. */
7032 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7033 {
7034 /* We already have the header, there's no need to read it in again. */
7035 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7036 }
7037 else
7038 {
7039 if (this_cu->is_debug_types)
7040 {
7041 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7042 &cu->header, section,
7043 abbrev_section, info_ptr,
7044 rcuh_kind::TYPE);
7045
7046 /* Since per_cu is the first member of struct signatured_type,
7047 we can go from a pointer to one to a pointer to the other. */
7048 sig_type = (struct signatured_type *) this_cu;
7049 gdb_assert (sig_type->signature == cu->header.signature);
7050 gdb_assert (sig_type->type_offset_in_tu
7051 == cu->header.type_cu_offset_in_tu);
7052 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7053
7054 /* LENGTH has not been set yet for type units if we're
7055 using .gdb_index. */
7056 this_cu->length = cu->header.get_length ();
7057
7058 /* Establish the type offset that can be used to lookup the type. */
7059 sig_type->type_offset_in_section =
7060 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7061
7062 this_cu->dwarf_version = cu->header.version;
7063 }
7064 else
7065 {
7066 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7067 &cu->header, section,
7068 abbrev_section,
7069 info_ptr,
7070 rcuh_kind::COMPILE);
7071
7072 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7073 if (this_cu->length == 0)
7074 this_cu->length = cu->header.get_length ();
7075 else
7076 gdb_assert (this_cu->length == cu->header.get_length ());
7077 this_cu->dwarf_version = cu->header.version;
7078 }
7079 }
7080
7081 /* Skip dummy compilation units. */
7082 if (info_ptr >= begin_info_ptr + this_cu->length
7083 || peek_abbrev_code (abfd, info_ptr) == 0)
7084 {
7085 dummy_p = true;
7086 return;
7087 }
7088
7089 /* If we don't have them yet, read the abbrevs for this compilation unit.
7090 And if we need to read them now, make sure they're freed when we're
7091 done. */
7092 if (abbrev_table != NULL)
7093 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7094 else
7095 {
7096 m_abbrev_table_holder
7097 = abbrev_table::read (objfile, abbrev_section,
7098 cu->header.abbrev_sect_off);
7099 abbrev_table = m_abbrev_table_holder.get ();
7100 }
7101
7102 /* Read the top level CU/TU die. */
7103 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7104 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7105
7106 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7107 {
7108 dummy_p = true;
7109 return;
7110 }
7111
7112 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7113 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7114 table from the DWO file and pass the ownership over to us. It will be
7115 referenced from READER, so we must make sure to free it after we're done
7116 with READER.
7117
7118 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7119 DWO CU, that this test will fail (the attribute will not be present). */
7120 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7121 if (dwo_name != nullptr)
7122 {
7123 struct dwo_unit *dwo_unit;
7124 struct die_info *dwo_comp_unit_die;
7125
7126 if (comp_unit_die->has_children)
7127 {
7128 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7129 " has children (offset %s) [in module %s]"),
7130 sect_offset_str (this_cu->sect_off),
7131 bfd_get_filename (abfd));
7132 }
7133 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7134 if (dwo_unit != NULL)
7135 {
7136 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7137 comp_unit_die, NULL,
7138 this, &info_ptr,
7139 &dwo_comp_unit_die,
7140 &m_dwo_abbrev_table) == 0)
7141 {
7142 /* Dummy die. */
7143 dummy_p = true;
7144 return;
7145 }
7146 comp_unit_die = dwo_comp_unit_die;
7147 }
7148 else
7149 {
7150 /* Yikes, we couldn't find the rest of the DIE, we only have
7151 the stub. A complaint has already been logged. There's
7152 not much more we can do except pass on the stub DIE to
7153 die_reader_func. We don't want to throw an error on bad
7154 debug info. */
7155 }
7156 }
7157 }
7158
7159 void
7160 cutu_reader::keep ()
7161 {
7162 /* Done, clean up. */
7163 gdb_assert (!dummy_p);
7164 if (m_new_cu != NULL)
7165 {
7166 /* We know that m_this_cu->cu is set, since we are in the process of
7167 parsing the CU. */
7168 gdb_assert (m_this_cu->cu != nullptr);
7169 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7170
7171 /* Link this CU into read_in_chain. */
7172 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7173 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7174 /* The chain owns it now. */
7175 m_new_cu.release ();
7176 }
7177 }
7178
7179 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7180 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7181 assumed to have already done the lookup to find the DWO file).
7182
7183 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7184 THIS_CU->is_debug_types, but nothing else.
7185
7186 We fill in THIS_CU->length.
7187
7188 THIS_CU->cu is always freed when done.
7189 This is done in order to not leave THIS_CU->cu in a state where we have
7190 to care whether it refers to the "main" CU or the DWO CU.
7191
7192 When parent_cu is passed, it is used to provide a default value for
7193 str_offsets_base and addr_base from the parent. */
7194
7195 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7196 dwarf2_per_objfile *dwarf2_per_objfile,
7197 struct dwarf2_cu *parent_cu,
7198 struct dwo_file *dwo_file)
7199 : die_reader_specs {},
7200 m_this_cu (this_cu)
7201 {
7202 struct objfile *objfile = dwarf2_per_objfile->objfile;
7203 struct dwarf2_section_info *section = this_cu->section;
7204 bfd *abfd = section->get_bfd_owner ();
7205 struct dwarf2_section_info *abbrev_section;
7206 const gdb_byte *begin_info_ptr, *info_ptr;
7207
7208 if (dwarf_die_debug)
7209 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7210 this_cu->is_debug_types ? "type" : "comp",
7211 sect_offset_str (this_cu->sect_off));
7212
7213 gdb_assert (this_cu->cu == NULL);
7214
7215 abbrev_section = (dwo_file != NULL
7216 ? &dwo_file->sections.abbrev
7217 : get_abbrev_section_for_cu (this_cu));
7218
7219 /* This is cheap if the section is already read in. */
7220 section->read (objfile);
7221
7222 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7223
7224 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7225 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7226 &m_new_cu->header, section,
7227 abbrev_section, info_ptr,
7228 (this_cu->is_debug_types
7229 ? rcuh_kind::TYPE
7230 : rcuh_kind::COMPILE));
7231
7232 if (parent_cu != nullptr)
7233 {
7234 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7235 m_new_cu->addr_base = parent_cu->addr_base;
7236 }
7237 this_cu->length = m_new_cu->header.get_length ();
7238
7239 /* Skip dummy compilation units. */
7240 if (info_ptr >= begin_info_ptr + this_cu->length
7241 || peek_abbrev_code (abfd, info_ptr) == 0)
7242 {
7243 dummy_p = true;
7244 return;
7245 }
7246
7247 m_abbrev_table_holder
7248 = abbrev_table::read (objfile, abbrev_section,
7249 m_new_cu->header.abbrev_sect_off);
7250
7251 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7252 m_abbrev_table_holder.get ());
7253 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7254 }
7255
7256 \f
7257 /* Type Unit Groups.
7258
7259 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7260 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7261 so that all types coming from the same compilation (.o file) are grouped
7262 together. A future step could be to put the types in the same symtab as
7263 the CU the types ultimately came from. */
7264
7265 static hashval_t
7266 hash_type_unit_group (const void *item)
7267 {
7268 const struct type_unit_group *tu_group
7269 = (const struct type_unit_group *) item;
7270
7271 return hash_stmt_list_entry (&tu_group->hash);
7272 }
7273
7274 static int
7275 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7276 {
7277 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7278 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7279
7280 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7281 }
7282
7283 /* Allocate a hash table for type unit groups. */
7284
7285 static htab_up
7286 allocate_type_unit_groups_table ()
7287 {
7288 return htab_up (htab_create_alloc (3,
7289 hash_type_unit_group,
7290 eq_type_unit_group,
7291 NULL, xcalloc, xfree));
7292 }
7293
7294 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7295 partial symtabs. We combine several TUs per psymtab to not let the size
7296 of any one psymtab grow too big. */
7297 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7298 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7299
7300 /* Helper routine for get_type_unit_group.
7301 Create the type_unit_group object used to hold one or more TUs. */
7302
7303 static struct type_unit_group *
7304 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7305 {
7306 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7307 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7308 struct dwarf2_per_cu_data *per_cu;
7309 struct type_unit_group *tu_group;
7310
7311 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7312 struct type_unit_group);
7313 per_cu = &tu_group->per_cu;
7314 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7315 per_cu->per_bfd = per_bfd;
7316
7317 if (per_bfd->using_index)
7318 {
7319 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7320 struct dwarf2_per_cu_quick_data);
7321 }
7322 else
7323 {
7324 unsigned int line_offset = to_underlying (line_offset_struct);
7325 dwarf2_psymtab *pst;
7326 std::string name;
7327
7328 /* Give the symtab a useful name for debug purposes. */
7329 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7330 name = string_printf ("<type_units_%d>",
7331 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7332 else
7333 name = string_printf ("<type_units_at_0x%x>", line_offset);
7334
7335 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7336 pst->anonymous = true;
7337 }
7338
7339 tu_group->hash.dwo_unit = cu->dwo_unit;
7340 tu_group->hash.line_sect_off = line_offset_struct;
7341
7342 return tu_group;
7343 }
7344
7345 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7346 STMT_LIST is a DW_AT_stmt_list attribute. */
7347
7348 static struct type_unit_group *
7349 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7350 {
7351 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7352 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7353 struct type_unit_group *tu_group;
7354 void **slot;
7355 unsigned int line_offset;
7356 struct type_unit_group type_unit_group_for_lookup;
7357
7358 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7359 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7360
7361 /* Do we need to create a new group, or can we use an existing one? */
7362
7363 if (stmt_list)
7364 {
7365 line_offset = DW_UNSND (stmt_list);
7366 ++tu_stats->nr_symtab_sharers;
7367 }
7368 else
7369 {
7370 /* Ugh, no stmt_list. Rare, but we have to handle it.
7371 We can do various things here like create one group per TU or
7372 spread them over multiple groups to split up the expansion work.
7373 To avoid worst case scenarios (too many groups or too large groups)
7374 we, umm, group them in bunches. */
7375 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7376 | (tu_stats->nr_stmt_less_type_units
7377 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7378 ++tu_stats->nr_stmt_less_type_units;
7379 }
7380
7381 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7382 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7383 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7384 &type_unit_group_for_lookup, INSERT);
7385 if (*slot != NULL)
7386 {
7387 tu_group = (struct type_unit_group *) *slot;
7388 gdb_assert (tu_group != NULL);
7389 }
7390 else
7391 {
7392 sect_offset line_offset_struct = (sect_offset) line_offset;
7393 tu_group = create_type_unit_group (cu, line_offset_struct);
7394 *slot = tu_group;
7395 ++tu_stats->nr_symtabs;
7396 }
7397
7398 return tu_group;
7399 }
7400 \f
7401 /* Partial symbol tables. */
7402
7403 /* Create a psymtab named NAME and assign it to PER_CU.
7404
7405 The caller must fill in the following details:
7406 dirname, textlow, texthigh. */
7407
7408 static dwarf2_psymtab *
7409 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7410 dwarf2_per_objfile *per_objfile,
7411 const char *name)
7412 {
7413 struct objfile *objfile = per_objfile->objfile;
7414 dwarf2_psymtab *pst;
7415
7416 pst = new dwarf2_psymtab (name, objfile, per_cu);
7417
7418 pst->psymtabs_addrmap_supported = true;
7419
7420 /* This is the glue that links PST into GDB's symbol API. */
7421 per_cu->v.psymtab = pst;
7422
7423 return pst;
7424 }
7425
7426 /* DIE reader function for process_psymtab_comp_unit. */
7427
7428 static void
7429 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7430 const gdb_byte *info_ptr,
7431 struct die_info *comp_unit_die,
7432 enum language pretend_language)
7433 {
7434 struct dwarf2_cu *cu = reader->cu;
7435 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7436 struct objfile *objfile = per_objfile->objfile;
7437 struct gdbarch *gdbarch = objfile->arch ();
7438 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7439 CORE_ADDR baseaddr;
7440 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7441 dwarf2_psymtab *pst;
7442 enum pc_bounds_kind cu_bounds_kind;
7443 const char *filename;
7444
7445 gdb_assert (! per_cu->is_debug_types);
7446
7447 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7448
7449 /* Allocate a new partial symbol table structure. */
7450 gdb::unique_xmalloc_ptr<char> debug_filename;
7451 static const char artificial[] = "<artificial>";
7452 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7453 if (filename == NULL)
7454 filename = "";
7455 else if (strcmp (filename, artificial) == 0)
7456 {
7457 debug_filename.reset (concat (artificial, "@",
7458 sect_offset_str (per_cu->sect_off),
7459 (char *) NULL));
7460 filename = debug_filename.get ();
7461 }
7462
7463 pst = create_partial_symtab (per_cu, per_objfile, filename);
7464
7465 /* This must be done before calling dwarf2_build_include_psymtabs. */
7466 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7467
7468 baseaddr = objfile->text_section_offset ();
7469
7470 dwarf2_find_base_address (comp_unit_die, cu);
7471
7472 /* Possibly set the default values of LOWPC and HIGHPC from
7473 `DW_AT_ranges'. */
7474 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7475 &best_highpc, cu, pst);
7476 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7477 {
7478 CORE_ADDR low
7479 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7480 - baseaddr);
7481 CORE_ADDR high
7482 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7483 - baseaddr - 1);
7484 /* Store the contiguous range if it is not empty; it can be
7485 empty for CUs with no code. */
7486 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7487 low, high, pst);
7488 }
7489
7490 /* Check if comp unit has_children.
7491 If so, read the rest of the partial symbols from this comp unit.
7492 If not, there's no more debug_info for this comp unit. */
7493 if (comp_unit_die->has_children)
7494 {
7495 struct partial_die_info *first_die;
7496 CORE_ADDR lowpc, highpc;
7497
7498 lowpc = ((CORE_ADDR) -1);
7499 highpc = ((CORE_ADDR) 0);
7500
7501 first_die = load_partial_dies (reader, info_ptr, 1);
7502
7503 scan_partial_symbols (first_die, &lowpc, &highpc,
7504 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7505
7506 /* If we didn't find a lowpc, set it to highpc to avoid
7507 complaints from `maint check'. */
7508 if (lowpc == ((CORE_ADDR) -1))
7509 lowpc = highpc;
7510
7511 /* If the compilation unit didn't have an explicit address range,
7512 then use the information extracted from its child dies. */
7513 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7514 {
7515 best_lowpc = lowpc;
7516 best_highpc = highpc;
7517 }
7518 }
7519 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7520 best_lowpc + baseaddr)
7521 - baseaddr);
7522 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7523 best_highpc + baseaddr)
7524 - baseaddr);
7525
7526 end_psymtab_common (objfile, pst);
7527
7528 if (!cu->per_cu->imported_symtabs_empty ())
7529 {
7530 int i;
7531 int len = cu->per_cu->imported_symtabs_size ();
7532
7533 /* Fill in 'dependencies' here; we fill in 'users' in a
7534 post-pass. */
7535 pst->number_of_dependencies = len;
7536 pst->dependencies
7537 = objfile->partial_symtabs->allocate_dependencies (len);
7538 for (i = 0; i < len; ++i)
7539 {
7540 pst->dependencies[i]
7541 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7542 }
7543
7544 cu->per_cu->imported_symtabs_free ();
7545 }
7546
7547 /* Get the list of files included in the current compilation unit,
7548 and build a psymtab for each of them. */
7549 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7550
7551 if (dwarf_read_debug)
7552 fprintf_unfiltered (gdb_stdlog,
7553 "Psymtab for %s unit @%s: %s - %s"
7554 ", %d global, %d static syms\n",
7555 per_cu->is_debug_types ? "type" : "comp",
7556 sect_offset_str (per_cu->sect_off),
7557 paddress (gdbarch, pst->text_low (objfile)),
7558 paddress (gdbarch, pst->text_high (objfile)),
7559 pst->n_global_syms, pst->n_static_syms);
7560 }
7561
7562 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7563 Process compilation unit THIS_CU for a psymtab. */
7564
7565 static void
7566 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7567 dwarf2_per_objfile *per_objfile,
7568 bool want_partial_unit,
7569 enum language pretend_language)
7570 {
7571 /* If this compilation unit was already read in, free the
7572 cached copy in order to read it in again. This is
7573 necessary because we skipped some symbols when we first
7574 read in the compilation unit (see load_partial_dies).
7575 This problem could be avoided, but the benefit is unclear. */
7576 if (this_cu->cu != NULL)
7577 free_one_cached_comp_unit (this_cu);
7578
7579 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
7580
7581 switch (reader.comp_unit_die->tag)
7582 {
7583 case DW_TAG_compile_unit:
7584 this_cu->unit_type = DW_UT_compile;
7585 break;
7586 case DW_TAG_partial_unit:
7587 this_cu->unit_type = DW_UT_partial;
7588 break;
7589 default:
7590 abort ();
7591 }
7592
7593 if (reader.dummy_p)
7594 {
7595 /* Nothing. */
7596 }
7597 else if (this_cu->is_debug_types)
7598 build_type_psymtabs_reader (&reader, reader.info_ptr,
7599 reader.comp_unit_die);
7600 else if (want_partial_unit
7601 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7602 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7603 reader.comp_unit_die,
7604 pretend_language);
7605
7606 this_cu->lang = this_cu->cu->language;
7607
7608 /* Age out any secondary CUs. */
7609 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7610 }
7611
7612 /* Reader function for build_type_psymtabs. */
7613
7614 static void
7615 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7616 const gdb_byte *info_ptr,
7617 struct die_info *type_unit_die)
7618 {
7619 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7620 struct objfile *objfile = dwarf2_per_objfile->objfile;
7621 struct dwarf2_cu *cu = reader->cu;
7622 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7623 struct signatured_type *sig_type;
7624 struct type_unit_group *tu_group;
7625 struct attribute *attr;
7626 struct partial_die_info *first_die;
7627 CORE_ADDR lowpc, highpc;
7628 dwarf2_psymtab *pst;
7629
7630 gdb_assert (per_cu->is_debug_types);
7631 sig_type = (struct signatured_type *) per_cu;
7632
7633 if (! type_unit_die->has_children)
7634 return;
7635
7636 attr = type_unit_die->attr (DW_AT_stmt_list);
7637 tu_group = get_type_unit_group (cu, attr);
7638
7639 if (tu_group->tus == nullptr)
7640 tu_group->tus = new std::vector<signatured_type *>;
7641 tu_group->tus->push_back (sig_type);
7642
7643 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7644 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7645 pst->anonymous = true;
7646
7647 first_die = load_partial_dies (reader, info_ptr, 1);
7648
7649 lowpc = (CORE_ADDR) -1;
7650 highpc = (CORE_ADDR) 0;
7651 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7652
7653 end_psymtab_common (objfile, pst);
7654 }
7655
7656 /* Struct used to sort TUs by their abbreviation table offset. */
7657
7658 struct tu_abbrev_offset
7659 {
7660 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7661 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7662 {}
7663
7664 signatured_type *sig_type;
7665 sect_offset abbrev_offset;
7666 };
7667
7668 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7669
7670 static bool
7671 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7672 const struct tu_abbrev_offset &b)
7673 {
7674 return a.abbrev_offset < b.abbrev_offset;
7675 }
7676
7677 /* Efficiently read all the type units.
7678 This does the bulk of the work for build_type_psymtabs.
7679
7680 The efficiency is because we sort TUs by the abbrev table they use and
7681 only read each abbrev table once. In one program there are 200K TUs
7682 sharing 8K abbrev tables.
7683
7684 The main purpose of this function is to support building the
7685 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7686 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7687 can collapse the search space by grouping them by stmt_list.
7688 The savings can be significant, in the same program from above the 200K TUs
7689 share 8K stmt_list tables.
7690
7691 FUNC is expected to call get_type_unit_group, which will create the
7692 struct type_unit_group if necessary and add it to
7693 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7694
7695 static void
7696 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7697 {
7698 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7699 abbrev_table_up abbrev_table;
7700 sect_offset abbrev_offset;
7701
7702 /* It's up to the caller to not call us multiple times. */
7703 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7704
7705 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7706 return;
7707
7708 /* TUs typically share abbrev tables, and there can be way more TUs than
7709 abbrev tables. Sort by abbrev table to reduce the number of times we
7710 read each abbrev table in.
7711 Alternatives are to punt or to maintain a cache of abbrev tables.
7712 This is simpler and efficient enough for now.
7713
7714 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7715 symtab to use). Typically TUs with the same abbrev offset have the same
7716 stmt_list value too so in practice this should work well.
7717
7718 The basic algorithm here is:
7719
7720 sort TUs by abbrev table
7721 for each TU with same abbrev table:
7722 read abbrev table if first user
7723 read TU top level DIE
7724 [IWBN if DWO skeletons had DW_AT_stmt_list]
7725 call FUNC */
7726
7727 if (dwarf_read_debug)
7728 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7729
7730 /* Sort in a separate table to maintain the order of all_type_units
7731 for .gdb_index: TU indices directly index all_type_units. */
7732 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7733 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7734
7735 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7736 sorted_by_abbrev.emplace_back
7737 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7738 sig_type->per_cu.section,
7739 sig_type->per_cu.sect_off));
7740
7741 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7742 sort_tu_by_abbrev_offset);
7743
7744 abbrev_offset = (sect_offset) ~(unsigned) 0;
7745
7746 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7747 {
7748 /* Switch to the next abbrev table if necessary. */
7749 if (abbrev_table == NULL
7750 || tu.abbrev_offset != abbrev_offset)
7751 {
7752 abbrev_offset = tu.abbrev_offset;
7753 abbrev_table =
7754 abbrev_table::read (dwarf2_per_objfile->objfile,
7755 &dwarf2_per_objfile->per_bfd->abbrev,
7756 abbrev_offset);
7757 ++tu_stats->nr_uniq_abbrev_tables;
7758 }
7759
7760 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7761 abbrev_table.get (), 0, false);
7762 if (!reader.dummy_p)
7763 build_type_psymtabs_reader (&reader, reader.info_ptr,
7764 reader.comp_unit_die);
7765 }
7766 }
7767
7768 /* Print collected type unit statistics. */
7769
7770 static void
7771 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7772 {
7773 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7774
7775 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7776 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7777 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7778 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7779 tu_stats->nr_uniq_abbrev_tables);
7780 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7781 tu_stats->nr_symtabs);
7782 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7783 tu_stats->nr_symtab_sharers);
7784 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7785 tu_stats->nr_stmt_less_type_units);
7786 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7787 tu_stats->nr_all_type_units_reallocs);
7788 }
7789
7790 /* Traversal function for build_type_psymtabs. */
7791
7792 static int
7793 build_type_psymtab_dependencies (void **slot, void *info)
7794 {
7795 struct dwarf2_per_objfile *dwarf2_per_objfile
7796 = (struct dwarf2_per_objfile *) info;
7797 struct objfile *objfile = dwarf2_per_objfile->objfile;
7798 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7799 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7800 dwarf2_psymtab *pst = per_cu->v.psymtab;
7801 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7802 int i;
7803
7804 gdb_assert (len > 0);
7805 gdb_assert (per_cu->type_unit_group_p ());
7806
7807 pst->number_of_dependencies = len;
7808 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7809 for (i = 0; i < len; ++i)
7810 {
7811 struct signatured_type *iter = tu_group->tus->at (i);
7812 gdb_assert (iter->per_cu.is_debug_types);
7813 pst->dependencies[i] = iter->per_cu.v.psymtab;
7814 iter->type_unit_group = tu_group;
7815 }
7816
7817 delete tu_group->tus;
7818 tu_group->tus = nullptr;
7819
7820 return 1;
7821 }
7822
7823 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7824 Build partial symbol tables for the .debug_types comp-units. */
7825
7826 static void
7827 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7828 {
7829 if (! create_all_type_units (dwarf2_per_objfile))
7830 return;
7831
7832 build_type_psymtabs_1 (dwarf2_per_objfile);
7833 }
7834
7835 /* Traversal function for process_skeletonless_type_unit.
7836 Read a TU in a DWO file and build partial symbols for it. */
7837
7838 static int
7839 process_skeletonless_type_unit (void **slot, void *info)
7840 {
7841 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7842 struct dwarf2_per_objfile *dwarf2_per_objfile
7843 = (struct dwarf2_per_objfile *) info;
7844 struct signatured_type find_entry, *entry;
7845
7846 /* If this TU doesn't exist in the global table, add it and read it in. */
7847
7848 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7849 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7850
7851 find_entry.signature = dwo_unit->signature;
7852 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7853 &find_entry, INSERT);
7854 /* If we've already seen this type there's nothing to do. What's happening
7855 is we're doing our own version of comdat-folding here. */
7856 if (*slot != NULL)
7857 return 1;
7858
7859 /* This does the job that create_all_type_units would have done for
7860 this TU. */
7861 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7862 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7863 *slot = entry;
7864
7865 /* This does the job that build_type_psymtabs_1 would have done. */
7866 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
7867 if (!reader.dummy_p)
7868 build_type_psymtabs_reader (&reader, reader.info_ptr,
7869 reader.comp_unit_die);
7870
7871 return 1;
7872 }
7873
7874 /* Traversal function for process_skeletonless_type_units. */
7875
7876 static int
7877 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7878 {
7879 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7880
7881 if (dwo_file->tus != NULL)
7882 htab_traverse_noresize (dwo_file->tus.get (),
7883 process_skeletonless_type_unit, info);
7884
7885 return 1;
7886 }
7887
7888 /* Scan all TUs of DWO files, verifying we've processed them.
7889 This is needed in case a TU was emitted without its skeleton.
7890 Note: This can't be done until we know what all the DWO files are. */
7891
7892 static void
7893 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7894 {
7895 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7896 if (get_dwp_file (dwarf2_per_objfile) == NULL
7897 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7898 {
7899 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7900 process_dwo_file_for_skeletonless_type_units,
7901 dwarf2_per_objfile);
7902 }
7903 }
7904
7905 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7906
7907 static void
7908 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7909 {
7910 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7911 {
7912 dwarf2_psymtab *pst = per_cu->v.psymtab;
7913
7914 if (pst == NULL)
7915 continue;
7916
7917 for (int j = 0; j < pst->number_of_dependencies; ++j)
7918 {
7919 /* Set the 'user' field only if it is not already set. */
7920 if (pst->dependencies[j]->user == NULL)
7921 pst->dependencies[j]->user = pst;
7922 }
7923 }
7924 }
7925
7926 /* Build the partial symbol table by doing a quick pass through the
7927 .debug_info and .debug_abbrev sections. */
7928
7929 static void
7930 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7931 {
7932 struct objfile *objfile = dwarf2_per_objfile->objfile;
7933
7934 if (dwarf_read_debug)
7935 {
7936 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7937 objfile_name (objfile));
7938 }
7939
7940 scoped_restore restore_reading_psyms
7941 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7942 true);
7943
7944 dwarf2_per_objfile->per_bfd->info.read (objfile);
7945
7946 /* Any cached compilation units will be linked by the per-objfile
7947 read_in_chain. Make sure to free them when we're done. */
7948 free_cached_comp_units freer (dwarf2_per_objfile);
7949
7950 build_type_psymtabs (dwarf2_per_objfile);
7951
7952 create_all_comp_units (dwarf2_per_objfile);
7953
7954 /* Create a temporary address map on a temporary obstack. We later
7955 copy this to the final obstack. */
7956 auto_obstack temp_obstack;
7957
7958 scoped_restore save_psymtabs_addrmap
7959 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7960 addrmap_create_mutable (&temp_obstack));
7961
7962 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7963 {
7964 if (per_cu->v.psymtab != NULL)
7965 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7966 continue;
7967 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7968 language_minimal);
7969 }
7970
7971 /* This has to wait until we read the CUs, we need the list of DWOs. */
7972 process_skeletonless_type_units (dwarf2_per_objfile);
7973
7974 /* Now that all TUs have been processed we can fill in the dependencies. */
7975 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7976 {
7977 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7978 build_type_psymtab_dependencies, dwarf2_per_objfile);
7979 }
7980
7981 if (dwarf_read_debug)
7982 print_tu_stats (dwarf2_per_objfile);
7983
7984 set_partial_user (dwarf2_per_objfile);
7985
7986 objfile->partial_symtabs->psymtabs_addrmap
7987 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7988 objfile->partial_symtabs->obstack ());
7989 /* At this point we want to keep the address map. */
7990 save_psymtabs_addrmap.release ();
7991
7992 if (dwarf_read_debug)
7993 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7994 objfile_name (objfile));
7995 }
7996
7997 /* Load the partial DIEs for a secondary CU into memory.
7998 This is also used when rereading a primary CU with load_all_dies. */
7999
8000 static void
8001 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8002 dwarf2_per_objfile *per_objfile)
8003 {
8004 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
8005
8006 if (!reader.dummy_p)
8007 {
8008 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8009 language_minimal);
8010
8011 /* Check if comp unit has_children.
8012 If so, read the rest of the partial symbols from this comp unit.
8013 If not, there's no more debug_info for this comp unit. */
8014 if (reader.comp_unit_die->has_children)
8015 load_partial_dies (&reader, reader.info_ptr, 0);
8016
8017 reader.keep ();
8018 }
8019 }
8020
8021 static void
8022 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8023 struct dwarf2_section_info *section,
8024 struct dwarf2_section_info *abbrev_section,
8025 unsigned int is_dwz)
8026 {
8027 const gdb_byte *info_ptr;
8028 struct objfile *objfile = dwarf2_per_objfile->objfile;
8029
8030 if (dwarf_read_debug)
8031 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8032 section->get_name (),
8033 section->get_file_name ());
8034
8035 section->read (objfile);
8036
8037 info_ptr = section->buffer;
8038
8039 while (info_ptr < section->buffer + section->size)
8040 {
8041 struct dwarf2_per_cu_data *this_cu;
8042
8043 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8044
8045 comp_unit_head cu_header;
8046 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8047 abbrev_section, info_ptr,
8048 rcuh_kind::COMPILE);
8049
8050 /* Save the compilation unit for later lookup. */
8051 if (cu_header.unit_type != DW_UT_type)
8052 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8053 else
8054 {
8055 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8056 sig_type->signature = cu_header.signature;
8057 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8058 this_cu = &sig_type->per_cu;
8059 }
8060 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8061 this_cu->sect_off = sect_off;
8062 this_cu->length = cu_header.length + cu_header.initial_length_size;
8063 this_cu->is_dwz = is_dwz;
8064 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8065 this_cu->section = section;
8066
8067 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8068
8069 info_ptr = info_ptr + this_cu->length;
8070 }
8071 }
8072
8073 /* Create a list of all compilation units in OBJFILE.
8074 This is only done for -readnow and building partial symtabs. */
8075
8076 static void
8077 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8078 {
8079 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8080 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8081 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8082
8083 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8084 if (dwz != NULL)
8085 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8086 1);
8087 }
8088
8089 /* Process all loaded DIEs for compilation unit CU, starting at
8090 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8091 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8092 DW_AT_ranges). See the comments of add_partial_subprogram on how
8093 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8094
8095 static void
8096 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8097 CORE_ADDR *highpc, int set_addrmap,
8098 struct dwarf2_cu *cu)
8099 {
8100 struct partial_die_info *pdi;
8101
8102 /* Now, march along the PDI's, descending into ones which have
8103 interesting children but skipping the children of the other ones,
8104 until we reach the end of the compilation unit. */
8105
8106 pdi = first_die;
8107
8108 while (pdi != NULL)
8109 {
8110 pdi->fixup (cu);
8111
8112 /* Anonymous namespaces or modules have no name but have interesting
8113 children, so we need to look at them. Ditto for anonymous
8114 enums. */
8115
8116 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8117 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8118 || pdi->tag == DW_TAG_imported_unit
8119 || pdi->tag == DW_TAG_inlined_subroutine)
8120 {
8121 switch (pdi->tag)
8122 {
8123 case DW_TAG_subprogram:
8124 case DW_TAG_inlined_subroutine:
8125 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8126 break;
8127 case DW_TAG_constant:
8128 case DW_TAG_variable:
8129 case DW_TAG_typedef:
8130 case DW_TAG_union_type:
8131 if (!pdi->is_declaration
8132 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8133 {
8134 add_partial_symbol (pdi, cu);
8135 }
8136 break;
8137 case DW_TAG_class_type:
8138 case DW_TAG_interface_type:
8139 case DW_TAG_structure_type:
8140 if (!pdi->is_declaration)
8141 {
8142 add_partial_symbol (pdi, cu);
8143 }
8144 if ((cu->language == language_rust
8145 || cu->language == language_cplus) && pdi->has_children)
8146 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8147 set_addrmap, cu);
8148 break;
8149 case DW_TAG_enumeration_type:
8150 if (!pdi->is_declaration)
8151 add_partial_enumeration (pdi, cu);
8152 break;
8153 case DW_TAG_base_type:
8154 case DW_TAG_subrange_type:
8155 /* File scope base type definitions are added to the partial
8156 symbol table. */
8157 add_partial_symbol (pdi, cu);
8158 break;
8159 case DW_TAG_namespace:
8160 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8161 break;
8162 case DW_TAG_module:
8163 if (!pdi->is_declaration)
8164 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8165 break;
8166 case DW_TAG_imported_unit:
8167 {
8168 struct dwarf2_per_cu_data *per_cu;
8169
8170 /* For now we don't handle imported units in type units. */
8171 if (cu->per_cu->is_debug_types)
8172 {
8173 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8174 " supported in type units [in module %s]"),
8175 objfile_name (cu->per_objfile->objfile));
8176 }
8177
8178 per_cu = dwarf2_find_containing_comp_unit
8179 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8180
8181 /* Go read the partial unit, if needed. */
8182 if (per_cu->v.psymtab == NULL)
8183 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8184 cu->language);
8185
8186 cu->per_cu->imported_symtabs_push (per_cu);
8187 }
8188 break;
8189 case DW_TAG_imported_declaration:
8190 add_partial_symbol (pdi, cu);
8191 break;
8192 default:
8193 break;
8194 }
8195 }
8196
8197 /* If the die has a sibling, skip to the sibling. */
8198
8199 pdi = pdi->die_sibling;
8200 }
8201 }
8202
8203 /* Functions used to compute the fully scoped name of a partial DIE.
8204
8205 Normally, this is simple. For C++, the parent DIE's fully scoped
8206 name is concatenated with "::" and the partial DIE's name.
8207 Enumerators are an exception; they use the scope of their parent
8208 enumeration type, i.e. the name of the enumeration type is not
8209 prepended to the enumerator.
8210
8211 There are two complexities. One is DW_AT_specification; in this
8212 case "parent" means the parent of the target of the specification,
8213 instead of the direct parent of the DIE. The other is compilers
8214 which do not emit DW_TAG_namespace; in this case we try to guess
8215 the fully qualified name of structure types from their members'
8216 linkage names. This must be done using the DIE's children rather
8217 than the children of any DW_AT_specification target. We only need
8218 to do this for structures at the top level, i.e. if the target of
8219 any DW_AT_specification (if any; otherwise the DIE itself) does not
8220 have a parent. */
8221
8222 /* Compute the scope prefix associated with PDI's parent, in
8223 compilation unit CU. The result will be allocated on CU's
8224 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8225 field. NULL is returned if no prefix is necessary. */
8226 static const char *
8227 partial_die_parent_scope (struct partial_die_info *pdi,
8228 struct dwarf2_cu *cu)
8229 {
8230 const char *grandparent_scope;
8231 struct partial_die_info *parent, *real_pdi;
8232
8233 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8234 then this means the parent of the specification DIE. */
8235
8236 real_pdi = pdi;
8237 while (real_pdi->has_specification)
8238 {
8239 auto res = find_partial_die (real_pdi->spec_offset,
8240 real_pdi->spec_is_dwz, cu);
8241 real_pdi = res.pdi;
8242 cu = res.cu;
8243 }
8244
8245 parent = real_pdi->die_parent;
8246 if (parent == NULL)
8247 return NULL;
8248
8249 if (parent->scope_set)
8250 return parent->scope;
8251
8252 parent->fixup (cu);
8253
8254 grandparent_scope = partial_die_parent_scope (parent, cu);
8255
8256 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8257 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8258 Work around this problem here. */
8259 if (cu->language == language_cplus
8260 && parent->tag == DW_TAG_namespace
8261 && strcmp (parent->name, "::") == 0
8262 && grandparent_scope == NULL)
8263 {
8264 parent->scope = NULL;
8265 parent->scope_set = 1;
8266 return NULL;
8267 }
8268
8269 /* Nested subroutines in Fortran get a prefix. */
8270 if (pdi->tag == DW_TAG_enumerator)
8271 /* Enumerators should not get the name of the enumeration as a prefix. */
8272 parent->scope = grandparent_scope;
8273 else if (parent->tag == DW_TAG_namespace
8274 || parent->tag == DW_TAG_module
8275 || parent->tag == DW_TAG_structure_type
8276 || parent->tag == DW_TAG_class_type
8277 || parent->tag == DW_TAG_interface_type
8278 || parent->tag == DW_TAG_union_type
8279 || parent->tag == DW_TAG_enumeration_type
8280 || (cu->language == language_fortran
8281 && parent->tag == DW_TAG_subprogram
8282 && pdi->tag == DW_TAG_subprogram))
8283 {
8284 if (grandparent_scope == NULL)
8285 parent->scope = parent->name;
8286 else
8287 parent->scope = typename_concat (&cu->comp_unit_obstack,
8288 grandparent_scope,
8289 parent->name, 0, cu);
8290 }
8291 else
8292 {
8293 /* FIXME drow/2004-04-01: What should we be doing with
8294 function-local names? For partial symbols, we should probably be
8295 ignoring them. */
8296 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8297 dwarf_tag_name (parent->tag),
8298 sect_offset_str (pdi->sect_off));
8299 parent->scope = grandparent_scope;
8300 }
8301
8302 parent->scope_set = 1;
8303 return parent->scope;
8304 }
8305
8306 /* Return the fully scoped name associated with PDI, from compilation unit
8307 CU. The result will be allocated with malloc. */
8308
8309 static gdb::unique_xmalloc_ptr<char>
8310 partial_die_full_name (struct partial_die_info *pdi,
8311 struct dwarf2_cu *cu)
8312 {
8313 const char *parent_scope;
8314
8315 /* If this is a template instantiation, we can not work out the
8316 template arguments from partial DIEs. So, unfortunately, we have
8317 to go through the full DIEs. At least any work we do building
8318 types here will be reused if full symbols are loaded later. */
8319 if (pdi->has_template_arguments)
8320 {
8321 pdi->fixup (cu);
8322
8323 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8324 {
8325 struct die_info *die;
8326 struct attribute attr;
8327 struct dwarf2_cu *ref_cu = cu;
8328
8329 /* DW_FORM_ref_addr is using section offset. */
8330 attr.name = (enum dwarf_attribute) 0;
8331 attr.form = DW_FORM_ref_addr;
8332 attr.u.unsnd = to_underlying (pdi->sect_off);
8333 die = follow_die_ref (NULL, &attr, &ref_cu);
8334
8335 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8336 }
8337 }
8338
8339 parent_scope = partial_die_parent_scope (pdi, cu);
8340 if (parent_scope == NULL)
8341 return NULL;
8342 else
8343 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8344 pdi->name, 0, cu));
8345 }
8346
8347 static void
8348 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8349 {
8350 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8351 struct objfile *objfile = dwarf2_per_objfile->objfile;
8352 struct gdbarch *gdbarch = objfile->arch ();
8353 CORE_ADDR addr = 0;
8354 const char *actual_name = NULL;
8355 CORE_ADDR baseaddr;
8356
8357 baseaddr = objfile->text_section_offset ();
8358
8359 gdb::unique_xmalloc_ptr<char> built_actual_name
8360 = partial_die_full_name (pdi, cu);
8361 if (built_actual_name != NULL)
8362 actual_name = built_actual_name.get ();
8363
8364 if (actual_name == NULL)
8365 actual_name = pdi->name;
8366
8367 partial_symbol psymbol;
8368 memset (&psymbol, 0, sizeof (psymbol));
8369 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8370 psymbol.ginfo.section = -1;
8371
8372 /* The code below indicates that the psymbol should be installed by
8373 setting this. */
8374 gdb::optional<psymbol_placement> where;
8375
8376 switch (pdi->tag)
8377 {
8378 case DW_TAG_inlined_subroutine:
8379 case DW_TAG_subprogram:
8380 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8381 - baseaddr);
8382 if (pdi->is_external
8383 || cu->language == language_ada
8384 || (cu->language == language_fortran
8385 && pdi->die_parent != NULL
8386 && pdi->die_parent->tag == DW_TAG_subprogram))
8387 {
8388 /* Normally, only "external" DIEs are part of the global scope.
8389 But in Ada and Fortran, we want to be able to access nested
8390 procedures globally. So all Ada and Fortran subprograms are
8391 stored in the global scope. */
8392 where = psymbol_placement::GLOBAL;
8393 }
8394 else
8395 where = psymbol_placement::STATIC;
8396
8397 psymbol.domain = VAR_DOMAIN;
8398 psymbol.aclass = LOC_BLOCK;
8399 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8400 psymbol.ginfo.value.address = addr;
8401
8402 if (pdi->main_subprogram && actual_name != NULL)
8403 set_objfile_main_name (objfile, actual_name, cu->language);
8404 break;
8405 case DW_TAG_constant:
8406 psymbol.domain = VAR_DOMAIN;
8407 psymbol.aclass = LOC_STATIC;
8408 where = (pdi->is_external
8409 ? psymbol_placement::GLOBAL
8410 : psymbol_placement::STATIC);
8411 break;
8412 case DW_TAG_variable:
8413 if (pdi->d.locdesc)
8414 addr = decode_locdesc (pdi->d.locdesc, cu);
8415
8416 if (pdi->d.locdesc
8417 && addr == 0
8418 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8419 {
8420 /* A global or static variable may also have been stripped
8421 out by the linker if unused, in which case its address
8422 will be nullified; do not add such variables into partial
8423 symbol table then. */
8424 }
8425 else if (pdi->is_external)
8426 {
8427 /* Global Variable.
8428 Don't enter into the minimal symbol tables as there is
8429 a minimal symbol table entry from the ELF symbols already.
8430 Enter into partial symbol table if it has a location
8431 descriptor or a type.
8432 If the location descriptor is missing, new_symbol will create
8433 a LOC_UNRESOLVED symbol, the address of the variable will then
8434 be determined from the minimal symbol table whenever the variable
8435 is referenced.
8436 The address for the partial symbol table entry is not
8437 used by GDB, but it comes in handy for debugging partial symbol
8438 table building. */
8439
8440 if (pdi->d.locdesc || pdi->has_type)
8441 {
8442 psymbol.domain = VAR_DOMAIN;
8443 psymbol.aclass = LOC_STATIC;
8444 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8445 psymbol.ginfo.value.address = addr;
8446 where = psymbol_placement::GLOBAL;
8447 }
8448 }
8449 else
8450 {
8451 int has_loc = pdi->d.locdesc != NULL;
8452
8453 /* Static Variable. Skip symbols whose value we cannot know (those
8454 without location descriptors or constant values). */
8455 if (!has_loc && !pdi->has_const_value)
8456 return;
8457
8458 psymbol.domain = VAR_DOMAIN;
8459 psymbol.aclass = LOC_STATIC;
8460 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8461 if (has_loc)
8462 psymbol.ginfo.value.address = addr;
8463 where = psymbol_placement::STATIC;
8464 }
8465 break;
8466 case DW_TAG_typedef:
8467 case DW_TAG_base_type:
8468 case DW_TAG_subrange_type:
8469 psymbol.domain = VAR_DOMAIN;
8470 psymbol.aclass = LOC_TYPEDEF;
8471 where = psymbol_placement::STATIC;
8472 break;
8473 case DW_TAG_imported_declaration:
8474 case DW_TAG_namespace:
8475 psymbol.domain = VAR_DOMAIN;
8476 psymbol.aclass = LOC_TYPEDEF;
8477 where = psymbol_placement::GLOBAL;
8478 break;
8479 case DW_TAG_module:
8480 /* With Fortran 77 there might be a "BLOCK DATA" module
8481 available without any name. If so, we skip the module as it
8482 doesn't bring any value. */
8483 if (actual_name != nullptr)
8484 {
8485 psymbol.domain = MODULE_DOMAIN;
8486 psymbol.aclass = LOC_TYPEDEF;
8487 where = psymbol_placement::GLOBAL;
8488 }
8489 break;
8490 case DW_TAG_class_type:
8491 case DW_TAG_interface_type:
8492 case DW_TAG_structure_type:
8493 case DW_TAG_union_type:
8494 case DW_TAG_enumeration_type:
8495 /* Skip external references. The DWARF standard says in the section
8496 about "Structure, Union, and Class Type Entries": "An incomplete
8497 structure, union or class type is represented by a structure,
8498 union or class entry that does not have a byte size attribute
8499 and that has a DW_AT_declaration attribute." */
8500 if (!pdi->has_byte_size && pdi->is_declaration)
8501 return;
8502
8503 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8504 static vs. global. */
8505 psymbol.domain = STRUCT_DOMAIN;
8506 psymbol.aclass = LOC_TYPEDEF;
8507 where = (cu->language == language_cplus
8508 ? psymbol_placement::GLOBAL
8509 : psymbol_placement::STATIC);
8510 break;
8511 case DW_TAG_enumerator:
8512 psymbol.domain = VAR_DOMAIN;
8513 psymbol.aclass = LOC_CONST;
8514 where = (cu->language == language_cplus
8515 ? psymbol_placement::GLOBAL
8516 : psymbol_placement::STATIC);
8517 break;
8518 default:
8519 break;
8520 }
8521
8522 if (where.has_value ())
8523 {
8524 if (built_actual_name != nullptr)
8525 actual_name = objfile->intern (actual_name);
8526 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8527 psymbol.ginfo.set_linkage_name (actual_name);
8528 else
8529 {
8530 psymbol.ginfo.set_demangled_name (actual_name,
8531 &objfile->objfile_obstack);
8532 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8533 }
8534 add_psymbol_to_list (psymbol, *where, objfile);
8535 }
8536 }
8537
8538 /* Read a partial die corresponding to a namespace; also, add a symbol
8539 corresponding to that namespace to the symbol table. NAMESPACE is
8540 the name of the enclosing namespace. */
8541
8542 static void
8543 add_partial_namespace (struct partial_die_info *pdi,
8544 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8545 int set_addrmap, struct dwarf2_cu *cu)
8546 {
8547 /* Add a symbol for the namespace. */
8548
8549 add_partial_symbol (pdi, cu);
8550
8551 /* Now scan partial symbols in that namespace. */
8552
8553 if (pdi->has_children)
8554 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8555 }
8556
8557 /* Read a partial die corresponding to a Fortran module. */
8558
8559 static void
8560 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8561 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8562 {
8563 /* Add a symbol for the namespace. */
8564
8565 add_partial_symbol (pdi, cu);
8566
8567 /* Now scan partial symbols in that module. */
8568
8569 if (pdi->has_children)
8570 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8571 }
8572
8573 /* Read a partial die corresponding to a subprogram or an inlined
8574 subprogram and create a partial symbol for that subprogram.
8575 When the CU language allows it, this routine also defines a partial
8576 symbol for each nested subprogram that this subprogram contains.
8577 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8578 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8579
8580 PDI may also be a lexical block, in which case we simply search
8581 recursively for subprograms defined inside that lexical block.
8582 Again, this is only performed when the CU language allows this
8583 type of definitions. */
8584
8585 static void
8586 add_partial_subprogram (struct partial_die_info *pdi,
8587 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8588 int set_addrmap, struct dwarf2_cu *cu)
8589 {
8590 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8591 {
8592 if (pdi->has_pc_info)
8593 {
8594 if (pdi->lowpc < *lowpc)
8595 *lowpc = pdi->lowpc;
8596 if (pdi->highpc > *highpc)
8597 *highpc = pdi->highpc;
8598 if (set_addrmap)
8599 {
8600 struct objfile *objfile = cu->per_objfile->objfile;
8601 struct gdbarch *gdbarch = objfile->arch ();
8602 CORE_ADDR baseaddr;
8603 CORE_ADDR this_highpc;
8604 CORE_ADDR this_lowpc;
8605
8606 baseaddr = objfile->text_section_offset ();
8607 this_lowpc
8608 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8609 pdi->lowpc + baseaddr)
8610 - baseaddr);
8611 this_highpc
8612 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8613 pdi->highpc + baseaddr)
8614 - baseaddr);
8615 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8616 this_lowpc, this_highpc - 1,
8617 cu->per_cu->v.psymtab);
8618 }
8619 }
8620
8621 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8622 {
8623 if (!pdi->is_declaration)
8624 /* Ignore subprogram DIEs that do not have a name, they are
8625 illegal. Do not emit a complaint at this point, we will
8626 do so when we convert this psymtab into a symtab. */
8627 if (pdi->name)
8628 add_partial_symbol (pdi, cu);
8629 }
8630 }
8631
8632 if (! pdi->has_children)
8633 return;
8634
8635 if (cu->language == language_ada || cu->language == language_fortran)
8636 {
8637 pdi = pdi->die_child;
8638 while (pdi != NULL)
8639 {
8640 pdi->fixup (cu);
8641 if (pdi->tag == DW_TAG_subprogram
8642 || pdi->tag == DW_TAG_inlined_subroutine
8643 || pdi->tag == DW_TAG_lexical_block)
8644 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8645 pdi = pdi->die_sibling;
8646 }
8647 }
8648 }
8649
8650 /* Read a partial die corresponding to an enumeration type. */
8651
8652 static void
8653 add_partial_enumeration (struct partial_die_info *enum_pdi,
8654 struct dwarf2_cu *cu)
8655 {
8656 struct partial_die_info *pdi;
8657
8658 if (enum_pdi->name != NULL)
8659 add_partial_symbol (enum_pdi, cu);
8660
8661 pdi = enum_pdi->die_child;
8662 while (pdi)
8663 {
8664 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8665 complaint (_("malformed enumerator DIE ignored"));
8666 else
8667 add_partial_symbol (pdi, cu);
8668 pdi = pdi->die_sibling;
8669 }
8670 }
8671
8672 /* Return the initial uleb128 in the die at INFO_PTR. */
8673
8674 static unsigned int
8675 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8676 {
8677 unsigned int bytes_read;
8678
8679 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8680 }
8681
8682 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8683 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8684
8685 Return the corresponding abbrev, or NULL if the number is zero (indicating
8686 an empty DIE). In either case *BYTES_READ will be set to the length of
8687 the initial number. */
8688
8689 static struct abbrev_info *
8690 peek_die_abbrev (const die_reader_specs &reader,
8691 const gdb_byte *info_ptr, unsigned int *bytes_read)
8692 {
8693 dwarf2_cu *cu = reader.cu;
8694 bfd *abfd = cu->per_objfile->objfile->obfd;
8695 unsigned int abbrev_number
8696 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8697
8698 if (abbrev_number == 0)
8699 return NULL;
8700
8701 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8702 if (!abbrev)
8703 {
8704 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8705 " at offset %s [in module %s]"),
8706 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8707 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8708 }
8709
8710 return abbrev;
8711 }
8712
8713 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8714 Returns a pointer to the end of a series of DIEs, terminated by an empty
8715 DIE. Any children of the skipped DIEs will also be skipped. */
8716
8717 static const gdb_byte *
8718 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8719 {
8720 while (1)
8721 {
8722 unsigned int bytes_read;
8723 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8724
8725 if (abbrev == NULL)
8726 return info_ptr + bytes_read;
8727 else
8728 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8729 }
8730 }
8731
8732 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8733 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8734 abbrev corresponding to that skipped uleb128 should be passed in
8735 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8736 children. */
8737
8738 static const gdb_byte *
8739 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8740 struct abbrev_info *abbrev)
8741 {
8742 unsigned int bytes_read;
8743 struct attribute attr;
8744 bfd *abfd = reader->abfd;
8745 struct dwarf2_cu *cu = reader->cu;
8746 const gdb_byte *buffer = reader->buffer;
8747 const gdb_byte *buffer_end = reader->buffer_end;
8748 unsigned int form, i;
8749
8750 for (i = 0; i < abbrev->num_attrs; i++)
8751 {
8752 /* The only abbrev we care about is DW_AT_sibling. */
8753 if (abbrev->attrs[i].name == DW_AT_sibling)
8754 {
8755 bool ignored;
8756 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8757 &ignored);
8758 if (attr.form == DW_FORM_ref_addr)
8759 complaint (_("ignoring absolute DW_AT_sibling"));
8760 else
8761 {
8762 sect_offset off = attr.get_ref_die_offset ();
8763 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8764
8765 if (sibling_ptr < info_ptr)
8766 complaint (_("DW_AT_sibling points backwards"));
8767 else if (sibling_ptr > reader->buffer_end)
8768 reader->die_section->overflow_complaint ();
8769 else
8770 return sibling_ptr;
8771 }
8772 }
8773
8774 /* If it isn't DW_AT_sibling, skip this attribute. */
8775 form = abbrev->attrs[i].form;
8776 skip_attribute:
8777 switch (form)
8778 {
8779 case DW_FORM_ref_addr:
8780 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8781 and later it is offset sized. */
8782 if (cu->header.version == 2)
8783 info_ptr += cu->header.addr_size;
8784 else
8785 info_ptr += cu->header.offset_size;
8786 break;
8787 case DW_FORM_GNU_ref_alt:
8788 info_ptr += cu->header.offset_size;
8789 break;
8790 case DW_FORM_addr:
8791 info_ptr += cu->header.addr_size;
8792 break;
8793 case DW_FORM_data1:
8794 case DW_FORM_ref1:
8795 case DW_FORM_flag:
8796 case DW_FORM_strx1:
8797 info_ptr += 1;
8798 break;
8799 case DW_FORM_flag_present:
8800 case DW_FORM_implicit_const:
8801 break;
8802 case DW_FORM_data2:
8803 case DW_FORM_ref2:
8804 case DW_FORM_strx2:
8805 info_ptr += 2;
8806 break;
8807 case DW_FORM_strx3:
8808 info_ptr += 3;
8809 break;
8810 case DW_FORM_data4:
8811 case DW_FORM_ref4:
8812 case DW_FORM_strx4:
8813 info_ptr += 4;
8814 break;
8815 case DW_FORM_data8:
8816 case DW_FORM_ref8:
8817 case DW_FORM_ref_sig8:
8818 info_ptr += 8;
8819 break;
8820 case DW_FORM_data16:
8821 info_ptr += 16;
8822 break;
8823 case DW_FORM_string:
8824 read_direct_string (abfd, info_ptr, &bytes_read);
8825 info_ptr += bytes_read;
8826 break;
8827 case DW_FORM_sec_offset:
8828 case DW_FORM_strp:
8829 case DW_FORM_GNU_strp_alt:
8830 info_ptr += cu->header.offset_size;
8831 break;
8832 case DW_FORM_exprloc:
8833 case DW_FORM_block:
8834 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8835 info_ptr += bytes_read;
8836 break;
8837 case DW_FORM_block1:
8838 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8839 break;
8840 case DW_FORM_block2:
8841 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8842 break;
8843 case DW_FORM_block4:
8844 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8845 break;
8846 case DW_FORM_addrx:
8847 case DW_FORM_strx:
8848 case DW_FORM_sdata:
8849 case DW_FORM_udata:
8850 case DW_FORM_ref_udata:
8851 case DW_FORM_GNU_addr_index:
8852 case DW_FORM_GNU_str_index:
8853 case DW_FORM_rnglistx:
8854 case DW_FORM_loclistx:
8855 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8856 break;
8857 case DW_FORM_indirect:
8858 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8859 info_ptr += bytes_read;
8860 /* We need to continue parsing from here, so just go back to
8861 the top. */
8862 goto skip_attribute;
8863
8864 default:
8865 error (_("Dwarf Error: Cannot handle %s "
8866 "in DWARF reader [in module %s]"),
8867 dwarf_form_name (form),
8868 bfd_get_filename (abfd));
8869 }
8870 }
8871
8872 if (abbrev->has_children)
8873 return skip_children (reader, info_ptr);
8874 else
8875 return info_ptr;
8876 }
8877
8878 /* Locate ORIG_PDI's sibling.
8879 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8880
8881 static const gdb_byte *
8882 locate_pdi_sibling (const struct die_reader_specs *reader,
8883 struct partial_die_info *orig_pdi,
8884 const gdb_byte *info_ptr)
8885 {
8886 /* Do we know the sibling already? */
8887
8888 if (orig_pdi->sibling)
8889 return orig_pdi->sibling;
8890
8891 /* Are there any children to deal with? */
8892
8893 if (!orig_pdi->has_children)
8894 return info_ptr;
8895
8896 /* Skip the children the long way. */
8897
8898 return skip_children (reader, info_ptr);
8899 }
8900
8901 /* Expand this partial symbol table into a full symbol table. SELF is
8902 not NULL. */
8903
8904 void
8905 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8906 {
8907 struct dwarf2_per_objfile *dwarf2_per_objfile
8908 = get_dwarf2_per_objfile (objfile);
8909
8910 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8911
8912 /* If this psymtab is constructed from a debug-only objfile, the
8913 has_section_at_zero flag will not necessarily be correct. We
8914 can get the correct value for this flag by looking at the data
8915 associated with the (presumably stripped) associated objfile. */
8916 if (objfile->separate_debug_objfile_backlink)
8917 {
8918 struct dwarf2_per_objfile *dpo_backlink
8919 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8920
8921 dwarf2_per_objfile->per_bfd->has_section_at_zero
8922 = dpo_backlink->per_bfd->has_section_at_zero;
8923 }
8924
8925 expand_psymtab (objfile);
8926
8927 process_cu_includes (dwarf2_per_objfile);
8928 }
8929 \f
8930 /* Reading in full CUs. */
8931
8932 /* Add PER_CU to the queue. */
8933
8934 static void
8935 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8936 enum language pretend_language)
8937 {
8938 per_cu->queued = 1;
8939 per_cu->per_bfd->queue.emplace (per_cu, pretend_language);
8940 }
8941
8942 /* If PER_CU is not yet queued, add it to the queue.
8943 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8944 dependency.
8945 The result is non-zero if PER_CU was queued, otherwise the result is zero
8946 meaning either PER_CU is already queued or it is already loaded.
8947
8948 N.B. There is an invariant here that if a CU is queued then it is loaded.
8949 The caller is required to load PER_CU if we return non-zero. */
8950
8951 static int
8952 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8953 struct dwarf2_per_cu_data *per_cu,
8954 enum language pretend_language)
8955 {
8956 /* We may arrive here during partial symbol reading, if we need full
8957 DIEs to process an unusual case (e.g. template arguments). Do
8958 not queue PER_CU, just tell our caller to load its DIEs. */
8959 if (per_cu->per_bfd->reading_partial_symbols)
8960 {
8961 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8962 return 1;
8963 return 0;
8964 }
8965
8966 /* Mark the dependence relation so that we don't flush PER_CU
8967 too early. */
8968 if (dependent_cu != NULL)
8969 dwarf2_add_dependence (dependent_cu, per_cu);
8970
8971 /* If it's already on the queue, we have nothing to do. */
8972 if (per_cu->queued)
8973 return 0;
8974
8975 /* If the compilation unit is already loaded, just mark it as
8976 used. */
8977 if (per_cu->cu != NULL)
8978 {
8979 per_cu->cu->last_used = 0;
8980 return 0;
8981 }
8982
8983 /* Add it to the queue. */
8984 queue_comp_unit (per_cu, pretend_language);
8985
8986 return 1;
8987 }
8988
8989 /* Process the queue. */
8990
8991 static void
8992 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8993 {
8994 if (dwarf_read_debug)
8995 {
8996 fprintf_unfiltered (gdb_stdlog,
8997 "Expanding one or more symtabs of objfile %s ...\n",
8998 objfile_name (dwarf2_per_objfile->objfile));
8999 }
9000
9001 /* The queue starts out with one item, but following a DIE reference
9002 may load a new CU, adding it to the end of the queue. */
9003 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
9004 {
9005 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
9006
9007 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
9008 /* Skip dummy CUs. */
9009 && item.per_cu->cu != NULL)
9010 {
9011 struct dwarf2_per_cu_data *per_cu = item.per_cu;
9012 unsigned int debug_print_threshold;
9013 char buf[100];
9014
9015 if (per_cu->is_debug_types)
9016 {
9017 struct signatured_type *sig_type =
9018 (struct signatured_type *) per_cu;
9019
9020 sprintf (buf, "TU %s at offset %s",
9021 hex_string (sig_type->signature),
9022 sect_offset_str (per_cu->sect_off));
9023 /* There can be 100s of TUs.
9024 Only print them in verbose mode. */
9025 debug_print_threshold = 2;
9026 }
9027 else
9028 {
9029 sprintf (buf, "CU at offset %s",
9030 sect_offset_str (per_cu->sect_off));
9031 debug_print_threshold = 1;
9032 }
9033
9034 if (dwarf_read_debug >= debug_print_threshold)
9035 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9036
9037 if (per_cu->is_debug_types)
9038 process_full_type_unit (per_cu, item.pretend_language);
9039 else
9040 process_full_comp_unit (per_cu, item.pretend_language);
9041
9042 if (dwarf_read_debug >= debug_print_threshold)
9043 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9044 }
9045
9046 item.per_cu->queued = 0;
9047 dwarf2_per_objfile->per_bfd->queue.pop ();
9048 }
9049
9050 if (dwarf_read_debug)
9051 {
9052 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9053 objfile_name (dwarf2_per_objfile->objfile));
9054 }
9055 }
9056
9057 /* Read in full symbols for PST, and anything it depends on. */
9058
9059 void
9060 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9061 {
9062 gdb_assert (!readin_p (objfile));
9063
9064 expand_dependencies (objfile);
9065
9066 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9067 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9068 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9069 }
9070
9071 /* See psympriv.h. */
9072
9073 bool
9074 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9075 {
9076 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9077 return per_objfile->symtab_set_p (per_cu_data);
9078 }
9079
9080 /* See psympriv.h. */
9081
9082 compunit_symtab *
9083 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9084 {
9085 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9086 return per_objfile->get_symtab (per_cu_data);
9087 }
9088
9089 /* Trivial hash function for die_info: the hash value of a DIE
9090 is its offset in .debug_info for this objfile. */
9091
9092 static hashval_t
9093 die_hash (const void *item)
9094 {
9095 const struct die_info *die = (const struct die_info *) item;
9096
9097 return to_underlying (die->sect_off);
9098 }
9099
9100 /* Trivial comparison function for die_info structures: two DIEs
9101 are equal if they have the same offset. */
9102
9103 static int
9104 die_eq (const void *item_lhs, const void *item_rhs)
9105 {
9106 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9107 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9108
9109 return die_lhs->sect_off == die_rhs->sect_off;
9110 }
9111
9112 /* Load the DIEs associated with PER_CU into memory. */
9113
9114 static void
9115 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9116 dwarf2_per_objfile *per_objfile,
9117 bool skip_partial,
9118 enum language pretend_language)
9119 {
9120 gdb_assert (! this_cu->is_debug_types);
9121
9122 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
9123 if (reader.dummy_p)
9124 return;
9125
9126 struct dwarf2_cu *cu = reader.cu;
9127 const gdb_byte *info_ptr = reader.info_ptr;
9128
9129 gdb_assert (cu->die_hash == NULL);
9130 cu->die_hash =
9131 htab_create_alloc_ex (cu->header.length / 12,
9132 die_hash,
9133 die_eq,
9134 NULL,
9135 &cu->comp_unit_obstack,
9136 hashtab_obstack_allocate,
9137 dummy_obstack_deallocate);
9138
9139 if (reader.comp_unit_die->has_children)
9140 reader.comp_unit_die->child
9141 = read_die_and_siblings (&reader, reader.info_ptr,
9142 &info_ptr, reader.comp_unit_die);
9143 cu->dies = reader.comp_unit_die;
9144 /* comp_unit_die is not stored in die_hash, no need. */
9145
9146 /* We try not to read any attributes in this function, because not
9147 all CUs needed for references have been loaded yet, and symbol
9148 table processing isn't initialized. But we have to set the CU language,
9149 or we won't be able to build types correctly.
9150 Similarly, if we do not read the producer, we can not apply
9151 producer-specific interpretation. */
9152 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9153
9154 reader.keep ();
9155 }
9156
9157 /* Add a DIE to the delayed physname list. */
9158
9159 static void
9160 add_to_method_list (struct type *type, int fnfield_index, int index,
9161 const char *name, struct die_info *die,
9162 struct dwarf2_cu *cu)
9163 {
9164 struct delayed_method_info mi;
9165 mi.type = type;
9166 mi.fnfield_index = fnfield_index;
9167 mi.index = index;
9168 mi.name = name;
9169 mi.die = die;
9170 cu->method_list.push_back (mi);
9171 }
9172
9173 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9174 "const" / "volatile". If so, decrements LEN by the length of the
9175 modifier and return true. Otherwise return false. */
9176
9177 template<size_t N>
9178 static bool
9179 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9180 {
9181 size_t mod_len = sizeof (mod) - 1;
9182 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9183 {
9184 len -= mod_len;
9185 return true;
9186 }
9187 return false;
9188 }
9189
9190 /* Compute the physnames of any methods on the CU's method list.
9191
9192 The computation of method physnames is delayed in order to avoid the
9193 (bad) condition that one of the method's formal parameters is of an as yet
9194 incomplete type. */
9195
9196 static void
9197 compute_delayed_physnames (struct dwarf2_cu *cu)
9198 {
9199 /* Only C++ delays computing physnames. */
9200 if (cu->method_list.empty ())
9201 return;
9202 gdb_assert (cu->language == language_cplus);
9203
9204 for (const delayed_method_info &mi : cu->method_list)
9205 {
9206 const char *physname;
9207 struct fn_fieldlist *fn_flp
9208 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9209 physname = dwarf2_physname (mi.name, mi.die, cu);
9210 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9211 = physname ? physname : "";
9212
9213 /* Since there's no tag to indicate whether a method is a
9214 const/volatile overload, extract that information out of the
9215 demangled name. */
9216 if (physname != NULL)
9217 {
9218 size_t len = strlen (physname);
9219
9220 while (1)
9221 {
9222 if (physname[len] == ')') /* shortcut */
9223 break;
9224 else if (check_modifier (physname, len, " const"))
9225 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9226 else if (check_modifier (physname, len, " volatile"))
9227 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9228 else
9229 break;
9230 }
9231 }
9232 }
9233
9234 /* The list is no longer needed. */
9235 cu->method_list.clear ();
9236 }
9237
9238 /* Go objects should be embedded in a DW_TAG_module DIE,
9239 and it's not clear if/how imported objects will appear.
9240 To keep Go support simple until that's worked out,
9241 go back through what we've read and create something usable.
9242 We could do this while processing each DIE, and feels kinda cleaner,
9243 but that way is more invasive.
9244 This is to, for example, allow the user to type "p var" or "b main"
9245 without having to specify the package name, and allow lookups
9246 of module.object to work in contexts that use the expression
9247 parser. */
9248
9249 static void
9250 fixup_go_packaging (struct dwarf2_cu *cu)
9251 {
9252 gdb::unique_xmalloc_ptr<char> package_name;
9253 struct pending *list;
9254 int i;
9255
9256 for (list = *cu->get_builder ()->get_global_symbols ();
9257 list != NULL;
9258 list = list->next)
9259 {
9260 for (i = 0; i < list->nsyms; ++i)
9261 {
9262 struct symbol *sym = list->symbol[i];
9263
9264 if (sym->language () == language_go
9265 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9266 {
9267 gdb::unique_xmalloc_ptr<char> this_package_name
9268 (go_symbol_package_name (sym));
9269
9270 if (this_package_name == NULL)
9271 continue;
9272 if (package_name == NULL)
9273 package_name = std::move (this_package_name);
9274 else
9275 {
9276 struct objfile *objfile = cu->per_objfile->objfile;
9277 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9278 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9279 (symbol_symtab (sym) != NULL
9280 ? symtab_to_filename_for_display
9281 (symbol_symtab (sym))
9282 : objfile_name (objfile)),
9283 this_package_name.get (), package_name.get ());
9284 }
9285 }
9286 }
9287 }
9288
9289 if (package_name != NULL)
9290 {
9291 struct objfile *objfile = cu->per_objfile->objfile;
9292 const char *saved_package_name = objfile->intern (package_name.get ());
9293 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9294 saved_package_name);
9295 struct symbol *sym;
9296
9297 sym = new (&objfile->objfile_obstack) symbol;
9298 sym->set_language (language_go, &objfile->objfile_obstack);
9299 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9300 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9301 e.g., "main" finds the "main" module and not C's main(). */
9302 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9303 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9304 SYMBOL_TYPE (sym) = type;
9305
9306 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9307 }
9308 }
9309
9310 /* Allocate a fully-qualified name consisting of the two parts on the
9311 obstack. */
9312
9313 static const char *
9314 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9315 {
9316 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9317 }
9318
9319 /* A helper that allocates a variant part to attach to a Rust enum
9320 type. OBSTACK is where the results should be allocated. TYPE is
9321 the type we're processing. DISCRIMINANT_INDEX is the index of the
9322 discriminant. It must be the index of one of the fields of TYPE.
9323 DEFAULT_INDEX is the index of the default field; or -1 if there is
9324 no default. RANGES is indexed by "effective" field number (the
9325 field index, but omitting the discriminant and default fields) and
9326 must hold the discriminant values used by the variants. Note that
9327 RANGES must have a lifetime at least as long as OBSTACK -- either
9328 already allocated on it, or static. */
9329
9330 static void
9331 alloc_rust_variant (struct obstack *obstack, struct type *type,
9332 int discriminant_index, int default_index,
9333 gdb::array_view<discriminant_range> ranges)
9334 {
9335 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9336 must be handled by the caller. */
9337 gdb_assert (discriminant_index >= 0
9338 && discriminant_index < type->num_fields ());
9339 gdb_assert (default_index == -1
9340 || (default_index >= 0 && default_index < type->num_fields ()));
9341
9342 /* We have one variant for each non-discriminant field. */
9343 int n_variants = type->num_fields () - 1;
9344
9345 variant *variants = new (obstack) variant[n_variants];
9346 int var_idx = 0;
9347 int range_idx = 0;
9348 for (int i = 0; i < type->num_fields (); ++i)
9349 {
9350 if (i == discriminant_index)
9351 continue;
9352
9353 variants[var_idx].first_field = i;
9354 variants[var_idx].last_field = i + 1;
9355
9356 /* The default field does not need a range, but other fields do.
9357 We skipped the discriminant above. */
9358 if (i != default_index)
9359 {
9360 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9361 ++range_idx;
9362 }
9363
9364 ++var_idx;
9365 }
9366
9367 gdb_assert (range_idx == ranges.size ());
9368 gdb_assert (var_idx == n_variants);
9369
9370 variant_part *part = new (obstack) variant_part;
9371 part->discriminant_index = discriminant_index;
9372 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9373 discriminant_index));
9374 part->variants = gdb::array_view<variant> (variants, n_variants);
9375
9376 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9377 gdb::array_view<variant_part> *prop_value
9378 = new (storage) gdb::array_view<variant_part> (part, 1);
9379
9380 struct dynamic_prop prop;
9381 prop.kind = PROP_VARIANT_PARTS;
9382 prop.data.variant_parts = prop_value;
9383
9384 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9385 }
9386
9387 /* Some versions of rustc emitted enums in an unusual way.
9388
9389 Ordinary enums were emitted as unions. The first element of each
9390 structure in the union was named "RUST$ENUM$DISR". This element
9391 held the discriminant.
9392
9393 These versions of Rust also implemented the "non-zero"
9394 optimization. When the enum had two values, and one is empty and
9395 the other holds a pointer that cannot be zero, the pointer is used
9396 as the discriminant, with a zero value meaning the empty variant.
9397 Here, the union's first member is of the form
9398 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9399 where the fieldnos are the indices of the fields that should be
9400 traversed in order to find the field (which may be several fields deep)
9401 and the variantname is the name of the variant of the case when the
9402 field is zero.
9403
9404 This function recognizes whether TYPE is of one of these forms,
9405 and, if so, smashes it to be a variant type. */
9406
9407 static void
9408 quirk_rust_enum (struct type *type, struct objfile *objfile)
9409 {
9410 gdb_assert (type->code () == TYPE_CODE_UNION);
9411
9412 /* We don't need to deal with empty enums. */
9413 if (type->num_fields () == 0)
9414 return;
9415
9416 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9417 if (type->num_fields () == 1
9418 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9419 {
9420 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9421
9422 /* Decode the field name to find the offset of the
9423 discriminant. */
9424 ULONGEST bit_offset = 0;
9425 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9426 while (name[0] >= '0' && name[0] <= '9')
9427 {
9428 char *tail;
9429 unsigned long index = strtoul (name, &tail, 10);
9430 name = tail;
9431 if (*name != '$'
9432 || index >= field_type->num_fields ()
9433 || (TYPE_FIELD_LOC_KIND (field_type, index)
9434 != FIELD_LOC_KIND_BITPOS))
9435 {
9436 complaint (_("Could not parse Rust enum encoding string \"%s\""
9437 "[in module %s]"),
9438 TYPE_FIELD_NAME (type, 0),
9439 objfile_name (objfile));
9440 return;
9441 }
9442 ++name;
9443
9444 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9445 field_type = TYPE_FIELD_TYPE (field_type, index);
9446 }
9447
9448 /* Smash this type to be a structure type. We have to do this
9449 because the type has already been recorded. */
9450 type->set_code (TYPE_CODE_STRUCT);
9451 type->set_num_fields (3);
9452 /* Save the field we care about. */
9453 struct field saved_field = type->field (0);
9454 type->set_fields
9455 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9456
9457 /* Put the discriminant at index 0. */
9458 TYPE_FIELD_TYPE (type, 0) = field_type;
9459 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9460 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9461 SET_FIELD_BITPOS (type->field (0), bit_offset);
9462
9463 /* The order of fields doesn't really matter, so put the real
9464 field at index 1 and the data-less field at index 2. */
9465 type->field (1) = saved_field;
9466 TYPE_FIELD_NAME (type, 1)
9467 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9468 TYPE_FIELD_TYPE (type, 1)->set_name
9469 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9470 TYPE_FIELD_NAME (type, 1)));
9471
9472 const char *dataless_name
9473 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9474 name);
9475 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9476 dataless_name);
9477 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9478 /* NAME points into the original discriminant name, which
9479 already has the correct lifetime. */
9480 TYPE_FIELD_NAME (type, 2) = name;
9481 SET_FIELD_BITPOS (type->field (2), 0);
9482
9483 /* Indicate that this is a variant type. */
9484 static discriminant_range ranges[1] = { { 0, 0 } };
9485 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9486 }
9487 /* A union with a single anonymous field is probably an old-style
9488 univariant enum. */
9489 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9490 {
9491 /* Smash this type to be a structure type. We have to do this
9492 because the type has already been recorded. */
9493 type->set_code (TYPE_CODE_STRUCT);
9494
9495 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9496 const char *variant_name
9497 = rust_last_path_segment (field_type->name ());
9498 TYPE_FIELD_NAME (type, 0) = variant_name;
9499 field_type->set_name
9500 (rust_fully_qualify (&objfile->objfile_obstack,
9501 type->name (), variant_name));
9502 }
9503 else
9504 {
9505 struct type *disr_type = nullptr;
9506 for (int i = 0; i < type->num_fields (); ++i)
9507 {
9508 disr_type = TYPE_FIELD_TYPE (type, i);
9509
9510 if (disr_type->code () != TYPE_CODE_STRUCT)
9511 {
9512 /* All fields of a true enum will be structs. */
9513 return;
9514 }
9515 else if (disr_type->num_fields () == 0)
9516 {
9517 /* Could be data-less variant, so keep going. */
9518 disr_type = nullptr;
9519 }
9520 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9521 "RUST$ENUM$DISR") != 0)
9522 {
9523 /* Not a Rust enum. */
9524 return;
9525 }
9526 else
9527 {
9528 /* Found one. */
9529 break;
9530 }
9531 }
9532
9533 /* If we got here without a discriminant, then it's probably
9534 just a union. */
9535 if (disr_type == nullptr)
9536 return;
9537
9538 /* Smash this type to be a structure type. We have to do this
9539 because the type has already been recorded. */
9540 type->set_code (TYPE_CODE_STRUCT);
9541
9542 /* Make space for the discriminant field. */
9543 struct field *disr_field = &disr_type->field (0);
9544 field *new_fields
9545 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9546 * sizeof (struct field)));
9547 memcpy (new_fields + 1, type->fields (),
9548 type->num_fields () * sizeof (struct field));
9549 type->set_fields (new_fields);
9550 type->set_num_fields (type->num_fields () + 1);
9551
9552 /* Install the discriminant at index 0 in the union. */
9553 type->field (0) = *disr_field;
9554 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9555 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9556
9557 /* We need a way to find the correct discriminant given a
9558 variant name. For convenience we build a map here. */
9559 struct type *enum_type = FIELD_TYPE (*disr_field);
9560 std::unordered_map<std::string, ULONGEST> discriminant_map;
9561 for (int i = 0; i < enum_type->num_fields (); ++i)
9562 {
9563 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9564 {
9565 const char *name
9566 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9567 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9568 }
9569 }
9570
9571 int n_fields = type->num_fields ();
9572 /* We don't need a range entry for the discriminant, but we do
9573 need one for every other field, as there is no default
9574 variant. */
9575 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9576 discriminant_range,
9577 n_fields - 1);
9578 /* Skip the discriminant here. */
9579 for (int i = 1; i < n_fields; ++i)
9580 {
9581 /* Find the final word in the name of this variant's type.
9582 That name can be used to look up the correct
9583 discriminant. */
9584 const char *variant_name
9585 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9586
9587 auto iter = discriminant_map.find (variant_name);
9588 if (iter != discriminant_map.end ())
9589 {
9590 ranges[i].low = iter->second;
9591 ranges[i].high = iter->second;
9592 }
9593
9594 /* Remove the discriminant field, if it exists. */
9595 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9596 if (sub_type->num_fields () > 0)
9597 {
9598 sub_type->set_num_fields (sub_type->num_fields () - 1);
9599 sub_type->set_fields (sub_type->fields () + 1);
9600 }
9601 TYPE_FIELD_NAME (type, i) = variant_name;
9602 sub_type->set_name
9603 (rust_fully_qualify (&objfile->objfile_obstack,
9604 type->name (), variant_name));
9605 }
9606
9607 /* Indicate that this is a variant type. */
9608 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9609 gdb::array_view<discriminant_range> (ranges,
9610 n_fields - 1));
9611 }
9612 }
9613
9614 /* Rewrite some Rust unions to be structures with variants parts. */
9615
9616 static void
9617 rust_union_quirks (struct dwarf2_cu *cu)
9618 {
9619 gdb_assert (cu->language == language_rust);
9620 for (type *type_ : cu->rust_unions)
9621 quirk_rust_enum (type_, cu->per_objfile->objfile);
9622 /* We don't need this any more. */
9623 cu->rust_unions.clear ();
9624 }
9625
9626 /* A helper function for computing the list of all symbol tables
9627 included by PER_CU. */
9628
9629 static void
9630 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9631 htab_t all_children, htab_t all_type_symtabs,
9632 struct dwarf2_per_cu_data *per_cu,
9633 struct compunit_symtab *immediate_parent)
9634 {
9635 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9636 if (*slot != NULL)
9637 {
9638 /* This inclusion and its children have been processed. */
9639 return;
9640 }
9641
9642 *slot = per_cu;
9643
9644 /* Only add a CU if it has a symbol table. */
9645 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
9646 if (cust != NULL)
9647 {
9648 /* If this is a type unit only add its symbol table if we haven't
9649 seen it yet (type unit per_cu's can share symtabs). */
9650 if (per_cu->is_debug_types)
9651 {
9652 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9653 if (*slot == NULL)
9654 {
9655 *slot = cust;
9656 result->push_back (cust);
9657 if (cust->user == NULL)
9658 cust->user = immediate_parent;
9659 }
9660 }
9661 else
9662 {
9663 result->push_back (cust);
9664 if (cust->user == NULL)
9665 cust->user = immediate_parent;
9666 }
9667 }
9668
9669 if (!per_cu->imported_symtabs_empty ())
9670 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9671 {
9672 recursively_compute_inclusions (result, all_children,
9673 all_type_symtabs, ptr, cust);
9674 }
9675 }
9676
9677 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9678 PER_CU. */
9679
9680 static void
9681 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9682 {
9683 gdb_assert (! per_cu->is_debug_types);
9684
9685 if (!per_cu->imported_symtabs_empty ())
9686 {
9687 int len;
9688 std::vector<compunit_symtab *> result_symtabs;
9689 htab_t all_children, all_type_symtabs;
9690 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
9691
9692 /* If we don't have a symtab, we can just skip this case. */
9693 if (cust == NULL)
9694 return;
9695
9696 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9697 NULL, xcalloc, xfree);
9698 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9699 NULL, xcalloc, xfree);
9700
9701 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9702 {
9703 recursively_compute_inclusions (&result_symtabs, all_children,
9704 all_type_symtabs, ptr, cust);
9705 }
9706
9707 /* Now we have a transitive closure of all the included symtabs. */
9708 len = result_symtabs.size ();
9709 cust->includes
9710 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9711 struct compunit_symtab *, len + 1);
9712 memcpy (cust->includes, result_symtabs.data (),
9713 len * sizeof (compunit_symtab *));
9714 cust->includes[len] = NULL;
9715
9716 htab_delete (all_children);
9717 htab_delete (all_type_symtabs);
9718 }
9719 }
9720
9721 /* Compute the 'includes' field for the symtabs of all the CUs we just
9722 read. */
9723
9724 static void
9725 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9726 {
9727 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9728 {
9729 if (! iter->is_debug_types)
9730 compute_compunit_symtab_includes (iter);
9731 }
9732
9733 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9734 }
9735
9736 /* Generate full symbol information for PER_CU, whose DIEs have
9737 already been loaded into memory. */
9738
9739 static void
9740 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9741 enum language pretend_language)
9742 {
9743 struct dwarf2_cu *cu = per_cu->cu;
9744 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9745 struct objfile *objfile = dwarf2_per_objfile->objfile;
9746 struct gdbarch *gdbarch = objfile->arch ();
9747 CORE_ADDR lowpc, highpc;
9748 struct compunit_symtab *cust;
9749 CORE_ADDR baseaddr;
9750 struct block *static_block;
9751 CORE_ADDR addr;
9752
9753 baseaddr = objfile->text_section_offset ();
9754
9755 /* Clear the list here in case something was left over. */
9756 cu->method_list.clear ();
9757
9758 cu->language = pretend_language;
9759 cu->language_defn = language_def (cu->language);
9760
9761 /* Do line number decoding in read_file_scope () */
9762 process_die (cu->dies, cu);
9763
9764 /* For now fudge the Go package. */
9765 if (cu->language == language_go)
9766 fixup_go_packaging (cu);
9767
9768 /* Now that we have processed all the DIEs in the CU, all the types
9769 should be complete, and it should now be safe to compute all of the
9770 physnames. */
9771 compute_delayed_physnames (cu);
9772
9773 if (cu->language == language_rust)
9774 rust_union_quirks (cu);
9775
9776 /* Some compilers don't define a DW_AT_high_pc attribute for the
9777 compilation unit. If the DW_AT_high_pc is missing, synthesize
9778 it, by scanning the DIE's below the compilation unit. */
9779 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9780
9781 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9782 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9783
9784 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9785 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9786 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9787 addrmap to help ensure it has an accurate map of pc values belonging to
9788 this comp unit. */
9789 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9790
9791 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9792 SECT_OFF_TEXT (objfile),
9793 0);
9794
9795 if (cust != NULL)
9796 {
9797 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9798
9799 /* Set symtab language to language from DW_AT_language. If the
9800 compilation is from a C file generated by language preprocessors, do
9801 not set the language if it was already deduced by start_subfile. */
9802 if (!(cu->language == language_c
9803 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9804 COMPUNIT_FILETABS (cust)->language = cu->language;
9805
9806 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9807 produce DW_AT_location with location lists but it can be possibly
9808 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9809 there were bugs in prologue debug info, fixed later in GCC-4.5
9810 by "unwind info for epilogues" patch (which is not directly related).
9811
9812 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9813 needed, it would be wrong due to missing DW_AT_producer there.
9814
9815 Still one can confuse GDB by using non-standard GCC compilation
9816 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9817 */
9818 if (cu->has_loclist && gcc_4_minor >= 5)
9819 cust->locations_valid = 1;
9820
9821 if (gcc_4_minor >= 5)
9822 cust->epilogue_unwind_valid = 1;
9823
9824 cust->call_site_htab = cu->call_site_htab;
9825 }
9826
9827 dwarf2_per_objfile->set_symtab (per_cu, cust);
9828
9829 /* Push it for inclusion processing later. */
9830 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9831
9832 /* Not needed any more. */
9833 cu->reset_builder ();
9834 }
9835
9836 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9837 already been loaded into memory. */
9838
9839 static void
9840 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9841 enum language pretend_language)
9842 {
9843 struct dwarf2_cu *cu = per_cu->cu;
9844 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9845 struct objfile *objfile = dwarf2_per_objfile->objfile;
9846 struct compunit_symtab *cust;
9847 struct signatured_type *sig_type;
9848
9849 gdb_assert (per_cu->is_debug_types);
9850 sig_type = (struct signatured_type *) per_cu;
9851
9852 /* Clear the list here in case something was left over. */
9853 cu->method_list.clear ();
9854
9855 cu->language = pretend_language;
9856 cu->language_defn = language_def (cu->language);
9857
9858 /* The symbol tables are set up in read_type_unit_scope. */
9859 process_die (cu->dies, cu);
9860
9861 /* For now fudge the Go package. */
9862 if (cu->language == language_go)
9863 fixup_go_packaging (cu);
9864
9865 /* Now that we have processed all the DIEs in the CU, all the types
9866 should be complete, and it should now be safe to compute all of the
9867 physnames. */
9868 compute_delayed_physnames (cu);
9869
9870 if (cu->language == language_rust)
9871 rust_union_quirks (cu);
9872
9873 /* TUs share symbol tables.
9874 If this is the first TU to use this symtab, complete the construction
9875 of it with end_expandable_symtab. Otherwise, complete the addition of
9876 this TU's symbols to the existing symtab. */
9877 if (sig_type->type_unit_group->compunit_symtab == NULL)
9878 {
9879 buildsym_compunit *builder = cu->get_builder ();
9880 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9881 sig_type->type_unit_group->compunit_symtab = cust;
9882
9883 if (cust != NULL)
9884 {
9885 /* Set symtab language to language from DW_AT_language. If the
9886 compilation is from a C file generated by language preprocessors,
9887 do not set the language if it was already deduced by
9888 start_subfile. */
9889 if (!(cu->language == language_c
9890 && COMPUNIT_FILETABS (cust)->language != language_c))
9891 COMPUNIT_FILETABS (cust)->language = cu->language;
9892 }
9893 }
9894 else
9895 {
9896 cu->get_builder ()->augment_type_symtab ();
9897 cust = sig_type->type_unit_group->compunit_symtab;
9898 }
9899
9900 dwarf2_per_objfile->set_symtab (per_cu, cust);
9901
9902 /* Not needed any more. */
9903 cu->reset_builder ();
9904 }
9905
9906 /* Process an imported unit DIE. */
9907
9908 static void
9909 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9910 {
9911 struct attribute *attr;
9912
9913 /* For now we don't handle imported units in type units. */
9914 if (cu->per_cu->is_debug_types)
9915 {
9916 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9917 " supported in type units [in module %s]"),
9918 objfile_name (cu->per_objfile->objfile));
9919 }
9920
9921 attr = dwarf2_attr (die, DW_AT_import, cu);
9922 if (attr != NULL)
9923 {
9924 sect_offset sect_off = attr->get_ref_die_offset ();
9925 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9926 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9927 dwarf2_per_cu_data *per_cu
9928 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9929
9930 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9931 into another compilation unit, at root level. Regard this as a hint,
9932 and ignore it. */
9933 if (die->parent && die->parent->parent == NULL
9934 && per_cu->unit_type == DW_UT_compile
9935 && per_cu->lang == language_cplus)
9936 return;
9937
9938 /* If necessary, add it to the queue and load its DIEs. */
9939 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9940 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
9941
9942 cu->per_cu->imported_symtabs_push (per_cu);
9943 }
9944 }
9945
9946 /* RAII object that represents a process_die scope: i.e.,
9947 starts/finishes processing a DIE. */
9948 class process_die_scope
9949 {
9950 public:
9951 process_die_scope (die_info *die, dwarf2_cu *cu)
9952 : m_die (die), m_cu (cu)
9953 {
9954 /* We should only be processing DIEs not already in process. */
9955 gdb_assert (!m_die->in_process);
9956 m_die->in_process = true;
9957 }
9958
9959 ~process_die_scope ()
9960 {
9961 m_die->in_process = false;
9962
9963 /* If we're done processing the DIE for the CU that owns the line
9964 header, we don't need the line header anymore. */
9965 if (m_cu->line_header_die_owner == m_die)
9966 {
9967 delete m_cu->line_header;
9968 m_cu->line_header = NULL;
9969 m_cu->line_header_die_owner = NULL;
9970 }
9971 }
9972
9973 private:
9974 die_info *m_die;
9975 dwarf2_cu *m_cu;
9976 };
9977
9978 /* Process a die and its children. */
9979
9980 static void
9981 process_die (struct die_info *die, struct dwarf2_cu *cu)
9982 {
9983 process_die_scope scope (die, cu);
9984
9985 switch (die->tag)
9986 {
9987 case DW_TAG_padding:
9988 break;
9989 case DW_TAG_compile_unit:
9990 case DW_TAG_partial_unit:
9991 read_file_scope (die, cu);
9992 break;
9993 case DW_TAG_type_unit:
9994 read_type_unit_scope (die, cu);
9995 break;
9996 case DW_TAG_subprogram:
9997 /* Nested subprograms in Fortran get a prefix. */
9998 if (cu->language == language_fortran
9999 && die->parent != NULL
10000 && die->parent->tag == DW_TAG_subprogram)
10001 cu->processing_has_namespace_info = true;
10002 /* Fall through. */
10003 case DW_TAG_inlined_subroutine:
10004 read_func_scope (die, cu);
10005 break;
10006 case DW_TAG_lexical_block:
10007 case DW_TAG_try_block:
10008 case DW_TAG_catch_block:
10009 read_lexical_block_scope (die, cu);
10010 break;
10011 case DW_TAG_call_site:
10012 case DW_TAG_GNU_call_site:
10013 read_call_site_scope (die, cu);
10014 break;
10015 case DW_TAG_class_type:
10016 case DW_TAG_interface_type:
10017 case DW_TAG_structure_type:
10018 case DW_TAG_union_type:
10019 process_structure_scope (die, cu);
10020 break;
10021 case DW_TAG_enumeration_type:
10022 process_enumeration_scope (die, cu);
10023 break;
10024
10025 /* These dies have a type, but processing them does not create
10026 a symbol or recurse to process the children. Therefore we can
10027 read them on-demand through read_type_die. */
10028 case DW_TAG_subroutine_type:
10029 case DW_TAG_set_type:
10030 case DW_TAG_array_type:
10031 case DW_TAG_pointer_type:
10032 case DW_TAG_ptr_to_member_type:
10033 case DW_TAG_reference_type:
10034 case DW_TAG_rvalue_reference_type:
10035 case DW_TAG_string_type:
10036 break;
10037
10038 case DW_TAG_base_type:
10039 case DW_TAG_subrange_type:
10040 case DW_TAG_typedef:
10041 /* Add a typedef symbol for the type definition, if it has a
10042 DW_AT_name. */
10043 new_symbol (die, read_type_die (die, cu), cu);
10044 break;
10045 case DW_TAG_common_block:
10046 read_common_block (die, cu);
10047 break;
10048 case DW_TAG_common_inclusion:
10049 break;
10050 case DW_TAG_namespace:
10051 cu->processing_has_namespace_info = true;
10052 read_namespace (die, cu);
10053 break;
10054 case DW_TAG_module:
10055 cu->processing_has_namespace_info = true;
10056 read_module (die, cu);
10057 break;
10058 case DW_TAG_imported_declaration:
10059 cu->processing_has_namespace_info = true;
10060 if (read_namespace_alias (die, cu))
10061 break;
10062 /* The declaration is not a global namespace alias. */
10063 /* Fall through. */
10064 case DW_TAG_imported_module:
10065 cu->processing_has_namespace_info = true;
10066 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10067 || cu->language != language_fortran))
10068 complaint (_("Tag '%s' has unexpected children"),
10069 dwarf_tag_name (die->tag));
10070 read_import_statement (die, cu);
10071 break;
10072
10073 case DW_TAG_imported_unit:
10074 process_imported_unit_die (die, cu);
10075 break;
10076
10077 case DW_TAG_variable:
10078 read_variable (die, cu);
10079 break;
10080
10081 default:
10082 new_symbol (die, NULL, cu);
10083 break;
10084 }
10085 }
10086 \f
10087 /* DWARF name computation. */
10088
10089 /* A helper function for dwarf2_compute_name which determines whether DIE
10090 needs to have the name of the scope prepended to the name listed in the
10091 die. */
10092
10093 static int
10094 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10095 {
10096 struct attribute *attr;
10097
10098 switch (die->tag)
10099 {
10100 case DW_TAG_namespace:
10101 case DW_TAG_typedef:
10102 case DW_TAG_class_type:
10103 case DW_TAG_interface_type:
10104 case DW_TAG_structure_type:
10105 case DW_TAG_union_type:
10106 case DW_TAG_enumeration_type:
10107 case DW_TAG_enumerator:
10108 case DW_TAG_subprogram:
10109 case DW_TAG_inlined_subroutine:
10110 case DW_TAG_member:
10111 case DW_TAG_imported_declaration:
10112 return 1;
10113
10114 case DW_TAG_variable:
10115 case DW_TAG_constant:
10116 /* We only need to prefix "globally" visible variables. These include
10117 any variable marked with DW_AT_external or any variable that
10118 lives in a namespace. [Variables in anonymous namespaces
10119 require prefixing, but they are not DW_AT_external.] */
10120
10121 if (dwarf2_attr (die, DW_AT_specification, cu))
10122 {
10123 struct dwarf2_cu *spec_cu = cu;
10124
10125 return die_needs_namespace (die_specification (die, &spec_cu),
10126 spec_cu);
10127 }
10128
10129 attr = dwarf2_attr (die, DW_AT_external, cu);
10130 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10131 && die->parent->tag != DW_TAG_module)
10132 return 0;
10133 /* A variable in a lexical block of some kind does not need a
10134 namespace, even though in C++ such variables may be external
10135 and have a mangled name. */
10136 if (die->parent->tag == DW_TAG_lexical_block
10137 || die->parent->tag == DW_TAG_try_block
10138 || die->parent->tag == DW_TAG_catch_block
10139 || die->parent->tag == DW_TAG_subprogram)
10140 return 0;
10141 return 1;
10142
10143 default:
10144 return 0;
10145 }
10146 }
10147
10148 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10149 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10150 defined for the given DIE. */
10151
10152 static struct attribute *
10153 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10154 {
10155 struct attribute *attr;
10156
10157 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10158 if (attr == NULL)
10159 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10160
10161 return attr;
10162 }
10163
10164 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10165 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10166 defined for the given DIE. */
10167
10168 static const char *
10169 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10170 {
10171 const char *linkage_name;
10172
10173 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10174 if (linkage_name == NULL)
10175 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10176
10177 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10178 See https://github.com/rust-lang/rust/issues/32925. */
10179 if (cu->language == language_rust && linkage_name != NULL
10180 && strchr (linkage_name, '{') != NULL)
10181 linkage_name = NULL;
10182
10183 return linkage_name;
10184 }
10185
10186 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10187 compute the physname for the object, which include a method's:
10188 - formal parameters (C++),
10189 - receiver type (Go),
10190
10191 The term "physname" is a bit confusing.
10192 For C++, for example, it is the demangled name.
10193 For Go, for example, it's the mangled name.
10194
10195 For Ada, return the DIE's linkage name rather than the fully qualified
10196 name. PHYSNAME is ignored..
10197
10198 The result is allocated on the objfile->per_bfd's obstack and
10199 canonicalized. */
10200
10201 static const char *
10202 dwarf2_compute_name (const char *name,
10203 struct die_info *die, struct dwarf2_cu *cu,
10204 int physname)
10205 {
10206 struct objfile *objfile = cu->per_objfile->objfile;
10207
10208 if (name == NULL)
10209 name = dwarf2_name (die, cu);
10210
10211 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10212 but otherwise compute it by typename_concat inside GDB.
10213 FIXME: Actually this is not really true, or at least not always true.
10214 It's all very confusing. compute_and_set_names doesn't try to demangle
10215 Fortran names because there is no mangling standard. So new_symbol
10216 will set the demangled name to the result of dwarf2_full_name, and it is
10217 the demangled name that GDB uses if it exists. */
10218 if (cu->language == language_ada
10219 || (cu->language == language_fortran && physname))
10220 {
10221 /* For Ada unit, we prefer the linkage name over the name, as
10222 the former contains the exported name, which the user expects
10223 to be able to reference. Ideally, we want the user to be able
10224 to reference this entity using either natural or linkage name,
10225 but we haven't started looking at this enhancement yet. */
10226 const char *linkage_name = dw2_linkage_name (die, cu);
10227
10228 if (linkage_name != NULL)
10229 return linkage_name;
10230 }
10231
10232 /* These are the only languages we know how to qualify names in. */
10233 if (name != NULL
10234 && (cu->language == language_cplus
10235 || cu->language == language_fortran || cu->language == language_d
10236 || cu->language == language_rust))
10237 {
10238 if (die_needs_namespace (die, cu))
10239 {
10240 const char *prefix;
10241 const char *canonical_name = NULL;
10242
10243 string_file buf;
10244
10245 prefix = determine_prefix (die, cu);
10246 if (*prefix != '\0')
10247 {
10248 gdb::unique_xmalloc_ptr<char> prefixed_name
10249 (typename_concat (NULL, prefix, name, physname, cu));
10250
10251 buf.puts (prefixed_name.get ());
10252 }
10253 else
10254 buf.puts (name);
10255
10256 /* Template parameters may be specified in the DIE's DW_AT_name, or
10257 as children with DW_TAG_template_type_param or
10258 DW_TAG_value_type_param. If the latter, add them to the name
10259 here. If the name already has template parameters, then
10260 skip this step; some versions of GCC emit both, and
10261 it is more efficient to use the pre-computed name.
10262
10263 Something to keep in mind about this process: it is very
10264 unlikely, or in some cases downright impossible, to produce
10265 something that will match the mangled name of a function.
10266 If the definition of the function has the same debug info,
10267 we should be able to match up with it anyway. But fallbacks
10268 using the minimal symbol, for instance to find a method
10269 implemented in a stripped copy of libstdc++, will not work.
10270 If we do not have debug info for the definition, we will have to
10271 match them up some other way.
10272
10273 When we do name matching there is a related problem with function
10274 templates; two instantiated function templates are allowed to
10275 differ only by their return types, which we do not add here. */
10276
10277 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10278 {
10279 struct attribute *attr;
10280 struct die_info *child;
10281 int first = 1;
10282
10283 die->building_fullname = 1;
10284
10285 for (child = die->child; child != NULL; child = child->sibling)
10286 {
10287 struct type *type;
10288 LONGEST value;
10289 const gdb_byte *bytes;
10290 struct dwarf2_locexpr_baton *baton;
10291 struct value *v;
10292
10293 if (child->tag != DW_TAG_template_type_param
10294 && child->tag != DW_TAG_template_value_param)
10295 continue;
10296
10297 if (first)
10298 {
10299 buf.puts ("<");
10300 first = 0;
10301 }
10302 else
10303 buf.puts (", ");
10304
10305 attr = dwarf2_attr (child, DW_AT_type, cu);
10306 if (attr == NULL)
10307 {
10308 complaint (_("template parameter missing DW_AT_type"));
10309 buf.puts ("UNKNOWN_TYPE");
10310 continue;
10311 }
10312 type = die_type (child, cu);
10313
10314 if (child->tag == DW_TAG_template_type_param)
10315 {
10316 c_print_type (type, "", &buf, -1, 0, cu->language,
10317 &type_print_raw_options);
10318 continue;
10319 }
10320
10321 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10322 if (attr == NULL)
10323 {
10324 complaint (_("template parameter missing "
10325 "DW_AT_const_value"));
10326 buf.puts ("UNKNOWN_VALUE");
10327 continue;
10328 }
10329
10330 dwarf2_const_value_attr (attr, type, name,
10331 &cu->comp_unit_obstack, cu,
10332 &value, &bytes, &baton);
10333
10334 if (TYPE_NOSIGN (type))
10335 /* GDB prints characters as NUMBER 'CHAR'. If that's
10336 changed, this can use value_print instead. */
10337 c_printchar (value, type, &buf);
10338 else
10339 {
10340 struct value_print_options opts;
10341
10342 if (baton != NULL)
10343 v = dwarf2_evaluate_loc_desc (type, NULL,
10344 baton->data,
10345 baton->size,
10346 baton->per_cu);
10347 else if (bytes != NULL)
10348 {
10349 v = allocate_value (type);
10350 memcpy (value_contents_writeable (v), bytes,
10351 TYPE_LENGTH (type));
10352 }
10353 else
10354 v = value_from_longest (type, value);
10355
10356 /* Specify decimal so that we do not depend on
10357 the radix. */
10358 get_formatted_print_options (&opts, 'd');
10359 opts.raw = 1;
10360 value_print (v, &buf, &opts);
10361 release_value (v);
10362 }
10363 }
10364
10365 die->building_fullname = 0;
10366
10367 if (!first)
10368 {
10369 /* Close the argument list, with a space if necessary
10370 (nested templates). */
10371 if (!buf.empty () && buf.string ().back () == '>')
10372 buf.puts (" >");
10373 else
10374 buf.puts (">");
10375 }
10376 }
10377
10378 /* For C++ methods, append formal parameter type
10379 information, if PHYSNAME. */
10380
10381 if (physname && die->tag == DW_TAG_subprogram
10382 && cu->language == language_cplus)
10383 {
10384 struct type *type = read_type_die (die, cu);
10385
10386 c_type_print_args (type, &buf, 1, cu->language,
10387 &type_print_raw_options);
10388
10389 if (cu->language == language_cplus)
10390 {
10391 /* Assume that an artificial first parameter is
10392 "this", but do not crash if it is not. RealView
10393 marks unnamed (and thus unused) parameters as
10394 artificial; there is no way to differentiate
10395 the two cases. */
10396 if (type->num_fields () > 0
10397 && TYPE_FIELD_ARTIFICIAL (type, 0)
10398 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10399 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10400 0))))
10401 buf.puts (" const");
10402 }
10403 }
10404
10405 const std::string &intermediate_name = buf.string ();
10406
10407 if (cu->language == language_cplus)
10408 canonical_name
10409 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10410 objfile);
10411
10412 /* If we only computed INTERMEDIATE_NAME, or if
10413 INTERMEDIATE_NAME is already canonical, then we need to
10414 intern it. */
10415 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10416 name = objfile->intern (intermediate_name);
10417 else
10418 name = canonical_name;
10419 }
10420 }
10421
10422 return name;
10423 }
10424
10425 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10426 If scope qualifiers are appropriate they will be added. The result
10427 will be allocated on the storage_obstack, or NULL if the DIE does
10428 not have a name. NAME may either be from a previous call to
10429 dwarf2_name or NULL.
10430
10431 The output string will be canonicalized (if C++). */
10432
10433 static const char *
10434 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10435 {
10436 return dwarf2_compute_name (name, die, cu, 0);
10437 }
10438
10439 /* Construct a physname for the given DIE in CU. NAME may either be
10440 from a previous call to dwarf2_name or NULL. The result will be
10441 allocated on the objfile_objstack or NULL if the DIE does not have a
10442 name.
10443
10444 The output string will be canonicalized (if C++). */
10445
10446 static const char *
10447 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10448 {
10449 struct objfile *objfile = cu->per_objfile->objfile;
10450 const char *retval, *mangled = NULL, *canon = NULL;
10451 int need_copy = 1;
10452
10453 /* In this case dwarf2_compute_name is just a shortcut not building anything
10454 on its own. */
10455 if (!die_needs_namespace (die, cu))
10456 return dwarf2_compute_name (name, die, cu, 1);
10457
10458 if (cu->language != language_rust)
10459 mangled = dw2_linkage_name (die, cu);
10460
10461 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10462 has computed. */
10463 gdb::unique_xmalloc_ptr<char> demangled;
10464 if (mangled != NULL)
10465 {
10466
10467 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10468 {
10469 /* Do nothing (do not demangle the symbol name). */
10470 }
10471 else if (cu->language == language_go)
10472 {
10473 /* This is a lie, but we already lie to the caller new_symbol.
10474 new_symbol assumes we return the mangled name.
10475 This just undoes that lie until things are cleaned up. */
10476 }
10477 else
10478 {
10479 /* Use DMGL_RET_DROP for C++ template functions to suppress
10480 their return type. It is easier for GDB users to search
10481 for such functions as `name(params)' than `long name(params)'.
10482 In such case the minimal symbol names do not match the full
10483 symbol names but for template functions there is never a need
10484 to look up their definition from their declaration so
10485 the only disadvantage remains the minimal symbol variant
10486 `long name(params)' does not have the proper inferior type. */
10487 demangled.reset (gdb_demangle (mangled,
10488 (DMGL_PARAMS | DMGL_ANSI
10489 | DMGL_RET_DROP)));
10490 }
10491 if (demangled)
10492 canon = demangled.get ();
10493 else
10494 {
10495 canon = mangled;
10496 need_copy = 0;
10497 }
10498 }
10499
10500 if (canon == NULL || check_physname)
10501 {
10502 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10503
10504 if (canon != NULL && strcmp (physname, canon) != 0)
10505 {
10506 /* It may not mean a bug in GDB. The compiler could also
10507 compute DW_AT_linkage_name incorrectly. But in such case
10508 GDB would need to be bug-to-bug compatible. */
10509
10510 complaint (_("Computed physname <%s> does not match demangled <%s> "
10511 "(from linkage <%s>) - DIE at %s [in module %s]"),
10512 physname, canon, mangled, sect_offset_str (die->sect_off),
10513 objfile_name (objfile));
10514
10515 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10516 is available here - over computed PHYSNAME. It is safer
10517 against both buggy GDB and buggy compilers. */
10518
10519 retval = canon;
10520 }
10521 else
10522 {
10523 retval = physname;
10524 need_copy = 0;
10525 }
10526 }
10527 else
10528 retval = canon;
10529
10530 if (need_copy)
10531 retval = objfile->intern (retval);
10532
10533 return retval;
10534 }
10535
10536 /* Inspect DIE in CU for a namespace alias. If one exists, record
10537 a new symbol for it.
10538
10539 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10540
10541 static int
10542 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10543 {
10544 struct attribute *attr;
10545
10546 /* If the die does not have a name, this is not a namespace
10547 alias. */
10548 attr = dwarf2_attr (die, DW_AT_name, cu);
10549 if (attr != NULL)
10550 {
10551 int num;
10552 struct die_info *d = die;
10553 struct dwarf2_cu *imported_cu = cu;
10554
10555 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10556 keep inspecting DIEs until we hit the underlying import. */
10557 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10558 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10559 {
10560 attr = dwarf2_attr (d, DW_AT_import, cu);
10561 if (attr == NULL)
10562 break;
10563
10564 d = follow_die_ref (d, attr, &imported_cu);
10565 if (d->tag != DW_TAG_imported_declaration)
10566 break;
10567 }
10568
10569 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10570 {
10571 complaint (_("DIE at %s has too many recursively imported "
10572 "declarations"), sect_offset_str (d->sect_off));
10573 return 0;
10574 }
10575
10576 if (attr != NULL)
10577 {
10578 struct type *type;
10579 sect_offset sect_off = attr->get_ref_die_offset ();
10580
10581 type = get_die_type_at_offset (sect_off, cu->per_cu);
10582 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10583 {
10584 /* This declaration is a global namespace alias. Add
10585 a symbol for it whose type is the aliased namespace. */
10586 new_symbol (die, type, cu);
10587 return 1;
10588 }
10589 }
10590 }
10591
10592 return 0;
10593 }
10594
10595 /* Return the using directives repository (global or local?) to use in the
10596 current context for CU.
10597
10598 For Ada, imported declarations can materialize renamings, which *may* be
10599 global. However it is impossible (for now?) in DWARF to distinguish
10600 "external" imported declarations and "static" ones. As all imported
10601 declarations seem to be static in all other languages, make them all CU-wide
10602 global only in Ada. */
10603
10604 static struct using_direct **
10605 using_directives (struct dwarf2_cu *cu)
10606 {
10607 if (cu->language == language_ada
10608 && cu->get_builder ()->outermost_context_p ())
10609 return cu->get_builder ()->get_global_using_directives ();
10610 else
10611 return cu->get_builder ()->get_local_using_directives ();
10612 }
10613
10614 /* Read the import statement specified by the given die and record it. */
10615
10616 static void
10617 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10618 {
10619 struct objfile *objfile = cu->per_objfile->objfile;
10620 struct attribute *import_attr;
10621 struct die_info *imported_die, *child_die;
10622 struct dwarf2_cu *imported_cu;
10623 const char *imported_name;
10624 const char *imported_name_prefix;
10625 const char *canonical_name;
10626 const char *import_alias;
10627 const char *imported_declaration = NULL;
10628 const char *import_prefix;
10629 std::vector<const char *> excludes;
10630
10631 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10632 if (import_attr == NULL)
10633 {
10634 complaint (_("Tag '%s' has no DW_AT_import"),
10635 dwarf_tag_name (die->tag));
10636 return;
10637 }
10638
10639 imported_cu = cu;
10640 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10641 imported_name = dwarf2_name (imported_die, imported_cu);
10642 if (imported_name == NULL)
10643 {
10644 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10645
10646 The import in the following code:
10647 namespace A
10648 {
10649 typedef int B;
10650 }
10651
10652 int main ()
10653 {
10654 using A::B;
10655 B b;
10656 return b;
10657 }
10658
10659 ...
10660 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10661 <52> DW_AT_decl_file : 1
10662 <53> DW_AT_decl_line : 6
10663 <54> DW_AT_import : <0x75>
10664 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10665 <59> DW_AT_name : B
10666 <5b> DW_AT_decl_file : 1
10667 <5c> DW_AT_decl_line : 2
10668 <5d> DW_AT_type : <0x6e>
10669 ...
10670 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10671 <76> DW_AT_byte_size : 4
10672 <77> DW_AT_encoding : 5 (signed)
10673
10674 imports the wrong die ( 0x75 instead of 0x58 ).
10675 This case will be ignored until the gcc bug is fixed. */
10676 return;
10677 }
10678
10679 /* Figure out the local name after import. */
10680 import_alias = dwarf2_name (die, cu);
10681
10682 /* Figure out where the statement is being imported to. */
10683 import_prefix = determine_prefix (die, cu);
10684
10685 /* Figure out what the scope of the imported die is and prepend it
10686 to the name of the imported die. */
10687 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10688
10689 if (imported_die->tag != DW_TAG_namespace
10690 && imported_die->tag != DW_TAG_module)
10691 {
10692 imported_declaration = imported_name;
10693 canonical_name = imported_name_prefix;
10694 }
10695 else if (strlen (imported_name_prefix) > 0)
10696 canonical_name = obconcat (&objfile->objfile_obstack,
10697 imported_name_prefix,
10698 (cu->language == language_d ? "." : "::"),
10699 imported_name, (char *) NULL);
10700 else
10701 canonical_name = imported_name;
10702
10703 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10704 for (child_die = die->child; child_die && child_die->tag;
10705 child_die = child_die->sibling)
10706 {
10707 /* DWARF-4: A Fortran use statement with a “rename list” may be
10708 represented by an imported module entry with an import attribute
10709 referring to the module and owned entries corresponding to those
10710 entities that are renamed as part of being imported. */
10711
10712 if (child_die->tag != DW_TAG_imported_declaration)
10713 {
10714 complaint (_("child DW_TAG_imported_declaration expected "
10715 "- DIE at %s [in module %s]"),
10716 sect_offset_str (child_die->sect_off),
10717 objfile_name (objfile));
10718 continue;
10719 }
10720
10721 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10722 if (import_attr == NULL)
10723 {
10724 complaint (_("Tag '%s' has no DW_AT_import"),
10725 dwarf_tag_name (child_die->tag));
10726 continue;
10727 }
10728
10729 imported_cu = cu;
10730 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10731 &imported_cu);
10732 imported_name = dwarf2_name (imported_die, imported_cu);
10733 if (imported_name == NULL)
10734 {
10735 complaint (_("child DW_TAG_imported_declaration has unknown "
10736 "imported name - DIE at %s [in module %s]"),
10737 sect_offset_str (child_die->sect_off),
10738 objfile_name (objfile));
10739 continue;
10740 }
10741
10742 excludes.push_back (imported_name);
10743
10744 process_die (child_die, cu);
10745 }
10746
10747 add_using_directive (using_directives (cu),
10748 import_prefix,
10749 canonical_name,
10750 import_alias,
10751 imported_declaration,
10752 excludes,
10753 0,
10754 &objfile->objfile_obstack);
10755 }
10756
10757 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10758 types, but gives them a size of zero. Starting with version 14,
10759 ICC is compatible with GCC. */
10760
10761 static bool
10762 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10763 {
10764 if (!cu->checked_producer)
10765 check_producer (cu);
10766
10767 return cu->producer_is_icc_lt_14;
10768 }
10769
10770 /* ICC generates a DW_AT_type for C void functions. This was observed on
10771 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10772 which says that void functions should not have a DW_AT_type. */
10773
10774 static bool
10775 producer_is_icc (struct dwarf2_cu *cu)
10776 {
10777 if (!cu->checked_producer)
10778 check_producer (cu);
10779
10780 return cu->producer_is_icc;
10781 }
10782
10783 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10784 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10785 this, it was first present in GCC release 4.3.0. */
10786
10787 static bool
10788 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10789 {
10790 if (!cu->checked_producer)
10791 check_producer (cu);
10792
10793 return cu->producer_is_gcc_lt_4_3;
10794 }
10795
10796 static file_and_directory
10797 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10798 {
10799 file_and_directory res;
10800
10801 /* Find the filename. Do not use dwarf2_name here, since the filename
10802 is not a source language identifier. */
10803 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10804 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10805
10806 if (res.comp_dir == NULL
10807 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10808 && IS_ABSOLUTE_PATH (res.name))
10809 {
10810 res.comp_dir_storage = ldirname (res.name);
10811 if (!res.comp_dir_storage.empty ())
10812 res.comp_dir = res.comp_dir_storage.c_str ();
10813 }
10814 if (res.comp_dir != NULL)
10815 {
10816 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10817 directory, get rid of it. */
10818 const char *cp = strchr (res.comp_dir, ':');
10819
10820 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10821 res.comp_dir = cp + 1;
10822 }
10823
10824 if (res.name == NULL)
10825 res.name = "<unknown>";
10826
10827 return res;
10828 }
10829
10830 /* Handle DW_AT_stmt_list for a compilation unit.
10831 DIE is the DW_TAG_compile_unit die for CU.
10832 COMP_DIR is the compilation directory. LOWPC is passed to
10833 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10834
10835 static void
10836 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10837 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10838 {
10839 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10840 struct attribute *attr;
10841 struct line_header line_header_local;
10842 hashval_t line_header_local_hash;
10843 void **slot;
10844 int decode_mapping;
10845
10846 gdb_assert (! cu->per_cu->is_debug_types);
10847
10848 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10849 if (attr == NULL)
10850 return;
10851
10852 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10853
10854 /* The line header hash table is only created if needed (it exists to
10855 prevent redundant reading of the line table for partial_units).
10856 If we're given a partial_unit, we'll need it. If we're given a
10857 compile_unit, then use the line header hash table if it's already
10858 created, but don't create one just yet. */
10859
10860 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10861 && die->tag == DW_TAG_partial_unit)
10862 {
10863 dwarf2_per_objfile->per_bfd->line_header_hash
10864 .reset (htab_create_alloc (127, line_header_hash_voidp,
10865 line_header_eq_voidp,
10866 free_line_header_voidp,
10867 xcalloc, xfree));
10868 }
10869
10870 line_header_local.sect_off = line_offset;
10871 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10872 line_header_local_hash = line_header_hash (&line_header_local);
10873 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10874 {
10875 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10876 &line_header_local,
10877 line_header_local_hash, NO_INSERT);
10878
10879 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10880 is not present in *SLOT (since if there is something in *SLOT then
10881 it will be for a partial_unit). */
10882 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10883 {
10884 gdb_assert (*slot != NULL);
10885 cu->line_header = (struct line_header *) *slot;
10886 return;
10887 }
10888 }
10889
10890 /* dwarf_decode_line_header does not yet provide sufficient information.
10891 We always have to call also dwarf_decode_lines for it. */
10892 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10893 if (lh == NULL)
10894 return;
10895
10896 cu->line_header = lh.release ();
10897 cu->line_header_die_owner = die;
10898
10899 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10900 slot = NULL;
10901 else
10902 {
10903 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10904 &line_header_local,
10905 line_header_local_hash, INSERT);
10906 gdb_assert (slot != NULL);
10907 }
10908 if (slot != NULL && *slot == NULL)
10909 {
10910 /* This newly decoded line number information unit will be owned
10911 by line_header_hash hash table. */
10912 *slot = cu->line_header;
10913 cu->line_header_die_owner = NULL;
10914 }
10915 else
10916 {
10917 /* We cannot free any current entry in (*slot) as that struct line_header
10918 may be already used by multiple CUs. Create only temporary decoded
10919 line_header for this CU - it may happen at most once for each line
10920 number information unit. And if we're not using line_header_hash
10921 then this is what we want as well. */
10922 gdb_assert (die->tag != DW_TAG_partial_unit);
10923 }
10924 decode_mapping = (die->tag != DW_TAG_partial_unit);
10925 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10926 decode_mapping);
10927
10928 }
10929
10930 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10931
10932 static void
10933 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10934 {
10935 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10936 struct objfile *objfile = dwarf2_per_objfile->objfile;
10937 struct gdbarch *gdbarch = objfile->arch ();
10938 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10939 CORE_ADDR highpc = ((CORE_ADDR) 0);
10940 struct attribute *attr;
10941 struct die_info *child_die;
10942 CORE_ADDR baseaddr;
10943
10944 prepare_one_comp_unit (cu, die, cu->language);
10945 baseaddr = objfile->text_section_offset ();
10946
10947 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10948
10949 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10950 from finish_block. */
10951 if (lowpc == ((CORE_ADDR) -1))
10952 lowpc = highpc;
10953 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10954
10955 file_and_directory fnd = find_file_and_directory (die, cu);
10956
10957 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10958 standardised yet. As a workaround for the language detection we fall
10959 back to the DW_AT_producer string. */
10960 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10961 cu->language = language_opencl;
10962
10963 /* Similar hack for Go. */
10964 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10965 set_cu_language (DW_LANG_Go, cu);
10966
10967 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10968
10969 /* Decode line number information if present. We do this before
10970 processing child DIEs, so that the line header table is available
10971 for DW_AT_decl_file. */
10972 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10973
10974 /* Process all dies in compilation unit. */
10975 if (die->child != NULL)
10976 {
10977 child_die = die->child;
10978 while (child_die && child_die->tag)
10979 {
10980 process_die (child_die, cu);
10981 child_die = child_die->sibling;
10982 }
10983 }
10984
10985 /* Decode macro information, if present. Dwarf 2 macro information
10986 refers to information in the line number info statement program
10987 header, so we can only read it if we've read the header
10988 successfully. */
10989 attr = dwarf2_attr (die, DW_AT_macros, cu);
10990 if (attr == NULL)
10991 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10992 if (attr && cu->line_header)
10993 {
10994 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10995 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10996
10997 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10998 }
10999 else
11000 {
11001 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11002 if (attr && cu->line_header)
11003 {
11004 unsigned int macro_offset = DW_UNSND (attr);
11005
11006 dwarf_decode_macros (cu, macro_offset, 0);
11007 }
11008 }
11009 }
11010
11011 void
11012 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11013 {
11014 struct type_unit_group *tu_group;
11015 int first_time;
11016 struct attribute *attr;
11017 unsigned int i;
11018 struct signatured_type *sig_type;
11019
11020 gdb_assert (per_cu->is_debug_types);
11021 sig_type = (struct signatured_type *) per_cu;
11022
11023 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11024
11025 /* If we're using .gdb_index (includes -readnow) then
11026 per_cu->type_unit_group may not have been set up yet. */
11027 if (sig_type->type_unit_group == NULL)
11028 sig_type->type_unit_group = get_type_unit_group (this, attr);
11029 tu_group = sig_type->type_unit_group;
11030
11031 /* If we've already processed this stmt_list there's no real need to
11032 do it again, we could fake it and just recreate the part we need
11033 (file name,index -> symtab mapping). If data shows this optimization
11034 is useful we can do it then. */
11035 first_time = tu_group->compunit_symtab == NULL;
11036
11037 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11038 debug info. */
11039 line_header_up lh;
11040 if (attr != NULL)
11041 {
11042 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11043 lh = dwarf_decode_line_header (line_offset, this);
11044 }
11045 if (lh == NULL)
11046 {
11047 if (first_time)
11048 start_symtab ("", NULL, 0);
11049 else
11050 {
11051 gdb_assert (tu_group->symtabs == NULL);
11052 gdb_assert (m_builder == nullptr);
11053 struct compunit_symtab *cust = tu_group->compunit_symtab;
11054 m_builder.reset (new struct buildsym_compunit
11055 (COMPUNIT_OBJFILE (cust), "",
11056 COMPUNIT_DIRNAME (cust),
11057 compunit_language (cust),
11058 0, cust));
11059 list_in_scope = get_builder ()->get_file_symbols ();
11060 }
11061 return;
11062 }
11063
11064 line_header = lh.release ();
11065 line_header_die_owner = die;
11066
11067 if (first_time)
11068 {
11069 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11070
11071 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11072 still initializing it, and our caller (a few levels up)
11073 process_full_type_unit still needs to know if this is the first
11074 time. */
11075
11076 tu_group->symtabs
11077 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11078 struct symtab *, line_header->file_names_size ());
11079
11080 auto &file_names = line_header->file_names ();
11081 for (i = 0; i < file_names.size (); ++i)
11082 {
11083 file_entry &fe = file_names[i];
11084 dwarf2_start_subfile (this, fe.name,
11085 fe.include_dir (line_header));
11086 buildsym_compunit *b = get_builder ();
11087 if (b->get_current_subfile ()->symtab == NULL)
11088 {
11089 /* NOTE: start_subfile will recognize when it's been
11090 passed a file it has already seen. So we can't
11091 assume there's a simple mapping from
11092 cu->line_header->file_names to subfiles, plus
11093 cu->line_header->file_names may contain dups. */
11094 b->get_current_subfile ()->symtab
11095 = allocate_symtab (cust, b->get_current_subfile ()->name);
11096 }
11097
11098 fe.symtab = b->get_current_subfile ()->symtab;
11099 tu_group->symtabs[i] = fe.symtab;
11100 }
11101 }
11102 else
11103 {
11104 gdb_assert (m_builder == nullptr);
11105 struct compunit_symtab *cust = tu_group->compunit_symtab;
11106 m_builder.reset (new struct buildsym_compunit
11107 (COMPUNIT_OBJFILE (cust), "",
11108 COMPUNIT_DIRNAME (cust),
11109 compunit_language (cust),
11110 0, cust));
11111 list_in_scope = get_builder ()->get_file_symbols ();
11112
11113 auto &file_names = line_header->file_names ();
11114 for (i = 0; i < file_names.size (); ++i)
11115 {
11116 file_entry &fe = file_names[i];
11117 fe.symtab = tu_group->symtabs[i];
11118 }
11119 }
11120
11121 /* The main symtab is allocated last. Type units don't have DW_AT_name
11122 so they don't have a "real" (so to speak) symtab anyway.
11123 There is later code that will assign the main symtab to all symbols
11124 that don't have one. We need to handle the case of a symbol with a
11125 missing symtab (DW_AT_decl_file) anyway. */
11126 }
11127
11128 /* Process DW_TAG_type_unit.
11129 For TUs we want to skip the first top level sibling if it's not the
11130 actual type being defined by this TU. In this case the first top
11131 level sibling is there to provide context only. */
11132
11133 static void
11134 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11135 {
11136 struct die_info *child_die;
11137
11138 prepare_one_comp_unit (cu, die, language_minimal);
11139
11140 /* Initialize (or reinitialize) the machinery for building symtabs.
11141 We do this before processing child DIEs, so that the line header table
11142 is available for DW_AT_decl_file. */
11143 cu->setup_type_unit_groups (die);
11144
11145 if (die->child != NULL)
11146 {
11147 child_die = die->child;
11148 while (child_die && child_die->tag)
11149 {
11150 process_die (child_die, cu);
11151 child_die = child_die->sibling;
11152 }
11153 }
11154 }
11155 \f
11156 /* DWO/DWP files.
11157
11158 http://gcc.gnu.org/wiki/DebugFission
11159 http://gcc.gnu.org/wiki/DebugFissionDWP
11160
11161 To simplify handling of both DWO files ("object" files with the DWARF info)
11162 and DWP files (a file with the DWOs packaged up into one file), we treat
11163 DWP files as having a collection of virtual DWO files. */
11164
11165 static hashval_t
11166 hash_dwo_file (const void *item)
11167 {
11168 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11169 hashval_t hash;
11170
11171 hash = htab_hash_string (dwo_file->dwo_name);
11172 if (dwo_file->comp_dir != NULL)
11173 hash += htab_hash_string (dwo_file->comp_dir);
11174 return hash;
11175 }
11176
11177 static int
11178 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11179 {
11180 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11181 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11182
11183 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11184 return 0;
11185 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11186 return lhs->comp_dir == rhs->comp_dir;
11187 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11188 }
11189
11190 /* Allocate a hash table for DWO files. */
11191
11192 static htab_up
11193 allocate_dwo_file_hash_table ()
11194 {
11195 auto delete_dwo_file = [] (void *item)
11196 {
11197 struct dwo_file *dwo_file = (struct dwo_file *) item;
11198
11199 delete dwo_file;
11200 };
11201
11202 return htab_up (htab_create_alloc (41,
11203 hash_dwo_file,
11204 eq_dwo_file,
11205 delete_dwo_file,
11206 xcalloc, xfree));
11207 }
11208
11209 /* Lookup DWO file DWO_NAME. */
11210
11211 static void **
11212 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11213 const char *dwo_name,
11214 const char *comp_dir)
11215 {
11216 struct dwo_file find_entry;
11217 void **slot;
11218
11219 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11220 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11221
11222 find_entry.dwo_name = dwo_name;
11223 find_entry.comp_dir = comp_dir;
11224 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11225 INSERT);
11226
11227 return slot;
11228 }
11229
11230 static hashval_t
11231 hash_dwo_unit (const void *item)
11232 {
11233 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11234
11235 /* This drops the top 32 bits of the id, but is ok for a hash. */
11236 return dwo_unit->signature;
11237 }
11238
11239 static int
11240 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11241 {
11242 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11243 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11244
11245 /* The signature is assumed to be unique within the DWO file.
11246 So while object file CU dwo_id's always have the value zero,
11247 that's OK, assuming each object file DWO file has only one CU,
11248 and that's the rule for now. */
11249 return lhs->signature == rhs->signature;
11250 }
11251
11252 /* Allocate a hash table for DWO CUs,TUs.
11253 There is one of these tables for each of CUs,TUs for each DWO file. */
11254
11255 static htab_up
11256 allocate_dwo_unit_table ()
11257 {
11258 /* Start out with a pretty small number.
11259 Generally DWO files contain only one CU and maybe some TUs. */
11260 return htab_up (htab_create_alloc (3,
11261 hash_dwo_unit,
11262 eq_dwo_unit,
11263 NULL, xcalloc, xfree));
11264 }
11265
11266 /* die_reader_func for create_dwo_cu. */
11267
11268 static void
11269 create_dwo_cu_reader (const struct die_reader_specs *reader,
11270 const gdb_byte *info_ptr,
11271 struct die_info *comp_unit_die,
11272 struct dwo_file *dwo_file,
11273 struct dwo_unit *dwo_unit)
11274 {
11275 struct dwarf2_cu *cu = reader->cu;
11276 sect_offset sect_off = cu->per_cu->sect_off;
11277 struct dwarf2_section_info *section = cu->per_cu->section;
11278
11279 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11280 if (!signature.has_value ())
11281 {
11282 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11283 " its dwo_id [in module %s]"),
11284 sect_offset_str (sect_off), dwo_file->dwo_name);
11285 return;
11286 }
11287
11288 dwo_unit->dwo_file = dwo_file;
11289 dwo_unit->signature = *signature;
11290 dwo_unit->section = section;
11291 dwo_unit->sect_off = sect_off;
11292 dwo_unit->length = cu->per_cu->length;
11293
11294 if (dwarf_read_debug)
11295 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11296 sect_offset_str (sect_off),
11297 hex_string (dwo_unit->signature));
11298 }
11299
11300 /* Create the dwo_units for the CUs in a DWO_FILE.
11301 Note: This function processes DWO files only, not DWP files. */
11302
11303 static void
11304 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11305 dwarf2_cu *cu, struct dwo_file &dwo_file,
11306 dwarf2_section_info &section, htab_up &cus_htab)
11307 {
11308 struct objfile *objfile = dwarf2_per_objfile->objfile;
11309 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11310 const gdb_byte *info_ptr, *end_ptr;
11311
11312 section.read (objfile);
11313 info_ptr = section.buffer;
11314
11315 if (info_ptr == NULL)
11316 return;
11317
11318 if (dwarf_read_debug)
11319 {
11320 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11321 section.get_name (),
11322 section.get_file_name ());
11323 }
11324
11325 end_ptr = info_ptr + section.size;
11326 while (info_ptr < end_ptr)
11327 {
11328 struct dwarf2_per_cu_data per_cu;
11329 struct dwo_unit read_unit {};
11330 struct dwo_unit *dwo_unit;
11331 void **slot;
11332 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11333
11334 memset (&per_cu, 0, sizeof (per_cu));
11335 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11336 per_cu.per_bfd = per_bfd;
11337 per_cu.is_debug_types = 0;
11338 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11339 per_cu.section = &section;
11340
11341 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11342 if (!reader.dummy_p)
11343 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11344 &dwo_file, &read_unit);
11345 info_ptr += per_cu.length;
11346
11347 // If the unit could not be parsed, skip it.
11348 if (read_unit.dwo_file == NULL)
11349 continue;
11350
11351 if (cus_htab == NULL)
11352 cus_htab = allocate_dwo_unit_table ();
11353
11354 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11355 struct dwo_unit);
11356 *dwo_unit = read_unit;
11357 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11358 gdb_assert (slot != NULL);
11359 if (*slot != NULL)
11360 {
11361 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11362 sect_offset dup_sect_off = dup_cu->sect_off;
11363
11364 complaint (_("debug cu entry at offset %s is duplicate to"
11365 " the entry at offset %s, signature %s"),
11366 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11367 hex_string (dwo_unit->signature));
11368 }
11369 *slot = (void *)dwo_unit;
11370 }
11371 }
11372
11373 /* DWP file .debug_{cu,tu}_index section format:
11374 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11375
11376 DWP Version 1:
11377
11378 Both index sections have the same format, and serve to map a 64-bit
11379 signature to a set of section numbers. Each section begins with a header,
11380 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11381 indexes, and a pool of 32-bit section numbers. The index sections will be
11382 aligned at 8-byte boundaries in the file.
11383
11384 The index section header consists of:
11385
11386 V, 32 bit version number
11387 -, 32 bits unused
11388 N, 32 bit number of compilation units or type units in the index
11389 M, 32 bit number of slots in the hash table
11390
11391 Numbers are recorded using the byte order of the application binary.
11392
11393 The hash table begins at offset 16 in the section, and consists of an array
11394 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11395 order of the application binary). Unused slots in the hash table are 0.
11396 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11397
11398 The parallel table begins immediately after the hash table
11399 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11400 array of 32-bit indexes (using the byte order of the application binary),
11401 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11402 table contains a 32-bit index into the pool of section numbers. For unused
11403 hash table slots, the corresponding entry in the parallel table will be 0.
11404
11405 The pool of section numbers begins immediately following the hash table
11406 (at offset 16 + 12 * M from the beginning of the section). The pool of
11407 section numbers consists of an array of 32-bit words (using the byte order
11408 of the application binary). Each item in the array is indexed starting
11409 from 0. The hash table entry provides the index of the first section
11410 number in the set. Additional section numbers in the set follow, and the
11411 set is terminated by a 0 entry (section number 0 is not used in ELF).
11412
11413 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11414 section must be the first entry in the set, and the .debug_abbrev.dwo must
11415 be the second entry. Other members of the set may follow in any order.
11416
11417 ---
11418
11419 DWP Version 2:
11420
11421 DWP Version 2 combines all the .debug_info, etc. sections into one,
11422 and the entries in the index tables are now offsets into these sections.
11423 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11424 section.
11425
11426 Index Section Contents:
11427 Header
11428 Hash Table of Signatures dwp_hash_table.hash_table
11429 Parallel Table of Indices dwp_hash_table.unit_table
11430 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11431 Table of Section Sizes dwp_hash_table.v2.sizes
11432
11433 The index section header consists of:
11434
11435 V, 32 bit version number
11436 L, 32 bit number of columns in the table of section offsets
11437 N, 32 bit number of compilation units or type units in the index
11438 M, 32 bit number of slots in the hash table
11439
11440 Numbers are recorded using the byte order of the application binary.
11441
11442 The hash table has the same format as version 1.
11443 The parallel table of indices has the same format as version 1,
11444 except that the entries are origin-1 indices into the table of sections
11445 offsets and the table of section sizes.
11446
11447 The table of offsets begins immediately following the parallel table
11448 (at offset 16 + 12 * M from the beginning of the section). The table is
11449 a two-dimensional array of 32-bit words (using the byte order of the
11450 application binary), with L columns and N+1 rows, in row-major order.
11451 Each row in the array is indexed starting from 0. The first row provides
11452 a key to the remaining rows: each column in this row provides an identifier
11453 for a debug section, and the offsets in the same column of subsequent rows
11454 refer to that section. The section identifiers are:
11455
11456 DW_SECT_INFO 1 .debug_info.dwo
11457 DW_SECT_TYPES 2 .debug_types.dwo
11458 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11459 DW_SECT_LINE 4 .debug_line.dwo
11460 DW_SECT_LOC 5 .debug_loc.dwo
11461 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11462 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11463 DW_SECT_MACRO 8 .debug_macro.dwo
11464
11465 The offsets provided by the CU and TU index sections are the base offsets
11466 for the contributions made by each CU or TU to the corresponding section
11467 in the package file. Each CU and TU header contains an abbrev_offset
11468 field, used to find the abbreviations table for that CU or TU within the
11469 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11470 be interpreted as relative to the base offset given in the index section.
11471 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11472 should be interpreted as relative to the base offset for .debug_line.dwo,
11473 and offsets into other debug sections obtained from DWARF attributes should
11474 also be interpreted as relative to the corresponding base offset.
11475
11476 The table of sizes begins immediately following the table of offsets.
11477 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11478 with L columns and N rows, in row-major order. Each row in the array is
11479 indexed starting from 1 (row 0 is shared by the two tables).
11480
11481 ---
11482
11483 Hash table lookup is handled the same in version 1 and 2:
11484
11485 We assume that N and M will not exceed 2^32 - 1.
11486 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11487
11488 Given a 64-bit compilation unit signature or a type signature S, an entry
11489 in the hash table is located as follows:
11490
11491 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11492 the low-order k bits all set to 1.
11493
11494 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11495
11496 3) If the hash table entry at index H matches the signature, use that
11497 entry. If the hash table entry at index H is unused (all zeroes),
11498 terminate the search: the signature is not present in the table.
11499
11500 4) Let H = (H + H') modulo M. Repeat at Step 3.
11501
11502 Because M > N and H' and M are relatively prime, the search is guaranteed
11503 to stop at an unused slot or find the match. */
11504
11505 /* Create a hash table to map DWO IDs to their CU/TU entry in
11506 .debug_{info,types}.dwo in DWP_FILE.
11507 Returns NULL if there isn't one.
11508 Note: This function processes DWP files only, not DWO files. */
11509
11510 static struct dwp_hash_table *
11511 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11512 struct dwp_file *dwp_file, int is_debug_types)
11513 {
11514 struct objfile *objfile = dwarf2_per_objfile->objfile;
11515 bfd *dbfd = dwp_file->dbfd.get ();
11516 const gdb_byte *index_ptr, *index_end;
11517 struct dwarf2_section_info *index;
11518 uint32_t version, nr_columns, nr_units, nr_slots;
11519 struct dwp_hash_table *htab;
11520
11521 if (is_debug_types)
11522 index = &dwp_file->sections.tu_index;
11523 else
11524 index = &dwp_file->sections.cu_index;
11525
11526 if (index->empty ())
11527 return NULL;
11528 index->read (objfile);
11529
11530 index_ptr = index->buffer;
11531 index_end = index_ptr + index->size;
11532
11533 version = read_4_bytes (dbfd, index_ptr);
11534 index_ptr += 4;
11535 if (version == 2)
11536 nr_columns = read_4_bytes (dbfd, index_ptr);
11537 else
11538 nr_columns = 0;
11539 index_ptr += 4;
11540 nr_units = read_4_bytes (dbfd, index_ptr);
11541 index_ptr += 4;
11542 nr_slots = read_4_bytes (dbfd, index_ptr);
11543 index_ptr += 4;
11544
11545 if (version != 1 && version != 2)
11546 {
11547 error (_("Dwarf Error: unsupported DWP file version (%s)"
11548 " [in module %s]"),
11549 pulongest (version), dwp_file->name);
11550 }
11551 if (nr_slots != (nr_slots & -nr_slots))
11552 {
11553 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11554 " is not power of 2 [in module %s]"),
11555 pulongest (nr_slots), dwp_file->name);
11556 }
11557
11558 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11559 htab->version = version;
11560 htab->nr_columns = nr_columns;
11561 htab->nr_units = nr_units;
11562 htab->nr_slots = nr_slots;
11563 htab->hash_table = index_ptr;
11564 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11565
11566 /* Exit early if the table is empty. */
11567 if (nr_slots == 0 || nr_units == 0
11568 || (version == 2 && nr_columns == 0))
11569 {
11570 /* All must be zero. */
11571 if (nr_slots != 0 || nr_units != 0
11572 || (version == 2 && nr_columns != 0))
11573 {
11574 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11575 " all zero [in modules %s]"),
11576 dwp_file->name);
11577 }
11578 return htab;
11579 }
11580
11581 if (version == 1)
11582 {
11583 htab->section_pool.v1.indices =
11584 htab->unit_table + sizeof (uint32_t) * nr_slots;
11585 /* It's harder to decide whether the section is too small in v1.
11586 V1 is deprecated anyway so we punt. */
11587 }
11588 else
11589 {
11590 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11591 int *ids = htab->section_pool.v2.section_ids;
11592 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11593 /* Reverse map for error checking. */
11594 int ids_seen[DW_SECT_MAX + 1];
11595 int i;
11596
11597 if (nr_columns < 2)
11598 {
11599 error (_("Dwarf Error: bad DWP hash table, too few columns"
11600 " in section table [in module %s]"),
11601 dwp_file->name);
11602 }
11603 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11604 {
11605 error (_("Dwarf Error: bad DWP hash table, too many columns"
11606 " in section table [in module %s]"),
11607 dwp_file->name);
11608 }
11609 memset (ids, 255, sizeof_ids);
11610 memset (ids_seen, 255, sizeof (ids_seen));
11611 for (i = 0; i < nr_columns; ++i)
11612 {
11613 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11614
11615 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11616 {
11617 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11618 " in section table [in module %s]"),
11619 id, dwp_file->name);
11620 }
11621 if (ids_seen[id] != -1)
11622 {
11623 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11624 " id %d in section table [in module %s]"),
11625 id, dwp_file->name);
11626 }
11627 ids_seen[id] = i;
11628 ids[i] = id;
11629 }
11630 /* Must have exactly one info or types section. */
11631 if (((ids_seen[DW_SECT_INFO] != -1)
11632 + (ids_seen[DW_SECT_TYPES] != -1))
11633 != 1)
11634 {
11635 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11636 " DWO info/types section [in module %s]"),
11637 dwp_file->name);
11638 }
11639 /* Must have an abbrev section. */
11640 if (ids_seen[DW_SECT_ABBREV] == -1)
11641 {
11642 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11643 " section [in module %s]"),
11644 dwp_file->name);
11645 }
11646 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11647 htab->section_pool.v2.sizes =
11648 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11649 * nr_units * nr_columns);
11650 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11651 * nr_units * nr_columns))
11652 > index_end)
11653 {
11654 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11655 " [in module %s]"),
11656 dwp_file->name);
11657 }
11658 }
11659
11660 return htab;
11661 }
11662
11663 /* Update SECTIONS with the data from SECTP.
11664
11665 This function is like the other "locate" section routines that are
11666 passed to bfd_map_over_sections, but in this context the sections to
11667 read comes from the DWP V1 hash table, not the full ELF section table.
11668
11669 The result is non-zero for success, or zero if an error was found. */
11670
11671 static int
11672 locate_v1_virtual_dwo_sections (asection *sectp,
11673 struct virtual_v1_dwo_sections *sections)
11674 {
11675 const struct dwop_section_names *names = &dwop_section_names;
11676
11677 if (section_is_p (sectp->name, &names->abbrev_dwo))
11678 {
11679 /* There can be only one. */
11680 if (sections->abbrev.s.section != NULL)
11681 return 0;
11682 sections->abbrev.s.section = sectp;
11683 sections->abbrev.size = bfd_section_size (sectp);
11684 }
11685 else if (section_is_p (sectp->name, &names->info_dwo)
11686 || section_is_p (sectp->name, &names->types_dwo))
11687 {
11688 /* There can be only one. */
11689 if (sections->info_or_types.s.section != NULL)
11690 return 0;
11691 sections->info_or_types.s.section = sectp;
11692 sections->info_or_types.size = bfd_section_size (sectp);
11693 }
11694 else if (section_is_p (sectp->name, &names->line_dwo))
11695 {
11696 /* There can be only one. */
11697 if (sections->line.s.section != NULL)
11698 return 0;
11699 sections->line.s.section = sectp;
11700 sections->line.size = bfd_section_size (sectp);
11701 }
11702 else if (section_is_p (sectp->name, &names->loc_dwo))
11703 {
11704 /* There can be only one. */
11705 if (sections->loc.s.section != NULL)
11706 return 0;
11707 sections->loc.s.section = sectp;
11708 sections->loc.size = bfd_section_size (sectp);
11709 }
11710 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11711 {
11712 /* There can be only one. */
11713 if (sections->macinfo.s.section != NULL)
11714 return 0;
11715 sections->macinfo.s.section = sectp;
11716 sections->macinfo.size = bfd_section_size (sectp);
11717 }
11718 else if (section_is_p (sectp->name, &names->macro_dwo))
11719 {
11720 /* There can be only one. */
11721 if (sections->macro.s.section != NULL)
11722 return 0;
11723 sections->macro.s.section = sectp;
11724 sections->macro.size = bfd_section_size (sectp);
11725 }
11726 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11727 {
11728 /* There can be only one. */
11729 if (sections->str_offsets.s.section != NULL)
11730 return 0;
11731 sections->str_offsets.s.section = sectp;
11732 sections->str_offsets.size = bfd_section_size (sectp);
11733 }
11734 else
11735 {
11736 /* No other kind of section is valid. */
11737 return 0;
11738 }
11739
11740 return 1;
11741 }
11742
11743 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11744 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11745 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11746 This is for DWP version 1 files. */
11747
11748 static struct dwo_unit *
11749 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11750 struct dwp_file *dwp_file,
11751 uint32_t unit_index,
11752 const char *comp_dir,
11753 ULONGEST signature, int is_debug_types)
11754 {
11755 const struct dwp_hash_table *dwp_htab =
11756 is_debug_types ? dwp_file->tus : dwp_file->cus;
11757 bfd *dbfd = dwp_file->dbfd.get ();
11758 const char *kind = is_debug_types ? "TU" : "CU";
11759 struct dwo_file *dwo_file;
11760 struct dwo_unit *dwo_unit;
11761 struct virtual_v1_dwo_sections sections;
11762 void **dwo_file_slot;
11763 int i;
11764
11765 gdb_assert (dwp_file->version == 1);
11766
11767 if (dwarf_read_debug)
11768 {
11769 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11770 kind,
11771 pulongest (unit_index), hex_string (signature),
11772 dwp_file->name);
11773 }
11774
11775 /* Fetch the sections of this DWO unit.
11776 Put a limit on the number of sections we look for so that bad data
11777 doesn't cause us to loop forever. */
11778
11779 #define MAX_NR_V1_DWO_SECTIONS \
11780 (1 /* .debug_info or .debug_types */ \
11781 + 1 /* .debug_abbrev */ \
11782 + 1 /* .debug_line */ \
11783 + 1 /* .debug_loc */ \
11784 + 1 /* .debug_str_offsets */ \
11785 + 1 /* .debug_macro or .debug_macinfo */ \
11786 + 1 /* trailing zero */)
11787
11788 memset (&sections, 0, sizeof (sections));
11789
11790 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11791 {
11792 asection *sectp;
11793 uint32_t section_nr =
11794 read_4_bytes (dbfd,
11795 dwp_htab->section_pool.v1.indices
11796 + (unit_index + i) * sizeof (uint32_t));
11797
11798 if (section_nr == 0)
11799 break;
11800 if (section_nr >= dwp_file->num_sections)
11801 {
11802 error (_("Dwarf Error: bad DWP hash table, section number too large"
11803 " [in module %s]"),
11804 dwp_file->name);
11805 }
11806
11807 sectp = dwp_file->elf_sections[section_nr];
11808 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11809 {
11810 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11811 " [in module %s]"),
11812 dwp_file->name);
11813 }
11814 }
11815
11816 if (i < 2
11817 || sections.info_or_types.empty ()
11818 || sections.abbrev.empty ())
11819 {
11820 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11821 " [in module %s]"),
11822 dwp_file->name);
11823 }
11824 if (i == MAX_NR_V1_DWO_SECTIONS)
11825 {
11826 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11827 " [in module %s]"),
11828 dwp_file->name);
11829 }
11830
11831 /* It's easier for the rest of the code if we fake a struct dwo_file and
11832 have dwo_unit "live" in that. At least for now.
11833
11834 The DWP file can be made up of a random collection of CUs and TUs.
11835 However, for each CU + set of TUs that came from the same original DWO
11836 file, we can combine them back into a virtual DWO file to save space
11837 (fewer struct dwo_file objects to allocate). Remember that for really
11838 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11839
11840 std::string virtual_dwo_name =
11841 string_printf ("virtual-dwo/%d-%d-%d-%d",
11842 sections.abbrev.get_id (),
11843 sections.line.get_id (),
11844 sections.loc.get_id (),
11845 sections.str_offsets.get_id ());
11846 /* Can we use an existing virtual DWO file? */
11847 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11848 virtual_dwo_name.c_str (),
11849 comp_dir);
11850 /* Create one if necessary. */
11851 if (*dwo_file_slot == NULL)
11852 {
11853 if (dwarf_read_debug)
11854 {
11855 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11856 virtual_dwo_name.c_str ());
11857 }
11858 dwo_file = new struct dwo_file;
11859 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11860 dwo_file->comp_dir = comp_dir;
11861 dwo_file->sections.abbrev = sections.abbrev;
11862 dwo_file->sections.line = sections.line;
11863 dwo_file->sections.loc = sections.loc;
11864 dwo_file->sections.macinfo = sections.macinfo;
11865 dwo_file->sections.macro = sections.macro;
11866 dwo_file->sections.str_offsets = sections.str_offsets;
11867 /* The "str" section is global to the entire DWP file. */
11868 dwo_file->sections.str = dwp_file->sections.str;
11869 /* The info or types section is assigned below to dwo_unit,
11870 there's no need to record it in dwo_file.
11871 Also, we can't simply record type sections in dwo_file because
11872 we record a pointer into the vector in dwo_unit. As we collect more
11873 types we'll grow the vector and eventually have to reallocate space
11874 for it, invalidating all copies of pointers into the previous
11875 contents. */
11876 *dwo_file_slot = dwo_file;
11877 }
11878 else
11879 {
11880 if (dwarf_read_debug)
11881 {
11882 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11883 virtual_dwo_name.c_str ());
11884 }
11885 dwo_file = (struct dwo_file *) *dwo_file_slot;
11886 }
11887
11888 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11889 dwo_unit->dwo_file = dwo_file;
11890 dwo_unit->signature = signature;
11891 dwo_unit->section =
11892 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11893 *dwo_unit->section = sections.info_or_types;
11894 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11895
11896 return dwo_unit;
11897 }
11898
11899 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11900 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11901 piece within that section used by a TU/CU, return a virtual section
11902 of just that piece. */
11903
11904 static struct dwarf2_section_info
11905 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11906 struct dwarf2_section_info *section,
11907 bfd_size_type offset, bfd_size_type size)
11908 {
11909 struct dwarf2_section_info result;
11910 asection *sectp;
11911
11912 gdb_assert (section != NULL);
11913 gdb_assert (!section->is_virtual);
11914
11915 memset (&result, 0, sizeof (result));
11916 result.s.containing_section = section;
11917 result.is_virtual = true;
11918
11919 if (size == 0)
11920 return result;
11921
11922 sectp = section->get_bfd_section ();
11923
11924 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11925 bounds of the real section. This is a pretty-rare event, so just
11926 flag an error (easier) instead of a warning and trying to cope. */
11927 if (sectp == NULL
11928 || offset + size > bfd_section_size (sectp))
11929 {
11930 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11931 " in section %s [in module %s]"),
11932 sectp ? bfd_section_name (sectp) : "<unknown>",
11933 objfile_name (dwarf2_per_objfile->objfile));
11934 }
11935
11936 result.virtual_offset = offset;
11937 result.size = size;
11938 return result;
11939 }
11940
11941 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11942 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11943 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11944 This is for DWP version 2 files. */
11945
11946 static struct dwo_unit *
11947 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11948 struct dwp_file *dwp_file,
11949 uint32_t unit_index,
11950 const char *comp_dir,
11951 ULONGEST signature, int is_debug_types)
11952 {
11953 const struct dwp_hash_table *dwp_htab =
11954 is_debug_types ? dwp_file->tus : dwp_file->cus;
11955 bfd *dbfd = dwp_file->dbfd.get ();
11956 const char *kind = is_debug_types ? "TU" : "CU";
11957 struct dwo_file *dwo_file;
11958 struct dwo_unit *dwo_unit;
11959 struct virtual_v2_dwo_sections sections;
11960 void **dwo_file_slot;
11961 int i;
11962
11963 gdb_assert (dwp_file->version == 2);
11964
11965 if (dwarf_read_debug)
11966 {
11967 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11968 kind,
11969 pulongest (unit_index), hex_string (signature),
11970 dwp_file->name);
11971 }
11972
11973 /* Fetch the section offsets of this DWO unit. */
11974
11975 memset (&sections, 0, sizeof (sections));
11976
11977 for (i = 0; i < dwp_htab->nr_columns; ++i)
11978 {
11979 uint32_t offset = read_4_bytes (dbfd,
11980 dwp_htab->section_pool.v2.offsets
11981 + (((unit_index - 1) * dwp_htab->nr_columns
11982 + i)
11983 * sizeof (uint32_t)));
11984 uint32_t size = read_4_bytes (dbfd,
11985 dwp_htab->section_pool.v2.sizes
11986 + (((unit_index - 1) * dwp_htab->nr_columns
11987 + i)
11988 * sizeof (uint32_t)));
11989
11990 switch (dwp_htab->section_pool.v2.section_ids[i])
11991 {
11992 case DW_SECT_INFO:
11993 case DW_SECT_TYPES:
11994 sections.info_or_types_offset = offset;
11995 sections.info_or_types_size = size;
11996 break;
11997 case DW_SECT_ABBREV:
11998 sections.abbrev_offset = offset;
11999 sections.abbrev_size = size;
12000 break;
12001 case DW_SECT_LINE:
12002 sections.line_offset = offset;
12003 sections.line_size = size;
12004 break;
12005 case DW_SECT_LOC:
12006 sections.loc_offset = offset;
12007 sections.loc_size = size;
12008 break;
12009 case DW_SECT_STR_OFFSETS:
12010 sections.str_offsets_offset = offset;
12011 sections.str_offsets_size = size;
12012 break;
12013 case DW_SECT_MACINFO:
12014 sections.macinfo_offset = offset;
12015 sections.macinfo_size = size;
12016 break;
12017 case DW_SECT_MACRO:
12018 sections.macro_offset = offset;
12019 sections.macro_size = size;
12020 break;
12021 }
12022 }
12023
12024 /* It's easier for the rest of the code if we fake a struct dwo_file and
12025 have dwo_unit "live" in that. At least for now.
12026
12027 The DWP file can be made up of a random collection of CUs and TUs.
12028 However, for each CU + set of TUs that came from the same original DWO
12029 file, we can combine them back into a virtual DWO file to save space
12030 (fewer struct dwo_file objects to allocate). Remember that for really
12031 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12032
12033 std::string virtual_dwo_name =
12034 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12035 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12036 (long) (sections.line_size ? sections.line_offset : 0),
12037 (long) (sections.loc_size ? sections.loc_offset : 0),
12038 (long) (sections.str_offsets_size
12039 ? sections.str_offsets_offset : 0));
12040 /* Can we use an existing virtual DWO file? */
12041 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12042 virtual_dwo_name.c_str (),
12043 comp_dir);
12044 /* Create one if necessary. */
12045 if (*dwo_file_slot == NULL)
12046 {
12047 if (dwarf_read_debug)
12048 {
12049 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12050 virtual_dwo_name.c_str ());
12051 }
12052 dwo_file = new struct dwo_file;
12053 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12054 dwo_file->comp_dir = comp_dir;
12055 dwo_file->sections.abbrev =
12056 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12057 sections.abbrev_offset, sections.abbrev_size);
12058 dwo_file->sections.line =
12059 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12060 sections.line_offset, sections.line_size);
12061 dwo_file->sections.loc =
12062 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12063 sections.loc_offset, sections.loc_size);
12064 dwo_file->sections.macinfo =
12065 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12066 sections.macinfo_offset, sections.macinfo_size);
12067 dwo_file->sections.macro =
12068 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12069 sections.macro_offset, sections.macro_size);
12070 dwo_file->sections.str_offsets =
12071 create_dwp_v2_section (dwarf2_per_objfile,
12072 &dwp_file->sections.str_offsets,
12073 sections.str_offsets_offset,
12074 sections.str_offsets_size);
12075 /* The "str" section is global to the entire DWP file. */
12076 dwo_file->sections.str = dwp_file->sections.str;
12077 /* The info or types section is assigned below to dwo_unit,
12078 there's no need to record it in dwo_file.
12079 Also, we can't simply record type sections in dwo_file because
12080 we record a pointer into the vector in dwo_unit. As we collect more
12081 types we'll grow the vector and eventually have to reallocate space
12082 for it, invalidating all copies of pointers into the previous
12083 contents. */
12084 *dwo_file_slot = dwo_file;
12085 }
12086 else
12087 {
12088 if (dwarf_read_debug)
12089 {
12090 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12091 virtual_dwo_name.c_str ());
12092 }
12093 dwo_file = (struct dwo_file *) *dwo_file_slot;
12094 }
12095
12096 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12097 dwo_unit->dwo_file = dwo_file;
12098 dwo_unit->signature = signature;
12099 dwo_unit->section =
12100 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12101 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12102 is_debug_types
12103 ? &dwp_file->sections.types
12104 : &dwp_file->sections.info,
12105 sections.info_or_types_offset,
12106 sections.info_or_types_size);
12107 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12108
12109 return dwo_unit;
12110 }
12111
12112 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12113 Returns NULL if the signature isn't found. */
12114
12115 static struct dwo_unit *
12116 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12117 struct dwp_file *dwp_file, const char *comp_dir,
12118 ULONGEST signature, int is_debug_types)
12119 {
12120 const struct dwp_hash_table *dwp_htab =
12121 is_debug_types ? dwp_file->tus : dwp_file->cus;
12122 bfd *dbfd = dwp_file->dbfd.get ();
12123 uint32_t mask = dwp_htab->nr_slots - 1;
12124 uint32_t hash = signature & mask;
12125 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12126 unsigned int i;
12127 void **slot;
12128 struct dwo_unit find_dwo_cu;
12129
12130 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12131 find_dwo_cu.signature = signature;
12132 slot = htab_find_slot (is_debug_types
12133 ? dwp_file->loaded_tus.get ()
12134 : dwp_file->loaded_cus.get (),
12135 &find_dwo_cu, INSERT);
12136
12137 if (*slot != NULL)
12138 return (struct dwo_unit *) *slot;
12139
12140 /* Use a for loop so that we don't loop forever on bad debug info. */
12141 for (i = 0; i < dwp_htab->nr_slots; ++i)
12142 {
12143 ULONGEST signature_in_table;
12144
12145 signature_in_table =
12146 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12147 if (signature_in_table == signature)
12148 {
12149 uint32_t unit_index =
12150 read_4_bytes (dbfd,
12151 dwp_htab->unit_table + hash * sizeof (uint32_t));
12152
12153 if (dwp_file->version == 1)
12154 {
12155 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12156 dwp_file, unit_index,
12157 comp_dir, signature,
12158 is_debug_types);
12159 }
12160 else
12161 {
12162 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12163 dwp_file, unit_index,
12164 comp_dir, signature,
12165 is_debug_types);
12166 }
12167 return (struct dwo_unit *) *slot;
12168 }
12169 if (signature_in_table == 0)
12170 return NULL;
12171 hash = (hash + hash2) & mask;
12172 }
12173
12174 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12175 " [in module %s]"),
12176 dwp_file->name);
12177 }
12178
12179 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12180 Open the file specified by FILE_NAME and hand it off to BFD for
12181 preliminary analysis. Return a newly initialized bfd *, which
12182 includes a canonicalized copy of FILE_NAME.
12183 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12184 SEARCH_CWD is true if the current directory is to be searched.
12185 It will be searched before debug-file-directory.
12186 If successful, the file is added to the bfd include table of the
12187 objfile's bfd (see gdb_bfd_record_inclusion).
12188 If unable to find/open the file, return NULL.
12189 NOTE: This function is derived from symfile_bfd_open. */
12190
12191 static gdb_bfd_ref_ptr
12192 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12193 const char *file_name, int is_dwp, int search_cwd)
12194 {
12195 int desc;
12196 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12197 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12198 to debug_file_directory. */
12199 const char *search_path;
12200 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12201
12202 gdb::unique_xmalloc_ptr<char> search_path_holder;
12203 if (search_cwd)
12204 {
12205 if (*debug_file_directory != '\0')
12206 {
12207 search_path_holder.reset (concat (".", dirname_separator_string,
12208 debug_file_directory,
12209 (char *) NULL));
12210 search_path = search_path_holder.get ();
12211 }
12212 else
12213 search_path = ".";
12214 }
12215 else
12216 search_path = debug_file_directory;
12217
12218 openp_flags flags = OPF_RETURN_REALPATH;
12219 if (is_dwp)
12220 flags |= OPF_SEARCH_IN_PATH;
12221
12222 gdb::unique_xmalloc_ptr<char> absolute_name;
12223 desc = openp (search_path, flags, file_name,
12224 O_RDONLY | O_BINARY, &absolute_name);
12225 if (desc < 0)
12226 return NULL;
12227
12228 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12229 gnutarget, desc));
12230 if (sym_bfd == NULL)
12231 return NULL;
12232 bfd_set_cacheable (sym_bfd.get (), 1);
12233
12234 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12235 return NULL;
12236
12237 /* Success. Record the bfd as having been included by the objfile's bfd.
12238 This is important because things like demangled_names_hash lives in the
12239 objfile's per_bfd space and may have references to things like symbol
12240 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12241 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12242
12243 return sym_bfd;
12244 }
12245
12246 /* Try to open DWO file FILE_NAME.
12247 COMP_DIR is the DW_AT_comp_dir attribute.
12248 The result is the bfd handle of the file.
12249 If there is a problem finding or opening the file, return NULL.
12250 Upon success, the canonicalized path of the file is stored in the bfd,
12251 same as symfile_bfd_open. */
12252
12253 static gdb_bfd_ref_ptr
12254 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12255 const char *file_name, const char *comp_dir)
12256 {
12257 if (IS_ABSOLUTE_PATH (file_name))
12258 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12259 0 /*is_dwp*/, 0 /*search_cwd*/);
12260
12261 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12262
12263 if (comp_dir != NULL)
12264 {
12265 gdb::unique_xmalloc_ptr<char> path_to_try
12266 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12267
12268 /* NOTE: If comp_dir is a relative path, this will also try the
12269 search path, which seems useful. */
12270 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12271 path_to_try.get (),
12272 0 /*is_dwp*/,
12273 1 /*search_cwd*/));
12274 if (abfd != NULL)
12275 return abfd;
12276 }
12277
12278 /* That didn't work, try debug-file-directory, which, despite its name,
12279 is a list of paths. */
12280
12281 if (*debug_file_directory == '\0')
12282 return NULL;
12283
12284 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12285 0 /*is_dwp*/, 1 /*search_cwd*/);
12286 }
12287
12288 /* This function is mapped across the sections and remembers the offset and
12289 size of each of the DWO debugging sections we are interested in. */
12290
12291 static void
12292 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12293 {
12294 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12295 const struct dwop_section_names *names = &dwop_section_names;
12296
12297 if (section_is_p (sectp->name, &names->abbrev_dwo))
12298 {
12299 dwo_sections->abbrev.s.section = sectp;
12300 dwo_sections->abbrev.size = bfd_section_size (sectp);
12301 }
12302 else if (section_is_p (sectp->name, &names->info_dwo))
12303 {
12304 dwo_sections->info.s.section = sectp;
12305 dwo_sections->info.size = bfd_section_size (sectp);
12306 }
12307 else if (section_is_p (sectp->name, &names->line_dwo))
12308 {
12309 dwo_sections->line.s.section = sectp;
12310 dwo_sections->line.size = bfd_section_size (sectp);
12311 }
12312 else if (section_is_p (sectp->name, &names->loc_dwo))
12313 {
12314 dwo_sections->loc.s.section = sectp;
12315 dwo_sections->loc.size = bfd_section_size (sectp);
12316 }
12317 else if (section_is_p (sectp->name, &names->loclists_dwo))
12318 {
12319 dwo_sections->loclists.s.section = sectp;
12320 dwo_sections->loclists.size = bfd_section_size (sectp);
12321 }
12322 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12323 {
12324 dwo_sections->macinfo.s.section = sectp;
12325 dwo_sections->macinfo.size = bfd_section_size (sectp);
12326 }
12327 else if (section_is_p (sectp->name, &names->macro_dwo))
12328 {
12329 dwo_sections->macro.s.section = sectp;
12330 dwo_sections->macro.size = bfd_section_size (sectp);
12331 }
12332 else if (section_is_p (sectp->name, &names->str_dwo))
12333 {
12334 dwo_sections->str.s.section = sectp;
12335 dwo_sections->str.size = bfd_section_size (sectp);
12336 }
12337 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12338 {
12339 dwo_sections->str_offsets.s.section = sectp;
12340 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12341 }
12342 else if (section_is_p (sectp->name, &names->types_dwo))
12343 {
12344 struct dwarf2_section_info type_section;
12345
12346 memset (&type_section, 0, sizeof (type_section));
12347 type_section.s.section = sectp;
12348 type_section.size = bfd_section_size (sectp);
12349 dwo_sections->types.push_back (type_section);
12350 }
12351 }
12352
12353 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12354 by PER_CU. This is for the non-DWP case.
12355 The result is NULL if DWO_NAME can't be found. */
12356
12357 static struct dwo_file *
12358 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12359 const char *dwo_name, const char *comp_dir)
12360 {
12361 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12362
12363 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12364 if (dbfd == NULL)
12365 {
12366 if (dwarf_read_debug)
12367 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12368 return NULL;
12369 }
12370
12371 dwo_file_up dwo_file (new struct dwo_file);
12372 dwo_file->dwo_name = dwo_name;
12373 dwo_file->comp_dir = comp_dir;
12374 dwo_file->dbfd = std::move (dbfd);
12375
12376 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12377 &dwo_file->sections);
12378
12379 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12380 dwo_file->sections.info, dwo_file->cus);
12381
12382 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12383 dwo_file->sections.types, dwo_file->tus);
12384
12385 if (dwarf_read_debug)
12386 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12387
12388 return dwo_file.release ();
12389 }
12390
12391 /* This function is mapped across the sections and remembers the offset and
12392 size of each of the DWP debugging sections common to version 1 and 2 that
12393 we are interested in. */
12394
12395 static void
12396 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12397 void *dwp_file_ptr)
12398 {
12399 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12400 const struct dwop_section_names *names = &dwop_section_names;
12401 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12402
12403 /* Record the ELF section number for later lookup: this is what the
12404 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12405 gdb_assert (elf_section_nr < dwp_file->num_sections);
12406 dwp_file->elf_sections[elf_section_nr] = sectp;
12407
12408 /* Look for specific sections that we need. */
12409 if (section_is_p (sectp->name, &names->str_dwo))
12410 {
12411 dwp_file->sections.str.s.section = sectp;
12412 dwp_file->sections.str.size = bfd_section_size (sectp);
12413 }
12414 else if (section_is_p (sectp->name, &names->cu_index))
12415 {
12416 dwp_file->sections.cu_index.s.section = sectp;
12417 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12418 }
12419 else if (section_is_p (sectp->name, &names->tu_index))
12420 {
12421 dwp_file->sections.tu_index.s.section = sectp;
12422 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12423 }
12424 }
12425
12426 /* This function is mapped across the sections and remembers the offset and
12427 size of each of the DWP version 2 debugging sections that we are interested
12428 in. This is split into a separate function because we don't know if we
12429 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12430
12431 static void
12432 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12433 {
12434 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12435 const struct dwop_section_names *names = &dwop_section_names;
12436 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12437
12438 /* Record the ELF section number for later lookup: this is what the
12439 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12440 gdb_assert (elf_section_nr < dwp_file->num_sections);
12441 dwp_file->elf_sections[elf_section_nr] = sectp;
12442
12443 /* Look for specific sections that we need. */
12444 if (section_is_p (sectp->name, &names->abbrev_dwo))
12445 {
12446 dwp_file->sections.abbrev.s.section = sectp;
12447 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12448 }
12449 else if (section_is_p (sectp->name, &names->info_dwo))
12450 {
12451 dwp_file->sections.info.s.section = sectp;
12452 dwp_file->sections.info.size = bfd_section_size (sectp);
12453 }
12454 else if (section_is_p (sectp->name, &names->line_dwo))
12455 {
12456 dwp_file->sections.line.s.section = sectp;
12457 dwp_file->sections.line.size = bfd_section_size (sectp);
12458 }
12459 else if (section_is_p (sectp->name, &names->loc_dwo))
12460 {
12461 dwp_file->sections.loc.s.section = sectp;
12462 dwp_file->sections.loc.size = bfd_section_size (sectp);
12463 }
12464 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12465 {
12466 dwp_file->sections.macinfo.s.section = sectp;
12467 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12468 }
12469 else if (section_is_p (sectp->name, &names->macro_dwo))
12470 {
12471 dwp_file->sections.macro.s.section = sectp;
12472 dwp_file->sections.macro.size = bfd_section_size (sectp);
12473 }
12474 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12475 {
12476 dwp_file->sections.str_offsets.s.section = sectp;
12477 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12478 }
12479 else if (section_is_p (sectp->name, &names->types_dwo))
12480 {
12481 dwp_file->sections.types.s.section = sectp;
12482 dwp_file->sections.types.size = bfd_section_size (sectp);
12483 }
12484 }
12485
12486 /* Hash function for dwp_file loaded CUs/TUs. */
12487
12488 static hashval_t
12489 hash_dwp_loaded_cutus (const void *item)
12490 {
12491 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12492
12493 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12494 return dwo_unit->signature;
12495 }
12496
12497 /* Equality function for dwp_file loaded CUs/TUs. */
12498
12499 static int
12500 eq_dwp_loaded_cutus (const void *a, const void *b)
12501 {
12502 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12503 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12504
12505 return dua->signature == dub->signature;
12506 }
12507
12508 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12509
12510 static htab_up
12511 allocate_dwp_loaded_cutus_table ()
12512 {
12513 return htab_up (htab_create_alloc (3,
12514 hash_dwp_loaded_cutus,
12515 eq_dwp_loaded_cutus,
12516 NULL, xcalloc, xfree));
12517 }
12518
12519 /* Try to open DWP file FILE_NAME.
12520 The result is the bfd handle of the file.
12521 If there is a problem finding or opening the file, return NULL.
12522 Upon success, the canonicalized path of the file is stored in the bfd,
12523 same as symfile_bfd_open. */
12524
12525 static gdb_bfd_ref_ptr
12526 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12527 const char *file_name)
12528 {
12529 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12530 1 /*is_dwp*/,
12531 1 /*search_cwd*/));
12532 if (abfd != NULL)
12533 return abfd;
12534
12535 /* Work around upstream bug 15652.
12536 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12537 [Whether that's a "bug" is debatable, but it is getting in our way.]
12538 We have no real idea where the dwp file is, because gdb's realpath-ing
12539 of the executable's path may have discarded the needed info.
12540 [IWBN if the dwp file name was recorded in the executable, akin to
12541 .gnu_debuglink, but that doesn't exist yet.]
12542 Strip the directory from FILE_NAME and search again. */
12543 if (*debug_file_directory != '\0')
12544 {
12545 /* Don't implicitly search the current directory here.
12546 If the user wants to search "." to handle this case,
12547 it must be added to debug-file-directory. */
12548 return try_open_dwop_file (dwarf2_per_objfile,
12549 lbasename (file_name), 1 /*is_dwp*/,
12550 0 /*search_cwd*/);
12551 }
12552
12553 return NULL;
12554 }
12555
12556 /* Initialize the use of the DWP file for the current objfile.
12557 By convention the name of the DWP file is ${objfile}.dwp.
12558 The result is NULL if it can't be found. */
12559
12560 static std::unique_ptr<struct dwp_file>
12561 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12562 {
12563 struct objfile *objfile = dwarf2_per_objfile->objfile;
12564
12565 /* Try to find first .dwp for the binary file before any symbolic links
12566 resolving. */
12567
12568 /* If the objfile is a debug file, find the name of the real binary
12569 file and get the name of dwp file from there. */
12570 std::string dwp_name;
12571 if (objfile->separate_debug_objfile_backlink != NULL)
12572 {
12573 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12574 const char *backlink_basename = lbasename (backlink->original_name);
12575
12576 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12577 }
12578 else
12579 dwp_name = objfile->original_name;
12580
12581 dwp_name += ".dwp";
12582
12583 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12584 if (dbfd == NULL
12585 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12586 {
12587 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12588 dwp_name = objfile_name (objfile);
12589 dwp_name += ".dwp";
12590 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12591 }
12592
12593 if (dbfd == NULL)
12594 {
12595 if (dwarf_read_debug)
12596 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12597 return std::unique_ptr<dwp_file> ();
12598 }
12599
12600 const char *name = bfd_get_filename (dbfd.get ());
12601 std::unique_ptr<struct dwp_file> dwp_file
12602 (new struct dwp_file (name, std::move (dbfd)));
12603
12604 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12605 dwp_file->elf_sections =
12606 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12607 dwp_file->num_sections, asection *);
12608
12609 bfd_map_over_sections (dwp_file->dbfd.get (),
12610 dwarf2_locate_common_dwp_sections,
12611 dwp_file.get ());
12612
12613 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12614 0);
12615
12616 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12617 1);
12618
12619 /* The DWP file version is stored in the hash table. Oh well. */
12620 if (dwp_file->cus && dwp_file->tus
12621 && dwp_file->cus->version != dwp_file->tus->version)
12622 {
12623 /* Technically speaking, we should try to limp along, but this is
12624 pretty bizarre. We use pulongest here because that's the established
12625 portability solution (e.g, we cannot use %u for uint32_t). */
12626 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12627 " TU version %s [in DWP file %s]"),
12628 pulongest (dwp_file->cus->version),
12629 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12630 }
12631
12632 if (dwp_file->cus)
12633 dwp_file->version = dwp_file->cus->version;
12634 else if (dwp_file->tus)
12635 dwp_file->version = dwp_file->tus->version;
12636 else
12637 dwp_file->version = 2;
12638
12639 if (dwp_file->version == 2)
12640 bfd_map_over_sections (dwp_file->dbfd.get (),
12641 dwarf2_locate_v2_dwp_sections,
12642 dwp_file.get ());
12643
12644 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12645 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12646
12647 if (dwarf_read_debug)
12648 {
12649 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12650 fprintf_unfiltered (gdb_stdlog,
12651 " %s CUs, %s TUs\n",
12652 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12653 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12654 }
12655
12656 return dwp_file;
12657 }
12658
12659 /* Wrapper around open_and_init_dwp_file, only open it once. */
12660
12661 static struct dwp_file *
12662 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12663 {
12664 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12665 {
12666 dwarf2_per_objfile->per_bfd->dwp_file
12667 = open_and_init_dwp_file (dwarf2_per_objfile);
12668 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12669 }
12670 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12671 }
12672
12673 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12674 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12675 or in the DWP file for the objfile, referenced by THIS_UNIT.
12676 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12677 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12678
12679 This is called, for example, when wanting to read a variable with a
12680 complex location. Therefore we don't want to do file i/o for every call.
12681 Therefore we don't want to look for a DWO file on every call.
12682 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12683 then we check if we've already seen DWO_NAME, and only THEN do we check
12684 for a DWO file.
12685
12686 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12687 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12688
12689 static struct dwo_unit *
12690 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12691 const char *dwo_name, const char *comp_dir,
12692 ULONGEST signature, int is_debug_types)
12693 {
12694 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12695 struct objfile *objfile = dwarf2_per_objfile->objfile;
12696 const char *kind = is_debug_types ? "TU" : "CU";
12697 void **dwo_file_slot;
12698 struct dwo_file *dwo_file;
12699 struct dwp_file *dwp_file;
12700
12701 /* First see if there's a DWP file.
12702 If we have a DWP file but didn't find the DWO inside it, don't
12703 look for the original DWO file. It makes gdb behave differently
12704 depending on whether one is debugging in the build tree. */
12705
12706 dwp_file = get_dwp_file (dwarf2_per_objfile);
12707 if (dwp_file != NULL)
12708 {
12709 const struct dwp_hash_table *dwp_htab =
12710 is_debug_types ? dwp_file->tus : dwp_file->cus;
12711
12712 if (dwp_htab != NULL)
12713 {
12714 struct dwo_unit *dwo_cutu =
12715 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12716 signature, is_debug_types);
12717
12718 if (dwo_cutu != NULL)
12719 {
12720 if (dwarf_read_debug)
12721 {
12722 fprintf_unfiltered (gdb_stdlog,
12723 "Virtual DWO %s %s found: @%s\n",
12724 kind, hex_string (signature),
12725 host_address_to_string (dwo_cutu));
12726 }
12727 return dwo_cutu;
12728 }
12729 }
12730 }
12731 else
12732 {
12733 /* No DWP file, look for the DWO file. */
12734
12735 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12736 dwo_name, comp_dir);
12737 if (*dwo_file_slot == NULL)
12738 {
12739 /* Read in the file and build a table of the CUs/TUs it contains. */
12740 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12741 }
12742 /* NOTE: This will be NULL if unable to open the file. */
12743 dwo_file = (struct dwo_file *) *dwo_file_slot;
12744
12745 if (dwo_file != NULL)
12746 {
12747 struct dwo_unit *dwo_cutu = NULL;
12748
12749 if (is_debug_types && dwo_file->tus)
12750 {
12751 struct dwo_unit find_dwo_cutu;
12752
12753 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12754 find_dwo_cutu.signature = signature;
12755 dwo_cutu
12756 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12757 &find_dwo_cutu);
12758 }
12759 else if (!is_debug_types && dwo_file->cus)
12760 {
12761 struct dwo_unit find_dwo_cutu;
12762
12763 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12764 find_dwo_cutu.signature = signature;
12765 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12766 &find_dwo_cutu);
12767 }
12768
12769 if (dwo_cutu != NULL)
12770 {
12771 if (dwarf_read_debug)
12772 {
12773 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12774 kind, dwo_name, hex_string (signature),
12775 host_address_to_string (dwo_cutu));
12776 }
12777 return dwo_cutu;
12778 }
12779 }
12780 }
12781
12782 /* We didn't find it. This could mean a dwo_id mismatch, or
12783 someone deleted the DWO/DWP file, or the search path isn't set up
12784 correctly to find the file. */
12785
12786 if (dwarf_read_debug)
12787 {
12788 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12789 kind, dwo_name, hex_string (signature));
12790 }
12791
12792 /* This is a warning and not a complaint because it can be caused by
12793 pilot error (e.g., user accidentally deleting the DWO). */
12794 {
12795 /* Print the name of the DWP file if we looked there, helps the user
12796 better diagnose the problem. */
12797 std::string dwp_text;
12798
12799 if (dwp_file != NULL)
12800 dwp_text = string_printf (" [in DWP file %s]",
12801 lbasename (dwp_file->name));
12802
12803 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12804 " [in module %s]"),
12805 kind, dwo_name, hex_string (signature),
12806 dwp_text.c_str (),
12807 this_unit->is_debug_types ? "TU" : "CU",
12808 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12809 }
12810 return NULL;
12811 }
12812
12813 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12814 See lookup_dwo_cutu_unit for details. */
12815
12816 static struct dwo_unit *
12817 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12818 const char *dwo_name, const char *comp_dir,
12819 ULONGEST signature)
12820 {
12821 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12822 }
12823
12824 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12825 See lookup_dwo_cutu_unit for details. */
12826
12827 static struct dwo_unit *
12828 lookup_dwo_type_unit (struct signatured_type *this_tu,
12829 const char *dwo_name, const char *comp_dir)
12830 {
12831 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12832 }
12833
12834 /* Traversal function for queue_and_load_all_dwo_tus. */
12835
12836 static int
12837 queue_and_load_dwo_tu (void **slot, void *info)
12838 {
12839 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12840 dwarf2_cu *cu = (dwarf2_cu *) info;
12841 ULONGEST signature = dwo_unit->signature;
12842 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12843
12844 if (sig_type != NULL)
12845 {
12846 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12847
12848 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12849 a real dependency of PER_CU on SIG_TYPE. That is detected later
12850 while processing PER_CU. */
12851 if (maybe_queue_comp_unit (NULL, sig_cu, cu->language))
12852 load_full_type_unit (sig_cu, cu->per_objfile);
12853 cu->per_cu->imported_symtabs_push (sig_cu);
12854 }
12855
12856 return 1;
12857 }
12858
12859 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12860 The DWO may have the only definition of the type, though it may not be
12861 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12862 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12863
12864 static void
12865 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12866 {
12867 struct dwo_unit *dwo_unit;
12868 struct dwo_file *dwo_file;
12869
12870 gdb_assert (!per_cu->is_debug_types);
12871 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12872 gdb_assert (per_cu->cu != NULL);
12873
12874 dwo_unit = per_cu->cu->dwo_unit;
12875 gdb_assert (dwo_unit != NULL);
12876
12877 dwo_file = dwo_unit->dwo_file;
12878 if (dwo_file->tus != NULL)
12879 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12880 per_cu->cu);
12881 }
12882
12883 /* Read in various DIEs. */
12884
12885 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12886 Inherit only the children of the DW_AT_abstract_origin DIE not being
12887 already referenced by DW_AT_abstract_origin from the children of the
12888 current DIE. */
12889
12890 static void
12891 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12892 {
12893 struct die_info *child_die;
12894 sect_offset *offsetp;
12895 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12896 struct die_info *origin_die;
12897 /* Iterator of the ORIGIN_DIE children. */
12898 struct die_info *origin_child_die;
12899 struct attribute *attr;
12900 struct dwarf2_cu *origin_cu;
12901 struct pending **origin_previous_list_in_scope;
12902
12903 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12904 if (!attr)
12905 return;
12906
12907 /* Note that following die references may follow to a die in a
12908 different cu. */
12909
12910 origin_cu = cu;
12911 origin_die = follow_die_ref (die, attr, &origin_cu);
12912
12913 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12914 symbols in. */
12915 origin_previous_list_in_scope = origin_cu->list_in_scope;
12916 origin_cu->list_in_scope = cu->list_in_scope;
12917
12918 if (die->tag != origin_die->tag
12919 && !(die->tag == DW_TAG_inlined_subroutine
12920 && origin_die->tag == DW_TAG_subprogram))
12921 complaint (_("DIE %s and its abstract origin %s have different tags"),
12922 sect_offset_str (die->sect_off),
12923 sect_offset_str (origin_die->sect_off));
12924
12925 std::vector<sect_offset> offsets;
12926
12927 for (child_die = die->child;
12928 child_die && child_die->tag;
12929 child_die = child_die->sibling)
12930 {
12931 struct die_info *child_origin_die;
12932 struct dwarf2_cu *child_origin_cu;
12933
12934 /* We are trying to process concrete instance entries:
12935 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12936 it's not relevant to our analysis here. i.e. detecting DIEs that are
12937 present in the abstract instance but not referenced in the concrete
12938 one. */
12939 if (child_die->tag == DW_TAG_call_site
12940 || child_die->tag == DW_TAG_GNU_call_site)
12941 continue;
12942
12943 /* For each CHILD_DIE, find the corresponding child of
12944 ORIGIN_DIE. If there is more than one layer of
12945 DW_AT_abstract_origin, follow them all; there shouldn't be,
12946 but GCC versions at least through 4.4 generate this (GCC PR
12947 40573). */
12948 child_origin_die = child_die;
12949 child_origin_cu = cu;
12950 while (1)
12951 {
12952 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12953 child_origin_cu);
12954 if (attr == NULL)
12955 break;
12956 child_origin_die = follow_die_ref (child_origin_die, attr,
12957 &child_origin_cu);
12958 }
12959
12960 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12961 counterpart may exist. */
12962 if (child_origin_die != child_die)
12963 {
12964 if (child_die->tag != child_origin_die->tag
12965 && !(child_die->tag == DW_TAG_inlined_subroutine
12966 && child_origin_die->tag == DW_TAG_subprogram))
12967 complaint (_("Child DIE %s and its abstract origin %s have "
12968 "different tags"),
12969 sect_offset_str (child_die->sect_off),
12970 sect_offset_str (child_origin_die->sect_off));
12971 if (child_origin_die->parent != origin_die)
12972 complaint (_("Child DIE %s and its abstract origin %s have "
12973 "different parents"),
12974 sect_offset_str (child_die->sect_off),
12975 sect_offset_str (child_origin_die->sect_off));
12976 else
12977 offsets.push_back (child_origin_die->sect_off);
12978 }
12979 }
12980 std::sort (offsets.begin (), offsets.end ());
12981 sect_offset *offsets_end = offsets.data () + offsets.size ();
12982 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12983 if (offsetp[-1] == *offsetp)
12984 complaint (_("Multiple children of DIE %s refer "
12985 "to DIE %s as their abstract origin"),
12986 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12987
12988 offsetp = offsets.data ();
12989 origin_child_die = origin_die->child;
12990 while (origin_child_die && origin_child_die->tag)
12991 {
12992 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12993 while (offsetp < offsets_end
12994 && *offsetp < origin_child_die->sect_off)
12995 offsetp++;
12996 if (offsetp >= offsets_end
12997 || *offsetp > origin_child_die->sect_off)
12998 {
12999 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13000 Check whether we're already processing ORIGIN_CHILD_DIE.
13001 This can happen with mutually referenced abstract_origins.
13002 PR 16581. */
13003 if (!origin_child_die->in_process)
13004 process_die (origin_child_die, origin_cu);
13005 }
13006 origin_child_die = origin_child_die->sibling;
13007 }
13008 origin_cu->list_in_scope = origin_previous_list_in_scope;
13009
13010 if (cu != origin_cu)
13011 compute_delayed_physnames (origin_cu);
13012 }
13013
13014 static void
13015 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13016 {
13017 struct objfile *objfile = cu->per_objfile->objfile;
13018 struct gdbarch *gdbarch = objfile->arch ();
13019 struct context_stack *newobj;
13020 CORE_ADDR lowpc;
13021 CORE_ADDR highpc;
13022 struct die_info *child_die;
13023 struct attribute *attr, *call_line, *call_file;
13024 const char *name;
13025 CORE_ADDR baseaddr;
13026 struct block *block;
13027 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13028 std::vector<struct symbol *> template_args;
13029 struct template_symbol *templ_func = NULL;
13030
13031 if (inlined_func)
13032 {
13033 /* If we do not have call site information, we can't show the
13034 caller of this inlined function. That's too confusing, so
13035 only use the scope for local variables. */
13036 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13037 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13038 if (call_line == NULL || call_file == NULL)
13039 {
13040 read_lexical_block_scope (die, cu);
13041 return;
13042 }
13043 }
13044
13045 baseaddr = objfile->text_section_offset ();
13046
13047 name = dwarf2_name (die, cu);
13048
13049 /* Ignore functions with missing or empty names. These are actually
13050 illegal according to the DWARF standard. */
13051 if (name == NULL)
13052 {
13053 complaint (_("missing name for subprogram DIE at %s"),
13054 sect_offset_str (die->sect_off));
13055 return;
13056 }
13057
13058 /* Ignore functions with missing or invalid low and high pc attributes. */
13059 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13060 <= PC_BOUNDS_INVALID)
13061 {
13062 attr = dwarf2_attr (die, DW_AT_external, cu);
13063 if (!attr || !DW_UNSND (attr))
13064 complaint (_("cannot get low and high bounds "
13065 "for subprogram DIE at %s"),
13066 sect_offset_str (die->sect_off));
13067 return;
13068 }
13069
13070 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13071 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13072
13073 /* If we have any template arguments, then we must allocate a
13074 different sort of symbol. */
13075 for (child_die = die->child; child_die; child_die = child_die->sibling)
13076 {
13077 if (child_die->tag == DW_TAG_template_type_param
13078 || child_die->tag == DW_TAG_template_value_param)
13079 {
13080 templ_func = new (&objfile->objfile_obstack) template_symbol;
13081 templ_func->subclass = SYMBOL_TEMPLATE;
13082 break;
13083 }
13084 }
13085
13086 newobj = cu->get_builder ()->push_context (0, lowpc);
13087 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13088 (struct symbol *) templ_func);
13089
13090 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13091 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13092 cu->language);
13093
13094 /* If there is a location expression for DW_AT_frame_base, record
13095 it. */
13096 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13097 if (attr != nullptr)
13098 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13099
13100 /* If there is a location for the static link, record it. */
13101 newobj->static_link = NULL;
13102 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13103 if (attr != nullptr)
13104 {
13105 newobj->static_link
13106 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13107 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13108 cu->per_cu->addr_type ());
13109 }
13110
13111 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13112
13113 if (die->child != NULL)
13114 {
13115 child_die = die->child;
13116 while (child_die && child_die->tag)
13117 {
13118 if (child_die->tag == DW_TAG_template_type_param
13119 || child_die->tag == DW_TAG_template_value_param)
13120 {
13121 struct symbol *arg = new_symbol (child_die, NULL, cu);
13122
13123 if (arg != NULL)
13124 template_args.push_back (arg);
13125 }
13126 else
13127 process_die (child_die, cu);
13128 child_die = child_die->sibling;
13129 }
13130 }
13131
13132 inherit_abstract_dies (die, cu);
13133
13134 /* If we have a DW_AT_specification, we might need to import using
13135 directives from the context of the specification DIE. See the
13136 comment in determine_prefix. */
13137 if (cu->language == language_cplus
13138 && dwarf2_attr (die, DW_AT_specification, cu))
13139 {
13140 struct dwarf2_cu *spec_cu = cu;
13141 struct die_info *spec_die = die_specification (die, &spec_cu);
13142
13143 while (spec_die)
13144 {
13145 child_die = spec_die->child;
13146 while (child_die && child_die->tag)
13147 {
13148 if (child_die->tag == DW_TAG_imported_module)
13149 process_die (child_die, spec_cu);
13150 child_die = child_die->sibling;
13151 }
13152
13153 /* In some cases, GCC generates specification DIEs that
13154 themselves contain DW_AT_specification attributes. */
13155 spec_die = die_specification (spec_die, &spec_cu);
13156 }
13157 }
13158
13159 struct context_stack cstk = cu->get_builder ()->pop_context ();
13160 /* Make a block for the local symbols within. */
13161 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13162 cstk.static_link, lowpc, highpc);
13163
13164 /* For C++, set the block's scope. */
13165 if ((cu->language == language_cplus
13166 || cu->language == language_fortran
13167 || cu->language == language_d
13168 || cu->language == language_rust)
13169 && cu->processing_has_namespace_info)
13170 block_set_scope (block, determine_prefix (die, cu),
13171 &objfile->objfile_obstack);
13172
13173 /* If we have address ranges, record them. */
13174 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13175
13176 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13177
13178 /* Attach template arguments to function. */
13179 if (!template_args.empty ())
13180 {
13181 gdb_assert (templ_func != NULL);
13182
13183 templ_func->n_template_arguments = template_args.size ();
13184 templ_func->template_arguments
13185 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13186 templ_func->n_template_arguments);
13187 memcpy (templ_func->template_arguments,
13188 template_args.data (),
13189 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13190
13191 /* Make sure that the symtab is set on the new symbols. Even
13192 though they don't appear in this symtab directly, other parts
13193 of gdb assume that symbols do, and this is reasonably
13194 true. */
13195 for (symbol *sym : template_args)
13196 symbol_set_symtab (sym, symbol_symtab (templ_func));
13197 }
13198
13199 /* In C++, we can have functions nested inside functions (e.g., when
13200 a function declares a class that has methods). This means that
13201 when we finish processing a function scope, we may need to go
13202 back to building a containing block's symbol lists. */
13203 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13204 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13205
13206 /* If we've finished processing a top-level function, subsequent
13207 symbols go in the file symbol list. */
13208 if (cu->get_builder ()->outermost_context_p ())
13209 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13210 }
13211
13212 /* Process all the DIES contained within a lexical block scope. Start
13213 a new scope, process the dies, and then close the scope. */
13214
13215 static void
13216 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13217 {
13218 struct objfile *objfile = cu->per_objfile->objfile;
13219 struct gdbarch *gdbarch = objfile->arch ();
13220 CORE_ADDR lowpc, highpc;
13221 struct die_info *child_die;
13222 CORE_ADDR baseaddr;
13223
13224 baseaddr = objfile->text_section_offset ();
13225
13226 /* Ignore blocks with missing or invalid low and high pc attributes. */
13227 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13228 as multiple lexical blocks? Handling children in a sane way would
13229 be nasty. Might be easier to properly extend generic blocks to
13230 describe ranges. */
13231 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13232 {
13233 case PC_BOUNDS_NOT_PRESENT:
13234 /* DW_TAG_lexical_block has no attributes, process its children as if
13235 there was no wrapping by that DW_TAG_lexical_block.
13236 GCC does no longer produces such DWARF since GCC r224161. */
13237 for (child_die = die->child;
13238 child_die != NULL && child_die->tag;
13239 child_die = child_die->sibling)
13240 {
13241 /* We might already be processing this DIE. This can happen
13242 in an unusual circumstance -- where a subroutine A
13243 appears lexically in another subroutine B, but A actually
13244 inlines B. The recursion is broken here, rather than in
13245 inherit_abstract_dies, because it seems better to simply
13246 drop concrete children here. */
13247 if (!child_die->in_process)
13248 process_die (child_die, cu);
13249 }
13250 return;
13251 case PC_BOUNDS_INVALID:
13252 return;
13253 }
13254 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13255 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13256
13257 cu->get_builder ()->push_context (0, lowpc);
13258 if (die->child != NULL)
13259 {
13260 child_die = die->child;
13261 while (child_die && child_die->tag)
13262 {
13263 process_die (child_die, cu);
13264 child_die = child_die->sibling;
13265 }
13266 }
13267 inherit_abstract_dies (die, cu);
13268 struct context_stack cstk = cu->get_builder ()->pop_context ();
13269
13270 if (*cu->get_builder ()->get_local_symbols () != NULL
13271 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13272 {
13273 struct block *block
13274 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13275 cstk.start_addr, highpc);
13276
13277 /* Note that recording ranges after traversing children, as we
13278 do here, means that recording a parent's ranges entails
13279 walking across all its children's ranges as they appear in
13280 the address map, which is quadratic behavior.
13281
13282 It would be nicer to record the parent's ranges before
13283 traversing its children, simply overriding whatever you find
13284 there. But since we don't even decide whether to create a
13285 block until after we've traversed its children, that's hard
13286 to do. */
13287 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13288 }
13289 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13290 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13291 }
13292
13293 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13294
13295 static void
13296 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13297 {
13298 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13299 struct objfile *objfile = per_objfile->objfile;
13300 struct gdbarch *gdbarch = objfile->arch ();
13301 CORE_ADDR pc, baseaddr;
13302 struct attribute *attr;
13303 struct call_site *call_site, call_site_local;
13304 void **slot;
13305 int nparams;
13306 struct die_info *child_die;
13307
13308 baseaddr = objfile->text_section_offset ();
13309
13310 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13311 if (attr == NULL)
13312 {
13313 /* This was a pre-DWARF-5 GNU extension alias
13314 for DW_AT_call_return_pc. */
13315 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13316 }
13317 if (!attr)
13318 {
13319 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13320 "DIE %s [in module %s]"),
13321 sect_offset_str (die->sect_off), objfile_name (objfile));
13322 return;
13323 }
13324 pc = attr->value_as_address () + baseaddr;
13325 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13326
13327 if (cu->call_site_htab == NULL)
13328 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13329 NULL, &objfile->objfile_obstack,
13330 hashtab_obstack_allocate, NULL);
13331 call_site_local.pc = pc;
13332 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13333 if (*slot != NULL)
13334 {
13335 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13336 "DIE %s [in module %s]"),
13337 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13338 objfile_name (objfile));
13339 return;
13340 }
13341
13342 /* Count parameters at the caller. */
13343
13344 nparams = 0;
13345 for (child_die = die->child; child_die && child_die->tag;
13346 child_die = child_die->sibling)
13347 {
13348 if (child_die->tag != DW_TAG_call_site_parameter
13349 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13350 {
13351 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13352 "DW_TAG_call_site child DIE %s [in module %s]"),
13353 child_die->tag, sect_offset_str (child_die->sect_off),
13354 objfile_name (objfile));
13355 continue;
13356 }
13357
13358 nparams++;
13359 }
13360
13361 call_site
13362 = ((struct call_site *)
13363 obstack_alloc (&objfile->objfile_obstack,
13364 sizeof (*call_site)
13365 + (sizeof (*call_site->parameter) * (nparams - 1))));
13366 *slot = call_site;
13367 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13368 call_site->pc = pc;
13369
13370 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13371 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13372 {
13373 struct die_info *func_die;
13374
13375 /* Skip also over DW_TAG_inlined_subroutine. */
13376 for (func_die = die->parent;
13377 func_die && func_die->tag != DW_TAG_subprogram
13378 && func_die->tag != DW_TAG_subroutine_type;
13379 func_die = func_die->parent);
13380
13381 /* DW_AT_call_all_calls is a superset
13382 of DW_AT_call_all_tail_calls. */
13383 if (func_die
13384 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13385 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13386 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13387 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13388 {
13389 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13390 not complete. But keep CALL_SITE for look ups via call_site_htab,
13391 both the initial caller containing the real return address PC and
13392 the final callee containing the current PC of a chain of tail
13393 calls do not need to have the tail call list complete. But any
13394 function candidate for a virtual tail call frame searched via
13395 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13396 determined unambiguously. */
13397 }
13398 else
13399 {
13400 struct type *func_type = NULL;
13401
13402 if (func_die)
13403 func_type = get_die_type (func_die, cu);
13404 if (func_type != NULL)
13405 {
13406 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13407
13408 /* Enlist this call site to the function. */
13409 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13410 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13411 }
13412 else
13413 complaint (_("Cannot find function owning DW_TAG_call_site "
13414 "DIE %s [in module %s]"),
13415 sect_offset_str (die->sect_off), objfile_name (objfile));
13416 }
13417 }
13418
13419 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13420 if (attr == NULL)
13421 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13422 if (attr == NULL)
13423 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13424 if (attr == NULL)
13425 {
13426 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13427 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13428 }
13429 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13430 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13431 /* Keep NULL DWARF_BLOCK. */;
13432 else if (attr->form_is_block ())
13433 {
13434 struct dwarf2_locexpr_baton *dlbaton;
13435
13436 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13437 dlbaton->data = DW_BLOCK (attr)->data;
13438 dlbaton->size = DW_BLOCK (attr)->size;
13439 dlbaton->per_objfile = per_objfile;
13440 dlbaton->per_cu = cu->per_cu;
13441
13442 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13443 }
13444 else if (attr->form_is_ref ())
13445 {
13446 struct dwarf2_cu *target_cu = cu;
13447 struct die_info *target_die;
13448
13449 target_die = follow_die_ref (die, attr, &target_cu);
13450 gdb_assert (target_cu->per_objfile->objfile == objfile);
13451 if (die_is_declaration (target_die, target_cu))
13452 {
13453 const char *target_physname;
13454
13455 /* Prefer the mangled name; otherwise compute the demangled one. */
13456 target_physname = dw2_linkage_name (target_die, target_cu);
13457 if (target_physname == NULL)
13458 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13459 if (target_physname == NULL)
13460 complaint (_("DW_AT_call_target target DIE has invalid "
13461 "physname, for referencing DIE %s [in module %s]"),
13462 sect_offset_str (die->sect_off), objfile_name (objfile));
13463 else
13464 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13465 }
13466 else
13467 {
13468 CORE_ADDR lowpc;
13469
13470 /* DW_AT_entry_pc should be preferred. */
13471 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13472 <= PC_BOUNDS_INVALID)
13473 complaint (_("DW_AT_call_target target DIE has invalid "
13474 "low pc, for referencing DIE %s [in module %s]"),
13475 sect_offset_str (die->sect_off), objfile_name (objfile));
13476 else
13477 {
13478 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13479 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13480 }
13481 }
13482 }
13483 else
13484 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13485 "block nor reference, for DIE %s [in module %s]"),
13486 sect_offset_str (die->sect_off), objfile_name (objfile));
13487
13488 call_site->per_cu = cu->per_cu;
13489
13490 for (child_die = die->child;
13491 child_die && child_die->tag;
13492 child_die = child_die->sibling)
13493 {
13494 struct call_site_parameter *parameter;
13495 struct attribute *loc, *origin;
13496
13497 if (child_die->tag != DW_TAG_call_site_parameter
13498 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13499 {
13500 /* Already printed the complaint above. */
13501 continue;
13502 }
13503
13504 gdb_assert (call_site->parameter_count < nparams);
13505 parameter = &call_site->parameter[call_site->parameter_count];
13506
13507 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13508 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13509 register is contained in DW_AT_call_value. */
13510
13511 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13512 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13513 if (origin == NULL)
13514 {
13515 /* This was a pre-DWARF-5 GNU extension alias
13516 for DW_AT_call_parameter. */
13517 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13518 }
13519 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13520 {
13521 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13522
13523 sect_offset sect_off = origin->get_ref_die_offset ();
13524 if (!cu->header.offset_in_cu_p (sect_off))
13525 {
13526 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13527 binding can be done only inside one CU. Such referenced DIE
13528 therefore cannot be even moved to DW_TAG_partial_unit. */
13529 complaint (_("DW_AT_call_parameter offset is not in CU for "
13530 "DW_TAG_call_site child DIE %s [in module %s]"),
13531 sect_offset_str (child_die->sect_off),
13532 objfile_name (objfile));
13533 continue;
13534 }
13535 parameter->u.param_cu_off
13536 = (cu_offset) (sect_off - cu->header.sect_off);
13537 }
13538 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13539 {
13540 complaint (_("No DW_FORM_block* DW_AT_location for "
13541 "DW_TAG_call_site child DIE %s [in module %s]"),
13542 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13543 continue;
13544 }
13545 else
13546 {
13547 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13548 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13549 if (parameter->u.dwarf_reg != -1)
13550 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13551 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13552 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13553 &parameter->u.fb_offset))
13554 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13555 else
13556 {
13557 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13558 "for DW_FORM_block* DW_AT_location is supported for "
13559 "DW_TAG_call_site child DIE %s "
13560 "[in module %s]"),
13561 sect_offset_str (child_die->sect_off),
13562 objfile_name (objfile));
13563 continue;
13564 }
13565 }
13566
13567 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13568 if (attr == NULL)
13569 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13570 if (attr == NULL || !attr->form_is_block ())
13571 {
13572 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13573 "DW_TAG_call_site child DIE %s [in module %s]"),
13574 sect_offset_str (child_die->sect_off),
13575 objfile_name (objfile));
13576 continue;
13577 }
13578 parameter->value = DW_BLOCK (attr)->data;
13579 parameter->value_size = DW_BLOCK (attr)->size;
13580
13581 /* Parameters are not pre-cleared by memset above. */
13582 parameter->data_value = NULL;
13583 parameter->data_value_size = 0;
13584 call_site->parameter_count++;
13585
13586 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13587 if (attr == NULL)
13588 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13589 if (attr != nullptr)
13590 {
13591 if (!attr->form_is_block ())
13592 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13593 "DW_TAG_call_site child DIE %s [in module %s]"),
13594 sect_offset_str (child_die->sect_off),
13595 objfile_name (objfile));
13596 else
13597 {
13598 parameter->data_value = DW_BLOCK (attr)->data;
13599 parameter->data_value_size = DW_BLOCK (attr)->size;
13600 }
13601 }
13602 }
13603 }
13604
13605 /* Helper function for read_variable. If DIE represents a virtual
13606 table, then return the type of the concrete object that is
13607 associated with the virtual table. Otherwise, return NULL. */
13608
13609 static struct type *
13610 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13611 {
13612 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13613 if (attr == NULL)
13614 return NULL;
13615
13616 /* Find the type DIE. */
13617 struct die_info *type_die = NULL;
13618 struct dwarf2_cu *type_cu = cu;
13619
13620 if (attr->form_is_ref ())
13621 type_die = follow_die_ref (die, attr, &type_cu);
13622 if (type_die == NULL)
13623 return NULL;
13624
13625 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13626 return NULL;
13627 return die_containing_type (type_die, type_cu);
13628 }
13629
13630 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13631
13632 static void
13633 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13634 {
13635 struct rust_vtable_symbol *storage = NULL;
13636
13637 if (cu->language == language_rust)
13638 {
13639 struct type *containing_type = rust_containing_type (die, cu);
13640
13641 if (containing_type != NULL)
13642 {
13643 struct objfile *objfile = cu->per_objfile->objfile;
13644
13645 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13646 storage->concrete_type = containing_type;
13647 storage->subclass = SYMBOL_RUST_VTABLE;
13648 }
13649 }
13650
13651 struct symbol *res = new_symbol (die, NULL, cu, storage);
13652 struct attribute *abstract_origin
13653 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13654 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13655 if (res == NULL && loc && abstract_origin)
13656 {
13657 /* We have a variable without a name, but with a location and an abstract
13658 origin. This may be a concrete instance of an abstract variable
13659 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13660 later. */
13661 struct dwarf2_cu *origin_cu = cu;
13662 struct die_info *origin_die
13663 = follow_die_ref (die, abstract_origin, &origin_cu);
13664 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13665 per_objfile->per_bfd->abstract_to_concrete
13666 [origin_die->sect_off].push_back (die->sect_off);
13667 }
13668 }
13669
13670 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13671 reading .debug_rnglists.
13672 Callback's type should be:
13673 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13674 Return true if the attributes are present and valid, otherwise,
13675 return false. */
13676
13677 template <typename Callback>
13678 static bool
13679 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13680 Callback &&callback)
13681 {
13682 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13683 struct objfile *objfile = dwarf2_per_objfile->objfile;
13684 bfd *obfd = objfile->obfd;
13685 /* Base address selection entry. */
13686 gdb::optional<CORE_ADDR> base;
13687 const gdb_byte *buffer;
13688 CORE_ADDR baseaddr;
13689 bool overflow = false;
13690
13691 base = cu->base_address;
13692
13693 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13694 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13695 {
13696 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13697 offset);
13698 return false;
13699 }
13700 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13701
13702 baseaddr = objfile->text_section_offset ();
13703
13704 while (1)
13705 {
13706 /* Initialize it due to a false compiler warning. */
13707 CORE_ADDR range_beginning = 0, range_end = 0;
13708 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13709 + dwarf2_per_objfile->per_bfd->rnglists.size);
13710 unsigned int bytes_read;
13711
13712 if (buffer == buf_end)
13713 {
13714 overflow = true;
13715 break;
13716 }
13717 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13718 switch (rlet)
13719 {
13720 case DW_RLE_end_of_list:
13721 break;
13722 case DW_RLE_base_address:
13723 if (buffer + cu->header.addr_size > buf_end)
13724 {
13725 overflow = true;
13726 break;
13727 }
13728 base = cu->header.read_address (obfd, buffer, &bytes_read);
13729 buffer += bytes_read;
13730 break;
13731 case DW_RLE_start_length:
13732 if (buffer + cu->header.addr_size > buf_end)
13733 {
13734 overflow = true;
13735 break;
13736 }
13737 range_beginning = cu->header.read_address (obfd, buffer,
13738 &bytes_read);
13739 buffer += bytes_read;
13740 range_end = (range_beginning
13741 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13742 buffer += bytes_read;
13743 if (buffer > buf_end)
13744 {
13745 overflow = true;
13746 break;
13747 }
13748 break;
13749 case DW_RLE_offset_pair:
13750 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13751 buffer += bytes_read;
13752 if (buffer > buf_end)
13753 {
13754 overflow = true;
13755 break;
13756 }
13757 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13758 buffer += bytes_read;
13759 if (buffer > buf_end)
13760 {
13761 overflow = true;
13762 break;
13763 }
13764 break;
13765 case DW_RLE_start_end:
13766 if (buffer + 2 * cu->header.addr_size > buf_end)
13767 {
13768 overflow = true;
13769 break;
13770 }
13771 range_beginning = cu->header.read_address (obfd, buffer,
13772 &bytes_read);
13773 buffer += bytes_read;
13774 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13775 buffer += bytes_read;
13776 break;
13777 default:
13778 complaint (_("Invalid .debug_rnglists data (no base address)"));
13779 return false;
13780 }
13781 if (rlet == DW_RLE_end_of_list || overflow)
13782 break;
13783 if (rlet == DW_RLE_base_address)
13784 continue;
13785
13786 if (!base.has_value ())
13787 {
13788 /* We have no valid base address for the ranges
13789 data. */
13790 complaint (_("Invalid .debug_rnglists data (no base address)"));
13791 return false;
13792 }
13793
13794 if (range_beginning > range_end)
13795 {
13796 /* Inverted range entries are invalid. */
13797 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13798 return false;
13799 }
13800
13801 /* Empty range entries have no effect. */
13802 if (range_beginning == range_end)
13803 continue;
13804
13805 range_beginning += *base;
13806 range_end += *base;
13807
13808 /* A not-uncommon case of bad debug info.
13809 Don't pollute the addrmap with bad data. */
13810 if (range_beginning + baseaddr == 0
13811 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13812 {
13813 complaint (_(".debug_rnglists entry has start address of zero"
13814 " [in module %s]"), objfile_name (objfile));
13815 continue;
13816 }
13817
13818 callback (range_beginning, range_end);
13819 }
13820
13821 if (overflow)
13822 {
13823 complaint (_("Offset %d is not terminated "
13824 "for DW_AT_ranges attribute"),
13825 offset);
13826 return false;
13827 }
13828
13829 return true;
13830 }
13831
13832 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13833 Callback's type should be:
13834 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13835 Return 1 if the attributes are present and valid, otherwise, return 0. */
13836
13837 template <typename Callback>
13838 static int
13839 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13840 Callback &&callback)
13841 {
13842 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13843 struct objfile *objfile = per_objfile->objfile;
13844 struct comp_unit_head *cu_header = &cu->header;
13845 bfd *obfd = objfile->obfd;
13846 unsigned int addr_size = cu_header->addr_size;
13847 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13848 /* Base address selection entry. */
13849 gdb::optional<CORE_ADDR> base;
13850 unsigned int dummy;
13851 const gdb_byte *buffer;
13852 CORE_ADDR baseaddr;
13853
13854 if (cu_header->version >= 5)
13855 return dwarf2_rnglists_process (offset, cu, callback);
13856
13857 base = cu->base_address;
13858
13859 per_objfile->per_bfd->ranges.read (objfile);
13860 if (offset >= per_objfile->per_bfd->ranges.size)
13861 {
13862 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13863 offset);
13864 return 0;
13865 }
13866 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13867
13868 baseaddr = objfile->text_section_offset ();
13869
13870 while (1)
13871 {
13872 CORE_ADDR range_beginning, range_end;
13873
13874 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13875 buffer += addr_size;
13876 range_end = cu->header.read_address (obfd, buffer, &dummy);
13877 buffer += addr_size;
13878 offset += 2 * addr_size;
13879
13880 /* An end of list marker is a pair of zero addresses. */
13881 if (range_beginning == 0 && range_end == 0)
13882 /* Found the end of list entry. */
13883 break;
13884
13885 /* Each base address selection entry is a pair of 2 values.
13886 The first is the largest possible address, the second is
13887 the base address. Check for a base address here. */
13888 if ((range_beginning & mask) == mask)
13889 {
13890 /* If we found the largest possible address, then we already
13891 have the base address in range_end. */
13892 base = range_end;
13893 continue;
13894 }
13895
13896 if (!base.has_value ())
13897 {
13898 /* We have no valid base address for the ranges
13899 data. */
13900 complaint (_("Invalid .debug_ranges data (no base address)"));
13901 return 0;
13902 }
13903
13904 if (range_beginning > range_end)
13905 {
13906 /* Inverted range entries are invalid. */
13907 complaint (_("Invalid .debug_ranges data (inverted range)"));
13908 return 0;
13909 }
13910
13911 /* Empty range entries have no effect. */
13912 if (range_beginning == range_end)
13913 continue;
13914
13915 range_beginning += *base;
13916 range_end += *base;
13917
13918 /* A not-uncommon case of bad debug info.
13919 Don't pollute the addrmap with bad data. */
13920 if (range_beginning + baseaddr == 0
13921 && !per_objfile->per_bfd->has_section_at_zero)
13922 {
13923 complaint (_(".debug_ranges entry has start address of zero"
13924 " [in module %s]"), objfile_name (objfile));
13925 continue;
13926 }
13927
13928 callback (range_beginning, range_end);
13929 }
13930
13931 return 1;
13932 }
13933
13934 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13935 Return 1 if the attributes are present and valid, otherwise, return 0.
13936 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13937
13938 static int
13939 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13940 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13941 dwarf2_psymtab *ranges_pst)
13942 {
13943 struct objfile *objfile = cu->per_objfile->objfile;
13944 struct gdbarch *gdbarch = objfile->arch ();
13945 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13946 int low_set = 0;
13947 CORE_ADDR low = 0;
13948 CORE_ADDR high = 0;
13949 int retval;
13950
13951 retval = dwarf2_ranges_process (offset, cu,
13952 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13953 {
13954 if (ranges_pst != NULL)
13955 {
13956 CORE_ADDR lowpc;
13957 CORE_ADDR highpc;
13958
13959 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13960 range_beginning + baseaddr)
13961 - baseaddr);
13962 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13963 range_end + baseaddr)
13964 - baseaddr);
13965 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13966 lowpc, highpc - 1, ranges_pst);
13967 }
13968
13969 /* FIXME: This is recording everything as a low-high
13970 segment of consecutive addresses. We should have a
13971 data structure for discontiguous block ranges
13972 instead. */
13973 if (! low_set)
13974 {
13975 low = range_beginning;
13976 high = range_end;
13977 low_set = 1;
13978 }
13979 else
13980 {
13981 if (range_beginning < low)
13982 low = range_beginning;
13983 if (range_end > high)
13984 high = range_end;
13985 }
13986 });
13987 if (!retval)
13988 return 0;
13989
13990 if (! low_set)
13991 /* If the first entry is an end-of-list marker, the range
13992 describes an empty scope, i.e. no instructions. */
13993 return 0;
13994
13995 if (low_return)
13996 *low_return = low;
13997 if (high_return)
13998 *high_return = high;
13999 return 1;
14000 }
14001
14002 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14003 definition for the return value. *LOWPC and *HIGHPC are set iff
14004 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14005
14006 static enum pc_bounds_kind
14007 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14008 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14009 dwarf2_psymtab *pst)
14010 {
14011 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14012 struct attribute *attr;
14013 struct attribute *attr_high;
14014 CORE_ADDR low = 0;
14015 CORE_ADDR high = 0;
14016 enum pc_bounds_kind ret;
14017
14018 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14019 if (attr_high)
14020 {
14021 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14022 if (attr != nullptr)
14023 {
14024 low = attr->value_as_address ();
14025 high = attr_high->value_as_address ();
14026 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14027 high += low;
14028 }
14029 else
14030 /* Found high w/o low attribute. */
14031 return PC_BOUNDS_INVALID;
14032
14033 /* Found consecutive range of addresses. */
14034 ret = PC_BOUNDS_HIGH_LOW;
14035 }
14036 else
14037 {
14038 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14039 if (attr != NULL)
14040 {
14041 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14042 We take advantage of the fact that DW_AT_ranges does not appear
14043 in DW_TAG_compile_unit of DWO files. */
14044 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14045 unsigned int ranges_offset = (DW_UNSND (attr)
14046 + (need_ranges_base
14047 ? cu->ranges_base
14048 : 0));
14049
14050 /* Value of the DW_AT_ranges attribute is the offset in the
14051 .debug_ranges section. */
14052 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14053 return PC_BOUNDS_INVALID;
14054 /* Found discontinuous range of addresses. */
14055 ret = PC_BOUNDS_RANGES;
14056 }
14057 else
14058 return PC_BOUNDS_NOT_PRESENT;
14059 }
14060
14061 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14062 if (high <= low)
14063 return PC_BOUNDS_INVALID;
14064
14065 /* When using the GNU linker, .gnu.linkonce. sections are used to
14066 eliminate duplicate copies of functions and vtables and such.
14067 The linker will arbitrarily choose one and discard the others.
14068 The AT_*_pc values for such functions refer to local labels in
14069 these sections. If the section from that file was discarded, the
14070 labels are not in the output, so the relocs get a value of 0.
14071 If this is a discarded function, mark the pc bounds as invalid,
14072 so that GDB will ignore it. */
14073 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14074 return PC_BOUNDS_INVALID;
14075
14076 *lowpc = low;
14077 if (highpc)
14078 *highpc = high;
14079 return ret;
14080 }
14081
14082 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14083 its low and high PC addresses. Do nothing if these addresses could not
14084 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14085 and HIGHPC to the high address if greater than HIGHPC. */
14086
14087 static void
14088 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14089 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14090 struct dwarf2_cu *cu)
14091 {
14092 CORE_ADDR low, high;
14093 struct die_info *child = die->child;
14094
14095 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14096 {
14097 *lowpc = std::min (*lowpc, low);
14098 *highpc = std::max (*highpc, high);
14099 }
14100
14101 /* If the language does not allow nested subprograms (either inside
14102 subprograms or lexical blocks), we're done. */
14103 if (cu->language != language_ada)
14104 return;
14105
14106 /* Check all the children of the given DIE. If it contains nested
14107 subprograms, then check their pc bounds. Likewise, we need to
14108 check lexical blocks as well, as they may also contain subprogram
14109 definitions. */
14110 while (child && child->tag)
14111 {
14112 if (child->tag == DW_TAG_subprogram
14113 || child->tag == DW_TAG_lexical_block)
14114 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14115 child = child->sibling;
14116 }
14117 }
14118
14119 /* Get the low and high pc's represented by the scope DIE, and store
14120 them in *LOWPC and *HIGHPC. If the correct values can't be
14121 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14122
14123 static void
14124 get_scope_pc_bounds (struct die_info *die,
14125 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14126 struct dwarf2_cu *cu)
14127 {
14128 CORE_ADDR best_low = (CORE_ADDR) -1;
14129 CORE_ADDR best_high = (CORE_ADDR) 0;
14130 CORE_ADDR current_low, current_high;
14131
14132 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14133 >= PC_BOUNDS_RANGES)
14134 {
14135 best_low = current_low;
14136 best_high = current_high;
14137 }
14138 else
14139 {
14140 struct die_info *child = die->child;
14141
14142 while (child && child->tag)
14143 {
14144 switch (child->tag) {
14145 case DW_TAG_subprogram:
14146 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14147 break;
14148 case DW_TAG_namespace:
14149 case DW_TAG_module:
14150 /* FIXME: carlton/2004-01-16: Should we do this for
14151 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14152 that current GCC's always emit the DIEs corresponding
14153 to definitions of methods of classes as children of a
14154 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14155 the DIEs giving the declarations, which could be
14156 anywhere). But I don't see any reason why the
14157 standards says that they have to be there. */
14158 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14159
14160 if (current_low != ((CORE_ADDR) -1))
14161 {
14162 best_low = std::min (best_low, current_low);
14163 best_high = std::max (best_high, current_high);
14164 }
14165 break;
14166 default:
14167 /* Ignore. */
14168 break;
14169 }
14170
14171 child = child->sibling;
14172 }
14173 }
14174
14175 *lowpc = best_low;
14176 *highpc = best_high;
14177 }
14178
14179 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14180 in DIE. */
14181
14182 static void
14183 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14184 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14185 {
14186 struct objfile *objfile = cu->per_objfile->objfile;
14187 struct gdbarch *gdbarch = objfile->arch ();
14188 struct attribute *attr;
14189 struct attribute *attr_high;
14190
14191 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14192 if (attr_high)
14193 {
14194 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14195 if (attr != nullptr)
14196 {
14197 CORE_ADDR low = attr->value_as_address ();
14198 CORE_ADDR high = attr_high->value_as_address ();
14199
14200 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14201 high += low;
14202
14203 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14204 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14205 cu->get_builder ()->record_block_range (block, low, high - 1);
14206 }
14207 }
14208
14209 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14210 if (attr != nullptr)
14211 {
14212 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14213 We take advantage of the fact that DW_AT_ranges does not appear
14214 in DW_TAG_compile_unit of DWO files. */
14215 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14216
14217 /* The value of the DW_AT_ranges attribute is the offset of the
14218 address range list in the .debug_ranges section. */
14219 unsigned long offset = (DW_UNSND (attr)
14220 + (need_ranges_base ? cu->ranges_base : 0));
14221
14222 std::vector<blockrange> blockvec;
14223 dwarf2_ranges_process (offset, cu,
14224 [&] (CORE_ADDR start, CORE_ADDR end)
14225 {
14226 start += baseaddr;
14227 end += baseaddr;
14228 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14229 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14230 cu->get_builder ()->record_block_range (block, start, end - 1);
14231 blockvec.emplace_back (start, end);
14232 });
14233
14234 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14235 }
14236 }
14237
14238 /* Check whether the producer field indicates either of GCC < 4.6, or the
14239 Intel C/C++ compiler, and cache the result in CU. */
14240
14241 static void
14242 check_producer (struct dwarf2_cu *cu)
14243 {
14244 int major, minor;
14245
14246 if (cu->producer == NULL)
14247 {
14248 /* For unknown compilers expect their behavior is DWARF version
14249 compliant.
14250
14251 GCC started to support .debug_types sections by -gdwarf-4 since
14252 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14253 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14254 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14255 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14256 }
14257 else if (producer_is_gcc (cu->producer, &major, &minor))
14258 {
14259 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14260 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14261 }
14262 else if (producer_is_icc (cu->producer, &major, &minor))
14263 {
14264 cu->producer_is_icc = true;
14265 cu->producer_is_icc_lt_14 = major < 14;
14266 }
14267 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14268 cu->producer_is_codewarrior = true;
14269 else
14270 {
14271 /* For other non-GCC compilers, expect their behavior is DWARF version
14272 compliant. */
14273 }
14274
14275 cu->checked_producer = true;
14276 }
14277
14278 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14279 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14280 during 4.6.0 experimental. */
14281
14282 static bool
14283 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14284 {
14285 if (!cu->checked_producer)
14286 check_producer (cu);
14287
14288 return cu->producer_is_gxx_lt_4_6;
14289 }
14290
14291
14292 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14293 with incorrect is_stmt attributes. */
14294
14295 static bool
14296 producer_is_codewarrior (struct dwarf2_cu *cu)
14297 {
14298 if (!cu->checked_producer)
14299 check_producer (cu);
14300
14301 return cu->producer_is_codewarrior;
14302 }
14303
14304 /* Return the default accessibility type if it is not overridden by
14305 DW_AT_accessibility. */
14306
14307 static enum dwarf_access_attribute
14308 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14309 {
14310 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14311 {
14312 /* The default DWARF 2 accessibility for members is public, the default
14313 accessibility for inheritance is private. */
14314
14315 if (die->tag != DW_TAG_inheritance)
14316 return DW_ACCESS_public;
14317 else
14318 return DW_ACCESS_private;
14319 }
14320 else
14321 {
14322 /* DWARF 3+ defines the default accessibility a different way. The same
14323 rules apply now for DW_TAG_inheritance as for the members and it only
14324 depends on the container kind. */
14325
14326 if (die->parent->tag == DW_TAG_class_type)
14327 return DW_ACCESS_private;
14328 else
14329 return DW_ACCESS_public;
14330 }
14331 }
14332
14333 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14334 offset. If the attribute was not found return 0, otherwise return
14335 1. If it was found but could not properly be handled, set *OFFSET
14336 to 0. */
14337
14338 static int
14339 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14340 LONGEST *offset)
14341 {
14342 struct attribute *attr;
14343
14344 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14345 if (attr != NULL)
14346 {
14347 *offset = 0;
14348
14349 /* Note that we do not check for a section offset first here.
14350 This is because DW_AT_data_member_location is new in DWARF 4,
14351 so if we see it, we can assume that a constant form is really
14352 a constant and not a section offset. */
14353 if (attr->form_is_constant ())
14354 *offset = attr->constant_value (0);
14355 else if (attr->form_is_section_offset ())
14356 dwarf2_complex_location_expr_complaint ();
14357 else if (attr->form_is_block ())
14358 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14359 else
14360 dwarf2_complex_location_expr_complaint ();
14361
14362 return 1;
14363 }
14364
14365 return 0;
14366 }
14367
14368 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14369
14370 static void
14371 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14372 struct field *field)
14373 {
14374 struct attribute *attr;
14375
14376 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14377 if (attr != NULL)
14378 {
14379 if (attr->form_is_constant ())
14380 {
14381 LONGEST offset = attr->constant_value (0);
14382 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14383 }
14384 else if (attr->form_is_section_offset ())
14385 dwarf2_complex_location_expr_complaint ();
14386 else if (attr->form_is_block ())
14387 {
14388 bool handled;
14389 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14390 if (handled)
14391 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14392 else
14393 {
14394 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14395 struct objfile *objfile = per_objfile->objfile;
14396 struct dwarf2_locexpr_baton *dlbaton
14397 = XOBNEW (&objfile->objfile_obstack,
14398 struct dwarf2_locexpr_baton);
14399 dlbaton->data = DW_BLOCK (attr)->data;
14400 dlbaton->size = DW_BLOCK (attr)->size;
14401 /* When using this baton, we want to compute the address
14402 of the field, not the value. This is why
14403 is_reference is set to false here. */
14404 dlbaton->is_reference = false;
14405 dlbaton->per_objfile = per_objfile;
14406 dlbaton->per_cu = cu->per_cu;
14407
14408 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14409 }
14410 }
14411 else
14412 dwarf2_complex_location_expr_complaint ();
14413 }
14414 }
14415
14416 /* Add an aggregate field to the field list. */
14417
14418 static void
14419 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14420 struct dwarf2_cu *cu)
14421 {
14422 struct objfile *objfile = cu->per_objfile->objfile;
14423 struct gdbarch *gdbarch = objfile->arch ();
14424 struct nextfield *new_field;
14425 struct attribute *attr;
14426 struct field *fp;
14427 const char *fieldname = "";
14428
14429 if (die->tag == DW_TAG_inheritance)
14430 {
14431 fip->baseclasses.emplace_back ();
14432 new_field = &fip->baseclasses.back ();
14433 }
14434 else
14435 {
14436 fip->fields.emplace_back ();
14437 new_field = &fip->fields.back ();
14438 }
14439
14440 new_field->offset = die->sect_off;
14441
14442 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14443 if (attr != nullptr)
14444 new_field->accessibility = DW_UNSND (attr);
14445 else
14446 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14447 if (new_field->accessibility != DW_ACCESS_public)
14448 fip->non_public_fields = 1;
14449
14450 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14451 if (attr != nullptr)
14452 new_field->virtuality = DW_UNSND (attr);
14453 else
14454 new_field->virtuality = DW_VIRTUALITY_none;
14455
14456 fp = &new_field->field;
14457
14458 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14459 {
14460 /* Data member other than a C++ static data member. */
14461
14462 /* Get type of field. */
14463 fp->type = die_type (die, cu);
14464
14465 SET_FIELD_BITPOS (*fp, 0);
14466
14467 /* Get bit size of field (zero if none). */
14468 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14469 if (attr != nullptr)
14470 {
14471 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14472 }
14473 else
14474 {
14475 FIELD_BITSIZE (*fp) = 0;
14476 }
14477
14478 /* Get bit offset of field. */
14479 handle_data_member_location (die, cu, fp);
14480 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14481 if (attr != nullptr)
14482 {
14483 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14484 {
14485 /* For big endian bits, the DW_AT_bit_offset gives the
14486 additional bit offset from the MSB of the containing
14487 anonymous object to the MSB of the field. We don't
14488 have to do anything special since we don't need to
14489 know the size of the anonymous object. */
14490 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14491 }
14492 else
14493 {
14494 /* For little endian bits, compute the bit offset to the
14495 MSB of the anonymous object, subtract off the number of
14496 bits from the MSB of the field to the MSB of the
14497 object, and then subtract off the number of bits of
14498 the field itself. The result is the bit offset of
14499 the LSB of the field. */
14500 int anonymous_size;
14501 int bit_offset = DW_UNSND (attr);
14502
14503 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14504 if (attr != nullptr)
14505 {
14506 /* The size of the anonymous object containing
14507 the bit field is explicit, so use the
14508 indicated size (in bytes). */
14509 anonymous_size = DW_UNSND (attr);
14510 }
14511 else
14512 {
14513 /* The size of the anonymous object containing
14514 the bit field must be inferred from the type
14515 attribute of the data member containing the
14516 bit field. */
14517 anonymous_size = TYPE_LENGTH (fp->type);
14518 }
14519 SET_FIELD_BITPOS (*fp,
14520 (FIELD_BITPOS (*fp)
14521 + anonymous_size * bits_per_byte
14522 - bit_offset - FIELD_BITSIZE (*fp)));
14523 }
14524 }
14525 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14526 if (attr != NULL)
14527 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14528 + attr->constant_value (0)));
14529
14530 /* Get name of field. */
14531 fieldname = dwarf2_name (die, cu);
14532 if (fieldname == NULL)
14533 fieldname = "";
14534
14535 /* The name is already allocated along with this objfile, so we don't
14536 need to duplicate it for the type. */
14537 fp->name = fieldname;
14538
14539 /* Change accessibility for artificial fields (e.g. virtual table
14540 pointer or virtual base class pointer) to private. */
14541 if (dwarf2_attr (die, DW_AT_artificial, cu))
14542 {
14543 FIELD_ARTIFICIAL (*fp) = 1;
14544 new_field->accessibility = DW_ACCESS_private;
14545 fip->non_public_fields = 1;
14546 }
14547 }
14548 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14549 {
14550 /* C++ static member. */
14551
14552 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14553 is a declaration, but all versions of G++ as of this writing
14554 (so through at least 3.2.1) incorrectly generate
14555 DW_TAG_variable tags. */
14556
14557 const char *physname;
14558
14559 /* Get name of field. */
14560 fieldname = dwarf2_name (die, cu);
14561 if (fieldname == NULL)
14562 return;
14563
14564 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14565 if (attr
14566 /* Only create a symbol if this is an external value.
14567 new_symbol checks this and puts the value in the global symbol
14568 table, which we want. If it is not external, new_symbol
14569 will try to put the value in cu->list_in_scope which is wrong. */
14570 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14571 {
14572 /* A static const member, not much different than an enum as far as
14573 we're concerned, except that we can support more types. */
14574 new_symbol (die, NULL, cu);
14575 }
14576
14577 /* Get physical name. */
14578 physname = dwarf2_physname (fieldname, die, cu);
14579
14580 /* The name is already allocated along with this objfile, so we don't
14581 need to duplicate it for the type. */
14582 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14583 FIELD_TYPE (*fp) = die_type (die, cu);
14584 FIELD_NAME (*fp) = fieldname;
14585 }
14586 else if (die->tag == DW_TAG_inheritance)
14587 {
14588 /* C++ base class field. */
14589 handle_data_member_location (die, cu, fp);
14590 FIELD_BITSIZE (*fp) = 0;
14591 FIELD_TYPE (*fp) = die_type (die, cu);
14592 FIELD_NAME (*fp) = fp->type->name ();
14593 }
14594 else
14595 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14596 }
14597
14598 /* Can the type given by DIE define another type? */
14599
14600 static bool
14601 type_can_define_types (const struct die_info *die)
14602 {
14603 switch (die->tag)
14604 {
14605 case DW_TAG_typedef:
14606 case DW_TAG_class_type:
14607 case DW_TAG_structure_type:
14608 case DW_TAG_union_type:
14609 case DW_TAG_enumeration_type:
14610 return true;
14611
14612 default:
14613 return false;
14614 }
14615 }
14616
14617 /* Add a type definition defined in the scope of the FIP's class. */
14618
14619 static void
14620 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14621 struct dwarf2_cu *cu)
14622 {
14623 struct decl_field fp;
14624 memset (&fp, 0, sizeof (fp));
14625
14626 gdb_assert (type_can_define_types (die));
14627
14628 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14629 fp.name = dwarf2_name (die, cu);
14630 fp.type = read_type_die (die, cu);
14631
14632 /* Save accessibility. */
14633 enum dwarf_access_attribute accessibility;
14634 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14635 if (attr != NULL)
14636 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14637 else
14638 accessibility = dwarf2_default_access_attribute (die, cu);
14639 switch (accessibility)
14640 {
14641 case DW_ACCESS_public:
14642 /* The assumed value if neither private nor protected. */
14643 break;
14644 case DW_ACCESS_private:
14645 fp.is_private = 1;
14646 break;
14647 case DW_ACCESS_protected:
14648 fp.is_protected = 1;
14649 break;
14650 default:
14651 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14652 }
14653
14654 if (die->tag == DW_TAG_typedef)
14655 fip->typedef_field_list.push_back (fp);
14656 else
14657 fip->nested_types_list.push_back (fp);
14658 }
14659
14660 /* A convenience typedef that's used when finding the discriminant
14661 field for a variant part. */
14662 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14663 offset_map_type;
14664
14665 /* Compute the discriminant range for a given variant. OBSTACK is
14666 where the results will be stored. VARIANT is the variant to
14667 process. IS_UNSIGNED indicates whether the discriminant is signed
14668 or unsigned. */
14669
14670 static const gdb::array_view<discriminant_range>
14671 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14672 bool is_unsigned)
14673 {
14674 std::vector<discriminant_range> ranges;
14675
14676 if (variant.default_branch)
14677 return {};
14678
14679 if (variant.discr_list_data == nullptr)
14680 {
14681 discriminant_range r
14682 = {variant.discriminant_value, variant.discriminant_value};
14683 ranges.push_back (r);
14684 }
14685 else
14686 {
14687 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14688 variant.discr_list_data->size);
14689 while (!data.empty ())
14690 {
14691 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14692 {
14693 complaint (_("invalid discriminant marker: %d"), data[0]);
14694 break;
14695 }
14696 bool is_range = data[0] == DW_DSC_range;
14697 data = data.slice (1);
14698
14699 ULONGEST low, high;
14700 unsigned int bytes_read;
14701
14702 if (data.empty ())
14703 {
14704 complaint (_("DW_AT_discr_list missing low value"));
14705 break;
14706 }
14707 if (is_unsigned)
14708 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14709 else
14710 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14711 &bytes_read);
14712 data = data.slice (bytes_read);
14713
14714 if (is_range)
14715 {
14716 if (data.empty ())
14717 {
14718 complaint (_("DW_AT_discr_list missing high value"));
14719 break;
14720 }
14721 if (is_unsigned)
14722 high = read_unsigned_leb128 (nullptr, data.data (),
14723 &bytes_read);
14724 else
14725 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14726 &bytes_read);
14727 data = data.slice (bytes_read);
14728 }
14729 else
14730 high = low;
14731
14732 ranges.push_back ({ low, high });
14733 }
14734 }
14735
14736 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14737 ranges.size ());
14738 std::copy (ranges.begin (), ranges.end (), result);
14739 return gdb::array_view<discriminant_range> (result, ranges.size ());
14740 }
14741
14742 static const gdb::array_view<variant_part> create_variant_parts
14743 (struct obstack *obstack,
14744 const offset_map_type &offset_map,
14745 struct field_info *fi,
14746 const std::vector<variant_part_builder> &variant_parts);
14747
14748 /* Fill in a "struct variant" for a given variant field. RESULT is
14749 the variant to fill in. OBSTACK is where any needed allocations
14750 will be done. OFFSET_MAP holds the mapping from section offsets to
14751 fields for the type. FI describes the fields of the type we're
14752 processing. FIELD is the variant field we're converting. */
14753
14754 static void
14755 create_one_variant (variant &result, struct obstack *obstack,
14756 const offset_map_type &offset_map,
14757 struct field_info *fi, const variant_field &field)
14758 {
14759 result.discriminants = convert_variant_range (obstack, field, false);
14760 result.first_field = field.first_field + fi->baseclasses.size ();
14761 result.last_field = field.last_field + fi->baseclasses.size ();
14762 result.parts = create_variant_parts (obstack, offset_map, fi,
14763 field.variant_parts);
14764 }
14765
14766 /* Fill in a "struct variant_part" for a given variant part. RESULT
14767 is the variant part to fill in. OBSTACK is where any needed
14768 allocations will be done. OFFSET_MAP holds the mapping from
14769 section offsets to fields for the type. FI describes the fields of
14770 the type we're processing. BUILDER is the variant part to be
14771 converted. */
14772
14773 static void
14774 create_one_variant_part (variant_part &result,
14775 struct obstack *obstack,
14776 const offset_map_type &offset_map,
14777 struct field_info *fi,
14778 const variant_part_builder &builder)
14779 {
14780 auto iter = offset_map.find (builder.discriminant_offset);
14781 if (iter == offset_map.end ())
14782 {
14783 result.discriminant_index = -1;
14784 /* Doesn't matter. */
14785 result.is_unsigned = false;
14786 }
14787 else
14788 {
14789 result.discriminant_index = iter->second;
14790 result.is_unsigned
14791 = TYPE_UNSIGNED (FIELD_TYPE
14792 (fi->fields[result.discriminant_index].field));
14793 }
14794
14795 size_t n = builder.variants.size ();
14796 variant *output = new (obstack) variant[n];
14797 for (size_t i = 0; i < n; ++i)
14798 create_one_variant (output[i], obstack, offset_map, fi,
14799 builder.variants[i]);
14800
14801 result.variants = gdb::array_view<variant> (output, n);
14802 }
14803
14804 /* Create a vector of variant parts that can be attached to a type.
14805 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14806 holds the mapping from section offsets to fields for the type. FI
14807 describes the fields of the type we're processing. VARIANT_PARTS
14808 is the vector to convert. */
14809
14810 static const gdb::array_view<variant_part>
14811 create_variant_parts (struct obstack *obstack,
14812 const offset_map_type &offset_map,
14813 struct field_info *fi,
14814 const std::vector<variant_part_builder> &variant_parts)
14815 {
14816 if (variant_parts.empty ())
14817 return {};
14818
14819 size_t n = variant_parts.size ();
14820 variant_part *result = new (obstack) variant_part[n];
14821 for (size_t i = 0; i < n; ++i)
14822 create_one_variant_part (result[i], obstack, offset_map, fi,
14823 variant_parts[i]);
14824
14825 return gdb::array_view<variant_part> (result, n);
14826 }
14827
14828 /* Compute the variant part vector for FIP, attaching it to TYPE when
14829 done. */
14830
14831 static void
14832 add_variant_property (struct field_info *fip, struct type *type,
14833 struct dwarf2_cu *cu)
14834 {
14835 /* Map section offsets of fields to their field index. Note the
14836 field index here does not take the number of baseclasses into
14837 account. */
14838 offset_map_type offset_map;
14839 for (int i = 0; i < fip->fields.size (); ++i)
14840 offset_map[fip->fields[i].offset] = i;
14841
14842 struct objfile *objfile = cu->per_objfile->objfile;
14843 gdb::array_view<variant_part> parts
14844 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14845 fip->variant_parts);
14846
14847 struct dynamic_prop prop;
14848 prop.kind = PROP_VARIANT_PARTS;
14849 prop.data.variant_parts
14850 = ((gdb::array_view<variant_part> *)
14851 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14852
14853 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14854 }
14855
14856 /* Create the vector of fields, and attach it to the type. */
14857
14858 static void
14859 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14860 struct dwarf2_cu *cu)
14861 {
14862 int nfields = fip->nfields ();
14863
14864 /* Record the field count, allocate space for the array of fields,
14865 and create blank accessibility bitfields if necessary. */
14866 type->set_num_fields (nfields);
14867 type->set_fields
14868 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14869
14870 if (fip->non_public_fields && cu->language != language_ada)
14871 {
14872 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14873
14874 TYPE_FIELD_PRIVATE_BITS (type) =
14875 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14876 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14877
14878 TYPE_FIELD_PROTECTED_BITS (type) =
14879 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14880 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14881
14882 TYPE_FIELD_IGNORE_BITS (type) =
14883 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14884 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14885 }
14886
14887 /* If the type has baseclasses, allocate and clear a bit vector for
14888 TYPE_FIELD_VIRTUAL_BITS. */
14889 if (!fip->baseclasses.empty () && cu->language != language_ada)
14890 {
14891 int num_bytes = B_BYTES (fip->baseclasses.size ());
14892 unsigned char *pointer;
14893
14894 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14895 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14896 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14897 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14898 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14899 }
14900
14901 if (!fip->variant_parts.empty ())
14902 add_variant_property (fip, type, cu);
14903
14904 /* Copy the saved-up fields into the field vector. */
14905 for (int i = 0; i < nfields; ++i)
14906 {
14907 struct nextfield &field
14908 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14909 : fip->fields[i - fip->baseclasses.size ()]);
14910
14911 type->field (i) = field.field;
14912 switch (field.accessibility)
14913 {
14914 case DW_ACCESS_private:
14915 if (cu->language != language_ada)
14916 SET_TYPE_FIELD_PRIVATE (type, i);
14917 break;
14918
14919 case DW_ACCESS_protected:
14920 if (cu->language != language_ada)
14921 SET_TYPE_FIELD_PROTECTED (type, i);
14922 break;
14923
14924 case DW_ACCESS_public:
14925 break;
14926
14927 default:
14928 /* Unknown accessibility. Complain and treat it as public. */
14929 {
14930 complaint (_("unsupported accessibility %d"),
14931 field.accessibility);
14932 }
14933 break;
14934 }
14935 if (i < fip->baseclasses.size ())
14936 {
14937 switch (field.virtuality)
14938 {
14939 case DW_VIRTUALITY_virtual:
14940 case DW_VIRTUALITY_pure_virtual:
14941 if (cu->language == language_ada)
14942 error (_("unexpected virtuality in component of Ada type"));
14943 SET_TYPE_FIELD_VIRTUAL (type, i);
14944 break;
14945 }
14946 }
14947 }
14948 }
14949
14950 /* Return true if this member function is a constructor, false
14951 otherwise. */
14952
14953 static int
14954 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14955 {
14956 const char *fieldname;
14957 const char *type_name;
14958 int len;
14959
14960 if (die->parent == NULL)
14961 return 0;
14962
14963 if (die->parent->tag != DW_TAG_structure_type
14964 && die->parent->tag != DW_TAG_union_type
14965 && die->parent->tag != DW_TAG_class_type)
14966 return 0;
14967
14968 fieldname = dwarf2_name (die, cu);
14969 type_name = dwarf2_name (die->parent, cu);
14970 if (fieldname == NULL || type_name == NULL)
14971 return 0;
14972
14973 len = strlen (fieldname);
14974 return (strncmp (fieldname, type_name, len) == 0
14975 && (type_name[len] == '\0' || type_name[len] == '<'));
14976 }
14977
14978 /* Check if the given VALUE is a recognized enum
14979 dwarf_defaulted_attribute constant according to DWARF5 spec,
14980 Table 7.24. */
14981
14982 static bool
14983 is_valid_DW_AT_defaulted (ULONGEST value)
14984 {
14985 switch (value)
14986 {
14987 case DW_DEFAULTED_no:
14988 case DW_DEFAULTED_in_class:
14989 case DW_DEFAULTED_out_of_class:
14990 return true;
14991 }
14992
14993 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14994 return false;
14995 }
14996
14997 /* Add a member function to the proper fieldlist. */
14998
14999 static void
15000 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15001 struct type *type, struct dwarf2_cu *cu)
15002 {
15003 struct objfile *objfile = cu->per_objfile->objfile;
15004 struct attribute *attr;
15005 int i;
15006 struct fnfieldlist *flp = nullptr;
15007 struct fn_field *fnp;
15008 const char *fieldname;
15009 struct type *this_type;
15010 enum dwarf_access_attribute accessibility;
15011
15012 if (cu->language == language_ada)
15013 error (_("unexpected member function in Ada type"));
15014
15015 /* Get name of member function. */
15016 fieldname = dwarf2_name (die, cu);
15017 if (fieldname == NULL)
15018 return;
15019
15020 /* Look up member function name in fieldlist. */
15021 for (i = 0; i < fip->fnfieldlists.size (); i++)
15022 {
15023 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15024 {
15025 flp = &fip->fnfieldlists[i];
15026 break;
15027 }
15028 }
15029
15030 /* Create a new fnfieldlist if necessary. */
15031 if (flp == nullptr)
15032 {
15033 fip->fnfieldlists.emplace_back ();
15034 flp = &fip->fnfieldlists.back ();
15035 flp->name = fieldname;
15036 i = fip->fnfieldlists.size () - 1;
15037 }
15038
15039 /* Create a new member function field and add it to the vector of
15040 fnfieldlists. */
15041 flp->fnfields.emplace_back ();
15042 fnp = &flp->fnfields.back ();
15043
15044 /* Delay processing of the physname until later. */
15045 if (cu->language == language_cplus)
15046 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15047 die, cu);
15048 else
15049 {
15050 const char *physname = dwarf2_physname (fieldname, die, cu);
15051 fnp->physname = physname ? physname : "";
15052 }
15053
15054 fnp->type = alloc_type (objfile);
15055 this_type = read_type_die (die, cu);
15056 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15057 {
15058 int nparams = this_type->num_fields ();
15059
15060 /* TYPE is the domain of this method, and THIS_TYPE is the type
15061 of the method itself (TYPE_CODE_METHOD). */
15062 smash_to_method_type (fnp->type, type,
15063 TYPE_TARGET_TYPE (this_type),
15064 this_type->fields (),
15065 this_type->num_fields (),
15066 TYPE_VARARGS (this_type));
15067
15068 /* Handle static member functions.
15069 Dwarf2 has no clean way to discern C++ static and non-static
15070 member functions. G++ helps GDB by marking the first
15071 parameter for non-static member functions (which is the this
15072 pointer) as artificial. We obtain this information from
15073 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15074 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15075 fnp->voffset = VOFFSET_STATIC;
15076 }
15077 else
15078 complaint (_("member function type missing for '%s'"),
15079 dwarf2_full_name (fieldname, die, cu));
15080
15081 /* Get fcontext from DW_AT_containing_type if present. */
15082 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15083 fnp->fcontext = die_containing_type (die, cu);
15084
15085 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15086 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15087
15088 /* Get accessibility. */
15089 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15090 if (attr != nullptr)
15091 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15092 else
15093 accessibility = dwarf2_default_access_attribute (die, cu);
15094 switch (accessibility)
15095 {
15096 case DW_ACCESS_private:
15097 fnp->is_private = 1;
15098 break;
15099 case DW_ACCESS_protected:
15100 fnp->is_protected = 1;
15101 break;
15102 }
15103
15104 /* Check for artificial methods. */
15105 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15106 if (attr && DW_UNSND (attr) != 0)
15107 fnp->is_artificial = 1;
15108
15109 /* Check for defaulted methods. */
15110 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15111 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15112 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15113
15114 /* Check for deleted methods. */
15115 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15116 if (attr != nullptr && DW_UNSND (attr) != 0)
15117 fnp->is_deleted = 1;
15118
15119 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15120
15121 /* Get index in virtual function table if it is a virtual member
15122 function. For older versions of GCC, this is an offset in the
15123 appropriate virtual table, as specified by DW_AT_containing_type.
15124 For everyone else, it is an expression to be evaluated relative
15125 to the object address. */
15126
15127 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15128 if (attr != nullptr)
15129 {
15130 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15131 {
15132 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15133 {
15134 /* Old-style GCC. */
15135 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15136 }
15137 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15138 || (DW_BLOCK (attr)->size > 1
15139 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15140 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15141 {
15142 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15143 if ((fnp->voffset % cu->header.addr_size) != 0)
15144 dwarf2_complex_location_expr_complaint ();
15145 else
15146 fnp->voffset /= cu->header.addr_size;
15147 fnp->voffset += 2;
15148 }
15149 else
15150 dwarf2_complex_location_expr_complaint ();
15151
15152 if (!fnp->fcontext)
15153 {
15154 /* If there is no `this' field and no DW_AT_containing_type,
15155 we cannot actually find a base class context for the
15156 vtable! */
15157 if (this_type->num_fields () == 0
15158 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15159 {
15160 complaint (_("cannot determine context for virtual member "
15161 "function \"%s\" (offset %s)"),
15162 fieldname, sect_offset_str (die->sect_off));
15163 }
15164 else
15165 {
15166 fnp->fcontext
15167 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15168 }
15169 }
15170 }
15171 else if (attr->form_is_section_offset ())
15172 {
15173 dwarf2_complex_location_expr_complaint ();
15174 }
15175 else
15176 {
15177 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15178 fieldname);
15179 }
15180 }
15181 else
15182 {
15183 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15184 if (attr && DW_UNSND (attr))
15185 {
15186 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15187 complaint (_("Member function \"%s\" (offset %s) is virtual "
15188 "but the vtable offset is not specified"),
15189 fieldname, sect_offset_str (die->sect_off));
15190 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15191 TYPE_CPLUS_DYNAMIC (type) = 1;
15192 }
15193 }
15194 }
15195
15196 /* Create the vector of member function fields, and attach it to the type. */
15197
15198 static void
15199 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15200 struct dwarf2_cu *cu)
15201 {
15202 if (cu->language == language_ada)
15203 error (_("unexpected member functions in Ada type"));
15204
15205 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15206 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15207 TYPE_ALLOC (type,
15208 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15209
15210 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15211 {
15212 struct fnfieldlist &nf = fip->fnfieldlists[i];
15213 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15214
15215 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15216 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15217 fn_flp->fn_fields = (struct fn_field *)
15218 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15219
15220 for (int k = 0; k < nf.fnfields.size (); ++k)
15221 fn_flp->fn_fields[k] = nf.fnfields[k];
15222 }
15223
15224 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15225 }
15226
15227 /* Returns non-zero if NAME is the name of a vtable member in CU's
15228 language, zero otherwise. */
15229 static int
15230 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15231 {
15232 static const char vptr[] = "_vptr";
15233
15234 /* Look for the C++ form of the vtable. */
15235 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15236 return 1;
15237
15238 return 0;
15239 }
15240
15241 /* GCC outputs unnamed structures that are really pointers to member
15242 functions, with the ABI-specified layout. If TYPE describes
15243 such a structure, smash it into a member function type.
15244
15245 GCC shouldn't do this; it should just output pointer to member DIEs.
15246 This is GCC PR debug/28767. */
15247
15248 static void
15249 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15250 {
15251 struct type *pfn_type, *self_type, *new_type;
15252
15253 /* Check for a structure with no name and two children. */
15254 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15255 return;
15256
15257 /* Check for __pfn and __delta members. */
15258 if (TYPE_FIELD_NAME (type, 0) == NULL
15259 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15260 || TYPE_FIELD_NAME (type, 1) == NULL
15261 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15262 return;
15263
15264 /* Find the type of the method. */
15265 pfn_type = TYPE_FIELD_TYPE (type, 0);
15266 if (pfn_type == NULL
15267 || pfn_type->code () != TYPE_CODE_PTR
15268 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15269 return;
15270
15271 /* Look for the "this" argument. */
15272 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15273 if (pfn_type->num_fields () == 0
15274 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15275 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15276 return;
15277
15278 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15279 new_type = alloc_type (objfile);
15280 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15281 pfn_type->fields (), pfn_type->num_fields (),
15282 TYPE_VARARGS (pfn_type));
15283 smash_to_methodptr_type (type, new_type);
15284 }
15285
15286 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15287 appropriate error checking and issuing complaints if there is a
15288 problem. */
15289
15290 static ULONGEST
15291 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15292 {
15293 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15294
15295 if (attr == nullptr)
15296 return 0;
15297
15298 if (!attr->form_is_constant ())
15299 {
15300 complaint (_("DW_AT_alignment must have constant form"
15301 " - DIE at %s [in module %s]"),
15302 sect_offset_str (die->sect_off),
15303 objfile_name (cu->per_objfile->objfile));
15304 return 0;
15305 }
15306
15307 ULONGEST align;
15308 if (attr->form == DW_FORM_sdata)
15309 {
15310 LONGEST val = DW_SND (attr);
15311 if (val < 0)
15312 {
15313 complaint (_("DW_AT_alignment value must not be negative"
15314 " - DIE at %s [in module %s]"),
15315 sect_offset_str (die->sect_off),
15316 objfile_name (cu->per_objfile->objfile));
15317 return 0;
15318 }
15319 align = val;
15320 }
15321 else
15322 align = DW_UNSND (attr);
15323
15324 if (align == 0)
15325 {
15326 complaint (_("DW_AT_alignment value must not be zero"
15327 " - DIE at %s [in module %s]"),
15328 sect_offset_str (die->sect_off),
15329 objfile_name (cu->per_objfile->objfile));
15330 return 0;
15331 }
15332 if ((align & (align - 1)) != 0)
15333 {
15334 complaint (_("DW_AT_alignment value must be a power of 2"
15335 " - DIE at %s [in module %s]"),
15336 sect_offset_str (die->sect_off),
15337 objfile_name (cu->per_objfile->objfile));
15338 return 0;
15339 }
15340
15341 return align;
15342 }
15343
15344 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15345 the alignment for TYPE. */
15346
15347 static void
15348 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15349 struct type *type)
15350 {
15351 if (!set_type_align (type, get_alignment (cu, die)))
15352 complaint (_("DW_AT_alignment value too large"
15353 " - DIE at %s [in module %s]"),
15354 sect_offset_str (die->sect_off),
15355 objfile_name (cu->per_objfile->objfile));
15356 }
15357
15358 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15359 constant for a type, according to DWARF5 spec, Table 5.5. */
15360
15361 static bool
15362 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15363 {
15364 switch (value)
15365 {
15366 case DW_CC_normal:
15367 case DW_CC_pass_by_reference:
15368 case DW_CC_pass_by_value:
15369 return true;
15370
15371 default:
15372 complaint (_("unrecognized DW_AT_calling_convention value "
15373 "(%s) for a type"), pulongest (value));
15374 return false;
15375 }
15376 }
15377
15378 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15379 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15380 also according to GNU-specific values (see include/dwarf2.h). */
15381
15382 static bool
15383 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15384 {
15385 switch (value)
15386 {
15387 case DW_CC_normal:
15388 case DW_CC_program:
15389 case DW_CC_nocall:
15390 return true;
15391
15392 case DW_CC_GNU_renesas_sh:
15393 case DW_CC_GNU_borland_fastcall_i386:
15394 case DW_CC_GDB_IBM_OpenCL:
15395 return true;
15396
15397 default:
15398 complaint (_("unrecognized DW_AT_calling_convention value "
15399 "(%s) for a subroutine"), pulongest (value));
15400 return false;
15401 }
15402 }
15403
15404 /* Called when we find the DIE that starts a structure or union scope
15405 (definition) to create a type for the structure or union. Fill in
15406 the type's name and general properties; the members will not be
15407 processed until process_structure_scope. A symbol table entry for
15408 the type will also not be done until process_structure_scope (assuming
15409 the type has a name).
15410
15411 NOTE: we need to call these functions regardless of whether or not the
15412 DIE has a DW_AT_name attribute, since it might be an anonymous
15413 structure or union. This gets the type entered into our set of
15414 user defined types. */
15415
15416 static struct type *
15417 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15418 {
15419 struct objfile *objfile = cu->per_objfile->objfile;
15420 struct type *type;
15421 struct attribute *attr;
15422 const char *name;
15423
15424 /* If the definition of this type lives in .debug_types, read that type.
15425 Don't follow DW_AT_specification though, that will take us back up
15426 the chain and we want to go down. */
15427 attr = die->attr (DW_AT_signature);
15428 if (attr != nullptr)
15429 {
15430 type = get_DW_AT_signature_type (die, attr, cu);
15431
15432 /* The type's CU may not be the same as CU.
15433 Ensure TYPE is recorded with CU in die_type_hash. */
15434 return set_die_type (die, type, cu);
15435 }
15436
15437 type = alloc_type (objfile);
15438 INIT_CPLUS_SPECIFIC (type);
15439
15440 name = dwarf2_name (die, cu);
15441 if (name != NULL)
15442 {
15443 if (cu->language == language_cplus
15444 || cu->language == language_d
15445 || cu->language == language_rust)
15446 {
15447 const char *full_name = dwarf2_full_name (name, die, cu);
15448
15449 /* dwarf2_full_name might have already finished building the DIE's
15450 type. If so, there is no need to continue. */
15451 if (get_die_type (die, cu) != NULL)
15452 return get_die_type (die, cu);
15453
15454 type->set_name (full_name);
15455 }
15456 else
15457 {
15458 /* The name is already allocated along with this objfile, so
15459 we don't need to duplicate it for the type. */
15460 type->set_name (name);
15461 }
15462 }
15463
15464 if (die->tag == DW_TAG_structure_type)
15465 {
15466 type->set_code (TYPE_CODE_STRUCT);
15467 }
15468 else if (die->tag == DW_TAG_union_type)
15469 {
15470 type->set_code (TYPE_CODE_UNION);
15471 }
15472 else
15473 {
15474 type->set_code (TYPE_CODE_STRUCT);
15475 }
15476
15477 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15478 TYPE_DECLARED_CLASS (type) = 1;
15479
15480 /* Store the calling convention in the type if it's available in
15481 the die. Otherwise the calling convention remains set to
15482 the default value DW_CC_normal. */
15483 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15484 if (attr != nullptr
15485 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15486 {
15487 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15488 TYPE_CPLUS_CALLING_CONVENTION (type)
15489 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15490 }
15491
15492 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15493 if (attr != nullptr)
15494 {
15495 if (attr->form_is_constant ())
15496 TYPE_LENGTH (type) = DW_UNSND (attr);
15497 else
15498 {
15499 struct dynamic_prop prop;
15500 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15501 cu->per_cu->addr_type ()))
15502 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15503 TYPE_LENGTH (type) = 0;
15504 }
15505 }
15506 else
15507 {
15508 TYPE_LENGTH (type) = 0;
15509 }
15510
15511 maybe_set_alignment (cu, die, type);
15512
15513 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15514 {
15515 /* ICC<14 does not output the required DW_AT_declaration on
15516 incomplete types, but gives them a size of zero. */
15517 TYPE_STUB (type) = 1;
15518 }
15519 else
15520 TYPE_STUB_SUPPORTED (type) = 1;
15521
15522 if (die_is_declaration (die, cu))
15523 TYPE_STUB (type) = 1;
15524 else if (attr == NULL && die->child == NULL
15525 && producer_is_realview (cu->producer))
15526 /* RealView does not output the required DW_AT_declaration
15527 on incomplete types. */
15528 TYPE_STUB (type) = 1;
15529
15530 /* We need to add the type field to the die immediately so we don't
15531 infinitely recurse when dealing with pointers to the structure
15532 type within the structure itself. */
15533 set_die_type (die, type, cu);
15534
15535 /* set_die_type should be already done. */
15536 set_descriptive_type (type, die, cu);
15537
15538 return type;
15539 }
15540
15541 static void handle_struct_member_die
15542 (struct die_info *child_die,
15543 struct type *type,
15544 struct field_info *fi,
15545 std::vector<struct symbol *> *template_args,
15546 struct dwarf2_cu *cu);
15547
15548 /* A helper for handle_struct_member_die that handles
15549 DW_TAG_variant_part. */
15550
15551 static void
15552 handle_variant_part (struct die_info *die, struct type *type,
15553 struct field_info *fi,
15554 std::vector<struct symbol *> *template_args,
15555 struct dwarf2_cu *cu)
15556 {
15557 variant_part_builder *new_part;
15558 if (fi->current_variant_part == nullptr)
15559 {
15560 fi->variant_parts.emplace_back ();
15561 new_part = &fi->variant_parts.back ();
15562 }
15563 else if (!fi->current_variant_part->processing_variant)
15564 {
15565 complaint (_("nested DW_TAG_variant_part seen "
15566 "- DIE at %s [in module %s]"),
15567 sect_offset_str (die->sect_off),
15568 objfile_name (cu->per_objfile->objfile));
15569 return;
15570 }
15571 else
15572 {
15573 variant_field &current = fi->current_variant_part->variants.back ();
15574 current.variant_parts.emplace_back ();
15575 new_part = &current.variant_parts.back ();
15576 }
15577
15578 /* When we recurse, we want callees to add to this new variant
15579 part. */
15580 scoped_restore save_current_variant_part
15581 = make_scoped_restore (&fi->current_variant_part, new_part);
15582
15583 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15584 if (discr == NULL)
15585 {
15586 /* It's a univariant form, an extension we support. */
15587 }
15588 else if (discr->form_is_ref ())
15589 {
15590 struct dwarf2_cu *target_cu = cu;
15591 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15592
15593 new_part->discriminant_offset = target_die->sect_off;
15594 }
15595 else
15596 {
15597 complaint (_("DW_AT_discr does not have DIE reference form"
15598 " - DIE at %s [in module %s]"),
15599 sect_offset_str (die->sect_off),
15600 objfile_name (cu->per_objfile->objfile));
15601 }
15602
15603 for (die_info *child_die = die->child;
15604 child_die != NULL;
15605 child_die = child_die->sibling)
15606 handle_struct_member_die (child_die, type, fi, template_args, cu);
15607 }
15608
15609 /* A helper for handle_struct_member_die that handles
15610 DW_TAG_variant. */
15611
15612 static void
15613 handle_variant (struct die_info *die, struct type *type,
15614 struct field_info *fi,
15615 std::vector<struct symbol *> *template_args,
15616 struct dwarf2_cu *cu)
15617 {
15618 if (fi->current_variant_part == nullptr)
15619 {
15620 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15621 "- DIE at %s [in module %s]"),
15622 sect_offset_str (die->sect_off),
15623 objfile_name (cu->per_objfile->objfile));
15624 return;
15625 }
15626 if (fi->current_variant_part->processing_variant)
15627 {
15628 complaint (_("nested DW_TAG_variant seen "
15629 "- DIE at %s [in module %s]"),
15630 sect_offset_str (die->sect_off),
15631 objfile_name (cu->per_objfile->objfile));
15632 return;
15633 }
15634
15635 scoped_restore save_processing_variant
15636 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15637 true);
15638
15639 fi->current_variant_part->variants.emplace_back ();
15640 variant_field &variant = fi->current_variant_part->variants.back ();
15641 variant.first_field = fi->fields.size ();
15642
15643 /* In a variant we want to get the discriminant and also add a
15644 field for our sole member child. */
15645 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15646 if (discr == nullptr)
15647 {
15648 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15649 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15650 variant.default_branch = true;
15651 else
15652 variant.discr_list_data = DW_BLOCK (discr);
15653 }
15654 else
15655 variant.discriminant_value = DW_UNSND (discr);
15656
15657 for (die_info *variant_child = die->child;
15658 variant_child != NULL;
15659 variant_child = variant_child->sibling)
15660 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15661
15662 variant.last_field = fi->fields.size ();
15663 }
15664
15665 /* A helper for process_structure_scope that handles a single member
15666 DIE. */
15667
15668 static void
15669 handle_struct_member_die (struct die_info *child_die, struct type *type,
15670 struct field_info *fi,
15671 std::vector<struct symbol *> *template_args,
15672 struct dwarf2_cu *cu)
15673 {
15674 if (child_die->tag == DW_TAG_member
15675 || child_die->tag == DW_TAG_variable)
15676 {
15677 /* NOTE: carlton/2002-11-05: A C++ static data member
15678 should be a DW_TAG_member that is a declaration, but
15679 all versions of G++ as of this writing (so through at
15680 least 3.2.1) incorrectly generate DW_TAG_variable
15681 tags for them instead. */
15682 dwarf2_add_field (fi, child_die, cu);
15683 }
15684 else if (child_die->tag == DW_TAG_subprogram)
15685 {
15686 /* Rust doesn't have member functions in the C++ sense.
15687 However, it does emit ordinary functions as children
15688 of a struct DIE. */
15689 if (cu->language == language_rust)
15690 read_func_scope (child_die, cu);
15691 else
15692 {
15693 /* C++ member function. */
15694 dwarf2_add_member_fn (fi, child_die, type, cu);
15695 }
15696 }
15697 else if (child_die->tag == DW_TAG_inheritance)
15698 {
15699 /* C++ base class field. */
15700 dwarf2_add_field (fi, child_die, cu);
15701 }
15702 else if (type_can_define_types (child_die))
15703 dwarf2_add_type_defn (fi, child_die, cu);
15704 else if (child_die->tag == DW_TAG_template_type_param
15705 || child_die->tag == DW_TAG_template_value_param)
15706 {
15707 struct symbol *arg = new_symbol (child_die, NULL, cu);
15708
15709 if (arg != NULL)
15710 template_args->push_back (arg);
15711 }
15712 else if (child_die->tag == DW_TAG_variant_part)
15713 handle_variant_part (child_die, type, fi, template_args, cu);
15714 else if (child_die->tag == DW_TAG_variant)
15715 handle_variant (child_die, type, fi, template_args, cu);
15716 }
15717
15718 /* Finish creating a structure or union type, including filling in
15719 its members and creating a symbol for it. */
15720
15721 static void
15722 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15723 {
15724 struct objfile *objfile = cu->per_objfile->objfile;
15725 struct die_info *child_die;
15726 struct type *type;
15727
15728 type = get_die_type (die, cu);
15729 if (type == NULL)
15730 type = read_structure_type (die, cu);
15731
15732 bool has_template_parameters = false;
15733 if (die->child != NULL && ! die_is_declaration (die, cu))
15734 {
15735 struct field_info fi;
15736 std::vector<struct symbol *> template_args;
15737
15738 child_die = die->child;
15739
15740 while (child_die && child_die->tag)
15741 {
15742 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15743 child_die = child_die->sibling;
15744 }
15745
15746 /* Attach template arguments to type. */
15747 if (!template_args.empty ())
15748 {
15749 has_template_parameters = true;
15750 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15751 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15752 TYPE_TEMPLATE_ARGUMENTS (type)
15753 = XOBNEWVEC (&objfile->objfile_obstack,
15754 struct symbol *,
15755 TYPE_N_TEMPLATE_ARGUMENTS (type));
15756 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15757 template_args.data (),
15758 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15759 * sizeof (struct symbol *)));
15760 }
15761
15762 /* Attach fields and member functions to the type. */
15763 if (fi.nfields () > 0)
15764 dwarf2_attach_fields_to_type (&fi, type, cu);
15765 if (!fi.fnfieldlists.empty ())
15766 {
15767 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15768
15769 /* Get the type which refers to the base class (possibly this
15770 class itself) which contains the vtable pointer for the current
15771 class from the DW_AT_containing_type attribute. This use of
15772 DW_AT_containing_type is a GNU extension. */
15773
15774 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15775 {
15776 struct type *t = die_containing_type (die, cu);
15777
15778 set_type_vptr_basetype (type, t);
15779 if (type == t)
15780 {
15781 int i;
15782
15783 /* Our own class provides vtbl ptr. */
15784 for (i = t->num_fields () - 1;
15785 i >= TYPE_N_BASECLASSES (t);
15786 --i)
15787 {
15788 const char *fieldname = TYPE_FIELD_NAME (t, i);
15789
15790 if (is_vtable_name (fieldname, cu))
15791 {
15792 set_type_vptr_fieldno (type, i);
15793 break;
15794 }
15795 }
15796
15797 /* Complain if virtual function table field not found. */
15798 if (i < TYPE_N_BASECLASSES (t))
15799 complaint (_("virtual function table pointer "
15800 "not found when defining class '%s'"),
15801 type->name () ? type->name () : "");
15802 }
15803 else
15804 {
15805 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15806 }
15807 }
15808 else if (cu->producer
15809 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15810 {
15811 /* The IBM XLC compiler does not provide direct indication
15812 of the containing type, but the vtable pointer is
15813 always named __vfp. */
15814
15815 int i;
15816
15817 for (i = type->num_fields () - 1;
15818 i >= TYPE_N_BASECLASSES (type);
15819 --i)
15820 {
15821 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15822 {
15823 set_type_vptr_fieldno (type, i);
15824 set_type_vptr_basetype (type, type);
15825 break;
15826 }
15827 }
15828 }
15829 }
15830
15831 /* Copy fi.typedef_field_list linked list elements content into the
15832 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15833 if (!fi.typedef_field_list.empty ())
15834 {
15835 int count = fi.typedef_field_list.size ();
15836
15837 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15838 TYPE_TYPEDEF_FIELD_ARRAY (type)
15839 = ((struct decl_field *)
15840 TYPE_ALLOC (type,
15841 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15842 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15843
15844 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15845 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15846 }
15847
15848 /* Copy fi.nested_types_list linked list elements content into the
15849 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15850 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15851 {
15852 int count = fi.nested_types_list.size ();
15853
15854 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15855 TYPE_NESTED_TYPES_ARRAY (type)
15856 = ((struct decl_field *)
15857 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15858 TYPE_NESTED_TYPES_COUNT (type) = count;
15859
15860 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15861 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15862 }
15863 }
15864
15865 quirk_gcc_member_function_pointer (type, objfile);
15866 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15867 cu->rust_unions.push_back (type);
15868
15869 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15870 snapshots) has been known to create a die giving a declaration
15871 for a class that has, as a child, a die giving a definition for a
15872 nested class. So we have to process our children even if the
15873 current die is a declaration. Normally, of course, a declaration
15874 won't have any children at all. */
15875
15876 child_die = die->child;
15877
15878 while (child_die != NULL && child_die->tag)
15879 {
15880 if (child_die->tag == DW_TAG_member
15881 || child_die->tag == DW_TAG_variable
15882 || child_die->tag == DW_TAG_inheritance
15883 || child_die->tag == DW_TAG_template_value_param
15884 || child_die->tag == DW_TAG_template_type_param)
15885 {
15886 /* Do nothing. */
15887 }
15888 else
15889 process_die (child_die, cu);
15890
15891 child_die = child_die->sibling;
15892 }
15893
15894 /* Do not consider external references. According to the DWARF standard,
15895 these DIEs are identified by the fact that they have no byte_size
15896 attribute, and a declaration attribute. */
15897 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15898 || !die_is_declaration (die, cu)
15899 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15900 {
15901 struct symbol *sym = new_symbol (die, type, cu);
15902
15903 if (has_template_parameters)
15904 {
15905 struct symtab *symtab;
15906 if (sym != nullptr)
15907 symtab = symbol_symtab (sym);
15908 else if (cu->line_header != nullptr)
15909 {
15910 /* Any related symtab will do. */
15911 symtab
15912 = cu->line_header->file_names ()[0].symtab;
15913 }
15914 else
15915 {
15916 symtab = nullptr;
15917 complaint (_("could not find suitable "
15918 "symtab for template parameter"
15919 " - DIE at %s [in module %s]"),
15920 sect_offset_str (die->sect_off),
15921 objfile_name (objfile));
15922 }
15923
15924 if (symtab != nullptr)
15925 {
15926 /* Make sure that the symtab is set on the new symbols.
15927 Even though they don't appear in this symtab directly,
15928 other parts of gdb assume that symbols do, and this is
15929 reasonably true. */
15930 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15931 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15932 }
15933 }
15934 }
15935 }
15936
15937 /* Assuming DIE is an enumeration type, and TYPE is its associated
15938 type, update TYPE using some information only available in DIE's
15939 children. In particular, the fields are computed. */
15940
15941 static void
15942 update_enumeration_type_from_children (struct die_info *die,
15943 struct type *type,
15944 struct dwarf2_cu *cu)
15945 {
15946 struct die_info *child_die;
15947 int unsigned_enum = 1;
15948 int flag_enum = 1;
15949
15950 auto_obstack obstack;
15951 std::vector<struct field> fields;
15952
15953 for (child_die = die->child;
15954 child_die != NULL && child_die->tag;
15955 child_die = child_die->sibling)
15956 {
15957 struct attribute *attr;
15958 LONGEST value;
15959 const gdb_byte *bytes;
15960 struct dwarf2_locexpr_baton *baton;
15961 const char *name;
15962
15963 if (child_die->tag != DW_TAG_enumerator)
15964 continue;
15965
15966 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15967 if (attr == NULL)
15968 continue;
15969
15970 name = dwarf2_name (child_die, cu);
15971 if (name == NULL)
15972 name = "<anonymous enumerator>";
15973
15974 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15975 &value, &bytes, &baton);
15976 if (value < 0)
15977 {
15978 unsigned_enum = 0;
15979 flag_enum = 0;
15980 }
15981 else
15982 {
15983 if (count_one_bits_ll (value) >= 2)
15984 flag_enum = 0;
15985 }
15986
15987 fields.emplace_back ();
15988 struct field &field = fields.back ();
15989 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15990 SET_FIELD_ENUMVAL (field, value);
15991 }
15992
15993 if (!fields.empty ())
15994 {
15995 type->set_num_fields (fields.size ());
15996 type->set_fields
15997 ((struct field *)
15998 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
15999 memcpy (type->fields (), fields.data (),
16000 sizeof (struct field) * fields.size ());
16001 }
16002
16003 if (unsigned_enum)
16004 TYPE_UNSIGNED (type) = 1;
16005 if (flag_enum)
16006 TYPE_FLAG_ENUM (type) = 1;
16007 }
16008
16009 /* Given a DW_AT_enumeration_type die, set its type. We do not
16010 complete the type's fields yet, or create any symbols. */
16011
16012 static struct type *
16013 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16014 {
16015 struct objfile *objfile = cu->per_objfile->objfile;
16016 struct type *type;
16017 struct attribute *attr;
16018 const char *name;
16019
16020 /* If the definition of this type lives in .debug_types, read that type.
16021 Don't follow DW_AT_specification though, that will take us back up
16022 the chain and we want to go down. */
16023 attr = die->attr (DW_AT_signature);
16024 if (attr != nullptr)
16025 {
16026 type = get_DW_AT_signature_type (die, attr, cu);
16027
16028 /* The type's CU may not be the same as CU.
16029 Ensure TYPE is recorded with CU in die_type_hash. */
16030 return set_die_type (die, type, cu);
16031 }
16032
16033 type = alloc_type (objfile);
16034
16035 type->set_code (TYPE_CODE_ENUM);
16036 name = dwarf2_full_name (NULL, die, cu);
16037 if (name != NULL)
16038 type->set_name (name);
16039
16040 attr = dwarf2_attr (die, DW_AT_type, cu);
16041 if (attr != NULL)
16042 {
16043 struct type *underlying_type = die_type (die, cu);
16044
16045 TYPE_TARGET_TYPE (type) = underlying_type;
16046 }
16047
16048 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16049 if (attr != nullptr)
16050 {
16051 TYPE_LENGTH (type) = DW_UNSND (attr);
16052 }
16053 else
16054 {
16055 TYPE_LENGTH (type) = 0;
16056 }
16057
16058 maybe_set_alignment (cu, die, type);
16059
16060 /* The enumeration DIE can be incomplete. In Ada, any type can be
16061 declared as private in the package spec, and then defined only
16062 inside the package body. Such types are known as Taft Amendment
16063 Types. When another package uses such a type, an incomplete DIE
16064 may be generated by the compiler. */
16065 if (die_is_declaration (die, cu))
16066 TYPE_STUB (type) = 1;
16067
16068 /* If this type has an underlying type that is not a stub, then we
16069 may use its attributes. We always use the "unsigned" attribute
16070 in this situation, because ordinarily we guess whether the type
16071 is unsigned -- but the guess can be wrong and the underlying type
16072 can tell us the reality. However, we defer to a local size
16073 attribute if one exists, because this lets the compiler override
16074 the underlying type if needed. */
16075 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16076 {
16077 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16078 underlying_type = check_typedef (underlying_type);
16079 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16080 if (TYPE_LENGTH (type) == 0)
16081 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16082 if (TYPE_RAW_ALIGN (type) == 0
16083 && TYPE_RAW_ALIGN (underlying_type) != 0)
16084 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16085 }
16086
16087 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16088
16089 set_die_type (die, type, cu);
16090
16091 /* Finish the creation of this type by using the enum's children.
16092 Note that, as usual, this must come after set_die_type to avoid
16093 infinite recursion when trying to compute the names of the
16094 enumerators. */
16095 update_enumeration_type_from_children (die, type, cu);
16096
16097 return type;
16098 }
16099
16100 /* Given a pointer to a die which begins an enumeration, process all
16101 the dies that define the members of the enumeration, and create the
16102 symbol for the enumeration type.
16103
16104 NOTE: We reverse the order of the element list. */
16105
16106 static void
16107 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16108 {
16109 struct type *this_type;
16110
16111 this_type = get_die_type (die, cu);
16112 if (this_type == NULL)
16113 this_type = read_enumeration_type (die, cu);
16114
16115 if (die->child != NULL)
16116 {
16117 struct die_info *child_die;
16118 const char *name;
16119
16120 child_die = die->child;
16121 while (child_die && child_die->tag)
16122 {
16123 if (child_die->tag != DW_TAG_enumerator)
16124 {
16125 process_die (child_die, cu);
16126 }
16127 else
16128 {
16129 name = dwarf2_name (child_die, cu);
16130 if (name)
16131 new_symbol (child_die, this_type, cu);
16132 }
16133
16134 child_die = child_die->sibling;
16135 }
16136 }
16137
16138 /* If we are reading an enum from a .debug_types unit, and the enum
16139 is a declaration, and the enum is not the signatured type in the
16140 unit, then we do not want to add a symbol for it. Adding a
16141 symbol would in some cases obscure the true definition of the
16142 enum, giving users an incomplete type when the definition is
16143 actually available. Note that we do not want to do this for all
16144 enums which are just declarations, because C++0x allows forward
16145 enum declarations. */
16146 if (cu->per_cu->is_debug_types
16147 && die_is_declaration (die, cu))
16148 {
16149 struct signatured_type *sig_type;
16150
16151 sig_type = (struct signatured_type *) cu->per_cu;
16152 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16153 if (sig_type->type_offset_in_section != die->sect_off)
16154 return;
16155 }
16156
16157 new_symbol (die, this_type, cu);
16158 }
16159
16160 /* Extract all information from a DW_TAG_array_type DIE and put it in
16161 the DIE's type field. For now, this only handles one dimensional
16162 arrays. */
16163
16164 static struct type *
16165 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16166 {
16167 struct objfile *objfile = cu->per_objfile->objfile;
16168 struct die_info *child_die;
16169 struct type *type;
16170 struct type *element_type, *range_type, *index_type;
16171 struct attribute *attr;
16172 const char *name;
16173 struct dynamic_prop *byte_stride_prop = NULL;
16174 unsigned int bit_stride = 0;
16175
16176 element_type = die_type (die, cu);
16177
16178 /* The die_type call above may have already set the type for this DIE. */
16179 type = get_die_type (die, cu);
16180 if (type)
16181 return type;
16182
16183 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16184 if (attr != NULL)
16185 {
16186 int stride_ok;
16187 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
16188
16189 byte_stride_prop
16190 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16191 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16192 prop_type);
16193 if (!stride_ok)
16194 {
16195 complaint (_("unable to read array DW_AT_byte_stride "
16196 " - DIE at %s [in module %s]"),
16197 sect_offset_str (die->sect_off),
16198 objfile_name (cu->per_objfile->objfile));
16199 /* Ignore this attribute. We will likely not be able to print
16200 arrays of this type correctly, but there is little we can do
16201 to help if we cannot read the attribute's value. */
16202 byte_stride_prop = NULL;
16203 }
16204 }
16205
16206 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16207 if (attr != NULL)
16208 bit_stride = DW_UNSND (attr);
16209
16210 /* Irix 6.2 native cc creates array types without children for
16211 arrays with unspecified length. */
16212 if (die->child == NULL)
16213 {
16214 index_type = objfile_type (objfile)->builtin_int;
16215 range_type = create_static_range_type (NULL, index_type, 0, -1);
16216 type = create_array_type_with_stride (NULL, element_type, range_type,
16217 byte_stride_prop, bit_stride);
16218 return set_die_type (die, type, cu);
16219 }
16220
16221 std::vector<struct type *> range_types;
16222 child_die = die->child;
16223 while (child_die && child_die->tag)
16224 {
16225 if (child_die->tag == DW_TAG_subrange_type)
16226 {
16227 struct type *child_type = read_type_die (child_die, cu);
16228
16229 if (child_type != NULL)
16230 {
16231 /* The range type was succesfully read. Save it for the
16232 array type creation. */
16233 range_types.push_back (child_type);
16234 }
16235 }
16236 child_die = child_die->sibling;
16237 }
16238
16239 /* Dwarf2 dimensions are output from left to right, create the
16240 necessary array types in backwards order. */
16241
16242 type = element_type;
16243
16244 if (read_array_order (die, cu) == DW_ORD_col_major)
16245 {
16246 int i = 0;
16247
16248 while (i < range_types.size ())
16249 type = create_array_type_with_stride (NULL, type, range_types[i++],
16250 byte_stride_prop, bit_stride);
16251 }
16252 else
16253 {
16254 size_t ndim = range_types.size ();
16255 while (ndim-- > 0)
16256 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16257 byte_stride_prop, bit_stride);
16258 }
16259
16260 /* Understand Dwarf2 support for vector types (like they occur on
16261 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16262 array type. This is not part of the Dwarf2/3 standard yet, but a
16263 custom vendor extension. The main difference between a regular
16264 array and the vector variant is that vectors are passed by value
16265 to functions. */
16266 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16267 if (attr != nullptr)
16268 make_vector_type (type);
16269
16270 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16271 implementation may choose to implement triple vectors using this
16272 attribute. */
16273 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16274 if (attr != nullptr)
16275 {
16276 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16277 TYPE_LENGTH (type) = DW_UNSND (attr);
16278 else
16279 complaint (_("DW_AT_byte_size for array type smaller "
16280 "than the total size of elements"));
16281 }
16282
16283 name = dwarf2_name (die, cu);
16284 if (name)
16285 type->set_name (name);
16286
16287 maybe_set_alignment (cu, die, type);
16288
16289 /* Install the type in the die. */
16290 set_die_type (die, type, cu);
16291
16292 /* set_die_type should be already done. */
16293 set_descriptive_type (type, die, cu);
16294
16295 return type;
16296 }
16297
16298 static enum dwarf_array_dim_ordering
16299 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16300 {
16301 struct attribute *attr;
16302
16303 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16304
16305 if (attr != nullptr)
16306 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16307
16308 /* GNU F77 is a special case, as at 08/2004 array type info is the
16309 opposite order to the dwarf2 specification, but data is still
16310 laid out as per normal fortran.
16311
16312 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16313 version checking. */
16314
16315 if (cu->language == language_fortran
16316 && cu->producer && strstr (cu->producer, "GNU F77"))
16317 {
16318 return DW_ORD_row_major;
16319 }
16320
16321 switch (cu->language_defn->la_array_ordering)
16322 {
16323 case array_column_major:
16324 return DW_ORD_col_major;
16325 case array_row_major:
16326 default:
16327 return DW_ORD_row_major;
16328 };
16329 }
16330
16331 /* Extract all information from a DW_TAG_set_type DIE and put it in
16332 the DIE's type field. */
16333
16334 static struct type *
16335 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16336 {
16337 struct type *domain_type, *set_type;
16338 struct attribute *attr;
16339
16340 domain_type = die_type (die, cu);
16341
16342 /* The die_type call above may have already set the type for this DIE. */
16343 set_type = get_die_type (die, cu);
16344 if (set_type)
16345 return set_type;
16346
16347 set_type = create_set_type (NULL, domain_type);
16348
16349 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16350 if (attr != nullptr)
16351 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16352
16353 maybe_set_alignment (cu, die, set_type);
16354
16355 return set_die_type (die, set_type, cu);
16356 }
16357
16358 /* A helper for read_common_block that creates a locexpr baton.
16359 SYM is the symbol which we are marking as computed.
16360 COMMON_DIE is the DIE for the common block.
16361 COMMON_LOC is the location expression attribute for the common
16362 block itself.
16363 MEMBER_LOC is the location expression attribute for the particular
16364 member of the common block that we are processing.
16365 CU is the CU from which the above come. */
16366
16367 static void
16368 mark_common_block_symbol_computed (struct symbol *sym,
16369 struct die_info *common_die,
16370 struct attribute *common_loc,
16371 struct attribute *member_loc,
16372 struct dwarf2_cu *cu)
16373 {
16374 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16375 struct objfile *objfile = per_objfile->objfile;
16376 struct dwarf2_locexpr_baton *baton;
16377 gdb_byte *ptr;
16378 unsigned int cu_off;
16379 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16380 LONGEST offset = 0;
16381
16382 gdb_assert (common_loc && member_loc);
16383 gdb_assert (common_loc->form_is_block ());
16384 gdb_assert (member_loc->form_is_block ()
16385 || member_loc->form_is_constant ());
16386
16387 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16388 baton->per_objfile = per_objfile;
16389 baton->per_cu = cu->per_cu;
16390 gdb_assert (baton->per_cu);
16391
16392 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16393
16394 if (member_loc->form_is_constant ())
16395 {
16396 offset = member_loc->constant_value (0);
16397 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16398 }
16399 else
16400 baton->size += DW_BLOCK (member_loc)->size;
16401
16402 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16403 baton->data = ptr;
16404
16405 *ptr++ = DW_OP_call4;
16406 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16407 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16408 ptr += 4;
16409
16410 if (member_loc->form_is_constant ())
16411 {
16412 *ptr++ = DW_OP_addr;
16413 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16414 ptr += cu->header.addr_size;
16415 }
16416 else
16417 {
16418 /* We have to copy the data here, because DW_OP_call4 will only
16419 use a DW_AT_location attribute. */
16420 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16421 ptr += DW_BLOCK (member_loc)->size;
16422 }
16423
16424 *ptr++ = DW_OP_plus;
16425 gdb_assert (ptr - baton->data == baton->size);
16426
16427 SYMBOL_LOCATION_BATON (sym) = baton;
16428 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16429 }
16430
16431 /* Create appropriate locally-scoped variables for all the
16432 DW_TAG_common_block entries. Also create a struct common_block
16433 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16434 is used to separate the common blocks name namespace from regular
16435 variable names. */
16436
16437 static void
16438 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16439 {
16440 struct attribute *attr;
16441
16442 attr = dwarf2_attr (die, DW_AT_location, cu);
16443 if (attr != nullptr)
16444 {
16445 /* Support the .debug_loc offsets. */
16446 if (attr->form_is_block ())
16447 {
16448 /* Ok. */
16449 }
16450 else if (attr->form_is_section_offset ())
16451 {
16452 dwarf2_complex_location_expr_complaint ();
16453 attr = NULL;
16454 }
16455 else
16456 {
16457 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16458 "common block member");
16459 attr = NULL;
16460 }
16461 }
16462
16463 if (die->child != NULL)
16464 {
16465 struct objfile *objfile = cu->per_objfile->objfile;
16466 struct die_info *child_die;
16467 size_t n_entries = 0, size;
16468 struct common_block *common_block;
16469 struct symbol *sym;
16470
16471 for (child_die = die->child;
16472 child_die && child_die->tag;
16473 child_die = child_die->sibling)
16474 ++n_entries;
16475
16476 size = (sizeof (struct common_block)
16477 + (n_entries - 1) * sizeof (struct symbol *));
16478 common_block
16479 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16480 size);
16481 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16482 common_block->n_entries = 0;
16483
16484 for (child_die = die->child;
16485 child_die && child_die->tag;
16486 child_die = child_die->sibling)
16487 {
16488 /* Create the symbol in the DW_TAG_common_block block in the current
16489 symbol scope. */
16490 sym = new_symbol (child_die, NULL, cu);
16491 if (sym != NULL)
16492 {
16493 struct attribute *member_loc;
16494
16495 common_block->contents[common_block->n_entries++] = sym;
16496
16497 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16498 cu);
16499 if (member_loc)
16500 {
16501 /* GDB has handled this for a long time, but it is
16502 not specified by DWARF. It seems to have been
16503 emitted by gfortran at least as recently as:
16504 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16505 complaint (_("Variable in common block has "
16506 "DW_AT_data_member_location "
16507 "- DIE at %s [in module %s]"),
16508 sect_offset_str (child_die->sect_off),
16509 objfile_name (objfile));
16510
16511 if (member_loc->form_is_section_offset ())
16512 dwarf2_complex_location_expr_complaint ();
16513 else if (member_loc->form_is_constant ()
16514 || member_loc->form_is_block ())
16515 {
16516 if (attr != nullptr)
16517 mark_common_block_symbol_computed (sym, die, attr,
16518 member_loc, cu);
16519 }
16520 else
16521 dwarf2_complex_location_expr_complaint ();
16522 }
16523 }
16524 }
16525
16526 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16527 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16528 }
16529 }
16530
16531 /* Create a type for a C++ namespace. */
16532
16533 static struct type *
16534 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16535 {
16536 struct objfile *objfile = cu->per_objfile->objfile;
16537 const char *previous_prefix, *name;
16538 int is_anonymous;
16539 struct type *type;
16540
16541 /* For extensions, reuse the type of the original namespace. */
16542 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16543 {
16544 struct die_info *ext_die;
16545 struct dwarf2_cu *ext_cu = cu;
16546
16547 ext_die = dwarf2_extension (die, &ext_cu);
16548 type = read_type_die (ext_die, ext_cu);
16549
16550 /* EXT_CU may not be the same as CU.
16551 Ensure TYPE is recorded with CU in die_type_hash. */
16552 return set_die_type (die, type, cu);
16553 }
16554
16555 name = namespace_name (die, &is_anonymous, cu);
16556
16557 /* Now build the name of the current namespace. */
16558
16559 previous_prefix = determine_prefix (die, cu);
16560 if (previous_prefix[0] != '\0')
16561 name = typename_concat (&objfile->objfile_obstack,
16562 previous_prefix, name, 0, cu);
16563
16564 /* Create the type. */
16565 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16566
16567 return set_die_type (die, type, cu);
16568 }
16569
16570 /* Read a namespace scope. */
16571
16572 static void
16573 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16574 {
16575 struct objfile *objfile = cu->per_objfile->objfile;
16576 int is_anonymous;
16577
16578 /* Add a symbol associated to this if we haven't seen the namespace
16579 before. Also, add a using directive if it's an anonymous
16580 namespace. */
16581
16582 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16583 {
16584 struct type *type;
16585
16586 type = read_type_die (die, cu);
16587 new_symbol (die, type, cu);
16588
16589 namespace_name (die, &is_anonymous, cu);
16590 if (is_anonymous)
16591 {
16592 const char *previous_prefix = determine_prefix (die, cu);
16593
16594 std::vector<const char *> excludes;
16595 add_using_directive (using_directives (cu),
16596 previous_prefix, type->name (), NULL,
16597 NULL, excludes, 0, &objfile->objfile_obstack);
16598 }
16599 }
16600
16601 if (die->child != NULL)
16602 {
16603 struct die_info *child_die = die->child;
16604
16605 while (child_die && child_die->tag)
16606 {
16607 process_die (child_die, cu);
16608 child_die = child_die->sibling;
16609 }
16610 }
16611 }
16612
16613 /* Read a Fortran module as type. This DIE can be only a declaration used for
16614 imported module. Still we need that type as local Fortran "use ... only"
16615 declaration imports depend on the created type in determine_prefix. */
16616
16617 static struct type *
16618 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16619 {
16620 struct objfile *objfile = cu->per_objfile->objfile;
16621 const char *module_name;
16622 struct type *type;
16623
16624 module_name = dwarf2_name (die, cu);
16625 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16626
16627 return set_die_type (die, type, cu);
16628 }
16629
16630 /* Read a Fortran module. */
16631
16632 static void
16633 read_module (struct die_info *die, struct dwarf2_cu *cu)
16634 {
16635 struct die_info *child_die = die->child;
16636 struct type *type;
16637
16638 type = read_type_die (die, cu);
16639 new_symbol (die, type, cu);
16640
16641 while (child_die && child_die->tag)
16642 {
16643 process_die (child_die, cu);
16644 child_die = child_die->sibling;
16645 }
16646 }
16647
16648 /* Return the name of the namespace represented by DIE. Set
16649 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16650 namespace. */
16651
16652 static const char *
16653 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16654 {
16655 struct die_info *current_die;
16656 const char *name = NULL;
16657
16658 /* Loop through the extensions until we find a name. */
16659
16660 for (current_die = die;
16661 current_die != NULL;
16662 current_die = dwarf2_extension (die, &cu))
16663 {
16664 /* We don't use dwarf2_name here so that we can detect the absence
16665 of a name -> anonymous namespace. */
16666 name = dwarf2_string_attr (die, DW_AT_name, cu);
16667
16668 if (name != NULL)
16669 break;
16670 }
16671
16672 /* Is it an anonymous namespace? */
16673
16674 *is_anonymous = (name == NULL);
16675 if (*is_anonymous)
16676 name = CP_ANONYMOUS_NAMESPACE_STR;
16677
16678 return name;
16679 }
16680
16681 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16682 the user defined type vector. */
16683
16684 static struct type *
16685 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16686 {
16687 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16688 struct comp_unit_head *cu_header = &cu->header;
16689 struct type *type;
16690 struct attribute *attr_byte_size;
16691 struct attribute *attr_address_class;
16692 int byte_size, addr_class;
16693 struct type *target_type;
16694
16695 target_type = die_type (die, cu);
16696
16697 /* The die_type call above may have already set the type for this DIE. */
16698 type = get_die_type (die, cu);
16699 if (type)
16700 return type;
16701
16702 type = lookup_pointer_type (target_type);
16703
16704 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16705 if (attr_byte_size)
16706 byte_size = DW_UNSND (attr_byte_size);
16707 else
16708 byte_size = cu_header->addr_size;
16709
16710 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16711 if (attr_address_class)
16712 addr_class = DW_UNSND (attr_address_class);
16713 else
16714 addr_class = DW_ADDR_none;
16715
16716 ULONGEST alignment = get_alignment (cu, die);
16717
16718 /* If the pointer size, alignment, or address class is different
16719 than the default, create a type variant marked as such and set
16720 the length accordingly. */
16721 if (TYPE_LENGTH (type) != byte_size
16722 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16723 && alignment != TYPE_RAW_ALIGN (type))
16724 || addr_class != DW_ADDR_none)
16725 {
16726 if (gdbarch_address_class_type_flags_p (gdbarch))
16727 {
16728 int type_flags;
16729
16730 type_flags = gdbarch_address_class_type_flags
16731 (gdbarch, byte_size, addr_class);
16732 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16733 == 0);
16734 type = make_type_with_address_space (type, type_flags);
16735 }
16736 else if (TYPE_LENGTH (type) != byte_size)
16737 {
16738 complaint (_("invalid pointer size %d"), byte_size);
16739 }
16740 else if (TYPE_RAW_ALIGN (type) != alignment)
16741 {
16742 complaint (_("Invalid DW_AT_alignment"
16743 " - DIE at %s [in module %s]"),
16744 sect_offset_str (die->sect_off),
16745 objfile_name (cu->per_objfile->objfile));
16746 }
16747 else
16748 {
16749 /* Should we also complain about unhandled address classes? */
16750 }
16751 }
16752
16753 TYPE_LENGTH (type) = byte_size;
16754 set_type_align (type, alignment);
16755 return set_die_type (die, type, cu);
16756 }
16757
16758 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16759 the user defined type vector. */
16760
16761 static struct type *
16762 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16763 {
16764 struct type *type;
16765 struct type *to_type;
16766 struct type *domain;
16767
16768 to_type = die_type (die, cu);
16769 domain = die_containing_type (die, cu);
16770
16771 /* The calls above may have already set the type for this DIE. */
16772 type = get_die_type (die, cu);
16773 if (type)
16774 return type;
16775
16776 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16777 type = lookup_methodptr_type (to_type);
16778 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16779 {
16780 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16781
16782 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16783 to_type->fields (), to_type->num_fields (),
16784 TYPE_VARARGS (to_type));
16785 type = lookup_methodptr_type (new_type);
16786 }
16787 else
16788 type = lookup_memberptr_type (to_type, domain);
16789
16790 return set_die_type (die, type, cu);
16791 }
16792
16793 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16794 the user defined type vector. */
16795
16796 static struct type *
16797 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16798 enum type_code refcode)
16799 {
16800 struct comp_unit_head *cu_header = &cu->header;
16801 struct type *type, *target_type;
16802 struct attribute *attr;
16803
16804 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16805
16806 target_type = die_type (die, cu);
16807
16808 /* The die_type call above may have already set the type for this DIE. */
16809 type = get_die_type (die, cu);
16810 if (type)
16811 return type;
16812
16813 type = lookup_reference_type (target_type, refcode);
16814 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16815 if (attr != nullptr)
16816 {
16817 TYPE_LENGTH (type) = DW_UNSND (attr);
16818 }
16819 else
16820 {
16821 TYPE_LENGTH (type) = cu_header->addr_size;
16822 }
16823 maybe_set_alignment (cu, die, type);
16824 return set_die_type (die, type, cu);
16825 }
16826
16827 /* Add the given cv-qualifiers to the element type of the array. GCC
16828 outputs DWARF type qualifiers that apply to an array, not the
16829 element type. But GDB relies on the array element type to carry
16830 the cv-qualifiers. This mimics section 6.7.3 of the C99
16831 specification. */
16832
16833 static struct type *
16834 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16835 struct type *base_type, int cnst, int voltl)
16836 {
16837 struct type *el_type, *inner_array;
16838
16839 base_type = copy_type (base_type);
16840 inner_array = base_type;
16841
16842 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16843 {
16844 TYPE_TARGET_TYPE (inner_array) =
16845 copy_type (TYPE_TARGET_TYPE (inner_array));
16846 inner_array = TYPE_TARGET_TYPE (inner_array);
16847 }
16848
16849 el_type = TYPE_TARGET_TYPE (inner_array);
16850 cnst |= TYPE_CONST (el_type);
16851 voltl |= TYPE_VOLATILE (el_type);
16852 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16853
16854 return set_die_type (die, base_type, cu);
16855 }
16856
16857 static struct type *
16858 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16859 {
16860 struct type *base_type, *cv_type;
16861
16862 base_type = die_type (die, cu);
16863
16864 /* The die_type call above may have already set the type for this DIE. */
16865 cv_type = get_die_type (die, cu);
16866 if (cv_type)
16867 return cv_type;
16868
16869 /* In case the const qualifier is applied to an array type, the element type
16870 is so qualified, not the array type (section 6.7.3 of C99). */
16871 if (base_type->code () == TYPE_CODE_ARRAY)
16872 return add_array_cv_type (die, cu, base_type, 1, 0);
16873
16874 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16875 return set_die_type (die, cv_type, cu);
16876 }
16877
16878 static struct type *
16879 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16880 {
16881 struct type *base_type, *cv_type;
16882
16883 base_type = die_type (die, cu);
16884
16885 /* The die_type call above may have already set the type for this DIE. */
16886 cv_type = get_die_type (die, cu);
16887 if (cv_type)
16888 return cv_type;
16889
16890 /* In case the volatile qualifier is applied to an array type, the
16891 element type is so qualified, not the array type (section 6.7.3
16892 of C99). */
16893 if (base_type->code () == TYPE_CODE_ARRAY)
16894 return add_array_cv_type (die, cu, base_type, 0, 1);
16895
16896 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16897 return set_die_type (die, cv_type, cu);
16898 }
16899
16900 /* Handle DW_TAG_restrict_type. */
16901
16902 static struct type *
16903 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16904 {
16905 struct type *base_type, *cv_type;
16906
16907 base_type = die_type (die, cu);
16908
16909 /* The die_type call above may have already set the type for this DIE. */
16910 cv_type = get_die_type (die, cu);
16911 if (cv_type)
16912 return cv_type;
16913
16914 cv_type = make_restrict_type (base_type);
16915 return set_die_type (die, cv_type, cu);
16916 }
16917
16918 /* Handle DW_TAG_atomic_type. */
16919
16920 static struct type *
16921 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16922 {
16923 struct type *base_type, *cv_type;
16924
16925 base_type = die_type (die, cu);
16926
16927 /* The die_type call above may have already set the type for this DIE. */
16928 cv_type = get_die_type (die, cu);
16929 if (cv_type)
16930 return cv_type;
16931
16932 cv_type = make_atomic_type (base_type);
16933 return set_die_type (die, cv_type, cu);
16934 }
16935
16936 /* Extract all information from a DW_TAG_string_type DIE and add to
16937 the user defined type vector. It isn't really a user defined type,
16938 but it behaves like one, with other DIE's using an AT_user_def_type
16939 attribute to reference it. */
16940
16941 static struct type *
16942 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16943 {
16944 struct objfile *objfile = cu->per_objfile->objfile;
16945 struct gdbarch *gdbarch = objfile->arch ();
16946 struct type *type, *range_type, *index_type, *char_type;
16947 struct attribute *attr;
16948 struct dynamic_prop prop;
16949 bool length_is_constant = true;
16950 LONGEST length;
16951
16952 /* There are a couple of places where bit sizes might be made use of
16953 when parsing a DW_TAG_string_type, however, no producer that we know
16954 of make use of these. Handling bit sizes that are a multiple of the
16955 byte size is easy enough, but what about other bit sizes? Lets deal
16956 with that problem when we have to. Warn about these attributes being
16957 unsupported, then parse the type and ignore them like we always
16958 have. */
16959 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16960 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16961 {
16962 static bool warning_printed = false;
16963 if (!warning_printed)
16964 {
16965 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16966 "currently supported on DW_TAG_string_type."));
16967 warning_printed = true;
16968 }
16969 }
16970
16971 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16972 if (attr != nullptr && !attr->form_is_constant ())
16973 {
16974 /* The string length describes the location at which the length of
16975 the string can be found. The size of the length field can be
16976 specified with one of the attributes below. */
16977 struct type *prop_type;
16978 struct attribute *len
16979 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16980 if (len == nullptr)
16981 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16982 if (len != nullptr && len->form_is_constant ())
16983 {
16984 /* Pass 0 as the default as we know this attribute is constant
16985 and the default value will not be returned. */
16986 LONGEST sz = len->constant_value (0);
16987 prop_type = cu->per_cu->int_type (sz, true);
16988 }
16989 else
16990 {
16991 /* If the size is not specified then we assume it is the size of
16992 an address on this target. */
16993 prop_type = cu->per_cu->addr_sized_int_type (true);
16994 }
16995
16996 /* Convert the attribute into a dynamic property. */
16997 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16998 length = 1;
16999 else
17000 length_is_constant = false;
17001 }
17002 else if (attr != nullptr)
17003 {
17004 /* This DW_AT_string_length just contains the length with no
17005 indirection. There's no need to create a dynamic property in this
17006 case. Pass 0 for the default value as we know it will not be
17007 returned in this case. */
17008 length = attr->constant_value (0);
17009 }
17010 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17011 {
17012 /* We don't currently support non-constant byte sizes for strings. */
17013 length = attr->constant_value (1);
17014 }
17015 else
17016 {
17017 /* Use 1 as a fallback length if we have nothing else. */
17018 length = 1;
17019 }
17020
17021 index_type = objfile_type (objfile)->builtin_int;
17022 if (length_is_constant)
17023 range_type = create_static_range_type (NULL, index_type, 1, length);
17024 else
17025 {
17026 struct dynamic_prop low_bound;
17027
17028 low_bound.kind = PROP_CONST;
17029 low_bound.data.const_val = 1;
17030 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17031 }
17032 char_type = language_string_char_type (cu->language_defn, gdbarch);
17033 type = create_string_type (NULL, char_type, range_type);
17034
17035 return set_die_type (die, type, cu);
17036 }
17037
17038 /* Assuming that DIE corresponds to a function, returns nonzero
17039 if the function is prototyped. */
17040
17041 static int
17042 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17043 {
17044 struct attribute *attr;
17045
17046 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17047 if (attr && (DW_UNSND (attr) != 0))
17048 return 1;
17049
17050 /* The DWARF standard implies that the DW_AT_prototyped attribute
17051 is only meaningful for C, but the concept also extends to other
17052 languages that allow unprototyped functions (Eg: Objective C).
17053 For all other languages, assume that functions are always
17054 prototyped. */
17055 if (cu->language != language_c
17056 && cu->language != language_objc
17057 && cu->language != language_opencl)
17058 return 1;
17059
17060 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17061 prototyped and unprototyped functions; default to prototyped,
17062 since that is more common in modern code (and RealView warns
17063 about unprototyped functions). */
17064 if (producer_is_realview (cu->producer))
17065 return 1;
17066
17067 return 0;
17068 }
17069
17070 /* Handle DIES due to C code like:
17071
17072 struct foo
17073 {
17074 int (*funcp)(int a, long l);
17075 int b;
17076 };
17077
17078 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17079
17080 static struct type *
17081 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17082 {
17083 struct objfile *objfile = cu->per_objfile->objfile;
17084 struct type *type; /* Type that this function returns. */
17085 struct type *ftype; /* Function that returns above type. */
17086 struct attribute *attr;
17087
17088 type = die_type (die, cu);
17089
17090 /* The die_type call above may have already set the type for this DIE. */
17091 ftype = get_die_type (die, cu);
17092 if (ftype)
17093 return ftype;
17094
17095 ftype = lookup_function_type (type);
17096
17097 if (prototyped_function_p (die, cu))
17098 TYPE_PROTOTYPED (ftype) = 1;
17099
17100 /* Store the calling convention in the type if it's available in
17101 the subroutine die. Otherwise set the calling convention to
17102 the default value DW_CC_normal. */
17103 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17104 if (attr != nullptr
17105 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17106 TYPE_CALLING_CONVENTION (ftype)
17107 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17108 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17109 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17110 else
17111 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17112
17113 /* Record whether the function returns normally to its caller or not
17114 if the DWARF producer set that information. */
17115 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17116 if (attr && (DW_UNSND (attr) != 0))
17117 TYPE_NO_RETURN (ftype) = 1;
17118
17119 /* We need to add the subroutine type to the die immediately so
17120 we don't infinitely recurse when dealing with parameters
17121 declared as the same subroutine type. */
17122 set_die_type (die, ftype, cu);
17123
17124 if (die->child != NULL)
17125 {
17126 struct type *void_type = objfile_type (objfile)->builtin_void;
17127 struct die_info *child_die;
17128 int nparams, iparams;
17129
17130 /* Count the number of parameters.
17131 FIXME: GDB currently ignores vararg functions, but knows about
17132 vararg member functions. */
17133 nparams = 0;
17134 child_die = die->child;
17135 while (child_die && child_die->tag)
17136 {
17137 if (child_die->tag == DW_TAG_formal_parameter)
17138 nparams++;
17139 else if (child_die->tag == DW_TAG_unspecified_parameters)
17140 TYPE_VARARGS (ftype) = 1;
17141 child_die = child_die->sibling;
17142 }
17143
17144 /* Allocate storage for parameters and fill them in. */
17145 ftype->set_num_fields (nparams);
17146 ftype->set_fields
17147 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17148
17149 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17150 even if we error out during the parameters reading below. */
17151 for (iparams = 0; iparams < nparams; iparams++)
17152 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17153
17154 iparams = 0;
17155 child_die = die->child;
17156 while (child_die && child_die->tag)
17157 {
17158 if (child_die->tag == DW_TAG_formal_parameter)
17159 {
17160 struct type *arg_type;
17161
17162 /* DWARF version 2 has no clean way to discern C++
17163 static and non-static member functions. G++ helps
17164 GDB by marking the first parameter for non-static
17165 member functions (which is the this pointer) as
17166 artificial. We pass this information to
17167 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17168
17169 DWARF version 3 added DW_AT_object_pointer, which GCC
17170 4.5 does not yet generate. */
17171 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17172 if (attr != nullptr)
17173 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17174 else
17175 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17176 arg_type = die_type (child_die, cu);
17177
17178 /* RealView does not mark THIS as const, which the testsuite
17179 expects. GCC marks THIS as const in method definitions,
17180 but not in the class specifications (GCC PR 43053). */
17181 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17182 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17183 {
17184 int is_this = 0;
17185 struct dwarf2_cu *arg_cu = cu;
17186 const char *name = dwarf2_name (child_die, cu);
17187
17188 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17189 if (attr != nullptr)
17190 {
17191 /* If the compiler emits this, use it. */
17192 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17193 is_this = 1;
17194 }
17195 else if (name && strcmp (name, "this") == 0)
17196 /* Function definitions will have the argument names. */
17197 is_this = 1;
17198 else if (name == NULL && iparams == 0)
17199 /* Declarations may not have the names, so like
17200 elsewhere in GDB, assume an artificial first
17201 argument is "this". */
17202 is_this = 1;
17203
17204 if (is_this)
17205 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17206 arg_type, 0);
17207 }
17208
17209 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17210 iparams++;
17211 }
17212 child_die = child_die->sibling;
17213 }
17214 }
17215
17216 return ftype;
17217 }
17218
17219 static struct type *
17220 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17221 {
17222 struct objfile *objfile = cu->per_objfile->objfile;
17223 const char *name = NULL;
17224 struct type *this_type, *target_type;
17225
17226 name = dwarf2_full_name (NULL, die, cu);
17227 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17228 TYPE_TARGET_STUB (this_type) = 1;
17229 set_die_type (die, this_type, cu);
17230 target_type = die_type (die, cu);
17231 if (target_type != this_type)
17232 TYPE_TARGET_TYPE (this_type) = target_type;
17233 else
17234 {
17235 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17236 spec and cause infinite loops in GDB. */
17237 complaint (_("Self-referential DW_TAG_typedef "
17238 "- DIE at %s [in module %s]"),
17239 sect_offset_str (die->sect_off), objfile_name (objfile));
17240 TYPE_TARGET_TYPE (this_type) = NULL;
17241 }
17242 if (name == NULL)
17243 {
17244 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17245 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17246 Handle these by just returning the target type, rather than
17247 constructing an anonymous typedef type and trying to handle this
17248 elsewhere. */
17249 set_die_type (die, target_type, cu);
17250 return target_type;
17251 }
17252 return this_type;
17253 }
17254
17255 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17256 (which may be different from NAME) to the architecture back-end to allow
17257 it to guess the correct format if necessary. */
17258
17259 static struct type *
17260 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17261 const char *name_hint, enum bfd_endian byte_order)
17262 {
17263 struct gdbarch *gdbarch = objfile->arch ();
17264 const struct floatformat **format;
17265 struct type *type;
17266
17267 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17268 if (format)
17269 type = init_float_type (objfile, bits, name, format, byte_order);
17270 else
17271 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17272
17273 return type;
17274 }
17275
17276 /* Allocate an integer type of size BITS and name NAME. */
17277
17278 static struct type *
17279 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17280 int bits, int unsigned_p, const char *name)
17281 {
17282 struct type *type;
17283
17284 /* Versions of Intel's C Compiler generate an integer type called "void"
17285 instead of using DW_TAG_unspecified_type. This has been seen on
17286 at least versions 14, 17, and 18. */
17287 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17288 && strcmp (name, "void") == 0)
17289 type = objfile_type (objfile)->builtin_void;
17290 else
17291 type = init_integer_type (objfile, bits, unsigned_p, name);
17292
17293 return type;
17294 }
17295
17296 /* Initialise and return a floating point type of size BITS suitable for
17297 use as a component of a complex number. The NAME_HINT is passed through
17298 when initialising the floating point type and is the name of the complex
17299 type.
17300
17301 As DWARF doesn't currently provide an explicit name for the components
17302 of a complex number, but it can be helpful to have these components
17303 named, we try to select a suitable name based on the size of the
17304 component. */
17305 static struct type *
17306 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17307 struct objfile *objfile,
17308 int bits, const char *name_hint,
17309 enum bfd_endian byte_order)
17310 {
17311 gdbarch *gdbarch = objfile->arch ();
17312 struct type *tt = nullptr;
17313
17314 /* Try to find a suitable floating point builtin type of size BITS.
17315 We're going to use the name of this type as the name for the complex
17316 target type that we are about to create. */
17317 switch (cu->language)
17318 {
17319 case language_fortran:
17320 switch (bits)
17321 {
17322 case 32:
17323 tt = builtin_f_type (gdbarch)->builtin_real;
17324 break;
17325 case 64:
17326 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17327 break;
17328 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17329 case 128:
17330 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17331 break;
17332 }
17333 break;
17334 default:
17335 switch (bits)
17336 {
17337 case 32:
17338 tt = builtin_type (gdbarch)->builtin_float;
17339 break;
17340 case 64:
17341 tt = builtin_type (gdbarch)->builtin_double;
17342 break;
17343 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17344 case 128:
17345 tt = builtin_type (gdbarch)->builtin_long_double;
17346 break;
17347 }
17348 break;
17349 }
17350
17351 /* If the type we found doesn't match the size we were looking for, then
17352 pretend we didn't find a type at all, the complex target type we
17353 create will then be nameless. */
17354 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17355 tt = nullptr;
17356
17357 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17358 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17359 }
17360
17361 /* Find a representation of a given base type and install
17362 it in the TYPE field of the die. */
17363
17364 static struct type *
17365 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17366 {
17367 struct objfile *objfile = cu->per_objfile->objfile;
17368 struct type *type;
17369 struct attribute *attr;
17370 int encoding = 0, bits = 0;
17371 const char *name;
17372 gdbarch *arch;
17373
17374 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17375 if (attr != nullptr)
17376 encoding = DW_UNSND (attr);
17377 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17378 if (attr != nullptr)
17379 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17380 name = dwarf2_name (die, cu);
17381 if (!name)
17382 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17383
17384 arch = objfile->arch ();
17385 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17386
17387 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17388 if (attr)
17389 {
17390 int endianity = DW_UNSND (attr);
17391
17392 switch (endianity)
17393 {
17394 case DW_END_big:
17395 byte_order = BFD_ENDIAN_BIG;
17396 break;
17397 case DW_END_little:
17398 byte_order = BFD_ENDIAN_LITTLE;
17399 break;
17400 default:
17401 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17402 break;
17403 }
17404 }
17405
17406 switch (encoding)
17407 {
17408 case DW_ATE_address:
17409 /* Turn DW_ATE_address into a void * pointer. */
17410 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17411 type = init_pointer_type (objfile, bits, name, type);
17412 break;
17413 case DW_ATE_boolean:
17414 type = init_boolean_type (objfile, bits, 1, name);
17415 break;
17416 case DW_ATE_complex_float:
17417 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17418 byte_order);
17419 if (type->code () == TYPE_CODE_ERROR)
17420 {
17421 if (name == nullptr)
17422 {
17423 struct obstack *obstack
17424 = &cu->per_objfile->objfile->objfile_obstack;
17425 name = obconcat (obstack, "_Complex ", type->name (),
17426 nullptr);
17427 }
17428 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17429 }
17430 else
17431 type = init_complex_type (name, type);
17432 break;
17433 case DW_ATE_decimal_float:
17434 type = init_decfloat_type (objfile, bits, name);
17435 break;
17436 case DW_ATE_float:
17437 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17438 break;
17439 case DW_ATE_signed:
17440 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17441 break;
17442 case DW_ATE_unsigned:
17443 if (cu->language == language_fortran
17444 && name
17445 && startswith (name, "character("))
17446 type = init_character_type (objfile, bits, 1, name);
17447 else
17448 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17449 break;
17450 case DW_ATE_signed_char:
17451 if (cu->language == language_ada || cu->language == language_m2
17452 || cu->language == language_pascal
17453 || cu->language == language_fortran)
17454 type = init_character_type (objfile, bits, 0, name);
17455 else
17456 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17457 break;
17458 case DW_ATE_unsigned_char:
17459 if (cu->language == language_ada || cu->language == language_m2
17460 || cu->language == language_pascal
17461 || cu->language == language_fortran
17462 || cu->language == language_rust)
17463 type = init_character_type (objfile, bits, 1, name);
17464 else
17465 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17466 break;
17467 case DW_ATE_UTF:
17468 {
17469 if (bits == 16)
17470 type = builtin_type (arch)->builtin_char16;
17471 else if (bits == 32)
17472 type = builtin_type (arch)->builtin_char32;
17473 else
17474 {
17475 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17476 bits);
17477 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17478 }
17479 return set_die_type (die, type, cu);
17480 }
17481 break;
17482
17483 default:
17484 complaint (_("unsupported DW_AT_encoding: '%s'"),
17485 dwarf_type_encoding_name (encoding));
17486 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17487 break;
17488 }
17489
17490 if (name && strcmp (name, "char") == 0)
17491 TYPE_NOSIGN (type) = 1;
17492
17493 maybe_set_alignment (cu, die, type);
17494
17495 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17496
17497 return set_die_type (die, type, cu);
17498 }
17499
17500 /* Parse dwarf attribute if it's a block, reference or constant and put the
17501 resulting value of the attribute into struct bound_prop.
17502 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17503
17504 static int
17505 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17506 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17507 struct type *default_type)
17508 {
17509 struct dwarf2_property_baton *baton;
17510 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17511 struct objfile *objfile = per_objfile->objfile;
17512 struct obstack *obstack = &objfile->objfile_obstack;
17513
17514 gdb_assert (default_type != NULL);
17515
17516 if (attr == NULL || prop == NULL)
17517 return 0;
17518
17519 if (attr->form_is_block ())
17520 {
17521 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17522 baton->property_type = default_type;
17523 baton->locexpr.per_cu = cu->per_cu;
17524 baton->locexpr.per_objfile = per_objfile;
17525 baton->locexpr.size = DW_BLOCK (attr)->size;
17526 baton->locexpr.data = DW_BLOCK (attr)->data;
17527 switch (attr->name)
17528 {
17529 case DW_AT_string_length:
17530 baton->locexpr.is_reference = true;
17531 break;
17532 default:
17533 baton->locexpr.is_reference = false;
17534 break;
17535 }
17536 prop->data.baton = baton;
17537 prop->kind = PROP_LOCEXPR;
17538 gdb_assert (prop->data.baton != NULL);
17539 }
17540 else if (attr->form_is_ref ())
17541 {
17542 struct dwarf2_cu *target_cu = cu;
17543 struct die_info *target_die;
17544 struct attribute *target_attr;
17545
17546 target_die = follow_die_ref (die, attr, &target_cu);
17547 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17548 if (target_attr == NULL)
17549 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17550 target_cu);
17551 if (target_attr == NULL)
17552 return 0;
17553
17554 switch (target_attr->name)
17555 {
17556 case DW_AT_location:
17557 if (target_attr->form_is_section_offset ())
17558 {
17559 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17560 baton->property_type = die_type (target_die, target_cu);
17561 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17562 prop->data.baton = baton;
17563 prop->kind = PROP_LOCLIST;
17564 gdb_assert (prop->data.baton != NULL);
17565 }
17566 else if (target_attr->form_is_block ())
17567 {
17568 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17569 baton->property_type = die_type (target_die, target_cu);
17570 baton->locexpr.per_cu = cu->per_cu;
17571 baton->locexpr.per_objfile = per_objfile;
17572 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17573 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17574 baton->locexpr.is_reference = true;
17575 prop->data.baton = baton;
17576 prop->kind = PROP_LOCEXPR;
17577 gdb_assert (prop->data.baton != NULL);
17578 }
17579 else
17580 {
17581 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17582 "dynamic property");
17583 return 0;
17584 }
17585 break;
17586 case DW_AT_data_member_location:
17587 {
17588 LONGEST offset;
17589
17590 if (!handle_data_member_location (target_die, target_cu,
17591 &offset))
17592 return 0;
17593
17594 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17595 baton->property_type = read_type_die (target_die->parent,
17596 target_cu);
17597 baton->offset_info.offset = offset;
17598 baton->offset_info.type = die_type (target_die, target_cu);
17599 prop->data.baton = baton;
17600 prop->kind = PROP_ADDR_OFFSET;
17601 break;
17602 }
17603 }
17604 }
17605 else if (attr->form_is_constant ())
17606 {
17607 prop->data.const_val = attr->constant_value (0);
17608 prop->kind = PROP_CONST;
17609 }
17610 else
17611 {
17612 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17613 dwarf2_name (die, cu));
17614 return 0;
17615 }
17616
17617 return 1;
17618 }
17619
17620 /* See read.h. */
17621
17622 struct type *
17623 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17624 {
17625 struct objfile *objfile = dwarf2_per_objfile->objfile;
17626 struct type *int_type;
17627
17628 /* Helper macro to examine the various builtin types. */
17629 #define TRY_TYPE(F) \
17630 int_type = (unsigned_p \
17631 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17632 : objfile_type (objfile)->builtin_ ## F); \
17633 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17634 return int_type
17635
17636 TRY_TYPE (char);
17637 TRY_TYPE (short);
17638 TRY_TYPE (int);
17639 TRY_TYPE (long);
17640 TRY_TYPE (long_long);
17641
17642 #undef TRY_TYPE
17643
17644 gdb_assert_not_reached ("unable to find suitable integer type");
17645 }
17646
17647 /* See read.h. */
17648
17649 struct type *
17650 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17651 {
17652 int addr_size = this->addr_size ();
17653 return int_type (addr_size, unsigned_p);
17654 }
17655
17656 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17657 present (which is valid) then compute the default type based on the
17658 compilation units address size. */
17659
17660 static struct type *
17661 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17662 {
17663 struct type *index_type = die_type (die, cu);
17664
17665 /* Dwarf-2 specifications explicitly allows to create subrange types
17666 without specifying a base type.
17667 In that case, the base type must be set to the type of
17668 the lower bound, upper bound or count, in that order, if any of these
17669 three attributes references an object that has a type.
17670 If no base type is found, the Dwarf-2 specifications say that
17671 a signed integer type of size equal to the size of an address should
17672 be used.
17673 For the following C code: `extern char gdb_int [];'
17674 GCC produces an empty range DIE.
17675 FIXME: muller/2010-05-28: Possible references to object for low bound,
17676 high bound or count are not yet handled by this code. */
17677 if (index_type->code () == TYPE_CODE_VOID)
17678 index_type = cu->per_cu->addr_sized_int_type (false);
17679
17680 return index_type;
17681 }
17682
17683 /* Read the given DW_AT_subrange DIE. */
17684
17685 static struct type *
17686 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17687 {
17688 struct type *base_type, *orig_base_type;
17689 struct type *range_type;
17690 struct attribute *attr;
17691 struct dynamic_prop low, high;
17692 int low_default_is_valid;
17693 int high_bound_is_count = 0;
17694 const char *name;
17695 ULONGEST negative_mask;
17696
17697 orig_base_type = read_subrange_index_type (die, cu);
17698
17699 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17700 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17701 creating the range type, but we use the result of check_typedef
17702 when examining properties of the type. */
17703 base_type = check_typedef (orig_base_type);
17704
17705 /* The die_type call above may have already set the type for this DIE. */
17706 range_type = get_die_type (die, cu);
17707 if (range_type)
17708 return range_type;
17709
17710 low.kind = PROP_CONST;
17711 high.kind = PROP_CONST;
17712 high.data.const_val = 0;
17713
17714 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17715 omitting DW_AT_lower_bound. */
17716 switch (cu->language)
17717 {
17718 case language_c:
17719 case language_cplus:
17720 low.data.const_val = 0;
17721 low_default_is_valid = 1;
17722 break;
17723 case language_fortran:
17724 low.data.const_val = 1;
17725 low_default_is_valid = 1;
17726 break;
17727 case language_d:
17728 case language_objc:
17729 case language_rust:
17730 low.data.const_val = 0;
17731 low_default_is_valid = (cu->header.version >= 4);
17732 break;
17733 case language_ada:
17734 case language_m2:
17735 case language_pascal:
17736 low.data.const_val = 1;
17737 low_default_is_valid = (cu->header.version >= 4);
17738 break;
17739 default:
17740 low.data.const_val = 0;
17741 low_default_is_valid = 0;
17742 break;
17743 }
17744
17745 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17746 if (attr != nullptr)
17747 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17748 else if (!low_default_is_valid)
17749 complaint (_("Missing DW_AT_lower_bound "
17750 "- DIE at %s [in module %s]"),
17751 sect_offset_str (die->sect_off),
17752 objfile_name (cu->per_objfile->objfile));
17753
17754 struct attribute *attr_ub, *attr_count;
17755 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17756 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17757 {
17758 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17759 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17760 {
17761 /* If bounds are constant do the final calculation here. */
17762 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17763 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17764 else
17765 high_bound_is_count = 1;
17766 }
17767 else
17768 {
17769 if (attr_ub != NULL)
17770 complaint (_("Unresolved DW_AT_upper_bound "
17771 "- DIE at %s [in module %s]"),
17772 sect_offset_str (die->sect_off),
17773 objfile_name (cu->per_objfile->objfile));
17774 if (attr_count != NULL)
17775 complaint (_("Unresolved DW_AT_count "
17776 "- DIE at %s [in module %s]"),
17777 sect_offset_str (die->sect_off),
17778 objfile_name (cu->per_objfile->objfile));
17779 }
17780 }
17781
17782 LONGEST bias = 0;
17783 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17784 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17785 bias = bias_attr->constant_value (0);
17786
17787 /* Normally, the DWARF producers are expected to use a signed
17788 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17789 But this is unfortunately not always the case, as witnessed
17790 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17791 is used instead. To work around that ambiguity, we treat
17792 the bounds as signed, and thus sign-extend their values, when
17793 the base type is signed. */
17794 negative_mask =
17795 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17796 if (low.kind == PROP_CONST
17797 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17798 low.data.const_val |= negative_mask;
17799 if (high.kind == PROP_CONST
17800 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17801 high.data.const_val |= negative_mask;
17802
17803 /* Check for bit and byte strides. */
17804 struct dynamic_prop byte_stride_prop;
17805 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17806 if (attr_byte_stride != nullptr)
17807 {
17808 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17809 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17810 prop_type);
17811 }
17812
17813 struct dynamic_prop bit_stride_prop;
17814 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17815 if (attr_bit_stride != nullptr)
17816 {
17817 /* It only makes sense to have either a bit or byte stride. */
17818 if (attr_byte_stride != nullptr)
17819 {
17820 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17821 "- DIE at %s [in module %s]"),
17822 sect_offset_str (die->sect_off),
17823 objfile_name (cu->per_objfile->objfile));
17824 attr_bit_stride = nullptr;
17825 }
17826 else
17827 {
17828 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17829 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17830 prop_type);
17831 }
17832 }
17833
17834 if (attr_byte_stride != nullptr
17835 || attr_bit_stride != nullptr)
17836 {
17837 bool byte_stride_p = (attr_byte_stride != nullptr);
17838 struct dynamic_prop *stride
17839 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17840
17841 range_type
17842 = create_range_type_with_stride (NULL, orig_base_type, &low,
17843 &high, bias, stride, byte_stride_p);
17844 }
17845 else
17846 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17847
17848 if (high_bound_is_count)
17849 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17850
17851 /* Ada expects an empty array on no boundary attributes. */
17852 if (attr == NULL && cu->language != language_ada)
17853 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17854
17855 name = dwarf2_name (die, cu);
17856 if (name)
17857 range_type->set_name (name);
17858
17859 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17860 if (attr != nullptr)
17861 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17862
17863 maybe_set_alignment (cu, die, range_type);
17864
17865 set_die_type (die, range_type, cu);
17866
17867 /* set_die_type should be already done. */
17868 set_descriptive_type (range_type, die, cu);
17869
17870 return range_type;
17871 }
17872
17873 static struct type *
17874 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17875 {
17876 struct type *type;
17877
17878 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17879 type->set_name (dwarf2_name (die, cu));
17880
17881 /* In Ada, an unspecified type is typically used when the description
17882 of the type is deferred to a different unit. When encountering
17883 such a type, we treat it as a stub, and try to resolve it later on,
17884 when needed. */
17885 if (cu->language == language_ada)
17886 TYPE_STUB (type) = 1;
17887
17888 return set_die_type (die, type, cu);
17889 }
17890
17891 /* Read a single die and all its descendents. Set the die's sibling
17892 field to NULL; set other fields in the die correctly, and set all
17893 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17894 location of the info_ptr after reading all of those dies. PARENT
17895 is the parent of the die in question. */
17896
17897 static struct die_info *
17898 read_die_and_children (const struct die_reader_specs *reader,
17899 const gdb_byte *info_ptr,
17900 const gdb_byte **new_info_ptr,
17901 struct die_info *parent)
17902 {
17903 struct die_info *die;
17904 const gdb_byte *cur_ptr;
17905
17906 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17907 if (die == NULL)
17908 {
17909 *new_info_ptr = cur_ptr;
17910 return NULL;
17911 }
17912 store_in_ref_table (die, reader->cu);
17913
17914 if (die->has_children)
17915 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17916 else
17917 {
17918 die->child = NULL;
17919 *new_info_ptr = cur_ptr;
17920 }
17921
17922 die->sibling = NULL;
17923 die->parent = parent;
17924 return die;
17925 }
17926
17927 /* Read a die, all of its descendents, and all of its siblings; set
17928 all of the fields of all of the dies correctly. Arguments are as
17929 in read_die_and_children. */
17930
17931 static struct die_info *
17932 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17933 const gdb_byte *info_ptr,
17934 const gdb_byte **new_info_ptr,
17935 struct die_info *parent)
17936 {
17937 struct die_info *first_die, *last_sibling;
17938 const gdb_byte *cur_ptr;
17939
17940 cur_ptr = info_ptr;
17941 first_die = last_sibling = NULL;
17942
17943 while (1)
17944 {
17945 struct die_info *die
17946 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17947
17948 if (die == NULL)
17949 {
17950 *new_info_ptr = cur_ptr;
17951 return first_die;
17952 }
17953
17954 if (!first_die)
17955 first_die = die;
17956 else
17957 last_sibling->sibling = die;
17958
17959 last_sibling = die;
17960 }
17961 }
17962
17963 /* Read a die, all of its descendents, and all of its siblings; set
17964 all of the fields of all of the dies correctly. Arguments are as
17965 in read_die_and_children.
17966 This the main entry point for reading a DIE and all its children. */
17967
17968 static struct die_info *
17969 read_die_and_siblings (const struct die_reader_specs *reader,
17970 const gdb_byte *info_ptr,
17971 const gdb_byte **new_info_ptr,
17972 struct die_info *parent)
17973 {
17974 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17975 new_info_ptr, parent);
17976
17977 if (dwarf_die_debug)
17978 {
17979 fprintf_unfiltered (gdb_stdlog,
17980 "Read die from %s@0x%x of %s:\n",
17981 reader->die_section->get_name (),
17982 (unsigned) (info_ptr - reader->die_section->buffer),
17983 bfd_get_filename (reader->abfd));
17984 dump_die (die, dwarf_die_debug);
17985 }
17986
17987 return die;
17988 }
17989
17990 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17991 attributes.
17992 The caller is responsible for filling in the extra attributes
17993 and updating (*DIEP)->num_attrs.
17994 Set DIEP to point to a newly allocated die with its information,
17995 except for its child, sibling, and parent fields. */
17996
17997 static const gdb_byte *
17998 read_full_die_1 (const struct die_reader_specs *reader,
17999 struct die_info **diep, const gdb_byte *info_ptr,
18000 int num_extra_attrs)
18001 {
18002 unsigned int abbrev_number, bytes_read, i;
18003 struct abbrev_info *abbrev;
18004 struct die_info *die;
18005 struct dwarf2_cu *cu = reader->cu;
18006 bfd *abfd = reader->abfd;
18007
18008 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18009 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18010 info_ptr += bytes_read;
18011 if (!abbrev_number)
18012 {
18013 *diep = NULL;
18014 return info_ptr;
18015 }
18016
18017 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18018 if (!abbrev)
18019 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18020 abbrev_number,
18021 bfd_get_filename (abfd));
18022
18023 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18024 die->sect_off = sect_off;
18025 die->tag = abbrev->tag;
18026 die->abbrev = abbrev_number;
18027 die->has_children = abbrev->has_children;
18028
18029 /* Make the result usable.
18030 The caller needs to update num_attrs after adding the extra
18031 attributes. */
18032 die->num_attrs = abbrev->num_attrs;
18033
18034 std::vector<int> indexes_that_need_reprocess;
18035 for (i = 0; i < abbrev->num_attrs; ++i)
18036 {
18037 bool need_reprocess;
18038 info_ptr =
18039 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18040 info_ptr, &need_reprocess);
18041 if (need_reprocess)
18042 indexes_that_need_reprocess.push_back (i);
18043 }
18044
18045 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18046 if (attr != nullptr)
18047 cu->str_offsets_base = DW_UNSND (attr);
18048
18049 attr = die->attr (DW_AT_loclists_base);
18050 if (attr != nullptr)
18051 cu->loclist_base = DW_UNSND (attr);
18052
18053 auto maybe_addr_base = die->addr_base ();
18054 if (maybe_addr_base.has_value ())
18055 cu->addr_base = *maybe_addr_base;
18056 for (int index : indexes_that_need_reprocess)
18057 read_attribute_reprocess (reader, &die->attrs[index]);
18058 *diep = die;
18059 return info_ptr;
18060 }
18061
18062 /* Read a die and all its attributes.
18063 Set DIEP to point to a newly allocated die with its information,
18064 except for its child, sibling, and parent fields. */
18065
18066 static const gdb_byte *
18067 read_full_die (const struct die_reader_specs *reader,
18068 struct die_info **diep, const gdb_byte *info_ptr)
18069 {
18070 const gdb_byte *result;
18071
18072 result = read_full_die_1 (reader, diep, info_ptr, 0);
18073
18074 if (dwarf_die_debug)
18075 {
18076 fprintf_unfiltered (gdb_stdlog,
18077 "Read die from %s@0x%x of %s:\n",
18078 reader->die_section->get_name (),
18079 (unsigned) (info_ptr - reader->die_section->buffer),
18080 bfd_get_filename (reader->abfd));
18081 dump_die (*diep, dwarf_die_debug);
18082 }
18083
18084 return result;
18085 }
18086 \f
18087
18088 /* Returns nonzero if TAG represents a type that we might generate a partial
18089 symbol for. */
18090
18091 static int
18092 is_type_tag_for_partial (int tag)
18093 {
18094 switch (tag)
18095 {
18096 #if 0
18097 /* Some types that would be reasonable to generate partial symbols for,
18098 that we don't at present. */
18099 case DW_TAG_array_type:
18100 case DW_TAG_file_type:
18101 case DW_TAG_ptr_to_member_type:
18102 case DW_TAG_set_type:
18103 case DW_TAG_string_type:
18104 case DW_TAG_subroutine_type:
18105 #endif
18106 case DW_TAG_base_type:
18107 case DW_TAG_class_type:
18108 case DW_TAG_interface_type:
18109 case DW_TAG_enumeration_type:
18110 case DW_TAG_structure_type:
18111 case DW_TAG_subrange_type:
18112 case DW_TAG_typedef:
18113 case DW_TAG_union_type:
18114 return 1;
18115 default:
18116 return 0;
18117 }
18118 }
18119
18120 /* Load all DIEs that are interesting for partial symbols into memory. */
18121
18122 static struct partial_die_info *
18123 load_partial_dies (const struct die_reader_specs *reader,
18124 const gdb_byte *info_ptr, int building_psymtab)
18125 {
18126 struct dwarf2_cu *cu = reader->cu;
18127 struct objfile *objfile = cu->per_objfile->objfile;
18128 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18129 unsigned int bytes_read;
18130 unsigned int load_all = 0;
18131 int nesting_level = 1;
18132
18133 parent_die = NULL;
18134 last_die = NULL;
18135
18136 gdb_assert (cu->per_cu != NULL);
18137 if (cu->per_cu->load_all_dies)
18138 load_all = 1;
18139
18140 cu->partial_dies
18141 = htab_create_alloc_ex (cu->header.length / 12,
18142 partial_die_hash,
18143 partial_die_eq,
18144 NULL,
18145 &cu->comp_unit_obstack,
18146 hashtab_obstack_allocate,
18147 dummy_obstack_deallocate);
18148
18149 while (1)
18150 {
18151 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18152
18153 /* A NULL abbrev means the end of a series of children. */
18154 if (abbrev == NULL)
18155 {
18156 if (--nesting_level == 0)
18157 return first_die;
18158
18159 info_ptr += bytes_read;
18160 last_die = parent_die;
18161 parent_die = parent_die->die_parent;
18162 continue;
18163 }
18164
18165 /* Check for template arguments. We never save these; if
18166 they're seen, we just mark the parent, and go on our way. */
18167 if (parent_die != NULL
18168 && cu->language == language_cplus
18169 && (abbrev->tag == DW_TAG_template_type_param
18170 || abbrev->tag == DW_TAG_template_value_param))
18171 {
18172 parent_die->has_template_arguments = 1;
18173
18174 if (!load_all)
18175 {
18176 /* We don't need a partial DIE for the template argument. */
18177 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18178 continue;
18179 }
18180 }
18181
18182 /* We only recurse into c++ subprograms looking for template arguments.
18183 Skip their other children. */
18184 if (!load_all
18185 && cu->language == language_cplus
18186 && parent_die != NULL
18187 && parent_die->tag == DW_TAG_subprogram)
18188 {
18189 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18190 continue;
18191 }
18192
18193 /* Check whether this DIE is interesting enough to save. Normally
18194 we would not be interested in members here, but there may be
18195 later variables referencing them via DW_AT_specification (for
18196 static members). */
18197 if (!load_all
18198 && !is_type_tag_for_partial (abbrev->tag)
18199 && abbrev->tag != DW_TAG_constant
18200 && abbrev->tag != DW_TAG_enumerator
18201 && abbrev->tag != DW_TAG_subprogram
18202 && abbrev->tag != DW_TAG_inlined_subroutine
18203 && abbrev->tag != DW_TAG_lexical_block
18204 && abbrev->tag != DW_TAG_variable
18205 && abbrev->tag != DW_TAG_namespace
18206 && abbrev->tag != DW_TAG_module
18207 && abbrev->tag != DW_TAG_member
18208 && abbrev->tag != DW_TAG_imported_unit
18209 && abbrev->tag != DW_TAG_imported_declaration)
18210 {
18211 /* Otherwise we skip to the next sibling, if any. */
18212 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18213 continue;
18214 }
18215
18216 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18217 abbrev);
18218
18219 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18220
18221 /* This two-pass algorithm for processing partial symbols has a
18222 high cost in cache pressure. Thus, handle some simple cases
18223 here which cover the majority of C partial symbols. DIEs
18224 which neither have specification tags in them, nor could have
18225 specification tags elsewhere pointing at them, can simply be
18226 processed and discarded.
18227
18228 This segment is also optional; scan_partial_symbols and
18229 add_partial_symbol will handle these DIEs if we chain
18230 them in normally. When compilers which do not emit large
18231 quantities of duplicate debug information are more common,
18232 this code can probably be removed. */
18233
18234 /* Any complete simple types at the top level (pretty much all
18235 of them, for a language without namespaces), can be processed
18236 directly. */
18237 if (parent_die == NULL
18238 && pdi.has_specification == 0
18239 && pdi.is_declaration == 0
18240 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18241 || pdi.tag == DW_TAG_base_type
18242 || pdi.tag == DW_TAG_subrange_type))
18243 {
18244 if (building_psymtab && pdi.name != NULL)
18245 add_psymbol_to_list (pdi.name, false,
18246 VAR_DOMAIN, LOC_TYPEDEF, -1,
18247 psymbol_placement::STATIC,
18248 0, cu->language, objfile);
18249 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18250 continue;
18251 }
18252
18253 /* The exception for DW_TAG_typedef with has_children above is
18254 a workaround of GCC PR debug/47510. In the case of this complaint
18255 type_name_or_error will error on such types later.
18256
18257 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18258 it could not find the child DIEs referenced later, this is checked
18259 above. In correct DWARF DW_TAG_typedef should have no children. */
18260
18261 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18262 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18263 "- DIE at %s [in module %s]"),
18264 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18265
18266 /* If we're at the second level, and we're an enumerator, and
18267 our parent has no specification (meaning possibly lives in a
18268 namespace elsewhere), then we can add the partial symbol now
18269 instead of queueing it. */
18270 if (pdi.tag == DW_TAG_enumerator
18271 && parent_die != NULL
18272 && parent_die->die_parent == NULL
18273 && parent_die->tag == DW_TAG_enumeration_type
18274 && parent_die->has_specification == 0)
18275 {
18276 if (pdi.name == NULL)
18277 complaint (_("malformed enumerator DIE ignored"));
18278 else if (building_psymtab)
18279 add_psymbol_to_list (pdi.name, false,
18280 VAR_DOMAIN, LOC_CONST, -1,
18281 cu->language == language_cplus
18282 ? psymbol_placement::GLOBAL
18283 : psymbol_placement::STATIC,
18284 0, cu->language, objfile);
18285
18286 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18287 continue;
18288 }
18289
18290 struct partial_die_info *part_die
18291 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18292
18293 /* We'll save this DIE so link it in. */
18294 part_die->die_parent = parent_die;
18295 part_die->die_sibling = NULL;
18296 part_die->die_child = NULL;
18297
18298 if (last_die && last_die == parent_die)
18299 last_die->die_child = part_die;
18300 else if (last_die)
18301 last_die->die_sibling = part_die;
18302
18303 last_die = part_die;
18304
18305 if (first_die == NULL)
18306 first_die = part_die;
18307
18308 /* Maybe add the DIE to the hash table. Not all DIEs that we
18309 find interesting need to be in the hash table, because we
18310 also have the parent/sibling/child chains; only those that we
18311 might refer to by offset later during partial symbol reading.
18312
18313 For now this means things that might have be the target of a
18314 DW_AT_specification, DW_AT_abstract_origin, or
18315 DW_AT_extension. DW_AT_extension will refer only to
18316 namespaces; DW_AT_abstract_origin refers to functions (and
18317 many things under the function DIE, but we do not recurse
18318 into function DIEs during partial symbol reading) and
18319 possibly variables as well; DW_AT_specification refers to
18320 declarations. Declarations ought to have the DW_AT_declaration
18321 flag. It happens that GCC forgets to put it in sometimes, but
18322 only for functions, not for types.
18323
18324 Adding more things than necessary to the hash table is harmless
18325 except for the performance cost. Adding too few will result in
18326 wasted time in find_partial_die, when we reread the compilation
18327 unit with load_all_dies set. */
18328
18329 if (load_all
18330 || abbrev->tag == DW_TAG_constant
18331 || abbrev->tag == DW_TAG_subprogram
18332 || abbrev->tag == DW_TAG_variable
18333 || abbrev->tag == DW_TAG_namespace
18334 || part_die->is_declaration)
18335 {
18336 void **slot;
18337
18338 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18339 to_underlying (part_die->sect_off),
18340 INSERT);
18341 *slot = part_die;
18342 }
18343
18344 /* For some DIEs we want to follow their children (if any). For C
18345 we have no reason to follow the children of structures; for other
18346 languages we have to, so that we can get at method physnames
18347 to infer fully qualified class names, for DW_AT_specification,
18348 and for C++ template arguments. For C++, we also look one level
18349 inside functions to find template arguments (if the name of the
18350 function does not already contain the template arguments).
18351
18352 For Ada and Fortran, we need to scan the children of subprograms
18353 and lexical blocks as well because these languages allow the
18354 definition of nested entities that could be interesting for the
18355 debugger, such as nested subprograms for instance. */
18356 if (last_die->has_children
18357 && (load_all
18358 || last_die->tag == DW_TAG_namespace
18359 || last_die->tag == DW_TAG_module
18360 || last_die->tag == DW_TAG_enumeration_type
18361 || (cu->language == language_cplus
18362 && last_die->tag == DW_TAG_subprogram
18363 && (last_die->name == NULL
18364 || strchr (last_die->name, '<') == NULL))
18365 || (cu->language != language_c
18366 && (last_die->tag == DW_TAG_class_type
18367 || last_die->tag == DW_TAG_interface_type
18368 || last_die->tag == DW_TAG_structure_type
18369 || last_die->tag == DW_TAG_union_type))
18370 || ((cu->language == language_ada
18371 || cu->language == language_fortran)
18372 && (last_die->tag == DW_TAG_subprogram
18373 || last_die->tag == DW_TAG_lexical_block))))
18374 {
18375 nesting_level++;
18376 parent_die = last_die;
18377 continue;
18378 }
18379
18380 /* Otherwise we skip to the next sibling, if any. */
18381 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18382
18383 /* Back to the top, do it again. */
18384 }
18385 }
18386
18387 partial_die_info::partial_die_info (sect_offset sect_off_,
18388 struct abbrev_info *abbrev)
18389 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18390 {
18391 }
18392
18393 /* Read a minimal amount of information into the minimal die structure.
18394 INFO_PTR should point just after the initial uleb128 of a DIE. */
18395
18396 const gdb_byte *
18397 partial_die_info::read (const struct die_reader_specs *reader,
18398 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18399 {
18400 struct dwarf2_cu *cu = reader->cu;
18401 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18402 unsigned int i;
18403 int has_low_pc_attr = 0;
18404 int has_high_pc_attr = 0;
18405 int high_pc_relative = 0;
18406
18407 for (i = 0; i < abbrev.num_attrs; ++i)
18408 {
18409 attribute attr;
18410 bool need_reprocess;
18411 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18412 info_ptr, &need_reprocess);
18413 /* String and address offsets that need to do the reprocessing have
18414 already been read at this point, so there is no need to wait until
18415 the loop terminates to do the reprocessing. */
18416 if (need_reprocess)
18417 read_attribute_reprocess (reader, &attr);
18418 /* Store the data if it is of an attribute we want to keep in a
18419 partial symbol table. */
18420 switch (attr.name)
18421 {
18422 case DW_AT_name:
18423 switch (tag)
18424 {
18425 case DW_TAG_compile_unit:
18426 case DW_TAG_partial_unit:
18427 case DW_TAG_type_unit:
18428 /* Compilation units have a DW_AT_name that is a filename, not
18429 a source language identifier. */
18430 case DW_TAG_enumeration_type:
18431 case DW_TAG_enumerator:
18432 /* These tags always have simple identifiers already; no need
18433 to canonicalize them. */
18434 name = DW_STRING (&attr);
18435 break;
18436 default:
18437 {
18438 struct objfile *objfile = dwarf2_per_objfile->objfile;
18439
18440 name
18441 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18442 }
18443 break;
18444 }
18445 break;
18446 case DW_AT_linkage_name:
18447 case DW_AT_MIPS_linkage_name:
18448 /* Note that both forms of linkage name might appear. We
18449 assume they will be the same, and we only store the last
18450 one we see. */
18451 linkage_name = attr.value_as_string ();
18452 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18453 See https://github.com/rust-lang/rust/issues/32925. */
18454 if (cu->language == language_rust && linkage_name != NULL
18455 && strchr (linkage_name, '{') != NULL)
18456 linkage_name = NULL;
18457 break;
18458 case DW_AT_low_pc:
18459 has_low_pc_attr = 1;
18460 lowpc = attr.value_as_address ();
18461 break;
18462 case DW_AT_high_pc:
18463 has_high_pc_attr = 1;
18464 highpc = attr.value_as_address ();
18465 if (cu->header.version >= 4 && attr.form_is_constant ())
18466 high_pc_relative = 1;
18467 break;
18468 case DW_AT_location:
18469 /* Support the .debug_loc offsets. */
18470 if (attr.form_is_block ())
18471 {
18472 d.locdesc = DW_BLOCK (&attr);
18473 }
18474 else if (attr.form_is_section_offset ())
18475 {
18476 dwarf2_complex_location_expr_complaint ();
18477 }
18478 else
18479 {
18480 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18481 "partial symbol information");
18482 }
18483 break;
18484 case DW_AT_external:
18485 is_external = DW_UNSND (&attr);
18486 break;
18487 case DW_AT_declaration:
18488 is_declaration = DW_UNSND (&attr);
18489 break;
18490 case DW_AT_type:
18491 has_type = 1;
18492 break;
18493 case DW_AT_abstract_origin:
18494 case DW_AT_specification:
18495 case DW_AT_extension:
18496 has_specification = 1;
18497 spec_offset = attr.get_ref_die_offset ();
18498 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18499 || cu->per_cu->is_dwz);
18500 break;
18501 case DW_AT_sibling:
18502 /* Ignore absolute siblings, they might point outside of
18503 the current compile unit. */
18504 if (attr.form == DW_FORM_ref_addr)
18505 complaint (_("ignoring absolute DW_AT_sibling"));
18506 else
18507 {
18508 const gdb_byte *buffer = reader->buffer;
18509 sect_offset off = attr.get_ref_die_offset ();
18510 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18511
18512 if (sibling_ptr < info_ptr)
18513 complaint (_("DW_AT_sibling points backwards"));
18514 else if (sibling_ptr > reader->buffer_end)
18515 reader->die_section->overflow_complaint ();
18516 else
18517 sibling = sibling_ptr;
18518 }
18519 break;
18520 case DW_AT_byte_size:
18521 has_byte_size = 1;
18522 break;
18523 case DW_AT_const_value:
18524 has_const_value = 1;
18525 break;
18526 case DW_AT_calling_convention:
18527 /* DWARF doesn't provide a way to identify a program's source-level
18528 entry point. DW_AT_calling_convention attributes are only meant
18529 to describe functions' calling conventions.
18530
18531 However, because it's a necessary piece of information in
18532 Fortran, and before DWARF 4 DW_CC_program was the only
18533 piece of debugging information whose definition refers to
18534 a 'main program' at all, several compilers marked Fortran
18535 main programs with DW_CC_program --- even when those
18536 functions use the standard calling conventions.
18537
18538 Although DWARF now specifies a way to provide this
18539 information, we support this practice for backward
18540 compatibility. */
18541 if (DW_UNSND (&attr) == DW_CC_program
18542 && cu->language == language_fortran)
18543 main_subprogram = 1;
18544 break;
18545 case DW_AT_inline:
18546 if (DW_UNSND (&attr) == DW_INL_inlined
18547 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18548 may_be_inlined = 1;
18549 break;
18550
18551 case DW_AT_import:
18552 if (tag == DW_TAG_imported_unit)
18553 {
18554 d.sect_off = attr.get_ref_die_offset ();
18555 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18556 || cu->per_cu->is_dwz);
18557 }
18558 break;
18559
18560 case DW_AT_main_subprogram:
18561 main_subprogram = DW_UNSND (&attr);
18562 break;
18563
18564 case DW_AT_ranges:
18565 {
18566 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18567 but that requires a full DIE, so instead we just
18568 reimplement it. */
18569 int need_ranges_base = tag != DW_TAG_compile_unit;
18570 unsigned int ranges_offset = (DW_UNSND (&attr)
18571 + (need_ranges_base
18572 ? cu->ranges_base
18573 : 0));
18574
18575 /* Value of the DW_AT_ranges attribute is the offset in the
18576 .debug_ranges section. */
18577 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18578 nullptr))
18579 has_pc_info = 1;
18580 }
18581 break;
18582
18583 default:
18584 break;
18585 }
18586 }
18587
18588 /* For Ada, if both the name and the linkage name appear, we prefer
18589 the latter. This lets "catch exception" work better, regardless
18590 of the order in which the name and linkage name were emitted.
18591 Really, though, this is just a workaround for the fact that gdb
18592 doesn't store both the name and the linkage name. */
18593 if (cu->language == language_ada && linkage_name != nullptr)
18594 name = linkage_name;
18595
18596 if (high_pc_relative)
18597 highpc += lowpc;
18598
18599 if (has_low_pc_attr && has_high_pc_attr)
18600 {
18601 /* When using the GNU linker, .gnu.linkonce. sections are used to
18602 eliminate duplicate copies of functions and vtables and such.
18603 The linker will arbitrarily choose one and discard the others.
18604 The AT_*_pc values for such functions refer to local labels in
18605 these sections. If the section from that file was discarded, the
18606 labels are not in the output, so the relocs get a value of 0.
18607 If this is a discarded function, mark the pc bounds as invalid,
18608 so that GDB will ignore it. */
18609 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18610 {
18611 struct objfile *objfile = dwarf2_per_objfile->objfile;
18612 struct gdbarch *gdbarch = objfile->arch ();
18613
18614 complaint (_("DW_AT_low_pc %s is zero "
18615 "for DIE at %s [in module %s]"),
18616 paddress (gdbarch, lowpc),
18617 sect_offset_str (sect_off),
18618 objfile_name (objfile));
18619 }
18620 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18621 else if (lowpc >= highpc)
18622 {
18623 struct objfile *objfile = dwarf2_per_objfile->objfile;
18624 struct gdbarch *gdbarch = objfile->arch ();
18625
18626 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18627 "for DIE at %s [in module %s]"),
18628 paddress (gdbarch, lowpc),
18629 paddress (gdbarch, highpc),
18630 sect_offset_str (sect_off),
18631 objfile_name (objfile));
18632 }
18633 else
18634 has_pc_info = 1;
18635 }
18636
18637 return info_ptr;
18638 }
18639
18640 /* Find a cached partial DIE at OFFSET in CU. */
18641
18642 struct partial_die_info *
18643 dwarf2_cu::find_partial_die (sect_offset sect_off)
18644 {
18645 struct partial_die_info *lookup_die = NULL;
18646 struct partial_die_info part_die (sect_off);
18647
18648 lookup_die = ((struct partial_die_info *)
18649 htab_find_with_hash (partial_dies, &part_die,
18650 to_underlying (sect_off)));
18651
18652 return lookup_die;
18653 }
18654
18655 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18656 except in the case of .debug_types DIEs which do not reference
18657 outside their CU (they do however referencing other types via
18658 DW_FORM_ref_sig8). */
18659
18660 static const struct cu_partial_die_info
18661 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18662 {
18663 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18664 struct objfile *objfile = dwarf2_per_objfile->objfile;
18665 struct dwarf2_per_cu_data *per_cu = NULL;
18666 struct partial_die_info *pd = NULL;
18667
18668 if (offset_in_dwz == cu->per_cu->is_dwz
18669 && cu->header.offset_in_cu_p (sect_off))
18670 {
18671 pd = cu->find_partial_die (sect_off);
18672 if (pd != NULL)
18673 return { cu, pd };
18674 /* We missed recording what we needed.
18675 Load all dies and try again. */
18676 per_cu = cu->per_cu;
18677 }
18678 else
18679 {
18680 /* TUs don't reference other CUs/TUs (except via type signatures). */
18681 if (cu->per_cu->is_debug_types)
18682 {
18683 error (_("Dwarf Error: Type Unit at offset %s contains"
18684 " external reference to offset %s [in module %s].\n"),
18685 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18686 bfd_get_filename (objfile->obfd));
18687 }
18688 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18689 dwarf2_per_objfile);
18690
18691 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18692 load_partial_comp_unit (per_cu, cu->per_objfile);
18693
18694 per_cu->cu->last_used = 0;
18695 pd = per_cu->cu->find_partial_die (sect_off);
18696 }
18697
18698 /* If we didn't find it, and not all dies have been loaded,
18699 load them all and try again. */
18700
18701 if (pd == NULL && per_cu->load_all_dies == 0)
18702 {
18703 per_cu->load_all_dies = 1;
18704
18705 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18706 THIS_CU->cu may already be in use. So we can't just free it and
18707 replace its DIEs with the ones we read in. Instead, we leave those
18708 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18709 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18710 set. */
18711 load_partial_comp_unit (per_cu, cu->per_objfile);
18712
18713 pd = per_cu->cu->find_partial_die (sect_off);
18714 }
18715
18716 if (pd == NULL)
18717 internal_error (__FILE__, __LINE__,
18718 _("could not find partial DIE %s "
18719 "in cache [from module %s]\n"),
18720 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18721 return { per_cu->cu, pd };
18722 }
18723
18724 /* See if we can figure out if the class lives in a namespace. We do
18725 this by looking for a member function; its demangled name will
18726 contain namespace info, if there is any. */
18727
18728 static void
18729 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18730 struct dwarf2_cu *cu)
18731 {
18732 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18733 what template types look like, because the demangler
18734 frequently doesn't give the same name as the debug info. We
18735 could fix this by only using the demangled name to get the
18736 prefix (but see comment in read_structure_type). */
18737
18738 struct partial_die_info *real_pdi;
18739 struct partial_die_info *child_pdi;
18740
18741 /* If this DIE (this DIE's specification, if any) has a parent, then
18742 we should not do this. We'll prepend the parent's fully qualified
18743 name when we create the partial symbol. */
18744
18745 real_pdi = struct_pdi;
18746 while (real_pdi->has_specification)
18747 {
18748 auto res = find_partial_die (real_pdi->spec_offset,
18749 real_pdi->spec_is_dwz, cu);
18750 real_pdi = res.pdi;
18751 cu = res.cu;
18752 }
18753
18754 if (real_pdi->die_parent != NULL)
18755 return;
18756
18757 for (child_pdi = struct_pdi->die_child;
18758 child_pdi != NULL;
18759 child_pdi = child_pdi->die_sibling)
18760 {
18761 if (child_pdi->tag == DW_TAG_subprogram
18762 && child_pdi->linkage_name != NULL)
18763 {
18764 gdb::unique_xmalloc_ptr<char> actual_class_name
18765 (language_class_name_from_physname (cu->language_defn,
18766 child_pdi->linkage_name));
18767 if (actual_class_name != NULL)
18768 {
18769 struct objfile *objfile = cu->per_objfile->objfile;
18770 struct_pdi->name = objfile->intern (actual_class_name.get ());
18771 }
18772 break;
18773 }
18774 }
18775 }
18776
18777 /* Return true if a DIE with TAG may have the DW_AT_const_value
18778 attribute. */
18779
18780 static bool
18781 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18782 {
18783 switch (tag)
18784 {
18785 case DW_TAG_constant:
18786 case DW_TAG_enumerator:
18787 case DW_TAG_formal_parameter:
18788 case DW_TAG_template_value_param:
18789 case DW_TAG_variable:
18790 return true;
18791 }
18792
18793 return false;
18794 }
18795
18796 void
18797 partial_die_info::fixup (struct dwarf2_cu *cu)
18798 {
18799 /* Once we've fixed up a die, there's no point in doing so again.
18800 This also avoids a memory leak if we were to call
18801 guess_partial_die_structure_name multiple times. */
18802 if (fixup_called)
18803 return;
18804
18805 /* If we found a reference attribute and the DIE has no name, try
18806 to find a name in the referred to DIE. */
18807
18808 if (name == NULL && has_specification)
18809 {
18810 struct partial_die_info *spec_die;
18811
18812 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18813 spec_die = res.pdi;
18814 cu = res.cu;
18815
18816 spec_die->fixup (cu);
18817
18818 if (spec_die->name)
18819 {
18820 name = spec_die->name;
18821
18822 /* Copy DW_AT_external attribute if it is set. */
18823 if (spec_die->is_external)
18824 is_external = spec_die->is_external;
18825 }
18826 }
18827
18828 if (!has_const_value && has_specification
18829 && can_have_DW_AT_const_value_p (tag))
18830 {
18831 struct partial_die_info *spec_die;
18832
18833 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18834 spec_die = res.pdi;
18835 cu = res.cu;
18836
18837 spec_die->fixup (cu);
18838
18839 if (spec_die->has_const_value)
18840 {
18841 /* Copy DW_AT_const_value attribute if it is set. */
18842 has_const_value = spec_die->has_const_value;
18843 }
18844 }
18845
18846 /* Set default names for some unnamed DIEs. */
18847
18848 if (name == NULL && tag == DW_TAG_namespace)
18849 name = CP_ANONYMOUS_NAMESPACE_STR;
18850
18851 /* If there is no parent die to provide a namespace, and there are
18852 children, see if we can determine the namespace from their linkage
18853 name. */
18854 if (cu->language == language_cplus
18855 && !cu->per_objfile->per_bfd->types.empty ()
18856 && die_parent == NULL
18857 && has_children
18858 && (tag == DW_TAG_class_type
18859 || tag == DW_TAG_structure_type
18860 || tag == DW_TAG_union_type))
18861 guess_partial_die_structure_name (this, cu);
18862
18863 /* GCC might emit a nameless struct or union that has a linkage
18864 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18865 if (name == NULL
18866 && (tag == DW_TAG_class_type
18867 || tag == DW_TAG_interface_type
18868 || tag == DW_TAG_structure_type
18869 || tag == DW_TAG_union_type)
18870 && linkage_name != NULL)
18871 {
18872 gdb::unique_xmalloc_ptr<char> demangled
18873 (gdb_demangle (linkage_name, DMGL_TYPES));
18874 if (demangled != nullptr)
18875 {
18876 const char *base;
18877
18878 /* Strip any leading namespaces/classes, keep only the base name.
18879 DW_AT_name for named DIEs does not contain the prefixes. */
18880 base = strrchr (demangled.get (), ':');
18881 if (base && base > demangled.get () && base[-1] == ':')
18882 base++;
18883 else
18884 base = demangled.get ();
18885
18886 struct objfile *objfile = cu->per_objfile->objfile;
18887 name = objfile->intern (base);
18888 }
18889 }
18890
18891 fixup_called = 1;
18892 }
18893
18894 /* Read the .debug_loclists header contents from the given SECTION in the
18895 HEADER. */
18896 static void
18897 read_loclist_header (struct loclist_header *header,
18898 struct dwarf2_section_info *section)
18899 {
18900 unsigned int bytes_read;
18901 bfd *abfd = section->get_bfd_owner ();
18902 const gdb_byte *info_ptr = section->buffer;
18903 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18904 info_ptr += bytes_read;
18905 header->version = read_2_bytes (abfd, info_ptr);
18906 info_ptr += 2;
18907 header->addr_size = read_1_byte (abfd, info_ptr);
18908 info_ptr += 1;
18909 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18910 info_ptr += 1;
18911 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18912 }
18913
18914 /* Return the DW_AT_loclists_base value for the CU. */
18915 static ULONGEST
18916 lookup_loclist_base (struct dwarf2_cu *cu)
18917 {
18918 /* For the .dwo unit, the loclist_base points to the first offset following
18919 the header. The header consists of the following entities-
18920 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18921 bit format)
18922 2. version (2 bytes)
18923 3. address size (1 byte)
18924 4. segment selector size (1 byte)
18925 5. offset entry count (4 bytes)
18926 These sizes are derived as per the DWARFv5 standard. */
18927 if (cu->dwo_unit != nullptr)
18928 {
18929 if (cu->header.initial_length_size == 4)
18930 return LOCLIST_HEADER_SIZE32;
18931 return LOCLIST_HEADER_SIZE64;
18932 }
18933 return cu->loclist_base;
18934 }
18935
18936 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18937 array of offsets in the .debug_loclists section. */
18938 static CORE_ADDR
18939 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18940 {
18941 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18942 struct objfile *objfile = dwarf2_per_objfile->objfile;
18943 bfd *abfd = objfile->obfd;
18944 ULONGEST loclist_base = lookup_loclist_base (cu);
18945 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18946
18947 section->read (objfile);
18948 if (section->buffer == NULL)
18949 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18950 "section [in module %s]"), objfile_name (objfile));
18951 struct loclist_header header;
18952 read_loclist_header (&header, section);
18953 if (loclist_index >= header.offset_entry_count)
18954 complaint (_("DW_FORM_loclistx pointing outside of "
18955 ".debug_loclists offset array [in module %s]"),
18956 objfile_name (objfile));
18957 if (loclist_base + loclist_index * cu->header.offset_size
18958 >= section->size)
18959 complaint (_("DW_FORM_loclistx pointing outside of "
18960 ".debug_loclists section [in module %s]"),
18961 objfile_name (objfile));
18962 const gdb_byte *info_ptr
18963 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18964
18965 if (cu->header.offset_size == 4)
18966 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18967 else
18968 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18969 }
18970
18971 /* Process the attributes that had to be skipped in the first round. These
18972 attributes are the ones that need str_offsets_base or addr_base attributes.
18973 They could not have been processed in the first round, because at the time
18974 the values of str_offsets_base or addr_base may not have been known. */
18975 static void
18976 read_attribute_reprocess (const struct die_reader_specs *reader,
18977 struct attribute *attr)
18978 {
18979 struct dwarf2_cu *cu = reader->cu;
18980 switch (attr->form)
18981 {
18982 case DW_FORM_addrx:
18983 case DW_FORM_GNU_addr_index:
18984 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18985 break;
18986 case DW_FORM_loclistx:
18987 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18988 break;
18989 case DW_FORM_strx:
18990 case DW_FORM_strx1:
18991 case DW_FORM_strx2:
18992 case DW_FORM_strx3:
18993 case DW_FORM_strx4:
18994 case DW_FORM_GNU_str_index:
18995 {
18996 unsigned int str_index = DW_UNSND (attr);
18997 if (reader->dwo_file != NULL)
18998 {
18999 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19000 DW_STRING_IS_CANONICAL (attr) = 0;
19001 }
19002 else
19003 {
19004 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19005 DW_STRING_IS_CANONICAL (attr) = 0;
19006 }
19007 break;
19008 }
19009 default:
19010 gdb_assert_not_reached (_("Unexpected DWARF form."));
19011 }
19012 }
19013
19014 /* Read an attribute value described by an attribute form. */
19015
19016 static const gdb_byte *
19017 read_attribute_value (const struct die_reader_specs *reader,
19018 struct attribute *attr, unsigned form,
19019 LONGEST implicit_const, const gdb_byte *info_ptr,
19020 bool *need_reprocess)
19021 {
19022 struct dwarf2_cu *cu = reader->cu;
19023 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19024 struct objfile *objfile = dwarf2_per_objfile->objfile;
19025 bfd *abfd = reader->abfd;
19026 struct comp_unit_head *cu_header = &cu->header;
19027 unsigned int bytes_read;
19028 struct dwarf_block *blk;
19029 *need_reprocess = false;
19030
19031 attr->form = (enum dwarf_form) form;
19032 switch (form)
19033 {
19034 case DW_FORM_ref_addr:
19035 if (cu->header.version == 2)
19036 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19037 &bytes_read);
19038 else
19039 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19040 &bytes_read);
19041 info_ptr += bytes_read;
19042 break;
19043 case DW_FORM_GNU_ref_alt:
19044 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19045 info_ptr += bytes_read;
19046 break;
19047 case DW_FORM_addr:
19048 {
19049 struct gdbarch *gdbarch = objfile->arch ();
19050 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19051 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19052 info_ptr += bytes_read;
19053 }
19054 break;
19055 case DW_FORM_block2:
19056 blk = dwarf_alloc_block (cu);
19057 blk->size = read_2_bytes (abfd, info_ptr);
19058 info_ptr += 2;
19059 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19060 info_ptr += blk->size;
19061 DW_BLOCK (attr) = blk;
19062 break;
19063 case DW_FORM_block4:
19064 blk = dwarf_alloc_block (cu);
19065 blk->size = read_4_bytes (abfd, info_ptr);
19066 info_ptr += 4;
19067 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19068 info_ptr += blk->size;
19069 DW_BLOCK (attr) = blk;
19070 break;
19071 case DW_FORM_data2:
19072 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19073 info_ptr += 2;
19074 break;
19075 case DW_FORM_data4:
19076 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19077 info_ptr += 4;
19078 break;
19079 case DW_FORM_data8:
19080 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19081 info_ptr += 8;
19082 break;
19083 case DW_FORM_data16:
19084 blk = dwarf_alloc_block (cu);
19085 blk->size = 16;
19086 blk->data = read_n_bytes (abfd, info_ptr, 16);
19087 info_ptr += 16;
19088 DW_BLOCK (attr) = blk;
19089 break;
19090 case DW_FORM_sec_offset:
19091 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19092 info_ptr += bytes_read;
19093 break;
19094 case DW_FORM_loclistx:
19095 {
19096 *need_reprocess = true;
19097 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19098 info_ptr += bytes_read;
19099 }
19100 break;
19101 case DW_FORM_string:
19102 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19103 DW_STRING_IS_CANONICAL (attr) = 0;
19104 info_ptr += bytes_read;
19105 break;
19106 case DW_FORM_strp:
19107 if (!cu->per_cu->is_dwz)
19108 {
19109 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19110 abfd, info_ptr, cu_header,
19111 &bytes_read);
19112 DW_STRING_IS_CANONICAL (attr) = 0;
19113 info_ptr += bytes_read;
19114 break;
19115 }
19116 /* FALLTHROUGH */
19117 case DW_FORM_line_strp:
19118 if (!cu->per_cu->is_dwz)
19119 {
19120 DW_STRING (attr)
19121 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19122 &bytes_read);
19123 DW_STRING_IS_CANONICAL (attr) = 0;
19124 info_ptr += bytes_read;
19125 break;
19126 }
19127 /* FALLTHROUGH */
19128 case DW_FORM_GNU_strp_alt:
19129 {
19130 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19131 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19132 &bytes_read);
19133
19134 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19135 DW_STRING_IS_CANONICAL (attr) = 0;
19136 info_ptr += bytes_read;
19137 }
19138 break;
19139 case DW_FORM_exprloc:
19140 case DW_FORM_block:
19141 blk = dwarf_alloc_block (cu);
19142 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19143 info_ptr += bytes_read;
19144 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19145 info_ptr += blk->size;
19146 DW_BLOCK (attr) = blk;
19147 break;
19148 case DW_FORM_block1:
19149 blk = dwarf_alloc_block (cu);
19150 blk->size = read_1_byte (abfd, info_ptr);
19151 info_ptr += 1;
19152 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19153 info_ptr += blk->size;
19154 DW_BLOCK (attr) = blk;
19155 break;
19156 case DW_FORM_data1:
19157 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19158 info_ptr += 1;
19159 break;
19160 case DW_FORM_flag:
19161 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19162 info_ptr += 1;
19163 break;
19164 case DW_FORM_flag_present:
19165 DW_UNSND (attr) = 1;
19166 break;
19167 case DW_FORM_sdata:
19168 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19169 info_ptr += bytes_read;
19170 break;
19171 case DW_FORM_udata:
19172 case DW_FORM_rnglistx:
19173 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19174 info_ptr += bytes_read;
19175 break;
19176 case DW_FORM_ref1:
19177 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19178 + read_1_byte (abfd, info_ptr));
19179 info_ptr += 1;
19180 break;
19181 case DW_FORM_ref2:
19182 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19183 + read_2_bytes (abfd, info_ptr));
19184 info_ptr += 2;
19185 break;
19186 case DW_FORM_ref4:
19187 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19188 + read_4_bytes (abfd, info_ptr));
19189 info_ptr += 4;
19190 break;
19191 case DW_FORM_ref8:
19192 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19193 + read_8_bytes (abfd, info_ptr));
19194 info_ptr += 8;
19195 break;
19196 case DW_FORM_ref_sig8:
19197 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19198 info_ptr += 8;
19199 break;
19200 case DW_FORM_ref_udata:
19201 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19202 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19203 info_ptr += bytes_read;
19204 break;
19205 case DW_FORM_indirect:
19206 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19207 info_ptr += bytes_read;
19208 if (form == DW_FORM_implicit_const)
19209 {
19210 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19211 info_ptr += bytes_read;
19212 }
19213 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19214 info_ptr, need_reprocess);
19215 break;
19216 case DW_FORM_implicit_const:
19217 DW_SND (attr) = implicit_const;
19218 break;
19219 case DW_FORM_addrx:
19220 case DW_FORM_GNU_addr_index:
19221 *need_reprocess = true;
19222 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19223 info_ptr += bytes_read;
19224 break;
19225 case DW_FORM_strx:
19226 case DW_FORM_strx1:
19227 case DW_FORM_strx2:
19228 case DW_FORM_strx3:
19229 case DW_FORM_strx4:
19230 case DW_FORM_GNU_str_index:
19231 {
19232 ULONGEST str_index;
19233 if (form == DW_FORM_strx1)
19234 {
19235 str_index = read_1_byte (abfd, info_ptr);
19236 info_ptr += 1;
19237 }
19238 else if (form == DW_FORM_strx2)
19239 {
19240 str_index = read_2_bytes (abfd, info_ptr);
19241 info_ptr += 2;
19242 }
19243 else if (form == DW_FORM_strx3)
19244 {
19245 str_index = read_3_bytes (abfd, info_ptr);
19246 info_ptr += 3;
19247 }
19248 else if (form == DW_FORM_strx4)
19249 {
19250 str_index = read_4_bytes (abfd, info_ptr);
19251 info_ptr += 4;
19252 }
19253 else
19254 {
19255 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19256 info_ptr += bytes_read;
19257 }
19258 *need_reprocess = true;
19259 DW_UNSND (attr) = str_index;
19260 }
19261 break;
19262 default:
19263 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19264 dwarf_form_name (form),
19265 bfd_get_filename (abfd));
19266 }
19267
19268 /* Super hack. */
19269 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19270 attr->form = DW_FORM_GNU_ref_alt;
19271
19272 /* We have seen instances where the compiler tried to emit a byte
19273 size attribute of -1 which ended up being encoded as an unsigned
19274 0xffffffff. Although 0xffffffff is technically a valid size value,
19275 an object of this size seems pretty unlikely so we can relatively
19276 safely treat these cases as if the size attribute was invalid and
19277 treat them as zero by default. */
19278 if (attr->name == DW_AT_byte_size
19279 && form == DW_FORM_data4
19280 && DW_UNSND (attr) >= 0xffffffff)
19281 {
19282 complaint
19283 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19284 hex_string (DW_UNSND (attr)));
19285 DW_UNSND (attr) = 0;
19286 }
19287
19288 return info_ptr;
19289 }
19290
19291 /* Read an attribute described by an abbreviated attribute. */
19292
19293 static const gdb_byte *
19294 read_attribute (const struct die_reader_specs *reader,
19295 struct attribute *attr, struct attr_abbrev *abbrev,
19296 const gdb_byte *info_ptr, bool *need_reprocess)
19297 {
19298 attr->name = abbrev->name;
19299 return read_attribute_value (reader, attr, abbrev->form,
19300 abbrev->implicit_const, info_ptr,
19301 need_reprocess);
19302 }
19303
19304 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19305
19306 static const char *
19307 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19308 LONGEST str_offset)
19309 {
19310 return dwarf2_per_objfile->per_bfd->str.read_string
19311 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19312 }
19313
19314 /* Return pointer to string at .debug_str offset as read from BUF.
19315 BUF is assumed to be in a compilation unit described by CU_HEADER.
19316 Return *BYTES_READ_PTR count of bytes read from BUF. */
19317
19318 static const char *
19319 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19320 const gdb_byte *buf,
19321 const struct comp_unit_head *cu_header,
19322 unsigned int *bytes_read_ptr)
19323 {
19324 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19325
19326 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19327 }
19328
19329 /* See read.h. */
19330
19331 const char *
19332 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19333 const struct comp_unit_head *cu_header,
19334 unsigned int *bytes_read_ptr)
19335 {
19336 bfd *abfd = objfile->obfd;
19337 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19338
19339 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19340 }
19341
19342 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19343 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19344 ADDR_SIZE is the size of addresses from the CU header. */
19345
19346 static CORE_ADDR
19347 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19348 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19349 int addr_size)
19350 {
19351 struct objfile *objfile = dwarf2_per_objfile->objfile;
19352 bfd *abfd = objfile->obfd;
19353 const gdb_byte *info_ptr;
19354 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19355
19356 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19357 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19358 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19359 objfile_name (objfile));
19360 if (addr_base_or_zero + addr_index * addr_size
19361 >= dwarf2_per_objfile->per_bfd->addr.size)
19362 error (_("DW_FORM_addr_index pointing outside of "
19363 ".debug_addr section [in module %s]"),
19364 objfile_name (objfile));
19365 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19366 + addr_base_or_zero + addr_index * addr_size);
19367 if (addr_size == 4)
19368 return bfd_get_32 (abfd, info_ptr);
19369 else
19370 return bfd_get_64 (abfd, info_ptr);
19371 }
19372
19373 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19374
19375 static CORE_ADDR
19376 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19377 {
19378 return read_addr_index_1 (cu->per_objfile, addr_index,
19379 cu->addr_base, cu->header.addr_size);
19380 }
19381
19382 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19383
19384 static CORE_ADDR
19385 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19386 unsigned int *bytes_read)
19387 {
19388 bfd *abfd = cu->per_objfile->objfile->obfd;
19389 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19390
19391 return read_addr_index (cu, addr_index);
19392 }
19393
19394 /* See read.h. */
19395
19396 CORE_ADDR
19397 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
19398 {
19399 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19400 struct dwarf2_cu *cu = per_cu->cu;
19401 gdb::optional<ULONGEST> addr_base;
19402 int addr_size;
19403
19404 /* We need addr_base and addr_size.
19405 If we don't have PER_CU->cu, we have to get it.
19406 Nasty, but the alternative is storing the needed info in PER_CU,
19407 which at this point doesn't seem justified: it's not clear how frequently
19408 it would get used and it would increase the size of every PER_CU.
19409 Entry points like dwarf2_per_cu_addr_size do a similar thing
19410 so we're not in uncharted territory here.
19411 Alas we need to be a bit more complicated as addr_base is contained
19412 in the DIE.
19413
19414 We don't need to read the entire CU(/TU).
19415 We just need the header and top level die.
19416
19417 IWBN to use the aging mechanism to let us lazily later discard the CU.
19418 For now we skip this optimization. */
19419
19420 if (cu != NULL)
19421 {
19422 addr_base = cu->addr_base;
19423 addr_size = cu->header.addr_size;
19424 }
19425 else
19426 {
19427 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
19428 addr_base = reader.cu->addr_base;
19429 addr_size = reader.cu->header.addr_size;
19430 }
19431
19432 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19433 addr_size);
19434 }
19435
19436 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19437 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19438 DWO file. */
19439
19440 static const char *
19441 read_str_index (struct dwarf2_cu *cu,
19442 struct dwarf2_section_info *str_section,
19443 struct dwarf2_section_info *str_offsets_section,
19444 ULONGEST str_offsets_base, ULONGEST str_index)
19445 {
19446 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19447 struct objfile *objfile = dwarf2_per_objfile->objfile;
19448 const char *objf_name = objfile_name (objfile);
19449 bfd *abfd = objfile->obfd;
19450 const gdb_byte *info_ptr;
19451 ULONGEST str_offset;
19452 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19453
19454 str_section->read (objfile);
19455 str_offsets_section->read (objfile);
19456 if (str_section->buffer == NULL)
19457 error (_("%s used without %s section"
19458 " in CU at offset %s [in module %s]"),
19459 form_name, str_section->get_name (),
19460 sect_offset_str (cu->header.sect_off), objf_name);
19461 if (str_offsets_section->buffer == NULL)
19462 error (_("%s used without %s section"
19463 " in CU at offset %s [in module %s]"),
19464 form_name, str_section->get_name (),
19465 sect_offset_str (cu->header.sect_off), objf_name);
19466 info_ptr = (str_offsets_section->buffer
19467 + str_offsets_base
19468 + str_index * cu->header.offset_size);
19469 if (cu->header.offset_size == 4)
19470 str_offset = bfd_get_32 (abfd, info_ptr);
19471 else
19472 str_offset = bfd_get_64 (abfd, info_ptr);
19473 if (str_offset >= str_section->size)
19474 error (_("Offset from %s pointing outside of"
19475 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19476 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19477 return (const char *) (str_section->buffer + str_offset);
19478 }
19479
19480 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19481
19482 static const char *
19483 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19484 {
19485 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19486 ? reader->cu->header.addr_size : 0;
19487 return read_str_index (reader->cu,
19488 &reader->dwo_file->sections.str,
19489 &reader->dwo_file->sections.str_offsets,
19490 str_offsets_base, str_index);
19491 }
19492
19493 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19494
19495 static const char *
19496 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19497 {
19498 struct objfile *objfile = cu->per_objfile->objfile;
19499 const char *objf_name = objfile_name (objfile);
19500 static const char form_name[] = "DW_FORM_GNU_str_index";
19501 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19502
19503 if (!cu->str_offsets_base.has_value ())
19504 error (_("%s used in Fission stub without %s"
19505 " in CU at offset 0x%lx [in module %s]"),
19506 form_name, str_offsets_attr_name,
19507 (long) cu->header.offset_size, objf_name);
19508
19509 return read_str_index (cu,
19510 &cu->per_objfile->per_bfd->str,
19511 &cu->per_objfile->per_bfd->str_offsets,
19512 *cu->str_offsets_base, str_index);
19513 }
19514
19515 /* Return the length of an LEB128 number in BUF. */
19516
19517 static int
19518 leb128_size (const gdb_byte *buf)
19519 {
19520 const gdb_byte *begin = buf;
19521 gdb_byte byte;
19522
19523 while (1)
19524 {
19525 byte = *buf++;
19526 if ((byte & 128) == 0)
19527 return buf - begin;
19528 }
19529 }
19530
19531 static void
19532 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19533 {
19534 switch (lang)
19535 {
19536 case DW_LANG_C89:
19537 case DW_LANG_C99:
19538 case DW_LANG_C11:
19539 case DW_LANG_C:
19540 case DW_LANG_UPC:
19541 cu->language = language_c;
19542 break;
19543 case DW_LANG_Java:
19544 case DW_LANG_C_plus_plus:
19545 case DW_LANG_C_plus_plus_11:
19546 case DW_LANG_C_plus_plus_14:
19547 cu->language = language_cplus;
19548 break;
19549 case DW_LANG_D:
19550 cu->language = language_d;
19551 break;
19552 case DW_LANG_Fortran77:
19553 case DW_LANG_Fortran90:
19554 case DW_LANG_Fortran95:
19555 case DW_LANG_Fortran03:
19556 case DW_LANG_Fortran08:
19557 cu->language = language_fortran;
19558 break;
19559 case DW_LANG_Go:
19560 cu->language = language_go;
19561 break;
19562 case DW_LANG_Mips_Assembler:
19563 cu->language = language_asm;
19564 break;
19565 case DW_LANG_Ada83:
19566 case DW_LANG_Ada95:
19567 cu->language = language_ada;
19568 break;
19569 case DW_LANG_Modula2:
19570 cu->language = language_m2;
19571 break;
19572 case DW_LANG_Pascal83:
19573 cu->language = language_pascal;
19574 break;
19575 case DW_LANG_ObjC:
19576 cu->language = language_objc;
19577 break;
19578 case DW_LANG_Rust:
19579 case DW_LANG_Rust_old:
19580 cu->language = language_rust;
19581 break;
19582 case DW_LANG_Cobol74:
19583 case DW_LANG_Cobol85:
19584 default:
19585 cu->language = language_minimal;
19586 break;
19587 }
19588 cu->language_defn = language_def (cu->language);
19589 }
19590
19591 /* Return the named attribute or NULL if not there. */
19592
19593 static struct attribute *
19594 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19595 {
19596 for (;;)
19597 {
19598 unsigned int i;
19599 struct attribute *spec = NULL;
19600
19601 for (i = 0; i < die->num_attrs; ++i)
19602 {
19603 if (die->attrs[i].name == name)
19604 return &die->attrs[i];
19605 if (die->attrs[i].name == DW_AT_specification
19606 || die->attrs[i].name == DW_AT_abstract_origin)
19607 spec = &die->attrs[i];
19608 }
19609
19610 if (!spec)
19611 break;
19612
19613 die = follow_die_ref (die, spec, &cu);
19614 }
19615
19616 return NULL;
19617 }
19618
19619 /* Return the string associated with a string-typed attribute, or NULL if it
19620 is either not found or is of an incorrect type. */
19621
19622 static const char *
19623 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19624 {
19625 struct attribute *attr;
19626 const char *str = NULL;
19627
19628 attr = dwarf2_attr (die, name, cu);
19629
19630 if (attr != NULL)
19631 {
19632 str = attr->value_as_string ();
19633 if (str == nullptr)
19634 complaint (_("string type expected for attribute %s for "
19635 "DIE at %s in module %s"),
19636 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19637 objfile_name (cu->per_objfile->objfile));
19638 }
19639
19640 return str;
19641 }
19642
19643 /* Return the dwo name or NULL if not present. If present, it is in either
19644 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19645 static const char *
19646 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19647 {
19648 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19649 if (dwo_name == nullptr)
19650 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19651 return dwo_name;
19652 }
19653
19654 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19655 and holds a non-zero value. This function should only be used for
19656 DW_FORM_flag or DW_FORM_flag_present attributes. */
19657
19658 static int
19659 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19660 {
19661 struct attribute *attr = dwarf2_attr (die, name, cu);
19662
19663 return (attr && DW_UNSND (attr));
19664 }
19665
19666 static int
19667 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19668 {
19669 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19670 which value is non-zero. However, we have to be careful with
19671 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19672 (via dwarf2_flag_true_p) follows this attribute. So we may
19673 end up accidently finding a declaration attribute that belongs
19674 to a different DIE referenced by the specification attribute,
19675 even though the given DIE does not have a declaration attribute. */
19676 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19677 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19678 }
19679
19680 /* Return the die giving the specification for DIE, if there is
19681 one. *SPEC_CU is the CU containing DIE on input, and the CU
19682 containing the return value on output. If there is no
19683 specification, but there is an abstract origin, that is
19684 returned. */
19685
19686 static struct die_info *
19687 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19688 {
19689 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19690 *spec_cu);
19691
19692 if (spec_attr == NULL)
19693 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19694
19695 if (spec_attr == NULL)
19696 return NULL;
19697 else
19698 return follow_die_ref (die, spec_attr, spec_cu);
19699 }
19700
19701 /* Stub for free_line_header to match void * callback types. */
19702
19703 static void
19704 free_line_header_voidp (void *arg)
19705 {
19706 struct line_header *lh = (struct line_header *) arg;
19707
19708 delete lh;
19709 }
19710
19711 /* A convenience function to find the proper .debug_line section for a CU. */
19712
19713 static struct dwarf2_section_info *
19714 get_debug_line_section (struct dwarf2_cu *cu)
19715 {
19716 struct dwarf2_section_info *section;
19717 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19718
19719 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19720 DWO file. */
19721 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19722 section = &cu->dwo_unit->dwo_file->sections.line;
19723 else if (cu->per_cu->is_dwz)
19724 {
19725 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19726
19727 section = &dwz->line;
19728 }
19729 else
19730 section = &dwarf2_per_objfile->per_bfd->line;
19731
19732 return section;
19733 }
19734
19735 /* Read the statement program header starting at OFFSET in
19736 .debug_line, or .debug_line.dwo. Return a pointer
19737 to a struct line_header, allocated using xmalloc.
19738 Returns NULL if there is a problem reading the header, e.g., if it
19739 has a version we don't understand.
19740
19741 NOTE: the strings in the include directory and file name tables of
19742 the returned object point into the dwarf line section buffer,
19743 and must not be freed. */
19744
19745 static line_header_up
19746 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19747 {
19748 struct dwarf2_section_info *section;
19749 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19750
19751 section = get_debug_line_section (cu);
19752 section->read (dwarf2_per_objfile->objfile);
19753 if (section->buffer == NULL)
19754 {
19755 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19756 complaint (_("missing .debug_line.dwo section"));
19757 else
19758 complaint (_("missing .debug_line section"));
19759 return 0;
19760 }
19761
19762 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19763 dwarf2_per_objfile, section,
19764 &cu->header);
19765 }
19766
19767 /* Subroutine of dwarf_decode_lines to simplify it.
19768 Return the file name of the psymtab for the given file_entry.
19769 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19770 If space for the result is malloc'd, *NAME_HOLDER will be set.
19771 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19772
19773 static const char *
19774 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19775 const dwarf2_psymtab *pst,
19776 const char *comp_dir,
19777 gdb::unique_xmalloc_ptr<char> *name_holder)
19778 {
19779 const char *include_name = fe.name;
19780 const char *include_name_to_compare = include_name;
19781 const char *pst_filename;
19782 int file_is_pst;
19783
19784 const char *dir_name = fe.include_dir (lh);
19785
19786 gdb::unique_xmalloc_ptr<char> hold_compare;
19787 if (!IS_ABSOLUTE_PATH (include_name)
19788 && (dir_name != NULL || comp_dir != NULL))
19789 {
19790 /* Avoid creating a duplicate psymtab for PST.
19791 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19792 Before we do the comparison, however, we need to account
19793 for DIR_NAME and COMP_DIR.
19794 First prepend dir_name (if non-NULL). If we still don't
19795 have an absolute path prepend comp_dir (if non-NULL).
19796 However, the directory we record in the include-file's
19797 psymtab does not contain COMP_DIR (to match the
19798 corresponding symtab(s)).
19799
19800 Example:
19801
19802 bash$ cd /tmp
19803 bash$ gcc -g ./hello.c
19804 include_name = "hello.c"
19805 dir_name = "."
19806 DW_AT_comp_dir = comp_dir = "/tmp"
19807 DW_AT_name = "./hello.c"
19808
19809 */
19810
19811 if (dir_name != NULL)
19812 {
19813 name_holder->reset (concat (dir_name, SLASH_STRING,
19814 include_name, (char *) NULL));
19815 include_name = name_holder->get ();
19816 include_name_to_compare = include_name;
19817 }
19818 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19819 {
19820 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19821 include_name, (char *) NULL));
19822 include_name_to_compare = hold_compare.get ();
19823 }
19824 }
19825
19826 pst_filename = pst->filename;
19827 gdb::unique_xmalloc_ptr<char> copied_name;
19828 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19829 {
19830 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19831 pst_filename, (char *) NULL));
19832 pst_filename = copied_name.get ();
19833 }
19834
19835 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19836
19837 if (file_is_pst)
19838 return NULL;
19839 return include_name;
19840 }
19841
19842 /* State machine to track the state of the line number program. */
19843
19844 class lnp_state_machine
19845 {
19846 public:
19847 /* Initialize a machine state for the start of a line number
19848 program. */
19849 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19850 bool record_lines_p);
19851
19852 file_entry *current_file ()
19853 {
19854 /* lh->file_names is 0-based, but the file name numbers in the
19855 statement program are 1-based. */
19856 return m_line_header->file_name_at (m_file);
19857 }
19858
19859 /* Record the line in the state machine. END_SEQUENCE is true if
19860 we're processing the end of a sequence. */
19861 void record_line (bool end_sequence);
19862
19863 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19864 nop-out rest of the lines in this sequence. */
19865 void check_line_address (struct dwarf2_cu *cu,
19866 const gdb_byte *line_ptr,
19867 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19868
19869 void handle_set_discriminator (unsigned int discriminator)
19870 {
19871 m_discriminator = discriminator;
19872 m_line_has_non_zero_discriminator |= discriminator != 0;
19873 }
19874
19875 /* Handle DW_LNE_set_address. */
19876 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19877 {
19878 m_op_index = 0;
19879 address += baseaddr;
19880 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19881 }
19882
19883 /* Handle DW_LNS_advance_pc. */
19884 void handle_advance_pc (CORE_ADDR adjust);
19885
19886 /* Handle a special opcode. */
19887 void handle_special_opcode (unsigned char op_code);
19888
19889 /* Handle DW_LNS_advance_line. */
19890 void handle_advance_line (int line_delta)
19891 {
19892 advance_line (line_delta);
19893 }
19894
19895 /* Handle DW_LNS_set_file. */
19896 void handle_set_file (file_name_index file);
19897
19898 /* Handle DW_LNS_negate_stmt. */
19899 void handle_negate_stmt ()
19900 {
19901 m_is_stmt = !m_is_stmt;
19902 }
19903
19904 /* Handle DW_LNS_const_add_pc. */
19905 void handle_const_add_pc ();
19906
19907 /* Handle DW_LNS_fixed_advance_pc. */
19908 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19909 {
19910 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19911 m_op_index = 0;
19912 }
19913
19914 /* Handle DW_LNS_copy. */
19915 void handle_copy ()
19916 {
19917 record_line (false);
19918 m_discriminator = 0;
19919 }
19920
19921 /* Handle DW_LNE_end_sequence. */
19922 void handle_end_sequence ()
19923 {
19924 m_currently_recording_lines = true;
19925 }
19926
19927 private:
19928 /* Advance the line by LINE_DELTA. */
19929 void advance_line (int line_delta)
19930 {
19931 m_line += line_delta;
19932
19933 if (line_delta != 0)
19934 m_line_has_non_zero_discriminator = m_discriminator != 0;
19935 }
19936
19937 struct dwarf2_cu *m_cu;
19938
19939 gdbarch *m_gdbarch;
19940
19941 /* True if we're recording lines.
19942 Otherwise we're building partial symtabs and are just interested in
19943 finding include files mentioned by the line number program. */
19944 bool m_record_lines_p;
19945
19946 /* The line number header. */
19947 line_header *m_line_header;
19948
19949 /* These are part of the standard DWARF line number state machine,
19950 and initialized according to the DWARF spec. */
19951
19952 unsigned char m_op_index = 0;
19953 /* The line table index of the current file. */
19954 file_name_index m_file = 1;
19955 unsigned int m_line = 1;
19956
19957 /* These are initialized in the constructor. */
19958
19959 CORE_ADDR m_address;
19960 bool m_is_stmt;
19961 unsigned int m_discriminator;
19962
19963 /* Additional bits of state we need to track. */
19964
19965 /* The last file that we called dwarf2_start_subfile for.
19966 This is only used for TLLs. */
19967 unsigned int m_last_file = 0;
19968 /* The last file a line number was recorded for. */
19969 struct subfile *m_last_subfile = NULL;
19970
19971 /* When true, record the lines we decode. */
19972 bool m_currently_recording_lines = false;
19973
19974 /* The last line number that was recorded, used to coalesce
19975 consecutive entries for the same line. This can happen, for
19976 example, when discriminators are present. PR 17276. */
19977 unsigned int m_last_line = 0;
19978 bool m_line_has_non_zero_discriminator = false;
19979 };
19980
19981 void
19982 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19983 {
19984 CORE_ADDR addr_adj = (((m_op_index + adjust)
19985 / m_line_header->maximum_ops_per_instruction)
19986 * m_line_header->minimum_instruction_length);
19987 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19988 m_op_index = ((m_op_index + adjust)
19989 % m_line_header->maximum_ops_per_instruction);
19990 }
19991
19992 void
19993 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19994 {
19995 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19996 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19997 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19998 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19999 / m_line_header->maximum_ops_per_instruction)
20000 * m_line_header->minimum_instruction_length);
20001 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20002 m_op_index = ((m_op_index + adj_opcode_d)
20003 % m_line_header->maximum_ops_per_instruction);
20004
20005 int line_delta = m_line_header->line_base + adj_opcode_r;
20006 advance_line (line_delta);
20007 record_line (false);
20008 m_discriminator = 0;
20009 }
20010
20011 void
20012 lnp_state_machine::handle_set_file (file_name_index file)
20013 {
20014 m_file = file;
20015
20016 const file_entry *fe = current_file ();
20017 if (fe == NULL)
20018 dwarf2_debug_line_missing_file_complaint ();
20019 else if (m_record_lines_p)
20020 {
20021 const char *dir = fe->include_dir (m_line_header);
20022
20023 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20024 m_line_has_non_zero_discriminator = m_discriminator != 0;
20025 dwarf2_start_subfile (m_cu, fe->name, dir);
20026 }
20027 }
20028
20029 void
20030 lnp_state_machine::handle_const_add_pc ()
20031 {
20032 CORE_ADDR adjust
20033 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20034
20035 CORE_ADDR addr_adj
20036 = (((m_op_index + adjust)
20037 / m_line_header->maximum_ops_per_instruction)
20038 * m_line_header->minimum_instruction_length);
20039
20040 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20041 m_op_index = ((m_op_index + adjust)
20042 % m_line_header->maximum_ops_per_instruction);
20043 }
20044
20045 /* Return non-zero if we should add LINE to the line number table.
20046 LINE is the line to add, LAST_LINE is the last line that was added,
20047 LAST_SUBFILE is the subfile for LAST_LINE.
20048 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20049 had a non-zero discriminator.
20050
20051 We have to be careful in the presence of discriminators.
20052 E.g., for this line:
20053
20054 for (i = 0; i < 100000; i++);
20055
20056 clang can emit four line number entries for that one line,
20057 each with a different discriminator.
20058 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20059
20060 However, we want gdb to coalesce all four entries into one.
20061 Otherwise the user could stepi into the middle of the line and
20062 gdb would get confused about whether the pc really was in the
20063 middle of the line.
20064
20065 Things are further complicated by the fact that two consecutive
20066 line number entries for the same line is a heuristic used by gcc
20067 to denote the end of the prologue. So we can't just discard duplicate
20068 entries, we have to be selective about it. The heuristic we use is
20069 that we only collapse consecutive entries for the same line if at least
20070 one of those entries has a non-zero discriminator. PR 17276.
20071
20072 Note: Addresses in the line number state machine can never go backwards
20073 within one sequence, thus this coalescing is ok. */
20074
20075 static int
20076 dwarf_record_line_p (struct dwarf2_cu *cu,
20077 unsigned int line, unsigned int last_line,
20078 int line_has_non_zero_discriminator,
20079 struct subfile *last_subfile)
20080 {
20081 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20082 return 1;
20083 if (line != last_line)
20084 return 1;
20085 /* Same line for the same file that we've seen already.
20086 As a last check, for pr 17276, only record the line if the line
20087 has never had a non-zero discriminator. */
20088 if (!line_has_non_zero_discriminator)
20089 return 1;
20090 return 0;
20091 }
20092
20093 /* Use the CU's builder to record line number LINE beginning at
20094 address ADDRESS in the line table of subfile SUBFILE. */
20095
20096 static void
20097 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20098 unsigned int line, CORE_ADDR address, bool is_stmt,
20099 struct dwarf2_cu *cu)
20100 {
20101 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20102
20103 if (dwarf_line_debug)
20104 {
20105 fprintf_unfiltered (gdb_stdlog,
20106 "Recording line %u, file %s, address %s\n",
20107 line, lbasename (subfile->name),
20108 paddress (gdbarch, address));
20109 }
20110
20111 if (cu != nullptr)
20112 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20113 }
20114
20115 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20116 Mark the end of a set of line number records.
20117 The arguments are the same as for dwarf_record_line_1.
20118 If SUBFILE is NULL the request is ignored. */
20119
20120 static void
20121 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20122 CORE_ADDR address, struct dwarf2_cu *cu)
20123 {
20124 if (subfile == NULL)
20125 return;
20126
20127 if (dwarf_line_debug)
20128 {
20129 fprintf_unfiltered (gdb_stdlog,
20130 "Finishing current line, file %s, address %s\n",
20131 lbasename (subfile->name),
20132 paddress (gdbarch, address));
20133 }
20134
20135 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20136 }
20137
20138 void
20139 lnp_state_machine::record_line (bool end_sequence)
20140 {
20141 if (dwarf_line_debug)
20142 {
20143 fprintf_unfiltered (gdb_stdlog,
20144 "Processing actual line %u: file %u,"
20145 " address %s, is_stmt %u, discrim %u%s\n",
20146 m_line, m_file,
20147 paddress (m_gdbarch, m_address),
20148 m_is_stmt, m_discriminator,
20149 (end_sequence ? "\t(end sequence)" : ""));
20150 }
20151
20152 file_entry *fe = current_file ();
20153
20154 if (fe == NULL)
20155 dwarf2_debug_line_missing_file_complaint ();
20156 /* For now we ignore lines not starting on an instruction boundary.
20157 But not when processing end_sequence for compatibility with the
20158 previous version of the code. */
20159 else if (m_op_index == 0 || end_sequence)
20160 {
20161 fe->included_p = 1;
20162 if (m_record_lines_p)
20163 {
20164 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20165 || end_sequence)
20166 {
20167 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20168 m_currently_recording_lines ? m_cu : nullptr);
20169 }
20170
20171 if (!end_sequence)
20172 {
20173 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20174
20175 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20176 m_line_has_non_zero_discriminator,
20177 m_last_subfile))
20178 {
20179 buildsym_compunit *builder = m_cu->get_builder ();
20180 dwarf_record_line_1 (m_gdbarch,
20181 builder->get_current_subfile (),
20182 m_line, m_address, is_stmt,
20183 m_currently_recording_lines ? m_cu : nullptr);
20184 }
20185 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20186 m_last_line = m_line;
20187 }
20188 }
20189 }
20190 }
20191
20192 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20193 line_header *lh, bool record_lines_p)
20194 {
20195 m_cu = cu;
20196 m_gdbarch = arch;
20197 m_record_lines_p = record_lines_p;
20198 m_line_header = lh;
20199
20200 m_currently_recording_lines = true;
20201
20202 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20203 was a line entry for it so that the backend has a chance to adjust it
20204 and also record it in case it needs it. This is currently used by MIPS
20205 code, cf. `mips_adjust_dwarf2_line'. */
20206 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20207 m_is_stmt = lh->default_is_stmt;
20208 m_discriminator = 0;
20209 }
20210
20211 void
20212 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20213 const gdb_byte *line_ptr,
20214 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20215 {
20216 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20217 the pc range of the CU. However, we restrict the test to only ADDRESS
20218 values of zero to preserve GDB's previous behaviour which is to handle
20219 the specific case of a function being GC'd by the linker. */
20220
20221 if (address == 0 && address < unrelocated_lowpc)
20222 {
20223 /* This line table is for a function which has been
20224 GCd by the linker. Ignore it. PR gdb/12528 */
20225
20226 struct objfile *objfile = cu->per_objfile->objfile;
20227 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20228
20229 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20230 line_offset, objfile_name (objfile));
20231 m_currently_recording_lines = false;
20232 /* Note: m_currently_recording_lines is left as false until we see
20233 DW_LNE_end_sequence. */
20234 }
20235 }
20236
20237 /* Subroutine of dwarf_decode_lines to simplify it.
20238 Process the line number information in LH.
20239 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20240 program in order to set included_p for every referenced header. */
20241
20242 static void
20243 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20244 const int decode_for_pst_p, CORE_ADDR lowpc)
20245 {
20246 const gdb_byte *line_ptr, *extended_end;
20247 const gdb_byte *line_end;
20248 unsigned int bytes_read, extended_len;
20249 unsigned char op_code, extended_op;
20250 CORE_ADDR baseaddr;
20251 struct objfile *objfile = cu->per_objfile->objfile;
20252 bfd *abfd = objfile->obfd;
20253 struct gdbarch *gdbarch = objfile->arch ();
20254 /* True if we're recording line info (as opposed to building partial
20255 symtabs and just interested in finding include files mentioned by
20256 the line number program). */
20257 bool record_lines_p = !decode_for_pst_p;
20258
20259 baseaddr = objfile->text_section_offset ();
20260
20261 line_ptr = lh->statement_program_start;
20262 line_end = lh->statement_program_end;
20263
20264 /* Read the statement sequences until there's nothing left. */
20265 while (line_ptr < line_end)
20266 {
20267 /* The DWARF line number program state machine. Reset the state
20268 machine at the start of each sequence. */
20269 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20270 bool end_sequence = false;
20271
20272 if (record_lines_p)
20273 {
20274 /* Start a subfile for the current file of the state
20275 machine. */
20276 const file_entry *fe = state_machine.current_file ();
20277
20278 if (fe != NULL)
20279 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20280 }
20281
20282 /* Decode the table. */
20283 while (line_ptr < line_end && !end_sequence)
20284 {
20285 op_code = read_1_byte (abfd, line_ptr);
20286 line_ptr += 1;
20287
20288 if (op_code >= lh->opcode_base)
20289 {
20290 /* Special opcode. */
20291 state_machine.handle_special_opcode (op_code);
20292 }
20293 else switch (op_code)
20294 {
20295 case DW_LNS_extended_op:
20296 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20297 &bytes_read);
20298 line_ptr += bytes_read;
20299 extended_end = line_ptr + extended_len;
20300 extended_op = read_1_byte (abfd, line_ptr);
20301 line_ptr += 1;
20302 switch (extended_op)
20303 {
20304 case DW_LNE_end_sequence:
20305 state_machine.handle_end_sequence ();
20306 end_sequence = true;
20307 break;
20308 case DW_LNE_set_address:
20309 {
20310 CORE_ADDR address
20311 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20312 line_ptr += bytes_read;
20313
20314 state_machine.check_line_address (cu, line_ptr,
20315 lowpc - baseaddr, address);
20316 state_machine.handle_set_address (baseaddr, address);
20317 }
20318 break;
20319 case DW_LNE_define_file:
20320 {
20321 const char *cur_file;
20322 unsigned int mod_time, length;
20323 dir_index dindex;
20324
20325 cur_file = read_direct_string (abfd, line_ptr,
20326 &bytes_read);
20327 line_ptr += bytes_read;
20328 dindex = (dir_index)
20329 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20330 line_ptr += bytes_read;
20331 mod_time =
20332 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20333 line_ptr += bytes_read;
20334 length =
20335 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20336 line_ptr += bytes_read;
20337 lh->add_file_name (cur_file, dindex, mod_time, length);
20338 }
20339 break;
20340 case DW_LNE_set_discriminator:
20341 {
20342 /* The discriminator is not interesting to the
20343 debugger; just ignore it. We still need to
20344 check its value though:
20345 if there are consecutive entries for the same
20346 (non-prologue) line we want to coalesce them.
20347 PR 17276. */
20348 unsigned int discr
20349 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20350 line_ptr += bytes_read;
20351
20352 state_machine.handle_set_discriminator (discr);
20353 }
20354 break;
20355 default:
20356 complaint (_("mangled .debug_line section"));
20357 return;
20358 }
20359 /* Make sure that we parsed the extended op correctly. If e.g.
20360 we expected a different address size than the producer used,
20361 we may have read the wrong number of bytes. */
20362 if (line_ptr != extended_end)
20363 {
20364 complaint (_("mangled .debug_line section"));
20365 return;
20366 }
20367 break;
20368 case DW_LNS_copy:
20369 state_machine.handle_copy ();
20370 break;
20371 case DW_LNS_advance_pc:
20372 {
20373 CORE_ADDR adjust
20374 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20375 line_ptr += bytes_read;
20376
20377 state_machine.handle_advance_pc (adjust);
20378 }
20379 break;
20380 case DW_LNS_advance_line:
20381 {
20382 int line_delta
20383 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20384 line_ptr += bytes_read;
20385
20386 state_machine.handle_advance_line (line_delta);
20387 }
20388 break;
20389 case DW_LNS_set_file:
20390 {
20391 file_name_index file
20392 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20393 &bytes_read);
20394 line_ptr += bytes_read;
20395
20396 state_machine.handle_set_file (file);
20397 }
20398 break;
20399 case DW_LNS_set_column:
20400 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20401 line_ptr += bytes_read;
20402 break;
20403 case DW_LNS_negate_stmt:
20404 state_machine.handle_negate_stmt ();
20405 break;
20406 case DW_LNS_set_basic_block:
20407 break;
20408 /* Add to the address register of the state machine the
20409 address increment value corresponding to special opcode
20410 255. I.e., this value is scaled by the minimum
20411 instruction length since special opcode 255 would have
20412 scaled the increment. */
20413 case DW_LNS_const_add_pc:
20414 state_machine.handle_const_add_pc ();
20415 break;
20416 case DW_LNS_fixed_advance_pc:
20417 {
20418 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20419 line_ptr += 2;
20420
20421 state_machine.handle_fixed_advance_pc (addr_adj);
20422 }
20423 break;
20424 default:
20425 {
20426 /* Unknown standard opcode, ignore it. */
20427 int i;
20428
20429 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20430 {
20431 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20432 line_ptr += bytes_read;
20433 }
20434 }
20435 }
20436 }
20437
20438 if (!end_sequence)
20439 dwarf2_debug_line_missing_end_sequence_complaint ();
20440
20441 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20442 in which case we still finish recording the last line). */
20443 state_machine.record_line (true);
20444 }
20445 }
20446
20447 /* Decode the Line Number Program (LNP) for the given line_header
20448 structure and CU. The actual information extracted and the type
20449 of structures created from the LNP depends on the value of PST.
20450
20451 1. If PST is NULL, then this procedure uses the data from the program
20452 to create all necessary symbol tables, and their linetables.
20453
20454 2. If PST is not NULL, this procedure reads the program to determine
20455 the list of files included by the unit represented by PST, and
20456 builds all the associated partial symbol tables.
20457
20458 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20459 It is used for relative paths in the line table.
20460 NOTE: When processing partial symtabs (pst != NULL),
20461 comp_dir == pst->dirname.
20462
20463 NOTE: It is important that psymtabs have the same file name (via strcmp)
20464 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20465 symtab we don't use it in the name of the psymtabs we create.
20466 E.g. expand_line_sal requires this when finding psymtabs to expand.
20467 A good testcase for this is mb-inline.exp.
20468
20469 LOWPC is the lowest address in CU (or 0 if not known).
20470
20471 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20472 for its PC<->lines mapping information. Otherwise only the filename
20473 table is read in. */
20474
20475 static void
20476 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20477 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20478 CORE_ADDR lowpc, int decode_mapping)
20479 {
20480 struct objfile *objfile = cu->per_objfile->objfile;
20481 const int decode_for_pst_p = (pst != NULL);
20482
20483 if (decode_mapping)
20484 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20485
20486 if (decode_for_pst_p)
20487 {
20488 /* Now that we're done scanning the Line Header Program, we can
20489 create the psymtab of each included file. */
20490 for (auto &file_entry : lh->file_names ())
20491 if (file_entry.included_p == 1)
20492 {
20493 gdb::unique_xmalloc_ptr<char> name_holder;
20494 const char *include_name =
20495 psymtab_include_file_name (lh, file_entry, pst,
20496 comp_dir, &name_holder);
20497 if (include_name != NULL)
20498 dwarf2_create_include_psymtab (include_name, pst, objfile);
20499 }
20500 }
20501 else
20502 {
20503 /* Make sure a symtab is created for every file, even files
20504 which contain only variables (i.e. no code with associated
20505 line numbers). */
20506 buildsym_compunit *builder = cu->get_builder ();
20507 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20508
20509 for (auto &fe : lh->file_names ())
20510 {
20511 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20512 if (builder->get_current_subfile ()->symtab == NULL)
20513 {
20514 builder->get_current_subfile ()->symtab
20515 = allocate_symtab (cust,
20516 builder->get_current_subfile ()->name);
20517 }
20518 fe.symtab = builder->get_current_subfile ()->symtab;
20519 }
20520 }
20521 }
20522
20523 /* Start a subfile for DWARF. FILENAME is the name of the file and
20524 DIRNAME the name of the source directory which contains FILENAME
20525 or NULL if not known.
20526 This routine tries to keep line numbers from identical absolute and
20527 relative file names in a common subfile.
20528
20529 Using the `list' example from the GDB testsuite, which resides in
20530 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20531 of /srcdir/list0.c yields the following debugging information for list0.c:
20532
20533 DW_AT_name: /srcdir/list0.c
20534 DW_AT_comp_dir: /compdir
20535 files.files[0].name: list0.h
20536 files.files[0].dir: /srcdir
20537 files.files[1].name: list0.c
20538 files.files[1].dir: /srcdir
20539
20540 The line number information for list0.c has to end up in a single
20541 subfile, so that `break /srcdir/list0.c:1' works as expected.
20542 start_subfile will ensure that this happens provided that we pass the
20543 concatenation of files.files[1].dir and files.files[1].name as the
20544 subfile's name. */
20545
20546 static void
20547 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20548 const char *dirname)
20549 {
20550 gdb::unique_xmalloc_ptr<char> copy;
20551
20552 /* In order not to lose the line information directory,
20553 we concatenate it to the filename when it makes sense.
20554 Note that the Dwarf3 standard says (speaking of filenames in line
20555 information): ``The directory index is ignored for file names
20556 that represent full path names''. Thus ignoring dirname in the
20557 `else' branch below isn't an issue. */
20558
20559 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20560 {
20561 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20562 filename = copy.get ();
20563 }
20564
20565 cu->get_builder ()->start_subfile (filename);
20566 }
20567
20568 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20569 buildsym_compunit constructor. */
20570
20571 struct compunit_symtab *
20572 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20573 CORE_ADDR low_pc)
20574 {
20575 gdb_assert (m_builder == nullptr);
20576
20577 m_builder.reset (new struct buildsym_compunit
20578 (per_cu->dwarf2_per_objfile->objfile,
20579 name, comp_dir, language, low_pc));
20580
20581 list_in_scope = get_builder ()->get_file_symbols ();
20582
20583 get_builder ()->record_debugformat ("DWARF 2");
20584 get_builder ()->record_producer (producer);
20585
20586 processing_has_namespace_info = false;
20587
20588 return get_builder ()->get_compunit_symtab ();
20589 }
20590
20591 static void
20592 var_decode_location (struct attribute *attr, struct symbol *sym,
20593 struct dwarf2_cu *cu)
20594 {
20595 struct objfile *objfile = cu->per_objfile->objfile;
20596 struct comp_unit_head *cu_header = &cu->header;
20597
20598 /* NOTE drow/2003-01-30: There used to be a comment and some special
20599 code here to turn a symbol with DW_AT_external and a
20600 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20601 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20602 with some versions of binutils) where shared libraries could have
20603 relocations against symbols in their debug information - the
20604 minimal symbol would have the right address, but the debug info
20605 would not. It's no longer necessary, because we will explicitly
20606 apply relocations when we read in the debug information now. */
20607
20608 /* A DW_AT_location attribute with no contents indicates that a
20609 variable has been optimized away. */
20610 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20611 {
20612 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20613 return;
20614 }
20615
20616 /* Handle one degenerate form of location expression specially, to
20617 preserve GDB's previous behavior when section offsets are
20618 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20619 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20620
20621 if (attr->form_is_block ()
20622 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20623 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20624 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20625 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20626 && (DW_BLOCK (attr)->size
20627 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20628 {
20629 unsigned int dummy;
20630
20631 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20632 SET_SYMBOL_VALUE_ADDRESS
20633 (sym, cu->header.read_address (objfile->obfd,
20634 DW_BLOCK (attr)->data + 1,
20635 &dummy));
20636 else
20637 SET_SYMBOL_VALUE_ADDRESS
20638 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20639 &dummy));
20640 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20641 fixup_symbol_section (sym, objfile);
20642 SET_SYMBOL_VALUE_ADDRESS
20643 (sym,
20644 SYMBOL_VALUE_ADDRESS (sym)
20645 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20646 return;
20647 }
20648
20649 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20650 expression evaluator, and use LOC_COMPUTED only when necessary
20651 (i.e. when the value of a register or memory location is
20652 referenced, or a thread-local block, etc.). Then again, it might
20653 not be worthwhile. I'm assuming that it isn't unless performance
20654 or memory numbers show me otherwise. */
20655
20656 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20657
20658 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20659 cu->has_loclist = true;
20660 }
20661
20662 /* Given a pointer to a DWARF information entry, figure out if we need
20663 to make a symbol table entry for it, and if so, create a new entry
20664 and return a pointer to it.
20665 If TYPE is NULL, determine symbol type from the die, otherwise
20666 used the passed type.
20667 If SPACE is not NULL, use it to hold the new symbol. If it is
20668 NULL, allocate a new symbol on the objfile's obstack. */
20669
20670 static struct symbol *
20671 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20672 struct symbol *space)
20673 {
20674 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20675 struct objfile *objfile = dwarf2_per_objfile->objfile;
20676 struct gdbarch *gdbarch = objfile->arch ();
20677 struct symbol *sym = NULL;
20678 const char *name;
20679 struct attribute *attr = NULL;
20680 struct attribute *attr2 = NULL;
20681 CORE_ADDR baseaddr;
20682 struct pending **list_to_add = NULL;
20683
20684 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20685
20686 baseaddr = objfile->text_section_offset ();
20687
20688 name = dwarf2_name (die, cu);
20689 if (name)
20690 {
20691 int suppress_add = 0;
20692
20693 if (space)
20694 sym = space;
20695 else
20696 sym = new (&objfile->objfile_obstack) symbol;
20697 OBJSTAT (objfile, n_syms++);
20698
20699 /* Cache this symbol's name and the name's demangled form (if any). */
20700 sym->set_language (cu->language, &objfile->objfile_obstack);
20701 /* Fortran does not have mangling standard and the mangling does differ
20702 between gfortran, iFort etc. */
20703 const char *physname
20704 = (cu->language == language_fortran
20705 ? dwarf2_full_name (name, die, cu)
20706 : dwarf2_physname (name, die, cu));
20707 const char *linkagename = dw2_linkage_name (die, cu);
20708
20709 if (linkagename == nullptr || cu->language == language_ada)
20710 sym->set_linkage_name (physname);
20711 else
20712 {
20713 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20714 sym->set_linkage_name (linkagename);
20715 }
20716
20717 /* Default assumptions.
20718 Use the passed type or decode it from the die. */
20719 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20720 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20721 if (type != NULL)
20722 SYMBOL_TYPE (sym) = type;
20723 else
20724 SYMBOL_TYPE (sym) = die_type (die, cu);
20725 attr = dwarf2_attr (die,
20726 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20727 cu);
20728 if (attr != nullptr)
20729 {
20730 SYMBOL_LINE (sym) = DW_UNSND (attr);
20731 }
20732
20733 attr = dwarf2_attr (die,
20734 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20735 cu);
20736 if (attr != nullptr)
20737 {
20738 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20739 struct file_entry *fe;
20740
20741 if (cu->line_header != NULL)
20742 fe = cu->line_header->file_name_at (file_index);
20743 else
20744 fe = NULL;
20745
20746 if (fe == NULL)
20747 complaint (_("file index out of range"));
20748 else
20749 symbol_set_symtab (sym, fe->symtab);
20750 }
20751
20752 switch (die->tag)
20753 {
20754 case DW_TAG_label:
20755 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20756 if (attr != nullptr)
20757 {
20758 CORE_ADDR addr;
20759
20760 addr = attr->value_as_address ();
20761 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20762 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20763 }
20764 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20765 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20766 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20767 add_symbol_to_list (sym, cu->list_in_scope);
20768 break;
20769 case DW_TAG_subprogram:
20770 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20771 finish_block. */
20772 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20773 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20774 if ((attr2 && (DW_UNSND (attr2) != 0))
20775 || cu->language == language_ada
20776 || cu->language == language_fortran)
20777 {
20778 /* Subprograms marked external are stored as a global symbol.
20779 Ada and Fortran subprograms, whether marked external or
20780 not, are always stored as a global symbol, because we want
20781 to be able to access them globally. For instance, we want
20782 to be able to break on a nested subprogram without having
20783 to specify the context. */
20784 list_to_add = cu->get_builder ()->get_global_symbols ();
20785 }
20786 else
20787 {
20788 list_to_add = cu->list_in_scope;
20789 }
20790 break;
20791 case DW_TAG_inlined_subroutine:
20792 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20793 finish_block. */
20794 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20795 SYMBOL_INLINED (sym) = 1;
20796 list_to_add = cu->list_in_scope;
20797 break;
20798 case DW_TAG_template_value_param:
20799 suppress_add = 1;
20800 /* Fall through. */
20801 case DW_TAG_constant:
20802 case DW_TAG_variable:
20803 case DW_TAG_member:
20804 /* Compilation with minimal debug info may result in
20805 variables with missing type entries. Change the
20806 misleading `void' type to something sensible. */
20807 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20808 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20809
20810 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20811 /* In the case of DW_TAG_member, we should only be called for
20812 static const members. */
20813 if (die->tag == DW_TAG_member)
20814 {
20815 /* dwarf2_add_field uses die_is_declaration,
20816 so we do the same. */
20817 gdb_assert (die_is_declaration (die, cu));
20818 gdb_assert (attr);
20819 }
20820 if (attr != nullptr)
20821 {
20822 dwarf2_const_value (attr, sym, cu);
20823 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20824 if (!suppress_add)
20825 {
20826 if (attr2 && (DW_UNSND (attr2) != 0))
20827 list_to_add = cu->get_builder ()->get_global_symbols ();
20828 else
20829 list_to_add = cu->list_in_scope;
20830 }
20831 break;
20832 }
20833 attr = dwarf2_attr (die, DW_AT_location, cu);
20834 if (attr != nullptr)
20835 {
20836 var_decode_location (attr, sym, cu);
20837 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20838
20839 /* Fortran explicitly imports any global symbols to the local
20840 scope by DW_TAG_common_block. */
20841 if (cu->language == language_fortran && die->parent
20842 && die->parent->tag == DW_TAG_common_block)
20843 attr2 = NULL;
20844
20845 if (SYMBOL_CLASS (sym) == LOC_STATIC
20846 && SYMBOL_VALUE_ADDRESS (sym) == 0
20847 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20848 {
20849 /* When a static variable is eliminated by the linker,
20850 the corresponding debug information is not stripped
20851 out, but the variable address is set to null;
20852 do not add such variables into symbol table. */
20853 }
20854 else if (attr2 && (DW_UNSND (attr2) != 0))
20855 {
20856 if (SYMBOL_CLASS (sym) == LOC_STATIC
20857 && (objfile->flags & OBJF_MAINLINE) == 0
20858 && dwarf2_per_objfile->per_bfd->can_copy)
20859 {
20860 /* A global static variable might be subject to
20861 copy relocation. We first check for a local
20862 minsym, though, because maybe the symbol was
20863 marked hidden, in which case this would not
20864 apply. */
20865 bound_minimal_symbol found
20866 = (lookup_minimal_symbol_linkage
20867 (sym->linkage_name (), objfile));
20868 if (found.minsym != nullptr)
20869 sym->maybe_copied = 1;
20870 }
20871
20872 /* A variable with DW_AT_external is never static,
20873 but it may be block-scoped. */
20874 list_to_add
20875 = ((cu->list_in_scope
20876 == cu->get_builder ()->get_file_symbols ())
20877 ? cu->get_builder ()->get_global_symbols ()
20878 : cu->list_in_scope);
20879 }
20880 else
20881 list_to_add = cu->list_in_scope;
20882 }
20883 else
20884 {
20885 /* We do not know the address of this symbol.
20886 If it is an external symbol and we have type information
20887 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20888 The address of the variable will then be determined from
20889 the minimal symbol table whenever the variable is
20890 referenced. */
20891 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20892
20893 /* Fortran explicitly imports any global symbols to the local
20894 scope by DW_TAG_common_block. */
20895 if (cu->language == language_fortran && die->parent
20896 && die->parent->tag == DW_TAG_common_block)
20897 {
20898 /* SYMBOL_CLASS doesn't matter here because
20899 read_common_block is going to reset it. */
20900 if (!suppress_add)
20901 list_to_add = cu->list_in_scope;
20902 }
20903 else if (attr2 && (DW_UNSND (attr2) != 0)
20904 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20905 {
20906 /* A variable with DW_AT_external is never static, but it
20907 may be block-scoped. */
20908 list_to_add
20909 = ((cu->list_in_scope
20910 == cu->get_builder ()->get_file_symbols ())
20911 ? cu->get_builder ()->get_global_symbols ()
20912 : cu->list_in_scope);
20913
20914 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20915 }
20916 else if (!die_is_declaration (die, cu))
20917 {
20918 /* Use the default LOC_OPTIMIZED_OUT class. */
20919 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20920 if (!suppress_add)
20921 list_to_add = cu->list_in_scope;
20922 }
20923 }
20924 break;
20925 case DW_TAG_formal_parameter:
20926 {
20927 /* If we are inside a function, mark this as an argument. If
20928 not, we might be looking at an argument to an inlined function
20929 when we do not have enough information to show inlined frames;
20930 pretend it's a local variable in that case so that the user can
20931 still see it. */
20932 struct context_stack *curr
20933 = cu->get_builder ()->get_current_context_stack ();
20934 if (curr != nullptr && curr->name != nullptr)
20935 SYMBOL_IS_ARGUMENT (sym) = 1;
20936 attr = dwarf2_attr (die, DW_AT_location, cu);
20937 if (attr != nullptr)
20938 {
20939 var_decode_location (attr, sym, cu);
20940 }
20941 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20942 if (attr != nullptr)
20943 {
20944 dwarf2_const_value (attr, sym, cu);
20945 }
20946
20947 list_to_add = cu->list_in_scope;
20948 }
20949 break;
20950 case DW_TAG_unspecified_parameters:
20951 /* From varargs functions; gdb doesn't seem to have any
20952 interest in this information, so just ignore it for now.
20953 (FIXME?) */
20954 break;
20955 case DW_TAG_template_type_param:
20956 suppress_add = 1;
20957 /* Fall through. */
20958 case DW_TAG_class_type:
20959 case DW_TAG_interface_type:
20960 case DW_TAG_structure_type:
20961 case DW_TAG_union_type:
20962 case DW_TAG_set_type:
20963 case DW_TAG_enumeration_type:
20964 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20965 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20966
20967 {
20968 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20969 really ever be static objects: otherwise, if you try
20970 to, say, break of a class's method and you're in a file
20971 which doesn't mention that class, it won't work unless
20972 the check for all static symbols in lookup_symbol_aux
20973 saves you. See the OtherFileClass tests in
20974 gdb.c++/namespace.exp. */
20975
20976 if (!suppress_add)
20977 {
20978 buildsym_compunit *builder = cu->get_builder ();
20979 list_to_add
20980 = (cu->list_in_scope == builder->get_file_symbols ()
20981 && cu->language == language_cplus
20982 ? builder->get_global_symbols ()
20983 : cu->list_in_scope);
20984
20985 /* The semantics of C++ state that "struct foo {
20986 ... }" also defines a typedef for "foo". */
20987 if (cu->language == language_cplus
20988 || cu->language == language_ada
20989 || cu->language == language_d
20990 || cu->language == language_rust)
20991 {
20992 /* The symbol's name is already allocated along
20993 with this objfile, so we don't need to
20994 duplicate it for the type. */
20995 if (SYMBOL_TYPE (sym)->name () == 0)
20996 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
20997 }
20998 }
20999 }
21000 break;
21001 case DW_TAG_typedef:
21002 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21003 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21004 list_to_add = cu->list_in_scope;
21005 break;
21006 case DW_TAG_base_type:
21007 case DW_TAG_subrange_type:
21008 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21009 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21010 list_to_add = cu->list_in_scope;
21011 break;
21012 case DW_TAG_enumerator:
21013 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21014 if (attr != nullptr)
21015 {
21016 dwarf2_const_value (attr, sym, cu);
21017 }
21018 {
21019 /* NOTE: carlton/2003-11-10: See comment above in the
21020 DW_TAG_class_type, etc. block. */
21021
21022 list_to_add
21023 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21024 && cu->language == language_cplus
21025 ? cu->get_builder ()->get_global_symbols ()
21026 : cu->list_in_scope);
21027 }
21028 break;
21029 case DW_TAG_imported_declaration:
21030 case DW_TAG_namespace:
21031 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21032 list_to_add = cu->get_builder ()->get_global_symbols ();
21033 break;
21034 case DW_TAG_module:
21035 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21036 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21037 list_to_add = cu->get_builder ()->get_global_symbols ();
21038 break;
21039 case DW_TAG_common_block:
21040 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21041 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21042 add_symbol_to_list (sym, cu->list_in_scope);
21043 break;
21044 default:
21045 /* Not a tag we recognize. Hopefully we aren't processing
21046 trash data, but since we must specifically ignore things
21047 we don't recognize, there is nothing else we should do at
21048 this point. */
21049 complaint (_("unsupported tag: '%s'"),
21050 dwarf_tag_name (die->tag));
21051 break;
21052 }
21053
21054 if (suppress_add)
21055 {
21056 sym->hash_next = objfile->template_symbols;
21057 objfile->template_symbols = sym;
21058 list_to_add = NULL;
21059 }
21060
21061 if (list_to_add != NULL)
21062 add_symbol_to_list (sym, list_to_add);
21063
21064 /* For the benefit of old versions of GCC, check for anonymous
21065 namespaces based on the demangled name. */
21066 if (!cu->processing_has_namespace_info
21067 && cu->language == language_cplus)
21068 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21069 }
21070 return (sym);
21071 }
21072
21073 /* Given an attr with a DW_FORM_dataN value in host byte order,
21074 zero-extend it as appropriate for the symbol's type. The DWARF
21075 standard (v4) is not entirely clear about the meaning of using
21076 DW_FORM_dataN for a constant with a signed type, where the type is
21077 wider than the data. The conclusion of a discussion on the DWARF
21078 list was that this is unspecified. We choose to always zero-extend
21079 because that is the interpretation long in use by GCC. */
21080
21081 static gdb_byte *
21082 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21083 struct dwarf2_cu *cu, LONGEST *value, int bits)
21084 {
21085 struct objfile *objfile = cu->per_objfile->objfile;
21086 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21087 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21088 LONGEST l = DW_UNSND (attr);
21089
21090 if (bits < sizeof (*value) * 8)
21091 {
21092 l &= ((LONGEST) 1 << bits) - 1;
21093 *value = l;
21094 }
21095 else if (bits == sizeof (*value) * 8)
21096 *value = l;
21097 else
21098 {
21099 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21100 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21101 return bytes;
21102 }
21103
21104 return NULL;
21105 }
21106
21107 /* Read a constant value from an attribute. Either set *VALUE, or if
21108 the value does not fit in *VALUE, set *BYTES - either already
21109 allocated on the objfile obstack, or newly allocated on OBSTACK,
21110 or, set *BATON, if we translated the constant to a location
21111 expression. */
21112
21113 static void
21114 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21115 const char *name, struct obstack *obstack,
21116 struct dwarf2_cu *cu,
21117 LONGEST *value, const gdb_byte **bytes,
21118 struct dwarf2_locexpr_baton **baton)
21119 {
21120 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21121 struct objfile *objfile = per_objfile->objfile;
21122 struct comp_unit_head *cu_header = &cu->header;
21123 struct dwarf_block *blk;
21124 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21125 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21126
21127 *value = 0;
21128 *bytes = NULL;
21129 *baton = NULL;
21130
21131 switch (attr->form)
21132 {
21133 case DW_FORM_addr:
21134 case DW_FORM_addrx:
21135 case DW_FORM_GNU_addr_index:
21136 {
21137 gdb_byte *data;
21138
21139 if (TYPE_LENGTH (type) != cu_header->addr_size)
21140 dwarf2_const_value_length_mismatch_complaint (name,
21141 cu_header->addr_size,
21142 TYPE_LENGTH (type));
21143 /* Symbols of this form are reasonably rare, so we just
21144 piggyback on the existing location code rather than writing
21145 a new implementation of symbol_computed_ops. */
21146 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21147 (*baton)->per_objfile = per_objfile;
21148 (*baton)->per_cu = cu->per_cu;
21149 gdb_assert ((*baton)->per_cu);
21150
21151 (*baton)->size = 2 + cu_header->addr_size;
21152 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21153 (*baton)->data = data;
21154
21155 data[0] = DW_OP_addr;
21156 store_unsigned_integer (&data[1], cu_header->addr_size,
21157 byte_order, DW_ADDR (attr));
21158 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21159 }
21160 break;
21161 case DW_FORM_string:
21162 case DW_FORM_strp:
21163 case DW_FORM_strx:
21164 case DW_FORM_GNU_str_index:
21165 case DW_FORM_GNU_strp_alt:
21166 /* DW_STRING is already allocated on the objfile obstack, point
21167 directly to it. */
21168 *bytes = (const gdb_byte *) DW_STRING (attr);
21169 break;
21170 case DW_FORM_block1:
21171 case DW_FORM_block2:
21172 case DW_FORM_block4:
21173 case DW_FORM_block:
21174 case DW_FORM_exprloc:
21175 case DW_FORM_data16:
21176 blk = DW_BLOCK (attr);
21177 if (TYPE_LENGTH (type) != blk->size)
21178 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21179 TYPE_LENGTH (type));
21180 *bytes = blk->data;
21181 break;
21182
21183 /* The DW_AT_const_value attributes are supposed to carry the
21184 symbol's value "represented as it would be on the target
21185 architecture." By the time we get here, it's already been
21186 converted to host endianness, so we just need to sign- or
21187 zero-extend it as appropriate. */
21188 case DW_FORM_data1:
21189 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21190 break;
21191 case DW_FORM_data2:
21192 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21193 break;
21194 case DW_FORM_data4:
21195 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21196 break;
21197 case DW_FORM_data8:
21198 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21199 break;
21200
21201 case DW_FORM_sdata:
21202 case DW_FORM_implicit_const:
21203 *value = DW_SND (attr);
21204 break;
21205
21206 case DW_FORM_udata:
21207 *value = DW_UNSND (attr);
21208 break;
21209
21210 default:
21211 complaint (_("unsupported const value attribute form: '%s'"),
21212 dwarf_form_name (attr->form));
21213 *value = 0;
21214 break;
21215 }
21216 }
21217
21218
21219 /* Copy constant value from an attribute to a symbol. */
21220
21221 static void
21222 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21223 struct dwarf2_cu *cu)
21224 {
21225 struct objfile *objfile = cu->per_objfile->objfile;
21226 LONGEST value;
21227 const gdb_byte *bytes;
21228 struct dwarf2_locexpr_baton *baton;
21229
21230 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21231 sym->print_name (),
21232 &objfile->objfile_obstack, cu,
21233 &value, &bytes, &baton);
21234
21235 if (baton != NULL)
21236 {
21237 SYMBOL_LOCATION_BATON (sym) = baton;
21238 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21239 }
21240 else if (bytes != NULL)
21241 {
21242 SYMBOL_VALUE_BYTES (sym) = bytes;
21243 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21244 }
21245 else
21246 {
21247 SYMBOL_VALUE (sym) = value;
21248 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21249 }
21250 }
21251
21252 /* Return the type of the die in question using its DW_AT_type attribute. */
21253
21254 static struct type *
21255 die_type (struct die_info *die, struct dwarf2_cu *cu)
21256 {
21257 struct attribute *type_attr;
21258
21259 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21260 if (!type_attr)
21261 {
21262 struct objfile *objfile = cu->per_objfile->objfile;
21263 /* A missing DW_AT_type represents a void type. */
21264 return objfile_type (objfile)->builtin_void;
21265 }
21266
21267 return lookup_die_type (die, type_attr, cu);
21268 }
21269
21270 /* True iff CU's producer generates GNAT Ada auxiliary information
21271 that allows to find parallel types through that information instead
21272 of having to do expensive parallel lookups by type name. */
21273
21274 static int
21275 need_gnat_info (struct dwarf2_cu *cu)
21276 {
21277 /* Assume that the Ada compiler was GNAT, which always produces
21278 the auxiliary information. */
21279 return (cu->language == language_ada);
21280 }
21281
21282 /* Return the auxiliary type of the die in question using its
21283 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21284 attribute is not present. */
21285
21286 static struct type *
21287 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21288 {
21289 struct attribute *type_attr;
21290
21291 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21292 if (!type_attr)
21293 return NULL;
21294
21295 return lookup_die_type (die, type_attr, cu);
21296 }
21297
21298 /* If DIE has a descriptive_type attribute, then set the TYPE's
21299 descriptive type accordingly. */
21300
21301 static void
21302 set_descriptive_type (struct type *type, struct die_info *die,
21303 struct dwarf2_cu *cu)
21304 {
21305 struct type *descriptive_type = die_descriptive_type (die, cu);
21306
21307 if (descriptive_type)
21308 {
21309 ALLOCATE_GNAT_AUX_TYPE (type);
21310 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21311 }
21312 }
21313
21314 /* Return the containing type of the die in question using its
21315 DW_AT_containing_type attribute. */
21316
21317 static struct type *
21318 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21319 {
21320 struct attribute *type_attr;
21321 struct objfile *objfile = cu->per_objfile->objfile;
21322
21323 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21324 if (!type_attr)
21325 error (_("Dwarf Error: Problem turning containing type into gdb type "
21326 "[in module %s]"), objfile_name (objfile));
21327
21328 return lookup_die_type (die, type_attr, cu);
21329 }
21330
21331 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21332
21333 static struct type *
21334 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21335 {
21336 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21337 struct objfile *objfile = dwarf2_per_objfile->objfile;
21338 char *saved;
21339
21340 std::string message
21341 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21342 objfile_name (objfile),
21343 sect_offset_str (cu->header.sect_off),
21344 sect_offset_str (die->sect_off));
21345 saved = obstack_strdup (&objfile->objfile_obstack, message);
21346
21347 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21348 }
21349
21350 /* Look up the type of DIE in CU using its type attribute ATTR.
21351 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21352 DW_AT_containing_type.
21353 If there is no type substitute an error marker. */
21354
21355 static struct type *
21356 lookup_die_type (struct die_info *die, const struct attribute *attr,
21357 struct dwarf2_cu *cu)
21358 {
21359 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21360 struct objfile *objfile = dwarf2_per_objfile->objfile;
21361 struct type *this_type;
21362
21363 gdb_assert (attr->name == DW_AT_type
21364 || attr->name == DW_AT_GNAT_descriptive_type
21365 || attr->name == DW_AT_containing_type);
21366
21367 /* First see if we have it cached. */
21368
21369 if (attr->form == DW_FORM_GNU_ref_alt)
21370 {
21371 struct dwarf2_per_cu_data *per_cu;
21372 sect_offset sect_off = attr->get_ref_die_offset ();
21373
21374 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21375 dwarf2_per_objfile);
21376 this_type = get_die_type_at_offset (sect_off, per_cu);
21377 }
21378 else if (attr->form_is_ref ())
21379 {
21380 sect_offset sect_off = attr->get_ref_die_offset ();
21381
21382 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21383 }
21384 else if (attr->form == DW_FORM_ref_sig8)
21385 {
21386 ULONGEST signature = DW_SIGNATURE (attr);
21387
21388 return get_signatured_type (die, signature, cu);
21389 }
21390 else
21391 {
21392 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21393 " at %s [in module %s]"),
21394 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21395 objfile_name (objfile));
21396 return build_error_marker_type (cu, die);
21397 }
21398
21399 /* If not cached we need to read it in. */
21400
21401 if (this_type == NULL)
21402 {
21403 struct die_info *type_die = NULL;
21404 struct dwarf2_cu *type_cu = cu;
21405
21406 if (attr->form_is_ref ())
21407 type_die = follow_die_ref (die, attr, &type_cu);
21408 if (type_die == NULL)
21409 return build_error_marker_type (cu, die);
21410 /* If we find the type now, it's probably because the type came
21411 from an inter-CU reference and the type's CU got expanded before
21412 ours. */
21413 this_type = read_type_die (type_die, type_cu);
21414 }
21415
21416 /* If we still don't have a type use an error marker. */
21417
21418 if (this_type == NULL)
21419 return build_error_marker_type (cu, die);
21420
21421 return this_type;
21422 }
21423
21424 /* Return the type in DIE, CU.
21425 Returns NULL for invalid types.
21426
21427 This first does a lookup in die_type_hash,
21428 and only reads the die in if necessary.
21429
21430 NOTE: This can be called when reading in partial or full symbols. */
21431
21432 static struct type *
21433 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21434 {
21435 struct type *this_type;
21436
21437 this_type = get_die_type (die, cu);
21438 if (this_type)
21439 return this_type;
21440
21441 return read_type_die_1 (die, cu);
21442 }
21443
21444 /* Read the type in DIE, CU.
21445 Returns NULL for invalid types. */
21446
21447 static struct type *
21448 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21449 {
21450 struct type *this_type = NULL;
21451
21452 switch (die->tag)
21453 {
21454 case DW_TAG_class_type:
21455 case DW_TAG_interface_type:
21456 case DW_TAG_structure_type:
21457 case DW_TAG_union_type:
21458 this_type = read_structure_type (die, cu);
21459 break;
21460 case DW_TAG_enumeration_type:
21461 this_type = read_enumeration_type (die, cu);
21462 break;
21463 case DW_TAG_subprogram:
21464 case DW_TAG_subroutine_type:
21465 case DW_TAG_inlined_subroutine:
21466 this_type = read_subroutine_type (die, cu);
21467 break;
21468 case DW_TAG_array_type:
21469 this_type = read_array_type (die, cu);
21470 break;
21471 case DW_TAG_set_type:
21472 this_type = read_set_type (die, cu);
21473 break;
21474 case DW_TAG_pointer_type:
21475 this_type = read_tag_pointer_type (die, cu);
21476 break;
21477 case DW_TAG_ptr_to_member_type:
21478 this_type = read_tag_ptr_to_member_type (die, cu);
21479 break;
21480 case DW_TAG_reference_type:
21481 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21482 break;
21483 case DW_TAG_rvalue_reference_type:
21484 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21485 break;
21486 case DW_TAG_const_type:
21487 this_type = read_tag_const_type (die, cu);
21488 break;
21489 case DW_TAG_volatile_type:
21490 this_type = read_tag_volatile_type (die, cu);
21491 break;
21492 case DW_TAG_restrict_type:
21493 this_type = read_tag_restrict_type (die, cu);
21494 break;
21495 case DW_TAG_string_type:
21496 this_type = read_tag_string_type (die, cu);
21497 break;
21498 case DW_TAG_typedef:
21499 this_type = read_typedef (die, cu);
21500 break;
21501 case DW_TAG_subrange_type:
21502 this_type = read_subrange_type (die, cu);
21503 break;
21504 case DW_TAG_base_type:
21505 this_type = read_base_type (die, cu);
21506 break;
21507 case DW_TAG_unspecified_type:
21508 this_type = read_unspecified_type (die, cu);
21509 break;
21510 case DW_TAG_namespace:
21511 this_type = read_namespace_type (die, cu);
21512 break;
21513 case DW_TAG_module:
21514 this_type = read_module_type (die, cu);
21515 break;
21516 case DW_TAG_atomic_type:
21517 this_type = read_tag_atomic_type (die, cu);
21518 break;
21519 default:
21520 complaint (_("unexpected tag in read_type_die: '%s'"),
21521 dwarf_tag_name (die->tag));
21522 break;
21523 }
21524
21525 return this_type;
21526 }
21527
21528 /* See if we can figure out if the class lives in a namespace. We do
21529 this by looking for a member function; its demangled name will
21530 contain namespace info, if there is any.
21531 Return the computed name or NULL.
21532 Space for the result is allocated on the objfile's obstack.
21533 This is the full-die version of guess_partial_die_structure_name.
21534 In this case we know DIE has no useful parent. */
21535
21536 static const char *
21537 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21538 {
21539 struct die_info *spec_die;
21540 struct dwarf2_cu *spec_cu;
21541 struct die_info *child;
21542 struct objfile *objfile = cu->per_objfile->objfile;
21543
21544 spec_cu = cu;
21545 spec_die = die_specification (die, &spec_cu);
21546 if (spec_die != NULL)
21547 {
21548 die = spec_die;
21549 cu = spec_cu;
21550 }
21551
21552 for (child = die->child;
21553 child != NULL;
21554 child = child->sibling)
21555 {
21556 if (child->tag == DW_TAG_subprogram)
21557 {
21558 const char *linkage_name = dw2_linkage_name (child, cu);
21559
21560 if (linkage_name != NULL)
21561 {
21562 gdb::unique_xmalloc_ptr<char> actual_name
21563 (language_class_name_from_physname (cu->language_defn,
21564 linkage_name));
21565 const char *name = NULL;
21566
21567 if (actual_name != NULL)
21568 {
21569 const char *die_name = dwarf2_name (die, cu);
21570
21571 if (die_name != NULL
21572 && strcmp (die_name, actual_name.get ()) != 0)
21573 {
21574 /* Strip off the class name from the full name.
21575 We want the prefix. */
21576 int die_name_len = strlen (die_name);
21577 int actual_name_len = strlen (actual_name.get ());
21578 const char *ptr = actual_name.get ();
21579
21580 /* Test for '::' as a sanity check. */
21581 if (actual_name_len > die_name_len + 2
21582 && ptr[actual_name_len - die_name_len - 1] == ':')
21583 name = obstack_strndup (
21584 &objfile->per_bfd->storage_obstack,
21585 ptr, actual_name_len - die_name_len - 2);
21586 }
21587 }
21588 return name;
21589 }
21590 }
21591 }
21592
21593 return NULL;
21594 }
21595
21596 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21597 prefix part in such case. See
21598 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21599
21600 static const char *
21601 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21602 {
21603 struct attribute *attr;
21604 const char *base;
21605
21606 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21607 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21608 return NULL;
21609
21610 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21611 return NULL;
21612
21613 attr = dw2_linkage_name_attr (die, cu);
21614 if (attr == NULL || DW_STRING (attr) == NULL)
21615 return NULL;
21616
21617 /* dwarf2_name had to be already called. */
21618 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21619
21620 /* Strip the base name, keep any leading namespaces/classes. */
21621 base = strrchr (DW_STRING (attr), ':');
21622 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21623 return "";
21624
21625 struct objfile *objfile = cu->per_objfile->objfile;
21626 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21627 DW_STRING (attr),
21628 &base[-1] - DW_STRING (attr));
21629 }
21630
21631 /* Return the name of the namespace/class that DIE is defined within,
21632 or "" if we can't tell. The caller should not xfree the result.
21633
21634 For example, if we're within the method foo() in the following
21635 code:
21636
21637 namespace N {
21638 class C {
21639 void foo () {
21640 }
21641 };
21642 }
21643
21644 then determine_prefix on foo's die will return "N::C". */
21645
21646 static const char *
21647 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21648 {
21649 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21650 struct die_info *parent, *spec_die;
21651 struct dwarf2_cu *spec_cu;
21652 struct type *parent_type;
21653 const char *retval;
21654
21655 if (cu->language != language_cplus
21656 && cu->language != language_fortran && cu->language != language_d
21657 && cu->language != language_rust)
21658 return "";
21659
21660 retval = anonymous_struct_prefix (die, cu);
21661 if (retval)
21662 return retval;
21663
21664 /* We have to be careful in the presence of DW_AT_specification.
21665 For example, with GCC 3.4, given the code
21666
21667 namespace N {
21668 void foo() {
21669 // Definition of N::foo.
21670 }
21671 }
21672
21673 then we'll have a tree of DIEs like this:
21674
21675 1: DW_TAG_compile_unit
21676 2: DW_TAG_namespace // N
21677 3: DW_TAG_subprogram // declaration of N::foo
21678 4: DW_TAG_subprogram // definition of N::foo
21679 DW_AT_specification // refers to die #3
21680
21681 Thus, when processing die #4, we have to pretend that we're in
21682 the context of its DW_AT_specification, namely the contex of die
21683 #3. */
21684 spec_cu = cu;
21685 spec_die = die_specification (die, &spec_cu);
21686 if (spec_die == NULL)
21687 parent = die->parent;
21688 else
21689 {
21690 parent = spec_die->parent;
21691 cu = spec_cu;
21692 }
21693
21694 if (parent == NULL)
21695 return "";
21696 else if (parent->building_fullname)
21697 {
21698 const char *name;
21699 const char *parent_name;
21700
21701 /* It has been seen on RealView 2.2 built binaries,
21702 DW_TAG_template_type_param types actually _defined_ as
21703 children of the parent class:
21704
21705 enum E {};
21706 template class <class Enum> Class{};
21707 Class<enum E> class_e;
21708
21709 1: DW_TAG_class_type (Class)
21710 2: DW_TAG_enumeration_type (E)
21711 3: DW_TAG_enumerator (enum1:0)
21712 3: DW_TAG_enumerator (enum2:1)
21713 ...
21714 2: DW_TAG_template_type_param
21715 DW_AT_type DW_FORM_ref_udata (E)
21716
21717 Besides being broken debug info, it can put GDB into an
21718 infinite loop. Consider:
21719
21720 When we're building the full name for Class<E>, we'll start
21721 at Class, and go look over its template type parameters,
21722 finding E. We'll then try to build the full name of E, and
21723 reach here. We're now trying to build the full name of E,
21724 and look over the parent DIE for containing scope. In the
21725 broken case, if we followed the parent DIE of E, we'd again
21726 find Class, and once again go look at its template type
21727 arguments, etc., etc. Simply don't consider such parent die
21728 as source-level parent of this die (it can't be, the language
21729 doesn't allow it), and break the loop here. */
21730 name = dwarf2_name (die, cu);
21731 parent_name = dwarf2_name (parent, cu);
21732 complaint (_("template param type '%s' defined within parent '%s'"),
21733 name ? name : "<unknown>",
21734 parent_name ? parent_name : "<unknown>");
21735 return "";
21736 }
21737 else
21738 switch (parent->tag)
21739 {
21740 case DW_TAG_namespace:
21741 parent_type = read_type_die (parent, cu);
21742 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21743 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21744 Work around this problem here. */
21745 if (cu->language == language_cplus
21746 && strcmp (parent_type->name (), "::") == 0)
21747 return "";
21748 /* We give a name to even anonymous namespaces. */
21749 return parent_type->name ();
21750 case DW_TAG_class_type:
21751 case DW_TAG_interface_type:
21752 case DW_TAG_structure_type:
21753 case DW_TAG_union_type:
21754 case DW_TAG_module:
21755 parent_type = read_type_die (parent, cu);
21756 if (parent_type->name () != NULL)
21757 return parent_type->name ();
21758 else
21759 /* An anonymous structure is only allowed non-static data
21760 members; no typedefs, no member functions, et cetera.
21761 So it does not need a prefix. */
21762 return "";
21763 case DW_TAG_compile_unit:
21764 case DW_TAG_partial_unit:
21765 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21766 if (cu->language == language_cplus
21767 && !dwarf2_per_objfile->per_bfd->types.empty ()
21768 && die->child != NULL
21769 && (die->tag == DW_TAG_class_type
21770 || die->tag == DW_TAG_structure_type
21771 || die->tag == DW_TAG_union_type))
21772 {
21773 const char *name = guess_full_die_structure_name (die, cu);
21774 if (name != NULL)
21775 return name;
21776 }
21777 return "";
21778 case DW_TAG_subprogram:
21779 /* Nested subroutines in Fortran get a prefix with the name
21780 of the parent's subroutine. */
21781 if (cu->language == language_fortran)
21782 {
21783 if ((die->tag == DW_TAG_subprogram)
21784 && (dwarf2_name (parent, cu) != NULL))
21785 return dwarf2_name (parent, cu);
21786 }
21787 return determine_prefix (parent, cu);
21788 case DW_TAG_enumeration_type:
21789 parent_type = read_type_die (parent, cu);
21790 if (TYPE_DECLARED_CLASS (parent_type))
21791 {
21792 if (parent_type->name () != NULL)
21793 return parent_type->name ();
21794 return "";
21795 }
21796 /* Fall through. */
21797 default:
21798 return determine_prefix (parent, cu);
21799 }
21800 }
21801
21802 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21803 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21804 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21805 an obconcat, otherwise allocate storage for the result. The CU argument is
21806 used to determine the language and hence, the appropriate separator. */
21807
21808 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21809
21810 static char *
21811 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21812 int physname, struct dwarf2_cu *cu)
21813 {
21814 const char *lead = "";
21815 const char *sep;
21816
21817 if (suffix == NULL || suffix[0] == '\0'
21818 || prefix == NULL || prefix[0] == '\0')
21819 sep = "";
21820 else if (cu->language == language_d)
21821 {
21822 /* For D, the 'main' function could be defined in any module, but it
21823 should never be prefixed. */
21824 if (strcmp (suffix, "D main") == 0)
21825 {
21826 prefix = "";
21827 sep = "";
21828 }
21829 else
21830 sep = ".";
21831 }
21832 else if (cu->language == language_fortran && physname)
21833 {
21834 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21835 DW_AT_MIPS_linkage_name is preferred and used instead. */
21836
21837 lead = "__";
21838 sep = "_MOD_";
21839 }
21840 else
21841 sep = "::";
21842
21843 if (prefix == NULL)
21844 prefix = "";
21845 if (suffix == NULL)
21846 suffix = "";
21847
21848 if (obs == NULL)
21849 {
21850 char *retval
21851 = ((char *)
21852 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21853
21854 strcpy (retval, lead);
21855 strcat (retval, prefix);
21856 strcat (retval, sep);
21857 strcat (retval, suffix);
21858 return retval;
21859 }
21860 else
21861 {
21862 /* We have an obstack. */
21863 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21864 }
21865 }
21866
21867 /* Get name of a die, return NULL if not found. */
21868
21869 static const char *
21870 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21871 struct objfile *objfile)
21872 {
21873 if (name && cu->language == language_cplus)
21874 {
21875 gdb::unique_xmalloc_ptr<char> canon_name
21876 = cp_canonicalize_string (name);
21877
21878 if (canon_name != nullptr)
21879 name = objfile->intern (canon_name.get ());
21880 }
21881
21882 return name;
21883 }
21884
21885 /* Get name of a die, return NULL if not found.
21886 Anonymous namespaces are converted to their magic string. */
21887
21888 static const char *
21889 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21890 {
21891 struct attribute *attr;
21892 struct objfile *objfile = cu->per_objfile->objfile;
21893
21894 attr = dwarf2_attr (die, DW_AT_name, cu);
21895 if ((!attr || !DW_STRING (attr))
21896 && die->tag != DW_TAG_namespace
21897 && die->tag != DW_TAG_class_type
21898 && die->tag != DW_TAG_interface_type
21899 && die->tag != DW_TAG_structure_type
21900 && die->tag != DW_TAG_union_type)
21901 return NULL;
21902
21903 switch (die->tag)
21904 {
21905 case DW_TAG_compile_unit:
21906 case DW_TAG_partial_unit:
21907 /* Compilation units have a DW_AT_name that is a filename, not
21908 a source language identifier. */
21909 case DW_TAG_enumeration_type:
21910 case DW_TAG_enumerator:
21911 /* These tags always have simple identifiers already; no need
21912 to canonicalize them. */
21913 return DW_STRING (attr);
21914
21915 case DW_TAG_namespace:
21916 if (attr != NULL && DW_STRING (attr) != NULL)
21917 return DW_STRING (attr);
21918 return CP_ANONYMOUS_NAMESPACE_STR;
21919
21920 case DW_TAG_class_type:
21921 case DW_TAG_interface_type:
21922 case DW_TAG_structure_type:
21923 case DW_TAG_union_type:
21924 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21925 structures or unions. These were of the form "._%d" in GCC 4.1,
21926 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21927 and GCC 4.4. We work around this problem by ignoring these. */
21928 if (attr && DW_STRING (attr)
21929 && (startswith (DW_STRING (attr), "._")
21930 || startswith (DW_STRING (attr), "<anonymous")))
21931 return NULL;
21932
21933 /* GCC might emit a nameless typedef that has a linkage name. See
21934 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21935 if (!attr || DW_STRING (attr) == NULL)
21936 {
21937 attr = dw2_linkage_name_attr (die, cu);
21938 if (attr == NULL || DW_STRING (attr) == NULL)
21939 return NULL;
21940
21941 /* Avoid demangling DW_STRING (attr) the second time on a second
21942 call for the same DIE. */
21943 if (!DW_STRING_IS_CANONICAL (attr))
21944 {
21945 gdb::unique_xmalloc_ptr<char> demangled
21946 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21947 if (demangled == nullptr)
21948 return nullptr;
21949
21950 DW_STRING (attr) = objfile->intern (demangled.get ());
21951 DW_STRING_IS_CANONICAL (attr) = 1;
21952 }
21953
21954 /* Strip any leading namespaces/classes, keep only the base name.
21955 DW_AT_name for named DIEs does not contain the prefixes. */
21956 const char *base = strrchr (DW_STRING (attr), ':');
21957 if (base && base > DW_STRING (attr) && base[-1] == ':')
21958 return &base[1];
21959 else
21960 return DW_STRING (attr);
21961 }
21962 break;
21963
21964 default:
21965 break;
21966 }
21967
21968 if (!DW_STRING_IS_CANONICAL (attr))
21969 {
21970 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21971 objfile);
21972 DW_STRING_IS_CANONICAL (attr) = 1;
21973 }
21974 return DW_STRING (attr);
21975 }
21976
21977 /* Return the die that this die in an extension of, or NULL if there
21978 is none. *EXT_CU is the CU containing DIE on input, and the CU
21979 containing the return value on output. */
21980
21981 static struct die_info *
21982 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21983 {
21984 struct attribute *attr;
21985
21986 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21987 if (attr == NULL)
21988 return NULL;
21989
21990 return follow_die_ref (die, attr, ext_cu);
21991 }
21992
21993 static void
21994 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21995 {
21996 unsigned int i;
21997
21998 print_spaces (indent, f);
21999 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22000 dwarf_tag_name (die->tag), die->abbrev,
22001 sect_offset_str (die->sect_off));
22002
22003 if (die->parent != NULL)
22004 {
22005 print_spaces (indent, f);
22006 fprintf_unfiltered (f, " parent at offset: %s\n",
22007 sect_offset_str (die->parent->sect_off));
22008 }
22009
22010 print_spaces (indent, f);
22011 fprintf_unfiltered (f, " has children: %s\n",
22012 dwarf_bool_name (die->child != NULL));
22013
22014 print_spaces (indent, f);
22015 fprintf_unfiltered (f, " attributes:\n");
22016
22017 for (i = 0; i < die->num_attrs; ++i)
22018 {
22019 print_spaces (indent, f);
22020 fprintf_unfiltered (f, " %s (%s) ",
22021 dwarf_attr_name (die->attrs[i].name),
22022 dwarf_form_name (die->attrs[i].form));
22023
22024 switch (die->attrs[i].form)
22025 {
22026 case DW_FORM_addr:
22027 case DW_FORM_addrx:
22028 case DW_FORM_GNU_addr_index:
22029 fprintf_unfiltered (f, "address: ");
22030 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22031 break;
22032 case DW_FORM_block2:
22033 case DW_FORM_block4:
22034 case DW_FORM_block:
22035 case DW_FORM_block1:
22036 fprintf_unfiltered (f, "block: size %s",
22037 pulongest (DW_BLOCK (&die->attrs[i])->size));
22038 break;
22039 case DW_FORM_exprloc:
22040 fprintf_unfiltered (f, "expression: size %s",
22041 pulongest (DW_BLOCK (&die->attrs[i])->size));
22042 break;
22043 case DW_FORM_data16:
22044 fprintf_unfiltered (f, "constant of 16 bytes");
22045 break;
22046 case DW_FORM_ref_addr:
22047 fprintf_unfiltered (f, "ref address: ");
22048 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22049 break;
22050 case DW_FORM_GNU_ref_alt:
22051 fprintf_unfiltered (f, "alt ref address: ");
22052 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22053 break;
22054 case DW_FORM_ref1:
22055 case DW_FORM_ref2:
22056 case DW_FORM_ref4:
22057 case DW_FORM_ref8:
22058 case DW_FORM_ref_udata:
22059 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22060 (long) (DW_UNSND (&die->attrs[i])));
22061 break;
22062 case DW_FORM_data1:
22063 case DW_FORM_data2:
22064 case DW_FORM_data4:
22065 case DW_FORM_data8:
22066 case DW_FORM_udata:
22067 case DW_FORM_sdata:
22068 fprintf_unfiltered (f, "constant: %s",
22069 pulongest (DW_UNSND (&die->attrs[i])));
22070 break;
22071 case DW_FORM_sec_offset:
22072 fprintf_unfiltered (f, "section offset: %s",
22073 pulongest (DW_UNSND (&die->attrs[i])));
22074 break;
22075 case DW_FORM_ref_sig8:
22076 fprintf_unfiltered (f, "signature: %s",
22077 hex_string (DW_SIGNATURE (&die->attrs[i])));
22078 break;
22079 case DW_FORM_string:
22080 case DW_FORM_strp:
22081 case DW_FORM_line_strp:
22082 case DW_FORM_strx:
22083 case DW_FORM_GNU_str_index:
22084 case DW_FORM_GNU_strp_alt:
22085 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22086 DW_STRING (&die->attrs[i])
22087 ? DW_STRING (&die->attrs[i]) : "",
22088 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22089 break;
22090 case DW_FORM_flag:
22091 if (DW_UNSND (&die->attrs[i]))
22092 fprintf_unfiltered (f, "flag: TRUE");
22093 else
22094 fprintf_unfiltered (f, "flag: FALSE");
22095 break;
22096 case DW_FORM_flag_present:
22097 fprintf_unfiltered (f, "flag: TRUE");
22098 break;
22099 case DW_FORM_indirect:
22100 /* The reader will have reduced the indirect form to
22101 the "base form" so this form should not occur. */
22102 fprintf_unfiltered (f,
22103 "unexpected attribute form: DW_FORM_indirect");
22104 break;
22105 case DW_FORM_implicit_const:
22106 fprintf_unfiltered (f, "constant: %s",
22107 plongest (DW_SND (&die->attrs[i])));
22108 break;
22109 default:
22110 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22111 die->attrs[i].form);
22112 break;
22113 }
22114 fprintf_unfiltered (f, "\n");
22115 }
22116 }
22117
22118 static void
22119 dump_die_for_error (struct die_info *die)
22120 {
22121 dump_die_shallow (gdb_stderr, 0, die);
22122 }
22123
22124 static void
22125 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22126 {
22127 int indent = level * 4;
22128
22129 gdb_assert (die != NULL);
22130
22131 if (level >= max_level)
22132 return;
22133
22134 dump_die_shallow (f, indent, die);
22135
22136 if (die->child != NULL)
22137 {
22138 print_spaces (indent, f);
22139 fprintf_unfiltered (f, " Children:");
22140 if (level + 1 < max_level)
22141 {
22142 fprintf_unfiltered (f, "\n");
22143 dump_die_1 (f, level + 1, max_level, die->child);
22144 }
22145 else
22146 {
22147 fprintf_unfiltered (f,
22148 " [not printed, max nesting level reached]\n");
22149 }
22150 }
22151
22152 if (die->sibling != NULL && level > 0)
22153 {
22154 dump_die_1 (f, level, max_level, die->sibling);
22155 }
22156 }
22157
22158 /* This is called from the pdie macro in gdbinit.in.
22159 It's not static so gcc will keep a copy callable from gdb. */
22160
22161 void
22162 dump_die (struct die_info *die, int max_level)
22163 {
22164 dump_die_1 (gdb_stdlog, 0, max_level, die);
22165 }
22166
22167 static void
22168 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22169 {
22170 void **slot;
22171
22172 slot = htab_find_slot_with_hash (cu->die_hash, die,
22173 to_underlying (die->sect_off),
22174 INSERT);
22175
22176 *slot = die;
22177 }
22178
22179 /* Follow reference or signature attribute ATTR of SRC_DIE.
22180 On entry *REF_CU is the CU of SRC_DIE.
22181 On exit *REF_CU is the CU of the result. */
22182
22183 static struct die_info *
22184 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22185 struct dwarf2_cu **ref_cu)
22186 {
22187 struct die_info *die;
22188
22189 if (attr->form_is_ref ())
22190 die = follow_die_ref (src_die, attr, ref_cu);
22191 else if (attr->form == DW_FORM_ref_sig8)
22192 die = follow_die_sig (src_die, attr, ref_cu);
22193 else
22194 {
22195 dump_die_for_error (src_die);
22196 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22197 objfile_name ((*ref_cu)->per_objfile->objfile));
22198 }
22199
22200 return die;
22201 }
22202
22203 /* Follow reference OFFSET.
22204 On entry *REF_CU is the CU of the source die referencing OFFSET.
22205 On exit *REF_CU is the CU of the result.
22206 Returns NULL if OFFSET is invalid. */
22207
22208 static struct die_info *
22209 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22210 struct dwarf2_cu **ref_cu)
22211 {
22212 struct die_info temp_die;
22213 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22214 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22215
22216 gdb_assert (cu->per_cu != NULL);
22217
22218 target_cu = cu;
22219
22220 if (cu->per_cu->is_debug_types)
22221 {
22222 /* .debug_types CUs cannot reference anything outside their CU.
22223 If they need to, they have to reference a signatured type via
22224 DW_FORM_ref_sig8. */
22225 if (!cu->header.offset_in_cu_p (sect_off))
22226 return NULL;
22227 }
22228 else if (offset_in_dwz != cu->per_cu->is_dwz
22229 || !cu->header.offset_in_cu_p (sect_off))
22230 {
22231 struct dwarf2_per_cu_data *per_cu;
22232
22233 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22234 dwarf2_per_objfile);
22235
22236 /* If necessary, add it to the queue and load its DIEs. */
22237 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22238 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22239
22240 target_cu = per_cu->cu;
22241 }
22242 else if (cu->dies == NULL)
22243 {
22244 /* We're loading full DIEs during partial symbol reading. */
22245 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22246 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22247 language_minimal);
22248 }
22249
22250 *ref_cu = target_cu;
22251 temp_die.sect_off = sect_off;
22252
22253 if (target_cu != cu)
22254 target_cu->ancestor = cu;
22255
22256 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22257 &temp_die,
22258 to_underlying (sect_off));
22259 }
22260
22261 /* Follow reference attribute ATTR of SRC_DIE.
22262 On entry *REF_CU is the CU of SRC_DIE.
22263 On exit *REF_CU is the CU of the result. */
22264
22265 static struct die_info *
22266 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22267 struct dwarf2_cu **ref_cu)
22268 {
22269 sect_offset sect_off = attr->get_ref_die_offset ();
22270 struct dwarf2_cu *cu = *ref_cu;
22271 struct die_info *die;
22272
22273 die = follow_die_offset (sect_off,
22274 (attr->form == DW_FORM_GNU_ref_alt
22275 || cu->per_cu->is_dwz),
22276 ref_cu);
22277 if (!die)
22278 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22279 "at %s [in module %s]"),
22280 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22281 objfile_name (cu->per_objfile->objfile));
22282
22283 return die;
22284 }
22285
22286 /* See read.h. */
22287
22288 struct dwarf2_locexpr_baton
22289 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22290 dwarf2_per_cu_data *per_cu,
22291 CORE_ADDR (*get_frame_pc) (void *baton),
22292 void *baton, bool resolve_abstract_p)
22293 {
22294 struct dwarf2_cu *cu;
22295 struct die_info *die;
22296 struct attribute *attr;
22297 struct dwarf2_locexpr_baton retval;
22298 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22299 struct objfile *objfile = dwarf2_per_objfile->objfile;
22300
22301 if (per_cu->cu == NULL)
22302 load_cu (per_cu, dwarf2_per_objfile, false);
22303 cu = per_cu->cu;
22304 if (cu == NULL)
22305 {
22306 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22307 Instead just throw an error, not much else we can do. */
22308 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22309 sect_offset_str (sect_off), objfile_name (objfile));
22310 }
22311
22312 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22313 if (!die)
22314 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22315 sect_offset_str (sect_off), objfile_name (objfile));
22316
22317 attr = dwarf2_attr (die, DW_AT_location, cu);
22318 if (!attr && resolve_abstract_p
22319 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22320 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22321 {
22322 CORE_ADDR pc = (*get_frame_pc) (baton);
22323 CORE_ADDR baseaddr = objfile->text_section_offset ();
22324 struct gdbarch *gdbarch = objfile->arch ();
22325
22326 for (const auto &cand_off
22327 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22328 {
22329 struct dwarf2_cu *cand_cu = cu;
22330 struct die_info *cand
22331 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22332 if (!cand
22333 || !cand->parent
22334 || cand->parent->tag != DW_TAG_subprogram)
22335 continue;
22336
22337 CORE_ADDR pc_low, pc_high;
22338 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22339 if (pc_low == ((CORE_ADDR) -1))
22340 continue;
22341 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22342 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22343 if (!(pc_low <= pc && pc < pc_high))
22344 continue;
22345
22346 die = cand;
22347 attr = dwarf2_attr (die, DW_AT_location, cu);
22348 break;
22349 }
22350 }
22351
22352 if (!attr)
22353 {
22354 /* DWARF: "If there is no such attribute, then there is no effect.".
22355 DATA is ignored if SIZE is 0. */
22356
22357 retval.data = NULL;
22358 retval.size = 0;
22359 }
22360 else if (attr->form_is_section_offset ())
22361 {
22362 struct dwarf2_loclist_baton loclist_baton;
22363 CORE_ADDR pc = (*get_frame_pc) (baton);
22364 size_t size;
22365
22366 fill_in_loclist_baton (cu, &loclist_baton, attr);
22367
22368 retval.data = dwarf2_find_location_expression (&loclist_baton,
22369 &size, pc);
22370 retval.size = size;
22371 }
22372 else
22373 {
22374 if (!attr->form_is_block ())
22375 error (_("Dwarf Error: DIE at %s referenced in module %s "
22376 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22377 sect_offset_str (sect_off), objfile_name (objfile));
22378
22379 retval.data = DW_BLOCK (attr)->data;
22380 retval.size = DW_BLOCK (attr)->size;
22381 }
22382 retval.per_objfile = dwarf2_per_objfile;
22383 retval.per_cu = cu->per_cu;
22384
22385 age_cached_comp_units (dwarf2_per_objfile);
22386
22387 return retval;
22388 }
22389
22390 /* See read.h. */
22391
22392 struct dwarf2_locexpr_baton
22393 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22394 dwarf2_per_cu_data *per_cu,
22395 CORE_ADDR (*get_frame_pc) (void *baton),
22396 void *baton)
22397 {
22398 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22399
22400 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22401 }
22402
22403 /* Write a constant of a given type as target-ordered bytes into
22404 OBSTACK. */
22405
22406 static const gdb_byte *
22407 write_constant_as_bytes (struct obstack *obstack,
22408 enum bfd_endian byte_order,
22409 struct type *type,
22410 ULONGEST value,
22411 LONGEST *len)
22412 {
22413 gdb_byte *result;
22414
22415 *len = TYPE_LENGTH (type);
22416 result = (gdb_byte *) obstack_alloc (obstack, *len);
22417 store_unsigned_integer (result, *len, byte_order, value);
22418
22419 return result;
22420 }
22421
22422 /* See read.h. */
22423
22424 const gdb_byte *
22425 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22426 dwarf2_per_cu_data *per_cu,
22427 obstack *obstack,
22428 LONGEST *len)
22429 {
22430 struct dwarf2_cu *cu;
22431 struct die_info *die;
22432 struct attribute *attr;
22433 const gdb_byte *result = NULL;
22434 struct type *type;
22435 LONGEST value;
22436 enum bfd_endian byte_order;
22437 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22438
22439 if (per_cu->cu == NULL)
22440 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
22441 cu = per_cu->cu;
22442 if (cu == NULL)
22443 {
22444 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22445 Instead just throw an error, not much else we can do. */
22446 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22447 sect_offset_str (sect_off), objfile_name (objfile));
22448 }
22449
22450 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22451 if (!die)
22452 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22453 sect_offset_str (sect_off), objfile_name (objfile));
22454
22455 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22456 if (attr == NULL)
22457 return NULL;
22458
22459 byte_order = (bfd_big_endian (objfile->obfd)
22460 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22461
22462 switch (attr->form)
22463 {
22464 case DW_FORM_addr:
22465 case DW_FORM_addrx:
22466 case DW_FORM_GNU_addr_index:
22467 {
22468 gdb_byte *tem;
22469
22470 *len = cu->header.addr_size;
22471 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22472 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22473 result = tem;
22474 }
22475 break;
22476 case DW_FORM_string:
22477 case DW_FORM_strp:
22478 case DW_FORM_strx:
22479 case DW_FORM_GNU_str_index:
22480 case DW_FORM_GNU_strp_alt:
22481 /* DW_STRING is already allocated on the objfile obstack, point
22482 directly to it. */
22483 result = (const gdb_byte *) DW_STRING (attr);
22484 *len = strlen (DW_STRING (attr));
22485 break;
22486 case DW_FORM_block1:
22487 case DW_FORM_block2:
22488 case DW_FORM_block4:
22489 case DW_FORM_block:
22490 case DW_FORM_exprloc:
22491 case DW_FORM_data16:
22492 result = DW_BLOCK (attr)->data;
22493 *len = DW_BLOCK (attr)->size;
22494 break;
22495
22496 /* The DW_AT_const_value attributes are supposed to carry the
22497 symbol's value "represented as it would be on the target
22498 architecture." By the time we get here, it's already been
22499 converted to host endianness, so we just need to sign- or
22500 zero-extend it as appropriate. */
22501 case DW_FORM_data1:
22502 type = die_type (die, cu);
22503 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22504 if (result == NULL)
22505 result = write_constant_as_bytes (obstack, byte_order,
22506 type, value, len);
22507 break;
22508 case DW_FORM_data2:
22509 type = die_type (die, cu);
22510 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22511 if (result == NULL)
22512 result = write_constant_as_bytes (obstack, byte_order,
22513 type, value, len);
22514 break;
22515 case DW_FORM_data4:
22516 type = die_type (die, cu);
22517 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22518 if (result == NULL)
22519 result = write_constant_as_bytes (obstack, byte_order,
22520 type, value, len);
22521 break;
22522 case DW_FORM_data8:
22523 type = die_type (die, cu);
22524 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22525 if (result == NULL)
22526 result = write_constant_as_bytes (obstack, byte_order,
22527 type, value, len);
22528 break;
22529
22530 case DW_FORM_sdata:
22531 case DW_FORM_implicit_const:
22532 type = die_type (die, cu);
22533 result = write_constant_as_bytes (obstack, byte_order,
22534 type, DW_SND (attr), len);
22535 break;
22536
22537 case DW_FORM_udata:
22538 type = die_type (die, cu);
22539 result = write_constant_as_bytes (obstack, byte_order,
22540 type, DW_UNSND (attr), len);
22541 break;
22542
22543 default:
22544 complaint (_("unsupported const value attribute form: '%s'"),
22545 dwarf_form_name (attr->form));
22546 break;
22547 }
22548
22549 return result;
22550 }
22551
22552 /* See read.h. */
22553
22554 struct type *
22555 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22556 dwarf2_per_cu_data *per_cu)
22557 {
22558 struct dwarf2_cu *cu;
22559 struct die_info *die;
22560
22561 if (per_cu->cu == NULL)
22562 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
22563 cu = per_cu->cu;
22564 if (!cu)
22565 return NULL;
22566
22567 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22568 if (!die)
22569 return NULL;
22570
22571 return die_type (die, cu);
22572 }
22573
22574 /* See read.h. */
22575
22576 struct type *
22577 dwarf2_get_die_type (cu_offset die_offset,
22578 struct dwarf2_per_cu_data *per_cu)
22579 {
22580 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22581 return get_die_type_at_offset (die_offset_sect, per_cu);
22582 }
22583
22584 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22585 On entry *REF_CU is the CU of SRC_DIE.
22586 On exit *REF_CU is the CU of the result.
22587 Returns NULL if the referenced DIE isn't found. */
22588
22589 static struct die_info *
22590 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22591 struct dwarf2_cu **ref_cu)
22592 {
22593 struct die_info temp_die;
22594 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22595 struct die_info *die;
22596
22597 /* While it might be nice to assert sig_type->type == NULL here,
22598 we can get here for DW_AT_imported_declaration where we need
22599 the DIE not the type. */
22600
22601 /* If necessary, add it to the queue and load its DIEs. */
22602
22603 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22604 read_signatured_type (sig_type, (*ref_cu)->per_objfile);
22605
22606 sig_cu = sig_type->per_cu.cu;
22607 gdb_assert (sig_cu != NULL);
22608 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22609 temp_die.sect_off = sig_type->type_offset_in_section;
22610 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22611 to_underlying (temp_die.sect_off));
22612 if (die)
22613 {
22614 struct dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22615
22616 /* For .gdb_index version 7 keep track of included TUs.
22617 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22618 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22619 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22620 {
22621 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22622 }
22623
22624 *ref_cu = sig_cu;
22625 if (sig_cu != cu)
22626 sig_cu->ancestor = cu;
22627
22628 return die;
22629 }
22630
22631 return NULL;
22632 }
22633
22634 /* Follow signatured type referenced by ATTR in SRC_DIE.
22635 On entry *REF_CU is the CU of SRC_DIE.
22636 On exit *REF_CU is the CU of the result.
22637 The result is the DIE of the type.
22638 If the referenced type cannot be found an error is thrown. */
22639
22640 static struct die_info *
22641 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22642 struct dwarf2_cu **ref_cu)
22643 {
22644 ULONGEST signature = DW_SIGNATURE (attr);
22645 struct signatured_type *sig_type;
22646 struct die_info *die;
22647
22648 gdb_assert (attr->form == DW_FORM_ref_sig8);
22649
22650 sig_type = lookup_signatured_type (*ref_cu, signature);
22651 /* sig_type will be NULL if the signatured type is missing from
22652 the debug info. */
22653 if (sig_type == NULL)
22654 {
22655 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22656 " from DIE at %s [in module %s]"),
22657 hex_string (signature), sect_offset_str (src_die->sect_off),
22658 objfile_name ((*ref_cu)->per_objfile->objfile));
22659 }
22660
22661 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22662 if (die == NULL)
22663 {
22664 dump_die_for_error (src_die);
22665 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22666 " from DIE at %s [in module %s]"),
22667 hex_string (signature), sect_offset_str (src_die->sect_off),
22668 objfile_name ((*ref_cu)->per_objfile->objfile));
22669 }
22670
22671 return die;
22672 }
22673
22674 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22675 reading in and processing the type unit if necessary. */
22676
22677 static struct type *
22678 get_signatured_type (struct die_info *die, ULONGEST signature,
22679 struct dwarf2_cu *cu)
22680 {
22681 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22682 struct signatured_type *sig_type;
22683 struct dwarf2_cu *type_cu;
22684 struct die_info *type_die;
22685 struct type *type;
22686
22687 sig_type = lookup_signatured_type (cu, signature);
22688 /* sig_type will be NULL if the signatured type is missing from
22689 the debug info. */
22690 if (sig_type == NULL)
22691 {
22692 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22693 " from DIE at %s [in module %s]"),
22694 hex_string (signature), sect_offset_str (die->sect_off),
22695 objfile_name (dwarf2_per_objfile->objfile));
22696 return build_error_marker_type (cu, die);
22697 }
22698
22699 /* If we already know the type we're done. */
22700 if (sig_type->type != NULL)
22701 return sig_type->type;
22702
22703 type_cu = cu;
22704 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22705 if (type_die != NULL)
22706 {
22707 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22708 is created. This is important, for example, because for c++ classes
22709 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22710 type = read_type_die (type_die, type_cu);
22711 if (type == NULL)
22712 {
22713 complaint (_("Dwarf Error: Cannot build signatured type %s"
22714 " referenced from DIE at %s [in module %s]"),
22715 hex_string (signature), sect_offset_str (die->sect_off),
22716 objfile_name (dwarf2_per_objfile->objfile));
22717 type = build_error_marker_type (cu, die);
22718 }
22719 }
22720 else
22721 {
22722 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22723 " from DIE at %s [in module %s]"),
22724 hex_string (signature), sect_offset_str (die->sect_off),
22725 objfile_name (dwarf2_per_objfile->objfile));
22726 type = build_error_marker_type (cu, die);
22727 }
22728 sig_type->type = type;
22729
22730 return type;
22731 }
22732
22733 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22734 reading in and processing the type unit if necessary. */
22735
22736 static struct type *
22737 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22738 struct dwarf2_cu *cu) /* ARI: editCase function */
22739 {
22740 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22741 if (attr->form_is_ref ())
22742 {
22743 struct dwarf2_cu *type_cu = cu;
22744 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22745
22746 return read_type_die (type_die, type_cu);
22747 }
22748 else if (attr->form == DW_FORM_ref_sig8)
22749 {
22750 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22751 }
22752 else
22753 {
22754 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22755
22756 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22757 " at %s [in module %s]"),
22758 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22759 objfile_name (dwarf2_per_objfile->objfile));
22760 return build_error_marker_type (cu, die);
22761 }
22762 }
22763
22764 /* Load the DIEs associated with type unit PER_CU into memory. */
22765
22766 static void
22767 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22768 dwarf2_per_objfile *per_objfile)
22769 {
22770 struct signatured_type *sig_type;
22771
22772 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22773 gdb_assert (! per_cu->type_unit_group_p ());
22774
22775 /* We have the per_cu, but we need the signatured_type.
22776 Fortunately this is an easy translation. */
22777 gdb_assert (per_cu->is_debug_types);
22778 sig_type = (struct signatured_type *) per_cu;
22779
22780 gdb_assert (per_cu->cu == NULL);
22781
22782 read_signatured_type (sig_type, per_objfile);
22783
22784 gdb_assert (per_cu->cu != NULL);
22785 }
22786
22787 /* Read in a signatured type and build its CU and DIEs.
22788 If the type is a stub for the real type in a DWO file,
22789 read in the real type from the DWO file as well. */
22790
22791 static void
22792 read_signatured_type (signatured_type *sig_type,
22793 dwarf2_per_objfile *per_objfile)
22794 {
22795 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22796
22797 gdb_assert (per_cu->is_debug_types);
22798 gdb_assert (per_cu->cu == NULL);
22799
22800 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
22801
22802 if (!reader.dummy_p)
22803 {
22804 struct dwarf2_cu *cu = reader.cu;
22805 const gdb_byte *info_ptr = reader.info_ptr;
22806
22807 gdb_assert (cu->die_hash == NULL);
22808 cu->die_hash =
22809 htab_create_alloc_ex (cu->header.length / 12,
22810 die_hash,
22811 die_eq,
22812 NULL,
22813 &cu->comp_unit_obstack,
22814 hashtab_obstack_allocate,
22815 dummy_obstack_deallocate);
22816
22817 if (reader.comp_unit_die->has_children)
22818 reader.comp_unit_die->child
22819 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22820 reader.comp_unit_die);
22821 cu->dies = reader.comp_unit_die;
22822 /* comp_unit_die is not stored in die_hash, no need. */
22823
22824 /* We try not to read any attributes in this function, because
22825 not all CUs needed for references have been loaded yet, and
22826 symbol table processing isn't initialized. But we have to
22827 set the CU language, or we won't be able to build types
22828 correctly. Similarly, if we do not read the producer, we can
22829 not apply producer-specific interpretation. */
22830 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22831
22832 reader.keep ();
22833 }
22834
22835 sig_type->per_cu.tu_read = 1;
22836 }
22837
22838 /* Decode simple location descriptions.
22839 Given a pointer to a dwarf block that defines a location, compute
22840 the location and return the value. If COMPUTED is non-null, it is
22841 set to true to indicate that decoding was successful, and false
22842 otherwise. If COMPUTED is null, then this function may emit a
22843 complaint. */
22844
22845 static CORE_ADDR
22846 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22847 {
22848 struct objfile *objfile = cu->per_objfile->objfile;
22849 size_t i;
22850 size_t size = blk->size;
22851 const gdb_byte *data = blk->data;
22852 CORE_ADDR stack[64];
22853 int stacki;
22854 unsigned int bytes_read, unsnd;
22855 gdb_byte op;
22856
22857 if (computed != nullptr)
22858 *computed = false;
22859
22860 i = 0;
22861 stacki = 0;
22862 stack[stacki] = 0;
22863 stack[++stacki] = 0;
22864
22865 while (i < size)
22866 {
22867 op = data[i++];
22868 switch (op)
22869 {
22870 case DW_OP_lit0:
22871 case DW_OP_lit1:
22872 case DW_OP_lit2:
22873 case DW_OP_lit3:
22874 case DW_OP_lit4:
22875 case DW_OP_lit5:
22876 case DW_OP_lit6:
22877 case DW_OP_lit7:
22878 case DW_OP_lit8:
22879 case DW_OP_lit9:
22880 case DW_OP_lit10:
22881 case DW_OP_lit11:
22882 case DW_OP_lit12:
22883 case DW_OP_lit13:
22884 case DW_OP_lit14:
22885 case DW_OP_lit15:
22886 case DW_OP_lit16:
22887 case DW_OP_lit17:
22888 case DW_OP_lit18:
22889 case DW_OP_lit19:
22890 case DW_OP_lit20:
22891 case DW_OP_lit21:
22892 case DW_OP_lit22:
22893 case DW_OP_lit23:
22894 case DW_OP_lit24:
22895 case DW_OP_lit25:
22896 case DW_OP_lit26:
22897 case DW_OP_lit27:
22898 case DW_OP_lit28:
22899 case DW_OP_lit29:
22900 case DW_OP_lit30:
22901 case DW_OP_lit31:
22902 stack[++stacki] = op - DW_OP_lit0;
22903 break;
22904
22905 case DW_OP_reg0:
22906 case DW_OP_reg1:
22907 case DW_OP_reg2:
22908 case DW_OP_reg3:
22909 case DW_OP_reg4:
22910 case DW_OP_reg5:
22911 case DW_OP_reg6:
22912 case DW_OP_reg7:
22913 case DW_OP_reg8:
22914 case DW_OP_reg9:
22915 case DW_OP_reg10:
22916 case DW_OP_reg11:
22917 case DW_OP_reg12:
22918 case DW_OP_reg13:
22919 case DW_OP_reg14:
22920 case DW_OP_reg15:
22921 case DW_OP_reg16:
22922 case DW_OP_reg17:
22923 case DW_OP_reg18:
22924 case DW_OP_reg19:
22925 case DW_OP_reg20:
22926 case DW_OP_reg21:
22927 case DW_OP_reg22:
22928 case DW_OP_reg23:
22929 case DW_OP_reg24:
22930 case DW_OP_reg25:
22931 case DW_OP_reg26:
22932 case DW_OP_reg27:
22933 case DW_OP_reg28:
22934 case DW_OP_reg29:
22935 case DW_OP_reg30:
22936 case DW_OP_reg31:
22937 stack[++stacki] = op - DW_OP_reg0;
22938 if (i < size)
22939 {
22940 if (computed == nullptr)
22941 dwarf2_complex_location_expr_complaint ();
22942 else
22943 return 0;
22944 }
22945 break;
22946
22947 case DW_OP_regx:
22948 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22949 i += bytes_read;
22950 stack[++stacki] = unsnd;
22951 if (i < size)
22952 {
22953 if (computed == nullptr)
22954 dwarf2_complex_location_expr_complaint ();
22955 else
22956 return 0;
22957 }
22958 break;
22959
22960 case DW_OP_addr:
22961 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22962 &bytes_read);
22963 i += bytes_read;
22964 break;
22965
22966 case DW_OP_const1u:
22967 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22968 i += 1;
22969 break;
22970
22971 case DW_OP_const1s:
22972 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22973 i += 1;
22974 break;
22975
22976 case DW_OP_const2u:
22977 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22978 i += 2;
22979 break;
22980
22981 case DW_OP_const2s:
22982 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22983 i += 2;
22984 break;
22985
22986 case DW_OP_const4u:
22987 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22988 i += 4;
22989 break;
22990
22991 case DW_OP_const4s:
22992 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22993 i += 4;
22994 break;
22995
22996 case DW_OP_const8u:
22997 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22998 i += 8;
22999 break;
23000
23001 case DW_OP_constu:
23002 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23003 &bytes_read);
23004 i += bytes_read;
23005 break;
23006
23007 case DW_OP_consts:
23008 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23009 i += bytes_read;
23010 break;
23011
23012 case DW_OP_dup:
23013 stack[stacki + 1] = stack[stacki];
23014 stacki++;
23015 break;
23016
23017 case DW_OP_plus:
23018 stack[stacki - 1] += stack[stacki];
23019 stacki--;
23020 break;
23021
23022 case DW_OP_plus_uconst:
23023 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23024 &bytes_read);
23025 i += bytes_read;
23026 break;
23027
23028 case DW_OP_minus:
23029 stack[stacki - 1] -= stack[stacki];
23030 stacki--;
23031 break;
23032
23033 case DW_OP_deref:
23034 /* If we're not the last op, then we definitely can't encode
23035 this using GDB's address_class enum. This is valid for partial
23036 global symbols, although the variable's address will be bogus
23037 in the psymtab. */
23038 if (i < size)
23039 {
23040 if (computed == nullptr)
23041 dwarf2_complex_location_expr_complaint ();
23042 else
23043 return 0;
23044 }
23045 break;
23046
23047 case DW_OP_GNU_push_tls_address:
23048 case DW_OP_form_tls_address:
23049 /* The top of the stack has the offset from the beginning
23050 of the thread control block at which the variable is located. */
23051 /* Nothing should follow this operator, so the top of stack would
23052 be returned. */
23053 /* This is valid for partial global symbols, but the variable's
23054 address will be bogus in the psymtab. Make it always at least
23055 non-zero to not look as a variable garbage collected by linker
23056 which have DW_OP_addr 0. */
23057 if (i < size)
23058 {
23059 if (computed == nullptr)
23060 dwarf2_complex_location_expr_complaint ();
23061 else
23062 return 0;
23063 }
23064 stack[stacki]++;
23065 break;
23066
23067 case DW_OP_GNU_uninit:
23068 if (computed != nullptr)
23069 return 0;
23070 break;
23071
23072 case DW_OP_addrx:
23073 case DW_OP_GNU_addr_index:
23074 case DW_OP_GNU_const_index:
23075 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23076 &bytes_read);
23077 i += bytes_read;
23078 break;
23079
23080 default:
23081 if (computed == nullptr)
23082 {
23083 const char *name = get_DW_OP_name (op);
23084
23085 if (name)
23086 complaint (_("unsupported stack op: '%s'"),
23087 name);
23088 else
23089 complaint (_("unsupported stack op: '%02x'"),
23090 op);
23091 }
23092
23093 return (stack[stacki]);
23094 }
23095
23096 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23097 outside of the allocated space. Also enforce minimum>0. */
23098 if (stacki >= ARRAY_SIZE (stack) - 1)
23099 {
23100 if (computed == nullptr)
23101 complaint (_("location description stack overflow"));
23102 return 0;
23103 }
23104
23105 if (stacki <= 0)
23106 {
23107 if (computed == nullptr)
23108 complaint (_("location description stack underflow"));
23109 return 0;
23110 }
23111 }
23112
23113 if (computed != nullptr)
23114 *computed = true;
23115 return (stack[stacki]);
23116 }
23117
23118 /* memory allocation interface */
23119
23120 static struct dwarf_block *
23121 dwarf_alloc_block (struct dwarf2_cu *cu)
23122 {
23123 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23124 }
23125
23126 static struct die_info *
23127 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23128 {
23129 struct die_info *die;
23130 size_t size = sizeof (struct die_info);
23131
23132 if (num_attrs > 1)
23133 size += (num_attrs - 1) * sizeof (struct attribute);
23134
23135 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23136 memset (die, 0, sizeof (struct die_info));
23137 return (die);
23138 }
23139
23140 \f
23141
23142 /* Macro support. */
23143
23144 /* An overload of dwarf_decode_macros that finds the correct section
23145 and ensures it is read in before calling the other overload. */
23146
23147 static void
23148 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23149 int section_is_gnu)
23150 {
23151 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23152 struct objfile *objfile = dwarf2_per_objfile->objfile;
23153 const struct line_header *lh = cu->line_header;
23154 unsigned int offset_size = cu->header.offset_size;
23155 struct dwarf2_section_info *section;
23156 const char *section_name;
23157
23158 if (cu->dwo_unit != nullptr)
23159 {
23160 if (section_is_gnu)
23161 {
23162 section = &cu->dwo_unit->dwo_file->sections.macro;
23163 section_name = ".debug_macro.dwo";
23164 }
23165 else
23166 {
23167 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23168 section_name = ".debug_macinfo.dwo";
23169 }
23170 }
23171 else
23172 {
23173 if (section_is_gnu)
23174 {
23175 section = &dwarf2_per_objfile->per_bfd->macro;
23176 section_name = ".debug_macro";
23177 }
23178 else
23179 {
23180 section = &dwarf2_per_objfile->per_bfd->macinfo;
23181 section_name = ".debug_macinfo";
23182 }
23183 }
23184
23185 section->read (objfile);
23186 if (section->buffer == nullptr)
23187 {
23188 complaint (_("missing %s section"), section_name);
23189 return;
23190 }
23191
23192 buildsym_compunit *builder = cu->get_builder ();
23193
23194 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23195 offset_size, offset, section_is_gnu);
23196 }
23197
23198 /* Return the .debug_loc section to use for CU.
23199 For DWO files use .debug_loc.dwo. */
23200
23201 static struct dwarf2_section_info *
23202 cu_debug_loc_section (struct dwarf2_cu *cu)
23203 {
23204 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23205
23206 if (cu->dwo_unit)
23207 {
23208 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23209
23210 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23211 }
23212 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23213 : &dwarf2_per_objfile->per_bfd->loc);
23214 }
23215
23216 /* A helper function that fills in a dwarf2_loclist_baton. */
23217
23218 static void
23219 fill_in_loclist_baton (struct dwarf2_cu *cu,
23220 struct dwarf2_loclist_baton *baton,
23221 const struct attribute *attr)
23222 {
23223 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23224 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23225
23226 section->read (dwarf2_per_objfile->objfile);
23227
23228 baton->per_objfile = dwarf2_per_objfile;
23229 baton->per_cu = cu->per_cu;
23230 gdb_assert (baton->per_cu);
23231 /* We don't know how long the location list is, but make sure we
23232 don't run off the edge of the section. */
23233 baton->size = section->size - DW_UNSND (attr);
23234 baton->data = section->buffer + DW_UNSND (attr);
23235 if (cu->base_address.has_value ())
23236 baton->base_address = *cu->base_address;
23237 else
23238 baton->base_address = 0;
23239 baton->from_dwo = cu->dwo_unit != NULL;
23240 }
23241
23242 static void
23243 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23244 struct dwarf2_cu *cu, int is_block)
23245 {
23246 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23247 struct objfile *objfile = dwarf2_per_objfile->objfile;
23248 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23249
23250 if (attr->form_is_section_offset ()
23251 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23252 the section. If so, fall through to the complaint in the
23253 other branch. */
23254 && DW_UNSND (attr) < section->get_size (objfile))
23255 {
23256 struct dwarf2_loclist_baton *baton;
23257
23258 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23259
23260 fill_in_loclist_baton (cu, baton, attr);
23261
23262 if (!cu->base_address.has_value ())
23263 complaint (_("Location list used without "
23264 "specifying the CU base address."));
23265
23266 SYMBOL_ACLASS_INDEX (sym) = (is_block
23267 ? dwarf2_loclist_block_index
23268 : dwarf2_loclist_index);
23269 SYMBOL_LOCATION_BATON (sym) = baton;
23270 }
23271 else
23272 {
23273 struct dwarf2_locexpr_baton *baton;
23274
23275 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23276 baton->per_objfile = dwarf2_per_objfile;
23277 baton->per_cu = cu->per_cu;
23278 gdb_assert (baton->per_cu);
23279
23280 if (attr->form_is_block ())
23281 {
23282 /* Note that we're just copying the block's data pointer
23283 here, not the actual data. We're still pointing into the
23284 info_buffer for SYM's objfile; right now we never release
23285 that buffer, but when we do clean up properly this may
23286 need to change. */
23287 baton->size = DW_BLOCK (attr)->size;
23288 baton->data = DW_BLOCK (attr)->data;
23289 }
23290 else
23291 {
23292 dwarf2_invalid_attrib_class_complaint ("location description",
23293 sym->natural_name ());
23294 baton->size = 0;
23295 }
23296
23297 SYMBOL_ACLASS_INDEX (sym) = (is_block
23298 ? dwarf2_locexpr_block_index
23299 : dwarf2_locexpr_index);
23300 SYMBOL_LOCATION_BATON (sym) = baton;
23301 }
23302 }
23303
23304 /* See read.h. */
23305
23306 struct objfile *
23307 dwarf2_per_cu_data::objfile () const
23308 {
23309 struct objfile *objfile = dwarf2_per_objfile->objfile;
23310
23311 /* Return the master objfile, so that we can report and look up the
23312 correct file containing this variable. */
23313 if (objfile->separate_debug_objfile_backlink)
23314 objfile = objfile->separate_debug_objfile_backlink;
23315
23316 return objfile;
23317 }
23318
23319 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23320 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23321 CU_HEADERP first. */
23322
23323 static const struct comp_unit_head *
23324 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23325 const struct dwarf2_per_cu_data *per_cu)
23326 {
23327 const gdb_byte *info_ptr;
23328
23329 if (per_cu->cu)
23330 return &per_cu->cu->header;
23331
23332 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23333
23334 memset (cu_headerp, 0, sizeof (*cu_headerp));
23335 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23336 rcuh_kind::COMPILE);
23337
23338 return cu_headerp;
23339 }
23340
23341 /* See read.h. */
23342
23343 int
23344 dwarf2_per_cu_data::addr_size () const
23345 {
23346 struct comp_unit_head cu_header_local;
23347 const struct comp_unit_head *cu_headerp;
23348
23349 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23350
23351 return cu_headerp->addr_size;
23352 }
23353
23354 /* See read.h. */
23355
23356 int
23357 dwarf2_per_cu_data::offset_size () const
23358 {
23359 struct comp_unit_head cu_header_local;
23360 const struct comp_unit_head *cu_headerp;
23361
23362 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23363
23364 return cu_headerp->offset_size;
23365 }
23366
23367 /* See read.h. */
23368
23369 int
23370 dwarf2_per_cu_data::ref_addr_size () const
23371 {
23372 struct comp_unit_head cu_header_local;
23373 const struct comp_unit_head *cu_headerp;
23374
23375 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23376
23377 if (cu_headerp->version == 2)
23378 return cu_headerp->addr_size;
23379 else
23380 return cu_headerp->offset_size;
23381 }
23382
23383 /* See read.h. */
23384
23385 CORE_ADDR
23386 dwarf2_per_cu_data::text_offset () const
23387 {
23388 struct objfile *objfile = dwarf2_per_objfile->objfile;
23389
23390 return objfile->text_section_offset ();
23391 }
23392
23393 /* See read.h. */
23394
23395 struct type *
23396 dwarf2_per_cu_data::addr_type () const
23397 {
23398 struct objfile *objfile = dwarf2_per_objfile->objfile;
23399 struct type *void_type = objfile_type (objfile)->builtin_void;
23400 struct type *addr_type = lookup_pointer_type (void_type);
23401 int addr_size = this->addr_size ();
23402
23403 if (TYPE_LENGTH (addr_type) == addr_size)
23404 return addr_type;
23405
23406 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23407 return addr_type;
23408 }
23409
23410 /* A helper function for dwarf2_find_containing_comp_unit that returns
23411 the index of the result, and that searches a vector. It will
23412 return a result even if the offset in question does not actually
23413 occur in any CU. This is separate so that it can be unit
23414 tested. */
23415
23416 static int
23417 dwarf2_find_containing_comp_unit
23418 (sect_offset sect_off,
23419 unsigned int offset_in_dwz,
23420 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23421 {
23422 int low, high;
23423
23424 low = 0;
23425 high = all_comp_units.size () - 1;
23426 while (high > low)
23427 {
23428 struct dwarf2_per_cu_data *mid_cu;
23429 int mid = low + (high - low) / 2;
23430
23431 mid_cu = all_comp_units[mid];
23432 if (mid_cu->is_dwz > offset_in_dwz
23433 || (mid_cu->is_dwz == offset_in_dwz
23434 && mid_cu->sect_off + mid_cu->length > sect_off))
23435 high = mid;
23436 else
23437 low = mid + 1;
23438 }
23439 gdb_assert (low == high);
23440 return low;
23441 }
23442
23443 /* Locate the .debug_info compilation unit from CU's objfile which contains
23444 the DIE at OFFSET. Raises an error on failure. */
23445
23446 static struct dwarf2_per_cu_data *
23447 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23448 unsigned int offset_in_dwz,
23449 struct dwarf2_per_objfile *dwarf2_per_objfile)
23450 {
23451 int low
23452 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23453 dwarf2_per_objfile->per_bfd->all_comp_units);
23454 struct dwarf2_per_cu_data *this_cu
23455 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23456
23457 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23458 {
23459 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23460 error (_("Dwarf Error: could not find partial DIE containing "
23461 "offset %s [in module %s]"),
23462 sect_offset_str (sect_off),
23463 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23464
23465 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23466 <= sect_off);
23467 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23468 }
23469 else
23470 {
23471 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23472 && sect_off >= this_cu->sect_off + this_cu->length)
23473 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23474 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23475 return this_cu;
23476 }
23477 }
23478
23479 #if GDB_SELF_TEST
23480
23481 namespace selftests {
23482 namespace find_containing_comp_unit {
23483
23484 static void
23485 run_test ()
23486 {
23487 struct dwarf2_per_cu_data one {};
23488 struct dwarf2_per_cu_data two {};
23489 struct dwarf2_per_cu_data three {};
23490 struct dwarf2_per_cu_data four {};
23491
23492 one.length = 5;
23493 two.sect_off = sect_offset (one.length);
23494 two.length = 7;
23495
23496 three.length = 5;
23497 three.is_dwz = 1;
23498 four.sect_off = sect_offset (three.length);
23499 four.length = 7;
23500 four.is_dwz = 1;
23501
23502 std::vector<dwarf2_per_cu_data *> units;
23503 units.push_back (&one);
23504 units.push_back (&two);
23505 units.push_back (&three);
23506 units.push_back (&four);
23507
23508 int result;
23509
23510 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23511 SELF_CHECK (units[result] == &one);
23512 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23513 SELF_CHECK (units[result] == &one);
23514 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23515 SELF_CHECK (units[result] == &two);
23516
23517 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23518 SELF_CHECK (units[result] == &three);
23519 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23520 SELF_CHECK (units[result] == &three);
23521 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23522 SELF_CHECK (units[result] == &four);
23523 }
23524
23525 }
23526 }
23527
23528 #endif /* GDB_SELF_TEST */
23529
23530 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23531
23532 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23533 dwarf2_per_objfile *per_objfile)
23534 : per_cu (per_cu),
23535 per_objfile (per_objfile),
23536 mark (false),
23537 has_loclist (false),
23538 checked_producer (false),
23539 producer_is_gxx_lt_4_6 (false),
23540 producer_is_gcc_lt_4_3 (false),
23541 producer_is_icc (false),
23542 producer_is_icc_lt_14 (false),
23543 producer_is_codewarrior (false),
23544 processing_has_namespace_info (false)
23545 {
23546 per_cu->cu = this;
23547 }
23548
23549 /* Destroy a dwarf2_cu. */
23550
23551 dwarf2_cu::~dwarf2_cu ()
23552 {
23553 per_cu->cu = NULL;
23554 }
23555
23556 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23557
23558 static void
23559 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23560 enum language pretend_language)
23561 {
23562 struct attribute *attr;
23563
23564 /* Set the language we're debugging. */
23565 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23566 if (attr != nullptr)
23567 set_cu_language (DW_UNSND (attr), cu);
23568 else
23569 {
23570 cu->language = pretend_language;
23571 cu->language_defn = language_def (cu->language);
23572 }
23573
23574 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23575 }
23576
23577 /* Increase the age counter on each cached compilation unit, and free
23578 any that are too old. */
23579
23580 static void
23581 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23582 {
23583 struct dwarf2_per_cu_data *per_cu, **last_chain;
23584
23585 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23586 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23587 while (per_cu != NULL)
23588 {
23589 per_cu->cu->last_used ++;
23590 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23591 dwarf2_mark (per_cu->cu);
23592 per_cu = per_cu->cu->read_in_chain;
23593 }
23594
23595 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23596 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23597 while (per_cu != NULL)
23598 {
23599 struct dwarf2_per_cu_data *next_cu;
23600
23601 next_cu = per_cu->cu->read_in_chain;
23602
23603 if (!per_cu->cu->mark)
23604 {
23605 delete per_cu->cu;
23606 *last_chain = next_cu;
23607 }
23608 else
23609 last_chain = &per_cu->cu->read_in_chain;
23610
23611 per_cu = next_cu;
23612 }
23613 }
23614
23615 /* Remove a single compilation unit from the cache. */
23616
23617 static void
23618 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23619 {
23620 struct dwarf2_per_cu_data *per_cu, **last_chain;
23621 struct dwarf2_per_objfile *dwarf2_per_objfile
23622 = target_per_cu->dwarf2_per_objfile;
23623
23624 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23625 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23626 while (per_cu != NULL)
23627 {
23628 struct dwarf2_per_cu_data *next_cu;
23629
23630 next_cu = per_cu->cu->read_in_chain;
23631
23632 if (per_cu == target_per_cu)
23633 {
23634 delete per_cu->cu;
23635 per_cu->cu = NULL;
23636 *last_chain = next_cu;
23637 break;
23638 }
23639 else
23640 last_chain = &per_cu->cu->read_in_chain;
23641
23642 per_cu = next_cu;
23643 }
23644 }
23645
23646 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23647 We store these in a hash table separate from the DIEs, and preserve them
23648 when the DIEs are flushed out of cache.
23649
23650 The CU "per_cu" pointer is needed because offset alone is not enough to
23651 uniquely identify the type. A file may have multiple .debug_types sections,
23652 or the type may come from a DWO file. Furthermore, while it's more logical
23653 to use per_cu->section+offset, with Fission the section with the data is in
23654 the DWO file but we don't know that section at the point we need it.
23655 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23656 because we can enter the lookup routine, get_die_type_at_offset, from
23657 outside this file, and thus won't necessarily have PER_CU->cu.
23658 Fortunately, PER_CU is stable for the life of the objfile. */
23659
23660 struct dwarf2_per_cu_offset_and_type
23661 {
23662 const struct dwarf2_per_cu_data *per_cu;
23663 sect_offset sect_off;
23664 struct type *type;
23665 };
23666
23667 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23668
23669 static hashval_t
23670 per_cu_offset_and_type_hash (const void *item)
23671 {
23672 const struct dwarf2_per_cu_offset_and_type *ofs
23673 = (const struct dwarf2_per_cu_offset_and_type *) item;
23674
23675 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23676 }
23677
23678 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23679
23680 static int
23681 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23682 {
23683 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23684 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23685 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23686 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23687
23688 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23689 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23690 }
23691
23692 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23693 table if necessary. For convenience, return TYPE.
23694
23695 The DIEs reading must have careful ordering to:
23696 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23697 reading current DIE.
23698 * Not trying to dereference contents of still incompletely read in types
23699 while reading in other DIEs.
23700 * Enable referencing still incompletely read in types just by a pointer to
23701 the type without accessing its fields.
23702
23703 Therefore caller should follow these rules:
23704 * Try to fetch any prerequisite types we may need to build this DIE type
23705 before building the type and calling set_die_type.
23706 * After building type call set_die_type for current DIE as soon as
23707 possible before fetching more types to complete the current type.
23708 * Make the type as complete as possible before fetching more types. */
23709
23710 static struct type *
23711 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23712 {
23713 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23714 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23715 struct objfile *objfile = dwarf2_per_objfile->objfile;
23716 struct attribute *attr;
23717 struct dynamic_prop prop;
23718
23719 /* For Ada types, make sure that the gnat-specific data is always
23720 initialized (if not already set). There are a few types where
23721 we should not be doing so, because the type-specific area is
23722 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23723 where the type-specific area is used to store the floatformat).
23724 But this is not a problem, because the gnat-specific information
23725 is actually not needed for these types. */
23726 if (need_gnat_info (cu)
23727 && type->code () != TYPE_CODE_FUNC
23728 && type->code () != TYPE_CODE_FLT
23729 && type->code () != TYPE_CODE_METHODPTR
23730 && type->code () != TYPE_CODE_MEMBERPTR
23731 && type->code () != TYPE_CODE_METHOD
23732 && !HAVE_GNAT_AUX_INFO (type))
23733 INIT_GNAT_SPECIFIC (type);
23734
23735 /* Read DW_AT_allocated and set in type. */
23736 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23737 if (attr != NULL && attr->form_is_block ())
23738 {
23739 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23740 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23741 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23742 }
23743 else if (attr != NULL)
23744 {
23745 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23746 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23747 sect_offset_str (die->sect_off));
23748 }
23749
23750 /* Read DW_AT_associated and set in type. */
23751 attr = dwarf2_attr (die, DW_AT_associated, cu);
23752 if (attr != NULL && attr->form_is_block ())
23753 {
23754 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23755 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23756 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23757 }
23758 else if (attr != NULL)
23759 {
23760 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23761 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23762 sect_offset_str (die->sect_off));
23763 }
23764
23765 /* Read DW_AT_data_location and set in type. */
23766 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23767 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23768 cu->per_cu->addr_type ()))
23769 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23770
23771 if (dwarf2_per_objfile->die_type_hash == NULL)
23772 dwarf2_per_objfile->die_type_hash
23773 = htab_up (htab_create_alloc (127,
23774 per_cu_offset_and_type_hash,
23775 per_cu_offset_and_type_eq,
23776 NULL, xcalloc, xfree));
23777
23778 ofs.per_cu = cu->per_cu;
23779 ofs.sect_off = die->sect_off;
23780 ofs.type = type;
23781 slot = (struct dwarf2_per_cu_offset_and_type **)
23782 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23783 if (*slot)
23784 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23785 sect_offset_str (die->sect_off));
23786 *slot = XOBNEW (&objfile->objfile_obstack,
23787 struct dwarf2_per_cu_offset_and_type);
23788 **slot = ofs;
23789 return type;
23790 }
23791
23792 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23793 or return NULL if the die does not have a saved type. */
23794
23795 static struct type *
23796 get_die_type_at_offset (sect_offset sect_off,
23797 struct dwarf2_per_cu_data *per_cu)
23798 {
23799 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23800 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23801
23802 if (dwarf2_per_objfile->die_type_hash == NULL)
23803 return NULL;
23804
23805 ofs.per_cu = per_cu;
23806 ofs.sect_off = sect_off;
23807 slot = ((struct dwarf2_per_cu_offset_and_type *)
23808 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23809 if (slot)
23810 return slot->type;
23811 else
23812 return NULL;
23813 }
23814
23815 /* Look up the type for DIE in CU in die_type_hash,
23816 or return NULL if DIE does not have a saved type. */
23817
23818 static struct type *
23819 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23820 {
23821 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23822 }
23823
23824 /* Add a dependence relationship from CU to REF_PER_CU. */
23825
23826 static void
23827 dwarf2_add_dependence (struct dwarf2_cu *cu,
23828 struct dwarf2_per_cu_data *ref_per_cu)
23829 {
23830 void **slot;
23831
23832 if (cu->dependencies == NULL)
23833 cu->dependencies
23834 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23835 NULL, &cu->comp_unit_obstack,
23836 hashtab_obstack_allocate,
23837 dummy_obstack_deallocate);
23838
23839 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23840 if (*slot == NULL)
23841 *slot = ref_per_cu;
23842 }
23843
23844 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23845 Set the mark field in every compilation unit in the
23846 cache that we must keep because we are keeping CU. */
23847
23848 static int
23849 dwarf2_mark_helper (void **slot, void *data)
23850 {
23851 struct dwarf2_per_cu_data *per_cu;
23852
23853 per_cu = (struct dwarf2_per_cu_data *) *slot;
23854
23855 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23856 reading of the chain. As such dependencies remain valid it is not much
23857 useful to track and undo them during QUIT cleanups. */
23858 if (per_cu->cu == NULL)
23859 return 1;
23860
23861 if (per_cu->cu->mark)
23862 return 1;
23863 per_cu->cu->mark = true;
23864
23865 if (per_cu->cu->dependencies != NULL)
23866 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23867
23868 return 1;
23869 }
23870
23871 /* Set the mark field in CU and in every other compilation unit in the
23872 cache that we must keep because we are keeping CU. */
23873
23874 static void
23875 dwarf2_mark (struct dwarf2_cu *cu)
23876 {
23877 if (cu->mark)
23878 return;
23879 cu->mark = true;
23880 if (cu->dependencies != NULL)
23881 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23882 }
23883
23884 static void
23885 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23886 {
23887 while (per_cu)
23888 {
23889 per_cu->cu->mark = false;
23890 per_cu = per_cu->cu->read_in_chain;
23891 }
23892 }
23893
23894 /* Trivial hash function for partial_die_info: the hash value of a DIE
23895 is its offset in .debug_info for this objfile. */
23896
23897 static hashval_t
23898 partial_die_hash (const void *item)
23899 {
23900 const struct partial_die_info *part_die
23901 = (const struct partial_die_info *) item;
23902
23903 return to_underlying (part_die->sect_off);
23904 }
23905
23906 /* Trivial comparison function for partial_die_info structures: two DIEs
23907 are equal if they have the same offset. */
23908
23909 static int
23910 partial_die_eq (const void *item_lhs, const void *item_rhs)
23911 {
23912 const struct partial_die_info *part_die_lhs
23913 = (const struct partial_die_info *) item_lhs;
23914 const struct partial_die_info *part_die_rhs
23915 = (const struct partial_die_info *) item_rhs;
23916
23917 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23918 }
23919
23920 struct cmd_list_element *set_dwarf_cmdlist;
23921 struct cmd_list_element *show_dwarf_cmdlist;
23922
23923 static void
23924 show_check_physname (struct ui_file *file, int from_tty,
23925 struct cmd_list_element *c, const char *value)
23926 {
23927 fprintf_filtered (file,
23928 _("Whether to check \"physname\" is %s.\n"),
23929 value);
23930 }
23931
23932 void _initialize_dwarf2_read ();
23933 void
23934 _initialize_dwarf2_read ()
23935 {
23936 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23937 Set DWARF specific variables.\n\
23938 Configure DWARF variables such as the cache size."),
23939 &set_dwarf_cmdlist, "maintenance set dwarf ",
23940 0/*allow-unknown*/, &maintenance_set_cmdlist);
23941
23942 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23943 Show DWARF specific variables.\n\
23944 Show DWARF variables such as the cache size."),
23945 &show_dwarf_cmdlist, "maintenance show dwarf ",
23946 0/*allow-unknown*/, &maintenance_show_cmdlist);
23947
23948 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23949 &dwarf_max_cache_age, _("\
23950 Set the upper bound on the age of cached DWARF compilation units."), _("\
23951 Show the upper bound on the age of cached DWARF compilation units."), _("\
23952 A higher limit means that cached compilation units will be stored\n\
23953 in memory longer, and more total memory will be used. Zero disables\n\
23954 caching, which can slow down startup."),
23955 NULL,
23956 show_dwarf_max_cache_age,
23957 &set_dwarf_cmdlist,
23958 &show_dwarf_cmdlist);
23959
23960 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23961 Set debugging of the DWARF reader."), _("\
23962 Show debugging of the DWARF reader."), _("\
23963 When enabled (non-zero), debugging messages are printed during DWARF\n\
23964 reading and symtab expansion. A value of 1 (one) provides basic\n\
23965 information. A value greater than 1 provides more verbose information."),
23966 NULL,
23967 NULL,
23968 &setdebuglist, &showdebuglist);
23969
23970 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23971 Set debugging of the DWARF DIE reader."), _("\
23972 Show debugging of the DWARF DIE reader."), _("\
23973 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23974 The value is the maximum depth to print."),
23975 NULL,
23976 NULL,
23977 &setdebuglist, &showdebuglist);
23978
23979 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23980 Set debugging of the dwarf line reader."), _("\
23981 Show debugging of the dwarf line reader."), _("\
23982 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23983 A value of 1 (one) provides basic information.\n\
23984 A value greater than 1 provides more verbose information."),
23985 NULL,
23986 NULL,
23987 &setdebuglist, &showdebuglist);
23988
23989 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23990 Set cross-checking of \"physname\" code against demangler."), _("\
23991 Show cross-checking of \"physname\" code against demangler."), _("\
23992 When enabled, GDB's internal \"physname\" code is checked against\n\
23993 the demangler."),
23994 NULL, show_check_physname,
23995 &setdebuglist, &showdebuglist);
23996
23997 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23998 no_class, &use_deprecated_index_sections, _("\
23999 Set whether to use deprecated gdb_index sections."), _("\
24000 Show whether to use deprecated gdb_index sections."), _("\
24001 When enabled, deprecated .gdb_index sections are used anyway.\n\
24002 Normally they are ignored either because of a missing feature or\n\
24003 performance issue.\n\
24004 Warning: This option must be enabled before gdb reads the file."),
24005 NULL,
24006 NULL,
24007 &setlist, &showlist);
24008
24009 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24010 &dwarf2_locexpr_funcs);
24011 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24012 &dwarf2_loclist_funcs);
24013
24014 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24015 &dwarf2_block_frame_base_locexpr_funcs);
24016 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24017 &dwarf2_block_frame_base_loclist_funcs);
24018
24019 #if GDB_SELF_TEST
24020 selftests::register_test ("dw2_expand_symtabs_matching",
24021 selftests::dw2_expand_symtabs_matching::run_test);
24022 selftests::register_test ("dwarf2_find_containing_comp_unit",
24023 selftests::find_containing_comp_unit::run_test);
24024 #endif
24025 }
This page took 0.562241 seconds and 4 git commands to generate.